@henols/c64-re-tools 1.0.0 → 2.0.0-rc.10

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (159) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +154 -37
  3. package/distribution/package.ts +21 -0
  4. package/distribution/plugin.ts +122 -0
  5. package/package.json +34 -40
  6. package/skills/c64-assembler/SKILL.md +76 -0
  7. package/skills/c64-assembler/scripts/assemble.ts +110 -0
  8. package/skills/c64-assembler/scripts/compare.ts +84 -0
  9. package/skills/c64-basic/SKILL.md +66 -0
  10. package/skills/c64-basic/scripts/basic.ts +46 -0
  11. package/skills/c64-disk/SKILL.md +93 -0
  12. package/skills/c64-disk/scripts/containers.ts +124 -0
  13. package/skills/c64-disk/scripts/disk.ts +120 -0
  14. package/skills/c64-emulator/SKILL.md +134 -0
  15. package/skills/c64-knowledge/SKILL.md +166 -0
  16. package/skills/c64-knowledge/scripts/knowledge.ts +492 -0
  17. package/skills/c64-memory-map/SKILL.md +79 -0
  18. package/skills/c64-memory-map/scripts/memmap.ts +92 -0
  19. package/skills/c64-provenance/SKILL.md +110 -0
  20. package/skills/c64-provenance/scripts/compare.ts +152 -0
  21. package/skills/c64-provenance/scripts/provenance.ts +334 -0
  22. package/skills/c64-provenance/scripts/runtime.ts +191 -0
  23. package/skills/c64-provenance/scripts/separate.ts +140 -0
  24. package/skills/c64-reverse-engineering/SKILL.md +144 -0
  25. package/skills/c64-source/SKILL.md +126 -0
  26. package/skills/c64-source/scripts/generate.ts +490 -0
  27. package/skills/c64-source/scripts/loader.ts +571 -0
  28. package/skills/c64-source/scripts/opcodes.ts +61 -0
  29. package/skills/c64-source/scripts/source.ts +595 -0
  30. package/skills/c64-static-analysis/SKILL.md +119 -0
  31. package/skills/c64-static-analysis/scripts/analyze.ts +275 -0
  32. package/skills/c64-static-analysis/scripts/coverage.ts +78 -0
  33. package/skills/c64-static-analysis/scripts/findings.ts +75 -0
  34. package/skills/c64-static-analysis/scripts/idioms.ts +149 -0
  35. package/skills/c64-static-analysis/scripts/instructions.ts +105 -0
  36. package/skills/c64-testing/SKILL.md +103 -0
  37. package/skills/c64-testing/references/scenario-format.md +116 -0
  38. package/skills/c64-testing/scripts/checklist.ts +51 -0
  39. package/skills/c64-testing/scripts/run.ts +189 -0
  40. package/skills/c64-testing/scripts/scenario.ts +493 -0
  41. package/skills/c64-testing/scripts/test.ts +65 -0
  42. package/skills/c64-unpacker/SKILL.md +135 -0
  43. package/skills/c64-unpacker/scripts/evidence.ts +207 -0
  44. package/skills/c64-unpacker/scripts/loads.ts +209 -0
  45. package/skills/c64-unpacker/scripts/machine.ts +80 -0
  46. package/skills/c64-unpacker/scripts/stages.ts +130 -0
  47. package/skills/c64-unpacker/scripts/unpack.ts +683 -0
  48. package/src/c64.ts +411 -0
  49. package/src/cli/ap-sdk.ts +44 -0
  50. package/src/cli/cleanup.ts +194 -0
  51. package/src/cli/main.ts +125 -0
  52. package/src/cli/status.ts +61 -0
  53. package/src/host/main.ts +134 -0
  54. package/src/host/server.ts +392 -0
  55. package/src/host/tools/c1541.ts +284 -0
  56. package/src/host/tools/index.ts +35 -0
  57. package/src/host/tools/petcat.ts +77 -0
  58. package/src/host/tools/status.ts +47 -0
  59. package/src/host/tools/version.ts +41 -0
  60. package/src/host/vice/adapter.ts +804 -0
  61. package/src/host/vice/binary-monitor.ts +389 -0
  62. package/src/host/vice/chips.ts +118 -0
  63. package/src/host/vice/connect.ts +34 -0
  64. package/src/host/vice/process.ts +413 -0
  65. package/src/host/vice/screen.ts +184 -0
  66. package/src/host/vice/session.ts +1426 -0
  67. package/src/host/vice/text-monitor.ts +223 -0
  68. package/src/host-client/autostart.ts +55 -0
  69. package/src/host-client/connect.ts +241 -0
  70. package/src/host-client/coverage.ts +63 -0
  71. package/src/host-client/steps.ts +170 -0
  72. package/src/host-client/tools.ts +89 -0
  73. package/src/host-client/transfer.ts +126 -0
  74. package/src/host-client/vice-session.ts +316 -0
  75. package/src/knowledge/database.ts +132 -0
  76. package/src/knowledge/history.ts +163 -0
  77. package/src/knowledge/import.ts +391 -0
  78. package/src/knowledge/read.ts +342 -0
  79. package/src/knowledge/schema.ts +229 -0
  80. package/src/knowledge/write.ts +663 -0
  81. package/src/mcp/main.ts +134 -0
  82. package/src/mcp/output.ts +32 -0
  83. package/src/mcp/server.ts +316 -0
  84. package/src/mcp/tools/debug.ts +327 -0
  85. package/src/mcp/tools/execution.ts +117 -0
  86. package/src/mcp/tools/input.ts +72 -0
  87. package/src/mcp/tools/machine.ts +100 -0
  88. package/src/mcp/tools/media.ts +129 -0
  89. package/src/mcp/tools/memory.ts +250 -0
  90. package/src/mcp/tools/video.ts +271 -0
  91. package/src/native/acme.ts +116 -0
  92. package/src/native/discover.ts +179 -0
  93. package/src/native/dxa.ts +166 -0
  94. package/src/native/ghidra/analyze.ts +86 -0
  95. package/src/native/ghidra/index.ts +173 -0
  96. package/src/native/ghidra/language/c64rt_6510.cspec +34 -0
  97. package/src/native/ghidra/language/c64rt_6510.ldefs +15 -0
  98. package/src/native/ghidra/language/c64rt_6510.pspec +15 -0
  99. package/src/native/ghidra/language/c64rt_6510.slaspec +596 -0
  100. package/src/native/ghidra/scripts/C64Export.java +274 -0
  101. package/src/native/ghidra/scripts/C64Prepare.java +60 -0
  102. package/src/native/local.ts +34 -0
  103. package/src/native/processes.ts +350 -0
  104. package/src/native/run.ts +172 -0
  105. package/src/native/staging.ts +83 -0
  106. package/src/native/status.ts +113 -0
  107. package/src/native/types.ts +257 -0
  108. package/src/native/watchdog.ts +78 -0
  109. package/src/package.ts +21 -0
  110. package/src/platform.ts +441 -0
  111. package/src/project.ts +39 -0
  112. package/src/protocol/framing.ts +132 -0
  113. package/src/protocol/messages.ts +259 -0
  114. package/src/protocol/tools.ts +266 -0
  115. package/src/protocol/vice.ts +1348 -0
  116. package/src/script.ts +60 -0
  117. package/src/time.ts +15 -0
  118. package/src/trace.ts +194 -0
  119. package/THIRD-PARTY-NOTICES.md +0 -26
  120. package/bin/cli.mjs +0 -283
  121. package/skills/acme-build/SKILL.md +0 -260
  122. package/skills/acme-build/scripts/acme.mjs +0 -341
  123. package/skills/acme-build/template.a +0 -53
  124. package/skills/c64-disk-access/SKILL.md +0 -159
  125. package/skills/c64-disk-access/scripts/c1541.mjs +0 -569
  126. package/skills/c64-memory-mapping/SKILL.md +0 -631
  127. package/skills/c64-memory-mapping/memmap.json +0 -8800
  128. package/skills/c64-memory-mapping/scripts/driver.mjs +0 -553
  129. package/skills/c64-petcat/SKILL.md +0 -87
  130. package/skills/c64-petcat/scripts/petcat.mjs +0 -221
  131. package/skills/c64-program-recon/SKILL.md +0 -706
  132. package/skills/c64-program-recon/references/control-flow.md +0 -180
  133. package/skills/c64-program-recon/references/graphics.md +0 -79
  134. package/skills/c64-program-recon/references/observation-hazards.md +0 -143
  135. package/skills/c64-program-recon/references/reconstruction.md +0 -133
  136. package/skills/c64-program-recon/references/sound-and-input.md +0 -74
  137. package/skills/c64-program-recon/references/tool-selection.md +0 -78
  138. package/skills/c64-program-recon/scripts/derive.mjs +0 -364
  139. package/skills/c64-program-recon/scripts/packer-finding.mjs +0 -709
  140. package/skills/c64-program-recon/templates/memory-map.template.md +0 -101
  141. package/skills/c64-provenance-diff/SKILL.md +0 -292
  142. package/skills/c64-provenance-diff/scripts/diff-images.mjs +0 -984
  143. package/skills/c64-provenance-diff/scripts/recovery-schema.mjs +0 -395
  144. package/skills/c64-ram-capture/RELEASES.json.example +0 -17
  145. package/skills/c64-ram-capture/SKILL.md +0 -474
  146. package/skills/c64-ram-capture/scripts/compare-cross-binary.mjs +0 -525
  147. package/skills/c64-ram-capture/scripts/compare.mjs +0 -258
  148. package/skills/c64-ram-capture/scripts/derive-transients.mjs +0 -575
  149. package/skills/c64-ram-capture/scripts/dump-artifacts.mjs +0 -317
  150. package/skills/c64-ram-capture/scripts/mcp-module.mjs +0 -174
  151. package/skills/c64-ram-capture/scripts/project-paths.mjs +0 -81
  152. package/skills/c64-ram-capture/scripts/releases.mjs +0 -109
  153. package/skills/c64-ram-capture/scripts/vsf-slice.mjs +0 -147
  154. package/skills/c64-ram-capture/scripts/watch-loads.mjs +0 -581
  155. package/skills/c64-ram-capture/templates/capture-record.template.md +0 -99
  156. package/skills/c64-ram-capture/transients/README.md +0 -137
  157. package/skills/routine-queue-walker/SKILL.md +0 -394
  158. package/skills/routine-queue-walker/scripts/completeness-report.mjs +0 -465
  159. package/skills/vice-wedge-triage/SKILL.md +0 -269
