@henols/c64-re-tools 2.5.0 → 2.6.0-rc.1

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.
@@ -260,6 +260,7 @@ Traps:
260
260
  5. Make sure that the result is the program. The code at the entry must not be the unpacker again. A segment can be data that a loader or a cracktro left, for example intro graphics. Keep it until other evidence shows that the program does not use it. Then capture again with `--exclude` for that range.
261
261
  - Exclude each range of a cracktro that the program does not read. A rebuild has no cracktro. Also exclude a picture, an intro or a credit screen of a crack.
262
262
  - Do not exclude the start screen of the program, also when the program does not read it. An example is the loading picture of the publisher. Record it as the start screen in the project knowledge. The loader of the rebuild shows it.
263
+ - When the start screen is only in an earlier stage of the loading, capture that stage too, while it shows the screen. Do not exclude the memory of the screen in that capture. Put it into the project knowledge in a space of its own, for example `bank1`.
263
264
  - Do not exclude other unused bytes, for example leftovers of the development or an old copy of a routine. A rebuild keeps them.
264
265
  - Keep a crack change that the program reads, for example a credit text in the game. Record it as a `crack` switch in the project knowledge.
265
266
  6. Put `<out>/image.json` into the project knowledge. The knowledge keeps the segments, the entry, the entry state and the stages. After that, you do not need the files in `<out>`.
