annotate python/ppci/target/token.py @ 365:98ff43cfdd36

Nasty bug in adr instruction
author Windel Bouwman
date Wed, 19 Mar 2014 22:32:04 +0100
parents 86b02c98a717
children 988f3fb861e4
rev   line source
342
86b02c98a717 Moved target directory
Windel Bouwman
parents:
diff changeset
1
86b02c98a717 Moved target directory
Windel Bouwman
parents:
diff changeset
2 import struct
86b02c98a717 Moved target directory
Windel Bouwman
parents:
diff changeset
3
86b02c98a717 Moved target directory
Windel Bouwman
parents:
diff changeset
4 def u16(h):
86b02c98a717 Moved target directory
Windel Bouwman
parents:
diff changeset
5 return struct.pack('<H', h)
86b02c98a717 Moved target directory
Windel Bouwman
parents:
diff changeset
6
86b02c98a717 Moved target directory
Windel Bouwman
parents:
diff changeset
7
86b02c98a717 Moved target directory
Windel Bouwman
parents:
diff changeset
8 def u32(x):
86b02c98a717 Moved target directory
Windel Bouwman
parents:
diff changeset
9 return struct.pack('<I', x)
86b02c98a717 Moved target directory
Windel Bouwman
parents:
diff changeset
10
86b02c98a717 Moved target directory
Windel Bouwman
parents:
diff changeset
11
86b02c98a717 Moved target directory
Windel Bouwman
parents:
diff changeset
12 def val2bit(v, bits):
86b02c98a717 Moved target directory
Windel Bouwman
parents:
diff changeset
13 b = []
86b02c98a717 Moved target directory
Windel Bouwman
parents:
diff changeset
14 for i in range(bits):
86b02c98a717 Moved target directory
Windel Bouwman
parents:
diff changeset
15 b.append(bool((1<<i) & v))
86b02c98a717 Moved target directory
Windel Bouwman
parents:
diff changeset
16 #b.reverse()
86b02c98a717 Moved target directory
Windel Bouwman
parents:
diff changeset
17 return b
86b02c98a717 Moved target directory
Windel Bouwman
parents:
diff changeset
18
86b02c98a717 Moved target directory
Windel Bouwman
parents:
diff changeset
19
86b02c98a717 Moved target directory
Windel Bouwman
parents:
diff changeset
20 def bit_range(b, e):
86b02c98a717 Moved target directory
Windel Bouwman
parents:
diff changeset
21 """ Property generator function """
86b02c98a717 Moved target directory
Windel Bouwman
parents:
diff changeset
22 getter = lambda s: s[b:e]
86b02c98a717 Moved target directory
Windel Bouwman
parents:
diff changeset
23 def setter(s, v):
86b02c98a717 Moved target directory
Windel Bouwman
parents:
diff changeset
24 s[b:e] = v
86b02c98a717 Moved target directory
Windel Bouwman
parents:
diff changeset
25 return property(getter, setter)
86b02c98a717 Moved target directory
Windel Bouwman
parents:
diff changeset
26
86b02c98a717 Moved target directory
Windel Bouwman
parents:
diff changeset
27
86b02c98a717 Moved target directory
Windel Bouwman
parents:
diff changeset
28 class Token:
86b02c98a717 Moved target directory
Windel Bouwman
parents:
diff changeset
29 def __init__(self, bitsize):
86b02c98a717 Moved target directory
Windel Bouwman
parents:
diff changeset
30 self.bitsize = bitsize
86b02c98a717 Moved target directory
Windel Bouwman
parents:
diff changeset
31 self.bit_value = 0
86b02c98a717 Moved target directory
Windel Bouwman
parents:
diff changeset
32
86b02c98a717 Moved target directory
Windel Bouwman
parents:
diff changeset
33 def set_bit(self, i, value):
86b02c98a717 Moved target directory
Windel Bouwman
parents:
diff changeset
34 value = bool(value)
86b02c98a717 Moved target directory
Windel Bouwman
parents:
diff changeset
35 assert i in range(0, self.bitsize)
86b02c98a717 Moved target directory
Windel Bouwman
parents:
diff changeset
36 mask = 1 << i
86b02c98a717 Moved target directory
Windel Bouwman
parents:
diff changeset
37 if value:
86b02c98a717 Moved target directory
Windel Bouwman
parents:
diff changeset
38 self.bit_value |= mask
86b02c98a717 Moved target directory
Windel Bouwman
parents:
diff changeset
39 else:
86b02c98a717 Moved target directory
Windel Bouwman
parents:
diff changeset
40 self.bit_value &= (~mask)
86b02c98a717 Moved target directory
Windel Bouwman
parents:
diff changeset
41
86b02c98a717 Moved target directory
Windel Bouwman
parents:
diff changeset
42 def __getitem__(self, key):
86b02c98a717 Moved target directory
Windel Bouwman
parents:
diff changeset
43 return False
86b02c98a717 Moved target directory
Windel Bouwman
parents:
diff changeset
44
86b02c98a717 Moved target directory
Windel Bouwman
parents:
diff changeset
45 def __setitem__(self, key, value):
86b02c98a717 Moved target directory
Windel Bouwman
parents:
diff changeset
46 if type(key) is int:
86b02c98a717 Moved target directory
Windel Bouwman
parents:
diff changeset
47 self.set_bit(key, value)
86b02c98a717 Moved target directory
Windel Bouwman
parents:
diff changeset
48 elif type(key) is slice:
86b02c98a717 Moved target directory
Windel Bouwman
parents:
diff changeset
49 assert key.step is None
86b02c98a717 Moved target directory
Windel Bouwman
parents:
diff changeset
50 bits = key.stop - key.start
86b02c98a717 Moved target directory
Windel Bouwman
parents:
diff changeset
51 value_bits = val2bit(value, bits)
86b02c98a717 Moved target directory
Windel Bouwman
parents:
diff changeset
52 for i in range(key.start, key.stop):
86b02c98a717 Moved target directory
Windel Bouwman
parents:
diff changeset
53 self.set_bit(i, value_bits[i - key.start])
86b02c98a717 Moved target directory
Windel Bouwman
parents:
diff changeset
54 else:
86b02c98a717 Moved target directory
Windel Bouwman
parents:
diff changeset
55 raise KeyError()
86b02c98a717 Moved target directory
Windel Bouwman
parents:
diff changeset
56