@henols/c64-re-tools 2.2.0-rc.3 → 2.2.0-rc.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@henols/c64-re-tools",
3
- "version": "2.2.0-rc.3",
3
+ "version": "2.2.0-rc.4",
4
4
  "description": "Agent-friendly Commodore 64 engineering toolkit for reverse engineering, debugging, development and testing.",
5
5
  "license": "MIT",
6
6
  "author": "Henrik Olsson",
@@ -1,18 +1,18 @@
1
1
  ---
2
2
  name: c64-docs
3
- description: Use this skill to write the documentation of a C64 program. The documentation explains the flows, state machines, tables, data formats and graphics, with links to the source lines and to the assets. The script generates the reference pages from the knowledge and the source. You write the other pages by the method of this skill. A second command of the script adds the links to each name and address and checks every link. Use it after the source is identical, or to keep the documentation correct after a change to the knowledge.
3
+ description: Use this skill to write the documentation of a C64 program. The documentation explains in plain words how the program works, with links to the main parts of the source. It also tells the findings, the development of the program, and its releases and cracks for preservation. The script generates the reference pages from the knowledge and the source. You write the other pages by the method of this skill. A second command of the script makes the links and checks every link. Use it after the source is identical, or to keep the documentation correct after a change to the knowledge.
4
4
  ---
5
5
 
6
6
  # c64-docs
7
7
 
8
8
  ## Purpose
9
9
 
10
- Write documentation that tells how a program works, and that points to the code. The documentation is Markdown in the project. The entry page is `README.md` in the project folder. The other pages and the reference are in `docs/`. The documentation has two parts:
10
+ Write documentation that explains how a program works, in plain words, and that points to the main parts of the code. The documentation is Markdown in the project. The entry page is `README.md` in the project folder. The other pages and the reference are in `docs/`. The documentation has two parts:
11
11
 
12
- - **The reference.** `docs.ts generate` generates it from the knowledge, the source map of c64-source and the image. It has a page for each module. The page gives the routines, their callers and callees, the data and a call graph. It also has pages for the names, the variables, the tables, the memory map and the graphics. Do not edit these pages. The next run writes over them.
13
- - **The pages that you write.** They tell how the program works. They give the flows, the state machines, the data formats and the task of each part. Use the page types in `references/page-types.md` and the rules in `references/style.md`.
12
+ - **The pages that you write.** They explain how the program works. They give the flows, the state machines, the data formats and the task of each part. They tell the findings, the development of the program, and its releases and cracks. They link only the main parts of the code. Use the page types in `references/page-types.md` and the rules in `references/style.md`.
13
+ - **The reference.** `docs.ts generate` generates it from the knowledge, the source map of c64-source and the image. It is the lookup for each routine, table and variable. It has a page for each module, with the routines, their callers and callees, the data and a call graph. It also has pages for the tables, the memory map with the variables, and the graphics. Do not edit these pages. The next run writes over them.
14
14
 
15
- `docs.ts link` connects the two parts. In each page that you write, a name in backticks gets a link to its line in the source. The address after the name gets a link to the entry of the name in the reference. When the source changes, `docs.ts link` moves the links to the new lines.
15
+ `docs.ts link` makes the links of the pages that you write. A name and its address in backticks become one link to the line of the name in the source. When the source changes, `docs.ts link` moves the links to the new lines.
16
16
 
17
17
  This skill does not change the knowledge.
18
18
 
@@ -70,26 +70,28 @@ node <skill>/scripts/docs.ts link README.md docs --source src --check
70
70
  2. Generate the source with c64-source. Make sure that `check.identical` is `true`. The script also writes `source-map.json`.
71
71
  3. Run `docs.ts generate`. Read `index.md` of the reference. A module with no routines, or a routine with no comment, shows what the knowledge does not have yet.
72
72
  4. Make the plan of the pages. Start from the modules and the call graphs. Find the main loop, the interrupt chain, the dispatch tables, the state machines and the data formats. Each subsystem gets one page in `docs/`. The entry page `README.md` in the project folder lists the pages. Use the page types in `references/page-types.md`.
73
- 5. Write each page. Read the code and the knowledge for each fact. Run the program with c64-testing or c64-emulator when a fact needs a run. Write each name with its address, for example `` `draw_room` `$09BC` ``. Mark each fact with its evidence. Several subagents can write the pages at the same time, one page each. Give each subagent `references/page-types.md` and `references/style.md`.
74
- 6. Record each new fact in the knowledge first: a name, a comment, a region or a module. Then the source and the reference show it too. A fact only in a page is lost at the next change of the knowledge.
75
- 7. Run `docs.ts link`. Read `problems` and correct each one. Then run `docs.ts link --check`. Make sure that `result` is `PASS`.
76
- 8. When the project folder is in a workspace with other projects, find `README.md` at the root of the workspace. This page lists each program of the workspace. Add one row for the program, with a link to `<project>/README.md` and one sentence. If the page does not exist, make it. Use the workspace index in `references/page-types.md`. `docs.ts link` does not read this page, because the page is not in the project folder.
77
- 9. After each change to the knowledge, do steps 2, 3 and 7 again. The links then go to the new lines.
73
+ 5. Collect the facts about the development and the releases. Read the leftover spaces and their comments in the knowledge. If the knowledge has none, run `provenance.ts leftovers` of c64-provenance. For two or more releases, run `provenance.ts compare` of c64-provenance. Get the crack stages from c64-unpacker. These facts go into the development section, the releases section and their pages.
74
+ 6. Write each page. Read the code and the knowledge for each fact. Run the program with c64-testing or c64-emulator when a fact needs a run. Explain each part in plain words before you name the code. Link only the main parts: write `` `draw_room` `$09BC` `` for a link, and `` `draw_room` `` for each other mention. Mark each fact with its evidence. Several subagents can write the pages at the same time, one page each. Give each subagent `references/page-types.md` and `references/style.md`.
75
+ 7. Record each new fact in the knowledge first: a name, a comment, a region or a module. Then the source and the reference show it too. A fact only in a page is lost at the next change of the knowledge.
76
+ 8. Run `docs.ts link`. Read `problems` and correct each one. Then run `docs.ts link --check`. Make sure that `result` is `PASS`.
77
+ 9. When the project folder is in a workspace with other projects, find `README.md` at the root of the workspace. This page lists each program of the workspace. Add one row for the program, with a link to `<project>/README.md` and one sentence. If the page does not exist, make it. Use the workspace index in `references/page-types.md`. `docs.ts link` does not read this page, because the page is not in the project folder.
78
+ 10. After each change to the knowledge, do steps 2, 3 and 8 again. The links then go to the new lines.
78
79
 
79
80
  ## The links
80
81
 
81
- `docs.ts link` changes these forms in the pages that you write. It does not change a code block, a mermaid diagram or an HTML comment. The examples are for a page in `docs/`. In `README.md`, the links start with `src/` and `docs/reference/`.
82
+ `docs.ts link` changes these forms in the pages that you write. It does not change a code block, a mermaid diagram or an HTML comment. The examples are for a page in `docs/`. In `README.md`, the links start with `src/`.
82
83
 
83
84
  | You write | `docs.ts link` writes |
84
85
  |---|---|
