Updated to use t400.lua from test3d-tools

This commit is contained in:
Izaya 2017-02-13 10:15:41 +00:00
parent f322b55a84
commit 812fbd44ae

View File

@ -1,10 +1,11 @@
local MEMSIZE=16 local MEMSIZE=16 -- does this still apply?
local SHUTUP = false local SHUTUP = false
if not digiline then if not digiline then
print("Digilines not found.") print("Digilines not found.")
return return
end end
local path = minetest.get_modpath(minetest.get_current_modname()) local path = minetest.get_modpath(minetest.get_current_modname())
local t400 = dofile(path.."/t400.lua")
local oldprint=print local oldprint=print
local function print(...) local function print(...)
if SHUTUP then return end if SHUTUP then return end
@ -31,51 +32,34 @@ local function push(pos,val)
end end
meta:set_string("stack",sstring) meta:set_string("stack",sstring)
end end
local function pop(pos)
local meta = minetest.get_meta(pos)
local sstring = ""
local sstab = string.split(meta:get_string("stack"),"\n")
local sval = table.remove(sstab,#sstab)
for k,v in ipairs(sstab) do
sstring = sstring .. v .. "\n"
end
meta:set_string("stack",sstring)
if not sval then sval = 0 end
return tonumber(sval) % 4096
end
local function t400_digiline_receive(pos, node, channel, msg) function rqaddr(pos,addr,tp)
print("Digiline: "..tostring(pos.x)..","..tostring(pos.y)..","..tostring(pos.z).."; Channel: "..tostring(channel).."; Message: "..tostring(msg))
local meta = minetest.get_meta(pos) local meta = minetest.get_meta(pos)
print(meta:get_string("state").." "..meta:get_string("waitingfor")) meta:set_string("state","waiting"..tostring(tp))
if meta:get_string("state") == "waitingdat" and meta:get_string("waitingfor") == channel then meta:set_string("addr",addr)
print("got data") print("rqaddr: "..addr.." "..tp)
push(pos,tonumber(msg)) digiline:receptor_send(pos,digiline.rules.default,meta:get_string("channel")..string.format("%X",tonumber(addr)),"get")
meta:set_int("pc",meta:get_int("pc")+1)
meta:set_string("state","running")
print("finished getting data")
elseif meta:get_string("state") == "waitingins" and meta:get_string("waitingfor") == channel then
meta:set_int("ci",tonumber(msg))
meta:set_string("state","running")
end end
end
print("Created T400 digiline function")
local function t400_set_meta(pos) local function t400_set_meta(pos)
local meta = minetest.get_meta(pos) local meta = minetest.get_meta(pos)
local ms = "" local stab = {}
stab.stack,stab.rstack = {},{}
for i = 1, MEMSIZE do for i = 1, MEMSIZE do
ms = ms .. "0\n" stab.stack[#stab.stack+1] = 0
stab.rstack[#stab.stack+1] = 0
end end
meta:set_string("stack",ms) stab.cins = 0
meta:set_string("retstack",ms) stab.pc = 0
meta:set_string("channel","Default") meta:set_string("stab",minetest.serialize(stab))
meta:set_string("channel","")
meta:set_string("rw","")
meta:set_string("state","running") meta:set_string("state","running")
meta:set_string("waitingfor","")
meta:set_int("pc",0) meta:set_int("pc",0)
meta:set_int("ci",0)
meta:set_string("formspec","size[5,5]\nlabel[0.4,0.5;T400 Execution Node]\nfield[0.5,2;4,1;channel;Channel prefix;${channel}]\nfield[0.5,3;4,1;pc;Program Counter;${pc}]field[0.5,4;4,1;state;State;${state}]") meta:set_string("formspec","size[5,5]\nlabel[0.4,0.5;T400 Execution Node]\nfield[0.5,2;4,1;channel;Channel prefix;${channel}]\nfield[0.5,3;4,1;pc;Program Counter;${pc}]field[0.5,4;4,1;state;State;${state}]")
print("Set metadata for a T400 node at "..pos2string(pos))
end end
minetest.register_node("test3d_t400:t400", { minetest.register_node("test3d_t400:t400", {
description = "T400 Execution Node", description = "T400 Execution Node",
tiles = { tiles = {
@ -90,125 +74,63 @@ minetest.register_node("test3d_t400:t400", {
}, },
}, },
}, },
on_construct = t400_set_meta,
groups = {snappy=1,choppy=2,oddly_breakable_by_hand=2,flammable=3}, groups = {snappy=1,choppy=2,oddly_breakable_by_hand=2,flammable=3},
on_construct = t400_set_meta,
on_receive_fields = function(pos,_,fields,sender)
print("Received fields for a T400 node at "..pos2string(pos))
for k,v in pairs(fields) do print(tostring(k).." = "..tostring(v)) end
local meta = minetest.get_meta(pos)
if fields.channel ~= nil then
meta:set_string("channel",fields.channel)
end
if fields.pc ~= nil then
local stab = minetest.deserialize(meta:get_string("stab"))
stab.pc = tonumber(fields.pc)
meta:set_int("pc",fields.pc) -- for display only
meta:set_string("stab",minetest.serialize(stab))
end
if fields.state ~= nil then
meta:set_string("state",fields.state)
end
end,
digiline = { digiline = {
receptor = {}, receptor = {},
effector = { effector = {
action = t400_digiline_receive action = function(pos,node,channel,msg)
}
},
on_receive_fields = function(pos,_,fields,sender)
if fields.channel == nil then return end
local meta = minetest.get_meta(pos) local meta = minetest.get_meta(pos)
fields.channel = fields.channel or "" local stab = minetest.deserialize(meta:get_string("stab"))
meta:set_string("channel",fields.channel) print(meta:get_string("state").." "..meta:get_string("addr"),channel.." "..msg.." = "..meta:get_string("channel")..string.format("%X",meta:get_string("addr")))
meta:set_string("pc",fields.pc) if meta:get_string("state") == "waitingread" and meta:get_string("channel")..string.format("%X",meta:get_string("addr")) == channel then
meta:set_string("state",fields.state) push(pos,tonumber(msg)%4096)
rqaddr(pos,stab.pc,"ins")
elseif meta:get_string("state") == "waitingins" and meta:get_string("channel")..string.format("%X",meta:get_string("addr")) == channel then
stab.ins = (tonumber(msg)%4096)
meta:set_string("state","running")
end end
end
}
}
}) })
print("T400 node registered") print("T400 node registered")
minetest.register_abm({ minetest.register_abm({
nodenames={"test3d_t400:t400"}, nodenames={"test3d_t400:t400"},
interval=0.1, interval=1,
chance=1, chance=1,
action = function(pos) action = function(pos)
local meta = minetest.get_meta(pos) local meta = minetest.get_meta(pos)
local nins = 1 if meta:get_string("state") ~= "running" then return false end
if meta:get_string("state") == "running" then local stab = minetest.deserialize(meta:get_string("stab"))
local skipnext = false local stab, rw, addr, dat = t400.run(stab)
print("Attempting to run T400 node at "..pos2string(pos)) meta:set_int("pc",stab.pc)
local ci = meta:get_int("ci") meta:set_string("stab",minetest.serialize(stab))
if ci == 4095 then -- ppc print(minetest.serialize(stab))
push(pos,meta:get_int("pc")) if rw == "read" then
elseif ci == 4094 then -- swp - why do I have this? rqaddr(pos,addr,rw)
local a = pop(pos)
local b = pop(pos)
print("Swapping "..tostring(a).." for "..tostring(b))
push(pos,b)
push(pos,a)
elseif ci == 4093 then -- read
skipnext = true
local addr = pop(pos)
meta:set_string("state","waitingdat")
meta:set_string("waitingfor",meta:get_string("channel")..string.format("%X",addr))
meta:set_int("pc",meta:get_int("pc")+nins)
digiline:receptor_send(pos,digiline.rules.default,meta:get_string("channel")..string.format("%X",addr),"get")
return
elseif ci == 4092 then -- write
local addr = pop(pos)
local dat = pop(pos)
print("Writing "..tostring(dat).." to address "..addr)
digiline:receptor_send(pos,digiline.rules.default,meta:get_string("channel")..string.format("%X",addr),dat)
elseif ci == 4091 then -- add
print("Adding stuff")
push(pos,pop(pos)+pop(pos))
elseif ci == 4090 then -- sub
push(pos,pop(pos)-pop(pos))
elseif ci == 4089 then -- jmp
local npc = pop(pos)
print("Jumping to "..tostring(npc))
meta:set_int("pc",npc)
nins=0
elseif ci == 4088 then -- sez
local val = pop(pos)
print(val,type(val))
if val == 0 then
print("Skipping because "..tostring(val).." = 0")
nins=nins+1
end
elseif ci == 4087 then -- jez
local addr = pop(pos)
local val = pop(pos)
if val == 0 then
print("Jumping to "..addr.." because "..val.." = 0")
meta:set_int("pc",addr)
nins=0
end
elseif ci == 4086 then -- jsr
local sstring = ""
local sstab = string.split(meta:get_string("retstack"),"\n")
sstab[#sstab+1] = tostring(tonumber(meta:get_int("pc")) % 4096)
if #sstab > MEMSIZE then
table.remove(sstab,1)
end
for k,v in ipairs(sstab) do
sstring = sstring .. v .. "\n"
end
meta:set_string("retstack",sstring)
meta:set_int("pc",pop(pos))
nins=0
elseif ci == 4085 then -- ret
local sstring = ""
local sstab = string.split(meta:get_string("retstack"),"\n")
local sval = tonumber(table.remove(sstab,#sstab))
for k,v in ipairs(sstab) do
sstring = sstring .. v .. "\n"
end
meta:set_string("retstack",sstring)
if not sval then sval = 0 end
meta:set_int("pc",sval)
elseif ci == 4084 then -- dup
local v = pop(pos)
push(pos,v)
push(pos,v)
print("Duplicating "..v)
elseif ci == 4083 then -- drop
print("Dropping stuff")
pop(pos)
else else
print("Pushing "..ci.." to "..pos2string(pos)) if rw == "write" then
push(pos,ci) digiline:receptor_send(pos,digiline.rules.default,meta:get_string("channel")..string.format("%X",addr),dat)
end
if not skipnext then
meta:set_int("pc",meta:get_int("pc")+nins)
meta:set_string("state","waitingins")
meta:set_string("waitingfor",meta:get_string("channel")..string.format("%X",meta:get_int("pc")))
digiline:receptor_send(pos, digiline.rules.default, meta:get_string("channel")..string.format("%X",meta:get_int("pc")), "get")
end end
rqaddr(pos,stab.pc,"ins")
end end
end end
}) })
print("T400 ABM registered")
print("T400 init finished")