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.
| Question | |
|---|---|
| 1 | Which parts of a single-cycle core hold state? |
| 2 | How does the register file know which registers to read? |
| 3 | What are the two operands of the ALU? |
| 4 | When does the result reach the register rd? |
| 5 | What 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
| Word | Meaning |
|---|---|
| State of a circuit | What a circuit remembers from one clock cycle to the next. Here: the PC and the registers. |
| Combinational logic | Logic with no memory. Its outputs follow its inputs, with no clock. |
| Datapath of a processor | The hardware that the data of an instruction travels through. |
| Single-cycle | A processor that completes each instruction in one clock cycle. |
ap_rv32_sc | The single-cycle build of ap_rv32, the processor of this track. |
| Instruction memory | The memory that holds the program. The PC is its address. |
| Fetch | The step that reads the instruction word at the PC. |
| Write enable | A wire that is 1 when a write must happen at the next clock edge. |
| Read port | An address input and a data output. The data follows the address at once. |
| Write port | An address, a data input and a write enable. The write happens at the clock edge. |
| Register file | The block that holds the registers x1 to x31. It has two read ports and one write port. |
| ALU | Arithmetic and logic unit. The block that adds, subtracts, shifts, compares and combines bits. |
| Operand | One of the two values that the ALU works on. |
| Control signal | A wire that tells part of the datapath what this instruction needs. |
| Write back | The step that puts the result into the register rd. |
One Picture: From the PC to the Register
- Processor
- Memory
- Control / FSM
- Fetch
The PC holds 8. The instruction memory returns the word at address 8: 006283b3. No clock edge is needed for that.
- 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.
- 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.
- Execute
The decoder says the operation is an add. The ALU adds 7 and 5. Its result is c in hex, which is 12.
- 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.
- 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.
| Signal | Width | What it decides |
|---|---|---|
alu_b_select | 1 bit | 0: the second operand is rs2_data. 1: it is the immediate. |
alu_operation | 4 bits | Which of ten operations the ALU does. |
reg_write | 1 bit | 1: 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.
| Step | Wire | Value | Why |
|---|---|---|---|
| Fetch | imem_addr | 8 | the PC |
| Fetch | instr | 006283b3 | the word at address 8 |
| Read | rs1_data | 7 | the field rs1 is 5, and x5 holds 7 |
| Read | rs2_data | 5 | the field rs2 is 6, and x6 holds 5 |
| Select | alu_operand_b | 5 | alu_b_select is 0: take rs2_data |
| Execute | alu_result | c | the operation is an add: 7 plus 5 is 12 |
| Write back | result | c | reg_write is 1 and rd is 7: x7 takes 12 at the edge |
| Next PC | pc_next | c | 8 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
Rule 1: The PC is the address of the instruction memory.
The word comes back in the same cycle. That is fetch.
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.
Rule 3: The first ALU operand is rs1_data. The second is rs2_data or the immediate.
The control signal alu_b_select chooses.
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.
Rule 5: At that same edge, the PC takes pc_next.
On this page, pc_next is always the PC plus 4.
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.
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];
endmoduleThe 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.
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;
endcaseSources 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.
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=10Rule 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 choice | The alternative | What the choice buys, and costs |
|---|---|---|
| Two read ports | One read port, used twice | Both operands arrive in the same cycle. The file needs two full sets of multiplexers. |
| Reads without a clock, writes at the edge | Clocked reads | A register is read and used in one cycle. The read takes time out of the cycle. |
| x0 has no storage | x0 is a register that software keeps at 0 | A zero is always at hand, and 32 flip-flops are saved. Each read port needs a compare with 0. |
| One ALU for every instruction | An adder, a shifter and a comparer of their own | One block, one result wire. Every instruction waits for the slowest operation. |
| A multiplexer in front of the ALU | A second ALU for immediates | The same ALU serves add and addi. One more control signal. |
| A memory that answers in the same cycle | A real memory, which answers a cycle later | The 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?
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=13Read 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 section looks ahead. The datapath of this page is the top third of the real core.
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.
The decoder
The given control signals come from the instruction word. S2.5 builds the block that makes them.
The length of the cycle
The clock must wait for the slowest instruction. S2.6 finds that path and counts its gates.
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?
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=10Read 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
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.