@henols/c64-re-tools 2.5.1-rc.2 → 2.6.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 (87) hide show
  1. package/package.json +2 -2
  2. package/skills/c64-assembler/SKILL.md +26 -12
  3. package/skills/c64-assembler/scripts/assembler.ts +25 -13
  4. package/skills/c64-development/SKILL.md +2 -2
  5. package/skills/c64-docs/SKILL.md +4 -4
  6. package/skills/c64-docs/scripts/docs.ts +7 -7
  7. package/skills/c64-docs/scripts/lib/reference.ts +3 -3
  8. package/skills/c64-docs/scripts/lib/source-map.ts +9 -4
  9. package/skills/c64-knowledge/SKILL.md +29 -20
  10. package/skills/c64-knowledge/scripts/knowledge.ts +33 -39
  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 +16 -10
  17. package/skills/c64-source/SKILL.md +60 -42
  18. package/skills/c64-source/scripts/lib/export.ts +76 -32
  19. package/skills/c64-source/scripts/lib/loader.ts +26 -7
  20. package/skills/c64-source/scripts/lib/source.ts +21 -24
  21. package/skills/c64-source/scripts/lib/stage.ts +6 -5
  22. package/skills/c64-source/scripts/source.ts +213 -106
  23. package/skills/c64-static-analysis/SKILL.md +7 -5
  24. package/skills/c64-static-analysis/scripts/lib/coverage.ts +2 -25
  25. package/skills/c64-static-analysis/scripts/lib/instructions.ts +22 -20
  26. package/skills/c64-static-analysis/scripts/static-analysis.ts +36 -43
  27. package/skills/c64-testing/SKILL.md +5 -2
  28. package/skills/c64-testing/references/recording-format.md +18 -3
  29. package/skills/c64-testing/references/scenario-format.md +7 -6
  30. package/skills/c64-testing/scripts/lib/diff-files.ts +4 -18
  31. package/skills/c64-testing/scripts/lib/play.ts +39 -41
  32. package/skills/c64-testing/scripts/lib/recording.ts +46 -37
  33. package/skills/c64-testing/scripts/lib/replay.ts +1 -1
  34. package/skills/c64-testing/scripts/lib/runs.ts +2 -2
  35. package/skills/c64-testing/scripts/lib/scenario.ts +17 -10
  36. package/skills/c64-unpacker/SKILL.md +4 -1
  37. package/skills/c64-unpacker/scripts/lib/loads.ts +34 -13
  38. package/skills/c64-unpacker/scripts/unpacker.ts +5 -3
  39. package/skills/emulator/SKILL.md +7 -12
  40. package/src/build-name.ts +50 -0
  41. package/src/c64-keys.ts +98 -0
  42. package/src/c64.ts +20 -0
  43. package/src/cli/main.ts +3 -29
  44. package/src/host/main.ts +1 -1
  45. package/src/host/tools/index.ts +1 -6
  46. package/src/host/vice/session.ts +88 -9
  47. package/src/host-client/steps.ts +17 -17
  48. package/src/host-client/tools.ts +2 -7
  49. package/src/host-client/vice-session.ts +7 -1
  50. package/src/knowledge/program.ts +44 -1
  51. package/src/knowledge/read.ts +28 -8
  52. package/src/knowledge/schema.ts +33 -9
  53. package/src/knowledge/start-screen.ts +4 -0
  54. package/src/knowledge/text.ts +4 -2
  55. package/src/knowledge/write.ts +59 -25
  56. package/src/mcp/inputs.ts +12 -0
  57. package/src/mcp/server.ts +1 -1
  58. package/src/mcp/tools/input.ts +38 -5
  59. package/src/mcp/tools/machine.ts +3 -42
  60. package/src/mcp/tools/media.ts +1 -1
  61. package/src/native/acme.ts +1 -1
  62. package/src/native/c64run.ts +1 -1
  63. package/src/native/discover.ts +3 -3
  64. package/src/native/dxa.ts +12 -1
  65. package/src/native/ghidra/analyze.ts +74 -63
  66. package/src/native/ghidra/c64rt-wasm.ts +4 -0
  67. package/src/native/ghidra/language/c64rt_6510.sla +0 -0
  68. package/src/native/ghidra/pcode.ts +222 -0
  69. package/src/native/ghidra/walk.ts +205 -0
  70. package/src/native/ghidra/wasm.ts +98 -0
  71. package/src/native/processes.ts +4 -4
  72. package/src/native/types.ts +2 -2
  73. package/src/platform.ts +30 -1
  74. package/src/project.ts +17 -4
  75. package/src/protocol/tools.ts +1 -45
  76. package/src/protocol/vice.ts +47 -0
  77. package/src/script.ts +1 -1
  78. package/src/vsf.ts +98 -0
  79. package/tools/c64run.c +9 -12
  80. package/tools/install-container.sh +1 -2
  81. package/src/cli/status.ts +0 -127
  82. package/src/host/tools/status.ts +0 -47
  83. package/src/native/ghidra/index.ts +0 -170
  84. package/src/native/ghidra/scripts/C64Export.java +0 -274
  85. package/src/native/ghidra/scripts/C64Prepare.java +0 -66
  86. package/src/native/status.ts +0 -135
  87. package/tools/install-ghidra.sh +0 -40
@@ -11,7 +11,7 @@
11
11
  // screen in the knowledge, the copier shows it after the copies and waits for
12
12
  // a key or a fire button: the code is start-screen.asm, the same for each program.
13
13
  // The loader loads the segments without the leftovers: the ranges of the
14
- // source that nothing uses are not in loader.prg.
14
+ // source that nothing uses are not in the loader program.
15
15
 
16
16
  import { readFileSync } from "node:fs";
17
17
  import { fileURLToPath } from "node:url";
@@ -24,6 +24,7 @@ import { isRomFont, startScreenRanges, type StartScreenRow } from "#src/knowledg
24
24
  import { WireFailure } from "#src/host-client/tools.ts";
25
25
  import { pack } from "#src/native/exomizer.ts";
26
26
  import type { ToolContext } from "#src/native/run.ts";
27
+ import type { AssembledSymbol } from "#src/native/types.ts";
27
28
 
28
29
  export interface EntryState {
29
30
  pc: number;
@@ -138,9 +139,7 @@ export function loaderLimits(plan: { cia?: CiaPlan }): string[] {
138
139
 
139
140
  /** The label of the loader's jump to the entry, in ACME's symbol list. */
140
141
  export const LOADER_JUMP = "c64_source_loader_jump";
141
- /** The labels of the start screen: its wait for a key or a fire button, and the code after the wait. */
142
- export const START_SCREEN_WAIT = "loader_start_screen_wait";
143
- export const START_SCREEN_CONTINUE = "loader_start_screen_continue";
142
+ export { START_SCREEN_CONTINUE, START_SCREEN_WAIT } from "#src/knowledge/start-screen.ts";
144
143
  /** The file of the start screen in the generated source. */
145
144
  export const START_SCREEN_FILE = "start_screen.asm";
146
145
  /** The code of the start screen: the same for each program. loader.asm sets its values. */
@@ -652,7 +651,7 @@ const SHOWN_FREE = 8;
652
651
  /**
653
652
  * The packed form of the loader, for an image too large for one loader file:
654
653
  * Exomizer packs the build of main.asm (from $0400) and the build of
655
- * loader.asm into loader.prg, which starts at the copier with all RAM
654
+ * loader.asm into the loader program, which starts at the copier with all RAM
656
655
  * visible ($01 = $34). loader.asm holds only the copier, in one free range,
657
656
  * and the bytes of pages $0002-$03ff, in pieces in the free ranges: the
658
657
  * unpacker of Exomizer uses the zero page and $0334-$03ff, so the copier
@@ -690,7 +689,7 @@ function planPacked(input: LoaderInput, blocks: Block[], cia: ReturnType<typeof
690
689
  const lines: string[] = [
691
690
  ...input.header.map((line) => `; ${line}`),
692
691
  "; The image is too large for one loader file, so this is the packed form. Exomizer packs the",
693
- `; build of main.asm and the build of this file into loader.prg, with --entry '${formatC64Address(copier)}' and --exit-memory '$34'.`,
692
+ `; build of main.asm and the build of this file into the loader program, with --entry '${formatC64Address(copier)}' and --exit-memory '$34'.`,
694
693
  "; The unpacker of Exomizer uses the zero page and $0334-$03ff, so the copier writes pages $0002-$03ff after the unpack.",
695
694
  "",
696
695
  "!cpu 6510",
@@ -816,7 +815,7 @@ function placePacked(free: AddressRange[], low: Parameters<typeof placePieces>[1
816
815
 
817
816
  /** What Exomizer makes of a packed loader. */
818
817
  export interface PackedLoader {
819
- /** loader.prg: it loads at $0801 and starts with RUN. */
818
+ /** The loader program: it loads at $0801 and starts with RUN. */
820
819
  program: Buffer;
821
820
  packedBytes: number;
822
821
  unpackedBytes: number;
@@ -992,3 +991,23 @@ function vicLines(state: EntryState, earlier: number): string[] {
992
991
  " bne .raster",
993
992
  ];
994
993
  }
994
+
995
+ /**
996
+ * The symbols of the loader program: the labels of the build of loader.asm, which
997
+ * includes main.asm, and of start_screen_stage.asm when the loader has a show
998
+ * stage. A test takes one symbols file. Refuses a name with two values.
999
+ */
1000
+ export function loaderSymbols(...builds: ReadonlyArray<readonly AssembledSymbol[]>): AssembledSymbol[] {
1001
+ const valueOf = (symbol: AssembledSymbol) => (symbol.kind === "address" ? symbol.address : symbol.value);
1002
+ const byName = new Map<string, AssembledSymbol>();
1003
+ for (const symbol of builds.flat()) {
1004
+ const known = byName.get(symbol.name);
1005
+ if (known !== undefined && valueOf(known) !== valueOf(symbol)) {
1006
+ throw new WireFailure("operation-failed", `The builds of the loader program give ${symbol.name} two values, ${valueOf(known)} and ${valueOf(symbol)}. This is a defect of the script.`);
1007
+ }
1008
+ // An address in one build can be outside the load range of the other build, where it is a constant.
1009
+ const kept = known === undefined || (known.kind === "constant" && symbol.kind === "address") ? symbol : known;
1010
+ byName.set(symbol.name, { ...kept, used: symbol.used || (known?.used ?? false) });
1011
+ }
1012
+ return [...byName.values()].sort((a, b) => (a.name < b.name ? -1 : a.name > b.name ? 1 : 0));
1013
+ }
@@ -14,10 +14,11 @@
14
14
  // each release has it, the default build does not, and the loader never
15
15
  // loads it.
16
16
 
17
+ import { CONFIG_FILE, configValues } from "#src/build-name.ts";
17
18
  import { type AddressRange, COVERAGE, formatC64Address, formatC64Byte, POINTER_TABLE_TYPES, pointerTableEntries, type PointerTableType } from "#src/c64.ts";
18
19
  import { WireFailure } from "#src/host-client/tools.ts";
19
20
  import { identifiers, operandText, replaceIdentifiers, type OperandExpression } from "#src/knowledge/expression.ts";
20
- import { type CommentPlacement, isAnalyzer, isGeneratedFile, LEFTOVERS_SWITCH, RELEASE_DEFINE, START_SCREEN_SWITCH, type Origin, type RegionType, type SwitchKind } from "#src/knowledge/schema.ts";
21
+ import { type CommentPlacement, isAnalyzer, isGeneratedFile, LEFTOVER_FOLDER, LEFTOVERS_SWITCH, SEGMENT_FOLDER, RELEASE_DEFINE, START_SCREEN_SWITCH, type Origin, type RegionType, type SwitchKind } from "#src/knowledge/schema.ts";
21
22
  import type { Area } from "#src/platform.ts";
22
23
  import { hasZeroPageForm, instructionWith, labelProblem, MODE_LENGTH, OPCODES, takesAddress, USE_KINDS, useKind, type UseKind } from "#src/opcodes.ts";
23
24
 
@@ -95,7 +96,6 @@ export interface SourceSwitch {
95
96
  /** A release: the build with RELEASE_<label> gives each switch the value of this release. */
96
97
  export interface SourceRelease {
97
98
  label: string;
98
- file: string;
99
99
  base: boolean;
100
100
  comment?: string;
101
101
  }
@@ -330,23 +330,24 @@ export function layout(input: SourceInput): Layout {
330
330
  const end = bounds[index + 1]! - 1;
331
331
  const segmentTitle = `Segment ${formatC64Address(segment.start)}-${formatC64Address(segment.end)}`;
332
332
  if (leftovers.some((range) => range.start === start)) {
333
- pieces.push({ start, end, file: `leftover_${hex(start)}.asm`, title: `Leftovers ${formatC64Address(start)}-${formatC64Address(end)}: ${end - start + 1} bytes that nothing uses. Only LEFTOVERS = 1 builds them.`, segmentStart: index === 0, leftover: true, items: [] });
333
+ pieces.push({ start, end, file: `${LEFTOVER_FOLDER}/leftover_${hex(start)}.asm`, title: `Leftovers ${formatC64Address(start)}-${formatC64Address(end)}: ${end - start + 1} bytes that nothing uses. Only LEFTOVERS = 1 builds them.`, segmentStart: index === 0, leftover: true, items: [] });
334
334
  continue;
335
335
  }
336
- // A module that leftovers cut goes on in a file of its own after them: <module>_xxxx.asm.
336
+ // A module that leftovers cut goes on in a file of its own after them, in its folder: <module>_xxxx.asm.
337
337
  const module = modules.find((row) => row.start === start);
338
338
  const holder = module ?? modules.filter((row) => row.start >= segment.start && row.start < start).at(-1);
339
339
  const title = (text: string | undefined) => text?.replace(/\s*\r?\n\s*/g, " ");
340
- const file = module?.file ?? (holder === undefined ? `segment_${hex(start)}.asm` : `${holder.file.replace(/\.asm$/i, "")}_${hex(start)}.asm`);
340
+ const file = module?.file ?? (holder === undefined ? `${SEGMENT_FOLDER}/segment_${hex(start)}.asm` : `${holder.file.replace(/\.asm$/i, "")}_${hex(start)}.asm`);
341
341
  const named = module === undefined && holder !== undefined ? `${title(holder.title) ?? holder.file}, from ${formatC64Address(start)}` : title(module?.title);
342
342
  pieces.push({ start, end, file, title: named ?? segmentTitle, segmentStart: index === 0, leftover: false, items: [] });
343
343
  }
344
344
  }
345
+ // In any letter case: the file systems of macOS and Windows ignore it.
345
346
  const files = new Map<string, number>();
346
347
  for (const piece of pieces) {
347
- const other = files.get(piece.file);
348
+ const other = files.get(piece.file.toLowerCase());
348
349
  if (other !== undefined) throw new WireFailure("invalid-input", `The file ${piece.file} is used at ${formatC64Address(other)} and at ${formatC64Address(piece.start)}. Give the module another file.`);
349
- files.set(piece.file, piece.start);
350
+ files.set(piece.file.toLowerCase(), piece.start);
350
351
  }
351
352
 
352
353
  for (const piece of pieces) {
@@ -560,6 +561,16 @@ export function layout(input: SourceInput): Layout {
560
561
  }
561
562
  }
562
563
  }
564
+ // A macro line also uses the addresses that its instructions name besides its args, for example the high byte
565
+ // CUTHI of +mov16 CUTLO: a name of the source counts that use too. The args count as macro argument above.
566
+ for (const candidate of accepted) {
567
+ const argAddresses = new Set(candidate.use.args.flatMap((arg) => identifiers(arg)).map((name) => byName.get(name)));
568
+ for (const item of candidate.items) {
569
+ if (!takesAddress(OPCODES[item.opcode]!.mode)) continue;
570
+ const value = operandValue(item.address, item.opcode, input.byteAt)!;
571
+ if (!argAddresses.has(value) && uses.has(value)) tally(value, useKind(item.opcode));
572
+ }
573
+ }
563
574
 
564
575
  const names = new Map<number, string>();
565
576
  const taken = new Set([...knowledgeNames, ...platform.values()]);
@@ -930,21 +941,7 @@ export const hasSwitchFile = (layout: Pick<Layout, "switches" | "releases" | "le
930
941
  /** True when the source has config.asm: the knowledge has switches or a start screen, or the source has leftovers. */
931
942
  export const hasConfigFile = (layout: Pick<Layout, "switches" | "startScreen" | "leftovers">) => layout.switches.length > 0 || layout.startScreen || layout.leftovers.length > 0;
932
943
 
933
- /** The file with the value of each switch. The user edits it, and generate keeps the values. */
934
- export const CONFIG_FILE = "config.asm";
935
-
936
- /** A value line of config.asm: NAME = 0 or NAME = 1. */
937
- const CONFIG_VALUE = /^\s*([A-Z][A-Z0-9_]*)\s*=\s*([01])\s*(?:;.*)?$/;
938
-
939
- /** The values of the switches in a config.asm text, by name. Lines that are not a value line are not read. */
940
- export function configValues(text: string): Map<string, 0 | 1> {
941
- const values = new Map<string, 0 | 1>();
942
- for (const line of text.split(/\r?\n/)) {
943
- const match = CONFIG_VALUE.exec(line);
944
- if (match !== null) values.set(match[1]!, match[2] === "1" ? 1 : 0);
945
- }
946
- return values;
947
- }
944
+ export { CONFIG_FILE, configValues };
948
945
 
949
946
  /**
950
947
  * config.asm: the value of each switch as a plain assignment, grouped by
@@ -984,7 +981,7 @@ export function configText(layout: Pick<Layout, "switches" | "patches" | "startS
984
981
  out.push(
985
982
  "",
986
983
  "; Leftovers: off by default",
987
- `${LEFTOVERS_SWITCH} = ${values.get(LEFTOVERS_SWITCH) ?? 0} ; ${bytes} bytes in ${layout.leftovers.length} leftover_xxxx.asm ${layout.leftovers.length === 1 ? "file" : "files"} that nothing uses. 1 builds them, so main.asm gives the bytes of the program. Each release has them. The loader never loads them`,
984
+ `${LEFTOVERS_SWITCH} = ${values.get(LEFTOVERS_SWITCH) ?? 0} ; ${bytes} bytes in ${layout.leftovers.length} ${layout.leftovers.length === 1 ? "file" : "files"} in ${LEFTOVER_FOLDER}/ that nothing uses. 1 builds them, so main.asm gives the bytes of the program. Each release has them. The loader never loads them`,
988
985
  );
989
986
  }
990
987
  return `${out.join("\n")}\n`;
@@ -1002,7 +999,7 @@ function switchesText(layout: Layout): string {
1002
999
  "; then each switch has the value of that release, and the build does not read config.asm.",
1003
1000
  ];
1004
1001
  for (const release of layout.releases) {
1005
- out.push("", `; ${release.label}${release.base ? " (the program of the knowledge)" : ""}: ${release.file}${release.comment === undefined ? "" : `. ${release.comment.replace(/\s*\r?\n\s*/g, " ")}`}`);
1002
+ out.push("", `; ${release.label}${release.base ? " (the program of the knowledge)" : ""}${release.comment === undefined ? "" : `: ${release.comment.replace(/\s*\r?\n\s*/g, " ")}`}`);
1006
1003
  out.push(`!ifdef ${RELEASE_DEFINE}${release.label} {`);
