diff --git a/README.md b/README.md index 37cca97..562593e 100644 --- a/README.md +++ b/README.md @@ -231,6 +231,35 @@ output> 'F' 70 `--input` 喂进去的值现在也会进 `in` 那一行(以前只有 live 现场提问才记),所以脚本跑完跟现场 敲的一样能看出输入输出怎么交错. +### 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 +也是这么做的. + 不可打印的字符(以及 0)在画面里写成 `\x0A` 这种转义,免得把帧撑坏;真正写到 stdout 时当然 还是原字节. 因为字符输出不带换行,程序跑完后 CLI 会补一个换行,免得提示符粘在后面. diff --git a/include/rot8/rot8_io.h b/include/rot8/rot8_io.h index c41b042..41c466f 100644 --- a/include/rot8/rot8_io.h +++ b/include/rot8/rot8_io.h @@ -19,12 +19,36 @@ bool rot8_io_quiet(void); #define ROT8_SERIAL_BITS 7 #define ROT8_SERIAL_MASK ((rot8_data)0x7F) +/* Where the frame fields sit inside a cell in serial mode: bits 0..6 are the + * character, bit 7 the parity bit, bit 8 the stop bit. An 11 bit cell holds a + * whole 7E1 frame with two bits left over. */ +#define ROT8_PARITY_BIT 7 +#define ROT8_STOP_BIT 8 + +typedef enum { + ROT8_PARITY_NONE = 0, /* bit 7 belongs to the tape */ + ROT8_PARITY_EVEN, /* 7E: bit 7 makes the number of 1s in 0..7 even */ + ROT8_PARITY_ODD /* 7O: ... odd */ +} rot8_parity; + void rot8_io_set_serial(bool serial); bool rot8_io_serial(void); /* True once OUT has actually written a character in serial mode; the CLI uses * it to close the line it just put out. */ bool rot8_io_wrote_serial(void); +/* Parity and stop bit only mean something in serial mode. With parity on, INP + * writes bit 7 from the character it read and OUT *checks* it; with stop on, + * INP sets bit 8 and OUT checks it is 1. OUT keeps sending the low 7 bits + * either way, so a bad frame is reported but still goes out (like a UART that + * flags and keeps going). */ +void rot8_io_set_parity(rot8_parity parity); +rot8_parity rot8_io_parity(void); +void rot8_io_set_stop(bool stop); +bool rot8_io_stop(void); +/* Frames whose parity or stop bit did not check out. */ +unsigned long rot8_io_errors(void); + /* INP takes its value from the queue pushed here first, then from the hook, * then from stdin. The queue is for scripts and tests (--input 1 2 3); the * hook is for the live view, which has to stop and ask the person watching. */ diff --git a/src/rot8_io.c b/src/rot8_io.c index c9e7057..d7349a0 100644 --- a/src/rot8_io.c +++ b/src/rot8_io.c @@ -9,6 +9,26 @@ static bool io_quiet = false; static bool io_serial = false; static bool io_wrote = false; +static bool io_stop = false; +static rot8_parity io_parity = ROT8_PARITY_NONE; +static unsigned long io_errors = 0; + +/* Parity of the low 7 bits: XOR them together. This is the whole "parity + * circuit" -- in a machine without an ALU it is a toggle that flips once per 1 + * bit seen, which is exactly what this fold computes. */ +static unsigned bit_parity(rot8_data value) { + unsigned p = 0; + for (unsigned v = (unsigned)(value & ROT8_SERIAL_MASK); v != 0; v >>= 1) { + p ^= v & 1u; + } + return (p); +} + +/* The parity bit a sender would append, for the configured convention. */ +static unsigned wanted_parity(rot8_data data) { + unsigned p = bit_parity(data); + return (io_parity == ROT8_PARITY_ODD ? (p ^ 1u) : p); +} /* INP values handed over before the run (scripts, tests) or asked for on the * fly (the live view). Small ring buffer: a tape that needs more than this @@ -31,6 +51,16 @@ bool rot8_io_serial(void) { return (io_serial); } bool rot8_io_wrote_serial(void) { return (io_wrote); } +void rot8_io_set_parity(rot8_parity parity) { io_parity = parity; } + +rot8_parity rot8_io_parity(void) { return (io_parity); } + +void rot8_io_set_stop(bool stop) { io_stop = stop; } + +bool rot8_io_stop(void) { return (io_stop); } + +unsigned long rot8_io_errors(void) { return (io_errors); } + void rot8_io_set_input_hook(rot8_input_fn fn, void *ctx) { input_hook = fn; input_hook_ctx = ctx; @@ -57,7 +87,27 @@ void rot8_out_io(rot8_iter *it) { } if (io_serial) { /* one character per OUT: the low 7 bits, no newline between them */ - putchar((int)(value & ROT8_SERIAL_MASK)); + rot8_data data = (rot8_data)(value & ROT8_SERIAL_MASK); + if (io_parity != ROT8_PARITY_NONE) { + unsigned got = (unsigned)((value >> ROT8_PARITY_BIT) & 1u); + unsigned want = wanted_parity(data); + if (got != want) { + io_errors++; + fprintf(stderr, + "rot8: parity error on OUT: %s parity, character %u " + "(0x%02lX) with bit 7 = %u\n", + io_parity == ROT8_PARITY_ODD ? "odd" : "even", + (unsigned)data, (unsigned long)data, got); + } + } + if (io_stop && ((value >> ROT8_STOP_BIT) & 1u) == 0) { + io_errors++; + fprintf(stderr, + "rot8: framing error on OUT: stop bit is 0, character %u " + "(0x%02lX)\n", + (unsigned)data, (unsigned long)data); + } + putchar((int)data); fflush(stdout); io_wrote = true; return; @@ -86,10 +136,21 @@ void rot8_inp_io(rot8_iter *it) { value = (rot8_data)n; } if (io_serial) { - /* bit 7 and above belong to the tape, not to the character: put the - * read value into the low 7 bits and leave the rest of the cell alone */ - rot8_data keep = (rot8_data)(rot8_out(it) & ~ROT8_SERIAL_MASK); - rot8_inp(it, (rot8_data)(keep | (value & ROT8_SERIAL_MASK))); + /* bit 7 and above belong to the frame, not to the character: put the + * read value into the low 7 bits, let the sender fill parity and stop + * if this machine is asked to, and leave everything above alone */ + rot8_data data = (rot8_data)(value & ROT8_SERIAL_MASK); + rot8_data frame = data; + rot8_data framed = ROT8_SERIAL_MASK; + if (io_parity != ROT8_PARITY_NONE) { + frame |= (rot8_data)(wanted_parity(data) << ROT8_PARITY_BIT); + framed |= (rot8_data)1u << ROT8_PARITY_BIT; + } + if (io_stop) { + frame |= (rot8_data)1u << ROT8_STOP_BIT; + framed |= (rot8_data)1u << ROT8_STOP_BIT; + } + rot8_inp(it, (rot8_data)((rot8_out(it) & ~framed) | frame)); return; } rot8_inp(it, value); diff --git a/tests/vm_test.c b/tests/vm_test.c index 83382e2..de26d04 100644 --- a/tests/vm_test.c +++ b/tests/vm_test.c @@ -44,6 +44,12 @@ static void usage(const char *program) { " cell as one character, INP reads one character\n" " into them and leaves bit 7 alone (cell >= 8 bits);\n" " --input then takes characters, not numbers\n" + " --parity
serial framing: none (default), even (7E) or odd\n"
+ " (7O); INP fills bit 7, OUT checks it\n"
+ " --stop serial framing: bit 8 is the stop bit and must be\n"
+ " 1 (INP sets it, OUT checks it); needs cell >= 9\n"
+ " --7e1 --serial --parity even --stop, i.e. a 7E1 frame in\n"
+ " one cell: 7 data bits, parity, stop, and 2 spare\n"
" -m, --mem