Write First Single Port RAM
When a RAM reads and writes the same address in one cycle, which data comes out? In write-first mode, the new data does.
This module implements a single port synchronous RAM named solution with write first behavior, also known as read through bypass. The memory array contains 16 words, each 8 bits wide. On every clock cycle, the module processes the provided addr. If we is asserted, the input din is written into the memory array and simultaneously forwarded directly to the output dout. If we is deasserted, the output dout updates with the existing data stored at the requested memory address.
- Clock edge:
posedge clk - Reset type: Asynchronous, active low
rst_n - Reset behavior: When
rst_nis 0, the output registerdoutis cleared to 0. The internal memory array contents remain uninitialized and are not cleared by reset. - Write first logic: The output
doutmust reflect the newly written data on the same clock edge thatweis asserted.
Cycle 1: rst_n=0, we=0 → dout=0
Cycle 2: rst_n=1, we=1, addr=5, din=8'hAA → dout=8'hAA (writes 0xAA to mem[5], bypasses to output)
Cycle 3: we=0, addr=5 → dout=8'hAA (reads mem[5])
Cycle 4: we=1, addr=2, din=8'hBB → dout=8'hBB (writes 0xBB to mem[2], bypasses to output)
Cycle 5: we=0, addr=2 → dout=8'hBB (reads mem[2])
Cycle 6: we=0, addr=5 → dout=8'hAA (reads mem[5])
| Signal | Direction | Width | Description |
|---|---|---|---|
clk |
input | 1 | Positive edge triggered clock |
rst_n |
input | 1 | Asynchronous active low reset; clears dout to 0 |
we |
input | 1 | Write enable; when 1, writes din to addr and bypasses to dout |
addr |
input | 4 | Memory address for read and write operations |
din |
input | 8 | Data input to be written into the memory |
dout |
output | 8 | Data output; updates synchronously on clk |
Constraints
- The memory array must be exactly 16 words deep by 8 bits wide.
- The output
doutmust be a registered output that updates strictly onposedge clkor asynchronousrst_n. - The bypass logic must ensure
doutreceivesdinin the same cycleweis high, without requiring a combinational path fromdintodoutthat bypasses the output register. - Do not attempt to reset the entire memory array; only reset the output register
dout.
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.
Free to solve. A Codiode account keeps your progress.
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 4 of 26 in the Memory and Storage track.
Related problems
Browse all problems · Verilog practice problems · Learning tracks