@henols/c64-re-tools 2.8.0-rc.2 → 2.8.0-rc.4
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 +12 -7
- 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 +10 -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 +142 -41
- package/skills/c64-unpacker/scripts/unpacker.ts +85 -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 +43 -71
- 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
|
@@ -6,16 +6,16 @@
|
|
|
6
6
|
// snapshot file, which a run can start from. No tool guesses where a load
|
|
7
7
|
// ends: the steps decide.
|
|
8
8
|
|
|
9
|
-
import { type AddressRange, COVERAGE, formatC64Address } from "#src/c64.ts";
|
|
9
|
+
import { type AddressRange, COVERAGE, formatC64Address, formatC64RangeFields, prgOf } from "#src/c64.ts";
|
|
10
10
|
import { Refusal } from "#src/errors.ts";
|
|
11
11
|
import { autostartLoaded, LOAD_LIMIT_FRAMES, RUN_LIMIT_FRAMES, typeRun } from "#src/host-client/autostart.ts";
|
|
12
12
|
import { IO_RAM, markIoRamWrites, readCoverage } from "#src/host-client/coverage.ts";
|
|
13
|
-
import { ACTION_STEP_KINDS, checkActionStep, checkStepNames,
|
|
13
|
+
import { ACTION_STEP_KINDS, checkActionStep, checkStepNames, runActionStep, type ActionStep, type ActionStepKind } from "#src/host-client/steps.ts";
|
|
14
|
+
import { isObject } from "#src/host-client/tools.ts";
|
|
14
15
|
import { knowledgeAt } from "#src/knowledge/names.ts";
|
|
15
|
-
import {
|
|
16
|
-
import {
|
|
17
|
-
import {
|
|
18
|
-
import { entryStateFile, json, machineState, readRam, readyState, removeStaleFiles, vectorsOf } from "./machine.ts";
|
|
16
|
+
import { readProjectFile, readProjectJson, writeReported } from "#src/host-client/transfer.ts";
|
|
17
|
+
import { readMemoryInParts, ViceSessionClient } from "#src/host-client/vice-session.ts";
|
|
18
|
+
import { entryStateFile, machineState, readyState, vectorsOf, writeCapture } from "./machine.ts";
|
|
19
19
|
import { FIRST_SEGMENT_ADDRESS } from "./stages.ts";
|
|
20
20
|
|
|
21
21
|
|
|
@@ -32,13 +32,7 @@ const KERNAL_KEY_WAIT = { start: 0xe5cd, end: 0xe5d5 };
|
|
|
32
32
|
|
|
33
33
|
/** Reads and checks the steps file of loads: action steps with $ addresses, and capture and save steps with new names. */
|
|
34
34
|
export function readLoadSteps(path: string): LoadStep[] {
|
|
35
|
-
|
|
36
|
-
try {
|
|
37
|
-
value = JSON.parse(readProjectFile(path).bytes.toString("utf8"));
|
|
38
|
-
} catch (error) {
|
|
39
|
-
if (error instanceof WireFailure) throw error;
|
|
40
|
-
throw new Refusal("invalid-input", `--steps ${path} is not JSON: ${(error as Error).message}`);
|
|
41
|
-
}
|
|
35
|
+
const value = readProjectJson(path, (message) => `--steps ${path} is not JSON: ${message}`);
|
|
42
36
|
const fail = (message: string): never => {
|
|
43
37
|
throw new Refusal("invalid-input", `--steps ${path}: ${message}`);
|
|
44
38
|
};
|
|
@@ -109,8 +103,6 @@ export function captureBlocks(before: Uint8Array, after: Uint8Array, map: Uint8A
|
|
|
109
103
|
return changedBlocks(before, after, map, 16, writesKnown);
|
|
110
104
|
}
|
|
111
105
|
|
|
112
|
-
const hex4 = (value: number) => value.toString(16).padStart(4, "0");
|
|
113
|
-
|
|
114
106
|
/**
|
|
115
107
|
* Runs the program and its steps. Each capture step writes <out>/<name>/:
|
|
116
108
|
* image.bin, image.json, entry_state.json, a PRG for each segment, and with
|
|
@@ -132,7 +124,7 @@ export async function loads(program: string, options: { out: string; steps: Load
|
|
|
132
124
|
const { bytes } = readProjectFile(program);
|
|
133
125
|
if (!(await autostartLoaded(client, program, bytes))) return { output: { loaded: false, reason: `The program was not in memory after ${LOAD_LIMIT_FRAMES} frames.`, captures: [] }, failed: true };
|
|
134
126
|
// The RAM before RUN, without the program, so the first capture holds the program too.
|
|
135
|
-
previous = await
|
|
127
|
+
previous = await readMemoryInParts(client, 0x0000, 0x10000, "ram");
|
|
136
128
|
const load = bytes[0]! | (bytes[1]! << 8);
|
|
137
129
|
previous.fill(0, load, Math.min(0x10000, load + bytes.length - 2));
|
|
138
130
|
loadedProgram = bytes;
|
|
@@ -144,7 +136,7 @@ export async function loads(program: string, options: { out: string; steps: Load
|
|
|
144
136
|
}
|
|
145
137
|
} else {
|
|
146
138
|
// A disk, tape or cartridge: VICE loads and starts it. The RAM test of the KERNAL puts back what it writes.
|
|
147
|
-
previous = await
|
|
139
|
+
previous = await readMemoryInParts(client, 0x0000, 0x10000, "ram");
|
|
148
140
|
await client.memmap({ action: "clear" });
|
|
149
141
|
await client.autostart({ path: program, index: 0, run: true });
|
|
150
142
|
writesKnown = false;
|
|
@@ -181,7 +173,7 @@ export async function loads(program: string, options: { out: string; steps: Load
|
|
|
181
173
|
}
|
|
182
174
|
if ((await client.status()).state === "running") await client.execution({ action: "pause", space: "c64" });
|
|
183
175
|
const registers = await client.registersGet("c64");
|
|
184
|
-
const memory = await
|
|
176
|
+
const memory = await readMemoryInParts(client, 0x0000, 0x10000, "ram");
|
|
185
177
|
const map = await readCoverage(client);
|
|
186
178
|
if (loadedProgram !== undefined) {
|
|
187
179
|
// The PRG itself is part of the first capture.
|
|
@@ -195,37 +187,30 @@ export async function loads(program: string, options: { out: string; steps: Load
|
|
|
195
187
|
const state = await machineState(client);
|
|
196
188
|
const vectors = vectorsOf(state, memory);
|
|
197
189
|
const directory = `${options.out}/${step.capture}`;
|
|
198
|
-
const written: Written[] = [];
|
|
199
|
-
const write = (name: string, bytes: Uint8Array) => written.push(writeReported(`${directory}/${name}`, bytes, { what: "--out" }));
|
|
200
|
-
write("image.bin", memory);
|
|
201
|
-
const files = segments.map((segment) => {
|
|
202
|
-
const name = `segment-${hex4(segment.start)}-${hex4(segment.end)}.prg`;
|
|
203
|
-
write(name, Buffer.concat([Buffer.from([segment.start & 0xff, segment.start >> 8]), memory.subarray(segment.start, segment.end + 1)]));
|
|
204
|
-
return name;
|
|
205
|
-
});
|
|
206
190
|
let drive: Record<string, unknown> | undefined;
|
|
191
|
+
const other: Array<{ name: string; bytes: Uint8Array }> = [];
|
|
207
192
|
if (options.drive) {
|
|
208
193
|
const ram = Buffer.from((await client.memoryRead({ address: 0x0000, size: DRIVE_RAM, space: "drive8", view: "cpu" })).data, "hex");
|
|
209
|
-
|
|
194
|
+
other.push({ name: "drive8.prg", bytes: prgOf(0x0000, ram) });
|
|
210
195
|
drive = { file: "drive8.prg", pc: formatC64Address((await client.registersGet("drive8")).pc), space: "drive8" };
|
|
211
196
|
}
|
|
212
|
-
const
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
197
|
+
const entry = entryStateFile(registers, state, vectors, clean);
|
|
198
|
+
const { written, removed } = writeCapture(
|
|
199
|
+
directory,
|
|
200
|
+
memory,
|
|
201
|
+
segments,
|
|
202
|
+
other,
|
|
203
|
+
(files) => ({
|
|
216
204
|
capture: step.capture,
|
|
217
205
|
entry: formatC64Address(registers.pc),
|
|
218
|
-
segments: segments.map((segment, at) => ({ ...
|
|
219
|
-
...(options.exclude.length === 0 ? {} : { excluded: options.exclude.map(
|
|
206
|
+
segments: segments.map((segment, at) => ({ ...formatC64RangeFields(segment), file: files[at] })),
|
|
207
|
+
...(options.exclude.length === 0 ? {} : { excluded: options.exclude.map(formatC64RangeFields) }),
|
|
220
208
|
vectors,
|
|
221
209
|
image: "image.bin",
|
|
222
210
|
...(drive === undefined ? {} : { drive }),
|
|
223
211
|
}),
|
|
212
|
+
entry.bytes,
|
|
224
213
|
);
|
|
225
|
-
const entry = entryStateFile(registers, state, vectors, clean);
|
|
226
|
-
write("entry_state.json", entry.bytes);
|
|
227
|
-
// A capture again into the folder: the files of the earlier capture that image.json no longer lists go.
|
|
228
|
-
const removed = removeStaleFiles(directory, [...files, ...(drive === undefined ? [] : ["drive8.prg"])]);
|
|
229
214
|
const keyWait = registers.pc >= KERNAL_KEY_WAIT.start && registers.pc <= KERNAL_KEY_WAIT.end;
|
|
230
215
|
captures.push({
|
|
231
216
|
capture: step.capture,
|
|
@@ -234,8 +219,8 @@ export async function loads(program: string, options: { out: string; steps: Load
|
|
|
234
219
|
...(removed.length === 0 ? {} : { removed }),
|
|
235
220
|
pc: formatC64Address(registers.pc),
|
|
236
221
|
...(keyWait ? { keyWait: true } : {}),
|
|
237
|
-
segments: segments.map(
|
|
238
|
-
...(options.exclude.length === 0 ? {} : { excluded: options.exclude.map(
|
|
222
|
+
segments: segments.map(formatC64RangeFields),
|
|
223
|
+
...(options.exclude.length === 0 ? {} : { excluded: options.exclude.map(formatC64RangeFields) }),
|
|
239
224
|
differsFromReset: entry.differsFromReset,
|
|
240
225
|
...(small === 0 ? {} : { smallChanges: small }),
|
|
241
226
|
...(drive === undefined ? {} : { drive: drive.file }),
|
|
@@ -5,18 +5,16 @@
|
|
|
5
5
|
import { readdirSync, rmSync } from "node:fs";
|
|
6
6
|
import { join } from "node:path";
|
|
7
7
|
|
|
8
|
-
import { formatC64Address } from "#src/c64.ts";
|
|
8
|
+
import { type AddressRange, formatC64Address, hexWord, prgOf } from "#src/c64.ts";
|
|
9
9
|
import { resetToReady } from "#src/host-client/entry-state.ts";
|
|
10
|
-
import { writeRefusal } from "#src/host-client/transfer.ts";
|
|
11
|
-
import type
|
|
10
|
+
import { writeRefusal, writeReported, type Written } from "#src/host-client/transfer.ts";
|
|
11
|
+
import { readMemoryInParts, type ViceSessionClient } from "#src/host-client/vice-session.ts";
|
|
12
12
|
import { projectRoot, resolveProjectPath } from "#src/project.ts";
|
|
13
13
|
|
|
14
14
|
type Registers = Awaited<ReturnType<ViceSessionClient["registersGet"]>>;
|
|
15
15
|
|
|
16
|
-
const MAX_READ = 4096;
|
|
17
|
-
|
|
18
16
|
/** The I/O areas of the entry state, read without side effects. The colour RAM is the colours of the screen: a start screen of a captured stage uses them. */
|
|
19
|
-
|
|
17
|
+
const IO_AREAS = { vic: [0xd000, 0x2f], sid: [0xd400, 0x1d], cia1: [0xdc00, 0x10], cia2: [0xdd00, 0x10], colour: [0xd800, 1000] } as const;
|
|
20
18
|
/** The I/O areas of chips: what differsFrom compares. The colour RAM is the content of the screen, not the state of the machine. */
|
|
21
19
|
const CHIP_AREAS = ["vic", "sid", "cia1", "cia2"] as const;
|
|
22
20
|
|
|
@@ -36,19 +34,10 @@ export interface MachineState {
|
|
|
36
34
|
romVectors: Buffer;
|
|
37
35
|
}
|
|
38
36
|
|
|
39
|
-
/** Reads `size` bytes from `address` in the RAM view, in parts. */
|
|
40
|
-
export async function readRam(client: ViceSessionClient, address: number, size: number): Promise<Buffer> {
|
|
41
|
-
const parts: Buffer[] = [];
|
|
42
|
-
for (let at = address; at < address + size; at += MAX_READ) {
|
|
43
|
-
parts.push(Buffer.from((await client.memoryRead({ address: at, size: Math.min(MAX_READ, address + size - at), space: "c64", view: "ram" })).data, "hex"));
|
|
44
|
-
}
|
|
45
|
-
return Buffer.concat(parts);
|
|
46
|
-
}
|
|
47
|
-
|
|
48
37
|
/** The state the program finds at its entry: what a rebuild's loader must recreate. */
|
|
49
38
|
export async function machineState(client: ViceSessionClient): Promise<MachineState> {
|
|
50
39
|
const port = Buffer.from((await client.memoryRead({ address: 0x0000, size: 2, space: "c64", view: "cpu" })).data, "hex");
|
|
51
|
-
const pages = await
|
|
40
|
+
const pages = await readMemoryInParts(client, 0x0000, 0x400, "ram");
|
|
52
41
|
const io = {} as Record<keyof typeof IO_AREAS, Buffer>;
|
|
53
42
|
for (const [name, [address, size]] of Object.entries(IO_AREAS)) io[name as keyof typeof IO_AREAS] = Buffer.from((await client.ioRead(address, size)).data, "hex");
|
|
54
43
|
// The colour RAM has 4 bits: a read gives other bits above them.
|
|
@@ -78,7 +67,7 @@ export async function readyState(client: ViceSessionClient): Promise<MachineStat
|
|
|
78
67
|
}
|
|
79
68
|
|
|
80
69
|
/** The bytes that differ from a clean machine: the CPU port, pages 0-3 from $0002, and the I/O areas. */
|
|
81
|
-
|
|
70
|
+
function differsFrom(state: MachineState, clean: MachineState): Array<{ address: string; entry: number; reset: number }> {
|
|
82
71
|
const differs: Array<{ address: string; entry: number; reset: number }> = [];
|
|
83
72
|
const compare = (base: number, entry: Buffer, reset: Buffer) => entry.forEach((value, offset) => value !== reset[offset] && differs.push({ address: formatC64Address(base + offset), entry: value, reset: reset[offset]! }));
|
|
84
73
|
compare(0x0000, state.port, clean.port);
|
|
@@ -137,4 +126,35 @@ export function removeStaleFiles(folder: string, written: readonly string[]): st
|
|
|
137
126
|
return removed.sort();
|
|
138
127
|
}
|
|
139
128
|
|
|
140
|
-
|
|
129
|
+
const json = (value: unknown) => Buffer.from(`${JSON.stringify(value, null, 2)}\n`);
|
|
130
|
+
|
|
131
|
+
/**
|
|
132
|
+
* Writes a capture into `folder` (a project path): image.bin with all of
|
|
133
|
+
* `memory`, a PRG for each segment, the other files, image.json (`image` of
|
|
134
|
+
* the names of the segment PRGs) and entry_state.json. Then removes the files
|
|
135
|
+
* of an earlier capture there that this one did not write. Gives the written
|
|
136
|
+
* files, the names of the segment PRGs and the removed files.
|
|
137
|
+
*/
|
|
138
|
+
export function writeCapture(
|
|
139
|
+
folder: string,
|
|
140
|
+
memory: Buffer,
|
|
141
|
+
segments: readonly AddressRange[],
|
|
142
|
+
other: ReadonlyArray<{ name: string; bytes: Uint8Array }>,
|
|
143
|
+
image: (files: string[]) => Record<string, unknown>,
|
|
144
|
+
entryState: Buffer,
|
|
145
|
+
): { written: Written[]; files: string[]; removed: string[] } {
|
|
146
|
+
const written: Written[] = [];
|
|
147
|
+
const write = (name: string, bytes: Uint8Array) => written.push(writeReported(`${folder}/${name}`, bytes, { what: "--out" }));
|
|
148
|
+
write("image.bin", memory);
|
|
149
|
+
const files = segments.map((segment) => {
|
|
150
|
+
const name = `segment-${hexWord(segment.start)}-${hexWord(segment.end)}.prg`;
|
|
151
|
+
write(name, prgOf(segment.start, memory.subarray(segment.start, segment.end + 1)));
|
|
152
|
+
return name;
|
|
153
|
+
});
|
|
154
|
+
for (const file of other) write(file.name, file.bytes);
|
|
155
|
+
write("image.json", json(image(files)));
|
|
156
|
+
write("entry_state.json", entryState);
|
|
157
|
+
// A capture again into the folder: the files of the earlier capture that image.json no longer lists go.
|
|
158
|
+
const removed = removeStaleFiles(folder, [...files, ...other.map((file) => file.name)]);
|
|
159
|
+
return { written, files, removed };
|
|
160
|
+
}
|
|
@@ -8,8 +8,8 @@
|
|
|
8
8
|
// to the stage, is the stage's own code too: a packer hands off by JMP,
|
|
9
9
|
// RTS or RTI and never runs its own code again.
|
|
10
10
|
|
|
11
|
-
import { type AddressRange, COVERAGE } from "#src/c64.ts";
|
|
12
|
-
import { writtenBlocks } from "#src/host-client/coverage.ts";
|
|
11
|
+
import { type AddressRange, COVERAGE, inRanges, rangesOverlap, subtract } from "#src/c64.ts";
|
|
12
|
+
import { IO_RAM, writtenBlocks } from "#src/host-client/coverage.ts";
|
|
13
13
|
|
|
14
14
|
const contains = (range: AddressRange, address: number) => address >= range.start && address <= range.end;
|
|
15
15
|
|
|
@@ -60,11 +60,6 @@ export function stagePoints(blocks: readonly AddressRange[], own: ReadonlySet<nu
|
|
|
60
60
|
return { exec, watch };
|
|
61
61
|
}
|
|
62
62
|
|
|
63
|
-
/** True when the address lies in one of the ranges. */
|
|
64
|
-
export function inRanges(ranges: readonly AddressRange[], address: number): boolean {
|
|
65
|
-
return ranges.some((range) => contains(range, address));
|
|
66
|
-
}
|
|
67
|
-
|
|
68
63
|
/**
|
|
69
64
|
* Where a call returns to the stage: the return address on top of the
|
|
70
65
|
* stack, when a JSR that the stage executed pushed it. `stack` is page 1.
|
|
@@ -96,42 +91,95 @@ export function executedIn(map: Uint8Array, block: AddressRange): AddressRange[]
|
|
|
96
91
|
/** Pages 0-3 ($0000-$03ff) are in entry_state.json: a written block that is all in them is no segment. */
|
|
97
92
|
export const FIRST_SEGMENT_ADDRESS = 0x0400;
|
|
98
93
|
|
|
94
|
+
/**
|
|
95
|
+
* The RAM that a program used after its entry, from the coverage map of a
|
|
96
|
+
* run from the entry: the RAM that it executed, and the RAM that it read and
|
|
97
|
+
* did not write. The map keeps no order, so a read of RAM that the run also
|
|
98
|
+
* wrote can be a read of the program's own value, not of a captured byte. In
|
|
99
|
+
* the I/O area, the map gives each access as an I/O access: an execution
|
|
100
|
+
* there is RAM code, but a read can be a chip register.
|
|
101
|
+
*/
|
|
102
|
+
export function usedAfterEntry(map: Uint8Array): (address: number) => boolean {
|
|
103
|
+
return (address) => {
|
|
104
|
+
const flags = map[address]!;
|
|
105
|
+
if (address >= IO_RAM.start && address <= IO_RAM.end) return (flags & (COVERAGE.ramExecute | COVERAGE.ioExecute)) !== 0;
|
|
106
|
+
return (flags & COVERAGE.ramExecute) !== 0 || ((flags & COVERAGE.ramRead) !== 0 && (flags & COVERAGE.ramWrite) === 0);
|
|
107
|
+
};
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
type Stages = ReadonlyArray<{ entry: number; wrote: readonly AddressRange[] }>;
|
|
111
|
+
type WrittenBlock = AddressRange & { writtenBy: number };
|
|
112
|
+
type DroppedRange = WrittenBlock & { stageEntry: number };
|
|
113
|
+
|
|
114
|
+
/** 0 below the I/O area, 1 in it, 2 above it. */
|
|
115
|
+
const ioSide = (address: number) => (address < IO_RAM.start ? 0 : address <= IO_RAM.end ? 1 : 2);
|
|
116
|
+
|
|
117
|
+
/** A range cut at the bounds of the I/O area: the RAM there that a stage changed never joins the blocks on its two sides. */
|
|
118
|
+
function cutAtIoArea(range: AddressRange): AddressRange[] {
|
|
119
|
+
if (!rangesOverlap(range, IO_RAM)) return [range];
|
|
120
|
+
const inside = { start: Math.max(range.start, IO_RAM.start), end: Math.min(range.end, IO_RAM.end) };
|
|
121
|
+
return [...subtract(range, [IO_RAM]), inside].sort((a, b) => a.start - b.start);
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
/**
|
|
125
|
+
* The blocks that the stages wrote, each cut at the bounds of the I/O area,
|
|
126
|
+
* with the leftover rule. A block is a leftover when it holds the entry of an
|
|
127
|
+
* intermediate stage that started after the block's writer, and no write from
|
|
128
|
+
* that stage on replaced the code at that entry. Code that a stage ran before
|
|
129
|
+
* a later stage wrote over it never drops a block, and the block that holds
|
|
130
|
+
* the capture entry is never a leftover. A block that an intermediate stage
|
|
131
|
+
* wrote inside a leftover, for example a variable of the loader, is a leftover
|
|
132
|
+
* too. `stageEntry` of a leftover is the entry of the later stage that makes
|
|
133
|
+
* it one.
|
|
134
|
+
*/
|
|
135
|
+
function stageBlocks(stages: Stages, entry: number): Array<WrittenBlock & { stageEntry?: number }> {
|
|
136
|
+
const last = stages.length - 1;
|
|
137
|
+
const replaced = (from: number, address: number) => stages.slice(from).some((stage) => inRanges(stage.wrote, address));
|
|
138
|
+
const laterEntry = (block: WrittenBlock) =>
|
|
139
|
+
contains(block, entry) ? undefined : stages.find((stage, index) => index > block.writtenBy && index < last && contains(block, stage.entry) && !replaced(index, stage.entry))?.entry;
|
|
140
|
+
const written = stages.flatMap((stage, index) => stage.wrote.flatMap(cutAtIoArea).map((range) => ({ start: range.start, end: range.end, writtenBy: index })));
|
|
141
|
+
const leftovers = written.flatMap((block) => {
|
|
142
|
+
const at = laterEntry(block);
|
|
143
|
+
return at === undefined ? [] : [{ ...block, at }];
|
|
144
|
+
});
|
|
145
|
+
const inLeftover = (block: WrittenBlock) =>
|
|
146
|
+
block.writtenBy < last && !contains(block, entry) ? leftovers.find((left) => block.start >= left.start && block.end <= left.end)?.at : undefined;
|
|
147
|
+
return written.map((block) => {
|
|
148
|
+
const at = laterEntry(block) ?? inLeftover(block);
|
|
149
|
+
return at === undefined ? block : { ...block, stageEntry: at };
|
|
150
|
+
});
|
|
151
|
+
}
|
|
152
|
+
|
|
99
153
|
/**
|
|
100
154
|
* The segments of a capture: the blocks that the stages wrote, without loader
|
|
101
|
-
* leftovers and without blocks that are all in pages 0-3
|
|
102
|
-
* holds them), joined into ranges that do not overlap or
|
|
103
|
-
*
|
|
104
|
-
*
|
|
105
|
-
*
|
|
106
|
-
*
|
|
107
|
-
*
|
|
108
|
-
*
|
|
109
|
-
*
|
|
110
|
-
*
|
|
111
|
-
*
|
|
112
|
-
* the PRG, is then a segment too (`loaded: true`), joined with the blocks that
|
|
113
|
-
* it overlaps or touches.
|
|
155
|
+
* leftovers (stageBlocks) and without blocks that are all in pages 0-3
|
|
156
|
+
* (the entry state holds them), joined into ranges that do not overlap or
|
|
157
|
+
* touch. A join never goes past a bound of the I/O area, so the RAM there is
|
|
158
|
+
* a segment of its own. The filter comes before the join: a join first would drop the
|
|
159
|
+
* program with a leftover. `used` tells the addresses that the program used
|
|
160
|
+
* after its entry: a part of a leftover that no later stage wrote is a segment
|
|
161
|
+
* when the program used an address in it, as a stage can copy code of the
|
|
162
|
+
* program into a leftover. `writtenBy` is the last stage that wrote into the
|
|
163
|
+
* range. When no written block holds the entry, the code there is as the
|
|
164
|
+
* program loaded it: `loaded`, the range of the PRG, is then a segment too
|
|
165
|
+
* (`loaded: true`), joined with the blocks that it overlaps or touches.
|
|
114
166
|
*/
|
|
115
167
|
export function captureSegments(
|
|
116
|
-
stages:
|
|
168
|
+
stages: Stages,
|
|
117
169
|
entry: number,
|
|
118
|
-
loaded?: AddressRange,
|
|
170
|
+
options: { loaded?: AddressRange; used?: (address: number) => boolean } = {},
|
|
119
171
|
): Array<AddressRange & ({ writtenBy: number } | { loaded: true })> {
|
|
120
|
-
const
|
|
121
|
-
const
|
|
122
|
-
const
|
|
123
|
-
|
|
124
|
-
const
|
|
125
|
-
|
|
126
|
-
const inLeftover = (block: AddressRange & { writtenBy: number }) =>
|
|
127
|
-
block.writtenBy < last && !contains(block, entry) && leftovers.some((left) => block.start >= left.start && block.end <= left.end);
|
|
128
|
-
const blocks = written
|
|
129
|
-
.filter((block) => !leftover(block, block.writtenBy) && !inLeftover(block) && (block.end >= FIRST_SEGMENT_ADDRESS || contains(block, entry)))
|
|
172
|
+
const { loaded, used } = options;
|
|
173
|
+
const written = stageBlocks(stages, entry);
|
|
174
|
+
const dropped = (block: { stageEntry?: number }) => block.stageEntry !== undefined;
|
|
175
|
+
const usedLeftovers = used === undefined ? [] : usedParts(written.filter(dropped), lastWriters(stages), used);
|
|
176
|
+
const blocks = [...written.filter((block) => !dropped(block)), ...usedLeftovers]
|
|
177
|
+
.filter((block) => block.end >= FIRST_SEGMENT_ADDRESS || contains(block, entry))
|
|
130
178
|
.sort((a, b) => a.start - b.start);
|
|
131
179
|
const segments: Array<AddressRange & { writtenBy: number }> = [];
|
|
132
180
|
for (const block of blocks) {
|
|
133
181
|
const previous = segments[segments.length - 1];
|
|
134
|
-
if (previous !== undefined && block.start <= previous.end + 1) {
|
|
182
|
+
if (previous !== undefined && block.start <= previous.end + 1 && ioSide(block.start) === ioSide(previous.end)) {
|
|
135
183
|
previous.end = Math.max(previous.end, block.end);
|
|
136
184
|
previous.writtenBy = Math.max(previous.writtenBy, block.writtenBy);
|
|
137
185
|
} else segments.push({ ...block });
|
|
@@ -147,14 +195,67 @@ export function captureSegments(
|
|
|
147
195
|
return [...apart, joined].sort((a, b) => a.start - b.start);
|
|
148
196
|
}
|
|
149
197
|
|
|
198
|
+
/**
|
|
199
|
+
* What a capture drops: the parts from FIRST_SEGMENT_ADDRESS on of the
|
|
200
|
+
* leftover blocks (stageBlocks) that no range of `kept`, the segments and
|
|
201
|
+
* the excluded ranges, holds. `writtenBy` is the stage that wrote the block,
|
|
202
|
+
* and `stageEntry` the entry of the later stage that makes it a leftover. A
|
|
203
|
+
* byte in the blocks of more than one stage is given once, with the last
|
|
204
|
+
* stage that wrote it, as `writtenBy` of a segment.
|
|
205
|
+
*/
|
|
206
|
+
export function droppedRanges(stages: Stages, entry: number, kept: readonly AddressRange[]): DroppedRange[] {
|
|
207
|
+
const parts: DroppedRange[] = [];
|
|
208
|
+
const given: AddressRange[] = [];
|
|
209
|
+
for (const block of stageBlocks(stages, entry).sort((a, b) => b.writtenBy - a.writtenBy)) {
|
|
210
|
+
if (block.stageEntry === undefined || block.end < FIRST_SEGMENT_ADDRESS) continue;
|
|
211
|
+
const range = { start: Math.max(block.start, FIRST_SEGMENT_ADDRESS), end: block.end };
|
|
212
|
+
for (const piece of subtract(range, [...kept, ...given])) parts.push({ ...piece, writtenBy: block.writtenBy, stageEntry: block.stageEntry });
|
|
213
|
+
given.push(range);
|
|
214
|
+
}
|
|
215
|
+
return parts.sort((a, b) => a.start - b.start);
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
/**
|
|
219
|
+
* The segments in the RAM under the I/O area in which the program executed
|
|
220
|
+
* no code after its entry. The map gives a read there as an I/O read, so only
|
|
221
|
+
* code that ran shows that the program uses that RAM.
|
|
222
|
+
*/
|
|
223
|
+
export function notExecutedUnderIo<T extends AddressRange>(segments: readonly T[], used: (address: number) => boolean): T[] {
|
|
224
|
+
return segments.filter((segment) => {
|
|
225
|
+
if (ioSide(segment.start) !== 1 || ioSide(segment.end) !== 1) return false;
|
|
226
|
+
for (let address = segment.start; address <= segment.end; address++) if (used(address)) return false;
|
|
227
|
+
return true;
|
|
228
|
+
});
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
/** For each address, the last stage that wrote it, or -1. */
|
|
232
|
+
function lastWriters(stages: ReadonlyArray<{ wrote: readonly AddressRange[] }>): Int8Array {
|
|
233
|
+
const writers = new Int8Array(0x10000).fill(-1);
|
|
234
|
+
stages.forEach((stage, index) => {
|
|
235
|
+
for (const range of stage.wrote) writers.fill(index, range.start, range.end + 1);
|
|
236
|
+
});
|
|
237
|
+
return writers;
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
/** The parts of the blocks that hold the bytes of their writer, each when the program used an address in it. */
|
|
241
|
+
function usedParts(blocks: ReadonlyArray<AddressRange & { writtenBy: number }>, writers: Int8Array, used: (address: number) => boolean): Array<AddressRange & { writtenBy: number }> {
|
|
242
|
+
const parts: Array<AddressRange & { writtenBy: number; used: boolean }> = [];
|
|
243
|
+
for (const block of blocks) {
|
|
244
|
+
let part: (AddressRange & { writtenBy: number; used: boolean }) | undefined;
|
|
245
|
+
for (let address = block.start; address <= block.end; address++) {
|
|
246
|
+
if (writers[address] !== block.writtenBy) {
|
|
247
|
+
part = undefined;
|
|
248
|
+
continue;
|
|
249
|
+
}
|
|
250
|
+
if (part === undefined) parts.push((part = { start: address, end: address, writtenBy: block.writtenBy, used: false }));
|
|
251
|
+
part.end = address;
|
|
252
|
+
part.used ||= used(address);
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
return parts.filter((part) => part.used).map(({ start, end, writtenBy }) => ({ start, end, writtenBy }));
|
|
256
|
+
}
|
|
257
|
+
|
|
150
258
|
/** The segments without the bytes of `holes`: a segment that a hole cuts gives its parts, each with the fields of the segment. */
|
|
151
259
|
export function withoutRanges<T extends AddressRange>(segments: readonly T[], holes: readonly AddressRange[]): T[] {
|
|
152
|
-
|
|
153
|
-
for (const hole of holes) {
|
|
154
|
-
pieces = pieces.flatMap((piece) => {
|
|
155
|
-
if (hole.end < piece.start || hole.start > piece.end) return [piece];
|
|
156
|
-
return [...(hole.start > piece.start ? [{ ...piece, end: hole.start - 1 }] : []), ...(hole.end < piece.end ? [{ ...piece, start: hole.end + 1 }] : [])];
|
|
157
|
-
});
|
|
158
|
-
}
|
|
159
|
-
return pieces;
|
|
260
|
+
return segments.flatMap((segment) => subtract(segment, holes).map((piece) => ({ ...segment, ...piece })));
|
|
160
261
|
}
|