asm/test.r8asm: call every macro, exercise every language feature

The old test.r8asm was a three round hash: `clb_add_nc` + `clb_mul`, nothing
else.  It proved "add and multiply are enough for a hash" and it was the file
every session was told to check, but a regression in, say, clb_divmod or raw_sub
could not show up there.

It is now the library's kitchen sink instead: one call per standard macro,
grouped from the smallest to the biggest

    A  lib        set_0 set_1 erase rol step back .anchor copy bit_copy
                  copy_to_buffer bit_copy_to_buffer clb_bit_mov
    B  bit slices clb_bit_add_nc clb_bit_inc_nc bit_raw_add/inc/and/or/xor
                  clb_bit_and/or/xor
    C  raw_*      raw_add raw_inc raw_and raw_or raw_xor raw_sub (operands kept)
    D  clb_*      clb_add_nc clb_inc_nc clb_neg_nc clb_sub_nc clb_not_ip
                  clb_inc_ip clb_neg_ip clb_dec_ip clb_nonzero clb_add_ip
    E  big        clb_ones + loop/endloop, clb_divmod, clb_digits
    F  language   a user %macro that uses %rep and %for, .var arithmetic,
                  two inline %synth blocks (map and stat, both cache hits)
    G  the old hash, now two rounds

Each call is followed by `output`, so the OUT stream is the fingerprint.
examples_test now runs this file too and compares it with the `;   out` line in
the header (it already did that for examples/), which makes the file an actual
assertion instead of a length in the docs: an example that stops printing what
its header says fails the test.

Numbers: 58722 characters, 215169 steps, 100 values.  r8forge_derive,
r8forge_tape_run and ROT-8-viz's rot8_test produce the identical stream (the
last six values are the hash: 1322 5 82 0 1786 94).

Two layout decisions come from that cross check:

  * The tape has to stay under 65536 instructions: that is ROT-8-viz's
    INS_SIZE (include/rot8/rot8.h) and read_tape truncates silently past it.
    An earlier version of this file was 76047 and the viz stopped after 99 of
    its 104 outputs, which is how the limit was found.
  * So the hash keeps two rounds and the two rounds share one clb_mul scratch
    block (erased in between) instead of carrying three separate ones.

The hash's own rules are unchanged: the multiplier stays 141 because it is the
historical value, not because small operands must sit in MUL any more.

SPEC 10.2, the AGENTS baseline/self-check/table and the README build snippet
are updated to the new numbers and to the 65536 limit.
This commit is contained in:
dsh committed 2026-11-08 00:15:39 +08:00
1 parent b666799950
commit 2e6d9cee86
5 files changed
+563 -116

No files matched your search

