Professional Documents
Culture Documents
4-bit Adder-Subtractor
Objectives:
Learn the concept of hierarchical design
Implement and simulate the 1-bit adder, 4-bit adder-subtractor and test its performance
What to do?
In this project, we will build a circuit to do adding and subtraction of 4-bit numbers and display the result.
As shown in the following code, we can use the module name FullAdd to describe the 1-bit full adder used in the circuit.
This module can be directly used to build the 4-bit adder. As shown in the following code, we can use the module name
FullAdd to describe the 1-bit full adder used in the circuit.
Cin
A
Sum
B FA
Cout
Part 1 Screenshots
1
2
Part Two: 4-bit Full adder
We can see that for the 1st 1-bit FullAdd, Cin is 0. For the rest of the 1-bit FullAdd, their Cin is the carryout from the
previous FullAdd (just as shown in Figure 2).
3
Figure 2. Build 4-bit adder using 1-bit FA
Part 2 Screenshot
4
5
6
Part Three: Simulate your 4-bit Adder-Subtractor
Write the Verilog Code to describe the 4-bit Adder/Subtractor based on the circuit learned in class (check your notes). You
can call the module AddSub as shown below.(For subtractor use 2’s complement).
7
8
Figure 3. Build 4-bit adder-Subtractor using 1-bit FA
Using the method from Project 2, you can provide different input values in the test bench code to test your circuit. A 200
ns delay is required between each input change.
For example we can add 4 + 3 using this code:
Note that initially x and y have 0’s in each of their 4 bits, so we only have to modify a single bit in x and 2 bits in y to
change their values to 4 and 3, respectively. We also could have directly set x and y to 4 and 3 using this code:
9
You can try a few simple values to make sure that your design works. To run simulation, first save your testbench (you
shall see a “test” showing up in the source window once you save the test bench). Highlight the test in the source window,
then click on Simulator Simulate Behavioral model, you should see the simulation results showing up.
Part 3 Screenshot
10
11
12
In-Class Project 4 – Worksheet: 4-bit Adder-Subtractor
module AddSub(
input [3:0] x,
input [3:0] y,
input Select,
output [3:0] Result,
output Cout
);
endmodule
module AddSubSim(
);
reg [3:0]x, y;
reg Select;
wire [3:0]Result;
wire Cout;
13
initial begin
x=0; y=1; Select =0; #100;
x=5; y=9; Select=0 ; #100;
x=7; y=8; Select=0 ; #100;
x=1; y=0; Select=1; #100;
x=9; y=9; Select=1; #100;
x=1; y=2; Select=1; #100;
end
endmodule
14
In-Class Project 4 – Worksheet 2: 4-bit Adder-Subtractor
(Board Testing) Play with your board to record the following calculation result:
15
Select X y Displayed result Cout Correct or not? (judge if there is
overflow if x and y are signed
numbers)
0 0 1 1(0001) 0 Correct, No overflow
Questions:
Overflow occurs when the resulting value of an operation performed on a valid representation of
numbers is out of the range of the valid values. The sum of two identically signed numbers may
very well exceed the range of the bit field of those two numbers, so overflow is a possibility in
this case.
4. (True/False) Overflow cannot occur when subtracting numbers with the same sign.
True
16
17