summaryrefslogtreecommitdiffstats
path: root/awlcounters.py
blob: 152a2542b7bddeaf887df96d38fee8525aaf234f (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
# -*- coding: utf-8 -*-
#
# AWL simulator - counters
# Copyright 2012-2013 Michael Buesch <m@bues.ch>
#
# Licensed under the terms of the GNU General Public License version 2.
#

from util import *


class Counter(object):
	def __init__(self, cpu, index):
		self.cpu = cpu
		self.index = index
		self.prevVKE_FR = 0
		self.prevVKE_S = 0
		self.prevVKE_ZV = 0
		self.prevVKE_ZR = 0
		self.counter = 0

	def get(self):
		return 1 if self.counter else 0

	def getValueBin(self):
		return self.counter

	def getValueBCD(self):
		bcd = self.counter % 10
		bcd |= ((self.counter // 10) % 10) << 4
		bcd |= ((self.counter // 100) % 10) << 8
		return bcd

	def set(self, VKE):
		if (~self.prevVKE_S & VKE) & 1:
			counterBCD = self.cpu.accu1.get()
			a, b, c = (counterBCD & 0xF),\
				  ((counterBCD >> 4) & 0xF),\
				  ((counterBCD >> 8) & 0xF)
			if counterBCD > 0x999 or a > 9 or b > 9 or c > 9:
				raise AwlSimError("Invalid BCD value")
			self.counter = a + (b * 10) + (c * 100)
		self.prevVKE_S = VKE

	def reset(self):
		self.counter = 0

	def run_FR(self, VKE):
		if (~self.prevVKE_FR & VKE) & 1:
			self.prevVKE_S = 0
			self.prevVKE_ZV = 0
			self.prevVKE_ZR = 0
		self.prevVKE_FR = VKE

	def run_ZV(self, VKE):
		if (~self.prevVKE_ZV & VKE) & 1:
			if self.counter < 999:
				self.counter += 1
		self.prevVKE_ZV = VKE

	def run_ZR(self, VKE):
		if (~self.prevVKE_ZR & VKE) & 1:
			if self.counter > 0:
				self.counter -= 1
		self.prevVKE_ZR = VKE
bues.ch cgit interface