H1.3 Almost-Full Thresholds

EasyFixVerilator · serverReview the topic

Fix the almost-full flag of a FIFO wrapper so that its programmable threshold includes the boundary.

Hardware context: Clocked FIFO plus ready/valid output adapter. Verify programmable occupancy threshold flags at their exact boundaries.

A producer slows down when its FIFO is almost full. Here almost_full rose one entry too late, and a burst overflowed the FIFO. The bug is at the boundary.

The module threshold_fifo wraps a trusted FIFO of 8-bit entries with DEPTH 1, 3 or 5. It counts the stored entries itself. It drives two extra flags from two threshold inputs, af_level and ae_level. Fix the module so that both flags follow the rules.

almost_full is 1 when the count is at least af_level. almost_empty is 1 when the count is at most ae_level. Both include equality, and both may be 1 at once. Neither flag blocks traffic; only full and empty do.

The tests change the thresholds while the clock is low and check both flags 1 ns later, before any edge. They check again just after every edge, for 32 edges.

Signals in the Timeline

SignalWidthRole
profile3input of each check
controls96input of each check
values0256input of each check
values1256input of each check
values2256input of each check
values3256input of each check
af_levels128input of each check
ae_levels128input of each check
Full specification
The ports (keep them)
module threshold_fifo #(parameter int DEPTH = 3) (
  input  bit        clk, initialize, reset, write_en, read_en,
  input  bit [31:0] write_data,
  input  int        af_level, ae_level,
  output wire       full, empty, read_valid,
  output wire [31:0] read_data,
  output wire       almost_full, almost_empty);
  // your wrapper around hardware_fifo
endmodule
Flag ruleMeaning
almost_fullcount >= af_level
almost_emptycount <= ae_level
ThresholdsEach is 0 to DEPTH. They may cross or be equal.
After resetThe count is 0, so almost_full = (af_level == 0) and almost_empty = 1.
TimingBoth flags are combinational. A threshold change shows before the next edge, with no request and no clock.
RuleWhat a correct FIFO does
WriteAccepted when reset = 0, write_en = 1 and fewer than DEPTH entries are stored before the edge. write_data joins the back.
ReadAccepted when reset = 0, read_en = 1 and at least one entry is stored before the edge. The oldest entry leaves.
Both on one edgeBoth decisions use the count before the edge. When empty, only the write is accepted. When full, only the read is accepted. Otherwise the read takes the oldest entry and the write joins the back.
ResponseThe edge that accepts a read loads the response registers. Right after that edge, read_valid = 1 and read_data holds the entry. This is true even when the read leaves the FIFO empty.
No readAfter an edge that accepts no read, read_valid = 0. read_data may then hold any old value.
Resetreset is synchronous, active high, and wins over both requests. It empties the FIFO and clears read_valid and read_data to 0.
Flagsfull = 1 exactly when DEPTH entries are stored. empty = 1 exactly when none are. Both follow the count with no delay.

Count only accepted requests. A write and a read accepted on the same edge leave the count unchanged.

error_edges has bit i set when either flag was wrong before or after edge i. pre_hits records which boundaries the test reached before an edge, and post_hits the same after an edge. In both, bit 0 means count = 0 and bit 1 means count = DEPTH. Bit 2 means count = af_level and bit 3 means count = ae_level.

The hits, counts and depth come from the test bench, not from your module. They show what each test exercised.

Examples

Example 1

Both Flags at a Shared Threshold
Input
Depth 3, af_level = ae_level = 1: write 0x11, write 0x22, read, then 29 idle edges
Output
error_edges = {}, pre_hits = {0, 2, 3}, post_hits = {2, 3}, accepted_writes = 2, accepted_reads = 1, final_count = 1, depth = 3

After the first write the count is 1, so both flags are 1. After the second write only almost_full is 1. The read brings the count back to 1, and both flags are 1 again.

Example 2

Thresholds Change While the FIFO Is Idle
Input
Depth 1, 32 idle edges; af_level alternates 0 and 1; ae_level stays 0
Output
error_edges = {}, pre_hits = {0, 2, 3}, post_hits = {0, 2, 3}, accepted_writes = 0, accepted_reads = 0, final_count = 0, depth = 1

The count stays 0, so almost_empty stays 1. almost_full is 1 while af_level is 0, and 0 while it is 1. A flag that updates only on an edge fails the checks before the edge.

Example 3

Extreme Thresholds Stay on Through Reset
Input
Depth 5, af_level = 0, ae_level = 5: write 0x01, write 0x02, reset with write+read 0x03, then 29 idle edges
Output
error_edges = {}, pre_hits = {0, 2}, post_hits = {0, 2}, accepted_writes = 2, accepted_reads = 0, final_count = 0, depth = 5

Every legal count is at least 0 and at most 5, so both flags are always 1. Reset empties the FIFO but does not clear either flag.

Constraints and Contract

  • af_level and ae_level are always 0 to DEPTH. The tests never drive x or z.
  • Both flags at 1 together is a legal result, not a bad setting.
  • Keep the module name, the parameter and the ports.

Hints

Hint 1

Write the flags as continuous assignments from the count. Do not register them.

Hint 2

Count only accepted requests. The full and empty outputs of the core tell you which requests are accepted.

Hint 3

Check equality. With af_level = 2 and two entries stored, is almost_full 1 in your code?

What the grader checks: The wrapper runs around a trusted FIFO at depths 1, 3 and 5. The tests check both flags after each threshold change and after each edge. They do not cover thresholds written from another clock domain.

57 checks on Verilator · server. Run the examples first, then check the full regression. Full checks use the server judge. Downloads contain public samples only. A passing result establishes the tested cases, not a proof for all possible inputs.

See all 9 steps and the final capstone

Report a problem with this page

Your code SystemVerilogSaved locally
Ln 1, Col 1Tab indents · Ctrl/⌘ + Enter runs samples
Not run yet

Both Flags at a Shared Threshold

Input
Depth 3, af_level = ae_level = 1: write 0x11, write 0x22, read, then 29 idle edges
Expected output
error_edges = {}, pre_hits = {0, 2, 3}, post_hits = {2, 3}, accepted_writes = 2, accepted_reads = 1, final_count = 1, depth = 3

After the first write the count is 1, so both flags are 1. After the second write only almost_full is 1. The read brings the count back to 1, and both flags are 1 again.

Run samples executes all 3 public checks. Check all tests runs 57 checks on the server.

Verilator · server