@henols/c64-re-tools 2.3.0-rc.5 → 2.3.0

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 (61) 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/recording.ts +7 -7
  39. package/skills/c64-testing/scripts/lib/replay.ts +7 -7
  40. package/skills/c64-testing/scripts/lib/runs.ts +6 -7
  41. package/skills/c64-testing/scripts/lib/scenario.ts +12 -6
  42. package/skills/c64-testing/scripts/testing.ts +35 -15
  43. package/skills/c64-unpacker/SKILL.md +273 -127
  44. package/skills/c64-unpacker/scripts/lib/loads.ts +3 -3
  45. package/skills/c64-unpacker/scripts/unpacker.ts +70 -11
  46. package/skills/emulator/SKILL.md +12 -12
  47. package/src/errors.ts +22 -2
  48. package/src/host/vice/session.ts +1 -1
  49. package/src/host-client/entry-state.ts +1 -1
  50. package/src/host-client/image.ts +3 -3
  51. package/src/host-client/steps.ts +1 -1
  52. package/src/knowledge/expression.ts +1 -1
  53. package/src/knowledge/names.ts +2 -3
  54. package/src/knowledge/program.ts +2 -3
  55. package/src/knowledge/write.ts +2 -2
  56. package/src/mcp/inputs.ts +1 -1
  57. package/src/mcp/tools/input.ts +1 -1
  58. package/src/mcp/tools/media.ts +1 -1
  59. package/src/native/exomizer.ts +2 -2
  60. package/src/project.ts +2 -2
  61. package/src/script.ts +164 -75
@@ -1,255 +1,387 @@
1
1
  ---
2
2
  name: c64-knowledge
3
- description: Use this skill to read, write, correct and review the durable knowledge of a C64 project in .c64-re-tools/knowledge/. The knowledge holds the program, its entry state and the coverage of the runs. It gives the knowledge at an address, and it names symbols, classifies memory regions, sets comments and records references. It records releases, switches, patches and macros for the generated source. It also shows the history of each change, and it can revert to an earlier revision. Do not use it for live emulator state or for static analysis.
3
+ description: Reads and changes the durable knowledge of a C64 project in .c64-re-tools/knowledge/. The knowledge holds the program and the coverage of the runs. It holds the names, regions, comments and references, and the releases, switches, patches and macros of the source. It keeps the history of each change. Use it to find the knowledge at an address or a name. Use it to record a name, a region, a comment or a reference after you interpret the evidence. Use it to put a program or the coverage of runs into the project. Use it to record the releases, switches, patches and macros of the generated source. Use it to read the history, to revert to an earlier revision, or to decide the conflicts of a git merge. It does not run the program, analyze the machine code or generate the source.
4
4
  ---
5
5
 
6
- # c64-knowledge
6
+ # C64 knowledge
7
7
 
8
- ## Purpose
8
+ Use this script to read and change the knowledge of a C64 project.
9
+ Use only the commands of the script. Do not read or change the project knowledge in another way.
9
10
 
10
- Read, write, correct and review the durable understanding of a C64 project. The knowledge also holds the program: its bytes, its entry state and the stages of its unpacker. It holds the coverage of the runs too. The other skills read the program and the coverage from the knowledge. The knowledge is text in `.c64-re-tools/knowledge/` in the project folder: one JSON line for each row. Commit that folder with git. The index of the text is `.c64-re-tools/local/knowledge.db`. Do not commit the index.
11
-
12
- This skill is the only write path for conclusions of the user and the LLM. It does not run an emulator or an analyzer. Write the knowledge only with the script. The script checks each write. Do not write or edit the files in `.c64-re-tools/knowledge/` or the index yourself.
13
-
14
- ## Inputs
15
-
16
- One of these:
11
+ ```bash
12
+ node <skill>/scripts/knowledge.ts <command>
13
+ ```
17
14
 
18
- - an address, a range or a symbol name to look up
19
- - text to search for
20
- - a change to a symbol, a region, a comment or a reference, with a short reason
21
- - a capture of the c64-unpacker skill, a PRG, or coverage maps to put into the knowledge
22
- - a request to see the history or a revision.
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 `at '$2100'`, 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.
23
19
 
24
- ## Tools
20
+ The knowledge is text in `.c64-re-tools/knowledge/` of the project. Commit that folder with git. Do not edit its files yourself. The script checks each change.
25
21
 
26
- ```
27
- node <skill>/scripts/knowledge.ts <command> [subjects] [options]
28
- ```
22
+ ---
29
23
 
30
- A symbol is the name of an address from the user or the LLM. An analyzer name is a second name from DXA, Ghidra or an idiom, for example `FUN_17e2`. Each analyzer keeps its own name at an address. `at`, `search` and `export` show the analyzer names. The generated source uses only the symbols, unless c64-source gets `--analyzer-names`. To use an analyzer name in the source, `rename` the address with it. A symbol name is a label for ACME: a letter or underscore, then letters, digits or underscores. The knowledge refuses the name of a 6502 instruction, for example `lda`, in upper or lower case.
24
+ ## Select the command
31
25
 
32
- | Need | Command |
26
+ | Goal | Command |
33
27
  |---|---|
34
- | Everything known at one address | `at '$2100'` |
28
+ | Find all knowledge at one address | `at '$2100'` |
35
29
  | Find names and comment text | `search player` |
36
- | Lists in a range | `symbols`, `regions`, `comments`, `references` with `--range '$2000-$2fff'` |
37
- | The names that DXA, Ghidra and the idioms gave | `symbols --analyzer` |
38
- | Only some origins | add `--origin semantic`, `--origin analyzer` (dxa, ghidra and heuristic), or `--origin user,llm` |
39
- | All knowledge in one file | `export --out .c64-re-tools/local/knowledge.json` |
40
- | Put the program into the knowledge | `image .c64-re-tools/local/capture/image.json` or `image original/game.prg` |
41
- | Add the coverage of runs | `coverage .c64-re-tools/local/runs/rooms/room0.bin ...` |
42
- | What the knowledge holds of the program | `program` |
43
- | The code of a range with names, comments, access and references | `listing '$0400-$04ff'` |
44
- | Name the symbol at an address | `rename '$2100' update_player --kind routine` |
45
- | The rows that one revision added and closed, also memory, entry and coverage rows | `revision 20261015T2145` |
30
+ | List the symbols in a range | `symbols --range '$2000-$2fff'` |
31
+ | List the names that the analyzers gave | `symbols --analyzer` |
32
+ | List only the facts of the user and the LLM | `symbols --origin semantic` |
33
+ | List the regions, comments or references in a range | `regions --range '$2000-$2fff'` |
34
+ | Show the code of a range with names, comments, access and references | `listing '$0400-$04ff'` |
35
+ | Write all knowledge to one file | `export --out .c64-re-tools/local/knowledge.json` |
36
+ | Find what the knowledge holds of the program | `program` |
37
+ | Put a captured program into the knowledge | `image .c64-re-tools/local/capture/image.json` |
38
+ | Put a PRG into the knowledge | `image original/game.prg` |
39
+ | Put drive code into the knowledge | `image drive.prg --space drive8` |
40
+ | Add the coverage of runs | `coverage .c64-re-tools/local/runs/rooms/room0.bin` |
41
+ | Name the symbol at an address | `rename '$2100' update_player --kind routine --reason 'reads joystick 2'` |
46
42
  | Remove a symbol | `remove-symbol '$2100'` |
47
43
  | Classify memory | `classify '$3000-$303f' sprite` |
48
44
  | Classify a table of records | `classify '$3000-$303b' bytes --element 6` |
49
- | Many writes as one revision | `apply -` with the JSON on standard input, or `apply <file>` |
50
- | One batch from the batches of parallel agents | `join a.json b.json --out .c64-re-tools/local/joined.json` |
51
45
  | Make memory unknown, and keep the analyzers out of it | `unclassify '$3000-$303f'` |
