ALU

There are quite a lot of ALUs, one for each type of operation. This made the core superscalar.

ALUs

alu0

alu0.sv

alu0 module

Imports

Imports 3 elements

  • core_config_pkg XLEN alu_commands_t REG_ADDR_W

Ports

Ports list 17 elements

Name

Direction

Description

Type

Size

Kind

Polarity

Fanout

clk

Input

logic

-

Combinatorial

-

Fanout : 0

rst_n

Input

logic

-

Combinatorial

Negative

Fanout : 0

arg0

Input

logic

[(core_config_pkg::XLEN - 1) : 0]

Combinatorial

-

Fanout : 0

arg1

Input

verilator lint_off UNUSEDSIGNAL

logic

[(core_config_pkg::XLEN - 1) : 0]

Combinatorial

-

Fanout : 0

addr

Input

logic

[(core_config_pkg::XLEN - 1) : 0]

Combinatorial

-

Fanout : 0

imm

Input

verilator lint_off UNUSEDSIGNAL

logic

[(core_config_pkg::XLEN - 1) : 0]

Combinatorial

-

Fanout : 0

cmd

Input

alu_commands_t

-

Combinatorial

-

Fanout : 0

busy

Output

logic

-

Register clk rst_n

-

Fanout : 0

res

Output

verilator lint_off UNDRIVEN

logic

[(core_config_pkg::XLEN - 1) : 0]

Register clk rst_n

-

Fanout : 0

jmp

Output

verilator lint_off UNDRIVEN

logic

[(core_config_pkg::XLEN - 1) : 0]

Combinatorial

-

Fanout : 0

valid

Output

logic

-

Register clk rst_n

-

Fanout : 0

req

Output

logic

-

Register clk rst_n

-

Fanout : 0

clear

Input

logic

-

Combinatorial

-

Fanout : 0

i 2 elements

Name

Direction

Description

Type

Size

Kind

Polarity

Fanout

i_rd

Input

logic

[(core_config_pkg::REG_ADDR_W - 1) : 0]

Register clk

-

Fanout : 0

i_error

Output

logic

-

Register clk rst_n

-

Fanout : 0

o 2 elements

Name

Direction

Description

Type

Size

Kind

Polarity

Fanout

o_rd

Output

logic

[(core_config_pkg::REG_ADDR_W - 1) : 0]

Register clk rst_n

-

Fanout : 0

o_error

Output

logic

-

Register clk rst_n

-

Fanout : 0

Processes

Process list 2 elements

• Process 1 Combinatorial

First, perform calculations (outputs from issuer are synchronous to clock).

• Process 2 Clocked Clocked by clk Reset by rst_n

-

Signals

Signals list 4 elements

Name

Description

Type

Size

Kind

Fanout

tmp_res

Storages types

logic

[(core_config_pkg::XLEN) : 0]

Combinatorial

Fanout : 0

unknown_instr

One more bit to handle overflow.

logic

-

Combinatorial

Fanout : 0

int_req

logic

-

Register clk rst_n

Fanout : 0

end_of_op

logic

-

Register clk rst_n

Fanout : 0

Instantiation templates

Instantiation templates 2 elements

alu0 <name> (
    .clk     (i_clk),
    .rst_n   (i_rst_n),
    .arg0    (i_arg0),
    .arg1    (i_arg1),
    .addr    (i_addr),
    .imm     (i_imm),
    .cmd     (i_cmd),
    .i_rd    (i_i_rd),
    .busy    (i_busy),
    .i_error (i_i_error),
    .res     (i_res),
    .jmp     (i_jmp),
    .o_rd    (i_o_rd),
    .valid   (i_valid),
    .o_error (i_o_error),
    .req     (i_req),
    .clear   (i_clear)
);
<name>: entity work.alu0
   port map (
        clk      => i_clk,
        rst_n    => i_rst_n,
        arg0     => i_arg0,
        arg1     => i_arg1,
        addr     => i_addr,
        imm      => i_imm,
        cmd      => i_cmd,
        i_rd     => i_i_rd,
        busy     => i_busy,
        i_error  => i_i_error,
        res      => i_res,
        jmp      => i_jmp,
        o_rd     => i_o_rd,
        valid    => i_valid,
        o_error  => i_o_error,
        req      => i_req,
        clear    => i_clear
    );

Source

Raw source Full file

/*
 *  File :      rtl/core/alu/alu0.sv
 *
 *  Author :    l.heywang <leonard.heywang@proton.me>
 *  Date :      25/10.2025
 *  
 *  Brief :     This file define the ALU0 module, which handle
 *              the most basic operations (ADD, SUB, AND, OR, XOR).
 */

`timescale 1ns / 1ps

import core_config_pkg::XLEN;
import core_config_pkg::alu_commands_t;
import core_config_pkg::REG_ADDR_W;

module alu0 (
    input  logic                                                  clk,
    input  logic                                                  rst_n,
    input  logic          [      (core_config_pkg::XLEN - 1) : 0] arg0,
    input  logic          [      (core_config_pkg::XLEN - 1) : 0] arg1,
    /* verilator lint_off UNUSEDSIGNAL */
    input  logic          [      (core_config_pkg::XLEN - 1) : 0] addr,
    input  logic          [      (core_config_pkg::XLEN - 1) : 0] imm,
    /* verilator lint_off UNUSEDSIGNAL */
    input  alu_commands_t                                         cmd,
    input  logic          [(core_config_pkg::REG_ADDR_W - 1) : 0] i_rd,
    output logic                                                  busy,
    output logic                                                  i_error,
    output logic          [      (core_config_pkg::XLEN - 1) : 0] res,
    /* verilator lint_off UNDRIVEN */
    output logic          [      (core_config_pkg::XLEN - 1) : 0] jmp,
    /* verilator lint_off UNDRIVEN */
    output logic          [(core_config_pkg::REG_ADDR_W - 1) : 0] o_rd,
    output logic                                                  valid,
    output logic                                                  o_error,
    output logic                                                  req,
    input  logic                                                  clear
);
    /*
     *  Storages types
     */
    logic [(core_config_pkg::XLEN) : 0] tmp_res;  // One more bit to handle overflow.
    logic                               unknown_instr;
    logic                               int_req;
    logic                               end_of_op;

    /*
     *  First, perform calculations (outputs from issuer are synchronous to clock).
     */
    always_comb begin

        unique case (cmd)

            core_config_pkg::c_ADD: begin
                // Calculation
                tmp_res       = {1'b0, arg0} + {1'b0, arg1};

                // Setting flags
                int_req       = 1'b1;
                unknown_instr = 1'b0;
            end
            core_config_pkg::c_SUB: begin
                // Calculation
                tmp_res       = {1'b0, arg0} - {1'b0, arg1};

                // Setting flags
                int_req       = 1'b1;
                unknown_instr = 1'b0;
            end
            core_config_pkg::c_AND: begin
                // Calculation
                tmp_res       = {1'b0, arg0} & {1'b0, arg1};

                // Setting flags
                int_req       = 1'b1;
                unknown_instr = 1'b0;
            end
            core_config_pkg::c_OR: begin
                // Calculation
                tmp_res       = {1'b0, arg0} | {1'b0, arg1};

                // Setting flags
                int_req       = 1'b1;
                unknown_instr = 1'b0;
            end
            core_config_pkg::c_XOR: begin
                // Calculation
                tmp_res       = {1'b0, arg0} ^ {1'b0, arg1};

                // Setting flags
                int_req       = 1'b1;
                unknown_instr = 1'b0;
            end
            default: begin
                // Calculation
                tmp_res       = 33'b0;

                // Setting flags
                int_req       = 1'b0;
                unknown_instr = 1'b1;
            end
        endcase
    end

    /*
     *  Second, latching the first stage outputs before outputing them for the
     *  commiter stage.
     */
    always_ff @(posedge clk or negedge rst_n) begin

        if (!rst_n) begin

            busy      <= 1'b0;
            res       <= 32'b0;
            i_error   <= 1'b0;
            o_error   <= 1'b0;
            req       <= 1'b0;
            o_rd      <= 5'b0;
            valid     <= 1'b0;
            end_of_op <= 1'b0;

        end else if (clear && end_of_op) begin

            busy      <= 1'b0;
            res       <= 32'b0;
            i_error   <= 1'b0;
            o_error   <= 1'b0;
            req       <= 1'b0;
            o_rd      <= 5'b0;
            valid     <= 1'b0;
            end_of_op <= 1'b0;

        end else if (!unknown_instr) begin

            busy      <= int_req;
            res       <= tmp_res[(core_config_pkg::XLEN-1) : 0];
            i_error   <= 1'b0;
            o_error   <= tmp_res[(core_config_pkg::XLEN)];
            req       <= 1'b0;
            o_rd      <= i_rd;
            valid     <= 1'b1;
            end_of_op <= 1'b1;

        end else begin

            busy      <= 1'b0;
            res       <= 32'b0;
            i_error   <= 1'b1;
            o_error   <= 1'b0;
            req       <= 1'b0;
            o_rd      <= 5'b0;
            valid     <= 1'b0;
            end_of_op <= 1'b0;

        end
    end
endmodule

alu1

alu1.sv

alu1 module

Imports

Imports 6 elements

  • core_config_pkg XLEN alu_commands_t IF_INC IF_MRET_UCODE IF_ECALL_UCODE IF_EBREAK_UCODE

Ports

Ports list 20 elements

Name

Direction

Description

Type

Size

Kind

Polarity

Fanout

clk

Input

logic

-

Combinatorial

-

Fanout : 0

rst_n

Input

logic

-

Combinatorial

Negative

Fanout : 0

arg0

Input

logic

[(core_config_pkg::XLEN - 1) : 0]

Combinatorial

-

Fanout : 0

arg1

Input

logic

[(core_config_pkg::XLEN - 1) : 0]

Combinatorial

-

Fanout : 0

addr

Input

logic

[(core_config_pkg::XLEN - 1) : 0]

Combinatorial

-

Fanout : 0

imm

Input

logic

[(core_config_pkg::XLEN - 1) : 0]

Combinatorial

-

Fanout : 0

cmd

Input

alu_commands_t

-

Combinatorial

-

Fanout : 0

busy

Output

logic

-

Register clk rst_n

-

Fanout : 0

res

Output

logic

[(core_config_pkg::XLEN - 1) : 0]

Register clk rst_n

-

Fanout : 0

jmp

Output

logic

[(core_config_pkg::XLEN - 1) : 0]

Register clk rst_n

-

Fanout : 0

valid

Output

logic

-

Register clk rst_n

-

Fanout : 0

req

Output

logic

-

Register clk rst_n

-

Fanout : 0

clear

Input

logic

-

Combinatorial

-

Fanout : 0

mispredict

Output

logic

-

Register clk rst_n

-

Fanout : 0

i 2 elements

Name

Direction

Description

Type

Size

Kind

Polarity

Fanout

i_rd

Input

logic

[(core_config_pkg::REG_ADDR_W - 1) : 0]

Register clk

-

Fanout : 0

i_error

Output

logic

-

Register clk rst_n

-

Fanout : 0

predict 2 elements

Name

Direction

Description

Type

Size

Kind

Polarity

Fanout

predict_in

Input

logic

-

Combinatorial

-

Fanout : 0

predict_ok

Output

logic

-

Register clk rst_n

-

Fanout : 0

o 2 elements

Name

Direction

Description

Type

Size

Kind

Polarity

Fanout

o_rd

Output

logic

[(core_config_pkg::REG_ADDR_W - 1) : 0]

Register clk rst_n

-

Fanout : 0

o_error

Output

logic

-

Register clk rst_n

-

Fanout : 0

Processes

Process list 2 elements

• Process 1 Combinatorial

First, perform calculations (outputs from issuer are synchronous to clock).

• Process 2 Clocked Clocked by clk Reset by rst_n

-

Signals

Signals list 7 elements

Name

Description

Type

Size

Kind

Fanout

unknown_instr

logic

-

Combinatorial

Fanout : 0

int_req

logic

-

Register clk rst_n

Fanout : 0

act_needed

logic

-

Register clk rst_n

Fanout : 0

end_of_op

logic

-

Register clk rst_n

Fanout : 0

predict_value

logic

-

Combinatorial

Fanout : 0

tmp 2 elements

Name

Description

Type

Size

Kind

Fanout

tmp_res

Storages types

logic

[(core_config_pkg::XLEN) : 0]

Combinatorial

Fanout : 0

tmp_jmp

One more bit to handle overflow.

logic

[(core_config_pkg::XLEN) : 0]

Combinatorial

Fanout : 0

Instantiation templates

Instantiation templates 2 elements

alu1 <name> (
    .clk        (i_clk),
    .rst_n      (i_rst_n),
    .arg0       (i_arg0),
    .arg1       (i_arg1),
    .addr       (i_addr),
    .imm        (i_imm),
    .cmd        (i_cmd),
    .i_rd       (i_i_rd),
    .busy       (i_busy),
    .i_error    (i_i_error),
    .predict_in (i_predict_in),
    .res        (i_res),
    .jmp        (i_jmp),
    .o_rd       (i_o_rd),
    .valid      (i_valid),
    .o_error    (i_o_error),
    .req        (i_req),
    .clear      (i_clear),
    .predict_ok (i_predict_ok),
    .mispredict (i_mispredict)
);
<name>: entity work.alu1
   port map (
        clk         => i_clk,
        rst_n       => i_rst_n,
        arg0        => i_arg0,
        arg1        => i_arg1,
        addr        => i_addr,
        imm         => i_imm,
        cmd         => i_cmd,
        i_rd        => i_i_rd,
        busy        => i_busy,
        i_error     => i_i_error,
        predict_in  => i_predict_in,
        res         => i_res,
        jmp         => i_jmp,
        o_rd        => i_o_rd,
        valid       => i_valid,
        o_error     => i_o_error,
        req         => i_req,
        clear       => i_clear,
        predict_ok  => i_predict_ok,
        mispredict  => i_mispredict
    );

Source

Raw source Full file

/*
 *  File :      rtl/core/alu/alu1.sv
 *
 *  Author :    l.heywang <leonard.heywang@proton.me>
 *  Date :      25/10.2025
 *  
 *  Brief :     This file define the ALU1 module, which
 *              handle the logic for the branches.
 *              It compare two values, and compute the associated 
 *              address, if needed.
 *              It also expose two signals, in case the logic was correctly predicted, or not.
 */

`timescale 1ns / 1ps

