From d50ccc7ddee27b7caf5fb39c324ba2624ff75165 Mon Sep 17 00:00:00 2001 From: Izaya Date: Mon, 13 Mar 2017 13:36:49 +1100 Subject: [PATCH] working T408 --- {test3d_t410 => test3d_t408}/depends.txt | 0 test3d_t408/emut408.lua | 58 +++++++ test3d_t408/init.lua | 198 +++++++++++++++++++++++ test3d_t408/t408.lua | 97 +++++++++++ test3d_t408/textures/t408.png | Bin 0 -> 778 bytes test3d_t410/init.lua | 30 ---- test3d_t410/textures/t416-side.png | Bin 217 -> 0 bytes test3d_t410/textures/t416-top.png | Bin 301 -> 0 bytes 8 files changed, 353 insertions(+), 30 deletions(-) rename {test3d_t410 => test3d_t408}/depends.txt (100%) create mode 100644 test3d_t408/emut408.lua create mode 100644 test3d_t408/init.lua create mode 100644 test3d_t408/t408.lua create mode 100644 test3d_t408/textures/t408.png delete mode 100644 test3d_t410/init.lua delete mode 100755 test3d_t410/textures/t416-side.png delete mode 100755 test3d_t410/textures/t416-top.png diff --git a/test3d_t410/depends.txt b/test3d_t408/depends.txt similarity index 100% rename from test3d_t410/depends.txt rename to test3d_t408/depends.txt diff --git a/test3d_t408/emut408.lua b/test3d_t408/emut408.lua new file mode 100644 index 0000000..c34d0c7 --- /dev/null +++ b/test3d_t408/emut408.lua @@ -0,0 +1,58 @@ +stab = {} +stab.stack = {} +stab.pc = 1 +stab.cins = 0 +mem = {} +t408 = require "t408" + +while true do + io.write("*> ") + i=io.read() + it = {} + for s in i:gmatch("%S+") do it[#it+1] = s end + if it[1] == "quit" then + break + elseif it[1] == "peek" then + print(mem[tonumber(it[2])]) + elseif it[1] == "poke" then + mem[tonumber(it[2])] = tonumber(it[3]) + elseif it[1] == "push" then + stab.stack[#stab.stack+1] = tonumber(it[2]) + elseif it[1] == "dumpstack" then + for k,v in ipairs(stab.stack) do print(k,v) end + elseif it[1] == "ppc" then + print(stab.pc) + elseif it[1] == "spc" then + stab.pc=tonumber(it[2]) + elseif it[1] == "step" then + local scount = tonumber(it[2]) or 1 + for i = 1, scount do + stab.cins = mem[stab.pc] + stab,rw,addr,val=t408.run(stab) + --print(rw,addr,val) + if rw == "halt" then print("Halted at "..tostring(stab.pc) )break end + if rw == "read" then + stab.stack[#stab.stack+1] = mem[addr] + elseif rw == "write" then + mem[addr] = val + if addr == 0 then io.write(string.char(val)) end + end + end + elseif it[1] == "debug" then + print(not t408.debug) + t408.debug = not t408.debug + elseif it[1] == "load" then + local f,e = io.open(it[2]) + if f == nil then + print("Error: "..e) + else + local n = tonumber(it[3]) or 0 + local c = f:read("*a") + f:close() + for v in c:gmatch("%d+") do + mem[n] = tonumber(v) or 0 + n = n + 1 + end + end + end +end diff --git a/test3d_t408/init.lua b/test3d_t408/init.lua new file mode 100644 index 0000000..95091b3 --- /dev/null +++ b/test3d_t408/init.lua @@ -0,0 +1,198 @@ +local MEMSIZE=16 +local path = minetest.get_modpath(minetest.get_current_modname()) +local t408_mem, t408_stab = {},{} +print("Looking for memory file at "..path.."/mem.dat") +local f=io.open(path.."/mem.dat","rb") +if f ~= nil then + local c = f:read("*a") + print("Found T408 memory file.") +-- rawset(_G,"t408_mem",minetest.deserialize(c)) + t408_mem = minetest.deserialize(c) + print("Contents: "..c) + f:close() +end +print("Looking for metadata file at "..path.."/stab.dat") +local f=io.open(path.."/stab.dat","rb") +if f ~= nil then + print("Found T408 metadata file.") + local c = f:read("*a") + t408_stab = minetest.deserialize(c) + print("Contents: "..c) + f:close() +end +local t408 = dofile(path.."/t408.lua") +t408.debug = true +print(type(_G.t408_mem)) +if not digiline then + print("Digilines not found.") + return +end +local oldprint=print +local function print(...) + for k,v in ipairs({...}) do + oldprint("[Test3D:T408] "..tostring(v)) + end +end +print("Loading.") + +local function t408_digiline_receive(pos, node, channel, msg) + local meta = minetest.get_meta(pos) + local nid = minetest.pos_to_string(pos) + local prea,preb = string.find(channel,meta:get_string("channel")) + print("Start channel: "..prea,"End channel: "..preb) + print("Recieved (pre)"..tostring(msg).." from "..tostring(channel).." (device channel: "..tostring(meta:get_string("channel"))..")") + if prea == 1 then + --if string.sub(channel,1,meta:get_string("channel"):len()) == meta:get_string("channel") then +-- local addr = tonumber(msg:sub(meta:get_string("channel"):len()+1)) +-- local addr = tonumber(channel) + print("Correct channel.") + local addr = tonumber(string.sub(channel,preb+1)) + print(addr) + if addr > -1 and addr < 16 then + print("Recieved "..tostring(msg).." from "..tostring(channel).." for "..tostring(addr).." (device channel: "..tostring(meta:get_string("channel"))..")") + t408_mem[nid][addr+224] = tonumber(msg) + end + end + --end +end +print("Created T408 digiline function") + +local function t408_read(pos,addr) + if t408_mem[pos] == nil then return false end + print("Read: "..tostring(pos).." "..tostring(addr)..": "..tostring(t408_mem[pos][addr])) + return t408_mem[pos][addr] or 0 +end +local function t408_write(pos,addr,val) + print(tostring(pos).." "..tostring(addr).." "..tostring(val)) + if t408_mem[pos] == nil then return false end + t408_mem[pos][tonumber(addr)] = (tonumber(val)%256) + print("Write: "..tostring(pos).." "..tostring(addr)..": "..tostring(t408_mem[pos][addr]).." = "..tostring(val)) + return true +end + +local function t408_set_meta(pos) + local meta = minetest.get_meta(pos) + meta:set_string("channel","") + meta:set_string("state","running") + t408_mem[minetest.pos_to_string(pos)] = {} + t408_stab[minetest.pos_to_string(pos)] = {["ci"]=1,["pc"]=1,["stack"]={},["rstack"]={}} + print("Created T408 memory table with ID "..minetest.pos_to_string(pos)) +end +minetest.register_node("test3d_t408:t408", { + description = "T408 Memory Node", + tiles = {{image="t408.png",animation={type="vertical_frames",aspect_w=16,aspect_h=16,length=1.8}}}, + on_construct = t408_set_meta, + groups = {snappy=1,choppy=2,oddly_breakable_by_hand=2,flammable=3}, + digiline = { + receptor = {}, + effector = { + action = function (a,b,c,d) pcall(t408_digiline_receive,a,b,c,d) end + } + }, + on_punch = 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) + if meta:get_string("state") == "running" then + meta:set_string("state","halted") + else + meta:set_string("state","running") + end + print(meta:get_string("state")) + end +}) +minetest.register_chatcommand("test3d-t408-read", { + params = "
", + description = "Read the memory in a T408 node", + func = function(name,str) + local tArg = {} + for s in str:gmatch("%S+") do + tArg[#tArg+1] = s + end + local nid,addr = tArg[1],tArg[2] + print(nid,addr) + minetest.chat_send_player(name,tostring(t408_read(nid,tonumber(addr)))) + end +}) +minetest.register_chatcommand("test3d-t408-write", { + params = "
", + description = "Read the memory in a T408 node", + func = function(name,str) + local tArg = {} + for s in str:gmatch("%S+") do + tArg[#tArg+1] = s + end + local nid,addr,val = tArg[1],tArg[2],tArg[3] + print(nid,addr,val) + minetest.chat_send_player(name,tostring(t408_write(nid,tonumber(addr),tostring(val)))) + end +}) +minetest.register_chatcommand("test3d-t408-set", { + params = " ", + description = "Set T408 variables.", + func = function(name,str) + local tArg = {} + for s in str:gmatch("%S+") do + tArg[#tArg+1] = s + end + local nid,addr,val = tArg[1],tArg[2],tArg[3] + local meta = minetest.get_meta(minetest.string_to_pos(nid)) + meta:set_string(addr,tArg[3] or "") + print("Set "..nid.."'s "..addr.." to "..(tArg[3] or "")) + end +}) +minetest.register_chatcommand("test3d-t408-dump", { + params = "
", + description = "Read the memory in a T408 node", + func = function(name,nid,addr,val) + for k,v in ipairs(t408_mem) do + print(k,v) + end + end +}) + +print("T408 node registered") + +minetest.register_abm({ + nodenames={"test3d_t408:t408"}, + interval=1, + chance=1, + action = function(pos) + local meta = minetest.get_meta(pos) + if meta:get_string("state") == "running" then + local nid = minetest.pos_to_string(pos) + local stab,rw,addr,val = t408_stab[nid],nil,0,0 + stab.cins = t408_mem[nid][stab.pc] + stab,rw,addr,val = t408.run(stab) + if rw == "read" then + stab.stack[#stab.stack+1] = t408_mem[nid][addr] + elseif rw == "preread" then + if addr > 223 and addr < 240 then + digiline:receptor_send(pos,digiline.rules.default,meta:get_string("channel")..string.format("%X",tonumber(addr)-224),"get") + end + elseif rw == "write" then + t408_mem[nid][addr] = tonumber(val) + print("Writing "..tostring(val).." to "..tostring(addr)) + if addr > 223 and addr < 240 then + digiline:receptor_send(pos,digiline.rules.default,meta:get_string("channel")..string.format("%X",tonumber(addr)-224),tonumber(val)) + print(string.char(val)) + end + end + t408_stab[nid] = stab + end + end +}) + +minetest.register_on_shutdown(function() + local f=io.open(path.."/mem.dat","wb") + if f then + f:write(minetest.serialize(t408_mem)) + f:close() + end + local f=io.open(path.."/stab.dat","wb") + if f then + f:write(minetest.serialize(t408_stab)) + f:close() + end +end) diff --git a/test3d_t408/t408.lua b/test3d_t408/t408.lua new file mode 100644 index 0000000..893074b --- /dev/null +++ b/test3d_t408/t408.lua @@ -0,0 +1,97 @@ +local MAXADDR = 255 +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%256 + if #s > 16 then + table.remove(s,1) + end +end + +function t.run(stab,ov) + -- variables + local pc = stab.pc or 1 + local ci = stab.cins or 0 + if ov then + ci = tonumber(ov) or 0 + end + 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 == 255 then -- ppc + push(stack,pc) + elseif ci == 254 then -- swp + a=pop(stack) + b=pop(stack) + push(stack,a) + push(stack,b) + elseif ci == 253 then -- read + rw = "read" -- terrible IPC + addr = pop(stack) + elseif ci == 252 then -- write + rw = "write" + addr = pop(stack) + dat = pop(stack) + --print("writing "..tostring(dat).." to "..tostring(addr)) + elseif ci == 251 then -- add + push(stack,pop(stack)+pop(stack)) + elseif ci == 250 then -- sub + push(stack,pop(stack)+pop(stack)) + elseif ci == 249 then -- jmp + pc = pop(stack) + pci = 0 + elseif ci == 248 then -- sez + if pop(stack) == 0 then + pci = pci + 1 + end + elseif ci == 247 then -- jez + local a = pop(stack) + if pop(stack) == 0 then + pc = a + pci = 0 + end + elseif ci == 246 then -- jsr + push(rstack,pc) + pc = pop(stack) + pci = 0 + elseif ci == 245 then -- ret + pc = pop(rstack) + elseif ci == 244 then -- dup + local v = pop(stack) + push(stack,v) + push(stack,v) + elseif ci == 243 then -- drop + pop(stack) + elseif ci == 242 then -- hlt + rw = "halt" + elseif ci == 241 then -- prd + rw = "preread" + addr = pop(stack) + else + push(stack,ci) + end + -- return stuff + stab = {} + stab.pc = pc + pci -- increment pc + if stab.pc > MAXADDR then + stab.pc = 0 + end + stab.cins = 0 + stab.stack = stack + stab.rstack = rstack + if t.debug then print("t408: pc: "..tostring(stab.pc),"cins: "..tostring(ci),"tos: "..tostring(stab.stack[1]).." #s: "..tostring(#stab.stack),stab.rw,stab.addr,stab.dat) end + return stab, rw, addr, dat +end + +return t diff --git a/test3d_t408/textures/t408.png b/test3d_t408/textures/t408.png new file mode 100644 index 0000000000000000000000000000000000000000..e5bfed3840363a715966e6332eb1bec92a3d06c7 GIT binary patch literal 778 zcmV+l1NHogP)+v8?yE3yLO72OU8Q|lz<#VJq zAoSR2e^5#x#HcL!cn|pKE3M^Ua`mUYmeVeN+4jvp*TUfUmHy`;9i)0NdZ-!r^@u}{ zP(8xXBUX=e0f7fMkH#TWJ-CBZ4@HkS^hnhs3_U{iNJEc^9$Gy*2T%3T4pKer#eVM* zh90SUq@hO`d6*ScJsO8j^@ts0PJ7Ob9*iEQ5SZ$5l|A4gm>-ND`gJ{~=)ssDf*z(& zoa@m*Fh4SS2-Y8r9$GyVJs9f`L6402(K+UNgbsopg7rs652+sdvVv@ltJOo$BV+v` z=+QZ5=V=4M`h(F!uz$(bgI{LY9*C<)#{NangR%bT9CJNl2SE?T{-xyy5CVsr}2fETO+` z9)k5p#{ETx`60N!WUM~~J-GLC{YIYJ-(Il=&p%rKlF>u3{@~&M!dQO@dSvWh+Vc++ z^i!XIT-(|?Bcq34|H9~@!}>$fgRy@R^vL~rP%i}j{XE z)7O>#AtN8F7R#c2-|hj0WHUn|N}Tg^b5rw5fLsO!=c3falGGH1^30M91$R&1fE2w{ zcA&Vwr;B5V#`$EMk{1HZnhmT@OP4Pf4zVhJ=EKIucFeh*@9peB*CikQBy)9mczCqW z?^g|DKH~YX?a-2+iC5YbGnkcCwhB$2Gsozb!~>}gc7~tws&+PkYz-hgJzf1=);T3K F0RRU9J!k*` diff --git a/test3d_t410/textures/t416-top.png b/test3d_t410/textures/t416-top.png deleted file mode 100755 index 05c2d9cee1905bb88dbad48a9fab6cafc1cf8e31..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 301 zcmeAS@N?(olHy`uVBq!ia0vp^0wB!61SBU+%rFB|oCO|{#S9GG!XV7ZFl&wkP>{XE z)7O>#AtN6vhoT=x%1)q=Y-UJAiF1B#Zfaf$kjuc}T$GwvlA5AWo>`Ki;O^-gkfN8$ z4isPP>EaloaX#6muvsvIUSM| zn{JWNoD;}90o22g nc;Kwt55`HWI(%w+mZ;@3gnwUQ(jzi|0>~epu6{1-oD!M<-NI?U