@henols/c64-re-tools 2.8.0-rc.3 → 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/SKILL.md +2 -2
- 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 +10 -9
- 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 +25 -5
- package/skills/c64-unpacker/scripts/unpacker.ts +32 -10
- 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 +21 -7
- 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/host/vice/chips.ts
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
// bank without side effects; the SID's write-only registers read back as
|
|
4
4
|
// last written.
|
|
5
5
|
|
|
6
|
-
import {
|
|
6
|
+
import { spriteData, vicScreenMemory } from "../../c64.ts";
|
|
7
7
|
import type { CiaState, SidState, SpriteState, ViciiMode, ViciiState } from "../../protocol/vice.ts";
|
|
8
8
|
|
|
9
9
|
export function viciiMode(d011: number, d016: number): ViciiMode {
|
|
@@ -20,15 +20,12 @@ export function viciiMode(d011: number, d016: number): ViciiMode {
|
|
|
20
20
|
export function decodeVicii(vic: Uint8Array, cia2: Uint8Array): ViciiState {
|
|
21
21
|
const d011 = vic[0x11]!;
|
|
22
22
|
const d016 = vic[0x16]!;
|
|
23
|
-
const
|
|
24
|
-
const mode = viciiMode(d011, d016);
|
|
25
|
-
const base = vicBankBase(cia2[0x00]!, cia2[0x02]!);
|
|
26
|
-
const bitmap = mode === "bitmap" || mode === "multicolor-bitmap";
|
|
23
|
+
const shown = vicScreenMemory(vic, cia2);
|
|
27
24
|
return {
|
|
28
25
|
rasterLine: ((d011 & 0x80) << 1) | vic[0x12]!,
|
|
29
|
-
mode,
|
|
30
|
-
screenAddress:
|
|
31
|
-
graphicsAddress:
|
|
26
|
+
mode: viciiMode(d011, d016),
|
|
27
|
+
screenAddress: shown.screen,
|
|
28
|
+
graphicsAddress: "bitmap" in shown ? shown.bitmap : shown.charset,
|
|
32
29
|
scrollX: d016 & 0x07,
|
|
33
30
|
scrollY: d011 & 0x07,
|
|
34
31
|
borderColor: vic[0x20]! & 0x0f,
|
|
@@ -49,7 +46,7 @@ export function decodeSprite(index: number, vic: Uint8Array, cia2: Uint8Array, p
|
|
|
49
46
|
expandX: bit(0x1d),
|
|
50
47
|
expandY: bit(0x17),
|
|
51
48
|
behindBackground: bit(0x1b),
|
|
52
|
-
dataAddress:
|
|
49
|
+
dataAddress: spriteData(cia2, pointers[index]!),
|
|
53
50
|
};
|
|
54
51
|
}
|
|
55
52
|
|
package/src/host/vice/session.ts
CHANGED
|
@@ -11,7 +11,7 @@ import { tmpdir } from "node:os";
|
|
|
11
11
|
import { join } from "node:path";
|
|
12
12
|
import { setTimeout as sleep } from "node:timers/promises";
|
|
13
13
|
|
|
14
|
-
import { formatC64Address, instructionLength } from "../../c64.ts";
|
|
14
|
+
import { formatC64Address, instructionLength, spritePointer } from "../../c64.ts";
|
|
15
15
|
import { matrixRows } from "../../c64-keys.ts";
|
|
16
16
|
import { encodePng, type IndexedFrame } from "../../graphics.ts";
|
|
17
17
|
import { accessedValue, instructionText, JAM_OPCODES, OPCODES, operandAddress } from "../../opcodes.ts";
|
|
@@ -1325,7 +1325,7 @@ export class ViceSession implements ViceSessionHandle {
|
|
|
1325
1325
|
const cia2 = await this.#machine.readIo(0xdd00, 0x10);
|
|
1326
1326
|
// Sprite pointers sit in the RAM the VIC-II reads, at the end of screen memory.
|
|
1327
1327
|
const screen = decodeVicii(vic, cia2).screenAddress;
|
|
1328
|
-
const pointers = Buffer.from(await this.#machine.readMemory({ address: screen
|
|
1328
|
+
const pointers = Buffer.from(await this.#machine.readMemory({ address: spritePointer(screen, 0), size: 8, space: "c64", view: "ram" }), "hex");
|
|
1329
1329
|
const wanted = indexes.length === 0 ? [0, 1, 2, 3, 4, 5, 6, 7] : indexes;
|
|
1330
1330
|
return wanted.map((index) => decodeSprite(index, vic, cia2, pointers));
|
|
1331
1331
|
}
|
package/src/host-client/image.ts
CHANGED
|
@@ -20,6 +20,8 @@ export interface Image {
|
|
|
20
20
|
state?: EntryState;
|
|
21
21
|
/** The stages of the capture in image.json, as the unpacker wrote them. */
|
|
22
22
|
stages?: Array<Record<string, unknown>>;
|
|
23
|
+
/** The PRG that the capture in image.json ran, as the project path that the capture got, and the SHA-256 of that file. */
|
|
24
|
+
program?: { file: string; sha256: string };
|
|
23
25
|
}
|
|
24
26
|
|
|
25
27
|
/** The kinds of media files that are no PRG, by extension. */
|
|
@@ -104,6 +106,11 @@ export function readImage(path: string): Image {
|
|
|
104
106
|
if (state !== undefined) image.state = state;
|
|
105
107
|
if (manifest.entry !== undefined) image.entry = address(manifest.entry, "entry");
|
|
106
108
|
if (Array.isArray(manifest.stages)) image.stages = manifest.stages.map((stage, index) => (isObject(stage) ? stage : fail(`stage ${index + 1} is not an object`)));
|
|
109
|
+
if (manifest.program !== undefined) {
|
|
110
|
+
const program = isObject(manifest.program) ? manifest.program : fail("its program is not an object");
|
|
111
|
+
if (typeof program.file !== "string" || program.file === "" || typeof program.sha256 !== "string" || !/^[0-9a-f]{64}$/.test(program.sha256)) fail("its program has no file and SHA-256");
|
|
112
|
+
image.program = { file: program.file as string, sha256: program.sha256 as string };
|
|
113
|
+
}
|
|
107
114
|
return image;
|
|
108
115
|
}
|
|
109
116
|
|
package/src/knowledge/import.ts
CHANGED
|
@@ -12,7 +12,7 @@
|
|
|
12
12
|
|
|
13
13
|
import type { DatabaseSync } from "node:sqlite";
|
|
14
14
|
|
|
15
|
-
import { type AddressRange, formatC64Address, formatC64Range, mergeRanges, rangesOverlap, type ReferenceKind, REFERENCE_KINDS, subtract, SYMBOL_NAME } from "../c64.ts";
|
|
15
|
+
import { type AddressRange, formatC64Address, formatC64Range, inRanges, mergeRanges, rangesOverlap, type ReferenceKind, REFERENCE_KINDS, subtract, SYMBOL_NAME } from "../c64.ts";
|
|
16
16
|
import { Refusal } from "../errors.ts";
|
|
17
17
|
import { transaction } from "./database.ts";
|
|
18
18
|
import { analyzerNamed, FACTS, referenceSuppressed, regionsOverlapping, suppressionsOverlapping, symbolAt, type ReferenceRow, type RegionRow, type SymbolRow } from "./read.ts";
|
|
@@ -96,7 +96,10 @@ export interface ImportResult {
|
|
|
96
96
|
revision: string | null;
|
|
97
97
|
/** The analyzer names. */
|
|
98
98
|
names: ImportCounts;
|
|
99
|
-
regions: ImportCounts
|
|
99
|
+
regions: ImportCounts & {
|
|
100
|
+
/** Data findings on executed code that the knowledge holds as code: not written, and no conflict. */
|
|
101
|
+
onExecutedCode: number;
|
|
102
|
+
};
|
|
100
103
|
references: ImportCounts & {
|
|
101
104
|
/** Findings from an address in a data region of the user, the LLM or a heuristic: false code, not written. */
|
|
102
105
|
inData: number;
|
|
@@ -180,7 +183,8 @@ function ownNames(db: DatabaseSync, analyzer: Analyzer, range: AddressRange, spa
|
|
|
180
183
|
* one for each analyzer at an address, and never to symbols. A run replaces
|
|
181
184
|
* the analyzer's own names in its coverage. A name that the user or the LLM
|
|
182
185
|
* gave the address already is not kept twice. A code or data kind that
|
|
183
|
-
* disagrees with the name of the address is a conflict
|
|
186
|
+
* disagrees with the name of the address is a conflict, except on dual-use
|
|
187
|
+
* bytes: they are code at one time and data at another.
|
|
184
188
|
*/
|
|
185
189
|
function importNames(db: DatabaseSync, change: Change, findings: NormalizedFindings, coverage: AddressRange[], result: ImportResult): void {
|
|
186
190
|
const { analyzer } = findings;
|
|
@@ -203,9 +207,10 @@ function importNames(db: DatabaseSync, change: Change, findings: NormalizedFindi
|
|
|
203
207
|
else replaced.add(own.address);
|
|
204
208
|
}
|
|
205
209
|
}
|
|
210
|
+
const dualUse = (address: number) => regionsOverlapping(db, address, address, space).some((row) => row.type === "dual-use");
|
|
206
211
|
for (const finding of findings.symbols) {
|
|
207
212
|
const current = symbolAt(db, finding.address, space);
|
|
208
|
-
if (current !== undefined && isCode(current.kind) !== isCode(finding.kind)) {
|
|
213
|
+
if (current !== undefined && isCode(current.kind) !== isCode(finding.kind) && !dualUse(finding.address)) {
|
|
209
214
|
result.conflicts.push({ category: "symbol", address: finding.address, reason: "kind", current, finding: { name: finding.name, kind: finding.kind } });
|
|
210
215
|
}
|
|
211
216
|
if (settled.has(finding.address)) continue;
|
|
@@ -270,12 +275,21 @@ function contradictedData(db: DatabaseSync, findings: NormalizedFindings, space:
|
|
|
270
275
|
return found;
|
|
271
276
|
}
|
|
272
277
|
|
|
278
|
+
/** True when the knowledge holds each executed instruction start in the range in a code or dual-use region. */
|
|
279
|
+
function executedIsCode(db: DatabaseSync, findings: NormalizedFindings, range: AddressRange, space: Space): boolean {
|
|
280
|
+
const code = regionsOverlapping(db, range.start, range.end, space).filter((row) => isCode(row.type) || row.type === "dual-use");
|
|
281
|
+
for (let address = range.start; address <= range.end; address++) if (findings.executed!(address) && !inRanges(code, address)) return false;
|
|
282
|
+
return true;
|
|
283
|
+
}
|
|
284
|
+
|
|
273
285
|
function importRegions(db: DatabaseSync, change: Change, given: NormalizedFindings, coverage: AddressRange[], result: ImportResult): void {
|
|
274
286
|
const { analyzer } = given;
|
|
275
287
|
const space = given.space ?? DEFAULT_SPACE;
|
|
276
|
-
// Data findings over code evidence are conflicts, not facts.
|
|
288
|
+
// Data findings over code evidence are conflicts, not facts. Where a run executed the code and the knowledge holds it as code, nothing is left to decide: the finding is only counted.
|
|
277
289
|
const contradicted = contradictedData(db, given, space);
|
|
278
|
-
|
|
290
|
+
const decided = contradicted.filter((item) => item.executed !== undefined && executedIsCode(db, given, item, space));
|
|
291
|
+
result.regions.onExecutedCode = decided.length;
|
|
292
|
+
result.conflicts.push(...contradicted.filter((item) => !decided.includes(item)));
|
|
279
293
|
const findings = { ...given, regions: given.regions.filter((region) => !contradicted.some((item) => item.start === region.start && item.end === region.end)) };
|
|
280
294
|
if (analyzer === "heuristic") {
|
|
281
295
|
// An idiom is specific evidence about its bytes: the DXA or decompiler region there gives way, its parts outside stay.
|
|
@@ -450,7 +464,7 @@ export function importFindings(db: DatabaseSync, findings: NormalizedFindings, c
|
|
|
450
464
|
const coverage = validateFindings(findings);
|
|
451
465
|
return transaction(db, () => {
|
|
452
466
|
const change = new Change(db, { origin: findings.analyzer, ...context }, `import-${findings.analyzer}`);
|
|
453
|
-
const result: ImportResult = { revision: null, names: counts(), regions: counts(), references: { ...counts(), inData: 0 }, conflicts: [] };
|
|
467
|
+
const result: ImportResult = { revision: null, names: counts(), regions: { ...counts(), onExecutedCode: 0 }, references: { ...counts(), inData: 0 }, conflicts: [] };
|
|
454
468
|
importNames(db, change, findings, coverage, result);
|
|
455
469
|
importRegions(db, change, findings, coverage, result);
|
|
456
470
|
importReferences(db, change, findings, coverage, result);
|
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]!;
|