The README had grown to 305 lines while features kept getting appended to it: a
35-line frame dump, a full option table, and three paragraphs of essays about
parity hardware. Split by what a reader needs first:
README.md intro + instruction table + quick start + I/O modes + live
view (trimmed sample) + geometry + 7E1 in five lines +
build/test 305 -> 181 lines
docs/serial.md bit 7 not owned by I/O, why characters are not space
separated, the frame layout, why a stop bit is a bit in the
cell, and the "parity costs one latch" essay
Verified the commands the new quick start and test sections show against the
build: --input R8F on serial_echo (prints R8F), -n on and/or/full_adder (numbers),
--dump on wave (259 cells), --7e1 with input A.
3.1 KiB
串行 I/O 与 7E1 帧
契约(默认模式、选项、--input 的两种含义)在 README.md 的「I/O 模式」;
这里放细节和"为什么"。
1. 字符占低 7 位,高位不归 I/O 管
串行模式下一个 cell 的低 7 位(bit 0..6)是字符,bit 7 以及更高的位不归 I/O 管:
INP 只改低 7 位,把原来的高半个 cell 原样留着。所以 tape 可以拿 bit 7 当标志位、长度位或者
别的什么,而读写字符时它不会被冲掉。想要 char 以外的数据,就放在那上面。
验证方法:把 cell 预置成 0x80 再读一个 A,cell 会是 0xC1(-+ 在 8 位下就是把 bit0
的 1 转到 bit7,然后 ,. 读一个字符再打出来,--dump 能看到)。
2. 字符流不加空格
串行模式下字符是连着显示的(字符流不该被空格拆开),数字模式(-n)才用空格分隔。
画面里长这样:
| in (3) R8F |
| out (3) R8F |
output> 'R' 82
output> '8' 56
output> 'F' 70
--input 喂进去的值也会进 in 那一行,所以脚本跑完跟现场敲的一样能看出输入输出怎么交错。
不可打印的字符(以及 0)在画面里写成 \x0A 这种转义,免得把帧撑坏;真正写到 stdout 时
当然还是原字节。因为字符输出不带换行,程序跑完后 CLI 会补一个换行,免得提示符粘在后面。
3. 7E1:7 数据位 + 偶校验 + 1 停止位
--7e1 = --serial --parity even --stop,一个 cell 装一整帧:
| bit | 8 | 7 | 6..0 |
|---|---|---|---|
| 含义 | 停止位(恒 1) | 偶校验位 | 7 位字符 |
11 位的 cell 放完这 9 位还剩 2 位给 tape 自己用。INP 负责填这两个帧位,OUT 负责查:
rot8_test --7e1 --input "A" tapes/serial_echo.rot8 # 读进来就带校验位和停止位
rot8_test --parity even --input "A" tape.rot8 # 只查/填校验位
rot8_test --stop --input "A" tape.rot8 # 只查/填停止位(要 cell >= 9)
帧不对的时候字符照样打出去(和真 UART 一样),但 stderr 上会记 parity error /
framing error,跑完汇总一句,进程退出码 4 —— 脚本可以直接用 $? 判断。
bit7 / bit8 只在对应开关打开时才归帧所有,否则还是 tape 的。
为什么"1 个停止位"是 cell 里的一个 bit
这台机器没有逐位时钟,一次 INP/OUT 就是一整帧(起始位由操作边界隐式给出),所以帧里
只剩停止位这一个显式字段。
硬件上算校验位要什么
什么都不用加。偶校验就是"1 的个数是偶数",而 ROT-8 的读头本来就一位一位地过 ring —— 每读到 一个 1 就翻转一次那个 1 位锁存(机械拨杆、继电器自锁触点、水的翻斗都行),7 位过完它上面就是 校验位。校验是同一个电路:把收到的校验位也一起喂进去,8 位过完期望它停在 0,于是连比较器 都省了 —— 只要"读头 + 一个可翻转的锁存 + 一个门"。真 UART 也是这么做的。