Files
dsh 5c414292d2 calc: a terminal front end -- echo, backspace editing and a prompt
The calculator now behaves like the terminal tape from the previous commit:
every character is echoed, backspace edits the digit window, Enter commits the
line and prints the next prompt, and '.' puts its result on its own line:

    > 12 34 + .
    0046
    > 12<BS>3 .
    0013
    > ^D

It is not the terminal's line buffer moved over: the calculator consumes one
character at a time, and replaying a buffered line would mean unrolling the
engine once per buffer cell, which does not fit.  Line editing here is the
digit window: backspace shifts it right by one, pads a '0' in front, echoes
"space, backspace" and keeps accumulating.

Three things had to be learned the hard way, all recorded in docs/calc.md ?8:

* a borrowed constant cell gets eaten.  KBS (the 8 that backspace subtracts)
  lived in SC+5, and the operator step erases SC..SC+6 wholesale, so the first
  byte after an operator was treated as a backspace -- every character of
  "12 34 + ." came out as an erase.  It is now loaded right before each use.
  It also must not borrow R: the EOF test writes R every round.
* set_1 only forces the low bit; cells lent to the classifier have to be
  re-initialised each round rather than assumed clean.
* the prompt after Enter has to be printed before the command dispatch and the
  operators run, because EH (the "this was Enter" flag) borrows SC+3, which
  mul uses as scratch -- the first version printed a prompt after every
  multiplication.

The tape went 64983 -> 73624 characters (+8.6k: three copies for the window
shift alone are ~4.4k, the two subtracts ~3k).  That is past the old
ROT-8-viz INS_SIZE of 65536; the current VM sizes its buffer from the tape and
runs 112802 characters at 16 bits and 336304 at 32 bits, so only the
historical budget is gone, not the ability to run it.  Ducking under 65536
again would mean dropping line editing: echo plus the Enter prompt alone is
about +2.5k, still over.

Verified: ctest 8/8 (examples_test checks the new header stream), the width
matrix at 11, 16 and 32 bits (calc stream, terminal tape and the viz's serial
output all agree; 73624 / 112802 / 336304 characters), and docs/calc.md records
step counts for the whole session list -- "12 34 + ." is 287589 steps, about
17% more than before the front end.
2026-11-19 14:27:30 +08:00

