Posts Tagged ‘reverse engineering chip bin’
PIC12C508 Code: The Calibration Word That Lives in the Last Address
A PIC12C508 holds 512 twelve-bit words and 511 of them are program. The last one is a measurement. Microchip trimmed each die’s internal oscillator at final test and stored the result at address 0x1FF, where the reset code is expected to fetch it and write it into OSCCAL. That word is why a code read from this part is not finished when the last instruction has been decoded.

What actually sits at the top of the array
The array is one-time programmable, organised as 512 words of twelve bits, and the programmer’s address counter runs to 0x1FF like any other location. Nothing marks the top word as special in the silicon. What makes it special is the convention: the assembler template places a movlw of that value at the reset vector, and the first few instructions of every build move it into the OSCCAL register before anything else happens. Skip the word and the code still assembles, still links at the right address, and still runs. It simply runs at whatever frequency the erased or default trim produces, which on this family is a spread wide enough to break any timing that matters.
Why a dump without it still looks complete
A read that stops at 0x1FE produces a file of the right length for 511 words and a checksum that is internally consistent, because the checksum was computed over the same shortened range. This is the trap: nothing in the file complains. The failure appears later, on the second device, as a serial port that returns framing errors at one baud rate and works at another, or as a watchdog timeout that fires a few percent early. Engineers then spend a day on the UART configuration, which is where the original firmware spent its own time, and never look at the top of the array.
What the protection bit does to that one word
The code-protect bit is in the configuration word, which on a twelve-bit core is not reachable by the program counter and is therefore addressed by a separate programming command. When it is set, reads of the array return zero, which decodes as a no-operation, across the whole range including 0x1FF. There is no exception carved out for the calibration word. That is worth stating plainly because customers often assume the trim value is “factory data” and therefore readable; on this part it is ordinary array content and it disappears with everything else.
Three routes, and what each returns
| Route | What it reaches | What it costs |
|---|---|---|
| In-circuit read, protection clear | Full array including 0x1FF | Minutes, no risk |
| In-circuit read, protection set | 0x000 everywhere | Nothing useful |
| Optical read of the array | Every cell, including the trim word | Decapsulation, days |
The third row is the one that answers the question, and it is also the one that recovers the trim value rather than inferring it. Where the device is still alive and the protection is clear, the first row is enough and the whole job is a file transfer. The middle row is what most enquiries are actually sitting on.
When the word cannot be recovered
If the die is damaged, or the top word was never programmed because the design used an external crystal, there is a fallback that does not require the original value: calibrate the replacement. Program the image with any legal trim, run the device against a reference clock, and sweep OSCCAL until the output frequency matches. It takes an afternoon and a frequency counter, and it produces a unit that behaves correctly without pretending to be a bit copy. The distinction between a bit copy and a calibrated rebuild is the same one that separates the four objects in copying a microcontroller.
The question to ask before quoting
Ask what the second unit has to do. If it has to talk to the same bus at the same rate, the trim word is part of the deliverable and the route has to reach address 0x1FF. If it only has to demonstrate the algorithm on a bench, a default trim will do and the job is smaller. The sibling part PIC12C509 doubles the array and keeps the same convention at its own top address, and the low-voltage variant covered in PIC12LC508 moves the trim spread wider still. Format and verification of the resulting image are handled as described in extracting MCU code.