@henols/c64-re-tools 2.3.0-rc.4 → 2.3.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 (62) hide show
  1. package/package.json +1 -1
  2. package/skills/c64-assembler/SKILL.md +149 -78
  3. package/skills/c64-assembler/scripts/assembler.ts +18 -11
  4. package/skills/c64-basic/SKILL.md +90 -52
  5. package/skills/c64-basic/scripts/basic.ts +15 -6
  6. package/skills/c64-development/SKILL.md +46 -73
  7. package/skills/c64-disk/SKILL.md +145 -73
  8. package/skills/c64-disk/scripts/disk.ts +11 -6
  9. package/skills/c64-docs/SKILL.md +199 -118
  10. package/skills/c64-docs/references/page-types.md +3 -3
  11. package/skills/c64-docs/references/style.md +9 -13
  12. package/skills/c64-docs/scripts/docs.ts +39 -15
  13. package/skills/c64-docs/scripts/lib/links.ts +1 -1
  14. package/skills/c64-docs/scripts/lib/reference.ts +2 -2
  15. package/skills/c64-docs/scripts/lib/source-map.ts +2 -2
  16. package/skills/c64-export/SKILL.md +21 -45
  17. package/skills/c64-knowledge/SKILL.md +489 -233
  18. package/skills/c64-knowledge/scripts/knowledge.ts +31 -26
  19. package/skills/c64-memory-map/SKILL.md +131 -65
  20. package/skills/c64-memory-map/scripts/memory-map.ts +19 -11
  21. package/skills/c64-packer/SKILL.md +120 -72
  22. package/skills/c64-packer/scripts/packer.ts +10 -3
  23. package/skills/c64-provenance/SKILL.md +301 -173
  24. package/skills/c64-provenance/scripts/lib/library.ts +6 -6
  25. package/skills/c64-provenance/scripts/provenance.ts +85 -36
  26. package/skills/c64-reverse-engineering/SKILL.md +55 -72
  27. package/skills/c64-source/SKILL.md +316 -224
  28. package/skills/c64-source/scripts/lib/export.ts +9 -14
  29. package/skills/c64-source/scripts/lib/loader.ts +5 -5
  30. package/skills/c64-source/scripts/lib/source.ts +5 -5
  31. package/skills/c64-source/scripts/source.ts +106 -21
  32. package/skills/c64-static-analysis/SKILL.md +188 -112
  33. package/skills/c64-static-analysis/scripts/static-analysis.ts +29 -9
  34. package/skills/c64-testing/SKILL.md +272 -166
  35. package/skills/c64-testing/references/recording-format.md +11 -12
  36. package/skills/c64-testing/references/scenario-format.md +20 -5
  37. package/skills/c64-testing/scripts/lib/diff-files.ts +8 -8
  38. package/skills/c64-testing/scripts/lib/play.ts +1 -1
  39. package/skills/c64-testing/scripts/lib/recording.ts +7 -7
  40. package/skills/c64-testing/scripts/lib/replay.ts +7 -7
  41. package/skills/c64-testing/scripts/lib/runs.ts +6 -7
  42. package/skills/c64-testing/scripts/lib/scenario.ts +12 -6
  43. package/skills/c64-testing/scripts/testing.ts +36 -16
  44. package/skills/c64-unpacker/SKILL.md +273 -127
  45. package/skills/c64-unpacker/scripts/lib/loads.ts +3 -3
  46. package/skills/c64-unpacker/scripts/unpacker.ts +70 -11
  47. package/skills/emulator/SKILL.md +12 -12
  48. package/src/errors.ts +22 -2
  49. package/src/host/vice/session.ts +1 -1
  50. package/src/host-client/entry-state.ts +1 -1
  51. package/src/host-client/image.ts +3 -3
  52. package/src/host-client/steps.ts +1 -1
  53. package/src/knowledge/expression.ts +1 -1
  54. package/src/knowledge/names.ts +2 -3
  55. package/src/knowledge/program.ts +2 -3
  56. package/src/knowledge/write.ts +2 -2
  57. package/src/mcp/inputs.ts +1 -1
  58. package/src/mcp/tools/input.ts +1 -1
  59. package/src/mcp/tools/media.ts +1 -1
  60. package/src/native/exomizer.ts +2 -2
  61. package/src/project.ts +2 -2
  62. package/src/script.ts +164 -75
@@ -1,319 +1,411 @@
1
1
  ---
2
2
  name: c64-source
3
- description: Use this skill to generate ACME source code for a C64 program from the project knowledge. The knowledge holds the program, its entry state, the coverage and the names. The script writes the source files. It checks that the source assembles to the same bytes as the program. It can also write a loader that sets the entry state of an unpacked program. It can also export the program as the start of a new program. The export is a new project with one value for each switch and without unused code. Use it to make a rebuild. Do not edit the generated files. Change the knowledge and generate again.
3
+ description: Generates ACME source code for a C64 program from the project knowledge. It checks that the source assembles to the same bytes as the program. It can also write a loader that sets the entry state of an unpacked program. It can also export the program as the start of a new program. The export is a new project with one value for each switch and without unused code. Use it to make a rebuild of a program. Use it when the source must show new names, comments, regions, modules, macros or patches of the knowledge. Use it to start a new program from a rebuild. It does not change the project knowledge. It does not build or test the program.
4
4
  ---
5
5
 
6
- # c64-source
6
+ # Generate source
7
7
 
8
- ## Purpose
8
+ Use this script to generate ACME source from the project knowledge, and to export the program as the start of a new program.
9
+ Use only the commands of the script. Do not write or edit the generated source in another way.
9
10
 
10
- Generate ACME source from the knowledge: the program, its entry state, the coverage, and the names and the other facts. The same knowledge gives the same source. Make the source better through the knowledge: names, comments, regions, modules and macros. Change the program through patches. Then generate again.
11
+ ```bash
12
+ node <skill>/scripts/source.ts <command>
13
+ ```
11
14
 
12
- This skill does not change the knowledge.
15
+ `<skill>` is the base directory of this skill. Run the script with `node`. You can run it from any directory.
16
+ Each command in this skill, for example `generate --out src`, goes in the place of `<command>`.
17
+ Each command prints one JSON envelope on stdout. See the section "JSON output".
18
+ To get the command list, run the script with no arguments.
13
19
 
14
- ## Inputs
20
+ ---
15
21
 
16
- - The program in the knowledge. Put it in with `image` of the c64-knowledge skill: a capture of the c64-unpacker skill, or a PRG.
17
- - The output directory for the source files, relative to the project folder.
18
- - Optional entry points of code.
19
- - Optional: the coverage in the knowledge. Add it with `coverage` of the c64-knowledge skill.
20
- - For the loader: the entry state of a capture in the knowledge.
22
+ ## Select the command
21
23
 
22
- ## Tools
24
+ | Goal | Command |
25
+ |---|---|
26
+ | Generate the source and check it | `generate --out src` |
27
+ | Generate the source with one more entry point of code | `generate --out src --entry '$080d'` |
28
+ | Generate the source of drive code or of a cartridge bank | `generate --out src --space drive8` |
29
+ | Generate the source and a loader that sets the entry state | `generate --out src --loader` |
30
+ | Use analyzer names for addresses that have no other name | `generate --out src --analyzer-names` |
31
+ | Export the program as the start of a new program | `export --to ../game-new` |
32
+ | Export the program, and make a range free | `export --to ../game-new --name game --drop '$081b-$0822'` |
33
+ | Export the program, and keep a range that nothing uses | `export --to ../game-new --keep '$2000-$20ff'` |
23
34
 
24
- ```
25
- node <skill>/scripts/source.ts generate --out src
26
- node <skill>/scripts/source.ts generate --out src --entry '$080d'
27
- node <skill>/scripts/source.ts generate --out src --loader
28
- ```
35
+ ---
29
36
 
