forked from rftafas/hdltools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
map_signals.py
144 lines (90 loc) · 2.91 KB
/
map_signals.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
from format_text import indent
from dict_code import VHDLenum
class MapObj:
def __init__(self, port, signal, conn_range = ""):
self.port = port
self.signal = signal
self.conn_range = conn_range
def code(self, indent_level = 0):
return self.port + self.conn_range + " => " + \
self.signal + ",\n"
class MapList(dict):
def __init__(self, elements_list, mode = "auto", *args, **kwargs):
self.elements_list = elements_list
if mode == "auto":
for key in self.elements_list:
name = self.elements_list[key].name
self[name] = MapObj(name, name)
elif mode == "pos":
if len(args) != len(self.elements_list):
print("Error, wrong number of elements in"
" signals mapping\n")
exit(-1)
else:
for source, target in zip(args, self.elements_list):
if type(source) == str:
source_name = source
elif hasattr(source, "name"):
source_name = source.name
else:
raise ValueError("Wrong source in"
" signal mapping")
target_name = self.elements_list[target].name
self[target_name] = MapObj(target_name,
source_name)
elif mode == "key":
if len(kwargs) != len(self.elements_list):
mapped = []
for key in kwargs:
mapped.append(key)
print("Elements to map: " +
str(self.elements_list) + "\nElements"
" mapped: " + str(mapped) + "\n")
raise ValueError("Wrong number of elements in"
" signals mapping")
for target in self.elements_list:
if self.elements_list[target].name in kwargs:
if type(kwargs[target]) == str:
source_name = kwargs[target]
elif hasattr(kwargs[target], "name"):
source_name = \
kwargs[target].name
else:
raise ValueError("Wrong source in"
" signal mapping")
target_name = self.elements_list[target].name
self[target_name] = MapObj(target_name,
source_name)
else:
raise ValueError("Signal not mapped")
elif mode == "self":
for key in self.elements_list:
name = self.elements_list[key].name
value = str(self.elements_list[key].value)
self[name] = MapObj(name, value)
elif mode == "no":
pass
else:
raise ValueError("Wrong instance mode")
def add(self, target_name, source, conn_range = ""):
present = False
for target in self.elements_list:
if target_name == self.elements_list[target].name:
present = True
if present:
if conn_range and target_name in self:
print(self.code())
del self[target_name]
if type(source) == str:
self[target_name + conn_range] = \
MapObj(target_name, source, conn_range)
elif hasattr(source, "name"):
self[target_name + conn_range] = \
MapObj(target_name, source.name,
conn_range)
else:
raise ValueError("Wrong source in signal mapping")
else:
raise ValueError("Signal " + target_name + " not present")
def code(self, indent_level = 0):
return VHDLenum(self, indent_level)