17. Saturating accumulator
Keep an 8-bit unsigned running total that never wraps around.
On each rising clock edge, in priority order: 1. rst = 1 or clear = 1: acc becomes 0 and sat becomes 0. 2. valid = 1 and sub = 0: add din. If the true sum is above 255, acc becomes 255 and sat becomes 1. 3. valid = 1 and sub = 1: subtract din. If the true result is below 0, acc becomes 0 and sat becomes 1. 4. Otherwise nothing changes.
sat is sticky: once set it stays 1 until rst or clear, even after later operations that do not saturate. A result of exactly 255 or exactly 0 is not saturation.
Ports
| Port | Dir | Bits | What it is |
|---|---|---|---|
clk | in | 1 | Clock. |
rst | in | 1 | Synchronous reset, active high. |
clear | in | 1 | Clears acc and sat. |
valid | in | 1 | Apply din on this edge. |
sub | in | 1 | 1 subtracts, 0 adds. |
din | in | 8 | Value to add or subtract. |
acc | out | 8 | The running total. |
sat | out | 1 | Sticky: some operation was clipped. |
Example
acc = 250, add 10: acc becomes 255 and sat becomes 1. acc = 200, add 55: acc becomes 255 and sat stays 0.
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.