PostHeaderIcon Reverse Engineering Microcontroller PIC16C554 Software: Recovering the State Machine

Thirty-three instructions, two levels of stack, no interrupts, and an eighteen-pin body holding five hundred and twelve instruction words. The PIC16C554 is a baseline-core device, and code written for it does not look like modern firmware. There are no interrupt handlers because there are no interrupts, no nested calls because the stack is two deep, and no scheduler because there is nothing to schedule.

What there is, in almost every design of this class, is a state machine: one RAM location holding the current state, a dispatch at the top of the loop, and a separate block of code per state. Recovering that structure is the single most useful thing an analysis of this part can do, and it is feasible because the constraints that made the original code awkward also make it regular.

Why the architecture forces this shape

Constraint Value Consequence for the code
instruction set 33 instructions no indexed addressing, no multiply, everything through W
stack 2 levels a call from inside a call is impossible
interrupts none all timing is polling
program memory 512 words each state’s code must be small
RAM tens of bytes the state variable is one of a handful of locations

With a two-level stack, the natural structure is a flat loop with no calls at all, or a loop that calls exactly one level deep. With no interrupts, everything is sequential: check inputs, advance the state, drive outputs, repeat. That is a state machine by construction, whether or not the author used the term.

Finding the state variable

The state variable has a signature that is hard to miss once you know it: a single RAM location that is written in several places, read at the top of the loop, and used to compute a jump.

Three observations confirm it. It is written with small literal values, usually in sequence, rather than computed. It is read immediately before a table lookup or a chain of comparisons. And the values it takes on correspond one-to-one with blocks of code that are otherwise unrelated to each other.

A second candidate sometimes appears: a flag byte where individual bits are tested with bit-oriented instructions. That is not the state, it is the condition set — the inputs to the transition logic. Distinguishing the two matters, because the state variable names the blocks and the flag byte names the transitions.

Two dispatch styles

The core has no indexed jump, so dispatch is done in one of two ways, and they read differently.

The table style. The state value is added to the program counter, and the resulting address holds a goto for the corresponding state. The table is a contiguous run of identical instructions with different targets, and the targets are the state blocks. This style is compact and is the one a careful author chooses.

The comparison chain. The state value is compared against each possible value in turn, with a conditional skip and a jump. This style is longer and reads as a staircase of comparisons. It is what a compiler or a hurried author produces, and it has the advantage that the order of comparisons often reflects the frequency of the states.

A file may contain both: a table for the main states and a chain for the sub-states within one of them. That nesting is itself a structural fact worth recording.

Reading a state block

Each state block has the same internal shape, which makes the analysis mechanical once the pattern is recognised.

The block begins with the condition tests: read the inputs, compare against expected values, skip ahead if they do not match. Then the action: write outputs, update counters, set a timer value. Then the transition: write the next state value, or leave the current one to repeat.

Part of the block Recognisable by What it tells you
entry tests reads of port pins followed by skips what the state waits for
action writes to port pins or to a counter what the product does in this state
dwell a delay loop or a counter comparison how long the state lasts
exit a write of a small literal to one address which state follows

The dwell element is the one that carries the timing of the product, and on a part with no interrupts it is always implemented as a counted loop. Converting those counts into milliseconds using the oscillator frequency gives the product’s timing diagram, which is often the most legible description of what the device does.

From blocks to a diagram

Once every block has been read, the diagram is a few minutes of drawing: states as circles, transitions as arrows labelled with the condition that causes them, dwell times as annotations.

The diagram is worth more than the listing for three reasons. It is independent of the instruction set, so an engineer who has never written baseline assembly can read it. It exposes missing transitions, which usually correspond to states that were removed in a later revision and whose code remains as dead blocks. And it makes modification tractable: adding a state or changing a dwell time is a local edit on the diagram, where in the listing it is a change in three places.

Dead code and the revision history

A 512-word part that has been through several firmware revisions often contains blocks that no transition reaches. On a part with no self-modifying code and a fixed dispatch table, reachability is computable: start from the reset vector, follow every jump and every table entry, and mark what is visited.

What remains unvisited is either a removed state, an early version of a routine left in place because there was room, or a block reached only through a computed jump that the analysis did not resolve. The third category is where mistakes happen, and it should be resolved before the other two are called dead.

Delivering an analysis

The useful package is a diagram, a table of states with their dwell times and conditions, a map of the handful of RAM locations with their roles, and the annotated listing that supports all three.

The listing is the appendix. The diagram and the table are the deliverable, because they are what a customer’s engineer will use to answer the question that prompted the job — usually “can this be changed to do X instead of Y”.

Where the array is mask-programmed and cannot be read electrically at all, PIC16CR83 heximal covers the different extraction path. Where the same small-array constraint shapes a mid-range part with interrupts, PIC16F610 binary reads the compiler fingerprints instead. And where the structure to be recovered is a bus protocol rather than a state machine, PIC16F747 code covers the dispatch table.

The short version

No interrupts, a two-deep stack and five hundred words produce a state machine whether the author intended one or not. Find the variable that is written with small literals and read before a jump, read each block as tests–action–dwell–exit, and deliver a diagram. The listing is evidence; the diagram is the answer.

Comments are closed.