ECE 287: A Random Number Every Second
The lecture ended with a challenge: build a design that displays a
~random number every ~second! You already have the three pieces from the
slides: an LFSR (lfsr), a one-second counter
(counter_50MHz), and a seven-segment decoder
(seven_segment). This page helps you understand each piece by
simulating it, and then gives you a starter file and a testbench for the
challenge. It does not give you the answer. The interesting part is yours.
Everything here runs in your browser on EDA Playground. Simulating first is much faster than compiling for the board every time you change one line.
Download everything (all four exercises)
Setting up EDA Playground
- Go to edaplayground.com and log in. An account is free. You need to be logged in to run a simulation.
- In the left-hand panel:
- Testbench + Design: SystemVerilog/Verilog
- Tools & Simulators: Icarus Verilog (the newest version listed)
- Tick Open EPWave after run so you see the waveforms.
- There are two code panes. The left one is
testbench.svand the right one isdesign.sv. Each exercise below has one of each. Click view to see a file in your browser, then use its Copy to clipboard button and paste it into the matching pane. You can also download the files and open them in any text editor. - Click Run. The printed output appears in the log at the bottom, and EPWave opens in a new window (see the next section).
- Click Save to keep your work. It gives you a link you can come back to, or share with your lab partner or TA.
Using EPWave (the waveform viewer)
EPWave opens empty. That’s normal: it doesn’t know which signals you care about, so you choose them. The log tells you what happened. The waveform shows when, clock by clock, and that is where most sequential bugs show up.
- If no EPWave window appears, your browser probably blocked the pop-up. Allow pop-ups for edaplayground.com and run again.
- Click Get Signals. On the left are the scopes:
tbis the testbench, andtb→dutis the design inside it. - Click a scope, select the signals listed in the exercise below, and click Append Selected.
- Use the zoom buttons to fit the whole run on screen, then zoom in on the part you want to read. Click in the waveform to place a cursor and read the values at that time.
- Multi-bit values like
numberorcountare shown as numbers on the bar. If they come up in hex, see if EPWave lets you switch that signal to decimal.
Each exercise below has an In EPWave task. Do it. It’s part of the exercise, not an extra.
Exercise 1: the LFSR
ex1_lfsr.zip
Files: design.sv: view · download,
testbench.sv: view · download
This is the slide’s LFSR, unchanged. The testbench resets it, loads a seed, prints 20 values, and measures how many clocks pass before a value repeats.
Try this:
- Do the numbers look random to you? Are they really random? What would happen if two boards loaded the same seed?
- Change
SEEDat the top of the testbench and run again. Does the period change? - Set
USE_SEEDto 0 so the seed is never loaded. What does the LFSR do after reset, and why? Look at the XOR taps and work out what happens to a register full of zeros. This one matters for the challenge.
In EPWave: add clk, rst,
en_seed, and number from the tb scope.
- Find the clock edge where
numberbecomes the seed. Isen_seedhigh before or after that edge? - Does
numberchange on every rising edge ofclk, or only some? Count how many different values you see in ten clock periods. - Run again with
USE_SEED = 0and look atnumber. What does the picture look like now?
Exercise 2: the seven-segment decoder
ex2_seven_segment.zip
Files: design.sv: view · download,
testbench.sv: view · download
The testbench feeds in every value from 0 to F and draws what the display would show, in text, in the log.
From here on the design builds up. Each design.sv keeps the
modules from the earlier exercises, complete. Their instantiations are in
testbench.sv but commented out, so only the new module is
tested. In the challenge your top level instantiates all of them.
Try this:
- A segment lights when its bit is 0. Why? Look up “active low”.
- Your random number is 8 bits, 0 to 255. How many decoders do you need
to show it in decimal? What range of values does each decoder ever see?
The bottom of the challenge file already has this part from the slides.
Read it and make sure you understand
/ 100 % 10.
In EPWave: add i and o from the
tb scope.
- There is no clock in this testbench. When does
ochange, compared withi? Place a cursor right whereichanges and reado. - Compare that with Exercise 1, where
numberonly changed on a clock edge. Which of these two modules is combinational, which is sequential, and how can you tell from the waveform alone?
Exercise 3: the one-second counter
ex3_counter.zip
Files: design.sv: view · download,
testbench.sv: view · download
A real second is 50,000,000 clocks, and simulating that would take far too
long. So in these files the limit is a parameter, MAX_COUNT. The
board uses the default of 50,000,000, and the testbench shrinks it to 10 clocks
so you can read every line.
Try this:
- How many clocks after
startgoes high doesdonego high? - While
startstays high, doesdoneever go back to 0? Look atcountin EPWave too. - The challenge needs something to happen once every second, over and
over. Does this counter do that as written? What would have to change,
either inside it or in how you drive
start? - On the slide the comparison was
count >= 7'd50000000. How many bits does 50,000,000 need? What does7'ddo to it?
In EPWave: add clk, start, and
done from the tb scope, and count from
tb → dut.
- Put one cursor where
startrises and another wheredonerises. How many clock periods are between them? Does it matchMAX_COUNTexactly? If not, why is it off by one? - Follow
countto the end of the run. What is it doing whiledonesits high? Work out how many clocks it would take beforedonewent low on its own (countis 27 bits).
The challenge: a random number every second
challenge.zip
Files: design.sv: view · download,
testbench.sv: view · download
design.sv holds all three modules from the exercises plus the
skeleton of the top-level module, random_display. Every
piece is already there and wired up: the LFSR (u_lfsr), the
counter (u_counter), a display register (shown),
and the three seven-segment decoders. What isn’t built yet is the logic
that makes them work together. That is three TODOs:
load_seed: when should the LFSR load the seed from the switches?counter_start: how do you get a tick every second, not just once?shown: on which clocks should the display take a new number, and from where?
Each TODO holds a placeholder value so the file compiles and runs as soon as you download it. It shows 000 forever. That is your starting point.
The testbench acts as the board and as you watching it. You shouldn’t need to change it. It drives the inputs below, reads the three HEX displays back into a number, prints the number every time it changes, and finishes with a summary. Inside the testbench, one second is 100 clocks.
| Board signal | Meaning |
|---|---|
CLOCK_50 | 50 MHz clock |
KEY[2] | Reset. Active low: it reads 0 while pressed. |
KEY[1] | Load the seed. Also active low. |
SW[7:0] | The seed value |
HEX2, HEX1, HEX0 | Hundreds, tens, and ones digits of the random number |
LEDR | Yours to use, optional |
Questions to think through before you write code:
- The LFSR makes a new number on every clock, 50 million a second. You
want a new number on the display about once a second. The skeleton gives
you a register,
shown, to remember the number between those moments. What makes it change, and what makes it hold still? - What is
one_second(the counter’sdone) for? What should happen on the clock when it goes high? - Remember Exercise 3. Once
donegoes high, does the counter start over by itself? What cancounter_startdo about that? - Remember Exercise 1. What will you see if the seed is never loaded?
KEY[1]is 0 when pressed, andload_seed(the LFSR’sen_seed) loads when it is 1. How do you connect them?
In EPWave (this is how you debug the challenge): from
tb add CLOCK_50 and KEY. From
tb → dut add random_number,
load_seed, counter_start, one_second,
and val. From tb → dut →
u_counter add count.
- Do one TODO at a time and check it here before moving on. After TODO 1,
is
random_numberchanging? After TODO 2, can you see the one-second rhythm inone_secondwhen you zoom out to the whole run? - Find where
KEY[1]is pressed. What doesload_seeddo there, and what doesrandom_numberdo on those clocks? - Put a cursor on a clock edge where
one_secondis high. What israndom_numberthere, and what doesvalbecome on the next edge? - Between two ticks,
random_numbershould change on every clock whilevalholds still. If both are changing, or both are frozen, that tells you which TODO to look at.
Reading the summary. If the log says…
| The summary says | Ask yourself |
|---|---|
| Never changed, stayed at 0 | Which TODOs still hold their placeholder? Was the seed ever loaded? See Exercise 1. |
| Never changed, stayed at 165 | 165 is the testbench’s seed. Is the LFSR loading the seed on every clock? Check which way round load_seed follows KEY[1]. |
| Changing much faster than once a second | Is shown taking a new value on every clock? What should only happen when one_second fires? |
| Changed once or twice, then stopped | Does your one-second tick happen again and again, or only once? See Exercise 3. |
| Not a 0–9 digit | Check the HEX wiring and the digit math at the bottom of the file. |
| About one new number per second | Nice. Do the numbers look random? Open EPWave and check that nothing changes between ticks. |
Then take it to the board
When the simulation looks right, use your random_display module
as the top level in Quartus. Leave ONE_SECOND at its default of
50,000,000, so one second really is one second, and use your usual pin
assignments for CLOCK_50, KEY, SW,
HEX0–HEX2, and LEDR. If the board
does something the simulation didn’t, the difference between the two is
your next clue.
Peter Jamieson, Electrical and Computer Engineering, Miami University. Questions go to me or your TA. Bring your saved EDA Playground link.