From 0e269aa89414181bdd809d24f51183e77aa738ac Mon Sep 17 00:00:00 2001 From: XeonSquared Date: Wed, 19 Apr 2017 02:40:25 +1000 Subject: [PATCH] Imported stuff. --- build.cfg | 4 ++ build.lua | 128 +++++++++++++++++++++++++++++++++ modules.cfg | 7 ++ modules/applications/luash.lua | 7 ++ modules/base/footer.lua | 14 ++++ modules/base/header.lua | 11 +++ modules/bcstart.lua | 1 + modules/counter.lua | 4 ++ modules/debug/debug.lua | 10 +++ modules/debug/heartbeat.lua | 6 ++ modules/drivers/dterm.lua | 43 +++++++++++ modules/drivers/gpu.lua | 43 +++++++++++ modules/drivers/keyboard.lua | 17 +++++ modules/evout.lua | 7 ++ modules/library/net.lua | 24 +++++++ modules/library/print.lua | 10 +++ modules/net.lua | 56 +++++++++++++++ modules/pev.lua | 8 +++ modules/ping.lua | 8 +++ modules/reptest.lua | 3 + modules/simplefs.lua | 1 + testboot.lua | 5 ++ 22 files changed, 417 insertions(+) create mode 100644 build.cfg create mode 100644 build.lua create mode 100644 modules.cfg create mode 100644 modules/applications/luash.lua create mode 100644 modules/base/footer.lua create mode 100644 modules/base/header.lua create mode 100644 modules/bcstart.lua create mode 100644 modules/counter.lua create mode 100644 modules/debug/debug.lua create mode 100644 modules/debug/heartbeat.lua create mode 100644 modules/drivers/dterm.lua create mode 100644 modules/drivers/gpu.lua create mode 100644 modules/drivers/keyboard.lua create mode 100644 modules/evout.lua create mode 100644 modules/library/net.lua create mode 100644 modules/library/print.lua create mode 100644 modules/net.lua create mode 100644 modules/pev.lua create mode 100644 modules/ping.lua create mode 100644 modules/reptest.lua create mode 100644 modules/simplefs.lua create mode 100644 testboot.lua diff --git a/build.cfg b/build.cfg new file mode 100644 index 0000000..9d90baf --- /dev/null +++ b/build.cfg @@ -0,0 +1,4 @@ +optomise no +test no +log no +opath kernel.lua diff --git a/build.lua b/build.lua new file mode 100644 index 0000000..e278831 --- /dev/null +++ b/build.lua @@ -0,0 +1,128 @@ +-- Initialization +log = "" +oldprint=print +function print(...) + oldprint(...) + logline = "" + if #{...} > 0 then + if #{...} > 1 then + for k,v in ipairs({...}) do + logline = logline .. "\t" .. tostring(v) + end + else + tA = {...} + logline = tostring(tA[1]) + end + end + log = (log .. logline .. "\n"):sub(1,-1) +end +print("Initializing and reading configuration") +ts={} +tA = {...} +ss="" +cfg={} +f=io.open("build.cfg","rb") +repeat + line = f:read("*l") + if line ~= nil then + w={} + for wo in line:gmatch("%S+") do table.insert(w, wo) end + cfg[w[1]] = w[2] + end +until line == nil or line == "" +if cfg.opath == nil then cfg.opath = "kernel.lua" end +print() +-- Module list +print("Reading modules to load") +tm={} +f=io.open("modules.cfg","rb") +function nl() + return f:read("*l") +end +for line in nl do + print(" - "..line) + table.insert(tm,line) +end +f:close() +print(tostring(#tm).." modules to load.\n") + +-- Loading modules +print("Loading modules") +for k,v in ipairs(tm) do + print(" - "..v.." - modules/"..v) + f=io.open("modules/"..v,"rb") + if cfg.optomise == "yes" then + data = f:read("*a") + else + data = "--"..v.."\n"..f:read("*a") + end + table.insert(ts,data) + f:close() +end +print(tostring(#tm).." modules loaded.\n") + +-- Generate source +print("Generating source") +for k,v in pairs(ts) do + ss=ss..v + io.write(".") +end +print() + +-- Optomise for space +if cfg.optomise == "yes" then +print("Optomising source") +sl=tostring(ss:len()) +no=0 +replacements={ +{" "," "}, +{"\n ","\n"}, +{"\n\n","\n"}, +{" == ","=="}, +{" ~= ","~="}, +{" >= ",">="}, +{" <= ","<="}, +{" > ",">"}, +{" < ","<"}, +{" = ","="}, +{", ",","}, +{" %+ ","+"}, +{" %- ","-"}, +{" %/ ","/"}, +{" %* ","*"}, +{" \n","\n"}, +} +for k,v in ipairs(replacements) do + while ss:find(v[1]) ~= nil do + ss=ss:gsub(v[1],v[2]) + io.write(".") + no=no+1 + end +end +print("\nBefore: "..sl.."\nAfter: "..tostring(ss:len()).."\n"..tostring(no).." optomisations made.\n") +end + +-- Output +print("Outputting to "..cfg.opath) +f=io.open(cfg.opath,"wb") +f:write(ss) +f:close() +print("Total size: "..tostring(ss:len()).."\n") + +-- Check syntax +if cfg.test == "yes" then +print("Checking for errors...") +err={pcall(load,ss)} +if err[1] ~= true then + print(table.unpack(err)) +else + print("No errors detected by load()") +end +end + +-- Write log +if cfg.log == "yes" then +f=io.open("build.log","wb") +f:write(log) +f:close() +end diff --git a/modules.cfg b/modules.cfg new file mode 100644 index 0000000..8714666 --- /dev/null +++ b/modules.cfg @@ -0,0 +1,7 @@ +base/header.lua +drivers/dterm.lua +library/print.lua +drivers/keyboard.lua +library/net.lua +applications/luash.lua +base/footer.lua diff --git a/modules/applications/luash.lua b/modules/applications/luash.lua new file mode 100644 index 0000000..f41fbbd --- /dev/null +++ b/modules/applications/luash.lua @@ -0,0 +1,7 @@ +s("lua shell",function() + print(_VERSION) + while true do + h("display","> ") + print(pcall(load(readln()))) + end +end) diff --git a/modules/base/footer.lua b/modules/base/footer.lua new file mode 100644 index 0000000..c54d2de --- /dev/null +++ b/modules/base/footer.lua @@ -0,0 +1,14 @@ +while #tT > 0 do + ev={computer.pullSignal(p)} + for k,v in ipairs(ev) do +-- wl(tostring(v)) + end + for k,v in ipairs(tT) do + _G.cT=k + if C.status(v[2])~="dead" then + C.resume(v[2],table.unpack(ev)) + else + T.remove(tT,k) + end + end +end diff --git a/modules/base/header.lua b/modules/base/header.lua new file mode 100644 index 0000000..75a3558 --- /dev/null +++ b/modules/base/header.lua @@ -0,0 +1,11 @@ +tT,p,C,T={},1,coroutine,table +function s(n,f,e) + T.insert(tT,{n,C.create(f),(e or {})}) +end +function l() +-- return T.remove(tT[cT][3],1) +end +function h(...) + computer.pushSignal(...) + coroutine.yield() +end diff --git a/modules/bcstart.lua b/modules/bcstart.lua new file mode 100644 index 0000000..4a9c9b6 --- /dev/null +++ b/modules/bcstart.lua @@ -0,0 +1 @@ +ns(01,computer.address()) diff --git a/modules/counter.lua b/modules/counter.lua new file mode 100644 index 0000000..a6a0f6c --- /dev/null +++ b/modules/counter.lua @@ -0,0 +1,4 @@ +function count() + for i = 1, 10 do print(i) C.yield() end +end +s("counter",count) diff --git a/modules/debug/debug.lua b/modules/debug/debug.lua new file mode 100644 index 0000000..bca70b5 --- /dev/null +++ b/modules/debug/debug.lua @@ -0,0 +1,10 @@ +s("debug thread",function() + print(string.format("%." .. 2 .. "f", (computer.totalMemory()/1024)) .. "k memory total") + while true do + print(string.format("%." .. 2 .. "f", (computer.totalMemory()-computer.freeMemory())/1024).."k memory used; "..tostring(#tT).." running processes; "..tostring(p).." polling delay") + for k,v in pairs(tT) do + print(tostring(k).." - "..tostring(v[1])) + end + coroutine.yield() + end +end) diff --git a/modules/debug/heartbeat.lua b/modules/debug/heartbeat.lua new file mode 100644 index 0000000..47a6780 --- /dev/null +++ b/modules/debug/heartbeat.lua @@ -0,0 +1,6 @@ +s("hearbeat",function() + while true do + computer.beep() + C.yield() + end +end) diff --git a/modules/drivers/dterm.lua b/modules/drivers/dterm.lua new file mode 100644 index 0000000..1d0bcdb --- /dev/null +++ b/modules/drivers/dterm.lua @@ -0,0 +1,43 @@ +do +local gpu, screen = component.list("gpu")(), component.list("screen")() +local sw, sh +local cx, cy = 1, 1 +if gpu and screen then + gp = component.proxy(gpu) + gp.bind(screen) + sw, sh = gp.getResolution() + gp.setResolution(sw, sh) + gp.setBackground(0x000000) + gp.setForeground(0xFFFFFF) + gp.fill(1, 1, sw, sh, " ") +end + +local function cv() + if cx > sw then cx,cy=1,cy+1 end + if cx < 1 then cx,cy=1,cy-1 end + if cy > sh then gp.copy(1,2,sw,sh-1,0,-1) gp.fill(1,sh,sw,1," ") cx,cy=1,sh end +end + +function wl(str) + if gpu and screen then + for c in str:gmatch(".") do + if c == "\n" then cy=cy+1 cx=1 + elseif c == "\f" then cx=1 cy=1 gp.fill(1, 1, sw, sh, " ") + elseif c == "\127" then cx=cx-1 gp.set(cx,cy," ") + else gp.set(cx,cy,c) cx=cx+1 + end cv() + end + end +end +wl("GPU initialized\n") + +s("display",function() +while true do + eT = ev + if eT[1] == "display" then + wl(tostring(eT[2])) + end + C.yield() +end +end) +end diff --git a/modules/drivers/gpu.lua b/modules/drivers/gpu.lua new file mode 100644 index 0000000..5ad4663 --- /dev/null +++ b/modules/drivers/gpu.lua @@ -0,0 +1,43 @@ +do +push = h +local gpu, screen = component.list("gpu")(), component.list("screen")() +local w, h +if gpu and screen then + gp = component.proxy(gpu) + component.invoke(gpu, "bind", screen) + w, h = component.invoke(gpu, "getResolution") + gp.setResolution(w, h) + gp.setBackground(0x000000) + gp.setForeground(0xFFFFFF) + gp.fill(1, 1, w, h, " ") +end +local y = 1 +function wl(msg) + if gpu and screen and gp then + gp.set(1, y, msg) + if y == h then + gp.copy(1, 2, w, h - 1, 0, -1) + gp.fill(1, h, w, 1, " ") + else + y = y + 1 + end + end +end +wl("GPU initialized.") +s("display",function(...) + while true do + eT = ev + if table.remove(eT,1) == "display" then + for k,v in ipairs(eT) do + wl(v) + end + end + coroutine.yield() + end +end) +function sbt(line) + gp.set(1,h,tostring(line)) +end +push("display","test") +sbt("Test.") +end diff --git a/modules/drivers/keyboard.lua b/modules/drivers/keyboard.lua new file mode 100644 index 0000000..8babe95 --- /dev/null +++ b/modules/drivers/keyboard.lua @@ -0,0 +1,17 @@ +function readln() + local s="" + h("display","|") + while true do + if ev[1] == "key_down" then + if ev[3] == 13 then + h("display","\127\n") return s + elseif ev[3] == 8 then + if s:len()>0 then s=s:sub(1,-2) h("display","\127\127|") end + elseif ev[3] > 31 and ev[3] < 127 then + s=s..string.char(ev[3]) h("display","\127"..string.char(ev[3]).."|") + end + end + C.yield() + end +end + diff --git a/modules/evout.lua b/modules/evout.lua new file mode 100644 index 0000000..441b009 --- /dev/null +++ b/modules/evout.lua @@ -0,0 +1,7 @@ +s("evp",function() + local evs={l()} + if evs ~= nil then + ns(T.unpack(evs)) + end + C.yield() +end) diff --git a/modules/library/net.lua b/modules/library/net.lua new file mode 100644 index 0000000..25fa223 --- /dev/null +++ b/modules/library/net.lua @@ -0,0 +1,24 @@ +tM,nP,nID = {}, 4096, 1 +for a,t in component.list("modem") do + table.insert(tM,component.proxy(a)) + component.proxy(a).open(nP) +end +function ns(id,po,msg) + h("sendmsg",id,po,msg) +end +s("network daemon",function () + print("Network daemon starting.\nNetwork ID: "..tostring(nID)) + while true do + if ev[1] == "sendmsg" then + local eT = ev + for k,v in ipairs(tM) do + v.broadcast(nP,eT[2],nID,eT[3],eT[4]) + end + elseif ev[1] == "modem_message" then + if ev[6] == nID then + h("net_msg",ev[7],ev[8],ev[9]) + end + end + C.yield() + end +end) diff --git a/modules/library/print.lua b/modules/library/print.lua new file mode 100644 index 0000000..fc167bf --- /dev/null +++ b/modules/library/print.lua @@ -0,0 +1,10 @@ +function print(...) + for k,v in pairs({...}) do + h("display",tostring(v).."\n") + end +end +function write(...) + for k,v in pairs({...}) do + h("display",tostring(v)) + end +end diff --git a/modules/net.lua b/modules/net.lua new file mode 100644 index 0000000..9245d5a --- /dev/null +++ b/modules/net.lua @@ -0,0 +1,56 @@ +tMn, tMs, tTs, CT, CO = {},{},{},component,computer +for m in CT.list("modem") do + T.insert(tMs,CT.proxy(m)) + tMs[#tMs].open(4096) +end +for m in CT.list("tunnel") do + T.insert(tTs,CT.proxy(m)) + tTs[#tTs].open(4096) +end + +function ncs(n) + for k,v in ipairs(tMn) do + if v == n then return true end + end +end +function grn() + repeat + mn=math.random(-65536,65535) + until not ncs(mn) + return mn +end +function rns(...) + for k,m in ipairs(tMs) do + m.broadcast(4096,...) + end + for m in ipairs(tTs) do + m.send(4096,...) + end +end +function ns(to,msg) + if msg:len() > 4096 then + msg=msg:sub(1,4096) + end + rns(grn(),to,CO.address(),msg) +end +function netrecv() + CO.beep() + while true do + evtype,_,_,_,_,mn,to,from,msg=l() + if evtype == "modem_message" then + ns("Debug 1",mn,to,from,msg) + if not ncs(mn) then + if to == CO.address() then + h("net_msg",from,msg) + CO.beep() + else + rns(mn,to,from,msg) + end + end + end + C.yield() + end +end +s("netrecv",function() + while true do ns(pcall(netrecv)) end +end) diff --git a/modules/pev.lua b/modules/pev.lua new file mode 100644 index 0000000..fd1bc80 --- /dev/null +++ b/modules/pev.lua @@ -0,0 +1,8 @@ +function pev() + for i = 1, 10 do + te=l() + print(T.unpack(te)) + C.yield() + end +end +s("evprint",pev) diff --git a/modules/ping.lua b/modules/ping.lua new file mode 100644 index 0000000..bb29b6e --- /dev/null +++ b/modules/ping.lua @@ -0,0 +1,8 @@ +s("pingd",function() + while true do + local evt={l()} + if evt[1] == "net_msg" then + ns(evt[2],CO.address,"Ping!") + end + end +end) diff --git a/modules/reptest.lua b/modules/reptest.lua new file mode 100644 index 0000000..b288b9a --- /dev/null +++ b/modules/reptest.lua @@ -0,0 +1,3 @@ +--[[this is test 1]] +--[[this is test 2]] +this is not test 3 --[[this is test 3]] this is also not test 3 diff --git a/modules/simplefs.lua b/modules/simplefs.lua new file mode 100644 index 0000000..e41287f --- /dev/null +++ b/modules/simplefs.lua @@ -0,0 +1 @@ +fs=component.proxy(computer.tmpAddress()) diff --git a/testboot.lua b/testboot.lua new file mode 100644 index 0000000..d353e34 --- /dev/null +++ b/testboot.lua @@ -0,0 +1,5 @@ +local os = require "os" +local computer = require "computer" +os.execute("cp /home/kbuild/kernel.lua /tmp/init.lua") +computer.setBootAddress(computer.tmpAddress()) +computer.shutdown(true)