4 对 1 多路复用器,在 Verilog 上实现加法、反转、与、或门

4-to-1 Multiplexer that implements addition, inversion, AND, OR gates on Verilog

我这学期刚开始学习 Verilog,我只是被困在创建一个 Verilog 模块的任务上,该模块使用多路复用对 2 个 8 位输入执行不同的操作。下面是我写的 Verilog 代码,我遇到了几个我不明白的错误。请帮忙!

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

两大问题:

首先,使用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 和 non-ANSI 样式 headers 我在端口列表中声明 fovf 作为连线,然后在其中声明单个位 reg。选择一种语法:

  • ANSI:(注意输出<b>reg</b>

    module eightbit_palu( input[7:0] a, input[7:0] b, 
      input[1:0] sel, output reg [7:0] f, output reg ovf );
    
  • Non-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 与 synthesized-gates 行为不匹配,并且是 Verilog 中的首选编码风格。只有在严格按照 1995 版标准时才需要手动定义灵敏度。
  • 您可以将条件 1、2 和 3 简化为按位运算:(例如 1 : f = ~b;2 : f = a & b;3 : f = a | b;)。 For-loops是另一种选择

  • ovf 是推断锁存器。闩锁不一定是坏的,但你需要知道你在用它们做什么。建议您仅在必要时使用 then。 What is inferred latch and how it is created when it is missing else statement in if condition.can anybody explain briefly?