Posts Tagged ‘mcu pic18f4620 program reading’
PIC18F4620: A Word-Addressed Machine and Its Config Words
The PIC18 architecture does not think in bytes. Its program memory is an array of instruction words, the program counter counts words, and a jump target is a word address. That is different enough from a byte-addressed machine that habits carried over produce consistent, plausible errors.
The other difference is architectural: the protection and oscillator settings live in configuration words at addresses far outside the program range, so a dump of the program is a dump of the program only.
Word addressing, and what it breaks
Three consequences:
A stored byte address is not a program address. Converting between them is a shift, and code that stores addresses for a table lookup often stores the byte form.
Instructions vary in length. Most are one word; calls, jumps and table operations are two. A disassembler has to know which, and a reader assuming uniform width will drift.
A dump’s length in bytes is twice its length in instructions. An eight-kilobyte image is four thousand instructions, which changes any estimate of how much code there is.
| Property | Value on this part |
|---|---|
| Instruction width | 16 bits, some 32 |
| Address unit | one instruction word |
| Table access | dedicated pointer, byte-oriented |
| Configuration | separate address range |
Table access is where the two views meet
Reading constant data from program memory uses a table pointer that addresses bytes while the program counter addresses words. That mismatch is deliberate — tables hold bytes — and it is the source of the most common off-by-one in PIC18 analysis.
A table lookup therefore looks like: set the pointer to a byte address, execute the read, consume one byte. A reader who treats the pointer as a program address will land at half the intended location and find data that looks like instructions.
The configuration words
The oscillator selection, the watchdog, the code protection bits and the brown-out settings are held in configuration words outside the program array. They are not in a hex dump of the program, and they are read through their own mechanism.
This is the same structural point that applies to every architecture: protection is configuration, and configuration is elsewhere. A recovery that omits the configuration words produces an image that cannot be made to run, because the oscillator setting alone decides whether the part starts at all.
What protection looks like here
Code protection is a set of bits in a configuration word, and its enforcement happens at the programming interface, not at execution. A protected part runs normally and refuses to be read.
The important practical consequence is the same one as everywhere: the operation that clears the protection bits also erases the program memory. There is no sequence that unlocks a part and leaves its contents intact, and any description that suggests otherwise is describing a different mechanism.
Reading order for a PIC18 device
- Read the device identifier and the configuration words. Record both before anything else.
- Read the program array, verifying each region twice.
- Establish the reset vector and the interrupt vector targets.
- Disassemble with word addressing, checking instruction width at every branch.
- Locate table accesses and convert their pointers from bytes to words for interpretation.
Step 5 is where the two address views are reconciled, and getting it wrong produces a reconstruction that is internally consistent and externally wrong.
Where this differs from the AVR habit
An analyst moving between architectures carries assumptions. Two of them fail here: that addresses are bytes, and that configuration lives somewhere in the image. Neither is true, and both errors are quiet.
The defence is procedural rather than intellectual: read the identifier and configuration first, write down the address unit, and never assume a width without checking the opcode.
Related notes
- PIC24FJ16GA002 and the phantom byte — the same family with a different word model.
- PIC18F2550 and protection blocks — the protection model on a related part.
- ATmega8A and lock modes — the same concept in a byte-addressed machine.
FAQ
Are PIC18 instructions always two bytes? Most are. A few are four, and those are the ones that break a naive disassembly.
Where are the configuration words? Outside the program address range, read through a dedicated operation.
Can the program be read if the part is protected? No, and the configuration that blocks it is the same one that has to be cleared to read it.
Why does a table pointer use bytes on a word machine? Because tables contain bytes, and the hardware provides both views.
Conclusion
A word-addressed machine punishes byte-oriented habits in a way that looks like a data problem. Reading the configuration words first, writing down the address unit, and checking instruction width at every branch are the three habits that keep a PIC18 analysis from drifting — and they cost less than a single wrong reconstruction.