52
- | Set or remove a comment | `comment '$2100' line Reads joystick 2.` / `uncomment '$2100' line` |
53
- | Add or remove a reference | `reference '$2000' '$2100' call` / `unreference ...` |
54
- | Places that a removal blocks | `suppressions` with `--range` |
55
- | Start a file of generated source | `module '$2000' player.asm --title 'Player movement'` / `unmodule '$2000'` |
56
- | Write the operand of an instruction as an expression | `operand '$8079' --lo irq_handler` / `unoperand '$8079'` |
57
- | Operand expressions | `operands` with `--range` |
58
- | Record a release of the program | `release ORIGINAL --file original/game.prg --base` / `unrelease V2` |
59
- | Write a switch | `switch PROTECTION --kind protection --releases ORIGINAL` / `unswitch PROTECTION` |
60
- | Write the code of a switch for a range | `patch '$100e-$1010' PROTECTION --code-file no-check.asm` / `unpatch '$100e'` |
61
- | Releases, switches and patches | `releases`, `switches`, and `patches` with `--switch` |
62
- | Write a macro | `macro inc16 --params dst --body-file inc16.asm` / `unmacro inc16` |
63
- | Write instructions as one use of a macro | `use '$1005-$100a' inc16 --args ptr` / `unuse '$1005'` |
64
- | Macros and their uses | `macros`, and `uses` with `--range` and `--macro` |
65
- | Files of generated source | `modules` |
66
- | The address spaces that hold knowledge | `spaces` |
67
- | History | `history '$2100'`, `history --name update_player`, `history --macro inc16`, `history --switch PROTECTION`, `revisions`, `revision 20261015T2145` |
68
- | Go back to an earlier state | `revert 20261015T2145` for all spaces, or `revert 20261015T2145 --space bank3` for one space |
46
+ | Write a comment | `comment '$2100' line Reads joystick 2.` |
47
+ | Add a reference | `reference '$2000' '$2100' call` |
48
+ | Find the places that a removal blocks | `suppressions --range '$2000-$2fff'` |
49
+ | Start a file of the generated source | `module '$2000' player.asm --title 'Player movement'` |
50
+ | Write the operand of an instruction as an expression | `operand '$8079' --lo irq_handler` |
51
+ | Record a release of the program | `release ORIGINAL --file original/game.prg --base` |
52
+ | Write a switch | `switch PROTECTION --kind protection --releases ORIGINAL` |
53
+ | Write the code of a switch for a range | `patch '$100e-$1010' PROTECTION --code-file no-check.asm` |
54
+ | List the releases, switches and patches | `switches` |
55
+ | Write a macro | `macro inc16 --params dst --body-file inc16.asm` |
56
+ | Write instructions as one use of a macro | `use '$1005-$100a' inc16 --args ptr` |
57
+ | Do many writes as one revision | `apply .c64-re-tools/local/batch.json` |
58
+ | Make one batch from the batches of parallel agents | `join a.json b.json --out .c64-re-tools/local/joined.json` |
59
+ | Read the history of an address or a name | `history '$2100'` |
60
+ | Read the last revisions | `revisions` |
61
+ | Go back to an earlier state | `revert 20261015T2145` |
69
62
  | Decide the conflicts after a git merge | `merge --reason 'merge of two computers'` |
70
63
 
71
- A revision has an id such as `20261015T214501.123-k3f9qz`. The id starts with the date and time. A command takes the full id, or a start of the id that only one revision has. The revision `0` is the knowledge before the first revision.
64
+ ---
72
65
 
73
- Every command takes `--space`: `c64` (the default), `drive8` for code in the 1541 drive, or `bank0` to `bank255` for cartridge banks. `revert` is different: without `--space`, it reverts all spaces. With `--space`, it reverts only that space. Drive code and banks use the same addresses as the C64, so each space has its own knowledge. A name is unique inside one space. In a batch, a write can name its own `space`.
66
+ ## Addresses, names and spaces
74
67
 
75
- Symbol kinds are `label`, `routine`, `variable`, `data` and `vector`. Reference kinds are `call`, `jump`, `read`, `write`, `reference` and `pointer`. Only an operand expression writes a `pointer` reference.
68
+ - A symbol is the name of an address from the user or the LLM. Get it from `at`, `search` or `symbols`.
69
+ - An analyzer name is a second name from DXA, Ghidra or an idiom search, for example `FUN_17e2`. Each analyzer keeps its own name at an address. Get them from `at`, `search` or `symbols --analyzer`.
70
+ - The generated source uses only the symbols. To use an analyzer name in the source, give the address that name with `rename`.
71
+ - A symbol name is a label for ACME: a letter or an underscore, then letters, digits or underscores. The knowledge refuses the name of a 6502 instruction, for example `lda`, in upper or lower case.
72
+ - A space is `c64` (the default), `drive8` for code in the 1541 drive, or `bank0` to `bank255` for cartridge banks. Drive code and banks use the same addresses as the C64. Thus each space has its own knowledge. A name is unique in one space.
73
+ - A revision id has the form `20261015T214501.123-k3f9qz`. It starts with the date and the time. Give the full id, or a start of the id that only one revision has. Get the ids from `revisions`, or from the `revision` field of a result. The revision `0` is the knowledge before the first revision.
74
+ - A file of a capture is the `image.json` that the capture of a packed program wrote. Keep the other files of the capture next to it.
76
75
 
77
- Region types:
76
+ ### Paths and the project folder
78
77
 
79
- | Type | Meaning |
80
- |---|---|
81
- | `code` | Instructions. |
82
- | `bytes`, `words`, `petscii`, `screen`, `bitmap`, `sprite` | Data of that form. |
83
- | `charset` | Characters of a character set, 8 bytes for each character. |
84
- | `addresses`, `addresses-split` | A table of addresses: one table of 16-bit values, or a low-byte table and then a high-byte table. |
85
- | `rts-addresses`, `rts-addresses-split` | The same, with each value the target minus 1, for a PHA, PHA, RTS dispatch. |
86
- | `jumps` | A table of `JMP abs` instructions. Each 3-byte entry is an entry point. It is code. |
87
- | `workspace` | RAM that the program writes at run time. Its bytes in the image do not matter. |
88
- | `unused` | Bytes that nothing executes or reads. |
89
- | `dual-use` | Code at one time and data or a vector at another time. No analyzer finding conflicts with it. |
78
+ - 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/`.
79
+ - 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.
80
+ - 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.
81
+ - A range is `<start>-<end>`, for example `'$c000-$cfff'`. In a JSON file, a range is `start` and `end`.
82
+ - Bytes in a JSON file are hex in a field `data`, for example `"data": "a900"`.
83
+ - 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.
84
+ - 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.
85
+ - The script refuses a folder where the command writes a file. It refuses a file where the command writes a folder.
86
+ - 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.
87
+ - 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.
88
+ - 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.
89
+ - 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`.
90
90
 
91
- `--element` gives the bytes of one element of a table. The length of the region must be a multiple of it. The result shows `element` and `count`.
91
+ ---
92
92
 
93
- ### Calling the script
93
+ ## Flags
94
94
 
95
- - Call a script as `node <skill>/scripts/<script>.ts <command> [subjects] [options]`. The command comes first.
96
- - `<script>.ts --help` lists the commands. `<script>.ts <command> --help` gives the subjects and the options of one command. The help is JSON: `{"text": "..."}`.
97
- - 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.
98
- - A range is `<start>-<end>`, for example `'$c000-$cfff'`. In a JSON file, a range is `start` and `end`.
99
- - Bytes in a JSON file are hex in a field `data`, for example `"data": "a900"`.
100
- - 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.
101
- - 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.
102
- - An error is `{"error": {"code": "...", "message": "..."}}`. The message tells what is wrong and what to do.
103
- - The exit status is 0 for done or PASS, 1 for FAIL, 2 for an error and 3 for INCONCLUSIVE.
95
+ Each flag applies only to the commands in the table. On another command, the script refuses the flag.
104
96
 
