aboutsummaryrefslogtreecommitdiffstats
path: root/awlsimhw_debug/main.py
blob: ab94e538d15ffa6cdcd41c7ec6727575cce96524 (plain)
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
# -*- coding: utf-8 -*-
#
# AWL simulator - Debug hardware interface
#
# Copyright 2013-2017 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.util import *
from awlsim.common.exceptions import *

#from awlsimhw_debug.main cimport * #@cy

from awlsim.core.hardware_params import *
from awlsim.core.hardware import * #+cimport
from awlsim.core.operatortypes import * #+cimport
from awlsim.core.operators import * #+cimport
from awlsim.core.offset import * #+cimport
from awlsim.core.cpu import * #+cimport


class HardwareInterface_Debug(AbstractHardwareInterface): #+cdef #@nocov
	name		= "debug"
	description	= "Debugging hardware module.\n"\
			  "This can be used to inject faults into the system."

	paramDescs = [
		HwParamDesc_bool("dummyParam",
				 description = "Unused dummy parameter"),
		HwParamDesc_int("startupErrorRate",
				defaultValue = 0,
				minValue = 0,
				description = "Error rate in startup (0 = none)"),
		HwParamDesc_int("shutdownErrorRate",
				defaultValue = 0,
				minValue = 0,
				description = "Error rate in shutdown (0 = none)"),
		HwParamDesc_int("inputErrorRate",
				defaultValue = 0,
				minValue = 0,
				description = "Error rate in input read (0 = none)"),
		HwParamDesc_int("outputErrorRate",
				defaultValue = 0,
				minValue = 0,
				description = "Error rate in output write (0 = none)"),
		HwParamDesc_int("directReadErrorRate",
				defaultValue = 0,
				minValue = 0,
				description = "Error rate in direct input read (0 = none)"),
		HwParamDesc_int("directWriteErrorRate",
				defaultValue = 0,
				minValue = 0,
				description = "Error rate in direct output write (0 = none)"),
	]

	def __init__(self, sim, parameters={}):
		AbstractHardwareInterface.__init__(self,
						   sim = sim,
						   parameters = parameters)
		self.__startupErrorRate = self.getParamValueByName("startupErrorRate")
		self.__startupErrorCount = 0
		self.__shutdownErrorRate = self.getParamValueByName("shutdownErrorRate")
		self.__shutdownErrorCount = 0
		self.__inputErrorRate = self.getParamValueByName("inputErrorRate")
		self.__inputErrorCount = 0
		self.__outputErrorRate = self.getParamValueByName("outputErrorRate")
		self.__outputErrorCount = 0
		self.__directReadErrorRate = self.getParamValueByName("directReadErrorRate")
		self.__directReadErrorCount = 0
		self.__directWriteErrorRate = self.getParamValueByName("directWriteErrorRate")
		self.__directWriteErrorCount = 0

	def doStartup(self):
		if self.__startupErrorRate:
			self.__startupErrorCount += 1
			if self.__startupErrorCount % self.__startupErrorRate == 0:
				self.raiseException("Synthetic startup error")

	def doShutdown(self):
		if self.__shutdownErrorRate:
			self.__shutdownErrorCount += 1
			if self.__shutdownErrorCount % self.__shutdownErrorRate == 0:
				self.raiseException("Synthetic shutdown error")

	def readInputs(self): #+cdef
		if self.__inputErrorRate:
			self.__inputErrorCount += 1
			if self.__inputErrorCount % self.__inputErrorRate == 0:
				self.raiseException("Synthetic input error")

		# Get the first input dword and write it back.
		dword = AwlMemoryObject_asScalar(
			self.sim.cpu.fetch(make_AwlOperator(AwlOperatorTypes.MEM_E,
							    32,
							    make_AwlOffset(self.inputAddressBase, 0),
							    None),
					   AwlOperatorWidths.WIDTH_MASK_32))
		dwordBytes = bytearray( ( ((dword >> 24) & 0xFF),
					  ((dword >> 16) & 0xFF),
					  ((dword >> 8) & 0xFF),
					  (dword & 0xFF) ) )
		self.sim.cpu.storeInputRange(self.inputAddressBase,
					     dwordBytes)

	def writeOutputs(self): #+cdef
		if self.__outputErrorRate:
			self.__outputErrorCount += 1
			if self.__outputErrorCount % self.__outputErrorRate == 0:
				self.raiseException("Synthetic output error")

		# Fetch a data range, but don't do anything with it.
		outData = self.sim.cpu.fetchOutputRange(self.outputAddressBase, 2)
		assert(outData)

	def directReadInput(self, accessWidth, accessOffset): #@nocy
#@cy	cdef bytearray directReadInput(self, uint32_t accessWidth, uint32_t accessOffset):
		if accessOffset < self.inputAddressBase:
			return bytearray()

		if self.__directReadErrorRate:
			self.__directReadErrorCount += 1
			if self.__directReadErrorCount % self.__directReadErrorRate == 0:
				self.raiseException("Synthetic directRead error")

		# Just read the current value from the CPU and return it.
		try:
			return self.sim.cpu.fetchInputRange(accessOffset, accessWidth // 8)
		except AwlSimError as e:
			# We may be out of process image range. Just return 0.
			return bytearray( (0,) * (accessWidth // 8) )

	def directWriteOutput(self, accessWidth, accessOffset, data): #@nocy
#@cy	cdef ExBool_t directWriteOutput(self, uint32_t accessWidth, uint32_t accessOffset, bytearray data) except ExBool_val:
		if accessOffset < self.outputAddressBase:
			return False

		if self.__directWriteErrorRate:
			self.__directWriteErrorCount += 1
			if self.__directWriteErrorCount % self.__directWriteErrorRate == 0:
				self.raiseException("Synthetic directWrite error")

		# Just pretend we wrote it somewhere.
		return True

# Module entry point
HardwareInterface = HardwareInterface_Debug
bues.ch cgit interface