data/full_repos/permissive/506186384/project-files/lab6/ALU.v
data/verilator_xmls/8f5b0b64-c7a8-4024-827d-8a98aac93181.xml
module ALU(
    input [31:0] input1,
    input [31:0] input2,
    input [3:0] ALUCtr,
    output zero,
    output overflow,
    output [31:0] ALURes
    );
    
    reg Zero;
    reg Overflow;
    reg [31:0] alures;
    reg signed [31:0] signed_input1;
    reg signed [31:0] signed_input2;
    reg signed [31:0] signed_result;
    




    
    always @ (input1 or input2 or ALUCtr)
    begin
        case(ALUCtr)
            4'b0000:
            begin
                alures = input1 & input2;
                if(alures == 0)
                    Zero = 1;
                else
                    Zero = 0;
                Overflow = 0;
            end
            
            4'b0001:
            begin
                alures = input1 | input2;
                if(alures == 0)
                    Zero = 1;
                else
                    Zero = 0;
                Overflow = 0;
            end
            
            4'b0010:
            begin
                alures = input1 + input2;
                if(alures == 0)
                    Zero = 1;
                else
                    Zero = 0;
                signed_input1 = input1;
                signed_input2 = input2;
                signed_result = alures;
                if(signed_input1 > 0 && signed_input2 > 0 && signed_result < 0
                || signed_input1 < 0 && signed_input2 < 0 && signed_result > 0) begin
                    Overflow = 1;
                end
                else begin
                    Overflow = 0;
                end
            end
            
            4'b1001:
            begin
                alures = input1 + input2;
                if(alures == 0)
                    Zero = 1;
                else
                    Zero = 0;
                Overflow = 0;
            end
            
            4'b0110:
            begin
                alures = input1 - input2;
                if(alures == 0)
                    Zero = 1;
                else
                    Zero = 0;
                signed_input1 = input1;
                signed_input2 = input2;
                signed_result = alures;
                if(signed_input1 > 0 && signed_input2 < 0 && signed_result < 0
                || signed_input1 < 0 && signed_input2 > 0 && signed_result > 0) begin
                    Overflow = 1;
                end
                else begin
                    Overflow = 0;
                end
            end
            
            4'b1010:
            begin
                alures = input1 - input2;
                if(alures == 0)
                    Zero = 1;
                else
                    Zero = 0;
                Overflow = 0;
            end
            
            4'b1000:
            begin
                if(input1 < input2)
                    alures = 1;
                else
                    alures = 0;
                if(alures == 0)
                    Zero = 1;
                else
                    Zero = 0;
                Overflow = 0;
            end
            
            4'b0111:
            begin
                signed_input1 = input1;
                signed_input2 = input2;
                if(signed_input1 < signed_input2)
                    alures = 1;
                else
                    alures = 0;
                if(alures == 0)
                    Zero = 1;
                else
                    Zero = 0;
                Overflow = 0;
            end
            
            4'b1100:
            begin
                alures = ~(input1 | input2);
                if(alures == 0)
                    Zero = 1;
                else
                    Zero = 0;
                Overflow = 0;
            end
            
            4'b0011:
            begin
                alures = input2 << input1[4:0];
                if(alures == 0)
                    Zero = 1;
                else
                    Zero = 0;
                Overflow = 0;
            end
            
            4'b0100:
            begin
                alures = input2 >> input1[4:0];
                if(alures == 0)
                    Zero = 1;
                else
                    Zero = 0;
                Overflow = 0;
            end
            
            4'b0101:
            begin
                alures = 0;
                Zero = 1;
                Overflow = 0;
            end
            
            4'b1011:
            begin
                alures = {{input2[15:0]},16'b0};
                if(alures == 0)
                    Zero = 1;
                else
                    Zero = 0;
                Overflow = 0;
            end
            
            4'b1101:
            begin
                alures = input1 ^ input2;
                if(alures == 0)
                    Zero = 1;
                else
                    Zero = 0;
                Overflow = 0;
            end
            
            4'b1110:
            begin
                signed_input2 = input2;
                alures = signed_input2 >>> input1[4:0];
                if(alures == 0)
                    Zero = 1;
                else
                    Zero = 0;
                Overflow = 0;
            end
            
            default:
            begin
                alures = 0;
                Zero = 1;
                Overflow = 0;
            end
        endcase
    end
    assign ALURes = alures;
    assign zero = Zero;
    assign overflow = Overflow;
    
endmodule
module ALU(
    input [31:0] input1,
    input [31:0] input2,
    input [3:0] ALUCtr,
    output zero,
    output overflow,
    output [31:0] ALURes
    );

    
    reg Zero;
    reg Overflow;
    reg [31:0] alures;
    reg signed [31:0] signed_input1;
    reg signed [31:0] signed_input2;
    reg signed [31:0] signed_result;
    




    
    always @ (input1 or input2 or ALUCtr)
    begin
        case(ALUCtr)
            4'b0000:
            begin
                alures = input1 & input2;
                if(alures == 0)
                    Zero = 1;
                else
                    Zero = 0;
                Overflow = 0;
            end
            
            4'b0001:
            begin
                alures = input1 | input2;
                if(alures == 0)
                    Zero = 1;
                else
                    Zero = 0;
                Overflow = 0;
            end
            
            4'b0010:
            begin
                alures = input1 + input2;
                if(alures == 0)
                    Zero = 1;
                else
                    Zero = 0;
                signed_input1 = input1;
                signed_input2 = input2;
                signed_result = alures;
                if(signed_input1 > 0 && signed_input2 > 0 && signed_result < 0
                || signed_input1 < 0 && signed_input2 < 0 && signed_result > 0) begin
                    Overflow = 1;
                end
                else begin
                    Overflow = 0;
                end
            end
            
            4'b1001:
            begin
                alures = input1 + input2;
                if(alures == 0)
                    Zero = 1;
                else
                    Zero = 0;
                Overflow = 0;
            end
            
            4'b0110:
            begin
                alures = input1 - input2;
                if(alures == 0)
                    Zero = 1;
                else
                    Zero = 0;
                signed_input1 = input1;
                signed_input2 = input2;
                signed_result = alures;
                if(signed_input1 > 0 && signed_input2 < 0 && signed_result < 0
                || signed_input1 < 0 && signed_input2 > 0 && signed_result > 0) begin
                    Overflow = 1;
                end
                else begin
                    Overflow = 0;
                end
            end
            
            4'b1010:
            begin
                alures = input1 - input2;
                if(alures == 0)
                    Zero = 1;
                else
                    Zero = 0;
                Overflow = 0;
            end
            
            4'b1000:
            begin
                if(input1 < input2)
                    alures = 1;
                else
                    alures = 0;
                if(alures == 0)
                    Zero = 1;
                else
                    Zero = 0;
                Overflow = 0;
            end
            
            4'b0111:
            begin
                signed_input1 = input1;
                signed_input2 = input2;
                if(signed_input1 < signed_input2)
                    alures = 1;
                else
                    alures = 0;
                if(alures == 0)
                    Zero = 1;
                else
                    Zero = 0;
                Overflow = 0;
            end
            
            4'b1100:
            begin
                alures = ~(input1 | input2);
                if(alures == 0)
                    Zero = 1;
                else
                    Zero = 0;
                Overflow = 0;
            end
            
            4'b0011:
            begin
                alures = input2 << input1[4:0];
                if(alures == 0)
                    Zero = 1;
                else
                    Zero = 0;
                Overflow = 0;
            end
            
            4'b0100:
            begin
                alures = input2 >> input1[4:0];
                if(alures == 0)
                    Zero = 1;
                else
                    Zero = 0;
                Overflow = 0;
            end
            
            4'b0101:
            begin
                alures = 0;
                Zero = 1;
                Overflow = 0;
            end
            
            4'b1011:
            begin
                alures = {{input2[15:0]},16'b0};
                if(alures == 0)
                    Zero = 1;
                else
                    Zero = 0;
                Overflow = 0;
            end
            
            4'b1101:
            begin
                alures = input1 ^ input2;
                if(alures == 0)
                    Zero = 1;
                else
                    Zero = 0;
                Overflow = 0;
            end
            
            4'b1110:
            begin
                signed_input2 = input2;
                alures = signed_input2 >>> input1[4:0];
                if(alures == 0)
                    Zero = 1;
                else
                    Zero = 0;
                Overflow = 0;
            end
            
            default:
            begin
                alures = 0;
                Zero = 1;
                Overflow = 0;
            end
        endcase
    end
    assign ALURes = alures;
    assign zero = Zero;
    assign overflow = Overflow;
    
endmodule