1007
1004
  for (const row of layout.switches) out.push(`${INDENT}${row.name} = ${row.releases.includes(release.label) ? 1 : 0}`);
1008
1005
  if (layout.leftovers.length > 0) out.push(`${INDENT}${LEFTOVERS_SWITCH} = 1`);
@@ -3,7 +3,7 @@
3
3
  // the loader as it is in memory before it starts (the plain loader file, or
4
4
  // the parts of the packed form), the bytes of the stage that the start screen
5
5
  // uses at their own addresses, the bytes of the loader that those cover in a
6
- // stash, and the show code into one loader.prg. The show code shows the
6
+ // stash, and the show code into one loader program. The show code shows the
7
7
  // screen with start-screen.asm, calls the routine of the stage, waits for a
8
8
  // key or a fire button, stops the interrupts of the stage, sets the CIAs as
9
9
  // RUN leaves them, copies the stash back and starts the loader.
@@ -43,7 +43,7 @@ export interface StagePlan {
43
43
  code: number;
44
44
  /** The parts of the loader under the bytes of the stage: where the show code copies them back from. */
45
45
  stash: Array<{ target: AddressRange; at: number }>;
46
- /** start_screen_stage.asm: the show code at `code`, then each part of the stage, packed after it. */
46
+ /** start_screen_stage.asm: the show code at `code`, and each part of the stage at its own address. */
47
47
  text: string;
48
48
  }
