Publish VCC Plus OSS source distribution for 3.6.2 build 60
This commit is contained in:
commit
c652a5276d
1192 files changed
+272368
No files matched your search
@@ -0,0 +1,278 @@
|
||||
# SPDX-License-Identifier: GPL-3.0-or-later
|
||||
"""Qualify Disk BASIC stream writes and positioning against independently decoded volumes."""
|
||||
import argparse
|
||||
import os
|
||||
import shutil
|
||||
from pathlib import Path
|
||||
import subprocess
|
||||
import tempfile
|
||||
|
||||
|
||||
SECTOR = 256
|
||||
GRANULE = 9 * SECTOR
|
||||
FAT = (17 * 18 + 1) * SECTOR
|
||||
TEXT_RECORDS = b"X" * 255 + b"\r\nMARS\rEUROPA\nTITAN\r\n"
|
||||
SENTINEL = b"previously saved data\x00\r\xff" * 57
|
||||
FILL = bytes((i * 3 + 11) & 255 for i in range(66 * GRANULE))
|
||||
DISTANT = bytes((i * 7 + i // SECTOR) & 255 for i in range(65537))
|
||||
|
||||
|
||||
def contents(raw):
|
||||
"""Decode directory and FAT independently, rejecting leaks, cross-links and cycles."""
|
||||
files = {}
|
||||
owners = set()
|
||||
for row in range(FAT + SECTOR, (17 + 1) * 18 * SECTOR, 32):
|
||||
entry = raw[row:row + 32]
|
||||
if entry[0] == 0xff:
|
||||
break
|
||||
if entry[0] == 0:
|
||||
continue
|
||||
name = entry[:8].decode().rstrip() + "." + entry[8:11].decode().rstrip()
|
||||
granule = entry[13]
|
||||
data = bytearray()
|
||||
while True:
|
||||
if granule >= 68 or granule in owners:
|
||||
raise AssertionError(f"{name} references missing or shared granule {granule}")
|
||||
owners.add(granule)
|
||||
addr = (granule + (2 if granule >= 34 else 0)) * GRANULE
|
||||
link = raw[FAT + granule]
|
||||
if link < 68:
|
||||
data.extend(raw[addr:addr + GRANULE])
|
||||
granule = link
|
||||
continue
|
||||
count, last = link - 0xc0, int.from_bytes(entry[14:16], "big")
|
||||
if count not in range(10) or last > SECTOR or (not count and last):
|
||||
raise AssertionError(f"Invalid final size in {name}: FAT {link} / directory {last}")
|
||||
length = (count - 1) * SECTOR + last if count else 0
|
||||
data.extend(raw[addr:addr + length])
|
||||
break
|
||||
if name in files:
|
||||
raise AssertionError(f"Duplicate directory entry {name}")
|
||||
files[name] = bytes(data)
|
||||
occupied = {i for i in range(68) if raw[FAT + i] != 255}
|
||||
if owners != occupied:
|
||||
raise AssertionError(f"Leaked/missing granules {sorted(owners ^ occupied)}")
|
||||
return files
|
||||
|
||||
|
||||
def check_files(folder, stage, expected):
|
||||
actual = contents((folder / (stage + ".dsk")).read_bytes())
|
||||
if actual != expected:
|
||||
names = {name for name in actual.keys() | expected.keys() if actual.get(name) != expected.get(name)}
|
||||
raise AssertionError(f"{stage}: mismatched {[(n, len(actual.get(n,b'')), len(expected.get(n,b''))) for n in names]}")
|
||||
|
||||
|
||||
def fragment_second_granule(raw, filename):
|
||||
"""Relocate the second granule to the end of the disk without changing the file."""
|
||||
directory = range(FAT + SECTOR, (17 + 1) * 18 * SECTOR, 32)
|
||||
entry = next(row for row in directory if raw[row:row + 11] == filename)
|
||||
head = raw[entry + 13]
|
||||
second = raw[FAT + head]
|
||||
unused = 67
|
||||
assert head < 34 and second < 34 and raw[FAT + unused] == 255
|
||||
old = second * GRANULE
|
||||
new = (unused + 2) * GRANULE # Skip track 17 on the way to the final track.
|
||||
raw[new:new + GRANULE] = raw[old:old + GRANULE]
|
||||
raw[old:old + GRANULE] = bytes(GRANULE)
|
||||
raw[FAT + unused] = raw[FAT + second]
|
||||
raw[FAT + head] = unused
|
||||
raw[FAT + second] = 255
|
||||
|
||||
|
||||
def write_fixture(folder, tool, scenario):
|
||||
volume = folder / "VOL.DSK"
|
||||
volume.write_bytes(b"\xff" * (35 * 18 * SECTOR))
|
||||
files = {"SAFE.DAT": SENTINEL}
|
||||
if scenario == "F":
|
||||
files["FILL.DAT"] = FILL
|
||||
elif scenario == "W":
|
||||
files["LOG.BIN"] = b"replace this old chain" * 150
|
||||
files["DISTANT.DAT"] = DISTANT
|
||||
if scenario in ("S", "U"):
|
||||
files.update({"DISTANT.DAT": DISTANT, "RECORDS.TXT": TEXT_RECORDS})
|
||||
if scenario == "U":
|
||||
files["RESET.DAT"] = b"old log" * 800
|
||||
if scenario == "N":
|
||||
files.update({f"PAD{i}.DAT": bytes([i]) for i in range(7)})
|
||||
files["DELETE.DAT"] = b"multi-granule" * 400
|
||||
if scenario == "C":
|
||||
files["CROSS.DAT"] = b"aliased by fixture"
|
||||
for name, data in files.items():
|
||||
(folder / name).write_bytes(data)
|
||||
subprocess.run([tool, "--binary", volume, name], cwd=folder, check=True)
|
||||
assert contents(volume.read_bytes()) == files
|
||||
if scenario in ("S", "U"):
|
||||
raw = bytearray(volume.read_bytes())
|
||||
fragment_second_granule(raw, b"DISTANT DAT")
|
||||
assert contents(raw) == files
|
||||
volume.write_bytes(raw)
|
||||
if scenario == "C":
|
||||
raw = bytearray(volume.read_bytes())
|
||||
directory = FAT + SECTOR
|
||||
assert raw[directory:directory + 4] == b"SAFE" and raw[directory + 32:directory + 37] == b"CROSS"
|
||||
raw[directory + 32 + 13] = raw[directory + 13]
|
||||
volume.write_bytes(raw)
|
||||
if scenario == "N":
|
||||
(folder / "SECOND.DSK").write_bytes(volume.read_bytes())
|
||||
(folder / "initial.dsk").write_bytes(volume.read_bytes())
|
||||
(folder / "startup.cfg").write_text("0=VOL.DSK\n")
|
||||
return volume
|
||||
|
||||
|
||||
def inspect(folder, scenario):
|
||||
base = {"SAFE.DAT": SENTINEL}
|
||||
if scenario == "N":
|
||||
expected = {f"PAD{i}.DAT": bytes([i]) for i in range(7)}
|
||||
expected.update({"OVERLAND.BAK": SENTINEL, "HOLD.TXT": b"still open"})
|
||||
check_files(folder, "closed", expected)
|
||||
restored = {"SAFE.DAT": SENTINEL, "DELETE.DAT": b"multi-granule" * 400, "HOLD.TXT": b""}
|
||||
restored.update({f"PAD{i}.DAT": bytes([i]) for i in range(7)})
|
||||
check_files(folder, "rollback", restored)
|
||||
if (folder / "drive1.dsk").exists():
|
||||
second = contents((folder / "SECOND.DSK").read_bytes())
|
||||
second["DRIVEONE.DAT"] = second.pop("SAFE.DAT")
|
||||
del second["DELETE.DAT"]
|
||||
check_files(folder, "drive1", second)
|
||||
elif scenario == "U":
|
||||
patched = bytearray(DISTANT)
|
||||
patched[255:259] = b"SECT"
|
||||
patched[2302:2306] = b"GRAN"
|
||||
expected = {**base, "DISTANT.DAT": patched + b"END!", "RECORDS.TXT": TEXT_RECORDS.replace(b"EUROPA", b"PHOBOS"),
|
||||
"TRIPS.DAT": b"\0\xffABC", "RESET.DAT": b"NOW", "EMPTY.TXT": b"", "CREATED.TXT": b"LOG\r",
|
||||
"FORMAT.TXT": b"HELLO 00042\r"}
|
||||
check_files(folder, "closed", expected)
|
||||
assert (folder / "protected.dsk").read_bytes() == (folder / "closed.dsk").read_bytes()
|
||||
elif scenario == "S":
|
||||
patched = bytearray((i + sector) & 255 for sector in range(10) for i in range(256))
|
||||
patched.extend(b"\xa5" * 19)
|
||||
patched[255:259] = b"SECT"
|
||||
patched[2302:2306] = b"GRAN"
|
||||
patched[-3:] = b"TAIL++"
|
||||
patched[0] = ord("H")
|
||||
patched.extend(b"END!")
|
||||
check_files(folder, "closed", {**base, "DISTANT.DAT": DISTANT + b"!", "RECORDS.TXT": TEXT_RECORDS,
|
||||
"PATCH.BIN": patched})
|
||||
elif scenario == "T":
|
||||
check_files(folder, "protected", {**base, "REPORT.BIN": b"ORBIT"})
|
||||
assert (folder / "protected.dsk").read_bytes() == (folder / "refused.dsk").read_bytes()
|
||||
check_files(folder, "closed", {**base, "REPORT.BIN": b"OXBIT"})
|
||||
elif scenario in ("C", "B", "M"):
|
||||
assert (folder / "closed.dsk").read_bytes() == (folder / "initial.dsk").read_bytes()
|
||||
elif scenario == "P":
|
||||
expected = dict(base, **{"REPORT.TXT": b"MARS RIDGE\r"})
|
||||
check_files(folder, "protected", expected)
|
||||
assert (folder / "protected.dsk").read_bytes() == (folder / "closed.dsk").read_bytes()
|
||||
elif scenario == "F":
|
||||
expected = dict(base, **{"FILL.DAT": FILL, "LAST.DAT": b"abcdefghij" * 230})
|
||||
check_files(folder, "full", expected)
|
||||
# The four remaining bytes physically fit; the buffered next sector does not.
|
||||
# Existing committed bytes and all unrelated data must survive.
|
||||
actual = contents((folder / "closed.dsk").read_bytes())
|
||||
last = actual.pop("LAST.DAT")
|
||||
committed = expected.pop("LAST.DAT")
|
||||
assert actual == expected and last.startswith(committed)
|
||||
tail = last[len(committed):]
|
||||
assert tail == b"\xf3" * 4
|
||||
else:
|
||||
binary = b"".join(bytes((i + sec) & 255 for i in range(256)) for sec in range(9))
|
||||
first = {**base, "REPORT.TXT": b"MARS RIDGE\r", "LOG.BIN": binary[:256], "DISTANT.DAT": DISTANT}
|
||||
check_files(folder, "sector", first)
|
||||
first["LOG.BIN"] = binary
|
||||
check_files(folder, "granule", first)
|
||||
first["LOG.BIN"] += b"\xff\x00"
|
||||
first["DISTANT.DAT"] += b"\xfe"
|
||||
first["REPORT.TXT"] += b"SECOND\r"
|
||||
check_files(folder, "append", first)
|
||||
first.update({"LOG.BIN": b"", "ABSENT.TXT": b"A\r"})
|
||||
check_files(folder, "closed", first)
|
||||
first["BASIC.TXT"] = b"FROM BASIC\r"
|
||||
check_files(folder, "basic", first)
|
||||
|
||||
|
||||
def main():
|
||||
parser = argparse.ArgumentParser(description=__doc__)
|
||||
parser.add_argument("runtime", type=Path)
|
||||
parser.add_argument("core", type=Path)
|
||||
parser.add_argument("roms", type=Path)
|
||||
parser.add_argument("--profile", action="append", choices=("disk", "rgb-hd", "rgb-fd", "sdc"))
|
||||
parser.add_argument("--suite", choices=("all", "writes", "position", "update", "directory"), default="all")
|
||||
args = parser.parse_args()
|
||||
runtime, core, roms = (p.resolve(strict=True) for p in (args.runtime, args.core, args.roms))
|
||||
fixture = Path(__file__).resolve().parent
|
||||
env = dict(os.environ, VCC_CMOC_COMPAT="1", PATH=str(runtime / "bin") + os.pathsep + os.environ["PATH"])
|
||||
with tempfile.TemporaryDirectory(prefix="vcc-stream-writes-") as tmp:
|
||||
work = Path(tmp)
|
||||
compiler = [runtime / "bin/cmoc", "--coco", "-Werror"]
|
||||
subprocess.run(["xcrun", "clang++", "-std=c++17", "-O2", "-mmacosx-version-min=14.0",
|
||||
"-I", fixture.parents[2] / "src", fixture / "run.cpp", core, "-o", "run"], cwd=work, check=True)
|
||||
# The installed archive uses -O2. Exercise both caller settings against
|
||||
# that archive and separately compile the stream implementation at -O0.
|
||||
builds = (("writes", "qualification", "-O2", False), ("position", "position", "-O2", False),
|
||||
("update", "update", "-O0", False), ("update", "update", "-O2", False),
|
||||
("update", "update", "-O0", True), ("directory", "directory", "-O0", False),
|
||||
("directory", "directory", "-O2", False))
|
||||
for suite, source, level, compile_streams in builds:
|
||||
if args.suite not in ("all", suite):
|
||||
continue
|
||||
implementation = "source" if compile_streams else "staged"
|
||||
objects = []
|
||||
if suite == "position":
|
||||
for obj, defines in (("legacy-first.o", ["-DLEGACY_FIRST"]), ("stdio-first.o", [])):
|
||||
subprocess.run([*compiler, level, *defines, "-c", "-o", obj, fixture / "mixed-rewind.c"],
|
||||
cwd=work, env=env, check=True)
|
||||
objects.append(obj)
|
||||
if compile_streams:
|
||||
for unit in ("streams", "format", "fileformat"):
|
||||
source_path = fixture.parents[2] / "sdk/cmoc/compat" / (unit + ".c")
|
||||
subprocess.run([*compiler, level, "-c", "-o", unit + ".o", source_path],
|
||||
cwd=work, env=env, check=True)
|
||||
objects.append(unit + ".o")
|
||||
disk_library = "-ldecbfile"
|
||||
if suite == "directory":
|
||||
# Swap only the ROM I/O object in a disposable copy so a guest
|
||||
# can exercise one transient write failure without a production hook.
|
||||
shutil.copyfile(runtime / "share/cmoc/lib/libdecbfile.a", work / "libdiskfault.a")
|
||||
subprocess.run([*compiler, level, "-c", "-o", "dskcon.o", fixture / "refuse-fat.c"],
|
||||
cwd=work, env=env, check=True)
|
||||
subprocess.run([runtime / "bin/lwar", "--replace", "libdiskfault.a", "dskcon.o"],
|
||||
cwd=work, env=env, check=True)
|
||||
objects.append("-L.")
|
||||
disk_library = "-ldiskfault"
|
||||
if suite == "update":
|
||||
objects.append(fixture / "formatted.c")
|
||||
if not compile_streams:
|
||||
objects.append("-lvcccompat")
|
||||
binary = work / (source + ".bin")
|
||||
# The all-source -O0 qualification program needs another 2 KiB
|
||||
# below the guest stack and the ROM mapped at $8000.
|
||||
origin = "--org=2000" if compile_streams else "--org=2800"
|
||||
subprocess.run([*compiler, level, origin, "-o", binary, fixture / (source + ".c"),
|
||||
*objects, disk_library], cwd=work, env=env, check=True)
|
||||
for profile in args.profile or ("disk", "rgb-fd", "rgb-hd", "sdc"):
|
||||
if suite == "writes":
|
||||
cases = [("W", "streams and BASIC exchange"), ("F", "disk-full partial write"),
|
||||
("P", "protected flush")]
|
||||
if profile == "disk":
|
||||
cases += [("C", "cross-linked FAT refusal"), ("B", "active Disk BASIC file refusal")]
|
||||
elif suite == "directory":
|
||||
cases = [("N", "rename without replacement and reclaim removed files")]
|
||||
elif suite == "position":
|
||||
cases = [("S", "seek, tell, rewind and in-place overwrite"),
|
||||
("T", "protected seek and rewind recovery")]
|
||||
if profile == "disk":
|
||||
cases.append(("M", "stdio and legacy rewind in both header orders"))
|
||||
else:
|
||||
cases = [("U", "update directions, protected writes, unchanged neighbors and EOF growth")]
|
||||
for cpu in ("6809", "6309"):
|
||||
for scenario, description in cases:
|
||||
with tempfile.TemporaryDirectory(dir=work) as test:
|
||||
folder = Path(test)
|
||||
volume = write_fixture(folder, runtime / "bin/writecocofile", scenario)
|
||||
subprocess.run([work / "run", roms, volume, binary, profile, cpu, scenario],
|
||||
cwd=folder, check=True)
|
||||
inspect(folder, scenario)
|
||||
print(f"PASS {profile} {cpu} {level} {implementation}: {description}", flush=True)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
@@ -0,0 +1,67 @@
|
||||
// SPDX-License-Identifier: GPL-3.0-or-later
|
||||
#include <stdio.h>
|
||||
#include <cmoc.h>
|
||||
#include <coco.h>
|
||||
#include <decbfile.h>
|
||||
|
||||
#define CHECK(test) do { if (!(test)) { printf("FAIL LINE %u ERR %u\n", (unsigned)__LINE__, (unsigned)vcc_file_error()); return 1; } } while (0)
|
||||
#define REFUSED(call, error) ((call) == EOF && vcc_file_error() == (error))
|
||||
|
||||
extern byte refuseNextFATWrite;
|
||||
|
||||
int main(void)
|
||||
{
|
||||
DECBDrive registry;
|
||||
byte before, after, unused;
|
||||
cls(1);
|
||||
printf("SELECT CHECK\n");
|
||||
byte testSecondDrive = waitkey(0) == 'D';
|
||||
if (testSecondDrive)
|
||||
{
|
||||
CHECK(rename("SAFE.DAT:1", "DRIVEONE.DAT:1") == 0);
|
||||
CHECK(remove("DELETE.DAT:1") == 0);
|
||||
}
|
||||
CHECK(decb_init(®istry, 1) == DECB_OK && decb_registerDrive(0) == DECB_OK);
|
||||
CHECK(decb_getNumFreeGranules(0, &before) == DECB_OK);
|
||||
unused = decb_findFreeGranule(0);
|
||||
CHECK(unused < 68 && decb_setFATEntry(0, unused, 0xc1) == DECB_OK);
|
||||
CHECK(REFUSED(remove("SAFE.DAT"), DECB_ERR_ALREADY_EXISTS));
|
||||
CHECK(REFUSED(rename("SAFE.DAT", "SAVED.DAT"), DECB_ERR_ALREADY_EXISTS));
|
||||
CHECK(decb_setFATEntry(0, unused, 0xff) == DECB_OK && decb_writeFAT(0) == DECB_OK);
|
||||
|
||||
CHECK(REFUSED(rename("SAFE.DAT", "MOVED.DAT:1"), DECB_ERR_INVALID_ARGUMENT));
|
||||
CHECK(REFUSED(rename("SAFE.DAT", NULL), DECB_ERR_INVALID_ARGUMENT));
|
||||
CHECK(REFUSED(rename(NULL, "NEW.DAT"), DECB_ERR_INVALID_ARGUMENT));
|
||||
CHECK(REFUSED(remove("BAD.*"), DECB_ERR_INVALID_ARGUMENT));
|
||||
CHECK(REFUSED(remove(NULL), DECB_ERR_INVALID_ARGUMENT));
|
||||
CHECK(REFUSED(remove("ABSENT.DAT"), DECB_ERR_NOT_FOUND));
|
||||
CHECK(REFUSED(rename("ABSENT.DAT", "NEW.DAT"), DECB_ERR_NOT_FOUND));
|
||||
CHECK(REFUSED(rename("Safe.Dat", "Safe.dat:0"), DECB_ERR_ALREADY_EXISTS));
|
||||
// The existing destination is past a full directory sector.
|
||||
CHECK(REFUSED(rename("SAFE.DAT", "DELETE.DAT"), DECB_ERR_ALREADY_EXISTS));
|
||||
|
||||
FILE *reader = fopen("Safe.Dat", "r");
|
||||
FILE *writer = fopen("Hold.Txt:0", "w");
|
||||
CHECK(reader && writer && fputs("still open", writer) == 0);
|
||||
CHECK(REFUSED(remove("SAFE.DAT:0"), DECB_ERR_ALREADY_EXISTS));
|
||||
CHECK(REFUSED(rename("safe.dat", "SAVED.DAT"), DECB_ERR_ALREADY_EXISTS));
|
||||
CHECK(REFUSED(rename("DELETE.DAT", "hold.txt"), DECB_ERR_ALREADY_EXISTS));
|
||||
CHECK(REFUSED(remove("HOLD.TXT"), DECB_ERR_ALREADY_EXISTS));
|
||||
// The directory write succeeds, then the FAT refuses a single write.
|
||||
// Report failure and recover the pre-delete directory when it is safe.
|
||||
refuseNextFATWrite = 1;
|
||||
CHECK(REFUSED(remove("DELETE.DAT"), DECB_ERR_IO) && !refuseNextFATWrite);
|
||||
printf("RECLAIM REFUSED\n");
|
||||
while (waitkey(0) != 'X') {}
|
||||
CHECK(rename("DELETE.DAT", "Doomed.Dat:0") == 0);
|
||||
CHECK(remove("doomed.dat") == 0 && REFUSED(remove("DOOMED.DAT"), DECB_ERR_NOT_FOUND));
|
||||
CHECK(fgetc(reader) == 'p' && fclose(reader) == 0 && fclose(writer) == 0);
|
||||
CHECK(rename("SAFE.DAT", "OverLand.Bak") == 0);
|
||||
FILE *empty = fopen("EMPTY.BIN", "wb");
|
||||
CHECK(empty && fclose(empty) == 0 && remove("empty.bin") == 0);
|
||||
// DELETE.DAT used three granules; HOLD.TXT now uses one of the free granules.
|
||||
CHECK(decb_getNumFreeGranules(0, &after) == DECB_OK && after == before + 2);
|
||||
decb_shutdown();
|
||||
printf("DIRECTORY PASSED\n");
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,59 @@
|
||||
// SPDX-License-Identifier: GPL-3.0-or-later
|
||||
#include <stdio.h>
|
||||
#include <cmoc.h>
|
||||
#include <decbfile.h>
|
||||
|
||||
#define CHECK(test) do { if (!(test)) { printf("FAIL LINE %u ERR %u\n", (unsigned)__LINE__, (unsigned)vcc_file_error()); return 1; } } while (0)
|
||||
|
||||
static int writeArgs(FILE *stream, const char *format, ...)
|
||||
{
|
||||
va_list args;
|
||||
va_start(args, format);
|
||||
int result = vfprintf(stream, format, args);
|
||||
va_end(args);
|
||||
return result;
|
||||
}
|
||||
|
||||
int formattedChecks(void)
|
||||
{
|
||||
FILE *stream = fopen("FORMAT.TXT", "w+b");
|
||||
char expected[64], actual[64];
|
||||
const char *format = "%%|%-6.3s|%04d|%.0u|%lo|%lX|%c|%lx";
|
||||
int length = snprintf(expected, sizeof(expected), format, "MARS", -7, 0U,
|
||||
0xFFFFFFFFUL, 0xDEADBEEFUL, 0, 0xABCUL);
|
||||
CHECK(stream && fprintf(stream, NULL) < 0 && ftell(stream) == 0L);
|
||||
CHECK(length > 0 && (unsigned) length < sizeof(expected));
|
||||
CHECK(writeArgs(stream, format, "MARS", -7, 0U, 0xFFFFFFFFUL, 0xDEADBEEFUL, 0, 0xABCUL) == length);
|
||||
CHECK(fseek(stream, 0L, SEEK_SET) == 0 && fread(actual, 1, length, stream) == (unsigned) length);
|
||||
CHECK(!memcmp(expected, actual, length));
|
||||
// A read with no EOF requires positioning, including for formatted writes.
|
||||
CHECK(fprintf(stream, "%s", "X") < 0 && vcc_file_error() == DECB_ERR_INVALID_ARGUMENT);
|
||||
CHECK(fseek(stream, 0L, SEEK_SET) == 0 && fprintf(stream, "%530s", "X") == 530);
|
||||
CHECK(fprintf(stream, "ABC%f") < 0);
|
||||
CHECK(fprintf(stream, "%32768d", 0) < 0 && ftell(stream) == 533L);
|
||||
CHECK(fseek(stream, 0L, SEEK_SET) == 0);
|
||||
for (unsigned i = 0; i < 529; ++i) CHECK(fgetc(stream) == ' ');
|
||||
CHECK(fgetc(stream) == 'X' && fgetc(stream) == 'A' && fgetc(stream) == 'B' && fgetc(stream) == 'C');
|
||||
// A valid format can emit more bytes than a 16-bit int can report.
|
||||
CHECK(fseek(stream, 0L, SEEK_SET) == 0 && fprintf(stream, "%32767sX", "") < 0);
|
||||
CHECK(ftell(stream) == 32768L && fclose(stream) == 0);
|
||||
|
||||
stream = fopen("FORMAT.TXT", "w");
|
||||
CHECK(stream && fprintf(stream, "") == 0);
|
||||
CHECK(fprintf(stream, "%s %05u\n", "HELLO", 42U) == 12 && fclose(stream) == 0);
|
||||
stream = fopen("FORMAT.TXT", "r");
|
||||
CHECK(stream && fprintf(stream, "") < 0 && vcc_file_error() == DECB_ERR_INVALID_ARGUMENT);
|
||||
CHECK(fclose(stream) == 0);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int formattedProtectedChecks(void)
|
||||
{
|
||||
// A short write can succeed in memory. A longer one reaches the
|
||||
// protected disk while formatting and reports the failure immediately.
|
||||
FILE *stream = fopen("SAFE.DAT", "r+b");
|
||||
CHECK(stream && fprintf(stream, "%s", "X") == 1);
|
||||
CHECK(fprintf(stream, "%300s", "Y") < 0 && ferror(stream) == DECB_ERR_IO);
|
||||
CHECK(fprintf(stream, "") < 0 && fclose(stream) == EOF);
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
// SPDX-License-Identifier: GPL-3.0-or-later
|
||||
// Compile both ways and link with a third source that uses stdio rewind alone.
|
||||
#ifdef LEGACY_FIRST
|
||||
#include <disk.h>
|
||||
#include <stdio.h>
|
||||
#define CHECK_ORDER checkLegacyFirst
|
||||
#else
|
||||
#include <stdio.h>
|
||||
#include <disk.h>
|
||||
#define CHECK_ORDER checkStdioFirst
|
||||
#endif
|
||||
|
||||
int CHECK_ORDER(void)
|
||||
{
|
||||
static byte fat[MAX_NUM_GRANULES];
|
||||
static struct FileDesc descriptor;
|
||||
void (*resetDescriptor)(struct FileDesc *) = rewind;
|
||||
void (*resetStream)(FILE *) = vcc_rewind;
|
||||
char bytes[3];
|
||||
FILE *stream;
|
||||
int ok;
|
||||
|
||||
// initdisk installs this source file’s FAT buffer without clearing the cache flag.
|
||||
fatUpToDate = FALSE;
|
||||
if (!initdisk(fat) || !openfile(&descriptor, "SAFE.DAT"))
|
||||
return 1;
|
||||
ok = read(&descriptor, bytes, 3) == 3 && memcmp(bytes, "pre", 3) == 0;
|
||||
resetDescriptor(&descriptor);
|
||||
ok = ok && read(&descriptor, bytes, 3) == 3 && memcmp(bytes, "pre", 3) == 0;
|
||||
if (close(&descriptor) || !ok)
|
||||
return 1;
|
||||
|
||||
stream = fopen("SAFE.DAT", "rb");
|
||||
if (!stream)
|
||||
return 1;
|
||||
ok = fgetc(stream) == 'p' && fgetc(stream) == 'r';
|
||||
resetStream(stream);
|
||||
ok = ok && ftell(stream) == 0L && fgetc(stream) == 'p';
|
||||
return fclose(stream) || !ok;
|
||||
}
|
||||
@@ -0,0 +1,160 @@
|
||||
// SPDX-License-Identifier: GPL-3.0-or-later
|
||||
// Host-created data and the final raw disk are checked independently of this API.
|
||||
#include <stdio.h>
|
||||
#include <limits.h>
|
||||
#include <cmoc.h>
|
||||
#include <coco.h>
|
||||
#include <decbfile.h>
|
||||
|
||||
#define CHECK(test) do { if (!(test)) { printf("FAIL LINE %u ERR %u\n", (unsigned)__LINE__, (unsigned)vcc_file_error()); return 1; } } while (0)
|
||||
|
||||
int checkLegacyFirst(void);
|
||||
int checkStdioFirst(void);
|
||||
|
||||
static byte block[256];
|
||||
|
||||
static int binaryInput(void)
|
||||
{
|
||||
FILE *f = fopen("DISTANT.DAT", "rb");
|
||||
CHECK(f && ftell(f) == 0L);
|
||||
CHECK(fseek(f, 255L, SEEK_SET) == 0);
|
||||
CHECK(fread(block, 1, 2, f) == 2 && block[0] == 249 && block[1] == 1);
|
||||
// This pair of bytes crosses a deliberately noncontiguous granule.
|
||||
CHECK(fseek(f, 2303L, SEEK_SET) == 0 && ftell(f) == 2303L);
|
||||
CHECK(fgetc(f) == 1 && fgetc(f) == 9);
|
||||
CHECK(fseek(f, -2L, SEEK_CUR) == 0 && fgetc(f) == 1);
|
||||
CHECK(fseek(f, -2L, SEEK_END) == 0 && ftell(f) == 65535L);
|
||||
CHECK(fgetc(f) == 248 && ftell(f) == 65536L);
|
||||
CHECK(fgetc(f) == 0 && ftell(f) == 65537L);
|
||||
CHECK(fgetc(f) == EOF && feof(f));
|
||||
CHECK(fseek(f, LONG_MAX, SEEK_CUR) != 0 && feof(f) && ftell(f) == 65537L);
|
||||
CHECK(fseek(f, LONG_MIN, SEEK_END) != 0 && feof(f) && ftell(f) == 65537L);
|
||||
CHECK(fseek(f, 0L, 17) != 0 && feof(f));
|
||||
CHECK(fseek(f, -65537L, SEEK_CUR) == 0 && !feof(f) && ftell(f) == 0L);
|
||||
CHECK(fseek(f, -1L, SEEK_SET) != 0 && ftell(f) == 0L);
|
||||
CHECK(fseek(f, 65538L, SEEK_SET) != 0 && ftell(f) == 0L && fgetc(f) == 0);
|
||||
CHECK(fseek(f, 65536L, SEEK_SET) == 0 && ftell(f) == 65536L && fgetc(f) == 0);
|
||||
CHECK(fgetc(f) == EOF && feof(f));
|
||||
rewind(f);
|
||||
CHECK(ftell(f) == 0L);
|
||||
CHECK(!feof(f));
|
||||
CHECK(!ferror(f));
|
||||
CHECK(fgetc(f) == 0);
|
||||
CHECK(fclose(f) == 0);
|
||||
f = fopen("DISTANT.DAT", "ab");
|
||||
CHECK(f && ftell(f) == 65537L && fseek(f, 0L, SEEK_SET) == 0);
|
||||
CHECK(ftell(f) == 0L && fputc('!', f) == '!' && ftell(f) == 65538L && fclose(f) == 0);
|
||||
CHECK(ftell(NULL) == -1L && fseek(NULL, 0L, SEEK_SET) != 0);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int textBookmarks(void)
|
||||
{
|
||||
FILE *f = fopen("RECORDS.TXT", "r");
|
||||
long before, after;
|
||||
char line[16];
|
||||
CHECK(f && fread(block, 1, 255, f) == 255);
|
||||
before = ftell(f); // The CR is the final byte of sector 0.
|
||||
CHECK(before >= 0 && fgetc(f) == '\n');
|
||||
after = ftell(f); // The paired LF starts sector 1.
|
||||
clearerr(f); // Preserve the pending CRLF translation.
|
||||
CHECK(after >= 0 && after != before && fgetc(f) == 'M');
|
||||
CHECK(fseek(f, before, SEEK_SET) == 0 && fgetc(f) == '\n' && fgetc(f) == 'M');
|
||||
CHECK(fseek(f, after, SEEK_SET) == 0);
|
||||
CHECK(fgets(line, sizeof(line), f) && !strcmp(line, "MARS\n"));
|
||||
CHECK(fseek(f, after, SEEK_SET) == 0);
|
||||
CHECK(fseek(f, 1L, SEEK_CUR) != 0 && fseek(f, -1L, SEEK_END) != 0);
|
||||
CHECK(ftell(f) == after && fgetc(f) == 'M');
|
||||
CHECK(fseek(f, 0L, SEEK_END) == 0 && fgetc(f) == EOF && feof(f));
|
||||
rewind(f);
|
||||
CHECK(!feof(f) && ftell(f) == 0L && fgetc(f) == 'X');
|
||||
CHECK(fclose(f) == 0);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int editBinary(void)
|
||||
{
|
||||
unsigned sector, i;
|
||||
const long originalSize = 2579L;
|
||||
FILE *f = fopen("PATCH.BIN", "wb");
|
||||
FILE *reader;
|
||||
CHECK(f && ftell(f) == 0L && fseek(f, 0L, SEEK_END) == 0);
|
||||
for (sector = 0; sector < 10; ++sector) {
|
||||
for (i = 0; i < sizeof(block); ++i) block[i] = (byte)(sector + i);
|
||||
CHECK(fwrite(block, 1, sizeof(block), f) == sizeof(block));
|
||||
}
|
||||
memset(block, 0xa5, 19);
|
||||
CHECK(fwrite(block, 1, 19, f) == 19 && ftell(f) == originalSize);
|
||||
// The last 19 bytes are still pending when the invalid seek is refused.
|
||||
CHECK(fseek(f, 1L, SEEK_END) != 0 && ftell(f) == originalSize);
|
||||
CHECK(fseek(f, -19L, SEEK_END) == 0 && ftell(f) == 2560L);
|
||||
reader = fopen("PATCH.BIN", "rb");
|
||||
CHECK(reader && fseek(reader, -1L, SEEK_END) == 0 && fgetc(reader) == 0xa5);
|
||||
CHECK(fseek(f, 255L, SEEK_SET) == 0 && fputs("SECT", f) >= 0 && ftell(f) == 259L);
|
||||
CHECK(fflush(f) == 0);
|
||||
CHECK(fseek(f, 2302L, SEEK_SET) == 0 && fputs("GRAN", f) >= 0);
|
||||
CHECK(fseek(f, -3L, SEEK_END) == 0 && fputs("TAIL++", f) >= 0);
|
||||
CHECK(ftell(f) == originalSize + 3L && fflush(f) == 0);
|
||||
CHECK(fseek(reader, 0L, SEEK_END) == 0 && ftell(reader) == originalSize);
|
||||
CHECK(fgetc(reader) == EOF && feof(reader));
|
||||
CHECK(fclose(reader) == 0);
|
||||
rewind(f);
|
||||
CHECK(ftell(f) == 0L && fputc('H', f) == 'H' && fclose(f) == 0);
|
||||
f = fopen("PATCH.BIN", "ab");
|
||||
CHECK(f && ftell(f) == originalSize + 3L && fseek(f, 3L, SEEK_SET) == 0);
|
||||
CHECK(ftell(f) == 3L && fputs("END", f) >= 0 && ftell(f) == originalSize + 6L);
|
||||
rewind(f);
|
||||
CHECK(ftell(f) == 0L && fputc('!', f) == '!' && ftell(f) == originalSize + 7L);
|
||||
CHECK(fclose(f) == 0);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int protectedSeek(void)
|
||||
{
|
||||
FILE *f = fopen("REPORT.BIN", "wb");
|
||||
CHECK(f && fputs("ORBIT", f) >= 0 && fseek(f, 1L, SEEK_SET) == 0);
|
||||
CHECK(fputc('X', f) == 'X' && ftell(f) == 2L);
|
||||
printf("PROTECT NOW\n");
|
||||
while (waitkey(0) != 'X') {}
|
||||
CHECK(fseek(f, 4L, SEEK_SET) != 0 && ferror(f) == DECB_ERR_IO);
|
||||
CHECK(ftell(f) == 2L);
|
||||
// Only this writer is cleared. The second stream has its own EOF.
|
||||
FILE *reader = fopen("REPORT.BIN", "rb");
|
||||
CHECK(reader && fseek(reader, 0L, SEEK_END) == 0 && fgetc(reader) == EOF && feof(reader));
|
||||
clearerr(f);
|
||||
CHECK(ferror(f) == 0 && ftell(f) == 2L && feof(reader));
|
||||
CHECK(fflush(f) == EOF && ferror(f) == DECB_ERR_IO);
|
||||
clearerr(reader);
|
||||
CHECK(!feof(reader) && !ferror(reader) && ferror(f) == DECB_ERR_IO);
|
||||
CHECK(ftell(reader) == 5L && fgetc(reader) == EOF && feof(reader));
|
||||
CHECK(fclose(reader) == 0);
|
||||
clearerr(reader);
|
||||
CHECK(vcc_file_error() == DECB_ERR_INVALID_ARGUMENT);
|
||||
rewind(f); // Still protected: position and error survive.
|
||||
CHECK(ftell(f) == 2L && ferror(f) == DECB_ERR_IO);
|
||||
printf("ALLOW RETRY\n");
|
||||
while (waitkey(0) != 'Y') {}
|
||||
rewind(f); // The buffered overwrite can now be delivered.
|
||||
CHECK(ftell(f) == 0L && !ferror(f) && fclose(f) == 0);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int main(void)
|
||||
{
|
||||
int result = 1;
|
||||
cls(1);
|
||||
printf("SELECT CHECK\n");
|
||||
switch (waitkey(0)) {
|
||||
case 'M':
|
||||
result = checkLegacyFirst() || checkStdioFirst();
|
||||
break;
|
||||
case 'S':
|
||||
result = binaryInput() || textBookmarks() || editBinary();
|
||||
break;
|
||||
case 'T':
|
||||
result = protectedSeek();
|
||||
break;
|
||||
}
|
||||
if (!result) printf("POSITIONS PASSED\n");
|
||||
return result;
|
||||
}
|
||||
@@ -0,0 +1,145 @@
|
||||
// SPDX-License-Identifier: GPL-3.0-or-later
|
||||
// The host examines actual directory, FAT and sectors at each pause.
|
||||
#include <stdio.h>
|
||||
#include <cmoc.h>
|
||||
#include <coco.h>
|
||||
#include <decbfile.h>
|
||||
|
||||
#define CHECK(test) do { if (!(test)) { printf("FAIL LINE %u ERR %u\n", (unsigned)__LINE__, (unsigned)vcc_file_error()); return 1; } } while (0)
|
||||
static byte block[256];
|
||||
static byte ten[10];
|
||||
static DECBDrive registry;
|
||||
|
||||
static void checkpoint(const char *name, char key)
|
||||
{
|
||||
printf("%s\n", name);
|
||||
while (waitkey(0) != key) {}
|
||||
}
|
||||
|
||||
static int writeFiles(void)
|
||||
{
|
||||
unsigned i, sector;
|
||||
FILE *text, *binary, *snapshot;
|
||||
char line[20];
|
||||
byte before, after, unused;
|
||||
// A caller with an unpublished low-level FAT owns it until it is saved.
|
||||
CHECK(decb_init(®istry, 1) == DECB_OK && decb_registerDrive(0) == DECB_OK);
|
||||
CHECK(decb_getNumFreeGranules(0, &before) == DECB_OK);
|
||||
unused = decb_findFreeGranule(0);
|
||||
CHECK(unused < 68 && decb_setFATEntry(0, unused, 0xc1) == DECB_OK);
|
||||
CHECK(!fopen("BUSY.TXT", "w") && vcc_file_error() == DECB_ERR_ALREADY_EXISTS);
|
||||
CHECK(decb_setFATEntry(0, unused, 0xff) == DECB_OK && decb_writeFAT(0) == DECB_OK);
|
||||
// Interleave two writers so a stale cached directory or FAT loses a file.
|
||||
text = fopen("REPORT.TXT", "w");
|
||||
binary = fopen("LOG.BIN:0", "wb");
|
||||
CHECK(text && binary);
|
||||
CHECK(fputs("MARS RIDGE\n", text) >= 0);
|
||||
CHECK(fwrite(NULL, 0, 70, binary) == 0 && fwrite(NULL, 70, 0, binary) == 0);
|
||||
for (i = 0; i < 256; ++i) block[i] = (byte)i;
|
||||
CHECK(fwrite(block, 16, 16, binary) == 16);
|
||||
CHECK(fflush(NULL) == 0 && fflush(text) == 0);
|
||||
checkpoint("FIRST FLUSH", 'X');
|
||||
|
||||
CHECK(fclose(text) == 0);
|
||||
CHECK(fopen("log.bin", "ab") == NULL); // another writer owns the same file
|
||||
snapshot = fopen("log.bin", "rb");
|
||||
CHECK(snapshot && fread(block, 1, 256, snapshot) == 256);
|
||||
for (i = 0; i < 256; ++i) CHECK(block[i] == (byte)i);
|
||||
for (sector = 1; sector < 9; ++sector) {
|
||||
for (i = 0; i < 256; ++i) block[i] = (byte)(i + sector);
|
||||
CHECK(fwrite(block, 16, 16, binary) == 16);
|
||||
}
|
||||
CHECK(fflush(binary) == 0);
|
||||
CHECK(fgetc(snapshot) == EOF && feof(snapshot) && !ferror(snapshot));
|
||||
CHECK(fclose(snapshot) == 0 && fclose(binary) == 0);
|
||||
checkpoint("FULL GRANULE", 'Y');
|
||||
|
||||
text = fopen("REPORT.TXT", "a");
|
||||
binary = fopen("LOG.BIN", "ab");
|
||||
CHECK(text && binary);
|
||||
CHECK(fputc(0x1ff, binary) == 255 && fputc(0, binary) == 0);
|
||||
CHECK(fputs("SECOND\n", text) >= 0);
|
||||
CHECK(fclose(text) == 0 && fclose(binary) == 0);
|
||||
text = fopen("REPORT.TXT", "r");
|
||||
CHECK(text && fgets(line, sizeof(line), text) && strcmp(line, "MARS RIDGE\n") == 0);
|
||||
CHECK(fgets(line, sizeof(line), text) && strcmp(line, "SECOND\n") == 0);
|
||||
CHECK(fclose(text) == 0);
|
||||
// This file already spans 65,537 bytes; the append position must not wrap.
|
||||
binary = fopen("DISTANT.DAT", "ab");
|
||||
CHECK(binary && fputc(0xfe, binary) == 254 && fclose(binary) == 0);
|
||||
checkpoint("APPENDED", 'Z');
|
||||
|
||||
binary = fopen("LOG.BIN", "wb"); // truncation gives back an occupied granule
|
||||
CHECK(binary && fflush(binary) == 0 && fclose(binary) == 0);
|
||||
text = fopen("ABSENT.TXT", "a"); // append creates a missing file
|
||||
CHECK(text && fputs("A\n", text) >= 0 && fclose(text) == 0);
|
||||
// The library created two files and shrank the original two-granule log.
|
||||
CHECK(decb_getNumFreeGranules(0, &after) == DECB_OK && after + 1 == before);
|
||||
decb_shutdown();
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int fillDisk(void)
|
||||
{
|
||||
FILE *out = fopen("LAST.DAT", "wb");
|
||||
unsigned i;
|
||||
CHECK(out);
|
||||
for (i = 0; i < 10; ++i) ten[i] = (byte)('a' + i);
|
||||
// One free granule before open. Check that fwrite counts complete items
|
||||
// and exposes the full-volume error whether a sector was buffered or not.
|
||||
for (i = 0; i < 230; ++i) CHECK(fwrite(ten, 10, 1, out) == 1);
|
||||
CHECK(fflush(out) == 0);
|
||||
checkpoint("LAST FOUR BYTES", 'X');
|
||||
// Four bytes fit on disk and one 256-byte sector fits in the buffer.
|
||||
// 260 bytes accepted means 86 complete three-byte objects.
|
||||
byte tail[270];
|
||||
memset(tail, 0xf3, sizeof(tail));
|
||||
CHECK(fwrite(tail, 3, 90, out) == 86);
|
||||
CHECK(ferror(out) == DECB_ERR_OUT_OF_SPACE);
|
||||
CHECK(fclose(out) == EOF);
|
||||
CHECK(fopen("NOSPACE.DAT", "wb") == NULL && vcc_file_error() == DECB_ERR_OUT_OF_SPACE);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int protectDuringFlush(void)
|
||||
{
|
||||
FILE *out = fopen("REPORT.TXT", "w");
|
||||
CHECK(out && fputs("MARS RIDGE\n", out) >= 0 && fflush(out) == 0);
|
||||
CHECK(fputs("UNSAVED\n", out) >= 0);
|
||||
checkpoint("PROTECT NOW", 'X');
|
||||
CHECK(fflush(out) == EOF && ferror(out) == DECB_ERR_IO);
|
||||
CHECK(fclose(out) == EOF && vcc_file_error() == DECB_ERR_IO);
|
||||
CHECK(!fopen("REPORT.TXT", "w") && vcc_file_error() == DECB_ERR_IO);
|
||||
CHECK(!fopen("ABSENT.TXT", "w") && vcc_file_error() == DECB_ERR_IO);
|
||||
CHECK(rename("SAFE.DAT", "SAVED.DAT") == EOF && vcc_file_error() == DECB_ERR_IO);
|
||||
CHECK(remove("SAFE.DAT") == EOF && vcc_file_error() == DECB_ERR_IO);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int main(void)
|
||||
{
|
||||
int result;
|
||||
cls(1);
|
||||
printf("SELECT CHECK\n");
|
||||
switch(waitkey(0)) {
|
||||
case 'B': // Disk BASIC has left an input file open on this drive.
|
||||
CHECK(!fopen("SAFE.DAT", "w") && vcc_file_error() == DECB_ERR_ALREADY_EXISTS);
|
||||
CHECK(rename("SAFE.DAT", "SAVED.DAT") == EOF && vcc_file_error() == DECB_ERR_ALREADY_EXISTS);
|
||||
CHECK(remove("SAFE.DAT") == EOF && vcc_file_error() == DECB_ERR_ALREADY_EXISTS);
|
||||
result = 0;
|
||||
break;
|
||||
case 'C': // Two directory entries name the same physical chain.
|
||||
CHECK(!fopen("SAFE.DAT", "w") && vcc_file_error() == DECB_ERR_CORRUPT_FAT);
|
||||
CHECK(!fopen("NEW.DAT", "a") && vcc_file_error() == DECB_ERR_CORRUPT_FAT);
|
||||
CHECK(rename("SAFE.DAT", "SAVED.DAT") == EOF && vcc_file_error() == DECB_ERR_CORRUPT_FAT);
|
||||
CHECK(remove("SAFE.DAT") == EOF && vcc_file_error() == DECB_ERR_CORRUPT_FAT);
|
||||
result = 0;
|
||||
break;
|
||||
case 'W': result = writeFiles(); break;
|
||||
case 'F': result = fillDisk(); break;
|
||||
case 'P': result = protectDuringFlush(); break;
|
||||
default: return 1;
|
||||
}
|
||||
if (!result) printf("WRITES PASSED\n");
|
||||
return result;
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
// SPDX-License-Identifier: GPL-3.0-or-later
|
||||
// Route every operation through the same ROM routine as decbfile, except for
|
||||
// one explicitly armed FAT write. This exercises a partial remove on a real
|
||||
// guest volume without changing either the library or the machine controller.
|
||||
#define decb_dskcon testROMDiskIO
|
||||
#include "../../../third_party/cmoc-libraries/decbfile/dskcon.c"
|
||||
#undef decb_dskcon
|
||||
|
||||
byte refuseNextFATWrite;
|
||||
|
||||
byte decb_dskcon(byte operation, void *buffer, byte drive, byte track, byte sector)
|
||||
{
|
||||
if (refuseNextFATWrite && operation == DECB_DSKCON_WRITE && track == 17 && sector == 2)
|
||||
{
|
||||
refuseNextFATWrite = 0;
|
||||
return FALSE;
|
||||
}
|
||||
return testROMDiskIO(operation, buffer, drive, track, sector);
|
||||
}
|
||||
@@ -0,0 +1,143 @@
|
||||
// SPDX-License-Identifier: GPL-3.0-or-later
|
||||
#include "../basic-disk-session.h"
|
||||
|
||||
using namespace BasicDiskTests;
|
||||
|
||||
static void snapshot(Session& session, const char *name) {
|
||||
const auto& original = session.original;
|
||||
Bytes volume;
|
||||
if (session.profile == Profile::rgbHD) {
|
||||
const auto& vhd = session.hard->image(0)->bytes();
|
||||
volume.assign(vhd.begin() + rgbStart, vhd.begin() + rgbStart + original.size());
|
||||
require(session.floppy->image(0)->bytes() == original, "Hard-disk run changed FD0");
|
||||
} else if (session.sdc) {
|
||||
volume = readFile(session.diskPath);
|
||||
} else {
|
||||
volume = session.floppy->image(0)->bytes();
|
||||
if (session.hard) require(!session.hard->image(0)->dirty(), "DRIVE OFF wrote to the VHD");
|
||||
}
|
||||
std::ofstream out(name, std::ios::binary);
|
||||
out.write(reinterpret_cast<const char*>(volume.data()), volume.size());
|
||||
require(bool(out), "Could not save volume at a checkpoint");
|
||||
}
|
||||
|
||||
static void protect(Session& session, bool enabled) {
|
||||
if (session.sdc) session.sdc->setWriteProtected(enabled);
|
||||
else if (session.profile == Profile::rgbHD) session.hard->image(0)->protect(enabled);
|
||||
else session.floppy->setWriteProtected(0, enabled);
|
||||
}
|
||||
|
||||
// Additional matrix chords for ordinary Disk BASIC file commands.
|
||||
static void basic(Session& session, const std::string& command) {
|
||||
std::vector<std::uint64_t> chords;
|
||||
for (const auto c : command) {
|
||||
unsigned bit;
|
||||
bool shift = false;
|
||||
if (c >= 'A' && c <= 'Z') bit = c - '@';
|
||||
else if (c >= '0' && c <= '9') bit = c - '0' + 32;
|
||||
else if (c == ' ') bit = 31;
|
||||
else if (c == ':') bit = 42;
|
||||
else if (c == ',') bit = 44;
|
||||
else if (c == '.') bit = 46;
|
||||
else if (c == '\r') bit = 48;
|
||||
else {
|
||||
shift = true;
|
||||
const std::string shifted = "!\"#$%&'()";
|
||||
const auto n = shifted.find(c);
|
||||
require(n != std::string::npos, "Unhandled guest key");
|
||||
bit = 33 + n;
|
||||
}
|
||||
chords.push_back((std::uint64_t(1) << bit) | (std::uint64_t(shift) << 55));
|
||||
}
|
||||
session.machine.paste(chords);
|
||||
for (unsigned n = 0; session.machine.pasteRemaining() && n < 9000; ++n) session.frames(1);
|
||||
require(!session.machine.pasteRemaining(), "Guest paste did not complete");
|
||||
if (!command.empty() && command.front() >= '0' && command.front() <= '9') { session.frames(10); return; }
|
||||
for (unsigned n = 0; n < 1200; ++n) {
|
||||
session.frames(1);
|
||||
const auto screen = session.machine.copyScreenText();
|
||||
require(screen.find("ERROR") == std::string::npos, "Disk BASIC command failed: " + screen);
|
||||
const auto end = screen.find_last_not_of(" \r\n");
|
||||
if (end != std::string::npos && end > 0 && screen.compare(end - 1, 2, "OK") == 0) return;
|
||||
}
|
||||
throw std::runtime_error("Disk BASIC did not return: " + session.machine.copyScreenText());
|
||||
}
|
||||
|
||||
static void run(const fs::path& roms, const fs::path& disk, const fs::path& binary,
|
||||
Processor cpu, Profile profile, const std::string& scenario) {
|
||||
Session session(roms, disk, cpu, profile);
|
||||
// Prime Disk BASIC's own DOS allocation table before the C program writes.
|
||||
basic(session, "PRINT FREE(0)\r");
|
||||
if (scenario == "B") basic(session, "OPEN\"I\",#1,\"SAFE.DAT\"\r");
|
||||
session.launch(binary);
|
||||
session.until("SELECT CHECK");
|
||||
if (scenario == "N" && profile == Profile::fd) {
|
||||
session.floppy->setWriteProtected(1, false);
|
||||
session.type("D");
|
||||
} else session.type(scenario);
|
||||
if (scenario == "W") {
|
||||
char key = 'X'; // Distinct keys at adjacent prompts avoid ROM keyboard repeat debounce.
|
||||
for (const auto& stage : {std::pair{"FIRST FLUSH", "sector.dsk"},
|
||||
{"FULL GRANULE", "granule.dsk"}, {"APPENDED", "append.dsk"}}) {
|
||||
session.until(stage.first);
|
||||
snapshot(session, stage.second);
|
||||
session.frames(10);
|
||||
session.type(std::string(1, key++));
|
||||
}
|
||||
} else if (scenario == "N") {
|
||||
session.until("RECLAIM REFUSED");
|
||||
snapshot(session, "rollback.dsk");
|
||||
session.type("X");
|
||||
} else if (scenario == "F") {
|
||||
session.until("LAST FOUR BYTES");
|
||||
snapshot(session, "full.dsk");
|
||||
session.type("X");
|
||||
} else if (scenario == "P" || scenario == "T" || scenario == "U") {
|
||||
session.until("PROTECT NOW");
|
||||
snapshot(session, "protected.dsk");
|
||||
protect(session, true);
|
||||
session.type("X");
|
||||
if (scenario == "T") {
|
||||
session.until("ALLOW RETRY");
|
||||
snapshot(session, "refused.dsk");
|
||||
protect(session, false);
|
||||
session.type("Y");
|
||||
}
|
||||
}
|
||||
if (scenario == "N") session.until("DIRECTORY PASSED");
|
||||
else if (scenario == "U") session.until("UPDATES PASSED");
|
||||
else if (scenario == "S" || scenario == "T" || scenario == "M") session.until("POSITIONS PASSED");
|
||||
else session.until("WRITES PASSED");
|
||||
session.verifyReturn();
|
||||
snapshot(session, "closed.dsk");
|
||||
if (scenario == "N" && profile == Profile::fd) {
|
||||
const auto& drive1 = session.floppy->image(1)->bytes();
|
||||
std::ofstream out("drive1.dsk", std::ios::binary);
|
||||
out.write(reinterpret_cast<const char*>(drive1.data()), drive1.size());
|
||||
require(bool(out), "Could not capture FD1");
|
||||
}
|
||||
if (scenario == "W") {
|
||||
basic(session, "10 OPEN\"I\",#1,\"REPORT.TXT\"\r");
|
||||
basic(session, "20 LINE INPUT #1,A$\r");
|
||||
basic(session, "30 CLOSE#1:PRINT A$\r");
|
||||
basic(session, "40 OPEN\"O\",#1,\"BASIC.TXT\"\r");
|
||||
basic(session, "50 PRINT#1,\"FROM BASIC\":CLOSE#1\r");
|
||||
basic(session, "RUN\r");
|
||||
session.until("MARS RIDGE");
|
||||
snapshot(session, "basic.dsk");
|
||||
}
|
||||
}
|
||||
|
||||
int main(int argc, char** argv) {
|
||||
try {
|
||||
require(argc == 7, "Usage: run ROMS DSK BIN PROFILE CPU TEST");
|
||||
const std::string profile = argv[4];
|
||||
const auto mode = profile == "disk" ? Profile::fd : profile == "rgb-fd" ? Profile::rgbFD :
|
||||
profile == "rgb-hd" ? Profile::rgbHD : Profile::sdc;
|
||||
const auto cpu = std::string(argv[5]) == "6809" ? Processor::MC6809 : Processor::HD6309;
|
||||
run(argv[1], argv[2], argv[3], cpu, mode, argv[6]);
|
||||
} catch (const std::exception& error) {
|
||||
std::cerr << error.what() << '\n';
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,141 @@
|
||||
// SPDX-License-Identifier: GPL-3.0-or-later
|
||||
// The host checks every final file byte against its own fixtures and compares
|
||||
// the raw volume before and after a guest flush on write-protected media.
|
||||
#include <stdio.h>
|
||||
#include <cmoc.h>
|
||||
#include <coco.h>
|
||||
#include <decbfile.h>
|
||||
|
||||
#define CHECK(test) do { if (!(test)) { printf("FAIL LINE %u ERR %u\n", (unsigned)__LINE__, (unsigned)vcc_file_error()); return 1; } } while (0)
|
||||
|
||||
int formattedChecks(void);
|
||||
int formattedProtectedChecks(void);
|
||||
|
||||
static int binaryUpdate(void)
|
||||
{
|
||||
FILE *f, *reader;
|
||||
byte pair[2];
|
||||
CHECK(!fopen("MISSING.DAT", "r+b") && vcc_file_error() == DECB_ERR_NOT_FOUND);
|
||||
CHECK(!fopen("SAFE.DAT", "r++") && vcc_file_error() == DECB_ERR_INVALID_ARGUMENT);
|
||||
CHECK(!fopen("SAFE.DAT", "rb+b") && vcc_file_error() == DECB_ERR_INVALID_ARGUMENT);
|
||||
reader = fopen("distant.dat", "rb");
|
||||
CHECK(reader && !fopen("DISTANT.DAT:0", "rb+") && vcc_file_error() == DECB_ERR_ALREADY_EXISTS);
|
||||
CHECK(fclose(reader) == 0);
|
||||
|
||||
f = fopen("DISTANT.DAT", "rb+");
|
||||
CHECK(f && ftell(f) == 0L && fgetc(f) == 0);
|
||||
CHECK(!fopen("distant.dat:0", "r+b") && vcc_file_error() == DECB_ERR_ALREADY_EXISTS);
|
||||
CHECK(!fopen("distant.dat:0", "w") && vcc_file_error() == DECB_ERR_ALREADY_EXISTS);
|
||||
// An all-stream flush cannot authorize a write after reading.
|
||||
CHECK(fflush(f) == EOF && vcc_file_error() == DECB_ERR_INVALID_ARGUMENT);
|
||||
CHECK(fflush(NULL) == 0);
|
||||
clearerr(f); // Clearing flags is not a direction change.
|
||||
CHECK(fputc('!', f) == EOF && vcc_file_error() == DECB_ERR_INVALID_ARGUMENT && ftell(f) == 1L);
|
||||
CHECK(fseek(f, 255L, SEEK_SET) == 0);
|
||||
CHECK(fwrite("SECT", 2, 2, f) == 2);
|
||||
// A rejected seek cannot authorize reading after writing.
|
||||
clearerr(f);
|
||||
CHECK(fseek(f, -1L, SEEK_SET) == EOF);
|
||||
CHECK(fread(pair, 1, 2, f) == 0 && vcc_file_error() == DECB_ERR_INVALID_ARGUMENT && ftell(f) == 259L);
|
||||
CHECK(fflush(NULL) == 0);
|
||||
CHECK(fgetc(f) == 22 && ftell(f) == 260L);
|
||||
reader = fopen("DISTANT.DAT", "rb");
|
||||
CHECK(reader && fseek(reader, 255L, SEEK_SET) == 0);
|
||||
CHECK(fgetc(reader) == 'S' && fgetc(reader) == 'E' && fclose(reader) == 0);
|
||||
|
||||
// Logical byte 2304 is in the deliberately relocated second granule.
|
||||
CHECK(fseek(f, 2302L, SEEK_SET) == 0 && fputs("GRAN", f) >= 0);
|
||||
CHECK(fflush(f) == 0 && fgetc(f) == 23);
|
||||
CHECK(fseek(f, -1L, SEEK_END) == 0 && ftell(f) == 65536L);
|
||||
CHECK(fread(pair, 1, 2, f) == 1 && pair[0] == 0 && feof(f));
|
||||
clearerr(f);
|
||||
CHECK(!feof(f) && !ferror(f) && ftell(f) == 65537L);
|
||||
// Clearing the indicator does not undo the read that encountered EOF.
|
||||
CHECK(fwrite("END", 1, 3, f) == 3 && !feof(f));
|
||||
CHECK(fseek(f, -3L, SEEK_END) == 0 && fgetc(f) == 'E' && ftell(f) == 65538L);
|
||||
CHECK(fclose(f) == 0);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int textUpdate(void)
|
||||
{
|
||||
FILE *f = fopen("RECORDS.TXT", "r+");
|
||||
long bookmark;
|
||||
char line[16];
|
||||
unsigned i;
|
||||
CHECK(f);
|
||||
for (i = 0; i < 255; ++i) CHECK(fgetc(f) == 'X');
|
||||
bookmark = ftell(f);
|
||||
CHECK(fgetc(f) == '\n');
|
||||
CHECK(fseek(f, bookmark, SEEK_SET) == 0);
|
||||
// Replace a CR at the end of one sector; its paired LF begins the next.
|
||||
CHECK(fputc('\n', f) == '\n' && fflush(f) == 0);
|
||||
CHECK(fgets(line, sizeof(line), f) && !strcmp(line, "MARS\n"));
|
||||
// Overwrite the next planet's name after positioning at the same cursor.
|
||||
CHECK(fseek(f, 0L, SEEK_CUR) == 0 && fputs("PHOBOS", f) >= 0);
|
||||
CHECK(fflush(f) == 0 && fgetc(f) == '\n');
|
||||
rewind(f);
|
||||
CHECK(ftell(f) == 0L && fgetc(f) == 'X' && fclose(f) == 0);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int createAppendUpdate(void)
|
||||
{
|
||||
FILE *f;
|
||||
CHECK(!fopen("SAFE.DAT", "wb+b") && !fopen("SAFE.DAT", "a++"));
|
||||
f = fopen("TRIPS.DAT", "w+b");
|
||||
CHECK(f && fputc(0, f) == 0 && fputc(255, f) == 255);
|
||||
rewind(f);
|
||||
CHECK(fgetc(f) == 0 && fgetc(f) == 255 && fclose(f) == 0);
|
||||
|
||||
// Replace an existing multi-granule file and release its old chain.
|
||||
f = fopen("RESET.DAT", "wb+");
|
||||
CHECK(f && fgetc(f) == EOF && feof(f));
|
||||
CHECK(fputs("NEW", f) >= 0 && fseek(f, 1L, SEEK_SET) == 0);
|
||||
CHECK(fputc('O', f) == 'O' && fclose(f) == 0);
|
||||
f = fopen("EMPTY.TXT", "w+");
|
||||
CHECK(f && fgetc(f) == EOF && feof(f) && fclose(f) == 0);
|
||||
|
||||
f = fopen("TRIPS.DAT", "a+b");
|
||||
CHECK(f && ftell(f) == 0L && fputc('A', f) == 'A' && ftell(f) == 3L);
|
||||
rewind(f);
|
||||
CHECK(fgetc(f) == 0 && fputc('X', f) == EOF && ftell(f) == 1L);
|
||||
CHECK(fseek(f, 0L, SEEK_CUR) == 0 && fwrite("BC", 1, 2, f) == 2);
|
||||
CHECK(fclose(f) == 0);
|
||||
f = fopen("DISTANT.DAT", "ab+");
|
||||
CHECK(f && fgetc(f) == 0 && fseek(f, 256L, SEEK_SET) == 0);
|
||||
CHECK(fputc('!', f) == '!' && ftell(f) == 65541L && fclose(f) == 0);
|
||||
f = fopen("CREATED.TXT", "a+");
|
||||
CHECK(f && fgetc(f) == EOF && fputs("LOG\n", f) >= 0);
|
||||
rewind(f);
|
||||
CHECK(fgetc(f) == 'L' && fclose(f) == 0);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int protectedUpdate(void)
|
||||
{
|
||||
printf("PROTECT NOW\n");
|
||||
while (waitkey(0) != 'X') {}
|
||||
// The Disk BASIC ROM reports write protection at the actual sector write.
|
||||
FILE *f = fopen("SAFE.DAT", "r+b");
|
||||
CHECK(f && fgetc(f) == 'p' && fseek(f, 0L, SEEK_SET) == 0);
|
||||
CHECK(fputc('!', f) == '!' && fflush(f) == EOF && ferror(f) == DECB_ERR_IO);
|
||||
CHECK(fclose(f) == EOF && vcc_file_error() == DECB_ERR_IO);
|
||||
f = fopen("SAFE.DAT", "r+b");
|
||||
CHECK(f && fgetc(f) == 'p' && fclose(f) == 0);
|
||||
CHECK(!fopen("SAFE.DAT", "w+") && !fopen("MISSING.DAT", "a+"));
|
||||
f = fopen("SAFE.DAT", "a+");
|
||||
CHECK(f && fgetc(f) == 'p' && fseek(f, 0L, SEEK_SET) == 0);
|
||||
CHECK(fputc('!', f) == '!' && fflush(f) == EOF && fclose(f) == EOF);
|
||||
return formattedProtectedChecks();
|
||||
}
|
||||
|
||||
int main(void)
|
||||
{
|
||||
cls(1);
|
||||
printf("SELECT CHECK\n");
|
||||
if (waitkey(0) != 'U' || binaryUpdate() || textUpdate()
|
||||
|| createAppendUpdate() || formattedChecks() || protectedUpdate()) return 1;
|
||||
printf("UPDATES PASSED\n");
|
||||
return 0;
|
||||
}
|
||||
Reference in new issue
Block a user