|
- import sys, os, glob, itertools, random
- sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
- from helpertest import *
- from objfmt import Obj, pn
- from cpu6502 import CPU
-
- OPS = {
- 'lt': lambda a,b: int(a<b), 'gt': lambda a,b: int(a>b), 'le': lambda a,b: int(a<=b), 'ge': lambda a,b: int(a>=b),
- 'eq': lambda a,b: int(a==b), 'ne': lambda a,b: int(a!=b),
- 'add': lambda a,b: (a+b)&0xFFFF, 'sub': lambda a,b: (a-b)&0xFFFF, 'and': lambda a,b: a&b, 'or': lambda a,b: a|b, 'xor': lambda a,b: a^b,
- 'mul': lambda a,b: (a*b)&0xFFFF,
- }
- def s16(v): return v-65536 if v>=32768 else v
- vals=[0,1,2,3,5,100,255,256,257,1000,32767,32768,40000,65535,65534,12345]
- def run(h, name, a, b):
- cpu = CPU(); h.install(cpu)
- cpu.m[0x14]=0; cpu.m[0x15]=0x80; cpu.sp=0xF0
- for i,v in enumerate((a&255,a>>8,b&255,b>>8)): cpu.m[0x33C+i]=v
- cpu.a=4; cpu.x=0; cpu.y=0
- cpu.call(h.addr(name))
- return cpu.m[0x33C]|cpu.m[0x33D]<<8, cpu
- for f in sorted(glob.glob('lib/c*.obj')):
- o = Obj.parse(open(f,'rb').read())
- if f.endswith('c105.obj') or 'cstart' in f or 'conv' in f or 'localdefs' in f: continue
- if any(bytes(n).startswith(b'C$') is False for n,k,off in o.defs): pass
- ext={}
- try:
- h = Helper(o, 0x6000, {nm.decode('latin1') if False else bytes(nm): 0xD000 for nm,k,off in o.refs})
- except Exception as ex:
- print(os.path.basename(f), 'load fail', ex); continue
- for nm,k,off in o.defs:
- if not bytes(nm).startswith(b'C$'): continue
- res = {}
- try:
- for signed in (False, True):
- match = {}
- for opn,fn in OPS.items():
- ok = True
- for a,b in itertools.product(vals, vals):
- r,_ = run(h, nm, a, b)
- A,B = (s16(a),s16(b)) if signed else (a,b)
- exp = fn(A,B)
- exp &= 0xFFFF
- if r != exp: ok = False; break
- if ok: match[opn]=True
- if match: res[signed] = list(match)
- except Exception as ex:
- res = 'ERR %s' % ex
- print('%-14s %-10s size %3d %s' % (os.path.basename(f), pn(nm), len(o.code), res))
|