@henols/c64-re-tools 2.8.0-rc.5 → 2.8.0-rc.7

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 (57) hide show
  1. package/package.json +1 -1
  2. package/skills/c64-assembler/SKILL.md +1 -1
  3. package/skills/c64-assembler/scripts/assembler.ts +78 -24
  4. package/skills/c64-docs/scripts/docs.ts +31 -10
  5. package/skills/c64-knowledge/SKILL.md +4 -3
  6. package/skills/c64-knowledge/scripts/knowledge.ts +31 -12
  7. package/skills/c64-packer/scripts/packer.ts +9 -2
  8. package/skills/c64-provenance/SKILL.md +1 -1
  9. package/skills/c64-provenance/scripts/lib/runtime.ts +5 -16
  10. package/skills/c64-source/references/loader.md +8 -7
  11. package/skills/c64-source/scripts/lib/loader.ts +4 -111
  12. package/skills/c64-source/scripts/source.ts +49 -78
  13. package/skills/c64-static-analysis/SKILL.md +2 -2
  14. package/skills/c64-static-analysis/scripts/lib/coverage.ts +56 -4
  15. package/skills/c64-static-analysis/scripts/static-analysis.ts +32 -21
  16. package/skills/c64-testing/SKILL.md +6 -0
  17. package/skills/c64-testing/scripts/lib/play.ts +8 -7
  18. package/skills/c64-testing/scripts/lib/recording.ts +218 -12
  19. package/skills/c64-testing/scripts/lib/replay.ts +7 -3
  20. package/skills/c64-testing/scripts/lib/runs.ts +5 -1
  21. package/skills/c64-testing/scripts/lib/scenario.ts +4 -0
  22. package/skills/c64-testing/scripts/testing.ts +24 -11
  23. package/skills/c64-unpacker/SKILL.md +6 -4
  24. package/skills/c64-unpacker/scripts/lib/loads.ts +4 -2
  25. package/skills/c64-unpacker/scripts/lib/machine.ts +128 -5
  26. package/skills/c64-unpacker/scripts/lib/stages.ts +5 -1
  27. package/skills/c64-unpacker/scripts/unpacker.ts +13 -12
  28. package/skills/emulator/SKILL.md +2 -2
  29. package/src/host/vice/adapter.ts +2 -1
  30. package/src/host-client/image.ts +33 -4
  31. package/src/host-client/tools.ts +1 -1
  32. package/src/host-client/transfer.ts +48 -26
  33. package/src/host-client/vice-session.ts +15 -0
  34. package/src/knowledge/database.ts +72 -19
  35. package/src/knowledge/import.ts +31 -8
  36. package/src/knowledge/names.ts +2 -2
  37. package/src/knowledge/program.ts +67 -7
  38. package/src/knowledge/schema.ts +6 -0
  39. package/src/knowledge/start-screen.ts +55 -2
  40. package/src/knowledge/write.ts +18 -12
  41. package/src/loader.ts +101 -33
  42. package/src/mcp/output.ts +13 -12
  43. package/src/mcp/server.ts +33 -22
  44. package/src/mcp/tools/debug.ts +21 -24
  45. package/src/mcp/tools/media.ts +27 -27
  46. package/src/mcp/tools/memory.ts +13 -17
  47. package/src/mcp/tools/video.ts +10 -12
  48. package/src/native/c64run.ts +39 -1
  49. package/src/native/decompiler/analyze.ts +1 -1
  50. package/src/native/dxa.ts +2 -2
  51. package/src/native/types.ts +26 -7
  52. package/src/program-code.ts +4 -2
  53. package/src/project.ts +224 -71
  54. package/src/protocol/vice.ts +5 -3
  55. package/src/script.ts +60 -15
  56. package/{skills/c64-source/scripts/lib → src}/stage.ts +142 -11
  57. package/tools/c64run.c +18 -5
@@ -1,17 +1,22 @@
1
1
  // The show stage of a start screen from a captured stage: the picture is not
2
2
  // in the program at the entry, so it shows before the loader. Exomizer packs
