@henols/c64-re-tools 2.7.1 → 2.8.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.
Files changed (70) hide show
  1. package/package.json +1 -1
  2. package/skills/c64-assembler/SKILL.md +23 -38
  3. package/skills/c64-assembler/scripts/assembler.ts +137 -17
  4. package/skills/c64-basic/SKILL.md +11 -31
  5. package/skills/c64-basic/scripts/basic.ts +4 -1
  6. package/skills/c64-development/SKILL.md +2 -2
  7. package/skills/c64-disk/SKILL.md +11 -32
  8. package/skills/c64-docs/SKILL.md +27 -45
  9. package/skills/c64-docs/references/page-types.md +20 -18
  10. package/skills/c64-docs/references/style.md +10 -4
  11. package/skills/c64-docs/scripts/docs.ts +13 -2
  12. package/skills/c64-docs/scripts/lib/reference.ts +47 -5
  13. package/skills/c64-knowledge/SKILL.md +138 -267
  14. package/skills/c64-knowledge/references/batches.md +48 -0
  15. package/skills/c64-knowledge/references/coverage.md +29 -0
  16. package/skills/c64-knowledge/references/source-knowledge.md +72 -0
  17. package/skills/c64-knowledge/references/start-screen.md +44 -0
  18. package/skills/c64-knowledge/scripts/knowledge.ts +266 -61
  19. package/skills/c64-knowledge/scripts/lib/check.ts +133 -0
  20. package/skills/c64-knowledge/scripts/lib/listing.ts +71 -8
  21. package/skills/c64-memory-map/SKILL.md +11 -32
  22. package/skills/c64-packer/SKILL.md +11 -31
  23. package/skills/c64-provenance/SKILL.md +43 -64
  24. package/skills/c64-provenance/scripts/lib/leftovers.ts +8 -3
  25. package/skills/c64-provenance/scripts/provenance.ts +65 -56
  26. package/skills/c64-reverse-engineering/SKILL.md +20 -47
  27. package/skills/c64-reverse-engineering/references/comment-check.md +42 -0
  28. package/skills/c64-reverse-engineering/references/versions.md +47 -0
  29. package/skills/c64-source/SKILL.md +48 -237
  30. package/skills/c64-source/references/export.md +50 -0
  31. package/skills/c64-source/references/loader.md +78 -0
  32. package/skills/c64-source/references/source-format.md +46 -0
  33. package/skills/c64-source/scripts/lib/export.ts +28 -13
  34. package/skills/c64-source/scripts/lib/loader.ts +4 -82
  35. package/skills/c64-source/scripts/lib/source.ts +3 -5
  36. package/skills/c64-source/scripts/source.ts +48 -51
  37. package/skills/c64-static-analysis/SKILL.md +52 -52
  38. package/skills/c64-static-analysis/scripts/lib/coverage.ts +55 -2
  39. package/skills/c64-static-analysis/scripts/lib/idioms.ts +43 -17
  40. package/skills/c64-static-analysis/scripts/static-analysis.ts +79 -15
  41. package/skills/c64-testing/SKILL.md +72 -58
  42. package/skills/c64-testing/references/recording-format.md +5 -80
  43. package/skills/c64-testing/references/scenario-format.md +4 -12
  44. package/skills/c64-testing/scripts/lib/play.ts +16 -8
  45. package/skills/c64-testing/scripts/lib/recording.ts +74 -9
  46. package/skills/c64-testing/scripts/lib/replay.ts +4 -0
  47. package/skills/c64-testing/scripts/lib/runs.ts +112 -1
  48. package/skills/c64-testing/scripts/lib/scenario.ts +12 -6
  49. package/skills/c64-testing/scripts/lib/start-screen.ts +27 -0
  50. package/skills/c64-testing/scripts/testing.ts +34 -13
  51. package/skills/c64-unpacker/SKILL.md +10 -30
  52. package/skills/emulator/SKILL.md +7 -36
  53. package/src/build-name.ts +12 -1
  54. package/src/c64.ts +16 -0
  55. package/src/knowledge/history.ts +8 -0
  56. package/src/knowledge/program.ts +84 -4
  57. package/src/knowledge/read.ts +46 -2
  58. package/src/knowledge/recording-files.ts +32 -0
  59. package/src/knowledge/schema.ts +3 -1
  60. package/src/knowledge/separate.ts +165 -11
  61. package/src/knowledge/start-screen.ts +99 -17
  62. package/src/knowledge/text.ts +2 -1
  63. package/src/knowledge/write.ts +15 -8
  64. package/src/loader.ts +167 -0
  65. package/src/native/decompiler/analyze.ts +7 -6
  66. package/src/native/decompiler/walk.ts +32 -2
  67. package/src/native/dxa.ts +4 -4
  68. package/src/native/types.ts +34 -4
  69. package/src/script.ts +102 -25
  70. package/skills/c64-export/SKILL.md +0 -63
@@ -1,6 +1,6 @@
1
1
  ---
2
2
  name: c64-provenance
3
- description: Compares two or more releases of a C64 program byte by byte and tells which releases agree at each difference. It can run each release and tell how the program uses each difference. It gives evidence for each range of the program in the project knowledge. It finds the leftovers in the program, for example texts, assembler source, BASIC lines, unused bytes, copies and dead code. It keeps a library of routines that several programs share, and finds these routines in a program. Use it to separate the program from the changes of a crack or of a release. The bytes that the releases share do not prove the bytes of the commercial original. Use it to find the history of a program in its leftovers. Use it to find known routines, for example a music driver, in a program. It does not disassemble or analyze the code, and it does not change the project knowledge.
3
+ description: Compares two or more releases of a C64 program byte by byte and tells which releases agree at each difference. It can run each release and tell how the program uses each difference. It gives evidence for each range of the program in the project knowledge. It finds the leftovers in the program, for example texts, assembler source, BASIC lines, unused bytes, copies and dead code. It keeps a library of routines that several programs share, and finds these routines in a program. Use it to separate the program from the changes of a crack or of a release. The bytes that the releases share do not prove the bytes of the commercial original. Use it to find the history of a program in its leftovers. Use it to find known routines, for example a music driver, in a program. It does not disassemble or analyze the code. It changes the project knowledge only when it writes the names of the found routines.
4
4
  ---
