@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
|
@@ -2,10 +2,9 @@
|
|
|
2
2
|
// each item. knowledge.ts reads and writes the files; these functions check
|
|
3
3
|
// each verdict and count the result, so the files stay consistent.
|
|
4
4
|
|
|
5
|
-
import { createHash } from "node:crypto";
|
|
6
|
-
|
|
7
5
|
import { SYMBOL_NAME } from "#src/c64.ts";
|
|
8
6
|
import { Refusal } from "#src/errors.ts";
|
|
7
|
+
import { sha256 } from "#src/knowledge/text.ts";
|
|
9
8
|
|
|
10
9
|
export const CHECK_VERDICTS = ["correct", "wrong", "unsure"] as const;
|
|
11
10
|
export type CheckVerdict = (typeof CHECK_VERDICTS)[number];
|
|
@@ -52,7 +51,7 @@ export interface Verdicts {
|
|
|
52
51
|
* in address order. A changed text or another row does not move a row.
|
|
53
52
|
*/
|
|
54
53
|
export function pickSample<T extends { space: string; address: number; placement: string }>(rows: readonly T[], seed: number, count: number): T[] {
|
|
55
|
-
const key = (row: T) =>
|
|
54
|
+
const key = (row: T) => sha256(`${seed}:${row.space}:${row.address}:${row.placement}`);
|
|
56
55
|
return rows
|
|
57
56
|
.map((row) => ({ row, key: key(row) }))
|
|
58
57
|
.sort((a, b) => (a.key < b.key ? -1 : a.key > b.key ? 1 : 0))
|
|
@@ -3,14 +3,19 @@
|
|
|
3
3
|
// the bytes from coverage files, and the references in and out. Nothing here
|
|
4
4
|
// reads files: knowledge.ts gives the lookups.
|
|
5
5
|
|
|
6
|
-
import { COVERAGE, formatC64Address, formatC64Byte, POINTER_TABLE_TYPES, pointerEntryText, pointerTableEntries, type PointerTableType, splitEntryByte } from "#src/c64.ts";
|
|
6
|
+
import { type AddressRange, COVERAGE, formatC64Address, formatC64Byte, hexByte, inRanges, POINTER_TABLE_TYPES, pointerEntryText, pointerTableEntries, type PointerTableType, splitEntryByte } from "#src/c64.ts";
|
|
7
|
+
import { CODE_REGION_TYPES, INSTRUCTION_REGION_TYPES } from "#src/knowledge/schema.ts";
|
|
7
8
|
import { instructionWith, MODE_LENGTH, OPCODES, type Mode } from "#src/opcodes.ts";
|
|
8
9
|
|
|
9
10
|
export interface ListingSource {
|
|
10
11
|
/** The image byte at an address, or undefined outside the image. */
|
|
11
12
|
byteAt: (address: number) => number | undefined;
|
|
12
|
-
/** The
|
|
13
|
-
|
|
13
|
+
/** The symbol of an address in the knowledge, without platform names. */
|
|
14
|
+
symbolOf: (address: number) => string | undefined;
|
|
15
|
+
/** The platform name of an address, of a ROM that the program uses. None without it. */
|
|
16
|
+
platformOf?: (address: number) => string | undefined;
|
|
17
|
+
/** The I/O area: inside the image, its platform names name the registers, not the RAM of the program, so they are no labels. */
|
|
18
|
+
io?: AddressRange;
|
|
14
19
|
/** The start of the platform entry of more bytes that holds an address outside the image (NMINV for its high byte), or undefined. */
|
|
15
20
|
entryStart?: (address: number) => number | undefined;
|
|
16
21
|
/** The analyzer names of an address, for reference. */
|
|
@@ -44,7 +49,8 @@ export interface ListingLine {
|
|
|
44
49
|
|
|
45
50
|
type Region = ListingSource["regions"][number];
|
|
46
51
|
|
|
47
|
-
const CODE = new Set(
|
|
52
|
+
const CODE = new Set<string>(CODE_REGION_TYPES);
|
|
53
|
+
const INSTRUCTIONS = new Set<string>(INSTRUCTION_REGION_TYPES);
|
|
48
54
|
const TABLES = new Set<string>(["words", ...POINTER_TABLE_TYPES]);
|
|
49
55
|
const DATA_PER_LINE = 8;
|
|
50
56
|
|
|
@@ -57,17 +63,31 @@ function accessOf(coverage: Uint8Array | undefined, start: number, length: numbe
|
|
|
57
63
|
return text === "" ? "-" : text;
|
|
58
64
|
}
|
|
59
65
|
|
|
66
|
+
/** The name of an address: its symbol, else its platform name. */
|
|
67
|
+
function nameOf(address: number, source: ListingSource): string | undefined {
|
|
68
|
+
return source.symbolOf(address) ?? source.platformOf?.(address);
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/** The label of a line of the image, as c64-source writes it: a platform name in the I/O area names the register, not the RAM there. */
|
|
72
|
+
function labelOf(address: number, source: ListingSource): string | undefined {
|
|
73
|
+
return source.io !== undefined && inRanges([source.io], address) ? source.symbolOf(address) : nameOf(address, source);
|
|
74
|
+
}
|
|
75
|
+
|
|
60
76
|
/**
|
|
61
77
|
* The name of an operand target, as c64-source writes it: its own name, else
|
|
62
|
-
* name+offset inside a
|
|
78
|
+
* name+offset inside a data region with a symbol at its start, or inside a
|
|
79
|
+
* platform entry of more bytes. A platform name at the start of a region is
|
|
80
|
+
* no name of the region: colour RAM in a region from $D000 is not SP0X+2048.
|
|
63
81
|
*/
|
|
64
82
|
function targetName(target: number, source: ListingSource): string | undefined {
|
|
65
|
-
const own =
|
|
83
|
+
const own = nameOf(target, source);
|
|
66
84
|
if (own !== undefined) return own;
|
|
67
|
-
const region = source.regions.find((row) => target > row.start && target <= row.end && !
|
|
68
|
-
const
|
|
69
|
-
|
|
70
|
-
|
|
85
|
+
const region = source.regions.find((row) => target > row.start && target <= row.end && !INSTRUCTIONS.has(row.type));
|
|
86
|
+
const table = region === undefined ? undefined : source.symbolOf(region.start);
|
|
87
|
+
if (table !== undefined) return `${table}+${target - region!.start}`;
|
|
88
|
+
const entry = source.entryStart?.(target);
|
|
89
|
+
const name = entry === undefined ? undefined : nameOf(entry, source);
|
|
90
|
+
return name === undefined ? undefined : `${name}+${target - entry!}`;
|
|
71
91
|
}
|
|
72
92
|
|
|
73
93
|
/** The operand value of an instruction as an expression: the name of its target where one exists, else the number. Empty for no operand. */
|
|
@@ -118,8 +138,8 @@ export function listing(source: ListingSource, start: number, end: number, optio
|
|
|
118
138
|
const lines: ListingLine[] = [];
|
|
119
139
|
const data = options.code !== true;
|
|
120
140
|
const line = (address: number, length: number, text: string): ListingLine => {
|
|
121
|
-
const bytes = Array.from({ length }, (_, offset) => source.byteAt(address + offset)
|
|
122
|
-
const label =
|
|
141
|
+
const bytes = Array.from({ length }, (_, offset) => hexByte(source.byteAt(address + offset)!)).join(" ");
|
|
142
|
+
const label = labelOf(address, source);
|
|
123
143
|
const second = source.secondNames(address).filter((name) => name !== label);
|
|
124
144
|
const { line: lineComments, side: own } = source.comments(address);
|
|
125
145
|
// A comment inside the line (knowledge of an older version) shows with its address, as in the source of c64-source.
|
|
@@ -162,9 +182,11 @@ export function listing(source: ListingSource, start: number, end: number, optio
|
|
|
162
182
|
const length = MODE_LENGTH[opcode.mode];
|
|
163
183
|
const bytes = Array.from({ length: length - 1 }, (_, offset) => source.byteAt(address + 1 + offset));
|
|
164
184
|
if (address + length - 1 <= region.end && bytes.every((value) => value !== undefined)) {
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
185
|
+
if (address >= start) {
|
|
186
|
+
// The operand expression of the knowledge in the syntax of the addressing mode, as c64-source writes it.
|
|
187
|
+
const operand = source.operand(address) ?? operandOf(opcode.mode, address, bytes[0] ?? 0, bytes[1] ?? 0, source);
|
|
188
|
+
lines.push(line(address, length, instructionWith(byte, operand)));
|
|
189
|
+
}
|
|
168
190
|
address += length;
|
|
169
191
|
continue;
|
|
170
192
|
}
|
|
@@ -182,7 +204,7 @@ export function listing(source: ListingSource, start: number, end: number, optio
|
|
|
182
204
|
let at = address;
|
|
183
205
|
while (size !== undefined && at <= end && texts.length * size < DATA_PER_LINE) {
|
|
184
206
|
const value = table.get(at);
|
|
185
|
-
if (value?.length !== size || (at !== address &&
|
|
207
|
+
if (value?.length !== size || (at !== address && labelOf(at, source) !== undefined) || (size === 2 && labelOf(at + 1, source) !== undefined)) break;
|
|
186
208
|
texts.push(value.text);
|
|
187
209
|
at += size;
|
|
188
210
|
}
|
|
@@ -195,7 +217,7 @@ export function listing(source: ListingSource, start: number, end: number, optio
|
|
|
195
217
|
// Bytes up to the next label, table value, region edge or end of the line.
|
|
196
218
|
const values: number[] = [];
|
|
197
219
|
let at = address;
|
|
198
|
-
while (at <= end && values.length < DATA_PER_LINE && source.byteAt(at) !== undefined && (at === address || (
|
|
220
|
+
while (at <= end && values.length < DATA_PER_LINE && source.byteAt(at) !== undefined && (at === address || (labelOf(at, source) === undefined && regionAt(at) === region && table?.has(at) !== true))) {
|
|
199
221
|
values.push(source.byteAt(at)!);
|
|
200
222
|
at++;
|
|
201
223
|
}
|
|
@@ -205,19 +227,29 @@ export function listing(source: ListingSource, start: number, end: number, optio
|
|
|
205
227
|
return lines;
|
|
206
228
|
}
|
|
207
229
|
|
|
208
|
-
/**
|
|
209
|
-
|
|
230
|
+
/**
|
|
231
|
+
* A listing as text lines, as the agent reads it: each line comment as a line
|
|
232
|
+
* of its own, `; <comment>`, before its line, and each line as
|
|
233
|
+
* `<address> <bytes> <access> <label>: <text> ; <comment> | refs in <n> out <n> | also <names>`.
|
|
234
|
+
* Two spaces separate the parts. A part that the line does not have is not there.
|
|
235
|
+
*/
|
|
236
|
+
export function listingLines(lines: readonly ListingLine[]): string[] {
|
|
210
237
|
const out: string[] = [];
|
|
211
238
|
for (const item of lines) {
|
|
212
|
-
for (const comment of item.lineComments ?? []) out.push(
|
|
213
|
-
const head = `${formatC64Address(item.address)} ${item.bytes.padEnd(9)} ${(item.access ?? "").padEnd(3)}`;
|
|
214
|
-
const body = (item.label === undefined ? "" : `${item.label} `).padEnd(20) + item.text;
|
|
239
|
+
for (const comment of item.lineComments ?? []) out.push(`; ${comment}`);
|
|
215
240
|
const notes = [
|
|
216
241
|
...(item.comment === undefined ? [] : [item.comment]),
|
|
217
242
|
...(item.referencesIn + item.referencesOut === 0 ? [] : [`refs in ${item.referencesIn} out ${item.referencesOut}`]),
|
|
218
243
|
...(item.analyzerNames === undefined ? [] : [`also ${item.analyzerNames.join(", ")}`]),
|
|
219
244
|
];
|
|
220
|
-
|
|
245
|
+
const parts = [
|
|
246
|
+
formatC64Address(item.address),
|
|
247
|
+
item.bytes,
|
|
248
|
+
...(item.access === undefined ? [] : [item.access]),
|
|
249
|
+
`${item.label === undefined ? "" : `${item.label}: `}${item.text}`,
|
|
250
|
+
...(notes.length === 0 ? [] : [`; ${notes.join(" | ")}`]),
|
|
251
|
+
];
|
|
252
|
+
out.push(parts.join(" "));
|
|
221
253
|
}
|
|
222
|
-
return
|
|
254
|
+
return out;
|
|
223
255
|
}
|
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
|
|
6
6
|
import { posix } from "node:path";
|
|
7
7
|
|
|
8
|
-
import { findHandoffs, formatC64Address, parseTokenized, prgRange } from "#src/c64.ts";
|
|
8
|
+
import { findHandoffs, formatC64Address, formatC64Byte, formatC64RangeFields, parseTokenized, prgRange } from "#src/c64.ts";
|
|
9
9
|
import { compareBuild, mergedPrograms } from "#src/compare.ts";
|
|
10
10
|
import { isRefusal, Refusal } from "#src/errors.ts";
|
|
11
11
|
import { readProjectFile, writeReported } from "#src/host-client/transfer.ts";
|
|
@@ -84,9 +84,9 @@ await runScript({
|
|
|
84
84
|
unpackedBytes,
|
|
85
85
|
percent: Math.round((packed.program.length / unpackedBytes) * 1000) / 10,
|
|
86
86
|
entry: formatC64Address(entry),
|
|
87
|
-
...(exitMemory === undefined ? {} : { exitMemory:
|
|
87
|
+
...(exitMemory === undefined ? {} : { exitMemory: formatC64Byte(exitMemory) }),
|
|
88
88
|
...(packed.cycles === undefined ? {} : { unpackSeconds: Math.round((packed.cycles / PAL_CYCLES_PER_SECOND) * 10) / 10 }),
|
|
89
|
-
inputs: programs.map((program) => ({ file: program.file, loadRange:
|
|
89
|
+
inputs: programs.map((program) => ({ file: program.file, loadRange: formatC64RangeFields(program), bytes: program.body.length })),
|
|
90
90
|
check: { entry: formatC64Address(packed.entry), ...check },
|
|
91
91
|
};
|
|
92
92
|
const hint =
|
|
@@ -24,6 +24,7 @@ To get the command list, run the script with no arguments.
|
|
|
24
24
|
| Goal | Command |
|
|
25
25
|
|---|---|
|
|
26
26
|
| Compare two or more releases | `compare releases/a.prg releases/b.prg releases/c.prg` |
|
|
27
|
+
| Compare the releases in the project knowledge | `compare V1 V2 V3` |
|
|
27
28
|
| Compare a release that loads at a different address | `compare releases/a.prg releases/b.prg --shift 2:-256` |
|
|
28
29
|
| Compare only the bytes in a range | `compare releases/a.prg releases/b.prg --range '$0900-$09ff'` |
|
|
29
30
|
| Map two releases with moved code in blocks of equal bytes | `compare releases/a/image.json releases/b/image.json --align --range '$0400-$bfff'` |
|
|
@@ -44,6 +45,7 @@ To get the command list, run the script with no arguments.
|
|
|
44
45
|
|
|
45
46
|
- `compare` takes two or more releases. A release is one of these:
|
|
46
47
|
- `knowledge`: the program in the project knowledge.
|
|
48
|
+
- The label of a release in the project knowledge, for example `V2`. The base release is the program in the project knowledge. For another release, the project knowledge keeps only its bytes. Thus `--frames` cannot start it. To run that release, give its file. A label that is not a release gets error `not-found`, with the labels of the releases.
|
|
47
49
|
- A PRG file. The script puts it at its load address.
|
|
48
50
|
- The `image.json` of a capture folder. Keep all files of the capture folder together. Without them, `--frames` cannot start the release.
|
|
49
51
|
- Get a PRG out of a disk image before you compare it. The script refuses a disk image, a tape image and a cartridge image.
|
|
@@ -106,23 +108,26 @@ Compares the releases byte by byte at the addresses where all releases have byte
|
|
|
106
108
|
|
|
107
109
|
`body` holds:
|
|
108
110
|
|
|
109
|
-
- `releases`: the `name`, the `shift` and the `segments` of each release. An image has bytes only in its segments.
|
|
111
|
+
- `releases`: the `release`, the `name`, the `shift` and the `segments` of each release. `release` is the number of the release. An image has bytes only in its segments.
|
|
112
|
+
- `outsideCommon`: the ranges of the release in `--range` that are not in `common`. Another release has no bytes there, so the script does not compare these bytes. Examine them in each release.
|
|
110
113
|
- `common`: the ranges where all releases have bytes. The script compares only these ranges.
|
|
111
114
|
- `identicalBytes` and `differingBytes`: the counts of the bytes.
|
|
112
115
|
- `identicalShare`: the share of identical bytes, with 3 decimals. It is 1 only when no byte differs.
|
|
113
|
-
- `differences`: each item has `start`, `end`, `values` and `groups`. `values` gives the bytes of each release, at most 32. `groups` gives the releases that have the same bytes there.
|
|
116
|
+
- `differences`: each item has `start`, `end`, `values` and `groups`. `values` gives the bytes of each release, at most 32, by the number of the release. `groups` gives the numbers of the releases that have the same bytes there.
|
|
114
117
|
- `moreDifferences: true`: `differences` holds only the first differences.
|
|
115
118
|
- `suggestedShifts`: only when `identicalShare` is less than 0.5. Each item has `release`, `shift` and `matchingSequences`. Each release has at most 3 shifts, with the most `matchingSequences` first. `current: true` marks the shift of this run.
|
|
119
|
+
- `matchingSequences` counts the sequences of 8 bytes of release 1 in `--range` that the release has at that shift.
|
|
120
|
+
- A shift has at least 8 `matchingSequences`. Thus a range of fewer than 15 bytes gets no shift.
|
|
116
121
|
- `shiftsNotSuggested`: each item has a `reason`. The reason tells why a shift does not fit in `$0000-$ffff`.
|
|
117
122
|
|
|
118
123
|
With `--shift`, the addresses of `segments`, `common` and `differences` are the release addresses plus the `shift` of the release.
|
|
119
124
|
|
|
120
125
|
With `--frames`, the body also holds:
|
|
121
126
|
|
|
122
|
-
- `runtime` in each difference: one item for each release that started. `start` and `end` are the addresses in the release, without the shift.
|
|
127
|
+
- `runtime` in each difference: one item for each release that started, by the number of the release. `start` and `end` are the addresses in the release, without the shift.
|
|
123
128
|
- `first`: the first access in that release: `read`, `write`, `execute` or `none`. An instruction that reads and then writes the same byte, for example INC, counts as `read`. The operand bytes of an executed instruction count as `execute`, for example the `$03` of `LDA #$03`.
|
|
124
129
|
- `changed`: `true` when the bytes at the end of the run are not the bytes of the release.
|
|
125
|
-
- `run`: `frames`, `steps` and `releases`. `releases` tells for each release if it started, or the `reason` that it did not start. `skipped` tells that the releases have no differences, so no release ran.
|
|
130
|
+
- `run`: `frames`, `steps` and `releases`. `releases` tells for each release, by its number, if it started, or the `reason` that it did not start. `skipped` tells that the releases have no differences, so no release ran.
|
|
126
131
|
|
|
127
132
|
A PRG starts with RUN after its load. An image starts at its entry, with the entry state of its capture.
|
|
128
133
|
|
|
@@ -138,17 +143,21 @@ With `--align`, the body holds a map in place of `differences` and the shift sug
|
|
|
138
143
|
`hint` and `nextAction`:
|
|
139
144
|
|
|
140
145
|
- `identicalShare` is low, and a shift is in `suggestedShifts`: `nextAction` runs `compare` with the first shift that the run does not have. Try each shift. Data that did not move and code that moved give different shifts.
|
|
141
|
-
- `identicalShare` is low,
|
|
146
|
+
- `identicalShare` is low, no shift is better, and `suggestedShifts` or `shiftsNotSuggested` has items: `nextAction` runs `compare` with `--align`. The code and the data can be at other addresses in each release.
|
|
142
147
|
- `moreDifferences` or a `more...` count is in the body: `hint` tells you to compare a smaller `--range`.
|
|
143
148
|
|
|
144
149
|
Traps:
|
|
145
150
|
|
|
146
151
|
- `common` is empty: the releases do not have bytes at the same addresses. Give `--shift`, or a `--range` that all releases have.
|
|
147
|
-
-
|
|
152
|
+
- `identicalShare` is low, and the body has no `suggestedShifts` and no `shiftsNotSuggested`: the bytes do not show a shift, for example in a small `--range`. The releases can have different bytes at the same addresses. Read `differences`.
|
|
153
|
+
- A release has `started: false`: the `reason` tells why it did not start. The other releases still give evidence. These releases do not start:
|
|
154
|
+
- A PRG without a BASIC start.
|
|
155
|
+
- An image whose capture folder has no entry state.
|
|
156
|
+
- The label of a release that is not the base release.
|
|
148
157
|
- Each release writes a range before it reads it: the range is probably runtime state, not a difference of the releases.
|
|
149
158
|
- A range has `first: none` in each release: the run did not use it. Run more frames or other steps before you decide.
|
|
150
159
|
- The script refuses `--align` with `--shift` or `--frames`. It refuses `--min-block` without `--align`, and `--steps` without `--frames`.
|
|
151
|
-
- Error `not-found`: the project has no file at that path.
|
|
160
|
+
- Error `not-found`: the project has no file at that path, or the project knowledge has no release with that label. The message gives the labels of the releases. For a file with a name in upper case and no extension, give `./` before the name.
|
|
152
161
|
|
|
153
162
|
---
|
|
154
163
|
|
|
@@ -169,9 +178,9 @@ Gives evidence from the program in the project knowledge, its stages and its cov
|
|
|
169
178
|
- `access` is `executed`, `read`, `written`, `read-written` or `untouched`. `untouched` bytes did not take part in the runs.
|
|
170
179
|
- A `read` byte right after executed code, or at a return address, can be a dummy read of the 6502. The program does not use it.
|
|
171
180
|
- `roots`: the addresses where the reachability starts, each with `address` and `from`. The roots are the entry, and the NMI, RESET and IRQ vectors at `$fffa`-`$ffff` when the program holds them. A text in place of the list tells why no code region is reachable.
|
|
172
|
-
- `codeRegions`: the code regions in the project knowledge, as a list. Each region has `reachable`, and `executed` with coverage. `unknown` is `true` for a region that
|
|
173
|
-
- `codeRegions.unreachable`: the code regions that no chain of call and jump references reaches from a root.
|
|
174
|
-
- `unresolved`: only when a code region is unknown. These are the flows whose targets the project knowledge does not give
|
|
181
|
+
- `codeRegions`: the code regions in the project knowledge, as a list. Each region has `reachable`, and `executed` with coverage. `unknown` is `true` for a region that no root reaches and that no run executed, when the project knowledge has no coverage. It is also `true` when a flow in `unresolved` can go to the region.
|
|
182
|
+
- `codeRegions.unreachable`: the code regions that no chain of call and jump references reaches from a root. When the code stores the address of a handler with immediates in an interrupt vector at `$0314`-`$0319` or `$fffa`-`$ffff`, that code reaches the handler.
|
|
183
|
+
- `unresolved`: only when a code region is unknown. These are the flows in the reached or executed code regions whose targets the project knowledge does not give.
|
|
175
184
|
- `outside`: only with coverage. `read` and `executed` give the RAM outside the program that the program used but did not write. A loader or an earlier stage put these bytes there. `generated` gives the code that the program wrote outside its segments.
|
|
176
185
|
- `out`, `bytes` and `sha256`: the `--out` file. It has the fields of the body, with all items of each list.
|
|
177
186
|
|
|
@@ -194,10 +203,10 @@ Finds what the program in the project knowledge holds in addition to its working
|
|
|
194
203
|
| `text` | Text, and lines of assembler source. `encoding` is `petscii`, `screen`, `ascii` or `high-bit`. `reversed: true` marks screen codes with bit 7 set. `source: true` marks lines of assembler source. A line end in `text` is a new line. `mnemonics` counts 6502 instructions in the text. A text of more than 120 characters is cut, and `length` gives its full length. |
|
|
195
204
|
| `basic` | Two or more BASIC lines away from the BASIC start. `linkedAt` is the address where BASIC linked the lines. It can be a different address from the address of the bytes now. |
|
|
196
205
|
| `fill` | One byte that repeats at least 16 times, or a pattern of two to four bytes that repeats for at least 32 bytes. `pattern` is the repeated bytes. |
|
|
197
|
-
| `residue` | Bytes that no run touched, and that are not text, BASIC, fill
|
|
206
|
+
| `residue` | Bytes that no run touched, and that are not text, BASIC, fill, a code region, dead code or unknown code. `bytes` shows the first 16 bytes. |
|
|
198
207
|
| `copies` | At least 32 bytes that are the same as earlier bytes of the program. `copyOf` is the address of the earlier bytes. |
|
|
199
|
-
| `deadCode` | Code
|
|
200
|
-
| `unknownCode` | Code
|
|
208
|
+
| `deadCode` | Code that no run executed and that nothing reaches. The script decodes the instructions from the entry, the routines, the code regions and the coverage. A call, a jump, a branch, a table entry or an interrupt vector reaches code. The reach starts at the entry, at the code that a run executed, at the patches and at the start screen. The leftover files of a generated source hold the same code. |
|
|
209
|
+
| `unknownCode` | Code that no run executed and that nothing reaches, but that is not dead code: a flow in `unresolved` can go to it. Each flow has `at` and `kind`: `indirect-jump`, `written-operand` or `pushed-return`. |
|
|
201
210
|
|
|
202
211
|
Each finding has `stages`: the unpack stages that wrote its bytes. A stage before the last stage is a depacker, an intro or a loader. Each finding also has `access`: the accesses of its bytes in the coverage.
|
|
203
212
|
|
|
@@ -220,9 +229,9 @@ Read the findings with care:
|
|
|
220
229
|
|
|
221
230
|
Traps:
|
|
222
231
|
|
|
223
|
-
- No coverage: `residue`
|
|
224
|
-
- No code regions: `deadCode` is empty, and `residue` can include code. Analyze the program for its code regions first.
|
|
225
|
-
- `unknownCode` has items: a flow in `unresolved` can go to the code
|
|
232
|
+
- No coverage: `residue`, `deadCode` and `unknownCode` are empty. Put the coverage of runs that use many parts of the program into the project knowledge.
|
|
233
|
+
- No code regions and no routines: the script decodes only the code that the entry and the coverage reach. Then `deadCode` is empty, and `residue` can include code. Analyze the program for its code regions first.
|
|
234
|
+
- `unknownCode` has items: a flow in `unresolved` can go to the code. Do not call this code dead. Find the targets of each flow in the emulator, and record a jump reference from the flow to each target in the project knowledge. When the flow goes outside the program, record the reference to that address.
|
|
226
235
|
- Error `not-found`: the project knowledge has no program. Put the program into the project knowledge.
|
|
227
236
|
|
|
228
237
|
---
|
|
@@ -7,7 +7,7 @@
|
|
|
7
7
|
// bytes. It never decides which bytes the publisher shipped: that needs evidence
|
|
8
8
|
// beyond one comparison.
|
|
9
9
|
|
|
10
|
-
import { type AddressRange,
|
|
10
|
+
import { type AddressRange, formatC64Range } from "#src/c64.ts";
|
|
11
11
|
|
|
12
12
|
export interface Release {
|
|
13
13
|
name: string;
|
|
@@ -20,15 +20,17 @@ export interface Release {
|
|
|
20
20
|
export interface DifferingRange {
|
|
21
21
|
start: number;
|
|
22
22
|
end: number;
|
|
23
|
-
/** The bytes of each release in this range,
|
|
24
|
-
values:
|
|
25
|
-
/**
|
|
26
|
-
groups:
|
|
23
|
+
/** The bytes of each release in this range, in the order of the releases: a name can be given two times. */
|
|
24
|
+
values: number[][];
|
|
25
|
+
/** The numbers of the releases (1 is the first) with the same bytes here, largest group first. */
|
|
26
|
+
groups: number[][];
|
|
27
27
|
}
|
|
28
28
|
|
|
29
29
|
export interface Comparison {
|
|
30
30
|
/** The addresses where every release has bytes. */
|
|
31
31
|
common: AddressRange[];
|
|
32
|
+
/** For each release, in order: its addresses in the range that are not in common, because another release has no bytes there. */
|
|
33
|
+
outsideCommon: AddressRange[][];
|
|
32
34
|
identicalBytes: number;
|
|
33
35
|
differingBytes: number;
|
|
34
36
|
differences: DifferingRange[];
|
|
@@ -54,10 +56,15 @@ export function identicalShare(identical: number, differing: number): number {
|
|
|
54
56
|
}
|
|
55
57
|
|
|
56
58
|
/** The ranges where every release has bytes, inside from..to. */
|
|
57
|
-
|
|
59
|
+
function commonRanges(releases: Release[], from = 0x0000, to = 0xffff): AddressRange[] {
|
|
58
60
|
return rangesOf(Array.from({ length: to - from + 1 }, (_, offset) => from + offset).filter((address) => releases.every((release) => covers(release, address))));
|
|
59
61
|
}
|
|
60
62
|
|
|
63
|
+
/** The addresses of a release inside from..to where another release has no bytes. */
|
|
64
|
+
function outsideOthers(release: Release, releases: Release[], from: number, to: number): AddressRange[] {
|
|
65
|
+
return rangesOf(addressesOf(release).filter((address) => address >= from && address <= to && !releases.every((other) => covers(other, address))));
|
|
66
|
+
}
|
|
67
|
+
|
|
61
68
|
/** Consecutive addresses in order as ranges. */
|
|
62
69
|
function rangesOf(addresses: number[]): AddressRange[] {
|
|
63
70
|
const ranges: AddressRange[] = [];
|
|
@@ -71,6 +78,7 @@ function rangesOf(addresses: number[]): AddressRange[] {
|
|
|
71
78
|
|
|
72
79
|
export function compareReleases(releases: Release[], options: { from?: number; to?: number; maxRanges?: number } = {}): Comparison {
|
|
73
80
|
const common = commonRanges(releases, options.from, options.to);
|
|
81
|
+
const outsideCommon = releases.map((release) => outsideOthers(release, releases, options.from ?? 0x0000, options.to ?? 0xffff));
|
|
74
82
|
let identicalBytes = 0;
|
|
75
83
|
const differing: number[] = [];
|
|
76
84
|
for (const range of common) {
|
|
@@ -90,17 +98,15 @@ export function compareReleases(releases: Release[], options: { from?: number; t
|
|
|
90
98
|
}
|
|
91
99
|
const limit = options.maxRanges ?? 64;
|
|
92
100
|
const differences = ranges.slice(0, limit).map((range) => {
|
|
93
|
-
const values:
|
|
94
|
-
const groups = new Map<string,
|
|
95
|
-
for (const
|
|
96
|
-
const bytes = Array.from({ length: range.end - range.start + 1 }, (_, offset) => release.byteAt(range.start + offset));
|
|
97
|
-
values[release.name] = bytes;
|
|
101
|
+
const values = releases.map((release) => Array.from({ length: range.end - range.start + 1 }, (_, offset) => release.byteAt(range.start + offset)));
|
|
102
|
+
const groups = new Map<string, number[]>();
|
|
103
|
+
for (const [index, bytes] of values.entries()) {
|
|
98
104
|
const key = bytes.join(",");
|
|
99
|
-
groups.set(key, [...(groups.get(key) ?? []),
|
|
105
|
+
groups.set(key, [...(groups.get(key) ?? []), index + 1]);
|
|
100
106
|
}
|
|
101
107
|
return { ...range, values, groups: [...groups.values()].sort((a, b) => b.length - a.length) };
|
|
102
108
|
});
|
|
103
|
-
return { common, identicalBytes, differingBytes: differing.length, differences, moreDifferences: ranges.length > limit };
|
|
109
|
+
return { common, outsideCommon, identicalBytes, differingBytes: differing.length, differences, moreDifferences: ranges.length > limit };
|
|
104
110
|
}
|
|
105
111
|
|
|
106
112
|
/** The addresses of a release in order, so that a window of bytes can run over its segments. */
|
|
@@ -127,15 +133,19 @@ function indexWindows(release: Release, addresses: number[]): Map<string, number
|
|
|
127
133
|
}
|
|
128
134
|
|
|
129
135
|
/** Most shifts that suggestShifts gives: data that did not move can outvote code that did. */
|
|
130
|
-
|
|
136
|
+
const MAX_SUGGESTED_SHIFTS = 3;
|
|
137
|
+
|
|
138
|
+
/** The fewest equal 8-byte sequences for a shift: fewer occur by chance, and a range of fewer than 15 bytes has fewer. */
|
|
139
|
+
export const MIN_MATCHING_SEQUENCES = 8;
|
|
131
140
|
|
|
132
141
|
/**
|
|
133
|
-
* The shifts that put `other` over `base` best: the
|
|
134
|
-
* differences between equal 8-byte sequences, most votes
|
|
135
|
-
*
|
|
142
|
+
* The shifts that put `other` over the bytes of `base` in `range` best: the
|
|
143
|
+
* most common address differences between equal 8-byte sequences, most votes
|
|
144
|
+
* first. A shift with fewer than MIN_MATCHING_SEQUENCES votes, or with less than
|
|
145
|
+
* a tenth of the best one's votes, is chance. Empty when nothing matches.
|
|
136
146
|
*/
|
|
137
|
-
export function suggestShifts(base: Release, other: Release): Array<{ shift: number; matches: number }> {
|
|
138
|
-
const positions = indexWindows(base, addressesOf(base));
|
|
147
|
+
export function suggestShifts(base: Release, other: Release, range: AddressRange = { start: 0x0000, end: 0xffff }): Array<{ shift: number; matches: number }> {
|
|
148
|
+
const positions = indexWindows(base, addressesOf(base).filter((address) => address >= range.start && address <= range.end));
|
|
139
149
|
const otherAddresses = addressesOf(other);
|
|
140
150
|
const votes = new Map<number, number>();
|
|
141
151
|
for (let at = 0; at + WINDOW <= otherAddresses.length; at++) {
|
|
@@ -145,7 +155,7 @@ export function suggestShifts(base: Release, other: Release): Array<{ shift: num
|
|
|
145
155
|
}
|
|
146
156
|
}
|
|
147
157
|
const ranked = [...votes].map(([shift, matches]) => ({ shift, matches })).sort((a, b) => b.matches - a.matches || Math.abs(a.shift) - Math.abs(b.shift));
|
|
148
|
-
return ranked.filter((found) => found.matches * 10 >= ranked[0]!.matches).slice(0, MAX_SUGGESTED_SHIFTS);
|
|
158
|
+
return ranked.filter((found) => found.matches >= MIN_MATCHING_SEQUENCES && found.matches * 10 >= ranked[0]!.matches).slice(0, MAX_SUGGESTED_SHIFTS);
|
|
149
159
|
}
|
|
150
160
|
|
|
151
161
|
export interface Block {
|
|
@@ -275,7 +285,7 @@ export type ShiftSuggestion = { release: number; shift: number; matchingSequence
|
|
|
275
285
|
export function shiftSuggestion(releases: Release[], applied: number[], index: number, found: { shift: number; matches: number }): ShiftSuggestion {
|
|
276
286
|
const outside = (release: Release, by: number) => release.segments.find((segment) => segment.start + by < 0 || segment.end + by > 0xffff);
|
|
277
287
|
const why = (number: number, by: number, segment: AddressRange) =>
|
|
278
|
-
`release ${number} moved by ${by} puts ${
|
|
288
|
+
`release ${number} moved by ${by} puts ${formatC64Range(segment)} ${segment.start + by < 0 ? "below $0000" : "past $ffff"}`;
|
|
279
289
|
const own = outside(releases[index]!, found.shift);
|
|
280
290
|
if (own === undefined) return { release: index + 1, shift: applied[index]! + found.shift, matchingSequences: found.matches };
|
|
281
291
|
const reasons = [why(index + 1, found.shift, own)];
|
|
@@ -1,13 +1,14 @@
|
|
|
1
1
|
// Leftovers in one program for the c64-provenance script: text in PETSCII,
|
|
2
2
|
// screen codes, ASCII or text with bit 7 set (Apple II), assembler source
|
|
3
3
|
// lines, BASIC lines away from the BASIC start, fills, untouched
|
|
4
|
-
// bytes outside code
|
|
5
|
-
// a byte range, and
|
|
4
|
+
// bytes outside code that no other finding explains, exact copies of
|
|
5
|
+
// a byte range, and the unused and the unknown code of the code reach. Each finding gets the stages and the accesses of
|
|
6
6
|
// its bytes from the separation. Evidence only: nothing here decides what a
|
|
7
7
|
// leftover is or who made it.
|
|
8
8
|
|
|
9
|
-
import { type AddressRange, basicLineText } from "#src/c64.ts";
|
|
9
|
+
import { type AddressRange, basicLineText, mergeRanges, subtract } from "#src/c64.ts";
|
|
10
10
|
import { MNEMONICS } from "#src/opcodes.ts";
|
|
11
|
+
import type { CodeReach } from "#src/knowledge/code-reach.ts";
|
|
11
12
|
import type { Access, Separation } from "#src/knowledge/separate.ts";
|
|
12
13
|
|
|
13
14
|
export interface LeftoversInput {
|
|
@@ -15,6 +16,8 @@ export interface LeftoversInput {
|
|
|
15
16
|
/** 64 KiB with the program bytes at their addresses. */
|
|
16
17
|
memory: Uint8Array;
|
|
17
18
|
separation: Separation;
|
|
19
|
+
/** The code reach of the program, as the leftovers of a generated source take it. */
|
|
20
|
+
code: Pick<CodeReach, "unused" | "unknown">;
|
|
18
21
|
/** The first address of the BASIC start, which RUN lists: it is no leftover. */
|
|
19
22
|
basicStart?: number;
|
|
20
23
|
/** The fewest characters of a text (default 8). */
|
|
@@ -62,12 +65,8 @@ export interface CopyFinding extends AddressRange, Placement {
|
|
|
62
65
|
copyOf: number;
|
|
63
66
|
}
|
|
64
67
|
|
|
65
|
-
/**
|
|
66
|
-
export
|
|
67
|
-
type: string;
|
|
68
|
-
/** false when the coverage shows that it did not run; absent without coverage. */
|
|
69
|
-
executed?: false;
|
|
70
|
-
}
|
|
68
|
+
/** Code that nothing that stays reaches and that no run executed. */
|
|
69
|
+
export type CodeFinding = AddressRange & Placement;
|
|
71
70
|
|
|
72
71
|
export interface Leftovers {
|
|
73
72
|
text: TextFinding[];
|
|
@@ -75,9 +74,10 @@ export interface Leftovers {
|
|
|
75
74
|
fill: FillFinding[];
|
|
76
75
|
residue: ResidueFinding[];
|
|
77
76
|
copies: CopyFinding[];
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
77
|
+
/** The unused code of the code reach. */
|
|
78
|
+
deadCode: CodeFinding[];
|
|
79
|
+
/** Code that nothing reaches and that no run executed, which is not dead: a flow that nobody resolved can go there. */
|
|
80
|
+
unknownCode: CodeFinding[];
|
|
81
81
|
}
|
|
82
82
|
|
|
83
83
|
const MIN_TEXT = 8;
|
|
@@ -399,20 +399,8 @@ export function findCopies(memory: Uint8Array, segments: AddressRange[]): CopyFi
|
|
|
399
399
|
return found;
|
|
400
400
|
}
|
|
401
401
|
|
|
402
|
-
/** Splits `range` at the parts that `taken` covers, and keeps the pieces of at least `min` bytes. */
|
|
403
|
-
function without(range: AddressRange, taken: AddressRange[], min: number): AddressRange[] {
|
|
404
|
-
const pieces: AddressRange[] = [];
|
|
405
|
-
let start = range.start;
|
|
406
|
-
for (const part of taken.filter((item) => item.start <= range.end && item.end >= range.start).sort((a, b) => a.start - b.start)) {
|
|
407
|
-
if (part.start > start) pieces.push({ start, end: part.start - 1 });
|
|
408
|
-
start = Math.max(start, part.end + 1);
|
|
409
|
-
}
|
|
410
|
-
if (start <= range.end) pieces.push({ start, end: range.end });
|
|
411
|
-
return pieces.filter((piece) => piece.end - piece.start + 1 >= min);
|
|
412
|
-
}
|
|
413
|
-
|
|
414
402
|
export function findLeftovers(input: LeftoversInput): Leftovers {
|
|
415
|
-
const { memory, segments, separation } = input;
|
|
403
|
+
const { memory, segments, separation, code } = input;
|
|
416
404
|
const ranges = separation.ranges;
|
|
417
405
|
const placement = (range: AddressRange): Placement => {
|
|
418
406
|
const parts = ranges.filter((part) => part.start <= range.end && part.end >= range.start);
|
|
@@ -433,15 +421,9 @@ export function findLeftovers(input: LeftoversInput): Leftovers {
|
|
|
433
421
|
if (last !== undefined && last.end + 1 === part.start) last.end = part.end;
|
|
434
422
|
else untouched.push({ start: part.start, end: part.end });
|
|
435
423
|
}
|
|
424
|
+
const deadCode = mergeRanges(code.unused);
|
|
436
425
|
// Untouched code is code that the runs did not get to, or dead code, which has its own list.
|
|
437
|
-
const explained = [...text, ...basic, ...fill, ...separation.codeRegions];
|
|
438
|
-
const residue = untouched.flatMap((range) =>
|
|
439
|
-
|
|
440
|
-
const unreached = separation.codeRegions
|
|
441
|
-
.filter((region) => region.reachable === false && region.executed !== true)
|
|
442
|
-
.map((region) => ({ start: region.start, end: region.end, type: region.type, ...(region.executed === false ? { executed: false as const } : {}), unknown: region.unknown === true }));
|
|
443
|
-
const finding = ({ unknown: _, ...region }: (typeof unreached)[number]): DeadCodeFinding => region;
|
|
444
|
-
const deadCode = unreached.filter((region) => !region.unknown).map(finding);
|
|
445
|
-
const unknownCode = unreached.filter((region) => region.unknown).map(finding);
|
|
446
|
-
return { text: placed(text), basic: placed(basic), fill: placed(fill), residue: placed(residue), copies: placed(findCopies(memory, segments)), deadCode: placed(deadCode), unknownCode: placed(unknownCode) };
|
|
426
|
+
const explained = [...text, ...basic, ...fill, ...separation.codeRegions, ...deadCode, ...code.unknown];
|
|
427
|
+
const residue = untouched.flatMap((range) => subtract(range, explained).filter((piece) => piece.end - piece.start + 1 >= MIN_RESIDUE));
|
|
428
|
+
return { text: placed(text), basic: placed(basic), fill: placed(fill), residue: placed(residue), copies: placed(findCopies(memory, segments)), deadCode: placed(deadCode), unknownCode: placed(code.unknown) };
|
|
447
429
|
}
|
|
@@ -4,20 +4,20 @@
|
|
|
4
4
|
// its start, so match finds it in another program at another address and
|
|
5
5
|
// suggests its names there as writes of the knowledge. Nothing here writes knowledge.
|
|
6
6
|
|
|
7
|
-
import { createHash } from "node:crypto";
|
|
8
7
|
import { existsSync, mkdirSync, readdirSync, readFileSync, realpathSync, renameSync, statSync, writeFileSync } from "node:fs";
|
|
9
8
|
import { dirname, join, relative, resolve, sep } from "node:path";
|
|
10
9
|
|
|
11
10
|
import { Refusal } from "#src/errors.ts";
|
|
12
11
|
import { writeRefusal, type Written } from "#src/host-client/transfer.ts";
|
|
13
|
-
import { formatC64Address, instructionLength } from "#src/c64.ts";
|
|
14
|
-
import { evaluate, identifiers, operandText } from "#src/knowledge/expression.ts";
|
|
12
|
+
import { formatC64Address, instructionLength, REFERENCE_KINDS, type ReferenceKind } from "#src/c64.ts";
|
|
13
|
+
import { evaluate, identifiers, OPERAND_SELECTS, operandText, type OperandSelect } from "#src/knowledge/expression.ts";
|
|
14
|
+
import { sha256 } from "#src/knowledge/text.ts";
|
|
15
15
|
import type { CommentRow, OperandRow, ReferenceRow, RegionRow, SymbolRow } from "#src/knowledge/read.ts";
|
|
16
|
-
import { COMMENT_PLACEMENTS, type CommentPlacement, isSemantic,
|
|
16
|
+
import { COMMENT_PLACEMENTS, type CommentPlacement, isSemantic, REGION_TYPES, type RegionType, SYMBOL_KINDS, type SymbolKind } from "#src/knowledge/schema.ts";
|
|
17
17
|
import { checkOutputPath, isInside, knowledgePart, LOCAL_DIRECTORY, PROJECT_DIRECTORY } from "#src/project.ts";
|
|
18
18
|
|
|
19
19
|
/** A routine shorter than this is not saved and not searched: its bytes occur by chance. */
|
|
20
|
-
|
|
20
|
+
const MIN_ROUTINE = 16;
|
|
21
21
|
|
|
22
22
|
/** A routine name is also its file name: a letter or underscore, then letters, digits or underscores. */
|
|
23
23
|
const ROUTINE_NAME = /^[A-Za-z_][A-Za-z0-9_]{0,63}$/;
|
|
@@ -181,10 +181,10 @@ export function routineFrom(source: RoutineSource): { routine: Routine; leftOut:
|
|
|
181
181
|
}
|
|
182
182
|
for (const at of relocations.keys()) variable.delete(at);
|
|
183
183
|
|
|
184
|
-
// The
|
|
185
|
-
//
|
|
186
|
-
const regions: Routine["regions"] =
|
|
187
|
-
for (const region of source.regions
|
|
184
|
+
// The regions of the user and the LLM inside the routine. The code regions of the analyzers give only the instructions:
|
|
185
|
+
// as a region, a match writes them again as the LLM's, also in the program that the routine came from.
|
|
186
|
+
const regions: Routine["regions"] = [];
|
|
187
|
+
for (const region of source.regions) {
|
|
188
188
|
if (region.start >= start && region.end <= end) regions.push({ start: region.start - start, end: region.end - start, type: region.type, ...(region.element === undefined ? {} : { element: region.element }) });
|
|
189
189
|
else leftOut.push({ what: `the ${region.type} region ${where(region.start)}-${where(region.end)}`, reason: "it crosses the border of the routine" });
|
|
190
190
|
}
|
|
@@ -372,7 +372,7 @@ export function saveRoutine(folder: string, routine: Routine, replace: boolean,
|
|
|
372
372
|
} catch (error) {
|
|
373
373
|
throw writeRefusal(error, out, "--library") ?? error;
|
|
374
374
|
}
|
|
375
|
-
return { out, bytes: bytes.length, sha256:
|
|
375
|
+
return { out, bytes: bytes.length, sha256: sha256(bytes) };
|
|
376
376
|
}
|
|
377
377
|
|
|
378
378
|
/** An image to search: its segments and its bytes inside them. */
|