Count to nineteen with a pushbutton
Online version: https://fpga.labsland.com/altera/digital-logic/lab-05/part-01/
Design a counter that repeats every twenty presses. LEDR4…0 show the count in binary. LEDR9 lights for one count to warn that the next press will return the value to zero.
Lab setup
LabsLand Online Editor · DE1-SoC
Physical DE1-SoCCyclone V · 5CSEMA5F31C6 · camera, switches and buttons in the browser
Run the simulation on your computer with Icarus Verilog (Verilog or SystemVerilog) or NVC (VHDL). Then build your design and test it on the remote DE1-SoC.
Build
- From the extracted worksheet folder, open
starter/modulo_count.qpfin the selected release of Quartus Prime Lite with Cyclone V support. The project-only download has the QPF at its root. - Edit the HDL file for
modulo_count. If the exercise generates a new HDL file, copy its contents into that same project file. - Choose Processing → Start Compilation. Check the Fitter report for device 5CSEMA5F31C6.
- In the LabsLand bitstream upload lab, choose Standard and upload
output_files/modulo_count.sof. Wait for programming confirmation before using the controls. - If your course uses a LabsLand classroom, the upload opens a firmware library instead: check that your SOF is selected and choose Enter board with selected firmware.
WIDTH sets the number of counter bits and MODULUS sets the number of states. Find the comparison that drives the warning light. Correct it so the last state is visible for a whole button press. Leave WIDTH at 5 and MODULUS at 20 for the board.
- Get the starterDownload
modulo_count.v - Open the LabsLand Online EditorOpen the lab Choose DE1-SoC · Verilog when you open the lab. LabsLand asks you to sign in. Use the access your course or institution provides.
- Follow the worksheetPredict first, then build, read the board and hand in. The build steps are below.
- Install Quartus Prime Lite with Cyclone V supportSkip this if it is already installed.
- Download the activityDownload worksheet and Quartus project Includes the worksheet, the Quartus project and the simulation files.
- Complete the design and compileStart with Predict the rollover in the worksheet below, then complete the design as its build steps describe. Open
modulo_count.qpfand choose Processing → Start Compilation. Quartus writesoutput_files/modulo_count.sof. - Upload the bitstream to LabsLandUpload bitstream LabsLand asks you to sign in. Use the access your course or institution provides.
Already compiled? Upload your .sof directly
Starter files · Verilog · VHDL · SystemVerilog
Worksheet and starter (.zip) for offline work or printing
Predict the rollover
Write down the five count bits and the warning light at 18, 19 and 0. How many different values would four bits represent? Choose a width that can represent every value from zero through nineteen.
Build
- In the LabsLand Online Editor, choose DE1-SoC for your HDL and the Standard interface.
- Create
modulo_count, paste the design and set it as the top level. - Choose Synthesize → Upload to FPGA. After editing the design, synthesize and upload it again to test the change on the board.
WIDTH sets the number of counter bits and MODULUS sets the number of states. Find the comparison that drives the warning light. Correct it so the last state is visible for a whole button press. Leave WIDTH at 5 and MODULUS at 20 for the board.
Simulate two sizes
Extract the worksheet ZIP and run these commands from the folder containing activity.md.
The testbench checks two counters with the same button inputs, one with twenty states and one with five. Run it on the starter first and note the first failing count. Then run it on your correction. A complete run checks 42 press/release pairs and reset between presses.
Verilog
iverilog -g2005 -s test_modulo_count -o bench starter/modulo_count.v simulation/test_modulo_count.v
vvp bench
SystemVerilog
iverilog -g2012 -s test_modulo_count -o bench starter/modulo_count.sv simulation/test_modulo_count.v
vvp bench
VHDL
nvc --std=2008 -a starter/modulo_count.vhd simulation/test_modulo_count.vhd
nvc --std=2008 -e test_modulo_count
nvc --std=2008 -r test_modulo_count
Test the board
Synthesize and upload. Press KEY0 once to reset. Each press on KEY1 should advance by one, and releasing it should leave the count unchanged. Wait until the camera shows the new count before pressing again. Pause before each of the last three presses so you can record the warning light.
| Number of KEY1 presses after reset | LEDR4…0 | LEDR9 |
|---|---|---|
| 1 | ||
| 18 | ||
| 19 | ||
| 20 | ||
| 21 |
Press KEY0 while the count is nonzero and leave KEY1 alone. Explain what this observation tells you about asynchronous reset. Submit the predictions, the simulation result for each width, and your board observations.
Source
Original LabsLand activity following FPGAcademy Digital Logic Lab 5, Part I. Verilog handout · VHDL handout · MIT
