Files
R8Forge/asm/test.r8asm
T
dsh 2e6d9cee86 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.
2026-11-08 00:15:39 +08:00

533 lines
13 KiB
Plaintext

; asm/test.r8asm —— 标准库的"全家桶"测试。
;
; 目标:**每个标准宏至少被真的调用一次**,顺带把 r8asm 的语言特性全走一遍
; (%include / %macro / %rep / %for / %synth 的 map 和 stat / .var / .anchor /
; .tp / .xor / step / back / loop / endloop / %this)。
;
; 每一段调用完就把结果 `output` 出来,所以整条纸带的 OUT 流就是这本书的指纹:
; 哪个宏回归了,这一行数就会变。r8forge_examples_test 会用下面这行 `; out`
; 核对它(那个测试顺带跑这个文件,不只跑 examples/)。
;
; 段落顺序 = 从最小到最大:
; 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
; 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
; ---------------------------------------------------------------- A 基础设施
.var A 0
.var B 1
.var T 2
.var U 3
; A1 set_1 / set_0:只动最低位
.tp A
set_1
output A ; 1
.tp A
set_0
output A ; 0
; A2 .xor 装常数,erase 清整格
.tp B
.xor 2047
output B ; 2047
erase B
output B ; 0
; A3 rol:1 左旋一格是 2(rol = _CELL_SIZE-1 次 ror)
load A 1
.tp A
rol
output A ; 2
; 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
; 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
; 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
clb_add_nc H C1 S1 Ci1
clb_mul S1 MU PL PH MCA MS MCi MT MF MNR
output PL ; 1322
output PH ; 5
output S1 ; 82(被乘数保留)
output MU ; 0(乘数被吃干净)
; 第二轮:同一组草稿格,先清掉上一轮留下的东西
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