如何在 verilog 中将启用端口连接到 4x1 MUX?

How to connect enable port to 4x1 MUX in verilog?

我正在尝试在 Verilog 中实现 4x1 多路复用器。我想连接启用 (en) 作为输入“1”(高)可以打开 MUX 和“0”(低)关闭此多路复用器的端口。 请在我的代码中提出一些修改建议。 提前致谢。

module mux_4_to_1(
    input d,c,b,a,      //Inputs
    input s1,s0,               //Select Lines
    input en,                  //enable
    output reg y                   //output
    );

always @ (*)
begin
    case (s0 | s1)
    2'b00 : y <= a;
    2'b01 : y <= b;
    2'b10 : y <= c;
    2'b11 : y <= d;
    default : y <= 0;
    endcase
end
endmodule

您希望 en 作为全局开关来打开或关闭多路复用器,因此它获得最高优先级。

always @ (*)
begin
    if (en) begin
        case ({s0, s1})  // pay attention here
        2'b00 : y = a;
        2'b01 : y = b;
        2'b10 : y = c;
        2'b11 : y = d;
        default : y = 0;
        endcase
    end
    else begin
        y = 1'b0;  // your question didn't specify the expected output when OFF
    end
end

请注意我已将 case (s0 | s1) 更改为 case ({s0, s1})
s0 | s1 returns 1 位宽的结果,而您需要 s0s1.

的串联

此外,我将所有NBA <= 替换为BA =

而且,default 分支实际上不可能在硅片中发生,因为您已经指定了所有可能的组合。但是如果你觉得它可能对模拟有帮助,你可以离开它。