5
5
 
6
6
  # C64 provenance
@@ -28,9 +28,11 @@ To get the command list, run the script with no arguments.
28
28
  | Compare only the bytes in a range | `compare releases/a.prg releases/b.prg --range '$0900-$09ff'` |
29
29
  | Map two releases with moved code in blocks of equal bytes | `compare releases/a/image.json releases/b/image.json --align --range '$0400-$bfff'` |
30
30
  | Find how each release uses each difference | `compare knowledge .c64-re-tools/local/b/image.json --frames 600 --steps .c64-re-tools/local/start-game.json` |
31
- | Get the evidence for each range of the program in the project knowledge | `separate --out .c64-re-tools/local/separate.json` |
32
- | Find the leftovers in the program | `leftovers --out .c64-re-tools/local/leftovers.json` |
31
+ | Get the evidence for each range of the program in the project knowledge | `separate` |
32
+ | Find the leftovers in the program | `leftovers` |
33
+ | Get all items of one list of leftovers | `leftovers --max-listed 1000 --fields deadCode` |
33
34
  | Find short texts, for example a group name | `leftovers --min-text 6` |
35
+ | Keep all leftovers in a file for a later step | `leftovers --out .c64-re-tools/local/leftovers.json` |
34
36
  | Save a module of the program as a routine of the library | `routine '$1000' --library ../library` |
35
37
  | Save a range of the program as a routine with a name | `routine '$1000-$1026' --name play --library ../library` |
36
38
  | Find the routines of the library in the program | `match --library ../library` |
@@ -43,7 +45,7 @@ To get the command list, run the script with no arguments.
43
45
  - `compare` takes two or more releases. A release is one of these:
44
46
  - `knowledge`: the program in the project knowledge.
45
47
  - A PRG file. The script puts it at its load address.
46
- - The `image.json` of a capture of the unpacked program. Put its `entry_state.json` beside it. Without that file, `--frames` cannot start the release.
48
+ - The `image.json` of a capture folder. Keep all files of the capture folder together. Without them, `--frames` cannot start the release.
47
49
  - Get a PRG out of a disk image before you compare it. The script refuses a disk image, a tape image and a cartridge image.
48
50
  - Unpack a packed release before you compare it.
49
51
  - The first release on the command line is release 1. `--shift` uses these numbers.
@@ -64,12 +66,11 @@ To get the command list, run the script with no arguments.
64
66
  - A range is `<start>-<end>`, for example `'$c000-$cfff'`. In a JSON file, a range is `start` and `end`.
65
67
  - Bytes in a JSON file are hex in a field `data`, for example `"data": "a900"`.
66
68
  - 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.
67
- - 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.
69
+ - The script refuses an `--out` path in `.c64-re-tools/knowledge/`. Change the project knowledge only with the commands that change it.
68
70
  - The script refuses a folder where the command writes a file. It refuses a file where the command writes a folder.
69
71
  - When a test or another file is the evidence for a finding in the project knowledge, move it out of `.c64-re-tools/local/` first. The project knowledge refuses that evidence in that folder.
