4-bit Serial Adder/Subtractor with Parallel Load

This is a tutorial I wrote for the "Digital Systems Design" course as an introduction to sequential design. "4-bit Serial Adder/Subtractor with Parallel Load" is a simple project which may help to understand use of variables in the "process" statement in VHDL. However, basic understanding of the circuits is necessary, so both schematics and VHDL implementations are given. All code is written for Basys2 development board and Xilinx ISE was used as a synthesizer/simulator.

The Circuit

A 4-bit serial adder circuit consists of two 4-bit shift registers with parallel load, a full adder, and a D-type flip-flop for storing carry-out. A simplified schematics of the circuit is shown below:

Simplified schematics of the 4-bit serial adder with parallel load. Two right-shift registers with parallel load, “A” and “B”; a full adder FA, and a D-type flip-flop for storing carry-out are used.

In order to load registers A_REG and B_REG with numbers, shift capability of the registers should be disabled and loading mode should be enabled. Loading of numbers from inputs A, B to registers A_REG, B_REG occurs in one clock cycle. After loading registers with numbers, shifting mode should be enabled to perform the arithmetic operation. The addition of numbers stored in A_REG and B_REG requires 4 cycles. Starting with the least significant bit, at each cycle one bit of number A and one bit of number B are being added. The sum is stored at the most significant bit of register A_REG. Carry-out output produced after each cycle is fed back to the full adder as a carry-in of the next significant bit. For this purpose one D-type flip-flop is used as a temporary storage element. The least significant bit of B_REG is fed to the input of the most significant bit of B_REG. Hence the circuit performs rotation operation for register B_REG.

Schematic Design in Xilinx ISE

Clone the project and checkout commit 5c385071530140074c8aa53dc40297b752ab0bd7:

Newer version of the code (commit 92c9460c533a0748104cbfb56988732b5c4095b8) contains 7-segment display and a bus, which groups individual bits of numbers A and B. The new version is not covered in this tutorial.

Create a new project with name "FourBitSerialAdderSubtractorSCH" and add exisiting source files from the archive provided:

  • Schematics/FourBitSerialAdderSubtractor.sch
  • Schematics/FullAdder.sch
  • Schematics/Basys2.ucf
  • Schematics/FourBitSerialAdderSubtractorSimulation.vhw

If you click on "FourBitSerialAdderSubtractor.sch" file in the top design, you will see the circuit of the 4-bit serial adder/subtractor with parallel load as shown below:

Schematics of the 4-bit serial adder/subtractor with parallel load drawn in Xilinx ISE. Number "B" can be negated in two’s complement form allowing subtraction operation mode.

Number "B" can be negated in two’s complement form allowing subtraction operation mode. The symbols labeled with "M2_1" are 2-to-1 multiplexers. "FD"s are D-type flip-flops. Full adder circuit is used as a module "FullAdder.sch". Its schematics is given below:

Schematics of the full adder module. The module is used in the top 4-bit serial adder/subtractor design.

If you press on "Generate Programming File" under "Processes" panel, you will get an error:

In order to overcome this, you should create a symbol for the full adder module by going to "Sources" -> "Implementation" and choosing the "FA - FullAdder" line under "FourBitSerialAdderSubtractor" top design. After selecting it, expand "Design Utilities" section and press on "Create Schematic Symbol". The procedure is shown below:

Now you have to update all schematic files. Go to device "Sources" -> "Implementation" and choose the "xc3s100e-5cp132" device. After selecting it, expand "Design Utilities" section and press on "Update All Schematic Files". The procedure is shown below:

The files are ready to use now. "FourBitSerialAdderSubtractorSimulation.vhw" is a VHDL file needed to simulate the circuit behavior. This simulates addition of two numbers: A=3 and B=1 and their subsequent subraction. You may also create a test bench waveform to simulate the circuit behavior instead. As explained in figure below the result of the arithmetical operation (addition/subtraction depending on the mode) will appear in register A_REG after 4 cycles:

 

Design Using VHDL

Clone the project:

Create a new project with name "FourBitSerialAdderSubtractorVHDL" and add exisiting source files from the downloaded directory:

  • VHDL/FourBitSerialAdderSubtractor.vhd
  • VHDL/FourBitSerialAdderSubtractorSimulation.vhw
  • VHDL/Basys2.ucf

The code of the 4-bit serial adder/subtractor is given in "FourBitSerialAdderSubtractor.vhd". Sequential part requires using "process()" statement:

A process has a "sensitivity list" given as input arguments. Whenever the value of one of the signals in the sensitivity list changes, the process is executed. For example, "process(CLK)" means that whenever the clock signal "CLK" changes (it can be both rising and falling edge), the process is executed.

If-then-else statements can be used only within the process statements. Using if-then-else statements outside the process will yield an error. Positive-edge-triggered D-type flip-flop is implemented by checking the rising edge of the clock signal using if-then-else statement. A typical implementation of the D-type flip-flop is given below:

There is a big difference between a signal and a variable. A variable is defined in the beginning of the process and has an immediate behavior. The assignment of the value to the variable occurs immediately with no delay:

Signal on the other hand can be considered as a flip-flop (or a register). The assignment of the value to the signal (i.e. signal transfer) occurs only after one clock cycle:

This may be a reason for many bugs.

The code of the 4-bit serial adder/subtractor with parallel load is given below:

"Sum" and "CarryOut" are part of the full adder circuit. Number "B" is rotated in register B_Reg. Simulation using "FourBitSerialAdderSubtractorSimulation.vhw" file shows the same behavior as with the schematics version of the project:

After checking the pinouts of the circuit in the floorplan area or by editing the UCF-file which is given in "Basys2.ucf", the bit-file for flashing Basys2 board can be generated. Press on the "Generate Programming File" button. If the compilation process ends successfully, flash the Basys2 board with the generated bit-file using Adept program.

To test the circuit, firstly turn on and off proper switches which set numbers A and B. Then press on the "Load" button and keep holding. Click on the "CLK" button once. The numbers will be loaded into the registers. Release "Load" button and press on the "CLK" button 4 times. The result of the addition should appear on the LEDs. To perform subtraction, keep holding both "Mode" and "Load" buttons and click on the "CLK" button. Release all buttons and press on the "CLK" button 4 times. The result of the subtraction should appear on the LEDs. Note that the VHDL version of the circuit has a seven-segment display to show the content of register A_REG.

Demo: adding A and B, where A=2, B=3, the result 5 is shown after four clock cycles:

Demo: subtracting B from A, where A=4, B=1, the result 3 is shown after four clock cycles: