@henols/c64-re-tools 2.8.0-rc.4 → 2.8.0-rc.5
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-knowledge/SKILL.md +7 -4
- package/skills/c64-knowledge/references/batches.md +2 -1
- package/skills/c64-knowledge/references/source-knowledge.md +1 -1
- package/skills/c64-knowledge/scripts/knowledge.ts +17 -19
- package/skills/c64-provenance/SKILL.md +2 -2
- package/skills/c64-provenance/scripts/lib/runtime.ts +54 -8
- package/skills/c64-provenance/scripts/provenance.ts +10 -3
- package/skills/c64-source/scripts/lib/source.ts +3 -1
- package/skills/c64-static-analysis/scripts/lib/idioms.ts +92 -42
- package/skills/c64-testing/SKILL.md +1 -0
- package/skills/c64-testing/scripts/lib/recording.ts +63 -18
- package/skills/c64-testing/scripts/lib/replay.ts +3 -6
- package/skills/c64-testing/scripts/testing.ts +8 -2
- package/skills/c64-unpacker/SKILL.md +8 -8
- package/skills/c64-unpacker/scripts/lib/loads.ts +7 -4
- package/skills/c64-unpacker/scripts/lib/machine.ts +40 -3
- package/skills/c64-unpacker/scripts/lib/stages.ts +8 -5
- package/skills/c64-unpacker/scripts/unpacker.ts +24 -6
- package/src/c64.ts +21 -0
- package/src/host/vice/chips.ts +6 -9
- package/src/host/vice/session.ts +2 -2
- package/src/host-client/image.ts +7 -0
- package/src/knowledge/import.ts +4 -2
- package/src/knowledge/join.ts +50 -2
- package/src/knowledge/program.ts +36 -17
- package/src/knowledge/start-screen.ts +3 -6
- package/src/knowledge/write.ts +2 -1
- package/src/opcodes.ts +5 -0
package/src/knowledge/join.ts
CHANGED
|
@@ -4,7 +4,10 @@
|
|
|
4
4
|
// there; at an address that no batch owns, the first batch in the order given
|
|
5
5
|
// wins. A name that a batch gave to an address, and that the join replaced
|
|
6
6
|
// with the name of another batch, is replaced in that batch's operand
|
|
7
|
-
// expressions and macro
|
|
7
|
+
// expressions, macro args, patch code and macro bodies too. In the comment
|
|
8
|
+
// texts of the batch, and after a ; in its code, the name is replaced as a
|
|
9
|
+
// whole word only when no English word can be the name: "the score" stays
|
|
10
|
+
// prose. The join writes no knowledge.
|
|
8
11
|
|
|
9
12
|
import { replaceIdentifiers } from "./expression.ts";
|
|
10
13
|
import { spaceOf } from "./schema.ts";
|
|
@@ -46,6 +49,13 @@ export interface JoinResult {
|
|
|
46
49
|
|
|
47
50
|
type Op<K extends BatchOperation["op"]> = Extract<BatchOperation, { op: K }>;
|
|
48
51
|
|
|
52
|
+
/** A name as a whole word of a comment: a word after a letter, a digit, _, . or $ is part of another word or a number. */
|
|
53
|
+
const WORD = /(?<![A-Za-z0-9_.$])\.?[A-Za-z_][A-Za-z0-9_]*/g;
|
|
54
|
+
/** A name that no English word is: with a dot first (a local label), an underscore, a digit, or a capital letter after a small one. */
|
|
55
|
+
const NO_WORD = /^\.|[_0-9]|[a-z][A-Z]/;
|
|
56
|
+
/** A string or a character of ACME, also one that the line ends before its quote: its text holds no name. */
|
|
57
|
+
const QUOTED = /("(?:\\.|[^"\\])*"?|'(?:\\.|[^'\\])*'?)/;
|
|
58
|
+
|
|
49
59
|
/** The value of a keyed write as text, to compare and report it. */
|
|
50
60
|
function valueOf(op: BatchOperation): string {
|
|
51
61
|
switch (op.op) {
|
|
@@ -134,6 +144,35 @@ export function joinBatches(batches: readonly Batch[]): JoinResult {
|
|
|
134
144
|
return text;
|
|
135
145
|
}
|
|
136
146
|
};
|
|
147
|
+
/** The comment text of a batch with each name that it lost and that no English word is. */
|
|
148
|
+
const followWords = (batch: Batch, space: string, text: string) => {
|
|
149
|
+
const map = renamedIn.get(batch)!;
|
|
150
|
+
return map.size === 0 ? text : text.replace(WORD, (word) => (NO_WORD.test(word) ? (map.get(`${space}|${word}`) ?? word) : word));
|
|
151
|
+
};
|
|
152
|
+
/** ACME code of a batch with each name that it lost, outside strings and `kept` (the params of a macro), and its comments after a ; as followWords gives them. */
|
|
153
|
+
const followCode = (batch: Batch, space: string, code: string, kept: readonly string[] = []) => {
|
|
154
|
+
const map = renamedIn.get(batch)!;
|
|
155
|
+
if (map.size === 0) return code;
|
|
156
|
+
const name = (word: string) => (kept.includes(word) ? word : (map.get(`${space}|${word}`) ?? word));
|
|
157
|
+
return code
|
|
158
|
+
.split("\n")
|
|
159
|
+
.map((line) => {
|
|
160
|
+
// The parts at odd indexes are strings. A ; outside a string starts the comment of the line.
|
|
161
|
+
let comment = false;
|
|
162
|
+
return line
|
|
163
|
+
.split(QUOTED)
|
|
164
|
+
.map((part, index) => {
|
|
165
|
+
if (comment) return followWords(batch, space, part);
|
|
166
|
+
if (index % 2 === 1) return part;
|
|
167
|
+
const at = part.indexOf(";");
|
|
168
|
+
if (at === -1) return part.replace(WORD, name);
|
|
169
|
+
comment = true;
|
|
170
|
+
return part.slice(0, at).replace(WORD, name) + followWords(batch, space, part.slice(at));
|
|
171
|
+
})
|
|
172
|
+
.join("");
|
|
173
|
+
})
|
|
174
|
+
.join("\n");
|
|
175
|
+
};
|
|
137
176
|
|
|
138
177
|
const comments = choose("comment", "comment", (write) => `${spaceOf(write)}|${write.address}|${write.placement}`, (write) => write.address);
|
|
139
178
|
const operands = choose("operand", "operand", (write) => `${spaceOf(write)}|${write.address}`, (write) => write.address);
|
|
@@ -191,19 +230,28 @@ export function joinBatches(batches: readonly Batch[]): JoinResult {
|
|
|
191
230
|
case "use":
|
|
192
231
|
if (once(write)) rest.push({ ...write, args: (write.args ?? []).map((arg) => follow(batch, spaceOf(write), arg)) });
|
|
193
232
|
break;
|
|
233
|
+
case "patch":
|
|
234
|
+
if (once(write)) rest.push({ ...write, code: followCode(batch, spaceOf(write), write.code), ...(write.comment === undefined ? {} : { comment: followWords(batch, spaceOf(write), write.comment) }) });
|
|
235
|
+
break;
|
|
194
236
|
default:
|
|
195
237
|
if (once(write)) rest.push(write);
|
|
196
238
|
}
|
|
197
239
|
}
|
|
198
240
|
}
|
|
199
241
|
const kept = <T extends { batch: Batch; write: BatchOperation }>(map: Map<string, T>) => [...map.values()].map(({ write }) => write);
|
|
242
|
+
const commentWrites = [...comments.values()].map(({ batch, write }) => ({ ...write, text: followWords(batch, spaceOf(write), write.text) }));
|
|
243
|
+
const macroWrites = [...macros.values()].map(({ batch, write }) => ({
|
|
244
|
+
...write,
|
|
245
|
+
body: followCode(batch, spaceOf(write), write.body, write.params),
|
|
246
|
+
...(write.comment === undefined ? {} : { comment: followWords(batch, spaceOf(write), write.comment) }),
|
|
247
|
+
}));
|
|
200
248
|
const operandWrites = [...operands.values()].map(({ batch, write }) => ({
|
|
201
249
|
...write,
|
|
202
250
|
...(write.symbol === undefined ? {} : { symbol: follow(batch, spaceOf(write), write.symbol) }),
|
|
203
251
|
...(write.expression === undefined ? {} : { expression: follow(batch, spaceOf(write), write.expression) }),
|
|
204
252
|
}));
|
|
205
253
|
return {
|
|
206
|
-
ops: [...removals, ...foreignRegions, ...ownRegions, ...kept(names), ...
|
|
254
|
+
ops: [...removals, ...foreignRegions, ...ownRegions, ...kept(names), ...commentWrites, ...kept(modules), ...references, ...macroWrites, ...operandWrites, ...rest],
|
|
207
255
|
choices,
|
|
208
256
|
clashes,
|
|
209
257
|
repeated,
|
package/src/knowledge/program.ts
CHANGED
|
@@ -6,7 +6,7 @@
|
|
|
6
6
|
import type { DatabaseSync } from "node:sqlite";
|
|
7
7
|
|
|
8
8
|
import { Refusal } from "../errors.ts";
|
|
9
|
-
import { type AddressRange, COVERAGE, formatC64Address, formatC64Byte, formatC64Range, inRanges, prgOf, rangesWhere } from "../c64.ts";
|
|
9
|
+
import { type AddressRange, COVERAGE, formatC64Address, formatC64Byte, formatC64Range, inRanges, mergeRanges, prgOf, rangesWhere } from "../c64.ts";
|
|
10
10
|
import { COVERAGE_KINDS, DEFAULT_SPACE, MEMORY_ROW, type CoverageKind, type Space } from "./schema.ts";
|
|
11
11
|
import { coverageRuns } from "./read.ts";
|
|
12
12
|
import { sha256 } from "./text.ts";
|
|
@@ -281,19 +281,28 @@ export function coverageAccess(db: DatabaseSync | undefined, space: Space = DEFA
|
|
|
281
281
|
};
|
|
282
282
|
}
|
|
283
283
|
|
|
284
|
-
/** The bytes of memory rows in
|
|
284
|
+
/** The bytes of memory rows in 64 KiB, and the ranges that they fill: rows that touch are one range (mergeRanges). */
|
|
285
285
|
function rowsImage(rows: ReadonlyArray<{ address: number; bytes: string }>): { segments: AddressRange[]; memory: Uint8Array } {
|
|
286
286
|
const memory = new Uint8Array(SIZE);
|
|
287
|
-
const
|
|
288
|
-
for (const row of rows) {
|
|
287
|
+
const ranges = rows.map((row) => {
|
|
289
288
|
const bytes = Buffer.from(row.bytes, "hex");
|
|
290
289
|
memory.set(bytes, row.address);
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
290
|
+
return { start: row.address, end: row.address + bytes.length - 1 };
|
|
291
|
+
});
|
|
292
|
+
return { segments: mergeRanges(ranges), memory };
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
/**
|
|
296
|
+
* True when an image holds the program of the knowledge: the same ranges and
|
|
297
|
+
* the same byte at each address. The knowledge keeps segments that touch as
|
|
298
|
+
* one range (rowsImage), so the segments of the image are joined in the same
|
|
299
|
+
* way before the compare.
|
|
300
|
+
*/
|
|
301
|
+
export function isProgram(program: Pick<ProgramImage, "segments" | "byteAt">, image: Pick<ProgramImage, "segments" | "byteAt">): boolean {
|
|
302
|
+
return JSON.stringify(mergeRanges(image.segments)) === JSON.stringify(program.segments) && program.segments.every((segment) => {
|
|
303
|
+
for (let address = segment.start; address <= segment.end; address++) if (program.byteAt(address) !== image.byteAt(address)) return false;
|
|
304
|
+
return true;
|
|
305
|
+
});
|
|
297
306
|
}
|
|
298
307
|
|
|
299
308
|
/** The program of a space, or undefined when the knowledge has no bytes for it. */
|
|
@@ -312,6 +321,11 @@ export function programImage(db: DatabaseSync | undefined, space: Space = DEFAUL
|
|
|
312
321
|
return image;
|
|
313
322
|
}
|
|
314
323
|
|
|
324
|
+
/** The entry of the program of a space, without its bytes; undefined when the knowledge has none. */
|
|
325
|
+
export function programEntry(db: DatabaseSync | undefined, space: Space = DEFAULT_SPACE): number | undefined {
|
|
326
|
+
return (db?.prepare("SELECT address FROM current_entries WHERE space = ?").get(space) as { address: number } | undefined)?.address;
|
|
327
|
+
}
|
|
328
|
+
|
|
315
329
|
/** The program of a space for a tool; refused with the way to put it in when the knowledge has none. */
|
|
316
330
|
export function requireProgram(db: DatabaseSync | undefined, space: Space = DEFAULT_SPACE): ProgramImage {
|
|
317
331
|
const image = programImage(db, space);
|
|
@@ -327,21 +341,26 @@ const STATE_PAGES = ["$0000", "$0100", "$0200", "$0300"] as const;
|
|
|
327
341
|
* The memory that the program has at its entry: the segments, and below them
|
|
328
342
|
* $0002-$03ff of the entry state (the zero page, the stack and the system
|
|
329
343
|
* pages hold code and data that the loader left). $0000 and $0001 are the
|
|
330
|
-
* CPU port, not memory. A segment byte wins over a state byte.
|
|
344
|
+
* CPU port, not memory. A segment byte wins over a state byte. `segments`
|
|
345
|
+
* are the ranges that hold these bytes, and `fromState` the ranges of the
|
|
346
|
+
* state pages in them: none when the state has no page. The analyzers read
|
|
347
|
+
* this memory, and the listing of the knowledge shows it.
|
|
331
348
|
*/
|
|
332
|
-
function entryMemory(image: ProgramImage):
|
|
349
|
+
export function entryMemory(image: ProgramImage): Pick<ProgramImage, "segments" | "memory" | "byteAt"> & { fromState: AddressRange[] } {
|
|
333
350
|
const memory = new Uint8Array(image.memory);
|
|
351
|
+
const fromState: AddressRange[] = [];
|
|
334
352
|
const pages = image.state?.pages;
|
|
335
|
-
if (typeof pages !== "object" || pages === null) return memory;
|
|
336
353
|
STATE_PAGES.forEach((page, index) => {
|
|
337
|
-
const hex = (pages as Record<string, unknown>)[page];
|
|
354
|
+
const hex = typeof pages === "object" && pages !== null ? (pages as Record<string, unknown>)[page] : undefined;
|
|
338
355
|
if (typeof hex !== "string" || !/^(?:[0-9a-fA-F]{2}){1,256}$/.test(hex)) return;
|
|
339
|
-
Buffer.from(hex, "hex")
|
|
356
|
+
const bytes = Buffer.from(hex, "hex");
|
|
357
|
+
bytes.forEach((byte, offset) => {
|
|
340
358
|
const address = index * 0x100 + offset;
|
|
341
359
|
if (address >= 2 && !inRanges(image.segments, address)) memory[address] = byte;
|
|
342
360
|
});
|
|
361
|
+
if (index * 0x100 + bytes.length > 2) fromState.push({ start: Math.max(index * 0x100, 2), end: index * 0x100 + bytes.length - 1 });
|
|
343
362
|
});
|
|
344
|
-
return memory;
|
|
363
|
+
return { segments: mergeRanges([...image.segments, ...fromState]), fromState: mergeRanges(fromState), memory, byteAt: (address) => memory[address]! };
|
|
345
364
|
}
|
|
346
365
|
|
|
347
366
|
/**
|
|
@@ -354,7 +373,7 @@ export function analyzedBytes(image: ProgramImage): { bytes: Buffer; kind: "prg"
|
|
|
354
373
|
const [segment] = image.segments;
|
|
355
374
|
return { bytes: prgOf(segment!.start, image.memory.subarray(segment!.start, segment!.end + 1)), kind: "prg" };
|
|
356
375
|
}
|
|
357
|
-
return { bytes: Buffer.from(entryMemory(image)), kind: "flat64k" };
|
|
376
|
+
return { bytes: Buffer.from(entryMemory(image).memory), kind: "flat64k" };
|
|
358
377
|
}
|
|
359
378
|
|
|
360
379
|
/**
|
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
// the colour RAM of the capture gives the colours. Thus the start screen of a
|
|
4
4
|
// stage needs no address from the user.
|
|
5
5
|
|
|
6
|
-
import {
|
|
6
|
+
import { formatC64Byte, vicScreenMemory } from "../c64.ts";
|
|
7
7
|
import { Refusal } from "../errors.ts";
|
|
8
8
|
import { LEFTOVERS_SWITCH, START_SCREEN_SWITCH, type StartScreenMode } from "./schema.ts";
|
|
9
9
|
|
|
@@ -70,18 +70,15 @@ function vicScreen(state: Record<string, unknown> | undefined, named: Named): Om
|
|
|
70
70
|
const vic = ioBytes(state, "vic");
|
|
71
71
|
const cia2 = ioBytes(state, "cia2");
|
|
72
72
|
if (state === undefined || vic === undefined || vic.length < 0x2f || cia2 === undefined) throw new Refusal("not-found", `${named.program} has no entry state with the VIC-II and CIA 2. ${named.again}`);
|
|
73
|
-
const [d011, d016
|
|
73
|
+
const [d011, d016] = [vic[0x11]!, vic[0x16]!];
|
|
74
74
|
if ((d011 & 0x10) === 0) throw new Refusal("invalid-input", `${named.capture} has the display off ($d011 = ${formatC64Byte(d011)}). ${named.again}`);
|
|
75
75
|
if ((d011 & 0x40) !== 0) throw new Refusal("invalid-input", `${named.capture} shows a screen in the extended colour mode, which a start screen does not have.`);
|
|
76
76
|
const bitmap = (d011 & 0x20) !== 0;
|
|
77
77
|
const multicolour = (d016 & 0x10) !== 0;
|
|
78
78
|
if (!bitmap && multicolour) throw new Refusal("invalid-input", `${named.capture} shows a multicolour text screen, which a start screen does not have.`);
|
|
79
|
-
const bank = vicBankBase(cia2[0]!, cia2[2]);
|
|
80
|
-
const offsets = d018Offsets(d018);
|
|
81
79
|
return {
|
|
82
80
|
mode: bitmap ? (multicolour ? "multicolour" : "hires") : "text",
|
|
83
|
-
|
|
84
|
-
...(bitmap ? { bitmap: bank + offsets.bitmap } : { charset: bank + offsets.characters }),
|
|
81
|
+
...vicScreenMemory(vic, cia2),
|
|
85
82
|
border: vic[0x20]! & 0x0f,
|
|
86
83
|
background: vic[0x21]! & 0x0f,
|
|
87
84
|
};
|
package/src/knowledge/write.ts
CHANGED
|
@@ -1003,7 +1003,8 @@ function unswitchStep(db: DatabaseSync, change: Change, request: InSpace & { nam
|
|
|
1003
1003
|
* A release. `rows` are the bytes of a release other than the base release,
|
|
1004
1004
|
* as memoryRows gives them: the address of each row and its bytes as hex. The
|
|
1005
1005
|
* base release has no bytes of its own: its bytes are the program. `file` and
|
|
1006
|
-
* `sha256` are the project file
|
|
1006
|
+
* `sha256` are the project file of the release and its SHA-256: the PRG that
|
|
1007
|
+
* the bytes come from, or for a capture the program file that the capture ran.
|
|
1007
1008
|
*/
|
|
1008
1009
|
type ReleaseRequest = InSpace & { label: string; base?: boolean; file?: string; sha256?: string; comment?: string; rows?: ReadonlyMap<number, string> };
|
|
1009
1010
|
|
package/src/opcodes.ts
CHANGED
|
@@ -107,6 +107,11 @@ export function accessedValue(opcode: number, before: { a: number; x: number; y:
|
|
|
107
107
|
}
|
|
108
108
|
}
|
|
109
109
|
|
|
110
|
+
/** True for a store, documented or undocumented: it writes its operand address and uses no byte there. An indexed store also makes a dummy read there first. */
|
|
111
|
+
export function isStore(opcode: number): boolean {
|
|
112
|
+
return WRITES.has(OPCODES[opcode]!.mnemonic);
|
|
113
|
+
}
|
|
114
|
+
|
|
110
115
|
/** The use of the operand address of an instruction that takes an address. Through (zp),y and (zp,x) the address is a pointer. */
|
|
111
116
|
export function useKind(opcode: number): UseKind {
|
|
112
117
|
const { mnemonic, mode } = OPCODES[opcode]!;
|