Files

100 lines
4.6 KiB
Python

#!/usr/bin/env python3
"""Bounded 6502 execution of original life arithmetic; generate PBS fixture cases."""
import hashlib,json
from pathlib import Path
from extract_graphics import ROOT,ROM_SHA256
P=(ROOT/'.tmp/zelda-1.nes').read_bytes()[16:]
assert hashlib.sha256((ROOT/'.tmp/zelda-1.nes').read_bytes()).hexdigest()==ROM_SHA256
DELTA=0x790
DAMAGE=P.index(bytes.fromhex('ac6206f007460d660e'))+DELTA
HEAL=P.index(bytes.fromhex('8501206c74d007ae7006'))+DELTA
class CPU:
def __init__(self,hv,hp,ring=0):
self.m=bytearray(65536);self.m[0x6c90:0x7f00]=P[0x6500:0x7770];self.m[0xc000:]=P[7*16384:]
self.m[0x66f]=hv;self.m[0x670]=hp;self.m[0x662]=ring
self.a=0;self.x=0;self.y=0;self.c=False;self.z=False;self.n=False;self.sp=253;self.depth=0;self.died=False
def nz(self,v):
v&=255;self.z=v==0;self.n=bool(v&128);return v
def byte(self):v=self.m[self.pc];self.pc+=1;return v
def word(self):lo=self.byte();return lo|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):
self.pc=entry
for _ in range(3000):
at=self.pc;op=self.byte()
if op==0x60:
if self.depth==0:return self.m[0x66f],self.m[0x670],self.died
self.pc=(self.pop()|self.pop()<<8)+1;self.depth-=1
elif op==0x20:
target=self.word()
if not 0x6c90<=target<0x7f00:
# Damage's sole external call is EndGameMode; it returns A=0.
assert entry==DAMAGE
self.died=True;self.a=self.nz(0)
else:
self.push((self.pc-1)>>8);self.push(self.pc-1);self.pc=target;self.depth+=1
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==0xa9:self.a=self.nz(self.byte())
elif op==0xa5:self.a=self.nz(self.m[self.byte()])
elif op==0xad:self.a=self.nz(self.m[self.word()])
elif op==0xac:self.y=self.nz(self.m[self.word()])
elif op==0xae:self.x=self.nz(self.m[self.word()])
elif op==0x85:self.m[self.byte()]=self.a
elif op==0x8d:self.m[self.word()]=self.a
elif op==0x29:self.a=self.nz(self.a&self.byte())
elif op==0x38:self.c=True
elif op in (0xe5,0xed):
rhs=self.m[self.byte() if op==0xe5 else self.word()];v=self.a-rhs-(not self.c);self.c=v>=0;self.a=self.nz(v)
elif op==0xc5:
rhs=self.m[self.byte()];self.c=self.a>=rhs;self.nz(self.a-rhs)
elif op==0x46:
q=self.byte();v=self.m[q];self.c=bool(v&1);self.m[q]=self.nz(v>>1)
elif op==0x66:
q=self.byte();v=self.m[q];v2=(v>>1)|(128 if self.c else 0);self.c=bool(v&1);self.m[q]=self.nz(v2)
elif op==0x4a:self.c=bool(self.a&1);self.a=self.nz(self.a>>1)
elif op in (0xce,0xee,0xc6):
q=self.byte() if op==0xc6 else self.word();self.m[q]=self.nz(self.m[q]+(1 if op==0xee else -1))
elif op==0x88:self.y=self.nz(self.y-1)
elif op==0xe8:self.x=self.nz(self.x+1)
elif op==0x48:self.push(self.a)
elif op==0x68:self.a=self.nz(self.pop())
else:raise AssertionError(f'Unsupported ${op:02X} at ${at:04X}')
raise AssertionError('Routine did not return')
def damage(hv,hp,amount,ring=0):
cpu=CPU(hv,hp,ring);cpu.m[13]=amount//256;cpu.m[14]=amount%256;return cpu.run(DAMAGE)
def heal(hv,hp,hearts):
cpu=CPU(hv,hp);cpu.a=hearts-1;return cpu.run(HEAL)[:2]
def model_damage(hv,hp,amount,ring):
amount//=2**ring;full=hv%16;low=amount%256;high=amount//256
while hp<low:
low-=hp
if full==0:return hv&240,0,True
full-=1;hp=255
hp-=low
if full<high:return hv&240,0,True
return (hv&240)+full-high,hp,False
def main():
count=0;heals=0
for capacity in (3,5,8,16):
for full in range(capacity):
for fraction in (0,1,63,127,128,254,255):
hv=(capacity-1)*16+full
for amount in (1,32,64,128,255,256,257,384,512,1024,65535):
for ring in (0,1,2):
assert damage(hv,fraction,amount,ring)==model_damage(hv,fraction,amount,ring),(hv,fraction,amount,ring)
count+=1
for amount in (1,3,16):
f=full;hp=fraction
for _ in range(amount):
if f==capacity-1:hp=255;break
f+=1
assert heal(hv,fraction,amount)==((capacity-1)*16+f,hp)
heals+=1
out=ROOT/'extracted/life';out.mkdir(exist_ok=True)
(out/'rom-validation.json').write_text(json.dumps({'rom_sha256':ROM_SHA256,'damage_cases':count,'healing_cases':heals,'mismatches':0,'damage_entry':hex(DAMAGE),'heal_entry':hex(HEAL),'ram_copy':{'prg':0x6500,'cpu':0x6c90,'length':0x1270},'scope':'Bounded original 6502 life routines versus port arithmetic model; EndGameMode side effects outside life bytes are stubbed.'},indent=2)+'\n')
print(json.dumps({'damage_cases':count,'healing_cases':heals,'mismatches':0}))
if __name__=='__main__':main()