70
- - A coverage map can stay in `.c64-re-tools/local/`. The project knowledge keeps its ranges and its SHA-256.
71
- - 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.
72
- - 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.
72
+ - A coverage map can stay in `.c64-re-tools/local/`. Do not move a coverage map after you put it into the project knowledge.
73
+ - To keep a run as evidence, use the command that keeps a run. It copies the run, its results and its snapshot files into `runs/<name>/`, and it changes the paths in the copy. Then git keeps them, and another person can do the run again.
73
74
  - 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`.
74
75
 
75
76
  ---
@@ -86,7 +87,7 @@ Each flag applies only to the commands in the table. On another command, the scr
86
87
  | `--min-block <n>` | `compare` | With `--align`: the minimum number of bytes in a block, from 8. The default is 16. |
87
88
  | `--frames <n>` | `compare` | Run each release for `n` frames, and give each listed difference its runtime evidence. The default is no run. |
88
89
  | `--steps <file>` | `compare` | With `--frames`: run the action steps of this file before the frames. The default is no steps. |
89
- | `--out <file>` | `separate`, `leftovers` | Write all items to this file. The default is no file. |
90
+ | `--out <file>` | `separate`, `leftovers` | Write the fields of the body to this file, with all items of each list. The default is no file. |
90
91
  | `--apply` | `match` | Write the changes to the project knowledge as one revision of the LLM. Without it, `match` writes nothing. |
91
92
  | `--max-listed <n>` | `separate`, `leftovers`, `match` | List at most `n` items of each list. The default is 50. `count` always gives the full number. |
92
93
  | `--min-text <n>` | `leftovers` | The minimum number of characters in a text, from 4. The default is 8. A short text, for example a group name, can need 6. |
@@ -123,7 +124,7 @@ With `--frames`, the body also holds:
123
124
  - `changed`: `true` when the bytes at the end of the run are not the bytes of the release.
124
125
  - `run`: `frames`, `steps` and `releases`. `releases` tells for each release if it started, or the `reason` that it did not start. `skipped` tells that the releases have no differences, so no release ran.
125
126
 
126
- A PRG starts with RUN after its load. An image starts at its entry, with the state of `entry_state.json`.
127
+ A PRG starts with RUN after its load. An image starts at its entry, with the entry state of its capture.
127
128
 
128
129
  With `--align`, the body holds a map in place of `differences` and the shift suggestions:
129
130
 
@@ -143,7 +144,7 @@ With `--align`, the body holds a map in place of `differences` and the shift sug
143
144
  Traps:
144
145
 
145
146
  - `common` is empty: the releases do not have bytes at the same addresses. Give `--shift`, or a `--range` that all releases have.
146
- - A release has `started: false`: the PRG has no BASIC start, or the image has no `entry_state.json`. The `reason` tells which. The other releases still give evidence.
147
+ - A release has `started: false`: the PRG has no BASIC start, or the capture folder of the image has no entry state. The `reason` tells which. The other releases still give evidence.
147
148
  - Each release writes a range before it reads it: the range is probably runtime state, not a difference of the releases.
148
149
  - A range has `first: none` in each release: the run did not use it. Run more frames or other steps before you decide.
149
150
  - The script refuses `--align` with `--shift` or `--frames`. It refuses `--min-block` without `--align`, and `--steps` without `--frames`.
@@ -168,16 +169,17 @@ Gives evidence from the program in the project knowledge, its stages and its cov
168
169
  - `access` is `executed`, `read`, `written`, `read-written` or `untouched`. `untouched` bytes did not take part in the runs.
169
170
  - A `read` byte right after executed code, or at a return address, can be a dummy read of the 6502. The program does not use it.
170
171
  - `roots`: the addresses where the reachability starts, each with `address` and `from`. The roots are the entry, and the NMI, RESET and IRQ vectors at `$fffa`-`$ffff` when the program holds them. A text in place of the list tells why no code region is reachable.
171
- - `codeRegions.total`: the number of code regions in the project knowledge.
172
- - `codeRegions.unreachable`: the code regions that no chain of call and jump references reaches from a root. The CPU also continues into the next code region when the last instruction of a region is not RTS, RTI, JMP, BRK or JAM.
173
- - `outside`: only with coverage. `read` and `executed` give the RAM outside the program that the program used but did not write. A loader or an earlier stage put these bytes there. `generated` gives the code that the program wrote outside its segments. A `generated` range can have gaps of two bytes or less: these are operand bytes.
174
- - `out`, `bytes` and `sha256`: the file of `--out`. It holds the roots, and all ranges, code regions and outside ranges.
172
+ - `codeRegions`: the code regions in the project knowledge, as a list. Each region has `reachable`, and `executed` with coverage. `unknown` is `true` for a region that is not dead code, as in `unknownCode` of `leftovers`.
173
+ - `codeRegions.unreachable`: the code regions that no chain of call and jump references reaches from a root.
174
+ - `unresolved`: only when a code region is unknown. These are the flows whose targets the project knowledge does not give, as in `leftovers`.
175
+ - `outside`: only with coverage. `read` and `executed` give the RAM outside the program that the program used but did not write. A loader or an earlier stage put these bytes there. `generated` gives the code that the program wrote outside its segments.
176
+ - `out`, `bytes` and `sha256`: the `--out` file. It has the fields of the body, with all items of each list.
175
177
 
176
- `hint`: a list is cut. Without `--out`, give a larger `--max-listed`, or give `--out`. With `--out`, read the file.
178
+ `hint`: a list is cut. Run the command again with a larger `--max-listed`, or read the `--out` file.
177
179
 
178
180
  Traps:
179
181
 
180
- - No code regions in the project knowledge: `codeRegions` is empty. Analyze the program for its code regions and its references first.
182
+ - No code regions in the project knowledge: `codeRegions.count` is 0. Analyze the program for its code regions and its references first.
181
183
  - Many `untouched` ranges: do more coverage runs for these parts. Put the coverage into the project knowledge before you decide.
182
184
  - Error `not-found`: the project knowledge has no program. Put the program into the project knowledge.
183
185
 
@@ -185,7 +187,7 @@ Traps:
185
187
 
186
188
  Finds what the program in the project knowledge holds in addition to its working code. It gives evidence, not a decision.
187
189
 
188
- `body` holds `entry`, `roots`, and `stages` or `noStages`, as `separate` gives them. `noCoverage` tells that the project knowledge has no coverage. Then `residue` is empty and no finding has `access`. The body also holds these lists:
190
+ `body` holds `entry`, `roots`, and `stages` or `noStages`, as `separate` gives them. `noCoverage` tells that the project knowledge has no coverage. Then `residue` is empty and no finding has `access`. The body also has one list for each kind of finding. Each list holds at most `--max-listed` findings, and its `count` gives all of them:
189
191
 
190
192
  | List | What it holds |
191
193
  |---|---|
@@ -195,12 +197,13 @@ Finds what the program in the project knowledge holds in addition to its working
195
197
  | `residue` | Bytes that no run touched, and that are not text, BASIC, fill or a code region. `bytes` shows the first 16 bytes. |
196
198
  | `copies` | At least 32 bytes that are the same as earlier bytes of the program. `copyOf` is the address of the earlier bytes. |
197
199
  | `deadCode` | Code regions that no root reaches, as in `codeRegions.unreachable` of `separate`, and that no run executed. |
200
+ | `unknownCode` | Code regions that no root reaches and that no run executed, but that are not dead code. No coverage shows that they did not run, or a flow in `unresolved` can go to them. Each flow has `at` and `kind`: `indirect-jump`, `written-operand` or `pushed-return`. |
198
201
 
199
202
  Each finding has `stages`: the unpack stages that wrote its bytes. A stage before the last stage is a depacker, an intro or a loader. Each finding also has `access`: the accesses of its bytes in the coverage.
200
203
 
201
- `out`, `bytes` and `sha256` give the file of `--out`. It holds all findings with the full texts.
204
+ `out`, `bytes` and `sha256` give the `--out` file. It has the fields of the body, with all findings and the full texts.
202
205
 
203
- `hint`: a list is cut. Without `--out`, give a larger `--max-listed`, or give `--out`. With `--out`, read the file.
206
+ `hint`: a list is cut. Run the command again with a larger `--max-listed`, or read the `--out` file. To get only the lists that you need, give `--fields`, for example `--fields text,deadCode`.
204
207
 
205
208
  Text from another computer can show the tools of the developer:
206
209
 
@@ -219,6 +222,7 @@ Traps:
219
222
 
220
223
  - No coverage: `residue` is empty. Put the coverage of runs that use many parts of the program into the project knowledge.
221
224
  - No code regions: `deadCode` is empty, and `residue` can include code. Analyze the program for its code regions first.
225
+ - `unknownCode` has items: a flow in `unresolved` can go to the code, or the knowledge has no coverage. Do not call this code dead. Find the targets of each flow in the emulator, and record a jump reference from the flow to each target in the project knowledge. When the flow goes outside the program, record the reference to that address.
222
226
  - Error `not-found`: the project knowledge has no program. Put the program into the project knowledge.
223
227
 
224
228
  ---
@@ -227,7 +231,7 @@ Traps:
227
231
 
228
232
  ### `routine <module|range>`
229
233
 
230
- Saves a routine of the program in the project knowledge to the library. The file keeps the user and LLM names, comments, regions, references and operand expressions in the range. It also keeps the code regions of each origin.
234
+ Saves a routine of the program in the project knowledge to the library. The routine file keeps the user and LLM names, comments, regions, references and operand expressions in the range.
231
235
 
232
236
  - A module ends at the next module, or at the end of its segment.
233
237
  - A routine has at least 16 bytes. Its last instruction must end in the range.
@@ -266,19 +270,12 @@ Finds each routine of the library in the program of the project knowledge.
266
270
  - `library` and `routines`: the library folder and the number of routines in it.
267
271
  - `matches`: each item has `routine`, `start`, `end` and `shift`. `shift` is the address in the program minus the address in the library.
268
272
  - `differing` in each match: each operand that addresses something outside the routine and that is different. It gives the value in the `library` and in the `program`. Examine each one. For example, the program can use other variables or another SID voice.
269
- - `changes`: with matches. `writes` lists the writes for the project knowledge. `known` counts the writes that the project knowledge has already. `leftOut` lists the writes that the script left out, and why. `check` lists each comment of `writes` that gives an address outside its routine. Each item has `routine`, `address`, `comment` and the `outside` addresses. Such an address can be different in this program. With `--apply`, `revision` gives the revision of the changes.
270
-
271
- The changes have the names, regions, comments, references and operand expressions of each routine at its address in the program. The script leaves out a write in these conditions:
272
-
273
- - The program has a user or LLM row at that place.
274
- - The program uses the name at another address.
275
- - A removal holds at that place.
276
- - The routine occurs more than one time, and the script did not choose this copy.
277
- - The script left out the name that an operand expression uses, or the expression does not give the operand in the program.
278
-
279
- A routine can occur more than one time. Then the script finds the nearest routines before and after it in the library that occur only one time. The script writes the names at the copy that has the shift of one of these routines. If no copy or more than one copy has such a shift, the script writes no names for the routine. Then `changes.leftOut` gives each copy with the reason `ambiguous`.
280
-
281
- An address `$xxxx` in a comment moves with the routine when the address is in the routine. An address outside the routine does not move, and `changes.check` gives the comment.
273
+ - `changes`: with matches.
274
+ - `writes`: the names, regions, comments, references and operand expressions of each routine at its address in the program. An address in a comment moves with the routine when the address is in the routine.
275
+ - `known`: the count of the writes that the project knowledge has already.
276
+ - `leftOut`: the writes that the script left out, each with its `reason`. For example, the program has a user or LLM row at that place. Or the routine has two or more copies, and the script cannot choose one (`ambiguous`).
277
+ - `check`: each comment of `writes` that gives an address outside its routine. Each item has `routine`, `address`, `comment` and the `outside` addresses. Such an address can be different in this program.
278
+ - `revision`: with `--apply`, the revision of the changes.
282
279
 
283
280
  `hint` and `nextAction`:
284
281
 
@@ -288,7 +285,8 @@ An address `$xxxx` in a comment moves with the routine when the address is in th
288
285
 
289
286
  Traps:
290
287
 
291
- - `match` does not change the project knowledge.
288
+ - Without `--apply`, `match` does not change the project knowledge.
289
+ - A copy in `changes.leftOut` with the reason `ambiguous` gets no names. Examine each copy. Record the names of the copy that you prove in the project knowledge.
292
290
  - Error `not-found`: the library folder does not exist, or it has no routine files. Save a routine with `routine` first.
293
291
  - Error `invalid-input` with "is a project folder": the library folder is a project. Give a folder beside the projects.
294
292
 
@@ -307,13 +305,13 @@ Traps:
307
305
  - Do not call releases independent when you do not know their relation. Two cracks can come from the same source.
308
306
  - Do not call a byte a byte of the commercial original only because one release has it. A majority of releases is also not proof.
309
307
  7. Record each release in the project knowledge. The base release is the program in the project knowledge.
310
- 8. Record each difference in the project knowledge as a switch and a patch. Select the kind of the switch:
308
+ 8. Record a switch in the project knowledge for each difference. Give the switch the labels of the releases that have it on. Select the kind of the switch:
311
309
  - `protection`: a copy protection, or the code that a crack changed to remove it.
312
310
  - `trainer` or `cheat`: a trainer or a cheat.
313
311
  - `crack`: another change of a cracker in the program, for example a credit text.
314
312
  - `fix`: a correction of a bug. Make one switch for each correction, not one switch for each release.
315
313
  - `change`: another difference.
316
- 9. Give the switch the labels of the releases that have it on. The patch holds the bytes that the base release does not have.
314
+ 9. For each range of a switch, record the patch from the bytes of a release that has the switch on. The patch holds the bytes that the base release does not have.
317
315
  10. Record each other conclusion in the project knowledge as a comment or a region. Do not record the full comparison.
318
316
 
319
317
  ### Find the leftovers
@@ -322,7 +320,7 @@ Traps:
322
320
  2. Put the coverage of runs that use many parts of the program into the project knowledge.
323
321
  3. Analyze the program for its code regions.
324
322
  4. Run `leftovers`. When you have two or more releases, also run `compare`.
325
- 5. Read `text` first: names, dates, greetings and credits. Then read `copies`, `residue`, `basic` and `deadCode`.
323
+ 5. Read `text` first: names, dates, greetings and credits. Then read `copies`, `residue`, `basic`, `fill` and `deadCode`.
326
324
  6. Examine each important finding. Disassemble or analyze its bytes, or examine them in the emulator.
327
325
  - Old code or old source text is evidence of the development. It does not prove the computer of the authors, the order of their work, or the path of a copy through the crackers.
328
326
  7. Record each finding that you proved in the project knowledge. Do not record the full list of leftovers.
@@ -344,24 +342,15 @@ Traps:
344
342
 
345
343
  Each command prints one JSON envelope on one line on stdout. Notes go to stderr.
346
344
 
347
- On success, the exit code is 0:
348
-
349
- ```json
350
- {"ok": true, "command": "<command>", "body": {}, "hint": "...", "nextAction": "..."}
351
- ```
352
-
353
- On failure, the exit code is 2:
354
-
355
- ```json
356
- {"ok": false, "command": "<command>", "code": "<code>", "error": "...", "hint": "...", "nextAction": "..."}
357
- ```
358
-
345
+ - On success, the exit code is 0: `{"ok": true, "command": "<command>", "body": {}, "hint": "...", "nextAction": "..."}`
346
+ - On failure, the exit code is 2: `{"ok": false, "command": "<command>", "code": "<code>", "error": "...", "hint": "...", "nextAction": "..."}`
359
347
  - `body` holds the result. The section of each command gives the fields of its body.
360
348
  - 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.
361
- - `error` is one sentence. It tells what is wrong.
362
- - `hint` tells what to do or what to examine. Read it before your next step. Some envelopes have no `hint`.
363
- - `nextAction` is a full command line. It runs from any directory. Run it when it agrees with your task. Some envelopes have no `nextAction`.
364
- - `code` does not change between versions. Do the action for the code:
349
+ - `error` is one sentence. It tells what is wrong. `hint` tells what to do or what to examine. Read it before your next step.
350
+ - `nextAction` is a full command line. It runs from any directory. Run it when it agrees with your task.
351
+ - A list has `count`, `listed` and `rest`. `listed` holds at most `--max-listed` items. `rest` tells how to get the other items. A file that the command wrote has `out`, `bytes` and `sha256`.
352
+ - To get only some fields of `body`, give `--fields <path>,<path>`, for example `--fields count,listed.start`. A path through a list gives that field of each item. Do not write code to filter an envelope.
353
+ - To get the command list, run the script with no arguments. To get the subjects and the flags of one command, run `<command> --help`.
365
354
 
366
355
  | `code` | Action |
367
356
  |---|---|
@@ -369,13 +358,3 @@ On failure, the exit code is 2:
369
358
  | `invalid-input`, `not-found` | Correct the call as `hint` tells. Then run the command again. |
370
359
  | `operation-failed` | Give `error` and `hint` to the user. |
371
360
  | Another code | Do as `hint` tells. |
372
-
373
- These fields have the same meaning in each body:
374
-
375
- | Field | Meaning |
376
- |---|---|
377
- | `out`, `bytes`, `sha256` | A file that the command wrote: its path, its size and its SHA-256. |
378
- | `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. |
379
-
380
- 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.
381
- To get the subjects and the flags of one command, run `<command> --help`. Then `body.subjects` and `body.options` give them.
@@ -76,6 +76,8 @@ export interface Leftovers {
76
76
  residue: ResidueFinding[];
77
77
  copies: CopyFinding[];
78
78
  deadCode: DeadCodeFinding[];
79
+ /** Code that nothing reaches and that no run executed, which is not dead: a flow that nobody resolved can go there, or no coverage shows that it did not run. */
80
+ unknownCode: DeadCodeFinding[];
79
81
  }
