forked from luca-della-vedova/axi_python_utilities
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJSONDriverParser.py
More file actions
83 lines (74 loc) · 3.06 KB
/
JSONDriverParser.py
File metadata and controls
83 lines (74 loc) · 3.06 KB
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
71
72
73
74
75
76
77
78
79
80
81
82
83
import re
import json
def parse_registers(filepath):
"""
Parses a header (.h) file containing register descriptions and definitions
and returns a JSON string.
Args:
filepath (str): Path to the header file.
Returns:
str: JSON string containing parsed register data.
"""
registers = []
definitions = {}
with open(filepath, 'r', encoding='utf-8') as file:
lines = file.readlines()
i = 0
while i < len(lines):
line = lines[i].strip()
# Match lines with an address and a colon, e.g., "// 0x10 : Data signal of Kp_PLL"
reg_match = re.match(r'^//\s*(0x[0-9A-Fa-f]+)\s*:\s*(.*)$', line)
if reg_match:
address = reg_match.group(1)
desc_text = reg_match.group(2).strip()
# If the description indicates "reserved", mark it as such.
if "reserved" in desc_text.lower():
entry = {
"address": address,
"type": "reserved"
}
else:
# Set default values; update if bit information is found.
entry = {
"name": "Unknown",
"address": address,
"description": desc_text,
"access": "Unknown"
}
# Check if the next line contains bit information.
if i + 1 < len(lines):
next_line = lines[i + 1].strip()
# Expected formats:
# Data signal: // bit 31~0 - Kp_PLL[31:0] (Read/Write)
# Control signal: // bit 0 - Vd_ap_vld (Read/COR)
bit_match = re.match(
r'^//\s+bit\s+[\d~]+\s*-\s*(\w+)(?:\[\d+:\d+\])?\s*\(([^)]+)\)',
next_line
)
if bit_match:
entry["name"] = bit_match.group(1)
entry["access"] = bit_match.group(2)
entry["address"] = int(entry["address"], 16)
i += 1 # Skip the bit line after processing it.
registers.append(entry)
i += 1
# Parse #define constants for register addresses.
for line in lines:
define_match = re.match(
r'^#define\s+(X\w+)_CONTROL_ADDR_(\w+)_DATA\s+(0x[0-9A-Fa-f]+)',
line.strip()
)
if define_match:
macro = define_match.group(1)
reg_name = define_match.group(2)
addr = define_match.group(3)
if macro not in definitions:
definitions[macro] = {}
definitions[macro][reg_name] = addr
#Delete resevred by searching dictionaries with the entry keyed "type", other entries do not have this key
registers = [reg for reg in registers if "type" not in reg]
registers = {reg["name"]: reg for reg in registers}
#delete name as entry inside every register
for reg in registers:
del registers[reg]["name"]
return registers