iverilog 错误可能源于不正确的变量类型
iverilog errors likely stemming from incorrect variable types
我是 verilog 编程的新手,正在致力于使用 2 个 4 位比较器实现一个 8 位无符号幅度比较器。我相信我的代码实现正确,但是我收到错误,我认为这是由于不正确的变量类型分配造成的。由于我是该语言的新手,我认为这是一个学习机会,但是我找不到足够的相关 material 来引导我找到解决方案。如果有人可以解释为什么我使用的类型不正确(或者如果这是我面临的不同问题),我们将不胜感激。
编辑:我改变了我对那个建议的回答,在 always 块之外进行模块实例化,并连接为 eq、gt 和 lt,但仍然出现错误。更新了错误代码。
module MagComp4Bit (input [3:0] a, input [3:0] b, output eq, output gt, output lt);
assign eq = a==b;
assign gt = a>b;
assign lt = a<b;
endmodule
module MagComp8Bit (input [7:0] a, input [7:0] b, output eq, output gt, output lt);
reg eq0, gt0, lt0, eq1, gt1, lt1;
MagComp4Bit comp1(a[3:0], b[3:0], eq0, gt0, lt0);
MagComp4Bit comp2(a[7:4], b[7:4], eq1, gt1, lt1);
always @(a, b)
begin
if (eq1) begin
eq = eq0? 1 : 0;
gt = gt0? 1 : 0;
lt = lt0? 1 : 0;
end
else begin
gt = gt1? 1 : 0;
lt = lt1? 1 : 0;
end
end
endmodule
module TestComparator;
reg[7:0] a, b;
wire eq, gt, lt;
MagComp8Bit compare(a, b, eq, gt, lt);
initial begin
$moniter("%d a=%b, b=%b, eq=%b, gt=%b, lt=%b",
$time, a, b, eq, gt, lt);
#10 a = 2;
b = 5;
end
endmodule
错误信息:
hw1p1.v:13: error: reg eq0; cannot be driven by primitives or continuous
assignment.
hw1p1.v:13: error: Output port expression must support continuous
assignment.
hw1p1.v:13: : Port 3 (eq) of MagComp4Bit is connected to eq0
hw1p1.v:13: error: reg gt0; cannot be driven by primitives or continuous
assignment.
hw1p1.v:13: error: Output port expression must support continuous
assignment.
hw1p1.v:13: : Port 4 (gt) of MagComp4Bit is connected to gt0
hw1p1.v:13: error: reg lt0; cannot be driven by primitives or continuous
assignment.
hw1p1.v:13: error: Output port expression must support continuous
assignment.
hw1p1.v:13: : Port 5 (lt) of MagComp4Bit is connected to lt0
hw1p1.v:14: error: reg eq1; cannot be driven by primitives or continuous
assignment.
hw1p1.v:14: error: Output port expression must support continuous
assignment.
hw1p1.v:14: : Port 3 (eq) of MagComp4Bit is connected to eq1
hw1p1.v:14: error: reg gt1; cannot be driven by primitives or continuous
assignment.
hw1p1.v:14: error: Output port expression must support continuous
assignment.
hw1p1.v:14: : Port 4 (gt) of MagComp4Bit is connected to gt1
hw1p1.v:14: error: reg lt1; cannot be driven by primitives or continuous
assignment.
hw1p1.v:14: error: Output port expression must support continuous
assignment.
hw1p1.v:14: : Port 5 (lt) of MagComp4Bit is connected to lt1
hw1p1.v:22: error: eq is not a valid l-value in TestComparator.compare.
hw1p1.v:9: : eq is declared here as wire.
hw1p1.v:23: error: gt is not a valid l-value in TestComparator.compare.
hw1p1.v:9: : gt is declared here as wire.
hw1p1.v:24: error: lt is not a valid l-value in TestComparator.compare.
hw1p1.v:9: : lt is declared here as wire.
hw1p1.v:27: error: gt is not a valid l-value in TestComparator.compare.
hw1p1.v:9: : gt is declared here as wire.
hw1p1.v:28: error: lt is not a valid l-value in TestComparator.compare.
hw1p1.v:9: : lt is declared here as wire.
17 error(s) during elaboration.
(P.S。我知道将测试台与其他模块一起包含是不合适的,但是当我一次看到所有内容时,我更容易学习。)
Verilog 模块不打算在 initial 或 always 块内实例化。这就是你应该搬家的原因:
MagComp4Bit(a[3:0], b[3:0], eq0, gt0, lt0);
MagComp4Bit(a[7:4], b[7:4], eq1, gt1, lt1);
在 always
街区外。此外,eq, gt, lt
应该在您的 TestComparator 模块中声明为连线。
wire
类型不能在任何程序块中分配(始终、初始、..)。您正在 always
内部执行此操作
always...
..
eq = eq0? 1 : 0;
其中eq
定义为没有任何数据类型的端口,默认为wire
。与其他 2 个端口相同:lt
和 gt
.
您需要稍微更改一下代码:
reg eqReg, ltReg, wrReg;
always @(a, b)
begin
if (eq1) begin
eqReg = eq0? 1 : 0;
gtReg = gt0? 1 : 0;
ltReg = lt0? 1 : 0;
end
else begin
gtReg = gt1? 1 : 0;
ltReg = lt1? 1 : 0;
end
end
assign eq = eqReg;
assign lt = ltReg;
assign gt = gtReg;
我是 verilog 编程的新手,正在致力于使用 2 个 4 位比较器实现一个 8 位无符号幅度比较器。我相信我的代码实现正确,但是我收到错误,我认为这是由于不正确的变量类型分配造成的。由于我是该语言的新手,我认为这是一个学习机会,但是我找不到足够的相关 material 来引导我找到解决方案。如果有人可以解释为什么我使用的类型不正确(或者如果这是我面临的不同问题),我们将不胜感激。
编辑:我改变了我对那个建议的回答,在 always 块之外进行模块实例化,并连接为 eq、gt 和 lt,但仍然出现错误。更新了错误代码。
module MagComp4Bit (input [3:0] a, input [3:0] b, output eq, output gt, output lt);
assign eq = a==b;
assign gt = a>b;
assign lt = a<b;
endmodule
module MagComp8Bit (input [7:0] a, input [7:0] b, output eq, output gt, output lt);
reg eq0, gt0, lt0, eq1, gt1, lt1;
MagComp4Bit comp1(a[3:0], b[3:0], eq0, gt0, lt0);
MagComp4Bit comp2(a[7:4], b[7:4], eq1, gt1, lt1);
always @(a, b)
begin
if (eq1) begin
eq = eq0? 1 : 0;
gt = gt0? 1 : 0;
lt = lt0? 1 : 0;
end
else begin
gt = gt1? 1 : 0;
lt = lt1? 1 : 0;
end
end
endmodule
module TestComparator;
reg[7:0] a, b;
wire eq, gt, lt;
MagComp8Bit compare(a, b, eq, gt, lt);
initial begin
$moniter("%d a=%b, b=%b, eq=%b, gt=%b, lt=%b",
$time, a, b, eq, gt, lt);
#10 a = 2;
b = 5;
end
endmodule
错误信息:
hw1p1.v:13: error: reg eq0; cannot be driven by primitives or continuous
assignment.
hw1p1.v:13: error: Output port expression must support continuous
assignment.
hw1p1.v:13: : Port 3 (eq) of MagComp4Bit is connected to eq0
hw1p1.v:13: error: reg gt0; cannot be driven by primitives or continuous
assignment.
hw1p1.v:13: error: Output port expression must support continuous
assignment.
hw1p1.v:13: : Port 4 (gt) of MagComp4Bit is connected to gt0
hw1p1.v:13: error: reg lt0; cannot be driven by primitives or continuous
assignment.
hw1p1.v:13: error: Output port expression must support continuous
assignment.
hw1p1.v:13: : Port 5 (lt) of MagComp4Bit is connected to lt0
hw1p1.v:14: error: reg eq1; cannot be driven by primitives or continuous
assignment.
hw1p1.v:14: error: Output port expression must support continuous
assignment.
hw1p1.v:14: : Port 3 (eq) of MagComp4Bit is connected to eq1
hw1p1.v:14: error: reg gt1; cannot be driven by primitives or continuous
assignment.
hw1p1.v:14: error: Output port expression must support continuous
assignment.
hw1p1.v:14: : Port 4 (gt) of MagComp4Bit is connected to gt1
hw1p1.v:14: error: reg lt1; cannot be driven by primitives or continuous
assignment.
hw1p1.v:14: error: Output port expression must support continuous
assignment.
hw1p1.v:14: : Port 5 (lt) of MagComp4Bit is connected to lt1
hw1p1.v:22: error: eq is not a valid l-value in TestComparator.compare.
hw1p1.v:9: : eq is declared here as wire.
hw1p1.v:23: error: gt is not a valid l-value in TestComparator.compare.
hw1p1.v:9: : gt is declared here as wire.
hw1p1.v:24: error: lt is not a valid l-value in TestComparator.compare.
hw1p1.v:9: : lt is declared here as wire.
hw1p1.v:27: error: gt is not a valid l-value in TestComparator.compare.
hw1p1.v:9: : gt is declared here as wire.
hw1p1.v:28: error: lt is not a valid l-value in TestComparator.compare.
hw1p1.v:9: : lt is declared here as wire.
17 error(s) during elaboration.
(P.S。我知道将测试台与其他模块一起包含是不合适的,但是当我一次看到所有内容时,我更容易学习。)
Verilog 模块不打算在 initial 或 always 块内实例化。这就是你应该搬家的原因:
MagComp4Bit(a[3:0], b[3:0], eq0, gt0, lt0);
MagComp4Bit(a[7:4], b[7:4], eq1, gt1, lt1);
在 always
街区外。此外,eq, gt, lt
应该在您的 TestComparator 模块中声明为连线。
wire
类型不能在任何程序块中分配(始终、初始、..)。您正在 always
always...
..
eq = eq0? 1 : 0;
其中eq
定义为没有任何数据类型的端口,默认为wire
。与其他 2 个端口相同:lt
和 gt
.
您需要稍微更改一下代码:
reg eqReg, ltReg, wrReg;
always @(a, b)
begin
if (eq1) begin
eqReg = eq0? 1 : 0;
gtReg = gt0? 1 : 0;
ltReg = lt0? 1 : 0;
end
else begin
gtReg = gt1? 1 : 0;
ltReg = lt1? 1 : 0;
end
end
assign eq = eqReg;
assign lt = ltReg;
assign gt = gtReg;