@@ -0,0 +1,191 @@
1
+ // Runtime evidence for the differences between releases (D50): each release
2
+ // runs in its own emulator, and each differing range gets its first access
3
+ // (read, write, execute or none) and whether its bytes changed by the end.
4
+ // Watchpoints and range breakpoints stop the run at an access; the coverage
5
+ // map then tells which range and which access it was.
6
+
7
+ import { COVERAGE, formatC64Address, instructionLength } from "#src/c64.ts";
8
+ import { runProgram } from "#src/host-client/autostart.ts";
9
+ import { readCoverage } from "#src/host-client/coverage.ts";
10
+ import { runActionStep, type ActionStep } from "#src/host-client/steps.ts";
11
+ import { MAX_MEMORY_READ, MAX_MEMORY_WRITE } from "#src/host-client/tools.ts";
12
+ import { readProjectFile } from "#src/host-client/transfer.ts";
13
+ import type { ViceSessionClient } from "#src/host-client/vice-session.ts";
14
+ import type { Range } from "./compare.ts";
15
+
16
+ export type FirstAccess = "read" | "write" | "execute" | "none";
17
+
18
+ export interface RangeAccess {
19
+ first: FirstAccess;
20
+ /** True when the bytes at the end differ from the bytes of the release. */
21
+ changed: boolean;
22
+ }
23
+
24
+ /** How a release starts in the emulator. */
25
+ export type Start =
26
+ | { kind: "program"; path: string }
27
+ | { kind: "image"; segments: Range[]; byteAt: (address: number) => number; state: ImageState };
28
+
29
+ export interface ImageState {
30
+ pc: number;
31
+ a: number;
32
+ x: number;
33
+ y: number;
34
+ sp: number;
35
+ flags: { n: boolean; v: boolean; d: boolean; i: boolean; z: boolean; c: boolean };
36
+ port: [number, number];
37
+ /** $0000-$03ff at the entry. */
38
+ pages: Uint8Array;
39
+ }
40
+
41
+ const MAX_WATCH = 256;
42
+ /** Frames until the KERNAL is at the READY prompt, before an image is written. */
43
+ const READY_FRAMES = 150;
44
+
45
+ const READ = COVERAGE.ramRead | COVERAGE.romRead | COVERAGE.ioRead;
46
+ const WRITE = COVERAGE.ramWrite | COVERAGE.ioWrite;
47
+ const EXECUTE = COVERAGE.ramExecute | COVERAGE.romExecute | COVERAGE.ioExecute;
48
+
49
+ /** The first access that coverage flags show; a read-modify-write reads first. */
50
+ export function accessOf(flags: number): FirstAccess {
51
+ if ((flags & EXECUTE) !== 0) return "execute";
52
+ if ((flags & READ) !== 0) return "read";
53
+ if ((flags & WRITE) !== 0) return "write";
54
+ return "none";
55
+ }
56
+
57
+ /** Starts the release; returns why it did not start, or undefined. */
58
+ async function start(client: ViceSessionClient, how: Start): Promise<string | undefined> {
59
+ if (how.kind === "program") {
60
+ const failed = await runProgram(client, how.path, readProjectFile(how.path).bytes);
61
+ if (failed === "not-loaded") return "The program was not in memory after autostart.";
62
+ if (failed === "not-started") return "The program did not start after RUN; it needs a BASIC start line.";
63
+ return undefined;
64
+ }
65
+ await client.reset({ mode: "hard", run: true });
66
+ await client.execution({ action: "advance-frames", count: READY_FRAMES, space: "c64" });
67
+ for (const segment of how.segments) {
68
+ for (let at = segment.start; at <= segment.end; at += MAX_MEMORY_WRITE) {
69
+ const last = Math.min(segment.end, at + MAX_MEMORY_WRITE - 1);
70
+ const bytes = Buffer.from(Array.from({ length: last - at + 1 }, (_, offset) => how.byteAt(at + offset)));
71
+ await client.memoryWrite({ address: at, data: bytes.toString("hex"), space: "c64", view: "ram" });
72
+ }
73
+ }
74
+ const { state } = how;
75
+ // Pages 0 to 3 from $0002, then the CPU port: the port bits switch what the CPU sees.
76
+ await client.memoryWrite({ address: 0x0002, data: Buffer.from(state.pages.subarray(2)).toString("hex"), space: "c64", view: "ram" });
77
+ await client.memoryWrite({ address: 0x0000, data: Buffer.from(state.port).toString("hex"), space: "c64", view: "cpu" });
78
+ await client.registersSet("c64", { pc: state.pc, a: state.a, x: state.x, y: state.y, sp: state.sp, flags: state.flags });
79
+ return undefined;
80
+ }
81
+
82
+ /**
83
+ * Runs one release and watches the ranges (addresses of the release, after
84
+ * any shift is taken away). `release` gives the release's own bytes.
85
+ */
86
+ export async function watchRelease(
87
+ client: ViceSessionClient,
88
+ how: Start,
89
+ ranges: Range[],
90
+ release: (address: number) => number,
91
+ run: { frames: number; steps: ActionStep[] },
92
+ ): Promise<{ started: true; accesses: RangeAccess[] } | { started: false; reason: string }> {
93
+ await client.reset({ mode: "hard", run: false });
94
+ const failed = await start(client, how);
95
+ if (failed !== undefined) return { started: false, reason: failed };
96
+ if ((await client.status()).state === "running") await client.execution({ action: "pause", space: "c64" });
97
+
98
+ const first: FirstAccess[] = ranges.map(() => "none");
99
+ const points = new Map<number, Array<{ kind: "watch" | "break"; id: number }>>();
100
+ for (const [index, range] of ranges.entries()) {
101
+ const ids: Array<{ kind: "watch" | "break"; id: number }> = [];
102
+ for (let at = range.start; at <= range.end; at += MAX_WATCH) {
103
+ const size = Math.min(MAX_WATCH, range.end - at + 1);
104
+ ids.push({ kind: "watch", id: (await client.watchpoint({ action: "add", address: at, size, access: "read-write", space: "c64" })).id });
105
+ ids.push({ kind: "break", id: (await client.breakpoint({ action: "add", address: at, size, space: "c64" })).id });
106
+ }
107
+ points.set(index, ids);
108
+ }
109
+ await client.memmap({ action: "clear" });
110
+
111
+ /**
112
+ * After a stop: the ranges that the coverage shows accessed get their first
113
+ * access, and lose their points. VICE marks only the opcode byte of an
114
+ * executed instruction, so an instruction that starts up to two bytes before
115
+ * a range, or in it, executes its operand bytes too (F-27).
116
+ */
117
+ const settle = async (): Promise<boolean> => {
118
+ const map = await readCoverage(client);
119
+ let found = false;
120
+ for (const [index, ids] of points) {
121
+ const range = ranges[index]!;
122
+ let flags = 0;
123
+ for (let address = range.start; address <= range.end; address++) flags |= map[address]!;
124
+ const from = Math.max(0, range.start - 2);
125
+ if ((flags & EXECUTE) === 0 && map.subarray(from, range.start).some((byte) => (byte & COVERAGE.ramExecute) !== 0)) {
126
+ const code = Buffer.from((await client.memoryRead({ address: from, size: range.start - from, space: "c64", view: "ram" })).data, "hex");
127
+ for (let address = from; address < range.start; address++) {
128
+ if ((map[address]! & COVERAGE.ramExecute) !== 0 && address + instructionLength(code[address - from]!) - 1 >= range.start) flags |= COVERAGE.ramExecute;
129
+ }
130
+ }
131
+ if (accessOf(flags) === "none") continue;
132
+ first[index] = accessOf(flags);
133
+ for (const point of ids) {
134
+ if (point.kind === "watch") await client.watchpoint({ action: "remove", id: point.id });
135
+ else await client.breakpoint({ action: "remove", id: point.id });
136
+ }
137
+ points.delete(index);
138
+ found = true;
139
+ }
140
+ return found;
141
+ };
142
+
143
+ // Each stop at a point settles at least one range, so the stops are bounded; the guard ends a run that stops without an access.
144
+ let guard = 4 * ranges.length + 16;
145
+ /** Runs frames; a stop at a point settles and goes on with the frames that are left. */
146
+ const runFrames = async (count: number) => {
147
+ let left = count;
148
+ while (left > 0 && guard-- > 0) {
149
+ const ran = await client.execution({ action: "advance-frames", count: left, space: "c64" });
150
+ left -= ran.advancedFrames ?? left;
151
+ if (left > 0 && !(await settle())) left--;
152
+ }
153
+ };
154
+ for (const step of run.steps) {
155
+ if ("frames" in step) {
156
+ await runFrames(step.frames);
157
+ continue;
158
+ }
159
+ if ("runUntil" in step) {
160
+ // A stop at a point is not the target: settle and run to the target again.
161
+ while (guard-- > 0) {
162
+ const reached = await runActionStep(client, step, (location) => Number.parseInt(location.slice(1), 16));
163
+ if (reached === undefined || reached.stopReason !== "watchpoint" && reached.stopReason !== "breakpoint") break;
164
+ await settle();
165
+ }
166
+ continue;
167
+ }
168
+ await runActionStep(client, step, (location) => Number.parseInt(location.slice(1), 16));
169
+ }
170
+ await runFrames(run.frames);
171
+ if (points.size > 0) await settle();
172
+
173
+ const accesses: RangeAccess[] = [];
174
+ for (const [index, range] of ranges.entries()) {
175
+ const parts: Buffer[] = [];
176
+ for (let at = range.start; at <= range.end; at += MAX_MEMORY_READ) {
177
+ parts.push(Buffer.from((await client.memoryRead({ address: at, size: Math.min(MAX_MEMORY_READ, range.end - at + 1), space: "c64", view: "ram" })).data, "hex"));
178
+ }
179
+ const now = Buffer.concat(parts);
180
+ accesses.push({ first: first[index]!, changed: now.some((byte, offset) => byte !== release(range.start + offset)) });
181
+ }
182
+ for (const ids of points.values()) {
183
+ for (const point of ids) {
184
+ if (point.kind === "watch") await client.watchpoint({ action: "remove", id: point.id });
185
+ else await client.breakpoint({ action: "remove", id: point.id });
186
+ }
187
+ }
188
+ return { started: true, accesses };
189
+ }
190
+
191
+ export const showRange = (range: Range) => `${formatC64Address(range.start)}-${formatC64Address(range.end)}`;
@@ -0,0 +1,140 @@
1
+ // Evidence from one release (D51): for each range of the image, the unpack
2
+ // stage that wrote it and the access class from coverage; for each code
3
+ // region of knowledge, whether call and jump references reach it from the
4
+ // entry; and what the program reads or executes outside the image. Evidence
5
+ // only: nothing here decides what is original.
6
+
7
+ import { COVERAGE, executedRamCode } from "#src/c64.ts";
8
+ import type { Range } from "./compare.ts";
9
+
10
+ export type Access = "executed" | "read" | "written" | "read-written" | "untouched";
11
+
12
+ export interface SeparationInput {
13
+ segments: Range[];
14
+ /** 64 KiB with the image bytes at their addresses. */
15
+ memory: Uint8Array;
16
+ entry?: number;
17
+ /** The stages of an unpacker capture, in order; the last one is the program. */
18
+ stages?: Array<{ entry: number; wrote: Range[] }>;
19
+ /** Joined coverage files (D40). */
20
+ coverage?: Uint8Array;
21
+ codeRegions: Array<Range & { type: string }>;
22
+ references: Array<{ from: number; to: number; kind: string }>;
23
+ }
24
+
25
+ export interface EvidenceRange extends Range {
26
+ /** The last stage that wrote the range; absent when no stage wrote it. */
27
+ stage?: number;
28
+ access?: Access;
29
+ }
30
+
31
+ export interface CodeRegionEvidence extends Range {
32
+ type: string;
33
+ /** Absent when the image has no entry. */
34
+ reachable?: boolean;
35
+ /** Absent without coverage. */
36
+ executed?: boolean;
37
+ }
38
+
39
+ export interface Separation {
40
+ ranges: EvidenceRange[];
41
+ codeRegions: CodeRegionEvidence[];
42
+ /**
43
+ * RAM outside the image and outside pages 0-3, from the coverage: `read`
44
+ * and `executed` the program never wrote (a loader or an earlier stage put
45
+ * those bytes there), and `generated` it wrote and then executed, in
46
+ * pieces of code with gaps of up to two operand bytes.
47
+ */
48
+ outside: { read: Range[]; executed: Range[]; generated: Range[] };
49
+ }
50
+
51
+ const SYSTEM_PAGES_END = 0x03ff;
52
+
53
+ /** Joins addresses in order into ranges; `gap` is the most bytes between two addresses in one range. */
54
+ function merge(addresses: number[], gap = 0, joins: (from: number, to: number) => boolean = () => true): Range[] {
55
+ const ranges: Range[] = [];
56
+ for (const address of addresses) {
57
+ const last = ranges.at(-1);
58
+ if (last !== undefined && address - last.end - 1 <= gap && joins(last.end, address)) last.end = address;
59
+ else ranges.push({ start: address, end: address });
60
+ }
61
+ return ranges;
62
+ }
63
+
64
+ export function separate(input: SeparationInput): Separation {
65
+ const inImage = (address: number) => input.segments.some((segment) => address >= segment.start && address <= segment.end);
66
+ const executed = new Uint8Array(0x10000);
67
+ if (input.coverage !== undefined) for (const range of executedRamCode(input.coverage, 0, input.memory)) executed.fill(1, range.start, range.end + 1);
68
+ const accessAt = (address: number): Access | undefined => {
69
+ if (input.coverage === undefined) return undefined;
70
+ if (executed[address] === 1) return "executed";
71
+ const flags = input.coverage[address]!;
72
+ const read = (flags & COVERAGE.ramRead) !== 0;
73
+ const written = (flags & COVERAGE.ramWrite) !== 0;
74
+ return read && written ? "read-written" : read ? "read" : written ? "written" : "untouched";
75
+ };
76
+ const stageAt = (address: number): number | undefined => {
77
+ const stages = input.stages ?? [];
78
+ for (let index = stages.length - 1; index >= 0; index--) if (stages[index]!.wrote.some((range) => address >= range.start && address <= range.end)) return index;
79
+ return undefined;
80
+ };
81
+
82
+ const ranges: EvidenceRange[] = [];
83
+ for (const segment of input.segments) {
84
+ for (let address = segment.start; address <= segment.end; address++) {
85
+ const stage = stageAt(address);
86
+ const access = accessAt(address);
87
+ const last = ranges.at(-1);
88
+ if (last !== undefined && last.end + 1 === address && last.stage === stage && last.access === access) {
89
+ last.end = address;
90
+ continue;
91
+ }
92
+ ranges.push({ start: address, end: address, ...(stage === undefined ? {} : { stage }), ...(access === undefined ? {} : { access }) });
93
+ }
94
+ }
95
+
96
+ // Reachability: from the region that holds the entry, through call and jump references, to a fixed point.
97
+ const regions = input.codeRegions.filter((region) => input.segments.some((segment) => region.start <= segment.end && segment.start <= region.end));
98
+ const holding = (address: number) => regions.findIndex((region) => address >= region.start && address <= region.end);
99
+ const reached = new Set<number>();
100
+ if (input.entry !== undefined && holding(input.entry) >= 0) reached.add(holding(input.entry));
101
+ const flows = input.references.filter((reference) => reference.kind === "call" || reference.kind === "jump");
102
+ for (let grown = true; grown; ) {
103
+ grown = false;
104
+ for (const flow of flows) {
105
+ const from = holding(flow.from);
106
+ const to = holding(flow.to);
107
+ if (from >= 0 && to >= 0 && reached.has(from) && !reached.has(to)) {
108
+ reached.add(to);
109
+ grown = true;
110
+ }
111
+ }
112
+ }
113
+ const codeRegions = regions.map((region, index): CodeRegionEvidence => {
114
+ let ran: boolean | undefined;
115
+ if (input.coverage !== undefined) {
116
+ ran = false;
117
+ for (let address = region.start; address <= region.end && !ran; address++) ran = executed[address] === 1;
118
+ }
119
+ return { start: region.start, end: region.end, type: region.type, ...(input.entry === undefined ? {} : { reachable: reached.has(index) }), ...(ran === undefined ? {} : { executed: ran }) };
120
+ });
121
+
122
+ const read: number[] = [];
123
+ const ranOutside: number[] = [];
124
+ const generated: number[] = [];
125
+ if (input.coverage !== undefined) {
126
+ for (let address = SYSTEM_PAGES_END + 1; address < 0x10000; address++) {
127
+ if (inImage(address)) continue;
128
+ const written = (input.coverage[address]! & COVERAGE.ramWrite) !== 0;
129
+ if (executed[address] === 1) (written ? generated : ranOutside).push(address);
130
+ else if (!written && (input.coverage[address]! & COVERAGE.ramRead) !== 0) read.push(address);
131
+ }
132
+ }
133
+ // The image has no bytes of generated code, so only its opcode bytes count as executed. VICE marks only
134
+ // opcode bytes: a gap of at most two operand bytes outside the image stays inside one piece of code (F-28).
135
+ const outsideImage = (from: number, to: number) => {
136
+ for (let address = from + 1; address < to; address++) if (inImage(address)) return false;
137
+ return true;
138
+ };
139
+ return { ranges, codeRegions, outside: { read: merge(read), executed: merge(ranOutside), generated: merge(generated, 2, outsideImage) } };
140
+ }
@@ -0,0 +1,144 @@
1
+ ---
2
+ name: c64-reverse-engineering
3
+ description: Use this skill to reverse engineer a C64 program or a part of it. The work goes from an unknown disk or file to documented understanding or a rebuilt program. It decides what to learn next and which specialist skill does that work. Use it for questions such as "what does this game do", "find the main loop", "understand $2100" or "rebuild this program".
4
+ ---
5
+
6
+ # c64-reverse-engineering
7
+
8
+ ## Purpose
9
+
10
+ Coordinate the reverse engineering of a C64 program. Decide the next unknown to examine and the specialist skill that examines it.
11
+
12
+ This skill does not do the specialist work itself.
13
+
14
+ ## Inputs
15
+
16
+ - The subject: a disk image or a program file in the project, or a program that runs in the emulator.
17
+ - The scope: the question or the target. Examples:
18
+ - identify the main loop
19
+ - understand the routine at `$2100`
20
+ - find the IRQ handler
21
+ - reconstruct the input system
22
+ - reverse engineer and rebuild the whole program
23
+ - The current project knowledge.
24
+
25
+ ## Tools / execution path
26
+
27
+ This skill uses the other skills. It does not use the host runtime or the emulator directly.
28
+
29
+ ## Workflow
30
+
31
+ Select the path for the scope:
32
+
33
+ - A narrow scope, for example one routine, the IRQ handler or the input system: use the loop.
34
+ - A full rebuild of the program: use the stages.
35
+
36
+ ### The loop
37
+
38
+ 1. Write down the scope. Stop when the scope is complete.
39
+ 2. Read the current knowledge for the subject with the c64-knowledge skill. Do not examine again what the knowledge already explains.
40
+ 3. Find the next unknown or blocker, and use the specialist for it:
41
+
42
+ | Unknown | Skill |
43
+ |---|---|
44
+ | The files on a disk image, or a file to extract | c64-disk |
45
+ | A BASIC program, and where it starts machine code | c64-basic |
46
+ | A program that can be packed or crunched | c64-unpacker |
47
+ | What the program does at run time: values, input, timing, screen | c64-emulator |
48
+ | The routines, code, data and references of the program | c64-static-analysis |
49
+ | The platform meaning of an address or a register | c64-memory-map |
50
+ | Differences between releases of the program | c64-provenance |
51
+ | Source code from the knowledge | c64-source |
52
+ | Source code to build | c64-assembler |
53
+ | Proof that a rebuild behaves as the original | c64-testing |
54
+
55
+ 4. Interpret the result. Keep observed facts apart from your conclusions.
56
+ 5. Record each conclusion that the evidence supports with the c64-knowledge skill: names of routines and variables, data classifications, comments.
57
+ 6. Go back to step 3 until the scope is complete.
58
+
59
+ ### The stages of a full rebuild
60
+
61
+ Do the stages in this sequence. Go to the next stage only when its exit check is true, or when the user accepts an exception. Write each exception in the report.
62
+
63
+ | Stage | Work | Exit check |
64
+ |---|---|---|
65
+ | 1. Acceptance | Ask the user for the criteria of the rebuild. Offer these: identical source, a verified loader, passed c64-testing scenarios, a list of the code that no run executed. Write the criteria in `report.md`. | The user gave the criteria. |
66
+ | 2. Get | c64-disk: list the disk. Extract the start file into `original/`. | The start file is in the project. |
67
+ | 3. Start | c64-basic: find the SYS address of the BASIC start. | You have the entry address. |
68
+ | 4. Unpack | c64-unpacker: examine the program. Capture a packed program into `unpacked/`. | `image.json` and `entry_state.json` exist, or the evidence shows no packer. |
69
+ | 5. Run | c64-emulator and the run of c64-testing: run each part that the scope names. Write a coverage file for each part into `coverage/`. | Each part that the scope names has a coverage file. |
70
+ | 6. Analyze | c64-static-analysis with `--coverage` for each coverage file. | No coverage conflict is open. |
71
+ | 7. Name | c64-knowledge and c64-memory-map: name each routine in the scope and give it a comment. Record the modules. | Each routine in the scope has a name and a comment. |
72
+ | 8. Generate | c64-source with `--coverage` into `src/`. For an unpacked program, add `--loader`. | `check.identical` is true. For an unpacked program, `loader.verified` is true. |
73
+ | 9. Build and test | c64-assembler with `--compare` into `build/`. c64-testing scenarios in `tests/`. | The criteria of stage 1 are true. |
74
+ | 10. Report | Write `report.md`. | The report has all its sections. |
75
+
76
+ In stage 5, if you cannot get to a part of the program, ask the user how to get to it. Do stages 7 and 8 again until the source is clear. Each run of c64-source writes the source again.
77
+
78
+ ### Deliverables of a full rebuild
79
+
80
+ - `.c64-re-tools/knowledge.db` with the names, regions, comments and modules
81
+ - the image and the entry state
82
+ - the coverage files
83
+ - the generated source
84
+ - the built program and the loader
85
+ - the c64-testing scenarios and their results
86
+ - `report.md`.
87
+
88
+ ### The report
89
+
90
+ `report.md` is short. It has these sections:
91
+
92
+ 1. Acceptance: each criterion of stage 1 and its result, and each exception that the user accepted.
93
+ 2. The program: what the program does, in a few sentences.
94
+ 3. Modules: the file, the address range and the title of each module.
95
+ 4. Untested code: all ranges in the `not_executed.json` file of c64-source. Tell why no run executed them, if you know.
96
+ 5. Open questions: what the evidence does not explain.
97
+
98
+ Keep observed facts apart from your conclusions.
99
+
100
+ ### Project layout
101
+
102
+ If the project has no layout, use this one. It is a proposal. No script checks it. Each script takes the paths that you give.
103
+
104
+ ```
105
+ original/ the disk image or the program as you got it
106
+ unpacked/ image.json, image.bin and entry_state.json from c64-unpacker
107
+ coverage/ coverage files from c64_memmap
108
+ src/ the generated source from c64-source
109
+ build/ programs from c64-assembler
110
+ tests/ c64-testing scenarios and their results
111
+ report.md the report
112
+ ```
113
+
114
+ If the project has a layout, use that layout.
115
+
116
+ ## Knowledge
117
+
118
+ Read:
119
+ - the knowledge for the addresses, ranges and routines in the scope
120
+ - the history of a conclusion that is surprising or that a person disputes.
121
+
122
+ Automatic write:
123
+ - none directly.
124
+
125
+ Record durable conclusions with the c64-knowledge skill. Do not store:
126
+ - the state of the current investigation
127
+ - a name only because a routine needs a temporary name
128
+ - guesses.
129
+
130
+ ## Result
131
+
132
+ A clear answer for the scope, with the durable conclusions in the project knowledge. For a full rebuild, the deliverables and the report, with the criteria of the user met or each exception in the report.
133
+
134
+ ## Failure and conflicts
135
+
136
+ - A skill reports `installation-incomplete`, or `machine-unavailable` with a message that the host runtime does not run: stop the whole task. Give the message to the user and wait for the user. Do not continue with other skills. Do not make a replacement for the tool or the host runtime, for example your own disk parser, disassembler or emulator script.
137
+ - If the evidence does not support a conclusion, keep the unknown and get more evidence. Do not make up a meaning.
138
+ - Before you replace current knowledge from the user or the LLM, read its history and the evidence for it.
139
+ - A conflict from the static analysis: find the correct answer at run time with the c64-emulator skill.
140
+ - A test that is INCONCLUSIVE does not show a defect. Find why the test could not compare.
141
+
142
+ ## Handoffs
143
+
144
+ All specialist skills in the table above.
@@ -0,0 +1,126 @@
1
+ ---
2
+ name: c64-source
3
+ description: Use this skill to generate ACME source code for a C64 program from its image and the project knowledge. The script runs Ghidra for the instructions and writes the source files. It checks with ACME that the source assembles to the same bytes as the image. It can also write a loader that sets the entry state of an unpacked program. Use it to make a rebuild. Do not edit the generated files. Change the knowledge and generate again.
4
+ ---
5
+
6
+ # c64-source
7
+
8
+ ## Purpose
9
+
10
+ Generate ACME source from an image and the knowledge. The same knowledge gives the same source. Make the source better through the knowledge: names, comments, regions and modules. Then generate again.
11
+
12
+ This skill does not change the knowledge.
13
+
14
+ ## Inputs
15
+
16
+ - The image: an `image.json` from c64-unpacker capture, or a PRG. The path is relative to the project directory.
17
+ - The output directory for the source files, relative to the project directory.
18
+ - Optional entry points for Ghidra.
19
+ - For the loader: the `entry_state.json` next to `image.json`.
20
+
21
+ ## Tools / execution path
22
+
23
+ The script runs Ghidra and ACME on this machine. With `--loader`, it also runs the loader in the emulator through the host runtime.
24
+
25
+ ```
26
+ node <skill>/scripts/generate.ts unpacked/image.json --out src
27
+ node <skill>/scripts/generate.ts game.prg --out src --entry '$080d'
28
+ node <skill>/scripts/generate.ts unpacked/image.json --out src --loader
29
+ ```
30
+
31
+ | Option | Meaning |
32
+ |---|---|
33
+ | `--out src` | The directory for the source files. The script writes over the files that it generated before. |
34
+ | `--entry '$080d'` | An entry point for Ghidra. Use it again for more entry points. The entry of `image.json`, the routines and the code regions in the knowledge are entry points too. |
35
+ | `--coverage coverage/attract.bin` | A coverage file of 64 KiB from `c64_memmap` with `out`. Use it again for more runs. Code that no run executed gets the line `; not executed` with its range. `notExecuted` lists it. |
36
+ | `--space drive8` | Generate the source of drive code or a cartridge bank from the knowledge of that space. `--coverage` and `--loader` are only for `c64`, and platform names are only for `c64`. |
37
+ | `--loader` | Also write `loader.asm` and `loader.prg`. This needs `image.json` with an entry, and `entry_state.json`. |
38
+
39
+ ## Workflow
40
+
41
+ 1. Get the image. For a packed program, use c64-unpacker capture. For a program without a packer, use the PRG.
42
+ 2. Run the script.
43
+ 3. Read `check`. `identical: true` tells that the source assembles to the image byte by byte.
44
+ 4. Read the source. A label `l_xxxx` is an address that has no name in the knowledge.
45
+ 5. Record names, comments, regions and modules with the c64-knowledge skill. Use `apply` for many changes.
46
+ 6. Run the script again. Do steps 4 to 6 again until the source is clear.
47
+ 7. Build the program with the c64-assembler skill: `--source-root src --entry main.asm`.
48
+ 8. For an unpacked program, run the script with `--loader` at the end. Test `loader.prg` against the original with the c64-testing skill.
49
+
50
+ ## The source
51
+
52
+ - `main.asm` sets the address of each segment and includes the files in address order.
53
+ - Each module in the knowledge is one file. A module starts at its address and stops at the next module or at the end of its segment. Without modules, each segment is one file, `segment_xxxx.asm`.
54
+ - `constants.asm` holds the names of addresses outside the image, for example `EXTCOL = $d020`.
55
+ - A label or a constant gets the name of the address in the knowledge. Without a knowledge name, it gets the platform name of the c64-memory-map skill, for example `EXTCOL` or `CHROUT`. A platform name wins over a name from an analyzer (`dxa`, `ghidra` or `heuristic`), for example `EXTCOL` over `ld020`. A `user` or `llm` name wins over a platform name. An address in the image without a name gets `l_xxxx`.
56
+ - Code regions, and the instructions that Ghidra decodes outside of data regions, become instructions.
57
+ - Data regions become data by their type: `!byte`, `!word`, `!pet`, `!scr`, `!fill`, tables of labels, and sprite bytes in binary.
58
+ - `lda+2 $0012` is an absolute operand below $0100. `+1` is a zero-page operand with a label.
59
+ - `sta NMINV+1` is an address inside a platform entry of more than one byte, for example the high byte of a vector.
60
+ - `name = * + 1` is a label inside an instruction, for example for code that changes its own operand.
61
+ - `!byte` with an instruction as its comment: ACME writes this instruction with a different opcode. The bytes keep the source identical.
62
+ - `not decoded` marks bytes in a code region that Ghidra did not decode.
63
+ - `; not executed $xxxx-$yyyy` starts code that no run of the `--coverage` files executed. The range is the same as in `not_executed.json`.
64
+
65
+ Do not edit the generated files. The next run writes over them.
66
+
67
+ ## The loader
68
+
69
+ `--loader` writes `loader.asm` and assembles it to `loader.prg`. The loader does these steps:
70
+
71
+ 1. `10 SYS 2061` starts it.
72
+ 2. It moves its copier to two free pages. The copier runs with interrupts off and with all RAM visible.
73
+ 3. It copies each segment and the pages $0002-$03ff of the entry state into place. The script selects an order in which no copy writes over the packed bytes of a copy that has not run yet.
74
+ 4. It sets the display registers of the VIC-II and the VIC bank from `io` in the entry state. Then it waits for the raster line of the entry.
75
+ 5. It sets the CIA timers, their latches and the interrupt masks from `io` and `cia` in the entry state. A running timer gets the value that counts down to the timer value of the entry at the entry.
76
+ 6. It sets $00/$01, the stack pointer, A, X, Y and the flags, and jumps to the entry.
77
+
78
+ The flags go through the free stack byte below the stack pointer (`stackByte`). The loader changes this byte. The loader does not set the SID, the time of day of the CIAs, the CIA interrupt flags or the interrupt registers of the VIC-II. `loader.limits` names each CIA value that the loader cannot set as it was at the entry. A program that reads the SID voice 3 for random numbers can still differ in a scenario. A raster sync does not change a timer or the SID. Then observe after the program sets up its own random numbers.
79
+
80
+ The script simulates the copies first. Then it runs `loader.prg` in the emulator to its jump to the entry. It compares the memory, $00/$01, the registers, the display registers of the VIC-II, the VIC bank and the raster line with the entry state there. It also compares the CIA timers, latches and interrupt masks. `loader.verified: true` tells that all of them are equal. An interrupt can run right at the entry, as in the original program.
81
+
82
+ ## Knowledge
83
+
84
+ Read:
85
+ - symbols, regions, comments, references and modules of all origins.
86
+
87
+ Automatic write:
88
+ - none.
89
+
90
+ Do not store:
91
+ - the generated source or its labels `l_xxxx`.
92
+
93
+ ## Result
94
+
95
+ One JSON object:
96
+
97
+ - `files`: the files in the output directory.
98
+ - `instructions`, `dataBytes` and `undecodedBytes`.
99
+ - `labels`: `named` labels from the knowledge and `generated` labels `l_xxxx`.
100
+ - `entriesInData`: each `--entry` in a data region of the knowledge, with the `region` and its `origin`. Ghidra does not decode an entry in a data region.
101
+ - `asBytes`: the instructions that the source has as bytes.
102
+ - `unusedModules`: modules in the knowledge whose start is in no segment of the image. Often this tells that the image does not hold code that the knowledge describes.
103
+ - `entryOutside`: the entry of `image.json` is in no segment of the image, with a `reason`. The image does not hold the program.
104
+ - `notExecuted`: with `--coverage`, the count of instructions that no run executed, the count of their ranges, and the first ranges. `all` names the file `not_executed.json` in `--out`. It has all of the ranges. Put all of these ranges in the report.
105
+ - `check`: `identical`, and the result for each segment. A different segment gives its first different address.
106
+ - `loader`: the program, the copies in their order, the copier page, `stackByte`, `verified` and `differences`. `limits` lists the CIA values that the loader cannot set. When the script cannot make the loader, `loader` has `verified: false` and a `reason`. The loader includes the source. Thus a source that does not assemble to the image also stops the loader.
107
+
108
+ The exit status is 0 when the source is identical and the emulator check of the loader passes. The exit status is 1 for a failed check, for an entry in no segment, and for an error. The exit status is 2 for a wrong argument.
109
+
110
+ ## Failure and conflicts
111
+
112
+ - `check.diagnostics`: ACME did not assemble the source. A name in the knowledge can be wrong for ACME. Read the diagnostics and change the name with c64-knowledge.
113
+ - `check.identical: false`: the script has a defect. Give the result to the user.
114
+ - `invalid-input` that names a symbol or a module: change that symbol or module in the knowledge.
115
+ - `entryOutside`: the run fails, but the script still writes and checks the source. With `--loader`, `loader.verified` is `false` with the same `reason`. Capture the image again with c64-unpacker `loads` or `capture`. Make sure that a segment holds the entry.
116
+ - `entriesInData`: an analyzer region of data hides the code at the entry. If the entry is code, record a code region at the entry with the c64-knowledge skill, for example `classify '$0500' '$05ff' code`. A region of the user or the LLM replaces the analyzer region. Then generate again.
117
+ - `loader.verified: false` with a `reason`: the script did not make the loader. The script still writes and checks the source. When no copy order or no free pages exist, or the loader needs too many copies, the reason gives the ranges. Capture the image again with fewer segments, or write the loader by hand.
118
+ - `loader.verified: false` with `differences`: the loader ran, and `differences` gives the bytes and registers that are not equal.
119
+ - `installation-incomplete`, or `machine-unavailable` with a message that the host runtime does not run: stop the task. Give the message to the user and wait for the user. The message gives the remedy. Do not make a replacement for the tool or the host runtime, for example your own disassembler or emulator script.
120
+
121
+ ## Handoffs
122
+
123
+ - Get an image → c64-unpacker
124
+ - Names, regions, comments and modules → c64-knowledge
125
+ - Build the program → c64-assembler
126
+ - Test the rebuild against the original → c64-testing