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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
|
# -*- coding: utf-8 -*-
#
# LinuxCNC fake Python HAL module for unit testing
#
# Copyright 2014 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
HAL_BIT = 1
HAL_FLOAT = 2
HAL_S32 = 3
HAL_U32 = 4
HAL_IN = 16
HAL_OUT = 32
HAL_IO = HAL_IN | HAL_OUT
HAL_RO = 64
HAL_RW = 192
class _Pin(object):
def __init__(self, halName, halType, halDir):
assert(halType in (HAL_BIT, HAL_FLOAT, HAL_S32, HAL_U32))
assert(halDir in (HAL_IN, HAL_OUT, HAL_IO))
self.halName = halName
self.halType = halType
self.halDir = halDir
self.signals = []
if self.halType == HAL_FLOAT:
self.setHalData(0.0)
else:
self.setHalData(0)
def setHalData(self, v):
if self.halType == HAL_BIT:
assert(v in (0, 1))
elif self.halType == HAL_FLOAT:
assert(isinstance(v, float))
elif self.halType == HAL_S32:
assert(v >= -2147483648 and v <= 2147483647)
elif self.halType == HAL_U32:
assert(v >= 0 and v <= 2147483647)
else:
assert(0)
self.halData = v
if self.halDir in (HAL_OUT, HAL_IO):
for signal in self.signals:
signal.setHalData(v)
def _connectSignal(self, signal):
if self.halDir == HAL_IN:
assert(not self.signals)
self.signals.append(signal)
class _Param(_Pin):
def __init__(self, halName, halType, halDir):
assert(halType in (HAL_BIT, HAL_FLOAT, HAL_S32, HAL_U32))
assert(halDir in (HAL_RO, HAL_RW))
self.halName = halName
self.halType = halType
self.halDir = halDir
if self.halType == HAL_FLOAT:
self.setHalData(0.0)
else:
self.setHalData(0)
class _Signal(object):
def __init__(self, halName):
self.halName = halName
self.halType = None
self.connectedPins = []
def connectPin(self, pin):
assert(pin.halDir in (HAL_IN, HAL_OUT, HAL_IO))
assert(pin not in self.connectedPins)
if self.halType is None:
assert(not self.connectedPins)
self.halType = pin.halType
assert(self.halType == pin.halType)
self.connectedPins.append(pin)
pin._connectSignal(self)
def setHalData(self, v):
for pin in self.connectedPins:
if pin.halDir in (HAL_IN, HAL_IO):
pin.setHalData(v)
class component(object):
def __init__(self, name):
self.__pins = {}
self.__params = {}
self.__signals = {}
self.__ready = False
def newpin(self, p, t, d):
assert(p not in self.__pins)
assert(p not in self.__params)
assert(not self.__ready)
assert(not self.__signals)
self.__pins[p] = _Pin(p, t, d)
def newparam(self, p, t, d):
assert(p not in self.__pins)
assert(p not in self.__params)
assert(not self.__ready)
assert(not self.__signals)
self.__params[p] = _Param(p, t, d)
def __sanitizePinName(self, pinName):
assert(pinName.startswith("awlsim."))
pinName = pinName[7:]
assert(pinName)
return pinName
def __importHalFile(self, filename):
assert(not self.__signals)
lines = open(filename, "r").readlines()
import re
setp_re = re.compile(r'^setp\s+([\w\.\-]+)\s+([\w\.\-]+)$')
net_re = re.compile(r'^net\s+([\w\.\-]+)\s+<?=?>?\s*([\w\.\-]+)(?:\s+<?=?>?\s*([\w\.\-]+))?$')
for line in lines:
line = line.strip()
if not line:
continue
if line.startswith("#"):
continue
m = setp_re.match(line)
if m: # setp statement
halName, value = m.group(1), m.group(2)
halName = self.__sanitizePinName(halName)
value = int(value)
try:
self.__params[halName].setHalData(value)
except KeyError:
self.__pins[halName].setHalData(value)
continue
m = net_re.match(line)
if m: # net statement
if len(m.groups()) == 2:
sigName, pin0Name, pin1Name =\
m.group(1), m.group(2), None
else:
sigName, pin0Name, pin1Name =\
m.group(1), m.group(2), m.group(3)
if sigName in self.__signals:
sig = self.__signals[sigName]
else:
sig = _Signal(sigName)
self.__signals[sigName] = sig
if pin0Name:
sig.connectPin(self.__pins[self.__sanitizePinName(pin0Name)])
if pin1Name:
sig.connectPin(self.__pins[self.__sanitizePinName(pin1Name)])
# Ignore other statements
def ready(self):
import os
halFile = os.environ.get("FAKEHAL_HALFILE")
if halFile:
self.__importHalFile(halFile)
self.__ready = True
def __getitem__(self, k):
assert(self.__ready)
try:
pin = self.__pins[k]
assert(pin.halDir in (HAL_IN, HAL_IO))
return pin.halData
except KeyError:
param = self.__params[k]
assert(param.halDir in (HAL_RW, HAL_RO))
return param.halData
def __setitem__(self, k, v):
assert(self.__ready)
pin = self.__pins[k]
assert(pin.halDir in (HAL_OUT, HAL_IO))
pin.setHalData(v)
|