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 - data structs
#
# Copyright 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.util import *
from awlsim.datatypes import *
class AwlStructField(object):
"Data structure field"
# name => Field name string
# offset => Field offset as AwlOffset
# dataType => AwlDataType
# initialValue => The initilization value
def __init__(self, name, offset, dataType, initialValue=0):
self.name = name
self.offset = offset
self.dataType = dataType
self.initialValue = initialValue
# Store a copy of the data type size, in bits and bytes.
self.bitSize = self.dataType.width
self.byteSize = intDivRoundUp(self.bitSize, 8)
assert(self.bitSize in (1, 8, 16, 32, 64))
class AwlStruct(object):
"Data structure"
def __init__(self):
self.fields = []
self.name2field = {}
# Return size, in bytes.
def getSize(self):
if not self.fields:
return 0
lastField = self.fields[-1]
return lastField.offset.byteOffset + lastField.byteSize
def addField(self, name, dataType):
if dataType.width == 1 and self.fields and\
self.fields[-1].bitSize == 1 and\
self.fields[-1].offset.bitOffset < 7:
# Consecutive bitfields are merged into one byte
offset = AwlOffset(self.fields[-1].offset.byteOffset,
self.fields[-1].offset.bitOffset + 1)
else:
offset = AwlOffset(self.getSize())
field = AwlStructField(name, offset, dataType)
self.fields.append(field)
if name:
self.name2field[name] = field
def addFieldAligned(self, name, dataType, byteAlignment):
padding = byteAlignment - self.getSize() % byteAlignment
if padding == byteAlignment:
padding = 0
while padding:
self.addField(None, AwlDataType.makeByName("BYTE"))
padding -= 1
self.addField(name, dataType)
def addFieldNaturallyAligned(self, name, dataType):
alignment = 1
if dataType.width > 8:
alignment = 2
self.addFieldAligned(name, dataType, alignment)
def getField(self, name):
try:
return self.name2field[name]
except KeyError:
raise AwlSimError("Data structure field '%s' not found" % name)
class AwlStructInstanceByte(GenericByte):
"Data structure byte"
def __init__(self, value=0):
GenericByte.__init__(self, value)
class AwlStructInstance(object):
"Data structure instance"
def __init__(self, struct):
self.struct = struct
self.__allocate()
def __allocate(self):
self.dataBytes = []
for field in self.struct.fields:
bsize = field.byteSize
value = field.initialValue
for i in range(field.byteSize):
b = (value >> ((bsize - i - 1) * 8)) & 0xFF
self.dataBytes.append(AwlStructInstanceByte(b))
def getFieldData(self, field):
offset, bitSize = field.offset, field.bitSize
if bitSize % 8 == 0:
nrBytes, value, off = bitSize // 8, 0, offset.byteOffset
assert(offset.bitOffset == 0)
while nrBytes:
value = (value << 8) | self.dataBytes[off].get()
nrBytes -= 1
off += 1
return value
elif bitSize == 1:
return self.dataBytes[offset.byteOffset].getBit(offset.bitOffset)
else:
raise AwlSimError("Invalid struct fetch of %d bits" % bitSize)
def setFieldData(self, field, value):
offset, bitSize = field.offset, field.bitSize
if bitSize % 8 == 0:
nrBytes = bitSize // 8
off = offset.byteOffset + nrBytes - 1
assert(offset.bitOffset == 0)
while nrBytes:
self.dataBytes[off].set(value)
value >>= 8
nrBytes -= 1
off -= 1
elif bitSize == 1:
self.dataBytes[offset.byteOffset].setBitValue(
offset.bitOffset, value)
else:
raise AwlSimError("Invalid struct write of %d bits" % bitSize)
def getFieldDataByName(self, name):
return self.getFieldData(self.struct.getField(name))
def setFieldDataByName(self, name, value):
self.setFieldData(self.struct.getField(name), value)
|