aboutsummaryrefslogtreecommitdiffstats
path: root/awlsim/gui/cpuconfig.py
blob: 6e9af77d90bb3fba626e0561ce60c3eefdb31855 (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
# -*- coding: utf-8 -*-
#
# AWL simulator - GUI CPU configuration widget
#
# Copyright 2012-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.gui.configdialog import *
from awlsim.gui.util import *


class ClockMemSpinBox(QSpinBox):
	OFF_TEXT = "none"

	def __init__(self, parent=None):
		QSpinBox.__init__(self, parent)

		self.valueChanged.connect(self.__handleValueChange)

		self.setMinimum(-1)
		self.setMaximum(0xFFFF)
		self.setValue(-1)

	def __handleValueChange(self, newValue):
		if newValue < 0:
			self.setPrefix("   ")
		else:
			self.setPrefix("MB ")

	def textFromValue(self, value):
		if value < 0:
			return self.OFF_TEXT
		return QSpinBox.textFromValue(self, value)

	def valueFromText(self, text):
		if text == self.OFF_TEXT:
			return -1
		return QSpinBox.valueFromText(self, text)

class CpuConfigWidget(QWidget):
	def __init__(self, parent=None):
		QWidget.__init__(self, parent)
		self.setLayout(QGridLayout())
		self.layout().setContentsMargins(QMargins())

		group = QGroupBox("Hardware", self)
		group.setLayout(QGridLayout())

		label = QLabel("Number of accumulator registers", self)
		label.setToolTip(
			"Select the number of ACCU registers the CPU will have.\n"
			"Note that this selection influences the semantics of some\n"
			"arithmetic instructions such as +I,-I,*I,/I and others.")
		group.layout().addWidget(label, 0, 0)
		self.accuCombo = QComboBox(self)
		self.accuCombo.addItem("2 accus", 2)
		self.accuCombo.addItem("4 accus", 4)
		self.accuCombo.setToolTip(label.toolTip())
		group.layout().addWidget(self.accuCombo, 0, 1)

		label = QLabel("Clock memory byte (Taktmerker)", self)
		label.setToolTip(
			"Select an M byte for use as clock memory byte (Taktmerker).")
		group.layout().addWidget(label, 1, 0)
		self.clockMemSpin = ClockMemSpinBox(self)
		self.clockMemSpin.setToolTip(label.toolTip())
		group.layout().addWidget(self.clockMemSpin, 1, 1)

		self.obTempCheckBox = QCheckBox("Enable writing of OB &TEMP "
			"entry-variables", self)
		self.obTempCheckBox.setToolTip(
			"If this box is not checked the entry variables in the\n"
			"ORGANIZATION_BLOCK's TEMP region will not be filled\n"
			"by the system on entry into the OB.")
		group.layout().addWidget(self.obTempCheckBox, 2, 0, 1, 2)

		self.layout().addWidget(group, 0, 0)

		group = QGroupBox("AWL language", self)
		group.setLayout(QGridLayout())

		label = QLabel("Mnemonics language", self)
		label.setToolTip(
			"Select the AWL/STL mnemonics type.\n"
			"This may be either German or International (English).\n"
			"Automatic will try to guess the mnemonics from the AWL/STL\n"
			"code. Note that this might fail, though.")
		group.layout().addWidget(label, 0, 0)
		self.mnemonicsCombo = QComboBox(self)
		self.mnemonicsCombo.addItem("Automatic", S7CPUSpecs.MNEMONICS_AUTO)
		self.mnemonicsCombo.addItem("English", S7CPUSpecs.MNEMONICS_EN)
		self.mnemonicsCombo.addItem("German", S7CPUSpecs.MNEMONICS_DE)
		self.mnemonicsCombo.setToolTip(label.toolTip())
		group.layout().addWidget(self.mnemonicsCombo, 0, 1)

		self.extInsnsCheckBox = QCheckBox("Enable e&xtended "
			"non-standard instructions", self)
		self.extInsnsCheckBox.setToolTip(
			"Enable special Awlsim specific AWL/STL instructions that are not available\n"
			"in the standard S7 language. Enabling this option is harmless even\n"
			"if such instructions are not used.")
		group.layout().addWidget(self.extInsnsCheckBox, 1, 0, 1, 2)

		self.layout().addWidget(group, 1, 0)

		self.layout().setRowStretch(2, 1)

	def loadFromProject(self, project):
		specs = project.getCpuSpecs()

		index = self.accuCombo.findData(specs.nrAccus)
		assert(index >= 0)
		self.accuCombo.setCurrentIndex(index)

		self.clockMemSpin.setValue(specs.clockMemByte)

		index = self.mnemonicsCombo.findData(specs.getConfiguredMnemonics())
		assert(index >= 0)
		self.mnemonicsCombo.setCurrentIndex(index)

		self.obTempCheckBox.setCheckState(
			Qt.Checked if project.getObTempPresetsEn() else\
			Qt.Unchecked
		)

		self.extInsnsCheckBox.setCheckState(
			Qt.Checked if project.getExtInsnsEn() else\
			Qt.Unchecked
		)

	def storeToProject(self, project):
		specs = project.getCpuSpecs()

		mnemonics = self.mnemonicsCombo.itemData(self.mnemonicsCombo.currentIndex())
		nrAccus = self.accuCombo.itemData(self.accuCombo.currentIndex())
		clockMemByte = self.clockMemSpin.value()
		obTempEnabled = self.obTempCheckBox.checkState() == Qt.Checked
		extInsnsEnabled = self.extInsnsCheckBox.checkState() == Qt.Checked

		specs = project.getCpuSpecs()
		specs.setConfiguredMnemonics(mnemonics)
		specs.setNrAccus(nrAccus)
		specs.setClockMemByte(clockMemByte)
		project.setObTempPresetsEn(obTempEnabled)
		project.setExtInsnsEn(extInsnsEnabled)

		return True

class CpuConfigDialog(AbstractConfigDialog):
	def __init__(self, project, parent=None):
		AbstractConfigDialog.__init__(self,
			project = project,
			iconName = "cpu",
			title = "CPU setup",
			centralWidget = CpuConfigWidget(),
			parent = parent)

	def loadFromProject(self):
		self.centralWidget.loadFromProject(self.project)

	def storeToProject(self):
		if self.centralWidget.storeToProject(self.project):
			self.settingsChanged.emit()
bues.ch cgit interface