我无法理解
generate
块内的模块是如何实例化的。
我正在尝试实现一个检测 1010 的序列检测器。为此,我正在尝试使用 2 D 触发器。求解序列的状态表后,我得到
D1 = (Q2&~X | Q1&~Q2&X), D2 = X, Z = Q1&Q2&~X,其中 X 是我的输入,Z 是所需的输出。
用Verilog实现,目前我写的代码是:
module d_flipflop(d, clk, q);
input d, clk;
output reg q;
always @ (posedge clk)
q <= d;
endmodule
module seq_detector (clk, inp, outp);
input [9:0] inp;
input clk;
output reg [9:0] outp;
wire d1 = 0;
wire q1 = inp[9], q2 = inp[8];
generate
genvar i;
for (i=9; i>=0; i=i-1) begin
d_flipflop ff2 (.d(inp[i]), .clk(clk), .q(q2) ); // X <=> inp[i]
assign d1 = (q2 & ~inp[i]) | (q1 & ~q2 & inp[i] );
d_flipflop ff1 (.d(d1), .clk(clk), .q(q1) );
assign outp[i] = q1 & q2 & ~inp[i];
end
endgenerate
endmodule
module TB_seq_detector();
reg [9:0] inp, clk;
wire [9:0] outp;
seq_detector DUT (.clk(clk), .inp(inp), .outp(outp) );
// Generating clock:
always #10 clk = ~clk;
// Initializing clk:
initial clk=0;
// Test case:
initial begin
inp = 10'b1000101011;
end
initial begin
$display("input: %b", inp[9:0]);
$display("output: %b", outp[9:0]);
end
initial #100 $finish;
endmodule
我知道 q1 和 q2 实际上并未被分配给 inp[9] 和 inp[8] (它们在时间 = 0 时为 x)。我该如何解决这个问题?我应该使用非阻塞赋值吗?另外,据我所知,生成块在模拟时间之前运行。所以我的clk也没有影响任何东西。我该如何解决这个问题?
您的代码存在问题,需要修复。
如果您的模拟器没有向您显示类似于以下内容的警告,那么您应该在 EDAPlayground 上的模拟器上运行您的代码:
Warning-[PCWM-W] Port connection width mismatch
"seq_detector DUT( .clk (clk), .inp (inp), .outp (outp));"
The following 10-bit expression is connected to 1-bit port "clk" of module
"seq_detector", instance "DUT".
Expression: clk
以下行将
clk
声明为 10 位信号:
reg [9:0] inp, clk;
您可能希望
clk
成为 1 位信号:
reg clk;
您有多个驱动程序用于
q1
信号:
ff1
实例的输出wire q1 = inp[9]
这会导致争用,导致 x(未知值)。您只能使用一个来源驱动
q1
。
q2
和d1
也是如此。