3
- // the loader as it is in memory before it starts (the plain loader file, or
4
- // the parts of the packed form), the bytes of the stage that the start screen
3
+ // the packed loader program, the bytes of the stage that the start screen
5
4
  // uses at their own addresses, the bytes of the loader that those cover in a
6
5
  // stash, and the show code into one loader program. The show code shows the
7
6
  // screen with start-screen.asm, calls the routine of the stage, waits for a
8
7
  // key or a fire button, stops the interrupts of the stage, sets the CIAs as
9
- // RUN leaves them, copies the stash back and starts the loader.
8
+ // RUN leaves them, copies the stash back and starts the loader. c64-source
9
+ // builds it for the loader that it checks, with the bytes of the stage from
10
+ // the knowledge; c64-assembler builds it for other switch values, with the
11
+ // bytes of the stage from start_screen_stage.asm of the source.
10
12
 
11
- import { type AddressRange, formatC64Address, formatC64Byte, formatC64Range, hexWord, mergeRanges, rangesOverlap, subtract } from "#src/c64.ts";
12
- import { WireFailure } from "#src/host-client/tools.ts";
13
- import type { StartScreenRow } from "#src/knowledge/read.ts";
14
- import { largestFree, placePieces, START_SCREEN_FILE, startScreenValues, type StartScreenPlan } from "./loader.ts";
13
+ import { type AddressRange, formatC64Address, formatC64Byte, formatC64Range, hexWord, mergeRanges, rangesOverlap, subtract } from "./c64.ts";
14
+ import { Refusal } from "./errors.ts";
15
+ import type { StartScreenRow } from "./knowledge/read.ts";
16
+ import { START_SCREEN_CONTINUE, START_SCREEN_FILE, START_SCREEN_WAIT, startScreenPlan, startScreenValues, type StartScreenPlan } from "./knowledge/start-screen.ts";
17
+ import { largestFree, packParts, type PackedLoader, placePieces } from "./loader.ts";
18
+ import type { ToolContext } from "./native/run.ts";
19
+ import { type AssembledSymbol, requiredLabel, symbolValues } from "./native/types.ts";
15
20
 
16
21
  /** Bytes at an address. */
