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

62 lines
3.4 KiB
Python

#!/usr/bin/env python3
"""Execute native TakeItem/PostDebit leaves; emit shop purchase oracle cases."""
import json
from extract_graphics import ROOT
from nes_object_graphics import P,DELTA
from validate_life_rom import CPU
from prepare_shop import metadata
TAKE=P.index(bytes.fromhex('a2088e0206c90ed005a2028e0206a612e005'))+DELTA
DEBIT=0x8000+P.index(bytes.fromhex('186d7e068d7e0660'))%16384
class Native(CPU):
def __init__(self):
super().__init__(34,255);self.m[0x8000:0xc000]=P[16384:32768]
def execute(self,entry):
self.pc=entry
for _ in range(2000):
at=self.pc;op=self.byte()
if op==0x60:
if self.depth==0:return
self.pc=(self.pop()|self.pop()<<8)+1;self.depth-=1
elif op==0x20:
target=self.word();self.push((self.pc-1)>>8);self.push(self.pc-1);self.depth+=1;self.pc=target
elif op==0x4c:self.pc=self.word()
elif op in [0xf0,0xd0,0xb0,0x90,0x30,0x10]:
d=self.byte();take={0xf0:self.z,0xd0:not self.z,0xb0:self.c,0x90:not self.c,0x30:self.n,0x10:not self.n}[op]
if take:self.pc+=d if d<128 else d-256
elif op in [0xa9,0xa5,0xad,0xbd,0xb9]:
v=self.byte() if op==0xa9 else self.m[self.byte()] if op==0xa5 else self.m[self.word()+(self.x if op==0xbd else self.y if op==0xb9 else 0)];self.a=self.nz(v)
elif op in [0xa2,0xa6,0xae]:self.x=self.nz(self.byte() if op==0xa2 else self.m[self.byte() if op==0xa6 else self.word()])
elif op==0xa0:self.y=self.nz(self.byte())
elif op in [0x85,0x8d,0x99]:
q=self.byte() if op==0x85 else self.word()+(self.y if op==0x99 else 0);self.m[q]=self.a
elif op in [0x86,0x8e]:self.m[self.byte() if op==0x86 else self.word()]=self.x
elif op in [0xc9,0xc0,0xe0,0xd9,0xcd]:
lhs=self.y if op==0xc0 else self.x if op==0xe0 else self.a;rhs=self.m[self.word()+self.y] if op==0xd9 else self.m[self.word()] if op==0xcd else self.byte();self.c=lhs>=rhs;self.nz(lhs-rhs)
elif op in [0x69,0x79,0x6d]:
v=self.byte() if op==0x69 else self.m[self.word()+(self.y if op==0x79 else 0)];r=self.a+v+self.c;self.c=r>255;self.a=self.nz(r)
elif op==0x29:self.a=self.nz(self.a&self.byte())
elif op==0xaa:self.x=self.nz(self.a)
elif op==0xa8:self.y=self.nz(self.a)
elif op==0x8a:self.a=self.nz(self.x)
elif op==0x48:self.push(self.a)
elif op==0x68:self.a=self.nz(self.pop())
elif op==0x18:self.c=False
else:raise AssertionError(f'Unsupported {op:02x} at {at:04x}')
raise AssertionError('Native leaf did not return')
def take(item,old):
slots={28:31,25:23,6:4};c=Native();c.m[0x12]=11;c.m[0x657+slots[item]]=old;c.a=item;c.execute(TAKE);return c.m[0x657+slots[item]],c.m[0x505],c.m[0x506]
def debit(price,pending=0):
c=Native();c.m[0x67e]=pending;c.a=price;c.execute(DEBIT);return c.m[0x67e]
def main():
m=metadata();cases=[]
for item,slot,price in zip(m['wares'],[31,23,4],m['prices']):
for old in [0,1,2,254,255]:
value,lift,duration=take(item,old);assert lift==item and duration==128
expected=1 if item==28 else min(255,old+1) if item==25 else max(1,old);assert value==expected
for money in sorted({0,price-1,price,price+1,255}):
for pending in [0,1]:
allowed=pending==0 and money>=price;result=value if allowed else old;queued=debit(price,pending) if allowed else pending
cases.append([item,slot,price,old,money,pending,int(allowed),result,queued])
(ROOT/'extracted/shop/native-cases.json').write_text(json.dumps({'take_entry_cpu':TAKE,'debit_entry_cpu':DEBIT,'cases':cases},indent=2)+'\n');print('Native item/debit shop cases:',len(cases))
if __name__=='__main__':main()