Diagnose a conditional branch with the board
Online version: https://fpga.labsland.com/altera/digital-logic/lab-10/part-05/
A program can report which instruction sent it down the wrong path. Build a processor with zero, negative and carry flags. Its diagnostic prints a word on the displays and either a test count or an instruction address on the LEDs.
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/branch_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
branch_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/branch_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.
First synthesize and upload the starter unchanged. Lower SW9, pulse KEY0 once, then raise SW9. Read HEX5 through HEX0 and the ten red LEDs from the camera. Convert the LED pattern to a hexadecimal word address.
- Get the starterDownload
branch_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 Read the flags in the worksheet below, then complete the design as its build steps describe. Open
branch_cpu.qpfand choose Processing → Start Compilation. Quartus writesoutput_files/branch_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
Read the flags
Sketch what happens at fetch, when the condition is checked and when the target reaches the program counter. For an instruction at word 0x11 that branches forward by 0x2C, find the target using the PC after fetch. How would a nine-bit offset of 0x1FF change that PC?
Predict Z, N and C after adding 1 to 0xFFFF and after subtracting 1 from zero. For subtraction, a carry of 1 means no borrow. Explain which flags a branch may read and which it should preserve.
Build
- In the LabsLand Online Editor, choose DE1-SoC for your HDL and the Standard interface.
- Create
branch_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.
First synthesize and upload the starter unchanged. Lower SW9, pulse KEY0 once, then raise SW9. Read HEX5 through HEX0 and the ten red LEDs from the camera. Convert the LED pattern to a hexadecimal word address.
Find the failing instruction
Assemble the supplied diagnostic program. Open the resulting text file and find the row corresponding to the address on your LEDs. Read the preceding ALU instruction. Decide whether this branch should have been taken, then correct the relevant condition in the 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.
python3 simulation/sbasm.py simulation/branch_diagnostic.s diagnostic.mif
The embedded program already matches the supplied assembly. If you change the assembly, insert the new image using the RAM loader and a copy of your repaired HDL.
Simulate
Extract the worksheet ZIP and run these commands from the folder containing activity.md.
This bench runs the diagnostic inside the processor, checks the six active-low pin patterns and reads all ten LED bits. With the starter it reports the failing address. With the repair it checks the success count and held result. Match the hexadecimal address after 0x to the address column in diagnostic.mif. Check the instruction on that row against the ten LEDs you read on the board.
Verilog
iverilog -g2005 -s test_branch -o bench starter/branch_cpu.v simulation/test_branch.v
vvp bench
SystemVerilog
iverilog -g2012 -s test_branch -o bench starter/branch_cpu.sv simulation/test_branch.v
vvp bench
VHDL
nvc --std=2008 -a starter/branch_cpu.vhd simulation/test_branch.vhd
nvc --std=2008 -e test_branch
nvc --std=2008 -r test_branch
Confirm the repair
Rebuild and upload the repaired processor. Repeat the same reset and run sequence. Lower SW9 and record what the displays and LEDs retain.
| Design | Six display characters | LEDs in hex | What the LED value identifies |
|---|---|---|---|
| Before repair | |||
| After repair |
Submit the failing instruction, its address and predicted target, the flag values that explain the failure, and both board readings. Identify one branch that fell through and one that was taken in the repaired program.
FPGAcademy supplied the diagnostic assembly and processor scaffold under the MIT license. Verilog handout · VHDL handout · MIT license
