Break IC PIC16F616 Heximal: Half the Configuration Is in the Code
There is a persistent misconception about what the configuration word configures. It configures the silicon, not the peripherals. It selects the oscillator source, arms the watchdog, sets the brown-out threshold and controls the protection bits, and it says nothing about how a comparator is wired or what a reference voltage is set to.
Those decisions are made by the firmware, in the first few dozen instructions after reset, as a sequence of register writes. On a device with a rich analogue front end that sequence is the most important part of the configuration, and it is invisible to anyone who treats the hex file as a list of instruction words with no meaning.
What is configured where
| Decision | Where it lives | How it appears in the image |
|---|---|---|
| oscillator source and divider | configuration word | a field at the top of the map |
| watchdog and brown-out | configuration word | fields at the top of the map |
| comparator enable and input selection | startup register writes | a run of literal loads and stores near address zero |
| programmable reference level | startup register writes | one or two registers written with a computed value |
| capture and PWM setup | startup register writes | timer and control registers written in a defined order |
| pin direction and analogue select | startup register writes | port direction and analogue-select registers |
The third through sixth rows are all in the code, and all four are things a customer rebuilding the product will need to reproduce exactly.
Why the startup sequence deserves to be read as a document
The order of the writes is not arbitrary. Each peripheral has a dependency structure, and the correct order follows it.
A comparator’s input selection depends on the pin’s analogue-select register, so the analogue select is written first. The reference depends on the comparator being disabled while its level is changed, so the comparator is disabled, the reference is set, and the comparator is re-enabled. The capture unit depends on a timer being configured, because it captures the timer’s value.
Read in that order, the sequence describes the design’s signal path. It says which pins are measured, what they are compared against, and what happens to the result. On a product whose entire function is a threshold decision, that is the function.
The reference level is a number worth extracting
The programmable reference on this family produces a fraction of the supply, set by a register value. The value is a constant in the code, and it names the threshold the product uses.
That constant is one of the most useful things to report, because it is usually the only place the product’s operating point appears anywhere in the device. A product that closes a relay when a sensor exceeds two-thirds of supply is a product whose specification was a voltage; the constant is that specification, expressed as a register value.
Two cautions apply. The reference is ratiometric to the supply on this generation, so the threshold moves with the supply voltage, which is a design choice rather than an error, and a customer comparing against an absolute threshold will find the product behaving oddly at low supply. And the register value may be computed at runtime from a stored constant, in which case the constant rather than the register write is what should be reported.
Reading the code with the analogue blocks in mind
Three passes over the startup region extract almost everything.
The first pass collects every write to an analogue-select or direction register. Those name the pins.
The second pass collects every write to a comparator or reference register, in order, including the values. Those name the thresholds and the hysteresis setting.
The third pass collects every read of a comparator output, either as a bit test or as a value loaded into a port. Those name the decisions.
Together the three passes produce a block diagram of the analogue front end — which is what the customer actually wants when the request is to understand the product.
Timing behaviour sits next to the analogue behaviour
A comparator’s output is usually debounced before it is acted on, and the debounce is a counting loop. Its constant is the product’s response time, and it is worth extracting alongside the threshold, because the pair together define the product’s behaviour more precisely than either alone.
Where the device has a capture unit, the same reasoning applies to the timing side: a capture configuration tells you what event is being measured and against what clock, and the resolution of the measurement follows from those two facts.
Where the configuration word still matters
It decides whether the device starts at all, and it decides what clock everything runs from — which changes every timing number extracted above.
An oscillator selection that divides the internal clock by four turns a fifty-millisecond debounce into two hundred milliseconds. Reporting timing constants without the oscillator setting is therefore reporting numbers that are only correct on one configuration.
The delivery
- the program image with the startup region identified by address range
- the analogue configuration decoded: pins, reference level, hysteresis, comparator mode
- the debounce and timing constants, with the oscillator setting they assume
- the configuration word decoded
- a signal-path description in plain language
- a list of any register writes whose purpose could not be determined
The last item is honest and occasionally long. A register written with a value whose meaning is not documented in any available datasheet revision should be reported as unexplained rather than assigned a guess.
For the completeness criteria that apply to an image of this size, PIC18F4220 binary boundaries sets out how to state ranges and blanks. Where the analogue block is the entire product and the code is barely more than the configuration sequence, PIC12F615 software recovery covers the eight-pin version. And for a sibling in the same mid-range line where the comparison is against a fixed internal reference rather than a programmable one, PIC16F684 binary recovery is the useful contrast.
The mistake this prevents
The mistake is delivering a hex file and calling it the configuration. A customer who receives that file, programs it, and finds the product behaving differently will conclude that the extraction was faulty, when in fact the file was correct and the reading of it was not attempted.
A hex file is a faithful record of what the device contained. A configuration description is an interpretation of what that content means. The second is what makes the first useful, and on a device whose analog behaviour is the product, the second is the deliverable.