@henols/c64-re-tools 2.8.0-rc.4 → 2.8.0-rc.6

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.
Files changed (67) hide show
  1. package/package.json +1 -1
  2. package/skills/c64-assembler/SKILL.md +1 -1
  3. package/skills/c64-assembler/scripts/assembler.ts +78 -24
  4. package/skills/c64-docs/scripts/docs.ts +31 -10
  5. package/skills/c64-knowledge/SKILL.md +11 -7
  6. package/skills/c64-knowledge/references/batches.md +2 -1
  7. package/skills/c64-knowledge/references/source-knowledge.md +1 -1
  8. package/skills/c64-knowledge/scripts/knowledge.ts +46 -29
  9. package/skills/c64-packer/scripts/packer.ts +9 -2
  10. package/skills/c64-provenance/SKILL.md +2 -2
  11. package/skills/c64-provenance/scripts/lib/runtime.ts +45 -10
  12. package/skills/c64-provenance/scripts/provenance.ts +10 -3
  13. package/skills/c64-source/references/loader.md +8 -7
  14. package/skills/c64-source/scripts/lib/loader.ts +4 -111
  15. package/skills/c64-source/scripts/lib/source.ts +3 -1
  16. package/skills/c64-source/scripts/source.ts +49 -78
  17. package/skills/c64-static-analysis/SKILL.md +2 -2
  18. package/skills/c64-static-analysis/scripts/lib/coverage.ts +56 -4
  19. package/skills/c64-static-analysis/scripts/lib/idioms.ts +92 -42
  20. package/skills/c64-static-analysis/scripts/static-analysis.ts +32 -21
  21. package/skills/c64-testing/SKILL.md +7 -0
  22. package/skills/c64-testing/scripts/lib/play.ts +8 -7
  23. package/skills/c64-testing/scripts/lib/recording.ts +278 -27
  24. package/skills/c64-testing/scripts/lib/replay.ts +9 -8
  25. package/skills/c64-testing/scripts/lib/runs.ts +5 -1
  26. package/skills/c64-testing/scripts/lib/scenario.ts +4 -0
  27. package/skills/c64-testing/scripts/testing.ts +30 -11
  28. package/skills/c64-unpacker/SKILL.md +13 -11
  29. package/skills/c64-unpacker/scripts/lib/loads.ts +10 -5
  30. package/skills/c64-unpacker/scripts/lib/machine.ts +163 -3
  31. package/skills/c64-unpacker/scripts/lib/stages.ts +13 -6
  32. package/skills/c64-unpacker/scripts/unpacker.ts +34 -15
  33. package/skills/emulator/SKILL.md +2 -2
  34. package/src/c64.ts +21 -0
  35. package/src/host/vice/adapter.ts +2 -1
  36. package/src/host/vice/chips.ts +6 -9
  37. package/src/host/vice/session.ts +2 -2
  38. package/src/host-client/image.ts +40 -4
  39. package/src/host-client/tools.ts +1 -1
  40. package/src/host-client/transfer.ts +48 -26
  41. package/src/host-client/vice-session.ts +15 -0
  42. package/src/knowledge/database.ts +72 -19
  43. package/src/knowledge/import.ts +32 -7
  44. package/src/knowledge/join.ts +50 -2
  45. package/src/knowledge/names.ts +2 -2
  46. package/src/knowledge/program.ts +103 -24
  47. package/src/knowledge/schema.ts +6 -0
  48. package/src/knowledge/start-screen.ts +57 -7
  49. package/src/knowledge/write.ts +20 -13
  50. package/src/loader.ts +101 -33
  51. package/src/mcp/output.ts +13 -12
  52. package/src/mcp/server.ts +33 -22
  53. package/src/mcp/tools/debug.ts +21 -24
  54. package/src/mcp/tools/media.ts +27 -27
  55. package/src/mcp/tools/memory.ts +13 -17
  56. package/src/mcp/tools/video.ts +10 -12
  57. package/src/native/c64run.ts +39 -1
  58. package/src/native/decompiler/analyze.ts +1 -1
  59. package/src/native/dxa.ts +2 -2
  60. package/src/native/types.ts +26 -7
  61. package/src/opcodes.ts +5 -0
  62. package/src/program-code.ts +4 -2
  63. package/src/project.ts +224 -71
  64. package/src/protocol/vice.ts +5 -3
  65. package/src/script.ts +60 -15
  66. package/{skills/c64-source/scripts/lib → src}/stage.ts +142 -11
  67. package/tools/c64run.c +18 -5
