2024-01-12 13:44:33 +01:00
|
|
|
/*
|
|
|
|
A and B are inputs, O is out
|
|
|
|
OP is the operation to perform:
|
|
|
|
000 = add
|
|
|
|
001 = sub
|
2024-01-17 12:24:33 +01:00
|
|
|
010 = mul_lo
|
|
|
|
011 = mul_hi
|
2024-01-12 13:44:33 +01:00
|
|
|
(011)
|
|
|
|
100 = and
|
|
|
|
101 = or
|
|
|
|
110 = xor
|
|
|
|
111 = not A
|
|
|
|
*/
|
|
|
|
module alu(
|
|
|
|
input [31:0] A,
|
|
|
|
input [31:0] B,
|
|
|
|
input [2:0] op,
|
|
|
|
output [31:0] O,
|
2024-01-12 20:45:05 +01:00
|
|
|
output Fflow,
|
2024-01-12 13:44:33 +01:00
|
|
|
output Fzero
|
|
|
|
);
|
|
|
|
|
2024-01-13 00:16:28 +01:00
|
|
|
wire [31:0] adder_out;
|
2024-01-12 13:44:33 +01:00
|
|
|
|
2024-01-13 00:16:28 +01:00
|
|
|
begin
|
|
|
|
wire subtraction = op == 3'b001;
|
|
|
|
wire [31:0] adder_B = subtraction ? ~B : B;
|
|
|
|
wire adder_cout;
|
|
|
|
|
|
|
|
carry_select_adder a(A, adder_B, subtraction, adder_out, adder_cout);
|
|
|
|
assign Fflow = subtraction ? ~adder_cout : adder_cout;
|
|
|
|
end
|
|
|
|
|
2024-01-17 12:24:33 +01:00
|
|
|
wire [31:0] mult_out_hi;
|
|
|
|
wire [31:0] mult_out_lo;
|
|
|
|
multiplier mult(A, B, mult_out_hi, mult_out_lo);
|
|
|
|
|
2024-01-13 00:16:28 +01:00
|
|
|
assign O =
|
|
|
|
op == 3'b000 ? adder_out :
|
|
|
|
op == 3'b001 ? adder_out :
|
2024-01-17 12:24:33 +01:00
|
|
|
op == 3'b010 ? mult_out_hi :
|
|
|
|
op == 3'b011 ? mult_out_lo :
|
2024-01-13 00:16:28 +01:00
|
|
|
op == 3'b100 ? A & B :
|
|
|
|
op == 3'b101 ? A | B :
|
|
|
|
op == 3'b110 ? A ^ B :
|
|
|
|
op == 3'b111 ? ~A :
|
|
|
|
'0;
|
2024-01-12 20:45:05 +01:00
|
|
|
|
|
|
|
assign Fzero = ~ (| O);
|
2024-01-12 13:44:33 +01:00
|
|
|
|
|
|
|
endmodule
|