forked from multigcs/LinuxCNC-RIO
-
Notifications
You must be signed in to change notification settings - Fork 0
/
projectLoader.py
265 lines (233 loc) · 9.8 KB
/
projectLoader.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
import copy
import glob
import importlib
import json
import os
import sys
def load(configfile):
project = {}
project["config"] = configfile
if not os.path.isfile(project["config"]):
print("")
print(f"this is not a file: {project['config']}")
print("")
exit(1)
try:
with open(project["config"], "r") as f:
data = f.read()
except IOError as err:
print("")
print(err)
print("")
exit(1)
try:
project["jdata"] = json.loads(data)
except ValueError as err:
print("")
print(f"JSON error: {err}")
print("please check your json syntax")
print("")
exit(1)
if "plugins" not in project["jdata"] and "slots" not in project["jdata"]:
print("ERROR: old json config format, please run 'python3 convert-configs.py'")
sys.exit(1)
# loading board data
board = project["jdata"].get("boardcfg")
if board:
print(f"loading board setup: {board}")
bdata = open(f"boards/{board}.json", "r").read()
project["board_data"] = json.loads(bdata)
if "name" in project["board_data"]:
project["board"] = project["board_data"]["name"]
for key, value in project["board_data"].items():
if key not in project["jdata"]:
project["jdata"][key] = value
print(f"loading board setup: {board}")
bdata = open(f"boards/{board}.json", "r").read()
project["board"] = json.loads(bdata)
if "name" in project["board"]:
project["board"]["boardname"] = project["board"]["name"]
del project["board"]["name"]
for key, value in project["board"].items():
if key not in project["jdata"]:
project["jdata"][key] = value
# loading modules
project["modules"] = {}
for path in glob.glob("modules/*.json"):
module = path.split("/")[1].split(".")[0]
mdata = open(path, "r").read()
project["modules"][module] = json.loads(mdata)
for ftype in ("interface", "expansion", "joints", "plugins"):
if ftype not in project["jdata"]:
project["jdata"][ftype] = []
# import module data
for slot_n, slot in enumerate(project["jdata"].get("slots", [])):
spins = slot["pins"]
slotname = slot.get("name", f"slot{slot_n}")
modules = []
# check old config style
if "module" in slot:
module = slot.get("module")
ssetup = slot.get("setup")
print(f"WARNING: found old config style for slot modules, please update: {module}")
modules.append({
"slot": slotname,
"module": module,
"setup": ssetup,
})
# check new config style
modules += project["jdata"].get("modules", {})
# merge modules
for modulesetup in modules:
if modulesetup.get("slot") != slotname:
continue
module = modulesetup.get("module")
ssetup = modulesetup.get("setup")
if module in project["modules"]:
module_data = copy.deepcopy(project["modules"][module])
if "enable" in module_data:
project["jdata"]["enable"] = module_data["enable"]
project["jdata"]["enable"]["pin"] = slot["pins"][
module_data["enable"]["pin"]
]
for ftype in ("interface", "expansion", "joints", "plugins"):
if ftype in module_data:
for jn, msetup in enumerate(module_data.get(ftype, [])):
# overwrite with setup data
if ssetup:
ssetup_data = ssetup.get(ftype, [])
if len(ssetup_data) > jn:
msetup.update(ssetup_data[jn])
# rewrite pins
if "pin" in msetup:
realpin = spins[msetup["pin"]]
msetup["pin"] = realpin
for pname, pin in msetup.get("pins", {}).items():
realpin = spins[pin]
msetup["pins"][pname] = realpin
module_data[ftype][jn] = msetup
# merge into jdata
project["jdata"][ftype] += module_data[ftype]
else:
print(f"ERROR: module {module} not found")
exit(1)
# loading plugins
project["plugins"] = {}
for path in sorted(glob.glob("plugins/*")):
plugin = path.split("/")[1]
if os.path.isfile(f"plugins/{plugin}/plugin.py"):
vplugin = importlib.import_module(".plugin", f"plugins.{plugin}")
project["plugins"][plugin] = vplugin.Plugin(project["jdata"])
project["generators"] = {}
for path in glob.glob("generators/*"):
generator = path.split("/")[1]
if os.path.isfile(f"generators/{generator}/{generator}.py"):
project["generators"][generator] = importlib.import_module(
f".{generator}", f"generators.{generator}"
)
project["verilog_files"] = []
project["component_files"] = []
project["verilog_defines"] = {}
project["gateware_extrafiles"] = {}
project["pinlists"] = {}
project["expansions"] = {}
project["internal_clock"] = None
project["osc_clock"] = project["jdata"]["clock"].get("osc", False)
project["internal_clock"] = project["jdata"]["clock"].get("internal")
if project["internal_clock"]:
pass
elif project["osc_clock"]:
project["pinlists"]["main"] = (
("sysclk_in", project["jdata"]["clock"]["pin"], "INPUT", False),
)
else:
project["pinlists"]["main"] = (
("sysclk", project["jdata"]["clock"]["pin"], "INPUT", False),
)
if "blink" in project["jdata"]:
project["pinlists"]["blink"] = (
("BLINK_LED", project["jdata"]["blink"]["pin"], "OUTPUT"),
)
if "error" in project["jdata"]:
project["pinlists"]["error"] = (
("ERROR_OUT", project["jdata"]["error"]["pin"], "OUTPUT"),
)
if "enable" in project["jdata"]:
project["pinlists"]["enable"] = (
("ENA", project["jdata"]["enable"]["pin"], "OUTPUT"),
)
for plugin in project["plugins"]:
if hasattr(project["plugins"][plugin], "pinlist"):
project["pinlists"][plugin] = project["plugins"][plugin].pinlist()
if hasattr(project["plugins"][plugin], "expansions"):
project["expansions"][plugin] = project["plugins"][plugin].expansions()
if hasattr(project["plugins"][plugin], "ipdefs"):
project["verilog_defines"].update(project["plugins"][plugin].ipdefs())
if hasattr(project["plugins"][plugin], "gateware_extrafiles"):
project["gateware_extrafiles"].update(
project["plugins"][plugin].gateware_extrafiles()
)
# pinmapping
pinmapping = project["jdata"].get("pinmapping", {})
for pname, pins in project["pinlists"].items():
project["pinlists"][pname] = list(project["pinlists"][pname])
if pins:
pins = list(pins)
for pn, pin in enumerate(pins):
project["pinlists"][pname][pn] = list(project["pinlists"][pname][pn])
if project["pinlists"][pname][pn][1] in pinmapping:
project["pinlists"][pname][pn][1] = pinmapping[project["pinlists"][pname][pn][1]]
# check for double assigned pins
double_pins = False
uniq_pins = {}
for pinlist in project["pinlists"].values():
for pinsetup in pinlist:
pin_id = pinsetup[1]
if pin_id in uniq_pins:
print()
print(f"ERROR: pin {pin_id} allready in use")
print(f" old: {uniq_pins[pin_id]}")
print(f" new: {pinsetup}")
double_pins = True
else:
uniq_pins[pin_id] = pinsetup
if double_pins:
print("")
exit(1)
for dtype in ("vin", "vout", "din", "dout", "joint", "bin", "bout"):
tname = f"{dtype}names"
if tname not in project:
project[tname] = []
for plugin in project["plugins"]:
if hasattr(project["plugins"][plugin], tname):
func = getattr(project["plugins"][plugin], tname)
ndata = func()
if ndata:
for part in ndata:
part["_plugin"] = plugin
project[tname] += ndata
project[f"{dtype}s"] = len(project[tname])
project[f"{dtype}s_total"] = max((len(project[tname]) + 7) // 8 * 8, 8)
project["jointtypes"] = []
for joint in project["jointnames"]:
project["jointtypes"].append(joint["type"])
project["joints_en_total"] = (project["joints"] + 7) // 8 * 8
project["tx_data_size"] = 32
project["tx_data_size"] += project["joints"] * 32
for vin in project["vinnames"]:
bits = vin.get("_bits", 32)
project["tx_data_size"] += bits
project["tx_data_size"] += project["dins_total"]
if "binnames" in project:
for binpart in project["binnames"]:
project["tx_data_size"] += binpart["size"]
project["rx_data_size"] = 32
project["rx_data_size"] += project["joints"] * 32
project["rx_data_size"] += project["vouts"] * 32
project["rx_data_size"] += project["joints_en_total"]
project["rx_data_size"] += project["douts_total"]
if "boutnames" in project:
for binpart in project["boutnames"]:
project["rx_data_size"] += binpart["size"]
project["data_size"] = max(project["tx_data_size"], project["rx_data_size"])
return project