Files
zelda-1-prometeu-port/tools/validate_inventory_rom.py
T

62 lines
2.2 KiB
Python

#!/usr/bin/env python3
"""Bounded native selection oracle; uses the actual ROM instruction stream."""
import json,random
from nes_object_graphics import P
from extract_graphics import ROOT
ENTRY=P.index(bytes.fromhex('85efa20920'),5*16384,6*16384)
def select(slots,start,direction):
m=bytearray(65536);m[0x8000:0xc000]=P[5*16384:6*16384];m[0x657:0x67f]=bytes(slots)
pc=0x8000+ENTRY%16384;a=direction;x=0;y=start;z=n=c=False;stack=[]
def nz(v):
nonlocal z,n
v &=255;z=v==0;n=v>=128;return v
def byte():
nonlocal pc
v=m[pc];pc+=1;return v
def word():return byte()|byte()<<8
for _ in range(1000):
at=pc;op=byte()
if op==0x60:
if not stack:return m[0x656]
pc=stack.pop()
elif op==0x20:
target=word();stack.append(pc);pc=target
elif op==0x4c:pc=word()
elif op==0x85:m[byte()]=a
elif op==0x8c:m[word()]=y
elif op==0xa2:x=nz(byte())
elif op==0xa0:y=nz(byte())
elif op==0xa5:a=nz(m[byte()])
elif op==0xad:a=nz(m[word()])
elif op==0xb9:a=nz(m[word()+y])
elif op==0xc0:
v=byte();c=y>=v;nz(y-v)
elif op==0x29:a=nz(a&byte())
elif op==0x4a:c=bool(a&1);a=nz(a>>1)
elif op==0xca:x=nz(x-1)
elif op==0x88:y=nz(y-1)
elif op==0xc8:y=nz(y+1)
elif op in (0xf0,0xd0,0x10,0x90,0xb0):
d=byte();take={0xf0:z,0xd0:not z,0x10:not n,0x90:not c,0xb0:c}[op]
if take:pc+=d if d<128 else d-256
else:raise AssertionError(f'Unsupported {op:02x} at {at:04x}')
raise AssertionError('Unbounded native selection')
def main():
cases=[];rng=random.Random(1986)
inventories=[[0]*40]
for slot in (1,2,3,7,15,29,30):
s=[0]*40;s[slot]=1;inventories.append(s)
for _ in range(24):
s=[0]*40
for slot in (1,2,3,4,5,6,7,8,15,29,30):s[slot]=rng.randrange(2)
inventories.append(s)
for s in inventories:
for start in (0,1,2,3,6,7,8):
for direction in (1,2,3,4,8):
# Native arrow without bow and direction 0 loops forever. Avoid that invalid cursor state.
if direction>=4 and start==2 and s[2] and not s[3]:continue
cases.append([s,start,direction,select(s,start,direction)])
out=ROOT/'extracted/inventory_screen/selection-oracle.json';out.write_text(json.dumps({'entry_prg':ENTRY,'cases':cases},indent=2)+'\n')
print(len(cases),'native selection cases')
if __name__=='__main__':main()