Counter from 0 to 11 then back to 0 (system verilog)

  • Thread starter Thread starter hoheiho
  • Start date Start date
  • Tags Tags
    Counter
Click For Summary

Discussion Overview

The discussion revolves around implementing a counter in System Verilog that counts from 0 to 11 and then resets to 0. Participants are exploring issues related to control flow in the code, the use of signals, and the functionality of the counter.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Exploratory

Main Points Raised

  • One participant describes their implementation and notes that the counter does not reset to 0 after reaching 12, questioning the use of "else if" in their code.
  • Another participant suggests that the control flow may not evaluate the "else if" condition if the first condition is true, proposing an alternative structure in VHDL.
  • There is a query about the purpose of the "ready" signal, with a participant explaining its function in the context of counting.
  • A participant asks whether they can use "count" for internal counting instead of "i," expressing confusion about the output variable's usability in internal logic.
  • Another participant, with no experience in Verilog, speculates that the structure of the code should be similar to VHDL, discussing variable declarations and their implications.

Areas of Agreement / Disagreement

Participants express differing views on the control flow and variable usage in the counter implementation. There is no consensus on the best approach or resolution to the issues raised.

Contextual Notes

Participants mention potential issues with variable declarations and control flow logic, but these remain unresolved within the discussion.

hoheiho
Messages
43
Reaction score
0
Hi all,
i am trying to do a counter using system verilog. The counter will count from 0 to 11 then back to 0 and start the counting again.

Input = rdy, if rdy = 0, start counting.
Output = count

I have done the simulation for my code and it is not working as i prefer. It will continues count until it's overflow but not back to 0 if the count = 12.
It seems like " else if (i==12)" doesn't work. How can i fix the problem? or i should not use else if here?

Thanks for the help
Ivan

Code:
always_ff @ (posedge clk,negedge rst)
  if(!rst)
      begin
      count<=0;
      i<=0;
      end
      else 
        begin
        if (!rdy)
          begin
          count<=count+1;
          i<=i+1;
          end
          else if (i==12)
            begin
              count<=0;
              i<=0;
            end
        else
          begin
          count<=count;
          i<=i;
          end
        end
        endmodule
 
Physics news on Phys.org
I don't know verilog, but the if ... else if ... control flow typically won't attempt to evaluate the second conditional "else if (i ==12)" in this case, if the first is true.

there are a million and one ways to implement a counter in a hardware descriptive language. i would probably structure VHDL as
Code:
if(rising_edge(clock) and not RST) then
      if(i<12) then
          i <= 1 + 1;
      else
          i <= 0;
      end if
else if ( RST ) then
         count <= 0;
end if

its been a long time, so my syntax is probably all wrong.

p.s, what's the point in the ready signal?
 
earlofwessex said:
I don't know verilog, but the if ... else if ... control flow typically won't attempt to evaluate the second conditional "else if (i ==12)" in this case, if the first is true.

there are a million and one ways to implement a counter in a hardware descriptive language. i would probably structure VHDL as
Code:
if(rising_edge(clock) and not RST) then
      if(i<12) then
          i <= 1 + 1;
      else
          i <= 0;
      end if
else if ( RST ) then
         count <= 0;
end if

its been a long time, so my syntax is probably all wrong.

p.s, what's the point in the ready signal?



Thanks for your reply
what does RST mean?
 
hoheiho said:
Thanks for your reply
what does RST mean?

sorry, its reset. I've set it up to be asynchronous.
 
earlofwessex said:
I don't know verilog, but the if ... else if ... control flow typically won't attempt to evaluate the second conditional "else if (i ==12)" in this case, if the first is true.

there are a million and one ways to implement a counter in a hardware descriptive language. i would probably structure VHDL as
Code:
if(rising_edge(clock) and not RST) then
      if(i<12) then
          i <= 1 + 1;
      else
          i <= 0;
      end if
else if ( RST ) then
         count <= 0;
end if

its been a long time, so my syntax is probably all wrong.

p.s, what's the point in the ready signal?
ready is the input, if the ready=0 and last for 3 clock cycle, it will count from 0 to 2.
 
earlofwessex said:
sorry, its reset. I've set it up to be asynchronous.

sorry, 1 more question

can i use count to the counting?not i.
like
if count < 12
...
..
i have try that in my code but it doesn't work. is that becuase count is the output so i cannot use it for internal counting?
 
hoheiho said:
sorry, 1 more question

can i use count to the counting?not i.
like
if count < 12
...
..
i have try that in my code but it doesn't work. is that becuase count is the output so i cannot use it for internal counting?

again, I have zero experience with verilog, but since it compiles down to the same thing as VHDL, i would imagine the structure of what you can do is similar.

in the code posted below, i and count are dealt with in exactly the same way. so unless you have declared them as different datatypes, there's no reason count would not work instead of i.

if you declare a variable as an output, then make some conditional statements based on its value, you must also be able to read it as an input. VHDL requires that this variable be declared as "inout" or "buffer" (or perhaps "signal" or "variable")

I assume that verilog has a similar restriction?
 

Similar threads

  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 1 ·
Replies
1
Views
12K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 9 ·
Replies
9
Views
21K
  • · Replies 6 ·
Replies
6
Views
6K
  • · Replies 7 ·
Replies
7
Views
3K
Replies
6
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 1 ·
Replies
1
Views
13K