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.

Only the selected R writes on an edge. The read address ra is independent.
Only the selected R writes on an edge. The read address ra is independent.

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.

Recorded simulation. Dashed lines mark rising edges; bus values are hexadecimal. Open the figure to enlarge it.
Recorded simulation. Dashed lines mark rising edges; bus values are hexadecimal. Open the figure to enlarge it.
Rising edgeInputs (hex)Observed after edge (hex)
E0rst_n=0, we=0, wa=0, ra=0, wd=00rd=00
E1rst_n=1, we=1, wa=1, ra=0, wd=52rd=00
E2rst_n=1, we=0, wa=0, ra=1, wd=00rd=52
E3rst_n=1, we=1, wa=3, ra=3, wd=FFrd=00
E4rst_n=1, we=0, wa=0, ra=1, wd=00rd=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

    Read the transcript

    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)

    Check your understanding

    1. How many storage bits hold three byte settings?
    2. Which legal address can be read while writing address 1?
    3. When does rd change after ra changes?
    4. Write FF to wa=3 with r1 holding 52. What is r1 now?
    5. Reset and write enable are both asserted. What does each word contain after the edge?