我正在研究使用结构verilog的纹波进位加法器,它应该接收两个随机输入并相应地进行计算。
我创建的一般rca计算正确,但由于某种原因,当我添加for循环并使用$random
生成时,我会得到奇怪的输出。
有人可以解释我哪里出错吗?以下是我的代码:
module full_adder(x,y,z,v,cout);
parameter delay = 1;
input x,y,z; //input a, b and c
output v,cout; //sum and carry out
xor #delay x1(w1,x,y);
xor #delay x2(v,w1,z);
and #delay a1(w2,z,y);
and #delay a2(w3,z,x);
and #delay a3(w4,x,y);
or #delay o1(cout, w2,w3,w4);
endmodule
module four_bit_adder(a,b,s,cout,cin);//four_bit_adder
input [15:0] a,b; //input a, b
input cin; //carry in
output [15:0] s; //output s
output cout; //carry out
wire [15:0] c;
full_adder fa1(a[0],b[0],cin,s[0],c0);
full_adder fa2(a[1],b[1],c0,s[1],c1);
.
.
.
full_adder fa16(a[15],b[15],c14,s[15],cout);
endmodule
module testAdder(a,b,s,cout,cin);
input [15:0] s;
input cout;
output [15:0] a,b;
output cin;
reg [15:0] a,b;
reg cin;
integer i;
integer seed1=4;
integer seed2=5;
initial begin
for(i=0; i<5000; i=i+1) begin
a = $random(seed1);
b = $random(seed2);
$monitor("a=%d, b=%d, cin=%d, s=%d, cout=%d",a,b,cin,s,cout);
$display("a=%d, b=%d, cin=%d, s=%d, cout=%d",a,b,cin,s,cout);
end
end
endmodule
以下是我得到的输出中的两行:
a=38893, b=58591, cin=x, s= z, cout=z
a=55136, b=58098, cin=x, s= z, cout=z
答案 0 :(得分:2)
这是一个组合电路,因此输入随着输入的变化而变化瞬间。但是,在这里您应用相同时间戳的所有输入,这应该不,因为full_adder
模块提供 1时间戳延迟。这可能不会导致此模块出现问题,但在建模顺序逻辑时可能会导致问题。在输入之间添加最少#10
个延迟。
此外,$monitor
会在信号列表中的每次更改上执行,因此无需在for
循环中使用它。只需在$monitor
条件下初始化initial
。
cin
也不是来自测试平台。 reg
的默认值为'x
,wire
的默认值为'z
。此处cin
为reg
,因此会显示默认值,即'x
还有一点,您必须实例化您的测试平台中的设计。并连接各自的端口。 testbench的输出作为设计的输入,反之亦然。这就像您在设计中full_adder
模块中实例化four_bit_adder
模块一样。
将testadder
视为顶级模块并在其中实例化设计。 否需要在此模块中声明端口作为输入和输出。将设计输入端口声明为reg
或wire
(示例:reg [15:0] a
当a
是设计输入端口时)和输出端口< / strong>为wire
(示例:wire [15:0] sum
当sum
为设计输入端口时)。
参考你的问题:
我创建的一般rca计算正确,但由于某种原因,当我添加for循环并使用$ random生成时,我得到奇怪的输出。
不使用$random
,而是使用$urandom_range()
生成某些范围中的随机数。使用SystemVerilog约束构造也可以提供帮助。请参阅this链接。
使用$urandom_range
将取消seed1
和seed2
的使用,它将使用一些随机机器种子生成随机值。
以下是模块testadder,需要进行一些更改:
module testAdder();
wire [15:0] s;
wire cout;
// output [15:0] a,b;
// output cin;
reg [15:0] a,b;
reg cin;
integer i;
integer seed1=4;
integer seed2=5;
// Instantiate design here
four_bit_adder fa(a,b,s,cout,cin);
initial begin
// Monitor here, only single time
$monitor("a=%d, b=%d, cin=%d, s=%d, cout=%d",a,b,cin,s,cout);
for(i=0; i<5000; i=i+1) begin
// Drive inputs with some delays.
#10;
// URANDOM_RANGE for input generation in a range
a = $urandom_range(0,15);
b = $urandom_range(0,15);
// a = $random(seed1);
// b = $random(seed2);
// Drive cin randomly.
cin = $random;
$display("a=%d, b=%d, cin=%d, s=%d, cout=%d",a,b,cin,s,cout);
end
end
endmodule
有关详细信息,请查看示例测试平台at this link。