added a new simpler tape loader and an archiving program for it

This commit is contained in:
Izaya 2019-07-22 09:45:40 +10:00
parent 24f5d543e1
commit f92cbde89b
3 changed files with 111 additions and 0 deletions

24
tapeloader/mkarchive.lua Normal file
View File

@ -0,0 +1,24 @@
local tArgs = {...}
local function cint(n,l)
local t={}
for i = 0, 7 do
t[i+1] = (n >> (i * 8)) & 0xFF
end
return string.reverse(string.char(table.unpack(t)):sub(1,l))
end
local function genHeader(fname,len)
return string.format("%s%s%s",cint(fname:len(),2),fname,cint(len,2))
end
for k,v in ipairs(tArgs) do
local f = io.open(v,"rb")
if f then
local content = f:read("*a")
f:close()
io.write(genHeader(v,content:len()))
io.write(content)
end
end
io.write(string.char(0):rep(2))

58
tapeloader/tapeloader.lua Normal file
View File

@ -0,0 +1,58 @@
local fs = component.proxy(computer.tmpAddress())
local init = function() end
if fs.exists("/init.lua") then
do
local tape = component.proxy(component.list("tape_drive")())
local function toint(s)
s=s or ""
local n = 0
local i = 1
while true do
local p = s:sub(i,i)
if p == "" then break end
local b = string.byte(p)
n = n << 8
n = n | b
i=i+1
end
return n
end
local function fwrite(name,len)
local dir = name:match("(.+)/.*%.?.+")
if (dir) then
fs.makeDirectory("/"..dir)
end
local fh = fs.open(name, "w")
fs.write(fh,tape.read(len))
fs.close(fh)
end
tape.seek(-math.huge)
while true do
local nlen = toint(tape.read(2))
if nlen == 0 then
break
end
local name = tape.read(nlen)
local fsize = toint(tape.read(2))
fwrite(name,fsize)
computer.beep()
end
end
end
do
local f=fs.open("/init.lua","rb")
if not f then error("no init.lua") end
local initstr = ""
local data = ""
repeat
initstr = initstr .. data
data = fs.read(f,2048)
until data == nil
init = load(initstr)
end
computer.getBootAddress = computer.tmpAddress
init()

29
tapeloader/unarchive.lua Normal file
View File

@ -0,0 +1,29 @@
local tArgs = {...}
local function toint(s)
s=s or ""
local n = 0
local i = 1
while true do
local p = s:sub(i,i)
if p == "" then break end
local b = string.byte(p)
n = n << 8
n = n | b
i=i+1
end
return n
end
local fi = io.open(tArgs[1])
while true do
local nlen = toint(fi:read(2))
if nlen == 0 then
break
end
local name = fi:read(nlen)
local fsize = toint(fi:read(2))
local fcontent = fi:read(fsize)
print(name,fsize)
end
fi:close()