aboutsummaryrefslogtreecommitdiffstats
path: root/awlsim/gui/fup/fup_elemarith.py
blob: c2a6ff23d1b0810b7abc730073d773f48fbc3a94 (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
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
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
# -*- coding: utf-8 -*-
#
# AWL simulator - FUP - Arithmetic element classes
#
# Copyright 2017-2018 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.xmlfactory import *

from awlsim.gui.fup.fup_base import *
from awlsim.gui.fup.fup_elem import *
from awlsim.gui.fup.fup_elemoperand import *


class FupElem_ARITH_factory(FupElem_factory):
	def parser_open(self, tag):
		assert(tag)
		x = tag.getAttrInt("x")
		y = tag.getAttrInt("y")
		subType = tag.getAttr("subtype")
		uuid = tag.getAttr("uuid", None)
		enabled = tag.getAttrBool("enabled", True)
		elemClass = {
			FupElem_ARITH_ADD_I.OP_SYM_NAME	: FupElem_ARITH_ADD_I,
			FupElem_ARITH_SUB_I.OP_SYM_NAME	: FupElem_ARITH_SUB_I,
			FupElem_ARITH_MUL_I.OP_SYM_NAME	: FupElem_ARITH_MUL_I,
			FupElem_ARITH_DIV_I.OP_SYM_NAME	: FupElem_ARITH_DIV_I,
			FupElem_ARITH_ADD_D.OP_SYM_NAME	: FupElem_ARITH_ADD_D,
			FupElem_ARITH_SUB_D.OP_SYM_NAME	: FupElem_ARITH_SUB_D,
			FupElem_ARITH_MUL_D.OP_SYM_NAME	: FupElem_ARITH_MUL_D,
			FupElem_ARITH_DIV_D.OP_SYM_NAME	: FupElem_ARITH_DIV_D,
			FupElem_ARITH_MOD_D.OP_SYM_NAME	: FupElem_ARITH_MOD_D,
			FupElem_ARITH_ADD_R.OP_SYM_NAME	: FupElem_ARITH_ADD_R,
			FupElem_ARITH_SUB_R.OP_SYM_NAME	: FupElem_ARITH_SUB_R,
			FupElem_ARITH_MUL_R.OP_SYM_NAME	: FupElem_ARITH_MUL_R,
			FupElem_ARITH_DIV_R.OP_SYM_NAME	: FupElem_ARITH_DIV_R,
		}.get(subType)
		if not elemClass:
			raise self.Error("Arithmetic subtype '%s' is not known "
				"to the element parser." % (
				subType))
		self.elem = elemClass(x=x, y=y, nrInputs=0, uuid=uuid, enabled=enabled)
		self.elem.grid = self.grid
		XmlFactory.parser_open(self, tag)

	def parser_beginTag(self, tag):
		if tag.name == "connections":
			self.parser_switchTo(FupConn.factory(elem=self.elem))
			return
		XmlFactory.parser_beginTag(self, tag)

	def parser_endTag(self, tag):
		if tag.name == "element":
			# Insert the element into the grid.
			if not self.grid.placeElem(self.elem):
				raise self.Error("<element> caused "
					"a grid collision.")
			self.parser_finish()
			return
		XmlFactory.parser_endTag(self, tag)

	def composer_getTags(self):
		elem = self.elem

		connTags = []
		for inp in elem.inputs:
			connTags.extend(inp.factory(conn=inp).composer_getTags())
		for out in elem.outputs:
			connTags.extend(out.factory(conn=out).composer_getTags())

		return [
			self.Tag(name="element",
				attrs={
					"type" : "arithmetic",
					"subtype" : elem.OP_SYM_NAME,
					"x" : str(elem.x),
					"y" : str(elem.y),
					"uuid" : str(elem.uuid),
					"enabled" : "0" if not elem.enabled else "",
				},
				tags=[
					self.Tag(name="connections",
						 tags=connTags),
				])
		]

class FupElem_ARITH(FupElem):
	"""Arithmetic FUP/FBD element base class"""

	factory			= FupElem_ARITH_factory

	FIXED_INPUTS		= [ "EN", ]
	FIXED_OUTPUTS		= [ "ENO", ]
	OPTIONAL_CONNS		= { "EN", "REM", "==0", "<>0", ">0", "<0",
				    ">=0", "<=0", "OV", "UO", "ENO", }
	BLANK_CONNS		= { "IN", "OUT", }
	HAVE_REMAINDER		= False

	# Sequence of special connections.
	__CONN_IN_SEQUENCE	= ( "EN", )
	__CONN_OUT_SEQUENCE	= ( "REM", "==0", "<>0", ">0", "<0",
				    ">=0", "<=0", "OV", "UO", "ENO", )

	def __init__(self, x, y, nrInputs=2, nrOutputs=1, **kwargs):
		FupElem.__init__(self, x, y, **kwargs)

		self.inputs = [ FupConnIn(self, text=text)
				for text in self.FIXED_INPUTS ]
		self.inputs.extend( FupConnIn(self, text=("IN%d" % i))
				    for i in range(nrInputs) )

		self.outputs = [ FupConnOut(self, text=("OUT%d" % i))
				 for i in range(nrOutputs) ]
		self.outputs.extend(FupConnOut(self, text=text)
				    for text in self.FIXED_OUTPUTS )

		self.__renumberConns()

	def __renumberConns(self):
		i = 0
		for conn in self.inputs:
			if not conn.text or conn.text.startswith("IN"):
				conn.text = "IN%d" % i
				i += 1
		i = 0
		for conn in self.outputs:
			if not conn.text or conn.text.startswith("OUT"):
				conn.text = "OUT%d" % i
				i += 1

	def __inferInInsertIndex(self, conn):
		beforeIndex = len(self.inputs)
		return beforeIndex

	def __inferOutInsertIndex(self, conn):
		connText = conn.text.upper()
		connOutSeq = self.__CONN_OUT_SEQUENCE
		if connText in connOutSeq:
			# This is one of the special outputs.
			# Add it to its fixed position (see connOutSeq).
			if connText in (c.text.upper() for c in self.outputs):
				return -1 # We already have this one.
			beforeIndex = len(self.outputs)
			for idx, c in enumerate(self.outputs):
				if c.text.upper() not in connOutSeq:
					continue
				if connOutSeq.index(connText) <\
				   connOutSeq.index(c.text.upper()):
					beforeIndex = idx
					break
		else:
			# This is a regular OUTx output.
			# Add it just before the special outputs.
			beforeIndex = len(self.outputs)
			for idx, c in enumerate(self.outputs):
				if c.text.upper() in connOutSeq:
					beforeIndex = idx
					break
		return beforeIndex

	# Overridden method. For documentation see base class.
	def insertConn(self, beforeIndex, conn):
		if not conn:
			return False
		if conn.IN:
			if beforeIndex is None:
				beforeIndex = self.__inferInInsertIndex(conn)
			if beforeIndex <= 0:
				return False
		else:
			if beforeIndex is None:
				beforeIndex = self.__inferOutInsertIndex(conn)
				if beforeIndex < 0:
					return False
		ok = FupElem.insertConn(self, beforeIndex, conn)
		if ok:
			self.__renumberConns()
		return ok

	# Overridden method. For documentation see base class.
	def getAreaViaPixCoord(self, pixelX, pixelY):
		if self.grid:
			cellWidth = self.grid.cellPixWidth
			cellHeight = self.grid.cellPixHeight
			totalWidth = cellWidth
			totalHeight = cellHeight * self.height
			xpad, ypad = self._xpadding, self._ypadding
			if pixelY >= ypad and\
			   pixelY < totalHeight - ypad:
				if pixelX < xpad:
					# inputs
					idx = pixelY // cellHeight
					lastIdx = len(self.inputs) - 1
					if idx <= lastIdx:
						return self.AREA_INPUT, idx
				elif pixelX >= totalWidth - xpad:
					# outputs
					idx = pixelY // cellHeight
					firstIdx = self.height - len(self.outputs)
					if idx >= firstIdx:
						idx -= firstIdx
						return self.AREA_OUTPUT, idx
				else:
					# body
					return self.AREA_BODY, 0
		return self.AREA_NONE, 0

	# Overridden method. For documentation see base class.
	def getConnRelCoords(self, conn):
		x, y = 0, -1
		if conn.IN:
			y = self.inputs.index(conn)
		elif conn.OUT:
			y = self.outputs.index(conn)
			if y >= 0:
				y = self.height - len(self.outputs) + y
		if x >= 0 and y >= 0:
			return x, y
		return FupElem.getConnRelCoords(self, conn)

	# Overridden method. For documentation see base class.
	@property
	def height(self):
		return max(len(self.inputs),
			   len(self.outputs))

	# Overridden method. For documentation see base class.
	def draw(self, painter):
		grid = self.grid
		if not grid:
			return
		cellWidth = grid.cellPixWidth
		cellHeight = grid.cellPixHeight
		xpad, ypad = self._xpadding, self._ypadding
		elemHeight = cellHeight * self.height
		elemWidth = cellWidth
		selected = self.selected

		# Draw body
		painter.setPen(self._outlineSelPen if selected
			       else self._outlinePen)
		painter.setBrush(self._bgSelBrush if selected
				 else self._bgBrush)
		(tlX, tlY), (trX, trY), (blX, blY), (brX, brY) = self._calcBodyBox()
		painter.drawRoundedRect(tlX, tlY,
					trX - tlX, blY - tlY,
					self.BODY_CORNER_RADIUS,
					self.BODY_CORNER_RADIUS)

		# Draw inputs
		painter.setBrush(self._bgSelBrush if selected
				 else self._bgBrush)
		painter.setFont(self.getFont(8))
		for i, conn in enumerate(self.inputs):
			cellIdx = i

			connPen = self._connPen\
				if (conn.isConnected or conn.text in self.OPTIONAL_CONNS)\
				else self._connOpenPen

			x = conn.drawOffset
			y = (cellIdx * cellHeight) + (cellHeight // 2)
			painter.setPen(connPen)
			painter.drawLine(x, y, xpad, y)

			if conn.text and\
			   not any(conn.text.startswith(b) for b in self.BLANK_CONNS):
				painter.setPen(connPen)
				x = xpad + 2
				y = (cellIdx * cellHeight)
				painter.drawText(x, y,
						 elemWidth - xpad - 2, cellHeight,
						 Qt.AlignLeft | Qt.AlignVCenter,
						 conn.text)

		# Draw outputs
		painter.setFont(self.getFont(8))
		for i, conn in enumerate(self.outputs):
			cellIdx = self.height - len(self.outputs) + i

			connPen = self._connPen\
				if (conn.isConnected or conn.text in self.OPTIONAL_CONNS)\
				else self._connOpenPen

			x = cellWidth - conn.drawOffset
			y = (cellIdx * cellHeight) + (cellHeight // 2)
			painter.setPen(connPen)
			painter.setBrush(self._bgSelBrush if selected
					 else self._bgBrush)
			painter.drawLine(cellWidth - xpad, y,
					 x, y)

			if conn.text and\
			   not any(conn.text.startswith(b) for b in self.BLANK_CONNS):
				x = 0
				y = (cellIdx * cellHeight)
				painter.drawText(x, y,
						 elemWidth - xpad - 2, cellHeight,
						 Qt.AlignRight | Qt.AlignVCenter,
						 conn.text)

		# Draw symbol text
		painter.setPen(self._outlineSelPen if selected
			       else self._outlinePen)
		painter.setBrush(self._bgSelBrush if selected
				 else self._bgBrush)
		painter.setFont(self.getFont(12, bold=True))
		painter.drawText(0, 0,
				 int(round(elemWidth * 0.75)), elemHeight,
				 Qt.AlignVCenter | Qt.AlignHCenter,
				 self.OP_SYM)

		# Draw disable-marker
		self._drawDisableMarker(painter)

	# Overridden method. For documentation see base class.
	def prepareContextMenu(self, menu, area=None, conn=None):
		menu.enableAddInput(True)
		menu.enableAddOutput(True)
		if conn:
			normalInputs = [ c for c in self.inputs
					 if c.text.upper() not in self.__CONN_IN_SEQUENCE ]
			normalOutputs = [ c for c in self.outputs
					  if c.text.upper() not in self.__CONN_OUT_SEQUENCE ]
			if conn.IN:
				if conn.text.upper() != "EN":
					menu.enableRemoveConn(len(normalInputs) > 2)
			else:
				if conn.text.upper() != "ENO":
					if conn.text.upper() in self.__CONN_OUT_SEQUENCE:
						menu.enableRemoveConn(True)
					else:
						menu.enableRemoveConn(len(normalOutputs) > 1)
			menu.enableDisconnWire(conn.isConnected)
		if not conn or conn.OUT:
			existing = set(c.text.upper() for c in self.outputs)
			if "REM" not in existing and self.HAVE_REMAINDER:
				menu.enableCustomAction(0, True, text="Add REMainder output")
			if "==0" not in existing:
				menu.enableCustomAction(1, True, text="Add ==0 output")
			if "<>0" not in existing:
				menu.enableCustomAction(2, True, text="Add <>0 output")
			if ">0" not in existing:
				menu.enableCustomAction(3, True, text="Add >0 output")
			if "<0" not in existing:
				menu.enableCustomAction(4, True, text="Add <0 output")
			if ">=0" not in existing:
				menu.enableCustomAction(5, True, text="Add >=0 output")
			if "<=0" not in existing:
				menu.enableCustomAction(6, True, text="Add <=0 output")
			if "OV" not in existing:
				menu.enableCustomAction(7, True, text="Add OV output")
			if "UO" not in existing:
				menu.enableCustomAction(8, True, text="Add UO output")

	def __addStateOutput(self, name):
		return self.addConn(FupConnOut(text=name))

	def __handleAddREM(self, index):
		return self.__addStateOutput("REM")

	def __handleAddEQ0(self, index):
		return self.__addStateOutput("==0")

	def __handleAddNE0(self, index):
		return self.__addStateOutput("<>0")

	def __handleAddGT0(self, index):
		return self.__addStateOutput(">0")

	def __handleAddLT0(self, index):
		return self.__addStateOutput("<0")

	def __handleAddGE0(self, index):
		return self.__addStateOutput(">=0")

	def __handleAddLE0(self, index):
		return self.__addStateOutput("<=0")

	def __handleAddOV(self, index):
		return self.__addStateOutput("OV")

	def __handleAddUO(self, index):
		return self.__addStateOutput("UO")

	CUSTOM_ACTIONS = (
		__handleAddREM,		# index 0
		__handleAddEQ0,		# index 1
		__handleAddNE0,		# index 2
		__handleAddGT0,		# index 3
		__handleAddLT0,		# index 4
		__handleAddGE0,		# index 5
		__handleAddLE0,		# index 6
		__handleAddOV,		# index 7
		__handleAddUO,		# index 8
	)

class FupElem_ARITH_ADD_I(FupElem_ARITH):
	"""+I FUP/FBD element"""

	OP_SYM			= "+I"
	OP_SYM_NAME		= "add-int"	# XML ABI name

class FupElem_ARITH_SUB_I(FupElem_ARITH):
	"""-I FUP/FBD element"""

	OP_SYM			= "-I"
	OP_SYM_NAME		= "sub-int"	# XML ABI name

class FupElem_ARITH_MUL_I(FupElem_ARITH):
	"""*I FUP/FBD element"""

	OP_SYM			= "*I"
	OP_SYM_NAME		= "mul-int"	# XML ABI name

class FupElem_ARITH_DIV_I(FupElem_ARITH):
	"""/I FUP/FBD element"""

	OP_SYM			= "/I"
	OP_SYM_NAME		= "div-int"	# XML ABI name
	HAVE_REMAINDER		= True

class FupElem_ARITH_ADD_D(FupElem_ARITH):
	"""+D FUP/FBD element"""

	OP_SYM			= "+D"
	OP_SYM_NAME		= "add-dint"	# XML ABI name

class FupElem_ARITH_SUB_D(FupElem_ARITH):
	"""-D FUP/FBD element"""

	OP_SYM			= "-D"
	OP_SYM_NAME		= "sub-dint"	# XML ABI name

class FupElem_ARITH_MUL_D(FupElem_ARITH):
	"""*D FUP/FBD element"""

	OP_SYM			= "*D"
	OP_SYM_NAME		= "mul-dint"	# XML ABI name

class FupElem_ARITH_DIV_D(FupElem_ARITH):
	"""/D FUP/FBD element"""

	OP_SYM			= "/D"
	OP_SYM_NAME		= "div-dint"	# XML ABI name

class FupElem_ARITH_MOD_D(FupElem_ARITH):
	"""MOD FUP/FBD element"""

	OP_SYM			= "MOD"
	OP_SYM_NAME		= "mod-dint"	# XML ABI name

class FupElem_ARITH_ADD_R(FupElem_ARITH):
	"""+R FUP/FBD element"""

	OP_SYM			= "+R"
	OP_SYM_NAME		= "add-real"	# XML ABI name

class FupElem_ARITH_SUB_R(FupElem_ARITH):
	"""-R FUP/FBD element"""

	OP_SYM			= "-R"
	OP_SYM_NAME		= "sub-real"	# XML ABI name

class FupElem_ARITH_MUL_R(FupElem_ARITH):
	"""*R FUP/FBD element"""

	OP_SYM			= "*R"
	OP_SYM_NAME		= "mul-real"	# XML ABI name

class FupElem_ARITH_DIV_R(FupElem_ARITH):
	"""/R FUP/FBD element"""

	OP_SYM			= "/R"
	OP_SYM_NAME		= "div-real"	# XML ABI name
bues.ch cgit interface