105
- ### Project folder
97
+ | Flag | Command | Effect |
98
+ |---|---|---|
99
+ | `--space <space>` | All commands | The space of the command: `c64`, `drive8`, or `bank0` to `bank255`. The default is `c64`. Without `--space`, `revert` reverts all spaces. |
100
+ | `--by <user\|llm>` | All commands in "Change the knowledge" | Who decided the change. The default is `llm`. Give `user` only when the user decided the change. |
101
+ | `--reason <text>` | All commands in "Change the knowledge" | Why you make the change. The revision keeps it, and `history` shows it. |
102
+ | `--evidence <kind:ref>` | All commands in "Change the knowledge" | One thing that the change rests on: `coverage:<map>`, `test:<file>`, `file:<file>` or `note:<text>`. You can give it more than one time. |
103
+ | `--expect-revision <revision>` | All commands in "Change the knowledge" | Refuse the change when the knowledge changed after this revision. Give the `revision` of your last read. |
104
+ | `--range <start>-<end>` | `symbols`, `regions`, `comments`, `references`, `suppressions`, `modules`, `operands`, `patches`, `uses`, `export` | Only the rows in this range. The default is all addresses. |
105
+ | `--origin <origin>` | `search`, `symbols`, `regions`, `comments`, `references`, `modules`, `operands`, `patches`, `macros`, `uses`, `export` | Only the rows of these origins: `user`, `llm`, `dxa`, `ghidra`, `heuristic`, `semantic` (user and llm), `analyzer` (dxa, ghidra and heuristic), or a comma list. The default is all origins. |
106
+ | `--max-listed <n>` | `search`, `symbols`, `regions`, `comments`, `references`, `suppressions`, `modules`, `operands`, `patches`, `switches`, `releases`, `macros`, `uses`, `spaces`, `history`, `revisions`, `revision`, `listing` | The most items in each list of the body. The default is 50. The counts are always complete. |
107
+ | `--kind <kind>` | `symbols`, `rename` | The symbol kind: `label`, `routine`, `variable`, `data` or `vector`. A read gives all kinds by default. A new symbol without `--kind` is a `label`. A `rename` without `--kind` keeps the kind of the symbol. |
108
+ | `--kind <kind>` | `references` | Only references of this kind: `call`, `jump`, `read`, `write`, `reference` or `pointer`. |
109
+ | `--kind <kind>` | `switch` | Necessary. The switch kind: `protection`, `trainer`, `cheat`, `crack`, `fix` or `change`. |
110
+ | `--analyzer` | `symbols` | List the analyzer names in place of the symbols. |
111
+ | `--type <type>` | `regions` | Only regions of this type. |
112
+ | `--switch <switch>` | `patches`, `unpatch` | Only the patches of this switch. For `unpatch`, the switch of the patch to remove. |
113
+ | `--switch <name>` | `history` | The history of a switch and its patches in place of an address. |
114
+ | `--macro <name>` | `uses` | Only the uses of this macro. |
115
+ | `--macro <name>` | `history` | The history of a macro in place of an address. |
116
+ | `--name <name>` | `history` | The history of a name in place of an address. |
117
+ | `--before <revision>` | `revisions` | Only the revisions before this one. |
118
+ | `--out <file>` | `export`, `join` | Necessary. The JSON file to write. |
119
+ | `--out <file>` | `listing` | Write the listing as text to this file in place of the lines in the body. |
120
+ | `--element <n>` | `classify` | The bytes of one element of a table. The length of the range must be a multiple of it. |
121
+ | `--title <text>` | `module` | The title of the file. A `module` write without a title keeps the title that the module has. |
122
+ | `--lo <symbol>` | `operand` | The operand is the low byte of the symbol plus the offset. |
123
+ | `--hi <symbol>` | `operand` | The operand is the high byte of the symbol plus the offset. |
124
+ | `--value <symbol>` | `operand` | The operand is the symbol plus the offset. |
125
+ | `--expr <text>` | `operand` | The operand is this ACME expression. |
126
+ | `--offset <n>` | `operand` | With `--lo`, `--hi` or `--value`: a whole number to add, for example `1` or `'$1000'`. Write a negative offset as `--offset=-1`. The default is 0. |
127
+ | `--code <text>` | `patch` | The ACME code of the patch. |
128
+ | `--code-file <file>` | `patch` | A project file with the ACME code of the patch. |
129
+ | `--nop` | `patch` | The patch is NOP only. |
130
+ | `--comment <text>` | `patch`, `switch`, `release`, `macro` | A comment for the row. A `patch`, `switch` or `macro` write without `--comment` keeps the comment that the row has. |
131
+ | `--default <0\|1>` | `switch` | The value in the default build. See the section of `switch`. |
132
+ | `--releases <A,B>` | `switch` | The labels of the releases that have the switch on, as a comma list. A write without it keeps the releases that the switch has. |
133
+ | `--file <file>` | `release` | Necessary. The PRG or `image.json` of the release, in a folder that git keeps. |
134
+ | `--base` | `release` | This release is the program of the knowledge. |
135
+ | `--params <a,b>` | `macro` | The params of the macro, as a comma list without a dot. The default is no params. |
136
+ | `--body <text>` | `macro` | The body of the macro. |
137
+ | `--body-file <file>` | `macro` | A project file with the body of the macro. |
138
+ | `--args <x, y>` | `use` | One expression for each param of the macro, as a comma list. |
106
139
 
