29. FIFO with count and almost flags
Build an 8-entry, 8-bit synchronous FIFO that also reports how full it is.
The read and write rules are those of a standard FIFO: rd_data shows the oldest entry whenever the FIFO is not empty; a write (wr_en) adds wr_data at the back unless the FIFO is full; a read (rd_en) removes the front entry unless it is empty; a read and a write in the same cycle each follow their own rule, so when neither flag is set the count stays the same.
Outputs, all from the current state:
- count: the number of entries, 0 to 8.
- full: count is 8. empty: count is 0.
- almost_full: count is 6 or more (two or fewer free slots), so a sender with a two-cycle reaction time can stop in time.
- almost_empty: count is 2 or less.
rst (synchronous, active high) empties the FIFO.
Ports
| Port | Dir | Bits | What it is |
|---|---|---|---|
clk | in | 1 | Clock. |
rst | in | 1 | Synchronous reset, active high. |
wr_en | in | 1 | Write request. |
wr_data | in | 8 | Data to write. |
rd_en | in | 1 | Read request. |
rd_data | out | 8 | The front entry. |
count | out | 4 | Entries stored, 0 to 8. |
full | out | 1 | count is 8. |
empty | out | 1 | count is 0. |
almost_full | out | 1 | count is 6 or more. |
almost_empty | out | 1 | count is 2 or less. |
Example
After six writes: count = 6, almost_full = 1, full = 0. A read and a write in the same cycle leave count at 6.
Written hints
AI hint
Submissions
No submissions yet. Submit your answer and it shows here. Sign in to keep them.
Submit your answer to have it graded. The hidden test runs on the server; you see whether it passes and, if not, the first case it gets wrong.
Press Build to check your code with Yosys. Errors and warnings show here, and a clean build draws the logic diagram.
Build your design to drive it by hand and see its signals here.