@henols/c64-re-tools 2.6.0-rc.2 → 2.6.0-rc.20
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 +2 -2
- package/skills/c64-assembler/SKILL.md +28 -14
- package/skills/c64-assembler/scripts/assembler.ts +28 -15
- package/skills/c64-development/SKILL.md +4 -4
- package/skills/c64-docs/SKILL.md +9 -7
- package/skills/c64-docs/scripts/docs.ts +24 -11
- package/skills/c64-docs/scripts/lib/reference.ts +119 -33
- package/skills/c64-docs/scripts/lib/source-map.ts +23 -4
- package/skills/c64-knowledge/SKILL.md +67 -42
- package/skills/c64-knowledge/scripts/knowledge.ts +90 -67
- package/skills/c64-knowledge/scripts/lib/listing.ts +18 -3
- package/skills/c64-packer/SKILL.md +1 -1
- package/skills/c64-provenance/SKILL.md +11 -10
- package/skills/c64-provenance/scripts/lib/library.ts +16 -16
- package/skills/c64-provenance/scripts/provenance.ts +33 -23
- package/skills/c64-reverse-engineering/SKILL.md +17 -11
- package/skills/c64-source/SKILL.md +71 -47
- package/skills/c64-source/scripts/lib/export.ts +89 -34
- package/skills/c64-source/scripts/lib/loader.ts +79 -36
- package/skills/c64-source/scripts/lib/source.ts +38 -32
- package/skills/c64-source/scripts/lib/stage.ts +11 -5
- package/skills/c64-source/scripts/lib/start-screen.asm +4 -2
- package/skills/c64-source/scripts/source.ts +278 -145
- package/skills/c64-static-analysis/SKILL.md +29 -25
- package/skills/c64-static-analysis/scripts/lib/coverage.ts +2 -25
- package/skills/c64-static-analysis/scripts/lib/findings.ts +11 -11
- package/skills/c64-static-analysis/scripts/lib/instructions.ts +26 -24
- package/skills/c64-static-analysis/scripts/lib/macros.ts +3 -1
- package/skills/c64-static-analysis/scripts/static-analysis.ts +69 -67
- package/skills/c64-testing/SKILL.md +15 -5
- package/skills/c64-testing/references/recording-format.md +26 -5
- package/skills/c64-testing/references/scenario-format.md +11 -8
- package/skills/c64-testing/scripts/lib/diff-files.ts +4 -18
- package/skills/c64-testing/scripts/lib/play.ts +39 -41
- package/skills/c64-testing/scripts/lib/recording.ts +220 -46
- package/skills/c64-testing/scripts/lib/replay.ts +32 -6
- package/skills/c64-testing/scripts/lib/runs.ts +8 -3
- package/skills/c64-testing/scripts/lib/scenario.ts +18 -11
- package/skills/c64-testing/scripts/testing.ts +23 -2
- package/skills/c64-unpacker/SKILL.md +10 -5
- package/skills/c64-unpacker/scripts/lib/loads.ts +34 -13
- package/skills/c64-unpacker/scripts/lib/machine.ts +7 -3
- package/skills/c64-unpacker/scripts/lib/stages.ts +18 -3
- package/skills/c64-unpacker/scripts/unpacker.ts +43 -9
- package/skills/emulator/SKILL.md +7 -12
- package/src/build-name.ts +55 -0
- package/src/c64-keys.ts +98 -0
- package/src/c64.ts +22 -2
- package/src/cli/main.ts +3 -29
- package/src/host/main.ts +3 -3
- package/src/host/server.ts +3 -1
- package/src/host/tools/index.ts +2 -7
- package/src/host/vice/process.ts +34 -17
- package/src/host/vice/session.ts +138 -31
- package/src/host-client/steps.ts +21 -18
- package/src/host-client/tools.ts +2 -7
- package/src/host-client/vice-session.ts +7 -1
- package/src/knowledge/evidence.ts +66 -0
- package/src/knowledge/history.ts +5 -5
- package/src/knowledge/import.ts +12 -5
- package/src/knowledge/program.ts +123 -32
- package/src/knowledge/read.ts +36 -13
- package/src/knowledge/schema.ts +43 -17
- package/src/knowledge/start-screen.ts +70 -0
- package/src/knowledge/text.ts +33 -5
- package/src/knowledge/write.ts +75 -36
- package/src/mcp/inputs.ts +12 -0
- package/src/mcp/server.ts +1 -1
- package/src/mcp/tools/debug.ts +23 -21
- package/src/mcp/tools/input.ts +38 -5
- package/src/mcp/tools/machine.ts +3 -42
- package/src/mcp/tools/media.ts +1 -1
- package/src/mcp/tools/memory.ts +12 -9
- package/src/native/acme.ts +1 -1
- package/src/native/c64run.ts +7 -4
- package/src/native/decompiler/analyze.ts +98 -0
- package/src/native/decompiler/c64rt-wasm.ts +4 -0
- package/src/native/decompiler/language/c64rt_6510.sla +0 -0
- package/src/native/{ghidra → decompiler}/language/c64rt_6510.slaspec +1 -1
- package/src/native/decompiler/pcode.ts +222 -0
- package/src/native/decompiler/walk.ts +205 -0
- package/src/native/decompiler/wasm.ts +98 -0
- package/src/native/discover.ts +3 -3
- package/src/native/dxa.ts +12 -1
- package/src/native/processes.ts +4 -4
- package/src/native/types.ts +27 -27
- package/src/opcodes.ts +1 -1
- package/src/platform.ts +30 -1
- package/src/project.ts +17 -4
- package/src/protocol/tools.ts +1 -45
- package/src/protocol/vice.ts +47 -0
- package/src/reference-pages.ts +64 -0
- package/src/script.ts +4 -4
- package/src/vsf.ts +98 -0
- package/tools/c64run.c +158 -15
- package/tools/install-container.sh +1 -2
- package/src/cli/status.ts +0 -127
- package/src/host/tools/status.ts +0 -47
- package/src/native/ghidra/analyze.ts +0 -87
- package/src/native/ghidra/index.ts +0 -170
- package/src/native/ghidra/scripts/C64Export.java +0 -274
- package/src/native/ghidra/scripts/C64Prepare.java +0 -66
- package/src/native/status.ts +0 -135
- package/tools/install-ghidra.sh +0 -40
- /package/src/native/{ghidra → decompiler}/language/c64rt_6510.cspec +0 -0
- /package/src/native/{ghidra → decompiler}/language/c64rt_6510.ldefs +0 -0
- /package/src/native/{ghidra → decompiler}/language/c64rt_6510.pspec +0 -0
|
@@ -10,6 +10,7 @@
|
|
|
10
10
|
// tells what stays. Without coverage nothing becomes free by itself.
|
|
11
11
|
|
|
12
12
|
import { type AddressRange, COVERAGE, formatC64Address, pointerTableEntries, POINTER_TABLE_TYPES, type PointerTableType } from "#src/c64.ts";
|
|
13
|
+
import { symbolsPath } from "#src/build-name.ts";
|
|
13
14
|
import { Refusal } from "#src/errors.ts";
|
|
14
15
|
import { NO_FALL_THROUGH, type Root, separate } from "#src/knowledge/separate.ts";
|
|
15
16
|
import type { RegionType } from "#src/knowledge/schema.ts";
|
|
@@ -22,14 +23,35 @@ import { OPCODES } from "#src/opcodes.ts";
|
|
|
22
23
|
*/
|
|
23
24
|
export const DROPPABLE_DATA: readonly RegionType[] = ["bytes", "words", ...POINTER_TABLE_TYPES, "petscii", "workspace", "unused"];
|
|
24
25
|
|
|
26
|
+
/** The data that the VIC-II reads: a region of another type that overlaps it stays too, for example unused bytes in a bitmap. */
|
|
27
|
+
const VIC_DATA: readonly RegionType[] = ["charset", "sprite", "bitmap", "screen"];
|
|
28
|
+
|
|
25
29
|
/** Regions of code: their bytes are the blocks of the decoded instructions. */
|
|
26
30
|
const CODE_TYPES: readonly RegionType[] = ["code", "jumps", "dual-use"];
|
|
27
31
|
|
|
28
32
|
/** The hardware vectors $fffa-$ffff: they stay when the image holds them. */
|
|
29
33
|
const VECTORS: AddressRange = { start: 0xfffa, end: 0xffff };
|
|
30
34
|
|
|
31
|
-
/**
|
|
32
|
-
|
|
35
|
+
/**
|
|
36
|
+
* The coverage flags that can be a use of the RAM byte: a read or a run of the
|
|
37
|
+
* ROM uses the ROM, not the RAM under it, so a JSR into the KERNAL keeps no byte
|
|
38
|
+
* of the image. The I/O flags stay, as VICE gives each access at $d000-$dfff as I/O.
|
|
39
|
+
*/
|
|
40
|
+
const RAM_ACCESS = 0xff & ~(COVERAGE.romRead | COVERAGE.romExecute);
|
|
41
|
+
|
|
42
|
+
/** The I/O area: two immediates stored there are chip values, such as a timer latch, not an address. */
|
|
43
|
+
const IO_AREA: AddressRange = { start: 0xd000, end: 0xdfff };
|
|
44
|
+
|
|
45
|
+
/** The registers that each instruction changes, other than by a load of an immediate or a transfer. */
|
|
46
|
+
const WRITES_A = new Set(["lda", "adc", "sbc", "and", "ora", "eor", "pla", "lax", "anc", "asr", "arr", "ane", "lxa", "las", "slo", "rla", "sre", "rra", "isc"]);
|
|
47
|
+
const WRITES_X = new Set(["ldx", "inx", "dex", "tsx", "lax", "sbx", "lxa", "las"]);
|
|
48
|
+
const WRITES_Y = new Set(["ldy", "iny", "dey"]);
|
|
49
|
+
/** The registers that a JSR gets an address in, low byte first, as the KERNAL and BASIC routines get a pointer. */
|
|
50
|
+
const CALL_PAIRS = [
|
|
51
|
+
["x", "y"],
|
|
52
|
+
["a", "y"],
|
|
53
|
+
["a", "x"],
|
|
54
|
+
] as const;
|
|
33
55
|
|
|
34
56
|
export interface DropInput {
|
|
35
57
|
segments: readonly AddressRange[];
|
|
@@ -41,8 +63,12 @@ export interface DropInput {
|
|
|
41
63
|
coverage?: Uint8Array;
|
|
42
64
|
regions: ReadonlyArray<{ start: number; end: number; type: RegionType }>;
|
|
43
65
|
references: ReadonlyArray<{ from: number; to: number; kind: string }>;
|
|
44
|
-
/** The addresses that the operand expression of an instruction names, by the address of
|
|
66
|
+
/** The addresses that the operand expression of an instruction names, by the address of each instruction with an operand expression; none for a constant. */
|
|
45
67
|
operandTargets: ReadonlyMap<number, readonly number[]>;
|
|
68
|
+
/** The ranges where the CPU sees ROM and not the RAM of the image: code does not go there, and no address points there. */
|
|
69
|
+
rom?: readonly AddressRange[];
|
|
70
|
+
/** The BASIC line that starts the program: RUN reads it, which no coverage from the entry shows. */
|
|
71
|
+
basic?: AddressRange;
|
|
46
72
|
/** The patch ranges whose bytes the export changed. */
|
|
47
73
|
changed: readonly AddressRange[];
|
|
48
74
|
keep: readonly AddressRange[];
|
|
@@ -115,42 +141,72 @@ function flowsOf(input: DropInput, inImage: (address: number) => boolean, indexe
|
|
|
115
141
|
const reads: DataReference[] = [];
|
|
116
142
|
const { memory } = input;
|
|
117
143
|
const word = (at: number) => memory[at]! | (memory[(at + 1) & 0xffff]! << 8);
|
|
118
|
-
const
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
});
|
|
131
|
-
immediates.length = 0;
|
|
144
|
+
const rom = (address: number) => inside(address, input.rom ?? []);
|
|
145
|
+
// The address of a vector, a table or an RTS target in two immediates of one block: the low byte and the high byte
|
|
146
|
+
// go into a pointer (two stores to N and N+1), into the registers of a JSR, or onto the stack (PHA high, PHA low).
|
|
147
|
+
type Immediate = { at: number; value: number } | undefined;
|
|
148
|
+
const registers: { a: Immediate; x: Immediate; y: Immediate } = { a: undefined, x: undefined, y: undefined };
|
|
149
|
+
const stores = new Map<number, NonNullable<Immediate>>();
|
|
150
|
+
const pushes: Immediate[] = [];
|
|
151
|
+
const pair = (low: Immediate, high: Immediate) => {
|
|
152
|
+
if (low === undefined || high === undefined) return;
|
|
153
|
+
const target = low.value | (high.value << 8);
|
|
154
|
+
if (!inImage(target) || rom(target)) return;
|
|
155
|
+
flows.push({ from: low.at, to: target }, { from: low.at, to: (target + 1) & 0xffff });
|
|
156
|
+
reads.push({ from: low.at, start: target, end: target });
|
|
132
157
|
};
|
|
158
|
+
const endBlock = () => {
|
|
159
|
+
registers.a = registers.x = registers.y = undefined;
|
|
160
|
+
stores.clear();
|
|
161
|
+
pushes.length = 0;
|
|
162
|
+
};
|
|
163
|
+
let previousEnd = -2;
|
|
133
164
|
for (const at of [...input.instructions.keys()].sort((a, b) => a - b)) {
|
|
134
165
|
const opcode = memory[at]!;
|
|
135
|
-
const { mode } = OPCODES[opcode]!;
|
|
136
|
-
if (at > previousEnd + 1)
|
|
166
|
+
const { mnemonic, mode } = OPCODES[opcode]!;
|
|
167
|
+
if (at > previousEnd + 1) endBlock();
|
|
137
168
|
previousEnd = at + input.instructions.get(at)! - 1;
|
|
138
169
|
const operand = input.instructions.get(at) === 3 ? word(at + 1) : memory[at + 1]!;
|
|
170
|
+
// The registers: a load of an immediate that no operand expression explains, a transfer, a store, a push, a call.
|
|
171
|
+
const loaded = mode === "imm" && !input.operandTargets.has(at) ? { at, value: operand } : undefined;
|
|
172
|
+
if (WRITES_A.has(mnemonic) || (mode === "imp" && ["asl", "lsr", "rol", "ror"].includes(mnemonic))) registers.a = mnemonic === "lda" ? loaded : undefined;
|
|
173
|
+
if (WRITES_X.has(mnemonic)) registers.x = mnemonic === "ldx" ? loaded : undefined;
|
|
174
|
+
if (WRITES_Y.has(mnemonic)) registers.y = mnemonic === "ldy" ? loaded : undefined;
|
|
175
|
+
if (mnemonic === "tax") registers.x = registers.a;
|
|
176
|
+
if (mnemonic === "tay") registers.y = registers.a;
|
|
177
|
+
if (mnemonic === "txa") registers.a = registers.x;
|
|
178
|
+
if (mnemonic === "tya") registers.a = registers.y;
|
|
179
|
+
const stored = { sta: registers.a, stx: registers.x, sty: registers.y }[mnemonic];
|
|
180
|
+
if ((mode === "zp" || mode === "abs") && ["sta", "stx", "sty"].includes(mnemonic) && !inside(operand, [IO_AREA])) {
|
|
181
|
+
stores.delete(operand);
|
|
182
|
+
if (stored !== undefined) {
|
|
183
|
+
stores.set(operand, stored);
|
|
184
|
+
pair(stored, stores.get((operand + 1) & 0xffff));
|
|
185
|
+
pair(stores.get((operand - 1) & 0xffff), stored);
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
if (mnemonic === "pha") {
|
|
189
|
+
pushes.push(registers.a);
|
|
190
|
+
if (pushes.length >= 2) pair(pushes.at(-1), pushes.at(-2));
|
|
191
|
+
}
|
|
192
|
+
if (mnemonic === "pla") pushes.pop();
|
|
193
|
+
if (mnemonic === "jsr") {
|
|
194
|
+
for (const [low, high] of CALL_PAIRS) pair(registers[low], registers[high]);
|
|
195
|
+
registers.a = registers.x = registers.y = undefined;
|
|
196
|
+
}
|
|
139
197
|
switch (mode) {
|
|
140
198
|
case "rel":
|
|
141
199
|
flows.push({ from: at, to: (at + 2 + (operand < 0x80 ? operand : operand - 0x100)) & 0xffff });
|
|
142
200
|
break;
|
|
143
|
-
case "imm":
|
|
144
|
-
immediates.push({ at, value: operand });
|
|
145
|
-
break;
|
|
146
201
|
case "ind":
|
|
147
202
|
reads.push({ from: at, start: operand, end: (operand + 1) & 0xffff });
|
|
148
203
|
if (inImage(operand) && inImage((operand + 1) & 0xffff)) flows.push({ from: at, to: word(operand) });
|
|
149
204
|
break;
|
|
150
205
|
case "abs":
|
|
151
206
|
case "zp":
|
|
152
|
-
if (opcode === 0x20 || opcode === 0x4c)
|
|
153
|
-
|
|
207
|
+
if (opcode === 0x20 || opcode === 0x4c) {
|
|
208
|
+
if (!rom(operand)) flows.push({ from: at, to: operand });
|
|
209
|
+
} else reads.push({ from: at, start: operand, end: operand });
|
|
154
210
|
break;
|
|
155
211
|
case "abx":
|
|
156
212
|
case "aby":
|
|
@@ -165,9 +221,8 @@ function flowsOf(input: DropInput, inImage: (address: number) => boolean, indexe
|
|
|
165
221
|
default:
|
|
166
222
|
break;
|
|
167
223
|
}
|
|
168
|
-
if (NO_FALL_THROUGH.has(opcode))
|
|
224
|
+
if (NO_FALL_THROUGH.has(opcode)) endBlock();
|
|
169
225
|
}
|
|
170
|
-
pairs();
|
|
171
226
|
for (const reference of input.references) {
|
|
172
227
|
if (inside(reference.from, input.changed)) continue;
|
|
173
228
|
if (reference.kind === "call" || reference.kind === "jump" || reference.kind === "pointer") flows.push({ from: reference.from, to: reference.to });
|
|
@@ -196,10 +251,10 @@ const isTable = (type: RegionType) => type === "jumps" || (POINTER_TABLE_TYPES a
|
|
|
196
251
|
*/
|
|
197
252
|
export function planDrops(input: DropInput): DropPlan {
|
|
198
253
|
const inImage = (address: number) => inside(address, input.segments);
|
|
199
|
-
const touched = (address: number) => input.coverage !== undefined && input.coverage[address] !== 0;
|
|
254
|
+
const touched = (address: number) => input.coverage !== undefined && (input.coverage[address]! & RAM_ACCESS) !== 0;
|
|
200
255
|
const executed = (address: number) => input.coverage !== undefined && (input.coverage[address]! & COVERAGE.ramExecute) !== 0;
|
|
201
256
|
const regions = input.regions.flatMap((region) => input.segments.filter((segment) => overlaps(region, segment)).map((segment) => ({ start: Math.max(region.start, segment.start), end: Math.min(region.end, segment.end), type: region.type })));
|
|
202
|
-
// The source writes a data region as data, also where
|
|
257
|
+
// The source writes a data region as data, also where the decompiler decoded instructions in it: those are no code.
|
|
203
258
|
const instructions = new Map([...input.instructions].filter(([start, length]) => !regions.some((region) => !CODE_TYPES.includes(region.type) && start <= region.end && region.start <= start + length - 1)));
|
|
204
259
|
const blocks = blocksOf(instructions, input.memory);
|
|
205
260
|
// An indexed operand reads in the region that holds it, and in the next region when it is the last byte (table-1,x).
|
|
@@ -275,11 +330,11 @@ export function planDrops(input: DropInput): DropPlan {
|
|
|
275
330
|
|
|
276
331
|
// Data: a region of a droppable type that no run touched and that nothing that stays names.
|
|
277
332
|
const gone = [...input.drop, ...droppedCode];
|
|
278
|
-
const imageStart = Math.min(...input.segments.map((segment) => segment.start));
|
|
279
333
|
const units = automatic
|
|
280
334
|
? regions.filter((region) => {
|
|
281
335
|
if (!DROPPABLE_DATA.includes(region.type) || gone.some((range) => overlaps(range, region))) return false;
|
|
282
|
-
if (
|
|
336
|
+
if (regions.some((other) => VIC_DATA.includes(other.type) && overlaps(other, region))) return false;
|
|
337
|
+
if ([...input.keep, ...input.changed, VECTORS, ...(input.basic === undefined ? [] : [input.basic])].some((range) => overlaps(range, region))) return false;
|
|
283
338
|
for (let address = region.start; address <= region.end; address++) if (touched(address)) return false;
|
|
284
339
|
return true;
|
|
285
340
|
})
|
|
@@ -377,7 +432,7 @@ export function rewriteTest(text: string, name: string, symbols: ReadonlyMap<str
|
|
|
377
432
|
if (inside(address, free)) return { reason: `it names ${location}, which is in a free range` };
|
|
378
433
|
} else if (!symbols.has(location)) return { reason: `it names ${location}, which the exported program does not have` };
|
|
379
434
|
}
|
|
380
|
-
const side = (program: string) => ({ program: `${program}.prg`, symbols: `${program}.
|
|
435
|
+
const side = (program: string) => ({ program: `${program}.prg`, symbols: symbolsPath(`${program}.prg`) });
|
|
381
436
|
if (scenario) {
|
|
382
437
|
// Both sides are the exported program now: a step of the original alone (for example a key for its intro) goes, and a step of the rebuild alone runs on both sides.
|
|
383
438
|
const { original: _original, rebuild: _rebuild, steps, ...rest } = test;
|
|
@@ -389,7 +444,7 @@ export function rewriteTest(text: string, name: string, symbols: ReadonlyMap<str
|
|
|
389
444
|
return { kind: "scenario", text: `${JSON.stringify({ ...rest, original: side(`baseline/${name}`), rebuild: side(`build/${name}`), steps: both }, null, 2)}\n` };
|
|
390
445
|
}
|
|
391
446
|
const start = test.start as Record<string, unknown>;
|
|
392
|
-
const symbolsFile = test.symbols === undefined || test.symbols === "none" ? {} : { symbols: `build/${name}.
|
|
447
|
+
const symbolsFile = test.symbols === undefined || test.symbols === "none" ? {} : { symbols: symbolsPath(`build/${name}.prg`) };
|
|
393
448
|
return { kind: "run", text: `${JSON.stringify({ ...test, start: { ...start, program: `build/${name}.prg` }, ...symbolsFile }, null, 2)}\n` };
|
|
394
449
|
}
|
|
395
450
|
|
|
@@ -417,7 +472,7 @@ export function readmeText(input: ReadmeInput): string {
|
|
|
417
472
|
"",
|
|
418
473
|
"## Build",
|
|
419
474
|
"",
|
|
420
|
-
`Assemble \`src/${input.entryFile}\` to \`build/${name}.prg\`, with its symbols in
|
|
475
|
+
`Assemble \`src/${input.entryFile}\` to \`build/${name}.prg\`, with its symbols in \`${symbolsPath(`build/${name}.prg`)}\`.`,
|
|
421
476
|
input.entryFile === "main.asm"
|
|
422
477
|
? "Compare the build with the program in the project knowledge: the source must give that program."
|
|
423
478
|
: "Assemble `src/main.asm` to `.c64-re-tools/local/program.prg`, and compare it with the program in the project knowledge: the source must give that program.",
|
|
@@ -449,7 +504,7 @@ export function readmeText(input: ReadmeInput): string {
|
|
|
449
504
|
"## Files",
|
|
450
505
|
"",
|
|
451
506
|
`- \`src/\`: the source. You own it now: change it as you like.`,
|
|
452
|
-
`- \`baseline/${name}.prg\` and
|
|
507
|
+
`- \`baseline/${name}.prg\` and \`${symbolsPath(`baseline/${name}.prg`)}\`: the exported program and its symbols, the reference of the scenarios.`,
|
|
453
508
|
"- `tests/`: the tests.",
|
|
454
509
|
"- `.c64-re-tools/knowledge/`: the names, comments and memory map of the exported program. They do not follow your changes to the source.",
|
|
455
510
|
"",
|
|
@@ -11,19 +11,23 @@
|
|
|
11
11
|
// screen in the knowledge, the copier shows it after the copies and waits for
|
|
12
12
|
// a key or a fire button: the code is start-screen.asm, the same for each program.
|
|
13
13
|
// The loader loads the segments without the leftovers: the ranges of the
|
|
14
|
-
// source that nothing uses are not in loader.
|
|
14
|
+
// source that nothing uses are not in the loader program.
|
|
15
15
|
|
|
16
16
|
import { readFileSync } from "node:fs";
|
|
17
17
|
import { fileURLToPath } from "node:url";
|
|
18
18
|
|
|
19
|
+
import { COPIER_LINE } from "#src/build-name.ts";
|
|
19
20
|
import { type AddressRange, formatC64Address, formatC64Byte, prgRange } from "#src/c64.ts";
|
|
20
21
|
import { compareBuild, mergedPrograms } from "#src/compare.ts";
|
|
21
22
|
import { Refusal } from "#src/errors.ts";
|
|
22
23
|
import { parseEntryState } from "#src/host-client/entry-state.ts";
|
|
23
24
|
import { isRomFont, startScreenRanges, type StartScreenRow } from "#src/knowledge/read.ts";
|
|
25
|
+
import { LOADER_JUMP } from "#src/knowledge/start-screen.ts";
|
|
26
|
+
import { romUseOfPort } from "#src/platform.ts";
|
|
24
27
|
import { WireFailure } from "#src/host-client/tools.ts";
|
|
25
28
|
import { pack } from "#src/native/exomizer.ts";
|
|
26
29
|
import type { ToolContext } from "#src/native/run.ts";
|
|
30
|
+
import type { AssembledSymbol } from "#src/native/types.ts";
|
|
27
31
|
|
|
28
32
|
export interface EntryState {
|
|
29
33
|
pc: number;
|
|
@@ -136,11 +140,7 @@ export function loaderLimits(plan: { cia?: CiaPlan }): string[] {
|
|
|
136
140
|
return plan.cia.limits;
|
|
137
141
|
}
|
|
138
142
|
|
|
139
|
-
|
|
140
|
-
export const LOADER_JUMP = "c64_source_loader_jump";
|
|
141
|
-
/** The labels of the start screen: its wait for a key or a fire button, and the code after the wait. */
|
|
142
|
-
export const START_SCREEN_WAIT = "loader_start_screen_wait";
|
|
143
|
-
export const START_SCREEN_CONTINUE = "loader_start_screen_continue";
|
|
143
|
+
export { LOADER_JUMP, START_SCREEN_CONTINUE, START_SCREEN_WAIT } from "#src/knowledge/start-screen.ts";
|
|
144
144
|
/** The file of the start screen in the generated source. */
|
|
145
145
|
export const START_SCREEN_FILE = "start_screen.asm";
|
|
146
146
|
/** The code of the start screen: the same for each program. loader.asm sets its values. */
|
|
@@ -172,7 +172,7 @@ export function startScreenPlan(row: StartScreenRow): StartScreenPlan {
|
|
|
172
172
|
bank,
|
|
173
173
|
border: row.border,
|
|
174
174
|
background: row.background,
|
|
175
|
-
colourMode: row.colours !== undefined ? 2 : row.colour !== undefined ? 1 : 0,
|
|
175
|
+
colourMode: row.colours !== undefined || row.colourRam === true ? 2 : row.colour !== undefined ? 1 : 0,
|
|
176
176
|
};
|
|
177
177
|
}
|
|
178
178
|
|
|
@@ -191,7 +191,8 @@ export function startScreenValues(row: StartScreenRow, plan: StartScreenPlan): s
|
|
|
191
191
|
value("background", plan.background),
|
|
192
192
|
value("colour_mode", plan.colourMode),
|
|
193
193
|
value("colour", row.colour ?? 0),
|
|
194
|
-
|
|
194
|
+
// The colour RAM of a captured stage is in the show code: STAGE_COLOURS in stage.ts.
|
|
195
|
+
`c64_source_ss_colours = ${row.colourRam === true ? "c64_source_stage_colours" : formatC64Address(row.colours ?? 0)}`,
|
|
195
196
|
];
|
|
196
197
|
}
|
|
197
198
|
|
|
@@ -216,11 +217,12 @@ const COPY_ENTRY = 7;
|
|
|
216
217
|
const TABLE_COPIES = Math.floor((256 - 2) / COPY_ENTRY);
|
|
217
218
|
/** A loader that loads into $d000 or higher would write the I/O area. */
|
|
218
219
|
const LOAD_LIMIT = 0xcfff;
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
{
|
|
223
|
-
{
|
|
220
|
+
/** The pages where the plain copier can start, the first one that is free: then the RAM that a ROM can hide, as copierAreas allows. */
|
|
221
|
+
const COPIER_PAGES = [
|
|
222
|
+
...Array.from({ length: 15 }, (_, index) => 0xc000 + index * 0x100),
|
|
223
|
+
...Array.from({ length: 0x9b }, (_, index) => 0x9e00 - index * 0x100),
|
|
224
|
+
...Array.from({ length: 0x20 }, (_, index) => 0xa000 + index * 0x100),
|
|
225
|
+
...Array.from({ length: 0x20 }, (_, index) => 0xe000 + index * 0x100),
|
|
224
226
|
];
|
|
225
227
|
|
|
226
228
|
const overlaps = (a: AddressRange, b: AddressRange) => a.start <= b.end && b.start <= a.end;
|
|
@@ -564,13 +566,14 @@ export function planLoader(input: LoaderInput): LoaderPlan {
|
|
|
564
566
|
throw new WireFailure("invalid-input", `No copy order exists: each of these copies writes over the packed bytes of another one: ${clashing.join(", ")}. Write the loader by hand.`);
|
|
565
567
|
}
|
|
566
568
|
const loaded = { start: LOAD, end };
|
|
569
|
+
const areas = copierAreas(state.port);
|
|
567
570
|
const copier = COPIER_PAGES.find((page) => {
|
|
568
571
|
const pages = { start: page, end: page + copierBytes - 1 };
|
|
569
|
-
return
|
|
572
|
+
return areas.some((area) => pages.start >= area.start && pages.end <= area.end) && !overlaps(pages, loaded) && plain.every((copy) => !overlaps(pages, copy.target));
|
|
570
573
|
});
|
|
571
574
|
if (copier === undefined) {
|
|
572
575
|
const count = copierPages === 2 ? "two" : "three";
|
|
573
|
-
throw new WireFailure("invalid-input", `No ${count} free pages exist for the copier in $
|
|
576
|
+
throw new WireFailure("invalid-input", `No ${count} free pages exist for the copier in ${areas.map(show).join(" or ")} outside the loader (${show(loaded)}) and the targets: ${plain.map((copy) => show(copy.target)).join(", ")}.`);
|
|
574
577
|
}
|
|
575
578
|
const copies: Copy[] = order.map((index) => {
|
|
576
579
|
const copy = plain[index]!;
|
|
@@ -640,26 +643,38 @@ export function planLoader(input: LoaderInput): LoaderPlan {
|
|
|
640
643
|
};
|
|
641
644
|
}
|
|
642
645
|
|
|
643
|
-
/**
|
|
644
|
-
const
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
|
|
646
|
+
/** The hardware vectors at $fffa-$ffff: the copier and the pieces of pages 0-3 stay below them. */
|
|
647
|
+
const BELOW_VECTORS = 0xfff9;
|
|
648
|
+
/** Where the packed form puts the bytes of pages 0-3: the copier reads them with all RAM visible ($01 = $34). */
|
|
649
|
+
const PIECE_AREA: AddressRange = { start: 0x0400, end: BELOW_VECTORS };
|
|
650
|
+
|
|
651
|
+
/**
|
|
652
|
+
* Where a loader puts its copier: the copier sets $01 to $35 and then to the
|
|
653
|
+
* entry's value, so it is in RAM that both show. A port bit that is an input
|
|
654
|
+
* reads 1.
|
|
655
|
+
*/
|
|
656
|
+
export function copierAreas(port: readonly [number, number]): AddressRange[] {
|
|
657
|
+
const rom = romUseOfPort((port[1] | ~port[0]) & 0x07);
|
|
658
|
+
if (rom.basic) return [{ start: 0x0400, end: 0x9fff }, { start: 0xc000, end: 0xcfff }];
|
|
659
|
+
return [{ start: 0x0400, end: 0xcfff }, ...(rom.kernal ? [] : [{ start: 0xe000, end: BELOW_VECTORS }])];
|
|
660
|
+
}
|
|
648
661
|
/** The free ranges that a refusal of the packed form names. */
|
|
649
662
|
const SHOWN_FREE = 8;
|
|
650
663
|
|
|
651
664
|
/**
|
|
652
665
|
* The packed form of the loader, for an image too large for one loader file:
|
|
653
666
|
* Exomizer packs the build of main.asm (from $0400) and the build of
|
|
654
|
-
* loader.asm into loader
|
|
667
|
+
* loader.asm into the loader program, which starts at the copier with all RAM
|
|
655
668
|
* visible ($01 = $34). loader.asm holds only the copier, in one free range,
|
|
656
669
|
* and the bytes of pages $0002-$03ff, in pieces in the free ranges: the
|
|
657
670
|
* unpacker of Exomizer uses the zero page and $0334-$03ff, so the copier
|
|
658
671
|
* writes them after the unpack. Between its ranges, loader.asm holds the
|
|
659
672
|
* bytes of the image, so its build does not change a segment.
|
|
660
673
|
*/
|
|
661
|
-
function planPacked(input: LoaderInput, blocks: Block[], cia: ReturnType<typeof ciaSection>,
|
|
674
|
+
function planPacked(input: LoaderInput, blocks: Block[], cia: ReturnType<typeof ciaSection>, copierCode: number, screen: StartScreenPlan | undefined, tooLarge: string): LoaderPlan {
|
|
662
675
|
const { state } = input;
|
|
676
|
+
// The packed copier starts with SEI, as the stub of the plain form does: an interrupt in the copies would go through the vectors in RAM.
|
|
677
|
+
const codeSize = copierCode + 1;
|
|
663
678
|
// The parts below $0400 without $00/$01, which the loader sets last.
|
|
664
679
|
const low = blocks.flatMap((block) => {
|
|
665
680
|
const range = { start: Math.max(block.range.start, 0x0002), end: Math.min(block.range.end, 0x03ff) };
|
|
@@ -668,16 +683,21 @@ function planPacked(input: LoaderInput, blocks: Block[], cia: ReturnType<typeof
|
|
|
668
683
|
return [{ what: cut ? `${block.what}, part ${show(range)}` : block.what, range, bytes: block.bytes }];
|
|
669
684
|
});
|
|
670
685
|
const lowBytes = low.reduce((sum, block) => sum + block.range.end - block.range.start + 1, 0);
|
|
671
|
-
const
|
|
672
|
-
const
|
|
686
|
+
const areas = copierAreas(state.port);
|
|
687
|
+
const free = subtract(PIECE_AREA, input.segments);
|
|
688
|
+
const copierFree = areas.flatMap((area) => subtract(area, input.segments));
|
|
689
|
+
const placed = placePacked(copierFree, free, low, codeSize);
|
|
673
690
|
if ("refusal" in placed) {
|
|
674
|
-
const largest = [
|
|
675
|
-
|
|
691
|
+
const largest = (ranges: AddressRange[]) => {
|
|
692
|
+
const shown = [...ranges].sort((a, b) => b.end - b.start - (a.end - a.start)).slice(0, SHOWN_FREE);
|
|
693
|
+
return shown.length === 0 ? "none" : shown.map((range) => `${show(range)} (${range.end - range.start + 1} bytes)`).join(", ");
|
|
694
|
+
};
|
|
695
|
+
const ranges = placed.refusal === "copies" ? `The largest free ranges for pages 0-3: ${largest(free)}.` : `The largest free ranges for the copier: ${largest(copierFree)}. The largest free ranges for pages 0-3: ${largest(free)}.`;
|
|
676
696
|
const why =
|
|
677
697
|
placed.refusal === "copies"
|
|
678
698
|
? `A packed loader needs more than ${MAX_PACKED_COPIES} copies for the ${lowBytes} bytes of pages 0-3 in the free ranges.`
|
|
679
|
-
: `A packed loader needs one free range of ${codeSize + COPY_ENTRY * Math.max(low.length, 1) + 2} bytes or more for its copier, and ${lowBytes} more free bytes for pages 0-3
|
|
680
|
-
throw new WireFailure("invalid-input", `${tooLarge} ${why}
|
|
699
|
+
: `A packed loader needs one free range of ${codeSize + COPY_ENTRY * Math.max(low.length, 1) + 2} bytes or more for its copier in ${areas.map(show).join(" or ")}, and ${lowBytes} more free bytes for pages 0-3 in ${show(PIECE_AREA)}, outside the segments. The copier runs with $01 = $35 and then with the $01 of the entry, so it is in RAM that both show.${screen === undefined ? "" : ` The start screen takes ${START_SCREEN_SIZES[screen.colourMode]} bytes of the copier: without it (unstart-screen), the copier is smaller.`}`;
|
|
700
|
+
throw new WireFailure("invalid-input", `${tooLarge} ${why} ${ranges} Write the loader by hand.`);
|
|
681
701
|
}
|
|
682
702
|
const { copier, pieces } = placed;
|
|
683
703
|
const copierEnd = copier + codeSize + COPY_ENTRY * pieces.length + 2 - 1;
|
|
@@ -689,8 +709,9 @@ function planPacked(input: LoaderInput, blocks: Block[], cia: ReturnType<typeof
|
|
|
689
709
|
const lines: string[] = [
|
|
690
710
|
...input.header.map((line) => `; ${line}`),
|
|
691
711
|
"; The image is too large for one loader file, so this is the packed form. Exomizer packs the",
|
|
692
|
-
`; build of main.asm and the build of this file into loader
|
|
712
|
+
`; build of main.asm and the build of this file into the loader program, with --entry '${formatC64Address(copier)}' and --exit-memory '$34'.`,
|
|
693
713
|
"; The unpacker of Exomizer uses the zero page and $0334-$03ff, so the copier writes pages $0002-$03ff after the unpack.",
|
|
714
|
+
COPIER_LINE,
|
|
694
715
|
"",
|
|
695
716
|
"!cpu 6510",
|
|
696
717
|
...settingsLines(input),
|
|
@@ -702,6 +723,7 @@ function planPacked(input: LoaderInput, blocks: Block[], cia: ReturnType<typeof
|
|
|
702
723
|
`; The copier runs at ${formatC64Address(copier)}. The flags pass through ${formatC64Address(stackByte)}, the free stack byte below the stack pointer.`,
|
|
703
724
|
`* = ${formatC64Address(copier)}`,
|
|
704
725
|
COPIER_SOURCE,
|
|
726
|
+
" sei",
|
|
705
727
|
// The entry by its address: main.asm, which has its label, is not in this build.
|
|
706
728
|
...copierLines(state, cia, stackByte, `${formatC64Address(input.entry)}${input.entryName === undefined ? "" : ` ; ${input.entryName} in main.asm`}`, screen !== undefined),
|
|
707
729
|
];
|
|
@@ -786,19 +808,19 @@ export function placePieces(ranges: AddressRange[], low: ReadonlyArray<{ what: s
|
|
|
786
808
|
|
|
787
809
|
/**
|
|
788
810
|
* Where the packed form puts its copier and the pieces of pages 0-3: the copier
|
|
789
|
-
* in one
|
|
811
|
+
* in one range of copierFree, a free page of the plain copier first, else the start of a
|
|
790
812
|
* free range. The placement with the fewest pieces wins, then the one with the
|
|
791
813
|
* shortest build of loader.asm.
|
|
792
814
|
*/
|
|
793
|
-
function placePacked(free: AddressRange[], low: Parameters<typeof placePieces>[1], codeSize: number): { copier: number; pieces: Piece[] } | { refusal: "room" | "copies" } {
|
|
794
|
-
const candidates = [...COPIER_PAGES, ...
|
|
815
|
+
function placePacked(copierFree: AddressRange[], free: AddressRange[], low: Parameters<typeof placePieces>[1], codeSize: number): { copier: number; pieces: Piece[] } | { refusal: "room" | "copies" } {
|
|
816
|
+
const candidates = [...COPIER_PAGES, ...copierFree.map((range) => range.start)];
|
|
795
817
|
// The table has one entry per piece: a split can make the copier larger, so the room for the table grows until it holds every piece.
|
|
796
818
|
for (let copies = Math.max(low.length, 1); copies <= MAX_PACKED_COPIES; ) {
|
|
797
819
|
const size = codeSize + COPY_ENTRY * copies + 2;
|
|
798
820
|
let best: { copier: number; pieces: Piece[]; span: number } | undefined;
|
|
799
821
|
for (const copier of candidates) {
|
|
800
822
|
const range = { start: copier, end: copier + size - 1 };
|
|
801
|
-
if (!
|
|
823
|
+
if (!copierFree.some((area) => range.start >= area.start && range.end <= area.end)) continue;
|
|
802
824
|
// A piece goes to the start of its range: a range below the copier adds its whole length to the build.
|
|
803
825
|
const distance = (other: AddressRange) => (other.end < range.start ? range.start - other.start : other.start - range.end);
|
|
804
826
|
const pieces = placePieces(free.flatMap((area) => subtract(area, [range])), low, distance);
|
|
@@ -815,7 +837,7 @@ function placePacked(free: AddressRange[], low: Parameters<typeof placePieces>[1
|
|
|
815
837
|
|
|
816
838
|
/** What Exomizer makes of a packed loader. */
|
|
817
839
|
export interface PackedLoader {
|
|
818
|
-
/** loader
|
|
840
|
+
/** The loader program: it loads at $0801 and starts with RUN. */
|
|
819
841
|
program: Buffer;
|
|
820
842
|
packedBytes: number;
|
|
821
843
|
unpackedBytes: number;
|
|
@@ -934,10 +956,11 @@ function copierLines(state: EntryState, cia: ReturnType<typeof ciaSection>, stac
|
|
|
934
956
|
// The CIA part runs after the raster wait: the wait ends its whole lines earlier, so the entry comes in its line.
|
|
935
957
|
...vicLines(state, Math.floor((cia?.ops.reduce((sum, op) => sum + op.cycles, 0) ?? 0) / LINE_CYCLES)),
|
|
936
958
|
...(cia === undefined ? [] : cia.ops.map((op) => ` ${op.text}`)),
|
|
937
|
-
|
|
938
|
-
" sta $00",
|
|
959
|
+
// $01 before $00: with the port bits still outputs, $01 shows no ROM that the port of the entry does not show.
|
|
939
960
|
` lda #${formatC64Byte(port1)}`,
|
|
940
961
|
" sta $01",
|
|
962
|
+
` lda #${formatC64Byte(port0)}`,
|
|
963
|
+
" sta $00",
|
|
941
964
|
` ldx #${formatC64Byte((state.sp - 1) & 0xff)}`,
|
|
942
965
|
" txs",
|
|
943
966
|
` lda #${formatC64Byte(state.flags)}`,
|
|
@@ -991,3 +1014,23 @@ function vicLines(state: EntryState, earlier: number): string[] {
|
|
|
991
1014
|
" bne .raster",
|
|
992
1015
|
];
|
|
993
1016
|
}
|
|
1017
|
+
|
|
1018
|
+
/**
|
|
1019
|
+
* The symbols of the loader program: the labels of the build of loader.asm, which
|
|
1020
|
+
* includes main.asm, and of start_screen_stage.asm when the loader has a show
|
|
1021
|
+
* stage. A test takes one symbols file. Refuses a name with two values.
|
|
1022
|
+
*/
|
|
1023
|
+
export function loaderSymbols(...builds: ReadonlyArray<readonly AssembledSymbol[]>): AssembledSymbol[] {
|
|
1024
|
+
const valueOf = (symbol: AssembledSymbol) => (symbol.kind === "address" ? symbol.address : symbol.value);
|
|
1025
|
+
const byName = new Map<string, AssembledSymbol>();
|
|
1026
|
+
for (const symbol of builds.flat()) {
|
|
1027
|
+
const known = byName.get(symbol.name);
|
|
1028
|
+
if (known !== undefined && valueOf(known) !== valueOf(symbol)) {
|
|
1029
|
+
throw new WireFailure("operation-failed", `The builds of the loader program give ${symbol.name} two values, ${valueOf(known)} and ${valueOf(symbol)}. This is a defect of the script.`);
|
|
1030
|
+
}
|
|
1031
|
+
// An address in one build can be outside the load range of the other build, where it is a constant.
|
|
1032
|
+
const kept = known === undefined || (known.kind === "constant" && symbol.kind === "address") ? symbol : known;
|
|
1033
|
+
byName.set(symbol.name, { ...kept, used: symbol.used || (known?.used ?? false) });
|
|
1034
|
+
}
|
|
1035
|
+
return [...byName.values()].sort((a, b) => (a.name < b.name ? -1 : a.name > b.name ? 1 : 0));
|
|
1036
|
+
}
|