107
- - 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/`.
108
- - 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.
109
- - 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.
110
- - 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/`.
111
- - 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.
112
- - 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.
113
- - 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`.
140
+ ---
114
141
 
115
- ## Workflow
142
+ ## Read the knowledge
116
143
 
117
- Read:
144
+ A read writes nothing. A project without knowledge gives empty lists and the revision `0`.
118
145
 
119
- 1. Use `at` for the addresses in the question before other work.
120
- 2. Use `history` when the current knowledge is surprising or disputed. Each row gives the `reason` and the `evidence` of the revision that wrote it.
146
+ Each row in a body has `origin` and `revision`. A row of a space other than `c64` also has `space`.
121
147
 
122
- Write:
148
+ When a git merge left conflicts in the knowledge, each read body also has `conflicts`. Each item gives the `conflict` and its `rows`. The `hint` tells you to decide the conflicts, and `nextAction` is the `merge` command. Each write stops until you run `merge`.
123
149
 
124
- 1. Read the current value with `at`. Note the `revision` in the result.
125
- 2. Decide the change. Keep observed facts apart from your conclusions.
126
- 3. Write it with `--reason` and `--expect-revision` set to the revision you read. Add `--evidence` for each thing that the change rests on: `coverage:<map>`, `test:<file>`, `file:<file>` or `note:<text>`.
127
- - `coverage:<map>` names a map by its path at the import. The script uses the last import of that map into the space of the write.
128
- - `test:<file>` and `file:<file>` name a file outside `.c64-re-tools/local/`. Move the file before the write.
129
- - The script keeps the SHA-256 of each file and map.
130
- 4. Read the result. A `revision` of `null` means that nothing changed.
150
+ ### `at <address>`
131
151
 
132
- Each write is one revision. A classification inside a region splits that region, so regions never overlap.
152
+ - `body` holds `address`, `comments`, `referencesFrom`, `referencesTo` and `revision`. When the knowledge has them, it also holds `symbol`, `analyzerNames`, `region`, `operand`, `patch`, `macroUse` and `suppressions`.
153
+ - `revision` is the current revision of the knowledge. Give it to `--expect-revision` of your next write.
154
+ - Use `at` for the addresses of the question before other work.
133
155
 
134
- A write can give the same fact that an analyzer gave. Then the fact gets the origin of the write, and `previous` in the result shows the row of the analyzer. The next analysis does not remove the fact. This is true for a symbol, a region, a comment and a reference. A `--by user` write over the same fact of `llm` also changes the origin to `user`. A `--by llm` write over the same fact of `user` changes nothing.
156
+ ### `search <text>`
135
157
 
136
- A comment and a module are at the start of an instruction. In a code region, the script refuses an address that is an operand byte of an instruction. The error gives the start of that instruction. In data, each address is correct.
158
+ - `body` holds the lists `symbols`, `analyzerNames` and `comments` with the text in their names or comment text.
159
+ - If a list is long, give a longer text.
137
160
 
138
- ## The program and the coverage
161
+ ### `symbols`
139
162
 
140
- The knowledge holds the program, so a project needs no image or coverage files. The other skills read them from the knowledge and take no path to them.
163
+ - `body.symbols` lists the symbols, each with `address`, `name` and `kind`.
164
+ - With `--analyzer`, `body.analyzerNames` lists the analyzer names in place of the symbols.
141
165
 
142
- 1. Capture a packed program with the c64-unpacker skill into `.c64-re-tools/local/`. Then put it in with `image .c64-re-tools/local/capture/image.json`. The script reads `image.bin`, `entry_state.json` and the stages next to it.
143
- 2. For a program without a packer, drive code or a cartridge bank, put the PRG in with `image <file.prg>`. Give `--space drive8` or `--space bank0` for drive code or a bank. A PRG has the extension `.prg` or no extension. The script refuses a file with another extension.
144
- 3. Write each coverage map of a run into `.c64-re-tools/local/`. Then add it with `coverage <map.bin> ...`.
145
- 4. Read what the knowledge holds with `program`: the segments, the entry, and the coverage maps that it has.
166
+ ### `regions`
146
167
 
147
- - `image` writes only the bytes that changed. The same image again writes nothing. A new image replaces the program of the space. The history keeps the old bytes.
148
- - `coverage` joins the maps. It keeps each address that a run executed, read or wrote. It also keeps each address that one run wrote and executed: self-modified code. `coverage` puts each map into the space of `--space`. You can add the same map to each space. A map that the space has is not added again.
149
- - The capture and the maps are not necessary after the import. Keep them in `.c64-re-tools/local/`.
150
- - A `revert` also gives back the coverage maps of that revision. A `revert` to a revision before an import removes the map and its ranges. Then you can import the map again, and `--evidence coverage:<map>` refuses it until you do.
168
+ - `body.regions` lists the regions, each with `start`, `end` and `type`. A table also has `element` and `count`.
151
169
 
152
- For more than a few writes, use `apply -` and give the writes on standard input as one JSON list. Each write has `op` and the arguments of the command with the same name, for example:
170
+ ### `comments`
153
171
 
154
- ```json
155
- [
156
- { "op": "rename", "address": "$2100", "name": "update_player", "kind": "routine" },
157
- { "op": "classify", "start": "$3000", "end": "$303b", "type": "bytes", "element": 6 },
158
- { "op": "comment", "address": "$2100", "placement": "line", "text": "Reads joystick 2." }
159
- ]
160
- ```
172
+ - `body.comments` lists the comments, each with `address`, `placement` and `text`.
161
173
 
162
- The ops and their fields:
174
+ ### `references`
163
175
 
164
- | op | Fields |
176
+ - `body.references` lists the references, each with `from`, `to` and `kind`.
177
+
178
+ ### `suppressions`
179
+
180
+ - `body.suppressions` lists the places that a removal blocks. Each item has `suppresses` (`symbol`, `region` or `reference`), the place, and the `reason` of the removal.
181
+ - `unclassify` and `unreference` block the place for DXA and Ghidra. A later analysis does not add a region in that range or that reference again. This is also true for a fact from the LLM.
182
+ - `remove-symbol` does not block the address, because an analyzer name never becomes a symbol.
183
+ - A `rename`, `classify` or `reference` at a blocked place ends the block there. A `revert` to a revision before the removal also ends the block.
184
+ - `at` and `history` also show the blocks.
185
+
186
+ ### `modules`
187
+
188
+ - `body.modules` lists the modules, each with `start`, `file` and `title`.
189
+
190
+ ### `operands`
191
+
192
+ - `body.operands` lists the operand expressions, each with `address`, `select` and `text`. `text` is the operand as the source writes it.
193
+
194
+ ### `patches`
195
+
196
+ - `body.patches` lists the patches, each with `start`, `end`, `bytes`, `switch`, `code` and `comment`.
197
+
198
+ ### `switches`
199
+
200
+ - `body.switches` lists the switches. Each item has `name`, `kind`, `default`, `base`, `releases`, `comment` and the ranges of its `patches`.
201
+ - `base` is the value of the switch in the program of the knowledge.
202
+
203
+ ### `releases`
204
+
205
+ - `body.releases` lists the releases, the base release first. Each item has `label`, `file`, `sha256`, `base` and `define`.
206
+ - `define` is the assembler define that builds the release.
207
+
208
+ ### `macros`
209
+
210
+ - `body.macros` lists the macros, each with `name`, `params`, `body` and `comment`.
211
+
212
+ ### `uses`
213
+
214
+ - `body.uses` lists the macro uses, each with `start`, `end`, `macro`, `args` and `text`. `text` is the line of the source, for example `+inc16 ptr`.
215
+
216
+ ### `spaces`
217
+
218
+ - `body.spaces` lists the spaces that hold knowledge.
219
+
220
+ ### `program`
221
+
222
+ - `body.image` is `null` when the knowledge has no program in the space. Then put the program in with `image`.
223
+ - Else `body.image` has `segments`, `bytes`, `entry`, `state` and `stages`. `state` is `true` when the knowledge has the entry state of a capture. `stages` is the number of unpacker stages.
224
+ - `body.coverage.maps` lists the coverage maps of the space. `body.coverage.ranges` gives the number of ranges of each access kind.
225
+
226
+ ### `listing <range>`
227
+
228
+ - `body.lines` lists one line for each instruction of a code region, and one `!byte` line for other bytes.
229
+ - Each line has `address`, `bytes`, `text`, `referencesIn` and `referencesOut`. It can also have `label`, `analyzerNames` and `comment`.
230
+ - `text` is the instruction with the names of its target. An operand expression of the knowledge replaces the operand.
231
+ - When the knowledge has coverage, `access` tells if a run executed (`x`), read (`r`) or wrote (`w`) the bytes of the line, or `-` for none.
232
+ - With `--out`, the body has `lines` as a number, and `out`, `bytes` and `sha256` of the text file.
233
+ - If the knowledge has no program in the space, the script refuses. Put the program in with `image` first.
234
+ - Use `listing` in place of your own disassembler.
235
+
236
+ ### `export`
237
+
238
+ - `body` holds `counts`, `revision`, `out`, `bytes` and `sha256`. `counts` gives the number of rows of each kind in the file.
239
+ - The file has all current knowledge of the space in the range, with the analyzer names and the source knowledge.
240
+
241
+ ---
242
+
243
+ ## Read the history
244
+
245
+ ### `history [<address>]`
246
+
247
+ - Give exactly one of `<address>`, `--name`, `--macro` and `--switch`.
248
+ - `body.history` lists the rows of all revisions. Each item has `entity`, the fields of the row, `fromRevision`, and `toRevision` when a later revision closed the row. It also has the `reason` and the `evidence` of the revision that wrote it.
249
+ - Use `history` when the current knowledge is surprising or disputed.
250
+
251
+ ### `revisions`
252
+
253
+ - `body.revisions` lists the revisions, newest first. Each item has `id`, `operation`, `description`, `createdAt`, `origin` and `evidence`.
254
+ - To get older revisions, give `--before` with the oldest `id` in `listed`.
255
+
256
+ ### `revision <revision>`
257
+
258
+ - `body.revision` gives the revision. `body.added` and `body.closed` list the rows that it added and closed.
259
+ - The rows include the memory, entry and coverage rows of the program.
260
+
261
+ ---
262
+
263
+ ## Change the knowledge
264
+
265
+ Each command in this group writes one revision. The first accepted write makes `.c64-re-tools/knowledge/`. A refused write writes nothing.
266
+
267
+ - `body.revision` is the new revision. A `revision` of `null` tells that nothing changed.
268
+ - When the write replaced a row of a different origin, `body.previous` gives that row as it was before. For example, an `llm` write over a `user` name gives `previous`. Read `previous` and tell the user about the change.
269
+ - A write can give the same fact that an analyzer gave. Then the fact gets the origin of the write, and the next analysis does not remove it.
270
+ - A `--by user` write over the same fact of `llm` changes the origin to `user`. A `--by llm` write over the same fact of `user` changes nothing.
271
+ - A comment, a module and an operand expression are at the start of an instruction. In a code region, the script refuses an address that is an operand byte of an instruction. The `hint` gives the start of that instruction. In data, each address is correct.
272
+
273
+ Errors of this group:
274
+
275
+ - `conflict`: the change contradicts the current knowledge. For example, the name belongs to another address. Read that address and its history. Then decide.
276
+ - `conflict` with "from a git merge": git joined the knowledge of two computers, and two rows are current where the knowledge permits one row. Run `merge`. Then write again.
277
+ - `stale-revision`: the knowledge changed after the revision of `--expect-revision`. Read the knowledge again. Then decide again.
278
+ - `not-found`: there is nothing to remove, no such revision, or no such file.
279
+ - `invalid-database` or `unsupported-schema`: give `error` and `hint` to the user. Do not edit the knowledge files.
280
+
281
+ ### `rename <address> <name>`
282
+
283
+ - `body.symbol` gives the symbol after the write.
284
+ - A `rename` changes the name in each operand expression and in each argument of a macro use.
285
+
286
+ ### `remove-symbol <address>`
287
+
288
+ - The knowledge refuses the removal while an operand expression or a macro use names the symbol. Remove the expression with `unoperand`, or the use with `unuse`, first.
289
+
290
+ ### `classify <range> <type>`
291
+
292
+ - `body.regions` gives the regions after the write. A classification inside a region splits that region. Thus regions never overlap.
293
+ - With `--element`, each region item also has `element` and `count`.
294
+
295
+ | Type | Meaning |
165
296
  |---|---|
166
- | `rename` | `address`, `name`, `kind` (optional) |
167
- | `remove-symbol` | `address` |
168
- | `classify` | `start`, `end`, `type`, `element` (optional) |
169
- | `unclassify` | `start`, `end` |
170
- | `comment` | `address`, `placement` (`line` or `side`), `text` |
171
- | `uncomment` | `address`, `placement` |
172
- | `reference`, `unreference` | `from`, `to`, `kind` |
173
- | `module` | `start`, `file`, `title` (optional) |
174
- | `unmodule` | `start` |
175
- | `operand` | `address`, `select` (`lo`, `hi`, `value` or `expr`), `symbol` and `offset` (optional), or `expression` for `expr` |
176
- | `unoperand` | `address` |
177
- | `patch` | `start`, `end`, `switch`, `code` (empty for NOP only), `comment` (optional) |
178
- | `unpatch` | `start`, `switch` (optional) |
179
- | `unuse` | `start` |
180
- | `macro` | `name`, `params` (a list or a comma list), `body`, `comment` (optional) |
181
- | `unmacro` | `name` |
182
- | `use` | `start`, `end`, `macro`, `args` (a list or a comma list) |
297
+ | `code` | Instructions. |
298
+ | `bytes`, `words`, `petscii`, `screen`, `bitmap`, `sprite` | Data of that form. |
299
+ | `charset` | Characters of a character set, 8 bytes for each character. |
300
+ | `addresses`, `addresses-split` | A table of addresses: one table of 16-bit values, or a low-byte table and then a high-byte table. |
301
+ | `rts-addresses`, `rts-addresses-split` | The same, with each value the target minus 1, for a PHA, PHA, RTS dispatch. |
302
+ | `jumps` | A table of `JMP abs` instructions. Each 3-byte entry is an entry point. It is code. |
303
+ | `workspace` | RAM that the program writes at run time. Its bytes in the image do not matter. |
304
+ | `unused` | Bytes that nothing executes or reads. |
305
+ | `dual-use` | Code at one time, and data or a vector at another time. No analyzer finding conflicts with it. |
183
306
 
184
- Each write can also have `space`. A `line` comment is a line of its own above the address. A `side` comment is at the end of the line of the address. The script checks an `operand`, `patch` or `use` write against the program of the knowledge in the space of `--space`. All writes become one revision. If the knowledge refuses one write, the script writes nothing, and the error names the write.
307
+ ### `unclassify <range>`
185
308
 
186
- The c64-provenance skill writes a batch with `match --out`. The batch holds the names of library routines at their addresses in this program. Read the `batch.leftOut` of its result first. Then apply the batch with the command in its `apply`.
309
+ - The range becomes unknown. The analyzers do not classify it again. See the section of `suppressions`.
187
310
 
188
- ## Listing
311
+ ### `comment <address> <placement> <text>...`
189
312
 
190
- `listing` reads the program and the other knowledge, and writes no knowledge. Use it in place of your own disassembler.
313
+ - `<placement>` is `line` or `side`. A `line` comment is a line of its own above the address. A `side` comment is at the end of the line of the address.
314
+ - `body.comment` gives the comment after the write.
191
315
 
192
- - `listing` gives one line for each instruction of a code region, and one `!byte` line for other bytes. A line has the name of the address, the instruction with the names of its target, and the comments. An operand expression of the knowledge replaces the operand. `analyzerNames` gives the second names. `referencesIn` and `referencesOut` count the references. When the knowledge has coverage, `access` tells if a run executed (`x`), read (`r`) or wrote (`w`) the bytes of the line, or `-` for none. `--out` writes the listing as text to a project file.
316
+ ### `uncomment <address> <placement>`
193
317
 
194
- ## Batches of parallel agents
318
+ - Removes the comment of that placement.
195
319
 
196
- When more agents name the program at the same time, give each agent one range of addresses. Each agent writes one batch file and does not apply it. A batch of an agent has the form `{"owner": {"start": "$1000", "end": "$1fff"}, "ops": [...]}`. `ops` holds the writes, as for `apply`. The owner ranges must not overlap.
320
+ ### `reference <from> <to> <kind>`
197
321
 
198
- `join` makes one batch from the batch files. It writes no knowledge.
322
+ - `<kind>` is `call`, `jump`, `read`, `write` or `reference`. Only an operand expression writes a `pointer` reference.
323
+ - `body.reference` gives the reference after the write.
199
324
 
200
- - At an address in the owner range of a batch, the name, the comment, the operand and the module of that batch stay. At an address that no batch owns, the first batch in the command stays.
201
- - A batch can give an address a name that `join` replaces. Then `join` also replaces that name in the operand expressions and macro args of that batch.
202
- - The regions of the owner come after the regions of the other batches. Thus the regions of the owner stay where they overlap.
203
- - A name from a batch stays when another batch removes the symbol at that address.
204
- - `choices` lists each place where the batches gave different values, with the value that stays.
205
- - When one name or one module file is at two addresses, `join` writes nothing and gives the error `conflict`. The message names the addresses and the batches. Correct the batches, then do `join` again.
325
+ ### `unreference <from> <to> <kind>`
206
326
 
207
- Then apply the joined batch with the command in `apply`.
327
+ - The analyzers do not add this reference again. See the section of `suppressions`.
208
328
 
209
- ## Modules
329
+ ### `module <start> <file.asm>`
210
330
 
211
- A module tells the c64-source skill where a file of the generated source starts. The file runs to the next module or to the end of its segment. Each address has one module, and each file name has one module. A file name has letters, digits, underscores or hyphens, and ends in `.asm`. The c64-source skill writes `main.asm`, `loader.asm`, `switches.asm`, `config.asm`, `constants.asm` and `macros.asm` itself. Thus a module cannot have one of these names, in upper or lower case. The title becomes the first line of the file. A `module` write without a title keeps the title that the module has. Modules have a history, and `revert` restores them.
331
+ - A module tells where a file of the generated source starts. The file runs to the next module or to the end of its segment.
332
+ - Each address has one module, and each file name has one module.
333
+ - A file name has letters, digits, underscores or hyphens, and ends in `.asm`. It has no folder.
334
+ - The generated source has the files `main.asm`, `loader.asm`, `switches.asm`, `config.asm`, `constants.asm` and `macros.asm` already. Thus a module cannot have one of these names, in upper or lower case.
335
+ - The title becomes the first line of the file.
336
+ - `body.module` gives the module after the write.
212
337
 
213
- ## Operand expressions
338
+ ### `unmodule <start>`
214
339
 
215
- An operand expression tells the c64-source skill how to write the operand of one instruction. The source then has a name, not a number. When code or data changes its length, the operand follows the address that moved.
340
+ - Removes the module that starts at the address.
216
341
 
217
- Give one selector:
342
+ ### `operand <address>`
218
343
 
219
- | Option | Operand | Source |
220
- |---|---|---|
221
- | `--lo <symbol>` | The low byte of the symbol plus the offset | `lda #<irq_handler` |
222
- | `--hi <symbol>` | The high byte of the symbol plus the offset | `lda #>(sprite_bank+$1000)` |
223
- | `--value <symbol>` | The symbol plus the offset | `lda bar_colours-1,x` |
224
- | `--expr <text>` | ACME expression text with names and numbers | `lda #(sprite_bank + 5*64) / 64` |
225
-
226
- - The address is the start of an instruction in a code region, as `listing` shows it. The script refuses an address in no code region and an operand byte of an instruction. Classify the code first.
227
- - `--offset` adds a whole number to the symbol. Write a negative offset as `--offset=-1`.
228
- - A name is a symbol of the knowledge or a platform name of the c64-memory-map skill.
344
+ An operand expression tells the generated source how to write the operand of one instruction. The source then has a name, not a number. When code or data changes its length, the operand follows the address that moved.
345
+
346
+ | Flag | Source |
347
+ |---|---|
348
+ | `--lo irq_handler` | `lda #<irq_handler` |
349
+ | `--hi sprite_bank --offset '$1000'` | `lda #>(sprite_bank+$1000)` |
350
+ | `--value bar_colours --offset=-1` | `lda bar_colours-1,x` |
351
+ | `--expr '(sprite_bank + 5*64) / 64'` | `lda #(sprite_bank + 5*64) / 64` |
352
+
353
+ - Give exactly one of `--lo`, `--hi`, `--value` and `--expr`.
354
+ - The knowledge must have the program of the space. Else the script refuses. Put the program in with `image` first.
355
+ - The address is the start of an instruction in a code region, as `listing` shows it. The script refuses an address in no code region. Classify the code first.
356
+ - A name is a symbol of the knowledge or a platform name of the C64 memory map.
229
357
  - `--expr` takes `$`, `%` and decimal numbers, names, brackets, the operators `+ - * / % << >> >>> & | XOR`, and `<`, `>`, `-` and `!` before an operand.
