Build and test a processor with a stack
Online version: https://fpga.labsland.com/altera/digital-logic/lab-11/part-08/
Make a 16-bit processor call a subroutine from inside another subroutine. Then use your processor to compare four ways of shifting a negative number. You will need the branch and memory-port concepts from Lab 10, a computer with Python 3 and access to the LabsLand DE1-SoC.
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/stack_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
stack_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/stack_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
stack_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 Trace the stack in the worksheet below, then complete the design as its build steps describe. Open
stack_cpu.qpfand choose Processing → Start Compilation. Quartus writesoutput_files/stack_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
Trace the stack
Open the assembly program. The program starts its stack pointer at 0x1000. RAM occupies word addresses 0x0000–0x0FFF. The first push must decrement the stack pointer before writing. Predict the first two addresses written by transform. In render, find the call that would destroy its return address unless the caller saved it. Follow the return through both subroutines.
Build
- In the LabsLand Online Editor, choose DE1-SoC for your HDL and the Standard interface.
- Create
stack_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.
Repair the shifter
The starter has a fault in one shift operation. Build and upload the starter unchanged first. Lower SW9, pulse KEY0, then raise SW9. SW6–5 selects logical left, logical right, arithmetic right or rotate right in that order. SW3–0 sets the amount. Leave SW4 down. The red LEDs echo the low control bits. Read HEX3–0 as one hexadecimal word.
| SW6–5 | Amount | Predicted from 0x9010 | Starter | After repair |
|---|---|---|---|---|
| 00 | 1 | |||
| 01 | 1 | |||
| 10 | 1 | |||
| 10 | 4 | |||
| 11 | 5 |
For which pair of rows does the starter give the same answer unexpectedly? Track bit 15 through the shifter. Correct the HDL, run the bench and upload the new design. Repeat both arithmetic-right rows. Compare the high bits with the logical-right result. Each switch setting starts from the same 0x9010 seed.
Simulate
Run these commands from the extracted worksheet folder. First try the supplied HDL, then repeat after editing the shifter in the starter file. The bench tests all 64 mode and amount pairs on a running processor with nested calls. It reports the first mismatch. For the arithmetic right shift, compare the new high bit with the original sign.
Verilog
iverilog -g2005 -s test_stack -o bench starter/stack_cpu.v simulation/test_stack.v
vvp bench
SystemVerilog
iverilog -g2012 -s test_stack -o bench starter/stack_cpu.sv simulation/test_stack.v
vvp bench
VHDL
nvc --std=2008 -a starter/stack_cpu.vhd simulation/test_stack.vhd
nvc --std=2008 -e test_stack
nvc --std=2008 -r test_stack
Run two diagnostic programs
Stack and shift checks uses a descending stack, saves 0x0F0F across a call and checks that the stack pointer returns to 0x1000. It also checks the carry from a left shift. The program leaves 0x055 on the LEDs on success and 0x00F on failure. Cumulative shifts applies each transformation to the previous result. Reset between modes. The first display is 9010. With a one-bit shift, predict its next value. Run this version in simulation, where you can step through the rapid display changes.
Save your shifter repair in the HDL file under starter/. From the extracted worksheet folder, assemble both programs and insert each one into its own copy of the processor.
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/stack_checks.s stack_checks.mif
python3 simulation/load_program.py stack_checks.mif starter/stack_cpu.v --output stack_checks_cpu.v
python3 simulation/sbasm.py simulation/cumulative_shifts.s cumulative_shifts.mif
python3 simulation/load_program.py cumulative_shifts.mif starter/stack_cpu.v --output cumulative_shifts_cpu.v
SystemVerilog
python3 simulation/sbasm.py simulation/stack_checks.s stack_checks.mif
python3 simulation/load_program.py stack_checks.mif starter/stack_cpu.sv --output stack_checks_cpu.sv
python3 simulation/sbasm.py simulation/cumulative_shifts.s cumulative_shifts.mif
python3 simulation/load_program.py cumulative_shifts.mif starter/stack_cpu.sv --output cumulative_shifts_cpu.sv
VHDL
python3 simulation/sbasm.py simulation/stack_checks.s stack_checks.mif
python3 simulation/load_program.py stack_checks.mif starter/stack_cpu.vhd --output stack_checks_cpu.vhd
python3 simulation/sbasm.py simulation/cumulative_shifts.s cumulative_shifts.mif
python3 simulation/load_program.py cumulative_shifts.mif starter/stack_cpu.vhd --output cumulative_shifts_cpu.vhd
Run the program testbench with each processor. The first run checks the stack and carry. The second selects arithmetic right shift (mode 2). The bench prints the initial word and five successive one-bit shifts. Before you run modes 1 and 3, predict the first step at which their displays will differ. Run each mode from reset, compare the two traces and identify which bit moved into bit 15. Use mode 0 to check the left shift.
Verilog
iverilog -g2005 -s test_program -o check stack_checks_cpu.v simulation/test_program.v
vvp check
iverilog -g2005 -s test_program -o check cumulative_shifts_cpu.v simulation/test_program.v
vvp check +MODE=2
SystemVerilog
iverilog -g2012 -s test_program -o check stack_checks_cpu.sv simulation/test_program.v
vvp check
iverilog -g2012 -s test_program -o check cumulative_shifts_cpu.sv simulation/test_program.v
vvp check +MODE=2
VHDL
nvc --std=2008 -a stack_checks_cpu.vhd simulation/test_program.vhd
nvc --std=2008 -e test_program
nvc --std=2008 -r test_program
nvc --std=2008 -a cumulative_shifts_cpu.vhd simulation/test_program.vhd
nvc --std=2008 -e -gmode=2 test_program
nvc --std=2008 -r test_program
Design your own assembly program
Choose a switch-controlled transformation and write a program to display its result. Use a nested call, preserve the return address and at least one working register on the stack, and leave an observable result on both the LEDs and the displays. Save a copy of simulation/shift_panel.s as my_program.s in the extracted worksheet folder and edit that copy. Assemble your work and insert the image into your repaired HDL.
Verilog
python3 simulation/sbasm.py my_program.s custom.mif
python3 simulation/load_program.py custom.mif starter/stack_cpu.v --output my_stack_cpu.v
SystemVerilog
python3 simulation/sbasm.py my_program.s custom.mif
python3 simulation/load_program.py custom.mif starter/stack_cpu.sv --output my_stack_cpu.sv
VHDL
python3 simulation/sbasm.py my_program.s custom.mif
python3 simulation/load_program.py custom.mif starter/stack_cpu.vhd --output my_stack_cpu.vhd
Keep a copy of the repaired processor. Copy the generated HDL from the last command into the original project source file. Keep its original filename, compile it and upload it using the same workflow. Reset the board before testing your two inputs. Submit your assembly, a stack trace showing two return addresses, and two predicted and observed board readings for inputs of your choice. Explain how either observation could reveal an incorrect return.
Based on FPGAcademy Digital Logic Lab 11, Part VIII. Verilog handout · VHDL handout · MIT license
