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

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.
@@ -9,12 +9,15 @@
9
9
  // patch is written as !if SWITCH { code } else { original }, with the
10
10
  // condition of the state that the program does not have, and a macro use as
11
11
  // one +name args line. config.asm holds the value of each switch, and
12
- // switches.asm the values of each release.
12
+ // switches.asm the values of each release. A range of leftovers (bytes that
13
+ // nothing uses) is a file of its own, in the build only with LEFTOVERS = 1:
14
+ // each release has it, the default build does not, and the loader never
15
+ // loads it.
13
16
 
14
17
  import { type AddressRange, COVERAGE, formatC64Address, formatC64Byte, POINTER_TABLE_TYPES, pointerTableEntries, type PointerTableType } from "#src/c64.ts";
15
18
  import { WireFailure } from "#src/host-client/tools.ts";
16
19
  import { identifiers, operandText, replaceIdentifiers, type OperandExpression } from "#src/knowledge/expression.ts";
17
- import { type CommentPlacement, isAnalyzer, isGeneratedFile, RELEASE_DEFINE, START_SCREEN_SWITCH, type Origin, type RegionType, type SwitchKind } from "#src/knowledge/schema.ts";
20
+ import { type CommentPlacement, isAnalyzer, isGeneratedFile, LEFTOVERS_SWITCH, RELEASE_DEFINE, START_SCREEN_SWITCH, type Origin, type RegionType, type SwitchKind } from "#src/knowledge/schema.ts";
18
21
  import type { Area } from "#src/platform.ts";
19
22
  import { hasZeroPageForm, instructionWith, labelProblem, MODE_LENGTH, OPCODES, takesAddress, USE_KINDS, useKind, type UseKind } from "#src/opcodes.ts";
20
23
 
@@ -59,6 +62,8 @@ export interface SourceInput {
59
62
  macros?: ReadonlyArray<{ name: string; params: readonly string[]; body: string; comment?: string }>;
60
63
  /** Macro uses of the knowledge: the instructions of a range, written as one use of a macro. */
61
64
  macroUses?: ReadonlyArray<{ start: number; end: number; macro: string; args: readonly string[] }>;
65
+ /** The leftovers: ranges inside the segments that nothing uses, in address order. Each one is a file that only LEFTOVERS = 1 builds. */
66
+ leftovers?: readonly AddressRange[];
62
67
  }
63
68
 
64
69
  /** A macro use that the source writes, with its args in the names of this source. */
@@ -213,6 +218,8 @@ export interface Piece {
213
218
  title: string;
214
219
  /** True when the piece starts a segment: the source sets the address there. */
215
220
  segmentStart: boolean;
221
+ /** True for a range of leftovers: only LEFTOVERS = 1 builds it, and the loader does not load it. */
222
+ leftover: boolean;
216
223
  items: Item[];
217
224
  }
218
225
 
@@ -254,6 +261,8 @@ export interface Layout {
254
261
  macrosAsInstructions: MacroAsInstructions[];
255
262
  /** The knowledge has a start screen: config.asm gets START_SCREEN. */
256
263
  startScreen: boolean;
264
+ /** The ranges of leftovers: config.asm gets LEFTOVERS. */
265
+ leftovers: AddressRange[];
257
266
  }
258
267
 
259
268
  /** ACME reads leading zeros as a 16-bit value, so a zero-page name gets two digits. */
