Set the speed of a binary counter in assembly
Online version: https://fpga.labsland.com/altera/digital-logic/lab-10/part-06/
Make the ten red LEDs count in binary. The starter counts, but adjusting SW2–0 does nothing to its speed. You'll repair the assembly program, build it into the processor in your chosen HDL and measure the result on a physical DE1-SoC. On your computer, use Python 3 to assemble the program and NVC (VHDL) or Icarus Verilog (Verilog and SystemVerilog) to check its timing before you open the lab.
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
Use the regenerated HDL that passed the simulation. Keep paced_cpu as the top-level name.
- From the extracted worksheet folder, open
starter/paced_cpu.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
paced_cpu. 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/paced_cpu.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.
- Get the starterDownload
paced_cpu.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 period in the worksheet below, then complete the design as its build steps describe. Open
paced_cpu.qpfand choose Processing → Start Compilation. Quartus writesoutput_files/paced_cpu.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 period
In paced_counter.s, mark the two backwards branches. The inner loop starts with r1 = 0x0200. Count the instructions spent in one inner iteration. Find where the outer loop gets its count and explain how you would avoid a zero starting count. The processor starts one instruction every 16,384 board clocks at 50 MHz. Estimate the period with one outer pass and with eight.
Assemble the starter
Download and extract the activity ZIP. From the folder containing activity.md, assemble the unchanged program and load it into the starter for your selected HDL.
If the assembler reports an error, correct the indicated line and assemble again before loading the memory image. A failed build leaves any previous .mif in place.
On Windows with the Python launcher, use py -3 wherever a command starts with python3.
Verilog
python3 simulation/sbasm.py simulation/paced_counter.s paced.mif
python3 simulation/load_program.py paced.mif starter/paced_cpu.v --output my_paced_cpu.v
SystemVerilog
python3 simulation/sbasm.py simulation/paced_counter.s paced.mif
python3 simulation/load_program.py paced.mif starter/paced_cpu.sv --output my_paced_cpu.sv
VHDL
python3 simulation/sbasm.py simulation/paced_counter.s paced.mif
python3 simulation/load_program.py paced.mif starter/paced_cpu.vhd --output my_paced_cpu.vhd
Simulate
Extract the worksheet ZIP and run these commands from the folder containing activity.md. The simulator reads the HDL file you generated in the assembly step.
The bench starts an instruction every eight simulated clock cycles, so you can test the same assembly loops more quickly. It measures the periods at 000 and 111, then checks the six settings in between. With SW9 down, the count must hold for two full fast-count periods. The final check resets the counter.
Record the two periods reported for the unchanged program. Change the mask in simulation/paced_counter.s so SW2–0 sets the outer-loop count. Leave the 512 inner iterations unchanged. Assemble the edit, regenerate the HDL and run the same bench again. Which period changed?
Verilog
iverilog -g2005 -s test_paced -o bench my_paced_cpu.v simulation/test_paced.v
vvp bench
SystemVerilog
iverilog -g2012 -s test_paced -o bench my_paced_cpu.sv simulation/test_paced.v
vvp bench
VHDL
nvc --std=2008 -a my_paced_cpu.vhd simulation/test_paced.vhd
nvc --std=2008 -e test_paced
nvc --std=2008 -r test_paced
Build
Use the regenerated HDL that passed the simulation. Keep paced_cpu as the top-level name.
- In the LabsLand Online Editor, choose DE1-SoC for your HDL and the Standard interface.
- Create
paced_cpu, 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.
Time the physical counter
With SW9 down, set SW2–0 to 000 and wait two seconds for the switches to settle, then pulse KEY0 once. Raise SW9. Note the binary count and time at two readings several seconds apart. Lower SW9 and confirm the count holds. Repeat from reset with SW2–0 at 111, allowing at least nine seconds for the slower run.
| SW2–0 | First count and time | Last count and time | Seconds per increment |
|---|---|---|---|
| 000 | |||
| 111 |
Read LED0 as the least significant bit. The camera can skip intermediate counts. Use two clearly readable values after the count starts moving. If the count is the same at both readings, keep timing until it changes. Divide elapsed time by the change in count. Compare the measured ratio with your prediction, allowing for the camera refresh. Which instructions run only once per displayed count?
Based on FPGAcademy Digital Logic Lab 10. Verilog handout · VHDL handout · MIT license
