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