@henols/c64-re-tools 2.8.0-rc.2 → 2.8.0-rc.3
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 +5 -2
- package/skills/c64-assembler/scripts/assembler.ts +55 -48
- package/skills/c64-disk/scripts/disk.ts +3 -3
- package/skills/c64-disk/scripts/lib/containers.ts +2 -2
- package/skills/c64-docs/SKILL.md +1 -1
- package/skills/c64-docs/scripts/docs.ts +12 -20
- package/skills/c64-docs/scripts/lib/links.ts +1 -1
- package/skills/c64-docs/scripts/lib/markdown.ts +1 -1
- package/skills/c64-docs/scripts/lib/reference.ts +59 -13
- package/skills/c64-knowledge/SKILL.md +17 -8
- package/skills/c64-knowledge/scripts/knowledge.ts +200 -218
- package/skills/c64-knowledge/scripts/lib/check.ts +2 -3
- package/skills/c64-knowledge/scripts/lib/listing.ts +56 -24
- package/skills/c64-packer/scripts/packer.ts +3 -3
- package/skills/c64-provenance/SKILL.md +25 -16
- package/skills/c64-provenance/scripts/lib/compare.ts +31 -21
- package/skills/c64-provenance/scripts/lib/leftovers.ts +17 -35
- package/skills/c64-provenance/scripts/lib/library.ts +10 -10
- package/skills/c64-provenance/scripts/lib/runtime.ts +3 -14
- package/skills/c64-provenance/scripts/provenance.ts +129 -98
- package/skills/c64-source/references/loader.md +2 -1
- package/skills/c64-source/scripts/lib/export.ts +54 -303
- package/skills/c64-source/scripts/lib/loader.ts +31 -51
- package/skills/c64-source/scripts/lib/source.ts +51 -41
- package/skills/c64-source/scripts/lib/stage.ts +9 -25
- package/skills/c64-source/scripts/source.ts +134 -294
- package/skills/c64-static-analysis/SKILL.md +10 -5
- package/skills/c64-static-analysis/scripts/lib/coverage.ts +1 -3
- package/skills/c64-static-analysis/scripts/lib/findings.ts +16 -3
- package/skills/c64-static-analysis/scripts/lib/idioms.ts +82 -10
- package/skills/c64-static-analysis/scripts/lib/triage.ts +112 -0
- package/skills/c64-static-analysis/scripts/static-analysis.ts +239 -147
- package/skills/c64-testing/SKILL.md +13 -7
- package/skills/c64-testing/scripts/lib/diff-files.ts +6 -13
- package/skills/c64-testing/scripts/lib/recording.ts +47 -66
- package/skills/c64-testing/scripts/lib/replay.ts +53 -9
- package/skills/c64-testing/scripts/lib/runs.ts +11 -29
- package/skills/c64-testing/scripts/lib/scenario.ts +30 -31
- package/skills/c64-testing/scripts/lib/start-screen.ts +42 -5
- package/skills/c64-testing/scripts/testing.ts +32 -7
- package/skills/c64-unpacker/SKILL.md +9 -7
- package/skills/c64-unpacker/scripts/lib/evidence.ts +23 -38
- package/skills/c64-unpacker/scripts/lib/loads.ts +24 -39
- package/skills/c64-unpacker/scripts/lib/machine.ts +38 -18
- package/skills/c64-unpacker/scripts/lib/stages.ts +124 -40
- package/skills/c64-unpacker/scripts/unpacker.ts +81 -68
- package/skills/emulator/SKILL.md +1 -0
- package/src/c64.ts +117 -10
- package/src/cli/ap-sdk.ts +1 -1
- package/src/cli/cleanup.ts +4 -2
- package/src/compare.ts +36 -16
- package/src/errors.ts +10 -1
- package/src/host/main.ts +2 -1
- package/src/host/server.ts +7 -11
- package/src/host/tools/c1541.ts +9 -3
- package/src/host/vice/adapter.ts +20 -43
- package/src/host/vice/binary-monitor.ts +22 -21
- package/src/host/vice/process.ts +31 -40
- package/src/host/vice/session.ts +187 -247
- package/src/host/vice/text-monitor.ts +3 -3
- package/src/host-client/autostart.ts +1 -1
- package/src/host-client/connect.ts +2 -1
- package/src/host-client/coverage.ts +0 -3
- package/src/host-client/entry-state.ts +4 -11
- package/src/host-client/image.ts +6 -11
- package/src/host-client/steps.ts +13 -113
- package/src/host-client/tools.ts +4 -4
- package/src/host-client/transfer.ts +10 -0
- package/src/host-client/vice-session.ts +10 -6
- package/src/knowledge/code-reach.ts +364 -0
- package/src/knowledge/conflicts.ts +34 -72
- package/src/knowledge/database.ts +40 -28
- package/src/knowledge/evidence.ts +2 -2
- package/src/knowledge/graph.ts +3 -3
- package/src/knowledge/history.ts +50 -90
- package/src/knowledge/import.ts +28 -68
- package/src/knowledge/join.ts +1 -1
- package/src/knowledge/names.ts +12 -7
- package/src/knowledge/program.ts +25 -46
- package/src/knowledge/read.ts +104 -100
- package/src/knowledge/schema.ts +9 -3
- package/src/knowledge/separate.ts +47 -19
- package/src/knowledge/start-screen.ts +7 -5
- package/src/knowledge/text.ts +56 -33
- package/src/knowledge/write.ts +274 -515
- package/src/loader.ts +7 -25
- package/src/mcp/inputs.ts +3 -3
- package/src/mcp/server.ts +19 -38
- package/src/mcp/session-source.ts +2 -1
- package/src/mcp/tools/debug.ts +97 -24
- package/src/mcp/tools/execution.ts +1 -1
- package/src/mcp/tools/input.ts +3 -3
- package/src/mcp/tools/machine.ts +3 -3
- package/src/mcp/tools/media.ts +4 -4
- package/src/mcp/tools/memory.ts +13 -17
- package/src/mcp/tools/video.ts +11 -25
- package/src/native/acme.ts +3 -3
- package/src/native/c64run.ts +2 -1
- package/src/native/decompiler/analyze.ts +10 -7
- package/src/native/decompiler/walk.ts +3 -4
- package/src/native/decompiler/wasm.ts +2 -2
- package/src/native/discover.ts +1 -1
- package/src/native/dxa.ts +9 -11
- package/src/native/exomizer.ts +1 -1
- package/src/native/processes.ts +4 -1
- package/src/native/run.ts +3 -3
- package/src/native/types.ts +5 -6
- package/src/opcodes.ts +3 -0
- package/src/platform.ts +9 -10
- package/src/program-code.ts +151 -0
- package/src/project.ts +8 -5
- package/src/protocol/framing.ts +51 -18
- package/src/protocol/messages.ts +1 -11
- package/src/protocol/tools.ts +0 -3
- package/src/protocol/vice.ts +588 -773
- package/src/script.ts +9 -14
- package/src/time.ts +1 -1
- package/src/trace.ts +3 -2
package/src/knowledge/names.ts
CHANGED
|
@@ -7,7 +7,8 @@ import type { DatabaseSync } from "node:sqlite";
|
|
|
7
7
|
|
|
8
8
|
import { c64AddressOf, formatC64Address } from "../c64.ts";
|
|
9
9
|
import { Refusal } from "../errors.ts";
|
|
10
|
-
import {
|
|
10
|
+
import { projectRoot } from "../project.ts";
|
|
11
|
+
import { withRead } from "./database.ts";
|
|
11
12
|
import { analyzerNamed, listAnalyzerNames, listSymbols, symbolNamed } from "./read.ts";
|
|
12
13
|
import { DEFAULT_SPACE, type Space, type SymbolKind } from "./schema.ts";
|
|
13
14
|
|
|
@@ -34,14 +35,18 @@ export function nameTable(db: DatabaseSync | undefined, space: Space = DEFAULT_S
|
|
|
34
35
|
return names;
|
|
35
36
|
}
|
|
36
37
|
|
|
37
|
-
/**
|
|
38
|
+
/** The name table of each project folder and space that knowledgeNames read in this process. A script resolves its names before it writes. */
|
|
39
|
+
const tables = new Map<string, Map<string, number>>();
|
|
40
|
+
|
|
41
|
+
/** addressOfName on the knowledge of the project: the name resolver of the scripts. It reads the knowledge once for each space, and again only to refuse a name. */
|
|
38
42
|
export function knowledgeNames(name: string, space: string): number {
|
|
39
|
-
const
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
43
|
+
const key = `${projectRoot()}\n${space}`;
|
|
44
|
+
let table = tables.get(key);
|
|
45
|
+
if (table === undefined) {
|
|
46
|
+
table = withRead((db) => nameTable(db, space));
|
|
47
|
+
tables.set(key, table);
|
|
44
48
|
}
|
|
49
|
+
return table.get(name) ?? withRead((db) => addressOfName(db, name, space));
|
|
45
50
|
}
|
|
46
51
|
|
|
47
52
|
/** An address of a step or a file: $ and 1 to 4 hex digits, else a name of the knowledge in the space. */
|
package/src/knowledge/program.ts
CHANGED
|
@@ -3,15 +3,14 @@
|
|
|
3
3
|
// capture and the coverage maps are imported one time; every later step reads
|
|
4
4
|
// them here, so a project needs no image or coverage files to be made again.
|
|
5
5
|
|
|
6
|
-
import { createHash } from "node:crypto";
|
|
7
6
|
import type { DatabaseSync } from "node:sqlite";
|
|
8
7
|
|
|
9
8
|
import { Refusal } from "../errors.ts";
|
|
10
|
-
import { type AddressRange, COVERAGE, formatC64Address, formatC64Byte } from "../c64.ts";
|
|
11
|
-
|
|
9
|
+
import { type AddressRange, COVERAGE, formatC64Address, formatC64Byte, formatC64Range, inRanges, prgOf, rangesWhere } from "../c64.ts";
|
|
12
10
|
import { COVERAGE_KINDS, DEFAULT_SPACE, MEMORY_ROW, type CoverageKind, type Space } from "./schema.ts";
|
|
13
11
|
import { coverageRuns } from "./read.ts";
|
|
14
|
-
import {
|
|
12
|
+
import { sha256 } from "./text.ts";
|
|
13
|
+
import { checkAddress, checkSpace, write, type Evidence, type OperandImage, type WriteContext } from "./write.ts";
|
|
15
14
|
|
|
16
15
|
/** The 64 KiB of one space: offset n is address n. */
|
|
17
16
|
const SIZE = 0x10000;
|
|
@@ -60,18 +59,6 @@ export interface CoverageMap {
|
|
|
60
59
|
bytes: Uint8Array;
|
|
61
60
|
}
|
|
62
61
|
|
|
63
|
-
/** The address ranges where `has` is true, in address order. */
|
|
64
|
-
function rangesWhere(has: (address: number) => boolean): AddressRange[] {
|
|
65
|
-
const ranges: AddressRange[] = [];
|
|
66
|
-
for (let address = 0; address < SIZE; address++) {
|
|
67
|
-
if (!has(address)) continue;
|
|
68
|
-
const start = address;
|
|
69
|
-
while (address + 1 < SIZE && has(address + 1)) address++;
|
|
70
|
-
ranges.push({ start, end: address });
|
|
71
|
-
}
|
|
72
|
-
return ranges;
|
|
73
|
-
}
|
|
74
|
-
|
|
75
62
|
/** The memory rows of the segments: at most MEMORY_ROW bytes, never across a multiple of MEMORY_ROW. */
|
|
76
63
|
export function memoryRows(segments: readonly AddressRange[], byteAt: (address: number) => number): Map<number, string> {
|
|
77
64
|
const rows = new Map<number, string>();
|
|
@@ -90,7 +77,7 @@ function checkSegments(segments: readonly AddressRange[]): void {
|
|
|
90
77
|
const sorted = [...segments].sort((a, b) => a.start - b.start);
|
|
91
78
|
sorted.forEach((segment, index) => {
|
|
92
79
|
if (!Number.isInteger(segment.start) || !Number.isInteger(segment.end) || segment.start < 0 || segment.end > 0xffff || segment.end < segment.start) {
|
|
93
|
-
throw new Refusal("invalid-input", `The segment ${
|
|
80
|
+
throw new Refusal("invalid-input", `The segment ${formatC64Range(segment)} is not a range from $0000 to $ffff.`);
|
|
94
81
|
}
|
|
95
82
|
if (index > 0 && segment.start <= sorted[index - 1]!.end) throw new Refusal("invalid-input", `The segments at ${formatC64Address(sorted[index - 1]!.start)} and ${formatC64Address(segment.start)} overlap.`);
|
|
96
83
|
});
|
|
@@ -104,7 +91,7 @@ function checkSegments(segments: readonly AddressRange[]): void {
|
|
|
104
91
|
export function setImage(db: DatabaseSync, context: WriteContext, request: ImageRequest): { revision: string | null; current: { space: Space; bytes: number; rowsAdded: number; rowsClosed: number; entry: boolean } } {
|
|
105
92
|
checkSpace(request);
|
|
106
93
|
checkSegments(request.segments);
|
|
107
|
-
if (request.entry !== undefined
|
|
94
|
+
if (request.entry !== undefined) checkAddress(request.entry, "The entry");
|
|
108
95
|
if (request.entry === undefined && (request.state !== undefined || request.stages !== undefined)) throw new Refusal("invalid-input", "A machine state or stages need an entry.");
|
|
109
96
|
const space = request.space ?? DEFAULT_SPACE;
|
|
110
97
|
const wanted = memoryRows(request.segments, request.byteAt);
|
|
@@ -141,8 +128,7 @@ export function setImage(db: DatabaseSync, context: WriteContext, request: Image
|
|
|
141
128
|
|
|
142
129
|
/** Address ranges as the knowledge text keeps them: "$1000-$1002 $1005". */
|
|
143
130
|
function rangesText(ranges: readonly AddressRange[]): string {
|
|
144
|
-
|
|
145
|
-
return ranges.map((range) => (range.start === range.end ? at(range.start) : `${at(range.start)}-${at(range.end)}`)).join(" ");
|
|
131
|
+
return ranges.map((range) => (range.start === range.end ? formatC64Address(range.start) : formatC64Range(range))).join(" ");
|
|
146
132
|
}
|
|
147
133
|
|
|
148
134
|
/** Calls `each` with the start and the end of each range of ranges as text. */
|
|
@@ -215,7 +201,7 @@ export function addCoverage(
|
|
|
215
201
|
}
|
|
216
202
|
// A path given twice is one map: the last one.
|
|
217
203
|
const byName = new Map(request.maps.map((map) => [map.name, map]));
|
|
218
|
-
const hashed = [...byName.values()].map((map) => ({ ...map, sha256:
|
|
204
|
+
const hashed = [...byName.values()].map((map) => ({ ...map, sha256: sha256(map.bytes) }));
|
|
219
205
|
// The runs of a name are oldest first: the last one is the latest map of the path.
|
|
220
206
|
const latest = new Map(coverageRuns(db).filter((run) => run.space === space).map((run) => [run.name, run.sha256]));
|
|
221
207
|
const withRows = new Set((db.prepare("SELECT DISTINCT map FROM current_coverage WHERE space = ?").all(space) as Array<{ map: string }>).map((row) => row.map));
|
|
@@ -295,11 +281,8 @@ export function coverageAccess(db: DatabaseSync | undefined, space: Space = DEFA
|
|
|
295
281
|
};
|
|
296
282
|
}
|
|
297
283
|
|
|
298
|
-
/** The
|
|
299
|
-
|
|
300
|
-
if (db === undefined) return undefined;
|
|
301
|
-
const rows = db.prepare("SELECT address, bytes FROM current_memory WHERE space = ? ORDER BY address").all(space) as Array<{ address: number; bytes: string }>;
|
|
302
|
-
if (rows.length === 0) return undefined;
|
|
284
|
+
/** The bytes of memory rows in address order, in 64 KiB, and the ranges that they fill: rows that touch are one range. */
|
|
285
|
+
function rowsImage(rows: ReadonlyArray<{ address: number; bytes: string }>): { segments: AddressRange[]; memory: Uint8Array } {
|
|
303
286
|
const memory = new Uint8Array(SIZE);
|
|
304
287
|
const segments: AddressRange[] = [];
|
|
305
288
|
for (const row of rows) {
|
|
@@ -310,6 +293,15 @@ export function programImage(db: DatabaseSync | undefined, space: Space = DEFAUL
|
|
|
310
293
|
if (last !== undefined && last.end + 1 === row.address) last.end = end;
|
|
311
294
|
else segments.push({ start: row.address, end });
|
|
312
295
|
}
|
|
296
|
+
return { segments, memory };
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
/** The program of a space, or undefined when the knowledge has no bytes for it. */
|
|
300
|
+
export function programImage(db: DatabaseSync | undefined, space: Space = DEFAULT_SPACE): ProgramImage | undefined {
|
|
301
|
+
if (db === undefined) return undefined;
|
|
302
|
+
const rows = db.prepare("SELECT address, bytes FROM current_memory WHERE space = ? ORDER BY address").all(space) as Array<{ address: number; bytes: string }>;
|
|
303
|
+
if (rows.length === 0) return undefined;
|
|
304
|
+
const { segments, memory } = rowsImage(rows);
|
|
313
305
|
const image: ProgramImage = { space, segments, memory, byteAt: (address) => memory[address]! };
|
|
314
306
|
const entry = db.prepare("SELECT address, state, stages FROM current_entries WHERE space = ?").get(space) as { address: number; state: string | null; stages: string | null } | undefined;
|
|
315
307
|
if (entry !== undefined) {
|
|
@@ -337,17 +329,16 @@ const STATE_PAGES = ["$0000", "$0100", "$0200", "$0300"] as const;
|
|
|
337
329
|
* pages hold code and data that the loader left). $0000 and $0001 are the
|
|
338
330
|
* CPU port, not memory. A segment byte wins over a state byte.
|
|
339
331
|
*/
|
|
340
|
-
|
|
332
|
+
function entryMemory(image: ProgramImage): Uint8Array {
|
|
341
333
|
const memory = new Uint8Array(image.memory);
|
|
342
334
|
const pages = image.state?.pages;
|
|
343
335
|
if (typeof pages !== "object" || pages === null) return memory;
|
|
344
|
-
const inSegment = (address: number) => image.segments.some((segment) => address >= segment.start && address <= segment.end);
|
|
345
336
|
STATE_PAGES.forEach((page, index) => {
|
|
346
337
|
const hex = (pages as Record<string, unknown>)[page];
|
|
347
338
|
if (typeof hex !== "string" || !/^(?:[0-9a-fA-F]{2}){1,256}$/.test(hex)) return;
|
|
348
339
|
Buffer.from(hex, "hex").forEach((byte, offset) => {
|
|
349
340
|
const address = index * 0x100 + offset;
|
|
350
|
-
if (address >= 2 && !
|
|
341
|
+
if (address >= 2 && !inRanges(image.segments, address)) memory[address] = byte;
|
|
351
342
|
});
|
|
352
343
|
});
|
|
353
344
|
return memory;
|
|
@@ -361,7 +352,7 @@ export function entryMemory(image: ProgramImage): Uint8Array {
|
|
|
361
352
|
export function analyzedBytes(image: ProgramImage): { bytes: Buffer; kind: "prg" | "flat64k" } {
|
|
362
353
|
if (image.segments.length === 1 && image.state === undefined) {
|
|
363
354
|
const [segment] = image.segments;
|
|
364
|
-
return { bytes:
|
|
355
|
+
return { bytes: prgOf(segment!.start, image.memory.subarray(segment!.start, segment!.end + 1)), kind: "prg" };
|
|
365
356
|
}
|
|
366
357
|
return { bytes: Buffer.from(entryMemory(image)), kind: "flat64k" };
|
|
367
358
|
}
|
|
@@ -393,8 +384,7 @@ export function operandImages(db: DatabaseSync, platform: ReadonlyMap<string, nu
|
|
|
393
384
|
const known = images.get(space);
|
|
394
385
|
if (known !== undefined) return known;
|
|
395
386
|
const image = requireProgram(db, space);
|
|
396
|
-
const
|
|
397
|
-
const made: OperandImage = { byteAt: (at) => (inside(at) ? image.byteAt(at) : undefined), platform };
|
|
387
|
+
const made: OperandImage = { byteAt: (at) => (inRanges(image.segments, at) ? image.byteAt(at) : undefined), platform };
|
|
398
388
|
images.set(space, made);
|
|
399
389
|
return made;
|
|
400
390
|
};
|
|
@@ -410,17 +400,7 @@ export function releaseImage(db: DatabaseSync | undefined, label: string, space:
|
|
|
410
400
|
const release = db.prepare("SELECT base FROM current_releases WHERE space = ? AND label = ?").get(space, label) as { base: number } | undefined;
|
|
411
401
|
if (release === undefined) return undefined;
|
|
412
402
|
if (release.base === 1) return programImage(db, space);
|
|
413
|
-
const
|
|
414
|
-
const memory = new Uint8Array(SIZE);
|
|
415
|
-
const segments: AddressRange[] = [];
|
|
416
|
-
for (const row of rows) {
|
|
417
|
-
const bytes = Buffer.from(row.bytes, "hex");
|
|
418
|
-
memory.set(bytes, row.address);
|
|
419
|
-
const end = row.address + bytes.length - 1;
|
|
420
|
-
const last = segments.at(-1);
|
|
421
|
-
if (last !== undefined && last.end + 1 === row.address) last.end = end;
|
|
422
|
-
else segments.push({ start: row.address, end });
|
|
423
|
-
}
|
|
403
|
+
const { segments, memory } = rowsImage(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 }>);
|
|
424
404
|
return { segments, byteAt: (address) => memory[address]! };
|
|
425
405
|
}
|
|
426
406
|
|
|
@@ -433,14 +413,13 @@ const PATCH_LINE_BYTES = 8;
|
|
|
433
413
|
* state. A release without a byte at each address of the range is refused.
|
|
434
414
|
*/
|
|
435
415
|
export function releaseCode(db: DatabaseSync | undefined, label: string, start: number, end: number, space: Space = DEFAULT_SPACE): string {
|
|
436
|
-
const range =
|
|
416
|
+
const range = formatC64Range({ start, end });
|
|
437
417
|
const release = db?.prepare("SELECT base FROM current_releases WHERE space = ? AND label = ?").get(space, label) as { base: number } | undefined;
|
|
438
418
|
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
419
|
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
420
|
const image = releaseImage(db, label, space)!;
|
|
441
|
-
const inside = (address: number) => image.segments.some((segment) => address >= segment.start && address <= segment.end);
|
|
442
421
|
for (let address = start; address <= end; address++) {
|
|
443
|
-
if (!
|
|
422
|
+
if (!inRanges(image.segments, 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
423
|
}
|
|
445
424
|
const lines: string[] = [];
|
|
446
425
|
for (let at = start; at <= end; at += PATCH_LINE_BYTES) {
|