From 1b8ff084557e32e344cfdc21ea4faf39c965601a Mon Sep 17 00:00:00 2001 From: dsh Date: Fri, 6 Nov 2026 23:02:09 +0800 Subject: [PATCH] Eight bit adders and increments, one bit per cell raw_add_bits8 A0..A7, B0..B7, S0..S7, carry = 25 cells raw_inc_bits8 a0..a7, s0..s7, carry = 17 cells Both follow the same protocol as the four bit version: no rotation, every cell holds a single bit, one slice per bit, and the final carry is dropped because the caller promises the result fits. The increment slices down to 3 cells -- a, carry (in -> out), s -- and %synth finds cost 25 in 4 blocks for it: s = a^Ci, carry = a&Ci. Its table was written as 8 entries because a 3 parameter stat line wants 2^3 values, and the s bit in the index does not matter, so the four entry pattern repeats. One mistake worth recording: set_1 takes no arguments and acts on the current cell, so 'set_1 %17' silently did nothing and the carry started at 0 instead of 1 -- which made 1+1 give 1 instead of 2 while 42+1 and 200+1 looked fine, since only the low bit was affected. The driver now does '.tp %17' then 'set_1'. Verified on the built in machine: raw_add_bits8 9 sums (100+50, 128+127, 200+55, 254+1, ...) 9/9 raw_inc_bits8 9 values (0, 1, 5, 15, 127, 200, 253, 254, ...) 9/9 Tapes: 2550 and 1514 characters. --- .r8forge_cache/5eaf6981c0c49914.txt | 5 ++++ asm/raw_add_bits8.r8asm | 26 +++++++++++++++++ asm/raw_inc_bits8.r8asm | 43 +++++++++++++++++++++++++++++ 3 files changed, 74 insertions(+) create mode 100644 .r8forge_cache/5eaf6981c0c49914.txt create mode 100644 asm/raw_add_bits8.r8asm create mode 100644 asm/raw_inc_bits8.r8asm diff --git a/.r8forge_cache/5eaf6981c0c49914.txt b/.r8forge_cache/5eaf6981c0c49914.txt new file mode 100644 index 0000000..2246075 --- /dev/null +++ b/.r8forge_cache/5eaf6981c0c49914.txt @@ -0,0 +1,5 @@ +# params 3 mode 0 metric 1 +1 4 0 +0 6 0 +2 3 1 +0 6 0 diff --git a/asm/raw_add_bits8.r8asm b/asm/raw_add_bits8.r8asm new file mode 100644 index 0000000..0530ed0 --- /dev/null +++ b/asm/raw_add_bits8.r8asm @@ -0,0 +1,26 @@ +%include "asm/lib.r8asm" +%include "asm/raw_add_nc4.r8asm" + +; 8 位加法器,每一位独占一个干净 cell,不做轮转: +; %1..%8 = A0..A7 +; %9..%16 = B0..B7 +; %17..%24 = S0..S7 +; %25 = 进位 +; +; 每个 cell 始终只放一位(0 或 1),所以 set_0 的前提成立;最高位产生的进位丢掉, +; 前提是 A+B <= 255(不进位输出)。 +; 总共 25 个 cell:每位 3 个 + 1 个进位。 +%macro raw_add_bits8 25 +.anchor before_raw_add_bits8 + erase %25 + + bit_add_nc4 %1 %9 %25 %17 + bit_add_nc4 %2 %10 %25 %18 + bit_add_nc4 %3 %11 %25 %19 + bit_add_nc4 %4 %12 %25 %20 + bit_add_nc4 %5 %13 %25 %21 + bit_add_nc4 %6 %14 %25 %22 + bit_add_nc4 %7 %15 %25 %23 + bit_add_nc4 %8 %16 %25 %24 + .tp before_raw_add_bits8 +%endmacro diff --git a/asm/raw_inc_bits8.r8asm b/asm/raw_inc_bits8.r8asm new file mode 100644 index 0000000..d4dcea0 --- /dev/null +++ b/asm/raw_inc_bits8.r8asm @@ -0,0 +1,43 @@ +%include "asm/lib.r8asm" + +; 忽略进位的自增切片,3 个 cell: +; p1..p3 = a, 进位(进->出), s +; +; 目标:s = a^Ci,进位 = a&Ci,a 结束为 0。 +; 表由定义直接算出:(a,Ci) = (0,0)->0、(1,0)->4、(0,1)->4、(1,1)->2, +; 也就是只落在「进位」和 s 这两位。s 的初值不影响结果,所以 8 项是这 4 项重复两遍。 +%synth bit_inc_nc3_synth + params 3 + stat 0 4 4 2 0 4 4 2 + metric flat +%endsynth + +%macro bit_inc_nc3 3 + .tp %3 + set_0 + bit_inc_nc3_synth %1 %2 %3 +%endmacro + +; 8 位自增,每位一个干净 cell: +; %1..%8 = a0..a7 +; %9..%16 = s0..s7 +; %17 = 进位(初值 1,因为 inc 就是 +1) +; +; 最后一位产生的进位丢掉:前提是 a < 255。 +%macro raw_inc_bits8 17 +.anchor before_raw_inc_bits8 + erase %17 + ; set_1 是不带参数的宏,作用于当前 cell,所以要先 .tp 过去 + .tp %17 + set_1 + + bit_inc_nc3 %1 %17 %9 + bit_inc_nc3 %2 %17 %10 + bit_inc_nc3 %3 %17 %11 + bit_inc_nc3 %4 %17 %12 + bit_inc_nc3 %5 %17 %13 + bit_inc_nc3 %6 %17 %14 + bit_inc_nc3 %7 %17 %15 + bit_inc_nc3 %8 %17 %16 + .tp before_raw_inc_bits8 +%endmacro