-
Notifications
You must be signed in to change notification settings - Fork 0
/
device.lua
278 lines (222 loc) · 6.87 KB
/
device.lua
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
local bitfield = require "bitfield"
local _M = {_NAME="device", _TYPE="module"}
local _MT = {__index=_M}
-------------------------------------------------------------------------
-- Key values
_M.DEV_TYPE_INVALID = 'invalid'
_M.DEV_TYPE_NONE = 'null'
_M.DEV_TYPE_TERMINAL = 'terminal'
_M.DEV_TYPE_STREAM = 'stream'
_M.DEV_TYPE_MACHINE = 'machine'
_M.DEV_STATUS_READY = 0x00 -- Device is ready
_M.DEV_STATUS_BUSY = 0x01 -- Device will be available soon, honest.
_M.DEV_STATUS_BLOCKED = 0x0f -- Device is blocked and will not become ready without aid
_M.DEV_STATUS_FAULT = 0xff -- Device has faulted, and/or died.
local dev_type_names = {
[_M.DEV_TYPE_NONE] = "Null Device";
[_M.DEV_TYPE_TERMINAL] = "Terminal Device";
[_M.DEV_TYPE_STREAM] = "Datastream Device";
[_M.DEV_TYPE_MACHINE] = "TMVM machine.";
}
_M.typecount = 5
-------------------------------------------------------------------------
-- Name stuff
local manufacturers = {"Perfetion", "Acme", "Incomputech", "Generic", "Lowest-bid", "ADHOC", "Path-E-Tech"}
local names = {
[_M.DEV_TYPE_NONE] = {"null port", "data retention dept.", "air gap", "Mu"};
[_M.DEV_TYPE_TERMINAL] = {"terminal", "display", "client", "PEBKAC"};
[_M.DEV_TYPE_STREAM] = {"port", "serial line", "jute twine"};
[_M.DEV_TYPE_MACHINE] = {"Machine", "'Titanic'", "'Icarus'", "Analytical Engine", "'Iron'", "'Rex'"};
}
function _M:generatename(devtype)
if not names[devtype] then return "Unknown Device" end
local rnd = math.random
local devnames = names[devtype]
local manu, devname = manufacturers[rnd(#manufacturers)], devnames[rnd(#devnames)]
return string.format("%s %s", manu, devname)
end
namer = namedevice
catalogue = {}
local function initstream(d)
local dev = {idx=idx, type=devtype}
d.stream = {}
d.receive = function(self, pin, val)
assert(self.started, "Not Started!")
self.stream:write(string.char(val))
return 0
end
d.send = function(self, pin)
assert(self.started, "Not Started!")
local val = self.istream:read(1)
if val then
return string.byte(val);
else
return false, DEV_STATUS_BLOCKED
end
end
if d.type == _M.DEV_TYPE_TERMINAL then
d.portmaps = {{1}}
else
d.portmaps = {{10}, {20}, {30}, {40}}
end
return d
end
local function initnull(d)
d.portmaps = {{0}}
d.receive = function(self, pin, val)
assert(self.started, "Not Started!")
return 0
end
d.send = function(self, pin)
assert(self.started, "Not Started!")
return 0
end
return d
end
-------------------------------------------------------------------------
-- exports
-- Creates a new device based on the given parms
--
-- returns: device
function _M:new(t)
assert(t.type, "Cannot create devices with no type!")
local d = {type=t.type,_TYPE='device', name=t.name or _M:generatename(t.type);
portmaps = t.portmaps, portmap={}}
setmetatable(d, _MT)
local err
if d.type == _M.DEV_TYPE_TERMINAL
or d.type == _M.DEV_TYPE_STREAM then
d=initstream(d)
elseif d.type == _M.DEV_TYPE_NONE then
d=initnull(d)
elseif catalogue[d.type] then
d, err = catalogue[d.type].init(d)
else
error(("Unknown device type: '%s'"):format(tostring(d.type)))
end
return d, err
end
local function testmap(sysmap, testmap)
local _, p
for _, p in pairs(testmap) do
if sysmap[p] then return false end
end
return true
end
-- examines the given portmap and returns a non-conflicting portmap
--
-- returns: portmap
-- or
-- returns: false, errmsg
function _M.findports(dev, portmap)
assert(not dev.started, "Can't remap started devices")
assert(dev and dev.portmaps and portmap, "Findports requires valid parms")
local _, pm
for _, pm in pairs(dev.portmaps) do
if testmap(portmap, pm) then
return pm, "Okay"
end
end
return false, "Could not find valid port-mapping"
end
-- Starts the device, and readies it for use
--
-- returns status
-- or
-- returns false, errmsg
function _M:start(host, idx)
self.host = host
self.idx = idx
self.portmaps = nil --note plural
if self.type == _M.DEV_TYPE_TERMINAL or self.type == _M.DEV_TYPE_STREAM then
local foname = string.format("%s--%d--%s.log", self.host.name, self.idx, self.name)
local finame = string.format("%s-%02x.input", dev_type_names[self.type], self.idx)
local err
self.stream, err = io.open(foname, "wb")
assert(self.stream, "Could not open stream: "..tostring(err))
self.istream, err = io.open(finame, "rb")
-- failing to open the input is not an error
self.started = true
return _M.DEV_STATUS_READY, "Okay";
elseif self.type == _M.DEV_TYPE_NONE then
self.started = true
return _M.DEV_STATUS_READY, "Okay";
elseif self.type == _M.DEV_TYPE_CLOCK then
-- nothing doing.
self.started = true
return _M.DEV_STATUS_READY, "Okay";
elseif catalogue[self.type] then
return catalogue[self.type].start(self)
else
return false, "Unknown device type: "..tostring(self.type)
end
return false, "Unknown start error"
end
function _M:registerport(adr, port)
self.portmap[adr]=port
return self
end
function _M:writeport(adr, data)
assert(self.portmap[adr], "Data written to unknown port!")
if self.receive then
return self:receive(self.portmap[adr], data)
end
return false, _M.DEV_STATUS_BLOCKED
end
function _M:readport(adr, data)
assert(self.portmap[adr], "Data read from unknown port!")
if self.send then
return self:send(self.portmap[adr])
end
return false, _M.DEV_STATUS_BLOCKED
end
function _MT.__tostring(self)
return string.format('%s, "%s"', dev_type_names[self.type], self.name)
end
-- librum
-- with more than a bit of inspiration from package.*
_M.loaded = {}
local devicepath = "devices/?.lua;devices/?.drv;"
function _M:locate(lib)
assert(type(lib) == 'string', "Not a valid librum name: " .. tostring(lib))
if _M.loaded[lib] then
return _M.loaded[lib]
else
local s, tmp, err
for p in devicepath:gmatch("[^;]*") do
tmp = p:gsub("?", lib)
tmp, err = loadfile(tmp)
if tmp then
s, tmp, err = pcall(tmp)
if s then
return tmp, err
else
break
end
end
end
return false, "could not be found"
end
end
function _M:load(lib)
local dev, err = _M:locate(lib)
assert(dev, tostring(err))
return _M:registerdevice(lib, dev)
end
function _M:registerdevice(id, d)
assert(id, "A device type must have an identifier")
assert(d.regdata, "A device must provide valid registration data")
local rd = d.regdata
assert(rd.name, "A device type must have a name")
assert(rd.desc, "Device descriptions are not optional!")
assert(type(rd.names) == 'table' and rd.names[1], "At least one instance name must be provided!")
assert(rd.typename, "A typename must be provided")
self[rd.typename] = id
catalogue[id] = d
return id
end
-- MODULE TAIL
-------------------------------------------------------------------------
-- in 5.1.x the custom is to set a global equal to the module name, in all others this ill-behaved.
if _VERSION == "Lua 5.1" then _G[_M._NAME]=_M end
return _M