INP read straight from stdin with scanf, which the live view cannot offer: the
view owns the terminal, and a tape that wants input either ate a number nobody
typed or blocked on a pipe.
INP now takes its value from, in order:
1. a queue filled by --input "7,5" (spaces or commas, scripts and tests)
2. a hook the live view installs: it prints 'input> ' under the frame and
reads a line, redraws on the next tick, and feeds 0 when stdin is done so
a tape behind a pipe still finishes
3. stdin, exactly as before
So nothing changes for a plain run, and --live can drive programs that need
input. tapes/echo.rot8 (just ',.') is the smallest tape that exercises it.
rot8_test --input 42 tapes/echo.rot8 -> 42
rot8_test --live tapes/echo.rot8 -> asks under the view
Tests: --input with one value, with a list, a live run fed through a pipe
(prints 5), and a live run with stdin already closed (feeds 0, terminates).
The --dump path and the untouched tapes (and, or, full_adder, wave) still work.
ROT-8
以无ALU为前提设计的1-Bit Brainfuck变体
以及用于测试该指令集的,以C实现的简单虚拟机
概述
ROT-8 ([rəʊˈteɪt]) 提供了一套简洁的指令集,使利用无ALU的各种奇葩元件(如类似差分机的机械计算机,红石电路,甚至是水槽水闸与水车)进行计算变得可行且通用
对,就是蒸汽朋克里最常见的那类东西,不过这个真的可以运行
指令集
| ROT-8指令 | 对应BF字符 | 行为 |
|---|---|---|
STP |
> |
数据指针右移 1 Cell |
BTP |
< |
数据指针左移 1 Cell |
ROR |
+ |
当前 Cell 循环右移 1 bit |
FLB |
- |
翻转当前 Cell 的 LSB (最低位) |
BIZ |
[ |
如果当前 Cell 的 LSB 为 0,跳到匹配的 ] 之后 |
RNZ |
] |
如果当前 Cell 的 LSB 不为 0,跳回匹配的 [ 之后 |
OUT |
. |
输出当前 Cell |
INP |
, |
从输入读取到当前 Cell |
方便起见,INP与OUT的实现不做定义,只要INP能改变整个Cell而OUT能读取整个Cell即可.
例如本项目以输入/输出十进制整数作为INP/OUT.
使用
rot8_test tape
可视化内存
虚拟机可以一边执行 tape,一边把整条纸带(256 个 Cell x 8 bit)画成一张实时视图:
./build/rot8_test --live tapes/wave.rot8
+--------------------------------------------------------+
| ROT-8 memory watch tapes/wave.rot8 |
+--------------------------------------------------------+
| step 500 pc 01F4/0527 op ROR |
+--------------------------------------------------------+
| cell 90/256 value 16 = 0x10 '.' |
| b7 b6 b5 b4 b3 b2 b1 b0 |
| . . . # . . . . |
+--------------------------------------------------------+
| | +0 +1 +2 +3 +4 +5 +6 +7 +8 +9 +A +B +C +D +E +F |
| 00 | 01 02 04 08 10 20 40 80 01 02 04 08 10 20 40 80 |
| 10 | 01 02 04 08 10 20 40 80 01 02 04 08 10 20 40 80 |
| 20 | 01 02 04 08 10 20 40 80 01 02 04 08 10 20 40 80 |
| 30 | 01 02 04 08 10 20 40 80 01 02 04 08 10 20 40 80 |
| 40 | 01 02 04 08 10 20 40 80 01 02 04 08 10 20 40 80 |
| 50 | 01 02 04 08 10 20 40 80 01 02 10 00 00 00 00 00 |
| | ^^ |
| 60 | 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |
| 70 | 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |
| 80 | 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |
| 90 | 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |
| A0 | 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |
| B0 | 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |
| C0 | 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |
| D0 | 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |
| E0 | 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |
| F0 | 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |
+--------------------------------------------------------+
| out (0) none yet |
+--------------------------------------------------------+
画面从上到下:
| 区域 | 内容 |
|---|---|
step / pc / op |
已经执行的指令数,程序计数器,以及 pc 处即将执行的指令 |
cell ... value |
指针所指 Cell 的编号、十进制值与十六进制值 |
b7 ... b0 |
该 Cell 的 8 位,# 为 1,. 为 0 |
| 纸带 | 每行 16 个 Cell,行首是十六进制地址,^^ 标出数据指针 |
out |
到此刻为止 OUT 输出的值(保留最近 10 个) |
参数:
| 选项 | 说明 |
|---|---|
-l, --live |
运行动画,原地刷新 |
-d, --dump |
结束时打印一张静态快照 |
-e, --every <n> |
每 n 条指令画一帧;0(默认)表示整段动画大约 300 帧 |
-f, --fps <n> |
刷新率上限,默认 30;0 表示不限速 |
-t, --tty / --no-tty |
强制当成/不当成终端(见下) |
-k, --compact / --full |
强制精简画面/完整画面 |
-c, --color / --no-color |
强制开/关 ANSI 颜色(默认跟随终端) |
-i, --input <v...> |
预先喂给 INP 的值(空格或逗号分隔) |
-h, --help |
帮助 |
不加 --live 与 --dump 时行为与以前完全一致:直接跑完,只留下 OUT 的输出.
--live 期间 OUT 会被画面接管,程序结束后再原样补打出来,所以重定向到文件依然拿到完整的输出.
输出不是终端(被管道/文件接管)时,--live 会退化成逐帧打印,可以直接 less 着看;程序也会在 stderr 上提一句.
画面高度是自动挑的:终端不足 30 行用 24 行的精简版,测不出高度时也先按 24 行来(猜你屏幕不大),--full
可以强制完整版. 用 --tty 之前那套原地重绘才生效,所以如果你的终端其实是通过管道接进来的
(网页版终端、某些 IDE 的面板,程序 _isatty 看不出来),画面会变成一帧帧往下滚 —— 加个 -t 就好了.
输入
INP 现在按这个顺序取值:先吃 --input 排好的队列,再问现场(live 模式),最后才回落到
stdin —— 所以不带 --input、也不开 --live 时行为与以前完全一样.
rot8_test --input 7,5 tapes/full_adder.rot8 # 脚本/测试:一次喂完
rot8_test --live tapes/echo.rot8 # 现场:画面下方问 input>
live 模式下如果队列空了,画面底下会出现 input> 提示,敲一个数回车就继续;stdin 已经关掉
(管道读完)时喂 0 并照常跑完,不会卡住. tapes/echo.rot8(内容就是 ,.)是拿来试这个的
最小例子.
构建与测试
构建
mkdir build
cd build
cmake ..
make -j$(nproc)
运行(测试)
./build/rot8_test tapes/and.rot8
./build/rot8_test tapes/or.rot8
./build/rot8_test tapes/full_adder.rot8
./build/rot8_test --dump tapes/full_adder.rot8 # 跑完看一眼纸带
./build/rot8_test --live tapes/wave.rot8 # 看它把纸带刷成一道波