30
- | Option | Meaning |
31
- |---|---|
32
- | `--out src` | The directory for the source files. The script writes over the files that it generated before. It removes a generated `.asm` file that this run does not write. A run without `--loader` keeps `loader.asm` and `loader.prg` of an earlier run. |
33
- | `--entry '$080d'` | An address where code starts. Use it again for more entry points. The entry of the program, the routines and the code regions in the knowledge are entry points too. |
34
- | `--analyzer-names` | Name an address that has no symbol and no platform name with its analyzer name, for example `FUN_17e2`. The order is heuristic, Ghidra, then DXA. Without it, the source makes its own names, for example `l_17e2`. |
35
- | `--space drive8` | Generate the source of drive code or a cartridge bank from the knowledge of that space. The coverage, `--loader` and the platform names are only for `c64`. |
36
- | `--loader` | Also write `loader.asm` and `loader.prg`. This needs a capture in the knowledge, with its entry and its entry state. |
37
- | `--max-listed 20` | List at most this number of items in each list. The default is 50. The counts are always in the result. |
37
+ ## The program and the folders
38
38
 
39
- When the knowledge has coverage, code that no run executed gets the line `; not executed` with its range. `notExecuted` lists it. The executed code is always instructions in the source.
39
+ - The program comes from the project knowledge. The entry, the entry state, the coverage, the names and the other facts also come from it. Put the program bytes into the project knowledge before the first command.
40
+ - For a packed program, capture the unpacked program at its entry point first. Then put the capture into the project knowledge.
41
+ - For the loader, the knowledge must have the entry and the entry state of a capture.
42
+ - `--out` is the folder for the source, relative to the project folder, for example `src`.
43
+ - `--entry` is an address where code starts. Give it only when the knowledge does not show the code. The entry of the program, the routines and the code regions of the knowledge are entry points already.
44
+ - `--space` is the knowledge space of the program. Use the space where the program bytes are in the project knowledge.
45
+ - `--to` is the folder of the new project. Use a new or empty folder beside the project, for example `../game-new`. No project can hold it.
46
+ - `--drop` is a range that becomes free. Get the range of executed code from `unreachableExecuted[].drop` of an earlier export. You can also give a data range.
47
+ - `--keep` is a range that must stay, also when nothing uses it.
40
48
 
41
- ### Calling the script
49
+ ### Paths and the project folder
42
50
 
43
- - Call a script as `node <skill>/scripts/<script>.ts <command> [subjects] [options]`. The command comes first.
44
- - `<script>.ts --help` lists the commands. `<script>.ts <command> --help` gives the subjects and the options of one command. The help is JSON: `{"text": "..."}`.
45
- - An address is `$` and 1 to 4 hex digits, for example `'$c000'` or `'$fb'`, or a name. A name is a symbol, else an analyzer name that only one analyzer gives. Quote an address in a shell.
51
+ - Give each path relative to the project folder. The project folder is the nearest folder at or above the working directory that holds `.c64-re-tools/`.
52
+ - If the working directory is not in a project, give `--project <folder>` to each command. If the working directory has projects below it, the script refuses and lists them.
53
+ - An address is `$` and 1 to 4 hex digits, for example `'$c000'` or `'$fb'`, or a name. A name is a symbol, else an analyzer name that only one analyzer gives. Put an address in single quotes in a shell.
46
54
  - A range is `<start>-<end>`, for example `'$c000-$cfff'`. In a JSON file, a range is `start` and `end`.
47
55
  - Bytes in a JSON file are hex in a field `data`, for example `"data": "a900"`.
48
- - The script examines each `--out` path before it starts work. It refuses a path in `.c64-re-tools/knowledge/` and the index of the knowledge, because only c64-knowledge writes them. It refuses a folder where the command writes a file, and a file where the command writes a folder.
49
- - The result is one JSON object on one line. A file that the script writes shows as `{"out", "bytes", "sha256"}`. A list shows as `{"count", "listed"}`, and `rest` tells where to get the rest of a long list.
50
- - An error is `{"error": {"code": "...", "message": "..."}}`. The message tells what is wrong and what to do.
51
- - The exit status is 0 for done or PASS, 1 for FAIL, 2 for an error and 3 for INCONCLUSIVE.
52
-
53
- ### Project folder
54
-
55
- - Paths are relative to the project folder. The project folder is the nearest folder at or above the working directory that holds `.c64-re-tools/`.
56
- - If the working directory is not in a project and has projects below it, the script refuses and lists them. Then give `--project <folder>` to each script.
57
56
  - Write each file that you make for your work in `.c64-re-tools/local/` of the project. Git ignores that folder. Examples are results, batches, step files, runs, snapshots and memory dumps.
58
- - Move a file out of `.c64-re-tools/local/` only when it is the evidence for a finding. Move it before you give it to `--evidence test:<file>` or `--evidence file:<file>` of c64-knowledge. These two refuse a file in `.c64-re-tools/local/`.
59
- - Do not move a coverage map after `coverage` of c64-knowledge imported it. `--evidence coverage:<map>` names the map by its path at the import. The knowledge keeps the coverage and the SHA-256 of the map, so the evidence stays valid.
60
- - Put a run that is evidence in `runs/<name>/`, with its run file, its results and the snapshot files that it uses. Change the paths in the run file to the new paths. Then git keeps them, and another person can do the run again.
57
+ - The script refuses an `--out` path in `.c64-re-tools/knowledge/` and the index of the knowledge. Change the project knowledge only with the commands that change it.
58
+ - The script refuses a folder where the command writes a file. It refuses a file where the command writes a folder.
59
+ - When a file is the evidence for a finding in the project knowledge, move it out of `.c64-re-tools/local/` first. The project knowledge refuses evidence in that folder.
60
+ - Do not move a coverage map after you put it into the project knowledge. The knowledge refers to the map by its path at that time.
61
+ - Put a run that is evidence in `runs/<name>/`, with its run file, its results and its snapshot files. Change the paths in the run file to the new paths. Then git keeps them, and another person can do the run again.
61
62
  - A shell redirect (`>`) is relative to the working directory, not to the project folder. From the folder above the project, write `<folder>/.c64-re-tools/local/result.json`.
62
63
 
63
- ## Workflow
64
+ ---
65
+
66
+ ## Flags
67
+
68
+ Each flag applies only to the commands in the table. On another command, the script refuses the flag.
64
69
 
