Lab 6

Subtract using the accumulator’s adder

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

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 add_sub_accumulator.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 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

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

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.

test_accumulator.v

test_accumulator.vhd

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