+9 -9
View File
@@ -38,9 +38,9 @@
**当前基线(都通过,别弄坏)**:
```sh
r8forge asm/test.r8asm # 28729 字符
r8forge asm/test.r8asm # 58722 字符(调用每个标准宏 + 每个语言特性)
r8forge_byte_op_test # 20 项(clb_divmod 16384 组、clb_digits 2048 个值)
r8forge_examples_test # examples/ 里 13 个程序,头部 `; out` 逐个核对
r8forge_examples_test # 13 个 example + asm/test.r8asm,头部 `; out` 逐个核对
r8forge_macro_docs_test # 47 macros in 21 files
r8forge_synth_test examples/full_add.r8asm # all eight combinations ok
r8forge_derive verify_loop.r8asm -1 0=5 # out 5 0
@@ -125,7 +125,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,28729 字符(3 轮 hash)
r8forge asm/test.r8asm # exit 0,58722 字符(全家桶:每个宏至少一次)
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 自增切片,进位同样进->出
@@ -230,8 +230,8 @@ inline constexpr uint32_t kDefaultCellMask = (1u << kDefaultCellSize) - 1u;
`fibonacci` 8761 → **11959**。写死的长度断言要跟着改。
* **`output` 打印整个 cell 的值**,不再是一个字节;`out` 里出现 257、1088 这种数是对的
(`127+130 = 257`、`200*200 = 19*2048 + 1088`)。
* **交叉验证要先对齐几何**:几何一致时同一条纸带输出完全相同(`asm/test.r8asm` 两边都是
`1322 1786 1600 5 94 127`);不一致时不是谁错了,是两台不同的机器。workspace 里的
* **交叉验证要先对齐几何**:几何一致时同一条纸带输出完全相同(`asm/test.r8asm` 的最后 6 个值
`1322 5 82 0 1786 94`,三份实现逐位一致);不一致时不是谁错了,是两台不同的机器。workspace 里的
`ROT-8-viz/` 是 upstream `starcells/ROT-8` 的 fork(`origin` = `dsh/ROT-8-viz`,`main`),
默认 **11 位 x 259 格**,几何还能运行期改(`--cell` / `--mem`;默认值与字段在
`include/rot8/rot8.h`,名字就是 `cell_size` / `mem_size`)。对答案的办法:
@@ -392,8 +392,8 @@ W 轮之后 `(PH:PL)` 就是积;`PL`(原来的 Q)被移空,所以 **MUL
纸带同时短了:**5007 → 3977 字符**。代价只有一条:乘数是 0/1 这种小值时反而慢一点点
(旧版循环 0/1 次就完了,新版固定要转满 `_CELL_SIZE` 轮)——拿它换「乘数再大也不会挂」。
所以 `asm/test.r8asm` 里那条"永远把小操作数放 MUL"的规矩**已经没用了**(保留 141 只是
为了跟历史结果对得上)。
所以 `asm/test.r8asm` 现在把 141 放在乘数位只是**沿用历史值**,不再有"小操作数放 MUL"
这条规矩要守(hash 的两轮各乘一次 141,步数一共两万多)。
拼法上踩过两个坑:
@@ -469,7 +469,7 @@ W 轮之后 `(PH:PL)` 就是积;`PL`(原来的 Q)被移空,所以 **MUL
| 东西 | 结论 |
|---|---|
| 库里的 7 个位运算块(and/or/xor/inc/add) | 合成版比手写短(8 位时代 1714 → 1666;位宽改了以后位级块不受影响,长度变的是字节级那层) |
| `asm/test.r8asm` 现在是一个 hash | 11 位下 **28729 字符**,`h = (h + c) * 141 mod 2048` 滚过 "R8F",跑出 `1322 1786 1600 5 94 127`(前三 = 三轮的 h,后三 = 三轮的 PH,即 `(h+c)*141 >> 11`)。只用 `clb_add_nc` + `clb_mul`,没有位运算 —— "会加减乘就能算 hash"的最小证明。`derive` 与 ROT-8-viz 两边逐位一致,**57330 步**(乘法器换成移位-加法之后,原来约 207 万步) |
| `asm/test.r8asm` 全家桶测试 | 11 位下 **58722 字符**、**215169 步**、100 个 `out`。`asm/` 里每个宏至少被调一次(含 `bit_raw_*`/`clb_bit_*`/`raw_*`/`clb_*_nc`/`clb_*_ip`/`clb_nonzero`/`clb_ones`/`clb_mul`/`clb_divmod`/`clb_digits`/`copy`/`bit_copy`/`*_to_buffer`/`loop`),语言特性也各走一遍(`%macro`/`%rep`/`%for`/`%synth` 的 map 和 stat/`.var`/`.anchor`/`step`/`back`)。头部 `; out ...` 是期望值,`r8forge_examples_test` 逐项核对;`derive`、`tape_run`、ROT-8-viz 三份实现一致。**必须留在 65536 条指令以下**(ROT-8-viz 的 `INS_SIZE`),所以 hash 只留两轮(尾 6 个值 `1322 5 82 0 1786 94`) |
| `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 |
@@ -488,7 +488,7 @@ W 轮之后 `(PH:PL)` 就是积;`PL`(原来的 Q)被移空,所以 **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 项 |
| `examples/` 里的例程 | **每组宏一份**,9 个程序(§10.3 的表)+ 4 个 `%synth` 例子。每个都是能跑的程序,头部 `; out ...` 是它打印什么;`r8forge_examples_test` 全部跑一遍逐项核对(缺那行也算失败),顺带打印纸带长度和步数 |
| `examples/` 里的例程 | **每组宏一份**,9 个程序(§10.3 的表)+ 4 个 `%synth` 例子。每个都是能跑的程序,头部 `; out ...` 是它打印什么;`r8forge_examples_test` 全部跑一遍逐项核对(缺那行也算失败),顺带打印纸带长度和步数 —— 它还会跑 `asm/test.r8asm`(全家桶) |
## 已经确认走不通的(别重试)
+2 -1
View File
@@ -503,9 +503,10 @@ cost(layer) = 2 # biz + rnz
```powershell
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.exe asm\test.r8asm > test.rot8 # 全家桶:每个标准宏至少一次
build\Release\r8forge_synth_test.exe # 合成 + 跑虚拟机自检
build\Release\r8forge_byte_op_test.exe # 字节级库穷举:65536×4 + 512 全对
build\Release\r8forge_examples_test.exe # examples/ 13 个 + asm/test.r8asm,核对头部 `; out`
build\Release\r8forge_macro_docs_test.exe # 宏注释格式(asm/ 下每个 %macro)
```
+19 -7
View File
@@ -654,13 +654,25 @@ clb_add_nc PL B_lo T Ci ; 低字节相加,进位出到 Ci(A、B 被
clb_add_ip PH Ci S F ; 把进位加进高字节(Ci 被吃)
```
### 10.2 `asm/test.r8asm`:一个滚动的 hash
### 10.2 `asm/test.r8asm`:标准库的"全家桶"测试
`h = (h + c) * 141 mod 2048`,消息 `"R8F"`(82 56 70),只用 `clb_add_nc` + `clb_mul`:
先把消息字节加到 h 上,再乘 141,跑三轮。11 位下纸带 **28729 字符**,
`r8forge_derive` / `r8forge_tape_run` / ROT-8-viz 三份实现都给出
`1322 1786 1600 5 94 127`(前三 = 三轮的 h,后三 = 三轮的 PH,即 `(h+c)*141 >> 11`),
共 **57330 步**。
不是演示,是**回归测试**:`asm/` 里每个宏至少被真的调用一次,r8asm 的语言特性也各走一遍
(`%include` / `%macro` / `%rep` / `%for` / `%synth` 的 `map` 和 `stat` / `.var` / `.anchor` /
`.tp` / `.xor` / `step` / `back` / `loop` / `endloop`)。每段调用完就 `output`,所以
整条 OUT 流就是指纹,头部 `; out ...` 写着期望值 —— `r8forge_examples_test` 会跑它并逐项
核对(同一个测试也跑 `examples/`,见 §10.3)。
段落:A 基础设施 → B 位级切片 → C `raw_*`(保留操作数)→ D `clb_*`(破坏性)→
E 大宏(`clb_ones` / `loop` / `clb_divmod` / `clb_digits`)→ F 语言特性(自定义
`%macro` + `%rep` + `%for` + 两份 `%synth`)→ G 两轮 hash(`clb_add_nc` + `clb_mul`)。
11 位下纸带 **58722 字符**、**215169 步**、100 个 `out`。两个约束值得记住:
* **必须留在 65536 条指令以下**:ROT-8-viz 的 `INS_SIZE`(`include/rot8/rot8.h`)是 65536,
超了 `read_tape` 会截断,交叉验证就废了。所以 hash 只留两轮、每个宏只调一次。
* 三个实现(`r8forge_derive`、`r8forge_tape_run`、ROT-8-viz 的 `rot8_test`)对这份 `out`
逐位一致;最后 6 个值 `1322 5 82 0 1786 94` 是老 hash 的指纹
(`1322`/`1786` = 两轮的 h,`5`/`94` = 两轮的 PH,`82` = 被乘数保留、`0` = 乘数被吃)。
### 10.3 `examples/` 里的例程:每组宏一份
@@ -696,7 +708,7 @@ r8forge_tape_run t.rot8 # 打印 out 和步数
|---|---|---|
| `r8forge_byte_op_test` | 20 个测试项 = 19 个宏(`clb_neg_nc` 的 Ci=1 / Ci=0 各算一项),见 §7.1、§7.2、§7.5、§7.6、§7.7、§8、§9 | **按返回值**:穷举 0..255(`clb_add_ip` 2048 抽样、`clb_mul` 65536 全穷举、`clb_ones` 12 个计数全穷举、`clb_divmod` 8 个除数 × 全部 2048 个被除数 = 16384 组全穷举、`clb_digits` 全部 2048 个值全穷举),任一不符即退出码非 0 |
| `r8forge_synth_test examples/full_add.r8asm` | `%synth` 端到端:汇编 + 跑 8 种输入 | `all eight combinations ok` |
| `r8forge_examples_test` | `examples/` 里全部 13 个程序(9 组宏例程 + 4 个 `%synth` 例子,见 §10.3) | **按返回值**:每个头部 `; out ...` 与实跑 `out` 逐项相等,缺这一行也算失败 |
| `r8forge_examples_test` | `examples/` 里全部 13 个程序(9 组宏例程 + 4 个 `%synth` 例子,见 §10.3)**加上 `asm/test.r8asm`**(§10.2) | **按返回值**:每个头部 `; out ...` 与实跑 `out` 逐项相等,缺这一行也算失败 |
| `r8forge verify_clb_bit_add_nc.r8asm` | 4 cell 加法切片(进位进→出) | 单点:`0+0+Ci` |
| `r8forge verify_clb_bit_inc_nc.r8asm` | 3 cell 自增切片 | 单点 |
| `r8forge_derive verify_loop.r8asm -1 0=N` | 动态循环:`N=0..11` 时循环体跑 `N` 次(`A = N`、`C = 0`) | 逐值 `out` |
+513 -91
View File
@@ -1,110 +1,532 @@
; 一个 hash:把消息当成多项式滚一遍,每步 h = (h + c) * 141 mod 2048。
; asm/test.r8asm —— 标准库的"全家桶"测试。
;
; 141 是奇数,所以乘它在 mod 2^11 上可逆 —— 每个输入位都能影响到结果,
; 这就是 hash 要的那种"雪崩"的弱化版(11 位也别指望真雪崩)。
; 为什么不用 FNV 那个 403:那是老乘法器的遗留 —— 老的 clb_mul 是重复加法,
; 轮数 = MUL 的值,403 放 MUL 会顶到 500 万步上限(141 那版三轮约 207 万步)。
; 现在的 clb_mul 是移位-加法,轮数只跟位宽有关(MC=100、MUL=403 实测 10210 步),
; 乘数多大都不再影响能不能跑完;这里留着 141 只是为了跟以前的结果对得上。
; **hash 的强度花在运行时间上,纸带长度完全不变** —— 这就是本仓库的取舍。
; 目标:**每个标准宏至少被真的调用一次**,顺带把 r8asm 的语言特性全走一遍
; (%include / %macro / %rep / %for / %synth 的 map 和 stat / .var / .anchor /
; .tp / .xor / step / back / loop / endloop / %this)。
;
; 只用两样东西:clb_add_nc(加)和 clb_mul(乘)。没有位运算,
; "会加减乘就能算 hash"的最小证明。
; 每一段调用完就把结果 `output` 出来,所以整条纸带的 OUT 流就是这本书的指纹:
; 哪个宏回归了,这一行数就会变。r8forge_examples_test 会用下面这行 `; out`
; 核对它(那个测试顺带跑这个文件,不只跑 examples/)。
;
; 消息是 "R8F" = 82 56 70(ASCII 高位置 0,11 位 cell 正好装得下)。
; 手算:
; h1 = (0 + 82) * 141 mod 2048 = 1322
; h2 = (1322 + 56) * 141 mod 2048 = 1786
; h3 = (1786 + 70) * 141 mod 2048 = 1600
; 段落顺序 = 从最小到最大:
; A 基础设施 set_0 / set_1 / erase / rol / step / back / .anchor /
; copy / bit_copy / copy_to_buffer / bit_copy_to_buffer /
; clb_bit_mov
; B 位级切片 clb_bit_add_nc / clb_bit_inc_nc / bit_raw_add / bit_raw_inc /
; bit_raw_and / bit_raw_or / bit_raw_xor / clb_bit_and /
; clb_bit_or / clb_bit_xor
; C 字节级 raw_* raw_add / raw_inc / raw_and / raw_or / raw_xor / raw_sub
; (这些保留操作数)
; D 字节级 clb_* clb_add_nc / clb_inc_nc / clb_neg_nc / clb_sub_nc /
; clb_not_ip / clb_inc_ip / clb_neg_ip / clb_dec_ip /
; clb_nonzero / clb_add_ip(这些吃掉操作数)
; E 大宏 clb_ones / loop+endloop / clb_divmod / clb_digits
; F 语言特性 %macro + %rep + %for / %synth(map 和 stat 各一份)
; G 老本行 两轮 hash(h = (h+c)*141 mod 2048),顺带 clb_mul
;
; 纸带必须留在 65536 条指令以下(ROT-8-viz 的 INS_SIZE),所以这里只放
; "每个宏一次" + 两轮 hash;想加更多调用前先看长度,超了那边就跑不完整。
;
; 没有 %include 的只有 asm/fibonacci.r8asm:那个文件是**程序**不是宏库,
; 包含进来会把它的代码直接插进纸带。
; out 1 0 2047 0 2 1 5 5 5 1 5 0 9 1 0 1 0 1 1 1 0 1 1 1 0 1 0 0 1 0 1 1 0 100 57 157 0 0 1 41 42 0 8 14 6 100 30 70 1 1978 0 157 0 0 1 2043 7 1 2042 42 2043 41 0 0 7 1 105 0 0 15 3 0 204 7 2047 10 49 50 51 52 0 7 10 11 12 20 21 7 7 7 0 1 0 1 1322 5 82 0 1786 94
%include "asm/lib.r8asm"
%include "asm/raw_add.r8asm"
%include "asm/raw_inc.r8asm"
%include "asm/raw_and.r8asm"
%include "asm/raw_or.r8asm"
%include "asm/raw_xor.r8asm"
%include "asm/raw_sub.r8asm"
%include "asm/raw_add_nc.r8asm"
%include "asm/raw_inc_nc.r8asm"
%include "asm/raw_neg_nc.r8asm"
%include "asm/raw_sub_nc.r8asm"
%include "asm/not_ip.r8asm"
%include "asm/inc_ip.r8asm"
%include "asm/dec_ip.r8asm"
%include "asm/add_ip.r8asm"
%include "asm/ones.r8asm"
%include "asm/mul.r8asm"
%include "asm/divmod.r8asm"
%include "asm/digits.r8asm"
; Macro load
; %1: Cell operand
; %2: Count value
;
; 这个文件自己的小工具:先把整格清干净,再 `.xor` 上常数。
; `.xor` 是异或,不清格就变成"和旧值异或",所以每次都先 erase。
; (它也顺便演示了一个普通宏怎么调库里的宏。)
%macro load 2
erase %1
.tp %1
.xor %2
%endmacro
; h 的初值就是 cell 的初值 0,不用写
.var H 0
; Macro stamp
; %1: Cell first
; %2: Count how_many
; %3: Count base
;
; %macro 里嵌 %for:循环变量既进 `.tp` 的表达式,也进 `.xor` 的表达式。
%macro stamp 3
%for k %2
.tp %1 + k
.xor %3 + k
%endfor
%endmacro
.var C1 2
.var C2 4
.var C3 6
; ---------------------------------------------------------------- A 基础设施
.var A 0
.var B 1
.var T 2
.var U 3
.var S1 8
.var S2 10
.var S3 12
; A1 set_1 / set_0:只动最低位
.tp A
set_1
output A ; 1
.tp A
set_0
output A ; 0
; 进位输入:clb_add_nc 不动它的初值,给 0 就是普通加法
.var Ci1 14
.var Ci2 16
.var Ci3 18
; A2 .xor 装常数,erase 清整格
.tp B
.xor 2047
output B ; 2047
erase B
output B ; 0
; 三轮乘法各用一组 cell,互不打扰
.var MC1 20
.var MU1 21
.var PL1 22
.var PH1 23
.var MCA1 24
.var MS1 25
.var MCi1 26
.var MT1 27
.var MF1 28
.var MNR1 29
; A3 rol:1 左旋一格是 2(rol = _CELL_SIZE-1 次 ror)
load A 1
.tp A
rol
output A ; 2
.var MC2 30
.var MU2 31
.var PL2 32
.var PH2 33
.var MCA2 34
.var MS2 35
.var MCi2 36
.var MT2 37
.var MF2 38
.var MNR2 39
; A4 step / back / .anchor:dp 走到 cell 4 再回来
.tp 4
set_1
.tp A
step 2 ; 0 -> 4(step 1 = 两条 stp)
.anchor landed
back 1 ; 4 -> 2
.tp landed ; 再跳回 cell 4
output landed ; 1
.var MC3 40
.var MU3 41
.var PL3 42
.var PH3 43
.var MCA3 44
.var MS3 45
.var MCi3 46
.var MT3 47
.var MF3 48
.var MNR3 49
; A5 copy:整格拷贝,源保留,缓冲由调用者给(T 这时是干净的)
load A 5
copy B T
output A ; 5
output B ; 5
; A6 bit_copy:只搬最低位(A=4,搬进 B 的 1 -> 5)
load A 4
load B 1
.tp B
bit_copy A T
output A ; 5
output B ; 1
; 消息和乘数放进 cell
.tp C1
.xor 82
.tp C2
.xor 56
.tp C3
.xor 70
.tp MU1
.xor 141
.tp MU2
.xor 141
.tp MU3
.xor 141
; A7 clb_bit_mov:搬一位,源被吃掉
.tp B
clb_bit_mov A
output A ; 5
output B ; 0
; A8 copy_to_buffer / bit_copy_to_buffer:把 dp 那一格搬到下一格
load 6 9
copy_to_buffer U
output 7 ; 9
.tp 7
erase 7 ; bit 版只动最低位,先清干净才能看出效果
.tp 6
set_1 ; cell 6 还是 9(奇数),set_1 之后不变
bit_copy_to_buffer U
output 7 ; 1
; ---------------------------------------------------------------- B 位级切片
.var PA 10
.var PB 11
.var PCi 12
.var PS 13
.var PCo 14
.var Pbuf 15
.var Pans 16
.var Ptmp 17
; B1 clb_bit_add_nc:1 + 1,进位进 0 -> S=0,进位出 1
load PA 1
load PB 1
erase PCi
clb_bit_add_nc PA PB PCi PS
output PS ; 0
output PCi ; 1
; B2 clb_bit_inc_nc:1 + 进位 1 -> S=0,进位出 1
load PA 1
load PCi 1
clb_bit_inc_nc PA PCi PS
output PS ; 0
output PCi ; 1
; B3 bit_raw_add:1 + 1,进位进 0 -> S=0,Co=1,A、B 保留
load PA 1
load PB 1
erase PCi
bit_raw_add PA PB PCi PS PCo Pbuf
output PA ; 1
output PB ; 1
output PS ; 0
output PCo ; 1
; B4 bit_raw_and / bit_raw_or / bit_raw_xor:1 和 1
load PA 1
load PB 1
bit_raw_and PA PB Pans Ptmp
output Pans ; 1
bit_raw_or PA PB Pans Ptmp
output Pans ; 1
bit_raw_xor PA PB Pans Ptmp
output Pans ; 0
; B5 clb_bit_and:手写的那个(3 cell,两个输入都被吃掉)
load PA 1
load PB 1
clb_bit_and PA PB Pans
output Pans ; 1
output PA ; 0
output PB ; 0
; B6 clb_bit_or / clb_bit_xor:合成的 3 cell 版
load PA 1
load PB 1
clb_bit_or PA PB Pans
output Pans ; 1
load PA 1
load PB 1
clb_bit_xor PA PB Pans
output Pans ; 0
; B7 bit_raw_inc:1 + 进位 0 -> S=1,Co=0,A 保留
load PA 1
erase PCi
bit_raw_inc PA PCi PS PCo Ptmp
output PA ; 1
output PS ; 1
output PCo ; 0
; ---------------------------------------------------------- C 字节级 raw_*
.var R1 20
.var R2 21
.var R3 22
.var Rci 23
.var Rco 24
.var Rtmp 25
.var Rbo 26
.var Rbc 27
.var Rbuf 28
.var Rci2 29
; C1 raw_add:100 + 57 = 157,进位出 0
load R1 100
load R2 57
erase Rci
raw_add R1 R2 Rci R3 Rco Rtmp
output R1 ; 100
output R2 ; 57
output R3 ; 157
output Rco ; 0
; C2 raw_add 的进位:2047 + 1 = 0(回绕),进位出 1
load R1 2047
load R2 1
erase Rci
raw_add R1 R2 Rci R3 Rco Rtmp
output R3 ; 0
output Rco ; 1
; C3 raw_inc:41 + 1 = 42(进位输入是真正的 1,不是"隐含加一"),A 保留
load R1 41
load Rci 1
raw_inc R1 Rci R3 Rco Rtmp
output R1 ; 41
output R3 ; 42
output Rco ; 0
; C4 raw_and / raw_or / raw_xor:12(1100) 和 10(1010)
load R1 12
load R2 10
raw_and R1 R2 R3 Rtmp
output R3 ; 8
raw_or R1 R2 R3 Rtmp
output R3 ; 14
raw_xor R1 R2 R3 Rtmp
output R3 ; 6
; C5 raw_sub:100 - 30 = 70,Bo=1(A >= B,没借位)
load R1 100
load R2 30
erase Rbc
erase Rci2
erase Rbuf
raw_sub R1 R2 R3 Rbo Rbc Rci2 Rbuf
output R1 ; 100
output R2 ; 30
output R3 ; 70
output Rbo ; 1
; C6 反过来:30 - 100 = 1978(回绕),Bo=0
load R1 30
load R2 100
erase Rbc
erase Rci2
erase Rbuf
raw_sub R1 R2 R3 Rbo Rbc Rci2 Rbuf
output R3 ; 1978
output Rbo ; 0
; ---------------------------------------------------------- D 字节级 clb_*
.var CA 30
.var CB 31
.var CS 32
.var CCi 33
.var CT 34
.var CF 35
; D1 clb_add_nc:100 + 57 = 157,进位进 0 出 0
load CA 100
load CB 57
erase CCi
clb_add_nc CA CB CS CCi
output CS ; 157
output CCi ; 0
; D2 clb_inc_nc:2047 + 进位 1 = 0,进位出 1
load CA 2047
load CCi 1
clb_inc_nc CA CS CCi
output CS ; 0
output CCi ; 1
; D3 clb_neg_nc:A=5、进位 1 -> -5 = 2043
load CA 5
load CCi 1
clb_neg_nc CA CS CCi
output CS ; 2043
; D4 clb_sub_nc:10 - 3 = 7,进位出 1(没借位)
load CA 10
load CB 3
load CCi 1
clb_sub_nc CA CB CS CCi
output CS ; 7
output CCi ; 1
; D5 clb_not_ip:A=5 -> 2042
load CA 5
clb_not_ip CA
output CA ; 2042
; D6 clb_inc_ip:41 -> 42
load CA 41
clb_inc_ip CA CT
output CA ; 42
; D7 clb_neg_ip:5 -> 2043
load CA 5
clb_neg_ip CA CT
output CA ; 2043
; D8 clb_dec_ip:42 -> 41
load CA 42
clb_dec_ip CA CT
output CA ; 41
; D9 clb_nonzero:B=0 -> F=0;B=7 -> F=1
erase CB
clb_nonzero CB CF CT
output CB ; 0
output CF ; 0
load CB 7
clb_nonzero CB CF CT
output CB ; 7
output CF ; 1
; D10 clb_add_ip:100 += 5 -> 105,B 被吃
load CA 100
load CB 5
clb_add_ip CA CB CT CF
output CA ; 105
output CB ; 0
; ---------------------------------------------------------------- E 大宏
.var OA 40
.var OB 41
.var OT 42
.var OF 43
; E1 clb_ones:4 -> 从末位起 4 个 1 = 15,输入被吃
load OA 4
clb_ones OA OB OT OF
output OA ; 0
output OB ; 15
; E2 loop / endloop:按运行期的 1 的个数跑 —— 3 个 1 就跑 3 圈
.var LC 44
.var LCNT 45
.var LCNT_T 46
load OA 3
clb_ones OA LC OT OF
loop LC
clb_inc_ip LCNT LCNT_T
endloop LC
output LCNT ; 3
output LC ; 0(配额正好用完)
; E3 clb_divmod:2047 / 10 = 204 余 7,N、D 都保留
.var DN 60
.var DD 61
.var DQ 62
.var DR 63
.var DS 64 ; S 起 7 格:64..70
load DN 2047
load DD 10
clb_divmod DN DD DQ DR DS
output DQ ; 204
output DR ; 7
output DN ; 2047
output DD ; 10
; E4 clb_digits:1234 -> '1' '2' '3' '4'
.var GN 80
.var REG 81 ; REG 起 4 格
.var GS 85 ; S 起 13 格:85..97
load GN 1234
clb_digits GN REG GS
output REG ; 49
output REG+1 ; 50
output REG+2 ; 51
output REG+3 ; 52
output GN ; 0(输入被吃)
; ------------------------------------------------------------ F 语言特性
; F1 .var 里也能算:先把 BASE 定义出来,再用它定义 NEXT
.var BASE 100
.var NEXT BASE + 1
load NEXT 7
output NEXT ; 7
; F2 %for:循环变量既进 .tp 也进 .xor
.var ROW 106
%for k 3
.tp ROW + k
.xor 10 + k
%endfor
output ROW ; 10
output ROW+1 ; 11
output ROW+2 ; 12
; F3 %macro 里嵌 %for
stamp 110 2 20
output 110 ; 20
output 111 ; 21
; F4 %rep:同一段文本抄 3 遍
%rep 3
output NEXT ; 7 7 7
%endrep
; F5 %synth:map 版(和 stat 版是同一张表,所以命中同一份缓存)
%synth fa_map
params 6
map 0 1 1 2 1 2 2 3
mode layers
%endsynth
; F6 %synth:stat 版,整张状态表(2^6 项)
%synth fa_stat
params 6
stat 0 9 10 19 12 21 22 31 0 9 10 19 12 21 22 31 0 9 10 19 12 21 22 31 0 9 10 19 12 21 22 31 0 9 10 19 12 21 22 31 0 9 10 19 12 21 22 31 0 9 10 19 12 21 22 31 0 9 10 19 12 21 22 31
mode layers
%endsynth
.var FA 120
.var FB 121
.var FCi 122
.var FS 123
.var FCo 124
.var FT 125
; 注意:合成的块只保证"输出位进来是 0"的调用(SPEC §3.6) ——
; 表对 S/Co/T 的初值是周期的,搜索器据此把这几位当成"不是输入"。
; 所以每次调用前都要把它们清干净,否则第二次调用就是垃圾。
erase FS
erase FCo
erase FT
load FA 1
load FB 1
erase FCi
fa_map FA FB FCi FS FCo FT
output FS ; 0(1+1+0 的和位)
output FCo ; 1(进位)
erase FS
erase FCo
erase FT
load FA 1
load FB 1
erase FCi
fa_stat FA FB FCi FS FCo FT
output FS ; 0
output FCo ; 1
; ---------------------------------------------------------------- G hash
; 老本行:h = (h + c) * 141 mod 2048,消息 "R8F" 的前两个字符 82、56。
; 两轮:h1 = 1322、h2 = 1786;PH 是 PL 装不下的那一半(5、94)。
;
; 这里只留两轮,是为了把整条纸带压在 ROT-8-viz 的 65536 条指令上限以下 ——
; 那个上限是 `INS_SIZE`(`include/rot8/rot8.h`),超了会被截断,交叉验证就没法做了。
; 两轮共用同一组 clb_mul 草稿格(每轮之间把它们清干净),省下一整份乘法器。
.var H 140
.var C1 141
.var C2 142
.var S1 143
.var S2 144
.var Ci1 145
.var Ci2 146
.var MU 147
.var PL 148
.var PH 149
.var MCA 150
.var MS 151
.var MCi 152
.var MT 153
.var MF 154
.var MNR 155
load C1 82
load C2 56
load MU 141
erase H
erase Ci1
erase Ci2
erase MS
erase MT
; 第一轮:h = (0 + 82) * 141
clb_add_nc H C1 S1 Ci1
clb_mul S1 MU1 PL1 PH1 MCA1 MS1 MCi1 MT1 MF1 MNR1
output PL1
clb_mul S1 MU PL PH MCA MS MCi MT MF MNR
output PL ; 1322
output PH ; 5
output S1 ; 82(被乘数保留)
output MU ; 0(乘数被吃干净)
; 第二轮:h = (1322 + 56) * 141
clb_add_nc PL1 C2 S2 Ci2
clb_mul S2 MU2 PL2 PH2 MCA2 MS2 MCi2 MT2 MF2 MNR2
output PL2
; 第三轮:h = (1786 + 70) * 141
clb_add_nc PL2 C3 S3 Ci3
clb_mul S3 MU3 PL3 PH3 MCA3 MS3 MCi3 MT3 MF3 MNR3
output PL3
; 顺便把三轮的 PH 打出来:乘法器给的是 (PH:PL) 两格的积,
; PH 就是 11 位装不下、进到第二个 cell 的那一半,等于 (h+c)*141 >> 11。
; 这一版三轮分别是 5 94 127 —— 也就是说中间结果其实都是超出 2047 的,
; 只是取模之后落在了 PL 里。
output PH1
output PH2
output PH3
; 第二轮:同一组草稿格,先清掉上一轮留下的东西
erase MU
erase MCA
erase MS
erase MT
load MU 141
clb_add_nc PL C2 S2 Ci2
clb_mul S2 MU PL PH MCA MS MCi MT MF MNR
output PL ; 1786
output PH ; 94
+20 -8
View File
@@ -161,18 +161,17 @@ int main(int argc, char **argv) {
std::sort(files.begin(), files.end());
int failures = 0;
for (const std::filesystem::path &file : files) {
const std::string name = file.filename().string();
auto check = [&](const std::filesystem::path &file, const std::string &name) {
std::vector<int> want;
if (!expected_out(file, want)) {
std::cout << name << ": no '; out ...' line in the header\n";
failures++;
continue;
return;
}
std::string tape;
if (!assemble(file, tape)) {
failures++;
continue;
return;
}
machine m;
m.run(tape);
@@ -187,10 +186,23 @@ int main(int argc, char **argv) {
failures++;
}
std::cout << "\n";
}
};
std::cout << (failures ? "FAILED: " + std::to_string(failures) + " example(s)\n"
: "all " + std::to_string(files.size()) +
" examples print what their headers say\n");
for (const std::filesystem::path &file : files)
check(file, file.filename().string());
// asm/test.r8asm is the flagship: it calls every macro in the library, so it
// gets exactly the same header check. It does not live in examples/ because
// it is a test, not a demo. The path is relative to the examples directory
// so this works whatever the working directory is.
const std::filesystem::path flagship = dir / ".." / "asm" / "test.r8asm";
const bool have_flagship = std::filesystem::exists(flagship);
if (have_flagship)
check(flagship, "asm/test.r8asm");
std::cout << (failures ? "FAILED: " + std::to_string(failures) + " file(s)\n"
: "all " + std::to_string(files.size()) + " examples" +
(have_flagship ? " and asm/test.r8asm" : "") +
" print what their headers say\n");
return failures ? 1 : 0;
}