65
- 1. Put the program into the knowledge with `image` of the c64-knowledge skill. For a packed program, capture it first with the c64-unpacker skill. For a program without a packer, use the PRG.
66
- 2. Run the script.
67
- 3. Read `result` and `check`. `check.identical: true` tells that the build of the base release assembles to the program byte by byte. Without switches, this build is the default build.
68
- 4. Read the source. A label `l_xxxx` is an address that has no name in the knowledge.
69
- 5. Record names, comments, regions and modules with the c64-knowledge skill. Use `apply` for many changes.
70
- 6. Run the script again. Do steps 4 to 6 again until the source is clear.
71
- 7. Build the program with the c64-assembler skill: `assembler.ts build src main.asm --out <file.prg>`. This is the default build. For a release, add `--release <label>`.
72
- 8. For an unpacked program, run the script with `--loader` at the end. Test `loader.prg` against the original with the c64-testing skill.
73
-
74
- ## The source
75
-
76
- - `main.asm` sets the address of each segment and includes the files in address order.
77
- - Each module in the knowledge is one file. A module starts at its address and stops at the next module or at the end of its segment. Without modules, each segment is one file, `segment_xxxx.asm`.
78
- - `constants.asm` holds the names of addresses outside the program, for example `EXTCOL = $d020`. The file puts the names in groups by memory area, for example zero page, VIC-II registers or CIA 1 registers. Each group is in address order. Drive code and banks have one group only. The side comment of a name tells what the address is. This text is the knowledge comment of the address. Without a comment, the text is the platform meaning. When the knowledge name is not the platform name, the platform name comes first. For an I/O mirror without a meaning, the text gives the register that the mirror repeats. Then `Used:` counts how the source uses the name. The kinds are `read`, `written`, `modified`, `called`, `jumped to`, `jumped through`, `branched to`, `pointer`, `address`, `table` and `macro argument`. `address` is an immediate operand expression. `table` is an entry in a table of addresses.
79
- - `config.asm` holds the value of each switch, for example `PROTECTION = 0`. The first value of a switch is its default. The switches are in groups by kind. The side comment gives the releases that have the switch on, the ranges of its patches and the comment of the switch. The user can change a value to 0 or 1. The next run keeps the values of the file. It adds a new switch with its default and removes a switch that the knowledge does not have.
80
- - `switches.asm` holds every 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. A build without a release reads the values from `config.asm`.
81
- - A label or a constant gets the name of the address in the knowledge. Without a knowledge name, it gets the platform name of the c64-memory-map skill, for example `EXTCOL` or `CHROUT`. A platform name wins over a name from an analyzer (`dxa`, `ghidra` or `heuristic`), for example `EXTCOL` over `ld020`. A `user` or `llm` name wins over a platform name. An address in the program without a name gets `l_xxxx`.
82
- - The source uses the names of a ROM only when the program uses that ROM. When the knowledge has coverage, a ROM is in use when a run executed code of it. A read of ROM bytes as data does not count. Without coverage, `$01` of the entry state tells which ROMs are in. When the program uses neither ROM, the zero page and pages 2 and 3 get no system names. `romNamesLeftOut` gives the ROMs that the source did not name, and the evidence. An operand expression or a macro use with such a name gets that reason.
83
- - `sta $d011 ; $d011: yScroll 3, rows 25, displayEnabled` is a store of an immediate to a register that the c64-memory-map skill decodes. The side comment gives the decoded value. A side comment in the knowledge replaces it.
70
+ | Flag | Command | Effect |
71
+ |---|---|---|
72
+ | `--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` and `loader.prg` of an earlier run. |
73
+ | `--entry <address>` | `generate` | An address where code starts. Give the flag again for more addresses. There is no default. |
74
+ | `--space <space>` | `generate` | The knowledge space: `c64`, `drive8`, or `bank0` to `bank255`. The default is `c64`. The coverage, `--loader` and the platform names apply only to `c64`. |
75
+ | `--loader` | `generate` | Also write `loader.asm` and `loader.prg`. It needs a capture in the knowledge. The default is no loader. |
76
+ | `--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`. |
77
+ | `--to <folder>` | `export` | The folder of the new project. It is necessary. The script also writes `<folder>.zip` beside the folder. |
78
+ | `--name <name>` | `export` | The name of `baseline/<name>.prg` and `build/<name>.prg`. The default is the name of the folder. |
79
+ | `--keep <start>-<end>` | `export` | A range that stays, also when nothing uses it. Give the flag again for more ranges. |
80
+ | `--drop <start>-<end>` | `export` | A range that becomes free. Give the flag again for more ranges. Code that stays must not go to the range or read it. |
81
+ | `--max-listed <n>` | `generate`, `export` | List at most `n` items in each list. The default is 50. The counts are always in the body. |
82
+
83
+ ---
84
+
85
+ ## Generate the source
86
+
87
+ ### `generate --out <dir>`
88
+
89
+ The command writes the source files, `source-map.json` and `not_executed.json` in `--out`. Then it checks them.
90
+ The same knowledge always gives the same source.
91
+ Do not edit the generated files. The next run writes over them. The values in `config.asm` are the one exception.
92
+ To make the source better, change the project knowledge. Then generate again.
93
+
94
+ `body` holds these fields:
95
+
96
+ - `result`: `PASS` when every check passes, else `FAIL`.
97
+ - `files`: the files in `--out`, each with `out`, `bytes` and `sha256`. `sourceMap` names `source-map.json`.
98
+ - `removed`: the files of an earlier run that the script removed.
99
+ - `instructions`, `dataBytes` and `undecodedBytes`.
100
+ - `labels`: `named` labels from the knowledge, and `generated` labels `l_xxxx`.
101
+ - `check`: `identical`, and the result for each segment of the build of the base release. A segment that is different gives its first different address.
102
+ - `config.notDefault`: each switch in `config.asm` with a value that is not its default, with its `value` and its `default`.
103
+ - `romNamesLeftOut`: the ROMs that the source does not name, and the evidence in `from`.
104
+ - `entryOutside`: the entry is in no segment of the program, with a `reason`.
105
+ - `dataOnly`: the knowledge has no entry, no routine, no code region and no executed address in the space.
106
+ - `entriesInData`: each `--entry` in a data region, with the `region` and its `origin`.
107
+ - `asBytes`: the instructions that the source has as bytes.
108
+ - `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.
109
+ - `unusedModules`: modules whose start is in no segment of the program. Often the program does not hold the code that the knowledge describes.
110
+ - `operandExpressions`: the count of instructions with an operand expression.
111
+ - `operandsAsNumbers`: each operand expression that the source has as a number, with its `address`, `expression` and `reason`.
112
+ - `macroUses`: the count of macro uses in the source.
113
+ - `macrosAsInstructions`: each macro use that the source has as instructions, with its `start`, `end`, `macro` and `reason`.
114
+ - `patchesNotWritten`: each patch that the source does not have, with its range, `switch` and `reason`.
115
+ - `switches`, `default`, `releases`: the checks of the switches. See "Switches and releases".
116
+ - `loader`: the check of the loader. See "The loader".
117
+
118
+ `result` is `FAIL` for each of these:
119
+
120
+ - The source does not assemble, or it does not assemble to the program.
121
+ - The entry is in no segment of the program.
122
+ - A patch is in `patchesNotWritten`.
123
+ - A switch or the default build does not fit.
124
+ - A release does not give its file. A release with `layout differs` does not give `FAIL`. But the base release must be `identical`.
125
+ - The loader is not verified.
126
+
127
+ `hint` gives the action for each finding. When a change of the project knowledge corrects a finding, `nextAction` is the same `generate` command. Run it after the change. A `PASS` without findings gives a `hint` and no `nextAction`.
128
+
129
+ Do these actions for the findings:
130
+
131
+ | Finding | Action |
132
+ |---|---|
133
+ | `check.diagnostics` | ACME does not assemble the source. A name in the knowledge can be wrong for ACME. Read the diagnostics. Rename the symbol in the project knowledge. |
134
+ | `check.identical: false` without diagnostics | This is a defect of the script. Give the result to the user. |
135
+ | `operandsAsNumbers` | Read each `reason`. Record the operand expression again, or remove it, in the project knowledge. |
136
+ | `macrosAsInstructions` | Read each `reason`. Correct the macro or the args in the project knowledge, or remove the use. The source stays identical, so this is not a failure. |
137
+ | `patchesNotWritten` | Read each `reason`. Move the start or the end of the patch to an instruction, or give the switch another name. If the switch is not in the knowledge, record the switch. |
138
+ | `entryOutside` | Capture the program again, so that a segment holds the entry. Put the capture into the project knowledge. The script still writes and checks the source. |
139
+ | `dataOnly` | This is not a failure. If the space has code, give `--entry`, or record a code region in the project knowledge. |
140
+ | `entriesInData` | An analyzer region of data hides the code at the entry. If the entry is code, record a code region at the entry in the project knowledge. A region of the user or the LLM replaces the analyzer region. |
141
+ | `invalid-input` that names a symbol or a module | Change that symbol or module in the project knowledge. |
142
+ | `conflict` | The knowledge has conflicts from a git merge. Merge the open changes of the project knowledge. Then run the command again. |
143
+ | A list with `rest` | Give a larger `--max-listed`. For `notExecuted`, read the file in `rest`. |
144
+
145
+ #### The source files
146
+
147
+ - `main.asm` sets the address of each segment. It includes the files in address order.
148
+ - Each module in the knowledge is one file. A module starts at its address. It stops at the next module or at the end of its segment. Without modules, each segment is one file, `segment_xxxx.asm`.
149
+ - `constants.asm` holds the names of addresses outside the program, for example `EXTCOL = $d020`. The names are in groups by memory area, in address order. Drive code and banks have one group only.
150
+ - The side comment of a constant is the knowledge comment of the address. Without a comment, it is the platform meaning. When the knowledge name is not the platform name, the platform name comes first. For an I/O mirror without a meaning, the comment gives the register that the mirror repeats.
151
+ - After the side comment, `Used:` counts how the source uses the name. The kinds are `read`, `written`, `modified`, `called`, `jumped to`, `jumped through`, `branched to`, `pointer`, `address`, `table` and `macro argument`. `address` is an immediate operand expression. `table` is an entry in a table of addresses.
152
+ - `config.asm` holds the value of each switch, for example `PROTECTION = 0`. The switches are in groups by kind. The side comment gives the releases that have the switch on, the ranges of its patches and the comment of the switch.
153
+ - 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.
154
+ - `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.
155
+ - `macros.asm` holds the macros of the knowledge. `main.asm` and `loader.asm` include it.
156
+
157
+ #### Names in the source
158
+
159
+ - A label or a constant gets the name of the address in the knowledge.
160
+ - Without a knowledge name, it gets the platform name, for example `EXTCOL` or `CHROUT`. A platform name wins over an analyzer name (`dxa`, `ghidra` or `heuristic`), for example `EXTCOL` over `ld020`. A `user` or `llm` name wins over a platform name.
161
+ - An address in the program without a name gets `l_xxxx`.
162
+ - The source uses the names of a ROM only when the program uses that ROM. With coverage, the program uses a ROM when a run executed code of it. A read of ROM bytes as data does not count. Without coverage, `$01` of the entry state tells which ROMs are in.
163
+ - When the program uses no ROM, the zero page and pages 2 and 3 get no system names. An operand expression or a macro use with such a name gets that reason.
164
+
165
+ #### Lines in the source
166
+
167
+ - `sta $d011 ; $d011: yScroll 3, rows 25, displayEnabled` is a store of an immediate to a register with a known meaning. The side comment gives the decoded value. A side comment in the knowledge replaces it.
84
168
  - Code regions, and the decoded instructions outside of data regions, become instructions.
