Random-access memory (RAM)

Set the write edge and read latency, then specify whether a same-cycle read and write at the same address should return old or new data. Check which access patterns infer the target’s memory blocks and retain the tool version, target device and report.

A collector writes its latest sample over the previous one. A reader uses that same address on the very same edge. Will it receive the old byte or its replacement? A RAM interface needs a specific answer.

On a same-address write, rd samples the old word as wd replaces it.
On a same-address write, rd samples the old word as wd replaces it.

The RTL

This single-port RAM has four byte locations and one address bus. Each rising edge samples the addressed memory word for rd. With we high it also schedules a write of wd to that location. Both nonblocking assignments use values from before the updates.

module sample_ram(
    input logic clk,
    input logic we,
    input logic [1:0] addr,
    input logic [7:0] wd,
    output logic [7:0] rd
);
logic [7:0] mem [0:3];
    always_ff @(posedge clk) begin
        rd <= mem[addr];
        if (we) mem[addr] <= wd;
    end
endmodule

Trace the outputs

Write hex 31 to address 1 and overwrite it with 52. After the second write, rd reads 31 although the stored word is now 52. Keep the address and turn off writes. The next edge returns 52.

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)
E0we=1, addr=1, wd=31rd=XX
E1we=1, addr=1, wd=52rd=31
E2we=0, addr=1, wd=00rd=52
E3we=1, addr=2, wd=A4rd=XX
E4we=0, addr=2, wd=00rd=A4
E5we=0, addr=1, wd=00rd=52

Design choices

The array starts uninitialized. A simulated first read from an unwritten location yields X. This example specifies old-data behavior. Check both the read latency and the collision mode when moving to a vendor memory.

Yosys synth_ice40 mapped this module to the following cell counts: SB_DFF: 8, SB_DFFE: 32, SB_LUT4: 20. The download records the tool versions and the commands to reproduce this mapping. Four bytes require 32 storage bits, with a further eight bits in the read register. This mapping used flip-flops and no block RAM.

Interactive lab

At address 1 write 31 and 52 on successive edges. Turn we off for the third edge and compare rd with the stored byte.

rd
stored
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 Random-access memory (RAM) RTL (.zip)

    Check your understanding

    1. What does this RTL simulation show for a first read from an unwritten address?
    2. Address 1 contains 31. Read and write 52 there on the same edge. What is rd?
    3. What is stored at location 1 after that edge?
    4. Change only addr between edges. What happens to rd?
    5. How many bytes fit in four eight-bit locations?