#!/usr/bin/env python3 """Bounded original Zora state/angle execution; calls outside each leaf are explicit.""" import json from extract_graphics import ROOT from nes_object_graphics import P from validate_combat_rom import CPU ANGLE=P.index(bytes.fromhex('8400a9ff8501a503c504'))+0x790 ZORA=P.index(bytes.fromhex('ad6c06d0fa206d8ab5acc903'))-65536+0x8000 class Native(CPU): def __init__(self): super().__init__(4);self.m[0x6c90:0x7f00]=P[0x6500:0x7770];self.shot=False;self.destroyed=False;self.drawn=None def run_leaf(self,entry): self.pc=entry;self.depth=0 for _ in range(3000): if self.pc==0xfeb1:self.destroyed=True;return at=self.pc;op=self.byte() if op==0x60: if not self.depth:return self.pc=(self.pop()|self.pop()<<8)+1;self.depth-=1 elif op==0x20: target=self.word() if target==0x77db:self.drawn=self.a elif target==0x79d0:pass # Weapon/contact collisions are tested separately in the guest. elif target==0x82af:self.shot=True else: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();yes={0xf0:self.z,0xd0:not self.z,0xb0:self.c,0x90:not self.c,0x30:self.n,0x10:not self.n}[op] if yes:self.pc+=d if d<128 else d-256 elif op in [0xa9,0xa5,0xad,0xb5,0xbd,0xb9]: 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] else:v=self.m[self.word()+self.y] self.a=self.nz(v) elif op in [0xa0,0xa4,0xb4,0xbc]: v=self.byte() if op==0xa0 else self.m[self.byte()] if op==0xa4 else self.m[(self.byte()+self.x)&255] if op==0xb4 else self.m[self.word()+self.x];self.y=self.nz(v) elif op in [0x85,0x95,0x9d,0x84,0x94]: q=self.word()+self.x if op==0x9d else (self.byte()+self.x)&255 if op in [0x95,0x94] else self.byte();self.m[q]=self.y if op in [0x84,0x94] else self.a elif op in [0xc9,0xc0,0xc5,0xcd]: v=self.m[self.byte()] if op==0xc5 else self.m[self.word()] if op==0xcd else self.byte();lhs=self.y if op==0xc0 else self.a;self.c=lhs>=v;self.nz(lhs-v) elif op in [0x69,0x65,0xe9,0xe5,0x7d]: v=self.m[self.byte()] if op in [0x65,0xe5] else self.m[self.word()+self.x] if op==0x7d else self.byte();r=self.a-v-(not self.c) if op in [0xe9,0xe5] else self.a+v+self.c;self.c=r>=0 if op in [0xe9,0xe5] else r>255;self.a=self.nz(r) elif op in [0xde,0xce]: q=self.word()+(self.x if op==0xde else 0);self.m[q]=self.nz(self.m[q]-1) elif op==0x88:self.y=self.nz(self.y-1) elif op==0xa8:self.y=self.nz(self.a) elif op==0x48:self.push(self.a) elif op==0x68:self.a=self.nz(self.pop()) elif op==0x18:self.c=False elif op==0x38:self.c=True elif op==0x0a:self.c=self.a>=128;self.a=self.nz(self.a*2) elif op==0x49:self.a=self.nz(self.a^self.byte()) else:raise AssertionError(f'Unsupported opcode {op:02x} at {at:04x}') raise AssertionError('Leaf did not return') def angle(x,y): c=Native();c.m[3]=x;c.m[4]=y;c.y=4;c.run_leaf(ANGLE);return c.y def main(): cases=[];angles=[] for x in range(0,256,7): for y in range(0,256,7): native=angle(x,y);a,b=max(x,y),min(x,y);i=4;delta= -1 if x>=y else 1 if a-b>=8: while True: i+=delta if i in (0,8):break a-=b if a