85
169
  - Data regions become data by their type: `!byte`, `!word`, `!pet`, `!scr`, `!fill`, tables of labels, and sprite bytes in binary.
86
170
  - `lda+2 $0012` is an absolute operand below $0100. `+1` is a zero-page operand with a label.
87
171
  - `sta NMINV+1` is an address inside a platform entry of more than one byte, for example the high byte of a vector.
88
- - `lda room_colours+5` is an address inside a data region whose start has a name. The address has no name of its own.
172
+ - `lda room_colours+5` is an address inside a data region with a named start. The address has no name of its own.
89
173
  - `name = * + 1` is a label inside an instruction, for example for code that changes its own operand.
90
- - `lda #<irq_handler` and `lda bar_colours-1,x` are operand expressions of the knowledge. The script writes each name of an expression with the name of its address in the source. An operand expression in a non-immediate mode that starts with a bracket gets `0+` before it, because ACME reads `(...)` as indirect.
174
+ - `lda #<irq_handler` and `lda bar_colours-1,x` are operand expressions of the knowledge. An expression in a non-immediate mode that starts with a bracket gets `0+` before it. ACME reads `(...)` as indirect.
91
175
  - `; = <irq_handler` at the end of an instruction with a number: the source cannot use this operand expression. `operandsAsNumbers` gives the reason.
92
176
  - `!byte` with an instruction as its comment: ACME writes this instruction with a different opcode. The bytes keep the source identical.
93
177
  - `not decoded` marks bytes in a code region that the script did not decode.
94
178
  - `; not executed $xxxx-$yyyy` starts code that no run of the coverage executed. The range is the same as in `not_executed.json`.
95
- - `+inc16 ptr` is a use of a macro of the knowledge. `macros.asm` holds the macros. `main.asm` and `loader.asm` include it. The comments inside a use go to its side comment.
96
- - `!zone patch_PROTECTION_100e` starts a patch of the knowledge. The condition is the state that the program does not have. It is `!if TRAINER` when the base release has the switch off. It is `!if PROTECTION = 0` when the base release has it on. The 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`. Thus all builds have them.
179
+ - `+inc16 ptr` is a use of a macro of the knowledge. The comments inside a use go to its side comment.
180
+ - `!zone patch_PROTECTION_100e` starts a patch. The condition is the switch state that the program does not have. It is `!if TRAINER` when the base release has the switch off. It is `!if PROTECTION = 0` when the base release has it on.
181
+ - 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`.
97
182
  - 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.
98
183
 
99
- Do not edit the generated files. The next run writes over them. The values in `config.asm` are the one exception.
184
+ #### Switches and releases
100
185
 
101
- ## Switches and macros
186
+ 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.
102
187
 
103
- The program of the knowledge is the base release. The default build is the clean program: no copy protection, no trainer, no cheat and no crack change, and all corrections. Thus the default build can differ from the program. The script does these checks:
188
+ The command does these checks. They use the defaults and the values of the knowledge, not the values in `config.asm`.
104
189
 
