summaryrefslogtreecommitdiffstats
path: root/awlsim/core/statusword.py
blob: 052bcab3e6702739aca0d46765f15cd15ba9187e (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
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
# -*- coding: utf-8 -*-
#
# AWL simulator - status word
#
# Copyright 2012-2016 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.compat import *

#from awlsim.core.dynattrs cimport * #@cy
#from awlsim.core.statusword cimport * #@cy

from awlsim.common.cpuspecs import *
from awlsim.common.datatypehelpers import *

from awlsim.core.dynattrs import * #@nocy
from awlsim.core.util import *


class S7StatusWord(DynAttrs): #+cdef
	"""STEP 7 status word
	The instance of this class holds the following nine
	attributes. One for each STW bit:
	NER, VKE, STA, OR, OS, OV, A0, A1, BIE
	"""

	name2nr_german = {
		"/ER"	: 0,
		"VKE"	: 1,
		"STA"	: 2,
		"OR"	: 3,
		"OS"	: 4,
		"OV"	: 5,
		"A0"	: 6,
		"A1"	: 7,
		"BIE"	: 8,
	}
	nr2name_german = pivotDict(name2nr_german)

	__english2german = {
		"/FC"	: "/ER",
		"RLO"	: "VKE",
		"STA"	: "STA",
		"OR"	: "OR",
		"OS"	: "OS",
		"OV"	: "OV",
		"CC1"	: "A1",
		"CC0"	: "A0",
		"BR"	: "BIE",
	}
	__german2english = pivotDict(__english2german)

	NR_BITS = 9

	# Dynamic attributes
	dynAttrs = {
		"NER"	: 0,
		"VKE"	: 0,
		"STA"	: 0,
		"OR"	: 0,
		"OS"	: 0,
		"OV"	: 0,
		"A0"	: 0,
		"A1"	: 0,
		"BIE"	: 0,
	}

	def __eq__(self, other): #@nocy
#@cy	cdef __eq(self, object other):
		return (self is other) or (\
			isinstance(other, S7StatusWord) and\
			self.getWord() == other.getWord()\
		)

	def __ne__(self, other):		#@nocy
		return not self.__eq__(other)	#@nocy

#@cy	def __richcmp__(self, object other, int op):
#@cy		if op == 2: # __eq__
#@cy			return self.__eq(other)
#@cy		elif op == 3: # __ne__
#@cy			return not self.__eq(other)
#@cy		return False

	@classmethod
	def getBitnrByName(cls, name, mnemonics):
		assert(mnemonics != S7CPUSpecs.MNEMONICS_AUTO)
		try:
			if mnemonics == S7CPUSpecs.MNEMONICS_EN:
				name = cls.__english2german[name]
			return cls.name2nr_german[name]
		except KeyError as e:
			raise AwlSimError("Invalid status word bit "
				"name: " + str(name))

	def __getNER(self):
		return self.NER

	def __getVKE(self):
		return self.VKE

	def __getSTA(self):
		return self.STA

	def __getOR(self):
		return self.OR

	def __getOS(self):
		return self.OS

	def __getOV(self):
		return self.OV

	def __getA0(self):
		return self.A0

	def __getA1(self):
		return self.A1

	def __getBIE(self):
		return self.BIE

	__bitnr2getter = (
		__getNER,
		__getVKE,
		__getSTA,
		__getOR,
		__getOS,
		__getOV,
		__getA0,
		__getA1,
		__getBIE,
	)

	def getByBitNumber(self, bitNumber):
		try:
			return self.__bitnr2getter[bitNumber](self)
		except IndexError as e:
			raise AwlSimError("Status word bit fetch '%d' "
				"out of range" % bitNumber)

	def reset(self): #@nocy
#@cy	cpdef reset(self):
		self.NER = self.VKE = self.STA =\
		self.OR = self.OS = self.OV =\
		self.A0 = self.A1 = self.BIE = 0

	def getWord(self): #@nocy
#@cy	cpdef uint16_t getWord(self):
		return self.NER | (self.VKE << 1) | (self.STA << 2) |\
		       (self.OR << 3) | (self.OS << 4) | (self.OV << 5) |\
		       (self.A0 << 6) | (self.A1 << 7) | (self.BIE << 8)

	def setWord(self, word): #@nocy
#@cy	cpdef setWord(self, uint16_t word):
		self.NER = word & 1
		self.VKE = (word >> 1) & 1
		self.STA = (word >> 2) & 1
		self.OR = (word >> 3) & 1
		self.OS = (word >> 4) & 1
		self.OV = (word >> 5) & 1
		self.A0 = (word >> 6) & 1
		self.A1 = (word >> 7) & 1
		self.BIE = (word >> 8) & 1

	def dup(self): #@nocy
#@cy	cpdef dup(self):
		new = S7StatusWord()
		new.NER = self.NER
		new.VKE = self.VKE
		new.STA = self.STA
		new.OR = self.OR
		new.OS = self.OS
		new.OV = self.OV
		new.A0 = self.A0
		new.A1 = self.A1
		new.BIE = self.BIE
		return new

	def setForFloatingPoint(self, pyFloat): #@nocy
#@cy	cpdef setForFloatingPoint(self, pyFloat):
		dword = pyFloatToDWord(pyFloat)
		dwordNoSign = dword & 0x7FFFFFFF
		if isDenormalPyFloat(pyFloat) or\
		   (dwordNoSign < 0x00800000 and dwordNoSign != 0):
			# denorm
			self.A1, self.A0, self.OV, self.OS = 0, 0, 1, 1
		elif dwordNoSign == 0:
			# zero
			self.A1, self.A0, self.OV = 0, 0, 0
		elif dwordNoSign >= 0x7F800000:
			if dwordNoSign == 0x7F800000:
				# inf
				if dword & 0x80000000:
					self.A1, self.A0, self.OV, self.OS = 0, 1, 1, 1
				else:
					self.A1, self.A0, self.OV, self.OS = 1, 0, 1, 1
			else:
				# nan
				self.A1, self.A0, self.OV, self.OS = 1, 1, 1, 1
		elif dword & 0x80000000:
			# norm neg
			self.A1, self.A0, self.OV = 0, 1, 0
		else:
			# norm pos
			self.A1, self.A0, self.OV = 1, 0, 0

	def getString(self, mnemonics):
		if mnemonics == S7CPUSpecs.MNEMONICS_AUTO:
			mnemonics = S7CPUSpecs.MNEMONICS_EN
		ret = []
		for i in range(self.NR_BITS - 1, -1, -1):
			name = self.nr2name_german[i]
			if mnemonics == S7CPUSpecs.MNEMONICS_EN:
				name = self.__german2english[name]
			ret.append("%s:%d" % (name, self.getByBitNumber(i)))
		return '  '.join(ret)

	def __repr__(self):
		return self.getString(S7CPUSpecs.MNEMONICS_DE)
bues.ch cgit interface