diff --git a/README.md b/README.md index 7a885e8..3b82e95 100644 --- a/README.md +++ b/README.md @@ -36,6 +36,7 @@ mkdir build && cd build && cmake .. && make -j$(nproc) ./build/rot8_test --input R8F tapes/serial_echo.rot8 # 默认串行:读字符、原样打出来 ./build/rot8_test -n tapes/and.rot8 # 要十进制整数就加 -n ./build/rot8_test --live tapes/wave.rot8 # 一边跑一边画内存 +./build/rot8_test --live --skip-moves tapes/wave.rot8 # 移动不单独出帧,只累计 ``` ## I/O 模式:默认串行(7 位 ASCII),`-n` 切回十进制 @@ -65,7 +66,12 @@ bit 7 及更高位**不归 I/O 管**(`INP` 只改低 7 位)、字符流为 ## 可视化内存 -`--live` 一边执行一边把整条纸带画成实时视图,`--dump` 只在结束时打一张静态快照: +`--live` 一边执行一边把整条纸带画成实时视图,`--dump` 只在结束时打一张静态快照。 +连续的 `>`/`<` **合成一帧**(状态栏写 `ptr +5 cell 91`,即这次访问走了多远、落到哪一格), +加 `--skip-moves` 则移动完全不渲染、只累计。移动永不跨 `[`/`]` 合并。 +环境变量 `ROT8_VIZ_FRAMES=1` 会让运行结束时向 stderr 打一行 +`draws/steps/moves/runs` 计数,用来量"合并省了多少帧": + ```sh ./build/rot8_test --live tapes/wave.rot8 diff --git a/include/rot8/rot8_viz.h b/include/rot8/rot8_viz.h index 54a809b..3c37f4d 100644 --- a/include/rot8/rot8_viz.h +++ b/include/rot8/rot8_viz.h @@ -30,6 +30,7 @@ typedef struct rot8_viz_opts { bool live; /* redraw while the program runs */ bool final_dump; /* static snapshot once the program terminates */ bool replay_output; /* mute OUT while live, print its values at the end */ + bool skip_moves; /* never draw a pointer move, only count it */ } rot8_viz_opts; rot8_viz *rot8_viz_new(const rot8_viz_opts *opts); diff --git a/src/rot8_viz.c b/src/rot8_viz.c index 1f54302..6f987c3 100644 --- a/src/rot8_viz.c +++ b/src/rot8_viz.c @@ -82,6 +82,18 @@ struct rot8_viz { size_t event_count; size_t event_cap; rot8_iter *last_iter; /* so the prompt can redraw the frame */ + size_t last_pc; /* pc the previous hook saw: the opcode that ran is + * not pc - 1 after a jump */ + ptrdiff_t move_delta; /* net cells the pointer moved since the last + * drawn frame */ + size_t move_count; /* moves since the last drawn frame (0 = none) */ + bool frame_due; /* a scheduled frame fell inside a move run */ + /* Frame counters. Set ROT8_VIZ_FRAMES in the environment and the run prints + * draws/steps/moves/runs at the end: that is how the coalescing numbers in + * README were measured, and it is the cheapest way to check a change.*/ + size_t dbg_moves; + size_t dbg_runs; + size_t dbg_draws; }; /* ------------------------------------------------------------------ * @@ -269,8 +281,10 @@ static void viz_caption(rot8_viz *viz, FILE *out, bool color) { static void viz_status(rot8_viz *viz, rot8_iter *iter, FILE *out, bool color) { char op[8]; char buf[96]; + char moves[48]; ptrdiff_t pc = rot8_ins_offset(iter); size_t length = rot8_ins_length(iter); + int n, m; viz_line ln; if (pc >= 0 && (size_t)pc < length) { @@ -278,9 +292,29 @@ static void viz_status(rot8_viz *viz, rot8_iter *iter, FILE *out, bool color) { } else { snprintf(op, sizeof op, "--"); } - snprintf(buf, sizeof buf, "step %6lu pc %04lX/%04lX op %s", - (unsigned long)rot8_step_count(iter), (unsigned long)pc, - (unsigned long)length, op); + n = snprintf(buf, sizeof buf, "step %6lu pc %04lX/%04lX op %s", + (unsigned long)rot8_step_count(iter), (unsigned long)pc, + (unsigned long)length, op); + if (viz->opts.live && viz->move_count != 0) { + /* The frame closes a run of pointer moves: say how far the pointer went + * and where it ended up, since no frame was drawn while it walked. */ + m = snprintf(moves, sizeof moves, " ptr %+ld cell %ld", + (long)viz->move_delta, (long)rot8_mem_offset(iter)); + if (n < (int)VIZ_MAX_TEXT && n + m <= (int)VIZ_MAX_TEXT) { + memcpy(buf + n, moves, (size_t)m + 1); + } else { + /* Very long step counts or a very wide tape: keep at least the + * movement, the cell is still in the focus line and the grid. */ + m = snprintf(moves, sizeof moves, " ptr %+ld", + (long)viz->move_delta); + if (n < (int)VIZ_MAX_TEXT && n + m <= (int)VIZ_MAX_TEXT) { + memcpy(buf + n, moves, (size_t)m + 1); + } + } + } + if (strlen(buf) > VIZ_MAX_TEXT) { + buf[VIZ_MAX_TEXT] = '\0'; + } line_open(&ln, viz, out, color); line_put(&ln, buf); line_close(&ln); @@ -848,7 +882,13 @@ void rot8_viz_hook(rot8_iter *iter, void *ctx) { rot8_viz *viz = ctx; size_t length; ptrdiff_t pc; + size_t executed_pc; + rot8_bytecode executed = OUT; + rot8_bytecode next = OUT; + bool moved; + bool run_continues; bool done; + bool due; if (viz == NULL || iter == NULL) { return; @@ -863,6 +903,19 @@ void rot8_viz_hook(rot8_iter *iter, void *ctx) { } pc = rot8_ins_offset(iter); + /* The opcode that just ran sits at pc - 1 -- unless the machine jumped, + * and then it sits wherever the previous call saw the pc. Keeping that + * pc is the only way to recognise a move that is part of a run. */ + executed_pc = viz->last_pc; + viz->last_pc = (size_t)pc; + if (executed_pc < length) { + executed = rot8_ins_at(iter, executed_pc); + } + if (pc >= 0 && (size_t)pc < length) { + next = rot8_ins_at(iter, (size_t)pc); + } + moved = (executed == STP || executed == BTP); + 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) { @@ -874,25 +927,66 @@ void rot8_viz_hook(rot8_iter *iter, void *ctx) { viz_log(viz, false, got); } + if (viz->opts.live && moved) { + /* A run of STP/BTP is one visual event, not one per cell: count the + * cells here and let the frame that closes the run report them. */ + viz->move_delta += (executed == STP) ? 1 : -1; + viz->move_count++; + viz->dbg_moves++; + } + /* The interpreter leaves ins_now one past the last instruction when the * tape is over, which is the only moment the run loop stops. */ done = (size_t)pc >= length; - if (viz->opts.live && (done || rot8_step_count(iter) % viz->every == 0)) { - viz_redraw(viz, iter, done); - viz->frames++; - if (viz->auto_every && viz->frames >= FRAMES_TARGET && - viz->every < (1u << 20)) { - /* A loop that runs longer than the tape is long would otherwise - * keep the view redrawing forever: look at it in bigger steps. */ - viz->every *= 2; - viz->frames /= 2; + due = done || rot8_step_count(iter) % viz->every == 0; + if (viz->opts.live) { + run_continues = moved && !done && (next == STP || next == BTP); + if (moved && !run_continues) { + viz->dbg_runs++; } - if (!done && viz->tty && viz->wait_ms != 0) { - viz_sleep_ms(viz->wait_ms); + if (run_continues) { + /* Do not stop the animation in the middle of a walk; if a frame + * fell due here, draw it when the run ends instead. */ + if (due) { + viz->frame_due = true; + } + } else if (viz->opts.skip_moves && moved && !done) { + /* --skip-moves: the move only counts; the next non-move + * instruction shows where the pointer went. The last frame of a + * program is still drawn, even when it ends on a move. */ + if (due) { + viz->frame_due = true; + } + } else if (due || viz->frame_due) { + if (getenv("ROT8_VIZ_NODRAW") == NULL) { + viz_redraw(viz, iter, done); + } + viz->dbg_draws++; + viz->frames++; + viz->frame_due = false; + viz->move_delta = 0; + viz->move_count = 0; + if (viz->auto_every && viz->frames >= FRAMES_TARGET && + viz->every < (1u << 20)) { + /* A loop that runs longer than the tape is long would otherwise + * keep the view redrawing forever: look at it in bigger steps. */ + viz->every *= 2; + viz->frames /= 2; + } + if (!done && viz->tty && viz->wait_ms != 0) { + viz_sleep_ms(viz->wait_ms); + } } } if (done) { + if (getenv("ROT8_VIZ_FRAMES") != NULL) { + fprintf(stderr, + "viz draws=%zu steps=%zu moves=%zu runs=%zu every=%zu " + "window=%zu\n", + viz->dbg_draws, rot8_step_count(iter), viz->dbg_moves, + viz->dbg_runs, viz->every, viz->frames); + } if (viz->opts.replay_output) { viz_replay_output(viz); } diff --git a/tests/vm_test.c b/tests/vm_test.c index 6b424ef..99edbe4 100644 --- a/tests/vm_test.c +++ b/tests/vm_test.c @@ -26,6 +26,10 @@ static void usage(const char *program) { "\n" "options:\n" " -l, --live watch the tape, redrawing the memory view in place\n" + " --skip-moves never draw pointer moves (>, <), only count them:\n" + " the next non-move instruction shows where the\n" + " pointer went (off by default; a run of moves is\n" + " normally drawn as a single frame)\n" " -d, --dump print one static memory snapshot when the tape ends\n" " -e, --every draw a frame every instructions (0 = about\n" " 300 frames per run, the default)\n" @@ -58,6 +62,10 @@ static void usage(const char *program) { " -b, --cell bits per cell, 1..%u (default %u)\n" " -h, --help show this text\n" "\n" + "A run of consecutive > / < is drawn as one frame when it ends, not one\n" + "frame per instruction; its status line says \"ptr +n\" and the cell the\n" + "pointer reached. --skip-moves drops those frames entirely.\n" + "\n" "examples:\n" " %s --live tapes/full_adder.rot8\n" " %s --dump tapes/and.rot8\n" @@ -204,6 +212,8 @@ int main(int argc, char **argv) { stop = true; } else if (strcmp(arg, "-l") == 0 || strcmp(arg, "--live") == 0) { opts.live = true; + } else if (strcmp(arg, "--skip-moves") == 0) { + opts.skip_moves = true; } else if (strcmp(arg, "-d") == 0 || strcmp(arg, "--dump") == 0) { opts.final_dump = true; } else if (strcmp(arg, "-c") == 0 || strcmp(arg, "--color") == 0) {