Programmable Devices
CPLDs, FPGAs, SoC FPGAs, Configuration, and Transceivers
21602 Discussions

Problem with a simple bi-direcional counter simulation

Altera_Forum
Honored Contributor II
1,184 Views

Hello this is my code  

 

module count_moda # (parameter modulus = 100) ( input clk, aclr_n, updown, output reg q ); initial q = 0; always @(posedge clk, negedge aclr_n) begin if (!aclr_n) q <= 0; else begin q <= q + (updown? 1'b1 : -1'b1); if (q == modulus-1) q <= 8'd0; // if (q == 0) // q <= modulus -1; end end endmodule 

 

The way it is right now it restarts when 99 is reached, then it starts to count up untill 10 when my updown signal goes to 0 and it starts decremetating to 9, 8 etc.. untill it goes to a negative value! 

But i want to go back to 99 when i reach 0 in the decrematating mode, don't know how to solve it 

If i uncomment the second if my result is always 0 or 99. 

 

 

Thanks
0 Kudos
2 Replies
Altera_Forum
Honored Contributor II
496 Views

You can't perform the overflow/underflow operation correctly without using the updown input, it seems so obvious...

0 Kudos
Altera_Forum
Honored Contributor II
496 Views

> if (q == modulus-1) > q <= 8'd0; 

this script should be 

 

if( updoun ) 

begin  

if( q == (modulus-1) ) 

q <= 8'h0; 

end 

else 

begin 

if( q == 8'h0 ) 

q <= (modulus-1) ; end 

 

I think this is obvious too. 

you haven't written reset logic for negative direction.
0 Kudos
Reply