341
|
1
|
|
2 import struct
|
|
3
|
|
4 def u16(h):
|
|
5 return struct.pack('<H', h)
|
340
|
6
|
|
7 def val2bit(v, bits):
|
|
8 b = []
|
|
9 for i in range(bits):
|
|
10 b.append(bool((1<<i) & v))
|
|
11 #b.reverse()
|
|
12 return b
|
|
13
|
|
14
|
|
15 def bit_range(b, e):
|
|
16 getter = lambda s: s[b:e]
|
|
17 def setter(s, v):
|
|
18 s[b:e] = v
|
|
19 return property(getter, setter)
|
|
20
|
|
21
|
341
|
22 class Token:
|
340
|
23 def set_bit(self, i, value):
|
|
24 value = bool(value)
|
|
25 assert i in range(0, 16)
|
|
26 mask = 1 << i
|
|
27 if value:
|
|
28 self.bit_value |= mask
|
|
29 else:
|
|
30 self.bit_value &= (~mask)
|
|
31
|
|
32 def __getitem__(self, key):
|
|
33 return False
|
|
34
|
|
35 def __setitem__(self, key, value):
|
|
36 if type(key) is int:
|
|
37 self.set_bit(key, value)
|
|
38 elif type(key) is slice:
|
|
39 assert key.step is None
|
|
40 bits = key.stop - key.start
|
|
41 value_bits = val2bit(value, bits)
|
|
42 for i in range(key.start, key.stop):
|
|
43 self.set_bit(i, value_bits[i - key.start])
|
|
44 else:
|
|
45 raise KeyError()
|
|
46
|
341
|
47 class ThumbToken(Token):
|
|
48 def __init__(self):
|
|
49 self.bit_value = 0
|
|
50
|
|
51
|
340
|
52 rd = bit_range(0, 3)
|
|
53
|
|
54 def encode(self):
|
|
55 return u16(self.bit_value)
|
|
56
|
|
57
|
341
|
58 class ArmToken(Token):
|
|
59 def __init__(self):
|
|
60 self.bit_value = 0
|
340
|
61
|