105
- 1. `check`: the build of the base release, with each switch at its base value, must give the program byte by byte.
106
- 2. `switches`: the script builds each switch alone at the other value. It does not build two switches together. The build must have the same length as the build of the base release. It can change bytes only in the ranges of the patches of the switch.
107
- 3. `default`: the default build must have the same length. It can change bytes only in the patches of the switches in `default.in`. These are the switches whose default is not their base value.
108
- 4. `releases`: the script builds each release with `RELEASE_<label>=1` and compares it with the file of the release.
190
+ 1. `check`: the build of the base release must give the program byte by byte. In this build, each switch has its base value.
191
+ 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.
192
+ 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`.
193
+ 4. `releases`: each release, built with `RELEASE_<label>=1`, against the file of the release.
109
194
 
110
- The checks use the defaults and the values of the knowledge. They do not use the values in `config.asm`. When `config.asm` has a value that is not the default, `config.notDefault` gives the switch, its value and its default.
195
+ Each item of `switches` has `name`, `kind`, `default`, `base`, `define` and the patches.
111
196
 
112
- Build the program with the c64-assembler skill:
197
+ - `result: fits` tells that the build is good. `changedBytes` counts the changed bytes.
198
+ - `overflow`: the code of a patch is longer than its range. Make the code shorter, or make the range larger, in the project knowledge.
199
+ - `changes outside`: the build changed a byte outside the ranges. The result gives the first such byte.
200
+ - `does not assemble`: read the ACME diagnostics. Correct the code of the patch.
201
+ - `insideNames` lists the labels inside a patch. Code that writes to one of these labels changes the patch when the switch is on.
113
202
 
114
- - The build of `config.asm`: no option. With the defaults, this is the default build.
115
- - A release: `--release ORIGINAL`.
116
- - One switch: set its value in `config.asm`, for example `TRAINER = 1`. Do not use `--define` for a switch. ACME stops with `Symbol already defined`, because `config.asm` also gives the switch a value.
203
+ `default` has `in`, `result` and `changedBytes`, as in `switches`. When `default` has `overflow` and the message `turn on one of them`, two alternatives are in the default build. Give one of the switches the default of the base release.
204
+
205
+ Each item of `releases` has `label`, `base`, `file`, `define` and `result`:
206
+
207
+ - `identical`: the build gives the bytes of the file.
208
+ - `different`: the release has bytes that no patch gives. Compare the release with the program. Then record a switch and a patch for each difference.
209
+ - `layout differs`: the release has other segments than the program. Patches cannot move code. This result does not fail the run.
210
+ - `file changed`: the file is not the file that the knowledge recorded. Put back the file, or record the release again in the project knowledge.
211
+ - `does not assemble`: read the ACME diagnostics.
117
212
 
118
213
  The script writes a patch only when its range is complete items of one file. A patch cannot cut an instruction. Its switch name cannot be a name of the source.
214
+ The script writes a macro use only when the use gives the bytes of the program. A label inside the range of a use also keeps the instructions. The macro line has no place for the label.
215
+
216
+ Build the program from the source in these ways:
119
217
 
120
- The script writes a macro use only when the use gives the bytes of the program. First it assembles each use alone at its address. Then it checks the full build. A use that fails stays instructions, and `macrosAsInstructions` gives the reason. A label inside the range of a use also keeps the instructions, because the macro line has no place for the label.
218
+ - The build of `config.asm`: no option. With the defaults, this is the default build.
219
+ - A release: the build option `--release ORIGINAL`.
220
+ - 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.
121
221
 
122
- ## The loader
222
+ #### The loader
123
223
 
124
224
  `--loader` writes `loader.asm` and assembles it to `loader.prg`. The loader does these steps:
125
225
 
126
226
  1. `10 SYS 2061` starts it.
127
- 2. It stops the interrupts and makes all RAM visible. Then it moves its copier to two free pages. Thus a copier at $a000 or higher is not read from the BASIC ROM.
128
- 3. It copies each segment and the pages $0002-$03ff of the entry state into place. The script selects an order in which no copy writes over the packed bytes of a copy that has not run yet.
129
- 4. It sets the screen registers of the VIC-II and the VIC bank from `io` in the entry state. Then it waits for the raster line of the entry.
130
- 5. It sets the CIA timers, their latches and the interrupt masks from `io` and `cia` in the entry state. A running timer gets the value that counts down to the timer value of the entry at the entry.
131
- 6. It sets $00/$01, the stack pointer, A, X, Y and the flags, and jumps to the entry.
227
+ 2. It stops the interrupts and makes all RAM visible. It moves its copier to two free pages.
228
+ 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.
229
+ 4. 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.
230
+ 5. 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.
231
+ 6. It sets $00/$01, the stack pointer, A, X, Y and the flags. Then it jumps to the entry.
132
232
 
133
- The copy table, the copier and the jump to the entry use labels, not numbers. `loader_from_xxxx` and `loader_end_xxxx` are around each packed segment, and `loader_to_xxxx` is its first address in place. The jump goes to the label of the entry. Thus a segment can change its length, and the loader still copies it and jumps to the entry that moved. The screen values, the raster line and the CIA values stay numbers. They are the entry state, not addresses of the program.
233
+ The copy table, the copier and the jump use labels, not numbers. `loader_from_xxxx` and `loader_end_xxxx` are around each packed segment. `loader_to_xxxx` is its first address in place. Thus a segment can change its length, and the loader still works.
134
234
 
135
- The flags go through the free stack byte below the stack pointer (`stackByte`). The loader changes this byte. The loader does not set the SID, the time of day of the CIAs, the CIA interrupt flags or the interrupt registers of the VIC-II. `loader.limits` names each CIA value that the loader cannot set as it was at the entry. A program that reads the SID voice 3 for random numbers can still differ in a scenario. A raster sync does not change a timer or the SID. Then observe after the program sets up its own random numbers. To sync the original and the rebuild in a test, see the sync rules of the c64-testing skill.
235
+ 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 original and the rebuild, observe after the program sets its own random numbers.
136
236
 
137
- The script runs `loader.prg` to its jump to the entry. It compares the memory, $00/$01, the registers, the screen registers of the VIC-II, the VIC bank and the raster line with the entry state there. It also compares the CIA timers, latches and interrupt masks. `loader.verified: true` tells that all of them are equal. An interrupt can run right at the entry, as in the original program.
237
+ The entry state is the state of the base release. Thus the script builds and checks `loader.prg` with the switch values of the base release. For a loader of the default build, build `loader.asm` with no option.
138
238
 
139
- The entry state is the state of the base release. Thus the script builds and checks `loader.prg` with the switch values of the base release. For a loader of the default build, build `loader.asm` with the c64-assembler skill and no option.
239
+ `loader` holds these fields:
140
240
 
141
- ### The packed loader
241
+ - `program`, the `copies` in their order, `copier` and `stackByte`.
242
+ - `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.
243
+ - `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.
244
+ - `packed`: `packedBytes` and `unpackedBytes` of the packed form.
142
245
 
143
- A loader file that loads into $d000 or higher writes the I/O area. When the image is too large for one loader file, `loader.asm` has the packed form. It has these differences:
246
+ Do these actions when `verified` is `false`:
247
+
248
+ | Field | Action |
249
+ |---|---|
250
+ | `reason` about the copy order, the free pages or the count of copies | The reason gives the ranges. Capture the program again with fewer segments, or write the loader by hand. |
251
+ | `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. |
252
+ | `reason` that the source does not assemble | Correct the source first. The loader includes the source. |
253
+ | `differences` | The loader ran. The list gives the bytes and the registers that are not equal. |
254
+ | `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. |
144
255
 
145
- - `loader.asm` holds only the copier and the bytes below $0400: pages $0002-$03ff of the entry state and the parts of segments there. The copier is in one free range, at `loader.copier`. The bytes below $0400 go into free ranges. When one range is too small, they go into more ranges, and each piece is a copy. Between these ranges, `loader.asm` holds the bytes of the image. The segments at $0400 and higher are not in `loader.asm`.
146
- - The script packs the build of `main.asm` from $0400 and the build of `loader.asm` into `loader.prg`. Where the two builds overlap, the bytes of `loader.asm` are in `loader.prg`.
147
- - `10 SYS` starts the unpacker. The unpacker writes all bytes, also at $d000-$dfff. Then it jumps to the copier with all RAM visible ($01 = $34).
148
- - The unpacker uses the zero page and $0334-$03ff. Thus the copier writes pages $0002-$03ff after the unpack. Then it does steps 4 to 6.
149
- - The jump to the entry uses the address of the entry, not its label. The build of `loader.asm` does not include `main.asm`.
256
+ #### The packed loader
150
257
 
151
- The script checks the unpack. All bytes of the two builds must be correct, and the unpacker must jump to the copier. Then the script checks `loader.prg` as for the plain loader. `loader.packed` gives `packedBytes` and `unpackedBytes`.
258
+ 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:
259
+
260
+ - `loader.asm` holds only the copier and the bytes below $0400. The segments at $0400 and higher are not in `loader.asm`. Between its free ranges, `loader.asm` holds the bytes of the program.
261
+ - `loader.prg` is the build of `main.asm` from $0400 and the build of `loader.asm`, packed into one file. Where the two builds overlap, `loader.prg` has the bytes of `loader.asm`.
262
+ - The jump to the entry uses the address of the entry, not its label.
152
263
 
153
264
  To make the packed loader for other switch values, do these steps:
154
265
 
155
- 1. Build the program with the c64-assembler skill: `assembler.ts build src main.asm --out build/main.prg`.
156
- 2. Build the copier with the c64-assembler skill: `assembler.ts build src loader.asm --out build/copier.prg`.
157
- 3. Pack the two files with the c64-packer skill: `packer.ts pack build/main.prg build/copier.prg --entry '$c000' --exit-memory '$34' --out build/loader.prg`. The comment at the start of `loader.asm` gives the `--entry` value.
266
+ 1. Assemble `main.asm` to `build/main.prg`.
267
+ 2. Assemble `loader.asm` to `build/copier.prg`.
268
+ 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`.
158
269
 
