Subtract using the accumulator’s adder
Online version: https://fpga.labsland.com/altera/digital-logic/lab-06/part-02/
Use SW8 to choose addition (0) or subtraction (1). In subtraction mode the circuit computes S − A. The operand A is the value captured at the previous clock edge. A byte waiting on the switches enters the calculation one edge later.
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/add_sub_accumulator.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
add_sub_accumulator. 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/add_sub_accumulator.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.
The starter complements A when SW8 is high. Complete the carry input so the adder computes both operations. Check the flags: LEDR8 and LEDR9 hold the previous operation’s carry and signed overflow. LEDR7…0 previews the next operation.
- Get the starterDownload
add_sub_accumulator.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 carry input in the worksheet below, then complete the design as its build steps describe. Open
add_sub_accumulator.qpfand choose Processing → Start Compilation. Quartus writesoutput_files/add_sub_accumulator.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 carry input
Write 0 − 2 with eight bits using an adder. Which byte replaces the subtracted operand? What must enter the least significant carry input? For unsigned subtraction, a final carry of one means that no borrow was needed.
Build
- In the LabsLand Online Editor, choose DE1-SoC for your HDL and the Standard interface.
- Create
add_sub_accumulator, 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.
The starter complements A when SW8 is high. Complete the carry input so the adder computes both operations. Check the flags: LEDR8 and LEDR9 hold the previous operation’s carry and signed overflow. LEDR7…0 previews the next operation.
Simulate
Extract the worksheet ZIP and run these commands from the folder containing activity.md.
The testbench tries every pair of eight-bit values in addition and subtraction. For subtraction, carry is 1 when the unsigned difference needs no borrow. Overflow tells you whether the signed result fits in eight bits. Run the starter, record the first failing operation and its operands, and repeat after your correction.
Verilog
iverilog -g2005 -s test_accumulator -o bench starter/add_sub_accumulator.v simulation/test_accumulator.v
vvp bench
SystemVerilog
iverilog -g2012 -s test_accumulator -o bench starter/add_sub_accumulator.sv simulation/test_accumulator.v
vvp bench
VHDL
nvc --std=2008 -a starter/add_sub_accumulator.vhd simulation/test_accumulator.vhd
nvc --std=2008 -e test_accumulator
nvc --std=2008 -r test_accumulator
Separate a mode change from a clock edge
Synthesize, upload and reset with KEY0. With SW8 low, enter 02 and press and release KEY1. HEX3…2 now shows A; HEX1…0 shows S. Change the switches to 01 and raise SW8 without touching KEY1. Predict the low LEDs and both register pairs before reading the camera.
| Action | A | S | Preview LEDs (hex) | Carry | Overflow |
|---|---|---|---|---|---|
| Select subtraction without clocking | |||||
| Release KEY1 with 01 on SW7…0 | |||||
| Release KEY1 with 00 on SW7…0 |
Reset and switch back to addition. Clock in 80 and then 01. With the mode now set to subtraction, clock in 00. Explain the flags for 80 − 01 using both signed and unsigned interpretations. Submit the table and your explanation.
Original LabsLand activity following FPGAcademy Digital Logic Lab 6, Part II. Verilog PDF · VHDL PDF · MIT
