Dr. Peter Andrew Jamieson — Academic in an information evolution

← Teaching

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

  1. Go to edaplayground.com and log in. An account is free. You need to be logged in to run a simulation.
  2. 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.
  3. There are two code panes. The left one is testbench.sv and the right one is design.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.
  4. Click Run. The printed output appears in the log at the bottom, and EPWave opens in a new window (see the next section).
  5. 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.

  1. If no EPWave window appears, your browser probably blocked the pop-up. Allow pop-ups for edaplayground.com and run again.
  2. Click Get Signals. On the left are the scopes: tb is the testbench, and tb → dut is the design inside it.
  3. Click a scope, select the signals listed in the exercise below, and click Append Selected.
  4. 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.
  5. Multi-bit values like number or count are 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:

In EPWave: add clk, rst, en_seed, and number from the tb scope.

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:

In EPWave: add i and o from the tb scope.

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:

In EPWave: add clk, start, and done from the tb scope, and count from tb → dut.

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:

  1. load_seed: when should the LFSR load the seed from the switches?
  2. counter_start: how do you get a tick every second, not just once?
  3. 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 signalMeaning
CLOCK_5050 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, HEX0Hundreds, tens, and ones digits of the random number
LEDRYours to use, optional

Questions to think through before you write code:

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.

Reading the summary. If the log says…

The summary saysAsk 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.