The register file holds the 32 registers every RISC-V instruction reads and writes. In each clock cycle the core reads two registers, the two source operands, and writes at most one, the result. This article specifies the register file you write in exercise E03: its ports, the four rules that define its behavior, why register x0 is not really stored, why a read in the same cycle as a write returns the old value, and how the hidden test checks all of it. It is the first block of the core that has a clock, and the first one whose timing you must think about.
12 min read | Beginner to Intermediate
What You'll Learn
- Describe the ports of a register file with two read ports and one write port
- Apply the four rules of the core's register file: combinational reads, clocked writes, x0 and no write-through
- Predict what each read port returns cycle by cycle around a write
- Write a register file that passes E03 and explain what it costs in hardware
Before You Start
This block needs Verilog's clocked logic: always blocks triggered by a clock edge, nonblocking assignments and arrays. The Verilog Tutorial covers them.
Registers and the Register File in the first half of the tutorial explains what registers are for, why RISC-V has 32 and how a register file circuit is organized; this article builds on it and does not repeat it. The Core's Specification has the coding rules. You can do E03 before or after the ALU exercises; it does not depend on them.
What the Register File Does in the Core
Every instruction names its registers by number in its encoding: 5-bit fields rs1 and rs2 for the sources and rd for the destination (RISC-V Instruction Formats and Encoding). The decoder pulls these fields out of the instruction and gives them to the register file. The register file returns the two source values to the ALU, and at the end of the cycle stores the result in rd.
Take add a0, a0, a5, which adds register a5 (x15) to a0 (x10). In the cycle that executes it, the register file reads x10 on one port and x15 on the other, the ALU adds the two values, and at the clock edge that ends the cycle the sum is written into x10. The instruction reads and writes the same register in the same cycle, and that is fine: the read sees the old value, the write stores the new one. That one detail is the subject of the most important rule below.
The Ports
The module name and ports are fixed:
module cvr32_regfile (
input logic clk,
input logic [4:0] ra1, // read address 1 (rs1)
input logic [4:0] ra2, // read address 2 (rs2)
output logic [31:0] rd1, // value of register ra1
output logic [31:0] rd2, // value of register ra2
input logic we, // write enable
input logic [4:0] wa, // write address (rd)
input logic [31:0] wd // write data
);
There are two independent read ports, each with an address in and a value out, and one write port with an enable, an address and the data. There is no reset input: see "No Reset" below.
The Four Rules
- Reads are combinational.
rd1is the value of registerra1, andrd2ofra2, at all times, with no clock involved. Changera1andrd1follows. - Writes happen at the rising clock edge. When
weis 1 at a rising edge ofclk, registerwatakes the valuewd. Whenweis 0, nothing changes. - x0 is always zero. Reading register 0 on either port returns 0, and a write to register 0 is ignored.
- No write-through. A read in the same cycle as a write to the same register returns the value stored before the clock edge. The new value is visible after the edge.
Here is rule 4 cycle by cycle, with register x7 holding 0x0000_0011 at the start and a write of 0x7777_7777 to x7 in cycle 2:
| Cycle | we, wa, wd | ra1 | rd1 during the cycle |
|---|---|---|---|
| 1 | 0 | 7 | 0x0000_0011 |
| 2 | 1, 7, 0x7777_7777 | 7 | 0x0000_0011 (the write has not happened yet) |
| 3 | 0 | 7 | 0x7777_7777 (written at the edge between cycles 2 and 3) |
This is what a register file built from flip-flops does naturally: a flip-flop's output changes only after the clock edge. Some register files add a write-through (or bypass) path that sends wd straight to a read port when it reads the register being written, so that rd1 in cycle 2 would already be 0x7777_7777. The single-cycle core does not need it, because an instruction never needs its own result. The 3-stage pipeline you build later does need the newest value in that situation, and you will add it there as a separate, visible forwarding path, in A Data Hazard and Its Forwarding Path. For the test to know where to look for it, the register file itself must not have one.
Quick Check
Q: In one cycle, we = 1, wa = 0, wd = 0x1234_5678, and ra1 = ra2 = 0. What are rd1 and rd2 in this cycle and the next?
Show Answer
0 and 0 in both cycles. Register 0 always reads as zero, and the write to it is ignored. Instructions that write x0 exist (the assembler's nop is addi x0, x0, 0), so the register file will see such writes in real programs.
How It Is Built
Inside, the register file is three things: storage for 31 registers, a write decoder that enables one of them, and two read multiplexers:
Storage. Registers x1 to x31 are 31 rows of 32 flip-flops. In Verilog that is an array of 32-bit values with an index range of 1 to 31; there is no row for x0, because x0 never holds anything but zero. Storing it anyway costs 32 flip-flops and opens the door to a bug: a write to x0 that is not blocked changes it.
The write side. In a clocked block, at each rising edge, if we is set and wa is not 0, the selected row takes wd. Use a nonblocking assignment (<=), as for every register. In hardware, the condition becomes a decoder that turns the 5-bit address into one enable signal per row; the rows that are not enabled keep their value.
The read side. Each read port selects one of 32 values (31 rows and the constant zero for address 0) by its 5-bit address: a 32-to-1 multiplexer, 32 bits wide. Two ports means two of these multiplexers side by side, both connected to every row. They are written as continuous assignments or a combinational block, outside the clocked block, so that they follow the address immediately.
This is a lot of hardware for something so simple to describe. In the synthesized single-cycle core, 992 of the 1,024 flip-flops are the 31 registers (the other 32 are the program counter), and the two read multiplexers are among the largest pieces of logic in the core. Real processors with many registers and ports build register files from special memory cells instead of flip-flops for that reason; Registers and the Register File discusses the cost.
No Reset
The register file has no reset, and the registers start with no defined value. That is how most processors work: resetting 992 flip-flops costs area and power, and software never reads a register before writing it. The start-up code sets the stack pointer before anything else, and the compiler never generates a read of a register that has not been written.
The two simulators show undefined values differently. In Icarus Verilog an unwritten register reads as X, the unknown value, which makes the mistake of reading one visible in the waveform. In Verilator it reads as 0. A core that only works because registers start at zero passes in one and fails in the other; the hidden tests run on the simulator the exercise uses, and the references were checked on both.
Quick Check
Q: A register file has two read ports and one write port, each with a 5-bit address. A colleague wants a third read port for a future instruction with three source registers. What has to be added?
Show Answer
A third 5-bit read address and 32-bit read data, and a third 32-to-1 multiplexer connected to every row. The storage and the write side stay as they are. Each read port adds a full set of multiplexers, which is why register files with many ports are expensive.
How It Is Tested
The hidden test drives the ports directly. It writes every register from x1 to x31 with a different value and reads all 32 back on both ports, with the two ports reading different registers at the same time. Then it writes to x0 and checks that x0 still reads as zero, gives a clock edge with we = 0 and checks that nothing changed, and does the cycle 2 read from the table above: a read of x7 during a write to x7 must return the old value, and the new value must appear after the edge. Finally it makes 300 random writes, each followed by a read of the written register and a random one, and compares everything with a model. That is 671 checks.
The test catches each of these mistakes: x0 stored like any other register, the write enable ignored, a write-through path, and the second read port reading ra1 instead of ra2. Failure messages name the port, the register, the expected and the actual value, and the situation, for example "after writing 0xdeadbeef to x0 (x0 must stay zero): read port 1, x0: expected 0x00000000, got 0xdeadbeef".
Where Is This Used?
Register files are among the most carefully optimized structures in any processor. A wide out-of-order core reads and writes many registers per cycle, so its register file can have well over a dozen ports, and its access time can limit the clock frequency. Designers spend real effort on banking it, splitting it, or keeping copies near the units that use them. The two-read, one-write file you build is the starting point of all of those designs.
Common Mistakes
Mistake #1: Storing x0
A 32-entry array written without a check on wa makes x0 writable: addi x0, x0, 1 changes it, and every later instruction that reads x0 gets the wrong value. Either leave x0 out of the storage or block writes to it, and return 0 for reads of address 0 in both cases.
Mistake #2: Reading through the clock
Writing the read ports inside the clocked block delays them by a cycle: rd1 then shows the register that ra1 named in the previous cycle. Reads belong outside the clocked block.
Mistake #3: Adding write-through "to be safe"
Passing wd to a read port during a write looks harmless, and in the single-cycle core it is. But it violates rule 4, the test checks rule 4, and in the pipeline it would hide the hazard that E14 and E15 ask you to handle explicitly.
Your Turn in the Lab
The exercise opens with a visible test, testbench.v, next to your design: a short list of directed checks that prints ok or FAIL for each. Simulate it as you work and add your own checks to it. When you submit, it runs together with the hidden tests, and your edits to it do not change the grading.
Write cvr32_regfile in E03. Before you submit, simulate it in your own project with a few writes and reads, and look at the waveform around a read during a write: the read port should change one cycle after the write, not in the same cycle. Keep the passed version as cvr32_regfile.sv.
Summary
- The register file has two combinational read ports (ra1 to rd1, ra2 to rd2) and one write port that writes wd to register wa at the rising clock edge when we is 1.
- x0 always reads as zero and ignores writes; it does not need to be stored.
- There is no write-through: a read during a write to the same register returns the old value, and the pipeline later adds its own forwarding path for that case.
- Registers are not reset; programs never read a register before writing it, and the simulators show unwritten registers differently.
- It is a large block: 992 of the 1,024 flip-flops of the single-cycle core, plus two 32-to-1 multiplexers 32 bits wide.