Question: My ALU isn't working right and the opcode and enable should be grab by the FSM that i have already made can you fix it

My ALU isn't working right and the opcode and enable should be grab by the FSM that i have already made can you fix it and make a testbench so it works?
module alu (
input [7:0] a,// Operand A
input [7:0] b,// Operand B
input [2:0] opcode, // Operation opcode from FSM
input enable, // Enable signal for ALU operation
output reg [7:0] result, // ALU result
output reg div_by_zero // Flag to indicate division by zero
);
// ALU Operations based on opcode
always @(*) begin
if (enable) begin
div_by_zero =0; // Default to no division by zero error
case (opcode)
3'b000: result = a + b; // Addition
3'b001: result = a - b; // Subtraction
3'b010: result = a * b; // Multiplication
3'b011: begin
if (b ==0) begin
result =8'b0; // Division by zero case
div_by_zero =1; // Set division by zero flag
end else begin
result = a / b; // Division
div_by_zero =0; // No error
end
end
3'b100: result = a & b; // AND
3'b101: result = a | b; // OR
3'b110: result = a <<1; // Shift Left
3'b111: result = a >>1; // Shift Right
default: result =8'b0; // Default to 0(error case)
endcase
end else begin
result =8'b0; // If ALU is not enabled, output 0
div_by_zero =0; // No division by zero error if ALU is disabled
end
end
endmodule

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!