Serial ASCII mode: OUT writes one character, INP reads one
-s / --serial switches INP and OUT from decimal numbers to characters. The
character is the low 7 bits of the cell, so the 8th bit stays the tape's own:
INP writes bits 0..6 and leaves everything from bit 7 up untouched, which is
where a flag, a length or a counter can live without the I/O trampling it.
Checked by hand: a cell preset to 0x80 that then reads 'A' ends at 0xC1, at 8
and at 11 bits per cell.
* include/rot8/rot8_io.h, src/rot8_io.c: the mode itself, plus
rot8_io_wrote_serial() so the CLI can close the line (a serial OUT writes
no newline, that is the point); stdin is read as characters as well, and
EOF feeds 0 instead of blocking.
* src/rot8_viz.c: the frame shows characters in this mode -- the out/in
boxes list them, the console prints output> 'R' 82, unprintables become
\x0A so a frame stays one line of ASCII, and the live prompt takes the
first character typed instead of parsing a number.
* tests/vm_test.c: -s/--serial, a cell_size < 8 check (a 7 bit character
plus a flag does not fit in fewer bits), --input becomes the text itself
(one INP per character, which is why the arguments are now collected and
pushed after the whole command line is known), and one trailing newline
at the end because serial OUT has none.
* tapes/serial_echo.rot8: ,.,.,. -- read three characters, echo them.
* README: what the mode does, the bit 7 rule, and three ways to run it.
Decimal mode is unchanged: --input 7,5 still prints 7 and the existing demos
still print their numbers.
This commit is contained in:
1 parent
c298bdca3b
commit
593e1c3b70
6 files changed
+174
-14
No files matched your search
@@ -195,6 +195,44 @@ output> 65
|
||||
stdin 已经关掉(管道读完)时喂 0 并照常跑完,不会卡住. `tapes/echo.rot8`(内容就是 `,.`)是
|
||||
拿来试这个的最小例子.
|
||||
|
||||
## 串行 I/O:7 位 ASCII 模式
|
||||
|
||||
`-s` / `--serial` 把 `INP`/`OUT` 从"十进制整数"切成"一个字符":
|
||||
|
||||
| | 十进制(默认) | 串行(`-s`) |
|
||||
|---|---|---|
|
||||
| `OUT` | 整格的值 + 换行 | **低 7 位**当字符,直接 `putchar`,不加换行 |
|
||||
| `INP` | 读一个十进制数,整格覆盖 | 读**一个字符**,只写低 7 位 |
|
||||
| `--input` | 数字列表 `7,5` | **字符串**,一个字符一个 `INP` |
|
||||
|
||||
**第 8 位(bit 7)以及更高的位不归 I/O 管**:`INP` 只改低 7 位,把原来的高半个 cell 原样留着 ——
|
||||
所以 tape 可以拿 bit 7 当标志位、长度位或者别的什么,而读写字符时它不会被冲掉. 想要 char
|
||||
以外的数据,就放在那上面.
|
||||
|
||||
要求 **cell 至少 8 位**;不够会直接报错退出(exit 2),因为 7 位字符加 1 位标志才勉强放得下:
|
||||
|
||||
```sh
|
||||
rot8_test --serial --input "R8F" tapes/serial_echo.rot8 # 打 R8F
|
||||
echo A | rot8_test -s tapes/echo.rot8 # 管道里喂一个字符
|
||||
rot8_test -s --live --input "hi" tapes/serial_echo.rot8 # 画面里也显示成字符
|
||||
```
|
||||
|
||||
`tapes/serial_echo.rot8` 就是 `,.,.,.`(读三个字符、原样打三个字符). 画面里在串行模式下会这样显示:
|
||||
|
||||
```
|
||||
| in (3) R 8 F |
|
||||
| out (3) R 8 F |
|
||||
output> 'R' 82
|
||||
output> '8' 56
|
||||
output> 'F' 70
|
||||
```
|
||||
|
||||
不可打印的字符(以及 0)在画面里写成 `\x0A` 这种转义,免得把帧撑坏;真正写到 stdout 时当然
|
||||
还是原字节. 因为字符输出不带换行,程序跑完后 CLI 会补一个换行,免得提示符粘在后面.
|
||||
|
||||
想验证 bit 7 真的没被动过:把 cell 预置成 `0x80` 再读一个 `A`,cell 会是 `0xC1`
|
||||
(`-+` 在 8 位下就是把 bit0 的 1 转到 bit7,然后 `,.` 读一个字符再打出来,`--dump` 能看到).
|
||||
|
||||
## 构建与测试
|
||||
|
||||
### 构建
|
||||
|
||||
@@ -10,6 +10,21 @@ void rot8_inp_io(rot8_iter *it);
|
||||
void rot8_io_set_quiet(bool quiet);
|
||||
bool rot8_io_quiet(void);
|
||||
|
||||
/* Serial ASCII mode. With it on (and at least 8 bits per cell) OUT writes the
|
||||
* low ROT8_SERIAL_BITS bits of the cell as one character, and INP reads one
|
||||
* character into those same bits, leaving bit 7 and everything above it
|
||||
* untouched -- so a tape can keep a flag, a length or a counter up there
|
||||
* without the I/O noticing. Below 8 bits per cell there is no room for a
|
||||
* character: leave the mode off. */
|
||||
#define ROT8_SERIAL_BITS 7
|
||||
#define ROT8_SERIAL_MASK ((rot8_data)0x7F)
|
||||
|
||||
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);
|
||||
|
||||
/* 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. */
|
||||
|
||||
+31
-2
@@ -7,6 +7,8 @@
|
||||
#include "rot8/rot8.h"
|
||||
|
||||
static bool io_quiet = false;
|
||||
static bool io_serial = false;
|
||||
static bool io_wrote = false;
|
||||
|
||||
/* 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
|
||||
@@ -23,6 +25,12 @@ void rot8_io_set_quiet(bool quiet) { io_quiet = quiet; }
|
||||
|
||||
bool rot8_io_quiet(void) { return (io_quiet); }
|
||||
|
||||
void rot8_io_set_serial(bool serial) { io_serial = serial; }
|
||||
|
||||
bool rot8_io_serial(void) { return (io_serial); }
|
||||
|
||||
bool rot8_io_wrote_serial(void) { return (io_wrote); }
|
||||
|
||||
void rot8_io_set_input_hook(rot8_input_fn fn, void *ctx) {
|
||||
input_hook = fn;
|
||||
input_hook_ctx = ctx;
|
||||
@@ -42,10 +50,19 @@ size_t rot8_io_pending_input(void) {
|
||||
}
|
||||
|
||||
void rot8_out_io(rot8_iter *it) {
|
||||
rot8_data value = rot8_out(it);
|
||||
// putchar(out(it));
|
||||
if (!io_quiet) {
|
||||
printf("%lu\n", (unsigned long)rot8_out(it));
|
||||
if (io_quiet) {
|
||||
return;
|
||||
}
|
||||
if (io_serial) {
|
||||
/* one character per OUT: the low 7 bits, no newline between them */
|
||||
putchar((int)(value & ROT8_SERIAL_MASK));
|
||||
fflush(stdout);
|
||||
io_wrote = true;
|
||||
return;
|
||||
}
|
||||
printf("%lu\n", (unsigned long)value);
|
||||
}
|
||||
|
||||
void rot8_inp_io(rot8_iter *it) {
|
||||
@@ -56,6 +73,11 @@ void rot8_inp_io(rot8_iter *it) {
|
||||
} else if (input_hook != NULL &&
|
||||
input_hook(it, input_hook_ctx, &value)) {
|
||||
/* the hook filled value in */
|
||||
} else if (io_serial) {
|
||||
/* the character stream *is* the input in this mode: no %ld parsing,
|
||||
* so a tape can read text. EOF reads as 0. */
|
||||
int c = getchar();
|
||||
value = (rot8_data)(c == EOF ? 0 : (c & ROT8_SERIAL_MASK));
|
||||
} else {
|
||||
long n;
|
||||
if (scanf("%ld", &n) != 1) {
|
||||
@@ -63,6 +85,13 @@ 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)));
|
||||
return;
|
||||
}
|
||||
rot8_inp(it, value);
|
||||
}
|
||||
|
||||
|
||||
+45
-9
@@ -4,6 +4,8 @@
|
||||
|
||||
#include "rot8/rot8_viz.h"
|
||||
|
||||
#include "rot8/rot8_io.h"
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <time.h>
|
||||
@@ -532,6 +534,21 @@ static void viz_grid(rot8_viz *viz, rot8_iter *iter, FILE *out, bool color) {
|
||||
}
|
||||
}
|
||||
|
||||
/* One value as it should read in serial mode: the character itself when it is
|
||||
* printable ASCII, otherwise an escape. Static buffer: callers copy or print
|
||||
* it before the next call. */
|
||||
static const char *viz_char(rot8_data value) {
|
||||
static char buf[8];
|
||||
unsigned int c = (unsigned int)(value & ROT8_SERIAL_MASK);
|
||||
if (c >= 0x20u && c < 0x7Fu) {
|
||||
buf[0] = (char)c;
|
||||
buf[1] = '\0';
|
||||
} else {
|
||||
snprintf(buf, sizeof buf, "\\x%02X", c);
|
||||
}
|
||||
return (buf);
|
||||
}
|
||||
|
||||
static void viz_outputs(rot8_viz *viz, FILE *out, bool color) {
|
||||
char buf[160];
|
||||
size_t first, i;
|
||||
@@ -550,8 +567,13 @@ static void viz_outputs(rot8_viz *viz, FILE *out, bool color) {
|
||||
n += snprintf(buf + n, sizeof buf - (size_t)n, " ..");
|
||||
}
|
||||
for (i = first; i < viz->output_count; i++) {
|
||||
n += snprintf(buf + n, sizeof buf - (size_t)n, " %u",
|
||||
(unsigned)viz->outputs[i]);
|
||||
if (rot8_io_serial()) {
|
||||
n += snprintf(buf + n, sizeof buf - (size_t)n, " %s",
|
||||
viz_char(viz->outputs[i]));
|
||||
} else {
|
||||
n += snprintf(buf + n, sizeof buf - (size_t)n, " %u",
|
||||
(unsigned)viz->outputs[i]);
|
||||
}
|
||||
}
|
||||
}
|
||||
if (n > 0 && (size_t)n > VIZ_MAX_TEXT) {
|
||||
@@ -587,8 +609,13 @@ static void viz_inputs(rot8_viz *viz, FILE *out, bool color) {
|
||||
n += snprintf(buf + n, sizeof buf - (size_t)n, " ..");
|
||||
}
|
||||
for (i = first; i < viz->input_count; i++) {
|
||||
n += snprintf(buf + n, sizeof buf - (size_t)n, " %u",
|
||||
(unsigned)viz->inputs[i]);
|
||||
if (rot8_io_serial()) {
|
||||
n += snprintf(buf + n, sizeof buf - (size_t)n, " %s",
|
||||
viz_char(viz->inputs[i]));
|
||||
} else {
|
||||
n += snprintf(buf + n, sizeof buf - (size_t)n, " %u",
|
||||
(unsigned)viz->inputs[i]);
|
||||
}
|
||||
}
|
||||
if (n > 0 && (size_t)n > VIZ_MAX_TEXT) {
|
||||
buf[VIZ_MAX_TEXT] = '\0';
|
||||
@@ -637,8 +664,12 @@ static void viz_console(rot8_viz *viz, FILE *out, bool color) {
|
||||
if (color) {
|
||||
fputs(ev->is_output ? ANSI_OUT : ANSI_IN, out);
|
||||
}
|
||||
fprintf(out, ev->is_output ? "output> %u" : "input> %u",
|
||||
(unsigned)ev->value);
|
||||
fprintf(out, "%s", ev->is_output ? "output>" : "input>");
|
||||
if (rot8_io_serial()) {
|
||||
fprintf(out, " '%s' %u", viz_char(ev->value), (unsigned)ev->value);
|
||||
} else {
|
||||
fprintf(out, " %u", (unsigned)ev->value);
|
||||
}
|
||||
if (color) {
|
||||
fputs(ANSI_RESET, out);
|
||||
}
|
||||
@@ -913,10 +944,15 @@ bool rot8_viz_ask(rot8_iter *iter, void *ctx, rot8_data *value) {
|
||||
return (false);
|
||||
}
|
||||
parsed = strtoul(line, &end, 0);
|
||||
if (end == line) {
|
||||
parsed = 0; /* nothing usable typed: feed 0 and keep going */
|
||||
if (rot8_io_serial()) {
|
||||
/* a character, not a number: take the first one typed, an empty line
|
||||
* feeds 0 (NUL) rather than hanging on nothing */
|
||||
*value = (rot8_data)((unsigned char)line[0] & ROT8_SERIAL_MASK);
|
||||
} else if (end == line) {
|
||||
*value = 0; /* nothing usable typed: feed 0 and keep going */
|
||||
} else {
|
||||
*value = (rot8_data)parsed;
|
||||
}
|
||||
*value = (rot8_data)parsed;
|
||||
viz_push_input(viz, *value);
|
||||
viz_log(viz, false, *value);
|
||||
viz->input_pending = false;
|
||||
|
||||
@@ -0,0 +1 @@
|
||||
,.,.,.
|
||||
+44
-3
@@ -40,6 +40,10 @@ static void usage(const char *program) {
|
||||
" --full always draw the full frame\n"
|
||||
" -i, --input <v...> feed INP values up front (spaces or commas)\n"
|
||||
" anything left over is asked for on the terminal\n"
|
||||
" -s, --serial serial ASCII: OUT writes the low 7 bits of the\n"
|
||||
" 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"
|
||||
" -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"
|
||||
@@ -49,10 +53,11 @@ static void usage(const char *program) {
|
||||
" %s --dump tapes/and.rot8\n"
|
||||
" %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 --mem 4096 --live tapes/wave.rot8\n"
|
||||
" %s --serial --cell 8 tapes/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, program, program, program, program);
|
||||
}
|
||||
|
||||
static bool parse_uint(const char *text, unsigned int *out) {
|
||||
@@ -99,6 +104,13 @@ static void push_input_list(const char *text) {
|
||||
}
|
||||
}
|
||||
|
||||
/* Serial mode: the argument is the text itself, one character per INP. */
|
||||
static void push_input_text(const char *text) {
|
||||
for (; *text != '\0'; text++) {
|
||||
rot8_io_push_input((rot8_data)((unsigned char)*text & ROT8_SERIAL_MASK));
|
||||
}
|
||||
}
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
const char *program = base_name(argv[0]);
|
||||
const char *path = NULL;
|
||||
@@ -107,6 +119,12 @@ int main(int argc, char **argv) {
|
||||
char ins[INS_SIZE];
|
||||
size_t length;
|
||||
rot8_viz *viz;
|
||||
/* --input is collected and only pushed once the whole command line is
|
||||
* known: in serial mode the same argument means characters, not numbers,
|
||||
* and --serial may come after it */
|
||||
const char *input_texts[64];
|
||||
int input_text_count = 0;
|
||||
bool serial = false;
|
||||
|
||||
rot8_config_default(&cfg);
|
||||
memset(&opts, 0, sizeof opts);
|
||||
@@ -126,7 +144,13 @@ int main(int argc, char **argv) {
|
||||
fprintf(stderr, "%s: --input needs a value or a list\n", program);
|
||||
return (2);
|
||||
}
|
||||
push_input_list(argv[++i]);
|
||||
if (input_text_count >= (int)(sizeof input_texts / sizeof input_texts[0])) {
|
||||
fprintf(stderr, "%s: too many --input arguments\n", program);
|
||||
return (2);
|
||||
}
|
||||
input_texts[input_text_count++] = argv[++i];
|
||||
} else if (strcmp(arg, "-s") == 0 || strcmp(arg, "--serial") == 0) {
|
||||
serial = true;
|
||||
} else if (strcmp(arg, "-l") == 0 || strcmp(arg, "--live") == 0) {
|
||||
opts.live = true;
|
||||
} else if (strcmp(arg, "-d") == 0 || strcmp(arg, "--dump") == 0) {
|
||||
@@ -197,6 +221,20 @@ int main(int argc, char **argv) {
|
||||
program, cfg.mem_size, cfg.cell_size);
|
||||
return (2);
|
||||
}
|
||||
if (serial && cfg.cell_size < 8) {
|
||||
fprintf(stderr,
|
||||
"%s: serial I/O needs at least 8 bits per cell (got %u)\n",
|
||||
program, cfg.cell_size);
|
||||
return (2);
|
||||
}
|
||||
rot8_io_set_serial(serial);
|
||||
for (int i = 0; i < input_text_count; i++) {
|
||||
if (serial) {
|
||||
push_input_text(input_texts[i]);
|
||||
} else {
|
||||
push_input_list(input_texts[i]);
|
||||
}
|
||||
}
|
||||
|
||||
if (!read_tape(path, ins, sizeof ins)) {
|
||||
fprintf(stderr, "%s: cannot read tape '%s'\n", program, path);
|
||||
@@ -206,6 +244,9 @@ int main(int argc, char **argv) {
|
||||
|
||||
if (!opts.live && !opts.final_dump) {
|
||||
rot8_run(ins, (rot8_ins_size)length);
|
||||
if (serial && rot8_io_wrote_serial()) {
|
||||
putchar('\n'); /* serial OUT writes no newline; end the line here */
|
||||
}
|
||||
return (0);
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user