// SPDX-License-Identifier: GPL-3.0-or-later #include "../../src/devices/DriveWireDevice.h" #include #include "CoreChecks.h" #include "../../src/devices/Ramdisk/memboard.h" #include "../../src/devices/ssc/tms7000.h" #include "../../src/devices/ssc/ssc_core.h" #include #include static void require(bool condition, const char* message) { if (!condition) throw std::runtime_error(message); } void CheckRamDisk() { require(!InitMemBoard(), "RAM disk allocation failed"); // Same offset and bank values as the Windows guest probe. WritePort(0x40, 0x34); WritePort(0x41, 0x12); for (unsigned char bank = 0; bank < 8; ++bank) { WritePort(0x42, bank); WriteArray(17 + bank); } for (unsigned char bank = 0; bank < 8; ++bank) { WritePort(0x42, bank); require(ReadArray() == 17 + bank, "RAM disk bank data aliases another bank"); require(ReadArray() == 17 + bank, "RAM disk read unexpectedly advances address"); } // High address bits are masked to three bits by the hardware contract. WritePort(0x42, 0xff); require(ReadArray() == 24, "RAM disk bank mask differs"); WritePort(0x40, 0xff); WritePort(0x41, 0xff); WriteArray(0xa5); require(ReadArray() == 0xa5, "Last RAM disk byte is inaccessible"); require(!InitMemBoard(), "RAM disk reinitialization failed"); require(ReadArray() == 0, "RAM disk address zero did not reset"); WritePort(0x40, 0xff); WritePort(0x41, 0xff); WritePort(0x42, 7); require(ReadArray() == 0, "RAM disk reinitialization did not clear last byte"); } void CheckMicrocontroller() { std::array rom{}; rom.fill(0xff); rom[0] = 0xb3; // INC A rom[1] = 0xe0; rom[2] = 0xfd; // JMP back rom[4094] = 0xf0; rom[4095] = 0; ssc::Tms7000 cpu; cpu.LoadRom(rom.data(), rom.size()); cpu.Reset(); require(cpu.PC() == 0xf000, "TMS7000 big-endian reset vector failed"); const auto initial = cpu.PeekReg(0); int cycles = 0; for (int i = 0; i < 50; ++i) cycles += cpu.Execute(360); require(cycles == 18000, "TMS7000 cycle accounting differs"); require(cpu.PC() == 0xf000, "TMS7000 branch target differs"); require(cpu.PeekReg(0) == uint8_t(initial + 1500), "TMS7000 increment/wrap failed"); } void CheckCartridgeBus() { ssc::SscCore core; core.Reset(); // An unaddressed cartridge must stay silent for MPI disk reads. for (int port = 0; port < 256; ++port) if (port != 0x7d && port != 0x7e) require(core.ReadPort(uint8_t(port)) == 0, "SSC intercepts another device's port"); require(core.ReadPort(0x7d) == 0xff, "SSC control-port read differs"); require((core.ReadPort(0x7e) & 0x80) != 0, "SSC initially busy"); core.WritePort(0x7e, 0x55); require((core.ReadPort(0x7e) & 0x80) == 0, "SSC command did not assert busy"); core.WritePort(0x7d, 0); require((core.ReadPort(0x7e) & 0x80) != 0, "SSC reset edge did not clear busy"); } namespace { using VCC::Devices::DriveWireDevice; using VCC::Devices::DriveWireTransport; struct EchoWire final : DriveWireTransport { State status = State::connecting; std::deque echo; State state() override { return status; } std::size_t read(std::uint8_t* bytes, std::size_t limit) override { std::size_t count = std::min(limit, echo.size()); for (std::size_t i = 0; i < count; ++i) { bytes[i] = echo.front(); echo.pop_front(); } return count; } bool write(const std::uint8_t* bytes, std::size_t size) override { echo.insert(echo.end(), bytes, bytes + size); return true; } }; } void CheckDriveWireProtocol() { DriveWireDevice dw; std::vector sockets; dw.setFactory([&](const std::string& host, std::uint16_t port) { require(host == "localhost" && port == 2323, "Wrong DriveWire TCP destination"); auto socket = std::make_unique(); sockets.push_back(socket.get()); return socket; }); auto send = [&](std::initializer_list bytes) { for (auto byte : bytes) dw.writePort(0x42, byte); }; auto receive = [&](const std::vector& expected) { for (auto byte : expected) { require(dw.readPort(0x41) == 2 && dw.readPort(0x41) == 2, "Becker polling consumed a byte"); require(dw.readPort(0x42) == byte, "DriveWire response mismatch"); } require(dw.readPort(0x41) == 0 && dw.readPort(0x42) == 0, "Unexpected trailing DriveWire response"); }; auto fast = [&](std::uint8_t channel, const std::string& data) { for (auto c : data) send({std::uint8_t(0x80 + channel), std::uint8_t(c)}); }; auto response = [&](std::uint8_t channel, const std::string& text) { send({0x43}); receive({std::uint8_t(17 + channel), std::uint8_t(text.size())}); send({0x63, channel, std::uint8_t(text.size())}); receive({text.begin(), text.end()}); }; // Only data-register writes feed packets. DWINIT responds after both bytes. dw.writePort(0x41, 0x43); send({0x5a}); receive({}); send({1}); receive({4}); fast(0, "tcp connect localhost 2323\r"); require(sockets.empty(), "Unopened DriveWire channel connected"); send({0x45, 0, 0xc4, 0, 0x29}); // driver Init and per-path SS.Open send({0xc4, 0, 0x28}); for (unsigned i = 0; i < 26; ++i) send({0xff}); // opaque SS.ComSt descriptor fast(0, "TCP CONNECT localhost 2323\r"); require(sockets.size() == 1, "Expected exactly one outbound connection"); send({0x43}); receive({0, 0}); // socket handshaking sockets[0]->status = DriveWireTransport::State::connected; response(0, "SUCCESS\n\r"); send({0xc4, 0, 0x29}); // duplicate open must preserve an active stream send({0x64, 0, 3, 0, 0xff, 0x43}); // preserve arbitrary bytes, including opcodes send({0x43}); receive({17, 3}); send({0x63, 0, 2}); receive({0, 0xff}); send({0x43}); receive({1, 0x43}); send({0xc3, 0, 0x7f}); sockets[0]->status = DriveWireTransport::State::closed; send({0x43}); receive({1, 0x7f}); // preserve bytes received just before EOF send({0x43}); receive({16, 0}); send({0x43}); receive({0, 0}); send({0x45, 2}); fast(2, "tcp listen 8080\r"); response(2, "FAIL 203\n\r"); send({0x43}); receive({16, 2}); send({0x45, 2}); fast(2, "tcp connect localhost 99999\r"); response(2, "FAIL 203\n\r"); send({0x43}); receive({16, 2}); require(sockets.size() == 1, "Invalid or inbound command created a socket"); send({0x45, 2}); fast(2, "tcp connect localhost 2323\r"); sockets.back()->status = DriveWireTransport::State::failed; response(2, "FAIL 246\n\r"); send({0x43}); receive({16, 2}); // Reset cancels an in-flight connection and restores byte framing. send({0x45, 2}); fast(2, "tcp connect localhost 2323\r"); send({0xff, 0x43}); receive({0, 0}); send({0x45, 2}); fast(2, "tcp connect localhost 2323\r"); require(sockets.size() == 4, "Connection did not reopen after reset"); sockets.back()->status = DriveWireTransport::State::connected; response(2, "SUCCESS\n\r"); send({0xc5, 2, 0x43}); receive({0, 0}); }