From d179aed54eeaea41e67f3354bc7f4a239920104e Mon Sep 17 00:00:00 2001 From: dsh Date: Sat, 7 Nov 2026 16:01:51 +0800 Subject: [PATCH] Serial I/O: characters in the frame are not space separated A character stream reads as text, so the in/out boxes now paste the run of them together (one space after the label, none between characters): 'out (3) R8F'. Decimal mode keeps its separators, where '789' would be a different number. While looking at that: queued --input values never reached the frame at all, only values typed at the live prompt did, because the recording lived inside rot8_viz_ask. It now happens in the hook when the INP actually executes, so the queue, a typed answer and stdin all show up the same way -- and each INP is recorded exactly once (the ask path no longer logs on its own). Checked: --serial --input R8F gives 'in (3) R8F' / 'out (3) R8F'; the decimal run still gives 'in (3) 7 8 9'; live prompting still logs one line per INP. --- README.md | 10 +++++++--- src/rot8_viz.c | 23 +++++++++++++++++++---- 2 files changed, 26 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 4c8c33b..37cca97 100644 --- a/README.md +++ b/README.md @@ -217,16 +217,20 @@ stdin 已经关掉(管道读完)时喂 0 并照常跑完,不会卡住. `tapes/ec rot8_test -s --live --input "hi" tapes/serial_echo.rot8 # 画面里也显示成字符 ``` -`tapes/serial_echo.rot8` 就是 `,.,.,.`(读三个字符、原样打三个字符). 画面里在串行模式下会这样显示: +`tapes/serial_echo.rot8` 就是 `,.,.,.`(读三个字符、原样打三个字符). 串行模式下字符是**连着**显示的 +(字符流不该被空格拆开),数字模式才用空格分隔;画面里长这样: ``` -| in (3) R 8 F | -| out (3) R 8 F | +| in (3) R8F | +| out (3) R8F | output> 'R' 82 output> '8' 56 output> 'F' 70 ``` +`--input` 喂进去的值现在也会进 `in` 那一行(以前只有 live 现场提问才记),所以脚本跑完跟现场 +敲的一样能看出输入输出怎么交错. + 不可打印的字符(以及 0)在画面里写成 `\x0A` 这种转义,免得把帧撑坏;真正写到 stdout 时当然 还是原字节. 因为字符输出不带换行,程序跑完后 CLI 会补一个换行,免得提示符粘在后面. diff --git a/src/rot8_viz.c b/src/rot8_viz.c index 1ebb581..d7d6c48 100644 --- a/src/rot8_viz.c +++ b/src/rot8_viz.c @@ -568,7 +568,12 @@ static void viz_outputs(rot8_viz *viz, FILE *out, bool color) { } for (i = first; i < viz->output_count; i++) { if (rot8_io_serial()) { - n += snprintf(buf + n, sizeof buf - (size_t)n, " %s", + /* a character stream, so the characters are not separated: one + * space after the label, then the run of them as text */ + if (i == first) { + n += snprintf(buf + n, sizeof buf - (size_t)n, " "); + } + 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", @@ -610,7 +615,10 @@ static void viz_inputs(rot8_viz *viz, FILE *out, bool color) { } for (i = first; i < viz->input_count; i++) { if (rot8_io_serial()) { - n += snprintf(buf + n, sizeof buf - (size_t)n, " %s", + if (i == first) { + n += snprintf(buf + n, sizeof buf - (size_t)n, " "); + } + 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", @@ -879,6 +887,13 @@ void rot8_viz_hook(rot8_iter *iter, void *ctx) { pc = rot8_ins_offset(iter); if (pc > 0 && rot8_ins_at(iter, (size_t)pc - 1) == OUT) { viz_push_output(viz, *rot8_iter_cell(iter)); + } else if (pc > 0 && rot8_ins_at(iter, (size_t)pc - 1) == INP) { + /* record what INP just put in the cell, whoever supplied it: the + * --input queue, the live prompt or stdin. Doing it here (and not in + * rot8_viz_ask) keeps queued inputs visible in the frame too. */ + rot8_data got = *rot8_iter_cell(iter); + viz_push_input(viz, got); + viz_log(viz, false, got); } /* The interpreter leaves ins_now one past the last instruction when the @@ -953,8 +968,8 @@ bool rot8_viz_ask(rot8_iter *iter, void *ctx, rot8_data *value) { } else { *value = (rot8_data)parsed; } - viz_push_input(viz, *value); - viz_log(viz, false, *value); + /* the value is recorded by the hook when the INP actually executes, so + * queued values and typed ones show up the same way */ viz->input_pending = false; if (viz->opts.live) { viz_redraw(viz, iter, false);