@henols/c64-re-tools 2.6.0-rc.2 → 2.6.0-rc.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/skills/c64-assembler/SKILL.md +26 -12
- package/skills/c64-assembler/scripts/assembler.ts +24 -11
- package/skills/c64-knowledge/SKILL.md +31 -16
- package/skills/c64-knowledge/scripts/knowledge.ts +62 -33
- package/skills/c64-packer/SKILL.md +1 -1
- package/skills/c64-reverse-engineering/SKILL.md +1 -1
- package/skills/c64-source/SKILL.md +51 -30
- package/skills/c64-source/scripts/lib/export.ts +76 -32
- package/skills/c64-source/scripts/lib/loader.ts +29 -9
- package/skills/c64-source/scripts/lib/source.ts +5 -16
- 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 +145 -46
- package/skills/c64-static-analysis/scripts/lib/coverage.ts +2 -25
- package/skills/c64-testing/SKILL.md +3 -1
- package/skills/c64-testing/references/recording-format.md +1 -0
- package/skills/c64-testing/references/scenario-format.md +5 -3
- package/skills/c64-testing/scripts/lib/recording.ts +25 -8
- package/skills/c64-testing/scripts/lib/replay.ts +1 -1
- package/skills/c64-testing/scripts/lib/scenario.ts +14 -5
- package/skills/c64-unpacker/SKILL.md +4 -2
- package/skills/c64-unpacker/scripts/lib/loads.ts +33 -12
- package/skills/c64-unpacker/scripts/lib/machine.ts +7 -3
- package/skills/c64-unpacker/scripts/unpacker.ts +5 -3
- package/src/build-name.ts +47 -0
- package/src/c64.ts +21 -1
- package/src/host/vice/session.ts +6 -1
- package/src/knowledge/read.ts +5 -2
- package/src/knowledge/schema.ts +5 -3
- package/src/knowledge/start-screen.ts +68 -0
- package/src/knowledge/text.ts +1 -0
- package/src/knowledge/write.ts +22 -16
- package/src/native/acme.ts +1 -1
- package/src/platform.ts +30 -1
- package/src/script.ts +3 -3
|
@@ -16,6 +16,7 @@ import { c64AddressOf, formatC64Address, parseC64Address } from "#src/c64.ts";
|
|
|
16
16
|
import { READY_FRAMES } from "#src/host-client/entry-state.ts";
|
|
17
17
|
import { ACTION_STEP_KINDS, checkActionStep, isInteger, isObject, keyboardBytes, renamedField, stepKind, type ActionStep, type Location } from "#src/host-client/steps.ts";
|
|
18
18
|
import { readProjectFile, type Written } from "#src/host-client/transfer.ts";
|
|
19
|
+
import { START_SCREEN_CONTINUE, START_SCREEN_WAIT } from "#src/knowledge/start-screen.ts";
|
|
19
20
|
import { PAL_FRAME_CYCLES, type C64RunSession, type CoreStatus } from "#src/native/c64run.ts";
|
|
20
21
|
import type { Workspace } from "#src/native/staging.ts";
|
|
21
22
|
import { namesOf, type Outcome } from "./scenario.ts";
|
|
@@ -387,10 +388,10 @@ function busiestSite(answer: string): { site: number; addr: number; count: numbe
|
|
|
387
388
|
* Runs to the point of an event. `map` moves a site to the rebuild. "halted"
|
|
388
389
|
* when a sample stop came first, "not-reached" when the point did not come before `until`.
|
|
389
390
|
*/
|
|
390
|
-
async function runToKey(core: C64RunSession, key: RecordedEvent["key"], until: number, map: Map<number, number
|
|
391
|
+
async function runToKey(core: C64RunSession, key: RecordedEvent["key"], until: number, map: Map<number, number>, skipped: number): Promise<"reached" | "not-reached" | "halted"> {
|
|
391
392
|
if ("previous" in key) return (await core.status("status")).word === "halted" ? "halted" : "reached";
|
|
392
393
|
if ("tick" in key) {
|
|
393
|
-
const status = await core.status(`run ${key.tick}`);
|
|
394
|
+
const status = await core.status(`run ${key.tick + skipped}`);
|
|
394
395
|
return status.word === "halted" ? "halted" : "reached";
|
|
395
396
|
}
|
|
396
397
|
const site = parseC64Address(key.site);
|
|
@@ -509,7 +510,7 @@ export async function record(
|
|
|
509
510
|
: busiest === undefined
|
|
510
511
|
? { tick: tick + 1 }
|
|
511
512
|
: { site: formatC64Address(busiest.site), addr: formatC64Address(busiest.addr), count: busiest.count + 1 };
|
|
512
|
-
if ((await runToKey(core, key, tick + 600 * PAL_FRAME_CYCLES, new Map())) !== "reached") return "the program did not read its input again within 600 frames";
|
|
513
|
+
if ((await runToKey(core, key, tick + 600 * PAL_FRAME_CYCLES, new Map(), 0)) !== "reached") return "the program did not read its input again within 600 frames";
|
|
513
514
|
events.push({ ...(step === undefined ? {} : { step }), action, key });
|
|
514
515
|
return doAction(core, action, at);
|
|
515
516
|
};
|
|
@@ -590,6 +591,8 @@ export interface SideRun {
|
|
|
590
591
|
end: CoreStatus;
|
|
591
592
|
/** Why the side stopped before its last checkpoint, if it did. */
|
|
592
593
|
problem?: string;
|
|
594
|
+
/** True when the rebuild went on after the wait of its start screen. */
|
|
595
|
+
startScreenSkipped?: boolean;
|
|
593
596
|
/**
|
|
594
597
|
* Input reads that the recording has no value for, which got the fixed idle value:
|
|
595
598
|
* at sites that it does not have, and after its end at a site.
|
|
@@ -620,7 +623,8 @@ export async function replaySide(
|
|
|
620
623
|
const move = original
|
|
621
624
|
? { code: same, data: same }
|
|
622
625
|
: { code: mover(namesOf(names, "code"), namesOf(subject.symbols, "code")), data: mover(namesOf(names), namesOf(subject.symbols)) };
|
|
623
|
-
const
|
|
626
|
+
const own = namesOf(subject.symbols);
|
|
627
|
+
const at = sideAddresses(own, subject.side, move);
|
|
624
628
|
const moved = original ? [] : siteMap(recording.sites, namesOf(names, "code"), namesOf(subject.symbols, "code"));
|
|
625
629
|
const map = new Map(moved);
|
|
626
630
|
const reads = workspace.path(`${subject.side}-${RECORDING_FILES.reads}`);
|
|
@@ -645,11 +649,24 @@ export async function replaySide(
|
|
|
645
649
|
await core.command(`sample ${hex(at.code(options.compare.checkpoint))} ${samples} ${rangesArgument([...options.compare.state, ...pinned], at)}${stop > 0 ? ` ${stop}` : ""}`);
|
|
646
650
|
}
|
|
647
651
|
await startProgram(core, prg, recording.load);
|
|
648
|
-
|
|
652
|
+
let limit = original ? recording.ticks : recording.ticks * REBUILD_TIME_FACTOR;
|
|
649
653
|
let problem: string | undefined;
|
|
650
|
-
for
|
|
654
|
+
// A loader with a start screen that RUN starts waits for a key or a fire button that the recording does not have:
|
|
655
|
+
// the rebuild goes on after the wait, as the check of the loader does. The cycles to the wait do not count.
|
|
656
|
+
const wait = own.get(START_SCREEN_WAIT);
|
|
657
|
+
const resume = own.get(START_SCREEN_CONTINUE);
|
|
658
|
+
let skipped = 0;
|
|
659
|
+
if (!original && !recording.load && wait !== undefined && resume !== undefined) {
|
|
660
|
+
const reached = await core.status(`run-pc ${hex(wait)} ${limit}`);
|
|
661
|
+
if (reached.word === "reached") {
|
|
662
|
+
skipped = reached.tick;
|
|
663
|
+
limit += skipped;
|
|
664
|
+
await core.status(`goto ${hex(resume)}`);
|
|
665
|
+
} else problem = `the rebuild did not come to the wait of its start screen at ${formatC64Address(wait)}`;
|
|
666
|
+
}
|
|
667
|
+
for (const event of problem === undefined ? recording.events : []) {
|
|
651
668
|
const what = event.step === undefined ? "the RESTORE of the play" : "step " + event.step;
|
|
652
|
-
const point = await runToKey(core, event.key, limit, map);
|
|
669
|
+
const point = await runToKey(core, event.key, limit, map, skipped);
|
|
653
670
|
if (point === "halted") break;
|
|
654
671
|
if (point === "not-reached") {
|
|
655
672
|
problem = "the program did not come to the point of " + what;
|
|
@@ -672,7 +689,7 @@ export async function replaySide(
|
|
|
672
689
|
await core.command("sample off");
|
|
673
690
|
lines = readFileSync(samples, "utf8").split("\n").filter((line) => line !== "");
|
|
674
691
|
}
|
|
675
|
-
return { samples: lines, end, unknown, misses, ...(problem === undefined ? {} : { problem }), ...(ram === undefined ? {} : { ram }) };
|
|
692
|
+
return { samples: lines, end, unknown, misses, ...(problem === undefined ? {} : { problem }), ...(skipped > 0 ? { startScreenSkipped: true } : {}), ...(ram === undefined ? {} : { ram }) };
|
|
676
693
|
}
|
|
677
694
|
|
|
678
695
|
export interface CheckpointDifference {
|
|
@@ -93,7 +93,7 @@ export async function replay(values: ReplayOptions): Promise<ReplayResult> {
|
|
|
93
93
|
const replayed = await side(rebuild, { compare: compare!, stop: expected.length, pins });
|
|
94
94
|
const difference = firstDifference(expected, replayed.samples, compare!.state);
|
|
95
95
|
// A read that the recording has no value for gets the idle value: it is in the result, never silent.
|
|
96
|
-
const base = { checkpoints: expected.length, movedSites, ...(replayed.unknown > 0 ? { idleReads: replayed.unknown } : {}) };
|
|
96
|
+
const base = { checkpoints: expected.length, movedSites, ...(replayed.startScreenSkipped === true ? { startScreen: "skipped" } : {}), ...(replayed.unknown > 0 ? { idleReads: replayed.unknown } : {}) };
|
|
97
97
|
if (difference === undefined && replayed.samples.length >= expected.length) return result("PASS", "rebuild", { ...base, compared: expected.length });
|
|
98
98
|
const reason =
|
|
99
99
|
difference !== undefined
|
|
@@ -33,6 +33,8 @@ export interface MemoryObservation {
|
|
|
33
33
|
view?: "cpu" | "ram";
|
|
34
34
|
/** The largest allowed difference per byte; 0 when not given. */
|
|
35
35
|
tolerance?: number;
|
|
36
|
+
/** Hex bits that the comparison uses: one byte for each byte, or one byte per byte of the range; all bits when not given. */
|
|
37
|
+
mask?: string;
|
|
36
38
|
/** Bytes in the range that the comparison leaves out, each with the reason. */
|
|
37
39
|
exclude?: Array<{ address: Location; reason: string }>;
|
|
38
40
|
}
|
|
@@ -207,7 +209,7 @@ function checkObserve(step: Record<string, unknown>, at: string, fail: (message:
|
|
|
207
209
|
checkLocation(item, `${at}: memory`, fail);
|
|
208
210
|
continue;
|
|
209
211
|
}
|
|
210
|
-
const itemFields = ["address", "size", "view", "tolerance", "exclude"];
|
|
212
|
+
const itemFields = ["address", "size", "view", "tolerance", "mask", "exclude"];
|
|
211
213
|
if (!isObject(item)) return fail(`${at}: memory must be a list of names, $ addresses or objects with address`);
|
|
212
214
|
for (const key of Object.keys(item)) if (!itemFields.includes(key)) fail(`${at}: a memory object has the field ${key}. ${renamedField(key, itemFields) ?? `It has ${itemFields.join(", ")}, and nothing else`}`);
|
|
213
215
|
if (item.view !== undefined && item.view !== "cpu" && item.view !== "ram") fail(`${at}: memory.view must be cpu or ram`);
|
|
@@ -223,6 +225,12 @@ function checkObserve(step: Record<string, unknown>, at: string, fail: (message:
|
|
|
223
225
|
checkLocation(item.address, `${at}: memory.address`, fail);
|
|
224
226
|
if (item.size !== undefined && !isInteger(item.size, 1, 0x10000)) fail(`${at}: memory.size must be a number from 1 to 65536`);
|
|
225
227
|
if (item.tolerance !== undefined && !isInteger(item.tolerance, 0, 0xff)) fail(`${at}: memory.tolerance must be a number from 0 to 255`);
|
|
228
|
+
if (item.mask !== undefined) {
|
|
229
|
+
const size = isInteger(item.size, 1, 0x10000) ? item.size : 1;
|
|
230
|
+
if (typeof item.mask !== "string" || !/^(?:[0-9a-fA-F]{2})+$/.test(item.mask) || (item.mask.length !== 2 && item.mask.length !== size * 2)) {
|
|
231
|
+
fail(`${at}: memory.mask must be hex bits, one byte for each byte (for example 0f) or one byte per byte of size`);
|
|
232
|
+
}
|
|
233
|
+
}
|
|
226
234
|
}
|
|
227
235
|
}
|
|
228
236
|
if (step.registers !== undefined) {
|
|
@@ -354,7 +362,7 @@ export function resolve(location: Location, symbols: Map<string, number>, side:
|
|
|
354
362
|
}
|
|
355
363
|
|
|
356
364
|
interface Observation {
|
|
357
|
-
memory: Array<{ label: string; address: number; bytes: number[]; tolerance: number; excluded: Array<{ offset: number; address: Location; reason: string }> }>;
|
|
365
|
+
memory: Array<{ label: string; address: number; bytes: number[]; tolerance: number; mask?: number[]; excluded: Array<{ offset: number; address: Location; reason: string }> }>;
|
|
358
366
|
registers: Partial<Record<ObservedRegister, unknown>>;
|
|
359
367
|
screen?: { match: boolean; mismatchingPixels: number; comparedPixels: number; mismatchRatio: number; bounds?: unknown };
|
|
360
368
|
vicii?: unknown;
|
|
@@ -444,7 +452,7 @@ async function runStep(client: ViceSessionClient, scenario: Scenario, side: Side
|
|
|
444
452
|
if (offset < 0 || offset >= size) throw new Refusal("invalid-input", `the exclude ${exclude.address} lies outside the observed ${spec.address} (${size} bytes)`);
|
|
445
453
|
return { offset, address: exclude.address, reason: exclude.reason };
|
|
446
454
|
});
|
|
447
|
-
observation.memory.push({ label: size > 1 ? `${spec.address} (${size} bytes)` : spec.address, address, bytes: [...Buffer.from(read.data, "hex")], tolerance: spec.tolerance ?? 0, excluded });
|
|
455
|
+
observation.memory.push({ label: size > 1 ? `${spec.address} (${size} bytes)` : spec.address, address, bytes: [...Buffer.from(read.data, "hex")], tolerance: spec.tolerance ?? 0, ...(spec.mask === undefined ? {} : { mask: [...Buffer.from(spec.mask, "hex")] }), excluded });
|
|
448
456
|
}
|
|
449
457
|
if (step.registers !== undefined) {
|
|
450
458
|
const registers = await client.registersGet("c64");
|
|
@@ -495,11 +503,12 @@ async function runStep(client: ViceSessionClient, scenario: Scenario, side: Side
|
|
|
495
503
|
|
|
496
504
|
type MemoryItem = Observation["memory"][number];
|
|
497
505
|
|
|
498
|
-
/** The bytes of a memory item
|
|
506
|
+
/** The bytes of a memory item whose bits of the mask differ by more than its tolerance, without the excluded bytes. */
|
|
499
507
|
function differingBytes(item: MemoryItem, other: MemoryItem): DifferingByte[] {
|
|
500
508
|
const hex = (byte: number) => byte.toString(16).padStart(2, "0");
|
|
501
509
|
const skipped = new Set(item.excluded.map((excluded) => excluded.offset));
|
|
502
|
-
|
|
510
|
+
const bits = (offset: number) => (item.mask === undefined ? 0xff : item.mask.length === 1 ? item.mask[0]! : item.mask[offset]!);
|
|
511
|
+
return item.bytes.flatMap((byte, offset) => (!skipped.has(offset) && Math.abs((byte & bits(offset)) - (other.bytes[offset]! & bits(offset))) > item.tolerance ? [{ address: formatC64Address(item.address + offset), original: hex(byte), rebuild: hex(other.bytes[offset]!) }] : []));
|
|
503
512
|
}
|
|
504
513
|
|
|
505
514
|
/** The most differing bytes of one memory item that a result lists when no --max-listed is given. */
|
|
@@ -61,6 +61,7 @@ Each step has one of these kinds:
|
|
|
61
61
|
- `{ "frames": 50 }` runs 1 to 30000 frames.
|
|
62
62
|
- `{ "runUntil": { "target": { "kind": "address", "address": "main_loop" }, "timeoutFrames": 300 } }` runs until the target. The target kinds are `address`, `memory` and `raster`. `timeoutFrames` is 1 to 30000. The default is 3000.
|
|
63
63
|
- `{ "capture": "level1" }` captures a part. Only `loads` takes this step. Give each capture step a different name of 1 to 64 letters, digits, dots, underscores or hyphens.
|
|
64
|
+
- `{ "save": "at-call" }` writes the machine state to `<out>/at-call.vsf`. Only `loads` takes this step. A run of the program can start from this snapshot file, for example at the call of a routine of a loading stage. Give each save step a different name.
|
|
64
65
|
|
|
65
66
|
An address in a step can be a name. The script refuses an unknown name before the program runs. The message gives the file and the step.
|
|
66
67
|
|
|
@@ -202,7 +203,7 @@ The folder `--out` gets these files:
|
|
|
202
203
|
- `image.bin`: all 64 KiB of RAM at the entry. This RAM includes the RAM under the ROMs and the I/O area. A segment can be in the RAM under the I/O area at `$d000`-`$dfff`.
|
|
203
204
|
- `image.json`: the entry, the segments, the vectors and the stages. `writtenBy` of a segment tells the last stage that wrote into it.
|
|
204
205
|
- One PRG for each segment.
|
|
205
|
-
- `entry_state.json`: the registers, `$00` and `$01`, pages 0 to 3, the VIC, SID and CIA registers, and the vectors. `cia` gives the CIA timer latches and interrupt masks. `differsFromReset` lists the bytes that are not as a clean C64 has them at READY. A rebuild must make these bytes the same, because the program can read them.
|
|
206
|
+
- `entry_state.json`: the registers, `$00` and `$01`, pages 0 to 3, the VIC, SID and CIA registers, the colour RAM, and the vectors. `io.colour` holds the 1000 colours at $d800. `cia` gives the CIA timer latches and interrupt masks. `differsFromReset` lists the bytes that are not as a clean C64 has them at READY. A rebuild must make these bytes the same, because the program can read them.
|
|
206
207
|
|
|
207
208
|
A segment is a block that a stage wrote, also a small block. These blocks are not segments:
|
|
208
209
|
|
|
@@ -231,6 +232,7 @@ A segment is a block of RAM from `$0400` that the program changed after the capt
|
|
|
231
232
|
|
|
232
233
|
- `loaded`: true when a capture has a segment.
|
|
233
234
|
- `captures`: one item for each capture step, with `capture`, `out`, `files`, `removed`, `pc`, `segments`, `excluded`, `differsFromReset`, `smallChanges` and `drive`.
|
|
235
|
+
- `saved`: one item for each save step, with `save`, `file` and `pc`.
|
|
234
236
|
- `smallChanges`: the count of changed bytes in blocks that are smaller than `--min-block`.
|
|
235
237
|
- `keyWait: true`: the CPU was in the KERNAL wait for a key, at READY or at an INPUT statement.
|
|
236
238
|
- `drive`: the name of the drive file, with `--drive`.
|
|
@@ -260,7 +262,7 @@ Traps:
|
|
|
260
262
|
5. Make sure that the result is the program. The code at the entry must not be the unpacker again. A segment can be data that a loader or a cracktro left, for example intro graphics. Keep it until other evidence shows that the program does not use it. Then capture again with `--exclude` for that range.
|
|
261
263
|
- Exclude each range of a cracktro that the program does not read. A rebuild has no cracktro. Also exclude a picture, an intro or a credit screen of a crack.
|
|
262
264
|
- Do not exclude the start screen of the program, also when the program does not read it. An example is the loading picture of the publisher. Record it as the start screen in the project knowledge. The loader of the rebuild shows it.
|
|
263
|
-
- When the start screen is only in an earlier stage of the loading, capture that stage too, while it shows the screen. Do not exclude the memory of the screen in that capture. Put it into the project knowledge in
|
|
265
|
+
- When the start screen is only in an earlier stage of the loading, capture that stage too, while it shows the screen. Do not exclude the memory of the screen in that capture. Put it into the project knowledge in the space `loading`. The capture holds the VIC-II registers and the colour RAM, so the start screen needs nothing more.
|
|
264
266
|
- Do not exclude other unused bytes, for example leftovers of the development or an old copy of a routine. A rebuild keeps them.
|
|
265
267
|
- Keep a crack change that the program reads, for example a credit text in the game. Record it as a `crack` switch in the project knowledge.
|
|
266
268
|
6. Put `<out>/image.json` into the project knowledge. The knowledge keeps the segments, the entry, the entry state and the stages. After that, you do not need the files in `<out>`.
|
|
@@ -1,8 +1,10 @@
|
|
|
1
1
|
// The loads command of c64-unpacker: one capture for each part of a
|
|
2
|
-
// multi-load program. The steps file has action steps
|
|
3
|
-
// capture writes what the C64 side changed since the previous
|
|
4
|
-
// image with its entry state, and with --drive the drive RAM as
|
|
5
|
-
// knowledge space drive8.
|
|
2
|
+
// multi-load program. The steps file has action steps, capture steps and save
|
|
3
|
+
// steps; a capture writes what the C64 side changed since the previous
|
|
4
|
+
// capture as an image with its entry state, and with --drive the drive RAM as
|
|
5
|
+
// a PRG for knowledge space drive8. A save writes the machine state as a
|
|
6
|
+
// snapshot file, which a run can start from. No tool guesses where a load
|
|
7
|
+
// ends: the steps decide.
|
|
6
8
|
|
|
7
9
|
import { type AddressRange, COVERAGE, formatC64Address } from "#src/c64.ts";
|
|
8
10
|
import { Refusal } from "#src/errors.ts";
|
|
@@ -17,9 +19,9 @@ import { entryStateFile, json, machineState, readRam, readyState, removeStaleFil
|
|
|
17
19
|
import { FIRST_SEGMENT_ADDRESS } from "./stages.ts";
|
|
18
20
|
|
|
19
21
|
|
|
20
|
-
export type LoadStep = ActionStep | { capture: string };
|
|
22
|
+
export type LoadStep = ActionStep | { capture: string } | { save: string };
|
|
21
23
|
|
|
22
|
-
/** A capture name: letters, digits, dots, underscores or hyphens. */
|
|
24
|
+
/** A capture or save name: letters, digits, dots, underscores or hyphens. */
|
|
23
25
|
const NAME = /^[A-Za-z0-9._-]{1,64}$/;
|
|
24
26
|
/** The drive RAM of a 1541. */
|
|
25
27
|
const DRIVE_RAM = 0x0800;
|
|
@@ -28,7 +30,7 @@ const KERNAL_LOAD = 0xffd5;
|
|
|
28
30
|
/** The KERNAL loop that waits for a key at READY. or at an INPUT statement. */
|
|
29
31
|
const KERNAL_KEY_WAIT = { start: 0xe5cd, end: 0xe5d5 };
|
|
30
32
|
|
|
31
|
-
/** Reads and checks the steps file of loads: action steps with $ addresses, and capture steps with new names. */
|
|
33
|
+
/** Reads and checks the steps file of loads: action steps with $ addresses, and capture and save steps with new names. */
|
|
32
34
|
export function readLoadSteps(path: string): LoadStep[] {
|
|
33
35
|
let value: unknown;
|
|
34
36
|
try {
|
|
@@ -42,9 +44,17 @@ export function readLoadSteps(path: string): LoadStep[] {
|
|
|
42
44
|
};
|
|
43
45
|
if (!Array.isArray(value) || value.length === 0) return fail("the steps are a JSON list with at least one step");
|
|
44
46
|
const names = new Set<string>();
|
|
47
|
+
const saves = new Set<string>();
|
|
45
48
|
value.forEach((step: unknown, index) => {
|
|
46
49
|
const at = `step ${index + 1}`;
|
|
47
50
|
if (!isObject(step)) return fail(`${at} must be an object`);
|
|
51
|
+
if ("save" in step) {
|
|
52
|
+
if (Object.keys(step).length !== 1) fail(`${at}: a save step has only save`);
|
|
53
|
+
if (typeof step.save !== "string" || !NAME.test(step.save)) fail(`${at}: save needs a name of 1 to 64 letters, digits, dots, underscores or hyphens`);
|
|
54
|
+
if (saves.has(step.save as string)) fail(`${at}: the save name ${step.save as string} is used before`);
|
|
55
|
+
saves.add(step.save as string);
|
|
56
|
+
return;
|
|
57
|
+
}
|
|
48
58
|
if ("capture" in step) {
|
|
49
59
|
if (Object.keys(step).length !== 1) fail(`${at}: a capture step has only capture`);
|
|
50
60
|
if (typeof step.capture !== "string" || !NAME.test(step.capture)) fail(`${at}: capture needs a name of 1 to 64 letters, digits, dots, underscores or hyphens`);
|
|
@@ -54,14 +64,14 @@ export function readLoadSteps(path: string): LoadStep[] {
|
|
|
54
64
|
}
|
|
55
65
|
const kinds = Object.keys(step).filter((key) => (ACTION_STEP_KINDS as readonly string[]).includes(key));
|
|
56
66
|
if (kinds.length > 1) return fail(`${at} has ${kinds.length} step kinds: ${kinds.join(", ")}. Give each one its own step`);
|
|
57
|
-
if (kinds.length !== 1) return fail(`${at} must have exactly one of ${[...VICE_STEP_KINDS, "capture"].join(", ")}`);
|
|
67
|
+
if (kinds.length !== 1) return fail(`${at} must have exactly one of ${[...VICE_STEP_KINDS, "capture", "save"].join(", ")}`);
|
|
58
68
|
// The step with its defaults filled in, as the shared schema of the action steps gives it.
|
|
59
69
|
const checked = checkActionStep(kinds[0] as ActionStepKind, step, at, fail);
|
|
60
70
|
// Its names resolve now: an unknown name stops the call before the emulator starts.
|
|
61
71
|
checkStepNames([checked], knowledgeAt(), `--steps ${path}`, index + 1);
|
|
62
72
|
(value as unknown[])[index] = checked;
|
|
63
73
|
});
|
|
64
|
-
if (names.size === 0) fail("the steps have no capture step");
|
|
74
|
+
if (names.size === 0 && saves.size === 0) fail("the steps have no capture step and no save step");
|
|
65
75
|
return value as LoadStep[];
|
|
66
76
|
}
|
|
67
77
|
|
|
@@ -149,12 +159,23 @@ export async function loads(program: string, options: { out: string; steps: Load
|
|
|
149
159
|
}
|
|
150
160
|
|
|
151
161
|
const captures: Array<Record<string, unknown>> = [];
|
|
162
|
+
const saved: Array<Record<string, unknown>> = [];
|
|
152
163
|
for (const [index, step] of options.steps.entries()) {
|
|
164
|
+
if ("save" in step) {
|
|
165
|
+
// The machine state as a snapshot file: a run starts from it, for example at the call of a loading stage.
|
|
166
|
+
if ((await client.status()).state === "running") await client.execution({ action: "pause", space: "c64" });
|
|
167
|
+
await client.snapshot({ action: "save", name: "loads-save" });
|
|
168
|
+
const path = `${options.out}/${step.save}.vsf`;
|
|
169
|
+
const file = writeReported(path, await client.snapshotExport("loads-save"), { what: "--out" });
|
|
170
|
+
await client.snapshot({ action: "remove", name: "loads-save" });
|
|
171
|
+
saved.push({ save: step.save, file, pc: formatC64Address((await client.registersGet("c64")).pc) });
|
|
172
|
+
continue;
|
|
173
|
+
}
|
|
153
174
|
if (!("capture" in step)) {
|
|
154
175
|
const notReached = await runActionStep(client, step, knowledgeAt());
|
|
155
176
|
if (notReached !== undefined && "runUntil" in step) {
|
|
156
177
|
const reason = `Step ${index + 1} did not reach its target in ${step.runUntil.timeoutFrames} frames. It stopped by ${notReached.stopReason} at ${formatC64Address(notReached.pc)}.`;
|
|
157
|
-
return { output: { loaded: false, reason, captures }, failed: true };
|
|
178
|
+
return { output: { loaded: false, reason, captures, ...(saved.length === 0 ? {} : { saved }) }, failed: true };
|
|
158
179
|
}
|
|
159
180
|
continue;
|
|
160
181
|
}
|
|
@@ -226,9 +247,9 @@ export async function loads(program: string, options: { out: string; steps: Load
|
|
|
226
247
|
if (captures.length > 0 && captures.every((capture) => (capture.segments as unknown[]).length === 0)) {
|
|
227
248
|
const waits = captures.some((capture) => capture.keyWait === true) ? " The C64 waited for a key at READY. or at an INPUT statement, so the load ended or did not start." : "";
|
|
228
249
|
const reason = `No capture has a segment: the program changed no block of RAM from ${formatC64Address(FIRST_SEGMENT_ADDRESS)} on.${waits}`;
|
|
229
|
-
return { output: { loaded: false, reason, captures }, failed: true };
|
|
250
|
+
return { output: { loaded: false, reason, captures, ...(saved.length === 0 ? {} : { saved }) }, failed: true };
|
|
230
251
|
}
|
|
231
|
-
return { output: { loaded: true, captures }, failed: false };
|
|
252
|
+
return { output: { loaded: true, captures, ...(saved.length === 0 ? {} : { saved }) }, failed: false };
|
|
232
253
|
} finally {
|
|
233
254
|
await client.close();
|
|
234
255
|
}
|
|
@@ -15,8 +15,10 @@ type Registers = Awaited<ReturnType<ViceSessionClient["registersGet"]>>;
|
|
|
15
15
|
|
|
16
16
|
const MAX_READ = 4096;
|
|
17
17
|
|
|
18
|
-
/** The I/O areas of the entry state, read without side effects. */
|
|
19
|
-
export const IO_AREAS = { vic: [0xd000, 0x2f], sid: [0xd400, 0x1d], cia1: [0xdc00, 0x10], cia2: [0xdd00, 0x10] } as const;
|
|
18
|
+
/** The I/O areas of the entry state, read without side effects. The colour RAM is the colours of the screen: a start screen of a captured stage uses them. */
|
|
19
|
+
export const IO_AREAS = { vic: [0xd000, 0x2f], sid: [0xd400, 0x1d], cia1: [0xdc00, 0x10], cia2: [0xdd00, 0x10], colour: [0xd800, 1000] } as const;
|
|
20
|
+
/** The I/O areas of chips: what differsFrom compares. The colour RAM is the content of the screen, not the state of the machine. */
|
|
21
|
+
const CHIP_AREAS = ["vic", "sid", "cia1", "cia2"] as const;
|
|
20
22
|
|
|
21
23
|
/** What no register read of a CIA gives. */
|
|
22
24
|
export interface CiaHidden {
|
|
@@ -49,6 +51,8 @@ export async function machineState(client: ViceSessionClient): Promise<MachineSt
|
|
|
49
51
|
const pages = await readRam(client, 0x0000, 0x400);
|
|
50
52
|
const io = {} as Record<keyof typeof IO_AREAS, Buffer>;
|
|
51
53
|
for (const [name, [address, size]] of Object.entries(IO_AREAS)) io[name as keyof typeof IO_AREAS] = Buffer.from((await client.ioRead(address, size)).data, "hex");
|
|
54
|
+
// The colour RAM has 4 bits: a read gives other bits above them.
|
|
55
|
+
io.colour = Buffer.from(io.colour.map((value) => value & 0x0f));
|
|
52
56
|
const [cia1, cia2] = (await client.cia("both")).chips.map(({ latchA, latchB, interruptMask }) => ({ latchA, latchB, interruptMask }));
|
|
53
57
|
const romVectors = Buffer.from((await client.memoryRead({ address: 0xfffa, size: 6, space: "c64", view: "cpu" })).data, "hex");
|
|
54
58
|
return { port, pages, io, cia: { cia1: cia1!, cia2: cia2! }, romVectors };
|
|
@@ -79,7 +83,7 @@ export function differsFrom(state: MachineState, clean: MachineState): Array<{ a
|
|
|
79
83
|
const compare = (base: number, entry: Buffer, reset: Buffer) => entry.forEach((value, offset) => value !== reset[offset] && differs.push({ address: formatC64Address(base + offset), entry: value, reset: reset[offset]! }));
|
|
80
84
|
compare(0x0000, state.port, clean.port);
|
|
81
85
|
compare(0x0002, state.pages.subarray(2), clean.pages.subarray(2));
|
|
82
|
-
for (const
|
|
86
|
+
for (const name of CHIP_AREAS) compare(IO_AREAS[name][0], state.io[name], clean.io[name]);
|
|
83
87
|
return differs;
|
|
84
88
|
}
|
|
85
89
|
|
|
@@ -642,7 +642,9 @@ function loaded(call: Call, { output, failed }: { output: Record<string, unknown
|
|
|
642
642
|
if (failed) return result;
|
|
643
643
|
const out = call.string("out")!;
|
|
644
644
|
const drive = call.flag("drive") ? ` Put the drive RAM ${out}/<name>/drive8.prg into the project knowledge in the space drive8.` : "";
|
|
645
|
-
|
|
645
|
+
const captures = (output.captures as unknown[]).length === 0 ? "" : `Put the capture ${out}/<name>/image.json of the part that you work on into the project knowledge.${drive}`;
|
|
646
|
+
const saved = output.saved === undefined ? "" : `A run of the program can start from a snapshot file ${out}/<name>.vsf.`;
|
|
647
|
+
return reply(result, { hint: [captures, saved].filter((text) => text !== "").join(" ") });
|
|
646
648
|
}
|
|
647
649
|
|
|
648
650
|
const COMMANDS: CommandSpec[] = [
|
|
@@ -702,10 +704,10 @@ const COMMANDS: CommandSpec[] = [
|
|
|
702
704
|
},
|
|
703
705
|
{
|
|
704
706
|
name: "loads",
|
|
705
|
-
summary: "Runs a multi-load program with the steps file. Each capture step {\"capture\": \"<name>\"} writes <dir>/<name>/ with image.bin, image.json, entry_state.json and a PRG for each block that the C64 side changed since the previous capture.",
|
|
707
|
+
summary: "Runs a multi-load program with the steps file. Each capture step {\"capture\": \"<name>\"} writes <dir>/<name>/ with image.bin, image.json, entry_state.json and a PRG for each block that the C64 side changed since the previous capture. Each save step {\"save\": \"<name>\"} writes the machine state to <dir>/<name>.vsf.",
|
|
706
708
|
subjects: [{ name: "<program>", help: "a PRG, or a disk, tape or cartridge image, relative to the project folder" }],
|
|
707
709
|
options: {
|
|
708
|
-
steps: { ...STEPS, required: true, help: "action steps and
|
|
710
|
+
steps: { ...STEPS, required: true, help: "action steps, capture steps and save steps (JSON)" },
|
|
709
711
|
out: { ...OPTIONS.out, output: "folder", value: "<dir>", required: true, help: "the folder for the captures, relative to the project folder" },
|
|
710
712
|
drive: { kind: "flag", help: "also write the 2 KiB drive RAM as drive8.prg, for knowledge space drive8" },
|
|
711
713
|
"min-block": { kind: "count", value: "<bytes>", min: 1, max: 0x10000, default: 64, help: "write a changed block of at least this many bytes (default 64)" },
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
// The name of a build: the file name tells what kind of build a program is.
|
|
2
|
+
// A release build is release_<label>. A build of config.asm is clean when each
|
|
3
|
+
// switch has its default, else custom_ with each switch that differs: its
|
|
4
|
+
// name when it is on, no_<name> when it is off. A build of an entry other
|
|
5
|
+
// than main.asm adds the entry, for example clean_loader. A source without
|
|
6
|
+
// config.asm and without a release keeps the name of its entry. The symbols
|
|
7
|
+
// of a build go into symbols/ next to the program, so the program folder
|
|
8
|
+
// holds only programs.
|
|
9
|
+
|
|
10
|
+
import { basename, dirname, extname, join } from "node:path";
|
|
11
|
+
|
|
12
|
+
/** The file with the value of each switch. The user edits it, and generate keeps the values. */
|
|
13
|
+
export const CONFIG_FILE = "config.asm";
|
|
14
|
+
|
|
15
|
+
/** A value line of config.asm: NAME = 0 or NAME = 1. */
|
|
16
|
+
const CONFIG_VALUE = /^\s*([A-Z][A-Z0-9_]*)\s*=\s*([01])\s*(?:;.*)?$/;
|
|
17
|
+
|
|
18
|
+
/** The values of the switches in a config.asm text, by name. Lines that are not a value line are not read. */
|
|
19
|
+
export function configValues(text: string): Map<string, 0 | 1> {
|
|
20
|
+
const values = new Map<string, 0 | 1>();
|
|
21
|
+
for (const line of text.split(/\r?\n/)) {
|
|
22
|
+
const match = CONFIG_VALUE.exec(line);
|
|
23
|
+
if (match !== null) values.set(match[1]!, match[2] === "1" ? 1 : 0);
|
|
24
|
+
}
|
|
25
|
+
return values;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/** What a build is made of: a release, or the values of config.asm with the default of each switch (0 when the defaults do not have it). */
|
|
29
|
+
export type BuildKind = { release: string } | { values: ReadonlyMap<string, number>; defaults: ReadonlyMap<string, number> };
|
|
30
|
+
|
|
31
|
+
/** The name of a build of `entry`, without an extension. */
|
|
32
|
+
export function buildName(entry: string, kind: BuildKind | undefined): string {
|
|
33
|
+
const base = basename(entry, extname(entry));
|
|
34
|
+
if (kind === undefined) return base;
|
|
35
|
+
let type: string;
|
|
36
|
+
if ("release" in kind) type = `release_${kind.release}`;
|
|
37
|
+
else {
|
|
38
|
+
const changed = [...kind.values].filter(([name, value]) => value !== (kind.defaults.get(name) ?? 0)).map(([name, value]) => (value === 0 ? `no_${name}` : name).toLowerCase());
|
|
39
|
+
type = changed.length === 0 ? "clean" : `custom_${changed.join("_")}`;
|
|
40
|
+
}
|
|
41
|
+
return base === "main" ? type : `${type}_${base}`;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/** The symbols file of a program: symbols/<name>.json in the folder of the program. */
|
|
45
|
+
export function symbolsPath(program: string): string {
|
|
46
|
+
return join(dirname(program), "symbols", `${basename(program, extname(program))}.json`).split("\\").join("/");
|
|
47
|
+
}
|
package/src/c64.ts
CHANGED
|
@@ -33,7 +33,7 @@ export interface AddressRange {
|
|
|
33
33
|
}
|
|
34
34
|
|
|
35
35
|
/** The knowledge spaces: the C64, the 1541 drive in unit 8, and cartridge banks 0 to 255. */
|
|
36
|
-
export const SPACE_PATTERN = /^(c64|drive8|bank(0|[1-9][0-9]?|1[0-9]{2}|2[0-4][0-9]|25[0-5]))$/;
|
|
36
|
+
export const SPACE_PATTERN = /^(c64|drive8|loading|bank(0|[1-9][0-9]?|1[0-9]{2}|2[0-4][0-9]|25[0-5]))$/;
|
|
37
37
|
|
|
38
38
|
/** VIC-II state with its addresses as $xxxx; the other values stay numbers. */
|
|
39
39
|
export function formatVicii<T extends { screenAddress: number; graphicsAddress: number }>(state: T): Omit<T, "screenAddress" | "graphicsAddress"> & { screenAddress: string; graphicsAddress: string } {
|
|
@@ -478,6 +478,26 @@ export function coverageEntries(map: Uint8Array, image: (address: number) => num
|
|
|
478
478
|
return entries;
|
|
479
479
|
}
|
|
480
480
|
|
|
481
|
+
const ROM_ACCESS = COVERAGE.romRead | COVERAGE.romExecute;
|
|
482
|
+
const RAM_USE = COVERAGE.ramRead | COVERAGE.ramExecute;
|
|
483
|
+
|
|
484
|
+
/**
|
|
485
|
+
* The image bytes under ROM that the runs never used: the CPU read or ran the
|
|
486
|
+
* ROM there, and never read or ran the RAM. A JSR $FFE4 or an IRQ
|
|
487
|
+
* vector at $FFFE then leads into ROM, not into the leftover RAM bytes of a
|
|
488
|
+
* flat image, so the ranges are data for an analysis run.
|
|
489
|
+
*/
|
|
490
|
+
export function romOnlyRanges(map: Uint8Array, image: (address: number) => number | undefined): AddressRange[] {
|
|
491
|
+
const ranges: AddressRange[] = [];
|
|
492
|
+
for (let address = 0; address < 0x10000; address++) {
|
|
493
|
+
if ((map[address]! & ROM_ACCESS) === 0 || (map[address]! & RAM_USE) !== 0 || image(address) === undefined) continue;
|
|
494
|
+
const last = ranges.at(-1);
|
|
495
|
+
if (last !== undefined && last.end === address - 1) last.end = address;
|
|
496
|
+
else ranges.push({ start: address, end: address });
|
|
497
|
+
}
|
|
498
|
+
return ranges;
|
|
499
|
+
}
|
|
500
|
+
|
|
481
501
|
/** Every executed instruction start in RAM inside the image: VICE marks the opcode byte of an instruction as executed, not its operand. */
|
|
482
502
|
export function executedStarts(map: Uint8Array, image: (address: number) => number | undefined): number[] {
|
|
483
503
|
const starts: number[] = [];
|
package/src/host/vice/session.ts
CHANGED
|
@@ -1402,7 +1402,12 @@ export class ViceSession implements ViceSessionHandle {
|
|
|
1402
1402
|
() => true,
|
|
1403
1403
|
() => false,
|
|
1404
1404
|
);
|
|
1405
|
-
if (!kept)
|
|
1405
|
+
if (!kept) {
|
|
1406
|
+
throw new WireFailure(
|
|
1407
|
+
"machine-state-lost",
|
|
1408
|
+
"The emulator did not restore this snapshot file, and the machine state from before the restore is lost. A snapshot file restores only in the emulator version and with the machine settings that saved it. Make the snapshot again in this emulator version, and load the program again.",
|
|
1409
|
+
);
|
|
1410
|
+
}
|
|
1406
1411
|
if (error instanceof WireFailure) throw error;
|
|
1407
1412
|
throw new WireFailure(
|
|
1408
1413
|
"invalid-input",
|
package/src/knowledge/read.ts
CHANGED
|
@@ -134,6 +134,7 @@ export interface ReleaseRow {
|
|
|
134
134
|
* Without `stage`, the bytes are in the program at the entry. With `stage`,
|
|
135
135
|
* they are in the memory of that space (a captured stage of the loading, for
|
|
136
136
|
* example), and `call` is a routine of the stage that the start screen calls.
|
|
137
|
+
* `colourRam` takes the colours from the colour RAM of the capture of the stage.
|
|
137
138
|
*/
|
|
138
139
|
export interface StartScreenRow {
|
|
139
140
|
space: Space;
|
|
@@ -147,6 +148,7 @@ export interface StartScreenRow {
|
|
|
147
148
|
background: number;
|
|
148
149
|
stage?: Space;
|
|
149
150
|
call?: number;
|
|
151
|
+
colourRam?: true;
|
|
150
152
|
comment?: string;
|
|
151
153
|
origin: Extract<Origin, "user" | "llm">;
|
|
152
154
|
revision: string;
|
|
@@ -202,7 +204,7 @@ export const SWITCH_COLUMNS = "space, name, kind, default_value, releases, comme
|
|
|
202
204
|
export const RELEASE_COLUMNS = "space, label, file, sha256, base, comment, origin, valid_from_revision";
|
|
203
205
|
export const MACRO_COLUMNS = "space, name, params, body, comment, origin, valid_from_revision";
|
|
204
206
|
export const MACRO_USE_COLUMNS = "space, start_address, end_address, macro, args, origin, valid_from_revision";
|
|
205
|
-
export const START_SCREEN_COLUMNS = "space, mode, screen, bitmap, charset, colours, colour, border, background, stage, call_address, comment, origin, valid_from_revision";
|
|
207
|
+
export const START_SCREEN_COLUMNS = "space, mode, screen, bitmap, charset, colours, colour, border, background, stage, call_address, colour_ram, comment, origin, valid_from_revision";
|
|
206
208
|
|
|
207
209
|
export function toSymbol(row: Raw): SymbolRow {
|
|
208
210
|
return { space: row.space as Space, address: row.address as number, name: row.name as string, kind: row.kind as SymbolKind, origin: row.origin as Origin, revision: row.valid_from_revision as string };
|
|
@@ -325,6 +327,7 @@ export function toStartScreen(row: Raw): StartScreenRow {
|
|
|
325
327
|
for (const key of ["bitmap", "charset", "colours", "colour"] as const) if (row[key] !== null && row[key] !== undefined) value[key] = row[key] as number;
|
|
326
328
|
if (row.stage !== null && row.stage !== undefined) value.stage = row.stage as Space;
|
|
327
329
|
if (row.call_address !== null && row.call_address !== undefined) value.call = row.call_address as number;
|
|
330
|
+
if (row.colour_ram === 1) value.colourRam = true;
|
|
328
331
|
if (row.comment !== null && row.comment !== undefined) value.comment = row.comment as string;
|
|
329
332
|
return value;
|
|
330
333
|
}
|
|
@@ -742,7 +745,7 @@ export function listSpaces(db: DatabaseSync | undefined): Space[] {
|
|
|
742
745
|
`SELECT space FROM current_symbols UNION SELECT space FROM current_analyzer_names UNION SELECT space FROM current_regions UNION SELECT space FROM current_comments UNION SELECT space FROM current_references UNION SELECT space FROM current_suppressions UNION SELECT space FROM current_modules UNION SELECT space FROM current_operands UNION SELECT space FROM current_patches UNION SELECT space FROM current_switches UNION SELECT space FROM current_releases UNION SELECT space FROM current_macros UNION SELECT space FROM current_macro_uses UNION SELECT space FROM current_start_screens UNION SELECT space FROM current_memory UNION SELECT space FROM current_entries UNION SELECT space FROM current_coverage`,
|
|
743
746
|
)
|
|
744
747
|
.all() as Array<{ space: Space }>;
|
|
745
|
-
const order = (space: Space) => (space === "c64" ? -
|
|
748
|
+
const order = (space: Space) => (space === "c64" ? -3 : space === "drive8" ? -2 : space === "loading" ? -1 : Number(space.slice(4)));
|
|
746
749
|
return rows.map((row) => row.space).sort((a, b) => order(a) - order(b));
|
|
747
750
|
}
|
|
748
751
|
|
package/src/knowledge/schema.ts
CHANGED
|
@@ -114,7 +114,7 @@ export function isGeneratedFile(file: string): boolean {
|
|
|
114
114
|
*/
|
|
115
115
|
export type Space = string;
|
|
116
116
|
export const DEFAULT_SPACE: Space = "c64";
|
|
117
|
-
const SPACE_CHECK = "space IN ('c64', 'drive8') OR space GLOB 'bank[0-9]' OR space GLOB 'bank[1-9][0-9]' OR space GLOB 'bank[12][0-9][0-9]'";
|
|
117
|
+
const SPACE_CHECK = "space IN ('c64', 'drive8', 'loading') OR space GLOB 'bank[0-9]' OR space GLOB 'bank[1-9][0-9]' OR space GLOB 'bank[12][0-9][0-9]'";
|
|
118
118
|
|
|
119
119
|
const list = (values: readonly string[]) => values.map((value) => `'${value}'`).join(", ");
|
|
120
120
|
|
|
@@ -321,15 +321,17 @@ function createSchema(db: DatabaseSync): void {
|
|
|
321
321
|
background INTEGER NOT NULL CHECK (background BETWEEN 0 AND 15),
|
|
322
322
|
stage TEXT CHECK (stage IS NULL OR (stage <> 'c64' AND (${SPACE_CHECK.replaceAll("space", "stage")}))),
|
|
323
323
|
call_address INTEGER CHECK (call_address BETWEEN 0 AND 65535),
|
|
324
|
+
colour_ram INTEGER CHECK (colour_ram = 1),
|
|
324
325
|
comment TEXT CHECK (comment IS NULL OR length(comment) > 0),
|
|
325
326
|
origin TEXT NOT NULL CHECK (origin IN ('user', 'llm')),
|
|
326
327
|
valid_from_revision TEXT NOT NULL REFERENCES revisions (id),
|
|
327
328
|
valid_to_revision TEXT REFERENCES revisions (id),
|
|
328
329
|
CHECK (call_address IS NULL OR stage IS NOT NULL),
|
|
330
|
+
CHECK (colour_ram IS NULL OR (stage IS NOT NULL AND colours IS NULL AND colour IS NULL AND mode <> 'hires')),
|
|
329
331
|
CHECK ((mode = 'text') = (charset IS NOT NULL)),
|
|
330
332
|
CHECK ((mode = 'text') = (bitmap IS NULL)),
|
|
331
333
|
CHECK (colours IS NULL OR colour IS NULL),
|
|
332
|
-
CHECK ((mode = 'hires') = (colours IS NULL AND colour IS NULL))
|
|
334
|
+
CHECK ((mode = 'hires') = (colours IS NULL AND colour IS NULL AND colour_ram IS NULL))
|
|
333
335
|
);
|
|
334
336
|
CREATE INDEX start_screens_space ON start_screens (space);
|
|
335
337
|
CREATE TABLE macro_uses (
|
|
@@ -397,7 +399,7 @@ function createSchema(db: DatabaseSync): void {
|
|
|
397
399
|
}
|
|
398
400
|
|
|
399
401
|
/** The version of the index this build makes. An index with another version is emptied and made again. */
|
|
400
|
-
export const INDEX_VERSION =
|
|
402
|
+
export const INDEX_VERSION = 13;
|
|
401
403
|
|
|
402
404
|
function indexVersion(db: DatabaseSync): number | undefined {
|
|
403
405
|
const hasMeta = db.prepare("SELECT 1 FROM sqlite_master WHERE type = 'table' AND name = 'meta'").get();
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
// The start screen of a captured stage, from the entry state of its capture:
|
|
2
|
+
// the VIC-II registers and the VIC bank give the mode and the addresses, and
|
|
3
|
+
// the colour RAM of the capture gives the colours. Thus the start screen of a
|
|
4
|
+
// stage needs no address from the user.
|
|
5
|
+
|
|
6
|
+
import { Refusal } from "../errors.ts";
|
|
7
|
+
import type { StartScreenMode } from "./schema.ts";
|
|
8
|
+
|
|
9
|
+
/** The labels of the start screen in a generated loader: its wait for a key or a fire button, and the code after the wait. */
|
|
10
|
+
export const START_SCREEN_WAIT = "loader_start_screen_wait";
|
|
11
|
+
export const START_SCREEN_CONTINUE = "loader_start_screen_continue";
|
|
12
|
+
|
|
13
|
+
/** The fields of a start screen that the capture of a stage gives. */
|
|
14
|
+
export interface StageScreen {
|
|
15
|
+
mode: StartScreenMode;
|
|
16
|
+
screen: number;
|
|
17
|
+
bitmap?: number;
|
|
18
|
+
charset?: number;
|
|
19
|
+
border: number;
|
|
20
|
+
background: number;
|
|
21
|
+
/** The colours are the colour RAM of the capture: not for hires. */
|
|
22
|
+
colourRam?: true;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/** The bytes of an I/O area of an entry state: io.<name>.bytes as hex, when the capture has them. */
|
|
26
|
+
function ioBytes(state: Record<string, unknown> | undefined, name: string): Buffer | undefined {
|
|
27
|
+
const io = state?.io as Record<string, { bytes?: unknown }> | undefined;
|
|
28
|
+
const bytes = io?.[name]?.bytes;
|
|
29
|
+
return typeof bytes === "string" && /^([0-9a-f]{2})+$/i.test(bytes) ? Buffer.from(bytes, "hex") : undefined;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/** The colour RAM of the capture of a stage: 1000 colours from $d800, or undefined when the capture has none. */
|
|
33
|
+
export function capturedColourRam(state: Record<string, unknown> | undefined): Uint8Array | undefined {
|
|
34
|
+
const bytes = ioBytes(state, "colour");
|
|
35
|
+
return bytes === undefined || bytes.length !== 1000 ? undefined : Uint8Array.from(bytes, (value) => value & 0x0f);
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* The start screen that the capture of a stage shows: its mode, its screen,
|
|
40
|
+
* its bitmap or charset, its colours and the colour RAM. Refuses by name when
|
|
41
|
+
* the capture does not show a start screen that the loader can show.
|
|
42
|
+
*/
|
|
43
|
+
export function startScreenOfStage(state: Record<string, unknown> | undefined, stage: string): StageScreen {
|
|
44
|
+
const again = `Capture the stage again while it shows the screen, and put the capture into the project knowledge with image --space ${stage}.`;
|
|
45
|
+
const vic = ioBytes(state, "vic");
|
|
46
|
+
const cia2 = ioBytes(state, "cia2");
|
|
47
|
+
if (state === undefined || vic === undefined || vic.length < 0x2f || cia2 === undefined) throw new Refusal("not-found", `The program in ${stage} has no entry state with the VIC-II and CIA 2. ${again}`);
|
|
48
|
+
const [d011, d016, d018] = [vic[0x11]!, vic[0x16]!, vic[0x18]!];
|
|
49
|
+
if ((d011 & 0x10) === 0) throw new Refusal("invalid-input", `The capture in ${stage} has the display off ($d011 = $${d011.toString(16).padStart(2, "0")}). ${again}`);
|
|
50
|
+
if ((d011 & 0x40) !== 0) throw new Refusal("invalid-input", `The capture in ${stage} shows a screen in the extended colour mode, which a start screen does not have.`);
|
|
51
|
+
const bitmap = (d011 & 0x20) !== 0;
|
|
52
|
+
const multicolour = (d016 & 0x10) !== 0;
|
|
53
|
+
if (!bitmap && multicolour) throw new Refusal("invalid-input", `The capture in ${stage} shows a multicolour text screen, which a start screen does not have.`);
|
|
54
|
+
const bank = (3 - (cia2[0]! & 0x03)) << 14;
|
|
55
|
+
const mode: StartScreenMode = bitmap ? (multicolour ? "multicolour" : "hires") : "text";
|
|
56
|
+
const screen = bank + ((d018 >> 4) & 0x0f) * 0x400;
|
|
57
|
+
const colourRam = mode === "hires" ? undefined : capturedColourRam(state);
|
|
58
|
+
if (mode !== "hires" && colourRam === undefined) throw new Refusal("not-found", `The capture in ${stage} has no colour RAM: it is from an older version. ${again}`);
|
|
59
|
+
return {
|
|
60
|
+
mode,
|
|
61
|
+
screen,
|
|
62
|
+
...(bitmap ? { bitmap: bank + ((d018 >> 3) & 1) * 0x2000 } : { charset: bank + ((d018 >> 1) & 7) * 0x800 }),
|
|
63
|
+
border: vic[0x20]! & 0x0f,
|
|
64
|
+
background: vic[0x21]! & 0x0f,
|
|
65
|
+
...(colourRam === undefined ? {} : { colourRam: true as const }),
|
|
66
|
+
};
|
|
67
|
+
}
|
|
68
|
+
|
package/src/knowledge/text.ts
CHANGED
|
@@ -202,6 +202,7 @@ export const TABLES: readonly TableSpec[] = [
|
|
|
202
202
|
field("background", "background", "integer"),
|
|
203
203
|
field("stage", "stage", "text", true),
|
|
204
204
|
field("call", "call_address", "address", true),
|
|
205
|
+
field("colourRam", "colour_ram", "integer", true),
|
|
205
206
|
field("comment", "comment", "text", true),
|
|
206
207
|
origin,
|
|
207
208
|
],
|