Skip to content

Algebraic data types

Is ? good enough to bring algebraic data types semantic into the language with zero overhead?


Ideas

type i_instr_t

    rd     : u5
    funct3 : b3
    rs1    : u5
    imm    : u12

---

// Enumerated type with allowed casting (allowed casting must always be explicit from bN)
// mux must handle invalid encoding (optional if casting not allowed)
type instr_t : b32

    [2:0]
    | RVC0 = 2'b00
    | RVC1 = 2'b01
    | RVC2 = 2'b10
    | RVI  = 2'b11 // can we match both RVI and LOAD in the same mux statement? what is the behavior in this case?
        [6:2]
        // LOAD, LOAD_FP and OP_IMM all are I type
        // only one variable can be contained
        | LOAD    = 5'b00000
        | LOAD_FP = 5'b00001
        | OP_IMM  = 5'b00100 : i_instr_t // the variable is a structure with the following fields
            [11:7]  rd
            [14:12] funct3
            [19:15] rs1
            [31:20] imm
        | CUST_0 = 5'b00010 : b26 // the variable is a b26
            [31:20]
    ...

---

component decoder

    i_instr : b32

lsu.i_data = ?
lsu.i_addr = ?
idest = '0
fdest = '0

// use casting here, result can be invalid
mux i_instr as instr_t
    LOAD l -> // let's name the variable l, so l : i_instr_t
        lsu.i_action = LOAD
        lsu.i_width  = l.funct3
        lsu.i_addr   = reg_bank[l.rs1] + l.imm
        idest        = l.rd
    LOAD_FP l ->
        lsu.i_action = LOAD
        lsu.i_width  = l.funct3
        lsu.i_addr   = reg_bank[l.rs1] + l.imm
        // ^^^^^ avoid code repetition here?
        fdest        = l.rd
    ...

More decoding?

type i_instr_t

    rd     : u5
    funct3 : b3
    rs1    : u5
    imm    : u12

    // Be able to declare more decoding here?
    // But I loose difference from opcode (LOAD vs OP_IMM)
    .funct3
    | ADDI  = 3'b000
    | SLTI  = 3'b010
    | SLTUI = 3'b011
    | XORI  = 3'b100
    | ORI   = 3'b110
    | ANDI  = 3'b111
        [31:20] imm : b12
    | SLLI = 3'b001
    | _    = 3'b101
        [31:25]
            | SRLI = 7'b0000000
            | SRAI = 7'b0100000
        [24:20] shamt
Edited by come_744
To upload designs, you'll need to enable LFS and have an admin enable hashed storage. More information