// SPDX-License-Identifier: GPL-3.0-or-later // Authored fixtures only; no ROM or disk content from external releases. #include "CoreChecks.h" #include "../../src/devices/FD502/Fd502Device.h" #include "../../src/core/MachineSession.h" #include "../../src/core/tcc1014mmu.h" #include #include #include using namespace VCC::Devices; namespace { void require(bool ok, const char* message) { if (!ok) throw std::runtime_error(message); } template void rejects(F f, const char* message) { bool rejected = false; try { f(); } catch (const std::exception&) { rejected = true; } require(rejected, message); } struct Host final : DeviceHost { unsigned nmis = 0; void cartridgeNmi() override { ++nmis; } }; struct Rig { Host host; Fd502Device fdc{std::vector(8192, 0x42)}; void w(unsigned p, unsigned v) { fdc.writePort(p, v, host); } std::uint8_t r(unsigned p) { return fdc.readPort(p, host); } void tick(unsigned count = 1) { while (count--) fdc.horizontalSync(host); } void finish() { for (unsigned n = 0; n < 200000 && fdc.snapshot().busy; ++n) tick(); require(!fdc.snapshot().busy, "Controller operation must finish within bounded scanlines"); } }; DiskImage fixture(DiskGeometry g = {}) { auto disk = DiskImage::blank(g); for (unsigned t = 0; t < g.tracks; ++t) for (unsigned side = 0; side < g.sides; ++side) for (unsigned s = g.firstSector; s < g.firstSector + g.sectors; ++s) { std::vector bytes(g.sectorBytes()); for (unsigned i = 0; i < bytes.size(); ++i) bytes[i] = t * 7 + side * 31 + s + i; require(disk.writeSector(t, side, s, bytes), "Author test sector"); } disk.markSaved(); return disk; } std::vector trackStream(unsigned track, unsigned side) { std::vector bytes; for (unsigned s = 1; s <= 18; ++s) { bytes.insert(bytes.end(), 12, 0x4e); bytes.insert(bytes.end(), {0xfe, std::uint8_t(track), std::uint8_t(side), std::uint8_t(s), 1, 0xf7}); bytes.insert(bytes.end(), 12, 0x4e); bytes.push_back(0xfb); bytes.insert(bytes.end(), 256, std::uint8_t(s)); bytes.push_back(0xf7); } bytes.resize(6272, 0x4e); return bytes; } using Bytes = std::vector; // 0xcdb4 is the CCITT seed after three MFM A1 sync bytes; FM starts at 0xffff. std::uint16_t testCRC(const Bytes& bytes, std::size_t offset, std::size_t count, std::uint16_t crc = 0xcdb4) { for (std::size_t i = offset; i < offset + count; ++i) { crc ^= static_cast(bytes[i]) << 8; for (unsigned bit = 0; bit < 8; ++bit) crc = (crc & 0x8000) ? (crc << 1) ^ 0x1021 : crc << 1; } return crc; } // A 128-byte deleted sector followed by a 256-byte sector, both claiming // cylinder 7 on physical head position 0. Bytes dmkFixture() { constexpr unsigned trackBytes = 6400; Bytes bytes(16 + trackBytes, 0x4e); std::fill_n(bytes.begin(), 16 + 128, 0); bytes[1] = 1; bytes[2] = trackBytes & 255; bytes[3] = trackBytes >> 8; bytes[4] = 0x10; auto record = [&](unsigned id, unsigned slot, unsigned sectorID, unsigned lengthCode, std::uint8_t mark) { bytes[16 + slot*2] = id & 255; bytes[17 + slot*2] = 0x80 | (id >> 8); const unsigned address = 16 + id; auto putCRC = [&](unsigned at, unsigned size) { const auto crc = testCRC(bytes, at, size); bytes[at+size] = crc >> 8; bytes[at+size+1] = crc & 255; }; std::fill_n(bytes.begin()+address-3, 3, 0xa1); bytes[address] = 0xfe; bytes[address+1] = 7; bytes[address+2] = 0; bytes[address+3] = sectorID; bytes[address+4] = lengthCode; putCRC(address, 5); const unsigned dam = address + 32; std::fill_n(bytes.begin()+dam-3, 3, 0xa1); bytes[dam] = mark; const unsigned size = 128u << lengthCode; std::fill_n(bytes.begin()+dam+1, size, sectorID); putCRC(dam, size+1); }; record(160, 0, 6, 0, 0xf8); record(480, 1, 2, 1, 0xfb); return bytes; } Bytes readCommand(Rig& r, unsigned cmd, unsigned count) { r.w(0x48, cmd); Bytes result; for (unsigned i = 0; i < count; ++i) result.push_back(r.r(0x4b)); r.tick(10); // completion after the final data byte return result; } void checkDmkTransfers() { Rig r; auto original = dmkFixture(); r.fdc.mount(0, DiskImage::load(original)); r.w(0x40, 0x29); auto first = readCommand(r, 0xc0, 6); auto second = readCommand(r, 0xc0, 6); require(first == Bytes(original.begin()+177, original.begin()+183) && second == Bytes(original.begin()+497, original.begin()+503), "DMK READ ADDRESS reports both real ID records, lengths, CRCs and on-disk ordering"); require(readCommand(r, 0xe0, 6272) == Bytes(original.begin()+144, original.end()), "DMK READ TRACK returns the raw revolution without the container pointer table"); r.w(0x4a, 6); r.w(0x48, 0x80); r.r(0x4b); require(r.r(0x48) == 0x10, "Wrong logical cylinder cannot match a DMK sector"); r.w(0x49, 7); require(readCommand(r, 0x80, 128) == Bytes(128, 6) && r.r(0x48) == 0x20, "DMK returns stored length and deleted-data mark after a complete read"); r.w(0x4a, 2); r.w(0x48, 0xa1); // write deleted data mark for (unsigned i = 0; i < 256; ++i) r.w(0x4b, 0xa5); r.tick(10); require(r.r(0x48) == 0 && r.fdc.image(0)->dirty(), "DMK write completed"); auto expected = original; constexpr unsigned dam = 16 + 480 + 32; expected[dam] = 0xf8; std::fill_n(expected.begin()+dam+1, 256, 0xa5); const auto crc = testCRC(expected, dam, 257); expected[dam+257] = crc >> 8; expected[dam+258] = crc & 255; require(expected == r.fdc.image(0)->bytes(), "DMK write updates only the mark, data and CRC"); expected[dam+258] ^= 1; r.fdc.mount(0, DiskImage::load(expected), true); r.w(0x4a, 2); require(readCommand(r, 0x80, 256) == Bytes(256, 0xa5) && r.r(0x48) == 0x28, "A deleted DMK sector with a bad data CRC returns the bytes and WD status"); // Formatting synthesizes fresh ID pointers inside this track only. r.w(0x49, 0); r.w(0x48, 0xf0); for (auto b : trackStream(0, 0)) r.w(0x4b, b); r.tick(10); require(r.r(0x48) == 0 && DiskImage::load(r.fdc.image(0)->bytes()).readSector(0, 0, 18) == Bytes(256, 18), "DMK formatted via WRITE TRACK reopens with its new sectors"); original[0] = 0xff; r.fdc.mount(0, DiskImage::load(original), true); r.fdc.setWriteProtected(0, false); // software override cannot clear the format protection r.w(0x48, 0xf0); require(r.r(0x48) == 0x50 && !r.fdc.image(0)->dirty(), "DMK header protects format commands"); } void checkFmAndDamagedRecords() { auto bytes = dmkFixture(); std::fill(bytes.begin()+16, bytes.end(), 0); bytes[16] = 160; // pointer with bit 15 clear: a single-density record in double-rate storage auto storeDoubled = [&](unsigned offset, Bytes field) { const auto crc = testCRC(field, 0, field.size(), 0xffff); field.push_back(crc >> 8); field.push_back(crc & 255); for (auto value : field) { bytes[16 + offset++] = value; bytes[16 + offset++] = value; } }; storeDoubled(160, {0xfe, 0, 0, 1, 1}); // A copy-protected track (as in Shamus) can put an extra table pointer // between a real ID and its data. Its malformed ID must not hide that data. bytes[18] = 178; storeDoubled(178, {0xfe, 9, 8, 7, 3}); bytes[16 + 178 + 10] ^= 1; bytes[16 + 178 + 11] ^= 1; Bytes data(257, 0xe5); data.front() = 0xfb; storeDoubled(200, data); Rig r; r.fdc.mount(0, DiskImage::load(bytes)); r.w(0x40, 0x29); r.w(0x4a, 1); readCommand(r, 0xc0, 6); // first, valid FM address const auto badAddress = readCommand(r, 0xc0, 6); require(badAddress[0] == 9 && badAddress[2] == 7 && r.r(0x48) == 8, "DMK READ ADDRESS delivers the original bytes and signals a bad address CRC"); require(readCommand(r, 0x80, 256) == Bytes(256, 0xe5) && r.r(0x48) == 0, "Mixed-density DMK decodes doubled FM bytes and their CRC"); r.w(0x4a, 1); r.w(0x48, 0xa0); for (unsigned i = 0; i < 256; ++i) r.w(0x4b, i); r.tick(10); for (unsigned i = 0; i < 256; ++i) data[1+i] = i; storeDoubled(200, data); require(r.fdc.image(0)->bytes() == bytes, "FM write updates both copies of each data and CRC byte"); // Actual protected disks may have tracks without readable IDs. Their raw // revolutions must survive and a corrupt pointer cannot alias the next track. bytes[16] = 0xff; bytes[17] = 0xbf; r.fdc.mount(0, DiskImage::load(bytes), true); r.w(0x4a, 1); r.w(0x48, 0x80); r.r(0x4b); require(r.r(0x48) == 0x10 && !r.fdc.image(0)->dirty(), "Invalid ID pointer fails safely"); require(readCommand(r, 0xe0, 6272) == Bytes(bytes.begin()+144, bytes.end()), "Track bytes remain readable without valid IDs"); bytes.pop_back(); rejects([&] { DiskImage::load(bytes); }, "Truncated DMK track image is rejected"); } void checkVdkTransfers() { Rig r; Bytes bytes(12 + 2 * 18 * 256, 0); bytes[0] = 'd'; bytes[1] = 'k'; bytes[2] = 12; bytes[4] = 0x10; bytes[5] = 0x10; bytes[8] = 1; bytes[9] = 2; const auto last = bytes.size() - 256; std::fill(bytes.begin()+last, bytes.end(), 0x3d); r.fdc.mount(0, DiskImage::load(bytes)); r.w(0x40, 0x69); r.w(0x4a, 18); require(readCommand(r, 0x80, 256) == Bytes(256, 0x3d), "VDK second-side sector is addressed after its header"); r.w(0x4a, 18); r.w(0x48, 0xa0); for (unsigned i = 0; i < 256; ++i) r.w(0x4b, 0x5c); r.tick(10); std::fill(bytes.begin()+last, bytes.end(), 0x5c); require(r.r(0x48) == 0 && bytes == r.fdc.image(0)->bytes(), "VDK write retains header and other sectors"); r.w(0x48, 0xe0); r.r(0x4b); require(r.r(0x48) == 0x10, "VDK sector images do not fabricate raw track data"); bytes[10] = 1; r.fdc.mount(0, DiskImage::load(bytes), true); r.w(0x48, 0xa0); require(r.r(0x48) == 0x50 && !r.fdc.image(0)->dirty(), "VDK header protects sector writes"); // Genuine VDKs may retain optional disk names/metadata after byte 12. bytes.insert(bytes.begin() + 12, {'C', 'O', 'C', 'O'}); bytes[2] = 16; const auto extended = DiskImage::load(bytes); require(extended.bytes() == bytes && extended.readSector(0, 1, 18) == Bytes(256, 0x5c), "Extended VDK headers preserve bytes and offset sector data correctly"); bytes[8] = 2; rejects([&] { DiskImage::load(bytes); }, "VDK cylinder count must match its payload"); bytes.erase(bytes.begin() + 12, bytes.begin() + 16); std::fill_n(bytes.begin(), 12, 0); bytes[9] = 2; const auto windows = DiskImage::load(bytes); require(windows.format() == DiskFormat::VDK && windows.geometry().tracks == 1 && windows.readSector(0, 1, 18) == Bytes(256, 0x5c), "Windows VCC legacy VDK side-count-only header preserves payload offsets"); } void checkCreatedTrackFormats() { for (auto kind : {DiskFormat::VDK, DiskFormat::DMK}) { const auto created = DiskImage::blank(DiskGeometry{}, kind); auto disk = DiskImage::load(created.bytes()); require(disk.format() == kind && disk.geometry().tracks == 35 && disk.geometry().sides == 1 && !disk.dirty(), "New VDK and DMK media export with recognized header and geometry"); Rig r; r.fdc.mount(0, std::move(disk)); r.w(0x40, 0x29); r.w(0x48, 0xf0); for (auto byte : trackStream(0, 0)) r.w(0x4b, byte); r.tick(10); const auto formatted = DiskImage::load(r.fdc.image(0)->bytes()); require(r.r(0x48) == 0 && formatted.format() == kind && formatted.readSector(0, 0, 1) == Bytes(256, 1) && formatted.readSector(0, 0, 18) == Bytes(256, 18), "Guest formatting on a newly created disk produces readable sectors after reopening"); } } } void CheckDiskImages() { checkCreatedTrackFormats(); auto disk = fixture(); require(disk.bytes().size() == 161280 && !disk.dirty(), "Standard 35-track headerless DSK size"); auto signatureCollision = disk.bytes(); signatureCollision[0] = 'd'; signatureCollision[1] = 'k'; require(DiskImage::load(signatureCollision).format() == DiskFormat::JVC, "Headerless DSK first-sector data may begin with the VDK signature"); auto reopened = DiskImage::load(disk.bytes()); require(reopened.readSector(34, 0, 18) == disk.readSector(34, 0, 18), "Last sector survives export/reload"); require(reopened.readSector(35, 0, 1).empty() && reopened.readSector(0, 1, 1).empty() && reopened.readSector(0, 0, 0).empty() && reopened.readSector(0, 0, 19).empty(), "Out-of-geometry reads cannot alias adjacent tracks"); for (unsigned size = 0; size <= 3; ++size) { auto image = fixture({3, 2, 9, size, 0}); auto copy = DiskImage::load(image.bytes()); require(copy.geometry().sides == 2 && copy.geometry().sectorBytes() == (128u << size) && copy.readSector(2, 1, 8) == image.readSector(2, 1, 8), "JVC variable sector size, sides and first sector"); } // One-, two- and three-byte JVC headers use defaults for absent fields. for (unsigned n = 1; n <= 3; ++n) { std::vector bytes{18, 1, 1}; bytes.resize(n); bytes.insert(bytes.end(), disk.bytes().begin(), disk.bytes().end()); require(DiskImage::load(bytes).geometry().tracks == 35, "Partial JVC header defaults"); } auto os9 = DiskImage::blank({40, 2, 18, 1, 1}).bytes(); os9.erase(os9.begin(), os9.begin() + 4); os9[0] = 0; os9[1] = 5; os9[2] = 160; os9[3] = 18; os9[16] = 1; // 1,440 sectors require(DiskImage::load(os9).format() == DiskFormat::OS9 && DiskImage::load(os9).geometry().sides == 2, "OS-9 descriptor selects two sides only when total sectors match the file"); for (auto bytes : {std::vector{}, std::vector(17), std::vector(4607), std::vector(161280 + 16)}) rejects([&] { DiskImage::load(bytes); }, "Malformed or unsupported containers rejected"); auto bad = DiskImage::blank({2, 2, 18, 1, 1}).bytes(); bad[2] = 255; rejects([&] { DiskImage::load(bad); }, "Invalid size code rejected before shifting"); bad[2] = 1; bad[0] = 0; rejects([&] { DiskImage::load(bad); }, "Zero sector divisor rejected"); rejects([&] { DiskImage::blank({257, 1, 18, 1, 1}); }, "Excessive tracks rejected"); rejects([&] { DiskImage::blank({1, 1, 1, 0, 1}); }, "Ambiguous half-block JVC payload rejected"); rejects([&] { DiskImage::load(std::vector(DiskImage::maxBytes + 1)); }, "Oversized image rejected"); auto readonly = DiskImage::load(disk.bytes(), true); auto original = readonly.bytes(); require(!readonly.writeSector(0, 0, 1, std::vector(256)) && readonly.bytes() == original && !readonly.dirty(), "Protected writes do not alter any byte"); auto stream = trackStream(0, 0); auto malformed = stream; malformed.back() = 0xfe; require(!reopened.formatTrack(0, 0, malformed) && reopened.bytes() == disk.bytes(), "Malformed formatting is atomic"); require(reopened.formatTrack(0, 0, stream) && reopened.dirty() && reopened.readSector(0, 0, 18) == std::vector(256, 18), "Valid write-track stream updates sectors"); } void CheckFloppyCommands() { Rig r; r.fdc.mount(0, fixture()); r.w(0x40, 0x29); r.w(0x4b, 3); r.w(0x48, 0x10); r.tick(99); require(r.fdc.snapshot().head == 0 && r.fdc.snapshot().busy, "Seek honors settle and step delay"); r.finish(); require(r.r(0x49) == 3 && r.fdc.snapshot().head == 3 && r.host.nmis == 1, "Seek and gated completion NMI"); r.w(0x48, 0x40); r.finish(); require(r.r(0x49) == 3 && r.fdc.snapshot().head == 4, "STEP IN without update keeps track register"); r.w(0x48, 0x70); r.finish(); require(r.r(0x49) == 2 && r.fdc.snapshot().head == 3, "STEP OUT updates the register independently of physical position"); r.w(0x48, 0); r.finish(); require(r.r(0x49) == 0 && (r.r(0x48) & 4), "RESTORE reports track zero"); r.w(0x40, 1); r.w(0x4b, 2); r.w(0x48, 0x10); r.tick(500); require(r.fdc.snapshot().busy && r.fdc.snapshot().head == 0, "Motor-off freezes seek"); const auto nmis = r.host.nmis; r.w(0x40, 9); r.finish(); require(r.host.nmis == nmis && r.fdc.snapshot().head == 2, "Completion NMI disabled by density/control bit"); r.w(0x48, 0xd8); require(r.host.nmis == nmis + 1, "Forced NMI bypasses control mask"); r.w(0x48, 0xd0); require(r.host.nmis == nmis + 1, "Force termination without interrupt"); r.w(0x40, 0x48); require(r.fdc.snapshot().drive == 3 && r.fdc.snapshot().side == 0, "Fourth single-sided drive select"); r.w(0x40, 0x49); require(r.fdc.snapshot().drive == 0 && r.fdc.snapshot().side == 1, "Bit six selects side when another drive is selected"); r.w(0x40, 0x0f); require(r.fdc.snapshot().drive == 2, "Upstream drive-select priority"); r.w(0x40, 8); r.w(0x48, 0x80); require(r.r(0x48) == 0x10, "Absent drive returns record not found"); } void CheckFloppyTransfers() { checkDmkTransfers(); checkFmAndDamagedRecords(); checkVdkTransfers(); Rig r; auto disk = fixture({2, 2, 18, 1, 1}); const auto expected = disk.readSector(0, 1, 18); r.fdc.mount(0, std::move(disk)); r.w(0x40, 0x69); r.w(0x4a, 18); r.w(0x48, 0x80); std::vector got; for (unsigned i = 0; i < 256; ++i) got.push_back(r.r(0x4b)); require(got == expected && r.host.nmis == 0, "Second-side sector data, completion deferred beyond last byte"); r.r(0x4b); require(!r.fdc.snapshot().busy && r.host.nmis == 1 && r.r(0x48) == 0, "Read completion delivers one NMI"); r.w(0x4a, 2); r.w(0x48, 0xa0); r.tick(5); require(r.r(0x48) & 2, "Write DRQ after initial delay"); for (unsigned i = 0; i < 256; ++i) r.w(0x4b, i ^ 0xa5); r.tick(6); auto changed = r.fdc.image(0)->readSector(0, 1, 2); for (unsigned i = 0; i < 256; ++i) require(changed[i] == (i ^ 0xa5), "Written sector bytes"); require(r.r(0x48) == 0 && r.fdc.image(0)->dirty(), "Completed write commits once without false lost-data flag"); auto copy = DiskImage::load(r.fdc.image(0)->bytes()); require(copy.readSector(0, 1, 2) == changed, "Written sector reopens"); r.w(0x4a, 17); r.w(0x48, 0x90); for (unsigned s = 17; s <= 18; ++s) for (unsigned i = 0; i < 256; ++i) require(r.r(0x4b) == copy.readSector(0, 1, s)[i], "Multi-sector data stream"); r.r(0x4b); require(r.r(0x48) == 0x10 && r.r(0x4a) == 19, "Multi-sector terminates at track boundary without aliasing"); r.w(0x4a, 17); r.w(0x48, 0xb0); for (unsigned i = 0; i < 512; ++i) r.w(0x4b, i < 256 ? 0xa5 : 0x5a); r.tick(10); require(r.r(0x48) == 0x10 && r.fdc.image(0)->readSector(0, 1, 18) == std::vector(256, 0x5a), "Multi-sector write commits last valid sector then stops"); require(r.fdc.image(0)->readSector(0, 1, 17) == std::vector(256, 0xa5), "Multi-sector write preserves first sector payload"); r.w(0x4a, 1); r.w(0x48, 0x80); r.finish(); require(r.r(0x48) == 4, "Unserviced transfer reports lost data"); r.w(0x48, 0xc0); std::vector id; for (unsigned i = 0; i < 6; ++i) id.push_back(r.r(0x4b)); r.r(0x4b); require(id[0] == 0 && id[1] == 1 && id[2] >= 1 && id[2] <= 18 && id[3] == 1, "Read address returns valid geometry"); r.w(0x40, 0x29); r.w(0x48, 0xf0); for (auto b : trackStream(0, 0)) r.w(0x4b, b); r.tick(10); require(r.r(0x48) == 0 && r.fdc.image(0)->readSector(0, 0, 1) == std::vector(256, 1), "Write-track formats selected side"); } void CheckFloppyProtection() { Rig r; auto disk = fixture(); const auto original = disk.bytes(); r.fdc.mount(0, DiskImage::load(original, true)); r.w(0x40, 0x29); r.w(0x4a, 1); for (unsigned command : {0xa0, 0xb0, 0xf0}) { r.w(0x48, command); for (unsigned i = 0; i < 300; ++i) r.w(0x4b, 0); require(r.r(0x48) == 0x50 && r.fdc.image(0)->bytes() == original && !r.fdc.image(0)->dirty(), "Protected controller writes leave source intact"); } r.fdc.mount(0, fixture()); r.w(0x4a, 1); r.w(0x48, 0xa0); for (unsigned i = 0; i < 100; ++i) r.w(0x4b, 0); rejects([&] { r.fdc.eject(0); }, "Busy eject rejected"); r.w(0x48, 0xd0); require(r.fdc.image(0)->bytes() == original, "Force interrupt drops incomplete sector"); r.w(0x48, 0xa0); r.w(0x4b, 0); r.w(0x40, 0x2a); require(r.r(0x48) == 0x10 && r.fdc.image(0)->bytes() == original, "Drive switch aborts buffered transfer"); r.w(0x40, 0x29); r.w(0x4a, 0); r.w(0x48, 0xa0); r.w(0x4b, 0); require(r.r(0x48) == 0x10 && r.fdc.image(0)->bytes() == original, "Sector zero cannot overwrite image header"); r.w(0x4a, 1); r.w(0x48, 0xa0); for (unsigned i = 0; i < 256; ++i) r.w(0x4b, 0x77); r.tick(10); rejects([&] { r.fdc.eject(0); }, "Dirty eject requires explicit discard"); rejects([&] { r.fdc.mount(0, fixture()); }, "Dirty replacement requires explicit discard"); const auto changed = r.fdc.image(0)->bytes(); r.fdc.reset(); require(r.fdc.image(0)->bytes() == changed && r.fdc.image(0)->dirty() && !r.fdc.snapshot().motor, "Reset preserves dirty media and stops motor"); r.fdc.markSaved(0); r.fdc.eject(0); require(!r.fdc.image(0), "Saved image can eject"); r.fdc.mount(0, DiskImage::blank()); r.fdc.eject(0, true); rejects([&] { r.fdc.eject(4); }, "Invalid drive index rejected"); for (unsigned drive = 0; drive < 4; ++drive) { auto image = fixture(); image.writeSector(0, 0, 1, std::vector(256, drive + 10)); image.markSaved(); r.fdc.mount(drive, std::move(image)); } for (unsigned drive = 0; drive < 4; ++drive) { r.w(0x40, (drive == 3 ? 0x40 : 1u << drive) | 8); r.w(0x4a, 1); r.w(0x48, 0x80); require(r.r(0x4b) == drive + 10, "Four mounted drives route independently"); r.w(0x48, 0xd0); } // Arbitrary port/command sequences must stay bounded even without media. for (unsigned i = 0; i < 4096; ++i) { r.w(0x40 + i % 16, (i * 73) & 255); r.tick(); r.r(0x40 + i % 16); } } void CheckBuiltinDeviceBus() { Host first, second; CartridgeBus bus; require(bus.readMemory(0xc000) == 0xff && bus.readPort(0x48) == 0 && bus.audio() == 0, "Empty cartridge bus values"); rejects([&] { bus.install(makeDevice(DeviceKind::FD502, {})); }, "Missing disk ROM rejected"); bus.install(makeDevice(DeviceKind::FD502, std::vector(8192, 0x55))); require(bus.readMemory(0xc000) == 0x55 && bus.readMemory(0xdfff) == 0x55 && bus.readMemory(0xe000) == 0xff, "ROM map and 16 KiB padding"); bus.connect(&first); bus.writePort(0x48, 0xd8); require(first.nmis == 1, "Interrupt reaches current host"); bus.connect(nullptr); bus.writePort(0x48, 0xd8); bus.horizontalSync(); require(first.nmis == 1, "Detach prevents callbacks into old host"); bus.connect(&second); bus.writePort(0x48, 0xd8); require(second.nmis == 1 && first.nmis == 1, "Reconnection isolates host lifetime"); for (unsigned port = 0; port < 256; ++port) { if (port == 0x40 || (port >= 0x48 && port <= 0x4b)) continue; bus.writePort(port, 0xff); require(bus.readPort(port) == 0, "Unowned ports remain inert"); } Fd502Device other(std::vector(8192)); bus.writePort(0x49, 10); require(other.readPort(0x49, first) == 0, "Controllers have no shared mutable state"); bus.install(nullptr); bus.writePort(0x48, 0xd8); require(second.nmis == 1, "Removal detaches device"); } void CheckFloppySession() { using namespace VCC::Mac; for (auto cpu : {Processor::MC6809, Processor::HD6309}) { MachineSession machine; auto device = makeDevice(DeviceKind::FD502, std::vector(8192, 0x42)); auto* fdc = static_cast(device.get()); fdc->mount(0, fixture()); machine.cartridges().install(std::move(device)); std::vector rom(32768, 0x12); const std::uint8_t program[] = {0x10,0xce,0x70,0x00,0x20,0xfe}; // LDS #$7000; BRA self std::copy(std::begin(program), std::end(program), rom.begin()); rom[0x100] = 0x7c; rom[0x101] = 0x40; rom[0x102] = 1; rom[0x103] = 0x3b; // INC $4001; RTI rom[0x7ffc] = 0x81; rom[0x7ffd] = 0; rom[0x7ffe] = 0x80; rom[0x7fff] = 0; machine.load(rom, cpu); machine.resume(); machine.advance(); require(MemRead8(0xc000) == 0x42, "Mapped cartridge ROM reaches guest MMU"); require(fdc->snapshot().ticks >= 262 && fdc->snapshot().ticks <= 263, "One controller tick per raster scanline"); MemWrite8(0, 0x4001); MemWrite8(0x29, 0xff40); MemWrite8(1, 0xff4a); MemWrite8(0x80, 0xff48); for (unsigned i = 0; i < 256; ++i) require(MemRead8(0xff4b) == std::uint8_t(1 + i), "Mapped disk port data"); MemRead8(0xff4b); machine.advance(); require(MemRead8(0x4001) == 1, "Controller completion NMI serviced by both CPU engines"); machine.pause(); const auto ticks = fdc->snapshot().ticks; machine.advance(); require(fdc->snapshot().ticks == ticks, "Pause freezes controller timing"); machine.softReset(); require(fdc->image(0) && !fdc->snapshot().busy && !fdc->snapshot().motor, "Soft reset retains media and cancels controller"); machine.reset(); require(machine.cartridges().current() == fdc && fdc->image(0), "Cold reset retains device ownership and disk"); CartridgeBus::Slots slots; slots[4] = makeDevice(DeviceKind::FD502, std::vector(8192)); slots[1] = makeDevice(DeviceKind::RamDisk, {}); machine.cartridges().configure(slots, true, 4); machine.setDriveWireFactory([](const std::string&, std::uint16_t) -> std::unique_ptr { throw std::logic_error("DriveWire initialization must not open a socket"); }); MemWrite8(0x5a, 0xff42); MemWrite8(0, 0xff42); // DWINIT require(MemRead8(0xff41) == 2, "FD-502 exposes the pending Becker response"); MemWrite8(0x30, 0xff7f); // ROM slot 4, I/O slot 1 MemWrite8(3, 0xff42); MemWrite8(0xa6, 0xff43); require(MemRead8(0xff41) == 0 && MemRead8(0xff43) == 0xa6, "RAM disk owns its overlapping I/O addresses"); MemWrite8(0x33, 0xff7f); require(MemRead8(0xff42) == 4, "Re-selecting the FD-502 returns the pending DriveWire response"); MemWrite8(0x5a, 0xff42); MemWrite8(0, 0xff42); machine.setDriveWireFactory({}); require(MemRead8(0xff41) == 0, "Disabling networking drops pending replies"); machine.shutdown(); machine.cartridges().writePort(0x48, 0xd8); // no dangling callback } }