@henols/c64-re-tools 2.5.0 → 2.6.0-rc.2

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.
@@ -0,0 +1,268 @@
1
+ // The show stage of a start screen from a captured stage: the picture is not
2
+ // in the program at the entry, so it shows before the loader. Exomizer packs
3
+ // the loader as it is in memory before it starts (the plain loader file, or
4
+ // the parts of the packed form), the bytes of the stage that the start screen
5
+ // uses at their own addresses, the bytes of the loader that those cover in a
6
+ // stash, and the show code into one loader.prg. The show code shows the
7
+ // screen with start-screen.asm, calls the routine of the stage, waits for a
8
+ // key or a fire button, stops the interrupts of the stage, sets the CIAs as
9
+ // RUN leaves them, copies the stash back and starts the loader.
10
+
11
+ import { type AddressRange, formatC64Address, formatC64Byte } from "#src/c64.ts";
12
+ import { WireFailure } from "#src/host-client/tools.ts";
13
+ import type { StartScreenRow } from "#src/knowledge/read.ts";
14
+ import { placePieces, START_SCREEN_FILE, startScreenValues, subtract, type StartScreenPlan } from "./loader.ts";
15
+
16
+ /** Bytes at an address. */
17
+ export interface StagePart {
18
+ start: number;
19
+ body: Uint8Array;
20
+ }
21
+
22
+ export interface StageInput {
23
+ /** The loader as it is in memory before it starts: the plain loader file, or the parts of the packed form. */
24
+ inner: StagePart[];
25
+ /** Where the loader starts, and the value of $01 that it needs there: $080d with $37, or the copier of the packed form with $34. */
26
+ innerStart: number;
27
+ innerPort: number;
28
+ /** The bytes of the stage that the start screen uses, in address order, at $0400 or higher. */
29
+ stage: StagePart[];
30
+ row: StartScreenRow;
31
+ plan: StartScreenPlan;
32
+ /** The lines that include switches.asm or config.asm, for START_SCREEN. */
33
+ settings: string[];
34
+ header: string[];
35
+ /** The bytes of the show code without its copy table (stageCodeSize). */
36
+ codeSize: number;
37
+ }
38
+
39
+ export interface StagePlan {
40
+ /** Where the show code runs. */
41
+ code: number;
42
+ /** The parts of the loader under the bytes of the stage: where the show code copies them back from. */
43
+ stash: Array<{ target: AddressRange; at: number }>;
44
+ /** start_screen_stage.asm: the show code at `code`, then each part of the stage, packed after it. */
45
+ text: string;
46
+ }
47
+
48
+ /** The file of the show stage in the generated source. */
49
+ export const STAGE_FILE = "start_screen_stage.asm";
50
+ /** The labels of the show stage: its start and the end of the show code. */
51
+ export const STAGE_START = "c64_source_stage";
52
+ export const STAGE_CODE_END = "c64_source_stage_code_end";
53
+ /** The label of a part of the stage: its first byte and the byte after it in the build of start_screen_stage.asm. */
54
+ export const stageLabel = (start: number, part: "from" | "end") => `stage_${part}_${start.toString(16).padStart(4, "0")}`;
55
+
56
+ /** One copy of the stash: the length, where it is read and where it is written. */
57
+ const COPY_ENTRY = 6;
58
+ /** X indexes the table, so the table holds at most this many copies in 256 bytes. */
59
+ const MAX_COPIES = Math.floor((256 - 2) / COPY_ENTRY);
60
+ /** Where the show code can run: RAM that no ROM and no I/O hides when $01 is $37, as for the call of the stage. */
61
+ const CODE_AREAS: readonly AddressRange[] = [
62
+ { start: 0x0400, end: 0x9fff },
63
+ { start: 0xc000, end: 0xcfff },
64
+ ];
65
+ /** The RAM that the stash can use: the copy runs with all RAM in, and not on the hardware vectors. */
66
+ const STASH_AREA: AddressRange = { start: 0x0400, end: 0xfff9 };
67
+ /** The free ranges that a refusal names. */
68
+ const SHOWN_FREE = 8;
69
+
70
+ const show = (range: AddressRange) => `${formatC64Address(range.start)}-${formatC64Address(range.end)}`;
71
+ const rangeOf = (part: StagePart): AddressRange => ({ start: part.start, end: part.start + part.body.length - 1 });
72
+
73
+ /** The ranges in address order, with each run of touching or overlapping ranges as one. */
74
+ function merged(ranges: readonly AddressRange[]): AddressRange[] {
75
+ const out: AddressRange[] = [];
76
+ for (const range of [...ranges].sort((a, b) => a.start - b.start)) {
77
+ const last = out.at(-1);
78
+ if (last !== undefined && range.start <= last.end + 1) last.end = Math.max(last.end, range.end);
79
+ else out.push({ ...range });
80
+ }
81
+ return out;
82
+ }
83
+
84
+ /** The byte of the parts at an address; the last part that holds it wins. */
85
+ function byteOf(parts: readonly StagePart[], address: number): number {
86
+ for (let index = parts.length - 1; index >= 0; index--) {
87
+ const part = parts[index]!;
88
+ if (address >= part.start && address < part.start + part.body.length) return part.body[address - part.start]!;
89
+ }
90
+ throw new WireFailure("operation-failed", `No part holds ${formatC64Address(address)}. This is a defect of the script.`);
91
+ }
92
+
93
+ /**
94
+ * Plans the show stage: the show code in a free range where it can run with
95
+ * the KERNAL in, and the bytes of the loader under the stage in the other free
96
+ * ranges. Refuses by name when the memory is too small.
97
+ */
98
+ export function planStage(input: StageInput): StagePlan {
99
+ const used = merged([...input.inner.map(rangeOf), ...input.stage.map(rangeOf)]);
100
+ const free = subtract(STASH_AREA, used);
101
+ const covered = merged(input.inner.map(rangeOf)).flatMap((inner) => input.stage.map(rangeOf).flatMap((part) => (part.start <= inner.end && inner.start <= part.end ? [{ start: Math.max(part.start, inner.start), end: Math.min(part.end, inner.end) }] : [])));
102
+ const stashed = merged(covered).map((range) => ({ what: `the loader at ${show(range)}`, range, bytes: (address: number) => byteOf(input.inner, address) }));
103
+ const refuse = (why: string): never => {
104
+ const largest = [...free].sort((a, b) => b.end - b.start - (a.end - a.start)).slice(0, SHOWN_FREE);
105
+ const ranges = largest.length === 0 ? "none" : largest.map((range) => `${show(range)} (${range.end - range.start + 1} bytes)`).join(", ");
106
+ throw new WireFailure("invalid-input", `${why} The largest free ranges outside the loader and the bytes of the stage: ${ranges}. Capture the stage with less memory, so that its program holds only the start screen and the routine of the call.`);
107
+ };
108
+ for (let copies = Math.max(stashed.length, 1); copies <= MAX_COPIES; ) {
109
+ const size = input.codeSize + COPY_ENTRY * copies + 2;
110
+ let best: { code: number; pieces: NonNullable<ReturnType<typeof placePieces>> } | undefined;
111
+ for (const range of free) {
112
+ for (const area of CODE_AREAS) {
113
+ const start = Math.max(range.start, area.start);
114
+ const code = { start, end: start + size - 1 };
115
+ if (code.end > Math.min(range.end, area.end)) continue;
116
+ const pieces = placePieces(free.flatMap((other) => subtract(other, [code])), stashed, (other) => Math.abs(other.start - code.start));
117
+ if (pieces !== undefined && (best === undefined || pieces.length < best.pieces.length)) best = { code: code.start, pieces };
118
+ }
119
+ }
120
+ if (best === undefined) return refuse(`The show stage needs ${size} free bytes for its code in $0400-$9fff or $c000-$cfff, and ${stashed.reduce((sum, item) => sum + item.range.end - item.range.start + 1, 0)} more free bytes for the bytes of the loader under the stage.`);
121
+ if (best.pieces.length <= copies) {
122
+ const stash = best.pieces.map((piece) => ({ target: piece.target, at: piece.at }));
123
+ return { code: best.code, stash, text: stageText(input, best.code, stash) };
124
+ }
125
+ copies = best.pieces.length;
126
+ }
127
+ return refuse(`The show stage needs more than ${MAX_COPIES} copies for the bytes of the loader under the stage.`);
128
+ }
129
+
130
+ /** The bytes as !byte lines of 16. */
131
+ function byteLines(part: StagePart): string[] {
132
+ const lines: string[] = [];
133
+ for (let offset = 0; offset < part.body.length; offset += 16) lines.push(` !byte ${[...part.body.subarray(offset, offset + 16)].map(formatC64Byte).join(",")}`);
134
+ return lines;
135
+ }
136
+
137
+ /** start_screen_stage.asm with the show code at `code` and the stash table of `stash`. */
138
+ export function stageText(input: StageInput, code: number, stash: StagePlan["stash"]): string {
139
+ const { row } = input;
140
+ const lines = [
141
+ ...input.header.map((line) => `; ${line}`),
142
+ `; The show stage of the start screen from ${row.stage}: Exomizer packs it with the loader into loader.prg, with --entry '${formatC64Address(code)}'.`,
143
+ "; It shows the start screen, waits for a key or a fire button, copies back the bytes of the loader that the stage covers, and starts the loader.",
144
+ "",
145
+ "!cpu 6510",
146
+ ...input.settings,
147
+ "",
148
+ ...startScreenValues(row, input.plan),
149
+ ...(row.call === undefined ? [] : [`c64_source_ss_call = ${formatC64Address(row.call)} ; the routine of ${row.stage} that the start screen calls before it waits`]),
150
+ "",
151
+ `* = ${formatC64Address(code)}`,
152
+ STAGE_START,
153
+ " sei",
154
+ `!zone c64_source_start_screen {`,
155
+ `!source "${START_SCREEN_FILE}"`,
156
+ "}",
157
+ "!zone c64_source_stage {",
158
+ " sei",
159
+ " lda #$35",
160
+ " sta $01",
161
+ " ; The interrupts and the sound of the stage stop, and the display is off.",
162
+ " lda #$7f",
163
+ " sta $dc0d",
164
+ " sta $dd0d",
165
+ " lda #$00",
166
+ " sta $d01a",
167
+ " sta $d418",
168
+ " sta $d015",
169
+ " lda #$0f",
170
+ " sta $d019",
171
+ " lda #$0b",
172
+ " sta $d011",
173
+ " ; The CIA timers as RUN leaves them, as the loader expects: CIA 1 timer A",
174
+ " ; runs with the latch $4025 for the interrupt, the others stop at $ffff.",
175
+ " lda #$00",
176
+ " sta $dc0e",
177
+ " sta $dc0f",
178
+ " sta $dd0e",
179
+ " sta $dd0f",
180
+ " lda #$ff",
181
+ " sta $dc06",
182
+ " sta $dc07",
183
+ " sta $dd04",
184
+ " sta $dd05",
185
+ " sta $dd06",
186
+ " sta $dd07",
187
+ " lda #$08",
188
+ " sta $dc0f",
189
+ " sta $dd0e",
190
+ " sta $dd0f",
191
+ " lda #$25",
192
+ " sta $dc04",
193
+ " lda #$40",
194
+ " sta $dc05",
195
+ " lda #$11",
196
+ " sta $dc0e",
197
+ " lda #$81",
198
+ " sta $dc0d",
199
+ " lda $dc0d",
200
+ " lda $dd0d",
201
+ " ; The stash goes back to the loader, with all RAM in.",
202
+ " lda #$34",
203
+ " sta $01",
204
+ " ldx #0",
205
+ ".next lda .table,x",
206
+ " ora .table+1,x",
207
+ " beq .done",
208
+ " lda .table,x",
209
+ " sta .count",
210
+ " lda .table+1,x",
211
+ " sta .count+1",
212
+ " lda .table+2,x",
213
+ " sta .read+1",
214
+ " lda .table+3,x",
215
+ " sta .read+2",
216
+ " lda .table+4,x",
217
+ " sta .write+1",
218
+ " lda .table+5,x",
219
+ " sta .write+2",
220
+ " ldy #0",
221
+ ".read lda $ffff,y",
222
+ ".write sta $ffff,y",
223
+ " iny",
224
+ " bne +",
225
+ " inc .read+2",
226
+ " inc .write+2",
227
+ "+ lda .count",
228
+ " bne +",
229
+ " dec .count+1",
230
+ "+ dec .count",
231
+ " lda .count",
232
+ " ora .count+1",
233
+ " bne .read",
234
+ " txa",
235
+ " clc",
236
+ ` adc #${COPY_ENTRY}`,
237
+ " tax",
238
+ " jmp .next",
239
+ `.done lda #${formatC64Byte(input.innerPort)}`,
240
+ " sta $01",
241
+ ` jmp ${formatC64Address(input.innerStart)}`,
242
+ ".count !word 0",
243
+ ".table",
244
+ ...stash.map((item) => ` !word ${item.target.end - item.target.start + 1}, ${formatC64Address(item.at)}, ${formatC64Address(item.target.start)} ; ${show(item.target)} of the loader`),
245
+ " !word 0",
246
+ "}",
247
+ STAGE_CODE_END,
248
+ ];
249
+ for (const part of input.stage) {
250
+ lines.push("", `; ${row.stage} ${show(rangeOf(part))}`, stageLabel(part.start, "from"), `!pseudopc ${formatC64Address(part.start)} {`, ...byteLines(part), "}", stageLabel(part.start, "end"));
251
+ }
252
+ return `${lines.join("\n")}\n`;
253
+ }
254
+
255
+ /**
256
+ * What Exomizer packs: the loader without the bytes under the stage, the
257
+ * stash, the bytes of the stage at their addresses and the show code. `build`
258
+ * is the build of start_screen_stage.asm; `labels` gives its labels.
259
+ */
260
+ export function stageParts(input: StageInput, plan: StagePlan, build: Uint8Array, labels: (name: string) => number): StagePart[] {
261
+ const start = build[0]! | (build[1]! << 8);
262
+ const slice = (from: number, to: number) => build.subarray(2 + from - start, 2 + to - start);
263
+ const stageRanges = input.stage.map(rangeOf);
264
+ const loader = input.inner.flatMap((part) => subtract(rangeOf(part), stageRanges).map((range) => ({ start: range.start, body: part.body.subarray(range.start - part.start, range.end - part.start + 1) })));
265
+ const stash = plan.stash.map((item) => ({ start: item.at, body: Uint8Array.from({ length: item.target.end - item.target.start + 1 }, (_, offset) => byteOf(input.inner, item.target.start + offset)) }));
266
+ const stage = input.stage.map((part) => ({ start: part.start, body: slice(labels(stageLabel(part.start, "from")), labels(stageLabel(part.start, "end"))) }));
267
+ return [...loader, ...stash, ...stage, { start: plan.code, body: slice(plan.code, labels(STAGE_CODE_END)) }];
268
+ }
@@ -12,6 +12,11 @@
12
12
  ; c64_source_ss_colour_mode: 0 no colour RAM, 1 one colour, 2 a copy of 1000 bytes
