-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathapi.lua
406 lines (354 loc) · 10.4 KB
/
api.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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
--[[
vm16
====
Copyright (C) 2019-2022 Joachim Stolberg
GPL v3
See LICENSE.txt for more information
vm16 API
]]--
local vm16lib = ...
if vm16lib.version() ~= "2.7.5" then
minetest.log("error", "[vm16] Install Lua library v2.7.5 (see readme.md)!")
end
local M = minetest.get_meta
local VMList = {}
local storage = minetest.get_mod_storage()
if storage:get_int("version") ~= 2 then
storage:from_table()
-- Use position hashed based on `vm16lib.hash_node_position`
storage:set_int("version", 2)
end
-------------------------------------------------------------------------------
local VERSION = 3.7 -- See readme.md
-------------------------------------------------------------------------------
local VM16_OK = 0 -- run to the end
local VM16_NOP = 1 -- nop command
local VM16_IN = 2 -- input command
local VM16_OUT = 3 -- output command
local VM16_SYS = 4 -- system call
local VM16_HALT = 5 -- CPU halt
local VM16_BREAK = 6 -- breakpoint
local VM16_ERROR = 7 -- invalid call
vm16.OK = VM16_OK
vm16.NOP = VM16_NOP
vm16.IN = VM16_IN
vm16.OUT = VM16_OUT
vm16.SYS = VM16_SYS
vm16.HALT = VM16_HALT
vm16.BREAK = VM16_BREAK
vm16.ERROR = VM16_ERROR
vm16.version = VERSION
vm16.testbit = vm16lib.testbit
vm16.is_ascii = vm16lib.is_ascii
vm16.CallResults = {[0]="OK", "NOP", "IN", "OUT", "SYS", "HALT", "BREAK", "ERROR"}
function vm16.get_position_from_hash(hash)
local x = (hash:byte(1) - 48) + (hash:byte(2) - 48) * 64 + (hash:byte(3) - 48) * 4096
local y = (hash:byte(4) - 48) + (hash:byte(5) - 48) * 64 + (hash:byte(6) - 48) * 4096
local z = (hash:byte(7) - 48) + (hash:byte(8) - 48) * 64 + (hash:byte(9) - 48) * 4096
return {x = x - 32768, y = y - 32768, z = z - 32768}
end
-- Implemented in C
vm16.hash_node_position = vm16lib.hash_node_position
local function store_breakpoint_addr(pos, vm, breakpoints)
local addr = vm16lib.get_pc(vm)
if breakpoints then
breakpoints.address = addr
if breakpoints[addr] then
vm16lib.poke(vm, addr, breakpoints[addr])
end
end
end
local function skip_break_instr(pos, vm, cpu_def, breakpoints)
local addr = vm16lib.get_pc(vm)
if breakpoints then
if breakpoints.address == addr then
if breakpoints[addr] then
vm16.run(pos, cpu_def, nil, 1)
vm16lib.poke(vm, addr, 0x0400)
breakpoints.address = nil
return true
else
vm16lib.set_pc(vm, addr + 1)
breakpoints.address = nil
return true
end
end
breakpoints.address = nil
end
end
function vm16.set_breakpoint(pos, addr, breakpoints)
if breakpoints then
breakpoints[addr] = vm16.peek(pos, addr)
vm16.poke(pos, addr, 0x0400)
end
end
function vm16.reset_breakpoint(pos, addr, breakpoints)
if breakpoints and breakpoints[addr] then
breakpoints.address = nil
vm16.poke(pos, addr, breakpoints[addr])
return true
end
end
-- ram_size is from 0 for 64 words, 1 for 128 words, up to 10 for 64 Kwords
function vm16.create(pos, ram_size)
--print("vm_create")
local hash = vm16lib.hash_node_position(pos)
VMList[hash] = vm16lib.init(ram_size)
local meta = minetest.get_meta(pos)
meta:set_string("vm16", "")
meta:set_int("vm16size", ram_size)
meta:mark_as_private("vm16")
return VMList[hash] ~= nil
end
function vm16.destroy(pos)
--print("vm_destroy")
minetest.get_meta(pos):set_string("vm16", "")
local hash = vm16lib.hash_node_position(pos)
VMList[hash] = nil
end
function vm16.is_loaded(pos)
local hash = vm16lib.hash_node_position(pos)
return VMList[hash] ~= nil
end
-- move VM from storage string to active
function vm16.vm_restore(pos)
--print("vm_restore")
local meta = minetest.get_meta(pos)
local hash = vm16lib.hash_node_position(pos)
if not VMList[hash] then
local s = storage:get_string(hash)
local size = meta:get_int("vm16size")
if s ~= "" and size > 0 then
VMList[hash] = vm16lib.init(size)
vm16lib.set_vm(VMList[hash], s)
end
end
end
-- move VM from active to storage string
local function vm_store(pos, vm)
--print("vm_store")
local hash = vm16lib.hash_node_position(pos)
local s = vm16lib.get_vm(vm)
storage:set_string(hash, s)
end
-- Read VM memory and return the data as ASCII string
function vm16.get_vm(pos)
local hash = vm16lib.hash_node_position(pos)
local vm = VMList[hash]
return vm and vm16lib.get_vm(vm)
end
-- Write given data string to the VM memory
function vm16.set_vm(pos, s)
local hash = vm16lib.hash_node_position(pos)
local vm = VMList[hash]
return vm and vm16lib.set_vm(vm, s)
end
function vm16.on_power_on(pos, ram_size)
--print("on_power_on")
if not vm16.is_loaded(pos) then
if vm16.create(pos, ram_size) then
return true
end
end
end
function vm16.on_power_off(pos)
--print("on_power_off")
if vm16.is_loaded(pos) then
vm16.destroy(pos)
return true
end
end
function vm16.on_load(pos)
--print("on_load")
if not vm16.is_loaded(pos) then
vm16.vm_restore(pos)
return true
end
end
-- returns size in words
function vm16.mem_size(pos)
local hash = vm16lib.hash_node_position(pos)
local vm = VMList[hash]
return vm and vm16lib.mem_size(vm)
end
-- load PC with given address
function vm16.set_pc(pos, addr)
local hash = vm16lib.hash_node_position(pos)
local vm = VMList[hash]
return vm and vm16lib.set_pc(vm, addr)
end
function vm16.get_pc(pos)
local hash = vm16lib.hash_node_position(pos)
local vm = VMList[hash]
return vm and vm16lib.get_pc(vm)
end
function vm16.deposit(pos, value)
local hash = vm16lib.hash_node_position(pos)
local vm = VMList[hash]
return vm and vm16lib.deposit(vm, value)
end
function vm16.read_mem(pos, addr, num)
local hash = vm16lib.hash_node_position(pos)
local vm = VMList[hash]
return vm and vm16lib.read_mem(vm, addr, num)
end
function vm16.write_mem(pos, addr, tbl)
local hash = vm16lib.hash_node_position(pos)
local vm = VMList[hash]
return vm and vm16lib.write_mem(vm, addr, tbl)
end
function vm16.read_mem_bin(pos, addr, num)
local hash = vm16lib.hash_node_position(pos)
local vm = VMList[hash]
return vm and vm16lib.read_mem_bin(vm, addr, num)
end
function vm16.write_mem_bin(pos, addr, s)
local hash = vm16lib.hash_node_position(pos)
local vm = VMList[hash]
return vm and vm16lib.write_mem_bin(vm, addr, s)
end
function vm16.read_mem_as_str(pos, addr, num)
local hash = vm16lib.hash_node_position(pos)
local vm = VMList[hash]
return vm and vm16lib.read_mem_as_str(vm, addr, num)
end
function vm16.write_mem_as_str(pos, addr, s)
local hash = vm16lib.hash_node_position(pos)
local vm = VMList[hash]
return vm and vm16lib.write_mem_as_str(vm, addr, s)
end
function vm16.read_ascii(pos, addr, num)
local hash = vm16lib.hash_node_position(pos)
local vm = VMList[hash]
return vm and vm16lib.read_ascii(vm, addr, num)
end
function vm16.write_ascii(pos, addr, s)
local hash = vm16lib.hash_node_position(pos)
local vm = VMList[hash]
return vm and vm16lib.write_ascii(vm, addr, s)
end
function vm16.write_ascii_16(pos, addr, s)
local hash = vm16lib.hash_node_position(pos)
local vm = VMList[hash]
return vm and vm16lib.write_ascii_16(vm, addr, s)
end
function vm16.peek(pos, addr)
local hash = vm16lib.hash_node_position(pos)
local vm = VMList[hash]
return vm and vm16lib.peek(vm, addr)
end
function vm16.poke(pos, addr, val)
local hash = vm16lib.hash_node_position(pos)
local vm = VMList[hash]
return vm and vm16lib.poke(vm, addr, val)
end
function vm16.get_cpu_reg(pos)
local hash = vm16lib.hash_node_position(pos)
local vm = VMList[hash]
return vm and vm16lib.get_cpu_reg(vm)
end
function vm16.set_cpu_reg(pos, regs)
local hash = vm16lib.hash_node_position(pos)
local vm = VMList[hash]
return vm and vm16lib.set_cpu_reg(vm, regs)
end
function vm16.get_io_reg(pos)
local hash = vm16lib.hash_node_position(pos)
local vm = VMList[hash]
return vm and vm16lib.get_io_reg(vm)
end
function vm16.set_io_reg(pos, io)
local hash = vm16lib.hash_node_position(pos)
local vm = VMList[hash]
return vm and vm16lib.set_io_reg(vm, io)
end
-- Write H16 string to VM memory
function vm16.write_h16(pos, s)
local hash = vm16lib.hash_node_position(pos)
local vm = VMList[hash]
return vm and vm16lib.write_h16(vm, s)
end
-- Generate H16 string from VM memory
function vm16.read_h16(pos, start_addr, size)
local hash = vm16lib.hash_node_position(pos)
local vm = VMList[hash]
start_addr = start_addr or 0
size = size or vm16lib.mem_size(vm)
return vm and vm16lib.read_h16(vm, start_addr, size)
end
function vm16.run(pos, cpu_def, breakpoints, steps)
local hash = vm16lib.hash_node_position(pos)
local vm = VMList[hash]
local resp = VM16_ERROR
local ran, costs
if not vm then
return resp
end
local cycles = steps or cpu_def.instr_per_cycle
if skip_break_instr(pos, vm, cpu_def, breakpoints) then
return VM16_OK
end
while cycles > 0 do
resp, ran = vm16lib.run(vm, cycles)
cycles = cycles - ran
if resp == VM16_NOP then
return VM16_NOP
elseif resp == VM16_BREAK then
store_breakpoint_addr(pos, vm, breakpoints)
local cpu = vm16lib.get_cpu_reg(vm)
cpu_def.on_update(pos, resp, cpu)
return VM16_BREAK
elseif resp == VM16_IN then
local io = vm16lib.get_io_reg(vm)
io.data, costs = cpu_def.on_input(pos, io.addr)
vm16lib.set_io_reg(vm, io)
costs = tonumber(costs)
cycles = cycles - (costs or cpu_def.input_costs)
elseif resp == VM16_OUT then
local io = vm16lib.get_io_reg(vm)
costs = cpu_def.on_output(pos, io.addr, io.data, io.B)
costs = tonumber(costs)
cycles = cycles - (costs or cpu_def.output_costs)
elseif resp == VM16_SYS then
local io = vm16lib.get_io_reg(vm)
io.data, costs = cpu_def.on_system(pos, io.addr, io.A, io.B, io.C)
io.data = io.data or 0
vm16lib.set_io_reg(vm, io)
costs = tonumber(costs)
cycles = cycles - (costs or cpu_def.system_costs)
elseif resp == VM16_HALT then
local cpu = vm16lib.get_cpu_reg(vm)
cpu_def.on_update(pos, resp, cpu)
return VM16_HALT
elseif resp == VM16_ERROR then
local cpu = vm16lib.get_cpu_reg(vm)
cpu_def.on_update(pos, resp, cpu)
return VM16_ERROR
end
end
return resp
end
minetest.register_on_shutdown(function()
--print("register_on_shutdown2")
for hash, vm in pairs(VMList) do
local pos = vm16.get_position_from_hash(hash)
vm_store(pos, vm)
end
--print("done")
end)
local function remove_unloaded_vms()
local tbl = table.copy(VMList)
local cnt = 0
VMList = {}
for hash, vm in pairs(tbl) do
local pos = vm16.get_position_from_hash(hash)
if minetest.get_node_or_nil(pos) then
VMList[hash] = vm
cnt = cnt + 1
else
vm_store(pos, vm)
end
end
minetest.after(60, remove_unloaded_vms)
end
minetest.after(60, remove_unloaded_vms)