You are on page 1of 9

Assignment-5

Name-Saurav Kumar Roll no-23ucc597

1)Full adder
Source code:
module fulladder(
input x,
input y,
input Cin,
output Cout,
output sum
);
assign sum=x^y^Cin;
assign Cout= (x&y)|((x^y)&(Cin));

endmodule

Test Bench:
module fulladder_tb(

);
reg x,y,Cin;
wire Cout,sum;
fulladder dut(.x(x),.y(y),.Cin(Cin),.Cout(Cout),.sum(sum));
integer i;
initial begin
for(i=0;i<8;i=i+1)
begin
{x,y,Cin}=i;
#10;
end
$finish;
end

endmodule

Waveform:

Schematic:
2) RIPPLE ADDER

Source code:
module rippleadder(
input [3:0] x,
input [3:0] y,
input cin,
output cout,
output [3:0] sum
);
wire c1,c2,c3;
fulladder f1(x[0],y[0],cin,c1,sum[0]);
fulladder f2(x[1],y[1],c1,c2,sum[1]);
fulladder f3(x[2],y[2],c2,c3,sum[2]);
fulladder f4(x[3],y[3],c3,cout,sum[3]);
endmodule

Test bench:
module rippleadder_tb(

);
reg [3:0]x;
reg [3:0]y;
reg cin;
wire cout;
wire [3:0]sum;

rippleadder dut(.x(x),.y(y),.cin(cin),.cout(cout),.sum(sum));
integer i;
initial begin
for(i=0;i<512;i=i+1)
begin
{x,y,cin}=i;
#10;
end
$finish;
end
endmodule

Schematic:
Waveform:
3) ALU
Source code:
module alu(
input [7:0] A,
input [7:0] B,
output
reg[7:0]Z,
input [2:0] S
);

always@(*) begin
case(S) 3'b000:Z=8'b0;
3'b001:Z= A&B;
3'b010:Z= A|B;
3'b011:Z= ~A;
3'b100:Z= A^B;
3'b101:Z= A+B;

3'b110:Z=A-B;
3'b111:Z=A*B;

endcase
end

endmodule

Test bench:
module alutb(

);
reg [7:0]A;
reg [7:0]B;
reg [2:0]S;
wire [7:0]Z;
alu dut(.A(A),.B(B),.S(S),.Z(Z));
initial begin
$display("A \t Y \t S \t Z");
A=8'b00000011; B=8'b00000011;S=3'b000;#10;
A=8'b00000111; B=8'b00001011;S=3'b001;#10;
A=8'b00100011; B=8'b00100001;S=3'b010;#10;
A=8'b00101011; B=8'b10000011;S=3'b011;#10;
A=8'b10010011; B=8'b01100011;S=3'b100;#10;
A=8'b00000001; B=8'b00010010;S=3'b101;#10;
A=8'b10000100; B=8'b00000011;S=3'b110;#10;
A=8'b01010011; B=8'b01000001;S=3'b111;#10;

$finish;
end
endmodule
Schematic:
Waveform:

You might also like