The serial INP deliberately kept bits 7 and above ("they belong to the frame,
leave everything above alone"). That made a tape depend on whatever happened to
be in the cell before the read, and the two R8Forge machines do not do it: their
-i assignment replaces the whole cell. Measured consequence: examples/calc.r8asm
(a 62585 character tape, cell 11, well inside the limit) printed
> 0000 / > 0000 / > 0000
here instead of "> 0046", while r8forge_tape_run and r8forge_derive agreed on
62 32 48 48 52 54 10. The digit window and the display were not at fault:
drv/calc_disp.r8asm already printed correctly in both machines.
INP now writes `frame` (character + parity/stop if framing is on, zeros above)
and no longer ORs the old cell back in. With that:
rot8_test --cell 11 --input "12 34 + ." calc.rot8 -> > 0046
and the smoke checks are unchanged: serial R8F, -n 512 1536 512, 7E1 A. README
and docs/serial.md said bits above the character stay untouched; they now say INP
writes the whole cell, which is what every implementation does.
64 lines
3.1 KiB
Markdown
64 lines
3.1 KiB
Markdown
# 串行 I/O 与 7E1 帧
|
||
|
||
契约(默认模式、选项、`--input` 的两种含义)在 [`README.md`](../README.md) 的「I/O 模式」;
|
||
这里放细节和"为什么"。
|
||
|
||
## 1. `INP` 写整格:字符占低 7 位,其余清零
|
||
|
||
串行模式下一个 cell 的低 7 位(bit 0..6)是字符,**`INP` 写整格**:bit 7 及以上清零(开了
|
||
parity/stop 时,对应的帧位由 `INP` 填)。早先的版本是"只改低 7 位、高位原样留着",那让纸带的
|
||
行为取决于格子里的旧值 —— 同一份程序在这台机器和 R8Forge 的两台机器上就不一样了。
|
||
|
||
验证方法:把 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` 负责**查**:
|
||
|
||
```sh
|
||
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 也是这么做的。
|