17
22
  export interface StagePart {
@@ -19,10 +24,16 @@ export interface StagePart {
19
24
  body: Uint8Array;
20
25
  }
21
26
 
27
+ /** The bytes of the stage that the start screen uses, in address order, at $0400 or higher, and the colour RAM of its capture when the start screen takes its colours from it. */
28
+ export interface StageBytes {
29
+ parts: StagePart[];
30
+ colourRam?: Uint8Array;
31
+ }
32
+
22
33
  export interface StageInput {
23
- /** The loader as it is in memory before it starts: the plain loader file, or the parts of the packed form. */
34
+ /** The loader as it is in memory before it starts: the packed loader program. */
24
35
  inner: StagePart[];
25
- /** Where the loader starts, and the value of $01 that it needs there: $080d with $37, or the copier of the packed form with $34. */
36
+ /** Where the loader starts, and the value of $01 that it needs there. */
26
37
  innerStart: number;
27
38
  innerPort: number;
28
39
  /** The bytes of the stage that the start screen uses, in address order, at $0400 or higher. */
@@ -34,6 +45,8 @@ export interface StageInput {
34
45
  /** The lines that include switches.asm or config.asm, for START_SCREEN. */
35
46
  settings: string[];
36
47
  header: string[];
48
+ /** The key of the start screen that the bytes of the stage are of (startScreenKey). */
49
+ key: string;
37
50
  /** The bytes of the show code without its copy table (stageCodeSize). */
38
51
  codeSize: number;
39
52
  }
@@ -56,7 +69,32 @@ export const STAGE_CODE_END = "c64_source_stage_code_end";
56
69
  export const STAGE_COLOURS = "c64_source_stage_colours";
57
70
  /** The label of a part of the stage: its first byte and the byte after it. */
58
71
  const stageLabel = (start: number, part: "from" | "end") => `stage_${part}_${hexWord(start)}`;
72
+ /** The line of start_screen_stage.asm with the key of its start screen. */
73
+ const STAGE_KEY_LINE = /^; The start screen key ([0-9a-f]{16}):/m;
74
+
75
+ /** The key of the start screen in a text of start_screen_stage.asm, or undefined when the text has none. */
76
+ export function stageKey(text: string): string | undefined {
77
+ return STAGE_KEY_LINE.exec(text)?.[1];
78
+ }
79
+
80
+ /**
81
+ * Why the bytes of the stage in a text of start_screen_stage.asm (in the
82
+ * folder `where`) are not for the start screen with `key`, or undefined when
83
+ * they are: the text has no key, as a source of an earlier version has none,
84
+ * or another key.
85
+ */
86
+ export function stageFileMismatch(text: string, key: string, where: string, stage: string): string | undefined {
87
+ const found = stageKey(text);
88
+ if (found === undefined) return `${STAGE_FILE} of ${where} has no start screen key, so its bytes of the stage ${stage} are not known to be of the start screen of the knowledge. Generate the source with the loader again.`;
89
+ if (found !== key) return `${STAGE_FILE} of ${where} is not of the start screen of the knowledge now: the start screen or the stage ${stage} changed after the generation of the source with the loader. Generate the source with the loader again.`;
90
+ return undefined;
91
+ }
59
92
 
93
+ /** The first label of a part, with the address of the part. */
94
+ const STAGE_PART_LABEL = /^stage_from_([0-9a-f]{4})$/;
95
+
96
+ /** The bytes of the colour RAM. */
97
+ const COLOURS = 1000;
60
98
  /** One copy of the stash: the length, where it is read and where it is written. */
61
99
  const COPY_ENTRY = 6;
62
100
  /** X indexes the table, so the table holds at most this many copies in 256 bytes. */
@@ -68,6 +106,12 @@ const CODE_AREAS: readonly AddressRange[] = [
68
106
  ];
69
107
  /** The RAM that the stash can use: the copy runs with all RAM in, and not on the hardware vectors. */
70
108
  const STASH_AREA: AddressRange = { start: 0x0400, end: 0xfff9 };
109
+ /** The packed loader program loads here, and RUN starts its unpacker after the BASIC line, with the BASIC ROM and the KERNAL in. */
110
+ const LOADER_LOAD = 0x0801;
111
+ const LOADER_RUN = 0x080d;
112
+ const LOADER_PORT = 0x37;
113
+ /** The loader program must end before the I/O area: loading it must not write $d000-$dfff. */
114
+ const LOAD_LIMIT = 0xcfff;
71
115
 
72
116
  const rangeOf = (part: StagePart): AddressRange => ({ start: part.start, end: part.start + part.body.length - 1 });
73
117
 
@@ -77,7 +121,7 @@ function byteOf(parts: readonly StagePart[], address: number): number {
77
121
  const part = parts[index]!;
78
122
  if (address >= part.start && address < part.start + part.body.length) return part.body[address - part.start]!;
79
123
  }
80
- throw new WireFailure("operation-failed", `No part holds ${formatC64Address(address)}. This is a defect of the script.`);
124
+ throw new Refusal("operation-failed", `No part holds ${formatC64Address(address)}. This is a defect of the script.`);
81
125
  }
82
126
 
83
127
  /**
@@ -91,7 +135,7 @@ export function planStage(input: StageInput): StagePlan {
91
135
  const covered = mergeRanges(input.inner.map(rangeOf)).flatMap((inner) => input.stage.map(rangeOf).flatMap((part) => (rangesOverlap(part, inner) ? [{ start: Math.max(part.start, inner.start), end: Math.min(part.end, inner.end) }] : [])));
92
136
  const stashed = mergeRanges(covered).map((range) => ({ what: `the loader at ${formatC64Range(range)}`, range, bytes: (address: number) => byteOf(input.inner, address) }));
93
137
  const refuse = (why: string): never => {
94
- throw new WireFailure("invalid-input", `${why} The largest free ranges outside the loader and the bytes of the stage: ${largestFree(free)}. Capture the stage with less memory, so that its program holds only the start screen and the routine of the call.`);
138
+ throw new Refusal("invalid-input", `${why} The largest free ranges outside the loader and the bytes of the stage: ${largestFree(free)}. Capture the stage with less memory, so that its program holds only the start screen and the routine of the call.`);
95
139
  };
96
140
  for (let copies = Math.max(stashed.length, 1); copies <= MAX_COPIES; ) {
97
141
  const size = input.codeSize + COPY_ENTRY * copies + 2;
@@ -129,6 +173,7 @@ export function stageText(input: StageInput, code: number, stash: StagePlan["sta
129
173
  ...input.header.map((line) => `; ${line}`),
130
174
  `; The show stage of the start screen from ${row.stage}: Exomizer packs it with the loader into the loader program, with --entry '${formatC64Address(code)}'.`,
131
175
  "; 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.",
176
+ `; The start screen key ${input.key}: a build with other switch values uses the bytes of the stage in this file only for a start screen with this key.`,
132
177
  "",
133
178
  "!cpu 6510",
134
179
  ...input.settings,
@@ -256,3 +301,89 @@ export function stageParts(input: StageInput, plan: StagePlan, build: Uint8Array
256
301
  const stage = input.stage.map((part) => ({ start: part.start, body: slice(labels(stageLabel(part.start, "from")), labels(stageLabel(part.start, "end"))) }));
257
302
  return [...loader, ...stash, ...stage, { start: plan.code, body: slice(plan.code, labels(STAGE_CODE_END)) }];
258
303
  }
304
+
305
+ /**
306
+ * The bytes of the stage in a build of start_screen_stage.asm: each part
307
+ * between its labels, and with `colourRam` the colour RAM at STAGE_COLOURS.
308
+ * The parts do not depend on the switch values of the build. Refuses by
309
+ * name when the build has no part, or no colour RAM that the start screen
310
+ * needs.
311
+ */
312
+ export function stageOfBuild(build: Uint8Array, symbols: readonly AssembledSymbol[], colourRam: boolean): StageBytes {
313
+ const labels = symbolValues(symbols);
314
+ const start = build[0]! | (build[1]! << 8);
315
+ const slice = (from: number, length: number) => Uint8Array.from(build.subarray(2 + from - start, 2 + from - start + length));
316
+ const parts = [...labels]
317
+ .flatMap(([name, from]) => {
318
+ const found = STAGE_PART_LABEL.exec(name);
319
+ const end = found === null ? undefined : labels.get(stageLabel(Number.parseInt(found[1]!, 16), "end"));
320
+ return end === undefined || end <= from ? [] : [{ start: from, body: slice(from, end - from) }];
321
+ })
322
+ .sort((a, b) => a.start - b.start);
323
+ const colours = labels.get(STAGE_COLOURS);
324
+ if (parts.length === 0 || (colourRam && colours === undefined)) {
325
+ throw new Refusal("invalid-input", `${STAGE_FILE} of the source does not hold the bytes of the stage that the start screen uses. Generate the source with the loader again.`);
326
+ }
327
+ return { parts, ...(colourRam ? { colourRam: slice(colours!, COLOURS) } : {}) };
328
+ }
329
+
330
+ /** A build of start_screen_stage.asm with the files of the source: its program and its labels. The function refuses when the text does not assemble. */
331
+ export type AssembleStage = (text: string) => Promise<{ program: Uint8Array; symbols: readonly AssembledSymbol[] }>;
332
+
333
+ /** What the show stage gives: the loader program, the text of start_screen_stage.asm, and what a check of it needs. */
334
+ export interface PackedStage {
335
+ /** The loader program: the pack of the show stage with the packed loader program. */
336
+ packed: PackedLoader;
337
+ /** start_screen_stage.asm of the plan. */
338
+ text: string;
339
+ /** The labels of the build of start_screen_stage.asm. */
340
+ symbols: AssembledSymbol[];
341
+ plan: StagePlan;
342
+ screen: StartScreenPlan;
343
+ /** The wait of the start screen for a key or a fire button, and the code after it. */
344
+ wait: number;
345
+ continue: number;
346
+ }
347
+
348
+ /**
349
+ * Builds the show stage of a start screen from a captured stage around a
350
+ * packed loader program: plans it, assembles start_screen_stage.asm with
351
+ * `assembleStage` and packs it with the loader as the loader is in memory
352
+ * before it starts. Its unpacker unpacks the loader over the start screen
353
+ * after the wait. Refuses by name.
354
+ */
355
+ export async function packStage(loader: Uint8Array, stage: StageBytes, row: StartScreenRow, text: { settings: string[]; header: string[]; key: string }, assembleStage: AssembleStage, context: ToolContext): Promise<PackedStage> {
356
+ const screen = startScreenPlan(row);
357
+ const input: StageInput = {
358
+ inner: [{ start: LOADER_LOAD, body: loader.subarray(2) }],
359
+ innerStart: LOADER_RUN,
360
+ innerPort: LOADER_PORT,
361
+ stage: stage.parts,
362
+ ...(stage.colourRam === undefined ? {} : { colourRam: stage.colourRam }),
363
+ row,
364
+ plan: screen,
365
+ settings: text.settings,
366
+ header: text.header,
367
+ key: text.key,
368
+ codeSize: 0,
369
+ };
370
+ const build = async (source: string) => {
371
+ const built = await assembleStage(source);
372
+ const labels = symbolValues(built.symbols);
373
+ const label = (name: string) => requiredLabel(labels, name, `The build of ${STAGE_FILE}`);
374
+ return { program: built.program, symbols: [...built.symbols], label };
375
+ };
376
+ // The size of the show code does not depend on its address: one build without a stash and without the parts gives it.
377
+ const probe = await build(stageText({ ...input, stage: [] }, 0x1000, []));
378
+ input.codeSize = probe.label(STAGE_CODE_END) - probe.label(STAGE_START) - 2;
379
+ const plan = planStage(input);
380
+ const built = await build(plan.text);
381
+ if (built.label(STAGE_START) !== plan.code || built.label(STAGE_CODE_END) - plan.code !== input.codeSize + COPY_ENTRY * plan.stash.length + 2) {
382
+ throw new Refusal("operation-failed", `The build of ${STAGE_FILE} does not have the planned layout. This is a defect of the script.`);
383
+ }
384
+ const parts = stageParts(input, plan, built.program, built.label);
385
+ const packed = await packParts(parts, plan.code, LOADER_PORT, "the loader and the start screen of the stage", "the show stage", context);
386
+ const end = LOADER_LOAD + packed.program.length - 3;
387
+ if (end > LOAD_LIMIT) throw new Refusal("invalid-input", `The loader with the start screen of ${row.stage} would end at ${formatC64Address(end)}, after $cfff: loading it would write the I/O area. Capture the stage with less memory, so that its program holds only the start screen and the routine of the call.`);
388
+ return { packed, text: plan.text, symbols: built.symbols, plan, screen, wait: built.label(START_SCREEN_WAIT), continue: built.label(START_SCREEN_CONTINUE) };
389
+ }
package/tools/c64run.c CHANGED
@@ -27,7 +27,7 @@
27
27
  #include "systems/c1541.h"
28
28
  #include "systems/c64.h"
29
29
 
30
- #define C64RUN_PROTOCOL 7
30
+ #define C64RUN_PROTOCOL 8
31
31
  // The key code of matrix place 0 (row 0, column 0); place n has code 0x80 + n.
32
32
  #define C64RUN_KEY_PLACE 0x80
33
33
  #ifndef CHIPS_COMMIT
@@ -237,6 +237,9 @@ static int counted_pcs;
237
237
  // "mark": the counts and the tick at the first opcode fetch at mark_pc, before that fetch counts.
238
238
  static uint64_t marked[MAX_COUNTED];
239
239
  static uint64_t mark_tick;
240
+ // "mark": pages 0-3 of RAM and the stack pointer at that fetch, the part of the entry state that a loader writes.
241
+ static uint8_t mark_ram[0x400];
242
+ static uint8_t mark_s;
240
243
  static int mark_pc = -1; // -1: no mark waits
241
244
  static int mark_set; // marked and mark_tick hold the mark
242
245
 
@@ -290,6 +293,8 @@ static void count_fetch(uint16_t pc) {
290
293
  for (int i = 0; i < counted_pcs; i++) marked[i] = counted[i];
291
294
  mark_tick = ticks;
292
295
  phase_get(&mark_phase);
296
+ memcpy(mark_ram, sys.ram, sizeof mark_ram);
297
+ mark_s = sys.cpu.S;
293
298
  mark_pc = -1;
294
299
  mark_set = 1;
295
300
  }
@@ -625,6 +630,14 @@ int main(int argc, char** argv) {
625
630
  printf("\n");
626
631
  }
627
632
  else printf("ok none\n");
633
+ } else if (!strcmp(cmd, "marked-ram")) {
634
+ // marked-ram: the stack pointer and RAM $0000-$03ff (hex) at the mark when it came, else none
635
+ if (mark_set) {
636
+ printf("ok s=%02x ram=", mark_s);
637
+ for (size_t i = 0; i < sizeof mark_ram; i++) printf("%02x", mark_ram[i]);
638
+ printf("\n");
639
+ }
640
+ else printf("ok none\n");
628
641
  } else if (!strcmp(cmd, "align")) {
629
642
  // align <phase hex>: at an opcode fetch, hold the CPU on the fetch (RDY, as the VIC-II
630
643
  // does on a bad line) until the beam is at the position of the phase, then give the
@@ -673,14 +686,14 @@ int main(int argc, char** argv) {
673
686
  for (int i = 0; i < site_count; i++) if (sites[i].pc == pc && sites[i].addr == addr) count = sites[i].count;
674
687
  printf("ok count=%llu\n", (unsigned long long)count);
675
688
  } else if (!strcmp(cmd, "joy")) {
676
- // joy <port 1 mask> <port 2 mask> (hex): 1 up, 2 down, 4 left, 8 right, 10 fire
689
+ // joy <port 1 mask> <port 2 mask> (hex): 1 up, 2 down, 4 left, 8 right, 10 fire. Answers the tick.
677
690
  unsigned j1, j2;
678
691
  if (sscanf(line, "%*s %x %x", &j1, &j2) != 2) { printf("err joy: give two masks\n"); continue; }
679
692
  c64_joystick(&sys, (uint8_t)j1, (uint8_t)j2);
680
- printf("ok\n");
693
+ printf("ok tick=%llu\n", (unsigned long long)ticks);
681
694
  } else if (!strcmp(cmd, "key")) {
682
695
  // key <code hex> down|up: 80 + place is the key at matrix place row * 8 + column
683
- // (80-bf), ff is RESTORE (NMI). chips holds at most 4 keys at once.
696
+ // (80-bf), ff is RESTORE (NMI). chips holds at most 4 keys at once. Answers the tick.
684
697
  unsigned code;
685
698
  if (sscanf(a1, "%x", &code) != 1 || (strcmp(a2, "down") && strcmp(a2, "up"))) { printf("err key: give a code and down or up\n"); continue; }
686
699
  if ((code < C64RUN_KEY_PLACE || code >= C64RUN_KEY_PLACE + 64) && code != C64_KEY_RESTORE) { printf("err key: give 80-bf (a matrix place) or ff (RESTORE)\n"); continue; }
@@ -692,7 +705,7 @@ int main(int argc, char** argv) {
692
705
  kbd_update(&sys.kbd, 20000);
693
706
  kbd_update(&sys.kbd, 0);
694
707
  }
695
- printf("ok\n");
708
+ printf("ok tick=%llu\n", (unsigned long long)ticks);
696
709
  } else if (!strcmp(cmd, "inputs")) {
697
710
  // inputs <addr|from-to,...>: the I/O addresses that record and replay handle
698
711
  memset(input_addr, 0, sizeof input_addr);