Register File with Hardwired Zero
RISC-V and MIPS reserve register 0 to always read as zero. Writes to it are allowed and simply have no effect, which gives the ISA a free constant and a place to throw results away.
The solution module implements a 32-entry by 32-bit register file with two independent combinational read ports and one synchronous write port. Register 0 (address 5'b00000) is hardwired to zero. Any read from address 0 must immediately return 32'b0. Any write to address 0 must be ignored, preserving the hardwired zero state. Reads from non-zero addresses return the current stored value. Writes to non-zero addresses update the stored value on the next clock edge.
Clocking and reset behave as follows:
- The write operation is synchronous and occurs on the positive edge of
clk. - The reset
rst_nis synchronous and active-low. Whenrst_nis 0 on a positive clock edge, all writable registers (addresses 1 through 31) are cleared to 0. - Reads are purely combinational. The read data outputs
rs1_dataandrs2_datamust immediately reflect the contents of the registers selected byrs1_addrandrs2_addr. - If a read and a write occur to the same non-zero address in the same cycle, the read must return the old stored value, as the write does not commit until the clock edge.
Worked Trace:
Cycle 1: rst_n=0, we=0, rs1_addr=0 → rs1_data=0
Cycle 2: rst_n=1, we=1, rd_addr=5, rd_data=32'hAA, rs1_addr=0 → rs1_data=0
Cycle 3: rst_n=1, we=1, rd_addr=0, rd_data=32'hFF, rs1_addr=5 → rs1_data=32'hAA
Cycle 4: rst_n=1, we=0, rd_addr=0, rd_data=32'h00, rs1_addr=0 → rs1_data=0
| Signal | Direction | Width | Description |
|---|---|---|---|
clk |
input | 1 | Positive-edge triggered clock |
rst_n |
input | 1 | Synchronous active-low reset; clears registers 1 to 31 to 0 |
we |
input | 1 | Write enable; active-high |
rs1_addr |
input | 5 | Read port 1 address |
rs2_addr |
input | 5 | Read port 2 address |
rd_addr |
input | 5 | Write port address |
rd_data |
input | 32 | Data to be written to rd_addr |
rs1_data |
output | 32 | Read port 1 data (combinational) |
rs2_data |
output | 32 | Read port 2 data (combinational) |
Constraints
- The clock edge is
posedge clk. - The reset is synchronous and active-low.
- When
rst_nis asserted (0), all registers from address 1 to 31 must be cleared to 0 on the clock edge. - Address 0 is hardwired to 0. A read from address 0 must combinationally return 0 regardless of what was previously written to it.
- A write to address 0 must not alter the state of any register.
- Reads are purely combinational; they do not wait for a clock edge.
- Writes are synchronous; they only update the register array on the clock edge.
- If
rst_nandweare both active in the same cycle, the reset takes priority and the write is ignored.
Topics
Solve this problem
Write the module in Verilog, SystemVerilog or VHDL. Your submission is compiled and simulated against the problem's testbench. You get the waveform back, not a stored answer.
This problem is part of Codiode Pro. The statement above is free to read.
The circuit builder and code editor need a desktop screen. On a phone, read the problem here and open it on a laptop to solve.
Part of Memory and Storage
Problem 10 of 26 in the Memory and Storage track.
Related problems
Browse all problems · Verilog practice problems · Learning tracks