-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathusableBlocks.py
382 lines (317 loc) · 13.2 KB
/
usableBlocks.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
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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
from blocks import *
from utilities import checkType, printErrorAndExit
class MooreMachine(HasInputConnections, HasOutputConnections, HasRegisters):
"""
A machine is both a HasInputConnections block and a HasOutputConnections block.
This represents the Moore Machine.
"""
def __init__(self, **kwargs):
"""
Use keyword arguments to pass the following parameters:
@param : env must be a simpy environment.
@param : clock must be of type Clock.
@param : nsl must be a valid function specifying next state logic.
@param : nsl_delay is the time taken by the NSL to run.
@param : ol must be a valid function specifying output logic.
@param : ol_delay is the time taken by the OL to run.
@param : blockID is the id of this input block. If blockId is a duplicate
or None, then new unique ID is given.
@param : register_delay is the time taken by the register to update.
"""
self.nsl = kwargs.get("nsl")
self.ol = kwargs.get("ol")
self.nsl_delay = kwargs.get("nsl_delay", 0.01)
self.ol_delay = kwargs.get("ol_delay", 0.01)
super().__init__(**kwargs)
self._scopeDump.add(f"Input to {self.getBlockID()}", 0, self._output[0])
def __str__(self):
"""
@return str : a string representation of this machine.
"""
return f"MooreMachine ID {self.getBlockID()}"
def __runNSL(self):
"""
Runs the next state logic if the input to this machine changed.
"""
# adding the inputs to scopedump
self._scopeDump.add(f"Input to {self.getBlockID()}", self._env.now, self.getInputVal())
# running the NSL
tempout = self.nsl(self.getPS(), self.getInputVal())
yield self._env.timeout(self.nsl_delay)
# updating the next State
self.setNS(tempout)
self._scopeDump.add(f"NS of {self.getBlockID()}", self._env.now, self.getNS())
def __runOL(self):
"""
Output logic runs when the output value is changed.
"""
temp = self.ol(self.getPS())
yield self._env.timeout(self.ol_delay)
self._output[0] = temp
self._scopeDump.add(f"output of {self.getBlockID()}", self._env.now, self._output[0])
# triggering events for the connected machines
self.processFanOut()
def runNSL(self):
self._env.process(self.__runNSL())
def runOL(self):
self._env.process(self.__runOL())
def run(self):
"""
Runs this block.
"""
self._env.process(self.__runNSL())
self._env.process(self.__runOL())
def isConnected(self):
"""
@return bool : True if this block is connected to everything, False otherwise.
"""
return self._clkVal != [] and self.nsl != None and self.ol != None and self.isConnectedToInput()
# left, right are for future versions. NOT USED IN CURRENT VERSION.
def input(self, left=None, right=None):
"""
@return MooreMachine : the instance of this class for connection purposes.
"""
self._isClock = 0 # 1 for clock, 0 for clock as input and -1 for not being used
return self
class MealyMachine(HasInputConnections, HasOutputConnections, HasRegisters):
"""
A machine is both a HasInputConnections block and a HasOutputConnections block.
This represents the Moore Machine.
"""
def __init__(self, **kwargs):
"""
Use keyword arguments to pass the following parameters:
@param : env must be a simpy environment.
@param : clock must be of type Clock.
@param : nsl must be a valid function specifying next state logic.
@param : nsl_delay is the time taken by the NSL to run.
@param : ol must be a valid function specifying output logic.
@param : ol_delay is the time taken by the OL to run.
@param : blockID is the id of this input block. If blockId is a duplicate
or None, then new unique ID is given.
@param : register_delay is the time taken by the register to update.
"""
self.nsl = kwargs.get("nsl")
self.nsl_delay = kwargs.get("nsl_delay", 0.01)
self.ol = kwargs.get("ol")
self.ol_delay = kwargs.get("ol_delay", 0.01)
super().__init__(**kwargs)
self._scopeDump.add(f"Input to {self.getBlockID()}", 0, self._output[0])
def __str__(self):
"""
@return str : a string representation of this machine.
"""
return f"Mealy Machine ID {self.getBlockID()}"
def __runNSL(self):
"""
Runs the next state logic if the input to this machine changed.
"""
# adding the inputs to scopedump
self._scopeDump.add(f"Input to {self.getBlockID()}", self._env.now, self.getInputVal())
# running the NSL
tempout = self.nsl(self.getPS(), self.getInputVal())
yield self._env.timeout(self.nsl_delay)
# updating the next State
self.setNS(tempout)
self._scopeDump.add(f"NS of {self.getBlockID()}", self._env.now, self.getNS())
def __runOL(self):
"""
Output logic runs when the output value is changed.
"""
temp = self.ol(self.getPS(), self.getInputVal())
yield self._env.timeout(self.ol_delay)
self._output[0] = temp
self._scopeDump.add(f"output of {self.getBlockID()}", self._env.now, self._output[0])
# triggering events for the connected machines
self.processFanOut()
def runNSL(self):
self._env.process(self.__runNSL())
def runOL(self):
self._env.process(self.__runOL())
def run(self):
"""
Runs this block.
"""
self._env.process(self.__runNSL())
self._env.process(self.__runOL())
def isConnected(self):
"""
@return bool : True if this block is connected to everything, False otherwise.
"""
return self._clkVal != [] and self.nsl != None and self.ol != None and self.isConnectedToInput()
# left, right are for future versions. NOT USED IN CURRENT VERSION.
def input(self, left=None, right=None):
"""
@return MooreMachine : the instance of this class for connection purposes.
"""
self._isClock = 0 # 1 for clock, 0 for clock as input and -1 for not being used
return self
class Input(HasOnlyOutputConnections):
"""
An input is a HasOutputConnections block.
This takes the input values from a file and passes it on in a format understood by the simulator.
"""
def __init__(self, **kwargs):
"""
Use keyword arguments to pass the following parameters:
@param inputList : must be a list that contains the input changes
at the time intervals.
@param env : is the simpy environment.
@param blockID : is the id of this input block. If blockID is a
duplicate or None, then new unique ID is given.
"""
inputList = kwargs.get("inputList", [(0, 0)])
maxOutSize = 2
for i in inputList:
if (len(bin(i[1])) > maxOutSize):
maxOutSize = len(bin(i[1]))
maxOutSize -= 2
self.__input = inputList
super().__init__(maxOutSize=maxOutSize, **kwargs)
self._scopeDump.add(f"Input to {self.getBlockID()}", 0, self._output[0])
def __str__(self):
"""
@return str : the string representation of this input block.
"""
return f"Input ID {self.getBlockID()}"
def _go(self):
"""
Runs the input at every change in input value specified by inputList.
"""
for i in self.__input:
yield self._env.timeout(i[0]-self._env.now)
self._output[0] = i[1]
self._scopeDump.add(f"Input to {self.getBlockID()}", self._env.now, self._output[0])
self.processFanOut()
class Clock(HasOnlyOutputConnections):
"""
A clock is a HasOutputConnections block.
This represents the Clock Block.
"""
def __init__(self, **kwargs):
"""
Use keyword arguments to pass the following parameters:
@param timePeriod : the time period of the clock.
@param onTime : the amount of time in each cycle that the clock shows high.
@param env : is the simpy environment.
@param blockID : is the id of this input block. If blockID is a
duplicate or None, then new unique ID is given.
@param initialValue : is the initial state of the clock.
"""
timePeriod = kwargs.get("timePeriod", 1)
onTime = kwargs.get("onTime", 0.5)
initialValue = kwargs.get("initialValue", 0)
checkType([(timePeriod, (int, float)), (onTime, (int, float)), (initialValue, int)])
if (timePeriod < onTime):
printErrorAndExit(f"Clock {self} cannot have timePeriod = {timePeriod} less than onTime = {onTime}.")
self.__timePeriod = timePeriod
self.__onTime = onTime
super().__init__(**kwargs)
self._output[0] = initialValue & 1
self._scopeDump.add(f"Clock {self.getBlockID()}", 0, self._output[0])
# left, right are for future versions. NOT USED IN CURRENT VERSION.
def output(self, left=None, right=None):
"""
@return Clock : this object for connection purposes.
"""
return self
def __str__(self):
"""
@return str : the string representation of this clock block.
"""
return f"Clock ID {self.getBlockID()}"
def _go(self):
"""
Runs the clock at every time period.
"""
while True:
yield self._env.timeout((1-self._output[0])*(self.__timePeriod - self.__onTime)+self._output[0]*(self.__onTime))
self._output[0] = 1 - self._output[0]
self._scopeDump.add(f"Clock {self.getBlockID()}", self._env.now, self._output[0])
self.processFanOut()
class Output(HasInputConnections):
"""
An Output is a HasInputConnections block.
This represents the Output Block.
blockID is the id of this input block. If None,
then new unique ID is given.
"""
def __init__(self, **kwargs):
"""
Use keyword arguments to pass the following parameters:
@param env : is a simpy environment.
@param blockID : is the id of this input block. If blockID is a
duplicate or None, then new unique ID is given.
"""
super().__init__(**kwargs)
def __str__(self):
"""
@return str : a string representation of the output block.
"""
return f"Output ID {self.getBlockID()}"
def __give(self):
"""
Adds the output value to this class every time there is a change in it.
"""
yield self._env.timeout(0.01)
self._scopeDump.add(f"Final Output from {self.getBlockID()}", self._env.now, self.getInputVal())
def run(self):
"""
Runs the output block
"""
self._env.process(self.__give())
def isConnected(self):
"""
@return bool : True if this block is connected to everything, False otherwise.
"""
return self.isConnectedToInput()
class Combinational(HasInputConnections, HasOutputConnections):
"""
This class is used to create a combinatorial block.
It requires a function to be passed to it which will be used to calculate the output.
"""
def __init__(self, **kwargs):
"""
Use keyword arguments to pass the following parameters:
@param func : is the function that will be used to calculate the output.
@param delay : is the delay in the output.
@param env : is the simpy environment.
@param plot : is a boolean variable which represents whether or not we should plot this class.
@param blockID : is the id of this input block. If blockID is a duplicate or None, then new unique ID is given.
@param initialValue : the initial value of the block.
"""
func = kwargs.get("func", None)
delay = kwargs.get("delay", 0)
initialValue = kwargs.get("initialValue", 0)
checkType([(initialValue, int), (delay, (float, int))])
self.__func = func
self.__delay = delay
self.__value = initialValue
super().__init__(**kwargs)
self._output[0] = initialValue
self._scopeDump.add(f"{self.getBlockID()} output", 0, self._output[0])
def __runFunc(self):
"""
Runs the block for the specified input and timeouts for the delay.
"""
self.__value = self.getInputVal()
self.__value = self.__func(self.__value)
yield self._env.timeout(self.__delay)
self._output[0] = self.__value
self._scopeDump.add(f"{self.getBlockID()} output", self._env.now, self._output[0])
self.processFanOut()
def __str__(self):
"""
@return str : a string representation of the block.
"""
return f"Combinational ID {self.getBlockID()}"
def run(self):
"""
Runs this block.
"""
self._env.process(self.__runFunc())
def isConnected(self):
"""
@return bool : True if this block is connected to everything, False otherwise.
"""
return self.isConnectedToInput()