@henols/c64-re-tools 2.1.8 → 2.2.0-rc.2

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.
@@ -6,7 +6,7 @@ import type { DatabaseSync } from "node:sqlite";
6
6
 
7
7
  import type { AddressRange } from "../c64.ts";
8
8
  import { Refusal } from "../errors.ts";
9
- import { DEFAULT_SPACE, type CommentPlacement, type OperandSelect, type Origin, type ReferenceKind, type RegionType, type Space, type SuppressionEntity, type SymbolKind } from "./schema.ts";
9
+ import { DEFAULT_SPACE, type CommentPlacement, type OperandSelect, type Origin, type ReferenceKind, type RegionType, type Space, type SuppressionEntity, type SwitchKind, type SymbolKind } from "./schema.ts";
10
10
  import { NO_REVISION } from "./text.ts";
11
11
 
12
12
  export interface SymbolRow {
@@ -86,8 +86,10 @@ export interface OperandRow {
86
86
  }
87
87
 
88
88
  /**
89
- * Code that replaces [start, end] when the build defines the switch as 1
90
- *. The code fits the range; NOP fills the rest. Empty code is only NOP.
89
+ * Code that replaces [start, end] when its switch does not have its base value:
90
+ * the program bytes are the base release, so the code is the other state. The
91
+ * code fits the range; NOP fills the rest. Empty code is only NOP. Patches of
92
+ * different switches can have the same range: they are alternatives.
91
93
  */
92
94
  export interface PatchRow {
93
95
  space: Space;
@@ -100,6 +102,30 @@ export interface PatchRow {
100
102
  revision: string;
101
103
  }
102
104
 
105
+ /** A switch of the patches: what it changes, its value in the default build, and the releases that have it on. */
106
+ export interface SwitchRow {
107
+ space: Space;
108
+ name: string;
109
+ kind: SwitchKind;
110
+ default: 0 | 1;
111
+ releases: string[];
112
+ comment?: string;
113
+ origin: Extract<Origin, "user" | "llm">;
114
+ revision: string;
115
+ }
116
+
117
+ /** A release of the program: its project file with the SHA-256, and whether its bytes are the program of the knowledge. */
118
+ export interface ReleaseRow {
119
+ space: Space;
120
+ label: string;
121
+ file: string;
122
+ sha256: string;
123
+ base: boolean;
124
+ comment?: string;
125
+ origin: Extract<Origin, "user" | "llm">;
126
+ revision: string;
127
+ }
128
+
103
129
  /** An ACME macro of the generated source. The body names a parameter with a dot in front: .dst. */
104
130
  export interface MacroRow {
105
131
  space: Space;
@@ -136,6 +162,8 @@ export const SUPPRESSION_COLUMNS = "space, entity, start_address, end_address, t
136
162
  export const MODULE_COLUMNS = "space, start_address, file, title, origin, valid_from_revision";
137
163
  export const OPERAND_COLUMNS = "space, address, selector, symbol, offset, expression, origin, valid_from_revision";
138
164
  export const PATCH_COLUMNS = "space, start_address, end_address, switch_name, code, comment, origin, valid_from_revision";
165
+ export const SWITCH_COLUMNS = "space, name, kind, default_value, releases, comment, origin, valid_from_revision";
166
+ export const RELEASE_COLUMNS = "space, label, file, sha256, base, comment, origin, valid_from_revision";
139
167
  export const MACRO_COLUMNS = "space, name, params, body, comment, origin, valid_from_revision";
140
168
  export const MACRO_USE_COLUMNS = "space, start_address, end_address, macro, args, origin, valid_from_revision";
141
169
 
@@ -219,6 +247,34 @@ export function toPatch(row: Raw): PatchRow {
219
247
  return patch;
220
248
  }
221
249
 
250
+ export function toSwitch(row: Raw): SwitchRow {
251
+ const value: SwitchRow = {
252
+ space: row.space as Space,
253
+ name: row.name as string,
254
+ kind: row.kind as SwitchKind,
255
+ default: row.default_value === 1 ? 1 : 0,
256
+ releases: items(row.releases as string),
257
+ origin: row.origin as SwitchRow["origin"],
258
+ revision: row.valid_from_revision as string,
259
+ };
260
+ if (row.comment !== null && row.comment !== undefined) value.comment = row.comment as string;
261
+ return value;
262
+ }
263
+
264
+ export function toRelease(row: Raw): ReleaseRow {
265
+ const release: ReleaseRow = {
266
+ space: row.space as Space,
267
+ label: row.label as string,
268
+ file: row.file as string,
269
+ sha256: row.sha256 as string,
270
+ base: row.base === 1,
271
+ origin: row.origin as ReleaseRow["origin"],
272
+ revision: row.valid_from_revision as string,
273
+ };
274
+ if (row.comment !== null && row.comment !== undefined) release.comment = row.comment as string;
275
+ return release;
276
+ }
277
+
222
278
  export function toMacro(row: Raw): MacroRow {
223
279
  const macro: MacroRow = { space: row.space as Space, name: row.name as string, params: items(row.params as string), body: row.body as string, origin: row.origin as MacroRow["origin"], revision: row.valid_from_revision as string };
224
280
  if (row.comment !== null && row.comment !== undefined) macro.comment = row.comment as string;
@@ -437,6 +493,44 @@ export function macroUsesOverlapping(db: DatabaseSync | undefined, start: number
437
493
  .map((row) => toMacroUse(row as Raw));
438
494
  }
439
495
 
496
+ export function switchNamed(db: DatabaseSync | undefined, name: string, space: Space = DEFAULT_SPACE): SwitchRow | undefined {
497
+ const row = db?.prepare(`SELECT ${SWITCH_COLUMNS} FROM current_switches WHERE space = ? AND name = ?`).get(space, name) as Raw | undefined;
498
+ return row === undefined ? undefined : toSwitch(row);
499
+ }
500
+
501
+ /** Current switches of a space, by name. */
502
+ export function listSwitches(db: DatabaseSync | undefined, origins?: OriginFilter, space: Space = DEFAULT_SPACE): SwitchRow[] {
503
+ if (db === undefined) return [];
504
+ return db
505
+ .prepare(`SELECT ${SWITCH_COLUMNS} FROM current_switches WHERE space = ? ORDER BY name`)
506
+ .all(space)
507
+ .map((row) => toSwitch(row as Raw))
508
+ .filter(kept(origins));
509
+ }
510
+
511
+ export function releaseLabeled(db: DatabaseSync | undefined, label: string, space: Space = DEFAULT_SPACE): ReleaseRow | undefined {
512
+ const row = db?.prepare(`SELECT ${RELEASE_COLUMNS} FROM current_releases WHERE space = ? AND label = ?`).get(space, label) as Raw | undefined;
513
+ return row === undefined ? undefined : toRelease(row);
514
+ }
515
+
516
+ /** Current releases of a space: the base release first, then by label. */
517
+ export function listReleases(db: DatabaseSync | undefined, origins?: OriginFilter, space: Space = DEFAULT_SPACE): ReleaseRow[] {
518
+ if (db === undefined) return [];
519
+ return db
520
+ .prepare(`SELECT ${RELEASE_COLUMNS} FROM current_releases WHERE space = ? ORDER BY base DESC, label`)
521
+ .all(space)
522
+ .map((row) => toRelease(row as Raw))
523
+ .filter(kept(origins));
524
+ }
525
+
526
+ /**
527
+ * The value of a switch in the program of the knowledge: 1 when the base
528
+ * release has it on. A patch holds the code of the other value.
529
+ */
530
+ export function baseValue(row: Pick<SwitchRow, "releases">, base: string | undefined): 0 | 1 {
531
+ return base !== undefined && row.releases.includes(base) ? 1 : 0;
532
+ }
533
+
440
534
  export function macroNamed(db: DatabaseSync | undefined, name: string, space: Space = DEFAULT_SPACE): MacroRow | undefined {
441
535
  const row = db?.prepare(`SELECT ${MACRO_COLUMNS} FROM current_macros WHERE space = ? AND name = ?`).get(space, name) as Raw | undefined;
442
536
  return row === undefined ? undefined : toMacro(row);
@@ -585,7 +679,7 @@ export function listSpaces(db: DatabaseSync | undefined): Space[] {
585
679
  if (db === undefined) return [];
586
680
  const rows = db
587
681
  .prepare(
588
- `SELECT space FROM current_symbols UNION SELECT space FROM current_analyzer_names UNION SELECT space FROM current_regions UNION SELECT space FROM current_comments UNION SELECT space FROM current_references UNION SELECT space FROM current_suppressions UNION SELECT space FROM current_modules UNION SELECT space FROM current_operands UNION SELECT space FROM current_patches UNION SELECT space FROM current_macros UNION SELECT space FROM current_macro_uses UNION SELECT space FROM current_memory UNION SELECT space FROM current_entries UNION SELECT space FROM current_coverage`,
682
+ `SELECT space FROM current_symbols UNION SELECT space FROM current_analyzer_names UNION SELECT space FROM current_regions UNION SELECT space FROM current_comments UNION SELECT space FROM current_references UNION SELECT space FROM current_suppressions UNION SELECT space FROM current_modules UNION SELECT space FROM current_operands UNION SELECT space FROM current_patches UNION SELECT space FROM current_switches UNION SELECT space FROM current_releases UNION SELECT space FROM current_macros UNION SELECT space FROM current_macro_uses UNION SELECT space FROM current_memory UNION SELECT space FROM current_entries UNION SELECT space FROM current_coverage`,
589
683
  )
590
684
  .all() as Array<{ space: Space }>;
591
685
  const order = (space: Space) => (space === "c64" ? -2 : space === "drive8" ? -1 : Number(space.slice(4)));
@@ -60,6 +60,19 @@ export type SuppressionEntity = (typeof SUPPRESSION_ENTITIES)[number];
60
60
  export const MODULE_FILE = /^[A-Za-z0-9_][A-Za-z0-9_-]{0,59}\.asm$/;
61
61
  /** A switch of the generated source: upper case, so it reads as a build option and not as a label. */
62
62
  export const SWITCH_NAME = /^[A-Z][A-Z0-9_]{1,31}$/;
63
+ /** A release label: the switch-name form. The build of a release defines RELEASE_<label>. */
64
+ export const RELEASE_LABEL = SWITCH_NAME;
65
+ /** The define that builds a release with its switch values. */
66
+ export const RELEASE_DEFINE = "RELEASE_";
67
+ /**
68
+ * What a switch changes. A copy protection, a trainer, a cheat and a change of
69
+ * a cracker are off by default; a fix is on by default; a change has the
70
+ * default that the writer gives.
71
+ */
72
+ export const SWITCH_KINDS = ["protection", "trainer", "cheat", "crack", "fix", "change"] as const;
73
+ export type SwitchKind = (typeof SWITCH_KINDS)[number];
74
+ /** The kinds that are always off by default: the default build is the clean program. */
75
+ export const OFF_KINDS: readonly SwitchKind[] = ["protection", "trainer", "cheat", "crack"];
63
76
  /** A macro name and a macro parameter. The body names a parameter with a dot in front: .dst. */
64
77
  export const MACRO_NAME = /^[A-Za-z_][A-Za-z0-9_]{0,31}$/;
65
78
  export const MACRO_PARAM = /^[a-z_][a-z0-9_]{0,31}$/;
@@ -75,7 +88,7 @@ export type CoverageKind = (typeof COVERAGE_KINDS)[number];
75
88
  export const MEMORY_ROW = 64;
76
89
 
77
90
  /** Files that c64-source writes itself: no module can have these names. */
78
- export const GENERATED_FILES = ["main.asm", "loader.asm", "constants.asm", "macros.asm"] as const;
91
+ export const GENERATED_FILES = ["main.asm", "loader.asm", "switches.asm", "constants.asm", "macros.asm"] as const;
79
92
 
80
93
  /** True for a generated file name in any letter case: the file systems of macOS and Windows ignore case. */
81
94
  export function isGeneratedFile(file: string): boolean {
@@ -244,6 +257,32 @@ function createSchema(db: DatabaseSync): void {
244
257
  valid_to_revision TEXT REFERENCES revisions (id)
245
258
  );
246
259
  CREATE INDEX patches_start ON patches (space, start_address);
260
+ CREATE TABLE switches (
261
+ id TEXT PRIMARY KEY,
262
+ space TEXT NOT NULL DEFAULT 'c64' CHECK (${SPACE_CHECK}),
263
+ name TEXT NOT NULL CHECK (length(name) > 0),
264
+ kind TEXT NOT NULL CHECK (kind IN (${list(SWITCH_KINDS)})),
265
+ default_value INTEGER NOT NULL CHECK (default_value IN (0, 1)),
266
+ releases TEXT NOT NULL,
267
+ comment TEXT CHECK (comment IS NULL OR length(comment) > 0),
268
+ origin TEXT NOT NULL CHECK (origin IN ('user', 'llm')),
269
+ valid_from_revision TEXT NOT NULL REFERENCES revisions (id),
270
+ valid_to_revision TEXT REFERENCES revisions (id)
271
+ );
272
+ CREATE INDEX switches_name ON switches (space, name);
273
+ CREATE TABLE releases (
274
+ id TEXT PRIMARY KEY,
275
+ space TEXT NOT NULL DEFAULT 'c64' CHECK (${SPACE_CHECK}),
276
+ label TEXT NOT NULL CHECK (length(label) > 0),
277
+ file TEXT NOT NULL CHECK (length(file) > 0),
278
+ sha256 TEXT NOT NULL CHECK (length(sha256) = 64),
279
+ base INTEGER NOT NULL CHECK (base IN (0, 1)),
280
+ comment TEXT CHECK (comment IS NULL OR length(comment) > 0),
281
+ origin TEXT NOT NULL CHECK (origin IN ('user', 'llm')),
282
+ valid_from_revision TEXT NOT NULL REFERENCES revisions (id),
283
+ valid_to_revision TEXT REFERENCES revisions (id)
284
+ );
285
+ CREATE INDEX releases_label ON releases (space, label);
247
286
  CREATE TABLE macros (
248
287
  id TEXT PRIMARY KEY,
249
288
  space TEXT NOT NULL DEFAULT 'c64' CHECK (${SPACE_CHECK}),
@@ -312,13 +351,15 @@ function createSchema(db: DatabaseSync): void {
312
351
  CREATE VIEW current_modules AS SELECT * FROM modules WHERE valid_to_revision IS NULL;
313
352
  CREATE VIEW current_operands AS SELECT * FROM operands WHERE valid_to_revision IS NULL;
314
353
  CREATE VIEW current_patches AS SELECT * FROM patches WHERE valid_to_revision IS NULL;
354
+ CREATE VIEW current_switches AS SELECT * FROM switches WHERE valid_to_revision IS NULL;
355
+ CREATE VIEW current_releases AS SELECT * FROM releases WHERE valid_to_revision IS NULL;
315
356
  CREATE VIEW current_macros AS SELECT * FROM macros WHERE valid_to_revision IS NULL;
316
357
  CREATE VIEW current_macro_uses AS SELECT * FROM macro_uses WHERE valid_to_revision IS NULL;
317
358
  `);
318
359
  }
319
360
 
320
361
  /** The version of the index this build makes. An index with another version is emptied and made again. */
321
- export const INDEX_VERSION = 8;
362
+ export const INDEX_VERSION = 9;
322
363
 
323
364
  function indexVersion(db: DatabaseSync): number | undefined {
324
365
  const hasMeta = db.prepare("SELECT 1 FROM sqlite_master WHERE type = 'table' AND name = 'meta'").get();
@@ -151,6 +151,28 @@ export const TABLES: readonly TableSpec[] = [
151
151
  origin,
152
152
  ],
153
153
  },
154
+ // Every switch of the patches in one place: its kind, its default and the releases that have it on.
155
+ {
156
+ table: "switches",
157
+ file: "switches.jsonl",
158
+ sort: "name",
159
+ fields: [
160
+ space,
161
+ field("name", "name", "text"),
162
+ field("kind", "kind", "text"),
163
+ field("default", "default_value", "integer"),
164
+ field("releases", "releases", "text"),
165
+ field("comment", "comment", "text", true),
166
+ origin,
167
+ ],
168
+ },
169
+ // The releases of the program: the file of each, and the one whose bytes are the program.
170
+ {
171
+ table: "releases",
172
+ file: "releases.jsonl",
173
+ sort: "label",
174
+ fields: [space, field("label", "label", "text"), field("file", "file", "text"), field("sha256", "sha256", "text"), field("base", "base", "integer"), field("comment", "comment", "text", true), origin],
175
+ },
154
176
  {
155
177
  table: "macros",
156
178
  file: "macros.jsonl",