Lab 11

Build and test a processor with a stack

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

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 stack_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

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

  1. In the LabsLand Online Editor, choose DE1-SoC for your HDL and the Standard interface.
  2. Create stack_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.

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.

test_stack.v

test_stack.vhd

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.

test_program.v

test_program.vhd

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