Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

specify integer range and cycle through it

Status
Not open for further replies.

drifterz

Newbie level 5
Joined
Aug 12, 2006
Messages
8
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,328
Hi,

I wish to implement a counter with a range say from 0 to 9. Is there a way to specify the range such that it will be only within the range 0 to 9? For example, if I use counter<=counter-1
The current counter value is 0 and I wish the subtraction to yield 9 instead of -1 automatically. Likewise when counter=9, counter + 1 will yield 0.
Is there a way to do this automatically without checking for it in the code using IF? TIA
 

If you are using Verilog, you need to check the count value. Try this compact syntax:

Up counter:
count <= count == 9 ? 0 : count + 1;

Down counter:
count <= count == 0 ? 9 : count - 1;

Up/down counter:
count <= down ? (count == 0 ? 9 : count - 1) : (count == 9 ? 0 : count + 1);
 

    drifterz

    Points: 2
    Helpful Answer Positive Rating
That's what I did in VHDL.
Was just thinking whether by specifying range 0 to 9 will cause it to cycle within this range.
Found that it doesn't work this way so will have to check in the code using IF statements as above.
Thanks nonetheless.
 

It wouldn't be helpful, if a VHDL synthesis tool adds range checking to each arithmetic expression for an INTEGER variable with a range. So there is no other option than performing the range check where you need it. However you may use a function to improve readability of your code, it should be able to get the range info from variable attributes in the function body.
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top