aboutsummaryrefslogtreecommitdiff
path: root/rtl/simple_alu.sv
blob: 8dc0586bee8fc80ddd35563f202087c70d4a87ad (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
// Simple 32-bit ALU supporting addition and subtraction (sel=0/1).
// Outputs are registered. Overflow/underflow detected via 33-bit extended arithmetic.
// Active-low synchronous reset. Used as a sandbox for formal verification.
module simple_alu (

    //inputs
    input logic        clk,
    input logic [31:0] a,
    input logic [31:0] b,
    input logic        sel,         //sel = 0 -> ADD, sel = 1 -> SUB
    input logic        rst,         //active LOW sync reset

    //outputs
    output logic [31:0] y,
    output logic        overflow
);

    logic [32:0] sum;
    logic [32:0] diff;

    always_comb begin
        sum  = {1'b0, a} + {1'b0, b};
        diff = {1'b0, a} - {1'b0, b};
    end

    always_ff @(posedge clk) begin
    if (~rst) begin
        y <= '0;
        overflow <= '0;
    end
    else begin
        case (sel)
        0:  begin
                y <= sum[31:0];
                overflow <= sum[32];
        end
        1:  begin
                y <= diff[31:0];
                overflow <= diff[32];
        end
        endcase
    end
    end
endmodule