13
13
  ; c64_source_ss_colour: the one colour (mode 1)
14
14
  ; c64_source_ss_colours: the address of the 1000 bytes (mode 2)
15
+ ; The show stage of a start screen from a captured stage can also set:
16
+ ; c64_source_ss_call: a routine of the stage, for example the start of the
17
+ ; loading tune. The code calls it with the KERNAL in and the interrupts on,
18
+ ; after the screen shows and before the wait. The interrupts stay on in the
19
+ ; wait, so each read of the keyboard and the joysticks is done with SEI.
15
20
 
16
21
  !ifndef START_SCREEN { START_SCREEN = 1 }
17
22
 
@@ -78,12 +83,23 @@
78
83
  }
79
84
  lda #c64_source_ss_d011
80
85
  sta $d011
86
+ !ifdef c64_source_ss_call {
87
+ lda #$37
88
+ sta $01
89
+ cli
90
+ jsr c64_source_ss_call
91
+ }
81
92
 
82
93
  ; Y is the phase: 0 waits until nothing is pressed, for example the
83
94
  ; RETURN of RUN; 1 waits for a press; 2 waits until it is released.
84
95
  loader_start_screen_wait
85
96
  ldy #0
86
- .poll lda #$ff
97
+ .poll
98
+ !ifdef c64_source_ss_call {
99
+ ; An interrupt of the stage must not write $dc00 between a write and a read.
100
+ sei
101
+ }
102
+ lda #$ff
87
103
  sta $dc00
88
104
  ; No keyboard row is driven: $dc01 gives joystick 1 alone.
89
105
  lda $dc01
@@ -107,12 +123,19 @@ loader_start_screen_wait
107
123
  lda $dc01
108
124
  and .joy
109
125
  bne .down
126
+ !ifdef c64_source_ss_call {
127
+ cli
128
+ }
110
129
  tya
111
130
  beq .settle
112
131
  cmp #2
113
132
  beq loader_start_screen_continue
114
133
  bne .poll
115
- .down cpy #1
134
+ .down
135
+ !ifdef c64_source_ss_call {
136
+ cli
137
+ }
138
+ cpy #1
116
139
  bne .poll
117
140
  iny
118
141
  bne .poll
@@ -130,6 +153,9 @@ loader_start_screen_wait
130
153
  bne .poll
131
154
 
132
155
  loader_start_screen_continue
156
+ !ifdef c64_source_ss_call {
157
+ sei
158
+ }
133
159
  lda .port
134
160
  sta $dc00
135
161
  lda .ddra