1
\$\begingroup\$

I've written this code for a counter, but I don't know why it doesn't work. The output remains at zero, and when I change load to 0, output changes to unknown.

Would you please help me to find the error?

module behav_counter( d, clk, clear, load, up_down, qd);
 input [7:0] d;
 input clk;
 input clear;
 input load;
 input up_down;
 output [7:0] qd;
 reg [7:0] cnt;
always @ (posedge clk or negedge clear)
if (!clear)
 cnt = 8'h00;
else if (load)
 cnt = d;
else if (up_down)
 cnt = cnt + 1;
else
 cnt = cnt - 1;
assign qd = cnt;
endmodule
module test_b;
 reg d,clk,clear,load,up_down;
 wire qd;
 behav_counter bc( d, clk, clear, load, up_down, qd);
 initial
 begin
 clear = 0;
 clk = 0;
 #15 clear = 1; 
 repeat (400)
 #5 clk = ~clk;
 end
 initial 
 begin
 up_down=1;
 load=1;
 d=8'h00;
 #23
 load=0;
 #100
 load=1;
 end
endmodule
toolic
10.8k11 gold badges31 silver badges35 bronze badges
asked Jan 13, 2017 at 5:33
\$\endgroup\$
0

1 Answer 1

3
\$\begingroup\$

When I run your code on 2 different simulators, I get compile warnings about port size differences for d and qd. Your counter declares them as 8-bit wide, but you connect 1-bit signals to them. If you don't get warnings, try it on edaplayground.

In test_b, declare them as 8-bit:

module test_b;
 reg clk,clear,load,up_down;
 reg [7:0] d;
 wire [7:0] qd;

Now I see qd[7:0] count up from 0 to 0xa when load=0.

answered Jan 13, 2017 at 13:24
\$\endgroup\$

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.