Cell width and tape length: 11 bits, 259 cells, by default

The assembler already took the width as an argument (import_file(path, cell_size))
and exposed _CELL_SIZE / _CELL_MAX / _ROL_STEP to programs, so every macro that
writes %rep _CELL_SIZE follows the width on its own.  What was missing was a
single place saying what the default is, and VMs that agree with it.

include/r8forge/geometry.hh now holds kDefaultCellBits = 11,
kDefaultMemoryCells = 259 and kDefaultCellMask, and the four VMs use it:

* cells are uint16_t, the tape is kDefaultMemoryCells long
* ROR is (v >> 1) | ((v & 1) << (kDefaultCellBits - 1)), not the 8 bit version
* input and output mask with kDefaultCellMask

The tests now state their expectations in terms of the width instead of a byte
(kCellMask, kBits, kCellMod).  Two things were genuinely wrong for 11 bits and
are fixed: clb_neg_* and clb_inc_* were compared against 256 - a and 0xff, and
asm/test.r8asm set B with .xor (~126)+1 -- .xor takes its constant modulo the
cell width, so that is 1922 in an 11 bit cell, not 130.

Results at 11 x 259:

    asm/test.r8asm        7262 characters, out 127 130 0 257 0 1088 19
                          (127+130 = 257, and 200*200 = 19*2048 + 1088)
    r8forge_byte_op_test  all 17 operations pass
    r8forge_synth_test    all eight combinations ok (bit level is width free)
    r8forge_macro_docs_test  41 macros in 17 files
    asm/fibonacci.r8asm   11959 characters, 13 terms now (no wrap at 256)

ROT-8-viz moved its defaults to 11 x 259 in the same change, so the tapes cross
check: both implementations print 127 130 0 257 0 1088 19 for asm/test.r8asm.
Tapes grow when the width does (raw_add 1647 -> 2199 characters), because the
%rep _CELL_SIZE loops are longer.

AGENTS.md gains a section on how the width flows through the tools, including
the trap that 2048 % 256 == 0 makes several 8 bit expectations still pass by
accident.
This commit is contained in:
dsh committed 2026-11-07 10:56:28 +08:00
1 parent cfe56e164a
commit 62c6f8979f
10 files changed
+221 -139

No files matched your search

