main.py 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. # -*- coding: utf-8 -*-
  2. #
  3. # AWL simulator - Debug hardware interface
  4. #
  5. # Copyright 2013-2017 Michael Buesch <m@bues.ch>
  6. #
  7. # This program is free software; you can redistribute it and/or modify
  8. # it under the terms of the GNU General Public License as published by
  9. # the Free Software Foundation; either version 2 of the License, or
  10. # (at your option) any later version.
  11. #
  12. # This program is distributed in the hope that it will be useful,
  13. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. # GNU General Public License for more details.
  16. #
  17. # You should have received a copy of the GNU General Public License along
  18. # with this program; if not, write to the Free Software Foundation, Inc.,
  19. # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  20. #
  21. from __future__ import division, absolute_import, print_function, unicode_literals
  22. #from awlsim.common.cython_support cimport * #@cy
  23. from awlsim.common.compat import *
  24. from awlsim.common.util import *
  25. from awlsim.common.exceptions import *
  26. #from awlsimhw_debug.main cimport * #@cy
  27. from awlsim.core.hardware_params import *
  28. from awlsim.core.hardware import * #+cimport
  29. from awlsim.core.operatortypes import * #+cimport
  30. from awlsim.core.operators import * #+cimport
  31. from awlsim.core.offset import * #+cimport
  32. from awlsim.core.cpu import * #+cimport
  33. class HardwareInterface_Debug(AbstractHardwareInterface): #+cdef #@nocov
  34. name = "debug"
  35. description = "Debugging hardware module.\n"\
  36. "This can be used to inject faults into the system."
  37. paramDescs = [
  38. HwParamDesc_bool("dummyParam",
  39. description = "Unused dummy parameter"),
  40. HwParamDesc_int("startupErrorRate",
  41. defaultValue = 0,
  42. minValue = 0,
  43. description = "Error rate in startup (0 = none)"),
  44. HwParamDesc_int("shutdownErrorRate",
  45. defaultValue = 0,
  46. minValue = 0,
  47. description = "Error rate in shutdown (0 = none)"),
  48. HwParamDesc_int("inputErrorRate",
  49. defaultValue = 0,
  50. minValue = 0,
  51. description = "Error rate in input read (0 = none)"),
  52. HwParamDesc_int("outputErrorRate",
  53. defaultValue = 0,
  54. minValue = 0,
  55. description = "Error rate in output write (0 = none)"),
  56. HwParamDesc_int("directReadErrorRate",
  57. defaultValue = 0,
  58. minValue = 0,
  59. description = "Error rate in direct input read (0 = none)"),
  60. HwParamDesc_int("directWriteErrorRate",
  61. defaultValue = 0,
  62. minValue = 0,
  63. description = "Error rate in direct output write (0 = none)"),
  64. ]
  65. def __init__(self, sim, parameters={}):
  66. AbstractHardwareInterface.__init__(self,
  67. sim = sim,
  68. parameters = parameters)
  69. self.__startupErrorRate = self.getParamValueByName("startupErrorRate")
  70. self.__startupErrorCount = 0
  71. self.__shutdownErrorRate = self.getParamValueByName("shutdownErrorRate")
  72. self.__shutdownErrorCount = 0
  73. self.__inputErrorRate = self.getParamValueByName("inputErrorRate")
  74. self.__inputErrorCount = 0
  75. self.__outputErrorRate = self.getParamValueByName("outputErrorRate")
  76. self.__outputErrorCount = 0
  77. self.__directReadErrorRate = self.getParamValueByName("directReadErrorRate")
  78. self.__directReadErrorCount = 0
  79. self.__directWriteErrorRate = self.getParamValueByName("directWriteErrorRate")
  80. self.__directWriteErrorCount = 0
  81. def doStartup(self):
  82. if self.__startupErrorRate:
  83. self.__startupErrorCount += 1
  84. if self.__startupErrorCount % self.__startupErrorRate == 0:
  85. self.raiseException("Synthetic startup error")
  86. def doShutdown(self):
  87. if self.__shutdownErrorRate:
  88. self.__shutdownErrorCount += 1
  89. if self.__shutdownErrorCount % self.__shutdownErrorRate == 0:
  90. self.raiseException("Synthetic shutdown error")
  91. def readInputs(self): #+cdef
  92. if self.__inputErrorRate:
  93. self.__inputErrorCount += 1
  94. if self.__inputErrorCount % self.__inputErrorRate == 0:
  95. self.raiseException("Synthetic input error")
  96. # Get the first input dword and write it back.
  97. dword = AwlMemoryObject_asScalar(
  98. self.sim.cpu.fetch(make_AwlOperator(AwlOperatorTypes.MEM_E,
  99. 32,
  100. make_AwlOffset(self.inputAddressBase, 0),
  101. None),
  102. AwlOperatorWidths.WIDTH_MASK_32))
  103. dwordBytes = bytearray( ( ((dword >> 24) & 0xFF),
  104. ((dword >> 16) & 0xFF),
  105. ((dword >> 8) & 0xFF),
  106. (dword & 0xFF) ) )
  107. self.sim.cpu.storeInputRange(self.inputAddressBase,
  108. dwordBytes)
  109. def writeOutputs(self): #+cdef
  110. if self.__outputErrorRate:
  111. self.__outputErrorCount += 1
  112. if self.__outputErrorCount % self.__outputErrorRate == 0:
  113. self.raiseException("Synthetic output error")
  114. # Fetch a data range, but don't do anything with it.
  115. outData = self.sim.cpu.fetchOutputRange(self.outputAddressBase, 2)
  116. assert(outData)
  117. def directReadInput(self, accessWidth, accessOffset): #@nocy
  118. #@cy cdef bytearray directReadInput(self, uint32_t accessWidth, uint32_t accessOffset):
  119. if accessOffset < self.inputAddressBase:
  120. return bytearray()
  121. if self.__directReadErrorRate:
  122. self.__directReadErrorCount += 1
  123. if self.__directReadErrorCount % self.__directReadErrorRate == 0:
  124. self.raiseException("Synthetic directRead error")
  125. # Just read the current value from the CPU and return it.
  126. try:
  127. return self.sim.cpu.fetchInputRange(accessOffset, accessWidth // 8)
  128. except AwlSimError as e:
  129. # We may be out of process image range. Just return 0.
  130. return bytearray( (0,) * (accessWidth // 8) )
  131. def directWriteOutput(self, accessWidth, accessOffset, data): #@nocy
  132. #@cy cdef ExBool_t directWriteOutput(self, uint32_t accessWidth, uint32_t accessOffset, bytearray data) except ExBool_val:
  133. if accessOffset < self.outputAddressBase:
  134. return False
  135. if self.__directWriteErrorRate:
  136. self.__directWriteErrorCount += 1
  137. if self.__directWriteErrorCount % self.__directWriteErrorRate == 0:
  138. self.raiseException("Synthetic directWrite error")
  139. # Just pretend we wrote it somewhere.
  140. return True
  141. # Module entry point
  142. HardwareInterface = HardwareInterface_Debug