@henols/c64-re-tools 2.6.0-rc.2 → 2.6.0-rc.20

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 (107) hide show
  1. package/package.json +2 -2
  2. package/skills/c64-assembler/SKILL.md +28 -14
  3. package/skills/c64-assembler/scripts/assembler.ts +28 -15
  4. package/skills/c64-development/SKILL.md +4 -4
  5. package/skills/c64-docs/SKILL.md +9 -7
  6. package/skills/c64-docs/scripts/docs.ts +24 -11
  7. package/skills/c64-docs/scripts/lib/reference.ts +119 -33
  8. package/skills/c64-docs/scripts/lib/source-map.ts +23 -4
  9. package/skills/c64-knowledge/SKILL.md +67 -42
  10. package/skills/c64-knowledge/scripts/knowledge.ts +90 -67
  11. package/skills/c64-knowledge/scripts/lib/listing.ts +18 -3
  12. package/skills/c64-packer/SKILL.md +1 -1
  13. package/skills/c64-provenance/SKILL.md +11 -10
  14. package/skills/c64-provenance/scripts/lib/library.ts +16 -16
  15. package/skills/c64-provenance/scripts/provenance.ts +33 -23
  16. package/skills/c64-reverse-engineering/SKILL.md +17 -11
  17. package/skills/c64-source/SKILL.md +71 -47
  18. package/skills/c64-source/scripts/lib/export.ts +89 -34
  19. package/skills/c64-source/scripts/lib/loader.ts +79 -36
  20. package/skills/c64-source/scripts/lib/source.ts +38 -32
  21. package/skills/c64-source/scripts/lib/stage.ts +11 -5
  22. package/skills/c64-source/scripts/lib/start-screen.asm +4 -2
  23. package/skills/c64-source/scripts/source.ts +278 -145
  24. package/skills/c64-static-analysis/SKILL.md +29 -25
  25. package/skills/c64-static-analysis/scripts/lib/coverage.ts +2 -25
  26. package/skills/c64-static-analysis/scripts/lib/findings.ts +11 -11
  27. package/skills/c64-static-analysis/scripts/lib/instructions.ts +26 -24
  28. package/skills/c64-static-analysis/scripts/lib/macros.ts +3 -1
  29. package/skills/c64-static-analysis/scripts/static-analysis.ts +69 -67
  30. package/skills/c64-testing/SKILL.md +15 -5
  31. package/skills/c64-testing/references/recording-format.md +26 -5
  32. package/skills/c64-testing/references/scenario-format.md +11 -8
  33. package/skills/c64-testing/scripts/lib/diff-files.ts +4 -18
  34. package/skills/c64-testing/scripts/lib/play.ts +39 -41
  35. package/skills/c64-testing/scripts/lib/recording.ts +220 -46
  36. package/skills/c64-testing/scripts/lib/replay.ts +32 -6
  37. package/skills/c64-testing/scripts/lib/runs.ts +8 -3
  38. package/skills/c64-testing/scripts/lib/scenario.ts +18 -11
  39. package/skills/c64-testing/scripts/testing.ts +23 -2
  40. package/skills/c64-unpacker/SKILL.md +10 -5
  41. package/skills/c64-unpacker/scripts/lib/loads.ts +34 -13
  42. package/skills/c64-unpacker/scripts/lib/machine.ts +7 -3
  43. package/skills/c64-unpacker/scripts/lib/stages.ts +18 -3
  44. package/skills/c64-unpacker/scripts/unpacker.ts +43 -9
  45. package/skills/emulator/SKILL.md +7 -12
  46. package/src/build-name.ts +55 -0
  47. package/src/c64-keys.ts +98 -0
  48. package/src/c64.ts +22 -2
  49. package/src/cli/main.ts +3 -29
  50. package/src/host/main.ts +3 -3
  51. package/src/host/server.ts +3 -1
  52. package/src/host/tools/index.ts +2 -7
  53. package/src/host/vice/process.ts +34 -17
  54. package/src/host/vice/session.ts +138 -31
  55. package/src/host-client/steps.ts +21 -18
  56. package/src/host-client/tools.ts +2 -7
  57. package/src/host-client/vice-session.ts +7 -1
  58. package/src/knowledge/evidence.ts +66 -0
  59. package/src/knowledge/history.ts +5 -5
  60. package/src/knowledge/import.ts +12 -5
  61. package/src/knowledge/program.ts +123 -32
  62. package/src/knowledge/read.ts +36 -13
  63. package/src/knowledge/schema.ts +43 -17
  64. package/src/knowledge/start-screen.ts +70 -0
  65. package/src/knowledge/text.ts +33 -5
  66. package/src/knowledge/write.ts +75 -36
  67. package/src/mcp/inputs.ts +12 -0
  68. package/src/mcp/server.ts +1 -1
  69. package/src/mcp/tools/debug.ts +23 -21
  70. package/src/mcp/tools/input.ts +38 -5
  71. package/src/mcp/tools/machine.ts +3 -42
  72. package/src/mcp/tools/media.ts +1 -1
  73. package/src/mcp/tools/memory.ts +12 -9
  74. package/src/native/acme.ts +1 -1
  75. package/src/native/c64run.ts +7 -4
  76. package/src/native/decompiler/analyze.ts +98 -0
  77. package/src/native/decompiler/c64rt-wasm.ts +4 -0
  78. package/src/native/decompiler/language/c64rt_6510.sla +0 -0
  79. package/src/native/{ghidra → decompiler}/language/c64rt_6510.slaspec +1 -1
  80. package/src/native/decompiler/pcode.ts +222 -0
  81. package/src/native/decompiler/walk.ts +205 -0
  82. package/src/native/decompiler/wasm.ts +98 -0
  83. package/src/native/discover.ts +3 -3
  84. package/src/native/dxa.ts +12 -1
  85. package/src/native/processes.ts +4 -4
  86. package/src/native/types.ts +27 -27
  87. package/src/opcodes.ts +1 -1
  88. package/src/platform.ts +30 -1
  89. package/src/project.ts +17 -4
  90. package/src/protocol/tools.ts +1 -45
  91. package/src/protocol/vice.ts +47 -0
  92. package/src/reference-pages.ts +64 -0
  93. package/src/script.ts +4 -4
  94. package/src/vsf.ts +98 -0
  95. package/tools/c64run.c +158 -15
  96. package/tools/install-container.sh +1 -2
  97. package/src/cli/status.ts +0 -127
  98. package/src/host/tools/status.ts +0 -47
  99. package/src/native/ghidra/analyze.ts +0 -87
  100. package/src/native/ghidra/index.ts +0 -170
  101. package/src/native/ghidra/scripts/C64Export.java +0 -274
  102. package/src/native/ghidra/scripts/C64Prepare.java +0 -66
  103. package/src/native/status.ts +0 -135
  104. package/tools/install-ghidra.sh +0 -40
  105. /package/src/native/{ghidra → decompiler}/language/c64rt_6510.cspec +0 -0
  106. /package/src/native/{ghidra → decompiler}/language/c64rt_6510.ldefs +0 -0
  107. /package/src/native/{ghidra → decompiler}/language/c64rt_6510.pspec +0 -0
