def make_F1(u, op, a, b, K): assert bool(u) == u, repr(u) assert ops['Mov'] <= op <= ops['Div'], repr(op) assert 0 <= a < 0x10, repr(a) assert 0 <= b < 0x10, repr(b) if not (-0x10000 <= K < 0x10000): raise ValueError('Immediate value out of bounds' ' -0x10000 <= imm < 0x10000: %i' % (K,)) v = K < 0 if v: # Convert K to signed int and truncate upper 16 bits. K = blong(py2signed(K))[16:] return blong( (1 << 30) + # set q (u << 29) + (v << 28) + (a << 24) + (b << 20) + (op << 16) + K )
def make_F3(cond, c, invert=False, v=False): # v = True -> PC to be stored in register R15 assert 0 <= cond < 0x111, repr(cond) assert 0 <= c < 0x10, repr(c) assert bool(invert) == invert, repr(invert) assert bool(v) == v, repr(v) return blong( (0b11 << 30) + # set p, q (v << 28) + (invert << 27) + (cond << 24) + c )
def make_F0(u, op, a, b, c): assert bool(u) == u, repr(u) assert ops['Mov'] <= op <= ops['Div'], repr(op) assert 0 <= a < 0x10, repr(a) assert 0 <= b < 0x10, repr(b) assert 0 <= c < 0x10, repr(c) return blong( (u << 29) + (a << 24) + (b << 20) + (op << 16) + c )
def dis(n): ''' Take an integer and return a human-readable string description of the assembly instruction. ''' IR = blong(n)[32:] p, q = IR[31], IR[30] if not p: if not q: return dis_F0(IR) return dis_F1(IR) if not q: return dis_F2(IR) if not IR[29]: return dis_F3(IR) return dis_F3imm(IR)
def _signed(n, bits=16): limit = 2**bits if -limit < n < limit: q = ((n < 0) << (bits - 1)) + abs(n) return blong(q)[bits:] raise ValueError
def decode_set(i, size=32): w = blong(i) return {n for n in range(size) if w[n]}