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
|
# -*- coding: utf-8 -*-
#
# AWL simulator
#
# Copyright 2012-2019 Michael Buesch <m@bues.ch>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
#
from __future__ import division, absolute_import, print_function, unicode_literals
#from awlsim.common.cython_support cimport * #@cy
from awlsim.common.compat import *
from awlsim.common.project import *
from awlsim.common.env import *
from awlsim.common.exceptions import *
from awlsim.common.profiler import *
from awlsim.common.util import *
from awlsim.core.cpu import * #+cimport
from awlsim.core.hardware import * #+cimport
from awlsim.core.hardware_loader import *
import sys
#cimport cython #@cy
__all__ = [
"AwlSim",
]
def AwlSim_decorator_profiled(profileLevel):
"""Profiled call decorator.
"""
def profiled_decorator(func):
@functools.wraps(func) #@nocy
def profiled_wrapper(self, *args, **kwargs):
if self._profileLevel >= profileLevel:
self._profileStart() #@nocov
try:
func(self, *args, **kwargs)
finally:
if self._profileLevel >= profileLevel:
self._profileStop() #@nocov
return profiled_wrapper
return profiled_decorator
def AwlSim_decorator_throwsAwlSimError(func):
"""Handler decorator for AwlSimError exceptions.
"""
@functools.wraps(func) #@nocy
def awlSimErrorExtension_wrapper(self, *args, **kwargs):
try:
func(self, *args, **kwargs)
except AwlSimError as e:
self._handleSimException(e)
return awlSimErrorExtension_wrapper
class AwlSim(object): #+cdef
"""Main awlsim core object.
"""
profiled = AwlSim_decorator_profiled
throwsAwlSimError = AwlSim_decorator_throwsAwlSimError
def __init__(self):
self.__registeredHardware = []
self.__registeredHardwareCount = 0
self.__hwStartupRequired = True
self._fatalHwErrors = True
self.cpu = S7CPU()
self.cpu.setPeripheralReadCallback(self.__peripheralReadCallback)
self.cpu.setPeripheralWriteCallback(self.__peripheralWriteCallback)
self.__setProfiler(AwlSimEnv.getProfileLevel())
def getCPU(self):
return self.cpu
def __setProfiler(self, profileLevel): #@nocov
self._profileLevel = profileLevel
if self._profileLevel <= 0:
return
self.__profiler = Profiler()
def _profileStart(self): #@nocov
self.__profiler.start()
def _profileStop(self): #@nocov
self.__profiler.stop()
def getProfileStats(self): #@nocov
if self._profileLevel <= 0:
return None
return self.__profiler.getResult()
def _handleSimException(self, e, fatal = True):
"""Handle an exception and add some information
to it. Note that this might get called twice or more often
for the same exception object.
"""
if not e.getCpu():
# The CPU reference is not set, yet.
# Set it to the current CPU.
e.setCpu(self.cpu)
if fatal:
# Re-raise the exception for upper layers to catch.
raise e
else:
# Non-fatal. Just log an error.
printError(str(e)) #@nocov
@throwsAwlSimError
def __handleMaintenanceRequest(self, e):
if e.requestType in (MaintenanceRequest.TYPE_SHUTDOWN,
MaintenanceRequest.TYPE_STOP,
MaintenanceRequest.TYPE_RTTIMEOUT):
# This is handled in the toplevel loop, so
# re-raise the exception.
raise e
try:
if e.requestType == MaintenanceRequest.TYPE_SOFTREBOOT:
# Run the CPU startup sequence again
self.cpu.startup()
else:
assert(0)
except MaintenanceRequest as e: #@nocov
raise AwlSimError("Recursive maintenance request")
@profiled(2)
@throwsAwlSimError
def reset(self):
self.cpu.reset()
self.unregisterAllHardware()
@profiled(2)
@throwsAwlSimError
def build(self):
self.cpu.build()
@profiled(2)
@throwsAwlSimError
def load(self, parseTree, rebuild = False, sourceManager = None):
self.cpu.load(parseTree, rebuild, sourceManager)
@profiled(2)
@throwsAwlSimError
def loadSymbolTable(self, symTab, rebuild = False):
self.cpu.loadSymbolTable(symTab, rebuild)
@profiled(2)
@throwsAwlSimError
def loadLibraryBlock(self, libSelection, rebuild = False):
self.cpu.loadLibraryBlock(libSelection, rebuild)
@profiled(2)
@throwsAwlSimError
def removeBlock(self, blockInfo, sanityChecks = True):
self.cpu.removeBlock(blockInfo, sanityChecks)
@profiled(2)
@throwsAwlSimError
def staticSanityChecks(self):
self.cpu.staticSanityChecks()
@profiled(2)
@throwsAwlSimError
def startup(self):
# Startup the hardware modules, if required.
if self.__hwStartupRequired:
self.hardwareStartup()
# Next time we need a startup again.
self.__hwStartupRequired = True
# Startup the CPU core.
try:
self.__readHwInputs()
self.cpu.startup()
self.__writeHwOutputs()
except MaintenanceRequest as e:
self.__handleMaintenanceRequest(e)
def runCycle(self): #+cpdef
if self._profileLevel >= 1:
self._profileStart() #@nocov
try:
if self.__registeredHardwareCount:
self.__readHwInputs()
self.cpu.runCycle()
if self.__registeredHardwareCount:
self.__writeHwOutputs()
self.cpu.sleepCyclePadding()
except AwlSimError as e:
self._handleSimException(e)
except MaintenanceRequest as e:
self.__handleMaintenanceRequest(e)
if self._profileLevel >= 1:
self._profileStop() #@nocov
@throwsAwlSimError
def shutdown(self):
"""Shutdown the Awlsim core.
This will unregister all hardware modules and shut down execution.
"""
self.unregisterAllHardware()
ps = self.getProfileStats()
if ps: #@nocov
sys.stdout.write("\n\nAwlsim core profile stats "
"(level %d) follow:\n" %\
self._profileLevel)
sys.stdout.write(ps)
sys.stdout.write("\n")
sys.stdout.flush()
def unregisterAllHardware(self):
for hw in self.__registeredHardware:
hw.shutdown()
self.__registeredHardware = []
self.__registeredHardwareCount = 0
def registerHardware(self, hwClassInst):
"""Register a new hardware interface."""
if hwClassInst.getParamValueByName("enabled"):
self.__registeredHardware.append(hwClassInst)
self.__registeredHardwareCount = len(self.__registeredHardware)
self.__hwStartupRequired = True
def registerHardwareClass(self, hwClass, parameters={}):
"""Register a new hardware interface class.
'parameters' is a dict of hardware specific parameters.
Returns the instance of the hardware class."""
hwClassInst = hwClass(sim = self,
parameters = parameters)
self.registerHardware(hwClassInst)
return hwClassInst
@classmethod
def loadHardwareModule(cls, name):
"""Load a hardware interface module.
'name' is the name of the module to load (without 'awlsimhw_' prefix).
Returns the HardwareInterface class."""
return HwModLoader.loadModule(name).getInterface()
@profiled(2)
@throwsAwlSimError
def hardwareStartup(self):
"""Startup all attached hardware modules.
"""
# Hw errors are fatal if debugging.
# Otherwise just log the errors, but don't abort the program.
self._fatalHwErrors = bool(Logging.loglevel >= Logging.LOG_DEBUG)
for hw in self.__registeredHardware:
try:
hw.startup()
except AwlSimError as e:
# Always fatal in startup.
self._handleSimException(e, fatal=True)
self.__hwStartupRequired = False
#@cy @cython.boundscheck(False)
def __readHwInputs(self): #+cdef
"""Read all hardware module inputs.
"""
#@cy cdef AbstractHardwareInterface hw
#@cy cdef uint32_t i
# Note: Bounds checking of the indexing operator [] is disabled
# by @cython.boundscheck(False) in this method.
for i in range(self.__registeredHardwareCount):
try:
hw = self.__registeredHardware[i]
hw.readInputs()
except AwlSimError as e:
self._handleSimException(e,
fatal = self._fatalHwErrors)
#@cy @cython.boundscheck(False)
def __writeHwOutputs(self): #+cdef
"""Write all hardware module outputs.
"""
#@cy cdef AbstractHardwareInterface hw
#@cy cdef uint32_t i
# Note: Bounds checking of the indexing operator [] is disabled
# by @cython.boundscheck(False) in this method.
for i in range(self.__registeredHardwareCount):
try:
hw = self.__registeredHardware[i]
hw.writeOutputs()
except AwlSimError as e:
self._handleSimException(e,
fatal = self._fatalHwErrors)
def __peripheralReadCallback(self, userData, width, offset):
"""The CPU issued a direct peripheral read access.
Poke all registered hardware modules, but only return the value
from the last module returning a valid value.
"""
#@cy cdef AbstractHardwareInterface hw
#@cy cdef bytearray retValue
#@cy cdef bytearray value
for hw in self.__registeredHardware:
try:
value = hw.directReadInput(width, offset)
if value:
return value
except AwlSimError as e:
self._handleSimException(e,
fatal = self._fatalHwErrors)
break
return bytearray()
def __peripheralWriteCallback(self, userData, width, offset, value):
"""The CPU issued a direct peripheral write access.
Send the write request down to all hardware modules.
Returns true, if any hardware accepted the value.
"""
#@cy cdef AbstractHardwareInterface hw
#@cy cdef _Bool retOk
#@cy cdef ExBool_t ok
retOk = False
try:
for hw in self.__registeredHardware:
ok = hw.directWriteOutput(width, offset, value)
retOk = ok or retOk
except AwlSimError as e:
self._handleSimException(e,
fatal = self._fatalHwErrors)
return False
return retOk
def __repr__(self): #@nocov
return str(self.cpu)
|