@henols/c64-re-tools 2.1.8 → 2.2.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.
- package/package.json +1 -1
- package/skills/c64-assembler/SKILL.md +4 -2
- package/skills/c64-assembler/scripts/assembler.ts +24 -12
- package/skills/c64-docs/references/page-types.md +1 -1
- package/skills/c64-knowledge/SKILL.md +66 -13
- package/skills/c64-knowledge/scripts/knowledge.ts +165 -18
- package/skills/c64-provenance/SKILL.md +11 -3
- package/skills/c64-reverse-engineering/SKILL.md +28 -13
- package/skills/c64-source/SKILL.md +33 -13
- package/skills/c64-source/scripts/lib/loader.ts +3 -1
- package/skills/c64-source/scripts/lib/source.ts +151 -37
- package/skills/c64-source/scripts/source.ts +135 -45
- package/skills/c64-unpacker/SKILL.md +4 -1
- package/src/knowledge/conflicts.ts +32 -8
- package/src/knowledge/history.ts +18 -0
- package/src/knowledge/read.ts +98 -4
- package/src/knowledge/schema.ts +43 -2
- package/src/knowledge/text.ts +22 -0
- package/src/knowledge/write.ts +248 -25
package/src/knowledge/schema.ts
CHANGED
|
@@ -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 =
|
|
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();
|
package/src/knowledge/text.ts
CHANGED
|
@@ -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",
|
package/src/knowledge/write.ts
CHANGED
|
@@ -11,12 +11,15 @@ import { transaction } from "./database.ts";
|
|
|
11
11
|
import { labelProblem, MNEMONICS } from "../opcodes.ts";
|
|
12
12
|
import { checkExpression, evaluate, identifiers, operandText, replaceIdentifiers } from "./expression.ts";
|
|
13
13
|
import {
|
|
14
|
+
baseValue,
|
|
14
15
|
COMMENT_COLUMNS,
|
|
15
16
|
commentsAt,
|
|
16
17
|
currentRevision,
|
|
17
18
|
listMacroUses,
|
|
18
19
|
listOperands,
|
|
19
20
|
listPatches,
|
|
21
|
+
listReleases,
|
|
22
|
+
listSwitches,
|
|
20
23
|
listText,
|
|
21
24
|
MACRO_COLUMNS,
|
|
22
25
|
MACRO_USE_COLUMNS,
|
|
@@ -33,6 +36,10 @@ import {
|
|
|
33
36
|
referencesFrom,
|
|
34
37
|
REGION_COLUMNS,
|
|
35
38
|
regionsOverlapping,
|
|
39
|
+
RELEASE_COLUMNS,
|
|
40
|
+
releaseLabeled,
|
|
41
|
+
SWITCH_COLUMNS,
|
|
42
|
+
switchNamed,
|
|
36
43
|
coverageRuns,
|
|
37
44
|
resolveRevision,
|
|
38
45
|
SUPPRESSION_COLUMNS,
|
|
@@ -48,7 +55,9 @@ import {
|
|
|
48
55
|
toPatch,
|
|
49
56
|
toReference,
|
|
50
57
|
toRegion,
|
|
58
|
+
toRelease,
|
|
51
59
|
toSuppression,
|
|
60
|
+
toSwitch,
|
|
52
61
|
toSymbol,
|
|
53
62
|
type CommentRow,
|
|
54
63
|
type CoverageRun,
|
|
@@ -59,10 +68,12 @@ import {
|
|
|
59
68
|
type PatchRow,
|
|
60
69
|
type ReferenceRow,
|
|
61
70
|
type RegionRow,
|
|
71
|
+
type ReleaseRow,
|
|
62
72
|
type SuppressionRow,
|
|
73
|
+
type SwitchRow,
|
|
63
74
|
type SymbolRow,
|
|
64
75
|
} from "./read.ts";
|
|
65
|
-
import { CODE_REGION_TYPES, COMMENT_PLACEMENTS, EVIDENCE_KINDS, MAX_ELEMENT, MODULE_FILE, DEFAULT_SPACE, GENERATED_FILES, isGeneratedFile, isSemantic, MACRO_NAME, MACRO_PARAM, 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 SymbolKind } from "./schema.ts";
|
|
76
|
+
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, 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 SwitchKind, type SymbolKind } from "./schema.ts";
|
|
66
77
|
import { newRevisionId, rowId, sqlName } from "./text.ts";
|
|
67
78
|
|
|
68
79
|
/** What a change rests on: a coverage run, a test or another project file with its SHA-256, or a note. */
|
|
@@ -257,6 +268,18 @@ export class Change {
|
|
|
257
268
|
.run(this.#rowId(), row.space, row.start, row.end, row.switch, row.code, row.comment ?? null, row.origin, this.id());
|
|
258
269
|
}
|
|
259
270
|
|
|
271
|
+
insertSwitch(row: Omit<SwitchRow, "revision">): void {
|
|
272
|
+
this.#db
|
|
273
|
+
.prepare("INSERT INTO switches (id, space, name, kind, default_value, releases, comment, origin, valid_from_revision) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)")
|
|
274
|
+
.run(this.#rowId(), row.space, row.name, row.kind, row.default, listText(row.releases), row.comment ?? null, row.origin, this.id());
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
insertRelease(row: Omit<ReleaseRow, "revision">): void {
|
|
278
|
+
this.#db
|
|
279
|
+
.prepare("INSERT INTO releases (id, space, label, file, sha256, base, comment, origin, valid_from_revision) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)")
|
|
280
|
+
.run(this.#rowId(), row.space, row.label, row.file, row.sha256, row.base ? 1 : 0, row.comment ?? null, row.origin, this.id());
|
|
281
|
+
}
|
|
282
|
+
|
|
260
283
|
insertMacro(row: Omit<MacroRow, "revision">): void {
|
|
261
284
|
this.#db
|
|
262
285
|
.prepare("INSERT INTO macros (id, space, name, params, body, comment, origin, valid_from_revision) VALUES (?, ?, ?, ?, ?, ?, ?, ?)")
|
|
@@ -298,7 +321,8 @@ export class Change {
|
|
|
298
321
|
/** A conflict in words: its rule, its space and the addresses of its rows. */
|
|
299
322
|
function describe(conflict: Conflict): string {
|
|
300
323
|
const place = (entry: ConflictRow) => {
|
|
301
|
-
if (entry.entity === "macro") return entry.row.name;
|
|
324
|
+
if (entry.entity === "macro" || entry.entity === "switch") return entry.row.name;
|
|
325
|
+
if (entry.entity === "release") return entry.row.label;
|
|
302
326
|
const row = entry.row as { address?: number; start?: number; end?: number; from?: number };
|
|
303
327
|
if (row.address !== undefined) return formatC64Address(row.address);
|
|
304
328
|
if (row.from !== undefined && entry.entity === "reference") return formatC64Address(row.from);
|
|
@@ -407,8 +431,9 @@ function renameStep(db: DatabaseSync, change: Change, origin: Origin, request: S
|
|
|
407
431
|
if (owner !== undefined && owner.address !== request.address) {
|
|
408
432
|
throw new Refusal("conflict", `The name ${request.name} already belongs to ${formatC64Address(owner.address)}. Rename or remove that symbol first.`);
|
|
409
433
|
}
|
|
410
|
-
|
|
411
|
-
|
|
434
|
+
if (switchNamed(db, request.name, space) !== undefined) throw new Refusal("conflict", `${request.name} is the name of a switch. Give the symbol another name.`);
|
|
435
|
+
const release = request.name.startsWith(RELEASE_DEFINE) ? releaseLabeled(db, request.name.slice(RELEASE_DEFINE.length), space) : undefined;
|
|
436
|
+
if (release !== undefined) throw new Refusal("conflict", `${request.name} builds the release ${release.label}. Give the symbol another name.`);
|
|
412
437
|
if (existing !== undefined) change.close("symbols", "space = ? AND address = ?", space, request.address);
|
|
413
438
|
change.insertSymbol({ space, address: request.address, name: request.name, kind, origin });
|
|
414
439
|
if (existing !== undefined && existing.name !== request.name) {
|
|
@@ -777,11 +802,15 @@ function checkInImage(image: OperandImage, start: number, end: number, what: str
|
|
|
777
802
|
|
|
778
803
|
type PatchRequest = InSpace & { start: number; end: number; switch: string; code: string; comment?: string };
|
|
779
804
|
|
|
805
|
+
function checkSwitchName(name: unknown, what = "A switch name"): void {
|
|
806
|
+
if (typeof name !== "string" || !SWITCH_NAME.test(name)) {
|
|
807
|
+
throw new Refusal("invalid-input", `${what} is an upper-case letter, then 1 to 31 upper-case letters, digits or underscores, for example PROTECTION.`);
|
|
808
|
+
}
|
|
809
|
+
}
|
|
810
|
+
|
|
780
811
|
function checkPatch(request: PatchRequest): void {
|
|
781
812
|
checkRange(request);
|
|
782
|
-
|
|
783
|
-
throw new Refusal("invalid-input", "A switch name is an upper-case letter, then 1 to 31 upper-case letters, digits or underscores, for example CRACK.");
|
|
784
|
-
}
|
|
813
|
+
checkSwitchName(request.switch, "The switch of a patch");
|
|
785
814
|
if (typeof request.code !== "string" || request.code.length > MAX_CODE) throw new Refusal("invalid-input", `Patch code has at most ${MAX_CODE} characters; empty code fills the range with NOP.`);
|
|
786
815
|
checkBraces(request.code, "The patch code");
|
|
787
816
|
checkCommentText(request.comment, "A patch comment");
|
|
@@ -837,34 +866,183 @@ function patchStep(db: DatabaseSync, change: Change, context: ChangeContext & {
|
|
|
837
866
|
if (least !== undefined && least > length) {
|
|
838
867
|
throw new Refusal("invalid-input", `The patch code is at least ${least} bytes, and the range ${range} has ${length}. Make the code shorter, or make the range larger.`);
|
|
839
868
|
}
|
|
840
|
-
const
|
|
841
|
-
if (
|
|
842
|
-
|
|
843
|
-
|
|
844
|
-
const other = patchesOverlapping(db, request.start, request.end, space).find((row) => row.start !== request.start || row.
|
|
869
|
+
const owner = switchNamed(db, request.switch, space);
|
|
870
|
+
if (owner === undefined) throw new Refusal("not-found", `There is no switch ${request.switch}. Write it first (switch ${request.switch} --kind <kind> --releases <labels>).`);
|
|
871
|
+
// A write at the start of a patch of the same switch changes that patch. Another switch can have the same range: an alternative.
|
|
872
|
+
const mine = (row: PatchRow) => row.start === request.start && row.switch === request.switch;
|
|
873
|
+
const other = patchesOverlapping(db, request.start, request.end, space).find((row) => !mine(row) && (row.start !== request.start || row.end !== request.end || row.switch === request.switch));
|
|
845
874
|
if (other !== undefined) {
|
|
846
|
-
throw new Refusal(
|
|
875
|
+
throw new Refusal(
|
|
876
|
+
"conflict",
|
|
877
|
+
`The patch ${range} of ${request.switch} overlaps the patch of ${other.switch} at ${formatC64Address(other.start)}-${formatC64Address(other.end)}. Patches of different switches can have the same range, as alternatives; no other overlap is allowed. Change or remove that patch first (unpatch).`,
|
|
878
|
+
);
|
|
847
879
|
}
|
|
848
880
|
const [use] = macroUsesOverlapping(db, request.start, request.end, space);
|
|
849
881
|
if (use !== undefined) {
|
|
850
882
|
throw new Refusal("conflict", `The patch ${range} overlaps the use of the macro ${use.macro} at ${formatC64Address(use.start)}-${formatC64Address(use.end)}. Remove that use first (unuse).`);
|
|
851
883
|
}
|
|
852
|
-
const
|
|
884
|
+
const find = () => patchesOverlapping(db, request.start, request.start, space).find(mine);
|
|
885
|
+
const existing = find();
|
|
853
886
|
const code = request.code.trimEnd();
|
|
854
887
|
// A patch write without a comment keeps the comment it has, as a module keeps its title.
|
|
855
888
|
const comment = request.comment === undefined ? existing?.comment : request.comment.trim();
|
|
856
889
|
const origin = context.origin as PatchRow["origin"];
|
|
857
|
-
if (existing !== undefined && existing.end === request.end && existing.
|
|
858
|
-
if (existing !== undefined) change.close("patches", "space = ? AND start_address = ?", space, request.start);
|
|
890
|
+
if (existing !== undefined && existing.end === request.end && existing.code === code && existing.comment === comment && !confirms(existing.origin, origin)) return { current: existing };
|
|
891
|
+
if (existing !== undefined) change.close("patches", "space = ? AND start_address = ? AND switch_name = ?", space, request.start, request.switch);
|
|
859
892
|
change.insertPatch({ space, start: request.start, end: request.end, switch: request.switch, code, ...(comment === undefined ? {} : { comment }), origin });
|
|
860
|
-
|
|
893
|
+
checkAlternatives(db, space, [owner]);
|
|
894
|
+
const current = find()!;
|
|
895
|
+
return existing !== undefined && existing.origin !== origin ? { current, previous: existing } : { current };
|
|
896
|
+
}
|
|
897
|
+
|
|
898
|
+
function unpatchStep(db: DatabaseSync, change: Change, _origin: Origin, request: InSpace & { start: number; switch?: string }): Step<null> {
|
|
899
|
+
const space = spaceOf(request);
|
|
900
|
+
const found = patchesOverlapping(db, request.start, request.start, space).filter((row) => row.start === request.start && (request.switch === undefined || row.switch === request.switch));
|
|
901
|
+
if (found.length === 0) throw new Refusal("not-found", `No patch${request.switch === undefined ? "" : ` of ${request.switch}`} starts at ${formatC64Address(request.start)}.`);
|
|
902
|
+
if (found.length > 1) throw new Refusal("invalid-input", `The patches of ${found.map((row) => row.switch).join(", ")} start at ${formatC64Address(request.start)}. Give the switch of the patch to remove.`);
|
|
903
|
+
change.close("patches", "space = ? AND start_address = ? AND switch_name = ?", space, request.start, found[0]!.switch);
|
|
904
|
+
return { current: null };
|
|
905
|
+
}
|
|
906
|
+
|
|
907
|
+
/** The label of the base release of a space: its bytes are the program of the knowledge. */
|
|
908
|
+
function baseLabel(db: DatabaseSync, space: Space): string | undefined {
|
|
909
|
+
return listReleases(db, undefined, space).find((release) => release.base)?.label;
|
|
910
|
+
}
|
|
911
|
+
|
|
912
|
+
/**
|
|
913
|
+
* Refuses alternatives that are both on in the default build: a patch is on
|
|
914
|
+
* when its switch does not have its base value. Two patches of one range can
|
|
915
|
+
* not both be on, so the default build must choose one.
|
|
916
|
+
*/
|
|
917
|
+
function checkAlternatives(db: DatabaseSync, space: Space, changed: readonly SwitchRow[]): void {
|
|
918
|
+
const base = baseLabel(db, space);
|
|
919
|
+
const switches = new Map(listSwitches(db, undefined, space).map((row) => [row.name, row]));
|
|
920
|
+
const names = new Set(changed.map((row) => row.name));
|
|
921
|
+
const ranges = new Map<string, PatchRow[]>();
|
|
922
|
+
for (const patch of listPatches(db, undefined, undefined, undefined, space)) ranges.set(`${patch.start}-${patch.end}`, [...(ranges.get(`${patch.start}-${patch.end}`) ?? []), patch]);
|
|
923
|
+
for (const patches of ranges.values()) {
|
|
924
|
+
if (patches.length < 2 || !patches.some((patch) => names.has(patch.switch))) continue;
|
|
925
|
+
const on = patches.filter((patch) => {
|
|
926
|
+
const owner = switches.get(patch.switch)!;
|
|
927
|
+
return owner.default !== baseValue(owner, base);
|
|
928
|
+
});
|
|
929
|
+
if (on.length < 2) continue;
|
|
930
|
+
const range = `${formatC64Address(patches[0]!.start)}-${formatC64Address(patches[0]!.end)}`;
|
|
931
|
+
throw new Refusal(
|
|
932
|
+
"conflict",
|
|
933
|
+
`The switches ${on.map((patch) => patch.switch).join(" and ")} have alternative patches at ${range}, and the default build puts in both of them. Give one of the switches the default of the base release (switch <name> --default).`,
|
|
934
|
+
);
|
|
935
|
+
}
|
|
936
|
+
}
|
|
937
|
+
|
|
938
|
+
type SwitchRequest = InSpace & { name: string; kind: SwitchKind; default?: 0 | 1; releases?: string[]; comment?: string };
|
|
939
|
+
|
|
940
|
+
function checkSwitch(request: SwitchRequest): void {
|
|
941
|
+
checkSpace(request);
|
|
942
|
+
checkSwitchName(request.name);
|
|
943
|
+
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>.`);
|
|
944
|
+
checkOneOf(SWITCH_KINDS, request.kind, "kind");
|
|
945
|
+
if (request.default !== undefined && request.default !== 0 && request.default !== 1) throw new Refusal("invalid-input", "default must be 0 or 1.");
|
|
946
|
+
if (OFF_KINDS.includes(request.kind) && request.default === 1) {
|
|
947
|
+
throw new Refusal("invalid-input", `A ${request.kind} switch is off in the default build: the default build is the clean program. Build a release, or define the switch, to put it in.`);
|
|
948
|
+
}
|
|
949
|
+
for (const label of request.releases ?? []) if (typeof label !== "string" || !RELEASE_LABEL.test(label)) throw new Refusal("invalid-input", `${JSON.stringify(label)} is not a release label.`);
|
|
950
|
+
checkCommentText(request.comment, "A switch comment");
|
|
951
|
+
}
|
|
952
|
+
|
|
953
|
+
/** The default of a switch: a fix is on, the kinds of OFF_KINDS are off, and a change needs one. */
|
|
954
|
+
function defaultOf(request: SwitchRequest, existing: SwitchRow | undefined): 0 | 1 {
|
|
955
|
+
if (request.default !== undefined) return request.default;
|
|
956
|
+
if (existing !== undefined && existing.kind === request.kind) return existing.default;
|
|
957
|
+
if (request.kind === "fix") return 1;
|
|
958
|
+
if (OFF_KINDS.includes(request.kind)) return 0;
|
|
959
|
+
throw new Refusal("invalid-input", `A ${request.kind} switch needs a default: give --default 0 or --default 1.`);
|
|
960
|
+
}
|
|
961
|
+
|
|
962
|
+
function switchStep(db: DatabaseSync, change: Change, context: ChangeContext & { image?: OperandImage }, request: SwitchRequest): Step<SwitchRow, SwitchRow> {
|
|
963
|
+
const space = spaceOf(request);
|
|
964
|
+
const base = baseLabel(db, space);
|
|
965
|
+
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).");
|
|
966
|
+
const existing = switchNamed(db, request.name, space);
|
|
967
|
+
const releases = [...new Set(request.releases ?? existing?.releases ?? [])].sort();
|
|
968
|
+
for (const label of releases) if (releaseLabeled(db, label, space) === undefined) throw new Refusal("not-found", `There is no release ${label}. Record it first (release ${label} --file <file>).`);
|
|
969
|
+
const value = defaultOf(request, existing);
|
|
970
|
+
const comment = request.comment === undefined ? existing?.comment : request.comment.trim();
|
|
971
|
+
const origin = context.origin as SwitchRow["origin"];
|
|
972
|
+
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 };
|
|
973
|
+
const symbol = symbolNamed(db, request.name, space);
|
|
974
|
+
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.`);
|
|
975
|
+
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.`);
|
|
976
|
+
// The patches hold the code of the value that the base release does not have: that value cannot change under them.
|
|
977
|
+
if (existing !== undefined && baseValue(existing, base) !== baseValue({ releases }, base)) {
|
|
978
|
+
const patches = listPatches(db, undefined, request.name, undefined, space);
|
|
979
|
+
if (patches.length > 0) {
|
|
980
|
+
throw new Refusal(
|
|
981
|
+
"conflict",
|
|
982
|
+
`The switch ${request.name} has patches at ${patches.map((row) => formatC64Address(row.start)).join(", ")}, and this write changes whether the base release ${base} has it on. The patches would then hold the wrong state. Remove them first (unpatch), then write them again.`,
|
|
983
|
+
);
|
|
984
|
+
}
|
|
985
|
+
}
|
|
986
|
+
if (existing !== undefined) change.close("switches", "space = ? AND name = ?", space, request.name);
|
|
987
|
+
change.insertSwitch({ space, name: request.name, kind: request.kind, default: value, releases, ...(comment === undefined ? {} : { comment }), origin });
|
|
988
|
+
const current = switchNamed(db, request.name, space)!;
|
|
989
|
+
checkAlternatives(db, space, [current]);
|
|
861
990
|
return existing !== undefined && existing.origin !== origin ? { current, previous: existing } : { current };
|
|
862
991
|
}
|
|
863
992
|
|
|
864
|
-
function
|
|
993
|
+
function unswitchStep(db: DatabaseSync, change: Change, _origin: Origin, request: InSpace & { name: string }): Step<null> {
|
|
865
994
|
const space = spaceOf(request);
|
|
866
|
-
if (
|
|
867
|
-
|
|
995
|
+
if (switchNamed(db, request.name, space) === undefined) throw new Refusal("not-found", `There is no switch ${request.name}.`);
|
|
996
|
+
const patches = listPatches(db, undefined, request.name, undefined, space);
|
|
997
|
+
if (patches.length > 0) throw new Refusal("conflict", `The switch ${request.name} has patches at ${patches.map((row) => formatC64Address(row.start)).join(", ")}. Remove them first (unpatch).`);
|
|
998
|
+
change.close("switches", "space = ? AND name = ?", space, request.name);
|
|
999
|
+
return { current: null };
|
|
1000
|
+
}
|
|
1001
|
+
|
|
1002
|
+
/** A release: `file` is a project path and `sha256` the hash of its bytes, both from the caller. */
|
|
1003
|
+
type ReleaseRequest = InSpace & { label: string; file: string; sha256: string; base?: boolean; comment?: string };
|
|
1004
|
+
|
|
1005
|
+
function checkRelease(request: ReleaseRequest): void {
|
|
1006
|
+
checkSpace(request);
|
|
1007
|
+
if (typeof request.label !== "string" || !RELEASE_LABEL.test(request.label)) {
|
|
1008
|
+
throw new Refusal("invalid-input", "A release label is an upper-case letter, then 1 to 31 upper-case letters, digits or underscores, for example ORIGINAL or FIRESTAR.");
|
|
1009
|
+
}
|
|
1010
|
+
if (typeof request.file !== "string" || request.file.trim().length === 0 || request.file.length > MAX_EVIDENCE_REF) throw new Refusal("invalid-input", `The file of a release has 1 to ${MAX_EVIDENCE_REF} characters.`);
|
|
1011
|
+
if (!/^[0-9a-f]{64}$/.test(request.sha256)) throw new Refusal("invalid-input", "The sha256 of a release is 64 lowercase hex digits.");
|
|
1012
|
+
checkCommentText(request.comment, "A release comment");
|
|
1013
|
+
}
|
|
1014
|
+
|
|
1015
|
+
function releaseStep(db: DatabaseSync, change: Change, origin: Origin, request: ReleaseRequest): Step<ReleaseRow, ReleaseRow> {
|
|
1016
|
+
const space = spaceOf(request);
|
|
1017
|
+
const existing = releaseLabeled(db, request.label, space);
|
|
1018
|
+
const base = request.base ?? existing?.base ?? false;
|
|
1019
|
+
const comment = request.comment === undefined ? existing?.comment : request.comment.trim();
|
|
1020
|
+
if (existing !== undefined && existing.file === request.file && existing.sha256 === request.sha256 && existing.base === base && existing.comment === comment && !confirms(existing.origin, origin)) return { current: existing };
|
|
1021
|
+
const other = baseLabel(db, space);
|
|
1022
|
+
if (base && other !== undefined && other !== request.label) throw new Refusal("conflict", `The release ${other} is the base release. A space has one base release.`);
|
|
1023
|
+
// Every switch takes its base value from the base release: it cannot change under them.
|
|
1024
|
+
if ((existing?.base ?? false) !== base) {
|
|
1025
|
+
const switches = listSwitches(db, undefined, space);
|
|
1026
|
+
if (switches.length > 0) throw new Refusal("conflict", `The base release decides the state of the program for the switches ${switches.map((row) => row.name).join(", ")}. Remove them first (unswitch) to change the base release.`);
|
|
1027
|
+
}
|
|
1028
|
+
const symbol = symbolNamed(db, `${RELEASE_DEFINE}${request.label}`, space);
|
|
1029
|
+
if (symbol !== undefined) throw new Refusal("conflict", `The build of the release defines ${RELEASE_DEFINE}${request.label}, and that is the name of the symbol at ${formatC64Address(symbol.address)}. Give the release another label.`);
|
|
1030
|
+
if (existing !== undefined) change.close("releases", "space = ? AND label = ?", space, request.label);
|
|
1031
|
+
change.insertRelease({ space, label: request.label, file: request.file, sha256: request.sha256, base, ...(comment === undefined ? {} : { comment }), origin: origin as ReleaseRow["origin"] });
|
|
1032
|
+
const current = releaseLabeled(db, request.label, space)!;
|
|
1033
|
+
return existing !== undefined && existing.origin !== origin ? { current, previous: existing } : { current };
|
|
1034
|
+
}
|
|
1035
|
+
|
|
1036
|
+
function unreleaseStep(db: DatabaseSync, change: Change, _origin: Origin, request: InSpace & { label: string }): Step<null> {
|
|
1037
|
+
const space = spaceOf(request);
|
|
1038
|
+
const existing = releaseLabeled(db, request.label, space);
|
|
1039
|
+
if (existing === undefined) throw new Refusal("not-found", `There is no release ${request.label}.`);
|
|
1040
|
+
const switches = listSwitches(db, undefined, space).filter((row) => existing.base || row.releases.includes(request.label));
|
|
1041
|
+
if (switches.length > 0) {
|
|
1042
|
+
const why = existing.base ? "decides the state of the program for" : "is named by";
|
|
1043
|
+
throw new Refusal("conflict", `The release ${request.label} ${why} the switches ${switches.map((row) => row.name).join(", ")}. Change or remove them first (switch, unswitch).`);
|
|
1044
|
+
}
|
|
1045
|
+
change.close("releases", "space = ? AND label = ?", space, request.label);
|
|
868
1046
|
return { current: null };
|
|
869
1047
|
}
|
|
870
1048
|
|
|
@@ -1069,21 +1247,45 @@ export function removeOperand(db: DatabaseSync, context: WriteContext, request:
|
|
|
1069
1247
|
}
|
|
1070
1248
|
|
|
1071
1249
|
/**
|
|
1072
|
-
* Writes code that replaces [start, end] when
|
|
1073
|
-
|
|
1074
|
-
*
|
|
1250
|
+
* Writes code that replaces [start, end] when its switch does not have its
|
|
1251
|
+
* base value, after a check that the switch exists, the range is in the image,
|
|
1252
|
+
* and it overlaps no macro use and no patch other than an alternative.
|
|
1075
1253
|
*/
|
|
1076
1254
|
export function setPatch(db: DatabaseSync, context: WriteContext, request: PatchRequest): WriteResult<PatchRow, PatchRow> {
|
|
1077
1255
|
checkPatch(request);
|
|
1078
1256
|
return result(write(db, context, "set-patch", (change) => patchStep(db, change, context, request)));
|
|
1079
1257
|
}
|
|
1080
1258
|
|
|
1081
|
-
export function removePatch(db: DatabaseSync, context: WriteContext, request: InSpace & { start: number }): WriteResult<null> {
|
|
1259
|
+
export function removePatch(db: DatabaseSync, context: WriteContext, request: InSpace & { start: number; switch?: string }): WriteResult<null> {
|
|
1082
1260
|
checkSpace(request);
|
|
1083
1261
|
checkAddress(request.start, "start");
|
|
1262
|
+
if (request.switch !== undefined) checkSwitchName(request.switch);
|
|
1084
1263
|
return result(write(db, context, "remove-patch", (change) => unpatchStep(db, change, context.origin, request)));
|
|
1085
1264
|
}
|
|
1086
1265
|
|
|
1266
|
+
/** Writes a switch: its kind, its default and the releases that have it on. The knowledge needs a base release first. */
|
|
1267
|
+
export function setSwitch(db: DatabaseSync, context: WriteContext, request: SwitchRequest): WriteResult<SwitchRow, SwitchRow> {
|
|
1268
|
+
checkSwitch(request);
|
|
1269
|
+
return result(write(db, context, "set-switch", (change) => switchStep(db, change, context, request)));
|
|
1270
|
+
}
|
|
1271
|
+
|
|
1272
|
+
export function removeSwitch(db: DatabaseSync, context: WriteContext, request: InSpace & { name: string }): WriteResult<null> {
|
|
1273
|
+
checkSpace(request);
|
|
1274
|
+
checkSwitchName(request.name);
|
|
1275
|
+
return result(write(db, context, "remove-switch", (change) => unswitchStep(db, change, context.origin, request)));
|
|
1276
|
+
}
|
|
1277
|
+
|
|
1278
|
+
/** Writes a release of the program: its project file and the SHA-256 of it. The caller checks the file. */
|
|
1279
|
+
export function setRelease(db: DatabaseSync, context: WriteContext, request: ReleaseRequest): WriteResult<ReleaseRow, ReleaseRow> {
|
|
1280
|
+
checkRelease(request);
|
|
1281
|
+
return result(write(db, context, "set-release", (change) => releaseStep(db, change, context.origin, request)));
|
|
1282
|
+
}
|
|
1283
|
+
|
|
1284
|
+
export function removeRelease(db: DatabaseSync, context: WriteContext, request: InSpace & { label: string }): WriteResult<null> {
|
|
1285
|
+
checkSpace(request);
|
|
1286
|
+
return result(write(db, context, "remove-release", (change) => unreleaseStep(db, change, context.origin, request)));
|
|
1287
|
+
}
|
|
1288
|
+
|
|
1087
1289
|
/** Writes a macro of the generated source. */
|
|
1088
1290
|
export function setMacro(db: DatabaseSync, context: WriteContext, request: MacroRequest): WriteResult<MacroRow, MacroRow> {
|
|
1089
1291
|
checkMacro(request);
|
|
@@ -1123,7 +1325,7 @@ export type BatchOperation =
|
|
|
1123
1325
|
| ({ op: "operand" } & OperandRequest)
|
|
1124
1326
|
| ({ op: "unoperand" } & AddressRequest)
|
|
1125
1327
|
| ({ op: "patch" } & PatchRequest)
|
|
1126
|
-
| ({ op: "unpatch" } & InSpace & { start: number })
|
|
1328
|
+
| ({ op: "unpatch" } & InSpace & { start: number; switch?: string })
|
|
1127
1329
|
| ({ op: "macro" } & MacroRequest)
|
|
1128
1330
|
| ({ op: "unmacro" } & InSpace & { name: string })
|
|
1129
1331
|
| ({ op: "use" } & MacroUseRequest)
|
|
@@ -1175,6 +1377,9 @@ export function applyBatch(db: DatabaseSync, context: WriteContext, operations:
|
|
|
1175
1377
|
case "patch":
|
|
1176
1378
|
return checkPatch(operation);
|
|
1177
1379
|
case "unpatch":
|
|
1380
|
+
if (operation.switch !== undefined) checkSwitchName(operation.switch);
|
|
1381
|
+
checkSpace(operation);
|
|
1382
|
+
return checkAddress(operation.start, "start");
|
|
1178
1383
|
case "unuse":
|
|
1179
1384
|
checkSpace(operation);
|
|
1180
1385
|
return checkAddress(operation.start, "start");
|
|
@@ -1271,6 +1476,16 @@ const KEYS = {
|
|
|
1271
1476
|
map: toPatch,
|
|
1272
1477
|
key: (row: PatchRow) => `${row.space}|${row.start}|${row.end}|${row.switch}|${row.code}|${row.comment ?? ""}|${row.origin}`,
|
|
1273
1478
|
},
|
|
1479
|
+
switches: {
|
|
1480
|
+
columns: SWITCH_COLUMNS,
|
|
1481
|
+
map: toSwitch,
|
|
1482
|
+
key: (row: SwitchRow) => `${row.space}|${row.name}|${row.kind}|${row.default}|${listText(row.releases)}|${row.comment ?? ""}|${row.origin}`,
|
|
1483
|
+
},
|
|
1484
|
+
releases: {
|
|
1485
|
+
columns: RELEASE_COLUMNS,
|
|
1486
|
+
map: toRelease,
|
|
1487
|
+
key: (row: ReleaseRow) => `${row.space}|${row.label}|${row.file}|${row.sha256}|${row.base}|${row.comment ?? ""}|${row.origin}`,
|
|
1488
|
+
},
|
|
1274
1489
|
macros: { columns: MACRO_COLUMNS, map: toMacro, key: (row: MacroRow) => `${row.space}|${row.name}|${listText(row.params)}|${row.body}|${row.comment ?? ""}|${row.origin}` },
|
|
1275
1490
|
macro_uses: { columns: MACRO_USE_COLUMNS, map: toMacroUse, key: (row: MacroUseRow) => `${row.space}|${row.start}|${row.end}|${row.macro}|${listText(row.args)}|${row.origin}` },
|
|
1276
1491
|
suppressions: {
|
|
@@ -1328,6 +1543,14 @@ export function revert(db: DatabaseSync, context: WriteContext, request: { revis
|
|
|
1328
1543
|
change.insertMacro(spec.map(row as never) as MacroRow);
|
|
1329
1544
|
continue;
|
|
1330
1545
|
}
|
|
1546
|
+
if (table === "switches") {
|
|
1547
|
+
change.insertSwitch(spec.map(row as never) as SwitchRow);
|
|
1548
|
+
continue;
|
|
1549
|
+
}
|
|
1550
|
+
if (table === "releases") {
|
|
1551
|
+
change.insertRelease(spec.map(row as never) as ReleaseRow);
|
|
1552
|
+
continue;
|
|
1553
|
+
}
|
|
1331
1554
|
if (table === "macro_uses") {
|
|
1332
1555
|
change.insertMacroUse(spec.map(row as never) as MacroUseRow);
|
|
1333
1556
|
continue;
|