+100 -58
View File
@@ -37,7 +37,8 @@
就是 `dsh@gitea.local`)。**agent 的提交用 dsh,不要用 `StarCells <peng080306@foxmail.com>`**
—— 那是仓库主人的身份。这条踩过:ROT-8-viz 一开始没配 user.name,我照抄了历史作者,
结果两个 repo 的提交都挂在了主人名下,后来用 `rebase --reset-author` / `filter-branch`
改回 dsh 并 force-push 才修好。
改回 dsh 并 force-push 才修好。`git commit` 报 `Author identity unknown` 就是这条配置又没了,
补 `git config user.name dsh` + `git config user.email dsh@gitea.local` 就行。
* **推代码**:`origin` = `https://gitea.starcells.ltd/dsh/R8Forge.git`。这台机器上
schannel 会报 `schannel: AcquireCredentialsHandle failed: SEC_E_NO_CREDENTIALS`,
`fetch` / `push` 都得走 openssl 后端 —— 已经写在这个 repo 的 local config 里
@@ -61,7 +62,7 @@ clang-cl /nologo /std:c++20 /EHsc /O2 /I include src/r8asm.cc src/synth.cc tests
自检:
```sh
r8forge asm/test.r8asm # 应 exit 0,5417 字符(加法 + 乘法各一段)
r8forge asm/test.r8asm # 应 exit 0,7262 字符(加法 + 乘法各一段)
r8forge_synth_test examples/full_add.r8asm # all eight combinations ok
r8forge verify_clb_bit_add_nc.r8asm # 4 cell 加法切片(进位进->出)
r8forge verify_clb_bit_inc_nc.r8asm # 3 cell 自增切片,进位同样进->出
@@ -119,30 +120,71 @@ void rot8_rnz(rot8_iter *iter) { if ((*(iter->mem_now) & 1) == 1) { ... } }
曾经这里还写过第三条("多字节 cell 的位读不出来")—— 那是把 `biz` 当成 Brainfuck 的
整格零判断推出来的,**错的**,已删。
## 字节级运算(一个 cell 一个字节)
## CELL_SIZE 与纸带长度是怎么用的
结论:**库里的字节级宏是对的,穷举全过**。`r8forge_byte_op_test` 实测(1.5 秒):
两个默认值集中在 **`include/r8forge/geometry.hh`**,现在是 **11 位 / 259 格**:
```cpp
inline constexpr size_t kDefaultCellBits = 11;
inline constexpr size_t kDefaultMemoryCells = 259;
inline constexpr uint32_t kDefaultCellMask = (1u << kDefaultCellBits) - 1u;
```
位宽和长度**都不在指令集里**(`ROR` 只是在一个 cell 里转圈,纸带是环),所以它们只以
"参数 + 常量"的形式存在,落在三处。改位宽时照这个清单走:
1. **汇编器**:`import_file(path, cell_size)` 的第二个参数就是位宽,6 个调用点都传
`kDefaultCellBits`。`set_cell_size` 顺手把三个名字注入程序作用域:
`_CELL_SIZE`(位宽)、`_CELL_MAX = (1 << 位宽) - 1`、`_ROL_STEP = 位宽 - 1`。
宏里写 `%rep _CELL_SIZE { … }` 就自动跟着位宽走(`erase`、`rol`、所有字节级驱动都这么写),
所以**位级宏、`%synth` 切片、位运算天然与位宽无关** —— 实测 `r8forge_synth_test` 在
11 位下照样 8/8 全过。
2. **四份虚拟机**(`tests/synth_test.cc`、`tests/byte_op_test.cc`、`src/derive.cc`、
`tests/tape_run.cc`):cell 存 `uint16_t`、数组长 `kDefaultMemoryCells`、
旋转必须是 `(v >> 1) | ((v & 1) << (kDefaultCellBits - 1))`、输入输出掩码用
`kDefaultCellMask`。**四份要一起改**,少改一份就会出现"两个实现不一致"的假象。
3. **测试期望**:一律用 `kCellMask` / `kBits` 写,不要出现 `0xff`、`>> 8`、`256 - a`。
**注意 2048 % 256 == 0**,所以不少 8 位期望在 11 位下碰巧还成立(and/or/xor/not/dec/sub
就是),别把"没报错"当成"不用改" —— `neg`、`add`、`inc`、`mul` 一改位宽当场就错。
踩过的坑:
* **`.xor` 的常数按当前位宽解释**。`asm/test.r8asm` 原来写 `.xor (~126)+1` 想置 130,
11 位下变成 `2048-126 = 1922`;改成直接写 `.xor 130`。
* **纸带会变长**:`%rep _CELL_SIZE` 从 8 轮变 11 轮,`raw_add` 1647 → **2199** 字符,
`fibonacci` 8761 → **11959**。写死的长度断言要跟着改。
* **`output` 打印整个 cell 的值**,不再是一个字节;`out` 里出现 257、1088 这种数是对的
(`127+130 = 257`、`200*200 = 19*2048 + 1088`)。
* **交叉验证要先对齐几何**:`ROT-8-viz` 现在默认也是 **11 位 / 259 格**(`--cell` / `--mem`
可改)。几何一致时同一条纸带输出完全相同(`asm/test.r8asm` 两边都是
`127 130 0 257 0 1088 19`);不一致时不是谁错了,是两台不同的机器。
* 位宽变大还有个正面效果:`fibonacci` 不再在 256 处回绕,现在跑出 **13 项**(到 233)。
## 字节级运算(一个 cell 一个机器字,默认 11 位)
结论:**库里的字节级宏是对的,穷举全过**。`r8forge_byte_op_test` 实测(1.5 秒)。
下面的纸带长度都是 **11 位 cell** 下的值(8 位时代的数字见 git 历史):
| 宏 | 驱动 | 纸带 | 结果 |
|---|---|---|---|
| `raw_add`(a+b,含进位) | `drv/raw_add_byte.r8asm` | 1647 字符 | **65536/65536** |
| `raw_and` | `drv/raw_and_byte.r8asm` | 639 字符 | **65536/65536** |
| `raw_or` | `drv/raw_or_byte.r8asm` | 575 字符 | **65536/65536** |
| `raw_xor` | `drv/raw_xor_byte.r8asm` | 463 字符 | **65536/65536** |
| `raw_inc`(a+进位) | `drv/raw_inc_byte.r8asm` | 1214 字符 | **512/512** |
| **`clb_add_nc`(a+b+进位,4 cell)** | `drv/raw_add_nc_byte.r8asm` | **688 字符** | **65536/65536** |
| **`clb_inc_nc`(a+进位,3 cell)** | `drv/raw_inc_nc_byte.r8asm` | **430 字符** | **256/256** |
| **`clb_neg_nc`(~a+进位,3 cell)** | `drv/raw_neg_nc_byte.r8asm` | **448 字符** | **256/256**(取负) |
| 同上,进位 0 | `drv/raw_not_nc_byte.r8asm` | **447 字符** | **256/256**(反码 `~a`) |
| **`clb_sub_nc`(a-b,4 cell)** | `drv/raw_sub_nc_byte.r8asm` | **707 字符** | **65536/65536** |
| **`clb_not_ip`(~a,原地,1 cell)** | `drv/not_ip_byte.r8asm` | **17 字符** | **256/256** |
| **`clb_inc_ip`(a+1,原地,2 cell)** | `drv/inc_ip_byte.r8asm` | **74 字符** | **256/256** |
| **`clb_neg_ip`(-a,原地,2 cell)** | `drv/neg_ip_byte.r8asm` | **90 字符** | **256/256** |
| **`clb_dec_ip`(a-1,原地,2 cell)** | `drv/dec_ip_byte.r8asm` | **106 字符** | **256/256** |
| **`clb_nonzero`(a!=0 的标志,原地)** | `drv/nonzero_byte.r8asm` | **209 字符** | **256/256** |
| **`clb_add_ip`(a+=b,原地,4 cell)** | `drv/add_ip_byte.r8asm` | **558 字符** | **2048 抽样全过**(O(b) 运行) |
| **`clb_mul`(mc×mul → 16 位积,10 cell)** | `drv/mul_byte.r8asm` | **3729 字符** | **64 抽样全过**(O(mul) 运行) |
| `asm/fibonacci.r8asm` | — | 8761 字符 | 跑出 `1 1 2 3 5 8 13 21 34 55`,和文件注释一致 |
| `raw_add`(a+b,含进位) | `drv/raw_add_byte.r8asm` | 2199 字符 | **65536/65536** |
| `raw_and` | `drv/raw_and_byte.r8asm` | 870 字符 | **65536/65536** |
| `raw_or` | `drv/raw_or_byte.r8asm` | 782 字符 | **65536/65536** |
| `raw_xor` | `drv/raw_xor_byte.r8asm` | 628 字符 | **65536/65536** |
| `raw_inc`(a+进位) | `drv/raw_inc_byte.r8asm` | 1619 字符 | **512/512** |
| **`clb_add_nc`(a+b+进位,4 cell)** | `drv/raw_add_nc_byte.r8asm` | **937 字符** | **65536/65536** |
| **`clb_inc_nc`(a+进位,3 cell)** | `drv/raw_inc_nc_byte.r8asm` | **586 字符** | **256/256** |
| **`clb_neg_nc`(~a+进位,3 cell)** | `drv/raw_neg_nc_byte.r8asm` | **610 字符** | **256/256**(取负) |
| 同上,进位 0 | `drv/raw_not_nc_byte.r8asm` | **609 字符** | **256/256**(反码 `~a`) |
| **`clb_sub_nc`(a-b,4 cell)** | `drv/raw_sub_nc_byte.r8asm` | **962 字符** | **65536/65536** |
| **`clb_not_ip`(~a,原地,1 cell)** | `drv/not_ip_byte.r8asm` | **23 字符** | **256/256** |
| **`clb_inc_ip`(a+1,原地,2 cell)** | `drv/inc_ip_byte.r8asm` | **89 字符** | **256/256** |
| **`clb_neg_ip`(-a,原地,2 cell)** | `drv/neg_ip_byte.r8asm` | **111 字符** | **256/256** |
| **`clb_dec_ip`(a-1,原地,2 cell)** | `drv/dec_ip_byte.r8asm` | **133 字符** | **256/256** |
| **`clb_nonzero`(a!=0 的标志,原地)** | `drv/nonzero_byte.r8asm` | **284 字符** | **256/256** |
| **`clb_add_ip`(a+=b,原地,4 cell)** | `drv/add_ip_byte.r8asm` | **726 字符** | **2048 抽样全过**(O(b) 运行) |
| **`clb_mul`(mc×mul → 两 cell 积,10 cell)** | `drv/mul_byte.r8asm` | **5007 字符** | **64 抽样全过**(O(mul) 运行) |
| `asm/fibonacci.r8asm` | — | 11959 字符 | 跑出 13 项 `1 1 2 3 5 8 13 21 34 55 89 144 233` |
`raw_*` 保留操作数(A、B 原样还回来),`clb_*_nc` 把 A、B 吃干净 —— 所以后者能省掉
`Co` 和 `tmp`:**4 cell 加法/减法、3 cell 自增/取负**。两者都用轮转驱动,差别只在
@@ -163,7 +205,7 @@ void rot8_rnz(rot8_iter *iter) { if ((*(iter->mem_now) & 1) == 1) { ... } }
| `clb_sub_nc A B S Ci` | 先 `clb_not_ip B` 把整个字节原地取反,再走 `clb_add_nc` | `A - B` | `A - B - 1` |
**取调整字节一次、提到循环外面**,比"每 bit 取反一次"更短(也少两个 `flb` 包装):
`clb_neg_nc` 468 → **448**、反码那一版 467 → **447**、`clb_sub_nc` 725 → **707**。
8 位时代是 468 → 448、467 → 447、725 → 707;11 位下的实测值见上表(610 / 609 / 962)。
B 本来就会被加法吃掉,所以在原地取反它没有任何代价。
`clb_sub_nc` 的进位输出是**没有借位**(`A >= B` 时为 1),所以它顺带就是无符号比较;
@@ -175,16 +217,16 @@ A=0 时整块跳过、S 只能留在 0,而取反要求 A=0 时 S=1 —— "无
### 原地(结果回到原来的 cell)
| 宏 | 干什么 | cell | 纸带 | 穷举 |
| 宏 | 干什么 | cell | 纸带(11 位) | 穷举 |
|---|---|---|---|---|
| `clb_not_ip A` | `A = ~A`(反码) | 1 | **17** | 256/256 |
| `clb_inc_ip A T` | `A = A + 1` | 2 | **74** | 256/256 |
| `clb_neg_ip A T` | `A = -A`(补码取负) | 2 | **90** | 256/256 |
| `clb_dec_ip A T` | `A = A - 1` | 2 | **106** | 256/256 |
| `clb_nonzero B F T` | `F = (B != 0)`,B 不动 | 3 | **209** | 256/256 |
| `clb_add_ip A B T F` | `A += B`,B 被吃掉 | 4 | **558** | 2048 抽样 |
| `clb_not_ip A` | `A = ~A`(反码) | 1 | **23** | 256/256 |
| `clb_inc_ip A T` | `A = A + 1` | 2 | **89** | 256/256 |
| `clb_neg_ip A T` | `A = -A`(补码取负) | 2 | **111** | 256/256 |
| `clb_dec_ip A T` | `A = A - 1` | 2 | **133** | 256/256 |
| `clb_nonzero B F T` | `F = (B != 0)`,B 不动 | 3 | **284** | 256/256 |
| `clb_add_ip A B T F` | `A += B`,B 被吃掉 | 4 | **726** | 2048 抽样 |
对比写回 S 的切片版:`clb_inc_nc` 430(3 cell)、`clb_neg_nc` 448(3 cell)——
对比写回 S 的切片版:`clb_inc_nc` 586(3 cell)、`clb_neg_nc` 610(3 cell)——
**原地版短 5 倍、少一个 cell**。`clb_neg_ip` 就是 `clb_not_ip` + `clb_inc_ip`。
**"原地补码不可能"这个结论是错的,这里记一下它是怎么错的**(别再复现同样的推理):
@@ -208,13 +250,13 @@ A=0 时整块跳过、S 只能留在 0,而取反要求 A=0 时 S=1 —— "无
顺带纠正另一半:`[ ... ]` 的"跑了就必然消耗被测位"不是死路 —— 只要被测的是**计数器**,
不是数据。这条以前被写成"手写也做不到(2 cell)",错的。
原地取反仍然是最便宜的一个:`%rep _CELL_SIZE { flb; ror }`,一个 cell、17 字符。
自减用补码恒等式拼出来,不用新算法:`B - 1 = ~(-B)`,所以 `clb_dec_ip` = `clb_neg_ip` + `clb_not_ip`(106 字符)。
原地取反仍然是最便宜的一个:`%rep _CELL_SIZE { flb; ror }`,一个 cell、23 字符。
自减用补码恒等式拼出来,不用新算法:`B - 1 = ~(-B)`,所以 `clb_dec_ip` = `clb_neg_ip` + `clb_not_ip`(133 字符)。
### 原地加法,以及"只有纸带长度是目标"这条
`clb_add_ip A B T F`:`A += B`,原地,4 个 cell(A、B、T、F),**558 字符**,
B 被吃干净。对比:`raw_add` 1647(6 cell,保留操作数)、`clb_add_nc` 688 + 每轮 `copy` 364。
`clb_add_ip A B T F`:`A += B`,原地,4 个 cell(A、B、T、F),**726 字符**,
B 被吃干净。对比:`raw_add` 2199(6 cell,保留操作数)、`clb_add_nc` 937 + 每轮一次 `copy`。
**没有去写逐位的进位链加法**:全加器的进位是 `maj(a,b,c)`,也就是两两的合取,而每个条件测试
都会吃掉被测的那一位,所以要么先拷贝好几份位、要么嵌套测试 —— 又长又脆。
@@ -234,16 +276,16 @@ B 被吃干净。对比:`raw_add` 1647(6 cell,保留操作数)、`clb_ad
### 乘法器(`asm/mul.r8asm`)
`clb_mul MC MUL PL PH MCA S Ci T F NR`:`(PH:PL) = MC * MUL`,10 个 cell,**3729 字符**,
`r8forge_byte_op_test` 抽 64 组全对(含 `255*255 = 0xFE01`),最坏运行约 100 万步。
`clb_mul MC MUL PL PH MCA S Ci T F NR`:`(PH:PL) = MC * MUL`,10 个 cell,**5007 字符**,
`r8forge_byte_op_test` 抽 64 组全对(含 `255*255 = 65025 = 31*2048 + 1473`),最坏运行约 100 万步。
用的是**重复加法**,不是移位-加法:
(PH:PL) = 0
while MUL != 0: (PH:PL) += MC ; MUL -= 1
理由还是「只有纸带长度是目标」:循环体在纸带里只出现一次,所以长度和 MUL 无关,
`fibonacci`(8761)都比它长。
理由还是「只有纸带长度是目标」:循环体在纸带里只出现一次,所以长度和 MUL 无关 ——
连 `fibonacci`(11 位下 11959 字符)都比它长。
16 位累加靠 `clb_add_nc` 的进位输出:低字节相加拿到进位,再用 `clb_add_ip` 把这一位加进高字节。
`clb_add_nc` 会把操作数吃干净,所以每轮先 `copy` 一份被乘数(`clb_add_ip` 不输出进位,
@@ -298,24 +340,24 @@ B 被吃干净。对比:`raw_add` 1647(6 cell,保留操作数)、`clb_ad
| 东西 | 结论 |
|---|---|
| 库里的 7 个位运算块(and/or/xor/inc/add) | 合成版比手写短:`asm/test.r8asm` 1714 → 1666 字符 |
| `asm/test.r8asm` 也演乘法 | 现在 1666 → **5417 字符**(`raw_add` 一段 + `clb_mul` 一段),跑出 `127 130 0 1 1 64 156`(= 127+130 和 200×200),`derive` 与 `tape_run` 两个实现一致 |
| `clb_bit_add_nc`(4 cell 忽略进位切片) | flat 代价 **27**,完整全加器切片是 38;8/8 组合正确 |
| `clb_bit_inc_nc`(3 cell 自增切片) | 表 `0 4 4 2 0 4 4 2`(3 参数)缓存里就有,4 blocks;纸带 54 字符,4/4 组合正确 |
| `clb_add_nc`(4 cell 字节加法) | 688 字符,65536 组合全对;比 6 cell 的 `raw_add` 少 2 个 cell、短 959 字符 |
| `clb_inc_nc`(3 cell 字节自增/加 0-1) | 430 字符,256 组合全对;比 5 cell 的 `raw_inc` 少 2 个 cell |
| `clb_neg_nc`(3 cell 取负/取反) | 448 / 447 字符,各 256 组合全对(Ci=1 取负、Ci=0 反码) |
| `clb_sub_nc`(4 cell 减法) | 707 字符,65536 组合全对;进位输出顺带是无符号比较(A >= B) |
| `clb_not_ip`(1 cell 原地取反) | **17 字符**,256 组合全对;比走切片那条路快 26 倍 |
| `clb_inc_ip`(2 cell 原地自增) | **74 字符**,256 组合全对;切片版 `clb_inc_nc` 是 430 |
| `clb_neg_ip`(2 cell 原地取负) | **90 字符**,256 组合全对;切片版 `clb_neg_nc` 是 448 |
| `clb_dec_ip`(2 cell 原地自减) | **106 字符** = `neg` + `not`(`B-1 = ~(-B)`),256 全对 |
| `clb_nonzero`(非零标志) | **209 字符**,256 全对,B 保持原样 |
| `clb_add_ip`(4 cell 原地加) | **558 字符**,2048 抽样全对;`raw_add` 1647 / `clb_add_nc`+copy 1052 |
| `clb_mul`(10 cell 8 位乘法,16 位积) | **3729 字符**,64 抽样全对;比 `fibonacci`(8761)短,和 MUL 大小无关 |
| `bit_raw_add`(6 cell 全加器切片) | 保留 A、B;字节级 `raw_add` 1647 字符,65536 组合全对 |
| `raw_and` / `raw_or` / `raw_xor` / `raw_inc` | 639 / 575 / 463 / 1214 字符,各 65536(inc 512)全对 |
| `asm/fibonacci.r8asm` | 8761 字符,跑出 10 项斐波那契,和注释一致 |
| 库里的 7 个位运算块(and/or/xor/inc/add) | 合成版比手写短(8 位时代 1714 → 1666;位宽改了以后位级块不受影响,长度变的是字节级那层) |
| `asm/test.r8asm` 也演乘法 | 11 位下 **7262 字符**(`raw_add` 一段 + `clb_mul` 一段),跑出 `127 130 0 257 0 1088 19`(= 127+130 和 200×200 拆成 19·2048+1088),`derive` 与 ROT-8-viz 两边一致 |
| `clb_bit_add_nc`(4 cell 忽略进位切片) | flat 代价 **27**,完整全加器切片是 38;8/8 组合正确(与位宽无关) |
| `clb_bit_inc_nc`(3 cell 自增切片) | 表 `0 4 4 2 0 4 4 2`(3 参数)缓存里就有,4 blocks;4/4 组合正确(与位宽无关) |
| `clb_add_nc`(4 cell 加法) | 11 位下 937 字符,65536 组合全对;比 6 cell 的 `raw_add` 少 2 个 cell |
| `clb_inc_nc`(3 cell 自增/加 0-1) | 586 字符,256 组合全对;比 5 cell 的 `raw_inc` 少 2 个 cell |
| `clb_neg_nc`(3 cell 取负/取反) | 610 / 609 字符,各 256 组合全对(Ci=1 取负、Ci=0 反码) |
| `clb_sub_nc`(4 cell 减法) | 962 字符,65536 组合全对;进位输出顺带是无符号比较(A >= B) |
| `clb_not_ip`(1 cell 原地取反) | **23 字符**,256 组合全对 |
| `clb_inc_ip`(2 cell 原地自增) | **89 字符**,256 组合全对;切片版 `clb_inc_nc` 是 586 |
| `clb_neg_ip`(2 cell 原地取负) | **111 字符**,256 组合全对;切片版 `clb_neg_nc` 是 610 |
| `clb_dec_ip`(2 cell 原地自减) | **133 字符** = `neg` + `not`(`B-1 = ~(-B)`),256 全对 |
| `clb_nonzero`(非零标志) | **284 字符**,256 全对,B 保持原样 |
| `clb_add_ip`(4 cell 原地加) | **726 字符**,2048 抽样全对;`raw_add` 2199 |
| `clb_mul`(10 cell 乘法,积占两个 cell) | **5007 字符**,64 抽样全对;和 MUL 的大小无关 |
| `bit_raw_add`(6 cell 全加器切片) | 保留 A、B;字节级 `raw_add` 2199 字符,65536 组合全对 |
| `raw_and` / `raw_or` / `raw_xor` / `raw_inc` | 870 / 782 / 628 / 1619 字符,各 65536(inc 512)全对 |
| `asm/fibonacci.r8asm` | 11959 字符,跑出 13 项(`… 89 144 233`),比 8 位时代多 3 项 |
## 已经确认走不通的(别重试)
@@ -324,7 +366,7 @@ B 被吃干净。对比:`raw_add` 1647(6 cell,保留操作数)、`clb_ad
* **原地写回的 `%synth` 表**:`A←~A^Ci`、`A←A^Ci`、`A←A^B^Ci`、`A←A^~B^Ci` 四个表全部报
`no program up to 8 layers`。原因是一个合成块只有一个条件 cell,区分不了 "A=1 且 Ci=0"
和 "A=1 且 Ci=1"。**但手写可以**:换成"清连续 1 + 计数器旋转"的算法,2 cell 就够
(`clb_inc_ip` 74 字符、`clb_neg_ip` 90 字符,见「原地」)。别把这条读成"手写也不行"。
(`clb_inc_ip` 89 字符、`clb_neg_ip` 111 字符,见「原地」)。别把这条读成"手写也不行"。
* **让切片自己在输入上取反**(表 `S=~A^Ci`、进位 `~A&Ci`,3 参数):`no program up to 8 layers`。
模型只会"牺牲一个 cell → 条件翻转别的 cell",输入为 0 时整块被跳过、S 只能保持 0,
表达不出"无条件翻转"。取反只能放在切片外面(一条 `flb`)。
+9 -2
View File
@@ -187,7 +187,13 @@ build/Release/r8forge_derive drv/bit_raw_add.r8asm 6
2. `map` 一项里的 bit0 落在 `%(K+1)`、bit1 落在 `%(K+2)`……超出 `params` 会直接报错。
3. `stat` 必须给 `2^params` 项,每项是那个输入组合下所有参数的取值。
## 字节级运算:一个 cell 一个字节
## 字节级运算:一个 cell 一个机器字
> 机械字默认 **11 位、纸带 259 格**,都定义在 `include/r8forge/geometry.hh`
> (`kDefaultCellBits` / `kDefaultMemoryCells`)。指令集本身不规定位宽,宏里用
> `%rep _CELL_SIZE` 的地方会自动跟着变,所以下面说的"字节级"其实是"机器字级";
> 下面引用的纸带长度是 8 位时代的实测值,11 位下这些宏会长一些(`raw_add` 2199 字符、
> `fibonacci` 11959 字符,完整数字见 `AGENTS.md`)。
```asm
%include "asm/raw_add.r8asm"
@@ -303,7 +309,8 @@ while MUL != 0: (PH:PL) += MC ; MUL -= 1
**这个设计本身是一条经验**:一开始以为乘法器缺"跨字节移位"和"条件累加",换成重复加法之后
两样都不需要了。先看目标函数(纸带长度),再选算法,别照着硬件乘法的样子拼。
`asm/fibonacci.r8asm` 也是这条路上的:8761 字符,跑出 `1 1 2 3 5 8 13 21 34 55`。
`asm/fibonacci.r8asm` 也是这条路上的:11 位下 11959 字符,跑出
`1 1 2 3 5 8 13 21 34 55 89 144 233`(13 项,位宽变大以后不再在 256 处回绕)。
### 一个坑:`biz`/`rnz` 看的是最低位
+6 -2
View File
@@ -13,10 +13,13 @@
.var Co 8
.var tmp 1
; 注意 `.xor` 的常数是按**当前 cell 位宽**解释的:这里位宽是 11 位
; (见 include/r8forge/geometry.hh),所以一个负常数会变成 2^11 减去它,
; 想置 130 就直接写 130. 以前 8 位时这里写的是 `(~126)+1`.
.tp A
.xor 127
.tp B
.xor (~126)+1
.xor 130
.tp Ci
.xor 0
@@ -29,7 +32,8 @@ output S
output Co
; 再乘一次:200 * 200 = 40000 = 0x9C40,低字节 0x40 = 64,高字节 0x9C = 156。
; 再乘一次:200 * 200 = 40000,11 位的 cell 装不下,于是拆成
; 低 cell 40000 mod 2048 = 1088、高 cell 40000 / 2048 = 19。
; 这一块只碰 16 号往后的 cell,不打扰上面加法用的那一组。
.var MC 16
.var MUL 17
+12
View File
@@ -0,0 +1,12 @@
#pragma once
#include <cstddef>
#include <cstdint>
// Machine geometry the tools use unless something says otherwise: how wide a
// cell is in bits, and how many cells the tape holds. The instruction set does
// not fix either (ROR just rotates inside a cell and the tape is a ring), so
// these are the only places the old 8 bit / 256 cell assumptions live.
inline constexpr size_t kDefaultCellBits = 11;
inline constexpr size_t kDefaultMemoryCells = 259;
inline constexpr uint32_t kDefaultCellMask = (1u << kDefaultCellBits) - 1u;
+1
View File
@@ -10,6 +10,7 @@
#include <string>
#include <vector>
#include "r8forge/geometry.hh"
#include "r8forge/r8veritas.hh"
// %synth: run the searcher on a truth table and return the block list as
+15 -13
View File
@@ -16,11 +16,13 @@
namespace {
struct machine {
std::vector<uint8_t> cells = std::vector<uint8_t>(256, 0);
std::vector<uint8_t> out;
std::vector<uint16_t> cells = std::vector<uint16_t>(kDefaultMemoryCells, 0);
std::vector<uint16_t> out;
size_t dp = 0;
static uint8_t ror(uint8_t v) { return uint8_t((v >> 1) | (v << 7)); }
static uint16_t ror(uint16_t v) {
return uint16_t((v >> 1) | ((v & 1u) << (kDefaultCellBits - 1)));
}
void run(const std::string &tape, size_t limit = 5'000'000) {
for (size_t pc = 0; pc < tape.size() && limit; pc++, limit--) {
@@ -88,14 +90,14 @@ int main(int argc, char **argv) {
const int params = std::atoi(argv[2]);
if (params == 0) { // just run the program and dump what it printed
r8asm a;
if (!a.import_file(file, 8)) {
if (!a.import_file(file, kDefaultCellBits)) {
std::cerr << "could not assemble " << file << "\n";
return 1;
}
machine m;
m.run(a.export_rot8());
std::cout << "out";
for (uint8_t v : m.out)
for (uint16_t v : m.out)
std::cout << " " << int(v);
std::cout << "\n";
return 0;
@@ -103,7 +105,7 @@ int main(int argc, char **argv) {
// params == -2: same as -1 but dumps every non zero cell afterwards
if (params == -2) {
r8asm a;
if (!a.import_file(file, 8)) {
if (!a.import_file(file, kDefaultCellBits)) {
std::cerr << "could not assemble " << file << "\n";
return 1;
}
@@ -114,11 +116,11 @@ int main(int argc, char **argv) {
continue;
}
const int at = std::atoi(std::string(argv[i], size_t(eq - argv[i])).c_str());
m.cells[size_t(at)] = uint8_t(std::atoi(eq + 1) & 0xff);
m.cells[size_t(at)] = uint16_t(std::atoi(eq + 1) & kDefaultCellMask);
}
m.run(a.export_rot8());
std::cout << "out";
for (uint8_t v : m.out) {
for (uint16_t v : m.out) {
std::cout << " " << int(v);
}
std::cout << "\ncells";
@@ -133,7 +135,7 @@ int main(int argc, char **argv) {
// params == -1: the rest of argv is addr=value; set those cells, run, dump OUT
if (params == -1) {
r8asm a;
if (!a.import_file(file, 8)) {
if (!a.import_file(file, kDefaultCellBits)) {
std::cerr << "could not assemble " << file << "\n";
return 1;
}
@@ -144,11 +146,11 @@ int main(int argc, char **argv) {
continue;
}
const int at = std::atoi(std::string(argv[i], size_t(eq - argv[i])).c_str());
m.cells[size_t(at)] = uint8_t(std::atoi(eq + 1) & 0xff);
m.cells[size_t(at)] = uint16_t(std::atoi(eq + 1) & kDefaultCellMask);
}
m.run(a.export_rot8());
std::cout << "out";
for (uint8_t v : m.out) {
for (uint16_t v : m.out) {
std::cout << " " << int(v);
}
std::cout << "\n";
@@ -163,7 +165,7 @@ int main(int argc, char **argv) {
addr[static_cast<size_t>(i)] = argc > 3 + i ? std::atoi(argv[3 + i]) : i;
r8asm assembler;
if (!assembler.import_file(file, 8)) {
if (!assembler.import_file(file, kDefaultCellBits)) {
std::cerr << "could not assemble " << file << "\n";
return 1;
}
@@ -174,7 +176,7 @@ int main(int argc, char **argv) {
for (int combo = 0; combo < (1 << params); combo++) {
machine m;
for (int b = 0; b < params; b++)
m.cells[static_cast<size_t>(addr[static_cast<size_t>(b)])] = uint8_t((combo >> b) & 1);
m.cells[static_cast<size_t>(addr[static_cast<size_t>(b)])] = uint16_t((combo >> b) & 1);
m.run(tape);
unsigned val = 0;
for (int b = 0; b < params; b++)
+1 -1
View File
@@ -8,7 +8,7 @@ int main(int argc, char **argv) {
return (2);
}
r8asm a;
if (!a.import_file(argv[1], 8))
if (!a.import_file(argv[1], kDefaultCellBits))
return (1);
std::string tape = a.export_rot8();
if (a.failed())
+61 -51
View File
@@ -17,14 +17,24 @@
namespace {
// The cell width is a knob (geometry.hh), so every expectation below is
// written in terms of it instead of in terms of a byte: kCellMod is one past
// the largest value a cell holds. The sweeps walk the first 256 values, which
// is a sample of the cell range rather than all of it.
constexpr int kBits = int(kDefaultCellBits);
constexpr int kCellMod = 1 << kBits;
constexpr int kCellMask = int(kDefaultCellMask);
struct machine {
std::vector<uint8_t> cells = std::vector<uint8_t>(256, 0);
std::vector<uint8_t> out;
std::vector<uint16_t> cells = std::vector<uint16_t>(kDefaultMemoryCells, 0);
std::vector<uint16_t> out;
size_t dp = 0;
size_t steps = 0;
bool finished = true; // false: the step limit ran out
static uint8_t ror(uint8_t v) { return uint8_t((v >> 1) | (v << 7)); }
static uint16_t ror(uint16_t v) {
return uint16_t((v >> 1) | ((v & 1u) << (kDefaultCellBits - 1)));
}
void run(const std::string &tape, size_t limit) {
for (size_t pc = 0; pc < tape.size(); pc++) {
@@ -81,7 +91,7 @@ struct machine {
std::string assemble(const std::string &file) {
r8asm assembler;
if (!assembler.import_file(file, 8) || assembler.failed()) {
if (!assembler.import_file(file, kDefaultCellBits) || assembler.failed()) {
std::cerr << "could not assemble " << file << "\n";
std::exit(1);
}
@@ -94,7 +104,7 @@ void report(const std::string &name, int a, int b, const machine &m,
const std::string &want) {
if (failures++ < 5) {
std::cerr << " WRONG " << name << " a=" << a << " b=" << b << " out";
for (uint8_t v : m.out)
for (uint16_t v : m.out)
std::cerr << " " << int(v);
std::cerr << " want " << want << " finished=" << m.finished
<< " steps=" << m.steps << "\n";
@@ -109,8 +119,8 @@ int test_bitop(const std::string &file, const char *name, int op) {
for (int a = 0; a < 256; a++)
for (int b = 0; b < 256; b++) {
machine m;
m.cells[0] = uint8_t(a); // A
m.cells[2] = uint8_t(b); // B
m.cells[0] = uint16_t(a); // A
m.cells[2] = uint16_t(b); // B
m.run(tape, 100'000);
const int want = op == 0 ? (a & b) : op == 1 ? (a | b) : (a ^ b);
const bool ok = m.finished && m.out.size() == 3 && m.out[0] == a &&
@@ -136,20 +146,20 @@ int test_add(const std::string &file) {
for (int a = 0; a < 256; a++)
for (int b = 0; b < 256; b++) {
machine m;
m.cells[0] = uint8_t(a);
m.cells[2] = uint8_t(b);
m.cells[0] = uint16_t(a);
m.cells[2] = uint16_t(b);
m.run(tape, 100'000);
const int sum = a + b;
const bool ok = m.finished && m.out.size() == 5 && m.out[0] == a &&
m.out[1] == b && m.out[2] == 0 &&
m.out[3] == (sum & 0xff) && m.out[4] == (sum >> 8);
m.out[3] == (sum & kCellMask) && m.out[4] == (sum >> kBits);
if (!ok) {
bad++;
if (bad <= 3)
report("raw_add", a, b, m,
std::to_string(a) + " " + std::to_string(b) + " 0 " +
std::to_string(sum & 0xff) + " " +
std::to_string(sum >> 8));
std::to_string(sum & kCellMask) + " " +
std::to_string(sum >> kBits));
}
}
std::cout << " " << (bad ? "FAILED" : "all 65536 combinations ok")
@@ -165,20 +175,20 @@ int test_inc(const std::string &file) {
for (int a = 0; a < 256; a++)
for (int ci = 0; ci < 2; ci++) {
machine m;
m.cells[0] = uint8_t(a);
m.cells[2] = uint8_t(ci);
m.cells[0] = uint16_t(a);
m.cells[2] = uint16_t(ci);
m.run(tape, 100'000);
const int sum = a + ci;
const bool ok = m.finished && m.out.size() == 4 && m.out[0] == a &&
m.out[1] == ci && m.out[2] == (sum & 0xff) &&
m.out[3] == (sum >> 8);
m.out[1] == ci && m.out[2] == (sum & kCellMask) &&
m.out[3] == (sum >> kBits);
if (!ok) {
bad++;
if (bad <= 3)
report("raw_inc", a, ci, m,
std::to_string(a) + " " + std::to_string(ci) + " " +
std::to_string(sum & 0xff) + " " +
std::to_string(sum >> 8));
std::to_string(sum & kCellMask) + " " +
std::to_string(sum >> kBits));
}
}
std::cout << " " << (bad ? "FAILED" : "all 512 combinations ok")
@@ -195,19 +205,19 @@ int test_add_nc(const std::string &file) {
for (int a = 0; a < 256; a++)
for (int b = 0; b < 256; b++) {
machine m;
m.cells[0] = uint8_t(a); // A
m.cells[2] = uint8_t(b); // B
m.cells[0] = uint16_t(a); // A
m.cells[2] = uint16_t(b); // B
m.run(tape, 100'000);
const int sum = a + b;
const bool ok = m.finished && m.out.size() == 4 && m.out[0] == 0 &&
m.out[1] == 0 && m.out[2] == (sum & 0xff) &&
m.out[3] == (sum >> 8);
m.out[1] == 0 && m.out[2] == (sum & kCellMask) &&
m.out[3] == (sum >> kBits);
if (!ok) {
bad++;
if (bad <= 3)
report("clb_add_nc", a, b, m,
"0 0 " + std::to_string(sum & 0xff) + " " +
std::to_string(sum >> 8));
"0 0 " + std::to_string(sum & kCellMask) + " " +
std::to_string(sum >> kBits));
}
}
std::cout << " " << (bad ? "FAILED" : "all 65536 combinations ok")
@@ -222,17 +232,17 @@ int test_inc_nc(const std::string &file) {
int bad = 0;
for (int a = 0; a < 256; a++) {
machine m;
m.cells[0] = uint8_t(a); // A
m.cells[0] = uint16_t(a); // A
m.run(tape, 100'000);
const int sum = a + 1;
const bool ok = m.finished && m.out.size() == 3 && m.out[0] == 0 &&
m.out[1] == (sum & 0xff) && m.out[2] == (sum >> 8);
m.out[1] == (sum & kCellMask) && m.out[2] == (sum >> kBits);
if (!ok) {
bad++;
if (bad <= 3)
report("clb_inc_nc", a, 1, m,
"0 " + std::to_string(sum & 0xff) + " " +
std::to_string(sum >> 8));
"0 " + std::to_string(sum & kCellMask) + " " +
std::to_string(sum >> kBits));
}
}
std::cout << " " << (bad ? "FAILED" : "all 256 combinations ok")
@@ -247,9 +257,9 @@ int test_neg_nc(const std::string &file) {
int bad = 0;
for (int a = 0; a < 256; a++) {
machine m;
m.cells[0] = uint8_t(a); // A
m.cells[0] = uint16_t(a); // A
m.run(tape, 100'000);
const int neg = (256 - a) & 0xff;
const int neg = (kCellMod - a) & kCellMask;
const int carry = a == 0 ? 1 : 0; // -0 is 0 but it does carry out
const bool ok = m.finished && m.out.size() == 3 && m.out[0] == 0 &&
m.out[1] == neg && m.out[2] == carry;
@@ -273,10 +283,10 @@ int test_sub_nc(const std::string &file) {
for (int a = 0; a < 256; a++)
for (int b = 0; b < 256; b++) {
machine m;
m.cells[0] = uint8_t(a); // A
m.cells[2] = uint8_t(b); // B
m.cells[0] = uint16_t(a); // A
m.cells[2] = uint16_t(b); // B
m.run(tape, 100'000);
const int diff = (a - b) & 0xff;
const int diff = (a - b) & kDefaultCellMask;
const int no_borrow = a >= b ? 1 : 0;
const bool ok = m.finished && m.out.size() == 4 && m.out[0] == 0 &&
m.out[1] == 0 && m.out[2] == diff && m.out[3] == no_borrow;
@@ -300,9 +310,9 @@ int test_not_nc(const std::string &file) {
int bad = 0;
for (int a = 0; a < 256; a++) {
machine m;
m.cells[0] = uint8_t(a); // A
m.cells[0] = uint16_t(a); // A
m.run(tape, 100'000);
const int inv = (~a) & 0xff;
const int inv = (~a) & kDefaultCellMask;
const bool ok = m.finished && m.out.size() == 3 && m.out[0] == 0 &&
m.out[1] == inv && m.out[2] == 0;
if (!ok) {
@@ -323,9 +333,9 @@ int test_not_ip(const std::string &file) {
int bad = 0;
for (int a = 0; a < 256; a++) {
machine m;
m.cells[0] = uint8_t(a); // A
m.cells[0] = uint16_t(a); // A
m.run(tape, 100'000);
const int inv = (~a) & 0xff;
const int inv = (~a) & kDefaultCellMask;
const bool ok =
m.finished && m.out.size() == 1 && m.out[0] == inv;
if (!ok) {
@@ -346,9 +356,9 @@ int test_inc_ip(const std::string &file) {
int bad = 0;
for (int a = 0; a < 256; a++) {
machine m;
m.cells[0] = uint8_t(a); // A
m.cells[0] = uint16_t(a); // A
m.run(tape, 100'000);
const int want = (a + 1) & 0xff;
const int want = (a + 1) & kDefaultCellMask;
const bool ok = m.finished && m.out.size() == 1 && m.out[0] == want &&
m.cells[1] == 0; // the counter cell is spent
if (!ok) {
@@ -369,9 +379,9 @@ int test_neg_ip(const std::string &file) {
int bad = 0;
for (int a = 0; a < 256; a++) {
machine m;
m.cells[0] = uint8_t(a); // A
m.cells[0] = uint16_t(a); // A
m.run(tape, 100'000);
const int want = (256 - a) & 0xff;
const int want = (kCellMod - a) & kCellMask;
const bool ok = m.finished && m.out.size() == 1 && m.out[0] == want;
if (!ok) {
bad++;
@@ -391,9 +401,9 @@ int test_dec_ip(const std::string &file) {
int bad = 0;
for (int a = 0; a < 256; a++) {
machine m;
m.cells[0] = uint8_t(a);
m.cells[0] = uint16_t(a);
m.run(tape, 100'000);
const int want = (a - 1) & 0xff;
const int want = (a - 1) & kDefaultCellMask;
const bool ok = m.finished && m.out.size() == 1 && m.out[0] == want;
if (!ok) {
bad++;
@@ -413,7 +423,7 @@ int test_nonzero(const std::string &file) {
int bad = 0;
for (int b = 0; b < 256; b++) {
machine m;
m.cells[0] = uint8_t(b); // B
m.cells[0] = uint16_t(b); // B
m.run(tape, 100'000);
const bool ok = m.finished && m.out.size() == 2 && m.out[0] == b &&
m.out[1] == (b != 0 ? 1 : 0);
@@ -439,11 +449,11 @@ int test_add_ip(const std::string &file) {
for (int a : accs)
for (int b = 0; b < 256; b++) {
machine m;
m.cells[0] = uint8_t(a); // A
m.cells[1] = uint8_t(b); // B
m.cells[0] = uint16_t(a); // A
m.cells[1] = uint16_t(b); // B
m.run(tape, 5'000'000);
n++;
const int want = (a + b) & 0xff;
const int want = (a + b) & kDefaultCellMask;
const bool ok = m.finished && m.out.size() == 2 && m.out[0] == want &&
m.out[1] == 0;
if (!ok) {
@@ -467,19 +477,19 @@ int test_mul(const std::string &file) {
for (int mc : vals)
for (int mu : vals) {
machine m;
m.cells[0] = uint8_t(mc); // MC
m.cells[1] = uint8_t(mu); // MUL
m.cells[0] = uint16_t(mc); // MC
m.cells[1] = uint16_t(mu); // MUL
m.run(tape, 5'000'000);
n++;
const int p = mc * mu;
const bool ok = m.finished && m.out.size() == 3 &&
m.out[0] == (p & 0xff) && m.out[1] == ((p >> 8) & 0xff) &&
m.out[0] == (p & kCellMask) && m.out[1] == ((p >> kBits) & kCellMask) &&
m.out[2] == mc;
if (!ok) {
bad++;
if (bad <= 3)
report("clb_mul", mc, mu, m,
std::to_string(p & 0xff) + " " + std::to_string((p >> 8) & 0xff) +
std::to_string(p & kCellMask) + " " + std::to_string((p >> kBits) & kCellMask) +
" " + std::to_string(mc));
}
}
+10 -7
View File
@@ -1,6 +1,7 @@
// Assembles examples/full_add.r8asm (whose body is synthesised at assembly
// time) and runs the resulting tape on a tiny ROT-8 machine for all eight
// input combinations.
#include "r8forge/geometry.hh"
#include "r8forge/r8asm.hh"
#include <cstdlib>
#include <iostream>
@@ -14,11 +15,13 @@ const char *const INSTRUCTIONS = "><-+[].r"; // r = our ror alias
// note "-" is ror, and there is no separate 'r'
struct machine {
std::vector<uint8_t> cells = std::vector<uint8_t>(256, 0);
std::vector<uint8_t> out;
std::vector<uint16_t> cells = std::vector<uint16_t>(kDefaultMemoryCells, 0);
std::vector<uint16_t> out;
size_t dp = 0;
static uint8_t ror(uint8_t v) { return uint8_t((v >> 1) | (v << 7)); }
static uint16_t ror(uint16_t v) {
return uint16_t((v >> 1) | ((v & 1u) << (kDefaultCellBits - 1)));
}
void run(const std::string &tape) {
for (size_t pc = 0; pc < tape.size(); pc++) {
@@ -79,7 +82,7 @@ struct machine {
int main(int argc, char **argv) {
const char *file = argc > 1 ? argv[1] : "examples/full_add.r8asm";
r8asm assembler;
if (!assembler.import_file(file, 8)) {
if (!assembler.import_file(file, kDefaultCellBits)) {
std::cerr << "could not assemble " << file << "\n";
return 1;
}
@@ -90,9 +93,9 @@ int main(int argc, char **argv) {
for (int combo = 0; combo < 8; combo++) {
const int a = combo & 1, b = (combo >> 1) & 1, c = (combo >> 2) & 1;
machine m;
m.cells[0] = uint8_t(a); // A
m.cells[1] = uint8_t(b); // B
m.cells[2] = uint8_t(c); // Ci
m.cells[0] = uint16_t(a); // A
m.cells[1] = uint16_t(b); // B
m.cells[2] = uint16_t(c); // Ci
m.run(tape);
const int sum = a ^ b ^ c;
const int carry = (a & b) | (a & c) | (b & c);
+6 -5
View File
@@ -1,3 +1,4 @@
#include "r8forge/geometry.hh"
// Independent ROT-8 interpreter. Reads a .rot8 tape straight from a file --
// no macro, no assembler, no r8asm -- and prints what `out` ('.') produced.
//
@@ -29,7 +30,7 @@ int main(int argc, char **argv) {
const std::string tape((std::istreambuf_iterator<char>(in)),
std::istreambuf_iterator<char>());
std::vector<uint8_t> cells(1024, 0);
std::vector<uint16_t> cells(kDefaultMemoryCells, 0);
bool trace = false;
for (int i = 2; i < argc; i++) {
const std::string a = argv[i];
@@ -41,10 +42,10 @@ int main(int argc, char **argv) {
if (eq == std::string::npos)
continue;
const size_t at = size_t(std::stoul(a.substr(0, eq)));
cells.at(at) = uint8_t(std::stoul(a.substr(eq + 1)) & 0xff);
cells.at(at) = uint16_t(std::stoul(a.substr(eq + 1)) & kDefaultCellMask);
}
std::vector<uint8_t> out;
std::vector<uint16_t> out;
size_t dp = 0;
const size_t limit = 5'000'000; // same as r8forge_derive
size_t pc = 0;
@@ -74,7 +75,7 @@ int main(int argc, char **argv) {
cells[dp] ^= 1;
break;
case '+': // ror
cells[dp] = uint8_t((cells[dp] >> 1) | (cells[dp] << 7));
cells[dp] = uint16_t((cells[dp] >> 1) | ((cells[dp] & 1u) << (kDefaultCellBits - 1)));
break;
// biz / rnz test the LOW BIT, not "is the cell zero" -- see ROT-8-viz // src/rot8.c. Reading them as a Brainfuck style zero test makes every
// byte level program in the library look like an endless loop.
@@ -117,7 +118,7 @@ int main(int argc, char **argv) {
}
std::cout << "out";
for (uint8_t v : out)
for (uint16_t v : out)
std::cout << " " << int(v);
std::cout << " (" << steps << " steps, cell0 ended 0x" << std::hex
<< int(cells[0]) << std::dec << ")\n";