In the previous lesson, a selector chose which sensor reading to display. Now the panel needs to add two readings. Each sensor provides four bits and can report up to 15. When one reports 15 and the other reports 1, the panel should display 16. A four-bit wrapping sequence number goes from 0 to 15 and then returns to 0. For that field, 15 + 1 should deliver 0. The purpose of each field determines where the carry goes.
How can one addition have two correct results?
Work through 1111 + 0001 from the right. The low position adds 1 + 1, keeps 0, and sends a carry to the next bit. The next three positions each add that carry to another 1 and keep 0. The final carry reaches bit 4, giving 10000. A modulo-16 port keeps the low four bits and delivers 0000. The panel needs the exact result, so its port has five bits.
assign full_sum_o = {1'b0, a_i} + {1'b0, b_i};
assign modulo_sum_o = full_sum_o[3:0];
The first line attaches a zero in front of each input, giving two five-bit operands. Five bits can hold the maximum sum of 30. The second line explicitly takes bits 3 through 0 of the completed sum, implementing the wrap at 16.
How can a five-bit destination still lose the carry?
Consider a version that puts one set of braces around the entire addition.
assign full_sum_o = {a_i + b_i};
These braces form a concatenation. It puts the bits of its fields next to one another. This one has a single field, a_i + b_i, whose width is determined inside the braces. The operands are four bits each, so the addition there produces four bits. For 15 + 1, that field already contains 0000. The outer assignment receives that four-bit unsigned field and pads it with zero, producing 00000. Making the destination wider leaves the loss inside the braces. In the direct assignment assign full_sum_o = a_i + b_i;, the five-bit destination sizes the addition and preserves the carry. Explicit padding in the original version makes the minimum width visible at the operands.
Further detail: where does the width come from?
IEEE 1800 distinguishes self-determined widths from widths that can depend on an expression's context. Each field inside a concatenation determines its own width, so the five-bit destination cannot change the addition inside these braces. Some expressions in direct assignments use the destination as sizing context. A full unsigned product of m-bit and n-bit operands can require m+n bits. Provide that width and check that the expression evaluates at the required precision. If the receiving interface is narrower, document whether it keeps low bits, rounds, or saturates.
Set widths and signedness before applying an offset
Next, the panel applies a calibration offset. The unsigned four-bit reading is 0 to 15. The four-bit two’s-complement offset is −8 to 7. Their sum ranges from −8 to 22. The upper end exceeds the signed five-bit maximum of 15, so the adjusted field needs six bits.
Let the reading be 2 and the offset be −3. The offset starts as 1101. Copy its sign bit into both new high positions, giving 111101. Pad the unsigned reading 0010 with two zeros, giving 000010. Interpret both extended patterns as six-bit signed numbers. Their sum is 111111, or −1, so the below-zero flag is 1.
output logic signed [5:0] adjusted_o;
logic signed [5:0] sample_ext, offset_ext;
assign sample_ext = $signed({2'b00, sample_i});
assign offset_ext = $signed({{2{offset_i[3]}}, offset_i});
assign adjusted_o = sample_ext + offset_ext;
assign below_zero_o = (adjusted_o < 6'sd0);
The braces assemble each six-bit pattern, and $signed gives that pattern a signed type. The comparison also uses signed values of the same width. Replace the addition with adjusted_o = sample_i + offset_i, and the unsigned reading makes this mixed addition treat both operands as unsigned. Widening 1101 adds zeros, so it becomes 001101, or 13. The resulting sum is 15. The signed comparison that follows correctly reports that 15 is above zero. The offset’s sign was lost before it reached the comparator.
Where do signed right shift and integer division differ?
Use signed four-bit −3 in a short experiment. With a one-position right shift, original bits 3, 2, and 1 connect to output bits 2, 1, and 0. The kind of shift determines the source of the new top bit. With a signed left operand, >>> copies the old sign bit there, turning 1101 into 1110, or −2. The logical operator >> fills with zero and gives 0110, or 6. Signed integer division by 4'sd2 rounds −1.5 toward zero, giving −1.
Further detail: shifting by four and reading bit 4
A signal-driven shift selects among different wire sources. In Icarus, shifting signed four-bit −3 right by four fills the output with the sign bit, giving 1111. The separate expression value_i[4] asks for a bit outside [3:0], giving X in this four-state simulation. Yosys 0.33 uses wires for the fixed shifts and retains a generic $sshr for the variable arithmetic shift. It simplifies this constant division by 2 to addition and bit logic. Division or remainder with a general input divisor can require more logic. Check the operand widths, known divisor conditions, and synthesis and timing reports. F13 revisits the hardware cost.
Check full precision, the low bits, and the sign separately
Download the F05 RTL, testbenches, VCDs and generic netlists. Extract the archive on Linux or WSL and run bash run.sh. The testbench drives inputs every 5 ns and compares the output 1 ns later. Each arithmetic test runs four directed vectors and all 16×16 binary combinations. A separate integer calculation supplies expectations for the two output fields. Each intentionally wrong version uses the same testbench as the corrected version.
| Check | Recorded result |
|---|---|
| Full sum and modulo, Icarus Verilog 12.0 | The corrected RTL has 0 mismatches in 260 checks on either port. The concatenation version misses 122 full sums and passes all modulo checks. |
| Adjusted sum and sign, Icarus Verilog 12.0 | The corrected RTL has 0 mismatches in 260 checks on either port. The mixed-type version misses 131 sums and 38 sign checks. |
Five-bit input port, Icarus with -Wall | A separate probe connects the concatenation to a five-bit port. Icarus reports an actual width of four and pads the upper bit. In simulation, the port gets 00000 and the direct assignment gets 10000. |
Generic Yosys 0.33 netlist after proc; opt | The full-sum $add has a five-bit result. The concatenation version has four. Both offset operands are signed in the corrected RTL and unsigned in the mixed version. |
The tool transcript and evidence with SHA-256 document the run. The warning comes from the explicit port probe. A compiler can accept the original assignment without a warning, so test the value required by the interface. The page’s interactive example follows the same arithmetic contract. The archive contains the RTL simulation and synthesis records.
For a handoff, record the full-result range, 0 to 30, its five-bit width, and the requirement to preserve the carry. Document the modulo field as four bits, with a range of 0 to 15 and a wrap at 16. Record the adjusted result as signed six-bit, with a range of −8 to 22, and define the flag against zero. The next lesson uses the same approach for channel count and channel width.
Transfer exercise
Represent −3 as a signed four-bit value. For a one-position arithmetic right shift, state the new high bit and the resulting pattern. Then calculate / 4'sd2. Which result meets an interface that rounds toward zero?
Solution
The input is 1101. Arithmetic right shift fills with 1 and produces 1110 (−2). Signed integer division rounds toward zero and produces 1111 (−1). The rounding contract requires −1.
References
- IEEE Std 1800-2017, §§11.4.3, 11.4.10, 11.6, and 11.8, covers arithmetic, shifts, expression sizing and signedness. Standard PDF
- Icarus Verilog documents language modes and available warnings. Command-line flags
- Yosys documents RTL conversion, optimization, and technology mapping. Synthesis starter