-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmem_map.py
70 lines (57 loc) · 1.75 KB
/
mem_map.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#
# mem_map.py
#
import uctypes
import urandom
AREA_HEAD = {
"magic": 0 | uctypes.UINT32,
}
RANDOM_SEED = 686889545
class MemMap():
class MemDesc():
def __init__(self, addr, size, magic):
self.addr = addr
self.size = size
self.magic = magic
def __init__(self, base, size, name_list):
"""
creates in-object object list of addresses + sizes.
"""
# remember random generator state (missing in uPy)
try:
rand_stat = urandom.getstate()
except AttributeError:
rand_stat = None
urandom.seed(RANDOM_SEED)
try:
self._base = base
self._name_map = {}
addr = self._base
# print("len name_list", len(name_list))
for i in range(0, len(name_list), 2):
name = name_list[i]
size = name_list[i+1]
# print(i, name, size)
if not isinstance(size, int):
raise ValueError # ("expected int address, not " + str(type(size)))
self._name_map[name] = MemMap.MemDesc(addr, size, urandom.getrandbits(32))
addr += size
finally:
if rand_stat:
urandom.setstate(rand_stat)
pass
def __getattr__(self, attr):
entry = self._name_map.get(attr)
if entry is None:
raise AttributeError("missing attribute: " + attr)
return entry
def test():
layout = [
'reserved', 64,
'fifo', 1024,
'something', 8,
]
mem = bytearray(2048)
m = MemMap(uctypes.addressof(mem), 2048, layout)
fifo_mem = m.fifo
print("fifo: addr={}".format(fifo_mem.addr))