Skip to content
DenseChip
Learn
PracticeCode labInterview

DenseChip

Navigate

Menu

LearnPracticeCode labInterview

Tools

All toolsSV editorCode repoResume
DenseChip

Practice, courses, and chip-design tools in one focused workspace.

LearnPracticeCode labInterviewToolsLearning paths

© 2026 DenseChip

AboutContactPrivacyTerms
Skip to lesson

Course contents

  • RTL Foundations36
  • Combinational Design25
  • Sequential Design27
  • FSM & Control27
  • Timing & Clocking39
  • Reset & CDC34
  • Memory & Buses57
    • SRAM and ROM
      • Inference Starts from Coding Style
      • Single-Port Synchronous RAM Template
      • Read-First vs Write-First
      • Dual-Port RAM Patterns
      • True Dual-Port RAM and Collisions
      • ROM Inference
    • Register Files
    • Valid/Ready Handshake
    • AXI-Lite
    • AXI-Stream
    • Bus Integration
    • Synchronous FIFO
    • Register-Map and CSR RTL
    • Memory Reliability
    • Peripheral RTL Integration
    • Full AXI4: RTL Roadmap
  • Advanced RTL28
  • Senior & Interview76
  • Glossary and Notation
Loading lesson

‹SRAM and ROM
  1. Topics
  2. RTL Design
  3. Memory & Buses
  4. SRAM and ROM
  5. Read-First vs Write-First

Part 7 · Memory & Buses · Intermediate

Read-First vs Write-First

Read-first, write-first and no-change behaviour when one cycle reads and writes the same address.

On this page

    Why this matters

    When a read and a write hit the same address in one cycle, the output depends on the RAM's mode. If the RTL asks for a mode the block lacks, the tool adds a comparator and bypass mux around the RAM, which costs logic and delay.

    After this lesson you can: compare read-first, write-first and no-change, and pick the mode a design needs before checking that the target supports it.

    Three modes

    A single-port RAM reads and writes through one address. When both happen in the same cycle, the output depends on the mode.

    Mode

    Output when you write address A in this cycle

    Block RAM support

    Read-first

    The old contents of A (read, then write)

    Supported on most FPGA block RAMs; for ASIC SRAM macros check the datasheet (single-port macros often hold Q or write-through during a write)

    Write-first (transparent)

    The new data being written

    Supported on many FPGA block RAMs

    No-change

    Unchanged: the previous output holds

    Supported; lowest power on some devices

    Behaviour is part of the RAM's contract. If the RTL describes write-first and the target block does not provide it, the tool adds a bypass mux and comparator around the RAM (extra logic and delay) to emulate it.

    If the RTL describes read-first and the block provides it natively, nothing extra is needed.

    Choosing a mode

    Tip

    Choose the mode that the design needs, then check that the target library supports it. In pipelines that need to see their own recent writes, write-first or an explicit bypass is the usual answer; for a plain table, read-first is simplest and cheapest.

    Common pitfall

    Between two different clocks a dual-port read/write collision can return old, new, or invalid data; prevent same-address accesses within the device's collision window. Even on a common clock, opposite-port reads require a supported collision mode: the single-port mode names alone do not guarantee that output.

    Check yourself A design needs a read of address A in the cycle right after it was written to see the new data. Which mode, if any, is required?

    None: any mode returns the new data on a read issued after the write (step 4 in the table below). Only a read in the same cycle as the write needs write-first behaviour or a bypass.

    Testbench

    All three modes plus a testbench that writes and reads the same word in each mode.

    ram_modes.sv
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    // Single-port RAM with the three classic read-during-write behaviours.
    //   READ_FIRST  : dout gets the OLD word, then the write happens
    //   WRITE_FIRST : dout gets the NEW data (the write data passes straight through)
    //   NO_CHANGE   : dout holds its previous value while writing
    module ram_modes #(parameter int MODE = 0, parameter int AW = 4, parameter int DW = 8) (
      input  logic          clk, we,
      input  logic [AW-1:0] addr,
      input  logic [DW-1:0] din,
      output logic [DW-1:0] dout
    );
      localparam int READ_FIRST = 0, WRITE_FIRST = 1, NO_CHANGE = 2;
      logic [DW-1:0] mem [2**AW];
    
      always_ff @(posedge clk) begin
        if (we) begin
          mem[addr] <= din;
          unique case (MODE)
            READ_FIRST:  dout <= mem[addr];       // old contents
            WRITE_FIRST: dout <= din;             // new contents
            NO_CHANGE:   ;                         // dout keeps its value
            default:     dout <= 'x;
          endcase
        end else begin
          dout <= mem[addr];
        end
      end
    endmodule
    

    Each slot is one clock cycle. A request offered in one slot lands at the edge ending that slot, so its effect shows in the outputs of the next slot.

    • Slots 0 and 1 agree at 0x22 in every mode: the read, then the same-address write still in flight.

    • Slot 2 is the one slot where the modes differ. Read-first still shows the old word 0x22, write-first shows the new word 0x11, and no-change holds 0x22.

    • Slots 3 to 5 agree at 0x11 again: any mode returns the new data on a read issued after the write.

    Step

    Operation

    Read-first output

    Write-first output

    No-change output

    1

    Write 0x22 to address 3

    –

    –

    –

    2

    Read address 3

    0x22

    0x22

    0x22

    3

    Write 0x11 to address 3 (same word)

    0x22 (old)

    0x11 (new)

    0x22 (kept)

    4

    Read address 3 again

    0x11

    0x11

    0x11

    5

    Write 0x77 to address 5

    (old word of 5)

    0x77

    0x11 (kept)

    Remember

    • The modes differ in one slot only: a read in the same cycle, at the same address, as the write.

    • Any mode returns the new data for a read issued after the write.

    • The mode is part of the RAM's contract. An unsupported mode costs a bypass mux and comparator.

    Rule of thumb: choose the mode the design needs first, then check the target provides it natively.

    Keep going

    • Practice: Single-port RAM with write-first behaviour

    • Practice: Dual-port RAM with read-after-write handling

    • Interview: run a mock interview on this topic

    Related topics

    • Dual-port RAM patterns

    Previous

    191 / 350

    Next: Dual-Port RAM Patterns

    Up next

    Up nextDual-Port RAM Patterns

    Sign in to save progress

    Practice this lessonReport an error

    See also

    • Inference Starts from Coding Style
    • True Dual-Port RAM and Collisions
    • ROM Inference
    weaddr3din2211d_rf2211d_wf2211d_nc2211
    Steps 2 to 4 of the table below: a read of 0x22, a same-address write of 0x11, then a read again, with only the slot after the write differing