Part of Curriculum: Memory and FIFO Design (Module 6: Programmable Watermarks (Almost-Full / Almost-Empty))
View Full Roadmap →
Level 3 Hard ★ Flagship #37 50 pts ✓ Solved

FIFO Almost-Full Logic — Threshold Calculation

Queues & Buffers · cdc, industry-rtl, arbiters, clock-domain-crossing, hard, fifo-thresholds

Generate an almost_full flag for a 16-entry FIFO that asserts whenever occupancy is greater than or equal to ALMOST_FULL_THRESHOLD (default 12).

Write a synthesizable Verilog module named top_module matching the ports below exactly.
📐 Circuit Schematic Diagram
wr_validrd_validoccupancy[4:0]almost_fullfull>clkrst_nFIFO ALMOST-FULL LOGDIGITAL HARDWARE BLOCK
⚡ Timing & Synthesizability Constraints

Timing Contract: Flag evaluates synchronously on `posedge clk`. Asserts when `occupancy >= ALMOST_FULL_THRESHOLD`.

RTL Constraints: FIFO capacity = 16. Threshold parameter `ALMOST_FULL_THRESHOLD = 12`.

Objective: Understand FIFO watermarking, Skid margins, and early backpressure signaling to compensate for pipeline latency.

Prerequisites: FIFO occupancy counters, threshold comparison.
NameDirWidthDescription
clkinput1Clock signal
areset_ninput1Active-low reset
wr_validinput1Valid write beat committed
rd_validinput1Valid read beat committed
occupancyoutput5Current word occupancy (0 to 16)
almost_fulloutput1Asserted when occupancy >= 12
fulloutput1Asserted when occupancy == 16
Expected Waveform (Golden Timing Contract)
💡 Need Guidance? Progressive Hints (3 Levels)
Hint 1: Conceptual Direction & Hardware Primitives
Maintain a 5-bit occupancy register (needs to hold values from 0 up to 16, requiring 5 bits).
Hint 2: Implementation Architecture & Approach
Update occupancy: `+1` on write, `-1` on read, no change on simultaneous read/write.
Hint 3: Edge Cases, Timing & Verilog Syntax
Compare `assign almost_full = (occupancy >= ALMOST_FULL_THRESHOLD);`.
🔒 Reveal Verified Reference RTL Solution (Click to expand)
⚠️ Self-Discovery Warning: Try solving and simulating first! Viewing the verified reference implementation will bypass the real debugging experience.
Verified Synthesizable RTL
module top_module #(
  parameter ALMOST_FULL_THRESHOLD = 12
)(
  input        clk,
  input        areset_n,
  input        wr_valid,
  input        rd_valid,
  output reg [4:0] occupancy,
  output       almost_full,
  output       full
);
  always @(posedge clk or negedge areset_n) begin
    if (!areset_n) begin
      occupancy <= 5'd0;
    end else begin
      case ({wr_valid, rd_valid})
        2'b10: occupancy <= occupancy + 1'b1;
        2'b01: occupancy <= occupancy - 1'b1;
        default: occupancy <= occupancy;
      endcase
    end
  end

  assign full        = (occupancy == 5'd16);
  assign almost_full = (occupancy >= ALMOST_FULL_THRESHOLD);
endmodule
Verilog-2001 / SV Saved
Font: |
// Click Run Tests (or press Ctrl+Enter) to compile your RTL and simulate against hidden testbenches.
Simulation Timing Diagram (VCD Waveform)
100%
No simulation waveform yet. Run tests to simulate and generate VCD traces.
No compilation run yet. Syntax warnings and elaboration messages will appear here.
No simulation output yet. Raw stdout from vvp will appear here.

RTL Problems / Level 3 · Queues & Buffers

What You Will Learn

By solving FIFO Almost-Full Logic — Threshold Calculation, you will acquire core industry skills:

  • Synthesizable hardware modeling in standard Verilog-2001 / SystemVerilog.
  • Correct handling of interface port contracts, edge-triggered clocks, and active-low/high resets.
  • Simulation waveform interpretation and debugging using timing diagrams (VCD traces).
  • Architectural avoidance of timing violations, glitching hazards, and synthesis warnings.

Problem Statement & Requirements

Generate an almost_full flag for a 16-entry FIFO that asserts whenever occupancy is greater than or equal to ALMOST_FULL_THRESHOLD (default 12).

Hardware design teams at leading semiconductor firms evaluate these exact port contracts, race-condition assertions, and corner cases during technical design reviews and ASIC engineer interviews.

Objective: Understand FIFO watermarking, Skid margins, and early backpressure signaling to compensate for pipeline latency.

Prerequisites: FIFO occupancy counters, threshold comparison.

Port List & Interface Timing

The hidden testbench instantiates top_module. Module port names and bit-widths must match the interface contract exactly:

NameDirWidthDescription
clkinput1Clock signal
areset_ninput1Active-low reset
wr_validinput1Valid write beat committed
rd_validinput1Valid read beat committed
occupancyoutput5Current word occupancy (0 to 16)
almost_fulloutput1Asserted when occupancy >= 12
fulloutput1Asserted when occupancy == 16

Timing Constraints & Synthesizable RTL Rules

⚡ Synthesizable Implementation Guidelines

Timing Contract: Flag evaluates synchronously on `posedge clk`. Asserts when `occupancy >= ALMOST_FULL_THRESHOLD`.

Synthesis Constraints: FIFO capacity = 16. Threshold parameter `ALMOST_FULL_THRESHOLD = 12`.

Architectural Deep Dive & Implementation Walkthrough

In high-throughput interconnects (Ethernet MACs, PCIe controllers), stopping an upstream transmitter takes several clock cycles of round-trip pipeline latency. If flow control waited until the FIFO was 100% full before asserting backpressure, packets in flight would overflow and be dropped. The `almost_full` watermark signals backpressure with enough head-room margin (the skid depth) to safely absorb in-flight packets.

Common RTL Designer Pitfalls

⚠️ Common Mistakes to Avoid

['Sizing `occupancy` to only 4 bits for a 16-entry FIFO: 4 bits can only count from 0 to 15, causing overflow at 16.', 'Using strict equality `occupancy == 12` instead of `>= 12`.']

Silicon & Hardware Engineering Notes

🛠️ ASIC vs FPGA Hardware Considerations

Configurable almost-full thresholds are standard features of hard FIFO macros in AMD/Xilinx UltraScale+ and Intel Agilex FPGAs.

Starter RTL Code

module top_module #(
  parameter ALMOST_FULL_THRESHOLD = 12
)(
  input        clk,
  input        areset_n,
  input        wr_valid,
  input        rd_valid,
  output reg [4:0] occupancy,
  output       almost_full,
  output       full
);

  // FIFO watermark tracking and almost_full threshold logic

