summaryrefslogtreecommitdiffstats
path: root/awlsim/instructions/insn_call.py
blob: d3ffa7671319a0f45ad4c7916228b1eac7ab3207 (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
# -*- coding: utf-8 -*-
#
# AWL simulator - instructions
#
# Copyright 2012-2013 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 awlsim.instructions.main import *


class AwlInsn_AbstractCall(AwlInsn):
	def staticSanityChecks(self):
		if len(self.ops) == 1:
			# FC/SFC call
			blockOper = self.ops[0]

			if blockOper.type == AwlOperator.BLKREF_FC:
				try:
					fc = self.cpu.fcs[blockOper.value.byteOffset]
				except KeyError as e:
					raise AwlSimError("Called FC not found",
						rawInsn = self.rawInsn)
			elif blockOper.type == AwlOperator.BLKREF_SFC:
				try:
					fc = self.cpu.sfcs[blockOper.value.byteOffset]
				except KeyError as e:
					raise AwlSimError("SFC %d not implemented, yet" %\
						blockOper.value.byteOffset,
						rawInsn = self.rawInsn)
			elif blockOper.type == AwlOperator.BLKREF_FB or\
			     blockOper.type == AwlOperator.BLKREF_SFB:
				raise AwlSimError("Missing DB in function "
					"block call",
					rawInsn = self.rawInsn)
			else:
				raise AwlSimError("Invalid CALL operand",
					rawInsn = self.rawInsn)

			if fc.interface.interfaceFieldCount != len(self.params):
				raise AwlSimError("Call interface mismatch. "
					"Passed %d parameters, but expected %d.\n"
					"====  The block interface is:\n%s\n====" %\
					(len(self.params), fc.interface.interfaceFieldCount,
					 str(fc.interface)),
					rawInsn = self.rawInsn)
		elif len(self.ops) == 2:
			# FB/SFB call
			blockOper = self.ops[0]
			dbOper = self.ops[1]

			if dbOper.type != AwlOperator.BLKREF_DB:
				raise AwlSimError("Second CALL operand is "
					"not a DB operand.",
					rawInsn = self.rawInsn)
			try:
				db = self.cpu.dbs[dbOper.value.byteOffset]
			except KeyError as e:
				raise AwlSimError("DB used in FB call not found",
					rawInsn = self.rawInsn)
			if not db.isInstanceDB():
				raise AwlSimError("DB %d is not an instance DB" %\
					dbOper.value.byteOffset,
					rawInsn = self.rawInsn)

			if blockOper.type == AwlOperator.BLKREF_FB:
				try:
					fb = self.cpu.fbs[blockOper.value.byteOffset]
				except KeyError as e:
					raise AwlSimError("Called FB not found",
						rawInsn = self.rawInsn)
				# TODO check if this is an FB-DB
				pass#TODO
			elif blockOper.type == AwlOperator.BLKREF_SFB:
				try:
					fb = self.cpu.sfbs[blockOper.value.byteOffset]
				except KeyError as e:
					raise AwlSimError("SFB %d not implemented, yet" %\
						blockOper.value.byteOffset,
						rawInsn = self.rawInsn)
				# TODO check if this is an SFB-DB
				pass#TODO
			elif blockOper.type == AwlOperator.BLKREF_FC or\
			     blockOper.type == AwlOperator.BLKREF_SFC:
				raise AwlSimError("Calling function, but "
					"a DB was specified.",
					rawInsn = self.rawInsn)
			else:
				raise AwlSimError("Invalid CALL operand",
					rawInsn = self.rawInsn)

			if db.codeBlock.index != fb.index:
				raise AwlSimError("DB %d is not an instance DB for FB %d" %\
					(dbOper.value.byteOffset,
					 blockOper.value.byteOffset),
					rawInsn = self.rawInsn)
		else:
			assert(0)

class AwlInsn_CALL(AwlInsn_AbstractCall):
	def __init__(self, cpu, rawInsn):
		AwlInsn_AbstractCall.__init__(self, cpu, AwlInsn.TYPE_CALL, rawInsn)
		self.assertOpCount((1,2))

		if len(self.ops) == 1:
			self.run = self.__run_CALL_FC
		else:
			self.run = self.__run_CALL_FB

	def __run_CALL_FC(self):
		self.cpu.run_CALL(self.ops[0], None, self.params)
		s = self.cpu.statusWord
		s.OS, s.OR, s.STA, s.NER = 0, 0, 1, 0

	def __run_CALL_FB(self):
		self.cpu.run_CALL(self.ops[0], self.ops[1], self.params)
		s = self.cpu.statusWord
		s.OS, s.OR, s.STA, s.NER = 0, 0, 1, 0

class AwlInsn_CC(AwlInsn_AbstractCall):
	def __init__(self, cpu, rawInsn):
		AwlInsn_AbstractCall.__init__(self, cpu, AwlInsn.TYPE_CC, rawInsn)
		self.assertOpCount(1)

	def run(self):
		s = self.cpu.statusWord
		if s.VKE:
			self.cpu.run_CALL(self.ops[0])
		s.OS, s.OR, s.STA, s.VKE, s.NER = 0, 0, 1, 1, 0

class AwlInsn_UC(AwlInsn_AbstractCall):
	def __init__(self, cpu, rawInsn):
		AwlInsn_AbstractCall.__init__(self, cpu, AwlInsn.TYPE_UC, rawInsn)
		self.assertOpCount(1)

	def run(self):
		self.cpu.run_CALL(self.ops[0])
		s = self.cpu.statusWord
		s.OS, s.OR, s.STA, s.NER = 0, 0, 1, 0
bues.ch cgit interface