230
- - The script compares the value of the expression with the operand in the program of the knowledge. If they are not equal, the script refuses the write.
358
+ - The script compares the value of the expression with the operand in the program. If they are not equal, the script refuses the write. The `error` gives both values.
231
359
  - Each name in the expression gets a `pointer` reference from the instruction. Thus `at` on the named address shows the code that uses the address.
232
- - A `rename` of a symbol changes the name in each expression. The knowledge refuses `remove-symbol` while an expression uses the symbol. Use `unoperand` first.
233
-
234
- Write an operand expression for each immediate that is a byte of an address. Also write one for each table read through the byte before the table. The pointer candidates of the c64-static-analysis skill show many of these places.
360
+ - `body.operand` gives the expression after the write, with its `text`.
361
+ - Write an operand expression for each immediate that is a byte of an address. Also write one for each table read through the byte before the table. The pointer candidates of a static analysis show many of these places.
235
362
 
236
- ## Releases and switches
363
+ ### `unoperand <address>`
237
364
 
238
- A rebuild keeps all of the program. It keeps unused bytes, dead code, old copies and leftovers of the development. It keeps a copy protection, a trainer and a cheat in the source. A switch puts each of them in or out of the build. Only a cracktro is not in the rebuild: see the c64-unpacker skill.
365
+ - Removes the operand expression at the address.
239
366
 
