aboutsummaryrefslogtreecommitdiff
path: root/rtl/simple_alu.sv
blob: a5ae9e71b110dbc8c4e3fcf787da2f74d457ba3e (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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
// 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_i,
    input logic [31:0] a_i,
    input logic [31:0] b_i,
    input logic        sel_i,         //sel = 0 -> ADD, sel = 1 -> SUB
    input logic        rst_ni,        //active LOW sync reset

    //outputs
    output logic [31:0] y_o,
    output logic        overflow_o
);

    logic [32:0] sum;
    logic [32:0] diff;
    logic [31:0] y_r;
    logic        overflow_r;
    logic [31:0] y_next;
    logic        overflow_next;

    always_comb begin
        sum  = {1'b0, a_i} + {1'b0, b_i};
        diff = {1'b0, a_i} - {1'b0, b_i};

        y_next        = '0;
        overflow_next = '0;

        if (rst_ni) begin
            case (sel_i)
                0: begin
                    y_next        = sum[31:0];
                    overflow_next = sum[32];
                end
                1: begin
                    y_next        = diff[31:0];
                    overflow_next = diff[32];
                end
                default: begin
                    y_next        = '0;
                    overflow_next = '0;
                end
            endcase
        end
    end

    always_ff @(posedge clk_i) begin
        y_r        <= y_next;
        overflow_r <= overflow_next;
    end

    assign y_o        = y_r;
    assign overflow_o = overflow_r;

endmodule