Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

TEST The verilog Code using a Testbench. I've copied my codes please follow the same stile and modify it so it works. You will need

TEST The verilog Code using a Testbench. I've copied my codes please follow the same stile and modify it so it works. You will need to the perform the operations in the table shown below in your test bench (ALU_tb) and paste the waveforms showing successful operations.
Code:
// ALU
module ALU (input [3:0] alu_in_a, alu_in_b, OPCODE, input Cin, output reg [3:0] Sum, output reg Cout, output OF);
reg [3:0] Bin;
wire [3:0] Bn, S;
wire Co;
com2s C1(alu_in_b, Bn);
ripple_adder_4bit FA4(alu_in_a, Bin, Cin, S, Co, OF);
always @ (*) begin
Bin =4'b000; Sum =4'b0000; Cout ='b0;
case (OPCODE)
//ALU operations
//0001 A+B+Cin : add with Cin
4'b0001 : begin
Bin = alu_in_b; Sum = S; Cout = Co;
end
//0010 A+B : add
4'b0010 : begin
Bin = alu_in_b; Sum = S; Cout = Co;
end
//0011 A-B : sub a from b
4'b0011 : begin
Bin = Bn; Sum = S; Cout = Co;
end
//0100(alu_in_a&alu_in_b) : bitwise NAND
4'b0100 : begin
Sum = ~(alu_in_a & alu_in_b);
end
//0101 ~(alu_in_a|alu_in_b) : bitwise OR
4'b0101 : begin
Sum =(alu_in_a | alu_in_b);
end
//0110 alu_in_a^alu_in_b : bitwise XOR
4'b0110 : begin
Sum = alu_in_a^alu_in_b;
end
//0111 Bitwise NOT a
4'b0111 : begin
Sum = ~alu_in_a;
end
//0111 Bitwise NOT a
4'b0111 : begin
Sum = ~alu_in_a;
end
//1000 Logical Shift a right 1 Bit
4'b1000 : begin
Sum = alu_in_a >>1;
end
default : begin
Sum =0; Cout =0;
end
endcase
end
endmodule
//4 Bit Ripple Carry Adder
module ripple_adder_4bit(input [3:0] A, B, input Cin,output [3:0] Sum, output Cout, OF);
full_adder FA1(Sum[0], Cout1, A[0], B[0], Cin);
full_adder FA2(Sum[1], Cout2, A[1], B[1], Cout1);
full_adder FA3(Sum[2], Cout3, A[2], B[2], Cout2);
full_adder FA4(Sum[3], Cout, A[3], B[3], Cout3);
xor X1(OF, Cout3, Cout);
endmodule
// Twos Compliment
module com2s (input [3:0] B, output [3:0] Bn);
wire [3:0] Bn1;
wire OF;
assign Bn1= ~B;
ripple_adder_4bit FA4(Bn1,4'b0000,1'b1, Bn, Cout, OF);
endmodule
// Full Adder
module full_adder(output S, Cout, input A, B, Cin);
wire Sum1, Cout1, Cout2;
half_adder HA1(Sum1, Cout1, A, B);
half_adder HA2(S, Cout2, Sum1, Cout1);
or O1(Cout, Cout1, Cout2);
endmodule
// Half Adder
module half_adder(output Sum, Cout, input A,B);
assign Sum = A^B;
assign Cout = A&B;
endmodule
Te// Test Bench Lab 2
module tb_alu;
// Inputs
reg [3:0] alu_in_a;
reg [3:0] alu_in_b;
reg [3:0] OPCODE;
reg Cin;
// Outputs
wire [3:0] Sum;
wire Cout;
wire OF;
// ALU module
ALU uut (.alu_in_a(alu_in_a),.alu_in_b(alu_in_b),.OPCODE(OPCODE),.Cin(Cin),.Sum(Sum),
.Cout(Cout),.OF(OF));
// Addition
initial begin
alu_in_a =4'b0011;
alu_in_b =4'b0011;
OPCODE =4'b0010;
Cin =1'b0;
#5;
$display("Sum =%b", Sum); // Output: 0110(#6)
// Addition With Cin
alu_in_a =4'b0110;
alu_in_b =4'b0101;
OPCODE =4'b0001;
Cin =1'b0;
#5;
$display("Sum =%b", Sum); // Output: 1011(#1)
// Subtraction
alu_in_a =4'b0111;
alu_in_b =4'b0110;
OPCODE =4'b0011;
Cin =1'b0;
#5;
$display("Sum =%b", Sum); // Output: -1
// Bitwise NAND
alu_in_a =4'b0111;
alu_in_b =4'b1010;
OPCODE =4'b0100;
Cin =1'b0;
#5;
$display("Sum =%b", Sum);
// Bitwise OR
alu_in_a =4'b0111;
alu_in_b =4'b0011;
OPCODE =4'b0101;
Cin =1'b0;
#5;
$display("Sum =%b", Sum);
// Bitwise XOR
alu_in_a =4'b0101;
alu_in_b =4'b1110;
OPCODE =4'b0110;
Cin =1'b0;
#5;
$display("Sum =%b", Sum);
// Bitwise NOT
alu_in_a =4'b1011;
alu_in_b =4'b0000;
OPCODE =4'b0111;
Cin =1'b0;
#5;
$display("Sum =%b", Sum);
// Logical Right shift
alu_in_a =4'b0101;
alu_in_b =4'b0000;
OPCODE =4'b1000;

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Progress Monitoring Data Tracking Organizer

Authors: Teacher'S Aid Publications

1st Edition

B0B7QCNRJ1

More Books

Students also viewed these Databases questions