The Single-Cycle Datapath

So far, a processor was a model that obeys the instruction set. This page opens the real one. It builds the wires that carry an add from the program counter to the register it writes.

You Will Be Able To

  • Name the two places where a single-cycle core holds state, and say when they change.
  • Follow an arithmetic instruction through fetch, register read, the ALU and the write back.
  • Read the real register file and the real ALU of ap_rv32.
Lesson
S2.4
Time
30 min
Runs on
Icarus service
Check
6 questions

An instruction word is 32 bits in a memory. A register is 32 flip-flops. Something must carry values from one to the other, and do the sum on the way. That something is a few wires, one adder and a clock, and this page reads all of it.

Before You Start

You need the fields of an instruction word from S2.2: rs1, rs2, rd and the immediate.

You need the instructions add, sub and addi from S2.1. You need to read a little SystemVerilog: a module, an assign and a clocked block.

Five questions this page answers
Question
1Which parts of a single-cycle core hold state?
2How does the register file know which registers to read?
3What are the two operands of the ALU?
4When does the result reach the register rd?
5What does the PC do while the ALU works?

The Idea: State, and Logic Between Two Clock Edges

A processor is two kinds of hardware. Some of it remembers: the PC and the registers. That is the state of the circuit, or its state for short. The rest only computes: it has outputs that follow its inputs, with no memory. That is combinational logic.

The datapath of a processor is the hardware that the data of an instruction travels through. This page calls it the datapath. It starts at the state, runs through the combinational logic, and ends at the state again.

In a single-cycle core, each instruction makes that whole trip in one clock cycle. A rising clock edge starts the instruction. The values travel through the logic. The next rising edge stores the results. Then the next instruction starts.

The core of this track is named ap_rv32. Its single-cycle build is a module named ap_rv32_sc. This page reads its datapath for an arithmetic instruction, such as add or addi. The next page adds memory, branches and jumps.

One part is missing on purpose. Something must tell the datapath what each instruction needs. Those wires are the control signals, and the decoder makes them. On this page they are given. S2.5 builds the decoder.

New Words

New words on this page
WordMeaning
State of a circuitWhat a circuit remembers from one clock cycle to the next. Here: the PC and the registers.
Combinational logicLogic with no memory. Its outputs follow its inputs, with no clock.
Datapath of a processorThe hardware that the data of an instruction travels through.
Single-cycleA processor that completes each instruction in one clock cycle.
ap_rv32_scThe single-cycle build of ap_rv32, the processor of this track.
Instruction memoryThe memory that holds the program. The PC is its address.
FetchThe step that reads the instruction word at the PC.
Write enableA wire that is 1 when a write must happen at the next clock edge.
Read portAn address input and a data output. The data follows the address at once.
Write portAn address, a data input and a write enable. The write happens at the clock edge.
Register fileThe block that holds the registers x1 to x31. It has two read ports and one write port.
ALUArithmetic and logic unit. The block that adds, subtracts, shifts, compares and combines bits.
OperandOne of the two values that the ALU works on.
Control signalA wire that tells part of the datapath what this instruction needs.
Write backThe step that puts the result into the register rd.

One Picture: From the PC to the Register

The Datapath of an Arithmetic InstructionStep through the picture. It follows one instruction, add x7, x5, x6, from the PC to the register it writes.
  • Processor
  • Memory
  • Control / FSM
The Datapath of an Arithmetic InstructionStep through the picture. It follows one instruction, add x7, x5, x6, from the PC to the register it writes. component PC pc_q: the address ofthis instruction component Instructionmemory returns the word atthe PC in the samecycle component Adder: + 4 pc_plus_4: theaddress of the nextword component Register file x1 to x31: two readports, one write port component Decoder andimmediate given on this page:the control signalsand imm component Operand select alu_operand_b:rs2_data or imm component ALU alu_result: one of tenoperations imem_addrinstr: rs1, rs2, rdinstrrs1_datars2_dataimm, alu_b_selectalu_operand_balu_operationresult, at the clockedgereg_writepc_qpc_next, at the clockedge
  1. component PCpc_q: the address of this instruction
    • imem_addr → Instruction memory
    • pc_q → Adder: + 4
  2. component Instruction memoryreturns the word at the PC in the same cycle
    • instr: rs1, rs2, rd → Register file
    • instr → Decoder and immediate
  3. component Register filex1 to x31: two read ports, one write port
    • rs1_data → ALU
    • rs2_data → Operand select
  4. component Decoder and immediategiven on this page: the control signals and imm
    • imm, alu_b_select → Operand select
    • alu_operation → ALU
    • reg_write → Register file
  5. component Operand selectalu_operand_b: rs2_data or imm
    • alu_operand_b → ALU
  6. component ALUalu_result: one of ten operations
    • result, at the clock edge → Register file
  7. component Adder: + 4pc_plus_4: the address of the next word
    • pc_next, at the clock edge → PC
  1. Fetch

    The PC holds 8. The instruction memory returns the word at address 8: 006283b3. No clock edge is needed for that.

  2. Read two registers

    Three fields of the word go straight to the register file. The fields rs1 and rs2 are 5 and 6. The file gives the values of x5 and x6: 7 and 5.

  3. Choose the second operand

    The decoder says alu_b_select is 0 for an add. The operand select passes rs2_data, the 5. For an addi it would pass the immediate.

  4. Execute

    The decoder says the operation is an add. The ALU adds 7 and 5. Its result is c in hex, which is 12.

  5. Write back

    The result waits at the write port. The field rd is 7 and reg_write is 1. At the clock edge, x7 takes the 12.

  6. The next PC

    All this time, an adder has added 4 to the PC. At the same clock edge, the PC takes c. The next instruction starts.

Read the diagram as text
  • PC: pc_q: the address of this instruction
  • Instruction memory: returns the word at the PC in the same cycle
  • Adder: + 4: pc_plus_4: the address of the next word
  • Register file: x1 to x31: two read ports, one write port
  • Decoder and immediate: given on this page: the control signals and imm
  • Operand select: alu_operand_b: rs2_data or imm
  • ALU: alu_result: one of ten operations
  • PC → Instruction memory: imem_addr
  • Instruction memory → Register file: instr: rs1, rs2, rd
  • Instruction memory → Decoder and immediate: instr
  • Register file → ALU: rs1_data
  • Register file → Operand select: rs2_data
  • Decoder and immediate → Operand select: imm, alu_b_select
  • Operand select → ALU: alu_operand_b
  • Decoder and immediate → ALU: alu_operation
  • ALU → Register file: result, at the clock edge
  • Decoder and immediate → Register file: reg_write
  • PC → Adder: + 4: pc_q
  • Adder: + 4 → PC: pc_next, at the clock edge

Only two boxes hold state: the PC and the register file. Everything between them is combinational logic.