240
- ### Releases
367
+ ### `release <label>`
241
368
 
242
- A release is a PRG or a c64-unpacker `image.json` of the program, in a folder that git keeps, for example `original/`. `release` records its label, its file and the SHA-256 of the file.
369
+ A rebuild keeps all of the program: unused bytes, dead code, old copies and leftovers of the development. It keeps a copy protection, a trainer and a cheat in the source. A switch puts each of them in or out of the build. Only a cracktro is not in the rebuild.
243
370
 
371
+ - A release is a PRG or the `image.json` of a capture, in a folder that git keeps, for example `original/`. The script refuses a file in `.c64-re-tools/local/`.
372
+ - A label is upper case, for example `ORIGINAL`, `FIRESTAR` or `V2`.
244
373
  - The base release is the release whose bytes are the program of the knowledge. Record it with `--base`. The script refuses `--base` when the segments or the bytes of the file are not the program.
245
374
  - A space has one base release. Record the base release before the first switch.
246
- - The build of a release defines `RELEASE_<label>=1`. Then each switch has the value of that release. The c64-source skill checks that this build gives the bytes of the file.
247
- - A label is upper case, for example `ORIGINAL`, `FIRESTAR` or `V2`.
248
- - `unrelease` refuses a release that a switch names. It also refuses the base release while the knowledge has switches.
375
+ - The build of a release gives each switch the value of that release. The check of the generated source compares that build with the bytes of the file.
376
+ - `body.release` gives the release, with its `sha256` and its `define`.
377
+
378
+ ### `unrelease <label>`
379
+
380
+ - The knowledge refuses a release that a switch names. It also refuses the base release while the knowledge has switches.
249
381
 
250
- ### Switches
382
+ ### `switch <name>`
251
383
 
252
- All switches are in `switches.jsonl` of the knowledge, and the c64-source skill writes all of them to `config.asm` with their defaults. A switch has a kind, a default and the releases that have it on.
384
+ A switch has a kind, a default, and the releases that have it on. The generated source writes all switches with their defaults.
253
385
 
254
386
  | Kind | What it changes | Default |
255
387
  |---|---|---|
@@ -257,107 +389,231 @@ All switches are in `switches.jsonl` of the knowledge, and the c64-source skill
257
389
  | `trainer` | A trainer, for example infinite lives | 0 (off) |
258
390
  | `cheat` | A cheat | 0 (off) |
259
391
  | `crack` | A change that a cracker made in the program, for example a credit text | 0 (off) |
260
- | `fix` | A correction that one or more releases have | 1 (on). Give `--default 0` only for an alternative (below). |
261
- | `change` | Another difference between releases | Give `--default 0` or `--default 1` |
392
+ | `fix` | A correction that one or more releases have | 1 (on). Give `--default 0` only for an alternative (see `patch`). |
393
+ | `change` | Another difference between releases | Give `--default 0` or `--default 1`. |
262
394
 
263
- - The default build is the clean program. It has no copy protection, trainer, cheat or crack change, and it has all corrections. The script refuses `--default 1` for `protection`, `trainer`, `cheat` and `crack`.
264
- - `--releases` lists the labels of the releases that have the switch on. A switch that no release has on, for example a trainer of your own, has no `--releases`.
395
+ - The default build is the clean program. It has no copy protection, trainer, cheat or crack change, and it has all corrections. Thus the script refuses `--default 1` for `protection`, `trainer`, `cheat` and `crack`.
396
+ - A switch that no release has on, for example a trainer of your own, has no `--releases`.
265
397
  - The base value of a switch is 1 when the base release has it on, else 0. The program of the knowledge has the base value of each switch.
266
398
  - A switch name is upper case, for example `PROTECTION`, `TRAINER` or `FIX_LIVES`. It cannot start with `RELEASE_`. It cannot be the name of a symbol or a platform name.
267
- - A `switch` write without `--releases` or `--comment` keeps the releases and the comment that the switch has.
268
399
  - The script refuses a change of the base value while the switch has patches. Remove the patches first, because they hold the other state.
269
- - `unswitch` refuses a switch that has patches.
270
- - `switches` gives each switch with `default`, `base`, `releases` and its patches. `releases` gives each release with its define.
400
+ - The script refuses defaults that put in two alternatives.
401
+ - `body.switch` gives the switch after the write, with its `base`.
271
402
 
272
- ### Patches
403
+ ### `unswitch <name>`
404
+
405
+ - The knowledge refuses a switch that has patches. Remove the patches with `unpatch` first.
406
+
407
+ ### `patch <range> <switch>`
273
408
 
274
409
  A patch is ACME code for a range of the program. The code is the bytes of the switch in the state that the base release does not have. Examples:
275
410
 
276
411
  - The base release has a copy protection. The switch `PROTECTION` names the base release. The code is the program without the protection, for example NOP in place of the `JSR` to the check.
277
- - The base release has the protection already removed, and another release has it: the code is the bytes of the protection from that release. When no release has these bytes, record in a comment that the crack removed the protection.
278
- - A correction is only in release `V2`: the switch `FIX_LIVES` names `V2`, and the code is the bytes of `V2`.
412
+ - The base release has the protection already removed, and another release has it. The code is the bytes of the protection from that release. When no release has these bytes, write in a comment that the crack removed the protection.
413
+ - A correction is only in release `V2`. The switch `FIX_LIVES` names `V2`, and the code is the bytes of `V2`.
279
414
  - A correction is in the base release and not in `V1`. The switch names the base release, and the code is the bytes of `V1`.
