# Single Die Roll Counter Wrap Around Nested Ternary Conditional

I have to emulate a single die roll, therefore it needs to wrap back to one at 6. D1 and D6 correspond to my 3-bit state encoding of the die at value 1 an 6, respectively. in corresponds to the switch roll, if the switch is on then the dice is rolling and value increments if not then it simply equals the last value given. I need equal probability so no I can't just ignore any values.

My problem is this:
I am trying to use a ternary conditional within one.
I'm hoping to stop the roll at D6 and just wrap to D1, if not then D6.

assign next  = in ?((count == D6) ? D1 :(count + 1)) : count;

DFF state_register(clk, next, state);


I don't think my conditional is right. Any help on reading it properly?

• it's correct expcet 'D6 needs to be D6. :) Jul 28, 2014 at 18:22
• You can do just "counter = (counter + 1) % 6;" and attach the ternary like so "assign next = in ? (count + 1) % 6 : count;" Jul 28, 2014 at 21:49
• Hmm I see at 7 it would give me 1 and then at 8 it would give me 2. It would give appearance of wraparound, however at 6, 6 % 6 = 1. I need 6 included, also i would get large numbers which I would have to store and operate on. This keeps it within the 1 to 6 range with wraparound, with small numbers. Jul 28, 2014 at 21:55
• just saw the rest of the comment, i gotcha now! Thanks! Jul 28, 2014 at 21:57

it's correct expcet 'D6 needs to be D6. :) D6 because i defined it above my module. I just used an apostrophe which it is not.

Besides, how does this all work? Well ternary conditionals work as so: variable = conditional ? true : false;

so next = count if my in is false.

Now what I have here is when it is true there is a nested ternary conditional:

''(count == D6) ? 'D1 : (count + 1)

so if count is equal to my D6 (my die has a value of 6) then I need it to wrap around toD1 (die of value 1), because the die here can't have value 7, 8, 9, etc. We want the counting to wrap back around.

Therefore,

when in is true and count is not D6 then I count up. when in is true and count isD6 then I make next equal to D1 when in is false then next is just equal to count, meaning it stays current and no "roll" is recorded.

Hope that all makes sense!