@@ -305,19 +314,32 @@ export function layout(input: SourceInput): Layout {
305
314
  // A patch starts and ends at an item boundary, so a data run stops there.
306
315
  const patches = [...(input.patches ?? [])].sort((a, b) => a.start - b.start);
307
316
  const macroUses = [...(input.macroUses ?? [])].sort((a, b) => a.start - b.start);
308
- const cuts = new Set([...patches, ...macroUses].flatMap((range) => [range.start, range.end + 1]));
317
+ const leftovers = (input.leftovers ?? []).filter((range) => input.segments.some((segment) => range.start >= segment.start && range.end <= segment.end));
318
+ const cuts = new Set([...patches, ...macroUses, ...leftovers].flatMap((range) => [range.start, range.end + 1]));
309
319
 
310
320
  const pieces: Piece[] = [];
321
+ const hex = (address: number) => address.toString(16).padStart(4, "0");
311
322
  for (const segment of input.segments) {
312
- const starts = modules.filter((module) => module.start > segment.start && module.start <= segment.end).map((module) => module.start);
313
- const bounds = [segment.start, ...starts, segment.end + 1];
323
+ const inside = (address: number) => address > segment.start && address <= segment.end;
324
+ // A range of leftovers is one file: a module that starts in it goes on after it.
325
+ const inLeftovers = (address: number) => leftovers.some((range) => address > range.start && address <= range.end);
326
+ const starts = [...modules.map((module) => module.start).filter((start) => !inLeftovers(start)), ...leftovers.flatMap((range) => [range.start, range.end + 1])].filter(inside);
327
+ const bounds = [...new Set([segment.start, ...starts, segment.end + 1])].sort((a, b) => a - b);
314
328
  for (let index = 0; index + 1 < bounds.length; index++) {
315
329
  const start = bounds[index]!;
316
330
  const end = bounds[index + 1]! - 1;
331
+ const segmentTitle = `Segment ${formatC64Address(segment.start)}-${formatC64Address(segment.end)}`;
332
+ if (leftovers.some((range) => range.start === start)) {
333
+ pieces.push({ start, end, file: `leftover_${hex(start)}.asm`, title: `Leftovers ${formatC64Address(start)}-${formatC64Address(end)}: ${end - start + 1} bytes that nothing uses. Only LEFTOVERS = 1 builds them.`, segmentStart: index === 0, leftover: true, items: [] });
334
+ continue;
335
+ }
336
+ // A module that leftovers cut goes on in a file of its own after them: <module>_xxxx.asm.
317
337
  const module = modules.find((row) => row.start === start);
318
- const file = module?.file ?? `segment_${start.toString(16).padStart(4, "0")}.asm`;
319
- const title = module?.title?.replace(/\s*\r?\n\s*/g, " ") ?? `Segment ${formatC64Address(segment.start)}-${formatC64Address(segment.end)}`;
320
- pieces.push({ start, end, file, title, segmentStart: index === 0, items: [] });
338
+ const holder = module ?? modules.filter((row) => row.start >= segment.start && row.start < start).at(-1);
339
+ const title = (text: string | undefined) => text?.replace(/\s*\r?\n\s*/g, " ");
340
+ const file = module?.file ?? (holder === undefined ? `segment_${hex(start)}.asm` : `${holder.file.replace(/\.asm$/i, "")}_${hex(start)}.asm`);
341
+ const named = module === undefined && holder !== undefined ? `${title(holder.title) ?? holder.file}, from ${formatC64Address(start)}` : title(module?.title);
342
+ pieces.push({ start, end, file, title: named ?? segmentTitle, segmentStart: index === 0, leftover: false, items: [] });
321
343
  }
322
344
  }
323
345
  const files = new Map<string, number>();
@@ -658,6 +680,7 @@ export function layout(input: SourceInput): Layout {
658
680
  macroUses: usesWritten,
659
681
  macrosAsInstructions: macrosAsInstructions.sort((a, b) => a.start - b.start),
660
682
  startScreen: input.startScreen === true,
683
+ leftovers: [...leftovers],
661
684
  };
662
685
  }
663
686
 
@@ -901,11 +924,11 @@ const SWITCH_GROUPS: ReadonlyArray<{ kind: SwitchKind; title: string }> = [
901
924
  { kind: "change", title: "Other changes" },
902
925
  ];
903
926
 
904
- /** True when the source has switches.asm: the knowledge has switches or releases. */
905
- export const hasSwitchFile = (layout: Pick<Layout, "switches" | "releases">) => layout.switches.length > 0 || layout.releases.length > 0;
927
+ /** True when the source has switches.asm: the knowledge has switches or releases, or the source has leftovers, which main.asm builds by LEFTOVERS. */
928
+ export const hasSwitchFile = (layout: Pick<Layout, "switches" | "releases" | "leftovers">) => layout.switches.length > 0 || layout.releases.length > 0 || layout.leftovers.length > 0;
906
929
 
907
- /** True when the source has config.asm: the knowledge has switches or a start screen. */
908
- export const hasConfigFile = (layout: Pick<Layout, "switches" | "startScreen">) => layout.switches.length > 0 || layout.startScreen;
930
+ /** True when the source has config.asm: the knowledge has switches or a start screen, or the source has leftovers. */
931
+ export const hasConfigFile = (layout: Pick<Layout, "switches" | "startScreen" | "leftovers">) => layout.switches.length > 0 || layout.startScreen || layout.leftovers.length > 0;
909
932
 
