113 lines
5.9 KiB
Python
113 lines
5.9 KiB
Python
#!/usr/bin/env python3
|
|
"""Execute bounded original 6502 history/drop routines; emit PBS oracle cases."""
|
|
import json
|
|
from extract_graphics import ROOT
|
|
from nes_object_graphics import P
|
|
MOD=P.index(bytes.fromhex('a005a5ebd92106f013'))
|
|
SAVE=P.index(bytes.fromhex('20cee629078502b10029f8'),MOD)
|
|
DROP=P.index(bytes.fromhex('a9008501bd1204a006'))
|
|
END=P.index(bytes.fromhex('a50095acc923d0034c'),DROP)
|
|
FAIRY=int.from_bytes(P[END+9:END+11],'little')
|
|
DESTROY=int.from_bytes(P[END+13:END+15],'little')
|
|
def cpu_addr(offset):return 0x8000+offset%16384
|
|
class CPU:
|
|
def __init__(self,bank):
|
|
self.m=bytearray(65536);self.m[0x8000:0xc000]=P[bank*16384:(bank+1)*16384];self.m[0xc000:]=P[7*16384:]
|
|
self.a=self.y=0;self.x=1;self.c=self.z=self.n=False;self.pc=0;self.sp=253;self.depth=0
|
|
def nz(self,v):v &=255;self.z=v==0;self.n=v>=128;return v
|
|
def byte(self):v=self.m[self.pc];self.pc+=1;return v
|
|
def word(self):return self.byte()|self.byte()<<8
|
|
def push(self,v):self.m[256+self.sp]=v&255;self.sp=(self.sp-1)&255
|
|
def pop(self):self.sp=(self.sp+1)&255;return self.m[256+self.sp]
|
|
def run(self,entry,drop=False):
|
|
self.pc=cpu_addr(entry)
|
|
for _ in range(1000):
|
|
if drop and self.pc==DESTROY:return -1
|
|
if drop and self.pc==FAIRY:return 35
|
|
at=self.pc;op=self.byte()
|
|
if op==0x60:
|
|
if self.depth==0:return self.m[0xac+self.x] if drop else None
|
|
self.pc=(self.pop()|self.pop()<<8)+1;self.depth-=1
|
|
elif op==0x20:
|
|
target=self.word()
|
|
if target==0xe6ce:
|
|
self.m[0]=0;self.m[1]=16;self.y=self.m[0xeb];self.a=self.nz(self.m[0x1000+self.y])
|
|
else:raise AssertionError(f'Unstubbed external call {target:04x}')
|
|
elif op==0x4c:self.pc=self.word()
|
|
elif op in (0xf0,0xd0,0x90,0xb0,0x10,0x30):
|
|
d=self.byte();take={0xf0:self.z,0xd0:not self.z,0x90:not self.c,0xb0:self.c,0x10:not self.n,0x30:self.n}[op]
|
|
if take:self.pc+=d if d<128 else d-256
|
|
elif op in (0xa9,0xa5,0xad,0xb5,0xbd,0xb9,0xb1):
|
|
if op==0xa9:v=self.byte()
|
|
elif op==0xa5:v=self.m[self.byte()]
|
|
elif op==0xad:v=self.m[self.word()]
|
|
elif op==0xb5:v=self.m[(self.byte()+self.x)&255]
|
|
elif op==0xbd:v=self.m[self.word()+self.x]
|
|
elif op==0xb9:v=self.m[self.word()+self.y]
|
|
else:q=self.byte();v=self.m[(self.m[q]|self.m[(q+1)&255]<<8)+self.y]
|
|
self.a=self.nz(v)
|
|
elif op in (0xa0,0xa4,0xac):self.y=self.nz(self.byte() if op==0xa0 else self.m[self.byte() if op==0xa4 else self.word()])
|
|
elif op in (0x85,0x95,0x9d,0x91):
|
|
if op==0x85:q=self.byte()
|
|
elif op==0x95:q=(self.byte()+self.x)&255
|
|
elif op==0x9d:q=self.word()+self.x
|
|
else:v=self.byte();q=(self.m[v]|self.m[(v+1)&255]<<8)+self.y
|
|
self.m[q]=self.a
|
|
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==0x29:self.a=self.nz(self.a&self.byte())
|
|
elif op==0x11:
|
|
q=self.byte();self.a=self.nz(self.a|self.m[(self.m[q]|self.m[(q+1)&255]<<8)+self.y])
|
|
elif op in (0x65,0x6d):
|
|
v=self.m[self.byte() if op==0x65 else self.word()]+self.a+self.c;self.c=v>255;self.a=self.nz(v)
|
|
elif op==0xe5:
|
|
v=self.a-self.m[self.byte()]-(not self.c);self.c=v>=0;self.a=self.nz(v)
|
|
elif op==0xe6:q=self.byte();self.m[q]=self.nz(self.m[q]+1)
|
|
elif op==0x88:self.y=self.nz(self.y-1)
|
|
elif op==0x18:self.c=False
|
|
elif op==0x38:self.c=True
|
|
elif op==0xa8:self.y=self.nz(self.a)
|
|
else:raise AssertionError(f'Unsupported ${op:02X} at ${at:04X}')
|
|
raise AssertionError('Routine did not return')
|
|
def remaining(flags,count,present):
|
|
c=CPU(5);c.m[0xeb]=103;c.m[0x1000+103]=flags;c.m[3]=count
|
|
if present:c.m[0x621]=103
|
|
c.run(MOD);return c.m[3],c.m[0x1000+103]
|
|
def save(flags,spawned,killed):
|
|
c=CPU(5);c.m[0xeb]=103;c.m[0x1000+103]=flags;c.m[0x34e]=spawned;c.m[0x34f]=killed
|
|
c.run(SAVE);return c.m[0x1000+103]
|
|
def drop(monster,cycle,r,kills,help_count,help_bomb):
|
|
c=CPU(4);c.m[0x412+1]=monster;c.m[0x52a]=cycle;c.m[0x19]=r;c.m[0x627]=kills;c.m[0x50]=help_count;c.m[0x51]=help_bomb
|
|
value=c.run(DROP,True);return value,c.m[0x50],c.m[0x51]
|
|
def main():
|
|
meta=json.loads((ROOT/'extracted/combat/rom-data.json').read_text());history=[];saves=[];drops=[]
|
|
for flag_high in (0,16,168):
|
|
for low in range(8):
|
|
flags=flag_high+low
|
|
for count in range(5):
|
|
for present in (False,True):
|
|
result=remaining(flags,count,present);k=low
|
|
if not present and k==7:k=0
|
|
expected=(max(0,count-k) if k!=7 else 0,flag_high+k)
|
|
assert result==expected,(flags,count,present,result,expected)
|
|
history.append([flags,count,present,*result])
|
|
for spawned in range(5):
|
|
for killed in range(6):
|
|
result=save(flags,spawned,killed);expected=flag_high+(7 if killed>=spawned or low+killed>=7 else low+killed)
|
|
assert result==expected;saves.append([flags,spawned,killed,result])
|
|
for cycle in range(10):
|
|
for r in range(256):
|
|
for kills,help_count,help_bomb in [(0,0,0),(9,9,0),(10,10,0),(10,10,1),(16,0,0)]:
|
|
result=drop(7,cycle,r,kills,help_count,help_bomb)
|
|
value=meta['items'][cycle];h=help_count;b=help_bomb
|
|
if kills==16:value=35;h=b=0
|
|
elif h>=10:value=0 if b else 15;h=b=0
|
|
elif r>=80:value=-1
|
|
assert result==(value,h,b),(cycle,r,kills,result,(value,h,b))
|
|
if r in (0,79,80,247,255):drops.append([cycle,r,kills,help_count,help_bomb,*result])
|
|
out=ROOT/'extracted/combat';report={'history_cases':len(history),'save_cases':len(saves),'drop_cases':10*256*5,'mismatches':0,'entrypoints':{'modify':hex(cpu_addr(MOD)),'save':hex(cpu_addr(SAVE)),'drop':hex(cpu_addr(DROP))},'scope':'Bounded original 6502 routines. GetRoomFlags supplies a test world-flags buffer; fairy setup and object destruction are exit boundaries. No full-game lockstep claim.'}
|
|
(out/'rom-validation.json').write_text(json.dumps(report,indent=2)+'\n');(out/'oracle-cases.json').write_text(json.dumps({'history':history,'saves':saves,'drops':drops},indent=2)+'\n');print(json.dumps(report))
|
|
if __name__=='__main__':main()
|