Forum Discussion

Altera_Forum's avatar
Altera_Forum
Icon for Honored Contributor rankHonored Contributor
15 years ago

Problem with a simple bi-direcional counter simulation

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

2 Replies

  • Altera_Forum's avatar
    Altera_Forum
    Icon for Honored Contributor rankHonored Contributor

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

  • Altera_Forum's avatar
    Altera_Forum
    Icon for Honored Contributor rankHonored Contributor

    > 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.