Files
r8asm-rev-tests/tools/stdmac_inc_fixB_probe.py
starcells 4c027a8082 raw_inc 完整修法 (C): 体内 %3/%4 对调 + 补 bit_raw_add 式入口清零
- bit_raw_inc 不清 S/Co/tmp 的 LSB 就进比特循环;入口 S 残留时 A 被污染(A_out = A ^ S_in),
  raw_add 因为每比特开头清 S/Co/buf 而免疫(对照实测)
- 修法 A(对调调用实参)/B(对调宏体)只在 S 入口干净时正确(2/10),C = B + 三块清零 → 10/10 脏入口 + 22/22 干净
- tools/stdmac_inc_state_probe.py: 入口脏值扫描 + raw_add 对照;tests/inc_fix_probe_{B,C}.r8asm.frag
2026-09-13 04:27:49 +00:00

45 lines
2.0 KiB
Python

#!/usr/bin/env python3
"""stdmac_inc_fixB_probe.py — raw_inc 两种修法对拍(A=改调用实参 / B=改宏体 %3/%4)。"""
import itertools
import os
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, build_tools, run_case, set_cell # noqa: E402
build_tools()
FRAG = open(os.path.join(HERE, "tests", "inc_fix_probe.r8asm.frag")).read()
FRAG += open(os.path.join(HERE, "tests", "inc_fix_probe_B.r8asm.frag")).read()
VALS = [0, 1, 2, 3, 5, 126, 127, 128, 170, 254, 255]
VARIANTS = (("现役 raw_inc", "raw_inc A Ci S Co tmp"),
("A: 换调用实参", "raw_inc_fix A Ci S Co tmp"),
("B: 换宏体 %3/%4", "raw_inc_B A Ci S Co tmp"))
for variant, call in VARIANTS:
bad = []
for a, cin in itertools.product(VALS, [0, 1]):
src = PRELUDE + FRAG + set_cell("A", a) + set_cell("Ci", cin)
src += "%s\noutput A\noutput Ci\noutput S\noutput Co\n" % call
got = run_case(src)
want = [a, cin, (a + cin) & 0xFF, (a + cin) >> 8]
if got != want:
bad.append((a, cin, want, got))
chain = []
p = call.split() # [name, A, Ci, S, Co, tmp]
call2 = " ".join([p[0], p[3], p[2], "D", p[4], p[5]]) # 第二级:把上一级 S 当输入,输出到独立 cell D
for a in (126, 254, 127):
src = PRELUDE + FRAG + set_cell("A", a) + set_cell("Ci", 1) + ".var D 10\n.tp D\n.xor 0\n"
src += "%s\n" % call + "output S\noutput Co\n"
src += "%s\n" % call2 + "output D\noutput Co\n"
got = run_case(src)
want = [a + 1, 0, (a + 2) & 0xFF, (a + 2) >> 8]
if got != want:
chain.append((a, want, got))
print("%-18s 单次失败 %2d / %d 链式失败 %d / 3" % (variant, len(bad), len(VALS) * 2, len(chain)))
for a, cin, want, got in bad[:3]:
print(" 单次 A=%-4d Ci=%d want=%s got=%s" % (a, cin, want, got))
for a, want, got in chain:
print(" 链式 A=%d want=%s got=%s" % (a, want, got))