@henols/c64-re-tools 2.6.0-rc.2 → 2.6.0-rc.20
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 +2 -2
- package/skills/c64-assembler/SKILL.md +28 -14
- package/skills/c64-assembler/scripts/assembler.ts +28 -15
- package/skills/c64-development/SKILL.md +4 -4
- package/skills/c64-docs/SKILL.md +9 -7
- package/skills/c64-docs/scripts/docs.ts +24 -11
- package/skills/c64-docs/scripts/lib/reference.ts +119 -33
- package/skills/c64-docs/scripts/lib/source-map.ts +23 -4
- package/skills/c64-knowledge/SKILL.md +67 -42
- package/skills/c64-knowledge/scripts/knowledge.ts +90 -67
- package/skills/c64-knowledge/scripts/lib/listing.ts +18 -3
- package/skills/c64-packer/SKILL.md +1 -1
- package/skills/c64-provenance/SKILL.md +11 -10
- package/skills/c64-provenance/scripts/lib/library.ts +16 -16
- package/skills/c64-provenance/scripts/provenance.ts +33 -23
- package/skills/c64-reverse-engineering/SKILL.md +17 -11
- package/skills/c64-source/SKILL.md +71 -47
- package/skills/c64-source/scripts/lib/export.ts +89 -34
- package/skills/c64-source/scripts/lib/loader.ts +79 -36
- package/skills/c64-source/scripts/lib/source.ts +38 -32
- package/skills/c64-source/scripts/lib/stage.ts +11 -5
- package/skills/c64-source/scripts/lib/start-screen.asm +4 -2
- package/skills/c64-source/scripts/source.ts +278 -145
- package/skills/c64-static-analysis/SKILL.md +29 -25
- package/skills/c64-static-analysis/scripts/lib/coverage.ts +2 -25
- package/skills/c64-static-analysis/scripts/lib/findings.ts +11 -11
- package/skills/c64-static-analysis/scripts/lib/instructions.ts +26 -24
- package/skills/c64-static-analysis/scripts/lib/macros.ts +3 -1
- package/skills/c64-static-analysis/scripts/static-analysis.ts +69 -67
- package/skills/c64-testing/SKILL.md +15 -5
- package/skills/c64-testing/references/recording-format.md +26 -5
- package/skills/c64-testing/references/scenario-format.md +11 -8
- package/skills/c64-testing/scripts/lib/diff-files.ts +4 -18
- package/skills/c64-testing/scripts/lib/play.ts +39 -41
- package/skills/c64-testing/scripts/lib/recording.ts +220 -46
- package/skills/c64-testing/scripts/lib/replay.ts +32 -6
- package/skills/c64-testing/scripts/lib/runs.ts +8 -3
- package/skills/c64-testing/scripts/lib/scenario.ts +18 -11
- package/skills/c64-testing/scripts/testing.ts +23 -2
- package/skills/c64-unpacker/SKILL.md +10 -5
- package/skills/c64-unpacker/scripts/lib/loads.ts +34 -13
- package/skills/c64-unpacker/scripts/lib/machine.ts +7 -3
- package/skills/c64-unpacker/scripts/lib/stages.ts +18 -3
- package/skills/c64-unpacker/scripts/unpacker.ts +43 -9
- package/skills/emulator/SKILL.md +7 -12
- package/src/build-name.ts +55 -0
- package/src/c64-keys.ts +98 -0
- package/src/c64.ts +22 -2
- package/src/cli/main.ts +3 -29
- package/src/host/main.ts +3 -3
- package/src/host/server.ts +3 -1
- package/src/host/tools/index.ts +2 -7
- package/src/host/vice/process.ts +34 -17
- package/src/host/vice/session.ts +138 -31
- package/src/host-client/steps.ts +21 -18
- package/src/host-client/tools.ts +2 -7
- package/src/host-client/vice-session.ts +7 -1
- package/src/knowledge/evidence.ts +66 -0
- package/src/knowledge/history.ts +5 -5
- package/src/knowledge/import.ts +12 -5
- package/src/knowledge/program.ts +123 -32
- package/src/knowledge/read.ts +36 -13
- package/src/knowledge/schema.ts +43 -17
- package/src/knowledge/start-screen.ts +70 -0
- package/src/knowledge/text.ts +33 -5
- package/src/knowledge/write.ts +75 -36
- package/src/mcp/inputs.ts +12 -0
- package/src/mcp/server.ts +1 -1
- package/src/mcp/tools/debug.ts +23 -21
- package/src/mcp/tools/input.ts +38 -5
- package/src/mcp/tools/machine.ts +3 -42
- package/src/mcp/tools/media.ts +1 -1
- package/src/mcp/tools/memory.ts +12 -9
- package/src/native/acme.ts +1 -1
- package/src/native/c64run.ts +7 -4
- package/src/native/decompiler/analyze.ts +98 -0
- package/src/native/decompiler/c64rt-wasm.ts +4 -0
- package/src/native/decompiler/language/c64rt_6510.sla +0 -0
- package/src/native/{ghidra → decompiler}/language/c64rt_6510.slaspec +1 -1
- package/src/native/decompiler/pcode.ts +222 -0
- package/src/native/decompiler/walk.ts +205 -0
- package/src/native/decompiler/wasm.ts +98 -0
- package/src/native/discover.ts +3 -3
- package/src/native/dxa.ts +12 -1
- package/src/native/processes.ts +4 -4
- package/src/native/types.ts +27 -27
- package/src/opcodes.ts +1 -1
- package/src/platform.ts +30 -1
- package/src/project.ts +17 -4
- package/src/protocol/tools.ts +1 -45
- package/src/protocol/vice.ts +47 -0
- package/src/reference-pages.ts +64 -0
- package/src/script.ts +4 -4
- package/src/vsf.ts +98 -0
- package/tools/c64run.c +158 -15
- package/tools/install-container.sh +1 -2
- package/src/cli/status.ts +0 -127
- package/src/host/tools/status.ts +0 -47
- package/src/native/ghidra/analyze.ts +0 -87
- package/src/native/ghidra/index.ts +0 -170
- package/src/native/ghidra/scripts/C64Export.java +0 -274
- package/src/native/ghidra/scripts/C64Prepare.java +0 -66
- package/src/native/status.ts +0 -135
- package/tools/install-ghidra.sh +0 -40
- /package/src/native/{ghidra → decompiler}/language/c64rt_6510.cspec +0 -0
- /package/src/native/{ghidra → decompiler}/language/c64rt_6510.ldefs +0 -0
- /package/src/native/{ghidra → decompiler}/language/c64rt_6510.pspec +0 -0
package/src/knowledge/program.ts
CHANGED
|
@@ -11,7 +11,7 @@ import { type AddressRange, COVERAGE, formatC64Address } 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";
|
|
14
|
-
import { checkSpace, write, type Evidence, type WriteContext } from "./write.ts";
|
|
14
|
+
import { checkSpace, write, type Evidence, type OperandImage, type WriteContext } from "./write.ts";
|
|
15
15
|
|
|
16
16
|
/** The 64 KiB of one space: offset n is address n. */
|
|
17
17
|
const SIZE = 0x10000;
|
|
@@ -139,53 +139,104 @@ export function setImage(db: DatabaseSync, context: WriteContext, request: Image
|
|
|
139
139
|
});
|
|
140
140
|
}
|
|
141
141
|
|
|
142
|
-
/**
|
|
142
|
+
/** Address ranges as the knowledge text keeps them: "$1000-$1002 $1005". */
|
|
143
|
+
function rangesText(ranges: readonly AddressRange[]): string {
|
|
144
|
+
const at = (address: number) => `$${address.toString(16).padStart(4, "0")}`;
|
|
145
|
+
return ranges.map((range) => (range.start === range.end ? at(range.start) : `${at(range.start)}-${at(range.end)}`)).join(" ");
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
/** Sets 1 in `map` at each address of ranges as text. */
|
|
149
|
+
function fillRanges(map: Uint8Array, text: string): void {
|
|
150
|
+
for (const item of text.split(" ")) {
|
|
151
|
+
const [start, end] = item.split("-");
|
|
152
|
+
map.fill(1, Number.parseInt(start!.slice(1), 16), Number.parseInt((end ?? start)!.slice(1), 16) + 1);
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
/** The union of the coverage of one space: a map of each kind, 1 at each address that a map of the space has. */
|
|
143
157
|
function currentCoverage(db: DatabaseSync, space: Space): Map<CoverageKind, Uint8Array> {
|
|
144
158
|
const maps = new Map<CoverageKind, Uint8Array>(COVERAGE_KINDS.map((kind) => [kind, new Uint8Array(SIZE)]));
|
|
145
|
-
for (const row of db.prepare("SELECT kind,
|
|
146
|
-
maps.get(row.kind)!.fill(1, row.start_address, row.end_address + 1);
|
|
147
|
-
}
|
|
159
|
+
for (const row of db.prepare("SELECT kind, ranges FROM current_coverage WHERE space = ?").all(space) as Array<{ kind: CoverageKind; ranges: string }>) fillRanges(maps.get(row.kind)!, row.ranges);
|
|
148
160
|
return maps;
|
|
149
161
|
}
|
|
150
162
|
|
|
163
|
+
/** The number of address ranges of each kind in the coverage of a space: the ranges of the union of its maps. */
|
|
164
|
+
export function coverageRangeCounts(db: DatabaseSync | undefined, space: Space = DEFAULT_SPACE): Partial<Record<CoverageKind, number>> {
|
|
165
|
+
if (db === undefined) return {};
|
|
166
|
+
const counts: Partial<Record<CoverageKind, number>> = {};
|
|
167
|
+
for (const [kind, map] of currentCoverage(db, space)) {
|
|
168
|
+
const count = rangesWhere((address) => map[address] === 1).length;
|
|
169
|
+
if (count > 0) counts[kind] = count;
|
|
170
|
+
}
|
|
171
|
+
return counts;
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
/** The ranges of each kind of one coverage map as text, for each kind that it has. wx is each address that the run both wrote and executed. */
|
|
175
|
+
function mapRanges(bytes: Uint8Array): Map<CoverageKind, string> {
|
|
176
|
+
const ranges = new Map<CoverageKind, string>();
|
|
177
|
+
for (const kind of COVERAGE_KINDS) {
|
|
178
|
+
const has = kind === "wx" ? (address: number) => (bytes[address]! & (COVERAGE.ramWrite | COVERAGE.ramExecute)) === (COVERAGE.ramWrite | COVERAGE.ramExecute) : (address: number) => (bytes[address]! & KIND_BITS[kind]) !== 0;
|
|
179
|
+
const found = rangesWhere(has);
|
|
180
|
+
if (found.length > 0) ranges.set(kind, rangesText(found));
|
|
181
|
+
}
|
|
182
|
+
return ranges;
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
/** The number of ranges of each kind that are in `after` and not in `before`. */
|
|
186
|
+
function gained(before: Map<CoverageKind, Uint8Array>, after: Map<CoverageKind, Uint8Array>): number {
|
|
187
|
+
let count = 0;
|
|
188
|
+
for (const kind of COVERAGE_KINDS) count += rangesWhere((address) => after.get(kind)![address] === 1 && before.get(kind)![address] === 0).length;
|
|
189
|
+
return count;
|
|
190
|
+
}
|
|
191
|
+
|
|
151
192
|
/**
|
|
152
|
-
*
|
|
153
|
-
*
|
|
154
|
-
*
|
|
155
|
-
*
|
|
156
|
-
*
|
|
193
|
+
* Puts coverage maps into the coverage of a space. The knowledge keeps the
|
|
194
|
+
* ranges of each map by its project path; the coverage of the space is the
|
|
195
|
+
* union of the maps. A map for a path that the space has already replaces
|
|
196
|
+
* the earlier map of that path: a run that is recorded again counts only
|
|
197
|
+
* with its new map. wx is each address that one run both wrote and
|
|
198
|
+
* executed. Each new map is evidence of the revision, with its space. A map
|
|
199
|
+
* that is the latest map of its path in the space is not imported again.
|
|
157
200
|
*/
|
|
158
201
|
export function addCoverage(
|
|
159
202
|
db: DatabaseSync,
|
|
160
203
|
context: WriteContext,
|
|
161
204
|
request: { space?: Space; maps: CoverageMap[] },
|
|
162
|
-
): { revision: string | null; current: { space: Space; imported: string[]; known: string[];
|
|
205
|
+
): { revision: string | null; current: { space: Space; imported: string[]; replaced: string[]; known: string[]; added: number; removed: number } } {
|
|
163
206
|
checkSpace(request);
|
|
164
207
|
const space = request.space ?? DEFAULT_SPACE;
|
|
165
208
|
for (const map of request.maps) {
|
|
166
209
|
if (map.bytes.length !== SIZE) throw new Refusal("invalid-input", `${map.name} has ${map.bytes.length} bytes; a coverage map has 65536, one for each address.`);
|
|
167
210
|
}
|
|
168
|
-
|
|
169
|
-
const
|
|
170
|
-
const
|
|
211
|
+
// A path given twice is one map: the last one.
|
|
212
|
+
const byName = new Map(request.maps.map((map) => [map.name, map]));
|
|
213
|
+
const hashed = [...byName.values()].map((map) => ({ ...map, sha256: createHash("sha256").update(map.bytes).digest("hex") }));
|
|
214
|
+
// The runs of a name are oldest first: the last one is the latest map of the path.
|
|
215
|
+
const latest = new Map(coverageRuns(db).filter((run) => run.space === space).map((run) => [run.name, run.sha256]));
|
|
216
|
+
const withRows = new Set((db.prepare("SELECT DISTINCT map FROM current_coverage WHERE space = ?").all(space) as Array<{ map: string }>).map((row) => row.map));
|
|
217
|
+
// A map that has no access has no rows: it is known by its SHA-256 alone.
|
|
218
|
+
const isKnown = (map: (typeof hashed)[number]) => latest.get(map.name) === map.sha256 && (withRows.has(map.name) || map.bytes.every((byte) => byte === 0));
|
|
219
|
+
const fresh = hashed.filter((map) => !isKnown(map));
|
|
171
220
|
const evidence: Evidence[] = [...(context.evidence ?? []), ...fresh.map((map): Evidence => ({ kind: "coverage", ref: map.name, sha256: map.sha256, ...(space === DEFAULT_SPACE ? {} : { space }) }))];
|
|
172
221
|
return write(db, { ...context, evidence }, "coverage", (change) => {
|
|
173
|
-
const result = { space, imported: fresh.map((map) => map.name), known: hashed.filter((map) => !fresh.includes(map)).map((map) => map.name),
|
|
222
|
+
const result = { space, imported: fresh.map((map) => map.name), replaced: fresh.filter((map) => latest.has(map.name) && latest.get(map.name) !== map.sha256).map((map) => map.name), known: hashed.filter((map) => !fresh.includes(map)).map((map) => map.name), added: 0, removed: 0 };
|
|
174
223
|
if (fresh.length === 0) return result;
|
|
175
|
-
// A revision for each import, also when
|
|
224
|
+
// A revision for each import, also when the coverage does not change: the maps are its evidence.
|
|
176
225
|
change.id();
|
|
177
|
-
const
|
|
226
|
+
const before = currentCoverage(db, space);
|
|
178
227
|
for (const map of fresh) {
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
result.ranges++;
|
|
186
|
-
}
|
|
228
|
+
// Only a kind whose ranges differ from the earlier map of the path changes.
|
|
229
|
+
const wanted = mapRanges(map.bytes);
|
|
230
|
+
const rows = db.prepare("SELECT id, kind, ranges FROM current_coverage WHERE space = ? AND map = ?").all(space, map.name) as Array<{ id: string; kind: CoverageKind; ranges: string }>;
|
|
231
|
+
for (const row of rows) {
|
|
232
|
+
if (wanted.get(row.kind) === row.ranges) wanted.delete(row.kind);
|
|
233
|
+
else change.close("coverage", "id = ?", row.id);
|
|
187
234
|
}
|
|
235
|
+
for (const [kind, ranges] of wanted) change.insertCoverage({ space, map: map.name, kind, ranges, origin: context.origin });
|
|
188
236
|
}
|
|
237
|
+
const after = currentCoverage(db, space);
|
|
238
|
+
result.added = gained(before, after);
|
|
239
|
+
result.removed = gained(after, before);
|
|
189
240
|
return result;
|
|
190
241
|
});
|
|
191
242
|
}
|
|
@@ -250,7 +301,7 @@ export function entryMemory(image: ProgramImage): Uint8Array {
|
|
|
250
301
|
|
|
251
302
|
/**
|
|
252
303
|
* The bytes for an analyzer: a PRG for one segment without an entry state,
|
|
253
|
-
* else the 64 KiB with the entry-state pages (entryMemory). DXA,
|
|
304
|
+
* else the 64 KiB with the entry-state pages (entryMemory). DXA, the decompiler and
|
|
254
305
|
* c64-source get the same bytes.
|
|
255
306
|
*/
|
|
256
307
|
export function analyzedBytes(image: ProgramImage): { bytes: Buffer; kind: "prg" | "flat64k" } {
|
|
@@ -268,13 +319,53 @@ export function analyzedBytes(image: ProgramImage): { bytes: Buffer; kind: "prg"
|
|
|
268
319
|
*/
|
|
269
320
|
export function programCoverage(db: DatabaseSync | undefined, space: Space = DEFAULT_SPACE): { map: Uint8Array; selfModified: Uint8Array } | undefined {
|
|
270
321
|
if (db === undefined) return undefined;
|
|
271
|
-
const
|
|
272
|
-
if (
|
|
322
|
+
const has = db.prepare("SELECT 1 FROM current_coverage WHERE space = ? LIMIT 1").get(space);
|
|
323
|
+
if (has === undefined) return undefined;
|
|
324
|
+
const union = currentCoverage(db, space);
|
|
273
325
|
const map = new Uint8Array(SIZE);
|
|
274
|
-
const
|
|
326
|
+
for (const [kind, marks] of union) if (kind !== "wx") for (let address = 0; address < SIZE; address++) if (marks[address] === 1) map[address]! |= KIND_BITS[kind];
|
|
327
|
+
return { map, selfModified: union.get("wx")! };
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
/**
|
|
331
|
+
* The program of each space in the knowledge that operand, patch and macro
|
|
332
|
+
* use writes in that space are checked against, with the platform names. A
|
|
333
|
+
* byte outside the segments of the program is not in it. Each program is
|
|
334
|
+
* read once, when a write in its space needs it.
|
|
335
|
+
*/
|
|
336
|
+
export function operandImages(db: DatabaseSync, platform: ReadonlyMap<string, number>): (space: string) => OperandImage {
|
|
337
|
+
const images = new Map<string, OperandImage>();
|
|
338
|
+
return (space) => {
|
|
339
|
+
const known = images.get(space);
|
|
340
|
+
if (known !== undefined) return known;
|
|
341
|
+
const image = requireProgram(db, space);
|
|
342
|
+
const inside = (at: number) => image.segments.some((segment) => at >= segment.start && at <= segment.end);
|
|
343
|
+
const made: OperandImage = { byteAt: (at) => (inside(at) ? image.byteAt(at) : undefined), platform };
|
|
344
|
+
images.set(space, made);
|
|
345
|
+
return made;
|
|
346
|
+
};
|
|
347
|
+
}
|
|
348
|
+
|
|
349
|
+
/**
|
|
350
|
+
* The bytes of a release: the program for the base release, else the bytes
|
|
351
|
+
* that the knowledge keeps for the release. Undefined when the space has no
|
|
352
|
+
* release with this label.
|
|
353
|
+
*/
|
|
354
|
+
export function releaseImage(db: DatabaseSync | undefined, label: string, space: Space = DEFAULT_SPACE): Pick<ProgramImage, "segments" | "byteAt"> | undefined {
|
|
355
|
+
if (db === undefined) return undefined;
|
|
356
|
+
const release = db.prepare("SELECT base FROM current_releases WHERE space = ? AND label = ?").get(space, label) as { base: number } | undefined;
|
|
357
|
+
if (release === undefined) return undefined;
|
|
358
|
+
if (release.base === 1) return programImage(db, space);
|
|
359
|
+
const rows = db.prepare("SELECT address, bytes FROM current_release_memory WHERE space = ? AND label = ? ORDER BY address").all(space, label) as Array<{ address: number; bytes: string }>;
|
|
360
|
+
const memory = new Uint8Array(SIZE);
|
|
361
|
+
const segments: AddressRange[] = [];
|
|
275
362
|
for (const row of rows) {
|
|
276
|
-
|
|
277
|
-
|
|
363
|
+
const bytes = Buffer.from(row.bytes, "hex");
|
|
364
|
+
memory.set(bytes, row.address);
|
|
365
|
+
const end = row.address + bytes.length - 1;
|
|
366
|
+
const last = segments.at(-1);
|
|
367
|
+
if (last !== undefined && last.end + 1 === row.address) last.end = end;
|
|
368
|
+
else segments.push({ start: row.address, end });
|
|
278
369
|
}
|
|
279
|
-
return {
|
|
370
|
+
return { segments, byteAt: (address) => memory[address]! };
|
|
280
371
|
}
|
package/src/knowledge/read.ts
CHANGED
|
@@ -7,7 +7,7 @@ import type { DatabaseSync } from "node:sqlite";
|
|
|
7
7
|
import type { AddressRange } from "../c64.ts";
|
|
8
8
|
import { Refusal } from "../errors.ts";
|
|
9
9
|
import { DEFAULT_SPACE, type CommentPlacement, type OperandSelect, type Origin, type ReferenceKind, type RegionType, type Space, type StartScreenMode, type SuppressionEntity, type SwitchKind, type SymbolKind } from "./schema.ts";
|
|
10
|
-
import { NO_REVISION } from "./text.ts";
|
|
10
|
+
import { compareText, NO_REVISION, sqlName, TABLES } from "./text.ts";
|
|
11
11
|
|
|
12
12
|
export interface SymbolRow {
|
|
13
13
|
space: Space;
|
|
@@ -114,12 +114,10 @@ export interface SwitchRow {
|
|
|
114
114
|
revision: string;
|
|
115
115
|
}
|
|
116
116
|
|
|
117
|
-
/** A release of the program
|
|
117
|
+
/** A release of the program, and whether its bytes are the program of the knowledge. The bytes of another release are in release_memory. */
|
|
118
118
|
export interface ReleaseRow {
|
|
119
119
|
space: Space;
|
|
120
120
|
label: string;
|
|
121
|
-
file: string;
|
|
122
|
-
sha256: string;
|
|
123
121
|
base: boolean;
|
|
124
122
|
comment?: string;
|
|
125
123
|
origin: Extract<Origin, "user" | "llm">;
|
|
@@ -134,6 +132,7 @@ export interface ReleaseRow {
|
|
|
134
132
|
* Without `stage`, the bytes are in the program at the entry. With `stage`,
|
|
135
133
|
* they are in the memory of that space (a captured stage of the loading, for
|
|
136
134
|
* example), and `call` is a routine of the stage that the start screen calls.
|
|
135
|
+
* `colourRam` takes the colours from the colour RAM of the capture of the stage.
|
|
137
136
|
*/
|
|
138
137
|
export interface StartScreenRow {
|
|
139
138
|
space: Space;
|
|
@@ -147,6 +146,7 @@ export interface StartScreenRow {
|
|
|
147
146
|
background: number;
|
|
148
147
|
stage?: Space;
|
|
149
148
|
call?: number;
|
|
149
|
+
colourRam?: true;
|
|
150
150
|
comment?: string;
|
|
151
151
|
origin: Extract<Origin, "user" | "llm">;
|
|
152
152
|
revision: string;
|
|
@@ -199,10 +199,10 @@ export const MODULE_COLUMNS = "space, start_address, file, title, origin, valid_
|
|
|
199
199
|
export const OPERAND_COLUMNS = "space, address, selector, symbol, offset, expression, origin, valid_from_revision";
|
|
200
200
|
export const PATCH_COLUMNS = "space, start_address, end_address, switch_name, code, comment, origin, valid_from_revision";
|
|
201
201
|
export const SWITCH_COLUMNS = "space, name, kind, default_value, releases, comment, origin, valid_from_revision";
|
|
202
|
-
export const RELEASE_COLUMNS = "space, label,
|
|
202
|
+
export const RELEASE_COLUMNS = "space, label, base, comment, origin, valid_from_revision";
|
|
203
203
|
export const MACRO_COLUMNS = "space, name, params, body, comment, origin, valid_from_revision";
|
|
204
204
|
export const MACRO_USE_COLUMNS = "space, start_address, end_address, macro, args, origin, valid_from_revision";
|
|
205
|
-
export const START_SCREEN_COLUMNS = "space, mode, screen, bitmap, charset, colours, colour, border, background, stage, call_address, comment, origin, valid_from_revision";
|
|
205
|
+
export const START_SCREEN_COLUMNS = "space, mode, screen, bitmap, charset, colours, colour, border, background, stage, call_address, colour_ram, comment, origin, valid_from_revision";
|
|
206
206
|
|
|
207
207
|
export function toSymbol(row: Raw): SymbolRow {
|
|
208
208
|
return { space: row.space as Space, address: row.address as number, name: row.name as string, kind: row.kind as SymbolKind, origin: row.origin as Origin, revision: row.valid_from_revision as string };
|
|
@@ -302,8 +302,6 @@ export function toRelease(row: Raw): ReleaseRow {
|
|
|
302
302
|
const release: ReleaseRow = {
|
|
303
303
|
space: row.space as Space,
|
|
304
304
|
label: row.label as string,
|
|
305
|
-
file: row.file as string,
|
|
306
|
-
sha256: row.sha256 as string,
|
|
307
305
|
base: row.base === 1,
|
|
308
306
|
origin: row.origin as ReleaseRow["origin"],
|
|
309
307
|
revision: row.valid_from_revision as string,
|
|
@@ -325,6 +323,7 @@ export function toStartScreen(row: Raw): StartScreenRow {
|
|
|
325
323
|
for (const key of ["bitmap", "charset", "colours", "colour"] as const) if (row[key] !== null && row[key] !== undefined) value[key] = row[key] as number;
|
|
326
324
|
if (row.stage !== null && row.stage !== undefined) value.stage = row.stage as Space;
|
|
327
325
|
if (row.call_address !== null && row.call_address !== undefined) value.call = row.call_address as number;
|
|
326
|
+
if (row.colour_ram === 1) value.colourRam = true;
|
|
328
327
|
if (row.comment !== null && row.comment !== undefined) value.comment = row.comment as string;
|
|
329
328
|
return value;
|
|
330
329
|
}
|
|
@@ -353,6 +352,30 @@ export function currentRevision(db: DatabaseSync | undefined): string {
|
|
|
353
352
|
return (db.prepare("SELECT max(id) AS id FROM revisions").get() as { id: string | null }).id ?? "0";
|
|
354
353
|
}
|
|
355
354
|
|
|
355
|
+
/**
|
|
356
|
+
* The newest revision that changed a row in one of `spaces`, or the revision
|
|
357
|
+
* of no knowledge when none did. A change in another space does not change it.
|
|
358
|
+
*/
|
|
359
|
+
export function spacesRevision(db: DatabaseSync | undefined, spaces: readonly Space[]): string {
|
|
360
|
+
if (db === undefined || spaces.length === 0) return NO_REVISION;
|
|
361
|
+
const marks = spaces.map(() => "?").join(", ");
|
|
362
|
+
let newest = NO_REVISION;
|
|
363
|
+
for (const spec of TABLES) {
|
|
364
|
+
const row = db.prepare(`SELECT max(valid_from_revision) AS added, max(valid_to_revision) AS closed FROM ${sqlName(spec.table)} WHERE space IN (${marks})`).get(...spaces) as { added: string | null; closed: string | null };
|
|
365
|
+
for (const id of [row.added, row.closed]) if (id !== null && compareText(id, newest) > 0) newest = id;
|
|
366
|
+
}
|
|
367
|
+
return newest;
|
|
368
|
+
}
|
|
369
|
+
|
|
370
|
+
/**
|
|
371
|
+
* The revision of the source of a space: the newest revision of that space and
|
|
372
|
+
* of the stage of its start screen. A change in another space does not change it.
|
|
373
|
+
*/
|
|
374
|
+
export function sourceRevision(db: DatabaseSync | undefined, space: Space = DEFAULT_SPACE): string {
|
|
375
|
+
const stage = space === DEFAULT_SPACE ? currentStartScreen(db, space)?.stage : undefined;
|
|
376
|
+
return spacesRevision(db, [space, ...(stage === undefined ? [] : [stage])]);
|
|
377
|
+
}
|
|
378
|
+
|
|
356
379
|
/** One import of a coverage map: its project path, its SHA-256, the space it went into and the revision. */
|
|
357
380
|
export interface CoverageRun {
|
|
358
381
|
name: string;
|
|
@@ -428,7 +451,7 @@ export function symbolNamed(db: DatabaseSync | undefined, name: string, space: S
|
|
|
428
451
|
return row === undefined ? undefined : toSymbol(row);
|
|
429
452
|
}
|
|
430
453
|
|
|
431
|
-
/** The analyzer names at an address: one for each analyzer, in the order heuristic,
|
|
454
|
+
/** The analyzer names at an address: one for each analyzer, in the order heuristic, decompiler, DXA. */
|
|
432
455
|
export function analyzerNamesAt(db: DatabaseSync | undefined, address: number, space: Space = DEFAULT_SPACE): SymbolRow[] {
|
|
433
456
|
if (db === undefined) return [];
|
|
434
457
|
return (db.prepare(`SELECT ${SYMBOL_COLUMNS} FROM current_analyzer_names WHERE space = ? AND address = ?`).all(space, address) as Raw[]).map(toSymbol).sort(byAnalyzer);
|
|
@@ -441,10 +464,10 @@ export function analyzerNamed(db: DatabaseSync | undefined, name: string, space:
|
|
|
441
464
|
}
|
|
442
465
|
|
|
443
466
|
/** The analyzers in the order that their names are used when no other name exists. */
|
|
444
|
-
export const ANALYZER_NAME_ORDER: readonly Origin[] = ["heuristic", "
|
|
467
|
+
export const ANALYZER_NAME_ORDER: readonly Origin[] = ["heuristic", "decompiler", "dxa"];
|
|
445
468
|
const byAnalyzer = (a: SymbolRow, b: SymbolRow) => a.address - b.address || ANALYZER_NAME_ORDER.indexOf(a.origin) - ANALYZER_NAME_ORDER.indexOf(b.origin);
|
|
446
469
|
|
|
447
|
-
/** Current analyzer names in a range, in address order and then heuristic,
|
|
470
|
+
/** Current analyzer names in a range, in address order and then heuristic, decompiler, DXA. */
|
|
448
471
|
export function listAnalyzerNames(db: DatabaseSync | undefined, range: AddressRange = ALL, kind?: SymbolKind, origins?: OriginFilter, space: Space = DEFAULT_SPACE): SymbolRow[] {
|
|
449
472
|
if (db === undefined) return [];
|
|
450
473
|
return (db.prepare(`SELECT ${SYMBOL_COLUMNS} FROM current_analyzer_names WHERE space = ? AND address BETWEEN ? AND ?`).all(space, range.start, range.end) as Raw[])
|
|
@@ -520,7 +543,7 @@ export function moduleAt(db: DatabaseSync | undefined, start: number, space: Spa
|
|
|
520
543
|
}
|
|
521
544
|
|
|
522
545
|
export function moduleWithFile(db: DatabaseSync | undefined, file: string, space: Space = DEFAULT_SPACE): ModuleRow | undefined {
|
|
523
|
-
const row = db?.prepare(`SELECT ${MODULE_COLUMNS} FROM current_modules WHERE space = ? AND file =
|
|
546
|
+
const row = db?.prepare(`SELECT ${MODULE_COLUMNS} FROM current_modules WHERE space = ? AND file = ? COLLATE NOCASE`).get(space, file) as Raw | undefined;
|
|
524
547
|
return row === undefined ? undefined : toModule(row);
|
|
525
548
|
}
|
|
526
549
|
|
|
@@ -742,7 +765,7 @@ export function listSpaces(db: DatabaseSync | undefined): Space[] {
|
|
|
742
765
|
`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_start_screens UNION SELECT space FROM current_memory UNION SELECT space FROM current_entries UNION SELECT space FROM current_coverage`,
|
|
743
766
|
)
|
|
744
767
|
.all() as Array<{ space: Space }>;
|
|
745
|
-
const order = (space: Space) => (space === "c64" ? -
|
|
768
|
+
const order = (space: Space) => (space === "c64" ? -3 : space === "drive8" ? -2 : space === "loading" ? -1 : Number(space.slice(4)));
|
|
746
769
|
return rows.map((row) => row.space).sort((a, b) => order(a) - order(b));
|
|
747
770
|
}
|
|
748
771
|
|
package/src/knowledge/schema.ts
CHANGED
|
@@ -8,12 +8,12 @@ import { REFERENCE_KINDS, type ReferenceKind } from "../c64.ts";
|
|
|
8
8
|
import { OPERAND_SELECTS } from "./expression.ts";
|
|
9
9
|
|
|
10
10
|
/** heuristic: 6502 idioms that the static analysis finds by pattern; an analyzer origin. */
|
|
11
|
-
export const ORIGINS = ["user", "llm", "dxa", "
|
|
11
|
+
export const ORIGINS = ["user", "llm", "dxa", "decompiler", "heuristic"] as const;
|
|
12
12
|
export type Origin = (typeof ORIGINS)[number];
|
|
13
13
|
/** The origins of semantic knowledge: what the user or the LLM decided. It wins over analyzer facts. */
|
|
14
14
|
export const SEMANTIC_ORIGINS = ["user", "llm"] as const satisfies readonly Origin[];
|
|
15
15
|
/** The analyzers: their facts and names give way to semantic knowledge. */
|
|
16
|
-
export const ANALYZERS = ["dxa", "
|
|
16
|
+
export const ANALYZERS = ["dxa", "decompiler", "heuristic"] as const satisfies readonly Origin[];
|
|
17
17
|
export type Analyzer = (typeof ANALYZERS)[number];
|
|
18
18
|
export const isSemantic = (origin: string | undefined): boolean => (SEMANTIC_ORIGINS as readonly (string | undefined)[]).includes(origin);
|
|
19
19
|
export const isAnalyzer = (origin: string | undefined): boolean => (ANALYZERS as readonly (string | undefined)[]).includes(origin);
|
|
@@ -56,8 +56,12 @@ export { OPERAND_SELECTS, type OperandSelect } from "./expression.ts";
|
|
|
56
56
|
export const SUPPRESSION_ENTITIES = ["symbol", "region", "reference"] as const;
|
|
57
57
|
export type SuppressionEntity = (typeof SUPPRESSION_ENTITIES)[number];
|
|
58
58
|
|
|
59
|
-
/**
|
|
60
|
-
|
|
59
|
+
/**
|
|
60
|
+
* A module file of the generated source: a name that ends in .asm, in the
|
|
61
|
+
* source folder or in one folder of it, for example music/driver.asm. A folder
|
|
62
|
+
* name is lower case, so two modules cannot differ only in its letter case.
|
|
63
|
+
*/
|
|
64
|
+
export const MODULE_FILE = /^(?:[a-z0-9_][a-z0-9_-]{0,29}\/)?[A-Za-z0-9_][A-Za-z0-9_-]{0,59}\.asm$/;
|
|
61
65
|
/** A switch of the generated source: upper case, so it reads as a build option and not as a label. */
|
|
62
66
|
export const SWITCH_NAME = /^[A-Z][A-Z0-9_]{1,31}$/;
|
|
63
67
|
/** A release label: the switch-name form. The build of a release defines RELEASE_<label>. */
|
|
@@ -100,12 +104,22 @@ export const MEMORY_ROW = 64;
|
|
|
100
104
|
/** Files that c64-source writes itself: no module can have these names. */
|
|
101
105
|
export const GENERATED_FILES = ["main.asm", "loader.asm", "switches.asm", "config.asm", "constants.asm", "macros.asm", "start_screen.asm"] as const;
|
|
102
106
|
|
|
103
|
-
/** The
|
|
104
|
-
export const
|
|
107
|
+
/** The folder of the leftovers in the generated source: leftovers/leftover_xxxx.asm. */
|
|
108
|
+
export const LEFTOVER_FOLDER = "leftovers";
|
|
109
|
+
/** The folder of the parts of segments that no module holds: segments/segment_xxxx.asm. */
|
|
110
|
+
export const SEGMENT_FOLDER = "segments";
|
|
111
|
+
/** The folders that c64-source writes itself: no module file is in them. */
|
|
112
|
+
export const GENERATED_FOLDERS = [LEFTOVER_FOLDER, SEGMENT_FOLDER] as const;
|
|
105
113
|
|
|
106
|
-
/**
|
|
114
|
+
/**
|
|
115
|
+
* True for a generated file name in any letter case (the file systems of macOS
|
|
116
|
+
* and Windows ignore case), also in a folder: a folder with a generated
|
|
117
|
+
* main.asm is the source of another space, not a folder of modules.
|
|
118
|
+
*/
|
|
107
119
|
export function isGeneratedFile(file: string): boolean {
|
|
108
|
-
|
|
120
|
+
const lower = file.toLowerCase();
|
|
121
|
+
const name = lower.slice(lower.lastIndexOf("/") + 1);
|
|
122
|
+
return (GENERATED_FILES as readonly string[]).includes(name) || GENERATED_FOLDERS.some((folder) => lower.startsWith(`${folder}/`));
|
|
109
123
|
}
|
|
110
124
|
|
|
111
125
|
/**
|
|
@@ -114,7 +128,7 @@ export function isGeneratedFile(file: string): boolean {
|
|
|
114
128
|
*/
|
|
115
129
|
export type Space = string;
|
|
116
130
|
export const DEFAULT_SPACE: Space = "c64";
|
|
117
|
-
const SPACE_CHECK = "space IN ('c64', 'drive8') OR space GLOB 'bank[0-9]' OR space GLOB 'bank[1-9][0-9]' OR space GLOB 'bank[12][0-9][0-9]'";
|
|
131
|
+
const SPACE_CHECK = "space IN ('c64', 'drive8', 'loading') OR space GLOB 'bank[0-9]' OR space GLOB 'bank[1-9][0-9]' OR space GLOB 'bank[12][0-9][0-9]'";
|
|
118
132
|
|
|
119
133
|
const list = (values: readonly string[]) => values.map((value) => `'${value}'`).join(", ");
|
|
120
134
|
|
|
@@ -162,7 +176,7 @@ function createSchema(db: DatabaseSync): void {
|
|
|
162
176
|
address INTEGER NOT NULL CHECK (address BETWEEN 0 AND 65535),
|
|
163
177
|
name TEXT NOT NULL CHECK (length(name) > 0),
|
|
164
178
|
kind TEXT NOT NULL CHECK (kind IN (${list(SYMBOL_KINDS)})),
|
|
165
|
-
origin TEXT NOT NULL CHECK (origin IN ('dxa', '
|
|
179
|
+
origin TEXT NOT NULL CHECK (origin IN ('dxa', 'decompiler', 'heuristic')),
|
|
166
180
|
valid_from_revision TEXT NOT NULL REFERENCES revisions (id),
|
|
167
181
|
valid_to_revision TEXT REFERENCES revisions (id)
|
|
168
182
|
);
|
|
@@ -287,8 +301,6 @@ function createSchema(db: DatabaseSync): void {
|
|
|
287
301
|
id TEXT PRIMARY KEY,
|
|
288
302
|
space TEXT NOT NULL DEFAULT 'c64' CHECK (${SPACE_CHECK}),
|
|
289
303
|
label TEXT NOT NULL CHECK (length(label) > 0),
|
|
290
|
-
file TEXT NOT NULL CHECK (length(file) > 0),
|
|
291
|
-
sha256 TEXT NOT NULL CHECK (length(sha256) = 64),
|
|
292
304
|
base INTEGER NOT NULL CHECK (base IN (0, 1)),
|
|
293
305
|
comment TEXT CHECK (comment IS NULL OR length(comment) > 0),
|
|
294
306
|
origin TEXT NOT NULL CHECK (origin IN ('user', 'llm')),
|
|
@@ -321,15 +333,17 @@ function createSchema(db: DatabaseSync): void {
|
|
|
321
333
|
background INTEGER NOT NULL CHECK (background BETWEEN 0 AND 15),
|
|
322
334
|
stage TEXT CHECK (stage IS NULL OR (stage <> 'c64' AND (${SPACE_CHECK.replaceAll("space", "stage")}))),
|
|
323
335
|
call_address INTEGER CHECK (call_address BETWEEN 0 AND 65535),
|
|
336
|
+
colour_ram INTEGER CHECK (colour_ram = 1),
|
|
324
337
|
comment TEXT CHECK (comment IS NULL OR length(comment) > 0),
|
|
325
338
|
origin TEXT NOT NULL CHECK (origin IN ('user', 'llm')),
|
|
326
339
|
valid_from_revision TEXT NOT NULL REFERENCES revisions (id),
|
|
327
340
|
valid_to_revision TEXT REFERENCES revisions (id),
|
|
328
341
|
CHECK (call_address IS NULL OR stage IS NOT NULL),
|
|
342
|
+
CHECK (colour_ram IS NULL OR (stage IS NOT NULL AND colours IS NULL AND colour IS NULL AND mode <> 'hires')),
|
|
329
343
|
CHECK ((mode = 'text') = (charset IS NOT NULL)),
|
|
330
344
|
CHECK ((mode = 'text') = (bitmap IS NULL)),
|
|
331
345
|
CHECK (colours IS NULL OR colour IS NULL),
|
|
332
|
-
CHECK ((mode = 'hires') = (colours IS NULL AND colour IS NULL))
|
|
346
|
+
CHECK ((mode = 'hires') = (colours IS NULL AND colour IS NULL AND colour_ram IS NULL))
|
|
333
347
|
);
|
|
334
348
|
CREATE INDEX start_screens_space ON start_screens (space);
|
|
335
349
|
CREATE TABLE macro_uses (
|
|
@@ -354,6 +368,17 @@ function createSchema(db: DatabaseSync): void {
|
|
|
354
368
|
valid_to_revision TEXT REFERENCES revisions (id)
|
|
355
369
|
);
|
|
356
370
|
CREATE INDEX memory_current ON memory (space, address) WHERE valid_to_revision IS NULL;
|
|
371
|
+
CREATE TABLE release_memory (
|
|
372
|
+
id TEXT PRIMARY KEY,
|
|
373
|
+
space TEXT NOT NULL DEFAULT 'c64' CHECK (${SPACE_CHECK}),
|
|
374
|
+
label TEXT NOT NULL CHECK (length(label) > 0),
|
|
375
|
+
address INTEGER NOT NULL CHECK (address BETWEEN 0 AND 65535),
|
|
376
|
+
bytes TEXT NOT NULL CHECK (length(bytes) BETWEEN 2 AND ${MEMORY_ROW * 2} AND length(bytes) % 2 = 0 AND address + length(bytes) / 2 <= 65536),
|
|
377
|
+
origin TEXT NOT NULL CHECK (origin IN ('user', 'llm')),
|
|
378
|
+
valid_from_revision TEXT NOT NULL REFERENCES revisions (id),
|
|
379
|
+
valid_to_revision TEXT REFERENCES revisions (id)
|
|
380
|
+
);
|
|
381
|
+
CREATE INDEX release_memory_current ON release_memory (space, label, address) WHERE valid_to_revision IS NULL;
|
|
357
382
|
CREATE TABLE entries (
|
|
358
383
|
id TEXT PRIMARY KEY,
|
|
359
384
|
space TEXT NOT NULL DEFAULT 'c64' CHECK (${SPACE_CHECK}),
|
|
@@ -368,15 +393,16 @@ function createSchema(db: DatabaseSync): void {
|
|
|
368
393
|
CREATE TABLE coverage (
|
|
369
394
|
id TEXT PRIMARY KEY,
|
|
370
395
|
space TEXT NOT NULL DEFAULT 'c64' CHECK (${SPACE_CHECK}),
|
|
396
|
+
map TEXT NOT NULL CHECK (length(map) > 0),
|
|
371
397
|
kind TEXT NOT NULL CHECK (kind IN (${list(COVERAGE_KINDS)})),
|
|
372
|
-
|
|
373
|
-
end_address INTEGER NOT NULL CHECK (end_address BETWEEN start_address AND 65535),
|
|
398
|
+
ranges TEXT NOT NULL CHECK (length(ranges) >= 5),
|
|
374
399
|
origin TEXT NOT NULL CHECK (origin IN ('user', 'llm')),
|
|
375
400
|
valid_from_revision TEXT NOT NULL REFERENCES revisions (id),
|
|
376
401
|
valid_to_revision TEXT REFERENCES revisions (id)
|
|
377
402
|
);
|
|
378
|
-
CREATE INDEX coverage_current ON coverage (space,
|
|
403
|
+
CREATE INDEX coverage_current ON coverage (space, map) WHERE valid_to_revision IS NULL;
|
|
379
404
|
CREATE VIEW current_memory AS SELECT * FROM memory WHERE valid_to_revision IS NULL;
|
|
405
|
+
CREATE VIEW current_release_memory AS SELECT * FROM release_memory WHERE valid_to_revision IS NULL;
|
|
380
406
|
CREATE VIEW current_entries AS SELECT * FROM entries WHERE valid_to_revision IS NULL;
|
|
381
407
|
CREATE VIEW current_coverage AS SELECT * FROM coverage WHERE valid_to_revision IS NULL;
|
|
382
408
|
CREATE VIEW current_symbols AS SELECT * FROM symbols WHERE valid_to_revision IS NULL;
|
|
@@ -397,7 +423,7 @@ function createSchema(db: DatabaseSync): void {
|
|
|
397
423
|
}
|
|
398
424
|
|
|
399
425
|
/** The version of the index this build makes. An index with another version is emptied and made again. */
|
|
400
|
-
export const INDEX_VERSION =
|
|
426
|
+
export const INDEX_VERSION = 15;
|
|
401
427
|
|
|
402
428
|
function indexVersion(db: DatabaseSync): number | undefined {
|
|
403
429
|
const hasMeta = db.prepare("SELECT 1 FROM sqlite_master WHERE type = 'table' AND name = 'meta'").get();
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
// The start screen of a captured stage, from the entry state of its capture:
|
|
2
|
+
// the VIC-II registers and the VIC bank give the mode and the addresses, and
|
|
3
|
+
// the colour RAM of the capture gives the colours. Thus the start screen of a
|
|
4
|
+
// stage needs no address from the user.
|
|
5
|
+
|
|
6
|
+
import { Refusal } from "../errors.ts";
|
|
7
|
+
import type { StartScreenMode } from "./schema.ts";
|
|
8
|
+
|
|
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
|
+
export const START_SCREEN_WAIT = "loader_start_screen_wait";
|
|
11
|
+
export const START_SCREEN_CONTINUE = "loader_start_screen_continue";
|
|
12
|
+
/** The label of the loader's jump to the entry, in ACME's symbol list. */
|
|
13
|
+
export const LOADER_JUMP = "c64_source_loader_jump";
|
|
14
|
+
|
|
15
|
+
/** The fields of a start screen that the capture of a stage gives. */
|
|
16
|
+
export interface StageScreen {
|
|
17
|
+
mode: StartScreenMode;
|
|
18
|
+
screen: number;
|
|
19
|
+
bitmap?: number;
|
|
20
|
+
charset?: number;
|
|
21
|
+
border: number;
|
|
22
|
+
background: number;
|
|
23
|
+
/** The colours are the colour RAM of the capture: not for hires. */
|
|
24
|
+
colourRam?: true;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/** The bytes of an I/O area of an entry state: io.<name>.bytes as hex, when the capture has them. */
|
|
28
|
+
function ioBytes(state: Record<string, unknown> | undefined, name: string): Buffer | undefined {
|
|
29
|
+
const io = state?.io as Record<string, { bytes?: unknown }> | undefined;
|
|
30
|
+
const bytes = io?.[name]?.bytes;
|
|
31
|
+
return typeof bytes === "string" && /^([0-9a-f]{2})+$/i.test(bytes) ? Buffer.from(bytes, "hex") : undefined;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/** The colour RAM of the capture of a stage: 1000 colours from $d800, or undefined when the capture has none. */
|
|
35
|
+
export function capturedColourRam(state: Record<string, unknown> | undefined): Uint8Array | undefined {
|
|
36
|
+
const bytes = ioBytes(state, "colour");
|
|
37
|
+
return bytes === undefined || bytes.length !== 1000 ? undefined : Uint8Array.from(bytes, (value) => value & 0x0f);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* The start screen that the capture of a stage shows: its mode, its screen,
|
|
42
|
+
* its bitmap or charset, its colours and the colour RAM. Refuses by name when
|
|
43
|
+
* the capture does not show a start screen that the loader can show.
|
|
44
|
+
*/
|
|
45
|
+
export function startScreenOfStage(state: Record<string, unknown> | undefined, stage: string): StageScreen {
|
|
46
|
+
const again = `Capture the stage again while it shows the screen, and put the capture into the project knowledge with image --space ${stage}.`;
|
|
47
|
+
const vic = ioBytes(state, "vic");
|
|
48
|
+
const cia2 = ioBytes(state, "cia2");
|
|
49
|
+
if (state === undefined || vic === undefined || vic.length < 0x2f || cia2 === undefined) throw new Refusal("not-found", `The program in ${stage} has no entry state with the VIC-II and CIA 2. ${again}`);
|
|
50
|
+
const [d011, d016, d018] = [vic[0x11]!, vic[0x16]!, vic[0x18]!];
|
|
51
|
+
if ((d011 & 0x10) === 0) throw new Refusal("invalid-input", `The capture in ${stage} has the display off ($d011 = $${d011.toString(16).padStart(2, "0")}). ${again}`);
|
|
52
|
+
if ((d011 & 0x40) !== 0) throw new Refusal("invalid-input", `The capture in ${stage} shows a screen in the extended colour mode, which a start screen does not have.`);
|
|
53
|
+
const bitmap = (d011 & 0x20) !== 0;
|
|
54
|
+
const multicolour = (d016 & 0x10) !== 0;
|
|
55
|
+
if (!bitmap && multicolour) throw new Refusal("invalid-input", `The capture in ${stage} shows a multicolour text screen, which a start screen does not have.`);
|
|
56
|
+
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
|
+
return {
|
|
62
|
+
mode,
|
|
63
|
+
screen,
|
|
64
|
+
...(bitmap ? { bitmap: bank + ((d018 >> 3) & 1) * 0x2000 } : { charset: bank + ((d018 >> 1) & 7) * 0x800 }),
|
|
65
|
+
border: vic[0x20]! & 0x0f,
|
|
66
|
+
background: vic[0x21]! & 0x0f,
|
|
67
|
+
...(colourRam === undefined ? {} : { colourRam: true as const }),
|
|
68
|
+
};
|
|
69
|
+
}
|
|
70
|
+
|