Professional Documents
Culture Documents
CS 354-02
Project #1
March 29, 2021
Description
This function takes 3 inputs and produces 1 output. If there are more 0’s than 1’s among the
input values, then the output is 1. Otherwise, the output is 0.
Truth Table
Inputs Output
x y z F
0 0 0 1
0 0 1 1
0 1 0 1
0 1 1 0
1 0 0 1
1 0 1 0
1 1 0 0
1 1 1 0
K-map
module minority_function(x,y,z,F)
input x;
input y;
input z;
output F;
assign F = (x&y)|(y&z)|(z&x);
end module
module conditional_inverter(x, y, F)
input x, y;
output F;
assign F = x? ~ y : y;
endmodule
III. 1-bit Full Adder (Cascading 2 Half Adders)
Description
This full adder is constructed from two half adders by connecting A and B as inputs for the first
half adder, connecting the sum of that to an input for the second half adder, connecting the carry-
in (c_in) to the other input for the second half adder, and then ORing two half adders carry
outputs to produce the final carry output (c_out).
Block Diagram
Verilog Source Code
module half_adder(A,B,sum,carry);
input A,B;
output sum,carry;
xor(sum,A,B);
and(carry,A,B);
endmodule
module full_adder(A,B,c_in,sum,c_out);
input A,B,c_in;
output sum,c_out;
wire s1,c1,c2;
half_adder HA1(A,B,s1,c1);
half_adder HA2(c_in,s1,sum,c2);
or(c_out,c1,c2);
endmodule
module full_adder_tb;
reg A;
reg B;
reg c_in;
wire sum;
wire c_out;
integer i;
full_adder uut (
.A(A),
.B(B),
.c_in(cin),
.sum(sum),
.c_out(c_out)
);
initial begin
A = 0;
B = 0;
c_in = 0;
end
always @ ( A or B or c_in )
begin
for ( i = 0; i < 8; i = i + 1 )
begin
#10 {A, B, c_in} = i;
$monitor( "%d ns: A + B + c_in = %b + %b + %b = c_out sum = %b %b",
$time, A, B, c_in, c_out, sum );
end
#10 $stop;
end
endmodule
Verilog Output
Description
A full adder is a combinational circuit that forms the arithmetic sum of three bits. It consists of 3
inputs: A and B, which represent the two significant bits to be added, and c_in which represents
the carry-in from the previous significant position. It has two outputs: sum which is the
arithmetic sum of A and B which can range from 0-3 and c_out to carry the value in case the
output of sum is 2 or 3, whose binary representation require two digits.
Truth Table
Inputs Outputs
0 0 0 0 0
0 0 1 1 0
0 1 0 1 0
0 1 1 0 1
1 0 0 1 0
1 0 1 0 1
1 1 0 1 1
1 1 1 1 1
K-maps
Gate-level Circuit Diagram (2-Level NAND Logic)
module full_adder (
input A,
input B,
input c_in,
output sum,
output c_out );
endmodule
module testbench;
reg A;
reg B;
reg c_in;
wire sum;
wire c_out;
integer i;
SingleStage uut (
.A(A),
Verilog Source Code
Verilog Output
V. 4-bit Adder/Subtractor
Description
This 4-bit adder/subtractor is constructed by cascading four 1-bit full adders. When the input M
is 0, the circuit acts as an adder. When the input M is 1, the circuit acts as a subtractor.
Block Diagram
Verilog Source Code
module half_adder(A, B, sum, c_out);
input A,B;
output sum,c_out;
assign s = A^B;
assign c_out = A & B;
endmodule
module FourBit_AddSub_TB;
reg [3:0] A;
reg [3:0] B;
reg c_in;
wire [3:0] sum;
wire c_out;
FourBit_AddSub uut (
.A(A),
.B(B),
.c_in(c_in),
.sum(sum),
.c_out(c_out)
);
reg [8:0] i;
initial begin
for (i = 0; i <= 511; i = i + 1) begin
A[3:0] = i[8:5]; B = i[4:1]; c_in = i[0]; #5;
end
end