@@ -30,7 +30,7 @@ export const c64Autostart = defineTool({
30
30
  "Without index, a disk image loads like LOAD\"*\",8,1. index selects one directory entry by its number in the directory list of the disk (0 is the first entry). " +
31
31
  "The tool refuses an index of a DEL entry, or an index after the last entry. " +
32
32
  "The machine runs when this tool returns, and the load continues. The emulator keeps warp mode on until it types RUN. " +
33
- "A key in that time stops the autostart, so c64_keyboard refuses keys until then. Run frames with c64_execution advance-frames until c64_status gives warp false. " +
33
+ "A key in that time stops the autostart, so c64_keyboard refuses keys until then. Run frames with c64_execution advance-frames, then type again. " +
34
34
  "The c64_timing stopwatch starts again from zero.",
35
35
  inputSchema: z
36
36
  .object({
@@ -186,7 +186,7 @@ export const c64Disassemble = defineTool({
186
186
  description:
187
187
  `Disassemble count instructions (1 to ${MAX_DISASSEMBLE}) from an address, as the bytes are in the live machine now. ` +
188
188
  "Use it to read code that a program changed or unpacked at run time. It includes the undocumented opcodes. " +
189
- `With out, it disassembles up to ${MAX_DISASSEMBLE_OUT} instructions, or to $ffff, and writes them as a listing to that project file. The result then gives the file in place of the instructions. ` +
189
+ `With out, it disassembles up to ${MAX_DISASSEMBLE_OUT} instructions, or to $ffff, and writes them as a listing to that project file. The result then gives the file in place of the instructions. A file name that ends in .json gets a JSON list of the instructions, as the result without out gives them. ` +
190
190
  "This does not change the run state of the machine.",
191
191
  inputSchema: z
192
192
  .object({
@@ -208,24 +208,27 @@ export const c64Disassemble = defineTool({
208
208
  const result = await session.disassemble(request);
209
209
  return { instructions: result.instructions.map((instruction) => ({ ...instruction, address: formatC64Address(instruction.address) })) };
210
210
  }
211
- const lines: string[] = [];
211
+ const instructions: Array<{ address: string; bytes: string; text: string }> = [];
212
212
  let address = request.address;
213
213
  // Ends at count, or where the disassembly would wrap past $ffff.
214
214
  let wrapped = false;
215
- while (!wrapped && lines.length < request.count && address <= 0xffff) {
216
- const { instructions } = await session.disassemble({ ...request, address, count: Math.min(MAX_DISASSEMBLE, request.count - lines.length) });
217
- if (instructions.length === 0) break;
218
- for (const instruction of instructions) {
219
- if (lines.length === request.count) break;
215
+ while (!wrapped && instructions.length < request.count && address <= 0xffff) {
216
+ const part = await session.disassemble({ ...request, address, count: Math.min(MAX_DISASSEMBLE, request.count - instructions.length) });
217
+ if (part.instructions.length === 0) break;
218
+ for (const instruction of part.instructions) {
219
+ if (instructions.length === request.count) break;
220
220
  if (instruction.address < address) {
221
221
  wrapped = true;
222
222
  break;
223
223
  }
224
- lines.push(`${formatC64Address(instruction.address)} ${spaced(instruction.bytes).padEnd(9)} ${instruction.text}`);
224
+ instructions.push({ ...instruction, address: formatC64Address(instruction.address) });
225
225
  address = instruction.address + instruction.bytes.length / 2;
226
226
  }
227
227
  }
228
- return { count: lines.length, ...writeOut(out, linesText(lines), project) };
228
+ const text = out.toLowerCase().endsWith(".json")
229
+ ? `${JSON.stringify(instructions, null, 1)}\n`
230
+ : linesText(instructions.map((instruction) => `${instruction.address} ${spaced(instruction.bytes).padEnd(9)} ${instruction.text}`));
231
+ return { count: instructions.length, ...writeOut(out, text, project) };
229
232
  },
230
233
  });
231
234
 
@@ -19,7 +19,7 @@ export function showSymbol(symbol: AssembledSymbol) {
19
19
  : { name: symbol.name, kind: "constant", value: `$${symbol.value.toString(16).padStart(4, "0")}`, used: symbol.used };
20
20
  }
21
21
 
22
- /** The text of a symbols file (--out-symbols): the symbols of a build, which c64-testing reads for the names of a program. */
22
+ /** The text of a symbols file (symbols/<build>.json next to the program): the symbols of a build, which c64-testing reads for the names of a program. */
23
23
  export const symbolsFileText = (symbols: readonly AssembledSymbol[]) => `${JSON.stringify({ symbols: symbols.map(showSymbol) }, null, 1)}\n`;
24
24
 
25
25
  const PROGRAM = "program.prg";
@@ -12,7 +12,7 @@ import type { SupervisedProcess } from "./processes.ts";
12
12
  import type { ToolContext } from "./run.ts";
13
13
 
14
14
  /** The command protocol that this module speaks; c64run --version and version name it. */
15
- export const C64RUN_PROTOCOL = 3;
15
+ export const C64RUN_PROTOCOL = 7;
16
16
 
17
17
  /** CPU cycles in one PAL frame: 312 lines of 63 cycles. */
18
18
  export const PAL_FRAME_CYCLES = 19_656;
@@ -22,15 +22,18 @@ const STOP_MS = 5_000;
22
22
  const COMMAND_MS = 30_000;
23
23
  /** The slowest run that a command limit allows for: c64run runs about 6 million cycles per second. */
24
24
  const SLOWEST_CYCLES_PER_SECOND = 1_000_000;
25
+ /** The argument of each run command that is the absolute tick it runs to at most: run <tick>, run-pc <pc> <tick> [<n>], run-reads <pc> <addr> <n> <tick>. */
26
+ const UNTIL_ARGUMENT: Record<string, number> = { run: 0, "run-pc": 1, "run-reads": 3 };
25
27
 
26
28
  /**
27
29
  * The time limit of a command, so that a core that hangs cannot keep the
28
- * script running. run, run-pc and run-reads end with the absolute tick that
29
- * they run to at most; the others answer at once.
30
+ * script running. A run command gets the time of the cycles up to the tick
31
+ * that it runs to at most; the others answer at once.
30
32
  */
31
33
  export function commandLimitMs(text: string): number {
32
34
  const [name, ...rest] = text.split(" ");
33
- const target = name === "run" || name === "run-pc" || name === "run-reads" ? Number(rest.at(-1)) : 0;
35
+ const at = UNTIL_ARGUMENT[name!];
36
+ const target = at === undefined ? 0 : Number(rest[at]);
34
37
  return COMMAND_MS + (Number.isFinite(target) ? (target * 1000) / SLOWEST_CYCLES_PER_SECOND : 0);
35
38
  }
36
39
 
@@ -0,0 +1,98 @@
1
+ // The decompiler analysis: the Ghidra SLEIGH decoder and decompiler with the NMOS
2
+ // 6510 language, in this process as a WebAssembly module. Knowledge seeds go
3
+ // in, the structural result comes out, and the result is checked completely
4
+ // before the script imports it.
5
+
6
+ import { describeError, WireFailure } from "../../protocol/messages.ts";
7
+ import { checkDecompilerResult, checkRequestBounds, DECOMPILER_LIMITS, imageRange, seedsInside, type DecompilerParams, type DecompilerResult } from "../types.ts";
8
+ import type { ToolContext } from "../run.ts";
9
+ import { walk } from "./walk.ts";
10
+ import { decompile, loadImage, setName, SLEIGH_VERSION } from "./wasm.ts";
11
+
12
+ /** The longest reason that the result check accepts. */
13
+ const MAX_REASON = 1000;
14
+
15
+ export async function analyze(
16
+ params: DecompilerParams,
17
+ image: Buffer,
18
+ context: ToolContext,
19
+ ): Promise<{ result: DecompilerResult }> {
20
+ checkRequestBounds(params);
21
+ const { start, end, body } = imageRange(params.imageKind, image);
22
+
23
+ // The decoder sees only the loaded bytes; labels may name any address.
24
+ const { entryPoints, codeStarts, dataRanges } = seedsInside(params, start, end);
25
+ if (entryPoints.length === 0) {
26
+ throw new WireFailure("invalid-input", "Give at least one entry point inside the program, for example the SYS address of its BASIC loader.");
27
+ }
28
+ context.signal.throwIfAborted();
29
+
30
+ let walked: ReturnType<typeof walk>;
31
+ try {
32
+ loadImage(body, start);
33
+ walked = walk(body, start, end, { entryPoints, codeStarts, dataRanges, labels: params.labels });
34
+ } catch (error) {
35
+ context.log?.(`The decoder failed: ${describeError(error)}`);
36
+ throw new WireFailure("operation-failed", `The decoder failed: ${describeError(error)}. Nothing was imported.`);
37
+ }
38
+ const { decompilations, notDecompiled } = decompileRoutines(params, walked.functions, body, start, context);
39
+
40
+ let result: DecompilerResult;
41
+ try {
42
+ result = checkDecompilerResult({ ...walked, decompilations, notDecompiled, completeness: { functions: true, regions: true, references: true }, toolVersion: SLEIGH_VERSION });
43
+ } catch (error) {
44
+ context.log?.(`Decompiler result rejected: ${describeError(error)}`);
45
+ throw new WireFailure("operation-failed", `The decompiler returned an incomplete or inconsistent result. Nothing was imported. The check found: ${describeError(error)}.`);
46
+ }
47
+ return { result };
48
+ }
49
+
50
+ /** The requested routines as C text, within the character bounds; the labels name routines and data. */
51
+ function decompileRoutines(params: DecompilerParams, functions: DecompilerResult["functions"], body: Uint8Array, start: number, context: ToolContext): Pick<DecompilerResult, "decompilations" | "notDecompiled"> {
52
+ const decompilations: DecompilerResult["decompilations"] = [];
53
+ const notDecompiled: NonNullable<DecompilerResult["notDecompiled"]> = [];
54
+ if (params.decompile.length === 0) return { decompilations, notDecompiled };
55
+ const entries = new Set(functions.map((fn) => fn.entry));
56
+ // A trap in the decompiler drops its image: the next routine loads it again.
57
+ let prepared = false;
58
+ const prepare = () => {
59
+ if (prepared) return;
60
+ loadImage(body, start);
61
+ for (const label of params.labels) {
62
+ try {
63
+ setName(label.address, label.name, entries.has(label.address) ? "function" : "data");
64
+ } catch (error) {
65
+ // The decompiler then prints its own name for the address.
66
+ context.log?.(`The decompiler refused the name ${label.name}: ${describeError(error)}`);
67
+ }
68
+ }
69
+ prepared = true;
70
+ };
71
+ let remaining = DECOMPILER_LIMITS.decompiledTotalChars;
72
+ for (const entry of params.decompile) {
73
+ if (!entries.has(entry)) {
74
+ notDecompiled.push({ entry, reason: "no function starts at this address" });
75
+ continue;
76
+ }
77
+ if (remaining <= 0) {
78
+ notDecompiled.push({ entry, reason: "the decompiled text of the other routines used the whole limit" });
79
+ continue;
80
+ }
81
+ let text: string;
82
+ try {
83
+ prepare();
84
+ text = decompile(entry);
85
+ } catch (error) {
86
+ prepared = false;
87
+ context.log?.(`The decompiler failed at ${entry}: ${describeError(error)}`);
88
+ notDecompiled.push({ entry, reason: describeError(error).slice(0, MAX_REASON) || "the decompiler gave no reason" });
89
+ continue;
90
+ }
91
+ const limit = Math.min(DECOMPILER_LIMITS.decompiledChars, remaining);
92
+ const truncated = text.length > limit;
93
+ if (truncated) text = text.slice(0, limit);
94
+ remaining -= text.length;
95
+ decompilations.push({ entry, text, truncated });
96
+ }
97
+ return { decompilations, notDecompiled };
98
+ }