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.
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.
| Rising edge | Inputs (hex) | Observed after edge (hex) |
|---|---|---|
| E0 | we=1, addr=1, wd=31 | rd=XX |
| E1 | we=1, addr=1, wd=52 | rd=31 |
| E2 | we=0, addr=1, wd=00 | rd=52 |
| E3 | we=1, addr=2, wd=A4 | rd=XX |
| E4 | we=0, addr=2, wd=00 | rd=A4 |
| E5 | we=0, addr=1, wd=00 | rd=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
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)