# Lab 8 Name: Manan Chavda Roll No.: CS22B017 --- ## Question 1 ```c= module full_add_testbench; reg a,b,cin; wire sum,cout; Fulladder f1(.a(a),.b(b),.c(cin),.sum(sum),.carry(cout)); initial begin a=1'b0;b=1'b0;cin=1'b0; #100 a=1'b0;b=1'b0;cin=1'b1; #100 a=1'b0;b=1'b1;cin=1'b0; #100 a=1'b0;b=1'b1;cin=1'b1; #100 a=1'b1;b=1'b0;cin=1'b0; #100 a=1'b1;b=1'b0;cin=1'b1; #100 a=1'b1;b=1'b1;cin=1'b0; #100 a=1'b1;b=1'b1;cin=1'b1; end endmodule module Fulladder(a,b,c,sum,carry); input a,b,c; output sum,carry; wire x,y,z; Halfadder h1(.a(a),.b(b),.sum(x),.carry(y)); Halfadder h2(.a(x),.b(c),.sum(sum),.carry(z)); or o1(carry,y,z); endmodule module Halfadder(a,b,sum,carry); input a,b; output carry,sum; assign sum = a^b; assign carry = a*b; endmodule ``` ___ ## Question 2 ```c= module RippleCarryAdder( input [3:0]a,b, input c, output carry, output [3:0]sum); wire c1,c2,c3 Fulladder f1(.a(a[0]),.b(b[0]),.c(c),.sum[0],.carry(c1)); Fulladder f2(.a(a[1]),.b(b[1]),.c(c1),.sum[1],.carry(c2)); Fulladder f3(.a(a[2]),.b(b[2]),.c(c2),.sum[2],.carry(c3)); Fulladder f4(.a(a[3]),.b(b[3]),.c(c3),.sum[3],.carry(carry)); endmodule //As fulladder is used in ripple carry adder we use it here. module Fulladder(a,b,c,sum,carry); input a,b,c; output sum,carry; wire x,y,z; Halfadder h1(.a(a),.b(b),.sum(x),.carry(y)); Halfadder h2(.a(x),.b(c),.sum(sum),.carry(z)); or o1(carry,y,z); endmodule module Halfadder(a,b,sum,carry); input a,b; output carry,sum; assign sum = a^b; assign carry = a*b; endmodule //Testbench: module rca_testbench; reg[3:0] a,b; reg c; wire[3:0] sum; wire c4; rca uut(a,b,c,c4,sum); initial begin c=0; a = 4’b0001;b= 4’b1000;//Here I have taken two cases for a and b not all the cases as said in the question. #100 a = 4’b1111 ; b = 4’b1001; end endmodule ``` ___ ## Question 3 ```c= module Multiplexer(input a, b, s, output u); assign u = (s == 1'b0) ? a : b; endmodule //testbench module MX_testbench; reg a, b, s; wire u; Multiplexer m1(.a(a), .b(b), .s(s), .u(u)); initial begin #100 a = 1'b0; b = 1'b0; s = 1'b0; #100 a = 1'b0; b = 1'b0; s = 1'b1; #100 a = 1'b0; b = 1'b1; s = 1'b0; #100 a = 1'b0; b = 1'b1; s = 1'b1; #100 a = 1'b1; b = 1'b0; s = 1'b0; #100 a = 1'b1; b = 1'b0; s = 1'b1; #100 a = 1'b1; b = 1'b1; s = 1'b0; #100 a = 1'b1; b = 1'b1; s = 1'b1; end endmodule ``` ___