Break MCU PIC16F527 Heximal: A Memory Size That Is Not a Power of Two
Almost every program memory in this class is a power of two: 512, 1024, 2048, 4096. Engineers size buffers accordingly, tools round up, and a read that returns exactly 1024 words confirms itself. Then a part like the PIC16F527 arrives with fifteen hundred and thirty-six words, and every one of those assumptions becomes a source of error.
An eight-pin device with that much program space and a small analogue front end is an odd combination, and it exists for a reason: it is what you use when the code does not fit in a kilobyte but the board has no room for a bigger package. The awkward size is the point.
Where the number comes from
| Boundary | Address | Note |
|---|---|---|
| start of program | 0x000 | reset vector |
| end of code region | 0x5FF | last implemented word of the array |
| configuration area | above the array | layout varies by family member |
| unimplemented range | above the array | reads as a fixed value, not as blank |
Fifteen hundred and thirty-six words spans 0x000 to 0x5FF. It is three pages of five hundred and twelve, which is the reason for the number: the array is built from complete pages, and the family chose three rather than four to keep the die small.
That has a direct consequence for the image: the third page is partially used, and the boundary between the last page and the configuration area is not a round address. A read of exactly 0x600 words includes the configuration word; a read of 0x5FF excludes it; a read of 0x800 includes six hundred and forty words of unimplemented space that a tool may fill with a default value.
What tools do with a default length
Three behaviours, all of which produce a file that looks right.
A tool that reads a power-of-two length reads 2048 words and appends five hundred and twelve words of whatever the unimplemented space returns. If that value is all ones, the file looks like a program with a long blank tail and nobody notices. If it is a pattern, the file contains a region that is neither code nor blank, and a later reader will try to interpret it.
A tool that reads exactly the requested length truncates or extends by a few words, and the configuration word ends up in the wrong place — usually included when it should not be, or omitted when it should be.
A tool that reads until the last non-blank word returns a file that ends at the last instruction, which on this part is a perfectly valid file and an incomplete image: the blank tail is information, because it says where the linker stopped.
Why the boundary has to be established, not assumed
On a power-of-two part the boundary is implied by the size. Here it has to be determined, and there are two ways.
The first is to read the full implemented range and then read beyond it. The values above the array are fixed and characteristic — they do not vary between addresses the way code does, and they do not look like all ones at every address. Comparing the two regions establishes the boundary empirically.
The second is to consult the device identification the programming interface returns, which states the array size. That is faster and requires the interface to answer, which on a device with a marginal connection it may not.
Both are worth doing on a first unit of a design, because the answer is then known for every subsequent unit of the same type.
Reading the program when the array is an odd length
The analysis is unaffected: fifteen hundred words of code is fifteen hundred words, and the reading techniques that apply to any part of this core apply here. What changes is the arithmetic around the analysis.
A table at the top of the third page has a base address that is not a round number, and any calculation of the table offset has to use the real base rather than a rounded one. A cross-page jump into the third page has a target that is not aligned to anything, which makes the disassembly of that region harder to check by inspection.
The practical mitigation is to work from the address rather than from an index, and to print the page and offset alongside every label.
The analogue front end on an eight-pin part
The reason this part exists is that it combines a small package with more code space than the eight-pin baseline and a modest analogue capability. On the bench, that means two things.
The pins are shared: an analogue input is also a digital I/O, and the configuration of which is which is written at initialisation. Reading that write tells you how many sensor inputs the product has, which is usually one or two, because eight pins does not allow more.
And the conversion is referenced to the supply on most designs of this class, which makes the product’s accuracy dependent on the regulator. A recovered image whose thresholds are expressed in raw counts can be converted into voltages only if the reference assumption is known, and the reference is set in a register, so it is readable.
Delivering a file whose length is a statement
| Item | Content | Why it matters here |
|---|---|---|
| array image | 0x000 to 0x5FF | the implemented range, exactly |
| boundary note | how the boundary was established | prevents a later default-length read |
| configuration | decoded fields | a value that does not sit at a round address |
| blank map | ranges that are erased | distinguishes blank from unimplemented |
The second item is specific to this part and worth the sentence it costs: a colleague who re-reads the same design in a year will otherwise use a tool default and end up with a file that is six hundred words too long.
Where the memory size is small enough that the tail is the configuration word itself, PIC12F508 code recovery covers that boundary. Where an oversized array on a bigger package shows how the extra space is actually used, PIC16C662 heximal reads the port structure. And where the small array forces the code into visible shapes, PIC16F610 binary covers the fingerprints.
The short version
Three pages, not four. Establish the boundary by reading past it, state the implemented range in the delivery, and never let a tool’s default length decide what the image contains.