910
933
  /** The file with the value of each switch. The user edits it, and generate keeps the values. */
911
934
  export const CONFIG_FILE = "config.asm";
@@ -928,7 +951,7 @@ export function configValues(text: string): Map<string, 0 | 1> {
928
951
  * kind. Without a value, a switch has its default. A comment tells the
929
952
  * releases that have the switch on, and its patches.
930
953
  */
931
- export function configText(layout: Pick<Layout, "switches" | "patches" | "startScreen">, values: ReadonlyMap<string, number> = new Map()): string {
954
+ export function configText(layout: Pick<Layout, "switches" | "patches" | "startScreen" | "leftovers">, values: ReadonlyMap<string, number> = new Map()): string {
932
955
  const patches = switchesOf(layout);
933
956
  const out = [
934
957
  "; Generated by c64-source. Edit the values here: generate keeps them.",
@@ -956,6 +979,14 @@ export function configText(layout: Pick<Layout, "switches" | "patches" | "startS
956
979
  if (layout.startScreen) {
957
980
  out.push("", "; Start screen: on by default", `${START_SCREEN_SWITCH} = ${values.get(START_SCREEN_SWITCH) ?? 1} ; the loader shows the start screen and waits for a key or a fire button. 0 starts the program at once`);
958
981
  }
982
+ if (layout.leftovers.length > 0) {
983
+ const bytes = layout.leftovers.reduce((sum, range) => sum + range.end - range.start + 1, 0);
984
+ out.push(
985
+ "",
986
+ "; Leftovers: off by default",
987
+ `${LEFTOVERS_SWITCH} = ${values.get(LEFTOVERS_SWITCH) ?? 0} ; ${bytes} bytes in ${layout.leftovers.length} leftover_xxxx.asm ${layout.leftovers.length === 1 ? "file" : "files"} that nothing uses. 1 builds them, so main.asm gives the bytes of the program. Each release has them. The loader never loads them`,
988
+ );
989
+ }
959
990
  return `${out.join("\n")}\n`;
960
991
  }
961
992
 
@@ -974,6 +1005,7 @@ function switchesText(layout: Layout): string {
974
1005
  out.push("", `; ${release.label}${release.base ? " (the program of the knowledge)" : ""}: ${release.file}${release.comment === undefined ? "" : `. ${release.comment.replace(/\s*\r?\n\s*/g, " ")}`}`);
975
1006
  out.push(`!ifdef ${RELEASE_DEFINE}${release.label} {`);
976
1007
  for (const row of layout.switches) out.push(`${INDENT}${row.name} = ${row.releases.includes(release.label) ? 1 : 0}`);
1008
+ if (layout.leftovers.length > 0) out.push(`${INDENT}${LEFTOVERS_SWITCH} = 1`);
977
1009
  out.push("}");
978
1010
  }
979
1011
  if (hasConfigFile(layout)) {
@@ -1310,7 +1342,8 @@ export function render(layout: Layout, asBytes: ReadonlySet<string>, header: str
1310
1342
  const main = [...header.map((line) => `; ${line}`), "", "!cpu 6510", ...(hasSwitchFile(layout) ? ['!source "switches.asm"'] : []), '!source "constants.asm"', ...(layout.macros.length > 0 ? ['!source "macros.asm"'] : [])];
1311
1343
  for (const piece of layout.pieces) {
1312
1344
  if (piece.segmentStart) main.push("", `* = ${formatC64Address(piece.start)}`);
1313
- main.push(`!source "${piece.file}"`);
1345
+ // Without the leftovers, the build skips their bytes: the addresses after them stay.
1346
+ main.push(piece.leftover ? `!if ${LEFTOVERS_SWITCH} { !source "${piece.file}" } else { !skip ${piece.end - piece.start + 1} }` : `!source "${piece.file}"`);
1314
1347
  }
1315
1348
  result.files.push({ name: "main.asm", text: `${main.join("\n")}\n` });
1316
1349
  if (coverage !== undefined) result.notExecuted = notExecuted;
@@ -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