Add a signed change to an unsigned level and hold the result inside 0 to 255 at both ends.
Hardware context: The arithmetic block of a small processor and the brightness adder of a lamp dimmer. Add a signed change to an unsigned level and saturate at 0 and at 255.
A lamp dimmer keeps its brightness as an unsigned byte. A rotary knob sends a signed change. With a plain adder, one click up at full brightness turns the lamp off. One click down at zero turns it fully on.
This is an Implement problem. Write level_adjust. The output y is the level plus the change, held inside 0 to 255. A sum above 255 gives 255. A sum below 0 gives 0.
The flag clipped is 1 when the true sum was outside 0 to 255, and 0 when y is the true sum.
The two inputs have different types. The level is unsigned and the change is signed. The check applies eight pairs, one per nanosecond, and records y and clipped after each.
Signals in the Timeline
| Signal | Width | Role |
|---|---|---|
script | 128 | input of each check |
Full specification
module level_adjust (
input logic [7:0] level, // the present level: 0 to 255, unsigned
input logic signed [7:0] delta, // the change: -128 to 127, two's complement
output logic [7:0] y, // level + delta, held inside 0 to 255
output logic clipped // 1 when the true sum was outside 0 to 255
);
// your design
endmodule
| True sum of level and delta | y | clipped |
|---|---|---|
| Above 255 | 255 | 1 |
| From 0 to 255 | The sum | 0 |
| Below 0 | 0 | 1 |
The result fields are named y_step0 and clipped_step0 for the first step, up to y_step7 and clipped_step7.
Examples
Example 1
Changes That Fit- Input
100 + 20; 100 - 20; 0 + 127; 255 - 128; 128 + 127; 128 - 128; 1 - 1; 254 + 1
- Output
y_step0 = 120, clipped_step0 = no, y_step1 = 80, clipped_step1 = no, y_step2 = 127, clipped_step2 = no, y_step3 = 127, clipped_step3 = no, y_step4 = 255, clipped_step4 = no, y_step5 = 0, clipped_step5 = no, y_step6 = 0, clipped_step6 = no, y_step7 = 255, clipped_step7 = no
Every sum is inside 0 to 255, so y is the plain sum and clipped is 0. The steps include the largest changes that still fit: 0 + 127, 255 - 128 and 128 + 127.
Example 2
Past the Top- Input
250 + 10; 255 + 1; 200 + 100; 129 + 127; 255 + 127; 128 + 127; 255 + 0; 250 + 5
- Output
y_step0 = 255, clipped_step0 = yes, y_step1 = 255, clipped_step1 = yes, y_step2 = 255, clipped_step2 = yes, y_step3 = 255, clipped_step3 = yes, y_step4 = 255, clipped_step4 = yes, y_step5 = 255, clipped_step5 = no, y_step6 = 255, clipped_step6 = no, y_step7 = 255, clipped_step7 = no
250 + 10 would be 260, so y is 255 and clipped is 1. A sum of exactly 255, as in 255 + 0 and 250 + 5, is not clipped.
Example 3
Past the Bottom- Input
5 - 10; 0 - 1; 100 - 128; 127 - 128; 0 - 128; 128 - 128; 0 + 0; 5 - 5
- Output
y_step0 = 0, clipped_step0 = yes, y_step1 = 0, clipped_step1 = yes, y_step2 = 0, clipped_step2 = yes, y_step3 = 0, clipped_step3 = yes, y_step4 = 0, clipped_step4 = yes, y_step5 = 0, clipped_step5 = no, y_step6 = 0, clipped_step6 = no, y_step7 = 0, clipped_step7 = no
5 - 10 would be -5, so y is 0 and clipped is 1. A sum of exactly 0, as in 5 - 5, is not clipped.
Constraints and Contract
- Inputs are known 0/1 values. Each pair is held for one nanosecond and the outputs are sampled at its end.
- Keep the module name and the ports, including their signed and unsigned declarations. The design is combinational.
Hints
Hint 1
Work out the smallest and the largest true sum. Then choose a signed width that holds both.
Hint 2
An expression is signed only when every operand is signed. The level is not. Give it extra 0 bits in front and read that wider value as signed.
Hint 3
Form the wide sum first. Then compare it with 255 and with 0, and pick one of three values for y.
What the grader checks: Six directed and eight seeded cases of eight steps each run on the block. Both outputs are compared with a model written in JavaScript after every step.
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