The control signals of this page
SignalWidthWhat it decides
alu_b_select1 bit0: the second operand is rs2_data. 1: it is the immediate.
alu_operation4 bitsWhich of ten operations the ALU does.
reg_write1 bit1: the instruction writes the register rd.

The names are those of ap_rv32_sc. The immediate, imm, comes from the immediate generator of S2.2.

The Smallest Example, Worked by Hand

Take one instruction: add x7, x5, x6. It sits at address 8. Before it runs, x5 holds 7 and x6 holds 5.

The assembler turns it into the word 006283b3. In an R word, rs2 is in bits 24 to 20 and rs1 in bits 19 to 15. The field rd is in bits 11 to 7.

One add, wire by wire
StepWireValueWhy
Fetchimem_addr8the PC
Fetchinstr006283b3the word at address 8
Readrs1_data7the field rs1 is 5, and x5 holds 7
Readrs2_data5the field rs2 is 6, and x6 holds 5
Selectalu_operand_b5alu_b_select is 0: take rs2_data
Executealu_resultcthe operation is an add: 7 plus 5 is 12
Write backresultcreg_write is 1 and rd is 7: x7 takes 12 at the edge
Next PCpc_nextc8 plus 4. It is chance that both values are c.

Values are in hex. Until the clock edge, x7 still holds its old value.

Now change the instruction to addi x7, x5, 5. Only one row changes its reason. The decoder sets alu_b_select to 1, and the operand select passes the immediate, which is 5.

The Rules

  1. Rule 1: The PC is the address of the instruction memory.

    The word comes back in the same cycle. That is fetch.

  2. Rule 2: The fields rs1 and rs2 go straight to the two read ports.

    They are at fixed places in the word. So the read starts before anything knows what the instruction is.

  3. Rule 3: The first ALU operand is rs1_data. The second is rs2_data or the immediate.

    The control signal alu_b_select chooses.

  4. Rule 4: The result is written to rd only when reg_write is 1.

    The write happens at the rising clock edge that ends the cycle.

  5. Rule 5: At that same edge, the PC takes pc_next.

    On this page, pc_next is always the PC plus 4.

  6. Rule 6: A write to x0 is thrown away.

    x0 has no flip-flops. It always reads 0.

These rules are lines of the real core. Here is its register file, complete. Find rule 4 and rule 6 in the clocked block.

