Skip to content

Demonstration: Designing an Electronic Dice

An electronic dice is a small job that uses nearly everything so far: flip-flops to remember a state, next-state logic to move between states, a decoder to turn bits into something a person can read, and a clock to drive it all.

It is also a good design problem because nobody tells you how to build it. The specification is one sentence: press a button, get a number from 1 to 6, and make it fair. Every step below is a decision, and every decision has a consequence you can see.

The demonstration has five steps, in order. Each one asks you to choose something and then shows what that choice does.

1 Flip-flops asks how many you need. 2 Count and reset shows three D flip-flops with their next-state logic. Choose which six of the eight states to use and how the count wraps round, then clock it, run it, or power it up in any state. 3 Wire the pips has you find which pips always light together, then pick an expression for each pip signal. 4 Is it fair? lets you try to cheat at different clock speeds. 5 Two dice asks how a second dice should be clocked.

Your choices carry between steps, so the count you choose in step 2 is the one you design a decoder for in step 3.

Design an Electronic Dice

Three flip-flops, four pip signals and one button. Then find out whether it is fair.

A dice needs six different states, one per face. Each D flip-flop stores one bit, so n flip-flops can hold 2n different states. Choose how many to use.

Flip-flops

Choose two flip-flops, then three, then four. Two give four states, which is not enough. Four give sixteen and waste ten of them. Three give eight, which is six faces and two states left over. Those two spare states are not a nuisance to ignore; they are the subject of the next step.

Open 2 Count and reset with the count set to 0 to 5 and the reset set to Asynchronous. The next-state logic is a plain binary counter, and a separate AND gate watches the outputs. Press Clock pulse until the count passes 101.

With slow motion on, the counter visibly reaches 110 before the reset gate clears it. It has to: the gate cannot detect a state the counter never enters. In hardware that state lasts a few nanoseconds, but for that moment the dice shows a five.

Now switch the count to 1 to 6. The forbidden state is now 111, and for those few nanoseconds the dice lights all seven pips. Switch to Synchronous and the reset gate disappears. The wrap is built into the next-state logic, so the counter goes straight from the last face to the first on a single clock edge.

Finally, use Power up in to start each design in an unused state. Every design here recovers, but some show a face that no dice has on the way.

Open 3 Wire the pips and click two pips to compare them across all six faces. Some pairs disagree on at least one face. Three pairs never do. Once all three are found, the seven pips reduce to four signals: the centre, two diagonals and the middle pair.

Now pick an expression for each signal, counting 1 to 6. The centre pip is lit on 1, 3 and 5, which are exactly the odd states, so it is driven by Q0 directly. The rising diagonal is lit from face 4 upwards, which is exactly when Q2 is 1. Two of the four signals need no gates at all.

Switch the count to 0 to 5 and do it again. Same dice, same flip-flops, and the decoder costs twice as many gates.

Open 4 Is it fair? at 2 Hz and use Hold to roll to stop on a six. It is easy. Then press Practised cheat to see 600 throws by someone who is good at it.

Raise the clock to 50 Hz, then 1 kHz, and repeat. The circuit has not changed and still contains nothing random. What changes is that a full cycle of six faces becomes much shorter than the scatter in a human release, and from then on aiming achieves nothing.

Open 5 Two dice and roll 600 times with Shared clock. Every roll is a double. Try each of the other three arrangements and compare the histograms with the triangle that two independent dice should give.

Quiz
Select 0/1

Why does a dice need exactly three flip-flops?

Quiz
Select 0/1

A dice counting 1 to 6 uses an asynchronous reset that detects 111. Why can all seven pips light, even briefly?

Quiz
Select 0/1

Counting 1 to 6, the centre pip is driven by Q0 with no gates at all. Why?

Quiz
Select 0/1

The dice circuit contains nothing random. What makes it fair?

Concept Match

Match each design choice to its consequence

A one-sentence specification turned into five separate decisions, and each one could be made well or badly while the dice still appeared to work.

  1. Count the states you need and use the fewest flip-flops that hold them, then decide deliberately what the spare states do.
  2. An asynchronous reset has to see the state it removes. A synchronous reset never enters it.
  3. Look for outputs that always move together before designing a decoder, and choose your state encoding with the decoder in mind.
  4. Randomness in a counter-based design comes from timing an asynchronous human event against a fast clock, not from the logic.
  5. Two identical circuits on one clock stay identical. Independence has to be designed in.