You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Verilog实现4:1多路复用器实现指定函数时输出异常排查

问题分析与修复方案

1. 端口方向错误(导致仿真输出Z)

fn_using_mux模块中,X是函数F的输入变量,却被错误定义为output,导致测试平台的tx(reg类型)无法正确驱动该端口,进而使多路复用器的输入信号未被有效赋值,输出始终为Z。

修复:
修改fn_using_mux的端口定义,将X改为input:

module fn_using_mux(Y, Z, X, F);
input Y, Z, X;  // X改为input
output F;
mux4to1 h2 (Y, Z, 1'b0, X, ~X, 1'b1);  // 常量显式标注位宽,避免隐式类型问题
endmodule

2. 多路复用器选择逻辑错误

mux4to1模块中,w3的与门逻辑错误:原代码用z, z代替了y, z,导致选择信号YZ的逻辑失效,无法正确选中I3。

修复:
修正w3的与门输入:

and (w3, y, z, I3);  // 替换原有的and (w3, z, z, I3);

3. 测试平台未赋值输入变量X

原测试平台中仅对ty、tz赋值,tx始终为默认的未知态x,导致仿真无法覆盖所有输入组合,结果不准确。

修复:
扩展测试平台,覆盖X、Y、Z的所有8种组合,并显式赋值tx:

module mux_test;
reg tx, ty, tz;
wire tf;
fn_using_mux m1 (ty, tz, tx, tf);

initial begin
$monitor("X=%b, Y=%b, Z=%b, F=%b", tx, ty, tz, tf);
// 遍历所有8种输入组合
tx = 0; ty = 0; tz = 0; #100;
tx = 0; ty = 0; tz = 1; #100;
tx = 0; ty = 1; tz = 0; #100;
tx = 0; ty = 1; tz = 1; #100;
tx = 1; ty = 0; tz = 0; #100;
tx = 1; ty = 0; tz = 1; #100;
tx = 1; ty = 1; tz = 0; #100;
tx = 1; ty = 1; tz = 1; #100;
end

initial begin
$dumpfile("dump.vcd");
$dumpvars(1);
end
endmodule

4. 关于“concurrent assignment to a non-net 'tx' is not permitted”错误

该错误是由于尝试用连续赋值(assign语句)给reg类型的tx赋值导致的。reg变量只能在initial或always过程块内赋值,不能用assign直接驱动。确保所有对tx的赋值都放在过程块内即可避免此错误。

完整修正后代码

多路复用器及函数实现模块

module mux4to1 (y, z, I0, I1, I2, I3, f);
input y, z, I0, I1, I2, I3;
output f;
wire yb, zb, w0, w1, w2, w3;
not (yb, y);
not (zb, z);
and (w0, yb, zb, I0);
and (w1, yb, z, I1);
and (w2, y, zb, I2);
and (w3, y, z, I3);  // 修正选择信号逻辑
or (f, w0, w1, w2, w3);
endmodule

module fn_using_mux(Y, Z, X, F);
input Y, Z, X;
output F;
mux4to1 h2 (Y, Z, 1'b0, X, ~X, 1'b1);
endmodule

测试平台

module mux_test;
reg tx, ty, tz;
wire tf;
fn_using_mux m1 (ty, tz, tx, tf);

initial begin
$monitor("X=%b, Y=%b, Z=%b, F=%b", tx, ty, tz, tf);
tx = 0; ty = 0; tz = 0; #100;
tx = 0; ty = 0; tz = 1; #100;
tx = 0; ty = 1; tz = 0; #100;
tx = 0; ty = 1; tz = 1; #100;
tx = 1; ty = 0; tz = 0; #100;
tx = 1; ty = 0; tz = 1; #100;
tx = 1; ty = 1; tz = 0; #100;
tx = 1; ty = 1; tz = 1; #100;
end

initial begin
$dumpfile("dump.vcd");
$dumpvars(1);
end
endmodule

仿真验证

修正后仿真将输出所有输入组合对应的F值,与函数F(X,Y,Z) = XY' + Y'Z' + X'Z'的真值表完全一致:

XYZF
0001
0010
0101
0110
1001
1011
1100
1111

内容的提问来源于stack exchange,提问作者Ahooey

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.13 09:38:08