85
- | `` `draw_room` `$09BC` `` | `` [`draw_room`](../src/room_draw.asm#L6) [`$09BC`](reference/modules/room_draw.md#draw_room) `` |
86
- | `` `draw_room` (`$09BC`) `` | the same links, in the brackets |
87
- | `` `draw_room` `$0AAF` `` | `$0AAF` is a place inside `draw_room`. The address gets a link to its line in the source. |
88
- | `` `room_script_vars+3` `$4E9A` `` | a name with an offset. The address must be the address of the name plus the offset. |
89
- | `` `draw_room` `` | the name gets a link to its line. |
86
+ | `` `draw_room` `$09BC` `` | `` [`draw_room` `$09BC`](../src/room_draw.asm#L6) ``: one link to the line of the name. |
87
+ | `` `draw_room` `$0AAF` `` | `$0AAF` is a place inside `draw_room`. The link goes to the line of `$0AAF`. |
88
+ | `` `room_script_vars+3` `$4E9A` `` | a name with an offset. The address must be the address of the name plus the offset. The link goes to the line of `$4E9A`. |
89
+ | `` `draw_room` `` | no link. |
90
+ | `` `scroll_moves` = `$10` `` | no link. The value is not an address. |
90
91
 
91
92
  - A name is a name of the knowledge or of the source. A word in backticks that is no name stays as it is.
92
- - In a list such as `` `a`/`b` `$1000`/`$1010` ``, each name gets its link. The addresses stay as they are.
93
+ - A name gets one link in each section. A section starts at a heading. A second link of the same name and address in the section is a `repeated-link` problem.
94
+ - A table gets no links. A name with its address in a table row is a `link-in-table` problem.
93
95
  - `docs.ts link` writes a link again when its line moves. A link that you made to another target stays as you wrote it.
94
96
  - `docs.ts link` checks every link and image: the file must exist, a `#anchor` must exist in the page, and a `#L` line must exist in the file.
95
97
 
@@ -97,10 +99,8 @@ node <skill>/scripts/docs.ts link README.md docs --source src --check
97
99
 
98
100
  - `index.md`: each file of the source with its range, title, the number of routines and the number of names.
99
101
  - `modules/<file>.md`: a call graph of the file, its data regions with their readers and writers, and an entry for each name. An entry has the comment, the callers, the table that holds it, and for a routine its calls, jumps, reads and writes.
100
- - `variables.md`: each name outside the image, with the routines that read it and write it.
101
102
  - `tables.md`: each table of addresses, jumps and words, entry by entry, with its target. An RTS table holds each target minus 1. The table gives the target.
102
- - `memory-map.md`: each region in address order.
103
- - `symbols.md`: each name in alphabetical order.
103
+ - `memory-map.md`: each region in address order. Then each variable outside the image, with its comment and the routines that read it and write it.
104
104
  - `graphics.md`: each `sprite`, `bitmap`, `charset` and `screen` region. A sprite, a bitmap or a charset shows in hires and in multicolour, because the knowledge has no graphics mode. A charset shows 16 characters in each row. A screen shows as text.
105
105
 
106
106
  ### Images and assets
@@ -144,8 +144,10 @@ Do not store:
144
144
  - `not-found` for `source-map.json`: generate the source with c64-source. Then run the script again.
145
145
  - `invalid-input` that names an old revision: the knowledge changed after the source. Generate the source again, then run `docs.ts generate`.
146
146
  - `invalid-input` that names a page in the reference directory: the page is not a page of `docs.ts generate`. Move the page out of the directory. Do not give `--out` a directory with pages that you wrote.
147
- - `unknown-name`: the page uses a name that is not in the knowledge. Record the name with c64-knowledge, or correct the page.
147
+ - `unknown-name`: the page gives an address after a name that is not in the knowledge. Record the name with c64-knowledge, or correct the page.
148
148
  - `wrong-address`: the address after the name is not the address of the name, and it is not a place inside it. The message gives the routine that holds the address. Correct the name or the address. For a value, write `` `scroll_moves` = `$10` ``. The script does not link such a mention until it is correct.
149
+ - `repeated-link`: the name has a link in the section already. Remove the address from this mention.
150
+ - `link-in-table`: a name with its address is in a table row. Remove the address in the table, and link the name in the text above the table.
149
151
  - `missing-file`, `missing-anchor` or `missing-line`: a link goes to nothing. Correct the link. An image of an asset must exist in the project.
150
152
  - `stale-reference`: the source or the reference is older than the knowledge. Do steps 2 and 3 of the workflow again.
151
153
  - `conflict`: the knowledge has conflicts from a git merge. Run `knowledge.ts merge` of c64-knowledge.
@@ -155,4 +157,6 @@ Do not store:
155
157
  - Names, comments, regions and modules → c64-knowledge
156
158
  - The source and its source map → c64-source
157
159
  - A fact that needs a run → c64-testing or c64-emulator
160
+ - Leftovers, traces of the development and the differences of releases → c64-provenance
161
+ - The stages of a crack or a packer → c64-unpacker
158
162
  - What an address of the C64 is → c64-memory-map
@@ -1,59 +1,69 @@
1
1
  # Page types
2
2
 
3
- Each page that you write is one of these types. The types come from the documentation of the Bruce Lee field run. Use the parts of a type that the program needs. Do not add a part with no content.
3
+ Each page that you write is one of these types. Use the parts of a type that the program needs. Do not add a part with no content. Write each page by the rules in `style.md`.
4
4
 
5
5
  ## 1. The entry page
6
6
 
7
7
  One entry page for the program: `README.md` in the project folder. A large subsystem can have an entry page of its own in `docs/`, for example `docs/rooms.md`. The entry page of the program has these parts, in this order:
8
8
 
9
- 1. **One paragraph.** What the program is, and which release the documentation is for.
10
- 2. **The table of pages.** One row for each page, with the link and one sentence about its content.
11
- 3. **The layers.** A mermaid diagram of the layers of the program. The layers are the data, the code that runs one time, the main loop and the interrupts. Under the diagram, a numbered list tells how the layers work together.
12
- 4. **The flow of a user.** A mermaid diagram of the steps of the player or user from start to end. An example is the order of the levels.
13
- 5. **Findings.** A table of the facts that a reader does not expect, each with its evidence. Put a bug of the original program here.
14
- 6. **The reference.** A link to `docs/reference/index.md`.
15
- 7. **Still open.** What is not known yet, and which run can find it.
16
- 8. **The releases and the rebuild.** What each release holds, for example a crack with a cracktro and a mover. Which release is the base release, the program of the knowledge. Which changes of the release to the machine state stay, and how the loader sets them. A table of the switches from `knowledge.ts switches`: the name, the kind, the default and the releases that have the switch on. How to build the default program and each release. Which scenarios compare the original with the rebuild.
9
+ 1. **One paragraph.** What the program is, who made it and when, and which release the documentation is for. Under it, one line defines the evidence marks: **Observed**, **Code** and **Conclusion**.
10
+ 2. **How the program works.** A short walk through the program in plain words, from the start to the main loop. Tell what the program does at each stage and why. Use one mermaid diagram of the layers and one of the flow of the player. Under the diagrams, a numbered list tells how the layers work together. Link only the entry of each layer, for example the cold start, the main loop and the interrupt. Use about 5 to 8 links in the full section.
11
+ 3. **The pages.** A table with one row for each page: the link and one sentence about its content. The sentences have no links to the code.
12
+ 4. **Findings.** The bugs and the facts that a reader does not expect. Write a bulleted list, not a table. Each item has a title in bold, one to three sentences, the evidence and a link to the page with the details.
13
+ 5. **The development.** How the authors made the program, as the code and the leftovers tell it. Give a summary here. When the facts fill more than a screen, put them on a development page (section 6) and link it.
14
+ 6. **Releases, cracks and preservation.** The known releases, what each crack changed, and what a preservation project must know. Give a summary here. When the facts fill more than a screen, put them on a releases page (section 7) and link it.
15
+ 7. **The rebuild.** Which release is the base release, the program of the knowledge. Which changes of the release to the machine state stay, and how the loader sets them. A table of the switches from `knowledge.ts switches`: the name, the kind, the default and the releases that have the switch on. How to build the default program and each release. Which scenarios compare the original with the rebuild.
16
+ 8. **Still open.** What is not known yet, and which run can find it.
17
+ 9. **The reference.** One line with a link to `docs/reference/index.md`.
17
18
 
18
19
  ```markdown
19
- # <Program>: <subject>
20
+ # <Program>
20
21
 
21
- <What the program is. The release. What this documentation covers.>
22
+ <What the program is. The authors and the year. The release that this documentation is for.>
22
23
 
23
- | Page | Content |
24
- |---|---|
25
- | [parts/main_loop.md](docs/parts/main_loop.md) | **The main loop.** <one sentence> |
24
+ ## How the program works
26
25
 
27
- ## 1. The layers
26
+ <Two or three short paragraphs: what happens from the start to the play, in the words of the program.>
28
27
 
29
28
  ~~~mermaid
30
29
  flowchart TB
31
- subgraph DATA["Data"]
30
+ A["Start: build the charsets"] --> B["Title screen"]
31
+ B --> C["Play: one pass of the main loop"]
32
32
  ...
33
33
  ~~~
34
34
 
35
- ## 2. The flow of a player
36
-
37
- ...
35
+ 1. **The start runs one time.** [`cold_start` `$0400`](src/cold_start.asm#L22) builds ... (**Code**)
36
+ 2. **The main loop sets the pace.** ...
38
37
 
39
- ## 3. Findings
38
+ ## The pages
40
39
 
41
- | Finding | Evidence |
40
+ | Page | Content |
42
41
  |---|---|
43
- | <fact> | Observed: <what run showed it> |
42
+ | [docs/main_loop.md](docs/main_loop.md) | **The main loop.** <one sentence> |
44
43
 
45
- ## 4. The reference
44
+ ## Findings
46
45
 
47
- [docs/reference/index.md](docs/reference/index.md)
46
+ - **<Title of the finding>.** <What happens, and why.> **Observed** in <run>. See [combat.md](docs/combat.md).
47
+
48
+ ## The development
49
+
50
+ <Summary, or a link to docs/development.md.>
51
+
52
+ ## Releases, cracks and preservation
53
+
54
+ <Summary, or a link to docs/releases.md.>
55
+
56
+ ## The rebuild
48
57
 
49
- ## 5. Still open
58
+ ...
59
+
60
+ ## Still open
50
61
 
51
62
  - <question> — <the run that can answer it>
52
63
 
53
- ## 6. The release and the rebuild
64
+ ## The reference
54
65
 
55
- - **The release.** <the file on the disk, and what the release adds to the program>
56
- - **The rebuild.** <the source, the loader and the scenarios>
66
+ [docs/reference/index.md](docs/reference/index.md)
57
67
  ```
58
68
 
59
69
  ## 2. The workspace index
@@ -74,17 +84,18 @@ Reverse-engineered C64 games. Each game has its own folder with the original dis
74
84
 
75
85
  One page for each subsystem, for example the main loop, the interrupt chain, the room generation or the sound player. It has these parts:
76
86
 
77
- 1. **The scope.** A list of what the page covers, and the sources (the source files, the assets).
78
- 2. **The conventions.** The terms of the page, for example "a pass is one run of `main_loop` `$1BC2`". Also the evidence labels. Use the conventions block of section 6.
79
- 3. **The overview.** A short text diagram or a mermaid flowchart of the calls, from the entry of the subsystem to its end.
80
- 4. **The catalogue.** A table of the data of the subsystem. Each row has the name, the address, the layout (bytes, entries, the meaning of each byte), the readers and the writers.
81
- 5. **The step-by-step walk.** For each main routine: a numbered list of what it does, in the order of the code. Each step gives the names and addresses. Add pseudo-code when a loop or a condition is not clear from the list.
82
- 6. **The state machine.** For code that changes from one state to another: a mermaid `stateDiagram-v2` and a transition table. The transition table has the state, the condition, the next state and the code that makes the change.
83
- 7. **The worked example.** One real case decoded by hand from the image, byte by byte. For example the first bytes of the map of level 1, and the screen cells that they give.
84
- 8. **Still open.**
87
+ 1. **What it does.** One paragraph in the words of the program. Tell what the subsystem does for the player and why the program needs it. Do not list the routines.
88
+ 2. **The conventions.** The terms of the page, for example "a pass is one run of the main loop". Use the conventions block of section 8.
89
+ 3. **How it works.** A small mermaid flowchart, then the explanation in prose. Put a fact that a reader does not expect into the text where it belongs. The findings of the entry page link to it. Link the entry routine of the subsystem one time. For the main routine, give a numbered list of what it does and why, in the order of the code. Name a helper routine only when the reader needs it. Add pseudo-code when a loop or a condition is not clear from the list.
90
+ 4. **The data.** Only the data that a reader needs to understand the subsystem. For each structure, give the layout: the bytes, the entries and the meaning of each byte. The reference lists every variable, so do not make a table of each variable.
91
+ 5. **The state machine.** For code that changes from one state to another: a mermaid `stateDiagram-v2` and a transition table. The transition table has the state, the condition, the next state and the name of the code that makes the change, without a link.
92
+ 6. **The worked example.** One real case decoded by hand from the image, byte by byte. For example the first bytes of the map of level 1, and the screen cells that they give.
93
+ 7. **Still open.**
85
94
 
86
95
  ```markdown
87
- ## 3. The state machine of <thing>
96
+ ## The state machine of <thing>
97
+
98
+ The slider waits until its timer is 0, then moves one step. [`update_slider` `$3F8C`](../src/sliders.asm#L40) makes each change.
88
99
 
89
100
  ~~~mermaid
90
101
  stateDiagram-v2
@@ -96,7 +107,7 @@ stateDiagram-v2
96
107
 
97
108
  | State | Condition | Next state | Code |
98
109
  |---|---|---|---|
99
- | Waiting `$00` | `move_timer` = 0 | Moving `$01` | `update_slider` `$3F8C` |
110
+ | Waiting `$00` | `move_timer` = 0 | Moving `$01` | `update_slider` |
100
111
  ```
101
112
 
102
113
  ## 4. The entity sections
@@ -104,8 +115,8 @@ stateDiagram-v2
104
115
  A program often has many items of one kind: levels, rooms, enemies, tunes. Give each item one section of the same form, in a page for a group of items. Each section has these parts:
105
116
 
106
117
  1. **The description.** What the item is, in one paragraph. Embed the image of the item when an asset or the reference has one.
107
- 2. **The data.** The table rows of the item: its entries in each table, with the values.
108
- 3. **The setup.** What the init handler of the item does, step by step.
118
+ 2. **The data.** The entries of the item in each table, with the values. Write the names without links.
119
+ 3. **The setup.** What the init handler of the item does, in a few sentences. Link the handler one time.
109
120
  4. **The behaviour.** What the frame handler of the item does: a state diagram and a transition table.
110
121
  5. **The links to other items.** The conditions in other items that change this item, for example a door that opens when another room is empty.
111
122
 
@@ -115,7 +126,7 @@ End the group page with a table of the dependencies between the items.
115
126
 
116
127
  One page for each format of data, for example a packed map, a music stream or a sprite format. It has these parts:
117
128
 
118
- 1. **Where the data is.** The table of pointers, the range of the data, and the routine that reads it.
129
+ 1. **Where the data is.** The table of pointers, the range of the data, and the routine that reads it. Link the routine one time.
119
130
  2. **The byte layout.** A table of each byte or bit field, with its meaning. For a stream: each command byte, its arguments and what it does.
120
131
  3. **The decoder.** Pseudo-code of the routine that reads the format.
121
132
  4. **A worked example.** The first bytes of one item, decoded by hand.
@@ -129,13 +140,38 @@ One page for each format of data, for example a packed map, a music stream or a
129
140
  | `$01-$7F` | The next byte, `b` times. |
130
141
  ```
131
142
 
132
- ## 6. The conventions block
143
+ ## 6. The development page
144
+
145
+ One page, `docs/development.md`, that tells how the authors made the program. The facts come from the code, the texts and the leftovers. Get the leftovers from `provenance.ts leftovers` of c64-provenance, or from the leftover spaces of the knowledge. Use the parts that the program has evidence for:
146
+
147
+ 1. **The authors and the version.** The names, the dates and the version texts in the program, for example a credit line or "REV.2".
148
+ 2. **The tools and the computer of the development.** The format of the source text in the leftovers, and the assembler. The computer that the authors used, for example an Apple II with a cross-assembler.
149
+ 3. **Other versions of the program.** Traces of a version for another computer, for example key codes of the Atari 8-bit.
150
+ 4. **Old versions in the image.** Old copies of code, tables, graphics or music. Tell what changed between the old copy and the version that runs.
151
+ 5. **The original names.** Fragments of a symbol table or source text that give the names of the authors. Tell which names of the knowledge come from them.
152
+ 6. **Unused and unfinished parts.** Code that nothing calls, texts and graphics that nothing shows, debug code and test code.
153
+ 7. **What the development tells.** A short conclusion, marked **Conclusion**, for example the order in which the authors made the parts.
154
+
155
+ For each fact, give the evidence. A leftover in another space of the knowledge has no line in the source. Write its name without its address. Then give its space and its address, for example "`old_demo_input_stream` in bank3 at `$F300`".
156
+
157
+ ## 7. The releases page
158
+
159
+ One page, `docs/releases.md`, for the releases of the program and the facts that a preservation project needs. Get the differences from `provenance.ts compare` of c64-provenance, and the crack stages from c64-unpacker. It has these parts:
160
+
161
+ 1. **The known releases.** A table with one row for each release. A row gives the label, the disk image, the file, the group, the date and the SHA-256 of the file. The file is the file of the release, for example the PRG on the disk. Write "unknown" for a value that no source gives.
162
+ 2. **The original.** Which release is nearest to the original program, and the evidence. Tell what is missing, for example the original loader or a release without a crack.
163
+ 3. **The cracks.** For each crack: the intro, the mover, the relocator, the packer, a trainer and the texts that it changed. Tell how the crack removed the copy protection.
164
+ 4. **The copy protection.** The original protection, and the parts of it that each release still has.
165
+ 5. **Traces of other people.** A release made from another crack, a memory dump of a program that ran, or tools on the disk.
166
+ 6. **The differences.** A table of the bytes where the releases differ, what each difference does, and the switch that gives it in the rebuild.
167
+
168
+ ## 8. The conventions block
133
169
 
134
170
  Put this block near the top of each subsystem page. Change the terms for the program.
135
171
 
136
172
  ```markdown
137
173
  **Conventions.**
138
174
  - Addresses are C64 addresses. Names are the names of the knowledge.
139
- - A *pass* is one run of `main_loop` `$1BC2`. A *frame* is one raster interrupt.
175
+ - A *pass* is one run of the main loop. A *frame* is one raster interrupt.
140
176
  - **Observed** marks a fact that a run showed. **Code** marks a fact from the code only. **Conclusion** marks an interpretation.
141
177
  ```
@@ -2,16 +2,31 @@
2
2
 
3
3
  These rules apply to each page that you write. `docs.ts link` checks the rules that a script can check.
4
4
 
5
- ## Names and addresses
5
+ The rules in this file are in Simplified Technical English. The pages that you write are not. Write the pages in plain English for a human reader.
6
6
 
7
- - Write each routine, table and variable with its name and its address: `` `draw_room` `$09BC` ``. `docs.ts link` gives the name a link to its line, and the address a link to its entry in the reference.
7
+ ## Explain first
8
+
9
+ - Start each section with what the part does and why. Use the words of the program: the rooms, the player, the lanterns, the score. Then name the code.
10
+ - A list of labels is not an explanation. Tell what the code does and why it does it in that way.
11
+ - Name a routine only when the reader needs it. Examples are the entry of a part, the routine that a section explains, and a routine that a fact is about. Do not name each helper that a routine calls.
12
+ - Do not copy the reference. The reference lists each routine, each caller and each table entry. A page that you write tells the story that the reference cannot tell.
13
+ - Give a number or a fact that shows a point. An example: "a pass takes 3 frames, so the game logic runs about 16 times each second".
14
+
15
+ ## Names and links
16
+
17
+ - Write a name and its address in backticks to make a link: `` `draw_room` `$09BC` ``. `docs.ts link` makes one link of the two, to the line of the name in the source.
18
+ - Write a name without its address for each other mention: `` `draw_room` ``. A name without an address gets no link.
19
+ - Link a name the first time that a section needs the reader to open the code. A name gets one link in each section. Each heading starts a section, also a heading of level 3 or lower. `docs.ts link` reports a second link as `repeated-link`.
20
+ - Put no more than about 3 links in a paragraph. Link the main parts, not each name.
21
+ - Put no links in a table. Write the names in the table without the address. Link the important names in the text above the table. `docs.ts link` reports a link in a table as `link-in-table`.
8
22
  - Use the name of the knowledge. Do not make a name that is only in the page. When a part has no good name, record a name with c64-knowledge first.
9
- - For a place inside a routine, write the name of the routine and the address of the place: `` `draw_room` `$0A04` ``. The address gets a link to its line.
23
+ - For a place inside a routine, write the name of the routine and the address of the place: `` `draw_room` `$0A04` ``. The link goes to the line of the place.
10
24
  - For a byte inside a table or a variable, write the name with an offset: `` `room_script_vars+3` `$4E9A` ``.
11
25
  - For a value, write an equals sign: `` `scroll_moves` = `$10` ``. Without the sign, `docs.ts link` reads `$10` as an address.
26
+ - For a name and an address that is not the address of the name, put a word between them: `` `irq_vector` points to `$48C1` ``.
27
+ - `docs.ts link` knows only the names of the space of the source. For a name in another space, for example a leftover, write the name without its address. Then give the space and the address: `` `old_demo_input_stream` in bank3 at `$F300` ``.
12
28
  - Write addresses with `$` and hex digits in upper case: `$09BC`. Write values in hex with `$`, or in decimal without `$`.
13
29
  - Write a range with a hyphen: `` `$4E97-$4E9B` ``.
14
- - Write the name and the address in backticks. A name without backticks gets no link.
15
30
 
16
31
  ## Evidence
17
32
 
@@ -19,23 +34,25 @@ These rules apply to each page that you write. `docs.ts link` checks the rules t
19
34
  - **Observed**: a run showed it.
20
35
  - **Code**: the code shows it, and no run showed it.
21
36
  - **Conclusion**: an interpretation of the facts.
22
- - Do not link to the files of a run. The run files are not part of the documentation. Name the run in the text when it helps a reader to do the run again.
37
+ - Put the mark at the end of the paragraph or the list item. When the facts of a paragraph have different evidence, mark each fact.
38
+ - Do not link to the files of a run in `.c64-re-tools/local/`. Name the run in the text when it helps a reader to do the run again. A scenario in `tests/` and a run in `runs/` are part of the project. You can link them.
23
39
  - Do not write a fact that you did not check in the code, the knowledge or a run. Put a question in **Still open**.
24
40
 
25
41
  ## Diagrams and images
26
42
 
27
- - Use mermaid for the diagrams: `flowchart` for calls and data flow, `stateDiagram-v2` for state machines. Write the name and the address in each node: `"draw_room $09BC"`.
28
- - A diagram gets no links. Give the links in a table or a list under the diagram.
29
- - Keep a diagram small: at most about 20 nodes. Make two diagrams before one diagram that a reader cannot read.
43
+ - Use mermaid for the diagrams: `flowchart` for calls and data flow, `stateDiagram-v2` for state machines.
44
+ - In a node, write what the step does in the words of the program. Add the name of the routine when the reader needs it: `"Draw the room (draw_room)"`.
45
+ - A diagram gets no links. Give the links in the text under the diagram.
46
+ - Keep a diagram small: at most about 12 nodes. Make two diagrams before one diagram that a reader cannot read.
30
47
  - Embed an image only from the project. It can be an image of the reference (`reference/images/`) or a PNG that a skill made. It can also be an asset that a script of the project made from a format of the program, for example `../assets/rooms/room00.png`. `docs.ts link` checks that each image exists.
31
48
  - Give each image one line of text that tells what it shows.
32
49
 
33
50
  ## Text
34
51
 
35
- - Write short sentences: one fact or one step in each sentence.
52
+ - Write plain English in short paragraphs. A sentence can join a cause and its effect.
36
53
  - Use a numbered list for steps in an order. Use a bulleted list for items without an order. Use a table for items with the same fields.
37
54
  - Use the same word for the same thing on each page. Define each term of the program one time, in the conventions block.
38
- - Write the pages for a person who knows the 6502 and the C64, but not the program.
55
+ - Write the entry page up to the findings for any reader who knows the C64 as a user. Write the other pages for a person who knows the 6502 and the C64, but not the program.
39
56
 
40
57
  ## Generated pages
41
58
 
@@ -221,8 +221,7 @@ function link(call: Call): Record<string, unknown> {
221
221
  return undefined;
222
222
  };
223
223
 
224
- // The entries of the reference: each <a id> that is a name.
225
- const entries = new Map<string, string>();
224
+ // The pages of the reference: each must be from the revision of the source.
226
225
  const referencePages = reference === undefined ? [] : markdownFiles(reference);
227
226
  for (const page of referencePages) {
228
227
  const text = files.text(page);
@@ -234,7 +233,6 @@ function link(call: Call): Record<string, unknown> {
234
233
  if (text.startsWith(GENERATED_MARK) && stamp !== null && stamp[1] !== map.revision) {
235
234
  problems.push({ page, line: 1, kind: "stale-reference", message: `${page} is from knowledge revision ${stamp[1]}, and the source from ${map.revision}. Run docs.ts generate again.` });
236
235
  }
237
- for (const match of text.matchAll(/<a id="([^"]+)"><\/a>/g)) if (addresses.has(match[1]!) && !entries.has(match[1]!)) entries.set(match[1]!, `${page}#${match[1]}`);
238
236
  }
239
237
  const inside = (path: string, directory: string | undefined) => directory !== undefined && (path === directory || path.startsWith(`${directory}/`));
240
238
  const context = {
@@ -243,7 +241,6 @@ function link(call: Call): Record<string, unknown> {
243
241
  const label = map.labelOf(name);
244
242
  return label === undefined ? undefined : `${map.pathOf(label.file)}#L${label.line}`;
245
243
  },
246
- entryOf: (name: string) => entries.get(name),
247
244
  bodyEnd,
248
245
  placeOf,
249
246
  lineOf: (address: number) => {
@@ -281,7 +278,7 @@ function link(call: Call): Record<string, unknown> {
281
278
  for (const problem of problems) byKind[problem.kind] = (byKind[problem.kind] ?? 0) + 1;
282
279
  const output: Record<string, unknown> = {
283
280
  pages: pages.length,
284
- ...(reference === undefined ? { reference: "none: no reference/ folder is in or beside the given pages. Give --reference <dir>, or run docs.ts generate for links to the entries" } : { reference }),
281
+ ...(reference === undefined ? { reference: "none: no reference/ folder is in or beside the given pages. Give --reference <dir>, or run docs.ts generate" } : { reference }),
285
282
  ...(check ? { notUpToDate: { links, pages: changedPages } } : { linked: { links, pages: changedPages } }),
286
283
  problems: { byKind, ...listed(problems, call.count("max-listed")!, (problem) => ({ at: `${problem.page}:${problem.line}`, kind: problem.kind, message: problem.message })) },
287
284
  };
@@ -292,11 +289,11 @@ const SOURCE = { kind: "path", value: "<dir>", help: "the source folder of c64-s
292
289
 
293
290
  await runScript({
294
291
  name: "docs",
295
- summary: "Generates the reference pages of a program, and links the name mentions of the documentation to the source and the reference.",
292
+ summary: "Generates the reference pages of a program, and links the names in the documentation to their lines in the source.",
296
293
  commands: [
297
294
  {
298
295
  name: "generate",
299
- summary: "Writes the reference pages: a page for each module, and pages for the names, the variables, the tables, the memory map and the graphics.",
296
+ summary: "Writes the reference pages: a page for each module, and pages for the tables, the memory map with the variables, and the graphics.",
300
297
  subjects: [{ name: "<source-dir>", help: SOURCE.help }],
301
298
  options: {
302
299
  out: { ...OPTIONS.out, output: "folder", value: "<dir>", help: "the folder for the reference pages, relative to the project folder, for example docs/reference. generate writes only there and refuses a page in it that it did not write", required: true },
@@ -310,7 +307,7 @@ await runScript({
310
307
  },
311
308
  {
312
309
  name: "link",
313
- summary: "Links the name mentions of the pages to the source and the reference, and checks every link and image. result is FAIL when a page has a problem, or with --check when a link is not up to date.",
310
+ summary: "Links each name with its address in the pages to its line in the source, and checks every link and image. result is FAIL when a page has a problem, or with --check when a link is not up to date.",
314
311
  subjects: [{ name: "<page>", help: ".md files and folders of the documentation, relative to the project folder, for example README.md docs. link reads each file and every .md file below each folder", list: true }],
315
312
  options: {
316
313
  source: { ...SOURCE, required: true },
@@ -318,7 +315,7 @@ await runScript({
318
315
  check: { kind: "flag", help: "write nothing. FAIL when a link is not up to date or a page has a problem" },
319
316
  "max-listed": OPTIONS["max-listed"],
320
317
  },
321
- notes: "A name in backticks gets a link to its line in the source. An address in backticks after the name gets a link to the entry of the name in the reference.",
318
+ notes: "A name in backticks with its address in backticks after it, for example `draw_room` `$09BC`, becomes one link to the line of the name in the source. A name without an address gets no link. A name gets one link in each section, and a table gets no links.",
322
319
  run: link,
323
320
  },
324
321
  ],
@@ -1,9 +1,12 @@
1
- // The links of a page written by a person. A name in backticks links to
2
- // its line in the source; the address after it links to the entry of the name
3
- // in the reference, or, for an address inside the routine or the data of the
4
- // name ("draw_room $0AAF", a place in draw_room), to the line of that address. A link that points into the source or the reference is
5
- // managed: docs.ts link writes it again when a line moves. Other links stay as the
6
- // person wrote them; docs.ts link only checks that their targets exist.
1
+ // The links of a page written by a person. A name in backticks with its
2
+ // address in backticks after it ("draw_room $09BC") is one link to the line of
3
+ // the name in the source, or, for an address inside the routine or the data of
4
+ // the name ("draw_room $0AAF", a place in draw_room), to the line of that
5
+ // address. A name without an address gets no link: the person gives the
6
+ // address only where a reader needs the code. A link that points into the
7
+ // source or the reference is managed: docs.ts link writes it again when a line
8
+ // moves. Other links stay as the person wrote them; docs.ts link only checks
9
+ // that their targets exist.
7
10
 
8
11
  import { posix } from "node:path";
9
12
 
@@ -16,8 +19,6 @@ export interface LinkContext {
16
19
  addressOf: (name: string) => number | undefined;
17
20
  /** The project path and line of the label of a name, as path#Lnn. */
18
21
  codeOf: (name: string) => string | undefined;
19
- /** The project path and anchor of the entry of a name, as path#name. */
20
- entryOf: (name: string) => string | undefined;
21
22
  /** The last address of the routine or data of a name: an address up to it is a place inside the name. */
22
23
  bodyEnd: (name: string) => number | undefined;
23
24
  /** The project path and line that writes an address, as path#Lnn. */
@@ -28,7 +29,7 @@ export interface LinkContext {
28
29
  managed: (path: string) => boolean;
29
30
  }
30
31
 
31
- export type ProblemKind = "unknown-name" | "wrong-address" | "missing-file" | "missing-anchor" | "missing-line" | "outside-project" | "stale-reference";
32
+ export type ProblemKind = "unknown-name" | "wrong-address" | "repeated-link" | "link-in-table" | "missing-file" | "missing-anchor" | "missing-line" | "outside-project" | "stale-reference";
32
33
 
33
34
  export interface Problem {
34
35
  page: string;
@@ -42,15 +43,13 @@ const NAME = String.raw`([A-Za-z_][A-Za-z0-9_]*)(\+(?:\$[0-9A-Fa-f]{1,4}|[0-9]+)
42
43
  const ADDRESS = String.raw`\$([0-9A-Fa-f]{2,4})(?:-\$[0-9A-Fa-f]{2,4})?`;
43
44
  const TICK = "`";
44
45
  /**
45
- * A name in backticks, linked or not, with an optional address in backticks
46
- * after it, linked or not. Groups: 1 the [ of a linked name, 2 the name, 3 its
47
- * offset, 4 the target of the name, 5 the space or bracket between them, 6 the
48
- * address text, 7 its start digits, 8 the target of the address.
46
+ * A name in backticks and an address in backticks after it, as text or as one
47
+ * link. Groups: 1 the [ of the link, 2 the name, 3 its offset, 4 the space
48
+ * between them, 5 the address text, 6 its start digits, 7 the target of the link.
49
49
  */
50
- const MENTION = new RegExp(
51
- String.raw`(\[)?${TICK}${NAME}${TICK}(?:\]\(([^)\s]*)\))?(?:(\s*\(\s*|\s+)\[?${TICK}(${ADDRESS})${TICK}(?:\]\(([^)\s]*)\))?)?`,
52
- "g",
53
- );
50
+ const MENTION = new RegExp(String.raw`(\[)?${TICK}${NAME}${TICK}(\s+)${TICK}(${ADDRESS})${TICK}(?:\]\(([^)\s]*)\))?`, "g");
51
+ const HEADING = /^#{1,6}\s/;
52
+ const TABLE_ROW = /^\s*\|/;
54
53
 
55
54
  const relative = (page: string, target: string) => {
56
55
  const [path, fragment] = target.split("#");
@@ -74,47 +73,48 @@ export function targetPath(page: string, target: string): string | undefined {
74
73
  export function linkPage(text: string, context: LinkContext): { text: string; changed: number; problems: Problem[] } {
75
74
  const problems: Problem[] = [];
76
75
  let changed = 0;
76
+ // The first link of each name and address in the section: a heading starts a new section.
77
+ let linkedAt = new Map<string, number>();
77
78
  const lines = proseLines(text).map((line, index) => {
78
79
  if (!line.prose) return line.text;
79
- return line.text.replace(MENTION, (whole: string, open: string | undefined, name: string, offset: string | undefined, nameTarget: string | undefined, space: string | undefined, addressText: string | undefined, digits: string | undefined, addressTarget: string | undefined, at: number, all: string) => {
80
- // "[`x`]" without a target is no link of ours: leave it.
81
- if (open !== undefined && nameTarget === undefined) return whole;
82
- const ownLink = (target: string | undefined) => target !== undefined && !context.managed(targetPath(context.page, target) ?? "");
80
+ if (HEADING.test(line.text)) linkedAt = new Map();
81
+ const inTable = TABLE_ROW.test(line.text);
82
+ return line.text.replace(MENTION, (whole: string, open: string | undefined, name: string, offset: string | undefined, space: string, addressText: string, digits: string, target: string | undefined) => {
83
+ // "[`x` `$10`]" without a target, or "`x` `$10`](y)" in a link of the person: no link of ours, leave it.
84
+ if ((open === undefined) !== (target === undefined)) return whole;
85
+ // A link to another target than the source is the person's own link: it stays as it is.
86
+ if (target !== undefined && !context.managed(targetPath(context.page, target) ?? "")) return whole;
83
87
  const address = context.addressOf(name);
84
88
  const report = (kind: ProblemKind, message: string) => problems.push({ page: context.page, line: index + 1, kind, message });
85
- // In a list "`a`/`b` `$1000`/`$1010`" the address belongs to another name: the name gets its link, the address stays as it is.
86
- const namePart = open === undefined ? `${TICK}${name}${offset ?? ""}${TICK}` : `[${TICK}${name}${offset ?? ""}${TICK}](${nameTarget})`;
87
- const listed = addressText !== undefined && (all[at - 1] === "/" || all[at + whole.length] === "/");
88
- const rest = listed ? whole.slice(namePart.length) : "";
89
- const claimed = addressText !== undefined;
90
- if (listed) addressText = digits = addressTarget = undefined;
89
+ const shown = `${TICK}${name}${offset ?? ""}${TICK}${space}${TICK}${addressText}${TICK}`;
91
90
  if (address === undefined) {
92
- // A word in backticks is no claim of a name; with an address after it, or with a link of ours, it is.
93
- if (claimed || (nameTarget !== undefined && !ownLink(nameTarget))) {
94
- report("unknown-name", `\`${name}\` is no name of the knowledge or the source. Apply the name to the knowledge, or correct the page.`);
95
- }
91
+ report("unknown-name", `\`${name}\` is no name of the knowledge or the source. Apply the name to the knowledge, or correct the page.`);
96
92
  return whole;
97
93
  }
98
94
  const delta = offset === undefined ? 0 : offset.startsWith("+$") ? Number.parseInt(offset.slice(2), 16) : Number.parseInt(offset.slice(1), 10);
99
- const given = digits === undefined ? undefined : Number.parseInt(digits, 16);
100
- const place = given !== undefined && given !== address + delta && given > address && given <= (context.bodyEnd(name) ?? address);
101
- if (given !== undefined && given !== address + delta && !place) {
95
+ const given = Number.parseInt(digits, 16);
96
+ const place = given !== address + delta && given > address && given <= (context.bodyEnd(name) ?? address);
97
+ if (given !== address + delta && !place) {
102
98
  const expected = `$${(address + delta).toString(16).toUpperCase().padStart(4, "0")}`;
103
- const holder = context.placeOf(given!);
104
- report("wrong-address", `\`${name}${offset ?? ""}\` is ${expected} in the knowledge, and the page gives ${addressText}${holder === undefined || holder === name ? "" : `, a place in \`${holder}\``}. Correct the name or the address.${digits!.length <= 2 ? ` For a value, write \`${name}\` = \`${addressText}\`.` : ""}`);
99
+ const holder = context.placeOf(given);
100
+ report("wrong-address", `\`${name}${offset ?? ""}\` is ${expected} in the knowledge, and the page gives ${addressText}${holder === undefined || holder === name ? "" : `, a place in \`${holder}\``}. Correct the name or the address.${digits.length <= 2 ? ` For a value, write \`${name}\` = \`${addressText}\`.` : ""}`);
105
101
  // A mention with a problem gets no link: the text stays as the person wrote it until it is correct.
106
102
  return whole;
107
103
  }
108
- const shownName = `${TICK}${name}${offset ?? ""}${TICK}`;
109
- const code = context.codeOf(name);
110
- const nameLink = ownLink(nameTarget) ? `[${shownName}](${nameTarget})` : code === undefined ? shownName : `[${shownName}](${relative(context.page, code)})`;
111
- let result = nameLink;
112
- if (addressText !== undefined) {
113
- const entry = place ? context.lineOf(given!) : context.entryOf(name);
114
- const shownAddress = `${TICK}${addressText}${TICK}`;
115
- result += space! + (ownLink(addressTarget) ? `[${shownAddress}](${addressTarget})` : entry === undefined ? shownAddress : `[${shownAddress}](${relative(context.page, entry)})`);
104
+ if (inTable) {
105
+ report("link-in-table", `\`${name}${offset ?? ""}\` ${addressText} is a link in a table. Write \`${name}\` without the address in the table, and link it in the text above the table.`);
106
+ return whole;
107
+ }
108
+ const key = `${name}${offset ?? ""} ${given}`;
109
+ const first = linkedAt.get(key);
110
+ if (first !== undefined) {
111
+ report("repeated-link", `\`${name}${offset ?? ""}\` ${addressText} has a link at line ${first} of this section already. Write \`${name}\` without the address here.`);
112
+ return whole;
116
113
  }
117
- result += rest;
114
+ linkedAt.set(key, index + 1);
115
+ // The name links to its line; a place inside it or a byte after it links to the line of that address.
116
+ const code = given === address ? context.codeOf(name) : (context.lineOf(given) ?? context.codeOf(name));
117
+ const result = code === undefined ? shown : `[${shown}](${relative(context.page, code)})`;
118
118
  if (result !== whole) changed++;
119
119
  return result;
120
120
  });
@@ -65,7 +65,7 @@ interface Entry {
65
65
  export function buildReference(input: ReferenceInput): { files: ReferenceFile[]; counts: Record<string, number> } {
66
66
  const { map, out } = input;
67
67
  const pieces = map.files.filter((file) => !NOT_PIECES.has(file));
68
- const pageOfFile = (file: string) => (file === "constants.asm" ? posix.join(out, "variables.md") : posix.join(out, "modules", `${stem(file)}.md`));
68
+ const pageOfFile = (file: string) => (file === "constants.asm" ? posix.join(out, "memory-map.md") : posix.join(out, "modules", `${stem(file)}.md`));
69
69
 
70
70
  // --- Names and their entries ------------------------------------------------
71
71
  const knowledgeNames = new Map(input.symbols.map((symbol) => [symbol.name, symbol]));
@@ -107,8 +107,12 @@ export function buildReference(input: ReferenceInput): { files: ReferenceFile[];
107
107
  const entry = entries.get(name);
108
108
  return entry === undefined ? `\`${text}\`` : `[\`${text}\`](${rel(page, entry.page)}#${name})`;
109
109
  };
110
- /** A name and its address: the name to its line, the address to its entry (the convention of every page). */
111
- const pair = (page: string, name: string, address: number) => `${codeOfName(page, name)} ${entryOfName(page, name, hex(address))}`;
110
+ /** A name and its address as one link to the line of the name (the convention of every page). */
111
+ const pair = (page: string, name: string, address: number) => {
112
+ const label = map.labelOf(name);
113
+ const shown = `\`${name}\` \`${hex(address)}\``;
114
+ return label === undefined ? shown : `[${shown}](${rel(page, map.pathOf(label.file))}#L${label.line})`;
115
+ };
112
116
  const codeOfAddress = (page: string, address: number) => {
113
117
  const line = map.lineOf(address);
114
118
  return line === undefined ? `\`${hex(address)}\`` : `[\`${hex(address)}\`](${rel(page, map.pathOf(line.file))}#L${line.line})`;
@@ -129,6 +133,13 @@ export function buildReference(input: ReferenceInput): { files: ReferenceFile[];
129
133
  return entries.has(named.name) ? entryOfName(page, named.name, shown) : codeOfName(page, named.name).replace(`\`${named.name}\``, `\`${shown}\``);
130
134
  };
131
135
  const capped = (items: string[]) => (items.length <= MAX_LISTED ? items.join(", ") : `${items.slice(0, MAX_LISTED).join(", ")} and ${items.length - MAX_LISTED} more`);
136
+ /** The routines that read or write a range, each linked to its entry. */
137
+ const ownersOf = (page: string, start: number, end: number, kind: string) =>
138
+ capped(
139
+ [...new Set(input.references.filter((reference) => reference.kind === kind && reference.to >= start && reference.to <= end).map((reference) => ownerAt(reference.from)?.name ?? hex(reference.from)))].map((name) =>
140
+ entries.has(name) ? entryOfName(page, name, name) : `\`${name}\``,
141
+ ),
142
+ );
132
143
 
133
144
  // --- The routine graph ----------------------------------------------------
134
145
  const tableReferences: GraphReference[] = [];
@@ -270,8 +281,7 @@ export function buildReference(input: ReferenceInput): { files: ReferenceFile[];
270
281
  if (regions.length > 0) {
271
282
  lines.push("", "## Data", "", "| Range | Type | Name | Read by | Written by |", "|---|---|---|---|---|");
272
283
  for (const region of regions) {
273
- const into = input.references.filter((reference) => reference.to >= region.start && reference.to <= region.end);
274
- const owners = (kind: string) => capped([...new Set(into.filter((reference) => reference.kind === kind).map((reference) => ownerAt(reference.from)?.name ?? hex(reference.from)))].map((name) => (entries.has(name) ? entryOfName(page, name, name) : `\`${name}\``)));
284
+ const owners = (kind: string) => ownersOf(page, region.start, region.end, kind);
275
285
  const name = entryAt.get(region.start);
276
286
  lines.push(`| ${codeOfAddress(page, region.start)}${region.end === region.start ? "" : `-\`${hex(region.end)}\``} | ${region.type}${region.element === undefined ? "" : ` of ${region.element}`} | ${name === undefined ? "" : codeOfName(page, name.name)} | ${owners("read")} | ${owners("write")} |`);
277
287
  }
@@ -283,18 +293,6 @@ export function buildReference(input: ReferenceInput): { files: ReferenceFile[];
283
293
  write(page, lines);
284
294
  }
285
295
 
286
- // --- Variables: the names outside the image -------------------------------
287
- {
288
- const page = posix.join(out, "variables.md");
289
- const outside = [...entries.values()].filter((entry) => map.labelOf(entry.name)?.file === "constants.asm").sort((a, b) => a.address - b.address);
290
- const inside = [...entries.values()].filter((entry) => entry.kind === "variable" && map.labelOf(entry.name)?.file !== "constants.asm").sort((a, b) => a.address - b.address);
291
- const lines = ["# Variables", "", `The names outside the image, with the routines that read and write them. Index: [the reference](${rel(page, posix.join(out, "index.md"))}).`];
292
- if (inside.length > 0) lines.push("", "Variables inside the image are on the page of their file:", "", ...inside.map((entry) => `- ${pair(page, entry.name, entry.address)}`));
293
- for (const entry of outside) lines.push(...entryText(page, entry));
294
- counts.entries += outside.length;
295
- write(page, lines);
296
- }
297
-
298
296
  // --- Tables -----------------------------------------------------------------
299
297
  {
300
298
  const page = posix.join(out, "tables.md");
@@ -358,26 +356,23 @@ export function buildReference(input: ReferenceInput): { files: ReferenceFile[];
358
356
  // --- Memory map ---------------------------------------------------------------
359
357
  {
360
358
  const page = posix.join(out, "memory-map.md");
361
- const lines = ["# Memory map", "", `Every region of the knowledge in address order. Index: [the reference](${rel(page, posix.join(out, "index.md"))}).`, "", "| Range | Bytes | Type | Name | File |", "|---|---|---|---|---|"];
359
+ const lines = ["# Memory map", "", `Every region of the knowledge in address order, then the variables outside the program. Index: [the reference](${rel(page, posix.join(out, "index.md"))}).`, "", "## Regions", "", "| Range | Bytes | Type | Name | File |", "|---|---|---|---|---|"];
362
360
  for (const region of [...input.regions].sort((a, b) => a.start - b.start)) {
363
361
  const name = entryAt.get(region.start);
364
362
  const line = map.lineOf(region.start);
365
363
  const file = line === undefined || !pieces.includes(line.file) ? "" : `[${line.file}](${rel(page, pageOfFile(line.file))})`;
366
364
  lines.push(`| ${codeOfAddress(page, region.start)}${region.end === region.start ? "" : `-\`${hex(region.end)}\``} | ${region.end - region.start + 1} | ${region.type} | ${name === undefined ? "" : pair(page, name.name, region.start)} | ${file} |`);
367
365
  }
368
- write(page, lines);
369
- }
370
-
371
- // --- Symbols --------------------------------------------------------------------
372
- {
373
- const page = posix.join(out, "symbols.md");
374
- const lines = ["# Names", "", `Every name in alphabetical order. The name links to its line in the source, the address to its entry. Index: [the reference](${rel(page, posix.join(out, "index.md"))}).`, "", "| Name | Address | Kind |", "|---|---|---|"];
375
- const names = [...new Set([...entries.keys(), ...input.symbols.map((symbol) => symbol.name)])].sort((a, b) => a.toLowerCase().localeCompare(b.toLowerCase()) || a.localeCompare(b));
376
- for (const name of names) {
377
- const entry = entries.get(name);
378
- const symbol = knowledgeNames.get(name);
379
- const address = entry?.address ?? symbol!.address;
380
- lines.push(`| ${codeOfName(page, name)} | ${entryOfName(page, name, hex(address))} | ${entry?.kind ?? symbol!.kind} |`);
366
+ // The names outside the image: zero page, the stack, work areas and the chips. The module pages link to their rows.
367
+ const outside = [...entries.values()].filter((entry) => map.labelOf(entry.name)?.file === "constants.asm").sort((a, b) => a.address - b.address);
368
+ if (outside.length > 0) {
369
+ lines.push("", "## Variables outside the program", "", "| Address | Name | Comment | Read by | Written by |", "|---|---|---|---|---|");
370
+ for (const entry of outside) {
371
+ const at = `\`${entry.spanEnd > entry.address ? range(entry.address, entry.spanEnd) : hex(entry.address)}\``;
372
+ const owners = (kind: string) => ownersOf(page, entry.address, entry.spanEnd, kind);
373
+ lines.push(`| ${at} | <a id="${entry.name}"></a>${codeOfName(page, entry.name)} | ${commentLines(entry.address).join(" ").replace(/\|/g, "\\|")} | ${owners("read")} | ${owners("write")} |`);
374
+ }
375
+ counts.entries += outside.length;
381
376
  }
382
377
  write(page, lines);
383
378
  }
@@ -388,7 +383,7 @@ export function buildReference(input: ReferenceInput): { files: ReferenceFile[];
388
383
  const lines = [
389
384
  "# Reference",
390
385
  "",
391
- `Generated from the knowledge and the source in [${map.directory}](${rel(page, map.directory)}). The other pages: [names](symbols.md), [variables](variables.md), [tables](tables.md), [memory map](memory-map.md) and [graphics](graphics.md).`,
386
+ `Generated from the knowledge and the source in [${map.directory}](${rel(page, map.directory)}). The other pages: [tables](tables.md), [memory map and variables](memory-map.md) and [graphics](graphics.md).`,
392
387
  "",
393
388
  "| File | Range | Title | Routines | Names |",
394
389
  "|---|---|---|---|---|",