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
    • Register Files
    • Valid/Ready Handshake
    • AXI-Lite
    • AXI-Stream
    • Bus Integration
    • Synchronous FIFO
      • Contract and Invariants
      • Pointers, Occupancy and Flags
      • Portable Registered-Output FIFO
      • Simultaneous Push and Pop
      • Standard vs Fall-Through Output
      • Almost Flags and Parameter Edges
      • Verification and Signoff
    • Register-Map and CSR RTL
    • Memory Reliability
    • Peripheral RTL Integration
    • Full AXI4: RTL Roadmap
  • Advanced RTL28
  • Senior & Interview76
  • Glossary and Notation
Loading lesson

‹Synchronous FIFO
  1. Topics
  2. RTL Design
  3. Memory & Buses
  4. Synchronous FIFO
  5. Almost Flags and Parameter Edges

Part 7 · Memory & Buses · Intermediate

Almost Flags and Parameter Edges

Reaction-latency thresholds, non-power-of-two depth, width guards, and configurations that should fail elaboration.

On this page

    Why this matters

    A producer that needs three cycles to react to almost_full will lose data if the margin is two entries. A margin larger than the depth silently underflows an unsigned expression, and the flag never asserts.

    After this lesson you can: derive an almost-full margin from the producer's reaction time, and reject illegal margins at elaboration.

    Almost flags buy reaction time

    full and empty describe hard boundaries. almost_full and almost_empty warn a client early enough for its pipeline or scheduler to react.

    Flag

    Typical equation

    Choose threshold from

    Almost full

    count >= DEPTH - AF_MARGIN

    Maximum writes already in flight after backpressure starts

    Almost empty

    count <= AE_MARGIN

    Reads or bursts already committed before the consumer can stop

    If a producer takes three cycles to observe almost-full and can issue one write per cycle, the margin needs at least three free entries, plus any implementation guard required by the interface.

    For the depth-five telemetry FIFO, a two-entry almost-full margin prevents overflow only if at most two additional writes arrive after the warning. The margin only holds if the producer reacts in time, so an integration test must model the producer delay and in-flight writes: starting at count 3, two trailing writes reach 5 safely; a third is rejected by full and loses data unless the producer retains/retries it.

    A depth-5 FIFO with AF_MARGIN 2 at the moment almost_full rises: three words stored, two free slots for the writes already in flight, and a third late write lost

    Read the row from the left: shaded slots hold data when the warning rises, dashed slots are what the margin keeps free.

    • The warning: with depth 5 and margin 2, almost_full rises when count reaches 3.

    • The margin: two writes already in flight land in slots 4 and 5, and the FIFO is exactly full.

    • The failure: a producer that reacts one cycle too late sends a third write. Full refuses it, and the word is lost unless the producer keeps it.

    design.sv
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    // snippet: threshold flags with guarded parameters
    // Generate-if guards run at elaboration, so lint and synthesis see them too.
    if (DEPTH < 2) begin : g_bad_depth
      $error("DEPTH must be at least 2");
    end
    if (DW < 1) begin : g_bad_dw
      $error("DW must be positive");
    end
    if (AF_MARGIN < 1 || AF_MARGIN > DEPTH) begin : g_bad_af
      $error("AF_MARGIN must be in 1..DEPTH");
    end
    if (AE_MARGIN < 0 || AE_MARGIN >= DEPTH) begin : g_bad_ae
      $error("AE_MARGIN must be in 0..DEPTH-1");
    end
    
    assign almost_full_o  = (count >= DEPTH - AF_MARGIN);
    assign almost_empty_o = (count <= AE_MARGIN);

    Parameter cases worth testing

    Configuration

    Why it matters

    DEPTH = 2

    Smallest useful queue stresses width and wrap expressions.

    DEPTH = 3

    Proves explicit non-power-of-two pointer wrap.

    DEPTH = 16

    Normal power-of-two implementation.

    DW = 1

    Catches zero-width slices and assumptions about byte data.

    Margins at minimum and maximum

    Catches unsigned underflow and off-by-one threshold errors.

    Watermarks versus almost flags

    Software-visible FIFOs often expose programmable high and low watermarks. Those are policy thresholds and may trigger interrupts. Hardware almost flags are usually fixed safety thresholds tied to pipeline reaction latency. Keep the two meanings separate even if both derive from occupancy.

    Common pitfall

    An unsigned expression such as DEPTH - AF_MARGIN silently underflows when the margin is illegal. Reject the configuration at elaboration instead of allowing a flag that can never assert.

    Check yourself A producer has four writes in flight after backpressure is asserted. What minimum free-space warning does it need?

    At least four entries must remain when the warning is acted upon, plus any extra cycle introduced by registering or crossing the backpressure signal.

    Check yourself Why include DEPTH = 3 in the parameter test matrix?

    It proves the pointers wrap explicitly at DEPTH-1. A design that relies on binary overflow will access index 3 before returning to zero.

    Remember

    • The margin must cover the cycles the producer needs to react, plus any register or crossing on the warning.

    • Test depths and margins at the limits: a one-bit width, depths of two and three, and a margin equal to the depth minus one.

    • Programmable watermarks are software policy. Almost flags are fixed hardware safety thresholds. Keep them separate.

    Rule of thumb: derive each threshold from a real reaction latency, never from a habit.

    How this was checked

    The test bench for the depth-five FIFO checks the threshold arithmetic only. The producer's reaction time is not modelled there, so lossless behaviour has to be shown in an integration test with the real producer delay.

    Keep going

    • Practice: FIFO with almost-full and almost-empty flags

    • Code lab: FIFO almost-full flag

    • Code lab: FIFO almost-empty flag

    • Interview: run a mock interview on this topic

    Related topics

    • Verification and signoff

    Previous

    221 / 350

    Next: Verification and Signoff

    Up next

    Up nextVerification and Signoff

    Sign in to save progress

    Practice this lessonReport an error

    See also

    • Contract and Invariants
    • Pointers, Occupancy and Flags
    • Portable Registered-Output FIFO