Lab 10

Set the speed of a binary counter in assembly

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

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.

  1. Get the starterDownload paced_cpu.v
  2. Open the LabsLand Online EditorOpen the lab LabsLand asks you to sign in. Use the access your course or institution provides.
  3. Follow the worksheetPredict first, then build, read the board and hand in. The build steps are below.

Starter files · Verilog · VHDL · SystemVerilog

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?

test_paced.v

test_paced.vhd

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.

  1. In the LabsLand Online Editor, choose DE1-SoC for your HDL and the Standard interface.
  2. Create paced_cpu, paste the design and set it as the top level.
  3. 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