summaryrefslogtreecommitdiffstats
path: root/awldatatypes.py
blob: 6686dbecd4bf2db18801c32808940e927c7a7499 (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
# -*- coding: utf-8 -*-
#
# AWL data types
# Copyright 2012 Michael Buesch <m@bues.ch>
#
# Licensed under the terms of the GNU General Public License version 2.
#

from util import *


class GenericInteger(object):
	def __init__(self, value, mask):
		self.value = value
		self.mask = mask
		assert(mask <= 0xFFFFFFFF)

	def set(self, value):
		self.value = value & self.mask

	def setByte(self, value):
		self.value = ((self.value & 0xFFFFFF00) |\
			      (value & 0xFF)) &\
			     self.mask

	def setWord(self, value):
		self.value = ((self.value & 0xFFFF0000) |\
			      (value & 0xFFFF)) &\
			     self.mask

	def setDWord(self, value):
		self.value = value & 0xFFFFFFFF & self.mask

	def get(self):
		return self.value

	def getByte(self):
		return self.value & 0xFF

	def getWord(self):
		return self.value & 0xFFFF

	def getDWord(self):
		return self.value & 0xFFFFFFFF

	def getSignedByte(self):
		return byteToSignedPyInt(self.value)

	def getSignedWord(self):
		return wordToSignedPyInt(self.value)

	def getSignedDWord(self):
		return dwordToSignedPyInt(self.value)

	def setBit(self, bitNumber):
		self.value = (self.value | (1 << bitNumber)) & self.mask

	def clearBit(self, bitNumber):
		self.value &= ~(1 << bitNumber)

	def setBitValue(self, bitNumber, value):
		if value:
			self.setBit(bitNumber)
		else:
			self.clearBit(bitNumber)

	def getBit(self, bitNumber):
		return 1 if (self.value & (1 << bitNumber)) else 0

	def toHex(self):
		if self.mask == 0xFF:
			return "%02X" % self.value
		elif self.mask == 0xFFFF:
			return "%04X" % self.value
		elif self.mask == 0xFFFFFFFF:
			return "%08X" % self.value
		else:
			assert(0)

class GenericByte(GenericInteger):
	def __init__(self, value=0):
		GenericInteger.__init__(self, value, 0xFF)

class GenericWord(GenericInteger):
	def __init__(self, value=0):
		GenericInteger.__init__(self, value, 0xFFFF)

class GenericDWord(GenericInteger):
	def __init__(self, value=0):
		GenericInteger.__init__(self, value, 0xFFFFFFFF)
bues.ch cgit interface