280
415
 
281
416
  Rules:
282
417
 
418
+ - Give exactly one of `--code`, `--code-file` and `--nop`.
283
419
  - Write the switch first. `patch` refuses a switch that the knowledge does not have.
284
420
  - Many patches can have the same switch.
285
- - The code must fit in the range. NOP ($EA) fills the bytes after the code. `--nop` gives a range of NOP only.
286
- - `patch` refuses code that is longer than the range when each line is a 6510 instruction, `!byte`, `!word`, `!fill` or a comment. `patch` does not check the length of other code, for example code with a label or a macro. It does not assemble the code. `generate` of the c64-source skill does these checks.
287
- - `--code` gives the code as text. `--code-file` reads the code from a project file. The code can use the labels and the macros of the source, and local labels with a dot, for example `.skip`.
288
- - The braces in the code must be pairs, because the source puts the code inside `{ }`.
289
- - Patches of different switches can have the same range. They are alternatives, for example two releases with two different corrections of one bug. The default build can put in only one of them. Thus `switch` refuses defaults that put in two alternatives.
421
+ - The code must fit in the range. NOP (`$EA`) fills the bytes after the code.
422
+ - `patch` refuses code that is longer than the range when each line is a 6510 instruction, `!byte`, `!word`, `!fill` or a comment. It does not check the length of other code, for example code with a label or a macro. The check of the generated source finds these errors.
423
+ - The code can use the labels and the macros of the source, and local labels with a dot, for example `.skip`.
424
+ - The braces in the code must be pairs.
425
+ - Patches of different switches can have the same range. They are alternatives, for example two releases with two different corrections of one bug. The default build can put in only one of them.
290
426
  - The knowledge refuses all other overlaps of patches. A patch cannot overlap a macro use.
291
427
  - A patch with the same start and the same switch replaces the patch.
292
- - When patches of two switches start at one address, give `unpatch` the switch with `--switch`.
293
428
  - Each byte of the range must be in the program of the knowledge.
294
- - A `patch` write without `--comment` keeps the comment that the patch has.
429
+ - `body.patch` gives the patch after the write.
295
430
 
296
- Build with the c64-assembler skill:
431
+ ### `unpatch <start>`
297
432
 
298
- - The build of `config.asm` has no options. With the defaults, this is the default build.
299
- - A release: `--release ORIGINAL`. A release build does not read `config.asm`.
300
- - One switch: set its value in `config.asm` of the source, for example `TRAINER = 1`.
433
+ - When patches of two switches start at the address, give the switch with `--switch`.
301
434
 
302
- ## Macros
435
+ ### `macro <name>`
303
436
 
304
437
  A macro writes a run of instructions as one line of the source, for example `+inc16 ptr`. Use macros to make the source shorter and easier to read.
305
438
 
306
- - `macro` writes the ACME macro: its name, its params and its body. `--params` gives the params as a comma list, without a dot. The body uses each param with a dot, for example `.dst`.
307
- - `use` writes the instructions from the start to the end as one use of a macro. `--args` gives one expression for each param, as a comma list. Each name in an arg is a symbol of the knowledge or a platform name.
308
- - The range of a use is complete instructions of the program of the knowledge.
309
- - A `rename` of a symbol changes the name in each arg. The knowledge refuses `remove-symbol` while an arg uses the symbol.
310
- - The knowledge refuses `unmacro` while the macro has uses. It also refuses a change of the number of params while the macro has uses.
439
+ - Give `--body` or `--body-file`. The body uses each param with a dot, for example `.dst`.
440
+ - The knowledge refuses a change of the number of params while the macro has uses.
441
+ - `body.macro` gives the macro after the write.
442
+
443
+ ### `unmacro <name>`
444
+
445
+ - The knowledge refuses a macro that has uses. Remove the uses with `unuse` first.
446
+
447
+ ### `use <range> <macro>`
448
+
449
+ - The range is complete instructions of the program of the knowledge.
450
+ - Each name in an argument is a symbol of the knowledge or a platform name.
311
451
  - A use cannot overlap a patch or another use.
452
+ - The generated source writes a use only when the use gives the bytes of the program. Else it writes the instructions and gives the reason.
453
+ - `body.use` gives the use after the write.
312
454
 
313
- The c64-source skill writes a use only when the use gives the bytes of the program. If not, it writes the instructions and gives the reason. The c64-static-analysis skill finds uses of known 6502 idioms and writes them to a batch with `--out-macro-batch`.
455
+ ### `unuse <start>`
314
456
 
315
- ## Removals
457
+ - Removes the macro use that starts at the address.
316
458
 
317
- `unclassify` and `unreference` block the place for DXA and Ghidra. An analysis does not add a region in that range or that reference again. This applies to every removed fact, also to a fact from the LLM. `remove-symbol` does not block the address, because an analyzer name never becomes a symbol.
459
+ ### `image <file>`
318
460
 
319
- - `suppressions` lists the blocked places with the reason of each removal. `at` and `history` also show them.
320
- - A `rename`, `classify` or `reference` at a blocked place ends the block there, because your own fact is at the place then.
321
- - A `revert` to a revision before the removal also ends the block.
461
+ - `<file>` is the `image.json` of a capture, or a PRG. A PRG has the extension `.prg` or no extension. The script refuses a file with another extension.
462
+ - For drive code or a cartridge bank, give `--space drive8` or `--space bank0`.
463
+ - `body` holds `revision`, `bytes`, `rowsAdded`, `rowsClosed` and `entryChanged`.
464
+ - `image` writes only the bytes that changed. The same image again writes nothing. A new image replaces the program of the space. The history keeps the old bytes.
465
+ - After the import, you do not need the file of the capture. Keep it in `.c64-re-tools/local/`.
322
466
 
323
- ## Knowledge
467
+ ### `coverage <map>...`
324
468
 
