R10.4.2 Scale and Saturate with Casts

MediumImplementIcarus Verilog + Yosys · serverReview the topic

Multiply a signed sample by an unsigned gain, round down and saturate, with every cast in the right place. The tests check all 4096 input pairs.

Hardware context: A packet filter with a masked address compare, a match counter and an alarm flag; and a gain stage that scales a signed byte by an unsigned gain in quarters and saturates. Design the scale-and-saturate stage with each cast in the right place.

A sensor channel scales each sample by a gain that software sets in quarters, from 0 to 3.75. The sample is a two's complement byte. The gain is unsigned. The result goes back on an 8-bit bus. A value that does not fit must stop at the nearest limit and raise a flag.

Three things meet here. A signed and an unsigned operand go into one multiply. A signed product is divided by 4. And a wide signed result is cut down to a byte. Each step is one cast or one operator, and each has a wrong form that compiles.

This is an Implement problem: write the body of gain_sat. Brief: ports x[7:0] and g[3:0] in, y[7:0] and sat out; combinational. y is x times g, divided by 4 and rounded toward minus infinity, then limited to -128 .. 127. sat is 1 exactly when the limit changed the value. Synthesis check: the design must build on the site's synthesis flow with no latch. The cell and level counts come from an open-source flow and are a proxy for area and depth, not a foundry result.

Signals in the Timeline

SignalWidthRole
pairs96input of each check
gain4input of each check
Full specification
The module (keep the name and the ports)
module gain_sat (
  input  logic [7:0] x,          // a two's complement sample, -128 to 127
  input  logic [3:0] g,          // the gain in quarters: 0 to 15 means 0.00 to 3.75
  output logic [7:0] y,          // x * g / 4, rounded down, limited to -128 .. 127
  output logic       sat         // 1 when the limit changed the result
);
  // your design
endmodule
StepRule
scalep = x * g, with x read as -128 to 127 and g as 0 to 15
divideq = p / 4 rounded toward minus infinity: -1/4 gives -1, and 1/4 gives 0
limity = 127 if q is above 127; y = -128 if q is below -128; else y = q
flagsat = 1 when q was above 127 or below -128, else 0

A pairs case shows y and sat for each of eight pairs. A sweep case holds one gain and applies all 256 samples. It reports how many answers were wrong, the first wrong x and the y given there, and how many inputs saturated.

Examples

Example 1

Gains of One and Less
Input
100 x 4/4, -100 x 4/4, 100 x 2/4, -100 x 2/4, 7 x 1/4, -7 x 1/4, 50 x 0/4, -128 x 4/4
Output
y_0 = 100, sat_0 = no, y_1 = -100, sat_1 = no, y_2 = 50, sat_2 = no, y_3 = -50, sat_3 = no, y_4 = 1, sat_4 = no, y_5 = -2, sat_5 = no, y_6 = 0, sat_6 = no, y_7 = -128, sat_7 = no

A gain of 4 quarters returns the sample. A gain of 1 quarter turns 7 into 1 and -7 into -2, because the divide rounds down.

Example 2

Results Past the Limits
Input
100 x 6/4, -100 x 6/4, 127 x 15/4, -128 x 15/4, 64 x 8/4, -64 x 8/4, -65 x 8/4, 63 x 8/4
Output
y_0 = 127, sat_0 = yes, y_1 = -128, sat_1 = yes, y_2 = 127, sat_2 = yes, y_3 = -128, sat_3 = yes, y_4 = 127, sat_4 = yes, y_5 = -128, sat_5 = no, y_6 = -128, sat_6 = yes, y_7 = 126, sat_7 = no

100 times 1.5 is 150, which stops at 127 with sat = 1. -65 times 2 is -130 and stops at -128. -64 times 2 is exactly -128 and is not a saturation.

Example 3

Rounding Down on Both Sides of Zero
Input
1 x 1/4, -1 x 1/4, 3 x 3/4, -3 x 3/4, 5 x 2/4, -5 x 2/4, 1 x 3/4, -1 x 3/4
Output
y_0 = 0, sat_0 = no, y_1 = -1, sat_1 = no, y_2 = 2, sat_2 = no, y_3 = -3, sat_3 = no, y_4 = 2, sat_4 = no, y_5 = -3, sat_5 = no, y_6 = 0, sat_6 = no, y_7 = -1, sat_7 = no

1 times a quarter is 0, and -1 times a quarter is -1. A divide that rounds toward zero would give 0 for both.

Constraints and Contract

  • Inputs are known 0/1 values. Each input is held one nanosecond, and y and sat are read at its end.
  • Keep the module name and the ports. x arrives on an unsigned port; the brief says how to read it.

Hints

Hint 1

An expression is signed only if every operand is signed. Make both operands of the multiply signed, and check what a sign cast does to a gain of 8 or more.

Hint 2

Work out the range of the product and of the quotient, and give each its own signed variable of enough bits.

Hint 3

Compare the wide quotient with the limits before you cut it down to 8 bits. Write the limits as signed literals of the quotient's width.

What the grader checks: Sixteen sweeps apply every sample with every gain. All 4096 input pairs are compared with an independent model, y and sat alike. Three directed and four seeded pair cases show single results. Correctness is exhaustive over the inputs; the synthesis check adds the latch count.

The submitted design must synthesize with 0 latches and no combinational loop. Counts come from the pinned open-source synthesis flow. They are proxies for logic size and depth, not physical area or timing.

23 checks on Icarus Verilog + Yosys · 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 2 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

Gains of One and Less

Input
100 x 4/4, -100 x 4/4, 100 x 2/4, -100 x 2/4, 7 x 1/4, -7 x 1/4, 50 x 0/4, -128 x 4/4
Expected output
y_0 = 100, sat_0 = no, y_1 = -100, sat_1 = no, y_2 = 50, sat_2 = no, y_3 = -50, sat_3 = no, y_4 = 1, sat_4 = no, y_5 = -2, sat_5 = no, y_6 = 0, sat_6 = no, y_7 = -128, sat_7 = no

A gain of 4 quarters returns the sample. A gain of 1 quarter turns 7 into 1 and -7 into -2, because the divide rounds down.

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

Icarus Verilog + Yosys · server