Embedded File
Embedded File
Group-4CO19
Roll No.-102103539
To study and verify the truth table of various logic gates (NOT, AND, OR, NAND,
NOR, EX-OR, & EX-NOR).
Code :
module logic_gates_test;
// Declare inputs
reg a, b;
wire not_a;
or U3 (or_ab, a, b);
initial begin
$display("---------------------------------");
a = 0; b = 0; #10 display_values();
a = 0; b = 1; #10 display_values();
a = 1; b = 0; #10 display_values();
a = 1; b = 1; #10 display_values();
$finish;
end
task display_values;
endtask
endmodule
OUTPUT :
Code :
module half_adder (
);
assign C = x & y; // C = xy
endmodule
module half_adder_tb;
half_adder uut (
.x(x),
.y(y),
.S(S),
.C(C)
);
initial begin
$display("-------------");
$finish;
end
endmodule
Code :
module full_adder (
);
assign S = (~x & ~y & z) | (~x & y & ~z) | (x & ~y & ~z) | (x & y & z);
endmodule
Test-Bench Code :
module full_adder_tb;
full_adder uut (
.x(x),
.y(y),
.z(z),
.S(S),
.C(C)
);
initial begin
$display("---------------");
$finish;
end
endmodule
Experiment 4 (Half Subtractor)
Code :
module half_subtractor (
input x, // Minuend
input y, // Subtrahend
);
TestBench Code
module half_subtractor_tb;
half_subtractor uut (
.x(x),
.y(y),
.D(D),
.B(B)
);
initial begin
$display("-------------");
$finish;
end
endmodule
Code :
module bcd_to_excess3 (
input A, B, C, D, // BCD input
);
assign X = (~B & D) | (~B & C) | (B & ~D); // X = B'D + B'C + BD'
assign Y = C ^ D; // Y = C XOR D
endmodule
module bcd_to_excess3_tb;
bcd_to_excess3 uut (
.A(A),
.B(B),
.C(C),
.D(D),
.W(W),
.X(X),
.Y(Y),
.Z(Z)
);
initial begin
$display(" A B C D | W X Y Z ");
$display("--------------------------");
$finish;
end
endmodule
Experiment 6 (Multiplexer)
Code :
module mux_4_to_1 (
output Y // Output
);
assign Y = (~S1 & ~S0 & I0) | (~S1 & S0 & I1) | (S1 & ~S0 & I2) | (S1 & S0 & I3);
endmodule
Test-Bench Code
module mux_4_to_1_tb;
mux_4_to_1 uut (
.I0(I0),
.I1(I1),
.I2(I2),
.I3(I3),
.S0(S0),
.S1(S1),
.Y(Y)
);
initial begin
$display("S1 S0 | I0 I1 I2 I3 | Y ");
$display("-----------------------");
I0 = 1; I1 = 0; I2 = 1; I3 = 0;
end
endmodule
Experiment 7 (Demultiplexer)
Code :
module demux_1_to_4 (
);
endmodule
module demux_1_to_4_tb;
demux_1_to_4 uut (
.D(D),
.S0(S0),
.S1(S1),
.Y0(Y0),
.Y1(Y1),
.Y2(Y2),
.Y3(Y3)
);
initial begin
$display("S1 S0 | D | Y0 Y1 Y2 Y3");
$display("-----------------------");
D = 1;
$finish;
end
endmodule
Experiment 8 (Decoder)
Code :
module decoder_2_to_4 (
input A, B, // Inputs
);
assign Y1 = ~A & B;
assign Y3 = A & B;
endmodule
module decoder_2_to_4_tb;
reg A, B; // Inputs
decoder_2_to_4 uut (
.A(A),
.B(B),
.Y0(Y0),
.Y1(Y1),
.Y2(Y2),
.Y3(Y3)
);
initial begin
$display("A B | Y0 Y1 Y2 Y3");
$display("-----------------");
$finish;
end
endmodule
Experiment 9 (Encoder)
Code :
module encoder_4_to_2 (
);
assign Y1 = I2 | I3;
endmodule
module encoder_4_to_2_tb;
encoder_4_to_2 uut (
.I0(I0),
.I1(I1),
.I2(I2),
.I3(I3),
.Y0(Y0),
.Y1(Y1)
);
initial begin
$display("I3 I2 I1 I0 | Y1 Y0");
$display("-------------------");
$finish;
end
endmodule
Experiment 10 (Flip-Flops )
Code :
module d_flip_flop (
);
end
endmodule
module d_flip_flop_tb;
d_flip_flop uut (
.D(D),
.CLK(CLK),
.Q(Q),
.Qn(Qn)
);
// Clock generation
initial begin
CLK = 0;
end
// Test sequence
initial begin
D = 1; #10;
D = 0; #10;
D = 1; #10;
$finish;
end
endmodule
Experiment 11 (Flip-Flops )
Code :
module jk_flip_flop (
input J, // J input
input K, // K input
);
if (J == 0 && K == 0) begin
Q <= Q; // No change
end
Q <= 0; // Reset
end
Q <= 1; // Set
end
end
end
endmodule
module jk_flip_flop_tb;
reg J; // J input
reg K; // K input
jk_flip_flop uut (
.J(J),
.K(K),
.CLK(CLK),
.Q(Q),
.Qn(Qn)
);
// Clock generation
initial begin
CLK = 0;
// Test sequence
initial begin
// Initialize inputs
J = 0; K = 0; #10;
J = 0; K = 1; #10;
J = 1; K = 0; #10;
J = 1; K = 1; #10;
J = 0; K = 0; #10;
J = 1; K = 1; #10;
$finish;
end
endmodule
Experiment 12 (Counter)
Code :
module t_flip_flop (
);
if (T)
end
endmodule
module async_counter (
);
assign Q = {Q3, Q2, Q1, Q[0]}; // Concatenate the outputs of the flip-flops to form the
4-bit counter
endmodule
module async_counter_tb;
async_counter uut (
.CLK(CLK),
.Q(Q)
);
// Clock generation
initial begin
CLK = 0;
end
// Test sequence
initial begin
#100;
$finish;
end
endmodule