80
82
 
81
83
  const MIN_TEXT = 8;
@@ -435,8 +437,11 @@ export function findLeftovers(input: LeftoversInput): Leftovers {
435
437
  const explained = [...text, ...basic, ...fill, ...separation.codeRegions];
436
438
  const residue = untouched.flatMap((range) => without(range, explained, MIN_RESIDUE));
437
439
  // The references miss IRQ vectors and jumps through tables: code that ran is not dead.
438
- const deadCode: DeadCodeFinding[] = separation.codeRegions
440
+ const unreached = separation.codeRegions
439
441
  .filter((region) => region.reachable === false && region.executed !== true)
440
- .map((region) => ({ start: region.start, end: region.end, type: region.type, ...(region.executed === false ? { executed: false as const } : {}) }));
441
- return { text: placed(text), basic: placed(basic), fill: placed(fill), residue: placed(residue), copies: placed(findCopies(memory, segments)), deadCode: placed(deadCode) };
442
+ .map((region) => ({ start: region.start, end: region.end, type: region.type, ...(region.executed === false ? { executed: false as const } : {}), unknown: region.unknown === true }));
443
+ const finding = ({ unknown: _, ...region }: (typeof unreached)[number]): DeadCodeFinding => region;
444
+ const deadCode = unreached.filter((region) => !region.unknown).map(finding);
445
+ const unknownCode = unreached.filter((region) => region.unknown).map(finding);
446
+ return { text: placed(text), basic: placed(basic), fill: placed(fill), residue: placed(residue), copies: placed(findCopies(memory, segments)), deadCode: placed(deadCode), unknownCode: placed(unknownCode) };
442
447
  }
