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
|
# -*- coding: utf-8 -*-
#
# AWL simulator - FUP compiler - Connection
#
# Copyright 2016-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.compat import *
from awlsim.common.xmlfactory import *
from awlsim.common.util import *
from awlsim.core.instructions.all_insns import * #+cimport
from awlsim.fupcompiler.base import *
import re
class FupCompiler_ConnFactory(XmlFactory):
def parser_open(self, tag=None):
self.inConn = False
XmlFactory.parser_open(self, tag)
def parser_beginTag(self, tag):
if not self.inConn:
if tag.name == "connection":
self.inConn = True
pos = tag.getAttrInt("pos")
dirIn = tag.getAttrInt("dir_in")
dirOut = tag.getAttrInt("dir_out")
wireId = tag.getAttrInt("wire")
text = tag.getAttr("text", "")
inverted = tag.getAttrBool("inverted", False)
uuid = tag.getAttr("uuid", None)
conn = FupCompiler_Conn(self.elem,
pos, dirIn, dirOut, wireId, text, inverted, uuid)
if not self.elem.addConn(conn):
raise self.Error("Invalid connection")
return
XmlFactory.parser_beginTag(self, tag)
def parser_endTag(self, tag):
if self.inConn:
if tag.name == "connection":
self.inConn = False
return
else:
if tag.name == "connections":
self.parser_finish()
return
XmlFactory.parser_endTag(self, tag)
class FupCompiler_Conn(FupCompiler_BaseObj):
factory = FupCompiler_ConnFactory
# Connection types
EnumGen.start
TYPE_UNKNOWN = EnumGen.item
TYPE_VKE = EnumGen.item # Bit operation
TYPE_ACCU = EnumGen.item # Byte/word/dword operation
EnumGen.end
# The target instruction to be used for TYPE_VKE/TYPE_ACCU compilation
EnumGen.start
TARGET_VKE_U = EnumGen.item
TARGET_VKE_O = EnumGen.item
TARGET_VKE_X = EnumGen.item
TARGET_ACCU1 = EnumGen.item
EnumGen.end
target2LoadInsnClass = {
TARGET_VKE_U : AwlInsn_U,
TARGET_VKE_O : AwlInsn_O,
TARGET_VKE_X : AwlInsn_X,
TARGET_ACCU1 : AwlInsn_L,
}
loadInsnClass2Target = pivotDict(target2LoadInsnClass)
target2StoreInsnClass = {
TARGET_VKE_U : AwlInsn_ASSIGN,
TARGET_VKE_O : AwlInsn_ASSIGN,
TARGET_VKE_X : AwlInsn_ASSIGN,
TARGET_ACCU1 : AwlInsn_T,
}
# Wire-ID used for unconnected connections
WIREID_NONE = -1
__slots__ = (
"elem",
"pos",
"dirIn",
"dirOut",
"wireId",
"text",
"inverted",
"virtual",
"wire",
)
@classmethod
def sorted(cls, connections):
"""Sort all connections from 'connections' sequence in ascending order by position.
The sorted list is returned.
"""
return sorted(connections, key=lambda c: c.pos)
@classmethod
def targetIsVKE(cls, target):
"""Returns True, if a target ID is a VKE target.
"""
return target in {cls.TARGET_VKE_U,
cls.TARGET_VKE_O,
cls.TARGET_VKE_X}
@classmethod
def targetToInsnClass(cls, target, toLoad=True, inverted=False):
"""Convert a target ID to its corresponding instruction class.
If 'toLoad' is True, the load instruction is returned.
If 'toLoad' is False, the store instruction is returned.
If 'inverted' is True, the inverted boolean is returned.
"""
from awlsim.fupcompiler.fupcompiler import FupCompiler
if inverted and not toLoad:
raise FupConnError("Boolean inversion is not "
"supported for boolean store instructions.")
if toLoad:
insnClass = cls.target2LoadInsnClass[target]
else:
insnClass = cls.target2StoreInsnClass[target]
if inverted:
insnClass = FupCompiler.invertedInsnClass[insnClass]
return insnClass
def __init__(self, elem, pos, dirIn, dirOut, wireId, text,
inverted=False,
virtual=False,
uuid=None):
FupCompiler_BaseObj.__init__(self, uuid=uuid)
self.elem = elem # FupCompiler_Elem
self.pos = pos # Position index
self.dirIn = bool(dirIn) # Input
self.dirOut = bool(dirOut) # Output
self.wireId = wireId # Wire ID number
self.text = text or "" # Connection text (optional)
self.inverted = bool(inverted) # True, if logically inverted connection
self.virtual = bool(virtual) # True, if this is a virtual connection
self.wire = None
@property
def compiler(self):
if self.elem:
return self.elem.compiler
return None
def hasText(self, text, caseSensitive=False):
"""Returns True, if the connection text is equal.
'text' is either a single string or a list of strings.
This does a case insensitive compare if caseSensitive=False.
"""
return any(strEqual(self.text, t, caseSensitive=caseSensitive)
for t in toList(text))
def textMatch(self, regexString, regexFlags=(re.IGNORECASE | re.DOTALL)):
"""Match the connection text to a regular expression.
Returns the match object, or None if there was no match.
"""
return re.match(regexString, self.text, regexFlags)
@property
def isOptional(self):
"""Returns True, if this connection is optional.
An optional connection does not have to be connected.
"""
return self.elem.connIsOptional(self)
@property
def connType(self):
"""Get the connection type.
This returns whether this connection is VKE based (TYPE_VKE)
or accu based (TYPE_ACCU).
If the type is unknown TYPE_UNKNOWN is returned.
"""
return self.elem.getConnType(self)
@property
def isConnected(self):
"""Returns True, if this connection is connected to a wire.
"""
return self.wire is not None
def getConnectedConns(self, getOutputs=False, getInputs=False):
"""Get all other connections that are connected
via self.wire to this connection.
This excludes self.
If 'getOutputs' is True, connections with dirOut=True are returned.
If 'getInputs' is True, connections with dirIn=True are returned.
"""
if self.wire:
for conn in self.wire.connections:
if conn is not self and\
((conn.dirOut and getOutputs) or\
(conn.dirIn and getInputs)):
yield conn
def getConnectedConn(self, getOutput=False, getInput=False):
"""Get the single connection that is connected to this connection.
If 'getOutput' is True, elements connected to the wire via OUT
connection are returned.
If 'getInput' is True, elements connected to the wire via OUT
connection are returned.
If neither 'getOutput' nor 'getInput' is True, None is returned.
If there is no matching element or more that one matching
element, an exception is raised.
"""
if not getOutput and not getInput:
return None
dirText = "%s%s%s" % (
"IN" if getInput else "",
"/" if getInput and getOutput else "",
"OUT" if getOutput else "")
selfText = (" \"%s\"" % self.text) if self.text else ""
connections = list(self.getConnectedConns(getOutputs=getOutput,
getInputs=getInput))
if len(connections) > 0:
if len(connections) > 1:
raise FupConnError("The connection%s of element '%s' does "
"only support a single %s-wire, "
"but has %d %s-connections." % (
selfText,
str(self.elem),
dirText,
len(connections),
dirText),
self)
return connections[0]
raise FupConnError("The connection%s of element '%s' does "
"does not have a valid %s-connected element." % (
selfText,
str(self.elem),
dirText),
self)
def getConnectedElems(self, viaOut=False, viaIn=False):
"""Get all elements that are connected to this connection.
If 'viaOut' is True, elements connected to the wire via OUT
connection are returned.
If 'viaIn' is True, elements connected to the wire via OUT
connection are returned.
If neither 'viaOut' nor 'viaIn' is True, no element is returned.
The element that belongs to 'self' is not returned.
"""
for conn in self.getConnectedConns(getOutputs=viaOut, getInputs=viaIn):
yield conn.elem
def getConnectedElem(self, viaOut=False, viaIn=False):
"""Get the single element that is connected to this connection.
If 'viaOut' is True, elements connected to the wire via OUT
connection are returned.
If 'viaIn' is True, elements connected to the wire via OUT
connection are returned.
If neither 'viaOut' nor 'viaIn' is True, None is returned.
If there is no matching element or more that one matching
element, an exception is raised.
"""
conn = self.getConnectedConn(getOutput=viaOut, getInput=viaIn)
if not conn:
return None
return conn.elem
def connectTo(self, otherConn):
if self.isConnected:
raise FupConnError("Connection %s is already connected" % (
str(self)),
self)
if otherConn.isConnected:
wire = otherConn.wire
else:
wire = self.elem.grid.newWire(virtual=True)
wire.addConn(otherConn)
wire.addConn(self)
def compileConn(self, target=None, targetInsnClass=None, inverted=False):
"""Compile the element that owns this connection.
A compile target has to be specified. That is either a VKE
instruction target, or an ACCU1 load target.
target: one of TARGET_xxx,
targetInsnClass: An AwlInsn_xxx class.
inverted: If True, generate an inverted boolean instruction.
Either target or targetInsnClass must be specified, but not both.
"""
assert(self.elem)
assert((target is not None) ^ (targetInsnClass is not None))
if targetInsnClass:
target = self.loadInsnClass2Target[targetInsnClass]
return self.elem.compileConn(self, target, inverted)
def __repr__(self):
return "FupCompiler_Conn(elem, pos=%d, dirIn=%s, dirOut=%s, "\
"wireId=%d, text=\"%s\", "\
"inverted=%s, virtual=%s)" % (
self.pos, self.dirIn, self.dirOut, self.wireId,
self.text, self.inverted, self.virtual)
def __str__(self):
fields = []
if self.text:
fields.append('"%s"' % self.text)
else:
fields.append("pos=%s" % self.pos)
if self.inverted:
fields.append("inverted")
if self.elem:
fields.append(str(self.elem))
if self.virtual:
fields.append("VIRTUAL")
return "CONNECTION(%s)" % (", ".join(fields))
|