79 lines
5.5 KiB
Markdown
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# TODO
"还没做"的事都收在这一个文件里,别散在 README/AGENTS/docs 中。每条写清楚**为什么值得做**和
**从哪开始**;做完就删掉(历史在 git log 里)。`SPEC.md` §12 那张"已经确认走不通"的表是
**结论**,不是待办,别混进来。
已经做完、从这份清单里移出去的:桌上计算器(`examples/calc.r8asm`,11 位 **73624** 字符、
三方一致、位宽矩阵见 `tests/widths.ps1`)、`INP` 整格写入的语义修正、参考虚拟机支持 >64k
纸带、**有符号数三步走**(`clb_digits_signed` / `clb_divmod_signed` / 计算器的一元负号,
契约在 SPEC §5.4、实现在 §7.10,穷举在 `r8forge_byte_op_test` 的 `test_digits_signed` 2048
个值 和 `test_divmod_signed` 4202 组)、**RPN 栈**(3 格栈 + 运算符立刻算 + `.` 真 peek,
`examples/calc.r8asm` 现在是真 RPN:`1 2 + 3 4 * + .` = `0015`)、**终端纸带**(行缓冲 +
退格编辑 + 命令分派,`examples/term.r8asm` 21721 字符:`lhi` 打 3、`rab` 反着打、退格真能
删掉一个字符、`<NL>` 和 EOF 都提交)、**终端接上计算器**(回显 + 退格行编辑 + 回车提示符,
`examples/calc.r8asm` 64983 → 73624 字符,见 docs/calc.md §8;代价是超过旧的 65536)。
## 交互计算(下一步)
* **迷你 `printf`**:`clb_puts` 和 `load` 已经有了(`examples/hello.r8asm` 能打一整句),还缺
`%d` / `%c` / `%s` 的格式化 —— 那需要"格式串 + 参数表"的约定,比 puts 复杂一档。
没有间接寻址,所以格式串在汇编期长度固定(像 `clb_puts` 的 LEN),参数表要靠
"第 k 个参数"的分派树;`%d` 直接是 `clb_digits_signed`,`%c` 是一条 `out`,
`%s` 得再约定"串 + 长度"两格。**`examples/term.r8asm` 和计算器的终端外壳都做完之后,
这条的用处要重新评估**(它原本是给终端纸带打格式化输出用的)。
* **栈再深一点**:现在是 3 格(`73624` 字符,已经超过 65536 了,所以"预算"不再是障碍)。
4 格多一次 push 的 `copy`(约 500 字符)→ ~74.1k。3 格够 `1 2 + 3 4 * +` 这种两层嵌套;
真要写长表达式就得再想办法(20 格要 ~10 万字符,见 docs/calc.md §4)。
## 工具
* **纸带级的"合并重复段落"**(这条原来的标题在交接时丢了,剩下的正文原样留着):
重复清位…… 要能重算跳转偏移。配套 `r8forge_tape_diff`(比较两条纸带的 OUT 流和步数)
证明等价 —— 一次改动全库受益。
* **全家桶要不要把两个有符号包装宏收回去**:`asm/test.r8asm` 现在是"每个宏至少调一次",
但 `clb_digits_signed` / `clb_divmod_signed` 被排除在外(各要多展开一份 `clb_digits` /
`clb_divmod`,+12.1k / +5.6k,而 65536 的预算只剩 ~3.2k)。两条路:把全家桶拆成两条
纸带,或者承认 65536 只是历史口径(viz 现在按纸带长度分配缓冲;实测 16 位的 calc 是
**96052** 字符、在 viz 里照跑,见 `tests/widths.ps1`)。现在的兜底是
`examples/signed_demo.r8asm` + `byte_op_test` 的穷举。
* **`%synth --verify`**:把 `.r8forge_cache/` 里每条结果按对应表的所有输入重跑一遍,
给"缓存已入库"兜底 —— 一条坏条目会让每次构建静默用错块。
* **CI**:暂时不做。本地 `ctest --test-dir build -C Release -j 4`(8 条,12 秒)和
`tests/widths.ps1`(位宽矩阵,约 20 分钟)都能手动跑;act_runner 的镜像装不出 cmake,
先放着。**ROT-8-viz 仍然零测试**:串行默认 / `-n` / 几何 / 7E1 全靠手测 —— 哪天真要接
CI,先补它。
* **`src/r8asm.cc:154`** 有个上游留下的 `// TODO: FILL THE BLANK`(`ROT8_NODE` 宏的
children 是空的)。那是继承来的实现,先记着,别顺手改。
## 画面
* **把 259 格当帧缓冲**:串行下 `\r`(0x0D)能原地重绘,1 bit/cell 的 16×16 位图或 8×8
生命游戏都能塞进纸带,`rot8_test --live` 里直接看它动。
## 要重新量的一条
* **`copy` 的脏缓冲**:SPEC §7.7 写着"缓冲格必须干净,脏了拷贝结果是 0",但 stage 2 的探针
(缓冲 2040、源 4)里 `copy` 结果是对的、缓冲也自己归零了。要么那条结论当初量的是别的路径
(比如 `clb_digits` 里连着用的写法),要么它已经不成立 —— 写清楚之前别把两种说法都留着。
顺便:`clb_bit_mov` 的「吃源」契约也值得一起复验。
## 想清楚但没做的优化
* **`clb_divmod` 预计算 `~D`**:单轮 2019 → 973 步,估计 28.6k → 17.6k 步,纸带净 −76;
没换是因为当时指名用 `raw_sub`。细节和注意事项见 [`docs/divmod.md`](docs/divmod.md) 第 6 节。
* **`clb_divmod` 里那个 495 字符的提交 `copy`**:还是全展开的,可以想压缩的办法。
## 顺手清掉的小尾巴
* viz 的 `read_tape`(固定缓冲那版)在 `vm_test.c` 改用 `read_tape_alloc` 之后应该没人用了:
要么删掉,要么在头注释里标 deprecated。
* viz 的 `INS_SIZE` 现在只是历史常量(真正的上限是 `ROT8_MAX_TAPE` = 16 M 条指令),
README 里可以说明一句。实测证据:16 位的 `examples/calc.r8asm` 是 **96052** 字符,
`rot8_test --cell 16 --input "12 34 + ."` 照样打出 `> 00046`(`tests/widths.ps1`)。
顺带:viz 只有在 cell **≥ 8 位**时才走串行 ASCII,7 位下每个 OUT 打一行数字 ——
`tests/widths.ps1` 的 viz 断言现在按这条跳过了 7 位(那一档由 `r8forge_derive` 兜)。