G1.2.1 The Sensor That Read 65,535

EasyFixIcarus · browser samples / server checksReview the topic

Fix a calibration adder that turns a sensor sample of -1 into 65,535.

Hardware context: A signed sensor sample widened into a calibration adder, and a byte negator for a motor driver. Repair the adder that zero-extends a negative 16-bit sample into 20 bits.

A pressure sensor sends each sample as a 16-bit two's-complement number. The calibrate block adds a 20-bit offset and passes the level on. In the lab every level was right. In the field the tank ran slightly below its zero mark, the sample became -1, and the display jumped to 65,535.

This is a Fix problem. The module below compiles and is right for every sample that is zero or positive. Find why a negative sample goes wrong and repair it.

The check applies four pairs of sample and offset, one per nanosecond, and records the level after each.

Signals in the Timeline

SignalWidthRole
script144input of each check
Full specification
The module (keep the name and the ports)
module calibrate (
  input  logic        [15:0] reading,   // the sample: two's complement, 16 bits
  input  logic signed [19:0] offset,    // the calibration offset: two's complement, 20 bits
  output logic signed [19:0] level      // offset + reading
);
  // the logic to repair
endmodule
SignalMeaning
readingThe sample, from -32,768 to 32,767 in two's complement. The port itself is declared without the signed keyword.
offsetThe calibration offset, a signed 20-bit number.
levelThe sum of the offset and the sample, as a signed 20-bit number. The test offsets keep the sum in range.

The result fields are named level_step0 to level_step3, one per step, shown as signed decimal numbers.

Examples

Example 1

The Bug Report
Input
reading ffff (-1), offset 0; reading 0000 (0), offset 0; reading 0001 (1), offset 0; reading fffe (-2), offset 0
Output
level_step0 = -1, level_step1 = 0, level_step2 = 1, level_step3 = -2

With no offset, the level must equal the sample: -1, 0, 1 and -2. The broken block shows 65,535 for the sample -1.

Example 2

Positive Samples
Input
reading 0064 (100), offset 50; reading 7fff (32767), offset 0; reading 0001 (1), offset -1; reading 1000 (4096), offset 1000
Output
level_step0 = 150, level_step1 = 32767, level_step2 = 0, level_step3 = 5096

For a sample that is zero or positive, the four new bits are 0 either way. This is why the lab tests passed.

Example 3

Negative Samples with an Offset
Input
reading ff9c (-100), offset 50; reading 8000 (-32768), offset 0; reading ffff (-1), offset 1; reading f000 (-4096), offset -1000
Output
level_step0 = -50, level_step1 = -32768, level_step2 = 0, level_step3 = -5096

The sample -100 with the offset 50 must give -50. The most negative sample, -32,768, must come through unchanged when the offset is 0.

Constraints and Contract

  • Inputs are known 0/1 values. Each pair is held for one nanosecond and level is sampled at its end.
  • Keep the module name and the ports, including the unsigned declaration of reading.

Hints

Hint 1

Write the sample -1 as sixteen bits. Then write the 20-bit number that the adder really adds. Which four bits were invented, and with what value?

Hint 2

A value is widened with copies of its top bit only when it is signed. Look at how the port reading is declared.

Hint 3

Tell the adder to read the sample as signed. The $signed function does it. So do four copies of bit 15 in front of the sample.

What the grader checks: Eight directed and eight seeded cases of four steps each run on the block. Every level is compared with a model written in JavaScript. A sum outside the 20-bit range is outside this problem.

16 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 3 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

The Bug Report

Input
reading ffff (-1), offset 0; reading 0000 (0), offset 0; reading 0001 (1), offset 0; reading fffe (-2), offset 0
Expected output
level_step0 = -1, level_step1 = 0, level_step2 = 1, level_step3 = -2

With no offset, the level must equal the sample: -1, 0, 1 and -2. The broken block shows 65,535 for the sample -1.

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

Icarus · browser samples / server checks