325
- Write after interpretation:
469
+ - `<map>` is a 64 KiB coverage map of a run. Write each map in `.c64-re-tools/local/`.
470
+ - `body` holds `revision`, `imported`, `known` and `ranges`. `imported` lists the new maps. `known` lists the maps that the space has already.
471
+ - `coverage` joins the maps. It keeps each address that a run executed, read or wrote. It also keeps each address that one run wrote and executed: self-modified code.
472
+ - `coverage` puts each map into the space of `--space`. You can add the same map to each space.
473
+ - `--evidence coverage:<map>` names a map by its path at the import. It refers to the last import of that map into the space of the write.
474
+
475
+ ### `apply <file>`
476
+
477
+ - `<file>` is a JSON list of writes. Give `-` to read the list from standard input.
478
+ - `body` holds `revision`, `writes` and `replaced`. Each item of `replaced` has the number of the `write`, its `op` and its `previous`.
479
+ - All writes become one revision. If the knowledge refuses one write, the script writes nothing, and the `error` names the write.
480
+ - Each write has `op` and the fields of the command with the same name. Each write can also have `space`. For example:
481
+
482
+ ```json
483
+ [
484
+ { "op": "rename", "address": "$2100", "name": "update_player", "kind": "routine" },
485
+ { "op": "classify", "start": "$3000", "end": "$303b", "type": "bytes", "element": 6 },
486
+ { "op": "comment", "address": "$2100", "placement": "line", "text": "Reads joystick 2." }
487
+ ]
488
+ ```
489
+
490
+ | op | Fields |
491
+ |---|---|
492
+ | `rename` | `address`, `name`, `kind` (optional) |
493
+ | `remove-symbol` | `address` |
494
+ | `classify` | `start`, `end`, `type`, `element` (optional) |
495
+ | `unclassify` | `start`, `end` |
496
+ | `comment` | `address`, `placement` (`line` or `side`), `text` |
497
+ | `uncomment` | `address`, `placement` |
498
+ | `reference`, `unreference` | `from`, `to`, `kind` |
499
+ | `module` | `start`, `file`, `title` (optional) |
500
+ | `unmodule` | `start` |
501
+ | `operand` | `address`, `select` (`lo`, `hi`, `value` or `expr`), `symbol` and `offset` (optional), or `expression` for `expr` |
502
+ | `unoperand` | `address` |
503
+ | `patch` | `start`, `end`, `switch`, `code` (empty for NOP only), `comment` (optional) |
504
+ | `unpatch` | `start`, `switch` (optional) |
505
+ | `macro` | `name`, `params` (a list or a comma list), `body`, `comment` (optional) |
506
+ | `unmacro` | `name` |
507
+ | `use` | `start`, `end`, `macro`, `args` (a list or a comma list) |
508
+ | `unuse` | `start` |
509
+
510
+ - The script checks an `operand`, `patch` or `use` write against the program of its space.
511
+ - Apply a batch from a library match or an idiom search in the same way. Read the notes of the result that wrote the batch first.
512
+
513
+ ### `revert <revision>`
514
+
515
+ - Makes the knowledge the same as at the revision. Without `--space`, all spaces revert. With `--space`, only that space reverts.
516
+ - `body` holds `revision`, `restores`, and `spaces: "all"` or `space`.
517
+ - A revert is a new revision. The history stays complete.
518
+ - A revert also gives back the coverage maps of that revision. A revert to a revision before an import removes the map and its ranges. Then `--evidence coverage:<map>` refuses the map until you import it again.
519
+
520
+ ### `merge`
521
+
522
+ - Decides each conflict that a git merge left. Of the rows of one conflict, the newest row stays. All choices become one revision.
523
+ - `body.choices` lists each choice, with the `conflict`, the row that is `kept` and the rows that are `closed`. Tell the user the choices.
524
+
525
+ ---
526
+
527
+ ## Batches of parallel agents
528
+
529
+ When more agents name the program at the same time, give each agent one range of addresses. Each agent writes one batch file and does not apply it. A batch has the form `{"owner": {"start": "$1000", "end": "$1fff"}, "ops": [...]}`. `ops` holds the writes, as for `apply`. The owner ranges must not overlap.
530
+
531
+ ### `join <batch>...`
532
+
533
+ - `join` makes one batch from two or more batch files. It writes no knowledge.
534
+ - `body` holds `writes`, `repeated`, `choices`, `out`, `bytes` and `sha256`.
535
+ - `choices` lists each place where the batches gave different values, with the value that stays.
536
+ - At an address in the owner range of a batch, the name, the comment, the operand and the module of that batch stay. At an address that no batch owns, the first batch in the command stays.
537
+ - A batch can give an address a name that `join` replaces. Then `join` also replaces that name in the operand expressions and macro arguments of that batch.
538
+ - The regions of the owner come after the regions of the other batches. Thus the regions of the owner stay where they overlap.
539
+ - A name from a batch stays when another batch removes the symbol at that address.
540
+ - When one name or one module file is at two addresses, `join` writes nothing and gives the error `conflict`. The `error` names the addresses and the batches. Correct the batches. Then do `join` again.
541
+ - The `hint` tells you to examine `choices`. `nextAction` is the `apply` command for the joined batch. Run it when the choices agree with your task.
542
+
543
+ ---
544
+
545
+ ## Workflow
546
+
547
+ Read:
548
+
549
+ 1. Run `at` for the addresses of the question.
550
+ 2. Run `history` when the current knowledge is surprising or disputed. Each row gives the `reason` and the `evidence` of the revision that wrote it.
551
+
552
+ Write:
553
+
554
+ 1. Read the current value with `at`. Keep the `revision` of the body.
555
+ 2. Decide the change. Keep observed facts apart from your conclusions.
556
+ 3. Write the change with `--reason`, and with `--expect-revision` set to the revision that you read. Give `--evidence` for each thing that the change rests on.
557
+ 4. Read the body. If it has `previous`, tell the user.
558
+
559
+ Put the program into the knowledge:
560
+
561
+ 1. For a packed program, capture the unpacked program into `.c64-re-tools/local/`. Then run `image` with its `image.json`.
562
+ 2. For a program without a packer, drive code or a cartridge bank, run `image` with the PRG.
563
+ 3. Write the coverage map of each run into `.c64-re-tools/local/`. Then run `coverage` with the maps.
564
+ 4. Run `program` to see the segments, the entry and the coverage maps.
565
+
566
+ Write knowledge only after interpretation:
326
567
 
327
568
  - a symbol name that tells what a routine or a variable does
328
569
  - a region type that the evidence supports
329
570
  - a comment that explains behavior that a name cannot show
330
571
  - a reference that the evidence supports.
331
572
 
332
- Do not store:
573
+ Do not write:
333
574
 
334
575
  - temporary investigation state
335
576
  - a guess, only because a routine needs a temporary name
336
577
  - confidence or status words in comment text.
337
578
 
338
- The script records the origin `llm`. Use `--by user` only when the user decides the change.
579
+ For runtime evidence, examine the program in the emulator. For static evidence, analyze the machine code. To make the source from the knowledge, generate the source. To name the routines that other programs share, match the program against a routine library.
580
+
581
+ ## JSON output
582
+
583
+ Each command prints one JSON envelope on one line on stdout. Notes go to stderr.
339
584
 
340
- ## Result
585
+ On success, the exit code is 0:
341
586
 
342
- For a read: the current or historical knowledge in compact JSON. Each list of a list command has `count` and `listed`. `--max-listed` limits `listed`. The default is 50. When the list is longer, `rest` tells how to get the other items. For `revisions`, give `--before` with the oldest revision in `listed`.
587
+ ```json
588
+ {"ok": true, "command": "<command>", "body": {}, "hint": "...", "nextAction": "..."}
589
+ ```
343
590
 
344
- For a write: the new revision and the current value after the write. If the write replaces a symbol, a region or an operand expression of a different origin, `previous` gives that row as it was before the write. For example, an `llm` write over a `user` name gives `previous`. Read `previous` and tell the user about the change. For `apply`, `replaced` lists each write that replaced a row. Each item has the number of the write, its `op` and its `previous`.
591
+ On failure, the exit code is 2:
345
592
 
346
- For `export`, `listing --out` and `join`: the written file as `{out, bytes, sha256}`.
593
+ ```json
594
+ {"ok": false, "command": "<command>", "code": "<code>", "error": "...", "hint": "...", "nextAction": "..."}
595
+ ```
347
596
 
348
- ## Failure and conflicts
597
+ - `body` holds the result. The section of each command gives the fields of its body.
598
+ - 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.
599
+ - `error` is one sentence. It tells what is wrong.
600
+ - `hint` tells what to do or what to examine. Read it before your next step. Some envelopes have no `hint`.
601
+ - `nextAction` is a full command line. It runs from any directory. Run it when it agrees with your task. Some envelopes have no `nextAction`.
602
+ - `code` does not change between versions. Do the action for the code:
349
603
 
350
- - `conflict`: the change contradicts current knowledge. For example, the name belongs to another address. Read that address and its history, then decide.
351
- - `stale-revision`: the knowledge changed after you read it. Read it again, then decide again.
352
- - `not-found`: there is nothing to remove, no such revision or no such file.
353
- - `conflict` with "from a git merge": git joined the knowledge of two computers, and two rows are current where the knowledge permits one row. For example, two names are at one address. Every read shows these rows in `conflicts`. Writes stop until you run `merge`. The `merge` command keeps the newest row of each conflict and closes the other rows, in one revision. Its result lists each choice. Tell the user the choices.
354
- - `invalid-database` or `unsupported-schema`: tell the user the message. Do not edit the knowledge files.
604
+ | `code` | Action |
605
+ |---|---|
606
+ | `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. |
607
+ | `invalid-input`, `not-found` | Correct the call as `hint` tells. Then run the command again. |
608
+ | `operation-failed` | Give `error` and `hint` to the user. |
609
+ | Another code | Do as `hint` tells. |
355
610
 
356
- A revert makes a new revision. The history stays complete.
611
+ These fields have the same meaning in each body:
357
612
 
358
- ## Handoffs
613
+ | Field | Meaning |
614
+ |---|---|
615
+ | `out`, `bytes`, `sha256` | A file that the command wrote: its path, its size and its SHA-256. |
616
+ | `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. |
359
617
 
360
- - Runtime evidence → emulator
361
- - Static evidence → c64-static-analysis
362
- - Source from the knowledge → c64-source
363
- - Names of routines that other programs share → c64-provenance
618
+ 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.
619
+ To get the subjects and the flags of one command, run `<command> --help`. Then `body.subjects` and `body.options` give them.