This commit is contained in:
abbie 2023-07-08 10:14:47 +00:00
parent 58106ec5b8
commit 96461a9929

168
bios.lua Normal file
View file

@ -0,0 +1,168 @@
term.clear()
term.setBackgroundColour(32768)
term.setTextColor(1)
term.setCursorPos(1,1)
_G._FEATHER_VERSION = "0.1.0"
function write(out)
bx,by = term.getSize()
cx,cy = term.getCursorPos()
i = 1
l = string.len(out) + 1
local function lnbreak()
cx,cy = term.getCursorPos()
term.setCursorPos(1, cy + 1)
end
while l > i do
if string.char(string.byte(out, i)) .. string.char(string.byte(out, i + 1)) == "\n" then
lnbreak()
i = i + 1
else
term.write(string.char(string.byte(out, i)))
end
cx,cy = term.getCursorPos()
if cx > bx then
lnbreak()
cx,cy = term.getCursorPos()
end
if cy > by then
term.scroll(1)
term.setCursorPos(1, by)
cx,cy = term.getCursorPos()
end
i = i + 1
end
return
end
function print(out)
write(out .. "\n")
return
end
print("FeatherBIOS " .. _G._FEATHER_VERSION)
print("LDR: print now available")
-- Before you ask why I don't implement os.pullEventRaw
-- that is because it literally is just an alias to
-- coroutine.yield even in the official BIOS.
--
-- Hence just use coroutine.yield(filter) as it will function
-- identically to os.pullEventRaw(filter). os.pullEvent
-- is implemented here basically the same way.
function totab(...)
local x = {}
for i,v in ipairs(arg) do
table.insert(x,v)
end
return x
end
function os.pullEvent(filter)
local data = totab(coroutine.yield(filter))
if data[1] == "terminate" then
error("Terminated")
end
return data
end
print("LDR: os.pullEvent now available")
function read()
local x = ""
local done = false
while not done do
local data = os.pullEvent()
if data[1] == "char" then
write(data[2])
x = x .. data[2]
elseif data[1] == "key" then
if data[2] == 28 then -- Enter key
done = true
write("\n")
elseif data[2] == 14 and #x ~= 0 then -- Backspace key
cx,cy = term.getCursorPos()
bx,by = term.getSize()
if cx == 1 then
term.setCursorPos(bx, cy - 1)
else
term.setCursorPos(cx - 1, cy)
end
write(" ")
if cx == 1 then
term.setCursorPos(bx, cy - 1)
else
term.setCursorPos(cx - 1, cy)
end
x = x:sub(1, -2)
elseif data[2] == 15 then -- Tab key
write(" ")
x = x .. " "
end
end
end
return x
end
print("LDR: read now available")
-- Backport Lua 5.2/5.3 features to Lua 5.1
if _VERSION == "Lua 5.1" then
local nload = load
local nloadstring = loadstring
function load(inp, chunkname, env)
if type(inp) == "function" then
local func, err = nload(inp, chunkname)
if func then
if env then
env._ENV = env
setfenv(func, env)
end
return func
else
return nil, err
end
elseif type(inp) == "string" then
local func, err = nloadstring(inp, chunkname)
if func then
if env then
env._ENV = env
setfenv(func, env)
end
return func
else
return nil, err
end
end
end
end
function loadfile(filename, env)
local file = fs.open(filename, "r")
if not file then return nil end
local func, err = load(file.readAll(), "@" .. filename, env)
file.close()
return func, err
end
function dofile(filename)
local file, e = loadfile(filename, _G)
if file then
return file()
else
error(e)
end
end
print("LDR: dofile now available")
print("LDR: Booting...")
dofile("/boot.lua")