Fix a fixed-point gain stage that keeps the wrong bits of its product and never saturates.
Hardware context: A fixed-point gain stage of an audio mixer and the display rounding of a digital scale. Repair the gain stage that keeps the low byte of a Q8.8 product and wraps when it should saturate.
An audio mixer scales each sample by a gain. The sample and the result are signed Q4.4 numbers, and the gain is an unsigned Q4.4 number. On the bench the output is noise, even at a gain of 1.0.
This is a Fix problem. The multiplier in the block is right. The block then takes the wrong bits out of the product. Find the bits that hold a Q4.4 result.
There is a second fault. A product can be larger than Q4.4 can hold. The result must then stop at the nearest end of the range: 7.9375 at the top and -8.0 at the bottom.
The check applies ten pairs of sample and gain, one per nanosecond, and records the output after each.
Signals in the Timeline
| Signal | Width | Role |
|---|---|---|
script | 160 | input of each check |
Full specification
module mix_gain (
input logic signed [7:0] sample, // signed Q4.4: -8.0 to 7.9375
input logic [7:0] gain, // unsigned Q4.4: 0.0 to 15.9375
output logic signed [7:0] out // signed Q4.4: sample x gain, saturated
);
// the logic to repair
endmodule
| Step | Rule |
|---|---|
| Multiply | The product of the two stored integers has eight fraction bits. |
| Drop bits | Drop the four lowest fraction bits. Do not round: a dropped part always makes the result smaller. |
| Saturate | A result above 7.9375 becomes 7.9375 (stored 127). A result below -8.0 becomes -8.0 (stored -128). |
The result fields are named out_step0 to out_step9. Each shows the stored integer of the output as a signed number: the value times 16.
Examples
Example 1
Small Samples, Small Gains- Input
2.5 x 1.5; 1 x 1; 0.5 x 0.5; 3 x 2; 1.25 x 4; 0 x 15; 7.9375 x 1; 2 x 0; 0.0625 x 1; 4 x 1.5
- Output
out_step0 = 60, out_step1 = 16, out_step2 = 4, out_step3 = 96, out_step4 = 80, out_step5 = 0, out_step6 = 127, out_step7 = 0, out_step8 = 1, out_step9 = 96
2.5 times 1.5 is 3.75, stored as 60. A gain of 1.0 must return the sample unchanged, and a gain of 0 must return 0.
Example 2
Negative Samples That Fit- Input
-2.5 x 1.5; -1 x 1; -0.5 x 0.5; -3 x 2; -1.25 x 4; -8 x 1; -4 x 2; -0.0625 x 1; -7.9375 x 1; -2 x 3.5
- Output
out_step0 = -60, out_step1 = -16, out_step2 = -4, out_step3 = -96, out_step4 = -80, out_step5 = -128, out_step6 = -128, out_step7 = -1, out_step8 = -127, out_step9 = -112
-2.5 times 1.5 is -3.75, stored as -60. The sample -8.0 at a gain of 1.0 is the most negative result that fits.
Example 3
Results Above the Top- Input
4 x 2; 7.9375 x 2; 1 x 8; 0.5 x 15.9375; 7.9375 x 15.9375; 3 x 3; 2 x 4; 5.5 x 1.5; 1 x 7.9375; 1 x 8.0625
- Output
out_step0 = 127, out_step1 = 127, out_step2 = 127, out_step3 = 127, out_step4 = 127, out_step5 = 127, out_step6 = 127, out_step7 = 127, out_step8 = 127, out_step9 = 127
4 times 2 is 8.0, one step above the largest Q4.4 value. The output stops at 7.9375, stored as 127. The broken block wraps round instead.
Constraints and Contract
- Inputs are known 0/1 values. Each pair is held for one nanosecond and out is sampled at its end.
- Keep the module name and the ports, including their signed and unsigned declarations.
Hints
Hint 1
Count the fraction bits. Each input has four, so the product has eight. The output has four again. Which bit of the product has the weight 1/16?
Hint 2
After you drop four bits, the value still has more integer bits than the output. Keep them all in a wider signed signal first.
Hint 3
Compare that wide value with the largest and the smallest stored integer of a signed byte. Pick one of three values for the output.
What the grader checks: Six directed and eight seeded cases of ten steps each run on the block. Every output is compared with a model written in JavaScript. Rounding modes other than dropping the low bits are outside this problem.
14 checks on Icarus · browser samples / server checks. 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