7E1 framing in serial mode: bit 7 parity, bit 8 stop
--7e1 is --serial --parity even --stop. One cell carries a whole frame:
bits 0..6 the character, bit 7 the even parity bit, bit 8 the stop bit, which
leaves two bits of an 11 bit cell to the tape.
* --parity none|even|odd: INP fills bit 7 from the character it read, OUT
checks it. --stop does the same for bit 8 being 1. Both imply --serial,
and a stop bit needs cell_size >= 9 (7+1+1).
* OUT still sends the character when a frame is bad, like a UART flagging and
carrying on, but says 'parity error' / 'framing error' on stderr, prints a
summary at the end and exits 4, so a script can test the frame with \True.
* The 7E1 round trip is checked: --7e1 --input A leaves 0x141 = 321 in the
cell (0x41 data, parity 0 because 'A' has two bits set, stop 1) and OUT
accepts it; flipping one data bit gives a parity error, and outputting a
cell with bit 8 clear gives a framing error.
* README: the frame layout, the switches, why the stop bit is one cell bit
(no bit clock: one INP/OUT is a whole frame) and what parity costs in
hardware -- one latching toggle driven by the read head, and the very same
circuit checks a frame if you feed it all 8 bits and expect 0.
This commit is contained in:
1 parent
d179aed54e
commit
754f885a64
4 files changed
+167
-6
No files matched your search
@@ -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 会补一个换行,免得提示符粘在后面.
|
||||
|
||||
|
||||
@@ -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. */
|
||||
|
||||
+66
-5
@@ -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);
|
||||
|
||||
+48
-1
@@ -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 <p> 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 <n> cells on the tape (default %u, max %u)\n"
|
||||
" -b, --cell <bits> bits per cell, 1..%u (default %u)\n"
|
||||
" -h, --help show this text\n"
|
||||
@@ -54,7 +60,8 @@ static void usage(const char *program) {
|
||||
" %s --live --input 7,5 tapes/full_adder.rot8\n"
|
||||
" %s --cell 16 --dump tapes/wave.rot8\n"
|
||||
" %s --mem 4096 --live tapes/wave.rot8\n"
|
||||
" %s --serial --cell 8 tapes/echo.rot8\n",
|
||||
" %s --serial --cell 8 tapes/echo.rot8\n"
|
||||
" %s --7e1 --input R8F tapes/serial_echo.rot8\n",
|
||||
program, (unsigned)ROT8_DEFAULT_MEM_SIZE, (unsigned)ROT8_MAX_MEM_SIZE,
|
||||
(unsigned)ROT8_MAX_CELL_SIZE, (unsigned)ROT8_DEFAULT_CELL_SIZE, program,
|
||||
program, program, program, program, program);
|
||||
@@ -125,6 +132,8 @@ int main(int argc, char **argv) {
|
||||
const char *input_texts[64];
|
||||
int input_text_count = 0;
|
||||
bool serial = false;
|
||||
bool stop = false;
|
||||
rot8_parity parity = ROT8_PARITY_NONE;
|
||||
|
||||
rot8_config_default(&cfg);
|
||||
memset(&opts, 0, sizeof opts);
|
||||
@@ -151,6 +160,31 @@ int main(int argc, char **argv) {
|
||||
input_texts[input_text_count++] = argv[++i];
|
||||
} else if (strcmp(arg, "-s") == 0 || strcmp(arg, "--serial") == 0) {
|
||||
serial = true;
|
||||
} else if (strcmp(arg, "--parity") == 0) {
|
||||
const char *p = i + 1 < argc ? argv[++i] : NULL;
|
||||
if (p == NULL) {
|
||||
fprintf(stderr, "%s: --parity needs none, even or odd\n", program);
|
||||
return (2);
|
||||
}
|
||||
if (strcmp(p, "none") == 0) {
|
||||
parity = ROT8_PARITY_NONE;
|
||||
} else if (strcmp(p, "even") == 0) {
|
||||
parity = ROT8_PARITY_EVEN;
|
||||
} else if (strcmp(p, "odd") == 0) {
|
||||
parity = ROT8_PARITY_ODD;
|
||||
} else {
|
||||
fprintf(stderr, "%s: --parity wants none, even or odd, not '%s'\n",
|
||||
program, p);
|
||||
return (2);
|
||||
}
|
||||
serial = true; /* framing only exists in serial mode */
|
||||
} else if (strcmp(arg, "--stop") == 0) {
|
||||
stop = true;
|
||||
serial = true;
|
||||
} else if (strcmp(arg, "--7e1") == 0) {
|
||||
serial = true;
|
||||
parity = ROT8_PARITY_EVEN;
|
||||
stop = true;
|
||||
} else if (strcmp(arg, "-l") == 0 || strcmp(arg, "--live") == 0) {
|
||||
opts.live = true;
|
||||
} else if (strcmp(arg, "-d") == 0 || strcmp(arg, "--dump") == 0) {
|
||||
@@ -227,7 +261,15 @@ int main(int argc, char **argv) {
|
||||
program, cfg.cell_size);
|
||||
return (2);
|
||||
}
|
||||
if (stop && cfg.cell_size < 9) {
|
||||
fprintf(stderr,
|
||||
"%s: a stop bit needs at least 9 bits per cell (got %u)\n",
|
||||
program, cfg.cell_size);
|
||||
return (2);
|
||||
}
|
||||
rot8_io_set_serial(serial);
|
||||
rot8_io_set_parity(parity);
|
||||
rot8_io_set_stop(stop);
|
||||
for (int i = 0; i < input_text_count; i++) {
|
||||
if (serial) {
|
||||
push_input_text(input_texts[i]);
|
||||
@@ -247,6 +289,11 @@ int main(int argc, char **argv) {
|
||||
if (serial && rot8_io_wrote_serial()) {
|
||||
putchar('\n'); /* serial OUT writes no newline; end the line here */
|
||||
}
|
||||
if (rot8_io_errors() != 0) {
|
||||
fprintf(stderr, "%s: %lu bad serial frame(s)\n", program,
|
||||
rot8_io_errors());
|
||||
return (4);
|
||||
}
|
||||
return (0);
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user