From 43390efe8a6d369db0d966bca09121aac93c9ffb Mon Sep 17 00:00:00 2001 From: dsh Date: Fri, 6 Nov 2026 23:39:58 +0800 Subject: [PATCH] Byte level add, one bit per cell: 65536 combinations ok The rotation based byte layer never ran. Measured with r8forge_derive -2: raw_add A=0,B=0 / 1+1 / 3+5 / 1+0 out empty (step limit hit) raw_add A=127,B=130 out 0 0 0 0 0, everything destroyed raw_and 0xF0 and 0xCC, 1 and 1, 0 and 5 out empty copy A=5 out empty (A=0 returns fine) Not "only operand preservation is missing": set_0 (= [-]) cannot clear a cell that holds more than one bit, and biz only answers "is this whole cell zero", because ror does not change zero-ness. So the bits of a packed byte are unreadable, and the whole ror + set_0 layer has no future. The docs now say that instead of blaming operand preservation. What replaces it is one bit per cell: asm/byte_add.r8asm 8 bit adder. Bit i owns four adjacent cells at base+4i: A, B, carry (in -> out), scratch. The synthesised four cell slice bit_raw_add_nc does the bit, clb_bit_mov moves the carry into the next quartet. A doubles as the accumulator, so calling byte_add again continues the sum. asm/pack_byte.r8asm eight flags -> one byte, the only direction that works. bit_raw_flip_if consumes a flag into flipping bit 0 of an accumulator that may already hold bits (no set_0 there). examples/add8.r8asm a+b, 1308 characters. A shared carry cell layout needs only 25 cells but was measured at 2517: paying .tp travel eight times beats saving seven carry cells. examples/add8_inc.r8asm add 1 three times, proving the block composes. tests/byte_add_test.cc r8forge_byte_add_test: runs the tapes on the ROT-8 machine over every input pair, 0.3 s. Also: AGENTS.md gains the measurement table, the third instruction set constraint (packed bytes are unreadable), the new files, and the note that build/ has to be rebuilt with the VS bundled cmake -- scoop's 4.4 reconfigures it and dies with "No preprocessor test for PellesC". --- AGENTS.md | 80 +++++++++++++++++-- CMakeLists.txt | 4 + README.md | 53 +++++++++++++ asm/byte_add.r8asm | 82 ++++++++++++++++++++ asm/pack_byte.r8asm | 75 ++++++++++++++++++ examples/add8.r8asm | 22 ++++++ examples/add8_inc.r8asm | 26 +++++++ tests/byte_add_test.cc | 167 ++++++++++++++++++++++++++++++++++++++++ 8 files changed, 502 insertions(+), 7 deletions(-) create mode 100644 asm/byte_add.r8asm create mode 100644 asm/pack_byte.r8asm create mode 100644 examples/add8.r8asm create mode 100644 examples/add8_inc.r8asm create mode 100644 tests/byte_add_test.cc diff --git a/AGENTS.md b/AGENTS.md index 65f4049..aa86565 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -11,6 +11,14 @@ 堆栈;用 `2>$null` 收干净,判断结果靠 `$LASTEXITCODE` 和 stdout。 * 写含中文的文件用编辑工具(UTF-8),**不要用 `Set-Content -Encoding ascii`** —— 汉字会变成 `?`(`examples/full_add_flat*.r8asm` 就这么坏过一次)。 +* **构建要用 VS 自带的 cmake**:`build/` 是 `Visual Studio 18 2026` 生成器配的, + 直接敲 PATH 上的 scoop cmake(4.4)会因为版本不一致重新配置,然后死在 + `No preprocessor test for "PellesC"`。用这个: + + ```sh + "C:/Program Files/Microsoft Visual Studio/18/Community/Common7/IDE/CommonExtensions/Microsoft/CMake/CMake/bin/cmake.exe" \ + --build build --config Release + ``` * 会话很长时传输容易失败,重要结论要落到 **commit message 和文件注释**里,别只留在对话里。 ## 构建与自检 @@ -32,6 +40,16 @@ r8forge_synth_test examples/full_add.r8asm # all eight combinations ok r8forge verify_raw_add_nc.r8asm # 4 cell 加法切片驱动 ``` +跑纸带的第四件(**唯一一个真的执行纸带、按返回值判断对错的**): + +```sh +r8forge_byte_add_test # add8: 65536/65536,add8_inc: 256/256 +``` + +它汇编 `examples/add8*.r8asm` 并在内置的 ROT-8 虚拟机里穷举输入。约 0.3 秒。 +`asm/test.r8asm` 那三件只证明"汇编得出来",不证明"跑得对" —— 这个区别踩过坑,见下面 +「字节级运算」。 + `r8forge_derive` 是验证工具: ```sh @@ -51,7 +69,7 @@ r8forge_derive -2 0=100 # 同上,另外倒出所有非零 ce ']' rnz 当前 cell 非零 -> 跳回匹配的 '[' ``` -由此推出两条**关键约束**: +由此推出三条**关键约束**: 1. **`set_0`(= `[-]`)只对 0 和 1 收敛。** `5` 会 `5->4->5->4` 死循环,`0x80` 永远不变,而且 tape 走不到下一条指令、不报错。 @@ -59,6 +77,53 @@ r8forge_derive -2 0=100 # 同上,另外倒出所有非零 ce → **清必须发生在 cell 还只放一位的时候。** 2. **合成的程序只碰每个 cell 的低 K 位**(K = 参与运算的输入位数),高位不追踪。 所以 `%synth` 出来的块只适用于"每格一位"的世界。 +3. **一个 cell 里放了多个位,它的位在程序里根本读不出来。** + 没有移位指令,只有 `ror`,而 `ror` 不改变"这个 cell 是不是 0",所以 `biz` 对整字节 + 永远只能回答"是不是 0";`flb` 只动最低位(写,不是读)。 + → **字节级运算只能一位一 cell**;"把一个字节放进一个 cell 再逐位处理"这条路在 + 这个 ISA 里不存在。详见下面「字节级运算」。 + +## 字节级运算(一位一 cell) + +先记结论:**库里原来那层"整字节放一个 cell + ror 轮转"的宏,在运行时全是坏的。** +全是实测,不是推的: + +| 调用 | 结果 | +|---|---| +| `raw_add` A=0,B=0 / 1+1 / 3+5 / 1+0 | 都跑不完(5M 步上限内 `out` 空) | +| `raw_add` A=127,B=130 | 跑完了,但 `out 0 0 0 0 0`,A/B/Ci/S/Co 全被毁 | +| `raw_and` 0xF0 & 0xCC、1 & 1、0 & 5 | 都跑不完 | +| `copy`(`lib.r8asm`)A=5 | 跑不完;只有 A=0 才正常返回 | + +原因就是约束 1 + 3:轮转驱动每轮都用 `set_0` 清一个已经装了 7 位的累加器,而切片里的 +`biz` 判的是整字节。**别去修它们**,那层的前提在 ROT-8 里不成立。 + +踩在这层上的还有两个现成文件,都别信: + +* `asm/test.r8asm`(自检里那 1666 字符)只证明**汇编**得出来;它跑的 `raw_add` 会卡住。 +* `asm/fibonacci.r8asm` 汇编出 8761 字符,跑起来只输出 `1 1` 就卡住。文件顶上那串 + `1 1 2 3 5 8 13 21 34 55` 是旧实现时代的记录,现在对不上。 + +能用的是一层新写的、一位一 cell 的东西: + +| 文件 | 内容 | +|---|---| +| `asm/byte_add.r8asm` | `byte_add %1`:8 位加法/累加。第 i 位占 `%1+4i`(A) `+4i+1`(B) `+4i+2`(进位) `+4i+3`(草稿);b7 的进位留在 `%1+30` | +| `asm/pack_byte.r8asm` | `pack_byte %1 %2 %3`:八个 flag(base、步长、目标)压成一个字节;`bit_raw_flip_if` 是它的积木 | +| `examples/add8.r8asm` | a+b,**1308 字符** | +| `examples/add8_inc.r8asm` | 连加三次 1,3142 字符,演示"可拼" | +| `tests/byte_add_test.cc` | `r8forge_byte_add_test`:65536 + 256 组合,0.3 秒 | + +四条实测属性: + +* **A 是累加器**:`byte_add` 之后 B 的八个 cell 全是 0,把下一个操作数摆进去再叫一次就接着加。 + 每次都清进位输入,所以是 mod 256(`add8_inc` 的进位只反映最后一次加的进位)。 +* **压字节是单向的**:`pack_byte` 之后 flag 全被吃掉;压出来的字节能 `out`,但**不能再参与运算** + (约束 3)。所以"字节级加法"的输入只能是 flag,输出才能是字节。 +* **cell 数 32**(8 组 quartet)。最早的共用进位版本只要 25 个 cell,但 8 个 bit 共用一个远端 + 进位 cell 让每片都付几十次 `.tp` 跳跃,实测 **2517 字符** —— 每 bit 自带进位 cell 换来 + 1308 字符,是划算的。 +* **拼接靠 A**:想算 a+b+c,就 `byte_add` 两次(第二次前把 c 摆进 B)。 ## %synth 相关 @@ -80,16 +145,17 @@ r8forge_derive -2 0=100 # 同上,另外倒出所有非零 ce | 库里的 7 个位运算块(and/or/xor/inc/add) | 合成版比手写短:`asm/test.r8asm` 1714 → 1666 字符 | | `bit_raw_add_nc`(4 cell 忽略进位切片) | flat 代价 **27**,完整全加器切片是 38;8/8 组合正确 | | `bit_raw_add`(6 cell 全加器切片) | 保留 A、B,配合轮转使用(原版库的做法) | +| `byte_add`(8 位加法器,一位一 cell) | 32 cell / 1308 字符;**65536 组合穷举全对** | +| `pack_byte`(八个 flag -> 一个字节) | 单向可写;`bit_raw_flip_if` 是唯一"条件翻转、不清目标"的积木 | ## 已经确认走不通的(别重试) * **3 个 cell 做加法切片**:层数上限 5 和 8 都报 `no program up to N layers`。 -* **轮转 + 合成切片**:轮转要求 A、B 保留,合成的程序只会吃掉操作数; - 把表改成"保留 A、B"后同样报 `no program up to 8 layers`。 - 只有手写的 `bit_raw_add` 能保留操作数(它有循环)。 -* **把每个 bit 拆进不同 cell 再拼字节级运算**:能跑通但 cell 数爆炸(8 位加法 25 个 cell), - 没有实用价值,相关文件已删除。 -* 字节级(2^16 项真值表)交给 `%synth`:搜索空间不可行,字节级只用轮转手写。 +* **轮转 + 合成切片做字节级运算**:`raw_add`/`raw_and`/`raw_or`/`raw_xor`/`raw_inc`/`copy`/ + `erase` 这一整层运行时是坏的(实测见上面「字节级运算」)。不是"只差操作数保留": + `set_0` 清不动带多个位的 cell,切片里的 `biz` 又只判整字节。 +* **把一个字节放进一个 cell 再逐位处理**:约束 3 —— 那些位读不出来,做不到。 +* 字节级(2^16 项真值表)交给 `%synth`:搜索空间不可行。 ## 命名与注释格式(`asm/lib.r8asm` 的约定) diff --git a/CMakeLists.txt b/CMakeLists.txt index ddf1b16..57943aa 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -26,6 +26,10 @@ target_link_libraries(r8forge_synth_test r8forge_core) add_executable(r8forge_derive src/derive.cc) target_link_libraries(r8forge_derive r8forge_core) +# Runs the byte level adders over every input combination. +add_executable(r8forge_byte_add_test tests/byte_add_test.cc) +target_link_libraries(r8forge_byte_add_test r8forge_core) + add_executable(r8forge_cost_par tests/cost_par.cc) target_link_libraries(r8forge_cost_par r8forge_core) diff --git a/README.md b/README.md index e3ef1c2..e28d63f 100644 --- a/README.md +++ b/README.md @@ -187,6 +187,47 @@ build/Release/r8forge_derive drv/bit_raw_add.r8asm 6 2. `map` 一项里的 bit0 落在 `%(K+1)`、bit1 落在 `%(K+2)`……超出 `params` 会直接报错。 3. `stat` 必须给 `2^params` 项,每项是那个输入组合下所有参数的取值。 +## 字节级运算:只能一位一 cell + +```asm +%include "asm/byte_add.r8asm" +%include "asm/pack_byte.r8asm" + +.var BITS 0 +.var P 15 + +byte_add BITS ; A 的八位在 0,4,...,28;B 的八位在 1,5,...,29 +pack_byte BITS 4 P ; 八位 -> 一个字节 +output P +output 30 ; b7 的进位 +``` + +`byte_add` 是 8 位加法器:第 i 位占 `BITS + 4i`(A)、`+4i+1`(B)、`+4i+2`(进位)、`+4i+3`(草稿)。 +四个 cell 紧邻,所以切片内部来回跳跃只有 1~2 步;进位到下一块用 `clb_bit_mov` 搬一格。 +A 同时是累加器 —— 算完 B 的八个 cell 归零,把下一个操作数摆进去再叫一次就接着加, +`examples/add8_inc.r8asm` 连加三次 1 就是这么写的。 + +**为什么不能"一个 cell 放一个字节再轮转"**:ROT-8 没有移位,只有 `ror`,而 `ror` 不改变 +"这个 cell 是不是 0",所以 `biz` 对整字节永远只能回答"是不是 0";`flb` 只写最低位,不读。 +一个带多个位的 cell,它的位在程序里根本读不出来 —— 再加上 `set_0` 对多字节 cell 不收敛。 +库里原来那层 `raw_add`/`raw_and`/`raw_or`/`raw_xor`/`raw_inc`/`copy` 全踩在这上面, +实测跑不完(连 `raw_add` 的 `0+0` 都卡死,`127+130` 能跑完但输出全 0)。所以字节级 +运算只能一位一 cell。 + +`r8forge_byte_add_test` 汇编 `examples/add8*.r8asm`,在 rot8 虚拟机里把每种输入都跑一遍, +0.3 秒: + +``` +add8: 1308 characters + all 65536 combinations ok +add8_inc: 3142 characters + all 256 combinations ok +byte level adders ok +``` + +`pack_byte` 是**单向**的:八个 flag 压成字节,压完 flag 全被吃掉;压出来的字节能 `out`, +但不能再参与运算。反过来把字节拆成八位 —— 做不到(同上)。 + ## 实现方式 `%synth` 在 `parse()` 里被读成一张真值表,随即调用搜索器;得到的块序列写成 @@ -318,6 +359,14 @@ cmake -S . -B build -G "Visual Studio 18 2026" -A x64 cmake --build build --config Release build\Release\r8forge.exe asm\test.r8asm > test.rot8 build\Release\r8forge_synth_test.exe # 合成 + 跑虚拟机自检 +build\Release\r8forge_byte_add_test.exe # 8 位加法器:65536 组合全对 +``` + +`build/` 已经有生成器了,重编译时**别用 PATH 上的另一个 cmake**(版本不一致会重新配置, +然后死在 `No preprocessor test for "PellesC"`);用配这个目录的那个: + +```powershell +& "C:\Program Files\Microsoft Visual Studio\18\Community\Common7\IDE\CommonExtensions\Microsoft\CMake\CMake\bin\cmake.exe" --build build --config Release ``` `CMakeLists.txt` 里没有 `set(CMAKE_CXX_FLAGS ...)`:那会把默认 flag 整个换掉,MSVC 下丢 @@ -332,6 +381,10 @@ src/r8asm.cc 汇编器实现 src/synth.cc 真值表 -> 搜索 tests/asm_test.cc 汇编器自检(纸带不变) tests/synth_test.cc 合成 + ROT-8 虚拟机自检 +tests/byte_add_test.cc 8 位加法器穷举自检(65536 组合) asm/ 标准库与既有实现 +asm/byte_add.r8asm 8 位加法器(一位一 cell) +asm/pack_byte.r8asm 八个 flag -> 一个字节 examples/full_add.r8asm 汇编期合成全加器 +examples/add8.r8asm a+b,结果压成一个字节省出来 ``` diff --git a/asm/byte_add.r8asm b/asm/byte_add.r8asm new file mode 100644 index 0000000..bc95557 --- /dev/null +++ b/asm/byte_add.r8asm @@ -0,0 +1,82 @@ +%include "asm/raw_add_nc.r8asm" + +; 8 位加法器,一位一 cell。A 既是操作数也是累加器:算完 A = (A + B) & 0xFF, +; 所以连着加第二个数时,只要把新的操作数摆进 B 的八个 cell 再叫一次就行。 +; +; 为什么不是"一个 cell 放一个字节再轮转":ROT-8 没有移位,只有循环右移(ror), +; 而 ror 不改变"这个 cell 是不是 0",所以 `biz` 对整字节永远只会回答"是不是 0"。 +; 一个带多个位的 cell,它的位在程序里读不出来 —— 而且 set_0 对多字节 cell 不收敛。 +; 实测 `asm/test.r8asm` 里的字节级 raw_add 就是这么死的(连 0+0 都跑不完)。 +; 所以字节级运算只能是一位一 cell。 +; +; 每个 bit 用 4 个 cell 的忽略进位切片(bit_raw_add_nc):A、B 被吃掉,进位就地在 +; 第 3 个 cell 里更新。8 个 bit 各自占一整块、块内四个 cell 紧邻,切片内部的来回 +; 跳跃就只有 1~2 步;进位和"和"各自用一条 clb_bit_mov 收到下一格。 +; +; 第 i 位(i = 0..7)占 %1 + 4i 起的四个 cell: +; +; %1 + 4i + 0 A_i 操作数 -> 和的第 i 位 +; %1 + 4i + 1 B_i 操作数,被吃掉(结束时 0) +; %1 + 4i + 2 进位 进 -> 出,结束时是第 i 位的进位输出 +; %1 + 4i + 3 草稿 切片先把和写在这里,再搬进 A_i +; +; 第 7 位的进位就是整个加法器的进位输出,留在 %1 + 30 上。 +; B 的位必须由调用者摆;B 用完变 0,可以直接摆下一轮的操作数。 + +; Macro byte_add +; %1: Cell base, bit i occupies %1 + 4i .. %1 + 4i + 3 as A, B, carry, scratch +%macro byte_add 1 +.anchor before_byte_add + +; 第 0 位的进位输入清零 +.tp %1 + 2 +set_0 + +bit_raw_add_nc %1 + 0 %1 + 1 %1 + 2 %1 + 3 +.tp %1 + 3 +clb_bit_mov %1 + 0 +.tp %1 + 2 +clb_bit_mov %1 + 6 + +bit_raw_add_nc %1 + 4 %1 + 5 %1 + 6 %1 + 7 +.tp %1 + 7 +clb_bit_mov %1 + 4 +.tp %1 + 6 +clb_bit_mov %1 + 10 + +bit_raw_add_nc %1 + 8 %1 + 9 %1 + 10 %1 + 11 +.tp %1 + 11 +clb_bit_mov %1 + 8 +.tp %1 + 10 +clb_bit_mov %1 + 14 + +bit_raw_add_nc %1 + 12 %1 + 13 %1 + 14 %1 + 15 +.tp %1 + 15 +clb_bit_mov %1 + 12 +.tp %1 + 14 +clb_bit_mov %1 + 18 + +bit_raw_add_nc %1 + 16 %1 + 17 %1 + 18 %1 + 19 +.tp %1 + 19 +clb_bit_mov %1 + 16 +.tp %1 + 18 +clb_bit_mov %1 + 22 + +bit_raw_add_nc %1 + 20 %1 + 21 %1 + 22 %1 + 23 +.tp %1 + 23 +clb_bit_mov %1 + 20 +.tp %1 + 22 +clb_bit_mov %1 + 26 + +bit_raw_add_nc %1 + 24 %1 + 25 %1 + 26 %1 + 27 +.tp %1 + 27 +clb_bit_mov %1 + 24 +.tp %1 + 26 +clb_bit_mov %1 + 30 + +bit_raw_add_nc %1 + 28 %1 + 29 %1 + 30 %1 + 31 +.tp %1 + 31 +clb_bit_mov %1 + 28 + +.tp before_byte_add +%endmacro diff --git a/asm/pack_byte.r8asm b/asm/pack_byte.r8asm new file mode 100644 index 0000000..d6cf201 --- /dev/null +++ b/asm/pack_byte.r8asm @@ -0,0 +1,75 @@ +; 把 8 个一位 cell 压成一个字节。 +; +; 这是"位 -> 字节"方向的唯一一条路:反过来(从一个带多个位的 cell 里读第 i 位)在 +; ROT-8 里做不到 —— 没有移位,而 ror 不改变"这个 cell 是不是 0",所以 `biz` 只能回答 +; "整字节是不是 0"。所以压好的字节是只写的:算得出来、能 out 出来,但没法和 1 相加, +; 也没法拆回一位一 cell。 + +; Macro bit_raw_flip_if +; %this: CellBit flag (0/1; 被吃掉,结束时为 0) +; %1: CellBit destination (flag 为 1 时翻转它的最低位,其余位不动) +; +; 注意 %1 不能用 set_0 清:它可能已经放了好几位,只允许 flb。 +%macro bit_raw_flip_if 1 +.anchor before_bit_raw_flip_if +.tp before_bit_raw_flip_if +biz + .tp %1 + flb + .tp before_bit_raw_flip_if + flb +rnz +%endmacro + +; Macro pack_byte +; %1: Cell base of the eight bits +; %2: Cell stride between them (1 means contiguous) +; %3: Cell destination, must be all zero on entry; ends up holding the byte +; +; 第 i 轮:flag 为 1 就翻转 %3 的最低位,然后把 %3 右旋一格。放进去的位每轮都被 +; 往高位推一格,总共推 8 - i 格,所以第 i 位正好落到第 i 位上。 +%macro pack_byte 3 +.anchor before_pack_byte + +.tp %1 + 0 * %2 +bit_raw_flip_if %3 +.tp %3 +ror + +.tp %1 + 1 * %2 +bit_raw_flip_if %3 +.tp %3 +ror + +.tp %1 + 2 * %2 +bit_raw_flip_if %3 +.tp %3 +ror + +.tp %1 + 3 * %2 +bit_raw_flip_if %3 +.tp %3 +ror + +.tp %1 + 4 * %2 +bit_raw_flip_if %3 +.tp %3 +ror + +.tp %1 + 5 * %2 +bit_raw_flip_if %3 +.tp %3 +ror + +.tp %1 + 6 * %2 +bit_raw_flip_if %3 +.tp %3 +ror + +.tp %1 + 7 * %2 +bit_raw_flip_if %3 +.tp %3 +ror + +.tp before_pack_byte +%endmacro diff --git a/examples/add8.r8asm b/examples/add8.r8asm new file mode 100644 index 0000000..ca9d975 --- /dev/null +++ b/examples/add8.r8asm @@ -0,0 +1,22 @@ +; 8 位加法器:两个 8 位数相加,一位一 cell,结果压成一个字节省出来。 +; +; 第 i 位占 cell 4i..4i+3:A_i、B_i、进位_i、草稿_i(见 asm/byte_add.r8asm)。 +; add 之后 A 的八个 cell 就是和,B 的八个 cell 是 0,进位停在 cell 4*7+2 = 30。 +; +; out 的第 1 个字节是 (A+B) & 0xFF,第 2 个是 b7 的进位。 +; +; 压字节的累加器用 cell 15(第 3 位的草稿 cell):add 之后它一定是 0,而且离 +; 八个 flag 都不远,比放到数组外面省 300 多个字符。 +%include "asm/byte_add.r8asm" +%include "asm/pack_byte.r8asm" + +.var BITS 0 +.var P 15 + +byte_add BITS + +; A 的八位在 0, 4, 8, ... 28 上,步长 4 +pack_byte BITS 4 P + +output P +output 30 diff --git a/examples/add8_inc.r8asm b/examples/add8_inc.r8asm new file mode 100644 index 0000000..42fd40e --- /dev/null +++ b/examples/add8_inc.r8asm @@ -0,0 +1,26 @@ +; 自增三次:证明 byte_add 是可以拼的 —— A 是累加器,B 每次都被吃掉,所以只要把 +; 新的操作数(这里是常量 1)摆回 B 的最低一位,再叫一次 byte_add 就行。 +; +; 第 i 位占 cell 4i..4i+3,A 在 4i 上,B 的最低一位在 cell 1 上。 +; out 的第 1 个字节是 (A+3) & 0xFF,第 2 个是 b7 的进位。 +%include "asm/byte_add.r8asm" +%include "asm/pack_byte.r8asm" + +.var BITS 0 +.var P 15 + +; B 的 cell 1 在 add 之后一定是 0,所以 flb 就是"摆一个 1 进去" +%macro byte_inc 1 +.tp %1 + 1 +flb +byte_add %1 +%endmacro + +byte_inc BITS +byte_inc BITS +byte_inc BITS + +pack_byte BITS 4 P + +output P +output 30 diff --git a/tests/byte_add_test.cc b/tests/byte_add_test.cc new file mode 100644 index 0000000..a63fa82 --- /dev/null +++ b/tests/byte_add_test.cc @@ -0,0 +1,167 @@ +// Assembles the byte-level adders (examples/add8*.r8asm) and runs them on a tiny +// ROT-8 machine for every input combination. This is the runtime check the +// library never had: pass/fail comes from executing the tape, not from looking +// at the character count. +// +// r8forge_byte_add_test [repo root] +// +// The paths in the sources are relative to the repo root, so run it from there. +#include "r8forge/r8asm.hh" +#include +#include +#include +#include + +namespace { + +struct machine { + std::vector cells = std::vector(256, 0); + std::vector out; + size_t dp = 0; + size_t steps = 0; + bool finished = true; // false: the step limit ran out (a loop that never ends) + + static uint8_t ror(uint8_t v) { return uint8_t((v >> 1) | (v << 7)); } + + void run(const std::string &tape, size_t limit) { + for (size_t pc = 0; pc < tape.size(); pc++) { + if (++steps > limit) { + finished = false; + return; + } + switch (tape[pc]) { + case '>': + dp++; + break; + case '<': + dp--; + break; + case '-': // flb + cells[dp] ^= 1; + break; + case '+': // ror + cells[dp] = ror(cells[dp]); + break; + case '[': + if (cells[dp] == 0) { + int depth = 1; + while (depth) { + pc++; + if (tape[pc] == '[') + depth++; + else if (tape[pc] == ']') + depth--; + } + } + break; + case ']': + if (cells[dp] != 0) { + int depth = 1; + while (depth) { + pc--; + if (tape[pc] == ']') + depth++; + else if (tape[pc] == '[') + depth--; + } + } + break; + case '.': + out.push_back(cells[dp]); + break; + default: // ',' (input) is not modelled + break; + } + } + } +}; + +// The examples lay bit i out in cell 4i + off: off 0 is A, 1 is B, 3 is scratch. +void put_bits(machine &m, int off, int value) { + for (int b = 0; b < 8; b++) + if (value & (1 << b)) + m.cells[size_t(4 * b + off)] ^= 1; +} + +std::string assemble(const std::string &file) { + r8asm assembler; + if (!assembler.import_file(file, 8) || assembler.failed()) { + std::cerr << "could not assemble " << file << "\n"; + std::exit(1); + } + return assembler.export_rot8(); +} + +int failures = 0; + +void report(const std::string &what, int a, int b, const machine &m, int sum, + int carry) { + if (failures++ < 5) + std::cerr << " WRONG " << what << " a=" << a << " b=" << b << " out"; + if (failures <= 5) { + for (uint8_t v : m.out) + std::cerr << " " << int(v); + std::cerr << " (expect low " << (sum & 0xff) << ", carry " << carry + << "), finished=" << m.finished << " steps=" << m.steps << "\n"; + } +} + +// Every (a, b) pair through examples/add8.r8asm. +int test_add(const std::string &file) { + const std::string tape = assemble(file); + std::cout << "add8: " << tape.size() << " characters\n"; + int bad = 0; + for (int a = 0; a < 256; a++) + for (int b = 0; b < 256; b++) { + machine m; + put_bits(m, 0, a); + put_bits(m, 1, b); + m.run(tape, 200'000); + const int sum = a + b; + const bool ok = m.finished && m.out.size() == 2 && + m.out[0] == (sum & 0xff) && m.out[1] == (sum >> 8); + if (!ok) { + bad++; + if (bad <= 3) + report("add8", a, b, m, sum, sum >> 8); + } + } + std::cout << " " << (bad ? "FAILED" : "all 65536 combinations ok") + << (bad ? " (" + std::to_string(bad) + " wrong)" : "") << "\n"; + return bad; +} + +// examples/add8_inc.r8asm adds 1 three times, so the carry is only the carry of +// that last add: earlier carries are dropped (the accumulator is 8 bits). +int test_inc(const std::string &file) { + const std::string tape = assemble(file); + std::cout << "add8_inc: " << tape.size() << " characters\n"; + int bad = 0; + for (int a = 0; a < 256; a++) { + machine m; + put_bits(m, 0, a); + m.run(tape, 400'000); + const int low = (a + 3) & 0xff; + const int carry = ((a + 2) & 0xff) == 0xff ? 1 : 0; + const bool ok = m.finished && m.out.size() == 2 && m.out[0] == low && + m.out[1] == carry; + if (!ok) { + bad++; + if (bad <= 3) + report("add8_inc", a, 3, m, a + 3, carry); + } + } + std::cout << " " << (bad ? "FAILED" : "all 256 combinations ok") + << (bad ? " (" + std::to_string(bad) + " wrong)" : "") << "\n"; + return bad; +} + +} // namespace + +int main(int argc, char **argv) { + const std::string root = argc > 1 ? argv[1] : "."; + const int bad = test_add(root + "/examples/add8.r8asm") + + test_inc(root + "/examples/add8_inc.r8asm"); + std::cout << (bad ? "FAILED\n" : "byte level adders ok\n"); + return bad ? 1 : 0; +}