@@ -45,7 +45,7 @@ To get the command list, run the script with no arguments.
45
45
 
46
46
  - `compare` takes two or more releases. A release is one of these:
47
47
  - `knowledge`: the program in the project knowledge.
48
- - The label of a release in the project knowledge, for example `V2`. The base release is the program in the project knowledge. For another release, the project knowledge keeps only its bytes. Thus `--frames` cannot start it. To run that release, give its file. A label that is not a release gets error `not-found`, with the labels of the releases.
48
+ - The label of a release in the project knowledge, for example `V2`. The base release is the program in the project knowledge. For another release, the project knowledge keeps only its bytes. Thus `--frames` cannot start it. To run that release, give its PRG when the PRG holds the bytes of the release. Else, give the `image.json` of a capture of the release. The error tells you which file to give. A label that is not a release gets error `not-found`, with the labels of the releases.
49
49
  - A PRG file. The script puts it at its load address.
50
50
  - The `image.json` of a capture folder. Keep all files of the capture folder together. Without them, `--frames` cannot start the release.
51
51
  - Get a PRG out of a disk image before you compare it. The script refuses a disk image, a tape image and a cartridge image.
@@ -125,7 +125,7 @@ With `--shift`, the addresses of `segments`, `common` and `differences` are the
125
125
  With `--frames`, the body also holds:
126
126
 
127
127
  - `runtime` in each difference: one item for each release that started, by the number of the release. `start` and `end` are the addresses in the release, without the shift.
128
- - `first`: the first access in that release: `read`, `write`, `execute` or `none`. An instruction that reads and then writes the same byte, for example INC, counts as `read`. The operand bytes of an executed instruction count as `execute`, for example the `$03` of `LDA #$03`.
128
+ - `first`: the first access in that release: `read`, `write`, `read-write`, `execute` or `none`. An instruction that reads and then writes the same byte, for example INC, counts as `read`. A store counts as `write`, also an indexed store that first makes a dummy read of the byte, for example `STA ($06),Y`. The operand bytes of an executed instruction count as `execute`, for example the `$03` of `LDA #$03`. During a `press` step, no access stops the run. Thus a range that the program reads and writes during a `press` step counts as `read-write`: the order is not known.
129
129
  - `changed`: `true` when the bytes at the end of the run are not the bytes of the release.
130
130
  - `run`: `frames`, `steps` and `releases`. `releases` tells for each release, by its number, if it started, or the `reason` that it did not start. `skipped` tells that the releases have no differences, so no release ran.
131
131
 
@@ -2,7 +2,8 @@
2
2
  // runs in its own emulator, and each differing range gets its first access
3
3
  // (read, write, execute or none) and whether its bytes changed by the end.
4
4
  // Watchpoints and range breakpoints stop the run at an access; the coverage
5
- // map then tells which range and which access it was.
5
+ // map then tells which range and which access it was, and the CPU history
6
+ // tells which instruction made a read and a write.
6
7
 
7
8
  import { type AddressRange, COVERAGE, EXECUTE, instructionLength, READ, WRITE } from "#src/c64.ts";
8
9
  import { runProgram } from "#src/host-client/autostart.ts";
@@ -10,10 +11,11 @@ import { readCoverage } from "#src/host-client/coverage.ts";
10
11
  import { startAtEntry, type EntryState } from "#src/host-client/entry-state.ts";
11
12
  import { runActionStep, type ActionStep } from "#src/host-client/steps.ts";
12
13
  import { knowledgeAt } from "#src/knowledge/names.ts";
