cleaned up some stuff, just the T408 right now

This commit is contained in:
Izaya 2017-03-13 13:37:30 +11:00
parent d50ccc7dde
commit bc92154936
8 changed files with 0 additions and 293 deletions

View File

@ -1,2 +0,0 @@
default
digilines

View File

@ -1,136 +0,0 @@
local MEMSIZE=16 -- does this still apply?
local SHUTUP = false
if not digiline then
print("Digilines not found.")
return
end
local path = minetest.get_modpath(minetest.get_current_modname())
local t400 = dofile(path.."/t400.lua")
local oldprint=print
local function print(...)
if SHUTUP then return end
for k,v in ipairs({...}) do
oldprint("[Test3D:T400] "..tostring(v))
end
end
print("Loading.")
local function pos2string(pos)
return tostring(pos.x)..","..tostring(pos.y)..","..tostring(pos.z)
end
local function push(pos,val)
local meta = minetest.get_meta(pos)
local sstring = ""
local sstab = string.split(meta:get_string("stack"),"\n")
sstab[#sstab+1] = tostring(tonumber(val) % 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("stack",sstring)
end
function rqaddr(pos,addr,tp)
local meta = minetest.get_meta(pos)
meta:set_string("state","waiting"..tostring(tp))
meta:set_string("addr",addr)
print("rqaddr: "..addr.." "..tp)
digiline:receptor_send(pos,digiline.rules.default,meta:get_string("channel")..string.format("%X",tonumber(addr)),"get")
end
local function t400_set_meta(pos)
local meta = minetest.get_meta(pos)
local stab = {}
stab.stack,stab.rstack = {},{}
for i = 1, MEMSIZE do
stab.stack[#stab.stack+1] = 0
stab.rstack[#stab.stack+1] = 0
end
stab.cins = 0
stab.pc = 0
meta:set_string("stab",minetest.serialize(stab))
meta:set_string("channel","")
meta:set_string("rw","")
meta:set_string("state","running")
meta:set_int("pc",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}]")
print("Set metadata for a T400 node at "..pos2string(pos))
end
minetest.register_node("test3d_t400:t400", {
description = "T400 Execution Node",
tiles = {
{
image="t400.png",
animation={
type = "vertical_frames",
aspect_w=16,
aspect_h=16,
length=18,
length=1.8,
},
},
},
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 = {
receptor = {},
effector = {
action = function(pos,node,channel,msg)
local meta = minetest.get_meta(pos)
local stab = minetest.deserialize(meta:get_string("stab"))
print(meta:get_string("state").." "..meta:get_string("addr"),channel.." "..msg.." = "..meta:get_string("channel")..string.format("%X",meta:get_string("addr")))
if meta:get_string("state") == "waitingread" and meta:get_string("channel")..string.format("%X",meta:get_string("addr")) == channel then
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
}
}
})
print("T400 node registered")
minetest.register_abm({
nodenames={"test3d_t400:t400"},
interval=1,
chance=1,
action = function(pos)
local meta = minetest.get_meta(pos)
if meta:get_string("state") ~= "running" then return false end
local stab = minetest.deserialize(meta:get_string("stab"))
local stab, rw, addr, dat = t400.run(stab)
meta:set_int("pc",stab.pc)
meta:set_string("stab",minetest.serialize(stab))
print(minetest.serialize(stab))
if rw == "read" then
rqaddr(pos,addr,rw)
else
if rw == "write" then
digiline:receptor_send(pos,digiline.rules.default,meta:get_string("channel")..string.format("%X",addr),dat)
end
rqaddr(pos,stab.pc,"ins")
end
end
})

View File

@ -1,85 +0,0 @@
local t = {}
-- internal state
--- program counter (pc)
--- current instruction (ci) -- only passed from the environment to the emulator
--- stack (stack)
local function pop(s)
return table.remove(s,#s) or 0
end
local function push(s,v)
v=v or 0
s[#s+1] = v%4096
if #s > 16 then
table.remove(s,1)
end
end
function t.run(stab)
-- variables
local pc = stab.pc or 1
local ci = stab.cins or 0
local stack = stab.stack or {}
local rstack = stab.rstack or {}
local pci = 1
local rw = nil
local addr = 0
local dat = 0
-- instruction processing
if ci == 4095 then -- ppc
push(stack,pc)
elseif ci == 4094 then -- swp
a=pop(stack)
b=pop(stack)
push(stack,a)
pusk(stack,b)
elseif ci == 4093 then -- read
rw = "read" -- terrible IPC
addr = pop(stack)
elseif ci == 4092 then -- write
rw = "write"
addr = pop(stack)
dat = pop(stack)
elseif ci == 4091 then -- add
push(stack,pop(stack)+pop(stack))
elseif ci == 4090 then -- sub
push(stack,pop(stack)+pop(stack))
elseif ci == 4089 then -- jmp
pc = pop(stack)
pci = 0
elseif ci == 4088 then -- sez
if pop(stack) == 0 then
pci = pci + 1
end
elseif ci == 4087 then -- jez
local a = pop(stack)
if pop(stack) == 0 then
pc = a
pci = 0
end
elseif ci == 4086 then -- jsr
push(rstack,pc)
pc = pop(stack)
pci = 0
elseif ci == 4085 then -- ret
pc = pop(rstack)
elseif ci == 4084 then -- dup
local v = pop(stack)
push(stack,v)
push(stack,v)
elseif ci == 4083 then -- drop
pop(stack)
elseif ci == 4082 then -- hlt
rw = "halt"
else
push(stack,ci)
end
-- return stuff
stab = {}
stab.pc = pc + pci -- increment pc
stab.cins = 0
stab.stack = stack
stab.rstack = rstack
return stab, rw, addr, dat
end
return t

Binary file not shown.

Before

Width:  |  Height:  |  Size: 778 B

View File

@ -1,2 +0,0 @@
default
digilines

View File

@ -1,68 +0,0 @@
local MEMSIZE=16
if not digiline then
print("Digilines not found.")
return
end
local oldprint=print
local function print(...)
for k,v in ipairs({...}) do
oldprint("[Test3D:T416] "..tostring(v))
end
end
print("Loading.")
local function t416_digiline_receive(pos, node, channel, msg)
--print("Digiline: "..tostring(pos.x)..","..tostring(pos.y)..","..tostring(pos.z).."; Channel: "..tostring(channel).."; Message: "..tostring(msg))
local meta = minetest.get_meta(pos)
local mem = string.split(meta:get_string("mem"),"\n")
local ms = ""
local chan = meta:get_string("channel")
local memoffset = meta:get_int("startaddr")
if channel:sub(1,#chan) == chan and tonumber(channel:sub(#chan+1),16)+1-memoffset <= MEMSIZE and tonumber(channel:sub(#chan+1),16)+1-memoffset > 0 then
local memaddr = tonumber(channel:sub(#chan+1),16)+1-memoffset
if tostring(msg):lower() == "get" then
digiline:receptor_send(pos, digiline.rules.default, channel, tonumber(mem[memaddr] or 0))
print("Reading address "..tostring(memaddr)..": "..tostring(mem[memaddr]))
else
mem[memaddr] = tonumber(msg) or 0
for k,v in ipairs(mem) do
ms = ms .. tostring(v) .. "\n"
end
meta:set_string("mem",ms)
print("Set address "..tostring(memaddr).." to "..tostring(tonumber(msg)))
end
end
end
print("Created T416 digiline function")
local function t416_set_meta(pos)
local meta = minetest.get_meta(pos)
local ms = ""
for i = 1, MEMSIZE do
ms = ms .. "0\n"
end
meta:set_string("mem",ms)
meta:set_string("channel","Default")
meta:set_int("startaddr",0)
meta:set_string("formspec","size[5,12]\nlabel[0.4,0.5;T416 Memory Node (16 words)]\nfield[0.5,2;4,1;channel;Channel prefix;${channel}]\nfield[0.5,3;4,1;startaddr;Starting address;${startaddr}]\ntextarea[0.5,4;4,8;mem;Memory contents;${mem}]")
end
minetest.register_node("test3d_t416:t416", {
description = "T416 Memory Node",
tiles = {"t416-top.png","t416-top.png","t416-side.png","t416-side.png","t416-side.png","t416-side.png"},
on_construct = t416_set_meta,
groups = {snappy=1,choppy=2,oddly_breakable_by_hand=2,flammable=3},
digiline = {
receptor = {},
effector = {
action = function (a,b,c,d) pcall(t416_digiline_receive,a,b,c,d) end
}
},
on_receive_fields = function(pos,_,fields,sender)
if fields.channel == nil then return end
local meta = minetest.get_meta(pos)
fields.channel = fields.channel or ""
meta:set_string("channel",fields.channel)
meta:set_int("startaddr",tonumber(fields.startaddr))
end
})
print("T416 node registered")

Binary file not shown.

Before

Width:  |  Height:  |  Size: 217 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 301 B