@henols/c64-re-tools 2.7.1 → 2.8.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.
- package/package.json +1 -1
- package/skills/c64-assembler/SKILL.md +23 -38
- package/skills/c64-assembler/scripts/assembler.ts +137 -17
- package/skills/c64-basic/SKILL.md +11 -31
- package/skills/c64-basic/scripts/basic.ts +4 -1
- package/skills/c64-development/SKILL.md +2 -2
- package/skills/c64-disk/SKILL.md +11 -32
- package/skills/c64-docs/SKILL.md +27 -45
- package/skills/c64-docs/references/page-types.md +20 -18
- package/skills/c64-docs/references/style.md +10 -4
- package/skills/c64-docs/scripts/docs.ts +13 -2
- package/skills/c64-docs/scripts/lib/reference.ts +47 -5
- package/skills/c64-knowledge/SKILL.md +138 -267
- package/skills/c64-knowledge/references/batches.md +48 -0
- package/skills/c64-knowledge/references/coverage.md +29 -0
- package/skills/c64-knowledge/references/source-knowledge.md +72 -0
- package/skills/c64-knowledge/references/start-screen.md +44 -0
- package/skills/c64-knowledge/scripts/knowledge.ts +266 -61
- package/skills/c64-knowledge/scripts/lib/check.ts +133 -0
- package/skills/c64-knowledge/scripts/lib/listing.ts +71 -8
- package/skills/c64-memory-map/SKILL.md +11 -32
- package/skills/c64-packer/SKILL.md +11 -31
- package/skills/c64-provenance/SKILL.md +43 -64
- package/skills/c64-provenance/scripts/lib/leftovers.ts +8 -3
- package/skills/c64-provenance/scripts/provenance.ts +65 -56
- package/skills/c64-reverse-engineering/SKILL.md +20 -47
- package/skills/c64-reverse-engineering/references/comment-check.md +42 -0
- package/skills/c64-reverse-engineering/references/versions.md +47 -0
- package/skills/c64-source/SKILL.md +48 -237
- package/skills/c64-source/references/export.md +50 -0
- package/skills/c64-source/references/loader.md +78 -0
- package/skills/c64-source/references/source-format.md +46 -0
- package/skills/c64-source/scripts/lib/export.ts +28 -13
- package/skills/c64-source/scripts/lib/loader.ts +4 -82
- package/skills/c64-source/scripts/lib/source.ts +3 -5
- package/skills/c64-source/scripts/source.ts +48 -51
- package/skills/c64-static-analysis/SKILL.md +52 -52
- package/skills/c64-static-analysis/scripts/lib/coverage.ts +55 -2
- package/skills/c64-static-analysis/scripts/lib/idioms.ts +43 -17
- package/skills/c64-static-analysis/scripts/static-analysis.ts +79 -15
- package/skills/c64-testing/SKILL.md +72 -58
- package/skills/c64-testing/references/recording-format.md +5 -80
- package/skills/c64-testing/references/scenario-format.md +4 -12
- package/skills/c64-testing/scripts/lib/play.ts +16 -8
- package/skills/c64-testing/scripts/lib/recording.ts +74 -9
- package/skills/c64-testing/scripts/lib/replay.ts +4 -0
- package/skills/c64-testing/scripts/lib/runs.ts +112 -1
- package/skills/c64-testing/scripts/lib/scenario.ts +12 -6
- package/skills/c64-testing/scripts/lib/start-screen.ts +27 -0
- package/skills/c64-testing/scripts/testing.ts +34 -13
- package/skills/c64-unpacker/SKILL.md +10 -30
- package/skills/emulator/SKILL.md +7 -36
- package/src/build-name.ts +12 -1
- package/src/c64.ts +16 -0
- package/src/knowledge/history.ts +8 -0
- package/src/knowledge/program.ts +84 -4
- package/src/knowledge/read.ts +46 -2
- package/src/knowledge/recording-files.ts +32 -0
- package/src/knowledge/schema.ts +3 -1
- package/src/knowledge/separate.ts +165 -11
- package/src/knowledge/start-screen.ts +99 -17
- package/src/knowledge/text.ts +2 -1
- package/src/knowledge/write.ts +15 -8
- package/src/loader.ts +167 -0
- package/src/native/decompiler/analyze.ts +7 -6
- package/src/native/decompiler/walk.ts +32 -2
- package/src/native/dxa.ts +4 -4
- package/src/native/types.ts +34 -4
- package/src/script.ts +102 -25
- package/skills/c64-export/SKILL.md +0 -63
package/src/knowledge/program.ts
CHANGED
|
@@ -7,7 +7,7 @@ import { createHash } from "node:crypto";
|
|
|
7
7
|
import type { DatabaseSync } from "node:sqlite";
|
|
8
8
|
|
|
9
9
|
import { Refusal } from "../errors.ts";
|
|
10
|
-
import { type AddressRange, COVERAGE, formatC64Address } from "../c64.ts";
|
|
10
|
+
import { type AddressRange, COVERAGE, formatC64Address, formatC64Byte } from "../c64.ts";
|
|
11
11
|
|
|
12
12
|
import { COVERAGE_KINDS, DEFAULT_SPACE, MEMORY_ROW, type CoverageKind, type Space } from "./schema.ts";
|
|
13
13
|
import { coverageRuns } from "./read.ts";
|
|
@@ -145,14 +145,19 @@ function rangesText(ranges: readonly AddressRange[]): string {
|
|
|
145
145
|
return ranges.map((range) => (range.start === range.end ? at(range.start) : `${at(range.start)}-${at(range.end)}`)).join(" ");
|
|
146
146
|
}
|
|
147
147
|
|
|
148
|
-
/**
|
|
149
|
-
function
|
|
148
|
+
/** Calls `each` with the start and the end of each range of ranges as text. */
|
|
149
|
+
function forRanges(text: string, each: (start: number, end: number) => void): void {
|
|
150
150
|
for (const item of text.split(" ")) {
|
|
151
151
|
const [start, end] = item.split("-");
|
|
152
|
-
|
|
152
|
+
each(Number.parseInt(start!.slice(1), 16), Number.parseInt((end ?? start)!.slice(1), 16));
|
|
153
153
|
}
|
|
154
154
|
}
|
|
155
155
|
|
|
156
|
+
/** Sets 1 in `map` at each address of ranges as text. */
|
|
157
|
+
function fillRanges(map: Uint8Array, text: string): void {
|
|
158
|
+
forRanges(text, (start, end) => map.fill(1, start, end + 1));
|
|
159
|
+
}
|
|
160
|
+
|
|
156
161
|
/** The union of the coverage of one space: a map of each kind, 1 at each address that a map of the space has. */
|
|
157
162
|
function currentCoverage(db: DatabaseSync, space: Space): Map<CoverageKind, Uint8Array> {
|
|
158
163
|
const maps = new Map<CoverageKind, Uint8Array>(COVERAGE_KINDS.map((kind) => [kind, new Uint8Array(SIZE)]));
|
|
@@ -241,6 +246,55 @@ export function addCoverage(
|
|
|
241
246
|
});
|
|
242
247
|
}
|
|
243
248
|
|
|
249
|
+
/** What the runs did at an address: executed (x wins), read and written (rw), read, written, or nothing (-). */
|
|
250
|
+
export type Access = "x" | "rw" | "r" | "w" | "-";
|
|
251
|
+
|
|
252
|
+
/** The bit of each coverage kind in a mask of kinds. */
|
|
253
|
+
const kindBit = (kind: CoverageKind) => 1 << COVERAGE_KINDS.indexOf(kind);
|
|
254
|
+
const maskOf = (kinds: readonly CoverageKind[]) => kinds.reduce((mask, kind) => mask | kindBit(kind), 0);
|
|
255
|
+
|
|
256
|
+
/** The kinds of coverage ranges that count as an execute, a read and a write: RAM, ROM and the I/O area. */
|
|
257
|
+
const EXECUTES = maskOf(["x", "rom-x", "io-x"]);
|
|
258
|
+
const READS = maskOf(["r", "rom-r", "io-r"]);
|
|
259
|
+
const WRITES = maskOf(["w", "io-w"]);
|
|
260
|
+
|
|
261
|
+
/** The access of a mask of coverage kinds: an execute wins, then a read and a write together. */
|
|
262
|
+
function accessOf(kinds: number): Access {
|
|
263
|
+
if ((kinds & EXECUTES) !== 0) return "x";
|
|
264
|
+
if ((kinds & READS) !== 0) return (kinds & WRITES) !== 0 ? "rw" : "r";
|
|
265
|
+
return (kinds & WRITES) !== 0 ? "w" : "-";
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
/**
|
|
269
|
+
* The access of the runs at an address in a space: the access of each coverage
|
|
270
|
+
* map that the space has (by its path, also a map without accesses), and the
|
|
271
|
+
* joined access of all maps. Undefined when the space has no coverage map.
|
|
272
|
+
*/
|
|
273
|
+
export function coverageAccess(db: DatabaseSync | undefined, space: Space = DEFAULT_SPACE): ((address: number) => { maps: Array<{ map: string; access: Access }>; access: Access }) | undefined {
|
|
274
|
+
if (db === undefined) return undefined;
|
|
275
|
+
const maps = [...new Set(coverageRuns(db).filter((run) => run.space === space).map((run) => run.name))];
|
|
276
|
+
if (maps.length === 0) return undefined;
|
|
277
|
+
// The kinds of each map at each address, as a mask: each lookup then reads one value for each map.
|
|
278
|
+
const masks = new Map(maps.map((map) => [map, new Uint16Array(SIZE)]));
|
|
279
|
+
for (const row of db.prepare("SELECT map, kind, ranges FROM current_coverage WHERE space = ?").all(space) as Array<{ map: string; kind: CoverageKind; ranges: string }>) {
|
|
280
|
+
const mask = masks.get(row.map);
|
|
281
|
+
if (mask === undefined) continue;
|
|
282
|
+
const bit = kindBit(row.kind);
|
|
283
|
+
forRanges(row.ranges, (start, end) => {
|
|
284
|
+
for (let address = start; address <= end; address++) mask[address]! |= bit;
|
|
285
|
+
});
|
|
286
|
+
}
|
|
287
|
+
return (address) => {
|
|
288
|
+
let all = 0;
|
|
289
|
+
const each = maps.map((map) => {
|
|
290
|
+
const kinds = masks.get(map)![address]!;
|
|
291
|
+
all |= kinds;
|
|
292
|
+
return { map, access: accessOf(kinds) };
|
|
293
|
+
});
|
|
294
|
+
return { maps: each, access: accessOf(all) };
|
|
295
|
+
};
|
|
296
|
+
}
|
|
297
|
+
|
|
244
298
|
/** The program of a space, or undefined when the knowledge has no bytes for it. */
|
|
245
299
|
export function programImage(db: DatabaseSync | undefined, space: Space = DEFAULT_SPACE): ProgramImage | undefined {
|
|
246
300
|
if (db === undefined) return undefined;
|
|
@@ -369,3 +423,29 @@ export function releaseImage(db: DatabaseSync | undefined, label: string, space:
|
|
|
369
423
|
}
|
|
370
424
|
return { segments, byteAt: (address) => memory[address]! };
|
|
371
425
|
}
|
|
426
|
+
|
|
427
|
+
/** The bytes of one !byte line of patch code. */
|
|
428
|
+
const PATCH_LINE_BYTES = 8;
|
|
429
|
+
|
|
430
|
+
/**
|
|
431
|
+
* The bytes of a release in [start, end] as patch code: !byte lines. The base
|
|
432
|
+
* release is refused: its bytes are the program, and a patch holds the other
|
|
433
|
+
* state. A release without a byte at each address of the range is refused.
|
|
434
|
+
*/
|
|
435
|
+
export function releaseCode(db: DatabaseSync | undefined, label: string, start: number, end: number, space: Space = DEFAULT_SPACE): string {
|
|
436
|
+
const range = `${formatC64Address(start)}-${formatC64Address(end)}`;
|
|
437
|
+
const release = db?.prepare("SELECT base FROM current_releases WHERE space = ? AND label = ?").get(space, label) as { base: number } | undefined;
|
|
438
|
+
if (release === undefined) throw new Refusal("not-found", `There is no release ${label}${space === DEFAULT_SPACE ? "" : ` in ${space}`}. Record it first with its file.`);
|
|
439
|
+
if (release.base === 1) throw new Refusal("invalid-input", `${label} is the base release: its bytes are the program, and a patch holds the bytes of the other state. Give the release that has the other bytes in ${range}.`);
|
|
440
|
+
const image = releaseImage(db, label, space)!;
|
|
441
|
+
const inside = (address: number) => image.segments.some((segment) => address >= segment.start && address <= segment.end);
|
|
442
|
+
for (let address = start; address <= end; address++) {
|
|
443
|
+
if (!inside(address)) throw new Refusal("invalid-input", `The release ${label} has no byte at ${formatC64Address(address)}, in the range ${range}. Give a range inside the bytes of the release.`);
|
|
444
|
+
}
|
|
445
|
+
const lines: string[] = [];
|
|
446
|
+
for (let at = start; at <= end; at += PATCH_LINE_BYTES) {
|
|
447
|
+
const bytes = Array.from({ length: Math.min(PATCH_LINE_BYTES, end - at + 1) }, (_, offset) => formatC64Byte(image.byteAt(at + offset)));
|
|
448
|
+
lines.push(`!byte ${bytes.join(",")}`);
|
|
449
|
+
}
|
|
450
|
+
return lines.join("\n");
|
|
451
|
+
}
|
package/src/knowledge/read.ts
CHANGED
|
@@ -114,11 +114,18 @@ export interface SwitchRow {
|
|
|
114
114
|
revision: string;
|
|
115
115
|
}
|
|
116
116
|
|
|
117
|
-
/**
|
|
117
|
+
/**
|
|
118
|
+
* A release of the program, and whether its bytes are the program of the
|
|
119
|
+
* knowledge. The bytes of another release are in release_memory. `file` and
|
|
120
|
+
* `sha256` are the file that recorded the release and its SHA-256; a release
|
|
121
|
+
* of an earlier toolkit version has neither.
|
|
122
|
+
*/
|
|
118
123
|
export interface ReleaseRow {
|
|
119
124
|
space: Space;
|
|
120
125
|
label: string;
|
|
121
126
|
base: boolean;
|
|
127
|
+
file?: string;
|
|
128
|
+
sha256?: string;
|
|
122
129
|
comment?: string;
|
|
123
130
|
origin: Extract<Origin, "user" | "llm">;
|
|
124
131
|
revision: string;
|
|
@@ -199,7 +206,7 @@ export const MODULE_COLUMNS = "space, start_address, file, title, origin, valid_
|
|
|
199
206
|
export const OPERAND_COLUMNS = "space, address, selector, symbol, offset, expression, origin, valid_from_revision";
|
|
200
207
|
export const PATCH_COLUMNS = "space, start_address, end_address, switch_name, code, comment, origin, valid_from_revision";
|
|
201
208
|
export const SWITCH_COLUMNS = "space, name, kind, default_value, releases, comment, origin, valid_from_revision";
|
|
202
|
-
export const RELEASE_COLUMNS = "space, label, base, comment, origin, valid_from_revision";
|
|
209
|
+
export const RELEASE_COLUMNS = "space, label, base, file, sha256, comment, origin, valid_from_revision";
|
|
203
210
|
export const MACRO_COLUMNS = "space, name, params, body, comment, origin, valid_from_revision";
|
|
204
211
|
export const MACRO_USE_COLUMNS = "space, start_address, end_address, macro, args, origin, valid_from_revision";
|
|
205
212
|
export const START_SCREEN_COLUMNS = "space, mode, screen, bitmap, charset, colours, colour, border, background, stage, call_address, colour_ram, comment, origin, valid_from_revision";
|
|
@@ -306,6 +313,8 @@ export function toRelease(row: Raw): ReleaseRow {
|
|
|
306
313
|
origin: row.origin as ReleaseRow["origin"],
|
|
307
314
|
revision: row.valid_from_revision as string,
|
|
308
315
|
};
|
|
316
|
+
if (row.file !== null && row.file !== undefined) release.file = row.file as string;
|
|
317
|
+
if (row.sha256 !== null && row.sha256 !== undefined) release.sha256 = row.sha256 as string;
|
|
309
318
|
if (row.comment !== null && row.comment !== undefined) release.comment = row.comment as string;
|
|
310
319
|
return release;
|
|
311
320
|
}
|
|
@@ -706,6 +715,41 @@ export function listReferences(db: DatabaseSync | undefined, range: AddressRange
|
|
|
706
715
|
.filter((reference) => (kind === undefined || reference.kind === kind) && kept(origins)(reference));
|
|
707
716
|
}
|
|
708
717
|
|
|
718
|
+
/** Where a reference goes, seen from a range: into it from outside, out of it, or with both ends inside it. */
|
|
719
|
+
export const REFERENCE_DIRECTIONS = ["in", "out", "inside"] as const;
|
|
720
|
+
export type ReferenceDirection = (typeof REFERENCE_DIRECTIONS)[number];
|
|
721
|
+
|
|
722
|
+
/** True for a reference that goes in the direction, seen from the range. */
|
|
723
|
+
export function goes(range: AddressRange, direction: ReferenceDirection): (row: Pick<ReferenceRow, "from" | "to">) => boolean {
|
|
724
|
+
const inside = (address: number) => address >= range.start && address <= range.end;
|
|
725
|
+
if (direction === "in") return (row) => inside(row.to) && !inside(row.from);
|
|
726
|
+
if (direction === "out") return (row) => inside(row.from) && !inside(row.to);
|
|
727
|
+
return (row) => inside(row.from) && inside(row.to);
|
|
728
|
+
}
|
|
729
|
+
|
|
730
|
+
/** The references counted by kind, and by target with the target that most references go to first. */
|
|
731
|
+
export function referenceCounts(rows: readonly ReferenceRow[]): { byKind: Partial<Record<ReferenceKind, number>>; byTarget: Array<{ to: number; count: number }> } {
|
|
732
|
+
const byKind: Partial<Record<ReferenceKind, number>> = {};
|
|
733
|
+
const targets = new Map<number, number>();
|
|
734
|
+
for (const row of rows) {
|
|
735
|
+
byKind[row.kind] = (byKind[row.kind] ?? 0) + 1;
|
|
736
|
+
targets.set(row.to, (targets.get(row.to) ?? 0) + 1);
|
|
737
|
+
}
|
|
738
|
+
const byTarget = [...targets].map(([to, count]) => ({ to, count })).sort((a, b) => b.count - a.count || a.to - b.to);
|
|
739
|
+
return { byKind, byTarget };
|
|
740
|
+
}
|
|
741
|
+
|
|
742
|
+
/** The regions counted by type, with their bytes in the range. */
|
|
743
|
+
export function regionCounts(rows: readonly RegionRow[], range: AddressRange = ALL): Partial<Record<RegionType, { count: number; bytes: number }>> {
|
|
744
|
+
const counts: Partial<Record<RegionType, { count: number; bytes: number }>> = {};
|
|
745
|
+
for (const row of rows) {
|
|
746
|
+
const item = (counts[row.type] ??= { count: 0, bytes: 0 });
|
|
747
|
+
item.count++;
|
|
748
|
+
item.bytes += Math.min(row.end, range.end) - Math.max(row.start, range.start) + 1;
|
|
749
|
+
}
|
|
750
|
+
return counts;
|
|
751
|
+
}
|
|
752
|
+
|
|
709
753
|
/** Current suppressions whose place lies in the range: a reference by its source or target. */
|
|
710
754
|
export function listSuppressions(db: DatabaseSync | undefined, range: AddressRange = ALL, space: Space = DEFAULT_SPACE): SuppressionRow[] {
|
|
711
755
|
if (db === undefined) return [];
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
// The files of a recording folder: the record of a session writes them, and the
|
|
2
|
+
// knowledge takes the coverage map of the folder. They are here, so the
|
|
3
|
+
// script that records and the script that keeps the knowledge share one list.
|
|
4
|
+
|
|
5
|
+
import { existsSync, statSync } from "node:fs";
|
|
6
|
+
import { join } from "node:path";
|
|
7
|
+
|
|
8
|
+
import { Refusal } from "../errors.ts";
|
|
9
|
+
import { projectRoot, resolveReadFile } from "../project.ts";
|
|
10
|
+
|
|
11
|
+
export const RECORDING_FILES = { recording: "recording.json", reads: "reads.txt", coverage: "coverage.bin" } as const;
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* The coverage map that a path names: the path itself, or for a recording
|
|
15
|
+
* folder the coverage map in it (<folder>/coverage.bin). The knowledge names a
|
|
16
|
+
* map by this path, so a new recording into the same folder replaces the map.
|
|
17
|
+
* A path that is no folder, or that does not exist, is given back as it is:
|
|
18
|
+
* the read of the map refuses it.
|
|
19
|
+
*/
|
|
20
|
+
export function coverageMapOf(path: string, root = projectRoot()): string {
|
|
21
|
+
let real: string;
|
|
22
|
+
try {
|
|
23
|
+
real = resolveReadFile(path, root);
|
|
24
|
+
} catch {
|
|
25
|
+
return path;
|
|
26
|
+
}
|
|
27
|
+
if (!statSync(real).isDirectory()) return path;
|
|
28
|
+
if (!existsSync(join(real, RECORDING_FILES.coverage)) || !existsSync(join(real, RECORDING_FILES.recording))) {
|
|
29
|
+
throw new Refusal("not-found", `${path} is a folder that holds no recording. Give a coverage map, or a folder that a record of a session wrote.`);
|
|
30
|
+
}
|
|
31
|
+
return `${path.replace(/\/+$/, "")}/${RECORDING_FILES.coverage}`;
|
|
32
|
+
}
|
package/src/knowledge/schema.ts
CHANGED
|
@@ -302,6 +302,8 @@ function createSchema(db: DatabaseSync): void {
|
|
|
302
302
|
space TEXT NOT NULL DEFAULT 'c64' CHECK (${SPACE_CHECK}),
|
|
303
303
|
label TEXT NOT NULL CHECK (length(label) > 0),
|
|
304
304
|
base INTEGER NOT NULL CHECK (base IN (0, 1)),
|
|
305
|
+
file TEXT CHECK (file IS NULL OR length(file) > 0),
|
|
306
|
+
sha256 TEXT CHECK (sha256 IS NULL OR length(sha256) = 64),
|
|
305
307
|
comment TEXT CHECK (comment IS NULL OR length(comment) > 0),
|
|
306
308
|
origin TEXT NOT NULL CHECK (origin IN ('user', 'llm')),
|
|
307
309
|
valid_from_revision TEXT NOT NULL REFERENCES revisions (id),
|
|
@@ -423,7 +425,7 @@ function createSchema(db: DatabaseSync): void {
|
|
|
423
425
|
}
|
|
424
426
|
|
|
425
427
|
/** The version of the index this build makes. An index with another version is emptied and made again. */
|
|
426
|
-
export const INDEX_VERSION =
|
|
428
|
+
export const INDEX_VERSION = 16;
|
|
427
429
|
|
|
428
430
|
function indexVersion(db: DatabaseSync): number | undefined {
|
|
429
431
|
const hasMeta = db.prepare("SELECT 1 FROM sqlite_master WHERE type = 'table' AND name = 'meta'").get();
|
|
@@ -3,10 +3,14 @@
|
|
|
3
3
|
// region of knowledge, whether call and jump references or the run on from
|
|
4
4
|
// its last instruction reach it from a root (the entry, and the NMI, RESET
|
|
5
5
|
// and IRQ vectors at $fffa-$ffff when the image holds them); and what the program reads
|
|
6
|
-
// or executes outside the image.
|
|
6
|
+
// or executes outside the image. A region that nothing reaches is unknown, not
|
|
7
|
+
// dead, while the program has a flow whose targets the knowledge does not give,
|
|
8
|
+
// or when no coverage shows what ran. Evidence only: nothing here decides which
|
|
7
9
|
// bytes the publisher shipped.
|
|
8
10
|
|
|
9
11
|
import { type AddressRange, COVERAGE, executedRamCode, instructionLength } from "../c64.ts";
|
|
12
|
+
import { OPCODES } from "../opcodes.ts";
|
|
13
|
+
import { isSemantic } from "./schema.ts";
|
|
10
14
|
|
|
11
15
|
export type Access = "executed" | "read" | "written" | "read-written" | "untouched";
|
|
12
16
|
|
|
@@ -20,7 +24,8 @@ export interface SeparationInput {
|
|
|
20
24
|
/** Joined coverage files. */
|
|
21
25
|
coverage?: Uint8Array;
|
|
22
26
|
codeRegions: Array<AddressRange & { type: string }>;
|
|
23
|
-
|
|
27
|
+
/** A jump or call reference of the user or the LLM also resolves the flow of its instruction. */
|
|
28
|
+
references: Array<{ from: number; to: number; kind: string; origin?: string | undefined }>;
|
|
24
29
|
/** More roots after the entry and the hardware vectors, for example code that a run executed. */
|
|
25
30
|
roots?: Root[];
|
|
26
31
|
}
|
|
@@ -37,6 +42,25 @@ export interface CodeRegionEvidence extends AddressRange {
|
|
|
37
42
|
reachable?: boolean;
|
|
38
43
|
/** Absent without coverage. */
|
|
39
44
|
executed?: boolean;
|
|
45
|
+
/** Not reached and not executed, but an unresolved flow can go there, or no coverage shows that it did not run. */
|
|
46
|
+
unknown?: true;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* A flow whose targets the knowledge does not give: a JMP (indirect) through
|
|
51
|
+
* a vector that the code's own immediates do not set, a JMP or JSR whose
|
|
52
|
+
* address bytes a run wrote, or an RTS or RTI to an address that the code
|
|
53
|
+
* pushed.
|
|
54
|
+
*/
|
|
55
|
+
export interface UnresolvedFlow {
|
|
56
|
+
at: number;
|
|
57
|
+
kind: "indirect-jump" | "written-operand" | "pushed-return";
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/** The computed flows of the code: the targets that the bytes of the program give, and the flows that stay open. */
|
|
61
|
+
export interface ComputedFlows {
|
|
62
|
+
targets: Array<{ from: number; to: number }>;
|
|
63
|
+
unresolved: UnresolvedFlow[];
|
|
40
64
|
}
|
|
41
65
|
|
|
42
66
|
/** An address where the reachability starts, and why: the entry, a hardware vector in the image, or a root that the caller gives. */
|
|
@@ -57,6 +81,8 @@ export interface Separation {
|
|
|
57
81
|
* pieces of code with gaps of up to two operand bytes.
|
|
58
82
|
*/
|
|
59
83
|
outside: { read: AddressRange[]; executed: AddressRange[]; generated: AddressRange[] };
|
|
84
|
+
/** The unresolved flows of the code regions that a root reaches or that a run executed: while one exists, an unreached code region is unknown. */
|
|
85
|
+
unresolved: UnresolvedFlow[];
|
|
60
86
|
}
|
|
61
87
|
|
|
62
88
|
const SYSTEM_PAGES_END = 0x03ff;
|
|
@@ -97,6 +123,125 @@ export function fallThrough(region: AddressRange, memory: Uint8Array): number |
|
|
|
97
123
|
return NO_FALL_THROUGH.has(memory[last]!) || at > 0xffff ? undefined : at;
|
|
98
124
|
}
|
|
99
125
|
|
|
126
|
+
/** The instruction starts of code regions: each region runs from its start, one instruction after the other. */
|
|
127
|
+
export function regionStarts(regions: readonly AddressRange[], memory: Uint8Array): number[] {
|
|
128
|
+
const starts: number[] = [];
|
|
129
|
+
for (const region of regions) for (let at = region.start; at <= region.end; at += instructionLength(memory[at]!)) starts.push(at);
|
|
130
|
+
return starts.sort((a, b) => a - b);
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
/** The address of the high byte of the vector of a JMP (N): the 6510 reads it from N + 1 in the same page, so JMP ($10ff) reads $1000. */
|
|
134
|
+
export const vectorHigh = (vector: number) => (vector & 0xff00) | ((vector + 1) & 0xff);
|
|
135
|
+
|
|
136
|
+
/** The registers that each instruction changes, other than by a load of an immediate or a transfer. */
|
|
137
|
+
export const WRITES_A = new Set(["lda", "adc", "sbc", "and", "ora", "eor", "pla", "lax", "anc", "asr", "arr", "ane", "lxa", "las", "slo", "rla", "sre", "rra", "isc"]);
|
|
138
|
+
export const WRITES_X = new Set(["ldx", "inx", "dex", "tsx", "lax", "sbx", "lxa", "las"]);
|
|
139
|
+
export const WRITES_Y = new Set(["ldy", "iny", "dey"]);
|
|
140
|
+
|
|
141
|
+
/** The instructions that change the byte at their operand with a value that no immediate gives. */
|
|
142
|
+
const CHANGES_MEMORY = new Set(["inc", "dec", "asl", "lsr", "rol", "ror", "sax", "slo", "rla", "sre", "rra", "dcp", "isc"]);
|
|
143
|
+
|
|
144
|
+
/**
|
|
145
|
+
* The computed flows of the instructions at `starts`: a JMP (N), a JMP or JSR
|
|
146
|
+
* whose address bytes a run wrote, and an RTS or RTI after pushes in its run
|
|
147
|
+
* of instructions. A jump or call reference of the user or the LLM from the
|
|
148
|
+
* instruction resolves its flow; for a pushed return, also one of the idiom
|
|
149
|
+
* pass, which reads the table of an RTS dispatch. Else the bytes resolve it:
|
|
150
|
+
* each byte holds its image byte and the immediates that the code stores
|
|
151
|
+
* there, and the flow goes to each word of these values. A byte that another
|
|
152
|
+
* write of the code changes, or that a run wrote while no store of the code
|
|
153
|
+
* does, or that is outside the image without such a store, leaves the flow
|
|
154
|
+
* unresolved.
|
|
155
|
+
*/
|
|
156
|
+
export function computedFlows(input: { starts: readonly number[]; memory: Uint8Array; inImage: (address: number) => boolean; coverage?: Uint8Array | undefined; references: ReadonlyArray<{ from: number; kind: string; origin?: string | undefined }> }): ComputedFlows {
|
|
157
|
+
const { memory } = input;
|
|
158
|
+
const written = (address: number) => input.coverage !== undefined && (input.coverage[address & 0xffff]! & COVERAGE.ramWrite) !== 0;
|
|
159
|
+
const jumpsFrom = (origins: (origin: string | undefined) => boolean) => new Set(input.references.filter((reference) => (reference.kind === "jump" || reference.kind === "call") && origins(reference.origin)).map((reference) => reference.from));
|
|
160
|
+
const resolved = jumpsFrom(isSemantic);
|
|
161
|
+
const fromTable = jumpsFrom((origin) => isSemantic(origin) || origin === "heuristic");
|
|
162
|
+
const starts = [...input.starts].sort((a, b) => a - b);
|
|
163
|
+
|
|
164
|
+
// The immediates that the code stores at each address; null when another write changes it.
|
|
165
|
+
const stored = new Map<number, Set<number> | null>();
|
|
166
|
+
const store = (address: number, value: number | undefined) => {
|
|
167
|
+
const known = stored.get(address);
|
|
168
|
+
if (known === null) return;
|
|
169
|
+
stored.set(address, value === undefined ? null : (known ?? new Set<number>()).add(value));
|
|
170
|
+
};
|
|
171
|
+
// The return addresses that the code pushed before an RTS or RTI, as far as immediates give them.
|
|
172
|
+
const returns: Array<{ at: number; low: number | undefined; high: number | undefined; offset: 0 | 1 }> = [];
|
|
173
|
+
let registers: { a?: number | undefined; x?: number | undefined; y?: number | undefined } = {};
|
|
174
|
+
let pushes: Array<number | undefined> = [];
|
|
175
|
+
let previousEnd = -2;
|
|
176
|
+
for (const at of starts) {
|
|
177
|
+
if (at > previousEnd + 1) {
|
|
178
|
+
registers = {};
|
|
179
|
+
pushes = [];
|
|
180
|
+
}
|
|
181
|
+
const opcode = memory[at]!;
|
|
182
|
+
const { mnemonic, mode } = OPCODES[opcode]!;
|
|
183
|
+
const length = instructionLength(opcode);
|
|
184
|
+
previousEnd = at + length - 1;
|
|
185
|
+
const operand = length === 3 ? memory[(at + 1) & 0xffff]! | (memory[(at + 2) & 0xffff]! << 8) : memory[(at + 1) & 0xffff]!;
|
|
186
|
+
if (mode === "zp" || mode === "abs") {
|
|
187
|
+
const register = mnemonic === "sta" ? "a" : mnemonic === "stx" ? "x" : mnemonic === "sty" ? "y" : undefined;
|
|
188
|
+
if (register !== undefined) store(operand, registers[register]);
|
|
189
|
+
else if (CHANGES_MEMORY.has(mnemonic)) store(operand, undefined);
|
|
190
|
+
}
|
|
191
|
+
if (mnemonic === "pha") pushes.push(registers.a);
|
|
192
|
+
if (mnemonic === "php") pushes.push(undefined);
|
|
193
|
+
if (mnemonic === "pla" || mnemonic === "plp") pushes.pop();
|
|
194
|
+
if (mnemonic === "txs") pushes = [];
|
|
195
|
+
if (mnemonic === "rts" && pushes.length >= 2) returns.push({ at, low: pushes.at(-1), high: pushes.at(-2), offset: 1 });
|
|
196
|
+
if (mnemonic === "rti" && pushes.length >= 3) returns.push({ at, low: pushes.at(-2), high: pushes.at(-3), offset: 0 });
|
|
197
|
+
const loaded = mode === "imm" ? operand : undefined;
|
|
198
|
+
if (WRITES_A.has(mnemonic) || (mode === "imp" && ["asl", "lsr", "rol", "ror"].includes(mnemonic))) registers.a = mnemonic === "lda" ? loaded : undefined;
|
|
199
|
+
if (WRITES_X.has(mnemonic)) registers.x = mnemonic === "ldx" ? loaded : undefined;
|
|
200
|
+
if (WRITES_Y.has(mnemonic)) registers.y = mnemonic === "ldy" ? loaded : undefined;
|
|
201
|
+
if (mnemonic === "tax") registers.x = registers.a;
|
|
202
|
+
if (mnemonic === "tay") registers.y = registers.a;
|
|
203
|
+
if (mnemonic === "txa") registers.a = registers.x;
|
|
204
|
+
if (mnemonic === "tya") registers.a = registers.y;
|
|
205
|
+
if (mnemonic === "jsr") registers = {};
|
|
206
|
+
if (NO_FALL_THROUGH.has(opcode)) {
|
|
207
|
+
registers = {};
|
|
208
|
+
pushes = [];
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
/** The values of a byte when the code reads it, or undefined when they are not known. */
|
|
213
|
+
const valuesAt = (address: number): number[] | undefined => {
|
|
214
|
+
const own = stored.get(address);
|
|
215
|
+
if (own === null || (own === undefined && (written(address) || !input.inImage(address)))) return undefined;
|
|
216
|
+
return [...new Set([...(own ?? []), ...(input.inImage(address) ? [memory[address]!] : [])])];
|
|
217
|
+
};
|
|
218
|
+
const targets: ComputedFlows["targets"] = [];
|
|
219
|
+
/** Adds a flow from `from` to each word of the bytes at `low` and `high`; false when a byte is not known. */
|
|
220
|
+
const wordsAt = (from: number, low: number, high: number) => {
|
|
221
|
+
const lows = valuesAt(low);
|
|
222
|
+
const highs = valuesAt(high);
|
|
223
|
+
if (lows === undefined || highs === undefined) return false;
|
|
224
|
+
for (const lowByte of lows) for (const highByte of highs) targets.push({ from, to: lowByte | (highByte << 8) });
|
|
225
|
+
return true;
|
|
226
|
+
};
|
|
227
|
+
const unresolved: UnresolvedFlow[] = [];
|
|
228
|
+
for (const at of starts) {
|
|
229
|
+
if (resolved.has(at)) continue;
|
|
230
|
+
const opcode = memory[at]!;
|
|
231
|
+
if (opcode === 0x6c) {
|
|
232
|
+
const vector = memory[(at + 1) & 0xffff]! | (memory[(at + 2) & 0xffff]! << 8);
|
|
233
|
+
if (!wordsAt(at, vector, vectorHigh(vector))) unresolved.push({ at, kind: "indirect-jump" });
|
|
234
|
+
} else if ((opcode === 0x4c || opcode === 0x20) && (written(at + 1) || written(at + 2))) {
|
|
235
|
+
if (!wordsAt(at, (at + 1) & 0xffff, (at + 2) & 0xffff)) unresolved.push({ at, kind: "written-operand" });
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
for (const pushed of returns) {
|
|
239
|
+
if (pushed.low !== undefined && pushed.high !== undefined) targets.push({ from: pushed.at, to: ((pushed.low | (pushed.high << 8)) + pushed.offset) & 0xffff });
|
|
240
|
+
else if (!fromTable.has(pushed.at)) unresolved.push({ at: pushed.at, kind: "pushed-return" });
|
|
241
|
+
}
|
|
242
|
+
return { targets, unresolved: unresolved.sort((a, b) => a.at - b.at) };
|
|
243
|
+
}
|
|
244
|
+
|
|
100
245
|
/** The most instructions outside the code regions that the run on from a region goes through, for example an instruction in a dual-use region. */
|
|
101
246
|
const MAX_RUN_ON = 16;
|
|
102
247
|
|
|
@@ -167,9 +312,11 @@ export function separate(input: SeparationInput): Separation {
|
|
|
167
312
|
const to = next === undefined ? undefined : runOn(next);
|
|
168
313
|
return to === undefined ? [] : [{ from: region.end, to }];
|
|
169
314
|
});
|
|
170
|
-
//
|
|
315
|
+
// A store anywhere in the code can change a vector, so the computed flows read every region.
|
|
316
|
+
const computed = computedFlows({ starts: regionStarts(regions, input.memory), memory: input.memory, inImage, coverage: input.coverage, references: input.references });
|
|
317
|
+
// From the regions that hold a root, through call and jump references, the targets of computed flows and the run on, to every region they reach.
|
|
171
318
|
const next = regions.map((): number[] => []);
|
|
172
|
-
for (const flow of [...input.references.filter((reference) => reference.kind === "call" || reference.kind === "jump"), ...runsOn]) {
|
|
319
|
+
for (const flow of [...input.references.filter((reference) => reference.kind === "call" || reference.kind === "jump"), ...computed.targets, ...runsOn]) {
|
|
173
320
|
const from = holding(flow.from);
|
|
174
321
|
const to = holding(flow.to);
|
|
175
322
|
if (from >= 0 && to >= 0) next[from]!.push(to);
|
|
@@ -190,13 +337,20 @@ export function separate(input: SeparationInput): Separation {
|
|
|
190
337
|
queue.push(to);
|
|
191
338
|
}
|
|
192
339
|
}
|
|
340
|
+
// Undefined without coverage.
|
|
341
|
+
const ran = regions.map((region): boolean | undefined => {
|
|
342
|
+
if (input.coverage === undefined) return undefined;
|
|
343
|
+
for (let address = region.start; address <= region.end; address++) if (executed[address] === 1) return true;
|
|
344
|
+
return false;
|
|
345
|
+
});
|
|
346
|
+
// Only a flow in code that a root reaches or that a run executed can run: a flow in dead code goes nowhere.
|
|
347
|
+
const unresolved = computed.unresolved.filter((flow) => {
|
|
348
|
+
const index = holding(flow.at);
|
|
349
|
+
return index >= 0 && (reached.has(index) || ran[index] === true);
|
|
350
|
+
});
|
|
193
351
|
const codeRegions = regions.map((region, index): CodeRegionEvidence => {
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
ran = false;
|
|
197
|
-
for (let address = region.start; address <= region.end && !ran; address++) ran = executed[address] === 1;
|
|
198
|
-
}
|
|
199
|
-
return { start: region.start, end: region.end, type: region.type, ...(roots.length === 0 ? {} : { reachable: reached.has(index) }), ...(ran === undefined ? {} : { executed: ran }) };
|
|
352
|
+
const unreached = roots.length > 0 && !reached.has(index) && ran[index] !== true;
|
|
353
|
+
return { start: region.start, end: region.end, type: region.type, ...(roots.length === 0 ? {} : { reachable: reached.has(index) }), ...(ran[index] === undefined ? {} : { executed: ran[index] }), ...(unreached && (unresolved.length > 0 || input.coverage === undefined) ? { unknown: true as const } : {}) };
|
|
200
354
|
});
|
|
201
355
|
|
|
202
356
|
const read: number[] = [];
|
|
@@ -216,5 +370,5 @@ export function separate(input: SeparationInput): Separation {
|
|
|
216
370
|
for (let address = from + 1; address < to; address++) if (inImage(address)) return false;
|
|
217
371
|
return true;
|
|
218
372
|
};
|
|
219
|
-
return { roots, ranges, codeRegions, outside: { read: merge(read), executed: merge(ranOutside), generated: merge(generated, 2, outsideImage) } };
|
|
373
|
+
return { roots, ranges, codeRegions, outside: { read: merge(read), executed: merge(ranOutside), generated: merge(generated, 2, outsideImage) }, unresolved };
|
|
220
374
|
}
|
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
// stage needs no address from the user.
|
|
5
5
|
|
|
6
6
|
import { Refusal } from "../errors.ts";
|
|
7
|
-
import type
|
|
7
|
+
import { LEFTOVERS_SWITCH, START_SCREEN_SWITCH, type StartScreenMode } from "./schema.ts";
|
|
8
8
|
|
|
9
9
|
/** The labels of the start screen in a generated loader: its wait for a key or a fire button, and the code after the wait. */
|
|
10
10
|
export const START_SCREEN_WAIT = "loader_start_screen_wait";
|
|
@@ -12,6 +12,21 @@ export const START_SCREEN_CONTINUE = "loader_start_screen_continue";
|
|
|
12
12
|
/** The label of the loader's jump to the entry, in ACME's symbol list. */
|
|
13
13
|
export const LOADER_JUMP = "c64_source_loader_jump";
|
|
14
14
|
|
|
15
|
+
/**
|
|
16
|
+
* The wait and the code after the wait of the start screen that a build shows,
|
|
17
|
+
* from the symbols of the build; undefined when the build shows none. A build
|
|
18
|
+
* with START_SCREEN = 0 or LEFTOVERS = 1 has both labels but skips the screen
|
|
19
|
+
* (the start screen code of the generated source). A symbol file without the
|
|
20
|
+
* constants is a build with the default values: the screen is on.
|
|
21
|
+
*/
|
|
22
|
+
export function startScreenLabels(symbols: ReadonlyMap<string, number>): { wait: number; continue: number } | undefined {
|
|
23
|
+
const wait = symbols.get(START_SCREEN_WAIT);
|
|
24
|
+
const resume = symbols.get(START_SCREEN_CONTINUE);
|
|
25
|
+
if (wait === undefined || resume === undefined) return undefined;
|
|
26
|
+
if ((symbols.get(START_SCREEN_SWITCH) ?? 1) === 0 || (symbols.get(LEFTOVERS_SWITCH) ?? 0) !== 0) return undefined;
|
|
27
|
+
return { wait, continue: resume };
|
|
28
|
+
}
|
|
29
|
+
|
|
15
30
|
/** The fields of a start screen that the capture of a stage gives. */
|
|
16
31
|
export interface StageScreen {
|
|
17
32
|
mode: StartScreenMode;
|
|
@@ -37,34 +52,101 @@ export function capturedColourRam(state: Record<string, unknown> | undefined): U
|
|
|
37
52
|
return bytes === undefined || bytes.length !== 1000 ? undefined : Uint8Array.from(bytes, (value) => value & 0x0f);
|
|
38
53
|
}
|
|
39
54
|
|
|
55
|
+
/** How a refusal names the capture: the program that has no entry state, the capture, and how to capture it again. */
|
|
56
|
+
interface Named {
|
|
57
|
+
program: string;
|
|
58
|
+
capture: string;
|
|
59
|
+
again: string;
|
|
60
|
+
}
|
|
61
|
+
|
|
40
62
|
/**
|
|
41
|
-
* The
|
|
42
|
-
*
|
|
43
|
-
* the
|
|
63
|
+
* The screen that the VIC-II of an entry state shows: its mode, its screen, its
|
|
64
|
+
* bitmap or charset and its colours of the border and the background, from the
|
|
65
|
+
* VIC-II registers and the VIC bank of CIA 2. Refuses by name when the capture
|
|
66
|
+
* does not show a screen that the loader can show.
|
|
44
67
|
*/
|
|
45
|
-
|
|
46
|
-
const again = `Capture the stage again while it shows the screen, and put the capture into the project knowledge with image --space ${stage}.`;
|
|
68
|
+
function vicScreen(state: Record<string, unknown> | undefined, named: Named): Omit<StageScreen, "colourRam"> {
|
|
47
69
|
const vic = ioBytes(state, "vic");
|
|
48
70
|
const cia2 = ioBytes(state, "cia2");
|
|
49
|
-
if (state === undefined || vic === undefined || vic.length < 0x2f || cia2 === undefined) throw new Refusal("not-found",
|
|
71
|
+
if (state === undefined || vic === undefined || vic.length < 0x2f || cia2 === undefined) throw new Refusal("not-found", `${named.program} has no entry state with the VIC-II and CIA 2. ${named.again}`);
|
|
50
72
|
const [d011, d016, d018] = [vic[0x11]!, vic[0x16]!, vic[0x18]!];
|
|
51
|
-
if ((d011 & 0x10) === 0) throw new Refusal("invalid-input",
|
|
52
|
-
if ((d011 & 0x40) !== 0) throw new Refusal("invalid-input",
|
|
73
|
+
if ((d011 & 0x10) === 0) throw new Refusal("invalid-input", `${named.capture} has the display off ($d011 = $${d011.toString(16).padStart(2, "0")}). ${named.again}`);
|
|
74
|
+
if ((d011 & 0x40) !== 0) throw new Refusal("invalid-input", `${named.capture} shows a screen in the extended colour mode, which a start screen does not have.`);
|
|
53
75
|
const bitmap = (d011 & 0x20) !== 0;
|
|
54
76
|
const multicolour = (d016 & 0x10) !== 0;
|
|
55
|
-
if (!bitmap && multicolour) throw new Refusal("invalid-input",
|
|
77
|
+
if (!bitmap && multicolour) throw new Refusal("invalid-input", `${named.capture} shows a multicolour text screen, which a start screen does not have.`);
|
|
56
78
|
const bank = (3 - (cia2[0]! & 0x03)) << 14;
|
|
57
|
-
const mode: StartScreenMode = bitmap ? (multicolour ? "multicolour" : "hires") : "text";
|
|
58
|
-
const screen = bank + ((d018 >> 4) & 0x0f) * 0x400;
|
|
59
|
-
const colourRam = mode === "hires" ? undefined : capturedColourRam(state);
|
|
60
|
-
if (mode !== "hires" && colourRam === undefined) throw new Refusal("not-found", `The capture in ${stage} has no colour RAM: it is from an older version. ${again}`);
|
|
61
79
|
return {
|
|
62
|
-
mode,
|
|
63
|
-
screen,
|
|
80
|
+
mode: bitmap ? (multicolour ? "multicolour" : "hires") : "text",
|
|
81
|
+
screen: bank + ((d018 >> 4) & 0x0f) * 0x400,
|
|
64
82
|
...(bitmap ? { bitmap: bank + ((d018 >> 3) & 1) * 0x2000 } : { charset: bank + ((d018 >> 1) & 7) * 0x800 }),
|
|
65
83
|
border: vic[0x20]! & 0x0f,
|
|
66
84
|
background: vic[0x21]! & 0x0f,
|
|
67
|
-
...(colourRam === undefined ? {} : { colourRam: true as const }),
|
|
68
85
|
};
|
|
69
86
|
}
|
|
70
87
|
|
|
88
|
+
/**
|
|
89
|
+
* The start screen that the capture of a stage shows: its mode, its screen,
|
|
90
|
+
* its bitmap or charset, its colours and the colour RAM. Refuses by name when
|
|
91
|
+
* the capture does not show a start screen that the loader can show.
|
|
92
|
+
*/
|
|
93
|
+
export function startScreenOfStage(state: Record<string, unknown> | undefined, stage: string): StageScreen {
|
|
94
|
+
const again = `Capture the stage again while it shows the screen, and put the capture into the project knowledge with image --space ${stage}.`;
|
|
95
|
+
const shown = vicScreen(state, { program: `The program in ${stage}`, capture: `The capture in ${stage}`, again });
|
|
96
|
+
const colourRam = shown.mode === "hires" ? undefined : capturedColourRam(state);
|
|
97
|
+
if (shown.mode !== "hires" && colourRam === undefined) throw new Refusal("not-found", `The capture in ${stage} has no colour RAM: it is from an older version. ${again}`);
|
|
98
|
+
return { ...shown, ...(colourRam === undefined ? {} : { colourRam: true as const }) };
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/** The bytes of the colour RAM. */
|
|
102
|
+
const COLOURS = 1000;
|
|
103
|
+
|
|
104
|
+
/**
|
|
105
|
+
* The first address of the program from $0400 where 1000 bytes hold the
|
|
106
|
+
* colours (the low 4 bits of each byte), outside $d000-$dfff, where the loader
|
|
107
|
+
* sees the I/O area. Undefined when the program has no such bytes.
|
|
108
|
+
*/
|
|
109
|
+
export function findColours(colours: Uint8Array, byteAt: (address: number) => number | undefined): number | undefined {
|
|
110
|
+
for (let start = 0x0400; start + COLOURS - 1 <= 0xffff; start++) {
|
|
111
|
+
if (start <= 0xdfff && start + COLOURS - 1 >= 0xd000) continue;
|
|
112
|
+
let offset = 0;
|
|
113
|
+
while (offset < COLOURS) {
|
|
114
|
+
const byte = byteAt(start + offset);
|
|
115
|
+
if (byte === undefined || (byte & 0x0f) !== colours[offset]) break;
|
|
116
|
+
offset++;
|
|
117
|
+
}
|
|
118
|
+
if (offset === COLOURS) return start;
|
|
119
|
+
}
|
|
120
|
+
return undefined;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
const CAPTURE_AGAIN = "Capture the program again while it shows the screen. Then give the image.json of that capture.";
|
|
124
|
+
|
|
125
|
+
/**
|
|
126
|
+
* The start screen in the program that a capture of the program shows: the
|
|
127
|
+
* mode, the addresses and the border and background colours from the VIC-II
|
|
128
|
+
* and CIA 2 of the capture. Refuses by name when the capture does not show a
|
|
129
|
+
* start screen that the loader can show.
|
|
130
|
+
*/
|
|
131
|
+
export function startScreenOfCapture(state: Record<string, unknown> | undefined, path: string): Omit<StageScreen, "colourRam"> {
|
|
132
|
+
return vicScreen(state, { program: `The capture ${path}`, capture: `The capture ${path}`, again: CAPTURE_AGAIN });
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
/**
|
|
136
|
+
* The colour RAM of a capture as the program gives it: one colour, or the
|
|
137
|
+
* address of 1000 bytes that the program has (findColours). Refuses with the
|
|
138
|
+
* reason when the program cannot give the colours.
|
|
139
|
+
*/
|
|
140
|
+
export function coloursOfCapture(state: Record<string, unknown> | undefined, path: string, byteAt: (address: number) => number | undefined): { colour: number } | { colours: number } {
|
|
141
|
+
const colours = capturedColourRam(state);
|
|
142
|
+
if (colours === undefined) throw new Refusal("not-found", `The capture ${path} has no colour RAM: it is from an older version. ${CAPTURE_AGAIN}`);
|
|
143
|
+
if (colours.every((colour) => colour === colours[0])) return { colour: colours[0]! };
|
|
144
|
+
const found = findColours(colours, byteAt);
|
|
145
|
+
if (found === undefined) {
|
|
146
|
+
throw new Refusal(
|
|
147
|
+
"not-found",
|
|
148
|
+
`The colour RAM of the capture ${path} has more than one colour, and the program of the knowledge has no 1000 bytes with these colours from $0400, outside $d000-$dfff. Thus the loader cannot set the colours. If a stage of the loading shows the screen, record the start screen of that stage.`,
|
|
149
|
+
);
|
|
150
|
+
}
|
|
151
|
+
return { colours: found };
|
|
152
|
+
}
|
package/src/knowledge/text.ts
CHANGED
|
@@ -172,11 +172,12 @@ export const TABLES: readonly TableSpec[] = [
|
|
|
172
172
|
],
|
|
173
173
|
},
|
|
174
174
|
// The releases of the program, and the one whose bytes are the program. The bytes of the other releases are in release_memory.jsonl.
|
|
175
|
+
// file and sha256 are the file that recorded the release and its SHA-256; a release of an earlier toolkit version has neither.
|
|
175
176
|
{
|
|
176
177
|
table: "releases",
|
|
177
178
|
file: "releases.jsonl",
|
|
178
179
|
sort: "label",
|
|
179
|
-
fields: [space, field("label", "label", "text"), field("base", "base", "integer"), field("comment", "comment", "text", true), origin],
|
|
180
|
+
fields: [space, field("label", "label", "text"), field("base", "base", "integer"), field("file", "file", "text", true), field("sha256", "sha256", "text", true), field("comment", "comment", "text", true), origin],
|
|
180
181
|
},
|
|
181
182
|
{
|
|
182
183
|
table: "macros",
|