14
+ import { isStore } from "#src/opcodes.ts";
13
15
  import { readProjectFile } from "#src/host-client/transfer.ts";
14
- import { readMemoryInParts, type ViceSessionClient } from "#src/host-client/vice-session.ts";
16
+ import { lastOpcode, readMemoryInParts, type ViceSessionClient } from "#src/host-client/vice-session.ts";
15
17
 
16
- export type FirstAccess = "read" | "write" | "execute" | "none";
18
+ export type FirstAccess = "read" | "write" | "read-write" | "execute" | "none";
17
19
 
18
20
  export interface RangeAccess {
19
21
  first: FirstAccess;
@@ -28,9 +30,16 @@ export type Start =
28
30
 
29
31
  const MAX_WATCH = 256;
30
32
 
31
- /** The first access that coverage flags show; a read-modify-write reads first. */
32
- export function accessOf(flags: number): FirstAccess {
33
+ /**
34
+ * The first access that coverage flags show. A read and a write at one stop
35
+ * are of the instruction that made the access (`opcode`, when the CPU history
36
+ * gives it): an indexed store makes a dummy read before its write, and a
37
+ * read-modify-write reads first. Without the instruction (the taps of a press,
38
+ * the end of the run), the order of a read and a write is not known: read-write.
39
+ */
40
+ export function accessOf(flags: number, opcode?: number): FirstAccess {
33
41
  if ((flags & EXECUTE) !== 0) return "execute";
42
+ if ((flags & READ) !== 0 && (flags & WRITE) !== 0) return opcode === undefined ? "read-write" : isStore(opcode) ? "write" : "read";
34
43
  if ((flags & READ) !== 0) return "read";
35
44
  if ((flags & WRITE) !== 0) return "write";
36
45
  return "none";
@@ -76,16 +85,30 @@ export async function watchRelease(
76
85
  points.set(index, ids);
77
86
  }
78
87
  await client.memmap({ action: "clear" });
88
+ /** Switches each point that has not settled on or off. */
89
+ const switchPoints = async (action: "enable" | "disable") => {
90
+ for (const ids of points.values()) {
91
+ for (const point of ids) {
92
+ if (point.kind === "watch") await client.watchpoint({ action, id: point.id });
93
+ else await client.breakpoint({ action, id: point.id });
94
+ }
95
+ }
96
+ };
79
97
 
80
98
  /**
81
99
  * After a stop: the ranges that the coverage shows accessed get their first
82
100
  * access, and lose their points. VICE marks only the opcode byte of an
83
101
  * executed instruction, so an instruction that starts up to two bytes before
84
- * a range, or in it, executes its operand bytes too.
102
+ * a range, or in it, executes its operand bytes too. A stop at a watchpoint
103
+ * comes after the instruction that made the access, so at a stop (`atPoint`)
104
+ * the last entry of the CPU history is that instruction. Each stop settles
105
+ * before the run goes on, so the flags of a range that has not settled are
106
+ * of the accesses since the last stop.
85
107
  */
86
- const settle = async (): Promise<boolean> => {
108
+ const settle = async (atPoint = true): Promise<boolean> => {
87
109
  const map = await readCoverage(client);
88
110
  let found = false;
111
+ let instruction: Promise<number | undefined> | undefined;
89
112
  for (const [index, ids] of points) {
90
113
  const range = ranges[index]!;
91
114
  let flags = 0;
@@ -98,7 +121,8 @@ export async function watchRelease(
98
121
  }
99
122
  }
100
123
  if (accessOf(flags) === "none") continue;
101
- first[index] = accessOf(flags);
124
+ const both = (flags & READ) !== 0 && (flags & WRITE) !== 0;
125
+ first[index] = accessOf(flags, atPoint && both ? await (instruction ??= lastOpcode(client)) : undefined);
102
126
  for (const point of ids) {
103
127
  if (point.kind === "watch") await client.watchpoint({ action: "remove", id: point.id });
104
128
  else await client.breakpoint({ action: "remove", id: point.id });
@@ -117,7 +141,9 @@ export async function watchRelease(
117
141
  while (left > 0 && guard-- > 0) {
118
142
  const ran = await client.execution({ action: "advance-frames", count: left, space: "c64" });
119
143
  left -= ran.advancedFrames ?? left;
120
- if (left > 0 && !(await settle())) left--;
144
+ // A stop at a point settles, also in the last frame.
145
+ const settled = ran.stopReason === "watchpoint" || ran.stopReason === "breakpoint" ? await settle() : false;
146
+ if (left > 0 && !settled) left--;
121
147
  }
122
148
  };
123
149
  const at = knowledgeAt();
@@ -135,10 +161,19 @@ export async function watchRelease(
135
161
  }
136
162
  continue;
137
163
  }
164
+ if ("press" in step) {
165
+ // A stop at a point ends the taps early and lets the keys go: the points are off for the taps, and the coverage of the taps gives the first access.
166
+ await switchPoints("disable");
167
+ await runActionStep(client, step, at);
168
+ await switchPoints("enable");
169
+ await settle(false);
170
+ continue;
171
+ }
172
+ // The other steps do not run the CPU.
138
173
  await runActionStep(client, step, at);
139
174
  }
140
175
  await runFrames(run.frames);
141
- if (points.size > 0) await settle();
176
+ if (points.size > 0) await settle(false);
142
177
 
143
178
  const accesses: RangeAccess[] = [];
144
179
  for (const [index, range] of ranges.entries()) {
@@ -11,7 +11,7 @@
11
11
  import { basename } from "node:path";
12
12
  import type { DatabaseSync } from "node:sqlite";
13
13
 
14
- import { type AddressRange, c64AddressOf, formatC64Address, formatC64Byte, formatC64Range, formatC64RangeFields, parseC64Address } from "#src/c64.ts";
14
+ import { type AddressRange, c64AddressOf, formatC64Address, formatC64Byte, formatC64Range, formatC64RangeFields, parseC64Address, prgOf } from "#src/c64.ts";
15
15
  import { Refusal } from "#src/errors.ts";
16
16
  import { parseEntryState, type EntryState } from "#src/host-client/entry-state.ts";
17
17
  import { readImage } from "#src/host-client/image.ts";
@@ -44,6 +44,7 @@ import {
44
44
  symbolSuppressed,
45
45
  } from "#src/knowledge/read.ts";
46
46
  import { CODE_REGION_TYPES, DEFAULT_SPACE, RELEASE_LABEL, SEMANTIC_ORIGINS } from "#src/knowledge/schema.ts";
47
+ import { sha256 } from "#src/knowledge/text.ts";
47
48
  import { platformAddresses } from "#src/platform.ts";
48
49
  import { decodeProgram, leftoverInput } from "#src/program-code.ts";
49
50
  import { projectRoot } from "#src/project.ts";
@@ -92,8 +93,14 @@ function labeledRelease(label: string): { image: ReleaseImage; noState: string }
92
93
  throw new Refusal("not-found", `There is no release ${label} in the project knowledge. ${known}`, { hint: `Give a release label, knowledge, or the path of a file. For a file with the name ${label}, give ./${label}.` });
93
94
  }
94
95
  if (release.base) return knowledgeProgram(db);
95
- const file = release.file === undefined ? "its file" : `its file ${release.file}`;
96
- return { image: releaseImage(db, label)!, noState: `The knowledge keeps only the bytes of the release ${label}, not its entry state. To run the release, give ${file} in place of ${label}.` };
96
+ const image = releaseImage(db, label)!;
97
+ // A PRG with the bytes of the release runs in its place. The program file that a capture ran holds other bytes, for example packed ones.
98
+ const [segment] = image.segments;
99
+ const runs = release.file !== undefined && image.segments.length === 1 && release.sha256 === sha256(prgOf(segment!.start, Uint8Array.from({ length: segment!.end - segment!.start + 1 }, (_, offset) => image.byteAt(segment!.start + offset))));
100
+ const remedy = runs
101
+ ? `give its file ${release.file} in place of ${label}`
102
+ : `capture the unpacked program${release.file === undefined ? "" : ` of ${release.file}`} with its entry state, and give the image.json of that capture in place of ${label}`;
103
+ return { image, noState: `The knowledge keeps only the bytes of the release ${label}, not its entry state. To run the release, ${remedy}.` };
97
104
  });
98
105
  }
99
106
 
@@ -9,6 +9,7 @@ This file gives the loader program of `generate --out <dir> --loader`, the field
9
9
  - `loader.program` is the loader program. `loader.symbols` is its symbols file. The symbols file holds the labels of `loader.asm`, `main.asm` and `start_screen_stage.asm`. Give the two files to a test of the loader program.
10
10
  - The loader program has the switch values of `config.asm`. With the defaults, it is the clean program: no copy protection, all corrections and no leftovers. The entry state is the state of the base release.
11
11
  - The name of the loader program changes with the switch values. Then the script removes the loader program of the earlier run and its symbols.
12
+ - This can remove `build/clean_loader.prg`, the loader program of the defaults. It is the default build. Then `hint` tells you how to make it again. To keep it, do not change `config.asm`. Get the loader program of other switch values from a build. See "The loader of other switch values".
12
13
  - The loader program sets the entry state of the capture and jumps to the entry. It sets the memory, $00/$01, the registers, the VIC-II, the colour RAM, the raster line and the CIA timers.
13
14
  - The loader changes the free stack byte below the stack pointer (`stackByte`). It does not set the SID, the time of day of the CIAs, the CIA interrupt flags or the interrupt registers of the VIC-II. A program that reads SID voice 3 for random numbers can thus give a different result. When you compare the reference and the rebuild, observe after the program sets its own random numbers.
14
15
  - For the loader program of a release, build `loader.asm` with the build option `--release <label>`. This is possible only when `loader.packed` is not in the result. That loader program also does not have the leftovers.
@@ -23,15 +24,15 @@ This file gives the loader program of `generate --out <dir> --loader`, the field
23
24
 
24
25
  ## The loader of other switch values
25
26
 
26
- To get the loader program of other switch values, build `main.asm` of the source with these switch values, for example `TRAINER=1`. `config.asm` does not change. The build result then has the loader program in `loader.out` and its symbols file in `loader.symbolsFile.out`. No check of this loader program runs, so `loader.verified` is `false`. Test the loader program before you trust it.
27
+ To get the loader program of other switch values, build `main.asm` of the source with these switch values, for example `TRAINER=1`. `config.asm` and the other files of the source do not change. The build result then has the loader program in `loader.out` and its symbols file in `loader.symbolsFile.out`. With a start screen of a captured stage, the loader program also has the show stage of `start_screen_stage.asm`. Then `loader.stage` gives the space of the stage. No check of this loader program runs, so `loader.verified` is `false`. Test the loader program before you trust it.
27
28
 
28
- The build refuses the loader program in these cases:
29
+ The build writes no loader program in these cases. `loader.reason` tells why.
29
30
 
30
- - A segment of the program starts below $0400.
31
- - The switch values change a byte that `loader.asm` holds between its free ranges.
32
- - The start screen is of a captured stage.
31
+ - The switch values change a byte that `loader.asm` holds. These bytes are between the free ranges of `loader.asm`, or below $0400.
32
+ - The start screen is of a captured stage, and the source has no `start_screen_stage.asm`. Or the file is of an earlier start screen, or of an earlier capture of the stage.
33
+ - The memory has no room for the show stage.
33
34
 
34
- In these cases, set the switch values in `config.asm`. Then run `generate --out <dir> --loader`.
35
+ Do the action that `loader.reason` gives. When it tells you to set the switch values in `config.asm`, the generation of the source with the loader removes `build/clean_loader.prg`. See "The loader program".
35
36
 
36
37
  ## The fields of the loader
37
38
 
@@ -74,6 +75,6 @@ The loader program then shows the start screen before it unpacks the program. Th
74
75
  - The loader program has only the bytes of the stage that the start screen uses. These are the screen, the bitmap and the charset. With a call, they are also each byte of the stage that is not free from the call, as in "The free ranges". A call thus needs the coverage of the stage.
75
76
  - With colours from the colour RAM of the capture, the loader program sets the colour RAM to these colours.
76
77
  - The patches of the stage are in at the defaults of their switches. Thus a patch under a switch can remove the text of a crack from the picture.
77
- - `start_screen_stage.asm` holds the show code and the bytes of the stage. Do not edit it. To make the loader program again, run `generate --out <dir> --loader`.
78
+ - `start_screen_stage.asm` holds the show code and the bytes of the stage. Do not edit it. To make the loader program again, run `generate --out <dir> --loader`. A build with other switch values also uses the bytes of the stage in this file.
78
79
  - The show stage needs Exomizer. The show code needs free memory in $0400-$9fff or $c000-$cfff. When the memory is too small, the reason gives the largest free ranges.
79
80
  - The check also compares the VIC-II, the screen bytes and the colour RAM with the start screen.
@@ -19,12 +19,12 @@ import { readFileSync } from "node:fs";
19
19
  import { fileURLToPath } from "node:url";
20
20
 
21
21
  import { COPIER_LINE } from "#src/build-name.ts";
22
- import { type AddressRange, formatC64Address, formatC64Byte, formatC64Range, hexWord, rangesOverlap, subtract, underRom } from "#src/c64.ts";
22
+ import { type AddressRange, formatC64Address, formatC64Byte, formatC64Range, rangesOverlap, subtract, underRom } from "#src/c64.ts";
23
23
  import { Refusal } from "#src/errors.ts";
24
24
  import { parseEntryState } from "#src/host-client/entry-state.ts";
25
- import { isRomFont, startScreenRanges, type StartScreenRow } from "#src/knowledge/read.ts";
26
- import { capturedColourRam, LOADER_JUMP } from "#src/knowledge/start-screen.ts";
27
- import { COPIER_SOURCE } from "#src/loader.ts";
25
+ import { startScreenRanges, type StartScreenRow } from "#src/knowledge/read.ts";
26
+ import { capturedColourRam, LOADER_JUMP, START_SCREEN_FILE, startScreenPlan, startScreenValues, type StartScreenPlan } from "#src/knowledge/start-screen.ts";
27
+ import { blockLabel, COPIER_SOURCE, largestFree, type Piece, placePieces, settingsLines } from "#src/loader.ts";
28
28
  import { romUseOfPort } from "#src/platform.ts";
29
29
  import { WireFailure } from "#src/host-client/tools.ts";
30
30
 
@@ -145,61 +145,11 @@ export function loaderLimits(plan: { cia?: CiaPlan; colourLimit?: string }): str
145
145
  return [...cia, ...(plan.colourLimit === undefined ? [] : [plan.colourLimit])];
146
146
  }
