`include "definitions.sv"
module ALU
#(parameter N=32)
(
input wire [N-1:0] iX, iY,
input wire [3:0] iALUop,
output logic [N-1:0]oF
);
import alu_defs::*;
logic S3,S2,S1,S0;
wire [N-1:0] A=iX;
wire [N-1:0] B=iY;
logic [N:0] result,process; // Bit width is N+1 bits
always_comb
begin
case ({S1,S0,S2,S3})
4'b0001: result = A + B ;
4'b0010: result = A + B^{31{1'b1}} + 1;
4'b0011: result = A & B;
4'b0100: result = A |B;
4'b0101: result = A ^ B;
4'b0110: result = $signed(A) >>>B[5:0];
4'b0111: result = A <<B[5:0];
4'b1000: result = A >>B[5:0];
4'b1001: begin process=A + B^{31{1'b1}} + 1;result=process[N-1]&A[N-1]&B[N-1]|process[N-1]&(~A[N-1])&(~B[N-1]);end
4'b1010: begin process=A + B^{31{1'b1}} + 1;result=process[N];end
default: result = {(N+1){1'bx}};
endcase
oF = result[N-1:0];
end
always_comb
begin
case (iALUop)
ADD: {S1,S0,S2,S3} = 4'b0001;
SUB: {S1,S0,S2,S3} = 4'b0010;
AND: {S1,S0,S2,S3} = 4'b0011;
OR: {S1,S0,S2,S3} = 4'b0100;
XOR: {S1,S0,S2,S3} = 4'b0101;
SRA: {S1,S0,S2,S3} = 4'b0110;
SLL: {S1,S0,S2,S3} = 4'b0111;
SRL: {S1,S0,S2,S3} = 4'b1000;
SLT: {S1,S0,S2,S3} = 4'b1001;
SLTU:{S1,S0,S2,S3} = 4'b1010;
default: {S1,S0,S2,S3} = 4'bx;
endcase
end
endmodule