"""Host checks for the Issue #264 CD_CONTNTS ($0C, $E024) caller-contract ledger. These tests check the ledger's rules and pin every row's side, status and value to a snapshot, so any change to those fields fails regardless of its citation. They run no emulator, read no ROM and quote no BIOS bytes. Passing them is not evidence of System Card behaviour. """ import re import unittest from collections import Counter from pathlib import Path ROOT = Path(__file__).resolve().parents[2] DOC = ROOT / 'docs' / 'research' / 'cd-contnts-contract.md' FIELDS = ['id', 'side', 'item', 'status', 'value', 'source', 'reason'] SIDES = {'caller', 'callee', 'reference', 'name'} STATUSES = {'SOURCE_FACT', 'HYPOTHESIS', 'UNKNOWN', 'BLOCKED_ABI'} FORBIDDEN_WORDS = ('VERIFIED', 'OBSERVED', 'PASS', 'DEV_CALLABLE') # Summary sources (S6, S7) and PENDING are not a basis for SOURCE_FACT on any side. SUMMARY_SOURCES = {'S6', 'S7'} # Pinned snapshot of (side, status, value) for every ledger row. Changing any of them fails the ledger # test until this snapshot is updated in the same change. EXPECTED = { 'F01': ('caller', 'SOURCE_FACT', '`$24`(JSR 先 `$E024`)'), 'F02': ('caller', 'SOURCE_FACT', '`0`'), 'F03': ('callee', 'UNKNOWN', 'UNKNOWN'), 'F04': ('callee', 'UNKNOWN', 'UNKNOWN'), 'F05': ('callee', 'UNKNOWN', 'UNKNOWN'), 'F06': ('callee', 'UNKNOWN', 'UNKNOWN'), 'F07': ('callee', 'UNKNOWN', 'UNKNOWN'), 'F08': ('caller', 'SOURCE_FACT', 'A≠0 で `cd_reset` へ戻る、A=0 で TOC 取得済みとして進む'), 'F09': ('callee', 'HYPOTHESIS', '0 を成功とみなす'), 'F10': ('callee', 'UNKNOWN', 'UNKNOWN'), 'F11': ('caller', 'SOURCE_FACT', 'N/Z は復元された戻り A で決まる'), 'F12': ('callee', 'UNKNOWN', 'UNKNOWN'), 'F13': ('callee', 'HYPOTHESIS', '呼出先は RTS で `call_bios` へ戻る'), 'F14': ('caller', 'SOURCE_FACT', '呼出し中 MPR7=`$00`、戻り時に呼出側の値へ復元'), 'F15': ('callee', 'UNKNOWN', 'UNKNOWN'), 'F16': ('callee', 'UNKNOWN', 'UNKNOWN'), 'F17': ('callee', 'UNKNOWN', 'UNKNOWN'), 'F18': ('caller', 'SOURCE_FACT', '呼出側は `cd_stat` の戻りを busy・ready の分岐に使い、待機してから呼ぶ'), 'F19': ('callee', 'UNKNOWN', 'UNKNOWN'), 'F20': ('caller', 'SOURCE_FACT', '非 0 で `cd_reset` に戻り、待機からやり直す'), 'F21': ('callee', 'UNKNOWN', 'UNKNOWN'), 'F22': ('name', 'SOURCE_FACT', 'HuC `cd_contnts`、Hu-Go `CD_CONTENTS`、本リポジトリ `CD_CONTNTS`'), 'F23': ('callee', 'HYPOTHESIS', '版ごとに値が異なる。3.0 の列は 2.x と同じ値で、1.0 および TG3.0 の列とは異なる'), 'F24': ('reference', 'HYPOTHESIS', '名前定義のみ。`default:` で未処理扱い(元のバイト列へ戻す、dispatcher の挙動)'), 'F25': ('reference', 'UNKNOWN', 'UNKNOWN'), } # Status counts as the rows stand. Enforced exactly by test_status_counts. EXPECTED_COUNTS = {'SOURCE_FACT': 8, 'HYPOTHESIS': 4, 'UNKNOWN': 13} # Field coverage required by the Issue #264 acceptance list. Each keyword must appear in the item cell of # the named row (exact row, not a substring of the whole table). REQUIRED_FIELD_ROWS = { 'Y': 'F01', 'A': 'F03', 'X': 'F04', 'P': 'F05', '$20F8': 'F06', 'フラグ': 'F12', 'SP': 'F13', 'MPR': 'F15', '副作用': 'F16', 'IRQ': 'F17', '失敗': 'F21', } def token_in(kw, text): """True if kw occurs as a whole token: not adjacent to an ASCII letter (so 'A' does not match 'Abc', 'MPR' matches 'MPR0–6').""" return re.search(rf'(?