endmodule

Verified Reference Solution

🔒 Reveal Verified Reference RTL Solution
module top_module #(
  parameter ALMOST_FULL_THRESHOLD = 12
)(
  input        clk,
  input        areset_n,
  input        wr_valid,
  input        rd_valid,
  output reg [4:0] occupancy,
  output       almost_full,
  output       full
);
  always @(posedge clk or negedge areset_n) begin
    if (!areset_n) begin
      occupancy <= 5'd0;
    end else begin
      case ({wr_valid, rd_valid})
        2'b10: occupancy <= occupancy + 1'b1;
        2'b01: occupancy <= occupancy - 1'b1;
        default: occupancy <= occupancy;
      endcase
    end
  end

  assign full        = (occupancy == 5'd16);
  assign almost_full = (occupancy >= ALMOST_FULL_THRESHOLD);
endmodule

Related RTL Problems in Level 3

Frequently Asked Questions

What does FIFO Almost-Full Logic — Threshold Calculation require?

Generate an almost_full flag for a 16-entry FIFO that asserts whenever occupancy is greater than or equal to ALMOST_FULL_THRESHOLD (default 12). Implement it as Verilog module top_module matching the listed ports.

Is this design combinational or sequential?

Tags: cdc, industry-rtl, arbiters, clock-domain-crossing, hard, fifo-thresholds. If a clock signal is present in the port table, implement it as sequential logic; otherwise, use continuous assignments or combinational blocks.

How is my solution graded?

A hidden SystemVerilog testbench simulates your module in the cloud judge. You pass when all directed corner cases match the golden model and the VCD timing waveform matches the contract.

How does this challenge help with semiconductor interviews?

Technical interviewers at NVIDIA, Qualcomm, Apple, and Intel routinely ask candidates to code, debug, and explain the timing waveforms of FIFO Almost-Full Logic — Threshold Calculation to assess real-world synthesizability and race-condition awareness.