I'm new to Verilog coding and stuck with an error in my code. Can someone help to debug it?
Main code:
module tff (q,t,clk,clear);
input t,clk,clear;
output reg q;
always @(negedge clk or posedge clear)
begin
if (clear) q <= 0;
else if(t==1) q <= ~q;
else q<= q;
end
endmodule
module seriesof3 (y,q1,q2,a,clk,clear);
input a,clk,clear,q1,q2;
output reg y,q1,q2;
always @(negedge clk or posedge clear)
begin
if (clear) begin
q1 <= 0;
q2 <= 0;
end
else begin
tff ta (q1,a,clk,clear);
tff tb (q2,~q1&(q2^a),clk,clear);
y <= a&q2;
end
end
endmodule
testbench code:
'timescale = 1 ms/1 ms
module testbench(y,a,clk,clear)
reg a,clk,clear;
wire y,q1,q2;
seriesof3 DUT (y,q1,q2,a,clk,clear);
initial
begin
$ dumpvars(0,test_bench);
$ dumpfile("first.vcd");
$ monitor ($time,"a=%b, Y= %b",a,y);
clear<=0;
a<=0;
#2 clear<=1;
#5 clear<=0;
end
forever #5 clk= ~clk;
begin
#8 a=0;
#5 a=1;
#5 a=1;
#5 a=1;
#5 a=1;
#5 a=0;
#5 a=1;
#5 a=1;
#5 a=0;
#5 a=1;
#5 a=1;
#5 $finish;
end
endmodule
Here I'm looking to detect three or more one's using the procedural method. I know I could have implemented it directly using the state diagram and updating states but I want to implement it as hardware implementation. Thanks in advance!
Yes, you have several issues :-)
tff
is instantiated inside of the always block (instances ta and tb). It is illegal in verilog. You need to move the instantiations outside.q1
andq2
as input and simultaneously as output. They should be only outputs.=
in time scale syntax.$
in $display and others.$
is a part of the name.forever
must be inside the initial block, you put it outside.q1
andq2
are driven multiple times by the aways block and the module instance. Since you also initialize them inside the tff module, you don not need to initialize them in seriesof3.forever
statement. This code will never execute. You need another initial block, I suggest a separate block for the forever statement. Also, you need to initialize your clk signal as well or it will stay at 'x'.Here is a syntactically fixed code: