Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

1Learning Outcomes

2Building a R-Type + addi processor

Let’s extend our R-Type datapath to support I-Type arithmetic and logical instructions, starting with addi. To support addi:

The addi instruction updates the same two states as R-Type instructions. But we now need to build an immediate imm!

To do so, we reuse what already exists in our R-Type datapath, then consider what additional blocks we need to add. In Figure 1, we notice:

addi: Reuse PC = PC + 4 loop and ideally the “add” operation in the ALU.

Figure 1:addi: Reuse PC = PC + 4 loop and ideally the “add” operation in the ALU.

We therefore need additional logic that, for I-Type instructions, feeds in an immediate imm to ALU input B (instead of R[rs2], used for R-Type). Figure 2 introduces the two new blocks and wires them to our existing datapath:

  1. A new mux selects the ALU input B based on a new control signal BSel. Read about muxes/multiplexors in a previous section.

    • Set BSel to 1 to pass in the immediate imm.

    • Set Bsel to 0 to pass in the register value R[rs2].

  2. A new block, the immediate generator, generates a 32-bit value imm from the instruction bits inst.

addi: Add the BSel mux and the ImmGen block.

Figure 2:addi: Add the BSel mux and the ImmGen block.

3Tracing the addi Datapath

Let’s walk through the addi datapath with this new knowledge.

Figure 3:The addi datapath. Use the menu bar to trace through the animation or download a copy of the PDF/PPTX file.

  1. Instruction Fetch: Increment PC to next instruction (see R-Type datapath). Read the instruction inst from IMEM.

  2. Instruction Decode:

    • Read R[rs1] from RegFile (see R-Type).

    • Set up the destination register rd for writing.

    • Build the immediate imm. For I-Type instructions, wire the upper 12 bits of the instruction inst[31:20] to the input to the Immediate Generator block.

    • Configure control logic.

      • Configure ImmSel to I-type immediates (for now, we only have one type of immediate).

      • Set RegWEn to 1.

      • Set BSel to 1.

      • Set ALUSel to Add.

    After some delay, the immediate generator block updates its output signal imm to the appropriate sign-extended 32-bit immediate value, register value R[rs1] is read, and control signals are set.

  3. Execute: Because the control line BSel=1 selects the generated immediate imm for ALU input B, our ALU computes R[rs1] + imm.

  4. Memory: (We don’t access DMEM, so skip this.)

  5. Write Back: Write ALU output to the destination register by connecting alu to RegFile’s wdata input (see R-Type).

    Around the next rising clock edge, wdata, RegWEn, and rd should be held stable through setup and hold time of RegFile.

4Immediate Generator Block

We encourage revisiting this section after reading a few more example datapath traces.

Immediate Generator Block

Figure 4:Immediate Generator Block

Table 1:Signals for Immediate Generator. Course project signal names, if different, are in parentheses.

NameDirectionBit WidthDescription
Inputinst (Instruction)32The instruction being executed
InputImmSel3Value determining how to reconstruct the immediate
Outputimm (Immediate)32Value of the immediate in the instruction

Recall that the bits of the immediate are stored in different bits of the instruction, depending on the type of the instruction. The ImmSel signal, which is implemented in the control logic, will determine which type of immediate this subcircuit should generate.

The immediate storage formats are listed below in Table 2.

Table 2:Immediate Storage Formats from the RISC-V Green Card.

TypeImmSel (default)Bits 31-20Bits 19-12Bit 11Bits 10-5Bits 4-1Bit 0
I0b000inst[31:20]inst[30:20]
S0b001inst[31]inst[30:25]inst[11:7]
B0b010inst[31]inst[7]inst[30:25]inst[11:8]0
U0b011inst[31:12]0
J0b100inst[31]inst[19:12]inst[20]inst[30:21]0

Observations/reminders:

In the following subsections, we “iteratively” build the immediate generator to support I-Type, then S-Type, then B-Type. We leave the implementation of J-Type and U-Type immediates to you and the course project.

4.1I-Type

First, suppose our datapath only supported immediates from I-Type instructions. In this case, the immediate generator would perform two operations as shown in Figure 5.

Immediate Generator Block: I-Type

Figure 5:Immediate Generator Block: I-Type

4.2I-Type, S-Type

Next, suppose our datapath supported immediates from both I-Type and S-Type instructions. The immediate generator must set the lower bits imm[4:0] based on the immediate type. In Figure 6, we implement this selection with a MUX.

Immediate Generator Block: I-Type, S-Type

Figure 6:Immediate Generator Block: I-Type, S-Type

4.3I-Type, S-Type, B-Type

Finally, suppose our datapath supports I-Type, S-Type, and B-Type instructions. The immediate generator design is shown in Figure 7, now with even more MUXes.

Immediate Generator Block: I-Type, S-Type

Figure 7:Immediate Generator Block: I-Type, S-Type

4.4Course Project Details

Table 3:Default ImmSel encodings for the course project. You are welcome to pick your own.

ImmGen Value
(for Project)
Immediate Type
0b000I
0b001S
0b010B
0b011U
0b100J