@@ -131,6 +131,9 @@ export interface ReleaseRow {
131
131
  * crack, that the loader of the generated source shows before the entry.
132
132
  * A text screen has a charset, a bitmap screen has a bitmap. The colour RAM
133
133
  * comes from 1000 bytes at `colours` or is one `colour`; a hires bitmap has none.
134
+ * Without `stage`, the bytes are in the program at the entry. With `stage`,
135
+ * they are in the memory of that space (a captured stage of the loading, for
136
+ * example), and `call` is a routine of the stage that the start screen calls.
134
137
  */
135
138
  export interface StartScreenRow {
136
139
  space: Space;
@@ -142,6 +145,8 @@ export interface StartScreenRow {
142
145
  colour?: number;
143
146
  border: number;
144
147
  background: number;
148
+ stage?: Space;
149
+ call?: number;
145
150
  comment?: string;
146
151
  origin: Extract<Origin, "user" | "llm">;
147
152
  revision: string;
@@ -197,7 +202,7 @@ export const SWITCH_COLUMNS = "space, name, kind, default_value, releases, comme
197
202
  export const RELEASE_COLUMNS = "space, label, file, sha256, base, comment, origin, valid_from_revision";
198
203
  export const MACRO_COLUMNS = "space, name, params, body, comment, origin, valid_from_revision";
199
204
  export const MACRO_USE_COLUMNS = "space, start_address, end_address, macro, args, origin, valid_from_revision";
200
- export const START_SCREEN_COLUMNS = "space, mode, screen, bitmap, charset, colours, colour, border, background, comment, origin, valid_from_revision";
205
+ export const START_SCREEN_COLUMNS = "space, mode, screen, bitmap, charset, colours, colour, border, background, stage, call_address, comment, origin, valid_from_revision";
201
206
 
202
207
  export function toSymbol(row: Raw): SymbolRow {
203
208
  return { space: row.space as Space, address: row.address as number, name: row.name as string, kind: row.kind as SymbolKind, origin: row.origin as Origin, revision: row.valid_from_revision as string };
@@ -318,6 +323,8 @@ export function toStartScreen(row: Raw): StartScreenRow {
318
323
  revision: row.valid_from_revision as string,
319
324
  };
320
325
  for (const key of ["bitmap", "charset", "colours", "colour"] as const) if (row[key] !== null && row[key] !== undefined) value[key] = row[key] as number;
326
+ if (row.stage !== null && row.stage !== undefined) value.stage = row.stage as Space;
327
+ if (row.call_address !== null && row.call_address !== undefined) value.call = row.call_address as number;
321
328
  if (row.comment !== null && row.comment !== undefined) value.comment = row.comment as string;
322
329
  return value;
323
330
  }
@@ -81,6 +81,8 @@ export const START_SCREEN_MODES = ["text", "hires", "multicolour"] as const;
81
81
  export type StartScreenMode = (typeof START_SCREEN_MODES)[number];
82
82
  /** The switch of the start screen in the generated source: no symbol, switch or release can have this name. */
83
83
  export const START_SCREEN_SWITCH = "START_SCREEN";
84
+ /** The switch of the leftovers in the generated source (bytes that nothing uses): no symbol, switch or release can have this name. */
85
+ export const LEFTOVERS_SWITCH = "LEFTOVERS";
84
86
  /** A macro name and a macro parameter. The body names a parameter with a dot in front: .dst. */
85
87
  export const MACRO_NAME = /^[A-Za-z_][A-Za-z0-9_]{0,31}$/;
86
88
  export const MACRO_PARAM = /^[a-z_][a-z0-9_]{0,31}$/;
@@ -98,9 +100,12 @@ export const MEMORY_ROW = 64;
98
100
  /** Files that c64-source writes itself: no module can have these names. */
99
101
  export const GENERATED_FILES = ["main.asm", "loader.asm", "switches.asm", "config.asm", "constants.asm", "macros.asm", "start_screen.asm"] as const;
100
102
 
103
+ /** The file of a range of leftovers in the generated source: leftover_xxxx.asm. */
104
+ export const LEFTOVER_FILE = /^leftover_[0-9a-f]{4}\.asm$/;
105
+
101
106
  /** True for a generated file name in any letter case: the file systems of macOS and Windows ignore case. */
102
107
  export function isGeneratedFile(file: string): boolean {
103
- return (GENERATED_FILES as readonly string[]).includes(file.toLowerCase());
108
+ return (GENERATED_FILES as readonly string[]).includes(file.toLowerCase()) || LEFTOVER_FILE.test(file.toLowerCase());
104
109
  }
105
110
 
106
111
  /**
@@ -314,10 +319,13 @@ function createSchema(db: DatabaseSync): void {
314
319
  colour INTEGER CHECK (colour BETWEEN 0 AND 15),
315
320
  border INTEGER NOT NULL CHECK (border BETWEEN 0 AND 15),
316
321
  background INTEGER NOT NULL CHECK (background BETWEEN 0 AND 15),
322
+ stage TEXT CHECK (stage IS NULL OR (stage <> 'c64' AND (${SPACE_CHECK.replaceAll("space", "stage")}))),
323
+ call_address INTEGER CHECK (call_address BETWEEN 0 AND 65535),
317
324
  comment TEXT CHECK (comment IS NULL OR length(comment) > 0),
318
325
  origin TEXT NOT NULL CHECK (origin IN ('user', 'llm')),
319
326
  valid_from_revision TEXT NOT NULL REFERENCES revisions (id),
320
327
  valid_to_revision TEXT REFERENCES revisions (id),
328
+ CHECK (call_address IS NULL OR stage IS NOT NULL),
321
329
  CHECK ((mode = 'text') = (charset IS NOT NULL)),
322
330
  CHECK ((mode = 'text') = (bitmap IS NULL)),
323
331
  CHECK (colours IS NULL OR colour IS NULL),
@@ -389,7 +397,7 @@ function createSchema(db: DatabaseSync): void {
389
397
  }
390
398
 
391
399
  /** The version of the index this build makes. An index with another version is emptied and made again. */
392
- export const INDEX_VERSION = 10;
400
+ export const INDEX_VERSION = 11;
393
401
 
394
402
  function indexVersion(db: DatabaseSync): number | undefined {
395
403
  const hasMeta = db.prepare("SELECT 1 FROM sqlite_master WHERE type = 'table' AND name = 'meta'").get();
@@ -200,6 +200,8 @@ export const TABLES: readonly TableSpec[] = [
200
200
  field("colour", "colour", "integer", true),
201
201
  field("border", "border", "integer"),
202
202
  field("background", "background", "integer"),
203
+ field("stage", "stage", "text", true),
204
+ field("call", "call_address", "address", true),
203
205
  field("comment", "comment", "text", true),
204
206
  origin,
205
207
  ],
@@ -78,7 +78,7 @@ import {
78
78
  type SwitchRow,
79
79
  type SymbolRow,
80
80
  } from "./read.ts";
81
- import { CODE_REGION_TYPES, COMMENT_PLACEMENTS, EVIDENCE_KINDS, MAX_ELEMENT, MODULE_FILE, DEFAULT_SPACE, GENERATED_FILES, isGeneratedFile, isSemantic, MACRO_NAME, MACRO_PARAM, OFF_KINDS, RELEASE_DEFINE, RELEASE_LABEL, START_SCREEN_MODES, START_SCREEN_SWITCH, SWITCH_KINDS, SWITCH_NAME, NO_ELEMENT_TYPES, OPERAND_SELECTS, POINTER_KIND, REFERENCE_KINDS, REGION_TYPES, SYMBOL_KINDS, type CommentPlacement, type EvidenceKind, type OperandSelect, type Origin, type ReferenceKind, type RegionType, type Space, type StartScreenMode, type SwitchKind, type SymbolKind } from "./schema.ts";
81
+ import { CODE_REGION_TYPES, COMMENT_PLACEMENTS, EVIDENCE_KINDS, MAX_ELEMENT, MODULE_FILE, DEFAULT_SPACE, GENERATED_FILES, isGeneratedFile, isSemantic, MACRO_NAME, MACRO_PARAM, OFF_KINDS, RELEASE_DEFINE, RELEASE_LABEL, LEFTOVERS_SWITCH, START_SCREEN_MODES, START_SCREEN_SWITCH, SWITCH_KINDS, SWITCH_NAME, NO_ELEMENT_TYPES, OPERAND_SELECTS, POINTER_KIND, REFERENCE_KINDS, REGION_TYPES, SYMBOL_KINDS, type CommentPlacement, type EvidenceKind, type OperandSelect, type Origin, type ReferenceKind, type RegionType, type Space, type StartScreenMode, type SwitchKind, type SymbolKind } from "./schema.ts";
82
82
  import { newRevisionId, rowId, sqlName } from "./text.ts";
83
83
 
84
84
  /** What a change rests on: a coverage run, a test or another project file with its SHA-256, or a note. */
@@ -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, 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.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, 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());
292
292
  }
293
293
 
294
294
  insertMacro(row: Omit<MacroRow, "revision">): void {
@@ -445,6 +445,7 @@ function renameStep(db: DatabaseSync, change: Change, origin: Origin, request: S
445
445
  }
446
446
  if (switchNamed(db, request.name, space) !== undefined) throw new Refusal("conflict", `${request.name} is the name of a switch. Give the symbol another name.`);
447
447
  if (request.name === START_SCREEN_SWITCH) throw new Refusal("conflict", `${START_SCREEN_SWITCH} is the switch of the start screen in the generated source. Give the symbol another name.`);
448
+ if (request.name === LEFTOVERS_SWITCH) throw new Refusal("conflict", `${LEFTOVERS_SWITCH} is the switch of the leftovers in the generated source. Give the symbol another name.`);
448
449
  const release = request.name.startsWith(RELEASE_DEFINE) ? releaseLabeled(db, request.name.slice(RELEASE_DEFINE.length), space) : undefined;
449
450
  if (release !== undefined) throw new Refusal("conflict", `${request.name} builds the release ${release.label}. Give the symbol another name.`);
450
451
  if (existing !== undefined) change.close("symbols", "space = ? AND address = ?", space, request.address);
@@ -959,6 +960,7 @@ function checkSwitch(request: SwitchRequest): void {
959
960
  checkSwitchName(request.name);
960
961
  if (request.name.startsWith(RELEASE_DEFINE)) throw new Refusal("invalid-input", `A switch name does not start with ${RELEASE_DEFINE}: the build of a release defines ${RELEASE_DEFINE}<label>.`);
961
962
  if (request.name === START_SCREEN_SWITCH) throw new Refusal("invalid-input", `${START_SCREEN_SWITCH} is the switch of the start screen in the generated source. Give the switch another name.`);
963
+ if (request.name === LEFTOVERS_SWITCH) throw new Refusal("invalid-input", `${LEFTOVERS_SWITCH} is the switch of the leftovers in the generated source. Give the switch another name.`);
962
964
  checkOneOf(SWITCH_KINDS, request.kind, "kind");
963
965
  if (request.default !== undefined && request.default !== 0 && request.default !== 1) throw new Refusal("invalid-input", "default must be 0 or 1.");
964
966
  if (OFF_KINDS.includes(request.kind) && request.default === 1) {
@@ -1064,7 +1066,7 @@ function unreleaseStep(db: DatabaseSync, change: Change, _origin: Origin, reques
1064
1066
  return { current: null };
1065
1067
  }
1066
1068
 
1067
- type StartScreenRequest = InSpace & { mode: StartScreenMode; screen: number; bitmap?: number; charset?: number; colours?: number; colour?: number; border: number; background: number; comment?: string };
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 };
1068
1070
 
1069
1071
  /**
1070
1072
  * The rules of the VIC-II for a start screen: the screen at a 1K step, the
@@ -1075,7 +1077,15 @@ type StartScreenRequest = InSpace & { mode: StartScreenMode; screen: number; bit
1075
1077
  */
1076
1078
  function checkStartScreen(request: StartScreenRequest): void {
1077
1079
  checkSpace(request);
1078
- if (spaceOf(request) !== DEFAULT_SPACE) throw new Refusal("invalid-input", "A start screen is only in the space c64.");
1080
+ if (spaceOf(request) !== DEFAULT_SPACE) throw new Refusal("invalid-input", "A start screen is only in the space c64. Give the space of its bytes with --stage.");
1081
+ if (request.stage !== undefined) {
1082
+ checkSpace({ space: request.stage });
1083
+ if (request.stage === DEFAULT_SPACE) throw new Refusal("invalid-input", "--stage is another space than c64: the space of a captured stage that holds the bytes of the start screen. Without --stage, the bytes are in the program.");
1084
+ }
1085
+ if (request.call !== undefined) {
1086
+ if (request.stage === undefined) throw new Refusal("invalid-input", "--call is a routine of the stage: give --stage too.");
1087
+ checkAddress(request.call, "call");
1088
+ }
1079
1089
  checkOneOf(START_SCREEN_MODES, request.mode, "mode");
1080
1090
  checkAddress(request.screen, "screen");
1081
1091
  for (const key of ["bitmap", "charset", "colours"] as const) if (request[key] !== undefined) checkAddress(request[key], key);
@@ -1118,8 +1128,26 @@ function checkStartScreen(request: StartScreenRequest): void {
1118
1128
  checkCommentText(request.comment, "A start screen comment");
1119
1129
  }
1120
1130
 
1131
+ /** The bytes of a start screen of a stage, and its call, must be in the program of that space. */
1132
+ function checkStage(db: DatabaseSync, request: StartScreenRequest): void {
1133
+ if (request.stage === undefined) return;
1134
+ const byteAt = programBytes(db, request.stage);
1135
+ if (byteAt(request.screen) === undefined && db.prepare("SELECT 1 FROM current_memory WHERE space = ? LIMIT 1").get(request.stage) === undefined) {
1136
+ throw new Refusal("not-found", `The knowledge has no program in ${request.stage}. Capture the stage while it shows the start screen, and put the capture into the project knowledge with image --space ${request.stage}. Then record the start screen again.`);
1137
+ }
1138
+ const inStage = (range: AddressRange) => {
1139
+ for (let address = range.start; address <= range.end; address++) if (byteAt(address) === undefined) return false;
1140
+ return true;
1141
+ };
1142
+ for (const { what, range } of startScreenRanges(request)) {
1143
+ if (!inStage(range)) throw new Refusal("invalid-input", `The ${what} at ${formatC64Address(range.start)}-${formatC64Address(range.end)} is not in the program in ${request.stage}. Capture the stage again, and do not exclude the memory of the start screen.`);
1144
+ }
1145
+ if (request.call !== undefined && !inStage({ start: request.call, end: request.call })) throw new Refusal("invalid-input", `The call ${formatC64Address(request.call)} is not in the program in ${request.stage}.`);
1146
+ }
1147
+
1121
1148
  function startScreenStep(db: DatabaseSync, change: Change, origin: Origin, request: StartScreenRequest): Step<StartScreenRow, StartScreenRow> {
1122
1149
  const space = spaceOf(request);
1150
+ checkStage(db, request);
1123
1151
  const existing = currentStartScreen(db, space);
1124
1152
  const comment = request.comment === undefined ? existing?.comment : request.comment.trim();
1125
1153
  const row: Omit<StartScreenRow, "revision"> = {
@@ -1132,6 +1160,8 @@ function startScreenStep(db: DatabaseSync, change: Change, origin: Origin, reque
1132
1160
  ...(request.colour === undefined ? {} : { colour: request.colour }),
1133
1161
  border: request.border,
1134
1162
  background: request.background,
1163
+ ...(request.stage === undefined ? {} : { stage: request.stage }),
1164
+ ...(request.call === undefined ? {} : { call: request.call }),
1135
1165
  ...(comment === undefined ? {} : { comment }),
1136
1166
  origin: origin as StartScreenRow["origin"],
1137
1167
  };
@@ -1605,7 +1635,7 @@ const KEYS = {
1605
1635
  columns: START_SCREEN_COLUMNS,
1606
1636
  map: toStartScreen,
1607
1637
  key: (row: StartScreenRow) =>
1608
- `${row.space}|${row.mode}|${row.screen}|${row.bitmap ?? ""}|${row.charset ?? ""}|${row.colours ?? ""}|${row.colour ?? ""}|${row.border}|${row.background}|${row.comment ?? ""}|${row.origin}`,
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}`,
1609
1639
  },
1610
1640
  macros: { columns: MACRO_COLUMNS, map: toMacro, key: (row: MacroRow) => `${row.space}|${row.name}|${listText(row.params)}|${row.body}|${row.comment ?? ""}|${row.origin}` },
1611
1641
  macro_uses: { columns: MACRO_USE_COLUMNS, map: toMacroUse, key: (row: MacroUseRow) => `${row.space}|${row.start}|${row.end}|${row.macro}|${listText(row.args)}|${row.origin}` },