Register array
Use an index to select a register for reading or writing, and draw the read mux and write enables separately. With three registers, test legal and out-of-range indices, then document the RTL interface policy and the structure the tool synthesizes.
A panel stores three sensor settings. The operator writes channel 1 while the display reads channel 0. Separate read and write addresses let those choices coexist.
The RTL
Three byte registers share a clock. At a rising edge, we and wa select which register takes wd. A separate mux routes the word at ra to rd. You can change the read selection without writing anything.
module register_bank(
input logic clk,
input logic rst_n,
input logic we,
input logic [1:0] wa,
input logic [1:0] ra,
input logic [7:0] wd,
output logic [7:0] rd
);
logic [7:0] regs [0:2];
always_ff @(posedge clk) begin
if (!rst_n) begin
for (int i=0; i<3; i++) regs[i] <= 8'h00;
end else if (we && wa < 3) begin
regs[wa] <= wd;
end
end
always_comb begin
rd = 8'h00;
if (ra < 3) rd = regs[ra];
end
endmodule
Trace the outputs
Clear the bank with synchronous reset. Write hex 52 to address 1 while reading address 0. The display still shows 00. Switch ra to 1 and the read bus shows 52, even with writes disabled.
| Rising edge | Inputs (hex) | Observed after edge (hex) |
|---|---|---|
| E0 | rst_n=0, we=0, wa=0, ra=0, wd=00 | rd=00 |
| E1 | rst_n=1, we=1, wa=1, ra=0, wd=52 | rd=00 |
| E2 | rst_n=1, we=0, wa=0, ra=1, wd=00 | rd=52 |
| E3 | rst_n=1, we=1, wa=3, ra=3, wd=FF | rd=00 |
| E4 | rst_n=1, we=0, wa=0, ra=1, wd=00 | rd=52 |
Design choices
Two address bits can also encode 3. This interface returns 00 for that read address and ignores writes to it. The last read confirms that a write of FF to the reserved address left channel 1 intact.
Yosys synth_ice40 mapped this module to the following cell counts: SB_DFFESR: 24, SB_LUT4: 22. The download records the tool versions and the commands to reproduce this mapping. The 24 enabled flip-flops hold three byte settings. LUTs implement combinational logic including read selection.
Interactive lab
Reset the bank on an edge. Release reset, enable a write of 52 at wa=1, and apply another edge. Then switch ra and watch the display.
- rd
- r0
- r1
- r2
Show the last eight observations
Teaching film
Run the RTL yourself
Extract the lab and follow the commands in its README. Compare the simulated outputs and waveform with your predictions from the interactive lab.
Download Register array RTL (.zip)