49
49
 
@@ -54,7 +54,7 @@ export const STAGE_START = "c64_source_stage";
54
54
  export const STAGE_CODE_END = "c64_source_stage_code_end";
55
55
  /** The label of the colour RAM of the capture in the show code. */
56
56
  export const STAGE_COLOURS = "c64_source_stage_colours";
57
- /** The label of a part of the stage: its first byte and the byte after it in the build of start_screen_stage.asm. */
57
+ /** The label of a part of the stage: its first byte and the byte after it. */
58
58
  export const stageLabel = (start: number, part: "from" | "end") => `stage_${part}_${start.toString(16).padStart(4, "0")}`;
59
59
 
60
60
  /** One copy of the stash: the length, where it is read and where it is written. */
@@ -143,7 +143,7 @@ export function stageText(input: StageInput, code: number, stash: StagePlan["sta
143
143
  const { row } = input;
144
144
  const lines = [
145
145
  ...input.header.map((line) => `; ${line}`),
146
- `; The show stage of the start screen from ${row.stage}: Exomizer packs it with the loader into loader.prg, with --entry '${formatC64Address(code)}'.`,
146
+ `; The show stage of the start screen from ${row.stage}: Exomizer packs it with the loader into the loader program, with --entry '${formatC64Address(code)}'.`,
147
147
  "; It shows the start screen, waits for a key or a fire button, copies back the bytes of the loader that the stage covers, and starts the loader.",
148
148
  "",
149
149
  "!cpu 6510",
@@ -251,8 +251,9 @@ export function stageText(input: StageInput, code: number, stash: StagePlan["sta
251
251
  ...(input.colourRam === undefined ? [] : [`; The colour RAM of the capture of ${row.stage}.`, STAGE_COLOURS, ...byteLines({ start: 0, body: input.colourRam })]),
252
252
  STAGE_CODE_END,
253
253
  ];
254
+ // Each part at its own address: the code is in a free range, so no part overlaps it, and the build never runs past $ffff.
254
255
  for (const part of input.stage) {
255
- lines.push("", `; ${row.stage} ${show(rangeOf(part))}`, stageLabel(part.start, "from"), `!pseudopc ${formatC64Address(part.start)} {`, ...byteLines(part), "}", stageLabel(part.start, "end"));
256
+ lines.push("", `; ${row.stage} ${show(rangeOf(part))}`, `* = ${formatC64Address(part.start)}`, stageLabel(part.start, "from"), ...byteLines(part), stageLabel(part.start, "end"));
256
257
  }
257
258
  return `${lines.join("\n")}\n`;
258
259
  }