Codiode

Read First Single Port RAM

MediumVerilog / SystemVerilogBuild

When a RAM reads and writes the same address in one cycle, which data comes out? In read-first mode, the old data does. That is the default for many FPGA block RAMs, and your RTL must match it or simulation and hardware will disagree.

The module computes a parameterized single-port synchronous RAM. On every positive clock edge, the memory evaluates the enable signal. If the enable is high, the memory accesses the provided address. If the write enable is also high, the input data is stored into the memory array at that address. The read data output must always reflect the data present at the address immediately before any write operation on that same clock cycle takes effect. If the enable signal is low, the memory array remains unchanged and the output holds its previous value.

  • Clock edge: posedge
  • Reset: No explicit reset for the memory array.
  • Output value on initialization: The memory array and the output register must be initialized to 0.
  • Priority rules: Enable dictates all activity. If enable is 0, write enable is ignored.

Cycle 1: en=1, we=1, addr=0, din=8'hAA • writes 0xAA, dout updates to 8'h00 (old data)
Cycle 2: en=1, we=0, addr=0, din=8'h00 • reads addr 0, dout updates to 8'hAA
Cycle 3: en=1, we=1, addr=0, din=8'hBB • writes 0xBB, dout updates to 8'hAA (old data)
Cycle 4: en=0, we=1, addr=1, din=8'hCC • no operation, dout holds 8'hAA
Cycle 5: en=1, we=0, addr=1, din=8'h00 • reads addr 1, dout updates to 8'h00

Signal Direction Width Description
clk input 1 Positive-edge triggered clock
en input 1 Memory enable; memory only updates when en is 1
we input 1 Write enable; active high, evaluated only if en is 1
addr input ADDR_WIDTH Memory address to read or write
din input DATA_WIDTH Data to be written into memory
dout output DATA_WIDTH Read data output; registered on clk

Constraints

  • The module must accept DATA_WIDTH (default 8) and ADDR_WIDTH (default 4) as parameters.
  • The memory array must be sized to exactly 2**ADDR_WIDTH elements.
  • Output dout must be updated synchronously on posedge clk.
  • Read-first behavior must be strictly enforced: dout must receive the old data at addr during a write.
  • If en is 0, both read and write operations are ignored and dout strictly holds its previous value.
  • Initial value of all memory locations and dout must be 0.

Topics

MemorySynchronousBlock RAM

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.

This problem is part of Codiode Pro. The statement above is free to read.

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 5 of 26 in the Memory and Storage track.

Related problems

Browse all problems · Verilog practice problems · Learning tracks

Solve this on a laptopSend yourself the link and continue there.