ap_rv32_regfile.sv, as built. The name REGISTER_COUNT is a constant of the package: 32.
module ap_rv32_regfile (
  input  logic        clk,          // clock
  input  logic        rst_n,        // reset, active low
  input  logic [4:0]  rs1_addr,     // first register to read
  output logic [31:0] rs1_data,     // its value
  input  logic [4:0]  rs2_addr,     // second register to read
  output logic [31:0] rs2_data,     // its value
  input  logic        write_enable, // write rd at this clock edge
  input  logic [4:0]  rd_addr,      // register to write; a write to x0 is discarded
  input  logic [31:0] rd_data       // value to write
);
  import ap_rv32_pkg::*;

  logic [31:0] registers_q [1:REGISTER_COUNT-1]; // x1 to x31; x0 has no storage

  always_ff @(posedge clk or negedge rst_n) begin
    if (!rst_n) begin
      for (int i = 1; i < REGISTER_COUNT; i++) registers_q[i] <= '0;
    end else if (write_enable && rd_addr != '0) begin
      registers_q[rd_addr] <= rd_data;
    end
  end

  assign rs1_data = (rs1_addr == '0) ? '0 : registers_q[rs1_addr];
  assign rs2_data = (rs2_addr == '0) ? '0 : registers_q[rs2_addr];

endmodule

The two assign lines at the bottom are the read ports. They have no clock: the data follows the address. The clocked block is the write port.

And here is the heart of the real ALU. One case statement chooses among ten operations.

From ap_rv32_alu.sv. A shift uses only the low five bits of the second operand.
assign shift_amount = operand_b[4:0];

always_comb begin
  result = '0;
  unique case (alu_operation)
    ALU_ADD:  result = operand_a + operand_b;
    ALU_SUB:  result = operand_a - operand_b;
    ALU_SLL:  result = operand_a << shift_amount;
    ALU_SLT:  result = {31'b0, $signed(operand_a) < $signed(operand_b)};
    ALU_SLTU: result = {31'b0, operand_a < operand_b};
    ALU_XOR:  result = operand_a ^ operand_b;
    ALU_SRL:  result = operand_a >> shift_amount;
    ALU_SRA:  result = $signed(operand_a) >>> shift_amount;
    ALU_OR:   result = operand_a | operand_b;
    ALU_AND:  result = operand_a & operand_b;
    default:  result = '0;
  endcase
Sources for These Rules

The parts of a single-cycle datapath are described here as textbooks commonly describe them. The plan of this track points to Patterson and Hennessy, sections 4.1 to 4.3, and to Harris and Harris, section 7.3. Those places are not checked. Every signal name, every line of code and every number on this page comes from the RTL of ap_rv32_sc in this repository.

Now Run It

The lab at the end of this page runs ten instructions. Its datapath has the wires of the picture, and its register file and ALU are the files above.

Each row shows one instruction. The third column shows three wires: a is rs1_data, b is alu_operand_b, and alu is alu_result. The last column shows the write back.

Printed by the lab on Icarus. The real core ap_rv32_sc and the instruction-level model of this track gave the same rows.
00000000 | addi x5, x0, 7 | a=0 b=7 alu=7 | x5 = 00000007
00000004 | addi x6, x0, 5 | a=0 b=5 alu=5 | x6 = 00000005
00000008 | add x7, x5, x6 | a=7 b=5 alu=c | x7 = 0000000c
0000000c | sub x8, x5, x6 | a=7 b=5 alu=2 | x8 = 00000002
00000010 | and x9, x5, x6 | a=7 b=5 alu=5 | x9 = 00000005
00000014 | or x10, x5, x6 | a=7 b=5 alu=7 | x10 = 00000007
00000018 | slli x11, x5, 4 | a=7 b=4 alu=70 | x11 = 00000070
0000001c | slt x12, x6, x5 | a=5 b=7 alu=1 | x12 = 00000001
00000020 | xori x13, x7, -1 | a=c b=ffffffff alu=fffffff3 | x13 = fffffff3
00000024 | add x0, x5, x6 | a=7 b=5 alu=c | x0 = 00000000
00000028 | ebreak | stop
x7=12 x8=2 x13=fffffff3 cycles=10

Rule 3 at work: compare rows 1 and 3. In row 1, an addi, b is the immediate 7. In row 3, an add, b is the value of x6.

Rule 2 at work: row 1 reads x0 on the first port, and a is 0.

The ALU at work: rows 3 to 6 have the same operands, 7 and 5. Only alu_operation differs, and the results are c, 2, 5 and 7.

Rule 6 at work: row 10 adds 7 and 5 and names x0 as rd. The ALU gives c, and x0 stays 0.

The last row counts 10 cycles for 10 instructions. The real core needed 12 cycles. Ten are for the instructions and one is for the ebreak. One more, the first after reset, only loads the PC.

Why It Is Built This Way

Design choiceThe alternativeWhat the choice buys, and costs
Two read portsOne read port, used twiceBoth operands arrive in the same cycle. The file needs two full sets of multiplexers.
Reads without a clock, writes at the edgeClocked readsA register is read and used in one cycle. The read takes time out of the cycle.
x0 has no storagex0 is a register that software keeps at 0A zero is always at hand, and 32 flip-flops are saved. Each read port needs a compare with 0.
One ALU for every instructionAn adder, a shifter and a comparer of their ownOne block, one result wire. Every instruction waits for the slowest operation.
A multiplexer in front of the ALUA second ALU for immediatesThe same ALU serves add and addi. One more control signal.
A memory that answers in the same cycleA real memory, which answers a cycle laterThe core stays one cycle long. A real SRAM cannot do this: ap_rv32_sc is for teaching.

The Trap: The Register That Is Read and Written at Once

Look at the instruction add x5, x5, x5. It reads x5 and writes x5. The result goes into the register file, and the register file feeds the ALU. That looks like a loop.

A reader who sees a loop expects trouble. Perhaps the new value runs round again within the cycle. With 3 in x5, would the add give 6, or 12, or more?

The same lab with another program. The real core and the model gave the same rows.
00000000 | addi x5, x0, 3 | a=0 b=3 alu=3 | x5 = 00000003
00000004 | add x5, x5, x5 | a=3 b=3 alu=6 | x5 = 00000006
00000008 | add x5, x5, x5 | a=6 b=6 alu=c | x5 = 0000000c
0000000c | addi x5, x5, 1 | a=c b=1 alu=d | x5 = 0000000d
00000010 | ebreak | stop
x5=13

Read row 2. Both operands are 3, the old value, for the whole cycle. The result 6 waits at the write port. Only the clock edge puts it into x5.

Read row 3. Now both operands are 6. Each add doubled the value once, and the last row shows 13.

There is no loop, because the write port has a clock. Remove that clock, and the value would run round as fast as the gates allow. That is why state changes only at an edge.

At Scale

This page2 kinds of instruction, 3 control signals
ap_rv32_sc48 instructions, 24 control signals

This section looks ahead. The datapath of this page is the top third of the real core.

  1. Memory, branches and jumps

    A load needs a second memory, for data, behind the ALU. A branch needs a second source for the PC. The next lesson adds both.

  2. The decoder

    The given control signals come from the instruction word. S2.5 builds the block that makes them.

  3. The length of the cycle

    The clock must wait for the slowest instruction. S2.6 finds that path and counts its gates.

  4. A register file for overlapping instructions

    When instructions overlap, one may read a register that an older one has not yet written. S2.8 deals with that.

Try It

Run the lab. Find the row where the second operand is an immediate, and the row where the write is thrown away.

Add a row that computes x14 = x7 - x8. You must give its class, its ALU operation and its immediate, as the decoder would.

Give the row for sub the class of the addi rows by mistake. Which wire changes, and what does x8 become?

RUNIcarus Verilog · simulator service
Edit the code, then run it
Expected: 00000000 | addi x5, x0, 7 | a=0 b=7 alu=7 | x5 = 00000007 00000004 | addi x6, x0, 5 | a=0 b=5 alu=5 | x6 = 00000005 00000008 | add x7, x5, x6 | a=7 b=5 alu=c | x7 = 0000000c 0000000c | sub x8, x5, x6 | a=7 b=5 alu=2 | x8 = 00000002 00000010 | and x9, x5, x6 | a=7 b=5 alu=5 | x9 = 00000005 00000014 | or x10, x5, x6 | a=7 b=5 alu=7 | x10 = 00000007 00000018 | slli x11, x5, 4 | a=7 b=4 alu=70 | x11 = 00000070 0000001c | slt x12, x6, x5 | a=5 b=7 alu=1 | x12 = 00000001 00000020 | xori x13, x7, -1 | a=c b=ffffffff alu=fffffff3 | x13 = fffffff3 00000024 | add x0, x5, x6 | a=7 b=5 alu=c | x0 = 00000000 00000028 | ebreak | stop x7=12 x8=2 x13=fffffff3 cycles=10
Read included file: ap_rv32_pkg.sv
// ap_rv32_pkg: the named constants of the ap_rv32 core.
// Opcodes, function fields, control-signal values, trap causes and CSR addresses.
// Used by both builds (ap_rv32_sc and ap_rv32). Taught in S2.2 and S2.5.
// Blueprint: docs/soc/ap_rv32-blueprint.md sections 4 (instructions), 5 (CSRs) and 7 (traps).
package ap_rv32_pkg;

  // ---- Opcodes: instruction bits 6:0
  localparam logic [6:0] OPCODE_LUI      = 7'b0110111;
  localparam logic [6:0] OPCODE_AUIPC    = 7'b0010111;
  localparam logic [6:0] OPCODE_JAL      = 7'b1101111;
  localparam logic [6:0] OPCODE_JALR     = 7'b1100111;
  localparam logic [6:0] OPCODE_BRANCH   = 7'b1100011;
  localparam logic [6:0] OPCODE_LOAD     = 7'b0000011;
  localparam logic [6:0] OPCODE_STORE    = 7'b0100011;
  localparam logic [6:0] OPCODE_OP_IMM   = 7'b0010011;
  localparam logic [6:0] OPCODE_OP       = 7'b0110011;
  localparam logic [6:0] OPCODE_MISC_MEM = 7'b0001111;
  localparam logic [6:0] OPCODE_SYSTEM   = 7'b1110011;

  // ---- funct3 (instruction bits 14:12) of the branches
  localparam logic [2:0] FUNCT3_BEQ  = 3'b000;
  localparam logic [2:0] FUNCT3_BNE  = 3'b001;
  localparam logic [2:0] FUNCT3_BLT  = 3'b100;
  localparam logic [2:0] FUNCT3_BGE  = 3'b101;
  localparam logic [2:0] FUNCT3_BLTU = 3'b110;
  localparam logic [2:0] FUNCT3_BGEU = 3'b111;

  // ---- funct3 of the arithmetic instructions (OP and OP_IMM) that the decoder must tell apart.
  // All eight values are the low three bits of alu_operation, below.
  localparam logic [2:0] FUNCT3_ADD_SUB = 3'b000;
  localparam logic [2:0] FUNCT3_SLL     = 3'b001;
  localparam logic [2:0] FUNCT3_SRL_SRA = 3'b101;

  // ---- funct3 of JALR and FENCE. Loads and stores: bits 1:0 are mem_size, bit 2 is unsigned.
  // SYSTEM: 000 is ecall, ebreak, mret, wfi; else bits 1:0 are csr_operation, bit 2 is immediate.
  localparam logic [2:0] FUNCT3_JALR  = 3'b000;
  localparam logic [2:0] FUNCT3_FENCE = 3'b000;

  // ---- funct7 (instruction bits 31:25)
  localparam logic [6:0] FUNCT7_BASE = 7'b0000000;
  localparam logic [6:0] FUNCT7_ALT  = 7'b0100000; // sub, sra, srai

  // ---- Instructions matched on all 32 bits
  localparam logic [31:0] INSTR_ECALL  = 32'h0000_0073;
  localparam logic [31:0] INSTR_EBREAK = 32'h0010_0073;
  localparam logic [31:0] INSTR_MRET   = 32'h3020_0073;
  localparam logic [31:0] INSTR_WFI    = 32'h1050_0073;

  // ---- imm_select: which immediate format the instruction carries
  localparam logic [2:0] IMM_I = 3'd0;
  localparam logic [2:0] IMM_S = 3'd1;
  localparam logic [2:0] IMM_B = 3'd2;
  localparam logic [2:0] IMM_U = 3'd3;
  localparam logic [2:0] IMM_J = 3'd4;

  // ---- alu_operation: bit 3 is instruction bit 30, bits 2:0 are funct3
  localparam logic [3:0] ALU_ADD  = 4'b0000;
  localparam logic [3:0] ALU_SUB  = 4'b1000;
  localparam logic [3:0] ALU_SLL  = 4'b0001;
  localparam logic [3:0] ALU_SLT  = 4'b0010;
  localparam logic [3:0] ALU_SLTU = 4'b0011;
  localparam logic [3:0] ALU_XOR  = 4'b0100;
  localparam logic [3:0] ALU_SRL  = 4'b0101;
  localparam logic [3:0] ALU_SRA  = 4'b1101;
  localparam logic [3:0] ALU_OR   = 4'b0110;
  localparam logic [3:0] ALU_AND  = 4'b0111;

  // ---- alu_a_select and alu_b_select: the two ALU operands
  localparam logic [1:0] ALU_A_RS1  = 2'd0;
  localparam logic [1:0] ALU_A_PC   = 2'd1;
  localparam logic [1:0] ALU_A_ZERO = 2'd2;
  localparam logic       ALU_B_RS2  = 1'b0;
  localparam logic       ALU_B_IMM  = 1'b1;

  // ---- result_select: the value written to rd
  localparam logic [1:0] RESULT_ALU       = 2'd0;
  localparam logic [1:0] RESULT_MEM       = 2'd1;
  localparam logic [1:0] RESULT_PC_PLUS_4 = 2'd2;
  localparam logic [1:0] RESULT_CSR       = 2'd3;

  // ---- mem_size: bytes moved by a load or store (as AHB hsize)
  localparam logic [1:0] SIZE_BYTE = 2'd0;
  localparam logic [1:0] SIZE_HALF = 2'd1;
  localparam logic [1:0] SIZE_WORD = 2'd2;

  // ---- csr_operation: what a CSR instruction does with its operand
  localparam logic [1:0] CSR_OPERATION_NONE  = 2'd0; // not a CSR instruction
  localparam logic [1:0] CSR_OPERATION_WRITE = 2'd1;
  localparam logic [1:0] CSR_OPERATION_SET   = 2'd2;
  localparam logic [1:0] CSR_OPERATION_CLEAR = 2'd3;

  // ---- Trap causes: the value written to mcause (bit 31 marks an interrupt)
  localparam logic [31:0] CAUSE_INSTR_ADDR_MISALIGNED = 32'd0;
  localparam logic [31:0] CAUSE_INSTR_ACCESS_FAULT    = 32'd1;
  localparam logic [31:0] CAUSE_ILLEGAL_INSTRUCTION   = 32'd2;
  localparam logic [31:0] CAUSE_BREAKPOINT            = 32'd3;
  localparam logic [31:0] CAUSE_LOAD_ADDR_MISALIGNED  = 32'd4;
  localparam logic [31:0] CAUSE_LOAD_ACCESS_FAULT     = 32'd5;
  localparam logic [31:0] CAUSE_STORE_ADDR_MISALIGNED = 32'd6;
  localparam logic [31:0] CAUSE_STORE_ACCESS_FAULT    = 32'd7;
  localparam logic [31:0] CAUSE_ECALL_FROM_M          = 32'd11;
  localparam logic [31:0] CAUSE_IRQ_SOFTWARE          = 32'h8000_0003;
  localparam logic [31:0] CAUSE_IRQ_TIMER             = 32'h8000_0007;
  localparam logic [31:0] CAUSE_IRQ_EXTERNAL          = 32'h8000_000b;

  // ---- Bit positions in mstatus, and in mie and mip
  localparam int MSTATUS_MIE_BIT  = 3;
  localparam int MSTATUS_MPIE_BIT = 7;
  localparam int IRQ_SOFTWARE_BIT = 3;
  localparam int IRQ_TIMER_BIT    = 7;
  localparam int IRQ_EXTERNAL_BIT = 11;

  // ---- CSR addresses (blueprint 5.1)
  localparam logic [11:0] CSR_MSTATUS    = 12'h300;
  localparam logic [11:0] CSR_MISA       = 12'h301;
  localparam logic [11:0] CSR_MIE        = 12'h304;
  localparam logic [11:0] CSR_MTVEC      = 12'h305;
  localparam logic [11:0] CSR_MSTATUSH   = 12'h310;
  localparam logic [11:0] CSR_MSCRATCH   = 12'h340;
  localparam logic [11:0] CSR_MEPC       = 12'h341;
  localparam logic [11:0] CSR_MCAUSE     = 12'h342;
  localparam logic [11:0] CSR_MTVAL      = 12'h343;
  localparam logic [11:0] CSR_MIP        = 12'h344;
  localparam logic [11:0] CSR_MCYCLE     = 12'hb00;
  localparam logic [11:0] CSR_MINSTRET   = 12'hb02;
  localparam logic [11:0] CSR_MCYCLEH    = 12'hb80;
  localparam logic [11:0] CSR_MINSTRETH  = 12'hb82;
  localparam logic [11:0] CSR_MVENDORID  = 12'hf11;
  localparam logic [11:0] CSR_MARCHID    = 12'hf12;
  localparam logic [11:0] CSR_MIMPID     = 12'hf13;
  localparam logic [11:0] CSR_MHARTID    = 12'hf14;
  localparam logic [11:0] CSR_MCONFIGPTR = 12'hf15;
  // Performance monitors 3 to 31 exist and read 0: first and last address of each range
  localparam logic [11:0] CSR_MHPMEVENT3      = 12'h323;
  localparam logic [11:0] CSR_MHPMEVENT31     = 12'h33f;
  localparam logic [11:0] CSR_MHPMCOUNTER3    = 12'hb03;
  localparam logic [11:0] CSR_MHPMCOUNTER31   = 12'hb1f;
  localparam logic [11:0] CSR_MHPMCOUNTER3H   = 12'hb83;
  localparam logic [11:0] CSR_MHPMCOUNTER31H  = 12'hb9f;
  // Address bits 11:10 of a read-only CSR
  localparam logic [1:0]  CSR_READ_ONLY = 2'b11;

  // ---- CSR values and write masks (a 1 marks a bit that software can change)
  localparam logic [31:0] MSTATUS_RESET      = 32'h0000_1800; // MPP = 11: machine mode only
  localparam logic [31:0] MSTATUS_WRITE_MASK = 32'h0000_0088; // MIE and MPIE
  localparam logic [31:0] MISA_VALUE         = 32'h4000_0100; // 32 bits, extension I
  localparam logic [31:0] MIMPID_VALUE       = 32'h0001_0000; // ap_rv32 version 1.0
  localparam logic [31:0] MIE_WRITE_MASK     = 32'h0000_0888; // MSIE, MTIE and MEIE
  localparam logic [31:0] MTVEC_WRITE_MASK   = 32'hffff_fffd; // BASE and MODE bit 0
  localparam logic [31:0] MEPC_WRITE_MASK    = 32'hffff_fffc; // instructions are 4-byte aligned
  localparam logic [31:0] ALL_BITS           = 32'hffff_ffff;

  // ---- Data bits that a byte and a half cover, from bit 0
  localparam logic [31:0] BYTE_BITS = 32'h0000_00ff;
  localparam logic [31:0] HALF_BITS = 32'h0000_ffff;

  // ---- Other
  localparam logic [1:0] PRIV_MACHINE   = 2'b11; // the only privilege level in v1.0
  localparam int         REGISTER_COUNT = 32;

endpackage
Read included file: ap_rv32_regfile.sv
// ap_rv32_regfile: the register file.
// 31 registers, x1 to x31, and x0 which always reads 0. Two read ports, one write port.
// A read is combinational; a write happens at the clock edge. All registers reset to 0.
// Used by both builds. Taught in S2.4. Blueprint: sections 2.3 and 6.
module ap_rv32_regfile (
  input  logic        clk,          // clock
  input  logic        rst_n,        // reset, active low
  input  logic [4:0]  rs1_addr,     // first register to read
  output logic [31:0] rs1_data,     // its value
  input  logic [4:0]  rs2_addr,     // second register to read
  output logic [31:0] rs2_data,     // its value
  input  logic        write_enable, // write rd at this clock edge
  input  logic [4:0]  rd_addr,      // register to write; a write to x0 is discarded
  input  logic [31:0] rd_data       // value to write
);
  import ap_rv32_pkg::*;

  logic [31:0] registers_q [1:REGISTER_COUNT-1]; // x1 to x31; x0 has no storage

  always_ff @(posedge clk or negedge rst_n) begin
    if (!rst_n) begin
      for (int i = 1; i < REGISTER_COUNT; i++) registers_q[i] <= '0;
    end else if (write_enable && rd_addr != '0) begin
      registers_q[rd_addr] <= rd_data;
    end
  end

  assign rs1_data = (rs1_addr == '0) ? '0 : registers_q[rs1_addr];
  assign rs2_data = (rs2_addr == '0) ? '0 : registers_q[rs2_addr];

endmodule
Read included file: ap_rv32_alu.sv
// ap_rv32_alu: the arithmetic and logic unit.
// Add, subtract, shifts, compares and bitwise logic on two 32-bit operands.
// Used by both builds. Taught in S2.4. Blueprint: section 2.3.
module ap_rv32_alu (
  input  logic [3:0]  alu_operation, // operation: one of the ALU_* constants
  input  logic [31:0] operand_a,     // first operand
  input  logic [31:0] operand_b,     // second operand; shifts use only its bits 4:0
  output logic [31:0] result         // result of the operation
);
  import ap_rv32_pkg::*;

  logic [4:0] shift_amount; // number of bit positions to shift by

  assign shift_amount = operand_b[4:0];

  always_comb begin
    result = '0;
    unique case (alu_operation)
      ALU_ADD:  result = operand_a + operand_b;
      ALU_SUB:  result = operand_a - operand_b;
      ALU_SLL:  result = operand_a << shift_amount;
      ALU_SLT:  result = {31'b0, $signed(operand_a) < $signed(operand_b)};
      ALU_SLTU: result = {31'b0, operand_a < operand_b};
      ALU_XOR:  result = operand_a ^ operand_b;
      ALU_SRL:  result = operand_a >> shift_amount;
      ALU_SRA:  result = $signed(operand_a) >>> shift_amount;
      ALU_OR:   result = operand_a | operand_b;
      ALU_AND:  result = operand_a & operand_b;
      default:  result = '0;
    endcase
  end

endmodule

Check Yourself

Predict the outputThis program puts 12 into x5 and 10 into x6. It subtracts, shifts the difference left by 3, and combines it with x5 by or. What does the last row say?
// What does x7 hold at the end?
// The bench plays the decoder and the immediate generator: each program row says what they would say.
// The files ap_rv32_pkg.sv, ap_rv32_regfile.sv, ap_rv32_alu.sv are the files of the real core.
module lab_datapath (
  input  logic        clk,            // clock
  input  logic        rst_n,          // reset, active low
  output logic [31:0] imem_addr,      // instruction memory: the PC
  input  logic [31:0] imem_rdata,     // the instruction at the PC, in the same cycle
  input  logic [31:0] imm,            // given: the immediate of this instruction
  input  logic        alu_b_select,   // given: second ALU operand, rs2 or the immediate
  input  logic [3:0]  alu_operation,  // given: what the ALU does
  input  logic        reg_write       // given: this instruction writes rd
);
  import ap_rv32_pkg::*;

  logic [31:0] pc_q;           // address of the instruction executing this cycle
  logic [31:0] instr;          // the instruction word
  logic [31:0] rs1_data;       // value of register rs1
  logic [31:0] rs2_data;       // value of register rs2
  logic [31:0] alu_operand_b;  // second ALU operand
  logic [31:0] alu_result;     // ALU result
  logic [31:0] result;         // value for rd
  logic [31:0] pc_plus_4;      // address of the next instruction in memory
  logic [31:0] pc_next;        // address of the instruction that executes next

  // ---- Fetch: the PC addresses the instruction memory.
  assign imem_addr = pc_q;
  assign instr     = imem_rdata;

  // ---- Read two registers. The write port is at the bottom of the picture: it takes the result.
  ap_rv32_regfile u_regfile (
    .clk(clk), .rst_n(rst_n),
    .rs1_addr(instr[19:15]), .rs1_data(rs1_data),
    .rs2_addr(instr[24:20]), .rs2_data(rs2_data),
    .write_enable(reg_write), .rd_addr(instr[11:7]), .rd_data(result)
  );

  // ---- Execute: the ALU.
  assign alu_operand_b = (alu_b_select == ALU_B_IMM) ? imm : rs2_data;

  ap_rv32_alu u_alu (
    .alu_operation(alu_operation), .operand_a(rs1_data), .operand_b(alu_operand_b),
    .result(alu_result)
  );

  // ---- Write back: on this page the ALU result is the only value for rd.
  assign result = alu_result;

  // ---- Next PC: on this page always the next word.
  assign pc_plus_4 = pc_q + 32'd4;
  assign pc_next   = pc_plus_4;

  always_ff @(posedge clk or negedge rst_n) begin
    if (!rst_n) pc_q <= '0;
    else pc_q <= pc_next;
  end
endmodule

module tb;
  import ap_rv32_pkg::*;
  logic        clk = 0, rst_n = 1;
  logic [31:0] imem_addr, imem_rdata, imm;
  logic        alu_b_select, reg_write;
  logic [3:0]  alu_operation;

  lab_datapath dp (.*);

  // ---- What the decoder says for each class of instruction. S2.5 opens this box.
  //                                reg_write, alu_b_select
  localparam logic [1:0] OP_IMM = {1'b1, ALU_B_IMM};
  localparam logic [1:0] OP     = {1'b1, ALU_B_RS2};
  localparam logic [1:0] STOP   = {1'b0, ALU_B_IMM};

  // ---- The memories of the bench.
  logic [31:0] rom [0:63];          // instruction memory, from address 0
  logic [1:0]  given [0:63];        // the control class of each row
  logic [3:0]  operation [0:63];    // the ALU operation of each row
  logic [31:0] immediate [0:63];    // the immediate of each row
  string       text [0:63];         // the assembly of each row, for the printed line
  int rows = 0, cycles = 0;
  task automatic row(input logic [31:0] word, input logic [1:0] control, input logic [3:0] alu, input logic [31:0] immediate_value, input string assembly);
    rom[rows] = word;  given[rows] = control;  operation[rows] = alu;  immediate[rows] = immediate_value;  text[rows] = assembly;  rows++;
  endtask

  // ---- The program.
  initial begin
    //  word          class   ALU      immediate    assembly
    row(32'h00c0_0293, OP_IMM, ALU_ADD, 32'd12,     "addi x5, x0, 12");   // 00
    row(32'h00a0_0313, OP_IMM, ALU_ADD, 32'd10,     "addi x6, x0, 10");   // 04
    row(32'h4062_83b3, OP,     ALU_SUB, 32'd0,      "sub x7, x5, x6");   // 08
    row(32'h0033_9393, OP_IMM, ALU_SLL, 32'd3,      "slli x7, x7, 3");   // 0c
    row(32'h0053_e3b3, OP,     ALU_OR,  32'd0,      "or x7, x7, x5");   // 10
    row(32'h0010_0073, STOP,   ALU_ADD, 32'd0,      "ebreak");   // 14
    #1 rst_n = 0;   // a real falling edge: the reset is asynchronous
    #2 rst_n = 1;
  end
  always #5 clk = ~clk;

  // ---- The wires between the bench and the datapath.
  assign imem_rdata    = rom[imem_addr[7:2]];
  assign imm           = immediate[imem_addr[7:2]];
  assign alu_operation = operation[imem_addr[7:2]];
  assign {reg_write, alu_b_select} = given[imem_addr[7:2]];

  function automatic logic [31:0] register(input int n);   // the value of register n, read from inside the register file
    return (n == 0) ? 32'd0 : dp.u_regfile.registers_q[n];
  endfunction

  // ---- One printed line for each instruction, at the clock edge that ends its cycle.
  string effect;
  always @(posedge clk) if (rst_n) begin
    cycles++;
    if (imem_rdata == 32'h0010_0073 || cycles > 200) begin
      if (cycles > 200) $display("still running after 200 cycles");
      else $display("%08h | ebreak | stop", imem_addr);
      $display("x7=%0d", register(7));
      $finish;
    end
    effect = "";
    if (reg_write) effect = $sformatf("x%0d = %08h", imem_rdata[11:7], (imem_rdata[11:7] == 0) ? 32'd0 : dp.result);
    $display("%08h | %s | %s", imem_addr, text[imem_addr[7:2]], effect);
  end
endmodule
Read included file: ap_rv32_pkg.sv
// ap_rv32_pkg: the named constants of the ap_rv32 core.
// Opcodes, function fields, control-signal values, trap causes and CSR addresses.
// Used by both builds (ap_rv32_sc and ap_rv32). Taught in S2.2 and S2.5.
// Blueprint: docs/soc/ap_rv32-blueprint.md sections 4 (instructions), 5 (CSRs) and 7 (traps).
package ap_rv32_pkg;

  // ---- Opcodes: instruction bits 6:0
  localparam logic [6:0] OPCODE_LUI      = 7'b0110111;
  localparam logic [6:0] OPCODE_AUIPC    = 7'b0010111;
  localparam logic [6:0] OPCODE_JAL      = 7'b1101111;
  localparam logic [6:0] OPCODE_JALR     = 7'b1100111;
  localparam logic [6:0] OPCODE_BRANCH   = 7'b1100011;
  localparam logic [6:0] OPCODE_LOAD     = 7'b0000011;
  localparam logic [6:0] OPCODE_STORE    = 7'b0100011;
  localparam logic [6:0] OPCODE_OP_IMM   = 7'b0010011;
  localparam logic [6:0] OPCODE_OP       = 7'b0110011;
  localparam logic [6:0] OPCODE_MISC_MEM = 7'b0001111;
  localparam logic [6:0] OPCODE_SYSTEM   = 7'b1110011;

  // ---- funct3 (instruction bits 14:12) of the branches
  localparam logic [2:0] FUNCT3_BEQ  = 3'b000;
  localparam logic [2:0] FUNCT3_BNE  = 3'b001;
  localparam logic [2:0] FUNCT3_BLT  = 3'b100;
  localparam logic [2:0] FUNCT3_BGE  = 3'b101;
  localparam logic [2:0] FUNCT3_BLTU = 3'b110;
  localparam logic [2:0] FUNCT3_BGEU = 3'b111;

  // ---- funct3 of the arithmetic instructions (OP and OP_IMM) that the decoder must tell apart.
  // All eight values are the low three bits of alu_operation, below.
  localparam logic [2:0] FUNCT3_ADD_SUB = 3'b000;
  localparam logic [2:0] FUNCT3_SLL     = 3'b001;
  localparam logic [2:0] FUNCT3_SRL_SRA = 3'b101;

  // ---- funct3 of JALR and FENCE. Loads and stores: bits 1:0 are mem_size, bit 2 is unsigned.
  // SYSTEM: 000 is ecall, ebreak, mret, wfi; else bits 1:0 are csr_operation, bit 2 is immediate.
  localparam logic [2:0] FUNCT3_JALR  = 3'b000;
  localparam logic [2:0] FUNCT3_FENCE = 3'b000;

  // ---- funct7 (instruction bits 31:25)
  localparam logic [6:0] FUNCT7_BASE = 7'b0000000;
  localparam logic [6:0] FUNCT7_ALT  = 7'b0100000; // sub, sra, srai

  // ---- Instructions matched on all 32 bits
  localparam logic [31:0] INSTR_ECALL  = 32'h0000_0073;
  localparam logic [31:0] INSTR_EBREAK = 32'h0010_0073;
  localparam logic [31:0] INSTR_MRET   = 32'h3020_0073;
  localparam logic [31:0] INSTR_WFI    = 32'h1050_0073;

  // ---- imm_select: which immediate format the instruction carries
  localparam logic [2:0] IMM_I = 3'd0;
  localparam logic [2:0] IMM_S = 3'd1;
  localparam logic [2:0] IMM_B = 3'd2;
  localparam logic [2:0] IMM_U = 3'd3;
  localparam logic [2:0] IMM_J = 3'd4;

  // ---- alu_operation: bit 3 is instruction bit 30, bits 2:0 are funct3
  localparam logic [3:0] ALU_ADD  = 4'b0000;
  localparam logic [3:0] ALU_SUB  = 4'b1000;
  localparam logic [3:0] ALU_SLL  = 4'b0001;
  localparam logic [3:0] ALU_SLT  = 4'b0010;
  localparam logic [3:0] ALU_SLTU = 4'b0011;
  localparam logic [3:0] ALU_XOR  = 4'b0100;
  localparam logic [3:0] ALU_SRL  = 4'b0101;
  localparam logic [3:0] ALU_SRA  = 4'b1101;
  localparam logic [3:0] ALU_OR   = 4'b0110;
  localparam logic [3:0] ALU_AND  = 4'b0111;

  // ---- alu_a_select and alu_b_select: the two ALU operands
  localparam logic [1:0] ALU_A_RS1  = 2'd0;
  localparam logic [1:0] ALU_A_PC   = 2'd1;
  localparam logic [1:0] ALU_A_ZERO = 2'd2;
  localparam logic       ALU_B_RS2  = 1'b0;
  localparam logic       ALU_B_IMM  = 1'b1;

  // ---- result_select: the value written to rd
  localparam logic [1:0] RESULT_ALU       = 2'd0;
  localparam logic [1:0] RESULT_MEM       = 2'd1;
  localparam logic [1:0] RESULT_PC_PLUS_4 = 2'd2;
  localparam logic [1:0] RESULT_CSR       = 2'd3;

  // ---- mem_size: bytes moved by a load or store (as AHB hsize)
  localparam logic [1:0] SIZE_BYTE = 2'd0;
  localparam logic [1:0] SIZE_HALF = 2'd1;
  localparam logic [1:0] SIZE_WORD = 2'd2;

  // ---- csr_operation: what a CSR instruction does with its operand
  localparam logic [1:0] CSR_OPERATION_NONE  = 2'd0; // not a CSR instruction
  localparam logic [1:0] CSR_OPERATION_WRITE = 2'd1;
  localparam logic [1:0] CSR_OPERATION_SET   = 2'd2;
  localparam logic [1:0] CSR_OPERATION_CLEAR = 2'd3;

  // ---- Trap causes: the value written to mcause (bit 31 marks an interrupt)
  localparam logic [31:0] CAUSE_INSTR_ADDR_MISALIGNED = 32'd0;
  localparam logic [31:0] CAUSE_INSTR_ACCESS_FAULT    = 32'd1;
  localparam logic [31:0] CAUSE_ILLEGAL_INSTRUCTION   = 32'd2;
  localparam logic [31:0] CAUSE_BREAKPOINT            = 32'd3;
  localparam logic [31:0] CAUSE_LOAD_ADDR_MISALIGNED  = 32'd4;
  localparam logic [31:0] CAUSE_LOAD_ACCESS_FAULT     = 32'd5;
  localparam logic [31:0] CAUSE_STORE_ADDR_MISALIGNED = 32'd6;
  localparam logic [31:0] CAUSE_STORE_ACCESS_FAULT    = 32'd7;
  localparam logic [31:0] CAUSE_ECALL_FROM_M          = 32'd11;
  localparam logic [31:0] CAUSE_IRQ_SOFTWARE          = 32'h8000_0003;
  localparam logic [31:0] CAUSE_IRQ_TIMER             = 32'h8000_0007;
  localparam logic [31:0] CAUSE_IRQ_EXTERNAL          = 32'h8000_000b;

  // ---- Bit positions in mstatus, and in mie and mip
  localparam int MSTATUS_MIE_BIT  = 3;
  localparam int MSTATUS_MPIE_BIT = 7;
  localparam int IRQ_SOFTWARE_BIT = 3;
  localparam int IRQ_TIMER_BIT    = 7;
  localparam int IRQ_EXTERNAL_BIT = 11;

  // ---- CSR addresses (blueprint 5.1)
  localparam logic [11:0] CSR_MSTATUS    = 12'h300;
  localparam logic [11:0] CSR_MISA       = 12'h301;
  localparam logic [11:0] CSR_MIE        = 12'h304;
  localparam logic [11:0] CSR_MTVEC      = 12'h305;
  localparam logic [11:0] CSR_MSTATUSH   = 12'h310;
  localparam logic [11:0] CSR_MSCRATCH   = 12'h340;
  localparam logic [11:0] CSR_MEPC       = 12'h341;
  localparam logic [11:0] CSR_MCAUSE     = 12'h342;
  localparam logic [11:0] CSR_MTVAL      = 12'h343;
  localparam logic [11:0] CSR_MIP        = 12'h344;
  localparam logic [11:0] CSR_MCYCLE     = 12'hb00;
  localparam logic [11:0] CSR_MINSTRET   = 12'hb02;
  localparam logic [11:0] CSR_MCYCLEH    = 12'hb80;
  localparam logic [11:0] CSR_MINSTRETH  = 12'hb82;
  localparam logic [11:0] CSR_MVENDORID  = 12'hf11;
  localparam logic [11:0] CSR_MARCHID    = 12'hf12;
  localparam logic [11:0] CSR_MIMPID     = 12'hf13;
  localparam logic [11:0] CSR_MHARTID    = 12'hf14;
  localparam logic [11:0] CSR_MCONFIGPTR = 12'hf15;
  // Performance monitors 3 to 31 exist and read 0: first and last address of each range
  localparam logic [11:0] CSR_MHPMEVENT3      = 12'h323;
  localparam logic [11:0] CSR_MHPMEVENT31     = 12'h33f;
  localparam logic [11:0] CSR_MHPMCOUNTER3    = 12'hb03;
  localparam logic [11:0] CSR_MHPMCOUNTER31   = 12'hb1f;
  localparam logic [11:0] CSR_MHPMCOUNTER3H   = 12'hb83;
  localparam logic [11:0] CSR_MHPMCOUNTER31H  = 12'hb9f;
  // Address bits 11:10 of a read-only CSR
  localparam logic [1:0]  CSR_READ_ONLY = 2'b11;

  // ---- CSR values and write masks (a 1 marks a bit that software can change)
  localparam logic [31:0] MSTATUS_RESET      = 32'h0000_1800; // MPP = 11: machine mode only
  localparam logic [31:0] MSTATUS_WRITE_MASK = 32'h0000_0088; // MIE and MPIE
  localparam logic [31:0] MISA_VALUE         = 32'h4000_0100; // 32 bits, extension I
  localparam logic [31:0] MIMPID_VALUE       = 32'h0001_0000; // ap_rv32 version 1.0
  localparam logic [31:0] MIE_WRITE_MASK     = 32'h0000_0888; // MSIE, MTIE and MEIE
  localparam logic [31:0] MTVEC_WRITE_MASK   = 32'hffff_fffd; // BASE and MODE bit 0
  localparam logic [31:0] MEPC_WRITE_MASK    = 32'hffff_fffc; // instructions are 4-byte aligned
  localparam logic [31:0] ALL_BITS           = 32'hffff_ffff;

  // ---- Data bits that a byte and a half cover, from bit 0
  localparam logic [31:0] BYTE_BITS = 32'h0000_00ff;
  localparam logic [31:0] HALF_BITS = 32'h0000_ffff;

  // ---- Other
  localparam logic [1:0] PRIV_MACHINE   = 2'b11; // the only privilege level in v1.0
  localparam int         REGISTER_COUNT = 32;

endpackage
Read included file: ap_rv32_regfile.sv
// ap_rv32_regfile: the register file.
// 31 registers, x1 to x31, and x0 which always reads 0. Two read ports, one write port.
// A read is combinational; a write happens at the clock edge. All registers reset to 0.
// Used by both builds. Taught in S2.4. Blueprint: sections 2.3 and 6.
module ap_rv32_regfile (
  input  logic        clk,          // clock
  input  logic        rst_n,        // reset, active low
  input  logic [4:0]  rs1_addr,     // first register to read
  output logic [31:0] rs1_data,     // its value
  input  logic [4:0]  rs2_addr,     // second register to read
  output logic [31:0] rs2_data,     // its value
  input  logic        write_enable, // write rd at this clock edge
  input  logic [4:0]  rd_addr,      // register to write; a write to x0 is discarded
  input  logic [31:0] rd_data       // value to write
);
  import ap_rv32_pkg::*;

  logic [31:0] registers_q [1:REGISTER_COUNT-1]; // x1 to x31; x0 has no storage

  always_ff @(posedge clk or negedge rst_n) begin
    if (!rst_n) begin
      for (int i = 1; i < REGISTER_COUNT; i++) registers_q[i] <= '0;
    end else if (write_enable && rd_addr != '0) begin
      registers_q[rd_addr] <= rd_data;
    end
  end

  assign rs1_data = (rs1_addr == '0) ? '0 : registers_q[rs1_addr];
  assign rs2_data = (rs2_addr == '0) ? '0 : registers_q[rs2_addr];

endmodule
Read included file: ap_rv32_alu.sv
// ap_rv32_alu: the arithmetic and logic unit.
// Add, subtract, shifts, compares and bitwise logic on two 32-bit operands.
// Used by both builds. Taught in S2.4. Blueprint: section 2.3.
module ap_rv32_alu (
  input  logic [3:0]  alu_operation, // operation: one of the ALU_* constants
  input  logic [31:0] operand_a,     // first operand
  input  logic [31:0] operand_b,     // second operand; shifts use only its bits 4:0
  output logic [31:0] result         // result of the operation
);
  import ap_rv32_pkg::*;

  logic [4:0] shift_amount; // number of bit positions to shift by

  assign shift_amount = operand_b[4:0];

  always_comb begin
    result = '0;
    unique case (alu_operation)
      ALU_ADD:  result = operand_a + operand_b;
      ALU_SUB:  result = operand_a - operand_b;
      ALU_SLL:  result = operand_a << shift_amount;
      ALU_SLT:  result = {31'b0, $signed(operand_a) < $signed(operand_b)};
      ALU_SLTU: result = {31'b0, operand_a < operand_b};
      ALU_XOR:  result = operand_a ^ operand_b;
      ALU_SRL:  result = operand_a >> shift_amount;
      ALU_SRA:  result = $signed(operand_a) >>> shift_amount;
      ALU_OR:   result = operand_a | operand_b;
      ALU_AND:  result = operand_a & operand_b;
      default:  result = '0;
    endcase
  end

endmodule
ConceptWhich parts of the datapath of this page hold state?
ConceptHow does the register file know which two registers to read?
ConceptWhat is the second operand of the ALU for addi x5, x5, 1?
ConceptWhen does the result of an add reach the register rd?
ConceptWhat happens to the PC while an add is in the ALU?

Common Mistakes

Wrong Expect the new value of a register in the cycle that writes it.

The write happens at the clock edge. Until then every read gives the old value.

Fix Read the new value in the next cycle.

Wrong Wait for the decoder before reading the registers.

The fields rs1 and rs2 are at fixed places. The read can start at once.

Fix Send the fields to the read ports directly, and decode beside them.

Wrong Leave reg_write at 1 for every instruction.

Then every instruction changes the register that its bits 11 to 7 name.

Fix Set reg_write only for an instruction that has a result for rd.

Remember

  • A single-cycle core holds state in the PC and the register file. Both change only at a rising clock edge.
  • The fields rs1 and rs2 go straight to the read ports. The values come back in the same cycle.
  • The ALU takes rs1_data, and either rs2_data or the immediate. The control signal alu_b_select chooses.
  • The result is written to rd at the edge when reg_write is 1. A write to x0 is thrown away.

Practice

Report a problem with this page