Question: For some reason a keep getting added 2 like it suppose to be 1 0 + 5 = 1 5 but I get 1 2

For some reason a keep getting added 2 like it suppose to be 10+5=15 but I get 12+5=17
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
div_by_zero =0; // Default to no division by zero
result =8'b0; // Default to zero result
if (enable) begin
case (opcode)
3'b000: result = a+b; // Addition
3'b001: result = a-b; // Subtraction
3'b010: result = a*b; // Multiplication
3'b011: begin // Division
if (b ==0) begin
result =8'b0;
div_by_zero =1;
end else begin
result = a/b;
end
end
3'b100: result = a&b; // AND
3'b101: result = a|b; // OR
3'b110: result = a1; // Shift Left
3'b111: result = a>>1; // Shift Right
default: result =8'b0; // Default case
endcase
end
end
endmodule
module alu_tb;
reg [7:0] a; // Test Operand A
reg [7:0] b; // Test Operand B
reg [2:0] opcode; // Test Operation opcode
reg enable; // Test Enable signal
wire [7:0] result; // ALU result
wire div_by_zero; // Division by zero flag
// Instantiate the ALU module
alu dut (
.a(a),
.b(b),
.opcode(opcode),
.enable(enable),
.result(result),
.div_by_zero(div_by_zero)
);
initial begin
// Test cases
// Test Addition
a =8'd10; b =8'd5; opcode =3'b000; enable =1;
#10;
// Test Subtraction
a =8'd15; b =8'd5; opcode =3'b001; enable =1;
#10;
// Test Multiplication
a =8'd3; b =8'd4; opcode =3'b010; enable =1;
#10;
// Test Division (No Error)
a =8'd20; b =8'd4; opcode =3'b011; enable =1;
#10;
// Test Division (Division by Zero)
a =8'd20; b =8'd0; opcode =3'b011; enable =1;
#10;
// Test AND
a =8'd170; b =8'd204; opcode =3'b100; enable =1; // Binary: a=10101010, b=11001100
#10;
// Test OR
a =8'd170; b =8'd204; opcode =3'b101; enable =1; // Binary: a=10101010, b=11001100
#10;
// Test Shift Left
a =8'd15; b =8'd0; opcode =3'b110; enable =1; // Binary: a=00001111
#10;
// Test Shift Right
a =8'd240; b =8'd0; opcode =3'b111; enable =1; // Binary: a=11110000
#10;
// Test ALU Disabled
a =8'd50; b =8'd25; opcode =3'b000; enable =0;
#10;
$finish;
end
endmodule
For some reason a keep getting added 2 like it

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock

I can see youve uploaded a file but it seems youre referring to a question or an issue regarding an ALU design in Verilog and youre wondering why an i... View full answer

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!