#!/usr/bin/python import sys ccodes = ('', 'ifS', 'ifN', 'ifC') mnems = ( 'NOP', 'LOG', None, 'BIT', 'BRNG', 'TRIM', 'MULI', 'PHA', 'ADD', 'SUB', None, None, 'HIST', 'HIST', None, None, ) cops = ("=0=", "<", "==", "<=", ">", "!=", ">=", "=1=") def l3tup(c): "(cc, m, op, opp, x, y, (i,i,i)), s" cc = (c>>30) & 3 op = (c>>24) & 0x3f x = c & 0xff y = (c>>16)&0xff i = c>>8 if op & 0b100000: i &= 0x1fffff if i&0x100000: i -= 0x200000 return (cc, op, None, 'ADDI', x, None, (i,), "R[%d]%+d" % (x,i)) if op & 0b010000: i &= 0xff cop = op & 7 if op & 0x001000: s = "R[%d]%+%d %s R[%d]" % (x, i, cops[cop], y) i = (i, 0) else: s = "R[%d] %s R[%d]%+d" % (x, cops[cop], y, i) i = (0, i) return (cc, op, cop, 'CMP', x, y, i, s) if op == 0b0110: f = i & 0xfff e = (i>>12)& 15 return (cc, op, None, 'MULI', x, None, (f, e), "R[%d]*%d>>%d # %.4g" % (x, f, e, f/(2.0**e))) if (op&0b111010)==0b1000: pm = "+-"[op&1] sx = i&15 sy = (i>>4)&15 if sx>=8: sx-=16 ssx = "<<%d" % -sx else: ssx = ">>%d" % sx if sy>=8: sy-=16 ssy = "<<%d" % -sy else: ssy = ">>%d" % sy return (cc, op, op&1, mnems[op], x, y, (sx,sy), "R[%d]%s %s R[%d]%s" % (x, ssx, pm, y, ssy)) if op==0b0111: i &= 0xffff return (cc, op, None, 'PHA', x, None, (i,), "R[%d]+%d" % (x,i)) if op==0b0001: return (cc, op, None, 'LOG', x, None, (), "R[%d]" % (x,)) if op==0b0101: u = i&0xff v = (i>>8)&0xff return (cc, op, None, 'TRIM', x, None, (u,v), "R[%d], %d, %d" % (x,u,v)) if op==0b0100: u = i&0x1f v = (i>>5)&0x1f return (cc, op, None, 'BRNG', x, None, (u,v), "R[%d]{%d:%d}" % (x,v,u)) if op==0b0011: opp = (c>>20)&15 i &= 0x1f m = 'BITS' if opp&4 else 'BITC' n = '~' if opp&8 else '' if (opp&3)==3: s = '1' if opp&8 else '0' elif (opp&3)==0: s = '%sR[%d]{%d}' % (n, x, i) else: s = '%s%s' % (n, "RCS0"[opp&3]) return (cc, op, opp, m, x, None, (i,), s) if op==0x0010: i &= 0xff return (cc, op, None, 'POKE', x, None, (i,), 'R[%d] = R[%d]' % (i,x)) if op==0b0000: opp = (i>>12)&15 if opp==0b1100: i &= 0x3ff return (cc,op,opp,'GOTO',None,None,(i,),"%d" % i) if opp==0b1000: return (cc,op,opp,'NOP',None,None,(),"") if opp==0: return (cc,op,opp,'STOP',None,None,(),"") raise ValueError("illegal opcode 0x%08x\n" % c) def l3dis(c, addr=0, form=" %(cond)-3s %(mnem)-5s %(args)s\n"): cc = l3tup(c) ccc = { "addr": addr, "page": addr>>8, "target": addr & 0xff, "cond": ccodes[cc[0]], "mnem": cc[3], "args": cc[7], "opcode": cc[1], "x": cc[4], "y": cc[5], "instr": c, } return form % ccc def main(): import fileinput, getopt form = " %(cond)-3s %(mnem)-5s %(args)s\n" opt,files = getopt.getopt(sys.argv[1:], "F:") for o,v in opt: if o=='-F': form = v+'\n' a = -1 for l in fileinput.input(files): if l[0]=='@': ll = l[1:].split() aa = int(ll[0],16) c = int(ll[1],16) if aa!=a: sys.stdout.write(".=%d\n" % aa) a = aa sys.stdout.write(l3dis(c,addr=a, form=form)) a += 1 if __name__ == '__main__': main()