Files
r8asm-rev-tests/tools/stdmac_clear_final.py
starcells 1907d210f7 收口清零必要集(更正:tmp 那块不是装饰,只有 Co 那块可删)
- 扩测 tmp 脏值后推翻上轮结论:tmp=85(bit1=0)测不出,tmp=255 立刻现形 16 例。
  机制:快照 bit_copy %5 %4 只清 dst 的 LSB,随后 ror 把 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例。
- 判据总结:入口那个位在被读之前有没有被确定性覆写。
2026-09-13 04:56:21 +00:00

100 lines
3.6 KiB
Python

#!/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))