Random 单位随机数发生器
我试图在Verilog中的每个时钟周期上生成一位随机数,用于测试目的。我在一个测试台模块中做这件事。这是代码Random 单位随机数发生器,random,verilog,test-bench,Random,Verilog,Test Bench,我试图在Verilog中的每个时钟周期上生成一位随机数,用于测试目的。我在一个测试台模块中做这件事。这是代码 module tb; // Inputs reg clk; reg in; // Outputs wire x; // Instantiate the Unit Under Test (UUT) single_bit uut ( .clk(clk), .in(in), .x(x) ); integer seed=1; initial begin
module tb;
// Inputs
reg clk;
reg in;
// Outputs
wire x;
// Instantiate the Unit Under Test (UUT)
single_bit uut (
.clk(clk),
.in(in),
.x(x)
);
integer seed=1;
initial
begin
clk=0;
forever
#5 clk=!clk;
in= $random(seed);
end
endmodule
但是在模拟之后,我得到了种子中的常量值1,并且中的有x。
非常感谢您的帮助。您的缩进有误导性。下面是代码的解析方式:
initial begin
clk=0;
forever #5 clk=!clk;
in= $random(seed);
end
您的代码永远不会到达中的赋值,因为您永远不会到达循环的末尾。由于您将
中的声明为reg
,并且从未为
中的赋值,因此它保留默认值x
对
中的赋值应该在永久
循环中。这将在每个时钟边缘为
中的生成0和1的随机流:
initial begin
clk=0;
forever begin
#5 clk=!clk;
in = $random(seed);
end
end