From 032c80e8f9b4c40717dcfe28a1ca294b3c3be9f7 Mon Sep 17 00:00:00 2001 From: StarCells Date: Sun, 13 Sep 2026 04:56:11 +0000 Subject: [PATCH] =?UTF-8?q?=E6=94=B6=E5=8F=A3=E6=B8=85=E9=9B=B6=E5=BF=85?= =?UTF-8?q?=E8=A6=81=E9=9B=86(=E6=9B=B4=E6=AD=A3:tmp=20=E9=82=A3=E5=9D=97?= =?UTF-8?q?=E4=B8=8D=E6=98=AF=E8=A3=85=E9=A5=B0,=E5=8F=AA=E6=9C=89=20Co=20?= =?UTF-8?q?=E9=82=A3=E5=9D=97=E5=8F=AF=E5=88=A0)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 扩测 tmp 脏值后推翻上轮结论:tmp=85(bit1=0)测不出,tmp=255 立刻现形 16 例 机制:快照 只清 dst 的 LSB,随后 把 tmp 原 bit1 转到 LSB 当 flag,那里无人赋值 - 三块清零的必要性: * 清 %3(Sum) 必需 —— Sum 只有 3 处 flb(异或),无任何赋值;异或链起点必须确定,且旋转累加器把未写位置送到 LSB 被 biz 当条件读 * 清 %5(tmp) 必需 —— ror 把入口 bit1 搬进 flag 位 * 清 %4(Co) 可删 —— Co 的 LSB 进循环前被 bit_copy 当 buf 清、循环里每轮被 clb_bit_mov 消费;高位无人读(声明即 CellBit) - 实测(判定: A/Ci 保持、S 正确、Co&1=正确进位): now 1570B 0例 / 去掉清Co(35) 1514B 0例 / 只留清Sum(3) 1442B 16例(tmp脏) / D2(glue erase S+tmp,体里全删) 1492B 0例 - 判据总结:入口那个位在被读之前有没有被确定性覆写 --- tests/inc_clear_final.r8asm.frag | 324 +++++++++++++++++++++++++++++++ tools/stdmac_clear_final.py | 99 ++++++++++ 2 files changed, 423 insertions(+) create mode 100644 tests/inc_clear_final.r8asm.frag create mode 100644 tools/stdmac_clear_final.py diff --git a/tests/inc_clear_final.r8asm.frag b/tests/inc_clear_final.r8asm.frag new file mode 100644 index 0000000..8258b1c --- /dev/null +++ b/tests/inc_clear_final.r8asm.frag @@ -0,0 +1,324 @@ +%macro bit_raw_inc_now 5 +.anchor before_bit_raw_inc_now +.tp %3 +biz + flb +rnz + +.tp %4 +biz + flb +rnz + +.tp %5 +biz + flb +rnz + +.tp %2 +biz + .tp %1 + flb + .tp %3 + flb + .tp %2 + flb +rnz + +.tp %1 +biz + .tp %4 + set_1 + .tp %5 + set_1 + .tp %1 + flb +rnz + +.tp %3 +biz + .tp %1 + set_1 + .tp %2 + set_1 + .tp %4 + set_1 + .tp %3 + flb +rnz + +.tp %5 +biz + .tp %1 + flb + .tp %3 + flb + .tp %4 + flb + .tp %5 + flb +rnz + +.tp before_bit_raw_inc_now + +%endmacro + +%macro raw_inc_now 5 +.anchor before_raw_inc_now + +.tp %2 +bit_copy %5 %4 +.tp %5 +ror +%rep _CELL_SIZE + bit_raw_inc_now %1 %2 %3 %4 %5 + .tp %4 + clb_bit_mov %2 + .tp %1 + ror + .tp %3 + ror +%endrep +.tp %2 +clb_bit_mov %4 +.tp %5 +rol +clb_bit_mov %2 +.tp before_raw_inc_now +%endmacro +%macro bit_raw_inc_35 5 +.anchor before_bit_raw_inc_35 +.tp %3 +biz + flb +rnz + +.tp %5 +biz + flb +rnz + +.tp %2 +biz + .tp %1 + flb + .tp %3 + flb + .tp %2 + flb +rnz + +.tp %1 +biz + .tp %4 + set_1 + .tp %5 + set_1 + .tp %1 + flb +rnz + +.tp %3 +biz + .tp %1 + set_1 + .tp %2 + set_1 + .tp %4 + set_1 + .tp %3 + flb +rnz + +.tp %5 +biz + .tp %1 + flb + .tp %3 + flb + .tp %4 + flb + .tp %5 + flb +rnz + +.tp before_bit_raw_inc_35 + +%endmacro + +%macro raw_inc_35 5 +.anchor before_raw_inc_35 + +.tp %2 +bit_copy %5 %4 +.tp %5 +ror +%rep _CELL_SIZE + bit_raw_inc_35 %1 %2 %3 %4 %5 + .tp %4 + clb_bit_mov %2 + .tp %1 + ror + .tp %3 + ror +%endrep +.tp %2 +clb_bit_mov %4 +.tp %5 +rol +clb_bit_mov %2 +.tp before_raw_inc_35 +%endmacro +%macro bit_raw_inc_3 5 +.anchor before_bit_raw_inc_3 +.tp %3 +biz + flb +rnz + +.tp %2 +biz + .tp %1 + flb + .tp %3 + flb + .tp %2 + flb +rnz + +.tp %1 +biz + .tp %4 + set_1 + .tp %5 + set_1 + .tp %1 + flb +rnz + +.tp %3 +biz + .tp %1 + set_1 + .tp %2 + set_1 + .tp %4 + set_1 + .tp %3 + flb +rnz + +.tp %5 +biz + .tp %1 + flb + .tp %3 + flb + .tp %4 + flb + .tp %5 + flb +rnz + +.tp before_bit_raw_inc_3 + +%endmacro + +%macro raw_inc_3 5 +.anchor before_raw_inc_3 + +.tp %2 +bit_copy %5 %4 +.tp %5 +ror +%rep _CELL_SIZE + bit_raw_inc_3 %1 %2 %3 %4 %5 + .tp %4 + clb_bit_mov %2 + .tp %1 + ror + .tp %3 + ror +%endrep +.tp %2 +clb_bit_mov %4 +.tp %5 +rol +clb_bit_mov %2 +.tp before_raw_inc_3 +%endmacro +%macro bit_raw_inc_D2 5 +.anchor before_bit_raw_inc_D2 +.tp %2 +biz + .tp %1 + flb + .tp %3 + flb + .tp %2 + flb +rnz + +.tp %1 +biz + .tp %4 + set_1 + .tp %5 + set_1 + .tp %1 + flb +rnz + +.tp %3 +biz + .tp %1 + set_1 + .tp %2 + set_1 + .tp %4 + set_1 + .tp %3 + flb +rnz + +.tp %5 +biz + .tp %1 + flb + .tp %3 + flb + .tp %4 + flb + .tp %5 + flb +rnz + +.tp before_bit_raw_inc_D2 + +%endmacro + +%macro raw_inc_D2 5 +.anchor before_raw_inc_D2 +erase %3 +erase %5 + +.tp %2 +bit_copy %5 %4 +.tp %5 +ror +%rep _CELL_SIZE + bit_raw_inc_D2 %1 %2 %3 %4 %5 + .tp %4 + clb_bit_mov %2 + .tp %1 + ror + .tp %3 + ror +%endrep +.tp %2 +clb_bit_mov %4 +.tp %5 +rol +clb_bit_mov %2 +.tp before_raw_inc_D2 +%endmacro diff --git a/tools/stdmac_clear_final.py b/tools/stdmac_clear_final.py new file mode 100644 index 0000000..fac6df8 --- /dev/null +++ b/tools/stdmac_clear_final.py @@ -0,0 +1,99 @@ +#!/usr/bin/env python3 +"""stdmac_clear_final.py — 收口:哪几块清零是必需的(修正上一轮"tmp 是装饰"的错误结论)。 + +tmp 的 LSB 之所以也要清:glue 里快照是 `bit_copy %5 %4` + `ror`,ror 会把 tmp 原来的 +**bit1** 转到 LSB;若 block7 的 flag 判定在那一刻读到 1,就走错分支。tmp=85(bit1=0)测不出, +tmp=255(bit1=1)必现。 +""" +import itertools +import os +import re +import sys + +HERE = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) +sys.path.insert(0, os.path.join(HERE, "tools")) +from stdmac_matrix import PRELUDE, REV, build_tools, run_case, set_cell # noqa: E402 + +raw = open(os.path.join(REV, "asm", "raw_inc.r8asm")).read() +body = re.search(r"%macro bit_raw_inc 5\n(.*?)\n%endmacro", raw, re.S).group(1) +_, rest = body.split(".tp %3\nbiz\n flb\nrnz\n", 1) +CLEARS = {"3": ".tp %3\nbiz\n flb\nrnz\n\n"} +for n in ("4", "5"): + blk = ".tp %" + n + "\nbiz\n flb\nrnz\n\n" + rest = rest.replace(blk, "", 1) + CLEARS[n] = blk +core = rest.strip("\n") + "\n" + +GLUE = """ +%macro {glue} 5 +.anchor before_{glue} +{pre} +.tp %2 +bit_copy %5 %4 +.tp %5 +ror +%rep _CELL_SIZE + {bit} %1 %2 %3 %4 %5 + .tp %4 + clb_bit_mov %2 + .tp %1 + ror + .tp %3 + ror +%endrep +.tp %2 +clb_bit_mov %4 +.tp %5 +rol +clb_bit_mov %2 +.tp before_{glue} +%endmacro +""" + +SPECS = (("now", "345", ""), + ("35", "35", ""), + ("3", "3", ""), + ("D2", "", "erase %3\nerase %5\n")) + +FRAG = "" +for name, keep, pre in SPECS: + bit = "bit_raw_inc_" + name + FRAG += ("%macro " + bit + " 5\n.anchor before_" + bit + "\n" + + "".join(CLEARS[x] for x in keep) + core.replace("bit_raw_inc", bit) + + "\n%endmacro\n" + GLUE.format(glue="raw_inc_" + name, bit=bit, pre=pre)) +open(os.path.join(HERE, "tests", "inc_clear_final.r8asm.frag"), "w").write(FRAG) + + +def case(name, a, cin, s_in, co_in, t_in): + src = PRELUDE + FRAG + set_cell("A", a) + set_cell("Ci", cin) + src += ".tp S\n.xor %d\n.tp Co\n.xor %d\n.tp tmp\n.xor %d\n" % (s_in, co_in, t_in) + src += "raw_inc_%s A Ci S Co tmp\noutput A\noutput Ci\noutput S\noutput Co\n" % name + return run_case(src) + + +build_tools() +INPUTS = [(0, 0), (0, 1), (1, 0), (1, 1), (126, 0), (127, 1), (254, 1)] +CO, S, TMP = [0, 85], [0, 127], [0, 85, 255] +VALS = [0, 1, 2, 3, 5, 126, 127, 128, 170, 254, 255] + +print("判定: A/Ci 保持、S=正确和、Co&1=正确进位(Co 高位不判)\n") +print("%-5s | %-12s | %-16s | tape" % ("保留", "干净全表22", "脏入口(A,Ci 7 组)")) +print("-" * 70) +for name, _, _ in SPECS: + clean_bad = [c for c in itertools.product(VALS, [0, 1]) + if case(name, c[0], c[1], 0, 0, 0) != [c[0], c[1], (c[0] + c[1]) & 0xFF, (c[0] + c[1]) >> 8]] + bad = [] + for (a, cin), co_in, s_in, t_in in itertools.product(INPUTS, CO, S, TMP): + got = case(name, a, cin, s_in, co_in, t_in) + tot = a + cin + if not (got[0] == a and got[1] == cin and got[2] == (tot & 0xFF) and (got[3] & 1) == (tot >> 8)): + bad.append(((a, cin, s_in, co_in, t_in), got)) + import subprocess, tempfile + with tempfile.NamedTemporaryFile("w", suffix=".r8asm", delete=False, dir=REV) as f: + f.write(PRELUDE + FRAG + "raw_inc_%s A Ci S Co tmp\n" % name) + p = f.name + tlen = len(subprocess.run([os.path.join(HERE, "build", "probe_dump"), p], cwd=REV, + capture_output=True, text=True).stdout) + os.unlink(p) + ex = "; ".join("(S脏%d,Co脏%d,tmp脏%d)" % (d[2], d[3], d[4]) for d, g in bad[:2]) or "全过" + print("%-5s | %-12s | %-16s | %d %s" % (name, "全过" if not clean_bad else "错", "%d 例受影响" % len(bad), tlen, ex))