147
147
 
148
- /** The file of the start screen in the generated source. */
149
- export const START_SCREEN_FILE = "start_screen.asm";
150
148
  /** The code of the start screen: the same for each program. loader.asm sets its values. */
151
149
  export const START_SCREEN_CODE = readFileSync(fileURLToPath(new URL("./start-screen.asm", import.meta.url)), "utf8");
152
150
  /** The bytes of the start screen code for each colour mode: none, one colour, a copy of 1000 bytes. */
153
151
  export const START_SCREEN_SIZES = [197, 216, 226] as const;
154
152
 
155
- /** The VIC-II values of a start screen, as the start screen code sets them. */
156
- export interface StartScreenPlan {
157
- d011: number;
158
- d016: number;
159
- d018: number;
160
- /** The VIC bank: 0 for $0000-$3fff to 3 for $c000-$ffff. */
161
- bank: number;
162
- border: number;
163
- background: number;
164
- /** 0: no colour RAM (hires), 1: one colour, 2: a copy of 1000 bytes. */
165
- colourMode: 0 | 1 | 2;
166
- }
167
-
168
- /** The VIC-II values that show a start screen of the knowledge. */
169
- export function startScreenPlan(row: StartScreenRow): StartScreenPlan {
170
- const bank = row.screen >> 14;
171
- const base = (row.bitmap === undefined ? ((row.charset! & 0x3fff) >> 11) << 1 : ((row.bitmap & 0x3fff) >> 13) << 3) | 0x01;
172
- return {
173
- d011: row.mode === "text" ? 0x1b : 0x3b,
174
- d016: row.mode === "multicolour" ? 0x18 : 0x08,
175
- d018: (((row.screen & 0x3fff) >> 10) << 4) | base,
176
- bank,
177
- border: row.border,
178
- background: row.background,
179
- colourMode: row.colours !== undefined || row.colourRam === true ? 2 : row.colour !== undefined ? 1 : 0,
180
- };
181
- }
182
-
183
- /** The lines of loader.asm that give the start screen code its values. */
184
- export function startScreenValues(row: StartScreenRow, plan: StartScreenPlan): string[] {
185
- const value = (name: string, number: number, comment?: string) => `c64_source_ss_${name} = ${formatC64Byte(number)}${comment === undefined ? "" : ` ; ${comment}`}`;
186
- const font = row.charset !== undefined && isRomFont(row.charset) ? " (the character ROM)" : "";
187
- const what = [`screen ${formatC64Address(row.screen)}`, ...(row.bitmap === undefined ? [] : [`bitmap ${formatC64Address(row.bitmap)}`]), ...(row.charset === undefined ? [] : [`charset ${formatC64Address(row.charset)}${font}`])];
188
- return [
189
- `; The start screen: ${row.mode}, ${what.join(", ")}.${row.comment === undefined ? "" : ` ${row.comment.replace(/\s*\r?\n\s*/g, " ")}`}`,
190
- value("d011", plan.d011),
191
- value("d016", plan.d016),
192
- value("d018", plan.d018),
193
- value("bank", 3 - plan.bank, `VIC bank ${formatC64Address(plan.bank << 14)}-${formatC64Address((plan.bank << 14) + 0x3fff)}`),
194
- value("border", plan.border),
195
- value("background", plan.background),
196
- value("colour_mode", plan.colourMode),
197
- value("colour", row.colour ?? 0),
198
- // The colour RAM of a captured stage is in the show code: STAGE_COLOURS in stage.ts.
199
- `c64_source_ss_colours = ${row.colourRam === true ? "c64_source_stage_colours" : formatC64Address(row.colours ?? 0)}`,
200
- ];
201
- }
202
-
203
153
  /** The copier lines that include the start screen code, in a zone of its own. */