@@ -1,12 +1,12 @@
1
1
  // The c64-provenance script: compares two or more releases byte by byte at
2
2
  // their addresses and reports where they differ and which releases agree. A
3
- // release is a PRG or an unpacker image.json. With --run, each release
3
+ // release is a PRG or an unpacker image.json. With --frames, each release
4
4
  // runs in its own emulator, and each listed difference gets its first access
5
5
  // and whether the run changed it. The separate command gives evidence from
6
6
  // the program in the knowledge, and the leftovers command lists what its
7
7
  // unused bytes hold: text, BASIC lines, fills and copies. The routine and match commands keep
8
8
  // routines that several programs share in a library folder and find them in the
9
- // program (D71).
9
+ // program.
10
10
 
11
11
  import { basename } from "node:path";
12
12
 
@@ -293,42 +293,44 @@ const NO_ROOTS = "none: the program in the knowledge has no entry, and the image
293
293
 
294
294
  const NO_STAGES = "The program in the knowledge has no stage log. Only a capture of the unpack stages gives one. A capture of the loads of a loader holds one part of a program and has none.";
295
295
 
296
- /** separate: evidence from the program in the knowledge, with all ranges in --out and the first ones in the result. */
296
+ /** separate: evidence from the program in the knowledge. --out gets the fields of the body with all items of each list. */
297
297
  function separateCommand(call: Call): unknown {
298
298
  const out = call.string("out");
299
- const max = call.count("max-listed")!;
300
299
  const { program, stages, coverage, found } = programEvidence();
301
300
  const loaded = { entry: program.entry, stages };
302
301
  const show = (range: AddressRange) => ({ start: formatC64Address(range.start), end: formatC64Address(range.end) });
303
302
  const ranges = found.ranges.map((range) => ({ ...show(range), ...(range.stage === undefined ? {} : { stage: range.stage }), ...(range.access === undefined ? {} : { access: range.access }) }));
304
- const codeRegions = found.codeRegions.map((region) => ({ ...show(region), type: region.type, ...(region.reachable === undefined ? {} : { reachable: region.reachable }), ...(region.executed === undefined ? {} : { executed: region.executed }) }));
303
+ const codeRegions = found.codeRegions.map((region) => ({ ...show(region), type: region.type, ...(region.reachable === undefined ? {} : { reachable: region.reachable }), ...(region.executed === undefined ? {} : { executed: region.executed }), ...(region.unknown === undefined ? {} : { unknown: true }) }));
304
+ const unresolved = found.unresolved.map((flow) => ({ at: formatC64Address(flow.at), kind: flow.kind }));
305
305
  const outside = { read: found.outside.read.map(show), executed: found.outside.executed.map(show), generated: found.outside.generated.map(show) };
306
306
  const roots = showRoots(found.roots);
307
- const written = out === undefined ? undefined : writeReported(out, `${JSON.stringify({ roots, ranges, codeRegions, outside }, null, 1)}\n`, { what: "--out" });
308
- const list = <T>(items: T[]) => listed(items, max, undefined, out ?? "a larger --max-listed, or --out <file>");
309
307
  const bytes = (filter: (range: (typeof found.ranges)[number]) => boolean) => found.ranges.filter(filter).reduce((sum, range) => sum + range.end - range.start + 1, 0);
310
308
  const lastStage = loaded.stages === undefined ? undefined : loaded.stages.length - 1;
311
- const body = {
312
- ...(loaded.entry === undefined ? {} : { entry: formatC64Address(loaded.entry) }),
313
- ...(lastStage === undefined
314
- ? { noStages: NO_STAGES }
315
- : {
316
- stages: lastStage + 1,
317
- bytesByStage: Object.fromEntries([...[...Array(lastStage + 1).keys()].map((stage): [string, number] => [String(stage), bytes((range) => range.stage === stage)]), ["none", bytes((range) => range.stage === undefined)]]),
318
- }),
319
- ...(coverage === undefined ? {} : { bytesByAccess: Object.fromEntries((["executed", "read", "written", "read-written", "untouched"] as const).map((access) => [access, bytes((range) => range.access === access)])) }),
320
- ranges: list(ranges),
321
- // The roots of the reachability: the entry and the hardware vectors in the image.
322
- roots: roots.length === 0 ? NO_ROOTS : roots,
323
- // total counts every code region; unreachable is a list like the others.
324
- codeRegions: {
325
- total: codeRegions.length,
326
- ...(roots.length === 0 ? {} : { unreachable: list(codeRegions.filter((region) => region.reachable === false)) }),
327
- },
328
- ...(coverage === undefined ? {} : { outside: { read: list(outside.read), executed: list(outside.executed), generated: list(outside.generated) } }),
329
- ...(written === undefined ? {} : written),
309
+ /** The body with at most `most` items in each list; `rest` tells where the other items are. */
310
+ const bodyOf = (most: number, rest: string) => {
311
+ const list = <T>(items: T[]) => listed(items, most, undefined, rest);
312
+ return {
313
+ ...(loaded.entry === undefined ? {} : { entry: formatC64Address(loaded.entry) }),
314
+ ...(lastStage === undefined
315
+ ? { noStages: NO_STAGES }
316
+ : {
317
+ stages: lastStage + 1,
318
+ bytesByStage: Object.fromEntries([...[...Array(lastStage + 1).keys()].map((stage): [string, number] => [String(stage), bytes((range) => range.stage === stage)]), ["none", bytes((range) => range.stage === undefined)]]),
319
+ }),
320
+ ...(coverage === undefined ? {} : { bytesByAccess: Object.fromEntries((["executed", "read", "written", "read-written", "untouched"] as const).map((access) => [access, bytes((range) => range.access === access)])) }),
321
+ ranges: list(ranges),
322
+ // The roots of the reachability: the entry and the hardware vectors in the image.
323
+ roots: roots.length === 0 ? NO_ROOTS : roots,
324
+ // Each code region with its evidence, and the ones that no root reaches.
325
+ codeRegions: {
326
+ ...list(codeRegions),
327
+ ...(roots.length === 0 ? {} : { unreachable: list(codeRegions.filter((region) => region.reachable === false)) }),
328
+ },
329
+ ...(unresolved.length === 0 ? {} : { unresolved: list(unresolved) }),
330
+ ...(coverage === undefined ? {} : { outside: { read: list(outside.read), executed: list(outside.executed), generated: list(outside.generated) } }),
331
+ };
330
332
  };
331
- return withFile(body, out);
333
+ return withFile(bodyOf, call.count("max-listed")!, out);
332
334
  }
333
335
 
334
336
  /** True when a list of the body ({count, listed}) holds fewer items than its count. */
@@ -339,25 +341,27 @@ function hasCutList(value: unknown, depth = 0): boolean {
339
341
  return Object.values(value).some((item) => hasCutList(item, depth + 1));
340
342
  }
341
343
 
342
- /** The note for a cut list: with --out, the file holds all items; without it, a larger --max-listed or --out gives them. */
343
- function cutNote(body: unknown, out: string | undefined): string | undefined {
344
- if (!hasCutList(body)) return undefined;
345
- return out === undefined ? "A list in the body is cut: count gives its full size. To get the rest, give a larger --max-listed." : `A list in the body is cut: count gives its full size. ${out} holds all items.`;
344
+ /** The note for a cut list of match: a larger --max-listed gives the rest. */
345
+ function cutNote(body: unknown): string | undefined {
346
+ return hasCutList(body) ? "A list in the body is cut: count gives its full size. To get the rest, give a larger --max-listed." : undefined;
346
347
  }
347
348
 
348
- /** A body with the note for a cut list when --out holds all items. Without --out, the envelope gives the note itself. */
349
- function withFile(body: unknown, out: string | undefined): unknown {
350
- const note = out === undefined ? undefined : cutNote(body, out);
351
- return note === undefined ? body : reply(body, { hint: note });
349
+ /**
350
+ * The body with at most `max` items in each list, and with --out the file of
351
+ * the same fields with all items. A cut list names a larger --max-listed, and
352
+ * the file of --out or --out itself: the envelope gives that note.
353
+ */
354
+ function withFile(bodyOf: (most: number, rest: string) => Record<string, unknown>, max: number, out: string | undefined): Record<string, unknown> {
355
+ const written = out === undefined ? undefined : writeReported(out, `${JSON.stringify(bodyOf(Number.POSITIVE_INFINITY, ""), null, 1)}\n`, { what: "--out" });
356
+ return { ...bodyOf(max, out === undefined ? "a larger --max-listed, or --out <file>" : `a larger --max-listed, or read ${out}`), ...(written === undefined ? {} : written) };
352
357
  }
353
358
 
354
359
  /** The longest text that the result shows; --out has all of it. */
355
360
  const SHOWN_TEXT = 120;
356
361
 
357
- /** leftovers: what the program in the knowledge holds besides its working code, with all findings in --out and the first ones in the result. */
362
+ /** leftovers: what the program in the knowledge holds besides its working code. --out gets the fields of the body with all items and the full texts. */
358
363
  function leftoversCommand(call: Call): unknown {
359
364
  const out = call.string("out");
360
- const max = call.count("max-listed")!;
361
365
  const min = call.count("min-text")!;
362
366
  const { program, stages, coverage, found } = programEvidence();
363
367
  const leftovers = findLeftovers({
@@ -381,28 +385,33 @@ function leftoversCommand(call: Call): unknown {
381
385
  residue: leftovers.residue.map((range) => ({ ...show(range), bytes: hexBytes([...program.memory.subarray(range.start, Math.min(range.end, range.start + 15) + 1)]) + (range.end - range.start >= 16 ? " ..." : "") })),
382
386
  copies: leftovers.copies.map(({ copyOf, ...rest }) => ({ ...show(rest), copyOf: formatC64Address(copyOf) })),
383
387
  deadCode: leftovers.deadCode.map(show),
388
+ unknownCode: leftovers.unknownCode.map(show),
389
+ unresolved: found.unresolved.map((flow) => ({ at: formatC64Address(flow.at), kind: flow.kind })),
384
390
  };
385
- const written = out === undefined ? undefined : writeReported(out, `${JSON.stringify(all, null, 1)}\n`, { what: "--out" });
386
- const list = <T>(items: T[]) => listed(items, max, undefined, out ?? "a larger --max-listed, or --out <file>");
387
- const body = {
388
- ...(program.entry === undefined ? {} : { entry: formatC64Address(program.entry) }),
389
- roots: found.roots.length === 0 ? NO_ROOTS : showRoots(found.roots),
390
- ...(stages === undefined ? { noStages: NO_STAGES } : { stages: stages.length }),
391
- ...(coverage === undefined ? { noCoverage: "The knowledge has no coverage, so residue is empty and no finding has access. Put the coverage of runs into the project knowledge." } : {}),
392
- text: list(all.text.map((item) => (item.text.length > SHOWN_TEXT ? { ...item, text: `${item.text.slice(0, SHOWN_TEXT)} ...`, length: item.text.length } : item))),
393
- basic: list(all.basic),
394
- fill: list(all.fill),
395
- residue: list(all.residue),
396
- copies: list(all.copies),
397
- deadCode: list(all.deadCode),
398
- ...(written === undefined ? {} : written),
391
+ /** The body with at most `most` items in each list; the file has the full texts. */
392
+ const bodyOf = (most: number, rest: string) => {
393
+ const list = <T>(items: T[]) => listed(items, most, undefined, rest);
394
+ const shown = Number.isFinite(most) ? SHOWN_TEXT : Number.POSITIVE_INFINITY;
395
+ return {
396
+ ...(program.entry === undefined ? {} : { entry: formatC64Address(program.entry) }),
397
+ roots: found.roots.length === 0 ? NO_ROOTS : showRoots(found.roots),
398
+ ...(stages === undefined ? { noStages: NO_STAGES } : { stages: stages.length }),
399
+ ...(coverage === undefined ? { noCoverage: "The knowledge has no coverage, so residue is empty and no finding has access. Put the coverage of runs into the project knowledge." } : {}),
400
+ text: list(all.text.map((item) => (item.text.length > shown ? { ...item, text: `${item.text.slice(0, shown)} ...`, length: item.text.length } : item))),
401
+ basic: list(all.basic),
402
+ fill: list(all.fill),
403
+ residue: list(all.residue),
404
+ copies: list(all.copies),
405
+ deadCode: list(all.deadCode),
406
+ ...(all.unknownCode.length === 0 ? {} : { unknownCode: list(all.unknownCode), unresolved: list(all.unresolved) }),
407
+ };
399
408
  };
400
- return withFile(body, out);
409
+ return withFile(bodyOf, call.count("max-listed")!, out);
401
410
  }
402
411
 
403
412
  const platform = () => new Map(platformNames().map((entry) => [entry.name, entry.address]));
404
413
 
405
- /** routine: saves a module or a range of the program in the knowledge, with its knowledge, as a routine of the library (D71). */
414
+ /** routine: saves a module or a range of the program in the knowledge, with its knowledge, as a routine of the library. */
406
415
  function routineCommand(call: Call): unknown {
407
416
  const given = call.subject(0);
408
417
  const library = call.string("library")!;
@@ -497,7 +506,7 @@ function matchCommand(call: Call): unknown {
497
506
  .sort((a, b) => a.start.localeCompare(b.start));
498
507
  const result: Record<string, unknown> = { library, routines: routines.length, matches: list(matches) };
499
508
  const noMatch = "No routine of the library is in the program. A packed program can hold a routine in a packed form. Unpack the program, put the unpacked program into the project knowledge, and run match again.";
500
- const withNote = (shown: unknown, hint: string) => [cutNote(shown, undefined), hint].filter((part) => part !== undefined).join(" ");
509
+ const withNote = (shown: unknown, hint: string) => [cutNote(shown), hint].filter((part) => part !== undefined).join(" ");
501
510
  if (matches.length === 0) return reply(result, { hint: noMatch });
502
511
  const db = openForRead();
503
512
  let suggestions: ReturnType<typeof suggest>;
@@ -569,14 +578,14 @@ const COMMANDS: CommandSpec[] = [
569
578
  {
570
579
  name: "separate",
571
580
  summary: "Evidence from the program in the knowledge: the unpack stage that wrote each range, its access in the coverage, whether call and jump references, or the run on from the code before it, reach each code region from the entry, and what the program reads or executes outside its segments.",
572
- options: { out: { ...OPTIONS.out, output: "file", value: "<file>", help: "write all ranges to this file" }, "max-listed": OPTIONS["max-listed"] },
581
+ options: { out: { ...OPTIONS.out, output: "file", value: "<file>", help: "write the fields of the body, with all items of each list, to this file" }, "max-listed": OPTIONS["max-listed"] },
573
582
  run: separateCommand,
574
583
  },
575
584
  {
576
585
  name: "leftovers",
577
586
  summary: "What the program in the knowledge holds besides its working code: texts in PETSCII, screen codes, ASCII or with bit 7 set (Apple II), assembler source, BASIC lines away from the BASIC start, fills, untouched bytes outside code, copies of earlier bytes, and code regions that nothing reaches and no run executed.",
578
587
  options: {
579
- out: { ...OPTIONS.out, output: "file", value: "<file>", help: "write all findings to this file" },
588
+ out: { ...OPTIONS.out, output: "file", value: "<file>", help: "write the fields of the body, with all findings and the full texts, to this file" },
580
589
  "min-text": { kind: "count", value: "<n>", min: 4, max: 999, default: 8, help: "the shortest text to find (default 8)" },
581
590
  "max-listed": OPTIONS["max-listed"],
582
591
  },