-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsimul.py
More file actions
323 lines (278 loc) · 13 KB
/
simul.py
File metadata and controls
323 lines (278 loc) · 13 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
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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
from lark import Token, Tree
from arch import Architecture
from base import Process, Variable, Statements, get_compile_value
from utils import evaluate, default_values
from typing import Tuple, List
import vcd_dump
class Simulation:
def __init__(self):
self.current_time = 0.
self.to_run_till = 0.
def perform_jump(self, archs: List[Architecture]):
min = 0.
for arch in archs:
for w in arch.waiting_process:
if min < w[1]:
min = w[1]
self.current_time = min
if self.current_time > self.to_run_till or min == 0.:
self.current_time = self.to_run_till + 1
simulation = Simulation()
def get_runtime_value(node, architecture: Architecture, symbol: List[Variable]) -> Tuple[str, str] | None:
'''
Binary Expression Tree
Literal Tree
Identifier Token
Return: Value, Type
'''
value = node.children[0]
if isinstance(value, Token):
if value.type == "IDENTIFIER":
for sym in symbol:
if sym.name == value.value:
return sym.value, sym.type
for s in architecture.signals:
if s.name == value.value:
return s.value, s.type
for p in architecture.entity.ports:
if p.name == value.value:
return p.value, p.type
if isinstance(value, Tree):
if value.data.value == "value":
return get_runtime_value(value, architecture, symbol)
if value.data.value == "literal":
literal = value.children[0]
return literal.value, literal.type
if value.data.value == "binary_expression":
v1, t1 = get_runtime_value(value.children[0], architecture, symbol)
v2, t2 = get_runtime_value(value.children[2], architecture, symbol)
op = value.children[1].value
result = evaluate(op, v1, v2, t1)
if op == '=' or op == '/=':
t1 = 'bool'
return result, t1
def execute_st(arch: Architecture, symbols: List[Variable], process: Process):
stack = process.statements.stack #copy reference only
def get_lvalue_reference(lval):
for p in arch.entity.ports:
if p.name == lval.value:
return p
for s in arch.signals:
if s.name == lval.value:
return s
for sym in symbols:
if sym.name == lval.value:
return sym
def evaluateCondition(condition: Tree) -> bool:
if isinstance(condition.children[0], Token) and condition.children[0].value == "(":
return evaluateCondition(condition.children[1])
elif isinstance(condition.children[1], Token) and condition.children[1].value == "and":
assert(isinstance(condition.children[0], Tree) and isinstance(condition.children[2], Tree))
return evaluateCondition(condition.children[0]) and evaluateCondition(condition.children[2])
elif isinstance(condition.children[1], Token) and condition.children[1].value == "or":
assert(isinstance(condition.children[0], Tree) and isinstance(condition.children[2], Tree))
return evaluateCondition(condition.children[0]) or evaluateCondition(condition.children[2])
elif isinstance(condition.children[0], Token) and condition.children[0].value == "not":
assert(isinstance(condition.children[1], Tree))
return not evaluateCondition(condition.children[1])
assert(isinstance(condition.children[0], Tree) and isinstance(condition.children[2], Tree) and isinstance(condition.children[1], Token))
lvalue = get_runtime_value(condition.children[0], arch, symbols)[0]
rvalue = get_runtime_value(condition.children[2], arch, symbols)[0]
if condition.children[1].value == "=":
if lvalue == rvalue:
return True
return False
elif condition.children[1].value == "/=":
if lvalue != rvalue:
return True
return False
return False
def fill_sts(stack, sts):
for st in reversed(sts.children):
stack.append(st.children[0])
while stack:
statement = stack[-1]
if statement.data.value == "shorthandprocess":
stack.pop()
lvalue = get_lvalue_reference(statement.children[0])
rvalue, _ = get_runtime_value(statement.children[1], arch, symbols)
lvalue.update_future_buffer(rvalue)
if lvalue not in arch.signals_changed:
arch.signals_changed.append(lvalue)
elif statement.data.value == "variable_assignment":
stack.pop()
lvalue = get_lvalue_reference(statement.children[0])
rvalue, _ = get_runtime_value(statement.children[1], arch, symbols)
lvalue.value = rvalue
elif statement.data.value == "wait":
stack.pop()
if len(statement.children) == 0:
return None
else:
def convert_to_nano(unit: str):
if unit == "ns":
return 1
elif unit == "us":
return 1_000
elif unit == "ms":
return 1_000_000
else: #"s"
return 1_000_000_000
value = int(statement.children[0])
unit = statement.children[1]
return value * convert_to_nano(unit)
elif statement.data.value == "report":
# TODO: Implement it
stack.pop()
return True
elif statement.data.value == "if_statement":
stack.pop()
condition = statement.children[0]
if(evaluateCondition(condition)):
# assert(isinstance(statement.children[1], Tree))
fill_sts(stack, statement.children[1])
else:
for i in range(2, len(statement.children)):
child = statement.children[i]
if child.data.value == "elsif":
childcondition = child.children[0]
if evaluateCondition(childcondition):
fill_sts(stack, child.children[1])
break
if child.data.value == "else":
fill_sts(stack, child.children[0])
elif statement.data.value == "while_statement":
condition = statement.children[0]
if evaluateCondition(condition):
fill_sts(stack, statement.children[1])
else:
stack.pop()
return None
def execute_process(process: Process, architecture: Architecture, waiting_process: bool) -> float | None:
# Set waiting_process True for arch.waiting_process and False for inactive_process
def execute_shorthandprocess(shprocess: Process): # WARNING: DO NOT REUSE FOR SHPROCESS INSIDE LONGFORM PROCESS!!!
def get_lvalue_reference(lval):
for p in architecture.entity.ports:
if p.name == lval.value:
return p
for s in architecture.signals:
if s.name == lval.value:
return s
statement = process.statements[0]
lvalue = get_lvalue_reference(statement.children[0])
rvalue, _ = get_runtime_value(statement.children[1], architecture, [])
lvalue.update_future_buffer(rvalue)
lvalue.value = rvalue
if lvalue not in architecture.signals_changed: #Intentional, replicate this
architecture.signals_changed.append(lvalue)
return None
def execute_longformprocess(lfprocess: Process, arch: Architecture): # Make this return None if it encounters wait without times
# wait breaks the execution and return queue_time
if len(lfprocess.statements.stack) == 0:
for st in reversed(lfprocess.statements.statements):
lfprocess.statements.stack.append(st)
queue_time = execute_st(arch, lfprocess.symbol_table, lfprocess)
if queue_time is not None:
return queue_time
# for i in range(lfprocess.statements.pc, len(lfprocess.statements.statements)):
# queue_time = execute_st(lfprocess.statements[i], arch, lfprocess.symbol_table, lfprocess)
# if queue_time is not None:
# return queue_time
if process.name == "shorthandprocess":
if waiting_process: # Only relevant for time 0
execute_shorthandprocess(process)
if len(process.sensitivity_list) > 0:
architecture.inactive_process.append(process)
else:
architecture.short_process_wo_sensitivity_list.append(process)
return None
else:
execute_shorthandprocess(process)
return None
else:
if waiting_process:
queue_time = execute_longformprocess(process, architecture)
if len(process.sensitivity_list) > 0: # Only relevant for time 0
architecture.inactive_process.append(process)
return None
return queue_time
else:
execute_longformprocess(process, architecture)
return None
def vcd_data(architectures: List[Architecture]):
print(f"At {simulation.current_time}")
for arch in architectures:
for p in arch.entity.ports:
print(p.name, p.value)
vcd_dump.dump(p.name, p.value)
for s in arch.signals:
print(s.name, s.value)
vcd_dump.dump(s.name, s.value)
for proc in arch.processes:
for v in proc.symbol_table:
print(v.name, v.value)
vcd_dump.set_time(simulation.current_time)
def run_simulation(exec_time: float, architectures: List[Architecture]):
simulation.to_run_till = simulation.current_time + exec_time
# Yes, super intentional
if simulation.current_time == 0.:
for arch in architectures:
for s in arch.signals:
s.value = default_values[s.type]
s.future_buffer = None
for pr in arch.processes:
try:
pr.statements.stack.clear()
except:
pass
arch.signals_changed.clear()
arch.waiting_process.clear()
arch.inactive_process.clear()
for process in arch.processes:
queue_time = execute_process(process, arch, True)
if queue_time is not None:
arch.waiting_process.append((process, queue_time))
temp_signals = arch.signals_changed[:]
arch.signals_changed.clear()
while(len(temp_signals)):
for sig in temp_signals:
sig.value = sig.value if sig.future_buffer is None or sig.future_buffer == 'None' else sig.future_buffer
sig.future_buffer = None
for sig in temp_signals:
for pr in sig.linked_process:
execute_process(pr, arch, False) # Guaranteed to be inactive (aka sensitivity list)
temp_signals.clear()
temp_signals = arch.signals_changed[:]
arch.signals_changed.clear()
vcd_data(architectures)
simulation.perform_jump(architectures)
while(simulation.current_time <= simulation.to_run_till):
for arch in architectures:
temp_process: List[Tuple[Process, float]] = []
for process in arch.waiting_process:
if process[1] == simulation.current_time:
queue_time = execute_process(process[0], arch, True)
if queue_time is not None:
temp_process.append((process[0], simulation.current_time + queue_time))
arch.waiting_process.clear()
arch.waiting_process.extend(temp_process)
for sproc in arch.short_process_wo_sensitivity_list:
execute_process(sproc,arch, False)
temp_signals = arch.signals_changed[:]
arch.signals_changed.clear()
while(len(temp_signals)):
for sig in temp_signals:
sig.value = sig.value if sig.future_buffer is None or sig.future_buffer == 'None' else sig.future_buffer
sig.future_buffer = None
for sig in temp_signals:
for pr in sig.linked_process:
execute_process(pr, arch, False) # Guaranteed to be inactive (aka sensitivity list)
temp_signals.clear()
temp_signals = arch.signals_changed[:]
arch.signals_changed.clear()
vcd_data(architectures)
simulation.perform_jump(architectures)
pass
simulation.current_time = simulation.to_run_till
vcd_data(architectures)
print()