204
154
  const START_SCREEN_LINES = ["!zone c64_source_start_screen {", `!source "${START_SCREEN_FILE}"`, "}"];
205
155
 
@@ -475,14 +425,6 @@ function checkStartScreen(input: LoaderInput): StartScreenPlan | undefined {
475
425
  return startScreenPlan(row);
476
426
  }
477
427
 
478
- /** The settings that loader.asm and the show stage include: switches.asm, else config.asm for a start screen, else none. */
479
- export function settingsLines(input: Pick<LoaderInput, "switches" | "config">): string[] {
480
- if (input.switches === true) return ['!source "switches.asm"'];
481
- return input.config === true ? ['!source "config.asm"'] : [];
482
- }
483
-
484
- /** The label of a block: its first byte and the byte after it in the loader file, and its first address where it runs. */
485
- const blockLabel = (start: number, part: "from" | "end" | "to") => `loader_${part}_${hexWord(start)}`;
486
428
  const plus = (label: string, offset: number) => (offset === 0 ? label : `${label}+${offset}`);
487
429
 
488
430
  /** A block of the loader: a segment with its files, or bytes of pages 0-3. */
@@ -698,14 +640,6 @@ export function copierAreas(port: readonly [number, number]): AddressRange[] {
698
640
  if (rom.basic) return [{ start: 0x0400, end: 0x9fff }, { start: 0xc000, end: 0xcfff }];
699
641
  return [{ start: 0x0400, end: 0xcfff }, ...(rom.kernal ? [] : [{ start: 0xe000, end: BELOW_VECTORS }])];
700
642
  }
