Problems

17. Saturating accumulator

MediumWrite the moduleAbout 20 min
sequentialarithmeticsaturationoverflow

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

Module sat_acc

PortDirBitsWhat it is
clkin1Clock.
rstin1Synchronous reset, active high.
clearin1Clears acc and sat.
validin1Apply din on this edge.
subin11 subtracts, 0 adds.
dinin8Value to add or subtract.
accout8The running total.
satout1Sticky: 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.

Stuck? . Sign in to save your progress. You can solve the free problems without an account.

Verilogsat_acc.v
14px
Loading the editor…

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.

Saturating accumulator: Verilog interview question | breadboardhub