159
- These commands do not work when a segment starts below $0400. Then the build of `main.asm` has bytes in the area of the unpacker. They also do not work when a patch is between the ranges of `loader.asm`, because there `loader.asm` has the bytes of the image. In these cases, use `generate --loader` for the base release.
270
+ These steps do not work when a segment starts below $0400. They also do not work when a patch is between the free ranges of `loader.asm`. In these cases, use `generate --loader` for the base release.
160
271
 
161
- ## Export for a new application
272
+ ---
162
273
 
163
- `export` writes the program of the knowledge as the start of a new program: a new project folder and a zip file of it. A person changes the source of the new project directly. The c64-export skill gives the full procedure.
274
+ ## Export a new program
164
275
 
165
- ```
166
- node <skill>/scripts/source.ts export --to ../game-new
167
- node <skill>/scripts/source.ts export --to ../game-new --name game --drop '$081b-$0822'
168
- ```
276
+ ### `export --to <folder>`
169
277
 
170
- | Option | Meaning |
278
+ The command writes the program of the knowledge as the start of a new program: a new project folder and a zip file of it. A person changes the source of the new project directly.
279
+ First, make sure that `generate` gives `PASS`. The command writes nothing until all its checks pass.
280
+
281
+ The command sets each switch to its export value:
282
+
283
+ - `fix`, `trainer` and `cheat`: 1. Trainers and cheats become a part of the new program.
284
+ - `protection` and `crack`: 0.
285
+ - `change`: its default.
286
+
287
+ The build with these values can change bytes only in the patches. Then the command makes the free ranges. Each free range gets the bytes $00 and the label `free_xxxx`. No address moves.
288
+
289
+ `body` holds these fields:
290
+
291
+ - `result`: `PASS` when the script wrote the new project. `FAIL` with `stage` and `reason` when a check failed. Then the script wrote nothing.
292
+ - `to` and `zip`: the folder and the zip file.
293
+ - `files`: the files of the new project.
294
+ - `switches`: each switch with `kind`, `base` and the export `value`.
295
+ - `free`: the count of free bytes, and each free range with `bytes`, `what` and `label`.
296
+ - `unreachableExecuted`: each range of executed code that the entry does not get to, with the `routine` before it and its `drop` value.
297
+ - `knowledge`: the counts of the rows of the new knowledge.
298
+ - `tests`: the `written` tests, and the `leftOut` tests with the reason.
299
+ - `loader`: the copies, the copier, `stackByte` and `simulated: true`. The export does not run the loader.
300
+ - `noCoverage` or `unrooted`: why nothing became free without `--drop`.
301
+ - `spacesLeftOut`: the spaces that the export does not have. The export has only the space `c64`.
302
+
303
+ 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.
304
+ On `FAIL`, `hint` gives the action for the stage:
305
+
306
+ | `stage` | Action |
171
307
  |---|---|
172
- | `--to ../game-new` | The folder of the new project, relative to the project folder. The folder must be new or empty, and no project can hold it. The script also writes `../game-new.zip`. |
173
- | `--name game` | The name of `baseline/game.prg` and `build/game.prg`. The default is the name of the folder. |
174
- | `--keep '$2000-$20ff'` | A range that stays, also when nothing uses it. Use it again for more ranges. |
175
- | `--drop '$081b-$0822'` | A range that becomes free. Use it again for more ranges. Code that stays must not go to the range or read it. |
176
- | `--max-listed 20` | List at most this number of items in each list. The default is 50. |
308
+ | `original` | The source of the knowledge does not pass. Run `generate`, and correct the project knowledge until `generate` gives `PASS`. Then export again. |
309
+ | `switches` | The build with the export values changes bytes outside the patches, or a patch does not fit. Read `build`. Run `generate`, and correct the patches. |
310
+ | `export` | This is a defect of the script. Give the result to the user. |
311
+ | `loader` | Read `reason`, and give it to the user. The export does not make the packed form of the loader. |
177
312
 
178
- The script does these steps. It writes nothing until all the checks pass.
313
+ Traps of the command:
179
314
 
180
- 1. It builds the source of the base release. The build must give the program byte by byte, with all the patches.
181
- 2. It sets each switch to its export value:
182
- - `fix`, `trainer` and `cheat`: 1. Trainers and cheats become a part of the new program.
183
- - `protection` and `crack`: 0.
184
- - `change`: its default.
185
- 3. It builds the program with these values. This build can change bytes only in the patches.
186
- 4. It finds the free ranges. Each free range gets the bytes $00 and the label `free_xxxx`. No address moves.
187
- 5. It makes the source of the new program. The source must assemble to the new program byte by byte. For a capture with an entry state, the script also makes `loader.asm` and checks its copies. It does not run the loader.
188
- 6. It writes the new project and the zip file.
315
+ - The command refuses a folder that is not empty, and a zip file that exists. Remove the folder and the zip file of an earlier export, or give another folder.
316
+ - The command refuses two switches that change one range. The export puts both alternatives in. In the project knowledge, give all but one of them the kind `change`, with the default of the base release.
317
+ - The command refuses a `--drop` range that code which stays goes to or reads. The message names that code. It also refuses a range that cuts code or a region.
189
318
 
190
- ### The free ranges
319
+ #### The free ranges
191
320
 
192
- The script divides the decoded instructions into blocks. A block stops after RTS, RTI, JMP, BRK or a JAM, and before a gap. A block of code becomes free only when both of these are true:
321
+ A block of code stops after RTS, RTI, JMP, BRK or a JAM, and before a gap. A block of code becomes free only when both of these are true:
193
322
 
194
323
  - No run of the coverage executed it.
195
- - No path gets to it from the entry, the hardware vectors or the code that stays. A path is a call, a jump, a branch, a reference of the knowledge or an operand expression. An entry of a table and two immediate operands that give its address are paths too.
324
+ - No path gets to it from the entry, the hardware vectors or the code that stays. A path is a call, a jump, a branch, a reference of the knowledge or an operand expression. An entry of a table, and two immediate operands that give its address, are paths too.
196
325
 
197
- Code that a run executed always stays. `unreachableExecuted` lists the executed code that the entry and the vectors do not get to. An example is a copy check whose call the export removed. Give its range to `--drop` to make it free.
326
+ Code that a run executed always stays. `unreachableExecuted` lists the executed code that the entry and the vectors do not get to. An example is a copy check whose call the export removed. Give its `drop` value to `export` to make it free.
198
327
 
199
328
  Data becomes free only when all of these are true:
200
329
 
201
330
  - Its region type is `bytes`, `words`, a table of addresses, `petscii`, `workspace` or `unused`.
202
331
  - No run read, wrote or executed a byte of it.
203
- - No code that stays reads it or names it, and no table that stays has an entry in it.
204
- - It is not at the first address of the program and not at $fffa-$ffff.
332
+ - No code that stays reads it or names it. No table that stays has an entry in it.
333
+ - It is not at the first address of the program, and not at $fffa-$ffff.
205
334
 
206
- The VIC-II reads `charset`, `sprite`, `bitmap` and `screen` data without the CPU, so the coverage does not show these reads. This data stays unless `--drop` names it. Bytes without a region also stay.
335
+ The coverage does not show the reads of the VIC-II. Thus `charset`, `sprite`, `bitmap` and `screen` data stay, unless `--drop` names them. Bytes without a region also stay.
207
336
 
208
- The entry is the entry of the knowledge. Without it, the entry is the address of the first SYS in the BASIC line of the program. Without coverage, nothing becomes free by itself. Without an entry and without the hardware vectors in the image, no code is unreachable.
337
+ The entry is the entry of the knowledge. Without it, the entry is the address of the first SYS in the BASIC line of the program. Without coverage, nothing becomes free without `--drop`. Without an entry and without the hardware vectors in the program, no code is unreachable.
209
338
 
