2016-01-29 100 views
0

我剛剛開始學習Verilog這個學期,並且我剛剛被困在創建Verilog模塊的任務上,該模塊使用複用來執行不同的操作2個8位輸入。下面是我寫的Verilog代碼,我收到了幾個我不明白的錯誤。請幫忙!在Verilog上實現加法,反轉,AND和或門的4對1複用器

module eightbit_palu(input[7:0] a, input[7:0] b, input[1:0] sel, output[7:0] f, output ovf); 

reg f, ovf; 
    always @ (a , b, sel) 

    case (sel) 
     0 : f = a + b; 
      ovf = f[8]^f[7]; 

     1 : f[0] = ~b[0]; 
      f[1] = ~b[1]; 
      f[2] = ~b[2]; 
      f[3] = ~b[3]; 
      f[4] = ~b[4]; 
      f[5] = ~b[5]; 
      f[6] = ~b[6]; 
      f[7] = ~b[7]; 

     2 : f[0] = a[0]&b[0]; f[1] = a[1]&b[1]; f[2] = a[2]&b[2]; f[3] = a[3]&b[3]; f[4] = a[4]&b[4]; 
      f[5] = a[5]&b[5]; f[6] = a[6]&b[6]; f[7] = a[7]&b[7]; 

     3 : f[0] = a[0]|b[0]; f[1] = a[1]|b[1]; f[2] = a[2]|b[2]; f[3] = a[3]|b[3]; f[4] = a[4]|b[4]; 
      f[5] = a[5]|b[5]; f[6] = a[6]|b[6]; f[7] = a[7]|b[7]; 
    endcase 

endmodule 

由模擬器所顯示的錯誤是:

8: syntax error
10: error: Incomprehensible case expression.
11: syntax error
19: error: Incomprehensible case expression.
19: syntax error
22: error: Incomprehensible case expression.
22: syntax error

+0

請提供錯誤。 – ToothlessRebel

+0

終端輸出 8:語法錯誤 10:錯誤:難以理解的大小寫表達式。 11:語法錯誤 19:錯誤:難以理解的大小寫表達式。 19:語法錯誤 22:錯誤:難以理解的大小寫表達式。 22:語法錯誤 – KishTheMagnanimous

+0

請編輯您的問題以包含此內容。我不知道Verilog,但你得到的錯誤信息是相當清楚的。語法錯誤意味着代碼寫入不正確,請查看您正在做什麼並查看是否可以發現差異。不可理解的情況下的表達意味着Verilog期望你的代碼是不正確的。我打賭在冒號前刪除空格,'1:'變成'1:'。 – ToothlessRebel

回答

2

兩個主要問題:

首先,使用Verilog,一系列程序語句必須由begin包圍 - end關鍵字

always @ (*) begin 
    case (sel) 
     0 : begin 
       f = a + b; 
       ovf = f[8]^f[7]; 
      end 

     1 : begin 
      f[0] = ~b[0]; 
      ... 
      end 

     ... 
    endcase 
end 

二,喲你在混合ANSI和非ANSI風格的頭文件,我聲明fovf作爲portlist中的連線,然後是單個位內部的reg。選擇一個語法:

  • ANSI:(注意output reg

    module eightbit_palu(input[7:0] a, input[7:0] b, 
        input[1:0] sel, output reg [7:0] f, output reg ovf); 
    
  • 非ANSI:

    module eightbit_palu(a, b, sel, f, ovf); 
        input[7:0] a; 
        input[7:0] b; 
        input[1:0] sel; 
        output [7:0] f; 
        output ovf; 
        reg [7:0] f; 
        reg ovf; 
    

改進建議:

  • always @ (a , b, sel)always @*

    • 自2001年以來,Verilog的支持組合邏輯塊的外卡靈敏度列表。這有助於防止代理RTL與合成門行爲不匹配,並且是Verilog中的首選編碼風格。只有嚴格遵守1995版標準時才需要手動定義靈敏度。
  • 可以simplfiy條件1,2,和3位運算:(如:1 : f = ~b;2 : f = a & b;3 : f = a | b;)。 For-loops是另一個選項

  • ovf是推斷的鎖存器。鎖存並不是必須的,但你需要知道你在做什麼。建議您僅在必要時使用。 What is inferred latch and how it is created when it is missing else statement in if condition.can anybody explain briefly?

+0

非常感謝Greg! @格雷格 – KishTheMagnanimous