aboutsummaryrefslogtreecommitdiffstats
path: root/awlsim/fupcompiler/elemconv.py
blob: 26cf7837d002c01875cb4afcd2856ea317557bfe (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
# -*- coding: utf-8 -*-
#
# AWL simulator - FUP compiler - Value conversion boxes
#
# 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.fupcompiler.elem import *
from awlsim.fupcompiler.elemoper import *
from awlsim.fupcompiler.elembool import *
from awlsim.fupcompiler.helpers import *

from awlsim.core.operators import * #+cimport
from awlsim.core.operatortypes import * #+cimport

from awlsim.core.instructions.all_insns import * #+cimport


class FupCompiler_ElemConv(FupCompiler_Elem):
	"""FUP compiler - Value conversion operation.
	"""

	ELEM_NAME		= "CONV"
	SUBTYPE			= None # Override this in the subclass
	CONV_INSN_CLASS		= None # Override this in the subclass

	EnumGen.start
	SUBTYPE_BTI	= EnumGen.item
	SUBTYPE_ITB	= EnumGen.item
	SUBTYPE_BTD	= EnumGen.item
	SUBTYPE_ITD	= EnumGen.item
	SUBTYPE_DTB	= EnumGen.item
	SUBTYPE_DTR	= EnumGen.item
	SUBTYPE_INVI	= EnumGen.item
	SUBTYPE_INVD	= EnumGen.item
	SUBTYPE_NEGI	= EnumGen.item
	SUBTYPE_NEGD	= EnumGen.item
	SUBTYPE_NEGR	= EnumGen.item
	SUBTYPE_TAW	= EnumGen.item
	SUBTYPE_TAD	= EnumGen.item
	SUBTYPE_RND	= EnumGen.item
	SUBTYPE_TRUNC	= EnumGen.item
	SUBTYPE_RNDP	= EnumGen.item
	SUBTYPE_RNDN	= EnumGen.item
	EnumGen.end

	str2subtype = {
		"bti"	: SUBTYPE_BTI,
		"itb"	: SUBTYPE_ITB,
		"btd"	: SUBTYPE_BTD,
		"itd"	: SUBTYPE_ITD,
		"dtb"	: SUBTYPE_DTB,
		"dtr"	: SUBTYPE_DTR,
		"invi"	: SUBTYPE_INVI,
		"invd"	: SUBTYPE_INVD,
		"negi"	: SUBTYPE_NEGI,
		"negd"	: SUBTYPE_NEGD,
		"negr"	: SUBTYPE_NEGR,
		"taw"	: SUBTYPE_TAW,
		"tad"	: SUBTYPE_TAD,
		"rnd"	: SUBTYPE_RND,
		"trunc"	: SUBTYPE_TRUNC,
		"rndp"	: SUBTYPE_RNDP,
		"rndn"	: SUBTYPE_RNDN,
	}

	@classmethod
	def parse(cls, grid, x, y, subType, content):
		try:
			subType = cls.str2subtype[subType]
			type2class = {
				cls.SUBTYPE_BTI		: FupCompiler_ElemConvBTI,
				cls.SUBTYPE_ITB		: FupCompiler_ElemConvITB,
				cls.SUBTYPE_BTD		: FupCompiler_ElemConvBTD,
				cls.SUBTYPE_ITD		: FupCompiler_ElemConvITD,
				cls.SUBTYPE_DTB		: FupCompiler_ElemConvDTB,
				cls.SUBTYPE_DTR		: FupCompiler_ElemConvDTR,
				cls.SUBTYPE_INVI	: FupCompiler_ElemConvINVI,
				cls.SUBTYPE_INVD	: FupCompiler_ElemConvINVD,
				cls.SUBTYPE_NEGI	: FupCompiler_ElemConvNEGI,
				cls.SUBTYPE_NEGD	: FupCompiler_ElemConvNEGD,
				cls.SUBTYPE_NEGR	: FupCompiler_ElemConvNEGR,
				cls.SUBTYPE_TAW		: FupCompiler_ElemConvTAW,
				cls.SUBTYPE_TAD		: FupCompiler_ElemConvTAD,
				cls.SUBTYPE_RND		: FupCompiler_ElemConvRND,
				cls.SUBTYPE_TRUNC	: FupCompiler_ElemConvTRUNC,
				cls.SUBTYPE_RNDP	: FupCompiler_ElemConvRNDP,
				cls.SUBTYPE_RNDN	: FupCompiler_ElemConvRNDN,
			}
			elemClass = None
			with contextlib.suppress(KeyError):
				elemClass = type2class[subType]
			if elemClass:
				return elemClass(grid=grid, x=x, y=y,
						 content=content)
		except KeyError:
			pass
		return None

	def __init__(self, grid, x, y, content, **kwargs):
		FupCompiler_Elem.__init__(self, grid=grid, x=x, y=y,
					  elemType=FupCompiler_Elem.TYPE_CONV,
					  subType=self.SUBTYPE,
					  content=content,
					  **kwargs)

	def connIsOptional(self, conn):
		if not self.enabled:
			return True
		return conn.hasText({ "EN", "ENO", })

	def getConnType(self, conn, preferVKE=False):
		if self.enabled and conn in self.connections:
			if conn.hasText({ "EN", "ENO", }):
				return FupCompiler_Conn.TYPE_VKE
			return FupCompiler_Conn.TYPE_ACCU
		return FupCompiler_Conn.TYPE_UNKNOWN

	def __getConnsEN(self):
		"""Get EN and ENO connections.
		"""
		conn_EN = self.getUniqueConnByText("EN", searchInputs=True)
		conn_ENO = self.getUniqueConnByText("ENO", searchOutputs=True)
		return conn_EN, conn_ENO

	def __getConnIN(self):
		"""Get the IN connection.
		"""
		conn_IN = self.getUniqueConnByText("IN", searchInputs=True)
		if not conn_IN:
			raise FupElemError("No IN connection "
				"in FUP element %s." % (
				str(self)),
				self)
		return conn_IN

	def __allConnsOUT(self):
		"""Get all OUTx connections.
		"""
		for conn in FupCompiler_Conn.sorted(self.outConnections):
			if conn.textMatch(r"OUT\d+"):
				yield conn

	def compileConn(self, conn, desiredTarget, inverted=False):
		if not self.enabled:
			return []
		insns = []
		assert(conn in self.connections)

		awlInsnClass = FupCompiler_Conn.targetToInsnClass(desiredTarget,
								  toLoad=True,
								  inverted=inverted)

		if conn.hasText("ENO"):
			self._compileConn_checkTarget(conn, desiredTarget, inverted,
						      targetExpectVKE=True,
						      allowInversion=True)
			if self.needCompile:
				insns.extend(self.compile())
				if inverted:
					insns.append(self.newInsn(AwlInsn_NOT))
			else:
				insns.extend(conn.elem._loadFromTemp(awlInsnClass, conn))
		elif conn.textMatch(r"OUT\d+"):
			self._compileConn_checkTarget(conn, desiredTarget, inverted,
						      targetExpectVKE=False,
						      allowInversion=False)
			if self.needCompile:
				insns.extend(self.compile())
			insns.extend(conn.elem._loadFromTemp(awlInsnClass, conn))
		else:
			return FupCompiler_Elem.compileConn(self, conn, desiredTarget, inverted)
		return insns

	def _doPreprocess(self):
		if not self.enabled:
			return
		# If the element connected to IN is not a LOAD operand, we must
		# take its ENO into account.
		# If we don't have a connection on EN, we implicitly connect
		# the IN-element's ENO to our EN here.
		# If we already have a connection on EN, we implicitly add an AND-element
		# between the IN-element's ENO and our EN.
		elemsA = []
		connectedElem = self.__getConnIN().getConnectedElem(viaOut=True)
		if not connectedElem.isType(FupCompiler_Elem.TYPE_OPERAND,
					    FupCompiler_ElemOper.SUBTYPE_LOAD):
			elemsA.append(connectedElem)
		FupCompiler_Helpers.genIntermediateBool(
				parentElem=self,
				elemsA=elemsA,
				connNamesA=(["ENO"] * len(elemsA)),
				elemB=self,
				connNameB="EN",
				boolElemClass=FupCompiler_ElemBoolAnd)

	def _doCompile(self):
		if not self.enabled:
			return []
		insns = []

		conn_EN, conn_ENO = self.__getConnsEN()
		enIsConnected = conn_EN is not None and conn_EN.isConnected
		enoIsConnected = conn_ENO is not None and conn_ENO.isConnected

		# Compile the element connected to the IN connection.
		conn_IN = self.__getConnIN()
		connectedElem = conn_IN.getConnectedElem(viaOut=True)
		# Only compile the element, if it is not a plain LOAD box.
		if connectedElem.needCompile and\
		   not connectedElem.isType(FupCompiler_Elem.TYPE_OPERAND,
					    FupCompiler_ElemOper.SUBTYPE_LOAD):
			insns.extend(connectedElem.compile())

		# Generate a jump target label name for the EN jump.
		# This might end up being unused, though.
		endLabel = self.grid.compiler.newLabel()

		# If we have an EN input, emit the corresponding conditional jump.
		# If EN is not a plain operator, this might involve compiling
		# the connected element.
		if enIsConnected:
			# Compile the element that drives this wire.
			otherConn = conn_EN.getConnectedConn(getOutput=True)
			insns.extend(otherConn.compileConn(targetInsnClass=AwlInsn_U,
							   inverted=False))

			# Emit the jump instruction.
			# This will evaluate the current VKE.
			insns.append(self.newInsn_JMP(AwlInsn_SPBNB, endLabel))

		# Compile the actual operation.
		otherConn = conn_IN.getConnectedConn(getOutput=True)
		otherElem = otherConn.elem
		# Compile the element connected to the input.
		insns.extend(otherConn.compileConn(targetInsnClass=AwlInsn_L))
		if otherConn.connType != FupCompiler_Conn.TYPE_ACCU:
			raise FupElemError("The IN connection "
				"of the FUP conversion box %s must not be connected "
				"to a bit (VKE) wire." % (
				str(self)),
				self)
		# Append the actual operation.
		insns.append(self.newInsn(self.CONV_INSN_CLASS))

		# Assign the outputs.
		storeToTempConns = set()
		for conn in self.__allConnsOUT():
			for otherElem in self.sorted(conn.getConnectedElems(viaIn=True)):
				if otherElem.isType(FupCompiler_Elem.TYPE_OPERAND,
						    FupCompiler_ElemOper.SUBTYPE_ASSIGN):
					insns.extend(otherElem.emitStore_ACCU())
				else:
					storeToTempConns.add(conn)
		if storeToTempConns:
			storeToTempConns.add(self.MAIN_RESULT)
			insns.extend(self._storeToTemp("DWORD", AwlInsn_T, storeToTempConns))

		# Make sure BIE is set, if EN is not connected and ENO is connected.
		if not enIsConnected and enoIsConnected:
			# set BIE=1 and /ER=0.
			insns.extend(self.newInsns_SET_BIE_CLR_ER())

		# Create the jump target label for EN=0.
		# This might end up being unused, though.
		insns.append(self.newInsn_NOP(labelStr=endLabel))

		# Handle ENO output.
		if enoIsConnected:
			# Add instruction:  U BIE
			insns.append(self.newInsn_LOAD_BIE(AwlInsn_U))

			# Add VKE assignment instruction.
			storeToTempConns = set()
			for otherElem in self.sorted(conn_ENO.getConnectedElems(viaIn=True)):
				if otherElem.isType(FupCompiler_Elem.TYPE_OPERAND,
						    FupCompiler_ElemOper.SUBTYPE_ASSIGN):
					insns.extend(otherElem.emitStore_VKE())
				else:
					storeToTempConns.add(conn_ENO)
			if storeToTempConns:
				insns.extend(self._storeToTemp("BOOL", AwlInsn_ASSIGN,
							       storeToTempConns))

		return insns

class FupCompiler_ElemConvBTI(FupCompiler_ElemConv):
	"""FUP compiler - Value conversion operation - BTI
	"""

	ELEM_NAME		= "BTI"
	SUBTYPE			= FupCompiler_ElemConv.SUBTYPE_BTI
	CONV_INSN_CLASS		= AwlInsn_BTI

class FupCompiler_ElemConvITB(FupCompiler_ElemConv):
	"""FUP compiler - Value conversion operation - ITB
	"""

	ELEM_NAME		= "ITB"
	SUBTYPE			= FupCompiler_ElemConv.SUBTYPE_ITB
	CONV_INSN_CLASS		= AwlInsn_ITB

class FupCompiler_ElemConvBTD(FupCompiler_ElemConv):
	"""FUP compiler - Value conversion operation - BTD
	"""

	ELEM_NAME		= "BTD"
	SUBTYPE			= FupCompiler_ElemConv.SUBTYPE_BTD
	CONV_INSN_CLASS		= AwlInsn_BTD

class FupCompiler_ElemConvITD(FupCompiler_ElemConv):
	"""FUP compiler - Value conversion operation - ITD
	"""

	ELEM_NAME		= "ITD"
	SUBTYPE			= FupCompiler_ElemConv.SUBTYPE_ITD
	CONV_INSN_CLASS		= AwlInsn_ITD

class FupCompiler_ElemConvDTB(FupCompiler_ElemConv):
	"""FUP compiler - Value conversion operation - DTB
	"""

	ELEM_NAME		= "DTB"
	SUBTYPE			= FupCompiler_ElemConv.SUBTYPE_DTB
	CONV_INSN_CLASS		= AwlInsn_DTB

class FupCompiler_ElemConvDTR(FupCompiler_ElemConv):
	"""FUP compiler - Value conversion operation - DTR
	"""

	ELEM_NAME		= "DTR"
	SUBTYPE			= FupCompiler_ElemConv.SUBTYPE_DTR
	CONV_INSN_CLASS		= AwlInsn_DTR

class FupCompiler_ElemConvINVI(FupCompiler_ElemConv):
	"""FUP compiler - Value conversion operation - INVI
	"""

	ELEM_NAME		= "INVI"
	SUBTYPE			= FupCompiler_ElemConv.SUBTYPE_INVI
	CONV_INSN_CLASS		= AwlInsn_INVI

class FupCompiler_ElemConvINVD(FupCompiler_ElemConv):
	"""FUP compiler - Value conversion operation - INVD
	"""

	ELEM_NAME		= "INVD"
	SUBTYPE			= FupCompiler_ElemConv.SUBTYPE_INVD
	CONV_INSN_CLASS		= AwlInsn_INVD

class FupCompiler_ElemConvNEGI(FupCompiler_ElemConv):
	"""FUP compiler - Value conversion operation - NEGI
	"""

	ELEM_NAME		= "NEGI"
	SUBTYPE			= FupCompiler_ElemConv.SUBTYPE_NEGI
	CONV_INSN_CLASS		= AwlInsn_NEGI

class FupCompiler_ElemConvNEGD(FupCompiler_ElemConv):
	"""FUP compiler - Value conversion operation - NEGD
	"""

	ELEM_NAME		= "NEGD"
	SUBTYPE			= FupCompiler_ElemConv.SUBTYPE_NEGD
	CONV_INSN_CLASS		= AwlInsn_NEGD

class FupCompiler_ElemConvNEGR(FupCompiler_ElemConv):
	"""FUP compiler - Value conversion operation - NEGR
	"""

	ELEM_NAME		= "NEGR"
	SUBTYPE			= FupCompiler_ElemConv.SUBTYPE_NEGR
	CONV_INSN_CLASS		= AwlInsn_NEGR

class FupCompiler_ElemConvTAW(FupCompiler_ElemConv):
	"""FUP compiler - Value conversion operation - TAW
	"""

	ELEM_NAME		= "TAW"
	SUBTYPE			= FupCompiler_ElemConv.SUBTYPE_TAW
	CONV_INSN_CLASS		= AwlInsn_TAW

class FupCompiler_ElemConvTAD(FupCompiler_ElemConv):
	"""FUP compiler - Value conversion operation - TAD
	"""

	ELEM_NAME		= "TAD"
	SUBTYPE			= FupCompiler_ElemConv.SUBTYPE_TAD
	CONV_INSN_CLASS		= AwlInsn_TAD

class FupCompiler_ElemConvRND(FupCompiler_ElemConv):
	"""FUP compiler - Value conversion operation - RND
	"""

	ELEM_NAME		= "RND"
	SUBTYPE			= FupCompiler_ElemConv.SUBTYPE_RND
	CONV_INSN_CLASS		= AwlInsn_RND

class FupCompiler_ElemConvTRUNC(FupCompiler_ElemConv):
	"""FUP compiler - Value conversion operation - TRUNC
	"""

	ELEM_NAME		= "TRUNC"
	SUBTYPE			= FupCompiler_ElemConv.SUBTYPE_TRUNC
	CONV_INSN_CLASS		= AwlInsn_TRUNC

class FupCompiler_ElemConvRNDP(FupCompiler_ElemConv):
	"""FUP compiler - Value conversion operation - RND+
	"""

	ELEM_NAME		= "RND+"
	SUBTYPE			= FupCompiler_ElemConv.SUBTYPE_RNDP
	CONV_INSN_CLASS		= AwlInsn_RNDP

class FupCompiler_ElemConvRNDN(FupCompiler_ElemConv):
	"""FUP compiler - Value conversion operation - RND-
	"""

	ELEM_NAME		= "RND-"
	SUBTYPE			= FupCompiler_ElemConv.SUBTYPE_RNDN
	CONV_INSN_CLASS		= AwlInsn_RNDN
bues.ch cgit interface