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

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 (36) hide show
  1. package/package.json +1 -1
  2. package/skills/c64-assembler/SKILL.md +26 -12
  3. package/skills/c64-assembler/scripts/assembler.ts +24 -11
  4. package/skills/c64-knowledge/SKILL.md +31 -16
  5. package/skills/c64-knowledge/scripts/knowledge.ts +62 -33
  6. package/skills/c64-packer/SKILL.md +1 -1
  7. package/skills/c64-reverse-engineering/SKILL.md +1 -1
  8. package/skills/c64-source/SKILL.md +51 -30
  9. package/skills/c64-source/scripts/lib/export.ts +76 -32
  10. package/skills/c64-source/scripts/lib/loader.ts +29 -9
  11. package/skills/c64-source/scripts/lib/source.ts +5 -16
  12. package/skills/c64-source/scripts/lib/stage.ts +11 -5
  13. package/skills/c64-source/scripts/lib/start-screen.asm +4 -2
  14. package/skills/c64-source/scripts/source.ts +145 -46
  15. package/skills/c64-static-analysis/scripts/lib/coverage.ts +2 -25
  16. package/skills/c64-testing/SKILL.md +3 -1
  17. package/skills/c64-testing/references/recording-format.md +1 -0
  18. package/skills/c64-testing/references/scenario-format.md +5 -3
  19. package/skills/c64-testing/scripts/lib/recording.ts +25 -8
  20. package/skills/c64-testing/scripts/lib/replay.ts +1 -1
  21. package/skills/c64-testing/scripts/lib/scenario.ts +14 -5
  22. package/skills/c64-unpacker/SKILL.md +4 -2
  23. package/skills/c64-unpacker/scripts/lib/loads.ts +33 -12
  24. package/skills/c64-unpacker/scripts/lib/machine.ts +7 -3
  25. package/skills/c64-unpacker/scripts/unpacker.ts +5 -3
  26. package/src/build-name.ts +47 -0
  27. package/src/c64.ts +21 -1
  28. package/src/host/vice/session.ts +6 -1
  29. package/src/knowledge/read.ts +5 -2
  30. package/src/knowledge/schema.ts +5 -3
  31. package/src/knowledge/start-screen.ts +68 -0
  32. package/src/knowledge/text.ts +1 -0
  33. package/src/knowledge/write.ts +22 -16
  34. package/src/native/acme.ts +1 -1
  35. package/src/platform.ts +30 -1
  36. package/src/script.ts +3 -3
@@ -99,8 +99,8 @@ export interface WriteContext {
99
99
  expectedRevision?: string;
100
100
  /** Kept with the revision when the write changes something. */
101
101
  evidence?: Evidence[];
102
- /** The image that an operand, patch or macro use write is checked against. */
103
- image?: OperandImage;
102
+ /** The image of a space that an operand, patch or macro use write in that space is checked against. */
103
+ image?: (space: Space) => OperandImage;
104
104
  }
105
105
 
106
106
  /** The bytes of the image and the platform names (C64 space only) that operand expressions, patches and macro uses are checked against. */
