@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.
- package/package.json +1 -1
- package/skills/c64-knowledge/SKILL.md +19 -4
- package/skills/c64-knowledge/scripts/knowledge.ts +10 -2
- package/skills/c64-reverse-engineering/SKILL.md +4 -1
- package/skills/c64-source/SKILL.md +51 -12
- package/skills/c64-source/scripts/lib/loader.ts +44 -16
- package/skills/c64-source/scripts/lib/source.ts +47 -14
- package/skills/c64-source/scripts/lib/stage.ts +268 -0
- package/skills/c64-source/scripts/lib/start-screen.asm +28 -2
- package/skills/c64-source/scripts/source.ts +351 -103
- package/skills/c64-unpacker/SKILL.md +1 -0
- package/src/knowledge/read.ts +8 -1
- package/src/knowledge/schema.ts +10 -2
- package/src/knowledge/text.ts +2 -0
- package/src/knowledge/write.ts +36 -6
package/package.json
CHANGED
|
@@ -53,6 +53,7 @@ The knowledge is text in `.c64-re-tools/knowledge/` of the project. Commit that
|
|
|
53
53
|
| Write the code of a switch for a range | `patch '$100e-$1010' PROTECTION --code-file no-check.asm` |
|
|
54
54
|
| List the releases, switches and patches | `switches` |
|
|
55
55
|
| Record the start screen of the program | `start-screen --mode multicolour --screen '$5c00' --bitmap '$6000' --colours '$8328' --border 0 --background 0` |
|
|
56
|
+
| Record the start screen of a captured stage, and its tune | `start-screen --mode multicolour --screen '$cc00' --bitmap '$e000' --colours '$8328' --border 3 --background 1 --stage bank1 --call '$9000'` |
|
|
56
57
|
| Remove the start screen | `unstart-screen` |
|
|
57
58
|
| Write a macro | `macro inc16 --params dst --body-file inc16.asm` |
|
|
58
59
|
| Write instructions as one use of a macro | `use '$1005-$100a' inc16 --args ptr` |
|
|
@@ -71,7 +72,7 @@ The knowledge is text in `.c64-re-tools/knowledge/` of the project. Commit that
|
|
|
71
72
|
- An analyzer name is a second name from DXA, Ghidra or an idiom search, for example `FUN_17e2`. Each analyzer keeps its own name at an address. Get them from `at`, `search` or `symbols --analyzer`.
|
|
72
73
|
- The generated source uses only the symbols. To use an analyzer name in the source, give the address that name with `rename`.
|
|
73
74
|
- A symbol name is a label for ACME: a letter or an underscore, then letters, digits or underscores. The knowledge refuses the name of a 6502 instruction, for example `lda`, in upper or lower case.
|
|
74
|
-
- A space is `c64` (the default), `drive8` for code in the 1541 drive, or `bank0` to `bank255` for cartridge banks. Drive code and
|
|
75
|
+
- A space is `c64` (the default), `drive8` for code in the 1541 drive, or `bank0` to `bank255` for cartridge banks and for a captured stage of the loading. Drive code, banks and stages use the same addresses as the C64. Thus each space has its own knowledge. A name is unique in one space.
|
|
75
76
|
- A revision id has the form `20261015T214501.123-k3f9qz`. It starts with the date and the time. Give the full id, or a start of the id that only one revision has. Get the ids from `revisions`, or from the `revision` field of a result. The revision `0` is the knowledge before the first revision.
|
|
76
77
|
- A file of a capture is the `image.json` that the capture of a packed program wrote. Keep the other files of the capture next to it.
|
|
77
78
|
|
|
@@ -149,6 +150,8 @@ Each flag applies only to the commands in the table. On another command, the scr
|
|
|
149
150
|
| `--colour <0-15>` | `start-screen` | One colour for all of the colour RAM, in place of `--colours`. |
|
|
150
151
|
| `--border <0-15>` | `start-screen` | Necessary. The border colour. |
|
|
151
152
|
| `--background <0-15>` | `start-screen` | Necessary. The background colour. |
|
|
153
|
+
| `--stage <space>` | `start-screen` | The space of a captured stage that holds the bytes of the screen, for example `bank1`. Without it, the bytes are in the program at the entry. |
|
|
154
|
+
| `--call <address>` | `start-screen` | A routine of the stage that the start screen calls before it waits, for example the start of the loading tune. It must return with RTS. It needs `--stage`. |
|
|
152
155
|
|
|
153
156
|
---
|
|
154
157
|
|
|
@@ -380,7 +383,9 @@ An operand expression tells the generated source how to write the operand of one
|
|
|
380
383
|
|
|
381
384
|
### `release <label>`
|
|
382
385
|
|
|
383
|
-
A rebuild keeps all of the program: unused bytes, dead code, old copies and leftovers of the development. It keeps a copy protection, a trainer and a cheat in the source. A switch puts each of them in or out of the build. A start screen of the program stays too: record it with `start-screen`. Only a cracktro and the other screens of a crack are not in the rebuild.
|
|
386
|
+
A rebuild keeps all of the program in the source: unused bytes, dead code, old copies and leftovers of the development. The default build and the loader do not have the leftovers. The build of a release has them. It keeps a copy protection, a trainer and a cheat in the source. A switch puts each of them in or out of the build. A start screen of the program stays too: record it with `start-screen`. Only a cracktro and the other screens of a crack are not in the rebuild.
|
|
387
|
+
|
|
388
|
+
Do not write a file of the rebuild by hand, for example a boot file, a packed picture or a script that changes bytes. Record each change in the knowledge, and generate the source.
|
|
384
389
|
|
|
385
390
|
- A release is a PRG or the `image.json` of a capture, in a folder that git keeps, for example `original/`. The script refuses a file in `.c64-re-tools/local/`.
|
|
386
391
|
- A label is upper case, for example `ORIGINAL`, `FIRESTAR` or `V2`.
|
|
@@ -398,7 +403,17 @@ A rebuild keeps all of the program: unused bytes, dead code, old copies and left
|
|
|
398
403
|
A start screen is a loading screen, a title screen or a presentation screen of the program itself. The loader of the generated source shows it before the program starts. Then the loader waits for a key, or for a fire button on joystick port 1 or 2.
|
|
399
404
|
|
|
400
405
|
- Record only a screen of the program itself. Do not record a screen of a crack, for example a picture, an intro or a credit screen of a crack group.
|
|
401
|
-
-
|
|
406
|
+
- If a release changed the screen of the program, stop and ask the user how to deal with it. An example is a picture with the text of a crack in it. Do not decide alone. Give the user these options:
|
|
407
|
+
1. Record patches under a switch that take out the change.
|
|
408
|
+
2. Use the screen as it is.
|
|
409
|
+
3. Do not record a start screen.
|
|
410
|
+
- The bytes of the screen must be in the program of the knowledge at the entry, or in a captured stage. Thus capture the program while the bytes of the screen are in memory.
|
|
411
|
+
- When the bytes are only in a stage of the loading, do these steps:
|
|
412
|
+
1. Capture the stage while it shows the screen. Do not exclude the memory of the screen.
|
|
413
|
+
2. Put the capture into the knowledge in a space of its own, for example `image stage/image.json --space bank1`.
|
|
414
|
+
3. If the stage plays a tune or does other work during the screen, find the routine that starts it. Run the stage with coverage while that routine runs. Put the map into the knowledge with `coverage <map> --space bank1`.
|
|
415
|
+
4. Record the start screen with `--stage bank1`, and with `--call` for the routine.
|
|
416
|
+
- With `--stage`, the addresses are in the program of that space. Each part and the call must be in it.
|
|
402
417
|
- Get the mode, the addresses and the colours from the VIC-II while the original program shows the screen.
|
|
403
418
|
- The colour RAM is not in the capture. Find the 1000 colour bytes in the program: compare them with the colour RAM at $d800 while the original shows the screen. Give their address with `--colours`. If the program does not have them, give one colour with `--colour`.
|
|
404
419
|
- `text` needs `--charset`. `hires` and `multicolour` need `--bitmap`. `text` and `multicolour` need `--colours` or `--colour`. `hires` uses neither.
|
|
@@ -406,7 +421,7 @@ A start screen is a loading screen, a title screen or a presentation screen of t
|
|
|
406
421
|
- In VIC bank 0 and 2, the VIC-II reads the character ROM at $1000-$1fff of the bank. A screen or a bitmap cannot be there. A charset at $1000 or $1800 of the bank is the character ROM.
|
|
407
422
|
- No part can be in $0000-$03ff. The colours cannot be in $d000-$dfff.
|
|
408
423
|
- A space has one start screen, and only the space `c64` has one. A write replaces the start screen of the knowledge.
|
|
409
|
-
- The generated source has the
|
|
424
|
+
- The generated source has the switches `START_SCREEN` and `LEFTOVERS`. Thus no symbol and no switch can have these names.
|
|
410
425
|
- `body.startScreen` gives the start screen after the write.
|
|
411
426
|
|
|
412
427
|
### `unstart-screen`
|
|
@@ -175,6 +175,8 @@ const startScreen = (row: StartScreenRow) => ({
|
|
|
175
175
|
...(row.colour === undefined ? {} : { colour: row.colour }),
|
|
176
176
|
border: row.border,
|
|
177
177
|
background: row.background,
|
|
178
|
+
...(row.stage === undefined ? {} : { stage: row.stage }),
|
|
179
|
+
...(row.call === undefined ? {} : { call: formatC64Address(row.call) }),
|
|
178
180
|
...(row.comment === undefined ? {} : { comment: row.comment }),
|
|
179
181
|
origin: row.origin,
|
|
180
182
|
revision: row.revision,
|
|
@@ -935,7 +937,7 @@ const WRITE_COMMANDS: CommandSpec[] = [
|
|
|
935
937
|
writeCommand(
|
|
936
938
|
{
|
|
937
939
|
name: "start-screen",
|
|
938
|
-
summary: "Records the start screen of the program: a loading, title or presentation screen of the program itself
|
|
940
|
+
summary: "Records the start screen of the program: a loading, title or presentation screen of the program itself, in memory at the entry or in a captured stage of the loading. The loader of the generated source shows it and waits for a key or a fire button before the entry.",
|
|
939
941
|
options: {
|
|
940
942
|
mode: { kind: "choice", value: "<mode>", choices: START_SCREEN_MODES, help: `one of ${START_SCREEN_MODES.join(", ")}`, required: true },
|
|
941
943
|
screen: { kind: "address", value: "<address>", help: "the screen: the 1000 characters of a text screen, or the 1000 colour bytes of a bitmap", required: true },
|
|
@@ -945,15 +947,19 @@ const WRITE_COMMANDS: CommandSpec[] = [
|
|
|
945
947
|
colour: { kind: "count", value: "<0-15>", min: 0, max: 15, help: "one colour for all of the colour RAM, in place of --colours" },
|
|
946
948
|
border: { kind: "count", value: "<0-15>", min: 0, max: 15, help: "the border colour", required: true },
|
|
947
949
|
background: { kind: "count", value: "<0-15>", min: 0, max: 15, help: "the background colour", required: true },
|
|
950
|
+
stage: { kind: "text", value: "<space>", help: "the space of a captured stage that holds the bytes of the screen, for example bank1. Without it, the bytes are in the program at the entry" },
|
|
951
|
+
call: { kind: "address", value: "<address>", help: "a routine of the stage that the start screen calls before it waits, for example the routine that starts the loading tune. It must return with RTS. Needs --stage" },
|
|
948
952
|
comment: { kind: "text", value: "<text>", help: "what the screen is" },
|
|
949
953
|
},
|
|
950
|
-
notes: "Record only a screen of the program itself, never a screen of a crack. text and multicolour need --colours or --colour; hires uses neither. A write replaces the start screen of the knowledge.",
|
|
954
|
+
notes: "Record only a screen of the program itself, never a screen of a crack. If a release changed the screen, for example with the text of a crack, stop and ask the user how to deal with it. text and multicolour need --colours or --colour; hires uses neither. With --stage, the addresses are in the program of that space. A write replaces the start screen of the knowledge.",
|
|
951
955
|
},
|
|
952
956
|
(call) => {
|
|
953
957
|
const context = contextOf(call);
|
|
954
958
|
const commentText = call.string("comment");
|
|
955
959
|
const optional = (key: "bitmap" | "charset" | "colours") => ((value) => (value === undefined ? {} : { [key]: value }))(call.address(key));
|
|
956
960
|
const colour = call.count("colour");
|
|
961
|
+
const stage = call.string("stage");
|
|
962
|
+
const routine = call.address("call");
|
|
957
963
|
const request = {
|
|
958
964
|
space: call.space,
|
|
959
965
|
mode: call.string("mode") as StartScreenMode,
|
|
@@ -964,6 +970,8 @@ const WRITE_COMMANDS: CommandSpec[] = [
|
|
|
964
970
|
...(colour === undefined ? {} : { colour }),
|
|
965
971
|
border: call.count("border")!,
|
|
966
972
|
background: call.count("background")!,
|
|
973
|
+
...(stage === undefined ? {} : { stage }),
|
|
974
|
+
...(routine === undefined ? {} : { call: routine }),
|
|
967
975
|
...(commentText === undefined ? {} : { comment: commentText }),
|
|
968
976
|
};
|
|
969
977
|
return (db) => shown(setStartScreen(db, context, request), "startScreen", startScreen);
|
|
@@ -37,7 +37,10 @@ These rules apply to each rebuild:
|
|
|
37
37
|
|
|
38
38
|
- Do not keep a cracktro. A capture of a packed program starts at the last stage, so the code of a cracktro is not in the program. When the capture has data of a cracktro that the program does not read, capture again and exclude that range.
|
|
39
39
|
- Keep the start screen of the program, for example the loading picture of the publisher. Keep its bytes in the capture, and record the start screen in the project knowledge. The loader of the rebuild then shows it and waits for a key or a fire button. Do not keep a screen of a crack, for example a picture, an intro or a credit screen of a crack group.
|
|
40
|
-
-
|
|
40
|
+
- When the start screen is only in a stage of the loading, capture that stage while it shows the screen. Put the capture into the project knowledge in a space of its own. Then record the start screen of that stage. Keep its tune: record the routine that starts the tune as the call of the start screen.
|
|
41
|
+
- A release can change the start screen, for example with the text of a crack. Then stop and ask the user how to deal with it. Do not decide alone.
|
|
42
|
+
- Keep all other bytes: unused bytes, dead code, old copies, texts and leftovers of the development. Record each one as a region with a comment. Do not remove them. The source keeps them, and the build of each release has them. The default build and the loader do not have them.
|
|
43
|
+
- Do not write a file of the rebuild by hand, for example a boot file, a packed picture or a script that changes bytes. Record each change in the project knowledge, and generate the source.
|
|
41
44
|
- Keep a copy protection in the source, off by default. Record a `protection` switch and its patches.
|
|
42
45
|
- Keep a trainer and a cheat in the source, off by default. Record a `trainer` or `cheat` switch and its patches.
|
|
43
46
|
- When two or more releases have different corrections, record each release and one `fix` switch for each correction. The switch names the releases that have the correction. The default build has all corrections.
|
|
@@ -72,10 +72,10 @@ Each flag applies only to the commands in the table. On another command, the scr
|
|
|
72
72
|
|
|
73
73
|
| Flag | Command | Effect |
|
|
74
74
|
|---|---|---|
|
|
75
|
-
| `--out <dir>` | `generate` | The folder for the source files. It is necessary. The script writes over the files of an earlier run. It removes a generated `.asm` file that this run does not write. A run without `--loader` keeps `loader.asm`, `loader.prg` and `
|
|
75
|
+
| `--out <dir>` | `generate` | The folder for the source files. It is necessary. The script writes over the files of an earlier run. It removes a generated `.asm` file that this run does not write. A run without `--loader` keeps `loader.asm`, `loader.prg`, `start_screen.asm` and `start_screen_stage.asm` of an earlier run. `staleLoader` tells when they are of another knowledge revision. |
|
|
76
76
|
| `--entry <address>` | `generate` | An address where code starts. Give the flag again for more addresses. There is no default. |
|
|
77
|
-
| `--space <space>` | `generate` | The knowledge space: `c64`, `drive8`, or `bank0` to `bank255`. The default is `c64`.
|
|
78
|
-
| `--loader` | `generate` | Also write `loader.asm` and `loader.prg`, and `start_screen.asm` when the knowledge has a start screen. The script writes `loader.prg` only after the check of the new loader ran. Else the `loader.prg` of an earlier run stays. It needs a capture in the knowledge. The default is no loader. |
|
|
77
|
+
| `--space <space>` | `generate` | The knowledge space: `c64`, `drive8`, or `bank0` to `bank255`. The default is `c64`. `--loader` and the platform names apply only to `c64`. The coverage is the coverage of the space. |
|
|
78
|
+
| `--loader` | `generate` | Also write `loader.asm` and `loader.prg`, and `start_screen.asm` when the knowledge has a start screen. A start screen of a captured stage also gives `start_screen_stage.asm`. The script writes `loader.prg` only after the check of the new loader ran. Else the `loader.prg` of an earlier run stays. It needs a capture in the knowledge. The default is no loader. |
|
|
79
79
|
| `--analyzer-names` | `generate` | Name an address without a name of the user or the LLM, and without a platform name, with its analyzer name, for example `FUN_17e2`. Without the flag, the source makes its own name, for example `l_17e2`. |
|
|
80
80
|
| `--to <folder>` | `export` | The folder of the new project. It is necessary. The script also writes `<folder>.zip` beside the folder. |
|
|
81
81
|
| `--name <name>` | `export` | The name of `baseline/<name>.prg` and `build/<name>.prg`. The default is the name of the folder. |
|
|
@@ -110,6 +110,7 @@ To make the source better, change the project knowledge. Then generate again.
|
|
|
110
110
|
- `asBytes`: the instructions that the source has as bytes.
|
|
111
111
|
- `notExecuted`: with coverage, the count of instructions that no run executed, and their ranges. `rest` names `not_executed.json`, which has all the ranges. Each range there has its source `file` and the `routine` before it.
|
|
112
112
|
- `unusedModules`: modules whose start is in no segment of the program. Often the program does not hold the code that the knowledge describes.
|
|
113
|
+
- `leftovers`: the bytes that nothing uses, with `bytes` and each range with its `file`. See "The leftovers". `noCoverage` tells that the knowledge has no coverage, so no range is a leftover.
|
|
113
114
|
- `operandExpressions`: the count of instructions with an operand expression.
|
|
114
115
|
- `operandsAsNumbers`: each operand expression that the source has as a number, with its `address`, `expression` and `reason`.
|
|
115
116
|
- `macroUses`: the count of macro uses in the source.
|
|
@@ -158,7 +159,8 @@ Do these actions for the findings:
|
|
|
158
159
|
- You can change a value in `config.asm` to 0 or 1. The next run keeps the values of the file. It adds a new switch with its default. It removes a switch that the knowledge does not have.
|
|
159
160
|
- When the knowledge has a start screen, `config.asm` also has `START_SCREEN = 1`. Set it to 0 to start the program at once. Then assemble `loader.asm` again.
|
|
160
161
|
- `start_screen.asm` is the code that shows the start screen and waits. It is the same for each program. `loader.asm` gives it the values of the start screen.
|
|
161
|
-
- `switches.asm` holds each release of the knowledge. `main.asm` and `loader.asm` include it first. The script writes it only when the knowledge has switches or releases. For each release, a block `!ifdef RELEASE_<label>` gives each switch the value of that release.
|
|
162
|
+
- `switches.asm` holds each release of the knowledge. `main.asm` and `loader.asm` include it first. The script writes it only when the knowledge has switches or releases, or when the source has leftovers. For each release, a block `!ifdef RELEASE_<label>` gives each switch the value of that release.
|
|
163
|
+
- `leftover_xxxx.asm` holds a range of leftovers. See "The leftovers".
|
|
162
164
|
- `macros.asm` holds the macros of the knowledge. `main.asm` and `loader.asm` include it.
|
|
163
165
|
|
|
164
166
|
#### Names in the source
|
|
@@ -190,15 +192,27 @@ Do these actions for the findings:
|
|
|
190
192
|
- A patch block has the code of the patch and NOP to the end of the range. `else` has the original code. The labels inside the range come before the zone as `name = * + n`.
|
|
191
193
|
- Alternatives are patches of two or more switches at one range. They follow as `} else { !if FIX_B {`. A line before them stops the build with `!error` when two of them are in.
|
|
192
194
|
|
|
195
|
+
#### The leftovers
|
|
196
|
+
|
|
197
|
+
A leftover is a range of the program that nothing uses: unused code, old copies and old data. The script finds the leftovers from the coverage, as the export finds its free ranges (see "The free ranges"). The start screen, the patches, the macro uses and the addresses that the code of a patch names are never leftovers.
|
|
198
|
+
|
|
199
|
+
- Each range of leftovers is a file `leftover_xxxx.asm`. When a range cuts a module, the module continues after the range in `<module>_xxxx.asm`. The module in the knowledge does not change.
|
|
200
|
+
- `main.asm` builds a leftover file only when `LEFTOVERS` is 1. When it is 0, the build skips the bytes. Thus the addresses after the range do not change.
|
|
201
|
+
- `config.asm` has `LEFTOVERS = 0`. Thus the default build is the clean program, without the leftovers.
|
|
202
|
+
- Each release has `LEFTOVERS = 1`. Thus the build of a release gives the bytes of its file.
|
|
203
|
+
- `loader.prg` never has the leftovers. The loader does not copy them, and the memory there keeps the bytes that were in it before.
|
|
204
|
+
- Without coverage, no range is a leftover. Put the coverage of the runs into the project knowledge. Then generate again.
|
|
205
|
+
- A leftover can be wrong when the coverage does not show a use. For example, the VIC-II reads a range, or the program reads a range before it writes it. Then a test of `loader.prg` fails. Record the region in the project knowledge, or put the coverage of more runs into it. Do not change the source.
|
|
206
|
+
|
|
193
207
|
#### Switches and releases
|
|
194
208
|
|
|
195
209
|
The program of the knowledge is the base release. The default build is the clean program: no copy protection, no trainer, no cheat, no crack change, and all corrections. Thus the default build can be different from the program.
|
|
196
210
|
|
|
197
211
|
The command does these checks. They use the defaults and the values of the knowledge, not the values in `config.asm`.
|
|
198
212
|
|
|
199
|
-
1. `check`: the build of the base release must give the program byte by byte. In this build, each switch has its base value.
|
|
213
|
+
1. `check`: the build of the base release must give the program byte by byte. In this build, each switch has its base value, and `LEFTOVERS` is 1.
|
|
200
214
|
2. `switches`: each switch alone at its other value. The build must have the same length as the base release. It can change bytes only in the patches of the switch.
|
|
201
|
-
3. `default`: the default build. It must have the same length. It can change bytes only in the patches of the switches in `default.in
|
|
215
|
+
3. `default`: the default build. It must have the same length. It can change bytes only in the patches of the switches in `default.in` and in the leftovers.
|
|
202
216
|
4. `releases`: each release, built with `RELEASE_<label>=1`, against the file of the release.
|
|
203
217
|
|
|
204
218
|
Each item of `switches` has `name`, `kind`, `default`, `base`, `define` and the patches.
|
|
@@ -224,7 +238,7 @@ The script writes a macro use only when the use gives the bytes of the program.
|
|
|
224
238
|
|
|
225
239
|
Build the program from the source in these ways:
|
|
226
240
|
|
|
227
|
-
- The build of `config.asm`: no option. With the defaults, this is the default build.
|
|
241
|
+
- The build of `config.asm`: no option. With the defaults, this is the default build, without the leftovers.
|
|
228
242
|
- A release: the build option `--release ORIGINAL`.
|
|
229
243
|
- One switch: set its value in `config.asm`, for example `TRAINER = 1`. Do not define a switch on the command line of the build. ACME stops with `Symbol already defined`, because `config.asm` also sets the switch.
|
|
230
244
|
|
|
@@ -234,7 +248,7 @@ Build the program from the source in these ways:
|
|
|
234
248
|
|
|
235
249
|
1. `10 SYS 2061` starts it.
|
|
236
250
|
2. It stops the interrupts and makes all RAM visible. It moves its copier to two free pages.
|
|
237
|
-
3. It copies each segment and the pages $0002-$03ff of the entry state into place. No copy writes over the packed bytes of a copy that has not run yet.
|
|
251
|
+
3. It copies each segment without the leftovers, and the pages $0002-$03ff of the entry state, into place. No copy writes over the packed bytes of a copy that has not run yet.
|
|
238
252
|
4. When the knowledge has a start screen and `START_SCREEN` is 1, it shows the start screen. It waits until the user releases all keys and fire buttons. Then it waits for a key, or for a fire button on joystick port 1 or 2, and for its release. A move of a joystick is not a key.
|
|
239
253
|
5. It sets the screen registers of the VIC-II and the VIC bank from the entry state. Then it waits for the raster line of the entry.
|
|
240
254
|
6. It sets the CIA timers, their latches and the interrupt masks from the entry state. A running timer gets the value that counts down to the timer value at the entry.
|
|
@@ -244,11 +258,11 @@ The copy table, the copier and the jump use labels, not numbers. `loader_from_xx
|
|
|
244
258
|
|
|
245
259
|
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. A start screen writes the colour RAM, and the loader does not set it again. When you compare the original and the rebuild, observe after the program sets its own random numbers.
|
|
246
260
|
|
|
247
|
-
The
|
|
261
|
+
The script builds and checks `loader.prg` with the values of `config.asm`. With the defaults, `loader.prg` is the clean program: no copy protection, all corrections and no leftovers. The entry state is the state of the base release. For a loader of a release, build `loader.asm` with the build option `--release <label>`. That loader also does not have the leftovers.
|
|
248
262
|
|
|
249
263
|
The start screen:
|
|
250
264
|
|
|
251
|
-
- The start screen needs the VIC-II values of the entry state. Its screen, bitmap, charset and colours must be in a segment of the program.
|
|
265
|
+
- The start screen needs the VIC-II values of the entry state. Its screen, bitmap, charset and colours must be in a segment of the program, or in a captured stage. See "The start screen of a captured stage".
|
|
252
266
|
- With a start screen, the copier uses three pages, not two.
|
|
253
267
|
- `loader.prg` always shows the start screen. To start the program at once, set `START_SCREEN = 0` in `config.asm`, and assemble `loader.asm`. Do not define `START_SCREEN` on the command line of the build: `config.asm` sets it. The build of a release does not read `config.asm`, so it shows the start screen.
|
|
254
268
|
- A test that runs `loader.prg` stops at the start screen, because the emulator cannot press a key of the keyboard matrix. Run until `loader_start_screen_wait`. Then set the PC to `loader_start_screen_continue`.
|
|
@@ -259,7 +273,8 @@ The start screen:
|
|
|
259
273
|
- `verified: true`: the check ran `loader.prg` to its jump to the entry. There, the memory, $00/$01, the registers and the VIC-II are equal to the entry state. The raster line and the CIA values are also equal. An interrupt can run right at the entry, as in the original program.
|
|
260
274
|
- `limits`: each CIA value that the loader cannot set as it was at the entry. An old capture has no CIA values. Then capture the program again at its entry point.
|
|
261
275
|
- `startScreen`: with a start screen, `shown: true` when the VIC-II had the values of the start screen, and `waits: true` when the start screen waited without a key. `wait` and `continue` are the addresses of the wait and of the code after it. The check sets the PC to `continue`, and then runs the loader to its jump.
|
|
262
|
-
- `packed`: `packedBytes` and `unpackedBytes` of the packed form.
|
|
276
|
+
- `packed`: `packedBytes` and `unpackedBytes` of the packed form. With a start screen of a captured stage, this is the packed program in `loader.prg`.
|
|
277
|
+
- `stage`: the show stage of a start screen of a captured stage. It has the `space` of the stage and the address of the show `code`. It also has the `bytes` of the stage in `loader.prg`, the `stash` copies, `packedBytes` and `unpackedBytes`. `startScreen.stage` names the space.
|
|
263
278
|
- When the check of `loader.prg` cannot run, the script does not write `loader.prg`. The `loader.prg` of an earlier run stays as it was. `loader.asm` is new. `reason` tells why the check did not run.
|
|
264
279
|
- `kept`: when the result has no new `loader.prg`, the path of the `loader.prg` of an earlier run. That file is not of this source.
|
|
265
280
|
|
|
@@ -271,10 +286,33 @@ Do these actions when `verified` is `false`:
|
|
|
271
286
|
| `reason` about the packed form | If a tool is missing, stop the task and give the message to the user. If the free ranges are too small, write the loader by hand. If the check of the unpack fails, give the reason to the user. |
|
|
272
287
|
| `reason` that the source does not assemble | Correct the source first. The loader includes the source. |
|
|
273
288
|
| `reason` about the start screen | Correct the start screen in the project knowledge, or capture the program again with the start screen in memory. If the start screen is not necessary, remove it from the project knowledge. |
|
|
289
|
+
| `reason` about the show stage | If the reason names the coverage of the stage, run the stage with coverage. Put the map into the project knowledge in the space of the stage. If the memory is too small, capture the stage again with less memory. If a tool is missing, stop the task and give the message to the user. |
|
|
274
290
|
| `reason` that the check of `loader.prg` did not run | Stop the task and give the reason to the user. When the user tells you that the emulator is available, run the same command again. |
|
|
275
291
|
| `differences` | The loader ran. The list gives the bytes and the registers that are not equal. |
|
|
276
292
|
| `stop` | The loader did not get to its copier or its jump. `stop` gives the address and `port`, the value of $01. `history` gives the last 16 instructions. `cause` gives the first byte of the copier or of a copy that is not correct. |
|
|
277
293
|
|
|
294
|
+
#### The start screen of a captured stage
|
|
295
|
+
|
|
296
|
+
Often the loading stage of a program shows a picture that is not in memory at the entry. Then the knowledge has the picture in a captured stage: a space such as `bank1`. `--stage` of the start screen names that space. `--call` names a routine of the stage, for example the routine that starts the loading tune.
|
|
297
|
+
|
|
298
|
+
The loader then always has the packed form, and the start screen is a program of its own in front of it:
|
|
299
|
+
|
|
300
|
+
1. Exomizer packs the program with the copier. This is the packed loader.
|
|
301
|
+
2. Exomizer packs the packed loader, the bytes of the stage and the show code into `loader.prg`.
|
|
302
|
+
3. When `loader.prg` starts, Exomizer unpacks these parts. The program stays packed. The bytes of the packed loader under the stage go to a stash in free memory.
|
|
303
|
+
4. The show code shows the start screen with `start_screen.asm`. With a call, it sets $01 to $37, starts the interrupts and calls the routine.
|
|
304
|
+
5. It waits for a key or a fire button, as the start screen in the program does.
|
|
305
|
+
6. It stops the interrupts and the sound, and it stops the screen. It sets the CIA timers as RUN leaves them.
|
|
306
|
+
7. It copies the stash back to the packed loader. Then it starts the packed loader, which unpacks the program over the start screen.
|
|
307
|
+
|
|
308
|
+
The program is packed while the screen shows. Thus the picture and the tune have more free memory.
|
|
309
|
+
|
|
310
|
+
- `loader.prg` has only the bytes of the stage that the start screen uses. These are the screen, the bitmap, the charset and the colours. 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.
|
|
311
|
+
- 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.
|
|
312
|
+
- `start_screen_stage.asm` holds the show code and the bytes of the stage. Do not edit it. To make `loader.prg` again, run `generate --loader`.
|
|
313
|
+
- 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.
|
|
314
|
+
- The check runs `loader.prg` to `loader_start_screen_wait` of the show stage. There, it compares the VIC-II with the start screen, and the screen bytes in memory with the stage. Then it sets the PC to `loader_start_screen_continue`, and it checks the loader as usual.
|
|
315
|
+
|
|
278
316
|
#### The packed loader
|
|
279
317
|
|
|
280
318
|
A loader file that loads into $d000 or higher writes the I/O area. When the program is too large for one loader file, `loader.asm` has the packed form:
|
|
@@ -285,7 +323,7 @@ A loader file that loads into $d000 or higher writes the I/O area. When the prog
|
|
|
285
323
|
|
|
286
324
|
To make the packed loader for other switch values, do these steps:
|
|
287
325
|
|
|
288
|
-
1. Assemble `main.asm` to `build/main.prg`.
|
|
326
|
+
1. Assemble `main.asm` to `build/main.prg`. The leftovers are $00 in this build.
|
|
289
327
|
2. Assemble `loader.asm` to `build/copier.prg`.
|
|
290
328
|
3. Pack the two files into `build/loader.prg`. The start address is the copier address in the comment at the start of `loader.asm`. The memory configuration at the exit is `$34`.
|
|
291
329
|
|
|
@@ -321,6 +359,7 @@ The build with these values can change bytes only in the patches. Then the comma
|
|
|
321
359
|
- `loader`: the copies, the copier, `stackByte` and `simulated: true`. The export does not run the loader.
|
|
322
360
|
- `noCoverage` or `unrooted`: why nothing became free without `--drop`.
|
|
323
361
|
- `spacesLeftOut`: the spaces that the export does not have. The export has only the space `c64`.
|
|
362
|
+
- `startScreenLeftOut`: the start screen is of a captured stage. The export does not have it, because the export has only the space `c64`.
|
|
324
363
|
|
|
325
364
|
On `PASS`, `hint` tells you to build and test the new project as its `README.md` tells. When the knowledge has no coverage, or `unreachableExecuted` has items, `hint` also tells how to make more ranges free.
|
|
326
365
|
On `FAIL`, `hint` gives the action for the stage:
|
|
@@ -10,6 +10,8 @@
|
|
|
10
10
|
// copier, and the copier writes only pages $0002-$03ff. With a start
|
|
11
11
|
// screen in the knowledge, the copier shows it after the copies and waits for
|
|
12
12
|
// a key or a fire button: the code is start-screen.asm, the same for each program.
|
|
13
|
+
// The loader loads the segments without the leftovers: the ranges of the
|
|
14
|
+
// source that nothing uses are not in loader.prg.
|
|
13
15
|
|
|
14
16
|
import { readFileSync } from "node:fs";
|
|
15
17
|
import { fileURLToPath } from "node:url";
|
|
@@ -105,6 +107,8 @@ export interface PackedBlock {
|
|
|
105
107
|
}
|
|
106
108
|
|
|
107
109
|
export interface LoaderPlan {
|
|
110
|
+
/** The ranges that the loader puts in place: the segments without the leftovers. */
|
|
111
|
+
loaded: AddressRange[];
|
|
108
112
|
blocks: PackedBlock[];
|
|
109
113
|
copies: Copy[];
|
|
110
114
|
/** The first of the two free pages where the copier runs. */
|
|
@@ -173,7 +177,7 @@ export function startScreenPlan(row: StartScreenRow): StartScreenPlan {
|
|
|
173
177
|
}
|
|
174
178
|
|
|
175
179
|
/** The lines of loader.asm that give the start screen code its values. */
|
|
176
|
-
function startScreenValues(row: StartScreenRow, plan: StartScreenPlan): string[] {
|
|
180
|
+
export function startScreenValues(row: StartScreenRow, plan: StartScreenPlan): string[] {
|
|
177
181
|
const value = (name: string, number: number, comment?: string) => `c64_source_ss_${name} = ${formatC64Byte(number)}${comment === undefined ? "" : ` ; ${comment}`}`;
|
|
178
182
|
const font = row.charset !== undefined && isRomFont(row.charset) ? " (the character ROM)" : "";
|
|
179
183
|
const what = [`screen ${formatC64Address(row.screen)}`, ...(row.bitmap === undefined ? [] : [`bitmap ${formatC64Address(row.bitmap)}`]), ...(row.charset === undefined ? [] : [`charset ${formatC64Address(row.charset)}${font}`])];
|
|
@@ -223,7 +227,7 @@ const overlaps = (a: AddressRange, b: AddressRange) => a.start <= b.end && b.sta
|
|
|
223
227
|
const show = (range: AddressRange) => `${formatC64Address(range.start)}-${formatC64Address(range.end)}`;
|
|
224
228
|
|
|
225
229
|
/** Parts of `range` that no range in `holes` covers. */
|
|
226
|
-
function subtract(range: AddressRange, holes: AddressRange[]): AddressRange[] {
|
|
230
|
+
export function subtract(range: AddressRange, holes: AddressRange[]): AddressRange[] {
|
|
227
231
|
let pieces = [range];
|
|
228
232
|
for (const hole of holes) {
|
|
229
233
|
pieces = pieces.flatMap((piece) => {
|
|
@@ -445,7 +449,7 @@ export function ciaSection(state: EntryState): { ops: Op[]; plan: CiaPlan } | un
|
|
|
445
449
|
}
|
|
446
450
|
|
|
447
451
|
export interface LoaderInput {
|
|
448
|
-
/** The
|
|
452
|
+
/** The ranges to load, each with the files that hold it, in address order: the segments without the leftovers. */
|
|
449
453
|
segments: Array<AddressRange & { files: string[] }>;
|
|
450
454
|
byteAt: (address: number) => number;
|
|
451
455
|
state: EntryState;
|
|
@@ -461,6 +465,8 @@ export interface LoaderInput {
|
|
|
461
465
|
config?: boolean;
|
|
462
466
|
/** The start screen of the knowledge: the copier shows it after the copies. */
|
|
463
467
|
startScreen?: StartScreenRow;
|
|
468
|
+
/** Always the packed form: the show stage of a start screen from a captured stage holds the packed program while it shows the screen. */
|
|
469
|
+
packed?: boolean;
|
|
464
470
|
}
|
|
465
471
|
|
|
466
472
|
/** The checks of a start screen against the program and the entry state. Refuses by name. */
|
|
@@ -545,11 +551,12 @@ export function planLoader(input: LoaderInput): LoaderPlan {
|
|
|
545
551
|
const codeSize = COPIER_CODE_SIZE + (state.vic === undefined ? 0 : VIC_SIZE) + ciaSize + (screen === undefined ? 0 : START_SCREEN_SIZES[screen.colourMode]);
|
|
546
552
|
const copierImage = at;
|
|
547
553
|
const end = copierImage + codeSize + COPY_ENTRY * plain.length + 2 - 1;
|
|
554
|
+
if (input.packed === true) return planPacked(input, blocks, cia, codeSize, screen, "The start screen of a captured stage shows while the program is still packed.");
|
|
548
555
|
// The end can be past $ffff, which is no address.
|
|
549
556
|
if (end > LOAD_LIMIT) return planPacked(input, blocks, cia, codeSize, screen, `The plain loader would end ${end > 0xffff ? "past $ffff" : `at ${formatC64Address(end)}`}, after ${formatC64Address(LOAD_LIMIT)}: loading it would write the I/O area.`);
|
|
550
557
|
// The CIA part and the start screen take room from the copy table in the copier's pages.
|
|
551
558
|
const maxCopies = Math.max(0, Math.min(TABLE_COPIES, Math.floor((copierBytes - codeSize - 2) / COPY_ENTRY)));
|
|
552
|
-
if (plain.length > maxCopies)
|
|
559
|
+
if (plain.length > maxCopies) return planPacked(input, blocks, cia, codeSize, screen, `The plain loader needs ${plain.length} copies, and its copier can do at most ${maxCopies}.`);
|
|
553
560
|
|
|
554
561
|
const order = copyOrder(plain);
|
|
555
562
|
if (order === undefined) {
|
|
@@ -621,6 +628,7 @@ export function planLoader(input: LoaderInput): LoaderPlan {
|
|
|
621
628
|
lines.push(" !word 0", "}", "}");
|
|
622
629
|
|
|
623
630
|
return {
|
|
631
|
+
loaded: input.segments.map(({ start, end }) => ({ start, end })),
|
|
624
632
|
blocks: blocks.map((block, index) => ({ what: block.what, range: block.range, packedAt: packedAt[index]!, bytes: block.bytes })),
|
|
625
633
|
copies,
|
|
626
634
|
copier,
|
|
@@ -719,6 +727,7 @@ function planPacked(input: LoaderInput, blocks: Block[], cia: ReturnType<typeof
|
|
|
719
727
|
parts.sort((a, b) => a.range.start - b.range.start);
|
|
720
728
|
for (const part of parts) lines.push("", ...part.text);
|
|
721
729
|
return {
|
|
730
|
+
loaded: input.segments.map(({ start, end }) => ({ start, end })),
|
|
722
731
|
blocks: pieces.map((piece) => ({ what: piece.what, range: piece.target, packedAt: piece.at, bytes: piece.bytes })),
|
|
723
732
|
copies,
|
|
724
733
|
copier,
|
|
@@ -735,7 +744,7 @@ function planPacked(input: LoaderInput, blocks: Block[], cia: ReturnType<typeof
|
|
|
735
744
|
const MAX_PACKED_COPIES = Math.floor((256 - 2) / COPY_ENTRY);
|
|
736
745
|
|
|
737
746
|
/** A piece of pages 0-3 in the packed form: its target and where the copier reads it. */
|
|
738
|
-
interface Piece {
|
|
747
|
+
export interface Piece {
|
|
739
748
|
what: string;
|
|
740
749
|
target: AddressRange;
|
|
741
750
|
at: number;
|
|
@@ -748,7 +757,7 @@ interface Piece {
|
|
|
748
757
|
* A part that does not fit in one range is split, and each piece is a copy.
|
|
749
758
|
* Undefined when the ranges are too small.
|
|
750
759
|
*/
|
|
751
|
-
function placePieces(ranges: AddressRange[], low: ReadonlyArray<{ what: string; range: AddressRange; bytes: (address: number) => number }>, distance: (range: AddressRange) => number): Piece[] | undefined {
|
|
760
|
+
export function placePieces(ranges: AddressRange[], low: ReadonlyArray<{ what: string; range: AddressRange; bytes: (address: number) => number }>, distance: (range: AddressRange) => number): Piece[] | undefined {
|
|
752
761
|
const left = ranges.map((range) => ({ ...range }));
|
|
753
762
|
const pieces: Piece[] = [];
|
|
754
763
|
let rest = low.reduce((sum, block) => sum + block.range.end - block.range.start + 1, 0);
|
|
@@ -815,24 +824,43 @@ export interface PackedLoader {
|
|
|
815
824
|
}
|
|
816
825
|
|
|
817
826
|
/**
|
|
818
|
-
* Packs the build of main.asm from $0400 and the build of
|
|
819
|
-
* copier wins where they overlap) with the entry at the copier
|
|
820
|
-
* and checks the unpack in the emulator of Exomizer: every byte
|
|
821
|
-
* builds, and the jump to the copier. Refuses by name when it does
|
|
827
|
+
* Packs the loaded ranges of the build of main.asm from $0400 and the build of
|
|
828
|
+
* loader.asm (the copier wins where they overlap) with the entry at the copier
|
|
829
|
+
* and $01 = $34, and checks the unpack in the emulator of Exomizer: every byte
|
|
830
|
+
* of the two builds, and the jump to the copier. Refuses by name when it does
|
|
831
|
+
* not hold.
|
|
822
832
|
*/
|
|
823
833
|
export async function packLoader(main: Uint8Array, copier: Uint8Array, plan: LoaderPlan, context: ToolContext): Promise<PackedLoader> {
|
|
824
|
-
|
|
834
|
+
return packParts(packedParts(main, copier, plan), plan.copier, 0x34, "main.asm and loader.asm", "the copier", context);
|
|
835
|
+
}
|
|
836
|
+
|
|
837
|
+
/** What Exomizer packs in the packed form: the loaded ranges of the build of main.asm from $0400, and the build of loader.asm. */
|
|
838
|
+
function packedParts(main: Uint8Array, copier: Uint8Array, plan: Pick<LoaderPlan, "loaded">): Array<{ start: number; body: Uint8Array }> {
|
|
839
|
+
// The unpacker uses pages 0-3; the copier writes them. The leftovers between the loaded ranges are not packed.
|
|
825
840
|
const program = prgRange(main);
|
|
826
|
-
const
|
|
827
|
-
|
|
841
|
+
const ranges = plan.loaded.flatMap((range) => {
|
|
842
|
+
const start = Math.max(range.start, 0x0400, program.start);
|
|
843
|
+
const end = Math.min(range.end, program.start + program.body.length - 1);
|
|
844
|
+
return start <= end ? [{ start, body: program.body.subarray(start - program.start, end - program.start + 1) }] : [];
|
|
845
|
+
});
|
|
846
|
+
return [...ranges, prgRange(copier)];
|
|
847
|
+
}
|
|
848
|
+
|
|
849
|
+
/**
|
|
850
|
+
* Packs parts (a later one wins where they overlap) into one program that
|
|
851
|
+
* unpacks itself, starts at `entry` with $01 = `exitMemory`, and checks the
|
|
852
|
+
* unpack in the emulator of Exomizer: every byte of the parts, and the jump
|
|
853
|
+
* to the entry. Refuses by name when it does not hold.
|
|
854
|
+
*/
|
|
855
|
+
export async function packParts(parts: ReadonlyArray<{ start: number; body: Uint8Array }>, entry: number, exitMemory: number, what: string, entryName: string, context: ToolContext): Promise<PackedLoader> {
|
|
828
856
|
const prg = (part: { start: number; body: Uint8Array }) => Buffer.concat([Buffer.from([part.start & 0xff, part.start >> 8]), part.body]);
|
|
829
|
-
const packed = await pack({ programs: parts.map(prg), entry
|
|
830
|
-
if (packed.entry !==
|
|
857
|
+
const packed = await pack({ programs: parts.map(prg), entry, exitMemory }, context);
|
|
858
|
+
if (packed.entry !== entry) throw new WireFailure("operation-failed", `The unpack in the emulator of Exomizer jumps to ${formatC64Address(packed.entry)}, not to ${entryName} at ${formatC64Address(entry)}.`);
|
|
831
859
|
const reference = mergedPrograms(parts);
|
|
832
860
|
const wrong = compareBuild(packed.unpacked, reference).segments.find((segment) => segment.result !== "identical");
|
|
833
861
|
if (wrong !== undefined) {
|
|
834
862
|
const first = wrong.firstDifference === undefined ? "" : `: the first is ${wrong.firstDifference.address}, ${wrong.firstDifference.built} and not ${wrong.firstDifference.reference}`;
|
|
835
|
-
throw new WireFailure("operation-failed", `The unpack in the emulator of Exomizer does not give ${wrong.start}-${wrong.end} of
|
|
863
|
+
throw new WireFailure("operation-failed", `The unpack in the emulator of Exomizer does not give ${wrong.start}-${wrong.end} of ${what} (${wrong.result}${first}).`);
|
|
836
864
|
}
|
|
837
865
|
const unpackedBytes = reference.segments.reduce((sum, segment) => sum + segment.end - segment.start + 1, 0);
|
|
838
866
|
return { program: packed.program, packedBytes: packed.program.length, unpackedBytes, ...(packed.cycles === undefined ? {} : { cycles: packed.cycles }) };
|
|
@@ -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
|
|
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
|
|
313
|
-
|
|
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
|
|
319
|
-
const title =
|
|
320
|
-
|
|
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
|
-
|
|
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;
|