forked from olofk/ipyxact
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gen_c_header.py
57 lines (49 loc) · 2.22 KB
/
gen_c_header.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
#ipyxact example. Parses an IP-XACT XML file called generic_example.xml
#and prints out a C header of the register maps found
import sys
from ipyxact.ipyxact import Component
def gen_mask(offset, width):
mask = 0
for i in range(offset, offset+width):
mask += 1<<i
return mask
def print_c_header(memory_maps, offset=0, name=None):
s = ""
for m in memory_maps.memoryMap:
if name:
mname=name.upper()
else:
mname = m.name.upper()
multiblock = len(m.addressBlock) > 1
for block in m.addressBlock:
if multiblock:
bname = mname + '_' + block.name.upper()
else:
bname = mname
for reg in sorted(block.register, key=lambda addr: addr.addressOffset):
s += "#define {}_{} 0x{:08X} \n".format(bname,
reg.name.upper(),
offset + block.baseAddress + reg.addressOffset)
if reg.field:
for f in sorted(reg.field, key=lambda x: x.bitOffset):
s += "#define {}_{}_{}_MASK 0x{:08X}\n".format(bname,
reg.name.upper().replace('-','_'),
f.name.upper().replace('-','_'),
gen_mask(f.bitOffset, f.bitWidth))
if f.enumeratedValues:
s += "\ntypedef enum {\n"
for es in f.enumeratedValues:
s += "".join([" {}_{} = {},\n".format(f.name.lower(), e.name, e.value<<f.bitOffset) for e in sorted(es.enumeratedValue, key=lambda x: x.value)])
s+= "}} {}_t;\n\n".format(f.name.lower())
s += "\n"
return s
def write_c_header(f, offset, name):
component = Component()
component.load(f)
return print_c_header(component.memoryMaps, offset, name)
if __name__ == "__main__":
f = open(sys.argv[1])
name = None
offset = 0
print(write_c_header(f, offset, name))
f.close()