@@ -287,8 +287,8 @@ export class Change {
287
287
 
288
288
  insertStartScreen(row: Omit<StartScreenRow, "revision">): void {
289
289
  this.#db
290
- .prepare("INSERT INTO start_screens (id, space, mode, screen, bitmap, charset, colours, colour, border, background, stage, call_address, comment, origin, valid_from_revision) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)")
291
- .run(this.#rowId(), row.space, row.mode, row.screen, row.bitmap ?? null, row.charset ?? null, row.colours ?? null, row.colour ?? null, row.border, row.background, row.stage ?? null, row.call ?? null, row.comment ?? null, row.origin, this.id());
290
+ .prepare("INSERT INTO start_screens (id, space, mode, screen, bitmap, charset, colours, colour, border, background, stage, call_address, colour_ram, comment, origin, valid_from_revision) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)")
291
+ .run(this.#rowId(), row.space, row.mode, row.screen, row.bitmap ?? null, row.charset ?? null, row.colours ?? null, row.colour ?? null, row.border, row.background, row.stage ?? null, row.call ?? null, row.colourRam === true ? 1 : null, row.comment ?? null, row.origin, this.id());
292
292
  }
293
293
 
294
294
  insertMacro(row: Omit<MacroRow, "revision">): void {
@@ -713,13 +713,13 @@ function checkOperandValue(db: DatabaseSync, space: Space, image: OperandImage,
713
713
  return identifiers(text).map((name) => resolve(name)!);
714
714
  }
715
715
 
716
- function operandStep(db: DatabaseSync, change: Change, context: ChangeContext & { image?: OperandImage }, request: OperandRequest): Step<OperandRow, OperandRow> {
716
+ function operandStep(db: DatabaseSync, change: Change, context: ChangeContext & { image?: (space: Space) => OperandImage }, request: OperandRequest): Step<OperandRow, OperandRow> {
717
717
  const space = spaceOf(request);
718
718
  if (context.image === undefined) throw new Refusal("invalid-input", "An operand expression is checked against the image, and no image was given.");
719
719
  const wanted: Omit<OperandRow, "space" | "address" | "origin" | "revision"> = { select: request.select, offset: request.offset ?? 0 };
720
720
  if (request.symbol !== undefined) wanted.symbol = request.symbol;
721
721
  if (request.expression !== undefined) wanted.expression = request.expression.trim();
722
- const targets = new Set(checkOperandValue(db, space, context.image, request, operandText(wanted)));
722
+ const targets = new Set(checkOperandValue(db, space, context.image(space), request, operandText(wanted)));
723
723
  const existing = operandAt(db, request.address, space);
724
724
  const origin = context.origin as OperandRow["origin"];
725
725
  if (existing !== undefined && existing.select === wanted.select && existing.symbol === wanted.symbol && existing.offset === wanted.offset && existing.expression === wanted.expression && !confirms(existing.origin, origin)) {
@@ -874,11 +874,12 @@ export function leastPatchBytes(code: string): number | undefined {
874
874
  return bytes;
875
875
  }
876
876
 
877
- function patchStep(db: DatabaseSync, change: Change, context: ChangeContext & { image?: OperandImage }, request: PatchRequest): Step<PatchRow, PatchRow> {
877
+ function patchStep(db: DatabaseSync, change: Change, context: ChangeContext & { image?: (space: Space) => OperandImage }, request: PatchRequest): Step<PatchRow, PatchRow> {
878
878
  const space = spaceOf(request);
879
879
  const range = `${formatC64Address(request.start)}-${formatC64Address(request.end)}`;
880
880
  if (context.image === undefined) throw new Refusal("invalid-input", "A patch is checked against the image, and no image was given.");
881
- checkInImage(context.image, request.start, request.end, "a patch");
881
+ const image = context.image(space);
882
+ checkInImage(image, request.start, request.end, "a patch");
882
883
  const least = leastPatchBytes(request.code);
883
884
  const length = request.end - request.start + 1;
884
885
  if (least !== undefined && least > length) {
@@ -979,7 +980,7 @@ function defaultOf(request: SwitchRequest, existing: SwitchRow | undefined): 0 |
979
980
  throw new Refusal("invalid-input", `A ${request.kind} switch needs a default: give --default 0 or --default 1.`);
980
981
  }
981
982
 
982
- function switchStep(db: DatabaseSync, change: Change, context: ChangeContext & { image?: OperandImage }, request: SwitchRequest): Step<SwitchRow, SwitchRow> {
983
+ function switchStep(db: DatabaseSync, change: Change, context: ChangeContext & { image?: (space: Space) => OperandImage }, request: SwitchRequest): Step<SwitchRow, SwitchRow> {
983
984
  const space = spaceOf(request);
984
985
  const base = baseLabel(db, space);
985
986
  if (base === undefined) throw new Refusal("not-found", "The knowledge has no base release. Record the release whose bytes are the program first (release <label> --file <file> --base).");
@@ -992,7 +993,7 @@ function switchStep(db: DatabaseSync, change: Change, context: ChangeContext & {
992
993
  if (existing !== undefined && existing.kind === request.kind && existing.default === value && listText(existing.releases) === listText(releases) && existing.comment === comment && !confirms(existing.origin, origin)) return { current: existing };
993
994
  const symbol = symbolNamed(db, request.name, space);
994
995
  if (symbol !== undefined) throw new Refusal("conflict", `The switch name ${request.name} is the name of the symbol at ${formatC64Address(symbol.address)}. Give the switch another name.`);
995
- if (space === DEFAULT_SPACE && context.image?.platform.has(request.name)) throw new Refusal("conflict", `The switch name ${request.name} is a C64 platform name. Give the switch another name.`);
996
+ if (space === DEFAULT_SPACE && context.image?.(space).platform.has(request.name)) throw new Refusal("conflict", `The switch name ${request.name} is a C64 platform name. Give the switch another name.`);
996
997
  // The patches hold the code of the value that the base release does not have: that value cannot change under them.
997
998
  if (existing !== undefined && baseValue(existing, base) !== baseValue({ releases }, base)) {
998
999
  const patches = listPatches(db, undefined, request.name, undefined, space);
@@ -1066,7 +1067,7 @@ function unreleaseStep(db: DatabaseSync, change: Change, _origin: Origin, reques
1066
1067
  return { current: null };
1067
1068
  }
1068
1069
 
1069
- type StartScreenRequest = InSpace & { mode: StartScreenMode; screen: number; bitmap?: number; charset?: number; colours?: number; colour?: number; border: number; background: number; stage?: Space; call?: number; comment?: string };
1070
+ type StartScreenRequest = InSpace & { mode: StartScreenMode; screen: number; bitmap?: number; charset?: number; colours?: number; colour?: number; border: number; background: number; stage?: Space; call?: number; colourRam?: boolean; comment?: string };
1070
1071
 
1071
1072
  /**
1072
1073
  * The rules of the VIC-II for a start screen: the screen at a 1K step, the
@@ -1099,7 +1100,11 @@ function checkStartScreen(request: StartScreenRequest): void {
1099
1100
  throw new Refusal("invalid-input", `A ${request.mode} start screen has a bitmap and no charset. Give --bitmap.`);
1100
1101
  }
1101
1102
  if (request.colours !== undefined && request.colour !== undefined) throw new Refusal("invalid-input", "Give the colour RAM as --colours or as --colour, not both.");
1102
- const colourRam = request.colours !== undefined || request.colour !== undefined;
1103
+ if (request.colourRam === true) {
1104
+ if (request.stage === undefined) throw new Refusal("invalid-input", "The colour RAM of a capture is only for a start screen of a stage: give --stage.");
1105
+ if (request.colours !== undefined || request.colour !== undefined) throw new Refusal("invalid-input", "The colours come from the colour RAM of the capture: do not give --colours or --colour too.");
1106
+ }
1107
+ const colourRam = request.colours !== undefined || request.colour !== undefined || request.colourRam === true;
1103
1108
  if (request.mode === "hires" && colourRam) throw new Refusal("invalid-input", "A hires bitmap does not use the colour RAM. Remove --colours and --colour.");
1104
1109
  if (request.mode !== "hires" && !colourRam) throw new Refusal("invalid-input", `A ${request.mode} start screen uses the colour RAM. Give --colours with the address of its 1000 bytes, or --colour with one colour.`);
1105
1110
  const step = (value: number, size: number, what: string) => {
@@ -1162,6 +1167,7 @@ function startScreenStep(db: DatabaseSync, change: Change, origin: Origin, reque
1162
1167
  background: request.background,
1163
1168
  ...(request.stage === undefined ? {} : { stage: request.stage }),
1164
1169
  ...(request.call === undefined ? {} : { call: request.call }),
1170
+ ...(request.colourRam === true ? { colourRam: true as const } : {}),
1165
1171
  ...(comment === undefined ? {} : { comment }),
1166
1172
  origin: origin as StartScreenRow["origin"],
1167
1173
  };
@@ -1259,11 +1265,11 @@ function renameInUses(db: DatabaseSync, change: Change, space: Space, from: stri
1259
1265
  }
1260
1266
  }
1261
1267
 
1262
- function useStep(db: DatabaseSync, change: Change, context: ChangeContext & { image?: OperandImage }, request: MacroUseRequest): Step<MacroUseRow, MacroUseRow> {
1268
+ function useStep(db: DatabaseSync, change: Change, context: ChangeContext & { image?: (space: Space) => OperandImage }, request: MacroUseRequest): Step<MacroUseRow, MacroUseRow> {
1263
1269
  const space = spaceOf(request);
1264
- const image = context.image;
1265
1270
  const range = `${formatC64Address(request.start)}-${formatC64Address(request.end)}`;
1266
- if (image === undefined) throw new Refusal("invalid-input", "A macro use is checked against the image, and no image was given.");
1271
+ if (context.image === undefined) throw new Refusal("invalid-input", "A macro use is checked against the image, and no image was given.");
1272
+ const image = context.image(space);
1267
1273
  const macro = macroNamed(db, request.macro, space);
1268
1274
  if (macro === undefined) throw new Refusal("not-found", `There is no macro ${request.macro}. Write it first (macro).`);
1269
1275
  const args = (request.args ?? []).map((arg) => arg.trim());
@@ -1635,7 +1641,7 @@ const KEYS = {
1635
1641
  columns: START_SCREEN_COLUMNS,
1636
1642
  map: toStartScreen,
1637
1643
  key: (row: StartScreenRow) =>
1638
- `${row.space}|${row.mode}|${row.screen}|${row.bitmap ?? ""}|${row.charset ?? ""}|${row.colours ?? ""}|${row.colour ?? ""}|${row.border}|${row.background}|${row.stage ?? ""}|${row.call ?? ""}|${row.comment ?? ""}|${row.origin}`,
1644
+ `${row.space}|${row.mode}|${row.screen}|${row.bitmap ?? ""}|${row.charset ?? ""}|${row.colours ?? ""}|${row.colour ?? ""}|${row.border}|${row.background}|${row.stage ?? ""}|${row.call ?? ""}|${row.colourRam === true ? 1 : ""}|${row.comment ?? ""}|${row.origin}`,
1639
1645
  },
1640
1646
  macros: { columns: MACRO_COLUMNS, map: toMacro, key: (row: MacroRow) => `${row.space}|${row.name}|${listText(row.params)}|${row.body}|${row.comment ?? ""}|${row.origin}` },
1641
1647
  macro_uses: { columns: MACRO_USE_COLUMNS, map: toMacroUse, key: (row: MacroUseRow) => `${row.space}|${row.start}|${row.end}|${row.macro}|${listText(row.args)}|${row.origin}` },
@@ -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";
package/src/platform.ts CHANGED
@@ -3,7 +3,7 @@
3
3
  // Names follow "Mapping the Commodore 64". Only platform facts live here:
4
4
  // an address without one belongs to the application.
5
5
 
6
- import { COVERAGE, d018Offsets, formatC64Address, vicBankBase } from "./c64.ts";
6
+ import { type AddressRange, COVERAGE, d018Offsets, formatC64Address, romOnlyRanges, vicBankBase } from "./c64.ts";
7
7
  import { Refusal } from "./errors.ts";
8
8
 
9
9
  export interface Entry {
@@ -433,6 +433,35 @@ export function romUseOfProgram(coverage: Uint8Array | undefined, state: Record<
433
433
  return { basic: true, kernal: true, from: "none" };
434
434
  }
435
435
 
436
+ /** The ROMs as the CPU sees them. */
437
+ const BASIC_ROM: AddressRange = { start: 0xa000, end: 0xbfff };
438
+ const KERNAL_ROM: AddressRange = { start: 0xe000, end: 0xffff };
439
+
440
+ /**
441
+ * The ranges of an image where the CPU sees ROM and not the RAM of the
442
+ * image, so a JSR there runs the ROM: with coverage, the image bytes that the
443
+ * runs used only as ROM; without coverage, the ROMs that $01 of the entry
444
+ * state switches in. A range with known code (`known`) in it is left out:
445
+ * that code stays code.
446
+ */
447
+ export function romRanges(coverage: Uint8Array | undefined, state: Record<string, unknown> | undefined, image: (address: number) => number | undefined, known: readonly number[]): AddressRange[] {
448
+ let ranges: AddressRange[] = [];
449
+ if (coverage !== undefined) ranges = romOnlyRanges(coverage, image);
450
+ else {
451
+ const port = (state?.cpuPort as Record<string, unknown> | undefined)?.$01;
452
+ const use = typeof port === "number" ? romUseOfPort(port) : { basic: false, kernal: false };
453
+ for (const rom of [...(use.basic ? [BASIC_ROM] : []), ...(use.kernal ? [KERNAL_ROM] : [])]) {
454
+ for (let address = rom.start; address <= rom.end; address++) {
455
+ if (image(address) === undefined) continue;
456
+ const last = ranges.at(-1);
457
+ if (last !== undefined && last.end === address - 1) last.end = address;
458
+ else ranges.push({ start: address, end: address });
459
+ }
460
+ }
461
+ }
462
+ return ranges.filter((range) => !known.some((address) => address >= range.start && address <= range.end));
463
+ }
464
+
436
465
  export type Decoded = Record<string, unknown>;
437
466
 
438
467
  const bits = (value: number) => [0, 1, 2, 3, 4, 5, 6, 7].filter((bit) => (value & (1 << bit)) !== 0);
package/src/script.ts CHANGED
@@ -88,7 +88,7 @@ export type OptionKind =
88
88
  | "number" // a value: $ and hex digits, decimal, or % and binary digits
89
89
  | "address" // $ and 1 to 4 hex digits, or a name
90
90
  | "range" // <start>-<end>, each an address
91
- | "space" // c64, drive8, or bank0 to bank255
91
+ | "space" // c64, drive8, loading, or bank0 to bank255
92
92
  | "choice"; // one of `choices`
93
93
 
94
94
  export interface OptionSpec {
@@ -121,7 +121,7 @@ export interface OptionSpec {
121
121
  /** The options that have one meaning in every script. A command can change the help, the value and whether it is required, never the kind. */
122
122
  export const OPTIONS = {
123
123
  out: { kind: "path", value: "<path>", help: "write the output here, relative to the project folder" },
124
- space: { kind: "space", value: "<space>", help: "the knowledge space: c64 (the default), drive8 for 1541 drive code, or bank0 to bank255 for a cartridge bank", default: "c64" },
124
+ space: { kind: "space", value: "<space>", help: "the knowledge space: c64 (the default), drive8 for 1541 drive code, loading for a captured stage of the loading, or bank0 to bank255 for a cartridge bank", default: "c64" },
125
125
  range: { kind: "range", value: "<start>-<end>", help: "only the addresses from start to end, for example '$c000-$cfff'" },
126
126
  entry: { kind: "address", value: "<address>", help: "an entry point of the program" },
127
127
  steps: { kind: "path", value: "<file>", help: "a JSON file of action steps that run after the start" },
@@ -608,7 +608,7 @@ export function parseCall(script: ScriptSpec, command: CommandSpec, argv: readon
608
608
  if (text === "") throw new UsageError(`--${name} needs a value`);
609
609
  if (spec.kind === "address" && !isAddressText(text)) throw new UsageError(`--${name} must be ${ADDRESS_FORM}, not ${JSON.stringify(text)}`);
610
610
  if (spec.kind === "range" && rangeParts(text) === undefined) throw new UsageError(`--${name} must be <start>-<end>, each ${ADDRESS_FORM}, for example '$c000-$cfff', not ${JSON.stringify(text)}`);
611
- if (spec.kind === "space" && !SPACE_PATTERN.test(text)) throw new UsageError(`--${name} must be c64, drive8, or bank0 to bank255, not ${JSON.stringify(text)}`);
611
+ if (spec.kind === "space" && !SPACE_PATTERN.test(text)) throw new UsageError(`--${name} must be c64, drive8, loading, or bank0 to bank255, not ${JSON.stringify(text)}`);
612
612
  if (spec.kind === "choice" && !(spec.choices ?? []).includes(text)) throw new UsageError(`--${name} must be one of ${(spec.choices ?? []).join(", ")}, not ${JSON.stringify(text)}`);
613
613
  if (spec.kind === "path") {
614
614
  if (spec.output === undefined) checkPath(text, `--${name}`, true);