ws-arch2: archive/d64 (C64 D64 / D71 / D81 read + write, PETSCII names as c1541)

This commit is contained in:
4DBug committed 2026-10-31 14:15:12 -05:00
1 parent 3243291877
commit 5ca22c4431
5 files changed
+585

No files matched your search

+1
View File
@@ -54,6 +54,7 @@ is tested under both runtimes (luau `--codegen -O2` and LuaJIT through the build
| archive | binhex | .hqx .hcx | yes | yes | forks: true | `tests/archive/binhex.luau` |
| archive | br | .br | yes | - | stream: true | `tests/archive/br.luau` |
| archive | bz2 | .bz2 .tbz2 .tbz | yes | - | stream: true | `tests/archive/bz2.luau` |
| archive | d64 | .d64 .d71 .d81 | yes | yes | variants: "d64", "d71", "d81" | `tests/archive/d64.luau` |
| archive | gzip | .gz .tgz .gzip | yes | yes | stream: true | `tests/archive/gzip.luau` |
| archive | macbinary | .bin .macbin | yes | yes | forks: true, variants: "I", "II", "III" | `tests/archive/macbinary.luau` |
| archive | tar | .tar | yes | yes | variants: "ustar", "pax", "gnu" | `tests/archive/tar.luau` |
+12
View File
@@ -602,6 +602,18 @@ local list: { Entry } = {
return require("@formats/archive/bz2")
end,
},
{
name = "d64",
category = "archive",
path = "archive/d64",
extensions = { "d64", "d71", "d81" },
mime = { "application/x-d64" },
deps = { "core/Err", "core/Bytes", "containers/Archive" },
caps = { decode = true, encode = true, variants = { "d64", "d71", "d81" } },
load = function()
return require("@formats/archive/d64")
end,
},
{
name = "gzip",
category = "archive",
+424
View File
@@ -0,0 +1,424 @@
--!strict
-- archive/d64: Commodore disk images as an Archive: D64 (1541: 35 / 40 / 42 tracks, with or without the error-info
-- block), D71 (1571: 70 tracks, double sided) and D81 (1581: 80 tracks of 40 sectors). The BAM / header (disk name and
-- id in meta.disk / meta.id), the directory chain, and every file's sector chain; PRG / SEQ / USR / REL files are
-- entries (entry.type, entry.blocks, entry.locked, entry.closed; REL files read their data chain, the side sectors are
-- skipped), deleted entries are skipped, unclosed ("splat") files are flagged closed = false. Names are PETSCII
-- translated as VICE's c1541 does (0x41..0x5A -> a..z, 0xC1..0xDA and 0x61..0x7A -> A..Z, other bytes to U+E0xx so the
-- name survives a round trip); entry.rawName keeps the 16 bytes. Writes D64 (35 tracks) / D71 / D81 images from any
-- Archive (files as PRG, or entry.type), allocating sectors near the directory track with interleave 10 as CBM DOS
-- does (not the same sectors as a real drive). Written from the 1541 / 1571 / 1581 DOS documentation (Peter Schepers'
-- disk format notes). Not supported: GEOS VLIR files (read as their first chain), 1581 partitions (CBM type) contents.
-- ------------------------------------------------------------------------------------------------ requires
local Err = require("@formats/core/Err")
local Bytes = require("@formats/core/Bytes")
local Archive = require("@formats/containers/Archive")
-- ------------------------------------------------------------------------------------------------ contract
local D64 = {
name = "d64",
category = "archive",
extensions = { "d64", "d71", "d81" },
mime = { "application/x-d64" },
deps = { "core/Err", "core/Bytes", "containers/Archive" },
caps = { decode = true, encode = true, variants = { "d64", "d71", "d81" } },
}
export type DecodeOptions = { maxEntries: number? } -- default 4096
export type EncodeOptions = {
variant: ("d64" | "d71" | "d81")?, -- default: the variant read, else "d64"
disk: string?, -- disk name (default "formats-luau" / the name read)
id: string?, -- two-character disk id (default "fl")
}
-- ------------------------------------------------------------------------------------------------ constants
local TYPES = { [0] = "del", "seq", "prg", "usr", "rel", "cbm", "dir" }
local TYPE_CODE = { del = 0, seq = 1, prg = 2, usr = 3, rel = 4 }
type Geometry = { kind: string, tracks: number, offsets: { number }, spt: { number }, dirTrack: number, total: number }
local function geometry(kind: string, tracks: number): Geometry
local offsets, spt = {}, {}
local o = 0
for t = 1, tracks do
local n
if kind == "d81" then
n = 40
else
local s = if kind == "d71" and t > 35 then t - 35 else t
n = if s <= 17 then 21 elseif s <= 24 then 19 elseif s <= 30 then 18 else 17
end
offsets[t], spt[t] = o, n
o += n * 256
end
return { kind = kind, tracks = tracks, offsets = offsets, spt = spt, dirTrack = if kind == "d81" then 40 else 18, total = o }
end
-- image sizes -> geometry (sizes with the error-info block appended too)
local function detect(len: number): Geometry?
for _, g in { { "d64", 35 }, { "d64", 40 }, { "d64", 42 }, { "d71", 70 }, { "d81", 80 } } do
local geo = geometry(g[1], g[2])
local sectors = geo.total // 256
if len == geo.total or (g[1] ~= "d81" and len == geo.total + sectors) then
return geo
end
end
return nil
end
-- ------------------------------------------------------------------------------------------------ sniff
function D64.sniff(r: Bytes.Reader): number
local geo = detect(r:size())
if not geo then
return 0
end
-- the BAM / header sector: dir track link and DOS version byte
local h = r:sub(geo.offsets[geo.dirTrack], 256)
local t, s, v = h:u8(), h:u8(), h:u8()
if geo.kind == "d81" then
return if t == 40 and s == 3 and v == 0x44 then 0.9 else 0.3
end
return if t == 18 and s == 1 and (v == 0x41 or v == 0) then 0.9 else 0.3
end
-- ------------------------------------------------------------------------------------------------ decode
local function petscii(raw: string): string
local out = table.create(#raw)
for i = 1, #raw do
local c = string.byte(raw, i)
if c >= 0x41 and c <= 0x5A then
out[i] = string.char(c + 32)
elseif c >= 0xC1 and c <= 0xDA then
out[i] = string.char(c - 0x80)
elseif c >= 0x61 and c <= 0x7A then
out[i] = string.char(c - 32)
elseif c >= 0x20 and c <= 0x40 and c ~= 0x2F then
out[i] = string.char(c)
else
out[i] = utf8.char(0xE000 + c)
end
end
return table.concat(out)
end
local function toPetscii(name: string): string
local out = {}
if not utf8.len(name) then
return string.sub(name, 1, 16)
end
for _, c in utf8.codes(name) do
local b
if c >= 0x61 and c <= 0x7A then
b = c - 32
elseif c >= 0x41 and c <= 0x5A then
b = c + 0x80
elseif c >= 0xE000 and c <= 0xE0FF then
b = c - 0xE000
elseif c >= 0x20 and c <= 0x40 and c ~= 0x2F then
b = c
else
b = 0x3F
end
table.insert(out, string.char(b))
end
return string.sub(table.concat(out), 1, 16)
end
local function encoder(a: any, o: any?): (buffer?, Err.Err?)
return D64.encode(a, o)
end
-- the bytes of a sector chain starting at (t, s); nil, message when it leaves the disk or loops
local function chain(b: buffer, base: number, geo: Geometry, t: number, s: number): (buffer?, string?)
local w = Bytes.writer()
local seen: { [number]: boolean } = {}
while t ~= 0 do
if t < 1 or t > geo.tracks or s >= geo.spt[t] then
return nil, string.format("chain leaves the disk at %d/%d", t, s)
end
local key = t * 256 + s
if seen[key] then
return nil, string.format("chain loops at %d/%d", t, s)
end
seen[key] = true
local o = base + geo.offsets[t] + s * 256
local nt, ns = buffer.readu8(b, o), buffer.readu8(b, o + 1)
if nt == 0 then
local last = math.max(ns, 1)
if last >= 2 then
w:bytes(buffer.fromstring(buffer.readstring(b, o + 2, last - 1)))
end
else
w:bytes(buffer.fromstring(buffer.readstring(b, o + 2, 254)))
end
t, s = nt, ns
end
return w:finish(), nil
end
function D64.decode(r: Bytes.Reader, opts: DecodeOptions?): (any, Err.Err?)
local o: DecodeOptions = opts or {}
local geo = detect(r:size())
if not geo then
return nil, Err.new(Err.CORRUPT, "not a D64 / D71 / D81 image size (" .. r:size() .. " bytes)")
end
local b, base = r.buf, r.base
local hdr = base + geo.offsets[geo.dirTrack]
local nameAt, idAt = if geo.kind == "d81" then 0x04 else 0x90, if geo.kind == "d81" then 0x16 else 0xA2
local diskRaw = buffer.readstring(b, hdr + nameAt, 16)
local a = Archive.new("d64", encoder)
a.meta.disk = petscii((string.gsub(diskRaw, "\160+$", "")))
a.meta.id = petscii(buffer.readstring(b, hdr + idAt, 2))
a.meta.variant = geo.kind
a.meta.tracks = geo.tracks
a.meta.errorInfo = r:size() ~= geo.total
a.meta.source = { format = "d64", variant = geo.kind, tracks = geo.tracks }
-- the directory chain
local t, s = buffer.readu8(b, hdr), buffer.readu8(b, hdr + 1)
local seen: { [number]: boolean } = {}
local count = 0
local maxEntries = o.maxEntries or 4096
while t ~= 0 do
if t < 1 or t > geo.tracks or s >= geo.spt[t] or seen[t * 256 + s] then
return nil, Err.new(Err.CORRUPT, string.format("bad directory sector %d/%d", t, s))
end
seen[t * 256 + s] = true
local so = base + geo.offsets[t] + s * 256
for k = 0, 7 do
local e = so + k * 32
local ft = buffer.readu8(b, e + 2)
local kind = bit32.band(ft, 15)
if ft ~= 0 and kind ~= 0 then
count += 1
if count > maxEntries then
return nil, Err.new(Err.LIMIT, "more than maxEntries directory entries")
end
local raw = buffer.readstring(b, e + 5, 16)
local name = petscii((string.gsub(raw, "\160+$", "")))
local base_name, n = name, 2
while a._byName[name] do
name = base_name .. ";" .. n
n += 1
end
local ty = TYPES[kind] or "?"
local entry: any = {
name = name,
rawName = raw,
type = ty,
closed = bit32.band(ft, 0x80) ~= 0,
locked = bit32.band(ft, 0x40) ~= 0,
blocks = buffer.readu16(b, e + 30),
dir = false,
method = "store",
}
if ty == "cbm" or ty == "dir" then
entry.size = 0
else
local data, err = chain(b, base, geo, buffer.readu8(b, e + 3), buffer.readu8(b, e + 4))
if not data then
return nil, Err.new(Err.CORRUPT, name .. ": " .. tostring(err))
end
entry.size = buffer.len(data)
entry.data = data
if ty == "rel" then
entry.recordLength = buffer.readu8(b, e + 23)
end
end
Archive.put(a, entry)
end
end
t, s = buffer.readu8(b, so), buffer.readu8(b, so + 1)
end
return a, nil
end
-- ------------------------------------------------------------------------------------------------ encode
-- BAM layout per variant: where each track's free count + bitmap lives
local function bamEntry(geo: Geometry, img: buffer, t: number): (number, number, number)
-- returns (offset of the free count, offset of the bitmap, bitmap bytes)
local dir = geo.offsets[geo.dirTrack]
if geo.kind == "d81" then
local side = if t <= 40 then 1 else 2
local tt = if t <= 40 then t else t - 40
local o = dir + side * 256 + 0x10 + (tt - 1) * 6
return o, o + 1, 5
end
if t <= 35 then
local o = dir + 4 + (t - 1) * 4
return o, o + 1, 3
end
-- d71 side 2: counts at 18/0 0xDD.., bitmaps on track 53 sector 0
return dir + 0xDD + (t - 36), geo.offsets[53] + (t - 36) * 3, 3
end
local function setFree(geo: Geometry, img: buffer, t: number, s: number, free: boolean)
local co, bo = bamEntry(geo, img, t)
local byte = bo + bit32.rshift(s, 3)
local mask = bit32.lshift(1, bit32.band(s, 7))
local cur = buffer.readu8(img, byte)
local isFree = bit32.band(cur, mask) ~= 0
if free and not isFree then
buffer.writeu8(img, byte, bit32.bor(cur, mask))
buffer.writeu8(img, co, buffer.readu8(img, co) + 1)
elseif not free and isFree then
buffer.writeu8(img, byte, bit32.band(cur, bit32.bnot(mask)))
buffer.writeu8(img, co, buffer.readu8(img, co) - 1)
end
end
function D64.encode(value: any, opts: EncodeOptions?): (buffer?, Err.Err?)
local o: EncodeOptions = opts or {}
local variant = o.variant or (if type(value) == "table" and type(value.meta) == "table" and value.meta.variant then value.meta.variant else "d64")
if variant ~= "d64" and variant ~= "d71" and variant ~= "d81" then
return nil, Err.new(Err.ARG, "variant must be d64, d71 or d81")
end
local files, err = Archive.files(value, "data")
if not files then
return nil, err
end
local geo = geometry(variant, if variant == "d64" then 35 elseif variant == "d71" then 70 else 80)
local img = buffer.create(geo.total)
local dirT = geo.dirTrack
local dir = geo.offsets[dirT]
local diskName = toPetscii(o.disk or (if type(value) == "table" and type(value.meta) == "table" and type(value.meta.disk) == "string" then value.meta.disk else "formats-luau"))
local id = toPetscii(o.id or "fl")
local function pad16(s: string): string
return s .. string.rep("\160", 16 - #s)
end
-- header / BAM
local firstDir
if variant == "d81" then
buffer.writestring(img, dir, "\40\3\68\0")
buffer.writestring(img, dir + 4, pad16(diskName) .. "\160\160" .. string.sub(id .. " ", 1, 2) .. "\160" .. "3D" .. "\160\160")
for side = 1, 2 do
local bo = dir + side * 256
buffer.writestring(img, bo, if side == 1 then "\40\2\68\187" else "\0\255\68\187")
buffer.writestring(img, bo + 4, string.sub(id .. " ", 1, 2) .. "\192\0")
end
firstDir = 3
else
buffer.writestring(img, dir, "\18\1\65" .. (if variant == "d71" then "\128" else "\0"))
buffer.writestring(img, dir + 0x90, pad16(diskName) .. "\160\160" .. string.sub(id .. " ", 1, 2) .. "\160" .. "2A" .. "\160\160\160\160")
firstDir = 1
end
for t = 1, geo.tracks do
for s = 0, geo.spt[t] - 1 do
setFree(geo, img, t, s, true)
end
end
-- system sectors: the header / BAM sectors and the first directory sector
local used = { [dirT * 256 + 0] = true, [dirT * 256 + firstDir] = true }
if variant == "d81" then
used[dirT * 256 + 1], used[dirT * 256 + 2] = true, true
elseif variant == "d71" then
for s = 0, geo.spt[53] - 1 do
used[53 * 256 + s] = true
end
end
for k in used do
setFree(geo, img, k // 256, k % 256, false)
end
local maxDirSectors = geo.spt[dirT] - firstDir
if #files > maxDirSectors * 8 then
return nil, Err.new(Err.LIMIT, "too many files for one directory track")
end
-- allocation: tracks nearest the directory track first, interleave 10
local order = {}
for d = 1, geo.tracks do
for _, t in { dirT - d, dirT + d } do
if t >= 1 and t <= geo.tracks and t ~= dirT and not (variant == "d71" and t == 53) then
table.insert(order, t)
end
end
end
local ti, lastS = 1, -10
local function alloc(): (number?, number?)
while ti <= #order do
local t = order[ti]
local n = geo.spt[t]
for k = 0, n - 1 do
local s = (lastS + 10 + k) % n
if not used[t * 256 + s] then
used[t * 256 + s] = true
setFree(geo, img, t, s, false)
lastS = s
return t, s
end
end
ti += 1
lastS = -10
end
return nil, nil
end
local dirSector, slot = firstDir, 0
local dirSectors = { firstDir }
for _, f in files do
local data = f.data
local n = buffer.len(data)
local blocks = math.max(1, math.ceil(n / 254))
local first_t, first_s = 0, 0
local prev = -1
for k = 0, blocks - 1 do
local t, s = alloc()
if not t or not s then
return nil, Err.new(Err.LIMIT, "disk full")
end
local so = geo.offsets[t] + s * 256
if prev < 0 then
first_t, first_s = t, s
else
buffer.writeu8(img, prev, t)
buffer.writeu8(img, prev + 1, s)
end
local len = math.min(254, n - k * 254)
if len > 0 then
buffer.copy(img, so + 2, data, k * 254, len)
end
buffer.writeu8(img, so + 1, if k == blocks - 1 then len + 1 else 0)
prev = so
end
if slot == 8 then
-- next directory sector on the directory track (interleave 3)
local nextS = nil
for k = 0, geo.spt[dirT] - 1 do
local s = (dirSector + 3 + k) % geo.spt[dirT]
if not used[dirT * 256 + s] then
nextS = s
break
end
end
if not nextS then
return nil, Err.new(Err.LIMIT, "directory full")
end
used[dirT * 256 + nextS] = true
setFree(geo, img, dirT, nextS, false)
buffer.writeu8(img, dir + dirSector * 256, dirT)
buffer.writeu8(img, dir + dirSector * 256 + 1, nextS)
dirSector, slot = nextS, 0
table.insert(dirSectors, nextS)
end
local e = dir + dirSector * 256 + slot * 32
local ty = if type(f.entry.type) == "string" and TYPE_CODE[f.entry.type] then TYPE_CODE[f.entry.type] else 2
buffer.writeu8(img, e + 2, 0x80 + ty)
buffer.writeu8(img, e + 3, first_t)
buffer.writeu8(img, e + 4, first_s)
local raw = if type(f.entry.rawName) == "string" and #f.entry.rawName == 16 then (string.gsub(f.entry.rawName, "\160+$", "")) else toPetscii(f.name)
buffer.writestring(img, e + 5, pad16(raw))
buffer.writeu16(img, e + 30, blocks)
slot += 1
end
buffer.writeu8(img, dir + dirSector * 256 + 1, 0xFF)
return img, nil
end
-- ------------------------------------------------------------------------------------------------ extras
-- new(variant?) -> an empty disk: a:add(name, data); a:write({ disk = "name", id = "ab" })
function D64.new(variant: string?): any
local a = Archive.new("d64", encoder)
a.meta.variant = variant or "d64"
a.meta.source = { format = "d64" }
return a
end
return D64
+75
View File
@@ -0,0 +1,75 @@
-- archive/d64 tests: D64 / D71 / D81 images made by VICE's c1541 (PRG / SEQ / USR, multi-sector, a deleted file, an
-- error-info block, 30 files over several directory sectors) list the files c1541 reads back (names, sizes, types,
-- data CRCs), disk name and id; our D64 / D71 / D81 output is read by c1541 (fixture step) and round-trips; PETSCII
-- names; errors; the fuzz pass. Fixtures: tools/fixtures/d64.py.
local T = require("@tests/T")
local F = require("@formats")
local D64 = require("@formats/archive/d64")
local Bytes = require("@formats/core/Bytes")
local Err = require("@formats/core/Err")
local E = require("@tests/archive/_entries")
local Fx = require("@tests/gen/d64")
local t = T.suite("d64")
t:eq(D64.name, "d64", "name")
local samples = {}
for _, c in Fx.cases do
local a, err = D64.decode(Bytes.reader(buffer.fromstring(c.data)))
t:check(a, c.name .. ": " .. tostring(err))
if a then
E.check(t, a, c.entries, c.name)
t:eq(a.meta.disk, c.disk, c.name .. ": disk name")
t:eq(a.meta.id, c.id, c.name .. ": disk id")
for _, w in c.entries do
local e = a._byName[w.name]
t:eq(e and e.type, w.type, c.name .. ": " .. w.name .. " type")
end
t:check(a._byName.gone == nil, c.name .. ": deleted file skipped")
end
t:eq(F.detect(c.data), "d64", c.name .. ": detect")
if c.name == "d64" then
table.insert(samples, c.data)
end
end
for _, o in Fx.ours do
t:check(o.ok, "c1541 reads our " .. o.variant .. ": " .. o.listing)
end
for _, v in { "d64", "d71", "d81" } do
local out = E.roundTrip(t, D64, { variant = v, disk = "round trip", id = "rt" }, "round trip " .. v)
if out then
local a = D64.decode(Bytes.reader(out)) :: any
t:eq(a.meta.variant, v, v .. ": variant")
t:eq(a.meta.disk, "round trip", v .. ": disk name")
end
end
-- PETSCII names: lower / upper case and other bytes survive a round trip
local a = D64.new()
a:add("Mixed Case 1", "x")
a:add("\u{E0D3}hift", "y")
local back = D64.decode(Bytes.reader(a:write() :: buffer)) :: any
t:check(back._byName["Mixed Case 1"] and back._byName["\u{E0D3}hift"] == nil and back._byName["Shift"], "petscii names")
-- errors
local function code(b: buffer): string?
local _, e = D64.decode(Bytes.reader(b))
return e and e.code
end
t:eq(code(buffer.create(1000)), Err.CORRUPT, "bad size")
local img = buffer.fromstring(Fx.cases[1].data)
local dir = 357 * 256 -- track 18 sector 0
buffer.writeu8(img, dir, 99)
t:eq(code(img), Err.CORRUPT, "directory link off the disk")
local many = D64.new()
for i = 1, 145 do
many:add("f" .. i, "x")
end
t:eq((select(2, many:write()) :: any).code, Err.LIMIT, "directory full")
local big = D64.new()
big:add("big", buffer.create(200000))
t:eq((select(2, big:write()) :: any).code, Err.LIMIT, "disk full")
t:fuzz("decode", function(b: buffer)
return D64.decode(Bytes.reader(b))
end, samples, { mutations = 60, garbage = 20 })
t:done()
+73
View File
@@ -0,0 +1,73 @@
# Commodore disk image fixtures (tools/fixtures.py): D64 / D71 / D81 images formatted and filled by VICE's `c1541`
# (PRG / SEQ / USR files, a multi-sector file, a deleted file, a full-ish directory), a D64 with the error-info block
# appended; every file read back by `c1541 -read` for the reference data. Our writer's D64 / D71 / D81 output
# (tools/fixtures/_arch2_ours.luau) is listed and read back by c1541.
# tests/gen/d64.luau { cases = { { name, data, disk, id, entries = { { name, size, crc, type } } } },
# ours = { { variant, ok, listing } } }
import importlib.util, os, shutil, zlib
_s = importlib.util.spec_from_file_location("_arch2", os.path.join(os.path.dirname(os.path.abspath(__file__)), "_arch2.py"))
A = importlib.util.module_from_spec(_s)
_s.loader.exec_module(A)
INPUTS = ["tools/fixtures/_arch2.py", "tools/fixtures/_arch2_ours.luau", "src/Formats/archive/d64"]
PKG = ["vice"]
def c1541(args, cwd):
return A.tool(PKG, ["c1541"] + args, cwd=cwd).decode(errors="replace")
def generate(ctx):
rng = ctx.rng(64)
tmp = A.tmpdir()
try:
files = [("hello", ",p", b"\x01\x08hello prg\n"), ("big data", ",s", rng.randbytes(9000)), ("user file", ",u", b"USR " * 100),
("exact", ",p", rng.randbytes(254 * 3)), ("one", ",p", b"\x00")]
for i, (_, _, d) in enumerate(files):
open(os.path.join(tmp, "f%d" % i), "wb").write(d)
cases = []
for kind, label in (("d64", "test disk,ab"), ("d71", "double,cd"), ("d81", "three d,ef")):
img = "t." + kind
args = ["-format", label, kind, img]
for i, (name, suf, _) in enumerate(files):
args += ["-write", "f%d" % i, name + suf]
args += ["-write", "f0", "gone", "-delete", "gone"]
c1541(args, tmp)
ents = []
for i, (name, suf, d) in enumerate(files):
out = os.path.join(tmp, "out%d" % i)
c1541([img, "-read", name + suf, out], tmp)
rd = open(out, "rb").read()
assert rd == d, (kind, name, len(rd), len(d))
ents.append({"name": name, "size": len(rd), "crc": zlib.crc32(rd), "type": {"p": "prg", "s": "seq", "u": "usr"}[suf[1]]})
data = open(os.path.join(tmp, img), "rb").read()
disk, did = label.split(",")
cases.append({"name": kind, "data": data, "disk": disk, "id": did, "entries": ents})
if kind == "d64":
cases.append({"name": "d64 with error info", "data": data + b"\x01" * 683, "disk": disk, "id": did, "entries": ents})
# many files: several directory sectors
args = ["-format", "many,mm", "d64", "many.d64"]
for i in range(30):
args += ["-write", "f0", "file%02d" % i]
c1541(args, tmp)
d0 = files[0][2]
cases.append({"name": "30 files", "data": open(os.path.join(tmp, "many.d64"), "rb").read(), "disk": "many", "id": "mm",
"entries": [{"name": "file%02d" % i, "size": len(d0), "crc": zlib.crc32(d0), "type": "prg"} for i in range(30)]})
ours = []
for variant in ("d64", "d71", "d81"):
img = os.path.join(tmp, "ours." + variant)
open(img, "wb").write(A.ours(ctx, "d64", "variant=" + variant))
listing = c1541([img, "-list"], tmp)
ok = True
for name, data in A.FILES:
if not data:
continue # c1541 reads a one-sector file with no data bytes (link 0/1) as 254 bytes
out = os.path.join(tmp, "o-" + name)
c1541([img, "-read", name + suf, out], tmp)
got = open(out, "rb").read() if os.path.exists(out) else None
ok = ok and got == data
ours.append({"variant": variant, "ok": ok, "listing": listing})
ctx.emit("d64", {"cases": cases, "ours": ours})
finally:
shutil.rmtree(tmp)