assignmnt_code
assignmnt_code
module ALU_4bit (
input [3:0] A, // 4-bit input A
input [3:0] B, // 4-bit input B
input [2:0] ALU_Control, // 3-bit ALU control signal
output reg [3:0] ALU_Result, // 4-bit result
output reg Zero // Zero flag
);
// ALU_Control[2:0] -> 000: ADD, 001: SUB, 010: AND, 011: OR, 100: XOR, 101: LSHIFT, 110: RSHIFT
always @ (A, B, ALU_Control)
begin
case (ALU_Control)
3'b000: ALU_Result = A + B; // Addition
3'b001: ALU_Result = A - B; // Subtraction
3'b010: ALU_Result = A & B; // AND
3'b011: ALU_Result = A | B; // OR
3'b100: ALU_Result = A ^ B; // XOR
3'b101: ALU_Result = A << 1; // Left Shift
3'b110: ALU_Result = A >> 1; // Right Shift
default: ALU_Result = 4'b0000; // Default case
endcase
endmodule
TESTBENCH
module ALU_4bit_tb;
// Inputs
reg [3:0] A;
reg [3:0] B;
reg [2:0] ALU_Control;
// Outputs
wire [3:0] ALU_Result;
wire Zero;
// Test procedure
initial begin
// Test Addition
A = 4'b0011; B = 4'b0001; ALU_Control = 3'b000; #10;
// Test Subtraction
A = 4'b1010; B = 4'b0100; ALU_Control = 3'b001; #10;
// Test AND
A = 4'b1100; B = 4'b1010; ALU_Control = 3'b010; #10;
// Test OR
A = 4'b1100; B = 4'b1010; ALU_Control = 3'b011; #10;
// Test XOR
A = 4'b1100; B = 4'b1010; ALU_Control = 3'b100; #10;
// Test Left Shift
A = 4'b0110; B = 4'b0000; ALU_Control = 3'b101; #10;
// Test Right Shift
A = 4'b0110; B = 4'b0000; ALU_Control = 3'b110; #10;
// Finish simulation
$finish;
end
endmodule
1)
2)
3)
4)
5)
6)
7)
SYNTHESIS
SCHEMATIC