main.py 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. # -*- coding: utf-8 -*-
  2. #
  3. # AWL simulator - Debug hardware interface
  4. #
  5. # Copyright 2013-2016 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.compat import *
  23. from awlsim.core.hardware import *
  24. from awlsim.core.operators import AwlOperator
  25. from awlsim.core.datatypes import AwlOffset
  26. class HardwareInterface(AbstractHardwareInterface):
  27. name = "debug"
  28. paramDescs = [
  29. HwParamDesc_bool("dummyParam",
  30. description = "Unused dummy parameter"),
  31. HwParamDesc_int("startupErrorRate",
  32. defaultValue = 0,
  33. minValue = 0,
  34. description = "Error rate in startup (0 = none)"),
  35. HwParamDesc_int("shutdownErrorRate",
  36. defaultValue = 0,
  37. minValue = 0,
  38. description = "Error rate in shutdown (0 = none)"),
  39. HwParamDesc_int("inputErrorRate",
  40. defaultValue = 0,
  41. minValue = 0,
  42. description = "Error rate in input read (0 = none)"),
  43. HwParamDesc_int("outputErrorRate",
  44. defaultValue = 0,
  45. minValue = 0,
  46. description = "Error rate in output write (0 = none)"),
  47. HwParamDesc_int("directReadErrorRate",
  48. defaultValue = 0,
  49. minValue = 0,
  50. description = "Error rate in direct input read (0 = none)"),
  51. HwParamDesc_int("directWriteErrorRate",
  52. defaultValue = 0,
  53. minValue = 0,
  54. description = "Error rate in direct output write (0 = none)"),
  55. ]
  56. def __init__(self, sim, parameters={}):
  57. AbstractHardwareInterface.__init__(self,
  58. sim = sim,
  59. parameters = parameters)
  60. self.__startupErrorRate = self.getParamValueByName("startupErrorRate")
  61. self.__startupErrorCount = 0
  62. self.__shutdownErrorRate = self.getParamValueByName("shutdownErrorRate")
  63. self.__shutdownErrorCount = 0
  64. self.__inputErrorRate = self.getParamValueByName("inputErrorRate")
  65. self.__inputErrorCount = 0
  66. self.__outputErrorRate = self.getParamValueByName("outputErrorRate")
  67. self.__outputErrorCount = 0
  68. self.__directReadErrorRate = self.getParamValueByName("directReadErrorRate")
  69. self.__directReadErrorCount = 0
  70. self.__directWriteErrorRate = self.getParamValueByName("directWriteErrorRate")
  71. self.__directWriteErrorCount = 0
  72. def doStartup(self):
  73. if self.__startupErrorRate:
  74. self.__startupErrorCount += 1
  75. if self.__startupErrorCount % self.__startupErrorRate == 0:
  76. self.raiseException("Synthetic startup error")
  77. def doShutdown(self):
  78. if self.__shutdownErrorRate:
  79. self.__shutdownErrorCount += 1
  80. if self.__shutdownErrorCount % self.__shutdownErrorRate == 0:
  81. self.raiseException("Synthetic shutdown error")
  82. def readInputs(self):
  83. if self.__inputErrorRate:
  84. self.__inputErrorCount += 1
  85. if self.__inputErrorCount % self.__inputErrorRate == 0:
  86. self.raiseException("Synthetic input error")
  87. # Get the first input dword and write it back.
  88. dword = self.sim.cpu.fetch(AwlOperator(AwlOperator.MEM_E,
  89. 32,
  90. AwlOffset(self.inputAddressBase)))
  91. dwordBytes = bytearray( ( ((dword >> 24) & 0xFF),
  92. ((dword >> 16) & 0xFF),
  93. ((dword >> 8) & 0xFF),
  94. (dword & 0xFF) ) )
  95. self.sim.cpu.storeInputRange(self.inputAddressBase,
  96. dwordBytes)
  97. def writeOutputs(self):
  98. if self.__outputErrorRate:
  99. self.__outputErrorCount += 1
  100. if self.__outputErrorCount % self.__outputErrorRate == 0:
  101. self.raiseException("Synthetic output error")
  102. # Fetch a data range, but don't do anything with it.
  103. outData = self.sim.cpu.fetchOutputRange(self.outputAddressBase,
  104. 512)
  105. assert(outData)
  106. def directReadInput(self, accessWidth, accessOffset):
  107. if accessOffset < self.inputAddressBase:
  108. return None
  109. if self.__directReadErrorRate:
  110. self.__directReadErrorCount += 1
  111. if self.__directReadErrorCount % self.__directReadErrorRate == 0:
  112. self.raiseException("Synthetic directRead error")
  113. # Just read the current value from the CPU and return it.
  114. return self.sim.cpu.fetch(AwlOperator(AwlOperator.MEM_E,
  115. accessWidth,
  116. AwlOffset(accessOffset)))
  117. def directWriteOutput(self, accessWidth, accessOffset, data):
  118. if accessOffset < self.outputAddressBase:
  119. return False
  120. if self.__directWriteErrorRate:
  121. self.__directWriteErrorCount += 1
  122. if self.__directWriteErrorCount % self.__directWriteErrorRate == 0:
  123. self.raiseException("Synthetic directWrite error")
  124. # Just pretend we wrote it somewhere.
  125. return True