Design of Combinational Logic: Full Adder, Adder, and ALU: 1 Today's Goal
Design of Combinational Logic: Full Adder, Adder, and ALU: 1 Today's Goal
Step 2 Check1 Perform the simulation using the test bench List 2: Test benchfa tb.v for fa.v
to confirm that the full adder works correctly. 1 ‘timescale 1ns / 1ps
2 module fa tb;
Step 3 Check2 Write a UCF(User Constraint File) (List 3), 3
embed it in the FPGA to confirm that the full adder 4 reg a,b,cin;
works in the FPGA correctly. 5 wire s, cout;
6 fa fa0 (.a(a), .b(b), .cin(cin), .s(s), .cout(cout));
7
Step 4 Write full adder using always statement(List 4) and 8 initial begin
confirm that it works correctly by the simulation using 9 a = 0; b = 0; cin = 0;
the test bench(List 2). 10 #100 a = 1; b = 0; cin = 0;
11 #100 a = 0; b = 1; cin = 0;
12 #100 a = 1; b = 1; cin = 0;
Step 5 Check 3 Write 4-bit adder using 4-full adders(List 5) 13 #100 a = 0; b = 0; cin = 1;
and confirm that it works correctly by the simulation 14 #100 a = 1; b = 0; cin = 1;
using its test bench(List 6). 15 #100 a = 0; b = 1; cin = 1;
16 #100 a = 1; b = 1; cin = 1;
17 #100 a = 0; b = 0; cin = 0;
Step 6 Check 4 Write ALU(List 7) and confirm that it works 18 end
correctly by the simulation using its test bench(List 8). 19 endmodule
3 Full adder
Full adder has 3 input bits a,b, cin and 2 output bits s, cout. 5 UCF(User Constraint file)
The sum of 3 input bits are computed and 2 out bits represent
the sum such that s is a lower bit, and count is an upper bit. The UCF defines the mapping between ports of the module
Assignment statements defines continuous assignments. and the pins of an FPGA. NET and LOC correspond to a
1
name of module port, and a name of FPGA pin.
6 Always statement
Always statements in List 4 is used to design combinational
logic. “always @ (...)” defines a event list. If the values of
signal (net) in the event list change, the following statement
is executed.
2
8 ALU
ALU (List 7) is used to compute a selected function. ALU List 7: ALU alu.v
has 3 input ports, f(5 bits), a(16 bits), b(16 bits), and one 1 ‘define ADD 5’b00000
output port s. f is used to select a function (operation), and 2 ‘define SUB 5’b00001
the resulting value is output from s. We assume that a, b, 3 ‘define MUL 5’b00010
s are signed integers (2’s complement). However, array of 4 ‘define SHL 5’b00011
5 ‘define SHR 5’b00100
bits (vector) in Verilog HDL is handled as unsigned integers. 6 ‘define BAND 5’b00101
Thus, for relational operators, we add 16’h8000 to a and b to 7 ‘define BOR 5’b00110
get correct results. 8 ‘define BXOR 5’b00111
9 ‘define AND 5’b01000
10 ‘define OR 5’b01001
11 ‘define EQ 5’b01010
9 Homeworks 12 ‘define NE 5’b01011
13 ‘define GE 5’b01100
14 ‘define LE 5’b01101
In your report, you must show enough explanation and the 15 ‘define GT 5’b01110
simulation results. 16 ‘define LT 5’b01111
17 ‘define NEG 5’b10000
Homework 1 Design an 8-bit adder using 8 full adders, and 18 ‘define NOT 5’b10001
write its test bench. Perform the simulation to confirm 19 ‘define BNOT 5’b10010
20
that the 8-bit adder works correctly. 21 module alu(a, b, f, s);
22
Homework 2 Write test benches for ALU to confirm that 23 input [15:0] a, b;
each of 19 functions works correctly. You should choose 24 input [4:0] f;
25 output [15:0] s;
various input b and a for each functin. For example, 26 reg [15:0] s;
for bianry arithmetic function, you should choose {b > 27 wire [15:0] x,y;
0, b < 0} × {a > 0, a < 0} (4 cases), and the case that 28
the reult is overflow. 29 assign x = a + 16’h8000;
30 assign y = b + 16’h8000;
31
32 always @(a or b or x or y or f)
33 case(f)
34 ‘ADD : s = b + a;
35 ‘SUB : s = b − a;
36 ‘MUL : s = b ∗ a;
37 ‘SHL : s = b << a;
38 ‘SHR : s = b >> a;
39 ‘BAND: s = b & a;
40 ‘BOR : s = b | a;
41 ‘BXOR: s = b ˆ a;
42 ‘AND : s = b && a;
43 ‘OR : s = b || a;
44 ‘EQ : s = b == a;
45 ‘NE : s = b != a;
46 ‘GE : s = y >= x;
47 ‘LE : s = y <= x;
48 ‘GT : s = y > x;
49 ‘LT : s = y < x;
50 ‘NEG : s = −a;
51 ‘BNOT : s = ˜a;
52 ‘NOT : s = !a;
53 default : s = 16’hxxxx;
54 endcase
55
56 endmodule
3
Table 1: Specification of ALU(Arithmetic and Logic Unit)
function f outputs
binary arithmetic ADD 00000 b + a (addition)
SUB 00001 b − a (subtraction)
MUL 00010 b * a (multiplication)
shift SHL 00011 b << a (left shift)
SHR 00100 b >> a (right shift)
bitwise BAND 00101 b & a (bitwise and)
BOR 00110 b | a (bitwise or)
BXOR 00111 b ^ a (bitwise xor)
logic AND 01000 b && a (logical and)
OR 01001 b || a (logical or)
relational EQ 01010 b==a (b is equal toa)
NE 01011 b!=a (b is not equal toa)
GE 01100 b>=a (b is larger than or euqal to a)
LE 01101 b<=a (b is smaller than or equal to a)
GT 01110 b>a (b is larger than a)
LT 01111 b<a (b is smaller than a)
unary arithmetic NEG 10000 −a (negation)
bitwise BNOT 10001 ~a (bitwise not)
logic NOT 10010 !a (logical not)