annotate python/target/armtoken.py @ 340:c7cc54c0dfdf devel

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