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

87 lines
3.4 KiB
Python

"""Bounded execution of this ROM's object descriptor routine (not a NES emulator)."""
from extract_graphics import ROOT, ROM_SHA256
import hashlib
P=(ROOT/'.tmp/zelda-1.nes').read_bytes()[16:]
assert hashlib.sha256((ROOT/'.tmp/zelda-1.nes').read_bytes()).hexdigest()==ROM_SHA256
DELTA=0x790
ATTR=P.index(bytes([255]+[0]*6+[5]*4+[129]*4+[1]*2),7*16384)
ENTRY=P.index(bytes.fromhex('a001d002a000840cbc4f03c8'))+DELTA
HALF=P.index(bytes.fromhex('c6074c'),ENTRY-DELTA,0x7770)
STOP=int.from_bytes(P[HALF+3:HALF+5],'little')
def descriptor(object_type,frame=0,mirrored=False,flip_x=False,item_slot=None,attribute=0,object_attribute=None):
"""Execute through DrawObjectWithType until Anim_WriteSpritePair begins."""
m=bytearray(65536);m[0x6c90:0x7f00]=P[0x6500:0x7770]
x=1;y=0;a=frame;pc=ENTRY+(0 if mirrored else 4);sp=253;c=z=n=False
assert item_slot is not None or object_type in (7,8,9,0x11,0x53,0x55,0x77,0x78,0x7b),'Only verified Octorok, Zora, rock, fireball and item descriptor paths are covered'
m[0x34f+x]=object_type;m[0x4bf+x]=P[ATTR+object_type] if object_attribute is None else object_attribute
m[0x0f]=int(flip_x)
if item_slot is not None:
pc=P.index(bytes.fromhex('8608a9018507a908850ab9'),0x6500,0x7770)+DELTA
x=15;y=item_slot;m[12]=frame;m[4]=m[5]=attribute
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
def push(v):
nonlocal sp
m[256+sp]=v&255;sp=(sp-1)&255
def pop():
nonlocal sp
sp=(sp+1)&255;return m[256+sp]
for _ in range(500):
if pc==STOP:
return {'tiles':[m[2],m[3]],'attributes':[m[4],m[5]],'sides':m[7]+1,'x_separation':m[10],'animation_index':m[14],'frame':m[13]}
at=pc;op=byte()
if op==0xa9:a=nz(byte())
elif op==0xa0:y=nz(byte())
elif op==0xa4:y=nz(m[byte()])
elif op==0xac:y=nz(m[word()])
elif op==0xbc:y=nz(m[word()+x])
elif op==0xa5:a=nz(m[byte()])
elif op==0xad:a=nz(m[word()])
elif op==0xb9:a=nz(m[word()+y])
elif op==0xbd:a=nz(m[word()+x])
elif op==0x85:m[byte()]=a
elif op==0x84:m[byte()]=y
elif op==0x86:m[byte()]=x
elif op==0x8d:m[word()]=a
elif op==0xc8:y=nz(y+1)
elif op in (0xc6,0xe6):
q=byte();m[q]=nz(m[q]+(1 if op==0xe6 else -1))
elif op==0xa8:y=nz(a)
elif op==0x18:c=False
elif op in (0x69,0x65):
v=byte();v=m[v] if op==0x65 else v;v=a+v+c;c=v>255;a=nz(v)
elif op in (0xe0,0xc9):
v=byte();lhs=x if op==0xe0 else a;c=lhs>=v;nz(lhs-v)
elif op==0x29:a=nz(a&byte())
elif op==0x49:a=nz(a^byte())
elif op in (0xf0,0xd0,0xb0,0x90):
d=byte();take={0xf0:z,0xd0:not z,0xb0:c,0x90:not c}[op]
if take:pc+=d if d<128 else d-256
elif op==0x4c:pc=word()
elif op==0x20:
target=word();push((pc-1)>>8);push(pc-1);pc=target
elif op==0x60:pc=(pop()|pop()<<8)+1
elif op==0x48:push(a)
elif op==0x68:a=nz(pop())
else:raise AssertionError(f'Unsupported opcode ${op:02x} at ${at:04x}')
raise AssertionError('Descriptor routine did not reach sprite emission')
def item(slot,frame=0,attribute=0,flip_x=False):
return descriptor(0,frame,False,flip_x,slot,attribute)
def octorok(direction,phase):
offset={'left':0,'right':0,'up':1,'down':2}[direction]
return descriptor(7,phase*3+offset,direction in ('up','down'),direction=='right')
if __name__=='__main__':
for phase in range(2):
for direction in ('left','up','down','right'):print(direction,phase,octorok(direction,phase))
print('rock',descriptor(0x53))