DDCO Lab_Solution 5&6
DDCO Lab_Solution 5&6
5. Design Verilog HDL to implement 4-bit binary adder. (If time permits implement the 4-bit
binary and decimal adder/Binary coded decimal(BCD) adder)
module adder_4bit(
input [3:0] a,
input [3:0] b,
input c0,
output [3:0] sum,
output carry_out
);
endmodule
half_adder h1 (.a_ha(a_fa),.b_ha(b_fa),.sum_ha(temp1),.carry_ha(temp3));
half_adder h2 (.a_ha(temp1),.b_ha(c_in_fa),.sum_ha(sum_fa),.carry_ha(temp2));
endmodule
endmodule
Testbench for 4-bit binary adder:
module tb_binary;
// Inputs
reg [3:0] a;
reg [3:0] b;
reg carry_in;
// Outputs
wire [3:0] sum;
wire carry;
initial begin
// Apply Inputs
a = 0; b = 0; carry_in = 0; #10;
a = 6; b = 9; carry_in = 0; #10;
a = 3; b = 3; carry_in = 1; #10;
a = 4; b = 5; carry_in = 0; #10;
a = 8; b = 2; carry_in = 0; #10;
a = 9; b = 9; carry_in = 1; #10;
end
endmodule
Truth table: Binary to decimal/BCD converter.
Fig. Block diagram of a decimal/BCD adder
module bcd_adder(a,b,carry_in,sum,carry);
endmodule
Testbench for Decimal/BCD adder:
module tb_bcdadder;
// Inputs
reg [3:0] a;
reg [3:0] b;
reg carry_in;
// Outputs
wire [3:0] sum;
wire carry;
initial begin
// Apply Inputs
a = 0; b = 0; carry_in = 0; #10;
a = 6; b = 9; carry_in = 0; #10;
a = 3; b = 3; carry_in = 1; #10;
a = 4; b = 5; carry_in = 0; #10;
a = 8; b = 2; carry_in = 0; #10;
a = 9; b = 9; carry_in = 1; #10;
end
endmodule
6. (a) Design Verilog program to implement different types of multiplexers like 2:1 and 4:1.
Boolean Expression:
Fig. 2:1 Multiplexer (symbol) Fig. 2:1 Multiplexer (logical)
𝑌 = 𝑠𝑒𝑙 . 𝐴 + 𝑠𝑒𝑙. 𝐵
Table. Truth table of 2:1 Multiplexer
Verilog Code for 2:1 multiplexer using assign statement: Selection Output
Input (sel) (Y)
module mux_assign( 0 Y=A
input a_in,
input b_in, 1 Y=B
input sel_in,
output y_out
);
Testbench:
module mux_assign_tb();
initial
begin
a_t=1'b0; b_t=1'b1; sel_t=1'b0;
#10;
a_t=1'b0; b_t=1'b1; sel_t=1'b1;
#10 $stop;
end
endmodule
Fig. 4:1 Multiplexer (TT and expr’n) Fig. 4:1 Multiplexer (logical)