NC firmware update (thanks spsf). opencomputers now uses mutiple gpus to speed up rendering and now has net drivers. currently working on kernel memory protections to prevent malicious kernel drivers. spm does not work on opencomputers, this is a known issue i am working on fixing it :D sorry for the slow development.
This commit is contained in:
1 parent
fbaafc7cff
commit
ef4b856ee6
89 files changed
+3955
-563
No files matched your search
@@ -0,0 +1,2 @@
|
||||
Hyperion
|
||||
Spsf
|
||||
@@ -0,0 +1,691 @@
|
||||
--:Minify:--
|
||||
local hostScreen = screen
|
||||
local hostChip = chip
|
||||
local hostYield = coroutine.yield
|
||||
|
||||
local function throwError(msg)
|
||||
local okS, sw = pcall(hostScreen.getSize)
|
||||
if okS then
|
||||
pcall(hostScreen.set, 170, 0, 0)
|
||||
pcall(hostScreen.draw)
|
||||
end
|
||||
pcall(hostChip.crash, "Hyperion NC boot failure: " .. tostring(msg))
|
||||
while true do hostYield() end
|
||||
end
|
||||
|
||||
local ok, err = xpcall(function()
|
||||
|
||||
local apis = {}
|
||||
local keep = {
|
||||
coroutine = true, debug = true, _VERSION = true, assert = true,
|
||||
error = true, getmetatable = true, ipairs = true, load = true,
|
||||
math = true, next = true, pairs = true, pcall = true,
|
||||
rawequal = true, rawget = true, rawlen = true, rawset = true,
|
||||
select = true, setmetatable = true, string = true, table = true,
|
||||
tonumber = true, tostring = true, type = true, xpcall = true,
|
||||
utf8 = true, _G = true
|
||||
}
|
||||
local moveKeys = {}
|
||||
for k in pairs(_G) do
|
||||
if not keep[k] then moveKeys[#moveKeys + 1] = k end
|
||||
end
|
||||
for i = 1, #moveKeys do
|
||||
local k = moveKeys[i]
|
||||
apis[k] = _G[k]
|
||||
_G[k] = nil
|
||||
end
|
||||
|
||||
local fsChip = apis.chip
|
||||
local fsEvent = apis.event
|
||||
local fsFiles = apis.files
|
||||
local fsScreen = apis.screen
|
||||
if not fsChip or not fsEvent or not fsFiles or not fsScreen then
|
||||
throwError("missing NeetComputers APIs (need screen/event/files/chip)")
|
||||
end
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
local dbgSethook = debug.sethook
|
||||
|
||||
|
||||
|
||||
function sleep(t)
|
||||
t = tonumber(t) or 0
|
||||
if t <= 0 then coroutine.yield() return end
|
||||
local target = fsChip.getUnixTime() + t
|
||||
while fsChip.getUnixTime() < target do coroutine.yield() end
|
||||
end
|
||||
|
||||
local bootUnix = fsChip.getUnixTime()
|
||||
|
||||
|
||||
local function fmtDate(t)
|
||||
t = tonumber(t) or 0
|
||||
local days = math.floor(t / 86400)
|
||||
local rem = math.floor(t - days * 86400)
|
||||
if rem < 0 then rem = 0 end
|
||||
local z = days + 719468
|
||||
local era = math.floor(z / 146097)
|
||||
local doe = z - era * 146097
|
||||
local yoe = math.floor((doe - math.floor(doe / 1460)
|
||||
+ math.floor(doe / 36524) - math.floor(doe / 146096)) / 365)
|
||||
local y = yoe + era * 400
|
||||
local doy = doe - (365 * yoe + math.floor(yoe / 4) - math.floor(yoe / 100))
|
||||
local mp = math.floor((5 * doy + 2) / 153)
|
||||
local d = doy - math.floor((153 * mp + 2) / 5) + 1
|
||||
local m = mp + 3
|
||||
if m > 12 then m = m - 12 end
|
||||
if m <= 2 then y = y + 1 end
|
||||
local hh = math.floor(rem / 3600)
|
||||
local mi = math.floor((rem - hh * 3600) / 60)
|
||||
local ss = rem - hh * 3600 - mi * 60
|
||||
return string.format("%04d-%02d-%02dT%02d:%02d:%02dZ", y, m, d, hh, mi, ss)
|
||||
end
|
||||
|
||||
|
||||
local function normPartList(raw)
|
||||
if type(raw) ~= "table" then return {} end
|
||||
if type(raw[1]) == "table" and raw[1].name == nil and #raw == 1 then
|
||||
raw = raw[1]
|
||||
end
|
||||
local out = {}
|
||||
for _, p in ipairs(raw) do
|
||||
local name = type(p) == "table" and p.name or p
|
||||
if type(name) == "string" and name ~= "" then
|
||||
out[#out + 1] = name
|
||||
end
|
||||
end
|
||||
return out
|
||||
end
|
||||
|
||||
local function diskSlots()
|
||||
local out = {}
|
||||
local okD, raw = pcall(fsFiles.getDisks)
|
||||
if okD and type(raw) == "table" then
|
||||
for _, d in ipairs(raw) do
|
||||
local slot = type(d) == "table" and (d.slot or d[1]) or d
|
||||
if type(slot) == "number" then out[#out + 1] = slot end
|
||||
end
|
||||
end
|
||||
if #out == 0 then out = {0} end
|
||||
table.sort(out)
|
||||
return out
|
||||
end
|
||||
|
||||
local function isBootPart(slot, part)
|
||||
local okA, a = pcall(fsFiles.exists, part .. ":/kernel.lua", slot)
|
||||
if not (okA and a) then return false end
|
||||
local okB, b = pcall(fsFiles.exists, part .. ":/nc/boot.lua", slot)
|
||||
return okB and b == true
|
||||
end
|
||||
|
||||
local BOOT_DISK, BOOT_PART = nil, nil
|
||||
local slots = diskSlots()
|
||||
for _, slot in ipairs(slots) do
|
||||
local okB, bp = pcall(fsFiles.getBootPath, slot)
|
||||
if okB and type(bp) == "string" and bp ~= "" then
|
||||
local part = bp:match("^([^:]+):")
|
||||
if part and isBootPart(slot, part) then
|
||||
BOOT_DISK, BOOT_PART = slot, part
|
||||
break
|
||||
end
|
||||
end
|
||||
end
|
||||
if not BOOT_DISK then
|
||||
local okB, bp = pcall(fsFiles.getBootPath)
|
||||
if okB and type(bp) == "string" and bp ~= "" then
|
||||
local part = bp:match("^([^:]+):")
|
||||
if part then
|
||||
for _, slot in ipairs(slots) do
|
||||
if isBootPart(slot, part) then
|
||||
BOOT_DISK, BOOT_PART = slot, part
|
||||
break
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
if not BOOT_DISK then
|
||||
for _, slot in ipairs(slots) do
|
||||
local okP, raw = pcall(fsFiles.getPartitions, slot)
|
||||
if okP then
|
||||
for _, part in ipairs(normPartList(raw)) do
|
||||
if isBootPart(slot, part) then
|
||||
BOOT_DISK, BOOT_PART = slot, part
|
||||
break
|
||||
end
|
||||
end
|
||||
end
|
||||
if BOOT_DISK then break end
|
||||
end
|
||||
end
|
||||
if not BOOT_DISK then
|
||||
throwError("no bootable Hyperion partition found")
|
||||
end
|
||||
|
||||
local function getFile(path)
|
||||
local okH, h = pcall(fsFiles.open, BOOT_PART .. ":" .. path, "r", BOOT_DISK)
|
||||
if not okH or not h then
|
||||
throwError("cannot open boot file: " .. path)
|
||||
end
|
||||
local okR, data = pcall(h.read, "a")
|
||||
pcall(h.close)
|
||||
if not okR or type(data) ~= "string" then
|
||||
throwError("cannot read boot file: " .. path)
|
||||
end
|
||||
return data
|
||||
end
|
||||
|
||||
local function readBootData(path)
|
||||
local okH, h = pcall(fsFiles.open, BOOT_PART .. ":" .. path, "rb", BOOT_DISK)
|
||||
if not okH or not h then return nil end
|
||||
local okR, data = pcall(h.read, "a")
|
||||
pcall(h.close)
|
||||
if not okR or type(data) ~= "string" then return nil end
|
||||
return data
|
||||
end
|
||||
|
||||
|
||||
|
||||
local function parsePSF(data)
|
||||
if not data or #data < 4 then return nil, "too short" end
|
||||
local b1, b2 = data:byte(1, 2)
|
||||
local glyphs, gw, gh = {}, 8, 0
|
||||
if b1 == 0x36 and b2 == 0x04 then
|
||||
local mode, size = data:byte(3, 4)
|
||||
if not size or size == 0 then return nil, "bad PSF1 header" end
|
||||
gh = size
|
||||
local n = (mode % 4 >= 2) and 512 or 256
|
||||
for i = 0, n - 1 do
|
||||
local off = 4 + i * size
|
||||
if off + size > #data then break end
|
||||
glyphs[i] = data:sub(off + 1, off + size)
|
||||
end
|
||||
elseif b1 == 0x72 and b2 == 0xb5 then
|
||||
local okU, headersize, flags, num, bpg, h, w =
|
||||
pcall(string.unpack, "<I4I4I4I4I4I4I4", data, 5)
|
||||
if not okU or not w or not h or w == 0 or h == 0 then
|
||||
return nil, "bad PSF2 header"
|
||||
end
|
||||
gw, gh = w, h
|
||||
for i = 0, num - 1 do
|
||||
local off = headersize + i * bpg
|
||||
if off + bpg > #data then break end
|
||||
glyphs[i] = data:sub(off + 1, off + bpg)
|
||||
end
|
||||
else
|
||||
return nil, "not a PSF file"
|
||||
end
|
||||
return {glyphs = glyphs, w = gw, h = gh, bpr = math.floor((gw + 7) / 8)}
|
||||
end
|
||||
|
||||
local fontData = readBootData("/nc/font.psf")
|
||||
if not fontData then throwError("missing /nc/font.psf on boot partition") end
|
||||
local font = parsePSF(fontData)
|
||||
fontData = nil
|
||||
if not font then throwError("cannot parse /nc/font.psf") end
|
||||
|
||||
local okS, sw, sh = pcall(fsScreen.getSize)
|
||||
if not okS or type(sw) ~= "number" then
|
||||
throwError("screen has no usable display")
|
||||
end
|
||||
local SW, SH = sw, sh
|
||||
local CW, CHH = font.w, font.h
|
||||
local COLS = math.max(1, math.floor(SW / CW))
|
||||
local ROWS = math.max(1, math.floor(SH / CHH))
|
||||
|
||||
local function split24(c)
|
||||
c = math.floor(tonumber(c) or 0)
|
||||
if c < 0 then c = 0 end
|
||||
if c > 0xFFFFFF then c = 0xFFFFFF end
|
||||
local r = math.floor(c / 65536)
|
||||
local g = math.floor((c - r * 65536) / 256)
|
||||
local b = c - r * 65536 - g * 256
|
||||
return r, g, b
|
||||
end
|
||||
|
||||
|
||||
local glyphCache = {}
|
||||
local function glyphBuffer(code, r, g, b)
|
||||
local key = code * 16777216 + r * 65536 + g * 256 + b
|
||||
local buf = glyphCache[key]
|
||||
if buf then return buf end
|
||||
local gl = font.glyphs[code] or font.glyphs[63]
|
||||
local on = string.char(r, g, b, 255)
|
||||
local off = string.char(0, 0, 0, 255)
|
||||
|
||||
|
||||
local chunks = {}
|
||||
local n = 0
|
||||
for yy = 0, CHH - 1 do
|
||||
for xx = 0, CW - 1 do
|
||||
local byte = gl and gl:byte(yy * font.bpr + math.floor(xx / 8) + 1) or 0
|
||||
n = n + 1
|
||||
if (math.floor(byte / (2 ^ (7 - (xx % 8)))) % 2) == 1 then
|
||||
chunks[n] = on
|
||||
else
|
||||
chunks[n] = off
|
||||
end
|
||||
end
|
||||
end
|
||||
buf = table.concat(chunks)
|
||||
glyphCache[key] = buf
|
||||
return buf
|
||||
end
|
||||
|
||||
local function drawTextCell(px, py, code, fg)
|
||||
local r, g, b = split24(fg)
|
||||
local buf = glyphBuffer(code, r, g, b)
|
||||
fsScreen.writeData(px, py, buf, CW)
|
||||
end
|
||||
|
||||
local function fillCellBG(px, py, bg)
|
||||
local r, g, b = split24(bg)
|
||||
fsScreen.fill(px, py, px + CW - 1, py + CHH - 1, r, g, b, 255)
|
||||
end
|
||||
|
||||
|
||||
|
||||
local function newTerm()
|
||||
local t = {
|
||||
cols = COLS, rows = ROWS,
|
||||
cx = 1, cy = 1,
|
||||
fg = 0xDBDBAA, bg = 0x000000,
|
||||
cursor = true,
|
||||
cells = {},
|
||||
dirty = {},
|
||||
lastCurX = nil, lastCurY = nil,
|
||||
}
|
||||
for y = 1, ROWS do
|
||||
local row = {}
|
||||
for x = 1, COLS do
|
||||
row[x] = {ch = 32, fg = t.fg, bg = t.bg}
|
||||
end
|
||||
t.cells[y] = row
|
||||
end
|
||||
|
||||
local function markRow(y)
|
||||
t.dirty[y] = true
|
||||
end
|
||||
|
||||
local function paintCell(x, y)
|
||||
local cell = t.cells[y][x]
|
||||
local px = (x - 1) * CW
|
||||
local py = (y - 1) * CHH
|
||||
fillCellBG(px, py, cell.bg)
|
||||
if cell.ch ~= 32 then
|
||||
drawTextCell(px, py, cell.ch, cell.fg)
|
||||
end
|
||||
end
|
||||
|
||||
local function hideCursor()
|
||||
if t.lastCurX then
|
||||
local x, y = t.lastCurX, t.lastCurY
|
||||
t.lastCurX, t.lastCurY = nil, nil
|
||||
if t.cells[y] and t.cells[y][x] then paintCell(x, y) end
|
||||
end
|
||||
end
|
||||
|
||||
local function scrollUp()
|
||||
hideCursor()
|
||||
if CHH < SH then
|
||||
local data = fsScreen.readData(0, CHH, SW - 1, SH - 1)
|
||||
fsScreen.writeData(0, 0, data, SW)
|
||||
end
|
||||
local top = table.remove(t.cells, 1)
|
||||
for x = 1, COLS do
|
||||
top[x].ch, top[x].fg, top[x].bg = 32, t.fg, t.bg
|
||||
end
|
||||
t.cells[ROWS] = top
|
||||
|
||||
|
||||
local shifted = {}
|
||||
for y in pairs(t.dirty) do
|
||||
if y > 1 then shifted[y - 1] = true end
|
||||
end
|
||||
shifted[ROWS] = true
|
||||
t.dirty = shifted
|
||||
end
|
||||
|
||||
function t.present()
|
||||
hideCursor()
|
||||
for y in pairs(t.dirty) do
|
||||
local row = t.cells[y]
|
||||
if row then
|
||||
for x = 1, COLS do paintCell(x, y) end
|
||||
end
|
||||
end
|
||||
t.dirty = {}
|
||||
if t.cursor then
|
||||
local px = (t.cx - 1) * CW
|
||||
local py = (t.cy - 1) * CHH
|
||||
local r, g, b = split24(t.fg)
|
||||
fsScreen.fill(px, py + CHH - 2, px + CW - 1, py + CHH - 1, r, g, b, 255)
|
||||
t.lastCurX, t.lastCurY = t.cx, t.cy
|
||||
end
|
||||
fsScreen.draw()
|
||||
end
|
||||
|
||||
function t.write(s)
|
||||
hideCursor()
|
||||
s = tostring(s)
|
||||
for i = 1, #s do
|
||||
local byte = s:byte(i)
|
||||
if byte == 10 then
|
||||
t.cx = 1
|
||||
t.cy = t.cy + 1
|
||||
elseif byte == 9 then
|
||||
t.cx = t.cx + (4 - ((t.cx - 1) % 4))
|
||||
elseif byte == 8 then
|
||||
if t.cx > 1 then
|
||||
t.cx = t.cx - 1
|
||||
local cell = t.cells[t.cy][t.cx]
|
||||
cell.ch, cell.fg, cell.bg = 32, t.fg, t.bg
|
||||
markRow(t.cy)
|
||||
end
|
||||
else
|
||||
if t.cx > COLS then
|
||||
t.cx = 1
|
||||
t.cy = t.cy + 1
|
||||
end
|
||||
if t.cy > ROWS then
|
||||
scrollUp()
|
||||
t.cy = ROWS
|
||||
end
|
||||
local cell = t.cells[t.cy][t.cx]
|
||||
cell.ch, cell.fg, cell.bg = byte, t.fg, t.bg
|
||||
markRow(t.cy)
|
||||
t.cx = t.cx + 1
|
||||
end
|
||||
if t.cx > COLS + 1 then t.cx = COLS + 1 end
|
||||
if t.cy > ROWS then
|
||||
scrollUp()
|
||||
t.cy = ROWS
|
||||
end
|
||||
end
|
||||
t.present()
|
||||
end
|
||||
|
||||
function t.clear()
|
||||
for y = 1, ROWS do
|
||||
local row = t.cells[y]
|
||||
for x = 1, COLS do
|
||||
row[x].ch, row[x].fg, row[x].bg = 32, t.fg, t.bg
|
||||
end
|
||||
end
|
||||
t.dirty = {}
|
||||
t.cx, t.cy = 1, 1
|
||||
hideCursor()
|
||||
local r, g, b = split24(t.bg)
|
||||
fsScreen.fill(0, 0, SW - 1, SH - 1, r, g, b, 255)
|
||||
t.present()
|
||||
end
|
||||
|
||||
function t.getSize()
|
||||
return t.cols, t.rows
|
||||
end
|
||||
|
||||
function t.getCursor()
|
||||
return t.cx, t.cy
|
||||
end
|
||||
|
||||
function t.setCursor(x, y)
|
||||
x, y = tonumber(x), tonumber(y)
|
||||
if not x or not y then error("Invalid arg") end
|
||||
if x < 1 then x = 1 end
|
||||
if y < 1 then y = 1 end
|
||||
if x > COLS then x = COLS end
|
||||
if y > ROWS then y = ROWS end
|
||||
hideCursor()
|
||||
t.cx, t.cy = x, y
|
||||
t.present()
|
||||
end
|
||||
|
||||
function t.setFg(c) t.fg = math.floor(tonumber(c) or 0) end
|
||||
function t.setBg(c) t.bg = math.floor(tonumber(c) or 0) end
|
||||
function t.getFg() return t.fg end
|
||||
function t.getBg() return t.bg end
|
||||
function t.setCursorVisible(v)
|
||||
t.cursor = v and true or false
|
||||
t.present()
|
||||
end
|
||||
|
||||
return t
|
||||
end
|
||||
|
||||
local ncGfx = {
|
||||
screenW = SW, screenH = SH,
|
||||
cellW = CW, cellH = CHH,
|
||||
cols = COLS, rows = ROWS,
|
||||
newTerm = newTerm,
|
||||
}
|
||||
|
||||
local con = newTerm()
|
||||
con.setCursorVisible(false)
|
||||
|
||||
local function readBootFile(name)
|
||||
local okH, h = pcall(fsFiles.open, BOOT_PART .. ":/nc/" .. name, "r", BOOT_DISK)
|
||||
if not okH or not h then return "" end
|
||||
local okR, data = pcall(h.read, "a")
|
||||
pcall(h.close)
|
||||
if not okR or type(data) ~= "string" then return "" end
|
||||
return data
|
||||
end
|
||||
|
||||
local function writeBootFile(name, text)
|
||||
local okH, h = pcall(fsFiles.open, BOOT_PART .. ":/nc/" .. name, "w", BOOT_DISK)
|
||||
if not okH or not h then return end
|
||||
pcall(h.write, tostring(text))
|
||||
pcall(h.flush)
|
||||
pcall(h.close)
|
||||
end
|
||||
|
||||
local eventQueue = {}
|
||||
local function queueEvent(ev, ...)
|
||||
if #eventQueue >= 512 then table.remove(eventQueue, 1) end
|
||||
eventQueue[#eventQueue + 1] = {ev, ...}
|
||||
end
|
||||
|
||||
local EFI = {
|
||||
getEpochMs = function() return math.floor(fsChip.getUnixTime() * 1000) end,
|
||||
getUptime = function() return math.floor((fsChip.getUnixTime() - bootUnix) * 1000) end,
|
||||
date = function() return fmtDate(fsChip.getUnixTime()) end,
|
||||
getMachineEvent = function()
|
||||
if #eventQueue > 0 then
|
||||
return table.unpack(table.remove(eventQueue, 1))
|
||||
else
|
||||
return nil
|
||||
end
|
||||
end,
|
||||
getEEPROM = function() return readBootFile("eeprom.dat") end,
|
||||
setEEPROM = function(_, text) writeBootFile("eeprom.dat", text) end,
|
||||
getNvram = function() return readBootFile("nvram.dat") end,
|
||||
setNvram = function(_, text) writeBootFile("nvram.dat", text) end,
|
||||
beep = function() end,
|
||||
architecture = "nc",
|
||||
firmware = apis,
|
||||
status = "running",
|
||||
nc = ncGfx,
|
||||
yield = function() coroutine.yield() end,
|
||||
screenCtl = {
|
||||
print = function(_, text) con.write(tostring(text) .. "\n") end,
|
||||
printInline = function(_, text) con.write(tostring(text)) end,
|
||||
clear = function() con.clear() end,
|
||||
resetCursor = function() con.setCursor(1, 1) end,
|
||||
setBackgroundColor = function(_, c) con.setBg(c) end,
|
||||
setTextColor = function(_, c) con.setFg(c) end,
|
||||
getBackgroundColor = function() return con.getBg() end,
|
||||
getTextColor = function() return con.getFg() end,
|
||||
enable = function() con.setCursorVisible(true) end,
|
||||
disable = function() con.setCursorVisible(false) end,
|
||||
},
|
||||
}
|
||||
|
||||
|
||||
|
||||
local sliceInfo = setmetatable({}, {__mode = "k"})
|
||||
|
||||
function EFI.resumeWithTimeout(co, timeout, ...)
|
||||
timeout = tonumber(timeout) or 0.05
|
||||
local start = fsChip.getUnixTime()
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
local info = sliceInfo[co]
|
||||
if info and info.result == "success" and (info.dur or 1) < 0.0002 then
|
||||
local r = {coroutine.resume(co, ...)}
|
||||
sliceInfo[co] = {result = (r[1] and "success" or "error"),
|
||||
dur = fsChip.getUnixTime() - start}
|
||||
if not r[1] then return "error", r[2] end
|
||||
return "success", table.unpack(r, 2)
|
||||
end
|
||||
if not dbgSethook then
|
||||
local r = {coroutine.resume(co, ...)}
|
||||
sliceInfo[co] = {result = (r[1] and "success" or "error"),
|
||||
dur = fsChip.getUnixTime() - start}
|
||||
if not r[1] then return "error", r[2] end
|
||||
return "success", table.unpack(r, 2)
|
||||
end
|
||||
local deadline = start + timeout
|
||||
dbgSethook(co, function()
|
||||
|
||||
|
||||
if coroutine.isyieldable() and fsChip.getUnixTime() >= deadline then
|
||||
coroutine.yield("timeout")
|
||||
end
|
||||
end, "", 1000)
|
||||
local ret = {coroutine.resume(co, ...)}
|
||||
local stop = fsChip.getUnixTime()
|
||||
if ret[1] and ret[2] == "timeout" then
|
||||
sliceInfo[co] = {result = "timeout", dur = stop - start}
|
||||
return "timeout"
|
||||
elseif ret[1] == false then
|
||||
sliceInfo[co] = {result = "error", dur = stop - start}
|
||||
return "error", ret[2]
|
||||
else
|
||||
if dbgSethook then pcall(dbgSethook, co) end
|
||||
sliceInfo[co] = {result = "success", dur = stop - start}
|
||||
return "success", table.unpack(ret, 2)
|
||||
end
|
||||
end
|
||||
|
||||
local Kernel = load(getFile("/kernel.lua"), "@Kernel")
|
||||
local initFsMod = load(getFile("/nc/initdisks"), "@Init_disks")
|
||||
local initFs = load(getFile("/initfs"), "@InitFs")
|
||||
if not Kernel then throwError("cannot parse kernel.lua") end
|
||||
if not initFsMod then throwError("cannot parse nc/initdisks") end
|
||||
if not initFs then throwError("cannot parse initfs") end
|
||||
local disks = initFsMod(apis, BOOT_DISK, BOOT_PART)
|
||||
local ifs = initFs()
|
||||
|
||||
local kernelCo = coroutine.create(function()
|
||||
local okK, errK = xpcall(Kernel, debug.traceback, EFI)
|
||||
EFI._kernelDone = true
|
||||
if not okK then
|
||||
EFI._kernelErr = errK
|
||||
EFI.status = EFI.status == "reboot" and "reboot" or "error"
|
||||
end
|
||||
end)
|
||||
|
||||
EFI.initfs = ifs
|
||||
EFI.disks = disks
|
||||
|
||||
local startTime = fsChip.getUnixTime()
|
||||
EFI.screenCtl:print("HyperionOS/NC booted from " .. tostring(BOOT_DISK)
|
||||
.. ":" .. BOOT_PART .. " in "
|
||||
.. string.format("%.2f", startTime - bootUnix) .. "s")
|
||||
|
||||
|
||||
local function pump()
|
||||
local okU, q = pcall(fsEvent.getQueue, "User")
|
||||
if okU and type(q) == "table" then
|
||||
for i = 1, #q do
|
||||
local e = q[i]
|
||||
if type(e) == "table" then
|
||||
local kind = e[1]
|
||||
if kind == "keyPressed" then
|
||||
local code, letter, mods = e[2], e[3], e[4]
|
||||
queueEvent("keyPressed", 1, code, letter, mods)
|
||||
if type(letter) == "string" and letter ~= "" then
|
||||
queueEvent("keyTyped", 1, letter)
|
||||
end
|
||||
elseif kind == "keyReleased" then
|
||||
queueEvent("keyReleased", 1, e[2], e[3], e[4])
|
||||
elseif kind == "mouseClicked" or kind == "mouseReleased"
|
||||
or kind == "mouseDragged" or kind == "mouseMoved"
|
||||
or kind == "mouseScrolled" then
|
||||
queueEvent(table.unpack(e))
|
||||
else
|
||||
queueEvent(table.unpack(e))
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
for _, label in ipairs({"System", "Network", "Peripheral",
|
||||
"Compatibility", "Unlabeled"}) do
|
||||
local okQ, qq = pcall(fsEvent.getQueue, label)
|
||||
if okQ and type(qq) == "table" then
|
||||
for i = 1, #qq do
|
||||
local e = qq[i]
|
||||
if type(e) == "table" then
|
||||
queueEvent(table.unpack(e))
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
local function waitKey()
|
||||
while true do
|
||||
pump()
|
||||
for i = 1, #eventQueue do
|
||||
local e = eventQueue[i]
|
||||
if e[1] == "keyPressed" or e[1] == "keyTyped" then
|
||||
eventQueue = {}
|
||||
return
|
||||
end
|
||||
end
|
||||
eventQueue = {}
|
||||
coroutine.yield()
|
||||
end
|
||||
end
|
||||
|
||||
while true do
|
||||
pump()
|
||||
local okR, errR = coroutine.resume(kernelCo)
|
||||
if not okR then
|
||||
con.setFg(0xFF0000)
|
||||
con.write("Hyperion kernel fault:\n" .. tostring(errR)
|
||||
.. "\nPress any key to shut down.")
|
||||
con.setFg(0xDBDBAA)
|
||||
waitKey()
|
||||
fsChip.shutdown()
|
||||
return
|
||||
end
|
||||
if coroutine.status(kernelCo) == "dead" then
|
||||
if EFI.status == "reboot" then
|
||||
fsChip.reboot()
|
||||
else
|
||||
if EFI._kernelErr then
|
||||
con.setFg(0xFF0000)
|
||||
con.write("Hyperion kernel error:\n"
|
||||
.. tostring(EFI._kernelErr)
|
||||
.. "\nPress any key to shut down.")
|
||||
con.setFg(0xDBDBAA)
|
||||
waitKey()
|
||||
end
|
||||
fsChip.shutdown()
|
||||
end
|
||||
return
|
||||
end
|
||||
coroutine.yield()
|
||||
end
|
||||
end, debug.traceback)
|
||||
|
||||
if not ok then
|
||||
throwError(err)
|
||||
end
|
||||
@@ -0,0 +1,2 @@
|
||||
|
||||
return true
|
||||
Binary file not shown.
@@ -0,0 +1,318 @@
|
||||
--:Minify:--
|
||||
-- HyperionOS NeetComputers initdisks.
|
||||
-- Adapts the NeetComputers files API (disks x partitions) to the Hyperion
|
||||
-- disk interface documented in docs/kernel/bootloaders/diskAPI.
|
||||
--
|
||||
-- Addresses look like "0:hyperion" (disk slot + partition name); the boot
|
||||
-- partition is additionally aliased as "$".
|
||||
--
|
||||
-- NeetComputers file headers are snapshots: reads see the file as it was at
|
||||
-- open time and writes only persist on flush/close (and always append). This
|
||||
-- adapter keeps a Lua-side buffer per open handle so that read/seek/write
|
||||
-- behave like regular POSIX files. Flush rewrites the whole file.
|
||||
|
||||
local args = {...}
|
||||
local apis = args[1]
|
||||
local bootDisk = args[2]
|
||||
local bootPart = args[3]
|
||||
|
||||
local files = apis.files
|
||||
|
||||
local function fp(part, path)
|
||||
if not path or path == "" then path = "/" end
|
||||
if path:sub(1, 1) ~= "/" then path = "/" .. path end
|
||||
return part .. ":" .. path
|
||||
end
|
||||
|
||||
local function normParts(raw)
|
||||
if type(raw) ~= "table" then return {} end
|
||||
-- tolerate a doubly-wrapped list
|
||||
if type(raw[1]) == "table" and raw[1].name == nil and #raw == 1 then
|
||||
raw = raw[1]
|
||||
end
|
||||
local out = {}
|
||||
for _, p in ipairs(raw) do
|
||||
local name = type(p) == "table" and p.name or p
|
||||
if type(name) == "string" and name ~= "" then
|
||||
out[#out + 1] = name
|
||||
end
|
||||
end
|
||||
return out
|
||||
end
|
||||
|
||||
local function diskSlots()
|
||||
local out = {}
|
||||
local ok, raw = pcall(files.getDisks)
|
||||
if ok and type(raw) == "table" then
|
||||
for _, d in ipairs(raw) do
|
||||
local slot = type(d) == "table" and (d.slot or d[1]) or d
|
||||
if type(slot) == "number" then
|
||||
out[#out + 1] = slot
|
||||
end
|
||||
end
|
||||
end
|
||||
if #out == 0 then out = {0} end
|
||||
table.sort(out)
|
||||
return out
|
||||
end
|
||||
|
||||
local function partReadOnly(slot, part)
|
||||
local ok, info = pcall(files.getPartition, part, slot)
|
||||
if ok and type(info) == "table" then
|
||||
return info.readonly == true
|
||||
end
|
||||
return false
|
||||
end
|
||||
|
||||
local function createDisk(id, slot, part)
|
||||
local readonly = partReadOnly(slot, part)
|
||||
local disk = {
|
||||
address = id,
|
||||
isReadOnly = function() return readonly end
|
||||
}
|
||||
|
||||
function disk:spaceUsed() return 0 end
|
||||
function disk:spaceTotal() return 2 ^ 40 end
|
||||
|
||||
function disk:list(path)
|
||||
local ok, r = pcall(files.getChildren, fp(part, path), slot)
|
||||
if not ok or type(r) ~= "table" then return nil, "not directory" end
|
||||
local out = {}
|
||||
for _, n in ipairs(r) do
|
||||
if type(n) == "string" then out[#out + 1] = n end
|
||||
end
|
||||
return out
|
||||
end
|
||||
|
||||
function disk:fileExists(path)
|
||||
local ok, r = pcall(files.exists, fp(part, path), slot)
|
||||
if not (ok and r) then return false end
|
||||
local ok2, d = pcall(files.isDir, fp(part, path), slot)
|
||||
if ok2 and d then return false end
|
||||
return true
|
||||
end
|
||||
|
||||
function disk:directoryExists(path)
|
||||
if not path or path == "" or path == "/" then return true end
|
||||
local ok, r = pcall(files.isDir, fp(part, path), slot)
|
||||
return ok and r == true
|
||||
end
|
||||
|
||||
function disk:type(path)
|
||||
local ok, r = pcall(files.exists, fp(part, path), slot)
|
||||
if not (ok and r) then return nil end
|
||||
local ok2, d = pcall(files.isDir, fp(part, path), slot)
|
||||
if ok2 and d then return "directory" end
|
||||
return "file"
|
||||
end
|
||||
|
||||
function disk:makeDirectory(path)
|
||||
local ok, r = pcall(files.makeDir, fp(part, path), slot)
|
||||
if not ok then return nil, "cannot create directory" end
|
||||
if r == false then return nil, "cannot create directory" end
|
||||
return true
|
||||
end
|
||||
|
||||
function disk:remove(path)
|
||||
local ok, r = pcall(files.delete, fp(part, path), slot)
|
||||
if not ok then return nil, "cannot delete" end
|
||||
if r == false then return nil, "cannot delete" end
|
||||
return true
|
||||
end
|
||||
|
||||
function disk:setLabel(label) return true end
|
||||
function disk:getLabel() return part end
|
||||
|
||||
function disk:attributes(path)
|
||||
local isDir = self:directoryExists(path)
|
||||
local size = 0
|
||||
if not isDir then
|
||||
local ok, h = pcall(files.open, fp(part, path), "r", slot)
|
||||
if ok and h then
|
||||
local ok2, data = pcall(h.read, "a")
|
||||
pcall(h.close)
|
||||
if ok2 and type(data) == "string" then size = #data end
|
||||
end
|
||||
end
|
||||
return {
|
||||
size = size,
|
||||
isDir = isDir,
|
||||
isReadOnly = readonly,
|
||||
modified = 0,
|
||||
created = 0
|
||||
}
|
||||
end
|
||||
|
||||
function disk:open(path, mode)
|
||||
mode = mode or "r"
|
||||
local wantWrite = mode ~= "r" and mode ~= "rb"
|
||||
|
||||
-- Snapshot the current content with a dedicated read handle.
|
||||
-- ("a"/"ab" headers are not readable themselves, and every header
|
||||
-- only appends, so all POSIX read/write/seek behavior below runs
|
||||
-- on this Lua-side buffer; persist() rewrites the file on flush.)
|
||||
local buf = ""
|
||||
if mode ~= "w" and mode ~= "wb" then
|
||||
local okR, hr = pcall(files.open, fp(part, path), "r", slot)
|
||||
if okR and hr then
|
||||
local okD, data = pcall(hr.read, "a")
|
||||
pcall(hr.close)
|
||||
if okD and type(data) == "string" then buf = data end
|
||||
end
|
||||
end
|
||||
|
||||
local ok, h = pcall(files.open, fp(part, path), mode, slot)
|
||||
if not ok or not h then
|
||||
return nil, "file not found or access denied"
|
||||
end
|
||||
-- The live header is only needed to prove the open mode is valid
|
||||
-- (existence/creation); persist() reopens the file itself.
|
||||
pcall(h.close)
|
||||
|
||||
local pos = 1
|
||||
if mode == "w" or mode == "wb" then
|
||||
buf = ""
|
||||
else
|
||||
pos = #buf + 1
|
||||
if mode == "r" or mode == "rb" or mode == "r+" or mode == "rb+"
|
||||
or mode == "r+b" or mode == "w+" or mode == "wb+"
|
||||
or mode == "w+b" then
|
||||
pos = 1
|
||||
end
|
||||
end
|
||||
|
||||
local closed = false
|
||||
local handle = {}
|
||||
|
||||
function handle.read(n)
|
||||
if closed then error("EBADF") end
|
||||
if type(n) == "number" then
|
||||
if n <= 0 then return nil end
|
||||
if pos > #buf then return nil end
|
||||
local chunk = buf:sub(pos, pos + n - 1)
|
||||
pos = pos + #chunk
|
||||
if chunk == "" then return nil end
|
||||
return chunk
|
||||
end
|
||||
local fmt = n or "l"
|
||||
if pos > #buf then return nil end
|
||||
if fmt == "a" then
|
||||
local chunk = buf:sub(pos)
|
||||
pos = #buf + 1
|
||||
return chunk
|
||||
elseif fmt == "l" or fmt == "L" then
|
||||
local s, e = buf:find("\n", pos, true)
|
||||
if not s then
|
||||
local chunk = buf:sub(pos)
|
||||
pos = #buf + 1
|
||||
return chunk
|
||||
end
|
||||
local chunk
|
||||
if fmt == "l" then chunk = buf:sub(pos, s - 1)
|
||||
else chunk = buf:sub(pos, s) end
|
||||
pos = s + 1
|
||||
return chunk
|
||||
else
|
||||
error("EINVAL")
|
||||
end
|
||||
end
|
||||
|
||||
function handle.readAll()
|
||||
if closed then error("EBADF") end
|
||||
local chunk = buf:sub(pos)
|
||||
pos = #buf + 1
|
||||
return chunk
|
||||
end
|
||||
|
||||
function handle.write(data)
|
||||
if closed then error("EBADF") end
|
||||
if not wantWrite then error("EACCES") end
|
||||
data = tostring(data)
|
||||
local before = buf:sub(1, pos - 1)
|
||||
local after = buf:sub(pos + #data)
|
||||
buf = before .. data .. after
|
||||
pos = pos + #data
|
||||
return true
|
||||
end
|
||||
|
||||
function handle.seek(whence, offset)
|
||||
if closed then error("EBADF") end
|
||||
whence = whence or "cur"
|
||||
offset = offset or 0
|
||||
local base
|
||||
if whence == "set" then base = 1
|
||||
elseif whence == "cur" then base = pos
|
||||
elseif whence == "end" then base = #buf + 1
|
||||
else error("EINVAL") end
|
||||
local np = base + offset
|
||||
if whence == "end" and offset > 0 then error("EINVAL") end
|
||||
if np < 1 then error("EINVAL") end
|
||||
if np > #buf + 1 then error("EINVAL") end
|
||||
pos = np
|
||||
return pos - 1
|
||||
end
|
||||
|
||||
local function persist()
|
||||
if not wantWrite then return true end
|
||||
local ok2, h2 = pcall(files.open, fp(part, path), "w", slot)
|
||||
if not ok2 or not h2 then return nil, "flush failed" end
|
||||
if #buf > 0 then
|
||||
local ok3, err = pcall(h2.write, buf)
|
||||
if not ok3 then pcall(h2.close) return nil, err end
|
||||
end
|
||||
local ok4, err = pcall(h2.flush)
|
||||
pcall(h2.close)
|
||||
if not ok4 then return nil, err end
|
||||
return true
|
||||
end
|
||||
|
||||
function handle.flush()
|
||||
if closed then error("EBADF") end
|
||||
local ok2, err = persist()
|
||||
if not ok2 then error(err or "EIO") end
|
||||
return true
|
||||
end
|
||||
|
||||
function handle.close()
|
||||
if closed then return true end
|
||||
closed = true
|
||||
if wantWrite then pcall(persist) end
|
||||
buf = ""
|
||||
return true
|
||||
end
|
||||
|
||||
return handle
|
||||
end
|
||||
|
||||
return disk
|
||||
end
|
||||
|
||||
local internal = {}
|
||||
local disks = {}
|
||||
|
||||
internal["$"] = createDisk("$", bootDisk, bootPart)
|
||||
|
||||
local function refresh()
|
||||
disks = {}
|
||||
for _, slot in ipairs(diskSlots()) do
|
||||
local ok, raw = pcall(files.getPartitions, slot)
|
||||
if ok and type(raw) == "table" then
|
||||
for _, part in ipairs(normParts(raw)) do
|
||||
if not (slot == bootDisk and part == bootPart) then
|
||||
local addr = tostring(slot) .. ":" .. part
|
||||
disks[addr] = createDisk(addr, slot, part)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
local function iter()
|
||||
refresh()
|
||||
local combined = {}
|
||||
for id, obj in pairs(internal) do combined[id] = obj end
|
||||
for id, obj in pairs(disks) do combined[id] = obj end
|
||||
return pairs(combined)
|
||||
end
|
||||
|
||||
return {refresh = refresh, list = iter}
|
||||
@@ -0,0 +1,11 @@
|
||||
--:Minify:--
|
||||
local kernel=...
|
||||
if kernel.arch~="nc" then
|
||||
kernel.deny("neetcomputers/01_devices.kmod")
|
||||
kernel.deny("neetcomputers/13_devraw.kmod")
|
||||
kernel.deny("neetcomputers/22_http.kmod")
|
||||
kernel.deny("neetcomputers/25_tty.kmod")
|
||||
kernel.deny("neetcomputers/26_keyboards.kmod")
|
||||
kernel.deny("neetcomputers/30_daemon.kmod")
|
||||
kernel.deny("neetcomputers/36_scheduler.kmod")
|
||||
end
|
||||
@@ -0,0 +1,19 @@
|
||||
--:Minify:--
|
||||
local kernel=...
|
||||
kernel.nc={}
|
||||
kernel.nc.io=kernel.apis.io
|
||||
kernel.nc.chip=kernel.apis.chip
|
||||
kernel.nc.event=kernel.apis.event
|
||||
kernel.nc.files=kernel.apis.files
|
||||
|
||||
kernel.log("Installed peripherals:")
|
||||
local ok, peripherals = pcall(kernel.nc.io.getPeripherals)
|
||||
if ok and type(peripherals)=="table" then
|
||||
for _, id in ipairs(peripherals) do
|
||||
local okT, typ = pcall(kernel.nc.io.getType, id)
|
||||
if not okT then typ = "unknown" end
|
||||
kernel.log(" "..tostring(id).." : "..tostring(typ))
|
||||
end
|
||||
else
|
||||
kernel.log(" (none)")
|
||||
end
|
||||
@@ -0,0 +1,28 @@
|
||||
--:Minify:--
|
||||
local kernel=...
|
||||
local io=kernel.nc.io
|
||||
|
||||
local function wrap(id)
|
||||
return function(op, mode)
|
||||
if op=="type" then
|
||||
return "device"
|
||||
elseif op=="open" then
|
||||
if kernel.uid~=0 then error("EACCES") end
|
||||
return io.wrapPeripheral(id)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
local ok, peripherals = pcall(io.getPeripherals)
|
||||
if ok and type(peripherals)=="table" then
|
||||
for _, id in ipairs(peripherals) do
|
||||
local obj=wrap(id)
|
||||
kernel.devfs.data["raw"][tostring(id)]=obj
|
||||
local okT, typ = pcall(io.getType, id)
|
||||
typ = (okT and type(typ)=="string") and typ or "unknown"
|
||||
local typefolder=kernel.devfs.data["raw"]["by-type"][typ]
|
||||
if not typefolder then typefolder={} end
|
||||
typefolder[tostring(#table.keys(typefolder)+1)] = obj
|
||||
kernel.devfs.data["raw"]["by-type"][typ]=typefolder
|
||||
end
|
||||
end
|
||||
Whitespace-only changes.
@@ -0,0 +1,133 @@
|
||||
--:Minify:--
|
||||
-- NeetComputers TTY driver: a cell-buffered text terminal rendered through
|
||||
-- the bootloader's shared framebuffer interface (EFI.nc.newTerm).
|
||||
local kernel = ...
|
||||
local ncgfx = kernel.EFI.nc
|
||||
if not ncgfx or not ncgfx.newTerm then
|
||||
kernel.panic("neetcomputers/25_tty.kmod: EFI.nc framebuffer missing")
|
||||
end
|
||||
|
||||
local term = ncgfx.newTerm()
|
||||
-- Interactive programs (hysh, lua REPL, micro) draw their own inverted
|
||||
-- block cursor; the terminal's hardware underline would sit next to it
|
||||
-- as a stray extra cursor, so keep it off.
|
||||
term.setCursorVisible(false)
|
||||
kernel.nc.term = term
|
||||
kernel.log("initialized nc framebuffer tty "
|
||||
.. tostring(ncgfx.screenW) .. "x" .. tostring(ncgfx.screenH)
|
||||
.. " (" .. tostring(term.cols) .. "x" .. tostring(term.rows) .. " chars)")
|
||||
|
||||
kernel.devfs.data["tty"]["1"] = function(op, mode)
|
||||
if op == "type" then
|
||||
return "Terminal"
|
||||
elseif op == "open" then
|
||||
local h = {
|
||||
read = function(amount) end,
|
||||
write = function(content)
|
||||
term.write(tostring(content))
|
||||
end,
|
||||
size = function()
|
||||
return term.getSize()
|
||||
end,
|
||||
clear = function()
|
||||
term.clear()
|
||||
end,
|
||||
gpos = function()
|
||||
return term.getCursor()
|
||||
end,
|
||||
spos = function(x, y)
|
||||
if not tonumber(x) or not tonumber(y) then error("Invailid arg") end
|
||||
term.setCursor(x, y)
|
||||
end,
|
||||
sfgc = function(c)
|
||||
term.setFg(c)
|
||||
end,
|
||||
sbgc = function(c)
|
||||
term.setBg(c)
|
||||
end,
|
||||
gfgc = function()
|
||||
return term.getFg()
|
||||
end,
|
||||
gbgc = function()
|
||||
return term.getBg()
|
||||
end,
|
||||
isvirt = function()
|
||||
return false
|
||||
end,
|
||||
gplt = function()
|
||||
return {
|
||||
0x000000, 0x000040, 0x000080, 0x0000C0, 0x0000FF,
|
||||
0x002400, 0x002440, 0x002480, 0x0024C0, 0x0024FF,
|
||||
0x004900, 0x004940, 0x004980, 0x0049C0, 0x0049FF,
|
||||
0x006D00, 0x006D40, 0x006D80, 0x006DC0, 0x006DFF,
|
||||
0x009200, 0x009240, 0x009280, 0x0092C0, 0x0092FF,
|
||||
0x00B600, 0x00B640, 0x00B680, 0x00B6C0, 0x00B6FF,
|
||||
0x00DB00, 0x00DB40, 0x00DB80, 0x00DBC0, 0x00DBFF,
|
||||
0x00FF00, 0x00FF40, 0x00FF80, 0x00FFC0, 0x00FFFF,
|
||||
|
||||
0x330000, 0x330040, 0x330080, 0x3300C0, 0x3300FF,
|
||||
0x332400, 0x332440, 0x332480, 0x3324C0, 0x3324FF,
|
||||
0x334900, 0x334940, 0x334980, 0x3349C0, 0x3349FF,
|
||||
0x336D00, 0x336D40, 0x336D80, 0x336DC0, 0x336DFF,
|
||||
0x339200, 0x339240, 0x339280, 0x3392C0, 0x3392FF,
|
||||
0x33B600, 0x33B640, 0x33B680, 0x33B6C0, 0x33B6FF,
|
||||
0x33DB00, 0x33DB40, 0x33DB80, 0x33DBC0, 0x33DBFF,
|
||||
0x33FF00, 0x33FF40, 0x33FF80, 0x33FFC0, 0x33FFFF,
|
||||
|
||||
0x660000, 0x660040, 0x660080, 0x6600C0, 0x6600FF,
|
||||
0x662400, 0x662440, 0x662480, 0x6624C0, 0x6624FF,
|
||||
0x664900, 0x664940, 0x664980, 0x6649C0, 0x6649FF,
|
||||
0x666D00, 0x666D40, 0x666D80, 0x666DC0, 0x666DFF,
|
||||
0x669200, 0x669240, 0x669280, 0x6692C0, 0x6692FF,
|
||||
0x66B600, 0x66B640, 0x66B680, 0x66B6C0, 0x66B6FF,
|
||||
0x66DB00, 0x66DB40, 0x66DB80, 0x66DBC0, 0x66DBFF,
|
||||
0x66FF00, 0x66FF40, 0x66FF80, 0x66FFC0, 0x66FFFF,
|
||||
|
||||
0x990000, 0x990040, 0x990080, 0x9900C0, 0x9900FF,
|
||||
0x992400, 0x992440, 0x992480, 0x9924C0, 0x9924FF,
|
||||
0x994900, 0x994940, 0x994980, 0x9949C0, 0x9949FF,
|
||||
0x996D00, 0x996D40, 0x996D80, 0x996DC0, 0x996DFF,
|
||||
0x999200, 0x999240, 0x999280, 0x9992C0, 0x9992FF,
|
||||
0x99B600, 0x99B640, 0x99B680, 0x99B6C0, 0x99B6FF,
|
||||
0x99DB00, 0x99DB40, 0x99DB80, 0x99DBC0, 0x99DBFF,
|
||||
0x99FF00, 0x99FF40, 0x99FF80, 0x99FFC0, 0x99FFFF,
|
||||
|
||||
0xCC0000, 0xCC0040, 0xCC0080, 0xCC00C0, 0xCC00FF,
|
||||
0xCC2400, 0xCC2440, 0xCC2480, 0xCC24C0, 0xCC24FF,
|
||||
0xCC4900, 0xCC4940, 0xCC4980, 0xCC49C0, 0xCC49FF,
|
||||
0xCC6D00, 0xCC6D40, 0xCC6D80, 0xCC6DC0, 0xCC6DFF,
|
||||
0xCC9200, 0xCC9240, 0xCC9280, 0xCC92C0, 0xCC92FF,
|
||||
0xCCB600, 0xCCB640, 0xCCB680, 0xCCB6C0, 0xCCB6FF,
|
||||
0xCCDB00, 0xCCDB40, 0xCCDB80, 0xCCDBC0, 0xCCDBFF,
|
||||
0xCCFF00, 0xCCFF40, 0xCCFF80, 0xCCFFC0, 0xCCFFFF,
|
||||
|
||||
0xFF0000, 0xFF0040, 0xFF0080, 0xFF00C0, 0xFF00FF,
|
||||
0xFF2400, 0xFF2440, 0xFF2480, 0xFF24C0, 0xFF24FF,
|
||||
0xFF4900, 0xFF4940, 0xFF4980, 0xFF49C0, 0xFF49FF,
|
||||
0xFF6D00, 0xFF6D40, 0xFF6D80, 0xFF6DC0, 0xFF6DFF,
|
||||
0xFF9200, 0xFF9240, 0xFF9280, 0xFF92C0, 0xFF92FF,
|
||||
0xFFB600, 0xFFB640, 0xFFB680, 0xFFB6C0, 0xFFB6FF,
|
||||
0xFFDB00, 0xFFDB40, 0xFFDB80, 0xFFDBC0, 0xFFDBFF,
|
||||
0xFFFF00, 0xFFFF40, 0xFFFF80, 0xFFFFC0, 0xFFFFFF,
|
||||
|
||||
0x000000, 0x0F0F0F, 0x1E1E1E, 0x2D2D2D,
|
||||
0x3C3C3C, 0x4B4B4B, 0x5A5A5A, 0x696969,
|
||||
0x787878, 0x878787, 0x969696, 0xA5A5A5,
|
||||
0xB4B4B4, 0xC3C3C3, 0xD2D2D2, 0xE1E1E1
|
||||
}
|
||||
end,
|
||||
gfx = function()
|
||||
return false
|
||||
end
|
||||
}
|
||||
if mode == "rw" then
|
||||
return h
|
||||
elseif mode == "r" then
|
||||
h["write"] = nil
|
||||
return h
|
||||
elseif mode == "w" then
|
||||
h["read"] = nil
|
||||
return h
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,129 @@
|
||||
--:Minify:--
|
||||
-- NeetComputers keyboard driver. The bootloader delivers User-queue key
|
||||
-- events as ("keyPressed", 1, code, letter, mods) /
|
||||
-- ("keyTyped", 1, letter) / ("keyReleased", 1, code, ...); the ncdaemon
|
||||
-- task (30_daemon.kmod) feeds the fifo below.
|
||||
local kernel=...
|
||||
kernel.nc.keyboards={}
|
||||
local kbs=kernel.nc.keyboards
|
||||
|
||||
local keyboard = {}
|
||||
|
||||
-- NeetComputers key codes: printable keys are ASCII, the rest follows the
|
||||
-- emulator/mod keybind tables (Return=13, arrows 128-131, ...).
|
||||
keyboard.keys = {
|
||||
back = 8,
|
||||
delete = 127,
|
||||
down = 131,
|
||||
enter = 13,
|
||||
home = 158,
|
||||
lcontrol = 132,
|
||||
left = 128,
|
||||
lmenu = 133,
|
||||
lshift = 14,
|
||||
pageDown = 161,
|
||||
pageUp = 160,
|
||||
rcontrol = 132,
|
||||
right = 129,
|
||||
rmenu = 133,
|
||||
rshift = 14,
|
||||
space = 32,
|
||||
tab = 9,
|
||||
up = 130,
|
||||
["end"] = 159,
|
||||
numpadenter = 13,
|
||||
}
|
||||
kernel.nc.keys=keyboard.keys
|
||||
|
||||
for i=0, 255 do
|
||||
kernel.nc.keys[string.char(i)]=i
|
||||
end
|
||||
|
||||
-------------------------------------------------------------------------------
|
||||
|
||||
function keyboard.isAltDown(addr)
|
||||
return kbs[addr] and kbs[addr].alt or false
|
||||
end
|
||||
|
||||
function keyboard.isControl(char)
|
||||
return type(char) == "number" and (char < 0x20 or (char >= 0x7F and char <= 0x9F))
|
||||
end
|
||||
|
||||
function keyboard.isControlDown(addr)
|
||||
return kbs[addr] and kbs[addr].ctrl or false
|
||||
end
|
||||
|
||||
function keyboard.isKeyDown(addr,charOrCode)
|
||||
if not kbs[addr] then return false end
|
||||
if type(charOrCode) == "string" then
|
||||
return kbs[addr].pressedChars[utf8 and utf8.codepoint(charOrCode) or charOrCode:byte()]
|
||||
elseif type(charOrCode) == "number" then
|
||||
return kbs[addr].pressedCodes[charOrCode]
|
||||
end
|
||||
end
|
||||
|
||||
function keyboard.isShiftDown(addr)
|
||||
return kbs[addr] and kbs[addr].shift or false
|
||||
end
|
||||
kernel.nc.keyboard=keyboard
|
||||
|
||||
local function serializeBool(bool)
|
||||
if bool then
|
||||
return "T"
|
||||
else
|
||||
return "F"
|
||||
end
|
||||
end
|
||||
|
||||
kbs["1"]={pressedChars={}, pressedCodes={}, events=kernel.newFifo(),
|
||||
ctrl=false, alt=false, shift=false}
|
||||
kernel.devfs.data["input"]["keyboard1"]=function(op,mode)
|
||||
if op=="type" then
|
||||
return "Keyboard"
|
||||
elseif op=="open" then
|
||||
local fifo=kernel.nc.keyboards["1"].events
|
||||
local h = {
|
||||
read=function(amount)
|
||||
amount = amount or 1
|
||||
-- Drain whatever is already queued first.
|
||||
local rv=""
|
||||
for i=1, amount do
|
||||
local event = {fifo.pop()}
|
||||
if event[1] then
|
||||
rv=rv..event[1]
|
||||
else
|
||||
break
|
||||
end
|
||||
end
|
||||
if rv~="" then return rv end
|
||||
-- Nothing queued: block the task until the ncdaemon
|
||||
-- feeds input instead of busy-spinning syscalls.
|
||||
local task=kernel.currentTask
|
||||
task.status="D"
|
||||
task.io=function()
|
||||
local event = {fifo.pop()}
|
||||
if event[1] then return event[1] end
|
||||
end
|
||||
return nil
|
||||
end,
|
||||
write=function(content)
|
||||
end,
|
||||
gctrl=function()
|
||||
local kb=kernel.nc.keyboards["1"]
|
||||
return serializeBool(kb.ctrl)..";"..serializeBool(kb.alt)
|
||||
end,
|
||||
isKeyDown=function(char)
|
||||
return keyboard.isKeyDown("1",char)
|
||||
end
|
||||
}
|
||||
if mode=="rw" then
|
||||
return h
|
||||
elseif mode=="r" then
|
||||
h["write"]=nil
|
||||
return h
|
||||
elseif mode=="w" then
|
||||
h["read"]=nil
|
||||
return h
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,106 @@
|
||||
--:Minify:--
|
||||
-- NeetComputers input daemon. Consumes kernel machine events produced by
|
||||
-- the bootloader event pump and feeds /dev/input/keyboard1. Ctrl+C sends
|
||||
-- SIGINT to all tasks, mirroring the OC/CCT daemons.
|
||||
local kernel=...
|
||||
local NCTRACEBACK = debug.traceback
|
||||
local keys=kernel.nc.keys
|
||||
kernel.nc.eventhooks=kernel.nc.eventhooks or {}
|
||||
|
||||
kernel.processes.ncdaemon = function()
|
||||
|
||||
local timeout = false
|
||||
|
||||
kernel.log("NC daemon started")
|
||||
|
||||
local function isCtrl(mods) return (math.floor((tonumber(mods) or 0) / 2) % 2) == 1 end
|
||||
|
||||
local specialKeyMap = {
|
||||
[keys.up] = "\27[A",
|
||||
[keys.down] = "\27[B",
|
||||
[keys.right] = "\27[C",
|
||||
[keys.left] = "\27[D",
|
||||
[keys.home] = "\27[H",
|
||||
[keys["end"]] = "\27[F",
|
||||
[keys.pageUp] = "\27[5~",
|
||||
[keys.pageDown] = "\27[6~",
|
||||
[keys.delete] = "\27[3~",
|
||||
[keys.enter] = "\n",
|
||||
[keys.back] = "\b",
|
||||
[keys.tab] = "\t",
|
||||
}
|
||||
|
||||
while true do
|
||||
|
||||
local event = {kernel.EFI.getMachineEvent()}
|
||||
|
||||
if event[1] then
|
||||
|
||||
local eventType = event[1]
|
||||
local kb = kernel.nc.keyboards["1"]
|
||||
|
||||
if eventType == "keyPressed" then
|
||||
local code, letter, mods = event[3], event[4], event[5]
|
||||
kb.pressedCodes[code] = true
|
||||
if type(letter) == "string" and #letter == 1 then
|
||||
kb.pressedChars[letter:byte()] = true
|
||||
end
|
||||
if isCtrl(mods) then
|
||||
kb.ctrl = true
|
||||
local ch = nil
|
||||
if type(letter) == "string" and #letter == 1 then
|
||||
ch = letter:lower():byte()
|
||||
elseif type(code) == "number" and code >= 64 and code <= 95 then
|
||||
ch = code
|
||||
end
|
||||
if ch and ch >= 97 and ch <= 122 then
|
||||
local byte = ch - 96
|
||||
if byte == 3 then
|
||||
for _, task in ipairs(syscall.getTasks()) do
|
||||
syscall.sigsend(task, 1)
|
||||
end
|
||||
else
|
||||
kb.events.push(string.char(byte))
|
||||
end
|
||||
end
|
||||
else
|
||||
local special = specialKeyMap[code]
|
||||
if special then
|
||||
kb.events.push(special)
|
||||
end
|
||||
end
|
||||
|
||||
elseif eventType == "keyReleased" then
|
||||
local code = event[3]
|
||||
kb.pressedCodes[code] = nil
|
||||
if type(event[4]) == "string" and #event[4] == 1 then
|
||||
kb.pressedChars[event[4]:byte()] = nil
|
||||
end
|
||||
if not isCtrl(event[5]) then kb.ctrl = false end
|
||||
|
||||
elseif eventType == "keyTyped" then
|
||||
if event[3] then kb.events.push(event[3]) end
|
||||
|
||||
else
|
||||
if kernel.nc.eventhooks[eventType] then
|
||||
local ok,err = xpcall(kernel.nc.eventhooks[eventType], NCTRACEBACK, table.unpack(event))
|
||||
if not ok then
|
||||
kernel.log("Error on eventhook "..eventType.."\n"..err)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
timeout = false
|
||||
|
||||
else
|
||||
timeout = true
|
||||
end
|
||||
|
||||
if timeout then
|
||||
sleep(0.05)
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
|
||||
kernel.log("NC daemon queued for execution")
|
||||
@@ -0,0 +1,187 @@
|
||||
--:Minify:--
|
||||
-- NeetComputers scheduler (adaptive timeslice + optional preemption
|
||||
-- via EFI.resumeWithTimeout; same design as the CCT/OC schedulers).
|
||||
local kernel=...
|
||||
-- Cache the real traceback: 48_dbg replaces _G.debug with a restricted
|
||||
-- table, and re-reading the global in this hot loop flakes on YSLua.
|
||||
local NCTRACEBACK = debug.traceback
|
||||
local alpha = 0.85
|
||||
local C_target = 0.01
|
||||
local Tmin = 0.0005
|
||||
local Tmax = 0.5
|
||||
local lambda_budget = 0.08
|
||||
local lambda_clamp = 0.03
|
||||
local lambda_var = 0.02
|
||||
local k_min = 0.5
|
||||
local k_max = 0.5
|
||||
local B = 0.01
|
||||
|
||||
function kernel.main()
|
||||
kernel.log("Starting main loop...")
|
||||
kernel.saveLog()
|
||||
local stopLog=5
|
||||
local logTasks=100
|
||||
|
||||
-- Host-tick budget: the bootloader resumes the kernel once per host
|
||||
-- frame and every EFI:yield ends that resume, so one syscall-step per
|
||||
-- frame makes interactive use crawl (roughly a frame per syscall).
|
||||
-- Run a bounded burst of passes per frame instead: a burst where no
|
||||
-- task ran means everything is parked, so yield at once. The step cap
|
||||
-- plus preemption keep a runaway task from tripping the host watchdog.
|
||||
local burstSteps = 0
|
||||
local maxBurstSteps = 64
|
||||
|
||||
while not kernel.exitMain do
|
||||
if kernel.config.logTasks then
|
||||
if logTasks<0 then
|
||||
kernel.log("Active Tasks:")
|
||||
for i,v in pairs(kernel.tasks) do
|
||||
kernel.log(v.name.." : "..v.status.." : "..tostring(v.pid).." : "..tostring(v.exit))
|
||||
end
|
||||
kernel.log("[END BLOCK]")
|
||||
logTasks=100
|
||||
end
|
||||
logTasks=logTasks-1
|
||||
end
|
||||
local N = 0
|
||||
local Tmin_hit = 0
|
||||
local Tmax_hit = 0
|
||||
local totalTaskTime = 0
|
||||
local taskTimes = {}
|
||||
|
||||
for id,hook in ipairs(kernel.mainHooks) do
|
||||
local ok,err = xpcall(hook, NCTRACEBACK)
|
||||
if not ok then
|
||||
kernel.log("[MAIN HOOK ERR]: "..err, "ERROR", 0xFF0000)
|
||||
kernel.mainHooks[id]=nil
|
||||
end
|
||||
end
|
||||
|
||||
for pid, task in pairs(kernel.tasks) do
|
||||
if kernel.exitMain then break end
|
||||
|
||||
for _,list in ipairs(kernel.perTaskHooks) do
|
||||
for id,hook in ipairs(list) do
|
||||
local ok,err = xpcall(hook, NCTRACEBACK, task, pid)
|
||||
if not ok then
|
||||
kernel.log("[TASK HOOK ERR]: "..err, "ERROR", 0xFF0000)
|
||||
list[id]=nil
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
if task.status == "R" then
|
||||
N = N + 1
|
||||
task.timeSlice = math.min(Tmax, math.max(Tmin, B / (N ^ alpha)))
|
||||
if task.status == "R" then
|
||||
local startTime = kernel.EFI:getEpochMs()
|
||||
local ret
|
||||
if stopLog > 0 then
|
||||
kernel.log("Running task " .. tostring(task.pid) .. " (" .. task.name .. ") with time slice " .. string.format("%.3f", task.timeSlice) .. "s", "DBUG", 0x00FFFF)
|
||||
end
|
||||
if kernel.config.preempt then
|
||||
if not task.debugger then
|
||||
ret = { kernel.EFI.resumeWithTimeout(task.coro, task.timeSlice, table.unpack(task.syscallReturn)) }
|
||||
else
|
||||
ret = { coroutine.resume(task.coro, table.unpack(task.syscallReturn)) }
|
||||
end
|
||||
else
|
||||
ret = { coroutine.resume(task.coro, table.unpack(task.syscallReturn)) }
|
||||
end
|
||||
|
||||
local elapsed = kernel.EFI:getEpochMs() - startTime
|
||||
task.lastTime = elapsed
|
||||
task.totalTime = (task.totalTime or 0) + elapsed
|
||||
task.numRuns = (task.numRuns or 0) + 1
|
||||
|
||||
taskTimes[#taskTimes+1] = elapsed
|
||||
totalTaskTime = totalTaskTime + elapsed
|
||||
|
||||
if elapsed <= Tmin then Tmin_hit = Tmin_hit + 1 end
|
||||
if elapsed >= Tmax then Tmax_hit = Tmax_hit + 1 end
|
||||
|
||||
if ret[1] == "error" or ret[1] == false then
|
||||
kernel.log("processHandlerException: " .. tostring(ret[2]), "ERROR", 0xFF0000)
|
||||
task.status = "Z"
|
||||
task.exit = "processHandlerException: " .. tostring(ret[2])
|
||||
|
||||
elseif ret[1] == "timeout" then
|
||||
task.ivs = task.ivs + 1
|
||||
task.syscallReturn = {}
|
||||
|
||||
elseif ret[1] == "success" or ret[1] == true then
|
||||
task.vs = task.vs + 1
|
||||
|
||||
if ret[2] == "syscall" then
|
||||
local scname = ret[3]
|
||||
if kernel.syscalls[scname] then
|
||||
if kernel.config.debugSyscalls then
|
||||
kernel.log("Task " .. task.pid .. " syscall: " .. scname, "DBUG", 0x00FFFF)
|
||||
for i = 4, #ret do
|
||||
kernel.log(" inval[" .. (i-3) .. "] = " .. tostring(ret[i]), "DBUG", 0x00FFFF)
|
||||
end
|
||||
end
|
||||
|
||||
local sysret = { xpcall(kernel.syscalls[scname], NCTRACEBACK, table.unpack(ret, 4)) }
|
||||
|
||||
if kernel.config.debugSyscalls then
|
||||
if not sysret[1] then
|
||||
kernel.log("Task " .. task.pid .. " syscall " .. scname .. " failed: " .. tostring(sysret[2]), "ERROR", 0xFF0000)
|
||||
else
|
||||
kernel.log("Task " .. task.pid .. " syscall " .. scname .. " ok, " .. (#sysret-1) .. " retvals", "DBUG", 0x00FFFF)
|
||||
for i = 2, #sysret do
|
||||
local v = type(sysret[i]) == "table" and table.serialize(sysret[i]) or tostring(sysret[i])
|
||||
kernel.log(" retval[" .. (i-1) .. "] = " .. v, "DBUG", 0x00FFFF)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
if not sysret[1] then
|
||||
task.syscallReturn = { false, sysret[2] }
|
||||
else
|
||||
task.syscallReturn = { true, table.unpack(sysret, 2) }
|
||||
end
|
||||
else
|
||||
task.syscallReturn = { false, "Unknown syscall: " .. tostring(scname) }
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
-- No yield per task anymore: passes run back-to-back in one
|
||||
-- host tick (see burst budget below). Yielding here cost a
|
||||
-- full frame per task-step.
|
||||
end
|
||||
|
||||
burstSteps = burstSteps + N
|
||||
if N == 0 or burstSteps >= maxBurstSteps then
|
||||
burstSteps = 0
|
||||
-- Yield to the host once per burst (even when no task ran):
|
||||
-- without this an idle system spins without voluntary yields
|
||||
-- and trips the host watchdog.
|
||||
kernel.EFI:yield()
|
||||
end
|
||||
|
||||
local T_prev_avg = (N > 0) and (totalTaskTime / N) or 0
|
||||
local T_prev_var = 0
|
||||
for _, t in ipairs(taskTimes) do
|
||||
T_prev_var = T_prev_var + (t - T_prev_avg) ^ 2
|
||||
end
|
||||
if N > 0 then T_prev_var = T_prev_var / N end
|
||||
|
||||
if N > 0 then
|
||||
local f_clamp = k_min * (Tmin_hit / N) - k_max * (Tmax_hit / N)
|
||||
local B_budget = (C_target * (N ^ (alpha - 1))) / math.max(T_prev_avg, 1e-8)
|
||||
B = B + lambda_budget * (B_budget - B) + lambda_clamp * f_clamp - lambda_var * T_prev_var
|
||||
end
|
||||
|
||||
kernel.hpv.reapDeadTasks()
|
||||
if stopLog > 0 then
|
||||
kernel.log("Executed " .. N .. " tasks, avg time: " .. string.format("%.2f", T_prev_avg) .. "ms, var: " .. string.format("%.2f", T_prev_var) .. ", B: " .. string.format("%.5f", B))
|
||||
stopLog = stopLog - 1
|
||||
kernel.saveLog()
|
||||
end
|
||||
end
|
||||
kernel.log("Exited main")
|
||||
kernel.saveLog()
|
||||
end
|
||||
@@ -0,0 +1 @@
|
||||
firmware for NeetComputers
|
||||
@@ -0,0 +1 @@
|
||||
1.0.0
|
||||
Reference in new issue
Block a user