| 1 | // Commander X16 Emulator — disassembly-accuracy core ("code map"). |
| 2 | // |
| 3 | // THE PROBLEM: 65C02/65C816 instructions are variable length, and on the |
| 4 | // 65C816 the operand width of many instructions depends on the M/X bits of the |
| 5 | // processor status register. A naive linear disassembler therefore drifts out |
| 6 | // of alignment: start one byte off (or assume the wrong operand width) and |
| 7 | // every following instruction is garbage until it accidentally re-syncs. |
| 8 | // |
| 9 | // THE FIX: capture ground truth while the program actually runs. Every time the |
| 10 | // CPU fetches an instruction we record (a) that the address is a REAL |
| 11 | // instruction start, keyed by the bank context that actually backs it, and (b) |
| 12 | // the effective processor status (M/X/E folded in) at that moment. The anchored |
| 13 | // disassembler then aligns instruction boundaries using, in priority order: |
| 14 | // 1. recorded instruction starts (live execution ground truth), |
| 15 | // 2. cc65 .dbg span starts (compiler-emitted boundaries), |
| 16 | // 3. a backward self-sync heuristic (try candidate offsets, keep the one that |
| 17 | // decodes cleanly onto the anchor). |
| 18 | // A decode is never allowed to swallow a recorded start UNLESS it is itself |
| 19 | // recorded -- two recorded starts that overlap are two real execution paths, |
| 20 | // not a bad guess straddling a good one -- so an over-wide guess is corrected |
| 21 | // at the next piece of hard evidence rather than drifting on. |
| 22 | // |
| 23 | // WHERE THE WIDTHS ARE STILL A GUESS, so callers know what they are getting: |
| 24 | // only five instructions change the 65C816 register widths -- REP, SEP, XCE, |
| 25 | // PLP and RTI. REP and SEP apply their own status bits exactly (the bits are in |
| 26 | // the operand), though the widths those imply are still subject to the E |
| 27 | // estimate below. XCE is exact only when both of its inputs are right, and |
| 28 | // neither is guaranteed: it swaps the carry and emulation flags, and the carry |
| 29 | // is not tracked through any data-dependent change (ADC, SBC, the compares, the |
| 30 | // shifts and rotates), while E is not recorded by anchors at all. The last two |
| 31 | // restore a status byte pulled off the stack; this linear, instruction-stream |
| 32 | // analysis does not recover it. |
| 33 | // |
| 34 | // None of these gaps is bounded unconditionally. |
| 35 | // |
| 36 | // A wrong STATUS estimate can be corrected when an accurate, believed anchor is |
| 37 | // reached -- but that anchor fixes its OWN line, and accuracy does not |
| 38 | // automatically carry forward: an anchor on a PLP or an RTI supplies the status |
| 39 | // going INTO it, and the byte it restores is still not modelled, so the very |
| 40 | // next line can be wrong again. A same-opcode stale anchor is "recorded" and |
| 41 | // hands back its old status, and there may be no accurate anchor ahead at all. |
| 42 | // |
| 43 | // A wrong E is weaker again, because anchors do not store E: an anchor fixes |
| 44 | // the width of its own line, and propagation then folds the stale E back in, |
| 45 | // which can mis-size the next unanchored line wherever the stale E and the real |
| 46 | // one imply different widths. It is not unrecoverable -- an XCE decoded with an |
| 47 | // accurate carry writes a correct E, as does an explicit CLC/SEC before one -- |
| 48 | // but nothing guarantees such a sequence appears, so no persistent recovery is |
| 49 | // promised. |
| 50 | // |
| 51 | // Note the emulation-mode escape clause applies to the ESTIMATE, not the |
| 52 | // machine: the real CPU in emulation mode always has 8-bit widths, but the |
| 53 | // fold-in here uses the estimated E, so a diverged estimate can size operands |
| 54 | // as though native while the machine is really in emulation. Pinned by tests. |
| 55 | // |
| 56 | // This costs nothing in practice unless all of the following hold at once: the |
| 57 | // machine is a 65C816 (on the 65C02 widths never vary), the line being drawn |
| 58 | // has no currently believed anchor, and the status the estimate carries into |
| 59 | // that line is not the status the code there really runs with. That last one is |
| 60 | // broader than the unmodelled instructions above -- an address-linear walk over |
| 61 | // a branch or call, or a walk started away from the PC, does it too. See |
| 62 | // docs/code-map-width-propagation.md. Every line this could affect already |
| 63 | // reports `recorded = false`. |
| 64 | // |
| 65 | // Anchors are not deleted when the code under them changes -- there is no write |
| 66 | // hook -- so each stores the opcode byte that was executing and is ignored once |
| 67 | // memory no longer matches. That catches most overlay loads, a second program |
| 68 | // loaded over the first, and self-modifying code, without needing a hook on |
| 69 | // every memory write. It is a one-byte check, not a proof: replacement code |
| 70 | // that happens to repeat the same opcode byte at the same address keeps the old |
| 71 | // anchor, and with it the old recorded status. Such an anchor is still |
| 72 | // believed, so it can cover a genuine instruction start in the new code; and on |
| 73 | // a 65C816 its stale status can also make the line decode wider -- or narrower |
| 74 | // -- than the new code really is. Self-correcting once an accurate anchor it |
| 75 | // has not swallowed is reached, if there is one ahead, and pinned by tests. See |
| 76 | // docs/code-map-width-propagation.md. |
| 77 | // |
| 78 | // This header is plain C and is shared by the C core (recording hooks, DAP) and |
| 79 | // the C++ ImGui panel (via debug_ui_bridge.h). |
| 80 | |
| 81 | |
| 82 | |
| 83 | |
| 84 | |
| 85 | |
| 86 | |
| 87 | extern "C" |
| 88 | |
| 89 | |
| 90 | |
| 91 | |
| 92 | |
| 93 | |
| 94 | |
| 95 | |
| 96 | |
| 97 | |
| 98 | |
| 99 | |
| 100 | |
| 101 | |
| 102 | |
| 103 | |
| 104 | |
| 105 | |
| 106 | |
| 107 | |
| 108 | |
| 109 | |
| 110 | |
| 111 | |
| 112 | |
| 113 | |
| 114 | |
| 115 | |
| 116 | |
| 117 | |
| 118 | |
| 119 | |
| 120 | |
| 121 | |
| 122 | |
| 123 | |
| 124 | |
| 125 | |
| 126 | |
| 127 | |
| 128 | |
| 129 | |
| 130 | |
| 131 | |
| 132 | |
| 133 | |
| 134 | |
| 135 | |
| 136 | |
| 137 | |
| 138 | |
| 139 | |
| 140 | |
| 141 | |
| 142 | |
| 143 | |
| 144 | |
| 145 | |
| 146 | |
| 147 | |
| 148 | |
| 149 | |
| 150 | |
| 151 | |
| 152 | |
| 153 | |
| 154 | |
| 155 | |
| 156 | |
| 157 | |
| 158 | |
| 159 | |
| 160 | |
| 161 | |
| 162 | |
| 163 | |
| 164 | |
| 165 | |
| 166 | |
| 167 | |
| 168 | |
| 169 | |
| 170 | |
| 171 | |
| 172 | |
| 173 | |
| 174 | |
| 175 | |
| 176 | |
| 177 | |
| 178 | |
| 179 | |
| 180 | |
| 181 | |
| 182 | |
| 183 | |
| 184 | |
| 185 | |
| 186 | |
| 187 | |
| 188 | |
| 189 | |
| 190 | |
| 191 | |
| 192 | |
| 193 | |
| 194 | |
| 195 | |
| 196 | |
| 197 | |
| 198 | __cplusplus |
| 199 | } // extern "C" |
| 200 | |
| 201 | |
| 202 | // _CODE_MAP_H_ |