import core_config_pkg::XLEN;
import core_config_pkg::alu_commands_t;

import core_config_pkg::IF_INC;
import core_config_pkg::IF_MRET_UCODE;
import core_config_pkg::IF_ECALL_UCODE;
import core_config_pkg::IF_EBREAK_UCODE;

module alu1 (
    input  logic                                                  clk,
    input  logic                                                  rst_n,
    input  logic          [      (core_config_pkg::XLEN - 1) : 0] arg0,
    input  logic          [      (core_config_pkg::XLEN - 1) : 0] arg1,
    input  logic          [      (core_config_pkg::XLEN - 1) : 0] addr,
    input  logic          [      (core_config_pkg::XLEN - 1) : 0] imm,
    input  alu_commands_t                                         cmd,
    input  logic          [(core_config_pkg::REG_ADDR_W - 1) : 0] i_rd,
    output logic                                                  busy,
    output logic                                                  i_error,
    input  logic                                                  predict_in,
    output logic          [      (core_config_pkg::XLEN - 1) : 0] res,
    output logic          [      (core_config_pkg::XLEN - 1) : 0] jmp,
    output logic          [(core_config_pkg::REG_ADDR_W - 1) : 0] o_rd,
    output logic                                                  valid,
    output logic                                                  o_error,
    output logic                                                  req,
    input  logic                                                  clear,
    output logic                                                  predict_ok,
    output logic                                                  mispredict
);
    /*
     *  Storages types
     */
    logic [(core_config_pkg::XLEN) : 0] tmp_res;  // One more bit to handle overflow.
    logic [(core_config_pkg::XLEN) : 0] tmp_jmp;  // One more bit to handle overflow.
    logic                               unknown_instr;
    logic                               int_req;
    logic                               act_needed;
    logic                               end_of_op;
    logic                               predict_value;

    /*
     *  Note : Two outputs are used here : jmp and res.
     *  
     *  Res :   Used to output the value that WILL be written into the register file
     *  Jmp :   Used to output the value to be written into the PC.
     *
     *  This enable the ability to perform single cycle JAL / JALR instructions and so on.
     */


    /*
     *  First, perform calculations (outputs from issuer are synchronous to clock).
     */
    always_comb begin

        unique case (cmd)

            /*
             *  SLTx instructions
             */
            core_config_pkg::c_SLT: begin
                // Calculation
                tmp_res       = ($signed(arg0) < $signed(arg1)) ? 33'b1 : 33'b0;
                tmp_jmp       = '0;

                // Setting flags
                act_needed    = 1'b0;
                predict_value = 1'b0;
                int_req       = 1'b1;
                unknown_instr = 1'b0;
            end
            core_config_pkg::c_SLTU: begin
                // Calculation
                tmp_res       = ($unsigned(arg0) < $unsigned(arg1)) ? 33'b1 : 33'b0;
                tmp_jmp       = '0;

                // Setting flags
                act_needed    = 1'b0;
                predict_value = 1'b0;
                int_req       = 1'b1;
                unknown_instr = 1'b0;
            end

            /*
             *  Branch conditions
             */
            core_config_pkg::c_BEQ: begin
                // Calculation
                tmp_jmp       = {1'b0, addr} + {1'b0, imm};
                tmp_res       = '0;

                // Setting flags
                act_needed    = (arg0 == arg1) ? 1'b1 : 1'b0;
                predict_value = predict_in;
                int_req       = 1'b1;
                unknown_instr = 1'b0;
            end
            core_config_pkg::c_BNE: begin
                // Calculation
                tmp_jmp       = {1'b0, addr} + {1'b0, imm};
                tmp_res       = '0;

                // Setting flags
                act_needed    = (arg0 == arg1) ? 1'b0 : 1'b1;
                predict_value = predict_in;
                int_req       = 1'b1;
                unknown_instr = 1'b0;
            end
            core_config_pkg::c_BGE: begin
                // Calculation
                tmp_jmp       = {1'b0, addr} + {1'b0, imm};
                tmp_res       = '0;

                // Setting flags
                act_needed    = ($signed(arg0) >= $signed(arg1)) ? 1'b1 : 1'b0;
                predict_value = predict_in;
                int_req       = 1'b1;
                unknown_instr = 1'b0;
            end
            core_config_pkg::c_BLT: begin
                // Calculation
                tmp_jmp       = {1'b0, addr} + {1'b0, imm};
                tmp_res       = '0;

                // Setting flags
                act_needed    = ($signed(arg0) < $signed(arg1)) ? 1'b1 : 1'b0;
                predict_value = predict_in;
                int_req       = 1'b1;
                unknown_instr = 1'b0;
            end
            core_config_pkg::c_BGEU: begin
                // Calculation
                tmp_jmp       = {1'b0, addr} + {1'b0, imm};
                tmp_res       = '0;

                // Setting flags
                act_needed    = ($unsigned(arg0) >= $unsigned(arg1)) ? 1'b1 : 1'b0;
                predict_value = predict_in;
                int_req       = 1'b1;
                unknown_instr = 1'b0;
            end
            core_config_pkg::c_BLTU: begin
                // Calculation
                tmp_jmp       = {1'b0, addr} + {1'b0, imm};
                tmp_res       = '0;

                // Setting flags
                act_needed    = ($unsigned(arg0) < $unsigned(arg1)) ? 1'b1 : 1'b0;
                predict_value = predict_in;
                int_req       = 1'b1;
                unknown_instr = 1'b0;
            end

            /*
             *  Jumps
             */
            core_config_pkg::c_JAL: begin
                // Calculation
                tmp_jmp       = {1'b0, addr} + {1'b0, IF_INC};
                tmp_res       = '0;

                // Setting flags
                act_needed    = 1'b0;  // always predicted
                predict_value = 1'b0;
                int_req       = 1'b1;
                unknown_instr = 1'b0;
            end
            core_config_pkg::c_JALR: begin
                // Calculation
                tmp_jmp       = ({1'b0, addr} + {1'b0, arg0}) & ~1;
                tmp_res       = {1'b0, addr} + IF_INC;

                // Setting flags
                act_needed    = 1'b1;  // always jumped
                predict_value = 1'b0;
                int_req       = 1'b1;
                unknown_instr = 1'b0;
            end
            /*
             *  Special instructions
             */
            core_config_pkg::c_EBREAK: begin
                // Calculation
                tmp_jmp       = {1'b0, IF_EBREAK_UCODE};
                tmp_res       = '0;

                // Setting flags
                act_needed    = 1'b1;  // always jumped
                predict_value = 1'b0;
                int_req       = 1'b1;
                unknown_instr = 1'b0;
            end
            core_config_pkg::c_ECALL: begin
                // Calculation
                tmp_jmp       = {1'b0, IF_ECALL_UCODE};
                tmp_res       = '0;

                // Setting flags
                act_needed    = 1'b1;  // always jumped
                predict_value = 1'b0;
                int_req       = 1'b1;
                unknown_instr = 1'b0;
            end
            core_config_pkg::c_MRET: begin
                // Calculation
                tmp_jmp       = {1'b0, IF_MRET_UCODE};
                tmp_res       = '0;

                // Setting flags
                act_needed    = 1'b1;  // always jumped
                predict_value = 1'b0;
                int_req       = 1'b1;
                unknown_instr = 1'b0;
            end
            /*
             *  Default case
             */
            default: begin
                // Calculation
                tmp_res       = 33'b0;
                tmp_jmp       = 33'b0;

                // Setting flags
                act_needed    = 1'b0;
                predict_value = 1'b0;
                int_req       = 1'b0;
                unknown_instr = 1'b1;
            end

        endcase
    end

    /*
     *  Second, latching the first stage outputs before outputing them for the
     *  commiter stage.
     */
    always_ff @(posedge clk or negedge rst_n) begin

        if (!rst_n) begin

            busy      <= 1'b0;
            res       <= 32'b0;
            jmp       <= 32'b0;
            i_error   <= 1'b0;
            o_error   <= 1'b0;
            req       <= 1'b0;
            o_rd      <= 5'b0;
            valid     <= 1'b0;
            end_of_op <= 1'b0;

        end else if (clear && end_of_op) begin

            busy       <= 1'b0;
            res        <= 32'b0;
            jmp        <= 32'b0;
            i_error    <= 1'b0;
            o_error    <= 1'b0;
            req        <= 1'b0;
            o_rd       <= 5'b0;
            valid      <= 1'b0;
            end_of_op  <= 1'b0;
            predict_ok <= 1'b0;
            mispredict <= 1'b0;

        end else if (!unknown_instr) begin

            busy       <= int_req;
            res        <= tmp_res[(core_config_pkg::XLEN-1) : 0];
            jmp        <= tmp_jmp[(core_config_pkg::XLEN-1) : 0];
            i_error    <= 1'b0;
            o_error    <= tmp_res[(core_config_pkg::XLEN)] | tmp_jmp[(core_config_pkg::XLEN)];
            req        <= act_needed;
            o_rd       <= i_rd;
            valid      <= 1'b1;
            end_of_op  <= 1'b1;
            predict_ok <= ~(act_needed ^ predict_value);
            mispredict <= act_needed ^ predict_value;

        end else begin

            busy       <= 1'b0;
            res        <= 32'b0;
            jmp        <= 32'b0;
            i_error    <= 1'b1;  // Set to 1
            o_error    <= 1'b0;
            req        <= 1'b0;
            o_rd       <= 5'b0;
            valid      <= 1'b0;
            end_of_op  <= 1'b0;
            predict_ok <= 1'b0;
            mispredict <= 1'b0;

        end
    end

endmodule

alu2

alu2.sv

alu2 module

Imports

Imports 3 elements

  • core_config_pkg XLEN REG_ADDR_W alu_commands_t

Ports

Ports list 17 elements

Name

Direction

Description

Type

Size

Kind

Polarity

Fanout

clk

Input

logic

-

Combinatorial

-

Fanout : 0

rst_n

Input

logic

-

Combinatorial

Negative

Fanout : 0

arg0

Input

logic

[(core_config_pkg::XLEN - 1) : 0]

Register clk

-

Fanout : 0

arg1

Input

verilator lint_off UNUSEDSIGNAL

logic

[(core_config_pkg::XLEN - 1) : 0]

Register clk

-

Fanout : 0

addr

Input

logic

[(core_config_pkg::XLEN - 1) : 0]

Combinatorial

-

Fanout : 0

imm

Input

verilator lint_off UNUSEDSIGNAL

logic

[(core_config_pkg::XLEN - 1) : 0]

Combinatorial

-

Fanout : 0

cmd

Input

alu_commands_t

-

Register clk

-

Fanout : 0

busy

Output

logic

-

Combinatorial

-

Fanout : 0

res

Output

verilator lint_off UNDRIVEN

logic

[(core_config_pkg::XLEN - 1) : 0]

Combinatorial

-

Fanout : 0

jmp

Output

verilator lint_off UNDRIVEN

logic

[(core_config_pkg::XLEN - 1) : 0]

Combinatorial

-

Fanout : 0

valid

Output

logic

-

Combinatorial

-

Fanout : 0

req

Output

logic

-

Combinatorial

-

Fanout : 0

clear

Input

logic

-

Combinatorial

-

Fanout : 0

i 2 elements

Name

Direction

Description

Type

Size

Kind

Polarity

Fanout

i_rd

Input

logic

[(core_config_pkg::REG_ADDR_W - 1) : 0]

Register clk

-

Fanout : 0

i_error

Output

logic

-

Combinatorial

-

Fanout : 0

o 2 elements

Name

Direction

Description

Type

Size

Kind

Polarity

Fanout

o_rd

Output

logic

[(core_config_pkg::REG_ADDR_W - 1) : 0]

Combinatorial

-

Fanout : 0

o_error

Output

logic

-

Combinatorial

-

Fanout : 0

Modules

Modules 3 elements

Processes

Process list 4 elements

• Process 1 Combinatorial

Operation decoder

• Process 2 Clocked Clocked by clk Reset by rst_n

Some sync logic to handle the FSM states and evolution

• Process 3 Combinatorial

State evolution logic

• Process 4 Combinatorial

Controlling the output logic

Signals

Signals list 41 elements

Name

Description

Type

Size

Kind

Fanout

state

state_t

-

Register clk rst_n

Fanout : 0

unknown_instr

Generics

logic

-

Combinatorial

Fanout : 0

res_int

logic

[(core_config_pkg::XLEN - 1) : 0]

Register clk rst_n

Fanout : 0

next 10 elements

Name

Description

Type

Size

Kind

Fanout

next_state

state_t

-

Register clk rst_n

Fanout : 0

next_res

logic

[(core_config_pkg::XLEN - 1) : 0]

Register clk rst_n

Fanout : 0

mul 3 elements

Name

Description

Type

Size

Kind

Fanout

next_mul_start

logic

-

Register clk rst_n

Fanout : 0

signed 2 elements

Name

Description

Type

Size

Kind

Fanout

next_mul_signed_a

logic

-

Register clk rst_n

Fanout : 0

next_mul_signed_b

logic

-

Register clk rst_n

Fanout : 0

div 2 elements

Name

Description

Type

Size

Kind

Fanout

next_div_start

logic

-

Register clk rst_n

Fanout : 0

next_div_signed

logic

-

Register clk rst_n

Fanout : 0

shift 3 elements

Name

Description

Type

Size

Kind

Fanout

next_shift_start

logic

-

Register clk rst_n

Fanout : 0

next_shift_left

logic

-

Register clk rst_n

Fanout : 0

next_shift_arithmetic

logic

-

Register clk rst_n

Fanout : 0

r 4 elements

Name

Description

Type

Size

Kind

Fanout

r_arg0

Registered data

logic

[(core_config_pkg::XLEN - 1) : 0]

Register clk rst_n

Fanout : 0

r_arg1

logic

[(core_config_pkg::XLEN - 1) : 0]

Register clk rst_n

Fanout : 0

r_i_rd

logic

[(core_config_pkg::REG_ADDR_W - 1) : 0]

Register clk rst_n

Fanout : 0

r_cmd

alu_commands_t

-

Register clk rst_n

Fanout : 0

mul 5 elements

Name

Description

Type

Size

Kind

Fanout

mul_start

Multiplier signals

logic

-

Register clk rst_n

Fanout : 0

mul_done

logic

-

Register clk rst_n

Fanout : 0

mul_product

logic

[((2 * core_config_pkg::XLEN) - 1) : 0]

Combinatorial

Fanout : 0

signed 2 elements

Name

Description

Type

Size

Kind

Fanout

mul_signed_a

logic

-

Register clk rst_n

Fanout : 0

mul_signed_b

logic

-

Register clk rst_n

Fanout : 0

out 8 elements

mul 3 elements

Name

Description

Type

Size

Kind

Fanout

out_mul_start

logic

-

Combinatorial

Fanout : 0

signed 2 elements

Name

Description

Type

Size

Kind

Fanout

out_mul_signed_a

logic

-

Combinatorial

Fanout : 0

out_mul_signed_b

logic

-

Combinatorial

Fanout : 0

div 2 elements

Name

Description

Type

Size

Kind

Fanout

out_div_start

logic

-

Combinatorial

Fanout : 0

out_div_signed

logic

-

Combinatorial

Fanout : 0

shift 3 elements

Name

Description

Type

Size

Kind

Fanout

out_shift_start

logic

-

Combinatorial

Fanout : 0

out_shift_left

logic

-

Combinatorial

Fanout : 0

out_shift_arithmetic

logic

-

Combinatorial

Fanout : 0

div 6 elements

Name

Description

Type

Size

Kind

Fanout

div_start

Divider signals

logic

-

Register clk rst_n

Fanout : 0

div_done

logic

-

Register clk rst_n

Fanout : 0

div_signed

logic

-

Register clk rst_n

Fanout : 0

div_quotient

logic

[(core_config_pkg::XLEN - 1) : 0]

Register clk rst_n

Fanout : 0

div_remainder

logic

[(core_config_pkg::XLEN - 1) : 0]

Register clk rst_n

Fanout : 0

div_by_zero

logic

-

Register clk rst_n

Fanout : 0

shift 5 elements

Name

Description

Type

Size

Kind

Fanout

shift_start

Shifter signals

logic

-

Register clk rst_n

Fanout : 0

shift_done

logic

-

Register clk rst_n

Fanout : 0

shift_left

logic

-

Register clk rst_n

Fanout : 0

shift_arithmetic

logic

-

Register clk rst_n

Fanout : 0

shift_result

logic

[(core_config_pkg::XLEN - 1) : 0]

Register clk rst_n

Fanout : 0

Instantiation templates

Instantiation templates 2 elements

alu2 <name> (
    .clk     (i_clk),
    .rst_n   (i_rst_n),
    .arg0    (i_arg0),
    .arg1    (i_arg1),
    .addr    (i_addr),
    .imm     (i_imm),
    .cmd     (i_cmd),
    .i_rd    (i_i_rd),
    .busy    (i_busy),
    .i_error (i_i_error),
    .res     (i_res),
    .jmp     (i_jmp),
    .o_rd    (i_o_rd),
    .valid   (i_valid),
    .o_error (i_o_error),
    .req     (i_req),
    .clear   (i_clear)
);
<name>: entity work.alu2
   port map (
        clk      => i_clk,
        rst_n    => i_rst_n,
        arg0     => i_arg0,
        arg1     => i_arg1,
        addr     => i_addr,
        imm      => i_imm,
        cmd      => i_cmd,
        i_rd     => i_i_rd,
        busy     => i_busy,
        i_error  => i_i_error,
        res      => i_res,
        jmp      => i_jmp,
        o_rd     => i_o_rd,
        valid    => i_valid,
        o_error  => i_o_error,
        req      => i_req,
        clear    => i_clear
    );

Source

Raw source Full file

/*
 *  File :      rtl/core/alu/alu2.sv
 *
 *  Author :    l.heywang <leonard.heywang@proton.me>
 *  Date :      25/10.2025
 *  
 *  Brief :     This file define the ALU2 module, the one
 *              who's charged to compute multiplication, 
 *              divisions and bit shifts. It's result may
 *              take multiple cycles to get finished, up to
 *              ~80 for the division !
 *
 *              This ALU depends on three module, each handling
 *              one operation, due to the relative complexity of
 *              theses.
 */

`timescale 1ns / 1ps

import core_config_pkg::XLEN;
import core_config_pkg::REG_ADDR_W;
import core_config_pkg::alu_commands_t;

module alu2 (
    input  logic                                                  clk,
    input  logic                                                  rst_n,
    input  logic          [      (core_config_pkg::XLEN - 1) : 0] arg0,
    input  logic          [      (core_config_pkg::XLEN - 1) : 0] arg1,
    /* verilator lint_off UNUSEDSIGNAL */
    input  logic          [      (core_config_pkg::XLEN - 1) : 0] addr,
    input  logic          [      (core_config_pkg::XLEN - 1) : 0] imm,
    /* verilator lint_off UNUSEDSIGNAL */
    input  alu_commands_t                                         cmd,
    input  logic          [(core_config_pkg::REG_ADDR_W - 1) : 0] i_rd,
    output logic                                                  busy,
    output logic                                                  i_error,
    output logic          [      (core_config_pkg::XLEN - 1) : 0] res,
    /* verilator lint_off UNDRIVEN */
    output logic          [      (core_config_pkg::XLEN - 1) : 0] jmp,
    /* verilator lint_off UNDRIVEN */
    output logic          [(core_config_pkg::REG_ADDR_W - 1) : 0] o_rd,
    output logic                                                  valid,
    output logic                                                  o_error,
    output logic                                                  req,
    input  logic                                                  clear
);
    typedef enum logic [1:0] {
        IDLE  = 2'b00,
        WAIT1 = 2'b01,
        WAIT2 = 2'b10,
        OUT   = 2'b11
    } state_t;

    state_t                                                state;
    state_t                                                next_state;

    // Registered data
    logic          [      (core_config_pkg::XLEN - 1) : 0] r_arg0;
    logic          [      (core_config_pkg::XLEN - 1) : 0] r_arg1;
    logic          [(core_config_pkg::REG_ADDR_W - 1) : 0] r_i_rd;
    alu_commands_t                                         r_cmd;

    // Generics
    logic                                                  unknown_instr;
    logic          [      (core_config_pkg::XLEN - 1) : 0] next_res;
    logic          [      (core_config_pkg::XLEN - 1) : 0] res_int;

    // Multiplier signals
    logic                                                  mul_start;
    logic                                                  mul_done;
    logic                                                  mul_signed_a;
    logic                                                  mul_signed_b;
    logic          [((2 * core_config_pkg::XLEN) - 1) : 0] mul_product;

    logic                                                  next_mul_start;
    logic                                                  next_mul_signed_a;
    logic                                                  next_mul_signed_b;
    logic                                                  out_mul_start;
    logic                                                  out_mul_signed_a;
    logic                                                  out_mul_signed_b;

    // Divider signals
    logic                                                  div_start;
    logic                                                  div_done;
    logic                                                  div_signed;
    logic          [      (core_config_pkg::XLEN - 1) : 0] div_quotient;
    logic          [      (core_config_pkg::XLEN - 1) : 0] div_remainder;
    logic                                                  div_by_zero;

    logic                                                  next_div_start;
    logic                                                  next_div_signed;
    logic                                                  out_div_start;
    logic                                                  out_div_signed;

    // Shifter signals
    logic                                                  shift_start;
    logic                                                  shift_done;
    logic                                                  shift_left;
    logic                                                  shift_arithmetic;
    logic          [      (core_config_pkg::XLEN - 1) : 0] shift_result;

    logic                                                  next_shift_start;
    logic                                                  next_shift_left;
    logic                                                  next_shift_arithmetic;
    logic                                                  out_shift_start;
    logic                                                  out_shift_left;
    logic                                                  out_shift_arithmetic;

    // Operation decoder
    always_comb begin
        next_mul_start        = 1'b0;
        next_div_start        = 1'b0;
        next_shift_start      = 1'b0;
        next_mul_signed_a     = 1'b0;
        next_mul_signed_b     = 1'b0;
        next_div_signed       = 1'b0;
        next_shift_left       = 1'b0;
        next_shift_arithmetic = 1'b0;
        unknown_instr         = 1'b0;

        unique case (cmd)
            // Multiplication operations
            core_config_pkg::c_MUL: begin
                next_mul_start    = 1'b1;
                next_mul_signed_a = 1'b1;
                next_mul_signed_b = 1'b1;
                unknown_instr     = 1'b0;

            end
            core_config_pkg::c_MULH: begin
                next_mul_start    = 1'b1;
                next_mul_signed_a = 1'b1;
                next_mul_signed_b = 1'b1;
                unknown_instr     = 1'b0;

            end
            core_config_pkg::c_MULHSU: begin
                next_mul_start    = 1'b1;
                next_mul_signed_a = 1'b1;
                next_mul_signed_b = 1'b0;
                unknown_instr     = 1'b0;

            end
            core_config_pkg::c_MULHU: begin
                next_mul_start    = 1'b1;
                next_mul_signed_a = 1'b0;
                next_mul_signed_b = 1'b0;
                unknown_instr     = 1'b0;

            end
            // Division operations
            core_config_pkg::c_DIV: begin
                next_div_start  = 1'b1;
                next_div_signed = 1'b1;
                unknown_instr   = 1'b0;

            end
            core_config_pkg::c_DIVU: begin
                next_div_start  = 1'b1;
                next_div_signed = 1'b0;
                unknown_instr   = 1'b0;

            end
            core_config_pkg::c_REM: begin
                next_div_start  = 1'b1;
                next_div_signed = 1'b1;
                unknown_instr   = 1'b0;

            end
            core_config_pkg::c_REMU: begin
                next_div_start  = 1'b1;
                next_div_signed = 1'b0;
                unknown_instr   = 1'b0;

            end
            // Shift operations
            core_config_pkg::c_SLL: begin
                next_shift_start      = 1'b1;
                next_shift_left       = 1'b1;
                next_shift_arithmetic = 1'b0;
                unknown_instr         = 1'b0;

            end
            core_config_pkg::c_SRL: begin
                next_shift_start      = 1'b1;
                next_shift_left       = 1'b0;
                next_shift_arithmetic = 1'b0;
                unknown_instr         = 1'b0;

            end
            core_config_pkg::c_SRA: begin
                next_shift_start      = 1'b1;
                next_shift_left       = 1'b0;
                next_shift_arithmetic = 1'b1;
                unknown_instr         = 1'b0;

            end
            default: unknown_instr = 1'b1;
        endcase
    end

    /*
     *  Some sync logic to handle the FSM states and evolution
     */
    always_ff @(posedge clk or negedge rst_n) begin

        if (!rst_n) begin

            // State
            state            <= IDLE;

            // Registering inputs
            r_arg0           <= 32'b0;
            r_arg1           <= 32'b0;
            r_cmd            <= core_config_pkg::c_NONE;
            r_i_rd           <= 5'b0;

            // Registering the outputs of the first decoder
            mul_start        <= 1'b0;
            div_start        <= 1'b0;
            shift_start      <= 1'b0;

            mul_signed_a     <= 1'b0;
            mul_signed_b     <= 1'b0;

            div_signed       <= 1'b0;

            shift_left       <= 1'b0;
            shift_arithmetic <= 1'b0;

            res_int          <= 32'b0;

        end else begin

            state   <= next_state;
            res_int <= next_res;

            if (state == IDLE) begin

                // Registering inputs
                r_arg0           <= arg0;
                r_arg1           <= arg1;
                r_cmd            <= cmd;
                r_i_rd           <= i_rd;

                // Registering the outputs of the first decoder
                mul_start        <= next_mul_start;
                div_start        <= next_div_start;
                shift_start      <= next_shift_start;

                mul_signed_a     <= next_mul_signed_a;
                mul_signed_b     <= next_mul_signed_b;

                div_signed       <= next_div_signed;

                shift_left       <= next_shift_left;
                shift_arithmetic <= next_shift_arithmetic;

            end
        end
    end

    /*
     *  State evolution logic
     */
    always_comb begin

        unique case (state)

            IDLE: next_state = (unknown_instr) ? IDLE : WAIT1;

            WAIT1: next_state = WAIT2;
            WAIT2: begin

                unique case (r_cmd)

                    core_config_pkg::c_MUL,
                    core_config_pkg::c_MULH,
                    core_config_pkg::c_MULHSU,
                    core_config_pkg::c_MULHU:
                    next_state = (mul_done) ? OUT : state;

                    core_config_pkg::c_DIV,
                    core_config_pkg::c_DIVU,
                    core_config_pkg::c_REM,
                    core_config_pkg::c_REMU:
                    next_state = (div_done) ? OUT : state;

                    core_config_pkg::c_SLL, core_config_pkg::c_SRL, core_config_pkg::c_SRA:
                    next_state = (shift_done) ? OUT : state;
                    default: next_state = IDLE;

                endcase

            end

            OUT: next_state = (clear) ? IDLE : state;

        endcase

    end

    /*
     *  Controlling the output logic
     */
    always_comb begin

        unique case (state)

            IDLE: begin

                out_mul_start        = 1'b0;
                out_div_start        = 1'b0;
                out_shift_start      = 1'b0;

                out_div_signed       = 1'b0;
                out_mul_signed_a     = 1'b0;
                out_mul_signed_b     = 1'b0;
                out_shift_arithmetic = 1'b0;
                out_shift_left       = 1'b0;

                next_res             = 32'b0;
                res                  = 32'b0;

                busy                 = 1'b0;
                o_rd                 = 5'b0;
                valid                = 1'b0;

            end
            WAIT1: begin

                out_mul_start        = mul_start;
                out_div_start        = div_start;
                out_shift_start      = shift_start;

                out_div_signed       = div_signed;
                out_mul_signed_a     = mul_signed_a;
                out_mul_signed_b     = mul_signed_b;
                out_shift_arithmetic = shift_arithmetic;
                out_shift_left       = shift_left;

                next_res             = 32'b0;
                res                  = 32'b0;

                busy                 = 1'b1;
                o_rd                 = 5'b0;
                valid                = 1'b0;

            end
            WAIT2: begin

                out_mul_start        = 1'b0;
                out_div_start        = 1'b0;
                out_shift_start      = 1'b0;

                // We hold theses signals all along the computation
                out_div_signed       = div_signed;
                out_mul_signed_a     = mul_signed_a;
                out_mul_signed_b     = mul_signed_b;
                out_shift_arithmetic = shift_arithmetic;
                out_shift_left       = shift_left;

                busy                 = 1'b1;
                o_rd                 = 5'b0;
                valid                = 1'b0;

                res                  = 32'b0;

                unique case (r_cmd)

                    core_config_pkg::c_MUL: next_res = mul_product[(core_config_pkg::XLEN-1) : 0];
                    core_config_pkg::c_MULH, core_config_pkg::c_MULHSU, core_config_pkg::c_MULHU:
                    next_res = mul_product[((2*core_config_pkg::XLEN)-1) : core_config_pkg::XLEN];

                    core_config_pkg::c_DIV, core_config_pkg::c_DIVU: next_res = div_quotient;
                    core_config_pkg::c_REM, core_config_pkg::c_REMU: next_res = div_remainder;

                    core_config_pkg::c_SLL, core_config_pkg::c_SRL, core_config_pkg::c_SRA:
                    next_res = shift_result;
                    default: next_res = 32'b0;

                endcase

            end
            OUT: begin

                out_mul_start        = 1'b0;
                out_div_start        = 1'b0;
                out_shift_start      = 1'b0;

                // We hold theses signals all along the computation
                out_div_signed       = 1'b0;
                out_mul_signed_a     = 1'b0;
                out_mul_signed_b     = 1'b0;
                out_shift_arithmetic = 1'b0;
                out_shift_left       = 1'b0;

                busy                 = 1'b1;
                o_rd                 = r_i_rd;
                valid                = 1'b1;

                next_res             = res_int;
                res                  = res_int;

            end
        endcase
    end

    /*
     *  Static assignements
     */

    assign i_error = unknown_instr & ~busy;
    assign o_error = div_by_zero;  // This is the only source of error in the ALU.
    assign req     = 1'b0;  // Unused signal here

    /*
     *  Then, instantiate the different sub-elements, for each operations.
     */

    booth multiplier (
        .clk     (clk),
        .rst_n   (rst_n),
        .start   (out_mul_start),
        .X_signed(out_mul_signed_a),
        .Y_signed(out_mul_signed_b),
        .X       (r_arg0),
        .Y       (r_arg1),
        .valid   (mul_done),
        .Z       (mul_product)
    );

    srt divider (
        .clk            (clk),
        .rst_n          (rst_n),
        .start          (out_div_start),
        .dividend_signed(out_div_signed),
        .divisor_signed (out_div_signed),
        .dividend       (r_arg0),
        .divisor        (r_arg1),
        .valid          (div_done),
        .quotient       (div_quotient),
        .remainder      (div_remainder),
        .div_by_zero    (div_by_zero)
    );

    shift shifter (
        .clk         (clk),
        .rst_n       (rst_n),
        .start       (out_shift_start),
        .data_in     (r_arg0),
        .shift_amount(r_arg1[($clog2(core_config_pkg::XLEN)-1) : 0]),
        .shift_left  (out_shift_left),
        .arithmetic  (out_shift_arithmetic),
        .data_out    (shift_result),
        .done        (shift_done)
    );

endmodule

alu4

alu4.sv

alu4 module

Imports

Imports 4 elements

  • core_config_pkg XLEN alu_commands_t CSR_ADDR_W REG_ADDR_W

Ports

Ports list 23 elements

Name

Direction

Description

Type

Size

Kind

Polarity

Fanout

clk

Input

logic

-

Combinatorial

-

Fanout : 0

rst_n

Input

logic

-

Combinatorial

Negative

Fanout : 0

arg0

Input

verilator lint_off UNUSEDSIGNAL

logic

[(core_config_pkg::XLEN - 1) : 0]

Combinatorial

-

Fanout : 0

arg1

Input

logic

[(core_config_pkg::XLEN - 1) : 0]

Combinatorial

-

Fanout : 0

addr

Input

logic

[(core_config_pkg::XLEN - 1) : 0]

Combinatorial

-

Fanout : 0

imm

Input

verilator lint_on UNUSEDSIGNAL

logic

[(core_config_pkg::XLEN - 1) : 0]

Combinatorial

-

Fanout : 0

cmd

Input

alu_commands_t

-

Combinatorial

-

Fanout : 0

busy

Output

logic

-

Combinatorial

-

Fanout : 0

res

Output

verilator lint_off UNDRIVEN

logic

[(core_config_pkg::XLEN - 1) : 0]

Combinatorial

-

Fanout : 0

jmp

Output

verilator lint_off UNDRIVEN

logic

[(core_config_pkg::XLEN - 1) : 0]

Combinatorial

-

Fanout : 0

valid

Output

logic

-

Combinatorial

-

Fanout : 0

req

Output

logic

-

Combinatorial

-

Fanout : 0

clear

Input

logic

-

Combinatorial

-

Fanout : 0

i 2 elements

Name

Direction

Description

Type

Size

Kind

Polarity

Fanout

i_rd

Input

logic

[(core_config_pkg::REG_ADDR_W - 1) : 0]

Combinatorial

-

Fanout : 0

i_error

Output

logic

-

Combinatorial

-

Fanout : 0

o 2 elements

Name

Direction

Description

Type

Size

Kind

Polarity

Fanout

o_rd

Output

logic

[(core_config_pkg::REG_ADDR_W - 1) : 0]

Combinatorial

-

Fanout : 0

o_error

Output

logic

-

Register clk rst_n

-

Fanout : 0

csr 6 elements

Name

Direction

Description

Type

Size

Kind

Polarity

Fanout

csr_wa

Output

logic

[(core_config_pkg::CSR_ADDR_W - 1) : 0]

Combinatorial

-

Fanout : 0

csr_ra

Output

logic

[(core_config_pkg::CSR_ADDR_W - 1) : 0]

Combinatorial

-

Fanout : 0

csr_we

Output

logic

-

Combinatorial

-

Fanout : 0

csr_wd

Output

logic

[(core_config_pkg::XLEN - 1) : 0]

Combinatorial

-

Fanout : 0

csr_rd

Input

logic

[(core_config_pkg::XLEN - 1) : 0]

Register clk

-

Fanout : 0

csr_err

Input

logic

-

Register clk

-

Fanout : 0

Processes

Process list 4 elements

• Process 1 Combinatorial

-

• Process 2 Clocked Clocked by clk Reset by rst_n

-

• Process 3 Combinatorial

State evolution logic

• Process 4 Combinatorial

Output handling logic

Signals

Signals list 16 elements

Name

Description

Type

Size

Kind

Fanout

state

FSM status

state_t

-

Register clk rst_n

Fanout : 0

write

Access mode

logic

-

Register clk rst_n

Fanout : 0

set

logic

-

Register clk rst_n

Fanout : 0

clearb

logic

-

Register clk rst_n

Fanout : 0

value

logic

[(core_config_pkg::XLEN - 1) : 0]

Register clk rst_n

Fanout : 0

readback

logic

[(core_config_pkg::XLEN - 1) : 0]

Register clk rst_n

Fanout : 0

address

logic

[(core_config_pkg::CSR_ADDR_W - 1) : 0]

Register clk rst_n

Fanout : 0

r_rd

Data transfer

logic

[(core_config_pkg::REG_ADDR_W - 1) : 0]

Register clk rst_n

Fanout : 0

unknown_instr

Identification

logic

-

Combinatorial

Fanout : 0

next 7 elements

Name

Description

Type

Size

Kind

Fanout

next_state

state_t

-

Register clk rst_n

Fanout : 0

next_write

logic

-

Register clk rst_n

Fanout : 0

next_set

logic

-

Register clk rst_n

Fanout : 0

next_clearb

logic

-

Register clk rst_n

Fanout : 0

next_value

logic

[(core_config_pkg::XLEN - 1) : 0]

Register clk rst_n

Fanout : 0

next_address

logic

[(core_config_pkg::CSR_ADDR_W - 1) : 0]

Register clk rst_n

Fanout : 0

next_rd

logic

[(core_config_pkg::REG_ADDR_W - 1) : 0]

Register clk rst_n

Fanout : 0

Instantiation templates

Instantiation templates 2 elements

alu4 <name> (
    .clk     (i_clk),
    .rst_n   (i_rst_n),
    .arg0    (i_arg0),
    .arg1    (i_arg1),
    .addr    (i_addr),
    .imm     (i_imm),
    .cmd     (i_cmd),
    .i_rd    (i_i_rd),
    .busy    (i_busy),
    .i_error (i_i_error),
    .res     (i_res),
    .jmp     (i_jmp),
    .o_rd    (i_o_rd),
    .valid   (i_valid),
    .o_error (i_o_error),
    .req     (i_req),
    .clear   (i_clear),
    .csr_wa  (i_csr_wa),
    .csr_ra  (i_csr_ra),
    .csr_we  (i_csr_we),
    .csr_wd  (i_csr_wd),
    .csr_rd  (i_csr_rd),
    .csr_err (i_csr_err)
);
<name>: entity work.alu4
   port map (
        clk      => i_clk,
        rst_n    => i_rst_n,
        arg0     => i_arg0,
        arg1     => i_arg1,
        addr     => i_addr,
        imm      => i_imm,
        cmd      => i_cmd,
        i_rd     => i_i_rd,
        busy     => i_busy,
        i_error  => i_i_error,
        res      => i_res,
        jmp      => i_jmp,
        o_rd     => i_o_rd,
        valid    => i_valid,
        o_error  => i_o_error,
        req      => i_req,
        clear    => i_clear,
        csr_wa   => i_csr_wa,
        csr_ra   => i_csr_ra,
        csr_we   => i_csr_we,
        csr_wd   => i_csr_wd,
        csr_rd   => i_csr_rd,
        csr_err  => i_csr_err
    );

Source

Raw source Full file

/*
 *  File :      rtl/core/alu/alu4.sv
 *
 *  Author :    l.heywang <leonard.heywang@proton.me>
 *  Date :      25/10.2025
 *  
 *  Brief :     This file define the ALU4 module, which handle the
 *              CSR registers accesses. It handle the swaps and masks
 *              required for theses operations.
 */

`timescale 1ns / 1ps

import core_config_pkg::XLEN;
import core_config_pkg::alu_commands_t;
import core_config_pkg::CSR_ADDR_W;
import core_config_pkg::REG_ADDR_W;

module alu4 (
    input  logic                                                  clk,
    input  logic                                                  rst_n,
    input  logic          [      (core_config_pkg::XLEN - 1) : 0] arg0,
    /* verilator lint_off UNUSEDSIGNAL */
    input  logic          [      (core_config_pkg::XLEN - 1) : 0] arg1,
    input  logic          [      (core_config_pkg::XLEN - 1) : 0] addr,
    input  logic          [      (core_config_pkg::XLEN - 1) : 0] imm,
    /* verilator lint_on UNUSEDSIGNAL */
    input  alu_commands_t                                         cmd,
    input  logic          [(core_config_pkg::REG_ADDR_W - 1) : 0] i_rd,
    output logic                                                  busy,
    output logic                                                  i_error,
    output logic          [      (core_config_pkg::XLEN - 1) : 0] res,
    /* verilator lint_off UNDRIVEN */
    output logic          [      (core_config_pkg::XLEN - 1) : 0] jmp,
    /* verilator lint_off UNDRIVEN */
    output logic          [(core_config_pkg::REG_ADDR_W - 1) : 0] o_rd,
    output logic                                                  valid,
    output logic                                                  o_error,
    output logic                                                  req,
    input  logic                                                  clear,
    output logic          [(core_config_pkg::CSR_ADDR_W - 1) : 0] csr_wa,
    output logic          [(core_config_pkg::CSR_ADDR_W - 1) : 0] csr_ra,
    output logic                                                  csr_we,
    output logic          [      (core_config_pkg::XLEN - 1) : 0] csr_wd,
    input  logic          [      (core_config_pkg::XLEN - 1) : 0] csr_rd,
    input  logic                                                  csr_err

);
    /*
     *  Custom types
     */
    typedef enum logic [2:0] {
        IDLE,
        READ,
        WRITE
    } state_t;

    /*
     *  Storage registers
     */
    // FSM status
    state_t state;
    state_t next_state;

    // Access mode
    logic write, next_write;
    logic set, next_set;
    logic clearb, next_clearb;
    logic [(core_config_pkg::XLEN - 1) : 0] value;
    logic [(core_config_pkg::XLEN - 1) : 0] next_value;
    logic [(core_config_pkg::XLEN - 1) : 0] readback;
    logic [(core_config_pkg::CSR_ADDR_W - 1) : 0] address;
    logic [(core_config_pkg::CSR_ADDR_W - 1) : 0] next_address;

    // Data transfer
    logic [(core_config_pkg::REG_ADDR_W - 1) : 0] r_rd, next_rd;

    // Identification
    logic unknown_instr;


    /*
     *  First, a comb block to identify the instruction and output 
     *  pre-control signals
     */
    always_comb begin

        unique case (cmd)

            core_config_pkg::c_CSRRW: begin

                next_write    = 1'b1;
                next_set      = 1'b0;
                next_clearb   = 1'b0;
                next_address  = imm[(core_config_pkg::CSR_ADDR_W-1) : 0];
                next_value    = arg0;
                next_rd       = i_rd;
                unknown_instr = 1'b0;

            end

            core_config_pkg::c_CSRRS: begin

                next_write    = 1'b0;
                next_set      = 1'b1;
                next_clearb   = 1'b0;
                next_address  = imm[(core_config_pkg::CSR_ADDR_W-1) : 0];
                next_value    = arg0;
                next_rd       = i_rd;
                unknown_instr = 1'b0;

            end

            core_config_pkg::c_CSRRC: begin

                next_write    = 1'b0;
                next_set      = 1'b0;
                next_clearb   = 1'b1;
                next_address  = imm[(core_config_pkg::CSR_ADDR_W-1) : 0];
                next_value    = ~arg0;  // Already performing the NOT operation here !
                next_rd       = i_rd;
                unknown_instr = 1'b0;

            end

            default: begin

                next_write    = 1'b0;
                next_set      = 1'b0;
                next_clearb   = 1'b0;
                next_address  = 12'b0;
                next_value    = 32'b0;
                next_rd       = 5'b0;
                unknown_instr = 1'b1;

            end

        endcase
    end

    /*
     *  Synchronous logic to handle the states
     */

    always_ff @(posedge clk or negedge rst_n) begin

        if (!rst_n) begin

            state    <= IDLE;

            write    <= 1'b0;
            set      <= 1'b0;
            clearb   <= 1'b0;
            value    <= 32'b0;
            readback <= 32'b0;

        end else begin

            state   <= next_state;
            o_error <= csr_err;

            if (state == IDLE) begin

                r_rd     <= next_rd;

                write    <= next_write;
                set      <= next_set;
                clearb   <= next_clearb;
                value    <= next_value;
                address  <= next_address;
                readback <= 32'b0;

            end else if (state == READ) begin

                readback <= csr_rd;

            end
        end
    end

    /* 
     *  State evolution logic
     */
    always_comb begin

        unique case (state)

            IDLE:  next_state = (unknown_instr) ? IDLE : READ;
            READ:  next_state = WRITE;
            WRITE: next_state = (clear) ? IDLE : WRITE;
        endcase
    end

    /*
     *  Output handling logic
     */
    always_comb begin

        unique case (state)

            IDLE: begin

                csr_wa = 12'b0;
                csr_ra = 12'b0;
                csr_we = 1'b0;
                csr_wd = 32'b0;

                req    = 1'b0;
                res    = 32'b0;
                busy   = 1'b0;
                o_rd   = 5'b0;
                valid  = 1'b0;

            end

            READ: begin

                csr_wa = 12'b0;
                csr_ra = address;
                csr_we = 1'b0;
                csr_wd = 32'b0;

                req    = 1'b0;
                res    = 32'b0;
                busy   = 1'b1;
                o_rd   = 5'b0;
                valid  = 1'b0;

            end

            WRITE: begin

                csr_wa = address;
                csr_ra = 12'b0;
                csr_we = 1'b1;

                unique case ({
                    write, set, clearb
                })

                    3'b100:  csr_wd = value;
                    3'b010:  csr_wd = readback | value;
                    3'b001:  csr_wd = readback & value;  // Value is already negated !
                    default: csr_wd = 32'b0;

                endcase

                req   = 1'b0;
                res   = readback;
                busy  = 1'b1;
                o_rd  = r_rd;
                valid = 1'b1;

            end

        endcase
    end

    assign i_error = unknown_instr;

endmodule

alu5

alu5.sv

alu5 module

Imports

Imports 2 elements

  • core_config_pkg XLEN alu_commands_t

Ports

Ports list 24 elements

Name

Direction

Description

Type

Size

Kind

Polarity

Fanout

clk

Input

Standard interface

logic

-

Combinatorial

-

Fanout : 0

rst_n

Input

logic

-

Combinatorial

Negative

Fanout : 0

arg0

Input

Issuer interface

logic

[(core_config_pkg::XLEN - 1) : 0]

Combinatorial

-

Fanout : 0

arg1

Input

verilator lint_off UNUSEDSIGNAL

logic

[(core_config_pkg::XLEN - 1) : 0]

Combinatorial

-

Fanout : 0

addr

Input

verilator lint_on UNUSEDSIGNAL

logic

[(core_config_pkg::XLEN - 1) : 0]

Combinatorial

-

Fanout : 0

imm

Input

logic

[(core_config_pkg::XLEN - 1) : 0]

Combinatorial

-

Fanout : 0

cmd

Input

alu_commands_t

-

Combinatorial

-

Fanout : 0

busy

Output

logic

-

Combinatorial

-

Fanout : 0

res

Output

Commiter interface

logic

[(core_config_pkg::XLEN - 1) : 0]

Combinatorial

-

Fanout : 0

jmp

Output

verilator lint_off UNDRIVEN

logic

[(core_config_pkg::XLEN - 1) : 0]

Combinatorial

-

Fanout : 0

valid

Output

logic

-

Combinatorial

-

Fanout : 0

req

Output

logic

-

Combinatorial

-

Fanout : 0

clear

Input

logic

-

Combinatorial

-

Fanout : 0

i 2 elements

Name

Direction

Description

Type

Size

Kind

Polarity

Fanout

i_rd

Input

logic

[(core_config_pkg::REG_ADDR_W - 1) : 0]

Register clk

-

Fanout : 0

i_error

Output

logic

-

Combinatorial

-

Fanout : 0

o 2 elements

Name

Direction

Description

Type

Size

Kind

Polarity

Fanout

o_rd

Output

verilator lint_off UNDRIVEN

logic

[(core_config_pkg::REG_ADDR_W - 1) : 0]

Combinatorial

-

Fanout : 0

o_error

Output

logic

-

Combinatorial

-

Fanout : 0

mem 7 elements

Name

Direction

Description

Type

Size

Kind

Polarity

Fanout

mem_addr

Output

Additionnal interface for the memory (which perform IO trough this ALU and only THIS ALU.)

logic

[(core_config_pkg::XLEN - 1) : 0]

Combinatorial

-

Fanout : 0

mem_byteen

Output

logic

[((core_config_pkg::XLEN / 8) - 1) : 0]

Combinatorial

-

Fanout : 0

mem_we

Output

logic

-

Combinatorial

-

Fanout : 0

mem_req

Output

logic

-

Combinatorial

-

Fanout : 0

mem_wdata

Output

logic

[(core_config_pkg::XLEN - 1) : 0]

Combinatorial

-

Fanout : 0

mem_rdata

Input

logic

[(core_config_pkg::XLEN - 1) : 0]

Combinatorial

-

Fanout : 0

mem_err

Input

logic

-

Register clk

-

Fanout : 0

Processes

Process list 4 elements

• Process 1 Combinatorial

First, configure the different parameters of the request

• Process 2 Clocked Clocked by clk Reset by rst_n

Some sync logic to handle the FSM states and evolution

• Process 3 Combinatorial

-

• Process 4 Combinatorial

-

Signals

Signals list 22 elements

Name

Description

Type

Size

Kind

Fanout

tmp_res

Storages types

logic

[(core_config_pkg::XLEN - 1) : 0]

Combinatorial

Fanout : 0

sext_req

logic

-

Register clk rst_n

Fanout : 0

address

logic

[(core_config_pkg::XLEN - 1) : 0]

Register clk rst_n

Fanout : 0

data

logic

[(core_config_pkg::XLEN - 1) : 0]

Register clk rst_n

Fanout : 0

data2

logic

[(core_config_pkg::XLEN - 1) : 0]

Register clk rst_n

Fanout : 0

bytes

logic

[((core_config_pkg::XLEN / 8) - 1) : 0]

Register clk rst_n

Fanout : 0

we

logic

-

Register clk rst_n

Fanout : 0

inp_req

logic

-

Register clk rst_n

Fanout : 0

half_val

logic

[15 : 0]

Combinatorial

Fanout : 0

byte_val

logic

[7 : 0]

Combinatorial

Fanout : 0

state

FSM sync

MEM_FSM

-

Register clk rst_n

Fanout : 0

next_state

MEM_FSM

-

Register clk rst_n

Fanout : 0

unknown_instr

logic

-

Combinatorial

Fanout : 0

r 9 elements

Name

Description

Type

Size

Kind

Fanout

r_rd

logic

[(core_config_pkg::REG_ADDR_W - 1) : 0]

Register clk rst_n

Fanout : 0

r_bytes

Registered logic

logic

[((core_config_pkg::XLEN / 8) - 1) : 0]

Register clk rst_n

Fanout : 0

r_address

logic

[(core_config_pkg::XLEN - 1) : 0]

Register clk rst_n

Fanout : 0

r_data

logic

[(core_config_pkg::XLEN - 1) : 0]

Register clk rst_n

Fanout : 0

r_data2

logic

[(core_config_pkg::XLEN - 1) : 0]

Register clk rst_n

Fanout : 0

r_sext_req

logic

-

Register clk rst_n

Fanout : 0

r_we

logic

-

Register clk rst_n

Fanout : 0

r_err

logic

-

Register clk rst_n

Fanout : 0

r_inp_req

logic

-

Register clk rst_n

Fanout : 0

Instantiation templates

Instantiation templates 2 elements

alu5 <name> (
    .clk        (i_clk),
    .rst_n      (i_rst_n),
    .arg0       (i_arg0),
    .arg1       (i_arg1),
    .addr       (i_addr),
    .imm        (i_imm),
    .cmd        (i_cmd),
    .i_rd       (i_i_rd),
    .busy       (i_busy),
    .i_error    (i_i_error),
    .res        (i_res),
    .jmp        (i_jmp),
    .o_rd       (i_o_rd),
    .valid      (i_valid),
    .o_error    (i_o_error),
    .req        (i_req),
    .clear      (i_clear),
    .mem_addr   (i_mem_addr),
    .mem_byteen (i_mem_byteen),
    .mem_we     (i_mem_we),
    .mem_req    (i_mem_req),
    .mem_wdata  (i_mem_wdata),
    .mem_rdata  (i_mem_rdata),
    .mem_err    (i_mem_err)
);
<name>: entity work.alu5
   port map (
        clk         => i_clk,
        rst_n       => i_rst_n,
        arg0        => i_arg0,
        arg1        => i_arg1,
        addr        => i_addr,
        imm         => i_imm,
        cmd         => i_cmd,
        i_rd        => i_i_rd,
        busy        => i_busy,
        i_error     => i_i_error,
        res         => i_res,
        jmp         => i_jmp,
        o_rd        => i_o_rd,
        valid       => i_valid,
        o_error     => i_o_error,
        req         => i_req,
        clear       => i_clear,
        mem_addr    => i_mem_addr,
        mem_byteen  => i_mem_byteen,
        mem_we      => i_mem_we,
        mem_req     => i_mem_req,
        mem_wdata   => i_mem_wdata,
        mem_rdata   => i_mem_rdata,
        mem_err     => i_mem_err
    );

Source

Raw source Full file

/*
 *  File :      rtl/core/alu/alu5.sv
 *
 *  Author :    l.heywang <leonard.heywang@proton.me>
 *  Date :      25/10.2025
 *  
 *  Brief :     This file define the ALU5 module, which is
 *              exclusively charged to handle the memory accesses.
 *              It perform timing and sign extensions if needed.
 */

`timescale 1ns / 1ps

import core_config_pkg::XLEN;
import core_config_pkg::alu_commands_t;

module alu5 (
    // Standard interface
    input logic clk,
    input logic rst_n,

    // Issuer interface
    input  logic          [      (core_config_pkg::XLEN - 1) : 0] arg0,
    input  logic          [      (core_config_pkg::XLEN - 1) : 0] arg1,
    /* verilator lint_off UNUSEDSIGNAL */
    input  logic          [      (core_config_pkg::XLEN - 1) : 0] addr,
    /* verilator lint_on UNUSEDSIGNAL */
    input  logic          [      (core_config_pkg::XLEN - 1) : 0] imm,
    input  alu_commands_t                                         cmd,
    input  logic          [(core_config_pkg::REG_ADDR_W - 1) : 0] i_rd,
    output logic                                                  busy,
    output logic                                                  i_error,

    // Commiter interface
    output logic [      (core_config_pkg::XLEN - 1) : 0] res,
    /* verilator lint_off UNDRIVEN */
    output logic [      (core_config_pkg::XLEN - 1) : 0] jmp,
    /* verilator lint_off UNDRIVEN */
    output logic [(core_config_pkg::REG_ADDR_W - 1) : 0] o_rd,
    output logic                                         valid,
    output logic                                         o_error,
    output logic                                         req,
    input  logic                                         clear,

    // Additionnal interface for the memory (which perform IO trough this ALU and only THIS ALU.)
    output logic [      (core_config_pkg::XLEN - 1) : 0] mem_addr,
    output logic [((core_config_pkg::XLEN / 8) - 1) : 0] mem_byteen,
    output logic                                         mem_we,
    output logic                                         mem_req,
    output logic [      (core_config_pkg::XLEN - 1) : 0] mem_wdata,
    input  logic [      (core_config_pkg::XLEN - 1) : 0] mem_rdata,
    input  logic                                         mem_err
);

    typedef enum logic [1:0] {
        IDLE,
        REQ,
        WAIT,
        OUT
    } MEM_FSM;

    /*
     *  Storages types
     */
    logic   [      (core_config_pkg::XLEN - 1) : 0] tmp_res;
    logic                                           sext_req;
    logic   [      (core_config_pkg::XLEN - 1) : 0] address;
    logic   [      (core_config_pkg::XLEN - 1) : 0] data;
    logic   [      (core_config_pkg::XLEN - 1) : 0] data2;
    logic   [((core_config_pkg::XLEN / 8) - 1) : 0] bytes;
    logic                                           we;
    logic   [(core_config_pkg::REG_ADDR_W - 1) : 0] r_rd;
    logic                                           inp_req;
    logic   [                               15 : 0] half_val;
    logic   [                                7 : 0] byte_val;

    // Registered logic
    logic   [((core_config_pkg::XLEN / 8) - 1) : 0] r_bytes;
    logic   [      (core_config_pkg::XLEN - 1) : 0] r_address;
    logic   [      (core_config_pkg::XLEN - 1) : 0] r_data;
    logic   [      (core_config_pkg::XLEN - 1) : 0] r_data2;
    logic                                           r_sext_req;
    logic                                           r_we;
    logic                                           r_err;
    logic                                           r_inp_req;


    // FSM sync
    MEM_FSM                                         state;
    MEM_FSM                                         next_state;
    logic                                           unknown_instr;

    /*
     *  First, configure the different parameters of the request
     */
    always_comb begin

        tmp_res = arg0 + imm;
        address = {tmp_res[(core_config_pkg::XLEN-1) : 2], 2'b0};

        unique case (cmd)

            core_config_pkg::c_LB: begin
                // Calculation
                bytes         = 4'b0001 << tmp_res[1:0];
                sext_req      = 1'b1;
                we            = 1'b0;
                data          = 32'b0;
                inp_req       = 1'b1;

                // Setting flags
                unknown_instr = 1'b0;
            end
            core_config_pkg::c_LH: begin
                // Calculation
                bytes         = 4'b0011 << (tmp_res[1] * 2);
                sext_req      = 1'b1;
                we            = 1'b0;
                data          = 32'b0;
                inp_req       = 1'b1;

                // Setting flags
                unknown_instr = 0;
            end
            core_config_pkg::c_LW: begin
                // Calculation
                bytes         = 4'b1111;
                sext_req      = 1'b1;
                we            = 1'b0;
                data          = 32'b0;
                inp_req       = 1'b1;

                // Setting flags
                unknown_instr = 0;
            end
            core_config_pkg::c_LBU: begin
                // Calculation
                bytes         = 4'b0001 << tmp_res[1:0];
                sext_req      = 1'b0;
                we            = 1'b0;
                data          = 32'b0;
                inp_req       = 1'b1;

                // Setting flags
                unknown_instr = 1'b0;
            end
            core_config_pkg::c_LHU: begin
                // Calculation
                bytes         = 4'b0011 << (tmp_res[1] * 2);
                sext_req      = 1'b0;
                we            = 1'b0;
                data          = 32'b0;
                inp_req       = 1'b1;

                // Setting flags
                unknown_instr = 0;
            end
            core_config_pkg::c_SB: begin
                // Calculation
                bytes    = 4'b0001 << tmp_res[1:0];
                sext_req = 1'b0;
                we       = 1'b1;
                case (tmp_res[1:0])

                    2'b00:   data = {24'b0, arg1[7 : 0]};
                    2'b01:   data = {16'b0, arg1[7 : 0], 8'b0};
                    2'b10:   data = {8'b0, arg1[7 : 0], 16'b0};
                    2'b11:   data = {arg1[7 : 0], 24'b0};
                    default: data = 0;

                endcase
                inp_req       = 1'b0;

                // Setting flags
                unknown_instr = 1'b0;
            end
            core_config_pkg::c_SH: begin
                // Calculation
                bytes    = 4'b0011 << (tmp_res[1] * 2);
                sext_req = 1'b0;
                we       = 1'b1;

                if (tmp_res[1]) begin
                    data = {arg1[15 : 0], 16'b0};
                end else begin
                    data = {16'b0, arg1[15 : 0]};
                end
                inp_req       = 1'b0;

                // Setting flags
                unknown_instr = 1'b0;
            end
            core_config_pkg::c_SW: begin
                // Calculation
                bytes         = 4'b1111;
                sext_req      = 1'b0;
                we            = 1'b1;
                data          = arg1;
                inp_req       = 1'b0;

                // Setting flags
                unknown_instr = 1'b0;
            end
            default: begin
                // Calculation
                bytes         = 4'b0;
                sext_req      = 1'b0;
                we            = 1'b0;
                data          = 32'b0;
                inp_req       = 1'b0;

                // Setting flags
                unknown_instr = 1'b1;
            end
        endcase
    end

    /*
     *  Some sync logic to handle the FSM states and evolution
     */
    always_ff @(posedge clk or negedge rst_n) begin

        if (!rst_n) begin

            state      <= IDLE;
            r_bytes    <= 4'b0;
            r_address  <= 32'b0;
            r_sext_req <= 1'b0;
            r_we       <= 1'b0;
            r_data     <= 32'b0;
            r_rd       <= 5'b0;
            r_inp_req  <= 1'b0;
            r_data2    <= 32'b0;
            r_err      <= 1'b0;

        end else begin

            state <= next_state;
            r_err <= mem_err;

            if (state == IDLE) begin

                r_bytes    <= bytes;
                r_address  <= address;
                r_sext_req <= sext_req;
                r_we       <= we;
                r_data     <= data;
                r_rd       <= i_rd;
                r_inp_req  <= inp_req;

            end else if (state == WAIT) begin

                r_data2 <= data2;

            end
        end
    end

    always_comb begin

        unique case (state)

            IDLE: begin
                if (!unknown_instr) begin

                    next_state = REQ;

                end else begin

                    next_state = IDLE;

                end
            end
            REQ:  next_state = WAIT;
            WAIT: next_state = OUT;
            OUT: begin
                if (clear) begin

                    next_state = IDLE;

                end else begin

                    next_state = OUT;

                end
            end

        endcase
    end

    always_comb begin

        unique case (state)

            IDLE: begin

                res        = 32'b0;
                o_rd       = 5'b0;
                valid      = 1'b0;
                o_error    = r_err;
                req        = 1'b0;
                busy       = 1'b0;

                mem_addr   = 32'b0;
                mem_we     = 1'b0;
                mem_req    = 1'b0;
                mem_wdata  = 32'b0;
                mem_byteen = 4'b0;

                half_val   = 16'b0;
                byte_val   = 8'b0;
                data2      = 32'b0;

            end
            REQ: begin

                res        = 32'b0;
                o_rd       = 5'b0;
                valid      = 1'b0;
                o_error    = r_err;
                req        = 1'b0;
                busy       = 1'b1;

                mem_addr   = r_address;
                mem_we     = r_we;
                mem_req    = 1'b1;
                mem_wdata  = r_data;
                mem_byteen = r_bytes;

                half_val   = 16'b0;
                byte_val   = 8'b0;
                data2      = 32'b0;

            end
            WAIT: begin

                res        = 32'b0;
                o_rd       = r_rd;
                valid      = 1'b0;
                o_error    = r_err;
                req        = 1'b0;
                busy       = 1'b1;

                mem_addr   = r_address;
                mem_we     = r_we;
                mem_req    = 1'b1;
                mem_wdata  = r_data;
                mem_byteen = r_bytes;

                if (r_inp_req) begin
                    data2 = mem_rdata;
                end else begin
                    data2 = 32'b0;
                end

                half_val = 16'b0;
                byte_val = 8'b0;

            end
            OUT: begin

                o_rd       = r_rd;
                valid      = 1'b1;
                o_error    = r_err;
                req        = 1'b0;
                busy       = 1'b1;

                mem_addr   = 32'b0;
                mem_we     = 1'b0;
                mem_req    = 1'b0;
                mem_wdata  = 32'b0;
                mem_byteen = 4'b0;

                half_val   = 16'b0;
                byte_val   = 8'b0;
                res        = 32'b0;
                data2      = 32'b0;

                case (r_bytes)

                    // LB
                    4'b1111: res = r_data2;

                    // LH
                    4'b0011: half_val = r_data2[15 : 0];
                    4'b1100: half_val = r_data2[31 : 16];

                    // LB
                    4'b0001: byte_val = r_data2[7 : 0];
                    4'b0010: byte_val = r_data2[15 : 8];
                    4'b0100: byte_val = r_data2[23 : 16];
                    4'b1000: byte_val = r_data2[31 : 24];

                    default: begin

                        half_val = 16'b0;
                        byte_val = 8'b0;
                        res      = 32'b0;

                    end

                endcase

                /* 
                 *  Performing sign extension of the data
                 */
                if (r_sext_req) begin

                    casez (r_bytes)
                        4'b1111: res = r_data2;  // full word
                        4'b0011, 4'b1100: res = {{16{half_val[15]}}, half_val};  // LH
                        4'b0001, 4'b0010, 4'b0100, 4'b1000:
                        res = {{24{byte_val[7]}}, byte_val};  // LB
                        default: res = 32'b0;
                    endcase

                end else begin

                    casez (r_bytes)
                        4'b1111:                            res = r_data2;  // LW
                        4'b0011, 4'b1100:                   res = {16'b0, half_val};  // LHU
                        4'b0001, 4'b0010, 4'b0100, 4'b1000: res = {24'b0, byte_val};  // LBU
                        default:                            res = 32'b0;
                    endcase
                end
            end
        endcase
    end

    assign i_error = unknown_instr;

endmodule

Operations

booth

booth.sv

booth module

Imports

Imports 1 element

  • core_config_pkg XLEN

Ports

Ports list 9 elements

Name

Direction

Description

Type

Size

Kind

Polarity

Fanout

clk

Input

logic

-

Combinatorial

-

Fanout : 0

rst_n

Input

logic

-

Combinatorial

Negative

Fanout : 0

start

Input

verilator lint_off UNUSEDSIGNAL

logic

-

Combinatorial

-

Fanout : 0

X_signed

Input

verilator lint_on UNUSEDSIGNAL

logic

-

Combinatorial

-

Fanout : 0

Y_signed

Input

logic

-

Combinatorial

-

Fanout : 0

X

Input

logic

[(core_config_pkg::XLEN - 1) : 0]

Combinatorial

-

Fanout : 0

Y

Input

logic

[(core_config_pkg::XLEN - 1) : 0]

Combinatorial

-

Fanout : 0

valid

Output

logic

-

Register clk rst_n

-

Fanout : 0

Z

Output

logic

[((2 * core_config_pkg::XLEN) - 1) : 0]

Combinatorial

-

Fanout : 0

Processes

Process list 2 elements

• Process 1 Clocked Clocked by clk Reset by rst_n

-

• Process 2 Combinatorial

-

Signals

Signals list 13 elements

Name

Description

Type

Size

Kind

Fanout

pres_state

state_t

-

Register clk rst_n

Fanout : 0

temp

logic

[1 : 0]

Register clk rst_n

Fanout : 0

count

logic

[($clog2(core_config_pkg::XLEN)) : 0]

Register clk rst_n

Fanout : 0

next 6 elements

Name

Description

Type

Size

Kind

Fanout

next_Z

Storage registers

logic signed

[((2 * core_config_pkg::XLEN) + 1) : 0]

Register clk rst_n

Fanout : 0

next_Y_ext

logic signed

[(core_config_pkg::XLEN) : 0]

Register clk rst_n

Fanout : 0

next_state

state_t

-

Register clk rst_n

Fanout : 0

next_temp

logic

[1 : 0]

Register clk rst_n

Fanout : 0

next_count

logic

[($clog2(core_config_pkg::XLEN)) : 0]

Register clk rst_n

Fanout : 0

next_valid

logic

-

Register clk rst_n

Fanout : 0

Z 2 elements

Name

Description

Type

Size

Kind

Fanout

Z_temp

logic signed

[((2 * core_config_pkg::XLEN) + 1) : 0]

Combinatorial

Fanout : 0

Z_reg

logic signed

[((2 * core_config_pkg::XLEN) + 1) : 0]

Register clk rst_n

Fanout : 0

Y 2 elements

Name

Description

Type

Size

Kind

Fanout

Y_ext

logic signed

[(core_config_pkg::XLEN) : 0]

Register clk rst_n

Fanout : 0

Y_op

logic signed

[(core_config_pkg::XLEN) : 0]

Combinatorial

Fanout : 0

Instantiation templates

Instantiation templates 2 elements

booth <name> (
    .clk      (i_clk),
    .rst_n    (i_rst_n),
    .start    (i_start),
    .X_signed (i_X_signed),
    .Y_signed (i_Y_signed),
    .X        (i_X),
    .Y        (i_Y),
    .valid    (i_valid),
    .Z        (i_Z)
);
<name>: entity work.booth
   port map (
        clk       => i_clk,
        rst_n     => i_rst_n,
        start     => i_start,
        X_signed  => i_X_signed,
        Y_signed  => i_Y_signed,
        X         => i_X,
        Y         => i_Y,
        valid     => i_valid,
        Z         => i_Z
    );

Source

Raw source Full file

/*
 *  File :      rtl/core/alu/operations/booth.sv
 *
 *  Author :    l.heywang <leonard.heywang@proton.me>
 *  Date :      25/10.2025
 *  
 *  Brief :     This file implement a Booth (Radix 2) multiplier
 *              for the ALU2 implementation.
 *              It support both signed and unsigned numbers for
 *              each operands, and thus, four operational modes.        
 */

/*
 *  This section of code is mostly based on the algorithm here :
 *  https://electrobinary.blogspot.com/2020/08/booth-multiplier-verilog-code.html
 *  
 *  With modifications : 
 *  - Support of 32 bits operands
 *  - Integration of some Verilog -> SystemVerilog improvements.
 *  - Support of both signed and unsigned operands.
 */

`timescale 1ns / 1ps
import core_config_pkg::XLEN;

module booth (

    input  logic                                         clk,
    input  logic                                         rst_n,
    input  logic                                         start,
    /* verilator lint_off UNUSEDSIGNAL */
    input  logic                                         X_signed,
    /* verilator lint_on UNUSEDSIGNAL */
    input  logic                                         Y_signed,
    input  logic [      (core_config_pkg::XLEN - 1) : 0] X,
    input  logic [      (core_config_pkg::XLEN - 1) : 0] Y,
    output logic                                         valid,
    output logic [((2 * core_config_pkg::XLEN) - 1) : 0] Z

);

    /*
     *  Custom types
     */
    typedef enum logic [0 : 0] {
        IDLE  = 1'b0,
        START = 1'b1
    } state_t;

    /* 
     *  Storage registers
     */
    logic signed [((2 * core_config_pkg::XLEN) + 1) : 0] next_Z;
    logic signed [((2 * core_config_pkg::XLEN) + 1) : 0] Z_temp;
    logic signed [((2 * core_config_pkg::XLEN) + 1) : 0] Z_reg;
    logic signed [          (core_config_pkg::XLEN) : 0] Y_ext;
    logic signed [          (core_config_pkg::XLEN) : 0] Y_op;
    logic signed [          (core_config_pkg::XLEN) : 0] next_Y_ext;
    state_t                                              next_state;
    state_t                                              pres_state;
    logic        [                                  1:0] temp;
    logic        [                                  1:0] next_temp;
    logic        [  ($clog2(core_config_pkg::XLEN)) : 0] count;
    logic        [  ($clog2(core_config_pkg::XLEN)) : 0] next_count;
    logic                                                next_valid;

    always_ff @(posedge clk or negedge rst_n) begin

        if (!rst_n) begin

            Z_reg      <= 66'b0;
            valid      <= 1'b0;
            pres_state <= IDLE;
            temp       <= 2'b0;
            count      <= '0;
            Y_ext      <= '0;

        end else begin

            Z_reg      <= next_Z;
            valid      <= next_valid;
            pres_state <= next_state;
            temp       <= next_temp;
            count      <= next_count;

            if (pres_state == IDLE) begin

                Y_ext <= next_Y_ext;

            end

        end
    end

    always_comb begin

        case (pres_state)

            IDLE: begin

                next_count = 0;
                next_valid = 0;
                Z_temp     = '0;
                Y_op       = '0;

                if (start) begin

                    next_state = START;

                    next_temp = {X[0], 1'b0};

                    /*
                     *  Perform sign handling conditions : 
                     *  - If the operand is unsigned, we always padd '0' as the MSB
                     *  - If the operand is   signed, we always sign extend to the MSB
                     */
                    next_Y_ext = (Y_signed) ? $signed({Y[31], Y}) : {1'd0, Y};

                    // Z is always maintained as this form, regardless of it's sign.
                    next_Z = {34'b0, X};

                end else begin

                    next_state = pres_state;
                    next_temp  = 0;
                    next_Z     = 0;
                    next_Y_ext = 0;

                end
            end

            START: begin

                Y_op = Y_signed ? $signed(Y_ext) : $unsigned(Y_ext);

                /*
                *   Not using default will indcate to quartus to infer muxes rather than equal + selectors.
                *   This lead to a gain of frequency of about 35 MHz.
                *   
                *   Note 2 : By preserving the MSB, we can reduce a bit the ripple carry chain, and thus,
                *   pass from 199.08 MHz to 200.72 MHz.
                */
                case (temp)
                    2'b00: Z_temp = Z_reg;  // + 0
                    2'b10: Z_temp = {Z_reg[65], Z_reg[64:32] - Y_op, Z_reg[31:0]};  // - Y
                    2'b01: Z_temp = {Z_reg[65], Z_reg[64:32] + Y_op, Z_reg[31:0]};  // + Y
                    2'b11: Z_temp = Z_reg;  // + 0
                endcase

                next_temp  = Z_reg[1:0];
                next_count = count + 1'b1;
                next_Z     = Z_temp >>> 1;

                next_valid = (count == 31) ? 1'b1 : 1'b0;
                next_state = (count == 31) ? IDLE : pres_state;

                next_Y_ext = 0;

            end
        endcase
    end

    assign Z = Z_reg[((2*core_config_pkg::XLEN)-1) : 0];

endmodule

shift

shift.sv

shift module

Imports

Imports 2 elements

  • core_config_pkg XLEN MAX_SHIFT_PER_CYCLE

Parameters

Parameters 1 element

• SHIFT_SIZE Type : logic Default : ($clog2(core_config_pkg::XLEN) - 1)

Parameters

Ports

Ports list 9 elements

Name

Direction

Description

Type

Size

Kind

Polarity

Fanout

clk

Input

logic

-

Combinatorial

-

Fanout : 0

rst_n

Input

logic

-

Combinatorial

Negative

Fanout : 0

start

Input

logic

-

Combinatorial

-

Fanout : 0

arithmetic

Input

logic

-

Combinatorial

-

Fanout : 0

done

Output

logic

-

Register clk rst_n

-

Fanout : 0

data 2 elements

Name

Direction

Description

Type

Size

Kind

Polarity

Fanout

data_in

Input

logic

[(core_config_pkg::XLEN - 1) : 0]

Combinatorial

-

Fanout : 0

data_out

Output

logic

[(core_config_pkg::XLEN - 1) : 0]

Register clk rst_n

-

Fanout : 0

shift 2 elements

Name

Direction

Description

Type

Size

Kind

Polarity

Fanout

shift_amount

Input

logic

[($clog2(core_config_pkg::XLEN)-1) : 0]

Combinatorial

-

Fanout : 0

shift_left

Input

logic

-

Combinatorial

-

Fanout : 0

Processes

Process list 2 elements

• Process 1 Clocked Clocked by clk Reset by rst_n

Synchronous logic

• Process 2 Combinatorial

Combinational logic

Signals

Signals list 13 elements

Name

Description

Type

Size

Kind

Fanout

pres_state

state_t

-

Register clk rst_n

Fanout : 0

step

logic

[SHIFT_SIZE : 0]

Combinatorial

Fanout : 0

next 7 elements

Name

Description

Type

Size

Kind

Fanout

next_state

state_t

-

Register clk rst_n

Fanout : 0

next_shifted

Temp storage

logic

[(core_config_pkg::XLEN - 1) : 0]

Register clk rst_n

Fanout : 0

next_remaining

logic

[SHIFT_SIZE : 0]

Register clk rst_n

Fanout : 0

next_shift_left

logic

-

Register clk rst_n

Fanout : 0

next_arithmetic

logic

-

Register clk rst_n

Fanout : 0

next_done

logic

-

Register clk rst_n

Fanout : 0

next_out

logic

[(core_config_pkg::XLEN - 1) : 0]

Register clk rst_n

Fanout : 0

r 4 elements

Name

Description

Type

Size

Kind

Fanout

r_shifted

Registers

logic

[(core_config_pkg::XLEN - 1) : 0]

Register clk rst_n

Fanout : 0

r_remaining

logic

[SHIFT_SIZE : 0]

Register clk rst_n

Fanout : 0

r_shift_left

logic

-

Register clk rst_n

Fanout : 0

r_arithmetic

logic

-

Register clk rst_n

Fanout : 0

Instantiation templates

Instantiation templates 2 elements

shift <name> #(
    .SHIFT_SIZE (($clog2(core_config_pkg::XLEN) - 1))

)(
    .clk          (i_clk),
    .rst_n        (i_rst_n),
    .start        (i_start),
    .data_in      (i_data_in),
    .shift_amount (i_shift_amount),
    .shift_left   (i_shift_left),
    .arithmetic   (i_arithmetic),
    .data_out     (i_data_out),
    .done         (i_done)
);
<name>: entity work.shift
    generic map (
    .SHIFT_SIZE /*($clog2(core_config_pkg::XLEN) - 1)*/
    )
   port map (
        clk           => i_clk,
        rst_n         => i_rst_n,
        start         => i_start,
        data_in       => i_data_in,
        shift_amount  => i_shift_amount,
        shift_left    => i_shift_left,
        arithmetic    => i_arithmetic,
        data_out      => i_data_out,
        done          => i_done
    );

Source

Raw source Full file

/*
 *  File :      rtl/core/alu/operations/shift.sv
 *
 *  Author :    l.heywang <leonard.heywang@proton.me>
 *  Date :      25/10.2025
 *  
 *  Brief :     This file implement a shifter to perform
 *              SLL / SRA / SRL instructions.
 *              It can shift up to MAX_SHIFT_PER_CYCLE bits,
 *              so for large shift, they may need multipe clock cycles.
 *              
 */

`timescale 1ns / 1ps

import core_config_pkg::XLEN;
import core_config_pkg::MAX_SHIFT_PER_CYCLE;

module shift (
    input  logic                                         clk,
    input  logic                                         rst_n,
    input  logic                                         start,
    input  logic [      (core_config_pkg::XLEN - 1) : 0] data_in,
    input  logic [($clog2(core_config_pkg::XLEN)-1) : 0] shift_amount,
    input  logic                                         shift_left,
    input  logic                                         arithmetic,
    output logic [      (core_config_pkg::XLEN - 1) : 0] data_out,
    output logic                                         done
);
    /*
     *  Parameters
     */
    localparam SHIFT_SIZE = ($clog2(core_config_pkg::XLEN) - 1);

    /*
     *  Enums
     */
    typedef enum logic [0:0] {
        IDLE  = 1'b0,
        SHIFT = 1'b1
    } state_t;

    state_t                                   pres_state;
    state_t                                   next_state;

    /*
     *  Registers
     */
    logic   [(core_config_pkg::XLEN - 1) : 0] r_shifted;
    logic   [                 SHIFT_SIZE : 0] r_remaining;
    logic                                     r_shift_left;
    logic                                     r_arithmetic;

    /* 
     *  Temp storage
     */
    logic   [(core_config_pkg::XLEN - 1) : 0] next_shifted;
    logic   [                 SHIFT_SIZE : 0] next_remaining;
    logic                                     next_shift_left;
    logic                                     next_arithmetic;
    logic                                     next_done;
    logic   [(core_config_pkg::XLEN - 1) : 0] next_out;

    /*
     *  Synchronous logic
     */
    always_ff @(posedge clk or negedge rst_n) begin

        if (!rst_n) begin

            r_shifted    <= 'b0;
            r_remaining  <= 'b0;

            r_shift_left <= 1'b0;
            r_arithmetic <= 1'b0;

            pres_state   <= IDLE;

            data_out     <= '0;
            done         <= 1'b0;

        end else begin

            r_remaining  <= next_remaining;
            r_shifted    <= next_shifted;

            r_shift_left <= next_shift_left;
            r_arithmetic <= next_arithmetic;

            pres_state   <= next_state;

            data_out     <= next_out;
            done         <= next_done;

        end
    end

    /*
     *  Combinational logic
     */
    always_comb begin

        unique case (pres_state)

            IDLE: begin

                if (start) begin

                    next_shifted    = data_in;
                    next_remaining  = shift_amount;

                    next_shift_left = shift_left;
                    next_arithmetic = arithmetic;

                    next_state      = SHIFT;

                    next_done       = 1'b0;
                    next_out        = 'b0;

                end else begin

                    next_shifted    = 'b0;
                    next_remaining  = 'b0;

                    next_shift_left = 1'b0;
                    next_arithmetic = 1'b0;

                    next_state      = IDLE;

                    next_done       = done;
                    next_out        = data_out;

                end
            end

            SHIFT: begin

                /*
                 *  Improvements ideas : 
                 *
                 *  Actually, the limiting factor of the ALU2 is right here, the shifts logics.
                 *  To ensure a maximal operating frequency, the shift is limited to 3 bits (
                 *  see core_config_pkg.sv).
                 *  
                 *  That's fine in most case, but for some, it may be cool to fasten up the 
                 *  process. Thus, a bypass shift with a fixed 8 bits could be imagined, and
                 *  called when r_remaining is greater than this value.
                 *
                 *  Will be *perhaps* be done in a future revision, when the testing profiled
                 *  the right values.
                 */

                logic [SHIFT_SIZE : 0] step;

                next_shift_left = r_shift_left;
                next_arithmetic = r_arithmetic;

                step = (r_remaining > MAX_SHIFT_PER_CYCLE[SHIFT_SIZE : 0]) ? 
                        MAX_SHIFT_PER_CYCLE [SHIFT_SIZE : 0] : 
                        r_remaining;

                next_remaining  = (r_remaining > MAX_SHIFT_PER_CYCLE[SHIFT_SIZE : 0]) ? 
                        (r_remaining - MAX_SHIFT_PER_CYCLE[SHIFT_SIZE : 0]) :
                        (0);

                unique case ({
                    r_shift_left, r_arithmetic
                })

                    2'b00 : next_shifted    = r_shifted >> step;
                    2'b01 : next_shifted    = $signed(r_shifted) >>> step;
                    2'b10,
                    2'b11 : next_shifted    = r_shifted << step;

                endcase

                if (r_remaining == 0) begin

                    next_state = IDLE;
                    next_done  = 1'b1;
                    next_out   = r_shifted;

                end else begin

                    next_state = SHIFT;
                    next_done  = 1'b0;
                    next_out   = 'b0;

                end
            end
        endcase
    end

endmodule

srt

srt.sv

srt module

Imports

Imports 1 element

  • core_config_pkg XLEN

Ports

Ports list 11 elements

Name

Direction

Description

Type

Size

Kind

Polarity

Fanout

clk

Input

logic

-

Combinatorial

-

Fanout : 0

rst_n

Input

logic

-

Combinatorial

Negative

Fanout : 0

start

Input

logic

-

Combinatorial

-

Fanout : 0

dividend_signed

Input

logic

-

Combinatorial

-

Fanout : 0

divisor_signed

Input

logic

-

Combinatorial

-

Fanout : 0

dividend

Input

logic

[(core_config_pkg::XLEN - 1) : 0]

Combinatorial

-

Fanout : 0

divisor

Input

logic

[(core_config_pkg::XLEN - 1) : 0]

Combinatorial

-

Fanout : 0

valid

Output

logic

-

Register clk rst_n

-

Fanout : 0

quotient

Output

logic

[(core_config_pkg::XLEN - 1) : 0]

Register clk rst_n

-

Fanout : 0

remainder

Output

logic

[(core_config_pkg::XLEN - 1) : 0]

Register clk rst_n

-

Fanout : 0

div_by_zero

Output

logic

-

Register clk rst_n

-

Fanout : 0

Processes

Process list 2 elements

• Process 1 Clocked Clocked by clk Reset by rst_n

Sequential logic

• Process 2 Combinatorial

Combinational logic

Signals

Signals list 41 elements

Name

Description

Type

Size

Kind

Fanout

pres_state

state_t

-

Register clk rst_n

Fanout : 0

count

logic

[5 : 0]

Register clk rst_n

Fanout : 0

dividend_neg_reg

Sign tracking

logic

-

Register clk rst_n

Fanout : 0

quotient_null

logic

-

Register clk rst_n

Fanout : 0

remainder_null

logic

-

Register clk rst_n

Fanout : 0

shifted_AQ

verilator lint_off UNUSEDSIGNAL

logic

[64 : 0]

Register clk rst_n

Fanout : 0

temp_sub

logic

[32 : 0]

Register clk rst_n

Fanout : 0

A_part

logic

[32 : 0]

Combinatorial

Fanout : 0

next 19 elements

Name

Description

Type

Size

Kind

Fanout

next_state

state_t

-

Register clk rst_n

Fanout : 0

next_divisor

logic

[31 : 0]

Register clk rst_n

Fanout : 0

next_count

logic

[5 : 0]

Register clk rst_n

Fanout : 0

next_valid

logic

-

Register clk rst_n

Fanout : 0

next_div_by_zero

logic

-

Register clk rst_n

Fanout : 0

next_dividend_neg

logic

-

Register clk rst_n

Fanout : 0

next_divisor_neg

logic

-

Register clk rst_n

Fanout : 0

next_quotient_null

logic

-

Register clk rst_n

Fanout : 0

next_remainder_null

logic

-

Register clk rst_n

Fanout : 0

next_shifted_AQ

verilator lint_on UNUSEDSIGNAL

logic

[64 : 0]

Register clk rst_n

Fanout : 0

next_temp_sub

logic

[32 : 0]

Register clk rst_n

Fanout : 0

next_AQ

logic

[64 : 0]

Register clk rst_n

Fanout : 0

sign 5 elements

Name

Description

Type

Size

Kind

Fanout

next_sign_error

logic

-

Register clk rst_n

Fanout : 0

mask 4 elements

Name

Description

Type

Size

Kind

Fanout

next_sign_mask_q

logic

[31 : 0]

Register clk rst_n

Fanout : 0

next_sign_mask_r

logic

[31 : 0]

Register clk rst_n

Fanout : 0

next_sign_mask_dividend

logic

[31 : 0]

Register clk rst_n

Fanout : 0

next_sign_mask_divisor

logic

[31 : 0]

Register clk rst_n

Fanout : 0

abs 2 elements

Name

Description

Type

Size

Kind

Fanout

next_abs_dividend

logic

[31 : 0]

Register clk rst_n

Fanout : 0

next_abs_divisor

logic

[31 : 0]

Register clk rst_n

Fanout : 0

divisor 2 elements

Name

Description

Type

Size

Kind

Fanout

divisor_reg

logic

[31 : 0]

Register clk rst_n

Fanout : 0

divisor_neg_reg

logic

-

Register clk rst_n

Fanout : 0

sign 5 elements

Name

Description

Type

Size

Kind

Fanout

sign_error

logic

-

Register clk rst_n

Fanout : 0

mask 4 elements

Name

Description

Type

Size

Kind

Fanout

sign_mask_q

logic

[31 : 0]

Register clk rst_n

Fanout : 0

sign_mask_r

logic

[31 : 0]

Register clk rst_n

Fanout : 0

sign_mask_dividend

logic

[31 : 0]

Register clk rst_n

Fanout : 0

sign_mask_divisor

logic

[31 : 0]

Register clk rst_n

Fanout : 0

final 2 elements

Name

Description

Type

Size

Kind

Fanout

final_quotient

logic

[31 : 0]

Register clk rst_n

Fanout : 0

final_remainder

logic

[31 : 0]

Register clk rst_n

Fanout : 0

abs 2 elements

Name

Description

Type

Size

Kind

Fanout

abs_dividend

logic

[31 : 0]

Register clk rst_n

Fanout : 0

abs_divisor

logic

[31 : 0]

Register clk rst_n

Fanout : 0

AQ 3 elements

Name

Description

Type

Size

Kind

Fanout

AQ_reg

verilator lint_off UNUSEDSIGNAL

logic

[64 : 0]

Register clk rst_n

Fanout : 0

AQ_reg2

logic

[64 : 0]

Register clk rst_n

Fanout : 0

AQ_reg3

verilator lint_on UNUSEDSIGNAL

logic

[64 : 0]

Register clk rst_n

Fanout : 0

Instantiation templates

Instantiation templates 2 elements

srt <name> (
    .clk             (i_clk),
    .rst_n           (i_rst_n),
    .start           (i_start),
    .dividend_signed (i_dividend_signed),
    .divisor_signed  (i_divisor_signed),
    .dividend        (i_dividend),
    .divisor         (i_divisor),
    .valid           (i_valid),
    .quotient        (i_quotient),
    .remainder       (i_remainder),
    .div_by_zero     (i_div_by_zero)
);
<name>: entity work.srt
   port map (
        clk              => i_clk,
        rst_n            => i_rst_n,
        start            => i_start,
        dividend_signed  => i_dividend_signed,
        divisor_signed   => i_divisor_signed,
        dividend         => i_dividend,
        divisor          => i_divisor,
        valid            => i_valid,
        quotient         => i_quotient,
        remainder        => i_remainder,
        div_by_zero      => i_div_by_zero
    );

Source

Raw source Full file

/*
 *  File :      rtl/core/alu/operations/srt.sv
 *
 *  Author :    l.heywang <leonard.heywang@proton.me>
 *  Date :      25/10.2025
 *  
 *  Brief :     This file implement an SRT (Radix 2) divider.
 *              Used to perform divisions on both signed and unsigned
 *              numbers (Cross cases aren't supported).
 *              Due to heavy pipelining, it's result isn't available
 *              until 71 clock cycles (~36 cpu cycles), unless an error
 *              occured (2 cycles then).                  
 */

`timescale 1ns / 1ps

import core_config_pkg::XLEN;

module srt (
    input  logic                                   clk,
    input  logic                                   rst_n,
    input  logic                                   start,
    input  logic                                   dividend_signed,
    input  logic                                   divisor_signed,
    input  logic [(core_config_pkg::XLEN - 1) : 0] dividend,
    input  logic [(core_config_pkg::XLEN - 1) : 0] divisor,
    output logic                                   valid,
    output logic [(core_config_pkg::XLEN - 1) : 0] quotient,
    output logic [(core_config_pkg::XLEN - 1) : 0] remainder,
    output logic                                   div_by_zero
);

    /*
     * State machine (similar to Booth multiplier)
     */
    typedef enum logic [2:0] {
        IDLE,
        IDLE2,
        IDLE3,
        DIVIDE,
        DIVIDE2,
        SIGN_COMP,
        SIGN_FIX1,
        SIGN_FIX2
    } state_t;

    state_t pres_state, next_state;

    /*
     * Registers
     * Structure: [Remainder (33 bits) | Quotient (32 bits)]
     * Similar to Booth's [AC | QR] structure
     */
    logic [31:0] divisor_reg;
    logic [31:0] next_divisor;
    logic [ 5:0] count;
    logic [ 5:0] next_count;
    logic        next_valid;
    logic        next_div_by_zero;
    logic        sign_error;
    logic        next_sign_error;

    // Sign tracking
    logic        dividend_neg_reg;
    logic        divisor_neg_reg;
    logic        next_dividend_neg;
    logic        next_divisor_neg;

    logic        next_quotient_null;
    logic        next_remainder_null;
    logic        quotient_null;
    logic        remainder_null;
    logic [31:0] final_quotient;
    logic [31:0] final_remainder;

    logic [31:0] abs_dividend;
    logic [31:0] abs_divisor;
    logic [31:0] next_abs_dividend;
    logic [31:0] next_abs_divisor;

    /* verilator lint_off UNUSEDSIGNAL */
    logic [64:0] shifted_AQ;
    logic [64:0] next_shifted_AQ;
    /* verilator lint_on UNUSEDSIGNAL */
    logic [32:0] temp_sub;
    logic [32:0] A_part;
    logic [32:0] next_temp_sub;
    logic [31:0] sign_mask_q;
    logic [31:0] sign_mask_r;
    logic [31:0] next_sign_mask_q;
    logic [31:0] next_sign_mask_r;
    logic [31:0] sign_mask_dividend;
    logic [31:0] sign_mask_divisor;
    logic [31:0] next_sign_mask_dividend;
    logic [31:0] next_sign_mask_divisor;

    /*
      * Note : Theses registers, AQ_regx are high fanout ones, so we use multiple
      * registers rather than one. This gave us a frequency increase which was not
      * negligeable.
      */
    logic [64:0] next_AQ;
    logic [64:0] AQ_reg;
    /* verilator lint_off UNUSEDSIGNAL */
    logic [64:0] AQ_reg2;
    logic [64:0] AQ_reg3;
    /* verilator lint_on UNUSEDSIGNAL */


    /*
     * Sequential logic
     */
    always_ff @(posedge clk or negedge rst_n) begin
        if (!rst_n) begin

            pres_state         <= IDLE;
            AQ_reg             <= '0;
            AQ_reg2            <= '0;
            AQ_reg3            <= '0;
            divisor_reg        <= '0;
            count              <= '0;
            valid              <= 1'b0;
            div_by_zero        <= 1'b0;
            sign_error         <= 1'b0;
            dividend_neg_reg   <= 1'b0;
            divisor_neg_reg    <= 1'b0;
            quotient_null      <= 1'b0;
            remainder_null     <= 1'b0;
            quotient           <= 32'b0;
            remainder          <= 32'b0;
            abs_dividend       <= 32'b0;
            abs_divisor        <= 32'b0;
            shifted_AQ         <= 65'b0;
            temp_sub           <= 33'b0;
            sign_mask_q        <= 32'b0;
            sign_mask_r        <= 32'b0;
            sign_mask_dividend <= 32'b0;
            sign_mask_divisor  <= 32'b0;

        end else begin

            pres_state         <= next_state;
            AQ_reg             <= next_AQ;
            AQ_reg2            <= next_AQ;
            AQ_reg3            <= next_AQ;
            divisor_reg        <= next_divisor;
            count              <= next_count;
            valid              <= next_valid;
            div_by_zero        <= next_div_by_zero;
            sign_error         <= next_sign_error;
            dividend_neg_reg   <= next_dividend_neg;
            divisor_neg_reg    <= next_divisor_neg;
            quotient_null      <= next_quotient_null;
            remainder_null     <= next_remainder_null;
            quotient           <= final_quotient;
            remainder          <= final_remainder;
            abs_dividend       <= next_abs_dividend;
            abs_divisor        <= next_abs_divisor;
            shifted_AQ         <= next_shifted_AQ;
            temp_sub           <= next_temp_sub;
            sign_mask_q        <= next_sign_mask_q;
            sign_mask_r        <= next_sign_mask_r;
            sign_mask_dividend <= next_sign_mask_dividend;
            sign_mask_divisor  <= next_sign_mask_divisor;

        end
    end

    /*
     * Combinational logic
     */
    always_comb begin
        // Defaults
        next_state              = IDLE;
        next_AQ                 = AQ_reg;
        next_divisor            = divisor_reg;
        next_count              = count;
        next_valid              = 1'b0;
        next_div_by_zero        = div_by_zero;
        next_sign_error         = sign_error;
        next_dividend_neg       = dividend_neg_reg;
        next_divisor_neg        = divisor_neg_reg;
        next_quotient_null      = 1'b0;
        next_remainder_null     = 1'b0;
        final_quotient          = quotient;
        final_remainder         = remainder;
        next_abs_dividend       = 32'b0;
        next_abs_divisor        = 32'b0;
        next_shifted_AQ         = AQ_reg;
        next_temp_sub           = 33'b0;
        next_sign_mask_q        = 32'b0;
        next_sign_mask_r        = 32'b0;
        next_sign_mask_dividend = 32'b0;
        next_sign_mask_divisor  = 32'b0;
        A_part                  = 33'b0;

        case (pres_state)

            /*
             *  Initialization cases
             */

            IDLE: begin

                if (start) begin

                    if (divisor == 32'd0) begin

                        next_div_by_zero = 1'b1;
                        next_sign_error = 1'b0;
                        next_state = SIGN_FIX1;
                        next_count = 6'b0;

                    end else if (dividend_signed ^ divisor_signed) begin

                        /* 
                         *  Note : Using two signess for the division lead to incorrect 
                         *  and unpredictable results.
                         *
                         *  XORing the signs ensure us to get an error in theses cases, or the
                         *  right result at the end !
                         */

                        next_div_by_zero = 1'b0;
                        next_sign_error = 1'b1;
                        next_state = SIGN_FIX1;
                        next_count = 6'b0;

                    end else begin

                        next_sign_mask_dividend = {32{dividend_signed && dividend[31]}};
                        next_sign_mask_divisor = {32{divisor_signed && divisor[31]}};

                        next_div_by_zero = 1'b0;
                        next_sign_error = 1'b0;
                        next_state = IDLE2;
                        next_count = 6'b0;

                    end
                end
            end

            IDLE2: begin

                next_div_by_zero = 1'b0;

                /*
                 *  Note : This combinational logic enable to negate a number without needing
                 *  carry chains. The logic is A XOR MASK + 1, where XOR is used as a wanted inverter.
                 */
                next_abs_dividend = (dividend ^ sign_mask_dividend) + {31'b0, sign_mask_dividend[0]};
                next_abs_divisor = (divisor ^ sign_mask_divisor) + {31'b0, sign_mask_divisor[0]};

                next_state = IDLE3;

            end

            IDLE3: begin

                next_dividend_neg = dividend_signed && dividend[31];
                next_divisor_neg  = divisor_signed && divisor[31];

                next_AQ           = {33'd0, abs_dividend};
                next_divisor      = abs_divisor;

                next_state        = DIVIDE;

            end

            /*
             *  Computing cases
             */
            DIVIDE: begin

                next_shifted_AQ = AQ_reg << 1;
                A_part          = next_shifted_AQ[64:32];

                next_temp_sub   = A_part - {1'b0, divisor_reg};
                next_state      = DIVIDE2;

            end

            DIVIDE2: begin

                next_AQ = (temp_sub[32]) ? 
                            {shifted_AQ[64:1], 1'b0} : 
                            {temp_sub, shifted_AQ[31:1], 1'b1};

                next_count = count + 6'd1;
                next_state = (count == 6'd31) ? SIGN_COMP : DIVIDE;

            end

            /*
             *  Output cases
             */
            SIGN_COMP: begin

                next_quotient_null = ~(|AQ_reg2[31:0]);
                next_remainder_null = ~(|AQ_reg2[63:32]);
                next_state = SIGN_FIX1;

            end

            SIGN_FIX1: begin

                next_sign_mask_q = {32{(dividend_neg_reg ^ divisor_neg_reg) && !quotient_null}};
                next_sign_mask_r = {32{(dividend_neg_reg && !remainder_null)}};

                next_state = SIGN_FIX2;

            end

            SIGN_FIX2: begin

                /*
                 *  Handling the RISC-V spec by forcing some specific values on the output buses, if needed.
                 */
                if (sign_error || div_by_zero) begin
                    final_quotient  = 32'hFFFFFFFF;
                    final_remainder = 32'h00000000;
                end else begin
                    /*
                     *  Note : This combinational logic enable to negate a number without needing
                     *  carry chains. The logic is A XOR MASK + 1, where XOR is used as a wanted inverter.
                     */
                    final_quotient  = (AQ_reg3[31:0] ^ sign_mask_q) + {31'b0, sign_mask_q[0]};
                    final_remainder = (AQ_reg3[63:32] ^ sign_mask_r) + {31'b0, sign_mask_r[0]};
                end

                next_valid = 1'b1;
                next_state = IDLE;
            end

            /* 
             *  Default handler
             */
            default: next_state = IDLE;
        endcase
    end
endmodule