701
- /** The free ranges that a refusal names. */
702
- const SHOWN_FREE = 8;
703
-
704
- /** The largest free ranges with their sizes, for a refusal: at most SHOWN_FREE of them, or none. */
705
- export function largestFree(ranges: readonly AddressRange[]): string {
706
- const shown = [...ranges].sort((a, b) => b.end - b.start - (a.end - a.start)).slice(0, SHOWN_FREE);
707
- return shown.length === 0 ? "none" : shown.map((range) => `${formatC64Range(range)} (${range.end - range.start + 1} bytes)`).join(", ");
708
- }
709
643
 
710
644
  /**
711
645
  * The packed form of the loader, for an image too large for one loader file:
@@ -811,47 +745,6 @@ function planPacked(input: LoaderInput, blocks: Block[], cia: ReturnType<typeof
811
745
  };
812
746
  }
813
747
 
814
- /** A piece of pages 0-3 in the packed form: its target and where the copier reads it. */
815
- export interface Piece {
816
- what: string;
817
- target: AddressRange;
818
- at: number;
819
- bytes: (address: number) => number;
820
- }
821
-
822
- /**
823
- * Puts the parts of pages 0-3 into the free ranges, in their order: each time
824
- * into the nearest range that holds the rest, else into the largest range.
825
- * A part that does not fit in one range is split, and each piece is a copy.
826
- * Undefined when the ranges are too small.
827
- */
828
- export function placePieces(ranges: AddressRange[], low: ReadonlyArray<{ what: string; range: AddressRange; bytes: (address: number) => number }>, distance: (range: AddressRange) => number): Piece[] | undefined {
829
- const left = ranges.map((range) => ({ ...range }));
830
- const pieces: Piece[] = [];
831
- let rest = low.reduce((sum, block) => sum + block.range.end - block.range.start + 1, 0);
832
- const length = (range: AddressRange) => range.end - range.start + 1;
833
- let target: AddressRange | undefined;
834
- for (const block of low) {
835
- let next = block.range.start;
836
- while (next <= block.range.end) {
837
- if (target === undefined || target.start > target.end) {
838
- const open = left.filter((range) => range.start <= range.end);
839
- if (open.length === 0) return undefined;
840
- const holding = open.filter((range) => length(range) >= rest).sort((a, b) => distance(a) - distance(b));
841
- target = holding[0] ?? open.sort((a, b) => length(b) - length(a))[0]!;
842
- }
843
- const count = Math.min(block.range.end - next + 1, length(target));
844
- const part = { start: next, end: next + count - 1 };
845
- const cut = part.start !== block.range.start || part.end !== block.range.end;
846
- pieces.push({ what: cut ? `${block.what}, bytes ${formatC64Range(part)}` : block.what, target: part, at: target.start, bytes: block.bytes });
847
- target.start += count;
848
- next += count;
849
- rest -= count;
850
- }
851
- }
852
- return pieces;
853
- }
854
-
855
748
  /**
856
749
  * Where the packed form puts its copier and the pieces of pages 0-3: the copier
857
750
  * in one range of copierFree, a free page of the plain copier first, else the start of a
@@ -438,7 +438,9 @@ export function layout(input: SourceInput): Layout {
438
438
  const use = (address: number, kind: UseKind) => {
439
439
  if (inImage(address)) {
440
440
  const table = tableStart(address);
441
- wanted.add(table ?? address);
441
+ // A table that starts outside the image is a constant: a label there has no line.
442
+ if (table !== undefined && !inImage(table)) count(table, kind);
443
+ else wanted.add(table ?? address);
442
444
  if (table !== undefined) insideEntry.set(address, table);
443
445
  } else if (symbols.has(address) || platform.has(address)) count(address, kind);
444
446
  else {