210
- ### The new project
339
+ #### The new project
211
340
 
212
- ```
213
- README.md the commands to build and test, the free ranges and the origin
341
+ ```text
342
+ README.md the steps to build and test, the free ranges and the origin
214
343
  src/ main.asm, constants.asm, macros.asm, the modules and loader.asm
215
344
  baseline/<name>.prg the exported program, and baseline/<name>.json with its symbols
216
345
  tests/ the tests of the project, changed for the new program
217
346
  .c64-re-tools/knowledge/ the knowledge of the new program
218
347
  ```
219
348
 
220
- - The source has no mark line, no `switches.asm` or `config.asm`, no patch zones and no `; not executed` lines. It keeps the names, the comments, the operand expressions and the macros. A line comment at each changed patch gives the switch, its kind, its value and the comments of the switch and the patch.
349
+ - The source has no mark line, no `switches.asm` or `config.asm`, no patch zones and no `; not executed` lines. It keeps the names, the comments, the operand expressions and the macros. A line comment at each changed patch gives the switch, its kind, its value and the comments.
221
350
  - The knowledge has one revision `export`. It holds the program, the entry and its state, the names, the comments, the regions and the modules. It has no history, analyzer names, references, operand expressions, macros, patches, switches, releases or coverage.
222
- - The tests come from the JSON files in `tests/`:
223
- - A scenario compares the build of the new project with the baseline. Both sides are the same program, so a step of the original side alone goes. A step of the rebuild side alone runs on both sides.
224
- - A run file runs the build of the new project.
225
- - Both take the names from the symbol files.
226
- - The script leaves out a test that restores a snapshot. It also leaves out a test with a name that the new program does not have, or with an address in a free range.
227
- - The script does not copy recordings, record files or `runs/`.
351
+ - A scenario of `tests/` compares the build of the new project with the baseline. Both sides are the same program. Thus a step of the original side alone goes. A step of the rebuild side alone runs on both sides.
352
+ - A run file of `tests/` runs the build of the new project. Scenarios and run files take the names from the symbol files.
353
+ - The export leaves out a test that restores a snapshot. It also leaves out a test with a name that the new program does not have, or with an address in a free range.
354
+ - The export does not copy recordings, record files or `runs/`.
228
355
  - The zip file holds the folder without `.c64-re-tools/local/`.
229
356
 
230
- The result of `export`:
357
+ ---
231
358
 
232
- - `result`: `PASS` when the script wrote the new project. `FAIL` with `stage` when a check failed: `original`, `switches`, `export` or `loader`. Then the script wrote nothing.
233
- - `to` and `zip`: the folder and the zip file.
234
- - `switches`: each switch with `kind`, `base` and the export `value`.
235
- - `free`: the count of free bytes, and each free range with `bytes`, `what` and `label`.
236
- - `unreachableExecuted`: each range of executed code that the entry does not get to, with the `routine` before it and its `--drop` option.
237
- - `knowledge`: the counts of the rows of the new knowledge.
238
- - `tests`: the `written` tests, and the `leftOut` tests with the reason.
239
- - `loader`: the copies, the copier, `stackByte` and `simulated: true`.
240
- - `noCoverage` or `unrooted`: why nothing became free by itself.
241
- - `spacesLeftOut`: the spaces that the export does not have. The export has only the space `c64`.
359
+ ## Workflow
242
360
 
243
- ## Knowledge
361
+ 1. Put the program bytes into the project knowledge. For a packed program, capture the unpacked program at its entry point first.
362
+ 2. Run `generate`.
363
+ 3. Read `result` and `check`. `check.identical: true` tells that the build of the base release is the program byte by byte.
364
+ 4. Read the source. A label `l_xxxx` is an address without a name in the knowledge.
365
+ 5. Record names, comments, regions and modules in the project knowledge.
366
+ 6. Run `generate` again. Do steps 4 to 6 again until the source is clear.
367
+ 7. Assemble `main.asm` to a program. This is the default build. For a release, give the build option `--release <label>`.
368
+ 8. For an unpacked program, run `generate` with `--loader` at the end. Compare `loader.prg` with the original program in a test.
369
+ 9. To start a new program from the rebuild, run `export`.
244
370
 
245
- Read:
246
- - symbols, regions, comments, references, modules, operand expressions, patches, macros and macro uses of all origins.
371
+ ---
247
372
 
248
- Automatic write:
249
- - none.
373
+ ## JSON output
250
374
 
251
- Do not store:
252
- - the generated source or its labels `l_xxxx`.
375
+ Each command prints one JSON envelope on one line on stdout. Notes go to stderr.
253
376
 
254
- ## Result
377
+ On success, the exit code is 0:
255
378
 
256
- - `result`: `PASS` when every check passes, else `FAIL`.
257
- - `files`: the files that the script wrote in `--out`, each with `out`, `bytes` and `sha256`. `sourceMap` names `source-map.json`.
258
- - `removed`: the files of an earlier run that the script removed from `--out` and did not write again.
259
- - `instructions`, `dataBytes` and `undecodedBytes`.
260
- - `labels`: `named` labels from the knowledge and `generated` labels `l_xxxx`.
261
- - `entriesInData`: each `--entry` in a data region of the knowledge, with the `region` and its `origin`. The script does not decode an entry in a data region.
262
- - `asBytes`: the instructions that the source has as bytes.
263
- - `operandExpressions`: the count of instructions that the source has with an operand expression.
264
- - `operandsAsNumbers`: the operand expressions that the source has as a number with a side comment, each with its `address`, `expression` and `reason`. An expression becomes a number when no instruction with an operand value is at the address, or when a name has no address. An expression that does not assemble to the program also becomes a number.
265
- - `unusedModules`: modules in the knowledge whose start is in no segment of the program. Often this tells that the program does not hold code that the knowledge describes.
266
- - `entryOutside`: the entry is in no segment of the program in the knowledge, with a `reason`. The capture does not hold the program.
267
- - `dataOnly`: the knowledge has no entry, no routine, no code region and no executed address in the space. Thus the source has only data. A table or a key page in a cartridge bank is an example.
268
- - `notExecuted`: when the knowledge has coverage, the count of instructions that no run executed, the count of their ranges, and the first ranges. `rest` names the file `not_executed.json` in `--out` when the list is not complete. This file has all of the ranges. Each range has its source `file` and the `routine` before it. `files` counts the ranges in each file.
269
- - `macroUses`: the count of macro uses that the source has.
270
- - `macrosAsInstructions`: the macro uses that the source has as instructions, each with its `start`, `end`, `macro` and `reason`.
271
- - `patchesNotWritten`: the patches that the source does not have, each with its range, `switch` and `reason`.
272
- - `check`: `identical`, and the result for each segment of the build of the base release. A different segment gives its first different address.
273
- - `switches`: the build of each switch that has patches. Each item has `kind`, `default`, `base`, the `define` for the c64-assembler skill and the patches. `result` is the result of the build with the switch at the value that is not `base`. `fits` tells that the build is good, and `changedBytes` counts the changed bytes. `overflow` tells that the code of a patch is longer than its range. `changes outside` gives the first byte outside the ranges that changed. `does not assemble` gives the diagnostics of ACME. `insideNames` lists the labels inside a patch. Code that writes to one of these labels changes the patch when the switch is on.
274
- - `default`: the default build. `in` lists the switches whose patches are in. `result` and `changedBytes` are as in `switches`.
275
- - `releases`: the build of each release. Each item has `label`, `base`, `file`, the `define` and the `result`. `identical` tells that the build gives the bytes of the file. `different` gives the segments that differ. `layout differs` tells that the release has other segments than the program: patches cannot move code, so this result does not fail the run. `file changed` tells that the file is not the file that the knowledge recorded. `does not assemble` gives the diagnostics of ACME.
276
- - `loader`: the program, the copies in their order, the copier page, `stackByte`, `verified` and `differences`. A packed loader also has `packed` with `packedBytes` and `unpackedBytes`. `limits` lists the CIA values that the loader cannot set. An entry state from an old capture has no CIA values. Then `limits` tells you to capture the program again. When the script cannot make the loader, `loader` has `verified: false` and a `reason`. The loader includes the source. Thus a source that does not assemble to the program also stops the loader.
277
-
278
- `result` is `PASS` when the source is identical and the check of the loader passes. `result` is `FAIL` for a failed check and for an entry in no segment. These also give `FAIL`:
279
-
280
- - a patch that the source does not have
281
- - a switch or a default build that does not fit
282
- - a release that the build does not give. A release with `layout differs` does not give `FAIL`, but the base release must be `identical`.
283
-
284
- `asBytes`, `notExecuted`, `operandsAsNumbers`, `macrosAsInstructions` and `patchesNotWritten` are lists of at most `--max-listed` items.
285
-
286
- ## Failure and conflicts
287
-
288
- - `check.diagnostics`: ACME did not assemble the source. A name in the knowledge can be wrong for ACME. Read the diagnostics and change the name with c64-knowledge.
289
- - `check.identical: false`: the script has a defect. Give the result to the user.
290
- - `operandsAsNumbers`: read each `reason`. Correct the expression with `operand` of the c64-knowledge skill, or remove it with `unoperand`.
291
- - `invalid-input` that names a symbol or a module: change that symbol or module in the knowledge.
292
- - `switches` with `overflow`: make the code of the patch shorter, or make the range of the patch larger, with `patch` of the c64-knowledge skill.
293
- - `switches` with `does not assemble`: read the diagnostics and correct the code of the patch.
294
- - `patchesNotWritten`: read each `reason`. Move the start or the end of the patch to an instruction, or give the switch another name. For a switch that is not in the knowledge, write it with `switch` of the c64-knowledge skill.
295
- - `default` with `overflow` and the message `turn on one of them`: two alternatives are in the default build. Give one of the switches the default of the base release.
296
- - `releases` with `different`: the release has bytes that no patch gives. Compare the releases with the c64-provenance skill. Then record a switch and a patch for each difference.
297
- - `releases` with `file changed`: put back the file, or record the release again with `release` of the c64-knowledge skill.
298
- - `macrosAsInstructions`: read each `reason`. Correct the macro or the args, or remove the use with `unuse`. The source stays identical, so this is not a failure.
299
- - `entryOutside`: the run fails, but the script still writes and checks the source. With `--loader`, `loader.verified` is `false` with the same `reason`. Capture the program again with c64-unpacker `loads` or `capture`. Make sure that a segment holds the entry. Then put it into the knowledge again with `image`.
300
- - `dataOnly`: this is not a failure. If the space has code, give `--entry` or classify the code with the c64-knowledge skill. Then generate again.
301
- - `entriesInData`: an analyzer region of data hides the code at the entry. If the entry is code, record a code region at the entry with the c64-knowledge skill, for example `classify '$0500-$05ff' code`. A region of the user or the LLM replaces the analyzer region. Then generate again.
302
- - `loader.verified: false` with a `reason`: the script did not make the loader. The script still writes and checks the source. When no copy order or no free pages exist, or the loader needs too many copies, the reason gives the ranges. Capture the program again with fewer segments, or write the loader by hand.
303
- - `loader.verified: false` with a `reason` about the packed form: the image is too large for one loader file. If the reason tells that a tool is missing, stop the task. Give the message to the user. If the free ranges are too small for the copier and the bytes below $0400, the reason gives the largest free ranges. Write the loader by hand. If the unpack check fails, give the reason to the user.
304
- - `loader.verified: false` with `differences`: the loader ran, and `differences` gives the bytes and registers that are not equal.
305
- - `loader.verified: false` with `stop`: the loader did not get to its copier or to its jump. `stop` gives the address where it stopped and `port`, the value of $01. `history` gives the last 16 instructions. `cause` gives the first byte of the copier that is not equal to its byte in the loader file. For a packed loader, `cause` gives the first byte of the copier that the unpack did not write. If the copier ran, `cause` can also give the first byte that a copy did not write correctly.
306
- - `export` with `stage: "original"`: the source of the knowledge does not pass. Run `generate`, and correct the knowledge until `generate` gives PASS.
307
- - `export` with `stage: "switches"`: the build with the export values changes bytes outside the patches, or a patch does not fit. Read `build`. Correct the patches.
308
- - `export` refuses two switches that change one range: the export puts both alternatives in. Give all but one of them the kind `change` and the default of the base release with `switch` of the c64-knowledge skill.
309
- - `export` refuses `--drop`: read the message. It names the code that stays and goes to the range or reads it, or the code or region that the range cuts.
310
- - `installation-incomplete` or `machine-unavailable`: stop the task. Give the message to the user and wait for the user. Do not install, start or change a tool yourself. Do not make a replacement for the tool, for example your own disassembler or emulator script.
311
-
312
- ## Handoffs
313
-
314
- - Capture a packed program → c64-unpacker
315
- - Put the program and the coverage into the knowledge → c64-knowledge
316
- - Names, regions, comments and modules → c64-knowledge
317
- - Build the program → c64-assembler
318
- - Test the rebuild against the original → c64-testing
319
- - The start of a new program from the rebuild → c64-export
379
+ ```json
380
+ {"ok": true, "command": "<command>", "body": {}, "hint": "...", "nextAction": "..."}
381
+ ```
382
+
383
+ On failure, the exit code is 2:
384
+
385
+ ```json
386
+ {"ok": false, "command": "<command>", "code": "<code>", "error": "...", "hint": "...", "nextAction": "..."}
387
+ ```
388
+
389
+ - `body` holds the result. The section of each command gives the fields of its body.
390
+ - A check gives its verdict in `body.result`: `PASS`, `FAIL` or `INCONCLUSIVE`. The exit code is 0 for each verdict. Thus read `body.result`, not the exit code.
391
+ - `error` is one sentence. It tells what is wrong.
392
+ - `hint` tells what to do or what to examine. Read it before your next step. Some envelopes have no `hint`.
393
+ - `nextAction` is a full command line. It runs from any directory. Run it when it agrees with your task. Some envelopes have no `nextAction`.
394
+ - `code` does not change between versions. Do the action for the code:
395
+
396
+ | `code` | Action |
397
+ |---|---|
398
+ | `installation-incomplete`, `machine-unavailable` | Stop the task. Give `error` and `hint` to the user and wait for the user. Do not install, start or change a tool. Do not make a replacement for the tool, for example your own disk parser, disassembler or emulator script. |
399
+ | `invalid-input`, `not-found` | Correct the call as `hint` tells. Then run the command again. |
400
+ | `operation-failed` | Give `error` and `hint` to the user. |
401
+ | Another code | Do as `hint` tells. |
402
+
403
+ These fields have the same meaning in each body:
404
+
405
+ | Field | Meaning |
406
+ |---|---|
407
+ | `out`, `bytes`, `sha256` | A file that the command wrote: its path, its size and its SHA-256. |
408
+ | `count`, `listed`, `rest` | A list. `count` is the number of items. `listed` holds the first items, at most `--max-listed`. `rest` tells how to get the other items. |
409
+
410
+ To get the command list, run the script with no arguments. The envelope has `command: "help"`, and `body.commands` gives the name, the summary and the synopsis of each command.
411
+ To get the subjects and the flags of one command, run `<command> --help`. Then `body.subjects` and `body.options` give them.