I am trying to simulate a 3 stage shift register with feedback loop using D-flipflop and XOR gate.
main.v:
module main(
d0,d1,d2,
clk ,
reset ,
q0,q1,q2);
input clk,reset;
inout d0,d1,d2,q0,q1,q2;
dff df1(.d(d0),.clk(clk),.reset(reset),.q(q0));
dff df2(.d(q0),.clk(clk),.reset(reset),.q(q1));
dff df3(.d(q1),.clk(clk),.reset(reset),.q(q2));
xor1 exor1(.a(q1), .b(q2), .c(d0));
endmodule
dff.v:
module dff(
d,
clk,
reset,
q);
input d,clk,reset;
output q;
reg q;
always @ ( posedge clk)
if (~reset) begin
q <= d;
end else begin
q <= 1'b0;
end
endmodule
main_tb.v:
module main_tb;
reg clk,reset;
//reg d0,d1,d2,q0,q1,q2;
wire d0,d1,d2,q0,q1,q2;
initial
begin
clk = 0;
reset = 1;
#15
reset = 0;
end
always begin
#5 clk = !clk;
end
main U0(
.d0(d0),
.d1(d1),
.d2(d2),
.clk(clk),
.reset(reset),
.q0(q0),
.q1(q1),
.q2(q2)
);
endmodule
Now ISim doesnt simulate this code. Where am i going wrong?
EDIT: edited the testbench code. ISim is stuck at 0ps.enter image description here
2 Answers 2
You have a reset
signal in your code, but you never actually assert it.
Without that, the simulator cannot put the FFs into a known state, so they just show "unknown" forever.
Assigning values to your q
wires in the testbench is useless — that just conflicts with the values that the dff
s are trying to produce.
-
\$\begingroup\$ So all the values get initialized in ISim, but the 'Run' button is greyed out \$\endgroup\$Rohith Reddy– Rohith Reddy2016年09月06日 23:25:23 +00:00Commented Sep 6, 2016 at 23:25
-
\$\begingroup\$ Hey @Dave Tweed, Any idea on how to 'Run' it. \$\endgroup\$Rohith Reddy– Rohith Reddy2016年09月07日 00:14:34 +00:00Commented Sep 7, 2016 at 0:14
-
\$\begingroup\$ In any LFSR if you reset to 0's then feedback must be inverted to D, otherwise it is stuck and that it wont work. If inverted feedback then all 1's is illegal for initial condition, with an odd number of XOR's \$\endgroup\$Tony Stewart EE since 1975– Tony Stewart EE since 19752016年09月07日 00:17:55 +00:00Commented Sep 7, 2016 at 0:17
Your clock generator should be
always @ // remove the (*)
#5 clk = !clk;
Also, you probably want to use a procedural force
statement and then release
the intermediate q signals.
-
\$\begingroup\$ Where should I add force statement? In main.v or in main_tb.v? \$\endgroup\$Rohith Reddy– Rohith Reddy2016年09月06日 23:01:17 +00:00Commented Sep 6, 2016 at 23:01
-
\$\begingroup\$ In main_tb. You want to force the q0-1 for at least one clock cycle to get the shift register into a known state, then release them \$\endgroup\$dave_59– dave_592016年09月06日 23:04:38 +00:00Commented Sep 6, 2016 at 23:04
-
\$\begingroup\$
initial begin clk = 0; reset = 0; force U0.d0 = 1'b0; force U0.q0 = 1'b0; force U0.q1 = 1'b1; force U0.d1 = 1'b0; force U0.d2 = 1'b0; #5 release U0.d0; release U0.q0; release U0.q1; release U0.d1; release U0.d2; end
\$\endgroup\$Rohith Reddy– Rohith Reddy2016年09月06日 23:11:13 +00:00Commented Sep 6, 2016 at 23:11 -
2\$\begingroup\$ Shouldn't need to force any signals, just assert the reset signal for a clock cycle or more. \$\endgroup\$Tom Carpenter– Tom Carpenter2016年09月06日 23:13:11 +00:00Commented Sep 6, 2016 at 23:13
-
\$\begingroup\$ I guess it depends on how you want to test the LFSR. \$\endgroup\$dave_59– dave_592016年09月06日 23:14:25 +00:00Commented Sep 6, 2016 at 23:14
always @ ( posedge clk) if (reset) begin q <= 1'b0; end else begin q <= d; end
. Synthesis tools are better at recognising logic when you stick to the standard patterns. \$\endgroup\$