PostHeaderIcon Reverse Engineering Microcontroller PIC16C717 Program: The Constants Are the Specification

An instruction listing answers the question “what does the program do”. It rarely answers the more useful question, which is “what is this product”. For a device built around an analogue input, the second answer is carried almost entirely by numbers: scale factors, offset corrections, table entries, comparison thresholds. Those numbers survive extraction intact, and they can be read as a specification even when the instruction flow is only partly understood.

The PIC16C717 sits in the eighteen-pin branch of the OTP family that includes the twenty-pin parts with the higher-resolution converter. It shares their analogue front end and their protection mechanism, and it carries less program memory. That combination — a converter resolving better than eight bits, and a small array to hold the correction data — makes the constants unusually prominent: on this part, a meaningful fraction of the image is numbers rather than instructions.

Four shapes constants take

Shape Where it appears What it usually is
a run of retlw values immediately after a dispatch a lookup table indexed by a computed offset
a pair of adjacent words in initialisation, moved to RAM a gain and an offset
a single literal used in a multiply inside an arithmetic sequence a fixed-point scale factor
a value compared then acted on in a polling loop a threshold, in raw counts

The first row is the most common and the easiest to miss, because a table on this core is not a data block — it is a sequence of return instructions carrying literals. A disassembler that shows them as instructions will print a run of similar returns with no explanation, and a reader who does not recognise the pattern will skip past the most informative part of the image.

Why the table exists at all

An eight-bit conversion is roughly linear and needs little correction. As resolution increases, the sensor’s own non-linearity, the reference tolerance and the amplifier offset all become visible in the last digits, and the designer is forced to correct them. The correction is cheaper in memory than in arithmetic: a table of a few dozen calibrated points, interpolated between, replaces a polynomial that would need multiplies the core does not have.

So the presence and the length of a table is itself a statement about the product. A short table of eight or sixteen entries suggests a coarse correction over a wide range. A long table of sixty-four or more suggests a narrow range measured precisely, which is the signature of an instrument rather than a controller.

Reading range and units from the endpoints

A linearisation table is monotonic. Its first and last entries correspond to the endpoints of the sensor’s range, and the spacing between entries corresponds to the resolution the designer cared about.

Three deductions follow almost mechanically:

The number of entries times the conversion range gives the raw step per entry, which tells you whether the designer was interpolating or tabulating every point.

The first entry, converted through the scale factor that follows the table, gives the low end of the measured quantity. The last entry gives the high end. The ratio between them is the range.

If the entries are not evenly spaced, the spacing is denser where the sensor is most non-linear — which identifies the sensor type. Thermistors are dense at the cold end, strain gauges are dense near zero, and gas sensors are dense at the low-concentration end.

That last deduction is the one that turns a recovered image into a product description. It is inference, and it should be labelled as such, but it is strong inference: the designer did not choose the spacing arbitrarily.

Fixed-point arithmetic and the scale factor

This core has no multiply instruction. Scaling is done with shifts and adds, or with a small multiply routine fed by literals. The literal that appears in a shift-and-add sequence is a fixed-point coefficient, and its position tells you the assumed binary point.

A coefficient of 0x00CD applied to a sixteen-bit value suggests a scale of about 0.1 in 8.8 fixed point. A coefficient of 0x0100 is a shift by eight — no scaling at all, just a byte move. Recognising the difference between a real coefficient and a shift keeps the arithmetic readable.

The presence of a divide-by-ten sequence is a strong hint about the output format: the product displays a decimal value with one digit after the point, and the display routine will be nearby.

Thresholds, and what they are compared against

Comparison constants are the decision points of the product. A threshold compared against a converted value is a trip level; a threshold compared against a counter is a timeout; a threshold compared against a difference is a hysteresis band.

The useful reading is not the number itself but the number relative to the range established from the table. A trip level at 85 percent of full scale is an over-range alarm. Two thresholds separated by a small gap are a hysteresis pair, and the gap is the noise immunity the designer chose — which tells you something about the sensor’s noise, and therefore about its type.

What can honestly be claimed

Claim Evidence Strength
measurement range table endpoints and scale factor high
resolution table spacing and display digits medium to high
sensor class non-linearity direction medium
alarm levels comparison constants high
sample rate loop timing and delay counts medium
exact part number of the sensor none of the above alone low

The last row is where reports go wrong. Identifying a sensor family from its correction curve is reasonable; naming a specific manufacturer’s part number from the same evidence is not, and a customer who acts on that guess will order the wrong component.

Delivering the numbers separately

The most useful artefact from a job on this part is a one-page table of the constants with their meaning, the inferred ranges, and the confidence attached to each. It sits alongside the image and the annotated listing, and in practice it is the page that gets circulated inside the customer’s engineering team, because it answers questions about the product that the source code alone would not.

Where the same family’s protection behaviour is the subject rather than its arithmetic, PIC16C711 program breaking covers the irreversible case. Where the part is the twenty-pin member with more memory and the same converter, PIC16C771 firmware continues the reading. And where the numbers are not a linearisation table but an encrypted authentication payload, PIC12F635 programs deals with that different kind of content.

The short version

Read the constants before the control flow. A table of literals gives the range, the resolution and the sensor class; a coefficient gives the fixed-point scale; a comparison gives the alarm level. The instruction listing tells you how the product works, but the numbers tell you what it measures.

Comments are closed.