// SPDX-License-Identifier: GPL-3.0-or-later // Optional integration check: user supplies the ROM; no ROM bytes live here. #include "CoreChecks.h" #include "../../src/devices/FD502/Fd502Device.h" #include "../../src/devices/HardDisk/HardDiskDevice.h" #include "../../src/devices/Orchestra90Device.h" #include "../../src/devices/SpeechSoundDevice.h" #include "../../src/core/MachineSession.h" #include "../../src/core/tcc1014mmu.h" #include #include #include #include #include #include namespace { void frames(VCC::Mac::MachineSession& machine, unsigned count) { while (count--) machine.advance(); } std::string screen() { std::string text; for (unsigned i = 0; i < 512; ++i) { const unsigned c = SafeMemRead8(0x400 + i) & 0x3f; text += static_cast(c < 32 ? c + 64 : c); if (i % 32 == 31) text += '\n'; } return text; } std::uint64_t key(char c) { unsigned bit; if (c >= 'A' && c <= 'Z') bit = c - '@'; else if (c >= '0' && c <= '9') bit = 32 + c - '0'; else if (c == ',') bit = 44; else if (c == '/') bit = 47; else if (c == '>') return (std::uint64_t{1} << 46) | (std::uint64_t{1} << 55); else if (c == '"') return (std::uint64_t{1} << 34) | (std::uint64_t{1} << 55); else if (c == ' ') bit = 31; else if (c == '\r') bit = 48; else if (c == '*') return (std::uint64_t{1} << 42) | (std::uint64_t{1} << 55); else throw std::logic_error("Unsupported authored probe character"); return std::uint64_t{1} << bit; } } namespace { void type(VCC::Mac::MachineSession& machine, const std::string& text) { for (char c : text) { machine.setKeyboard(key(c)); frames(machine, 4); machine.setKeyboard(0); frames(machine, 4); } } } namespace { // Direct Run from either workspace must supply a callable entry and leave BASIC // responsive after RTS, including when rerunning from inside an earlier build. void checkWorkspaceReturn(VCC::Mac::MachineSession& machine) { const std::vector program{0, 0, 2, 0x3f, 0, 0x12, 0x39, 0xff, 0, 0, 0x3f, 0}; // NOP, RTS const auto word = [](unsigned at) { return (SafeMemRead8(at) << 8) | SafeMemRead8(at + 1); }; const auto stack = word(0x21) - 2; const auto before = machine.snapshot(); machine.launchAssembly(program, {}, {}, true, false, before.generation, before.debuggerProgramRevision, before.debuggerMappingFingerprint); const auto entry = machine.snapshot(); if (entry.cpu.PC != 0x3f00 || entry.cpu.S != stack || entry.cpu.DP || (entry.cpu.CC & 0x50) || word(stack) != 0xac73) throw std::runtime_error("Assembly Direct Run did not establish the BASIC return frame"); machine.debuggerStep(); if (machine.snapshot().cpu.PC != 0x3f01) throw std::runtime_error("Assembly Direct Run missed its next instruction"); auto overlap = program; overlap.insert(overlap.end() - 5, {0, 0, 1, std::uint8_t(stack >> 8), std::uint8_t(stack), 0}); try { machine.debuggerLoadBinary(overlap, 0x3f00); throw std::runtime_error("A development image overwrote BASIC's return frame"); } catch (const std::invalid_argument&) {} if (word(stack) != 0xac73 || machine.snapshot().cpu.PC != 0x3f01 || machine.snapshot().debuggerProgramRevision != entry.debuggerProgramRevision) throw std::runtime_error("Rejected development image changed the session"); machine.debuggerLoadBinary(program, 0x3f00); if (machine.snapshot().cpu.S != stack) throw std::runtime_error("Rerunning a development image nested its stack"); machine.debuggerClearDevelopmentBreakpoints(); machine.resume(); frames(machine, 60); type(machine, "PRINT 8*9\r"); frames(machine, 30); if (screen().find(" 72 ") == std::string::npos) throw std::runtime_error("BASIC did not accept a new command after the workspace program returned"); } void pasteText(VCC::Mac::MachineSession& machine, const std::string& text) { std::vector chords; for(char c:text) { if(c>='a' && c<='z') chords.push_back(key(c-'a'+'A')); else if(c>='A' && c<='Z') chords.push_back(key(c) | (std::uint64_t{1}<<55)); else chords.push_back(key(c)); } machine.paste(chords); for(unsigned n=0;machine.pasteRemaining() && n<20000;++n)machine.advance(); if(machine.pasteRemaining())throw std::runtime_error("Paced text did not complete"); } } void CheckRomBoot(const char* path) { std::ifstream file(path, std::ios::binary); std::vector bytes(32769); file.read(reinterpret_cast(bytes.data()), bytes.size()); if (file.gcount() != 32768) throw std::runtime_error("ROM check needs a readable 32 KiB system ROM"); bytes.resize(32768); for (auto processor : {VCC::Mac::Processor::MC6809, VCC::Mac::Processor::HD6309}) { VCC::Mac::MachineSession machine; machine.load(bytes, processor); machine.resume(); frames(machine, 300); if (screen().find("OK") == std::string::npos) throw std::runtime_error("ROM did not reach BASIC"); checkWorkspaceReturn(machine); type(machine, "10 PRINT 6*7\rRUN\r"); frames(machine, 60); if (screen().find(" 42 ") == std::string::npos) throw std::runtime_error("BASIC keyboard/program check did not print 42"); machine.pause(); const auto before = machine.snapshot(); frames(machine, 5); if (machine.snapshot().cycles != before.cycles) throw std::runtime_error("Paused ROM continued executing"); machine.resume(); type(machine, "CLS\r"); machine.softReset(); frames(machine, 300); type(machine, "LIST\r"); frames(machine, 20); if (screen().find("PRINT 6*7") == std::string::npos) throw std::runtime_error("Soft reset lost the BASIC program"); type(machine, "CLS\rRUN\r"); frames(machine, 60); if (screen().find(" 42 ") == std::string::npos) throw std::runtime_error("Preserved BASIC program did not run after soft reset"); machine.pause(); machine.softReset(); frames(machine, 5); if (machine.snapshot().cycles != 0 || machine.snapshot().state != VCC::Mac::RunState::Paused) throw std::runtime_error("Paused soft reset executed guest code"); machine.resume(); frames(machine, 300); type(machine, "CLS\rRUN\r"); frames(machine, 60); if (screen().find(" 42 ") == std::string::npos) throw std::runtime_error("Paused soft reset lost the BASIC program"); type(machine, "SOUND100,10\r"); bool sound=false; for(unsigned i=0;i<120;++i) { machine.advance(); for(auto sample:machine.takeAudio()) if(sample.left!=0 || sample.right!=0) sound=true; } if(!sound) throw std::runtime_error("BASIC SOUND produced no audio samples"); for(auto format:{VCC::Mac::TapeFormat::CAS,VCC::Mac::TapeFormat::WAV}) { auto& tape=machine.cassette(); tape.record(format); type(machine, "CSAVE\"TEST\"\r"); frames(machine,1800); auto saved=tape.exportImage(); if(saved.size()<100 || tape.snapshot().motor) throw std::runtime_error("BASIC CSAVE did not finish recording"); tape.mount(saved,format); tape.setFast(false); tape.play(); type(machine,"NEW\rCLS\rCLOAD\r"); frames(machine,2400); type(machine,"LIST\rRUN\r"); frames(machine,60); if(screen().find(" 42 ")==std::string::npos) { std::fprintf(stderr,"Tape load screen:\n%s\n",screen().c_str()); throw std::runtime_error(format==VCC::Mac::TapeFormat::CAS ? "CAS round trip failed" : "WAV round trip failed"); } if(format==VCC::Mac::TapeFormat::CAS) { tape.rewind(); tape.setFast(true); tape.play(); type(machine,"NEW\rCLS\rCLOAD\r"); frames(machine,1200); type(machine,"RUN\r"); frames(machine,60); if(screen().find(" 42 ")==std::string::npos) throw std::runtime_error("Fast CAS round trip failed"); } } std::printf("PASS: BASIC SOUND, CAS/WAV CSAVE/CLOAD, fast CAS (%s)\n", processor == VCC::Mac::Processor::MC6809 ? "6809" : "6309"); machine.reset(); machine.resume(); frames(machine, 300); if (screen().find("OK") == std::string::npos || screen().find(" 42 ") != std::string::npos) throw std::runtime_error("Cold reset did not return to a clean BASIC prompt"); std::printf("PASS: supplied ROM BASIC boot, keyboard program, pause, soft/cold reset (%s)\n", processor == VCC::Mac::Processor::MC6809 ? "6809" : "6309"); // Two authored BIN data blocks: clear text RAM, then execute a program // that writes BIN OK to the display. No assembled binary is bundled. std::vector binary{0,2,0,4,0}; binary.insert(binary.end(),512,0x60); std::vector code; const std::string message = "BIN OK"; for (unsigned i=0;i bytes(size + 1); file.read(reinterpret_cast(bytes.data()), bytes.size()); if (file.gcount() != size) throw std::runtime_error("Disk check needs a 32 KiB system ROM and 8 KiB disk ROM"); bytes.resize(size); return bytes; }; const auto systemRom = readRom(systemPath, 32768), diskRom = readRom(diskPath, 8192); auto checkScreen = [](const char* expected, const char* operation) { const auto text = screen(); if (text.find(expected) == std::string::npos || text.find("ERROR") != std::string::npos) { std::fprintf(stderr, "%s screen:\n%s\n", operation, text.c_str()); throw std::runtime_error(operation); } }; auto checkFormat = [&](auto cpu, DiskFormat format, const char* formatName) { VCC::Mac::MachineSession machine; auto device = makeDevice(DeviceKind::FD502, diskRom); auto* fdc = static_cast(device.get()); fdc->mount(0, DiskImage::blank({}, format)); machine.cartridges().install(std::move(device)); machine.load(systemRom, cpu); machine.resume(); frames(machine, 300); checkScreen("DISK EXTENDED", "Disk BASIC boot"); type(machine, "CLS\rDSKINI0\r"); frames(machine, 6000); checkScreen("OK", "DSKINI formatting"); if (fdc->snapshot().busy) throw std::runtime_error("DSKINI left the controller busy"); type(machine, "10 PRINT 6*7\rSAVE\"TEST\"\r"); frames(machine, 1200); checkScreen("OK", "BASIC disk SAVE"); type(machine, "CLS\rDIR\r"); frames(machine, 300); checkScreen("TEST", "Directory after SAVE"); const auto saved = fdc->image(0)->bytes(); fdc->markSaved(0); fdc->eject(0); fdc->mount(0, DiskImage::load(saved)); machine.reset(); frames(machine, 300); type(machine, "CLS\rLOAD\"TEST\"\r"); frames(machine, 1200); type(machine, "LIST\rRUN\r"); frames(machine, 60); checkScreen("PRINT 6*7", "Reloaded disk program LIST"); checkScreen(" 42 ", "Reloaded disk program RUN"); if (fdc->image(0)->bytes() != saved) throw std::runtime_error("Reading changed the disk image"); fdc->mount(0, DiskImage::load(saved, true)); type(machine, "CLS\rSAVE\"BLOCK\"\r"); frames(machine, 600); if (screen().find("?WP ERROR") == std::string::npos || fdc->image(0)->bytes() != saved) throw std::runtime_error("Write-protected SAVE must report WP ERROR and preserve disk bytes"); std::printf("PASS: Disk BASIC DSKINI, SAVE, DIR, eject/reopen, cold boot, LOAD/LIST/RUN and write protection (%s, %s)\n", formatName, cpu == VCC::Mac::Processor::MC6809 ? "6809" : "6309"); }; for (auto cpu : {VCC::Mac::Processor::MC6809, VCC::Mac::Processor::HD6309}) { checkFormat(cpu, DiskFormat::JVC, "DSK"); checkFormat(cpu, DiskFormat::DMK, "DMK"); checkFormat(cpu, DiskFormat::VDK, "VDK"); } } // Optional reference-image check. All writes stay in an owned memory copy; // supplied system ROM, RGBDOS ROM and NitrOS-9 VHD are opened read-only. void CheckHardDiskRom(const char* systemPath, const char* rgbPath, const char* imagePath, const char* dataPath) { using namespace VCC::Devices; auto read = [](const char* path, std::size_t limit) { std::ifstream file(path, std::ios::binary | std::ios::ate); auto length = file.tellg(); if (!file || length <= 0 || std::uint64_t(length) > limit) throw std::runtime_error("Invalid supplied hard-disk fixture"); std::vector bytes(static_cast(length)); file.seekg(0); if (!file.read(reinterpret_cast(bytes.data()), bytes.size())) throw std::runtime_error("Unable to read supplied hard-disk fixture"); return bytes; }; const auto system = read(systemPath,32768), rgb = read(rgbPath,16384), original = read(imagePath,HardDiskImage::maxBytes); // Read a standard OS-9 RBF file descriptor and its segment list. Used only // for independent verification of the guest's on-disk result. auto fileBytes = [](const std::vector& disk, std::uint32_t descriptor) { const auto fd = std::size_t(descriptor) * 256; if (fd + 256 > disk.size()) throw std::runtime_error("RBF descriptor outside image"); std::uint32_t size = 0; for (unsigned i=9;i<13;++i) size = (size<<8)|disk[fd+i]; if (size > HardDiskImage::maxBytes) throw std::runtime_error("RBF file size outside limit"); std::vector result; for (unsigned entry=16; entry+5<=256 && result.size() disk.size()) throw std::runtime_error("RBF segment outside image"); result.insert(result.end(),disk.begin()+start,disk.begin()+start+length); } if (result.size()!=size) throw std::runtime_error("Incomplete RBF file"); return result; }; for (auto cpu : {VCC::Mac::Processor::MC6809,VCC::Mac::Processor::HD6309}) { VCC::Mac::MachineSession machine; auto hard = std::make_shared(); hard->mount(0,HardDiskImage(original)); if (dataPath) hard->mount(1,HardDiskImage(read(dataPath,HardDiskImage::maxBytes))); auto floppy = std::make_shared(rgb); machine.cartridges().configure({nullptr,hard,nullptr,nullptr,floppy},true,4); machine.load(system,cpu); if (dataPath) machine.requestNitros9Boot(); machine.resume(); try { frames(machine,600); } catch (...) { std::fprintf(stderr,"Boot screen:\n%s\n",screen().c_str()); throw; } if (!dataPath && screen().find("HARD DISK BASIC")==std::string::npos) throw std::runtime_error("RGBDOS did not boot"); if (!dataPath) type(machine,"DOS255\r"); frames(machine,3600); if (machine.nitros9BootPending()) throw std::runtime_error("Automatic boot still pending"); type(machine,"ECHO VCCPORTCHECK >/H0/VCCMACTEST\r"); frames(machine,600); const auto& result = hard->image(0)->bytes(); auto root = (result[8]<<16)|(result[9]<<8)|result[10]; auto entries = fileBytes(result,root); bool found = false; for (std::size_t i=0;i+32<=entries.size();i+=32) { if (!entries[i]) continue; std::string name; for (unsigned j=0;j<29;++j) { auto c=entries[i+j]; name += char(c&127); if(c&128) break; } if (name!="vccmactest") continue; auto fd = (entries[i+29]<<16)|(entries[i+30]<<8)|entries[i+31]; auto bytes = fileBytes(result,fd); auto text = std::string(bytes.begin(),bytes.end()); if (text.find("vccportcheck")==std::string::npos) throw std::runtime_error("NitrOS-9 file contents mismatch"); // Reopen the serialized image independently and read the same file. HardDiskImage reopened(result); if (fileBytes(reopened.bytes(),fd)!=bytes) throw std::runtime_error("Hard disk reopen mismatch"); found = true; break; } if (!found || !hard->image(0)->dirty()) throw std::runtime_error("NitrOS-9 did not create its test file"); if (dataPath) { pasteText(machine,"echo sharedportcheck >/h1/vccmactest\r"); frames(machine,600); // Interleave reads from HD0 with writes to HD1; this catches a // driver that selects a unit once instead of on every transfer. pasteText(machine,"dir /dd/games >/h1/catalog\r"); frames(machine,600); const auto shared = hard->image(1)->bytes(); const auto before = read(dataPath,HardDiskImage::maxBytes); if (!std::equal(before.begin()+0x5a000*256,before.end(),shared.begin()+0x5a000*256)) throw std::runtime_error("OS-9 write modified BASIC volumes"); auto root1=(shared[8]<<16)|(shared[9]<<8)|shared[10]; auto directory=fileBytes(shared,root1); bool written=false, catalog=false; for (std::size_t i=0;i+32<=directory.size();i+=32) { if (!directory[i]) continue; std::string name; for(unsigned j=0;j<29;++j) { auto c=directory[i+j];name+=char(c&127);if(c&128)break; } if(name!="vccmactest" && name!="catalog")continue; auto fd=(directory[i+29]<<16)|(directory[i+30]<<8)|directory[i+31]; auto bytes=fileBytes(shared,fd); if(name=="catalog") { std::string listing(bytes.begin(),bytes.end()); std::transform(listing.begin(),listing.end(),listing.begin(),[](unsigned char c) { return c >= 'A' && c <= 'Z' ? c+32 : c; }); catalog=listing.find("rogue")!=std::string::npos; continue; } if(std::string(bytes.begin(),bytes.end()).find("sharedportcheck")==std::string::npos) throw std::runtime_error("Shared HD1 write content mismatch"); HardDiskImage reopened(shared); if(fileBytes(reopened.bytes(),fd)!=bytes)throw std::runtime_error("Shared HD1 reopen mismatch"); written=true; } if(!written || !catalog)throw std::runtime_error("NitrOS-9 HD1 write or cross-drive catalogue read failed"); std::printf("PASS: automatic DOS 255, shared OS-9 HD1 write/reopen, BASIC volumes unchanged\n"); } std::printf("PASS: RGBDOS, NitrOS-9 boot, guest file creation and raw-image reopen (%s)\n",cpu==VCC::Mac::Processor::MC6809?"6809":"6309"); } } namespace { void installPlanetSource(VCC::Mac::MachineSession& machine) { // Packaging and the documented regeneration commands run from the checkout. std::ifstream file("examples/basic/planet.bas"); if (!file) throw std::runtime_error("Cannot open examples/basic/planet.bas; run from the repository root"); std::string source{std::istreambuf_iterator(file), {}}; machine.basicLoadSource(source, false); frames(machine, 30); } void runPlanetStarter(VCC::Mac::MachineSession& machine, char choice) { type(machine, "CLS\rRUN\r"); frames(machine, 30); type(machine, "3"); // An unrelated key leaves BASIC waiting at the menu. const auto menu = machine.copyScreenText(); if (menu.find("What planet do you live on?\n1) Earth\n2) Mars") == std::string::npos || menu.find("World!") != std::string::npos || menu.find("OK") != std::string::npos) throw std::runtime_error("Planet starter did not wait for a choice:\n" + menu); type(machine, std::string(1, choice)); frames(machine, 30); const auto result = machine.copyScreenText(); const auto greeting = result.find(choice == '1' ? "Hello Nice Blue Dot World!" : "Hello Cold Red Dot World!"); if (greeting == std::string::npos || result.find("OK", greeting) == std::string::npos) throw std::runtime_error("Planet starter did not greet the player and return to BASIC:\n" + result); type(machine, "WIDTH 32\r"); // The disk command probes use the 32-column BASIC display. } } // Reproducible starter media, authored through real BASIC and independently // reloaded on both CPUs. Only the caller-supplied output directory is written. void CreateStarterMedia(const char* systemPath, const char* diskPath, const char* output) { using namespace VCC::Devices; using namespace VCC::Mac; auto read = [](const char* path, std::size_t size) { std::ifstream f(path, std::ios::binary); std::vector data(size + 1); f.read(reinterpret_cast(data.data()), data.size()); if (std::size_t(f.gcount()) != size) throw std::runtime_error("Starter media requires a 32 KiB system and 8 KiB disk ROM"); data.resize(size); return data; }; auto expect = [](const char* text) { if (screen().find(text) == std::string::npos || screen().find("ERROR") != std::string::npos) throw std::runtime_error(std::string("Starter verification failed: ") + text + "\n" + screen()); }; const auto system = read(systemPath,32768), rom = read(diskPath,8192); std::vector disk, tape; { MachineSession m; auto fdc=std::make_shared(rom); fdc->mount(0,DiskImage::blank()); m.cartridges().configure({nullptr,nullptr,nullptr,nullptr,fdc},true,4); m.load(system);m.resume();frames(m,300);expect("DISK EXTENDED"); type(m,"CLS\rDSKINI0\r");frames(m,6000);expect("OK"); installPlanetSource(m); type(m,"SAVE\"HELLO\"\r");frames(m,1200);expect("OK"); type(m,"CLS\rDIR\r");frames(m,120);expect("HELLO");disk=fdc->image(0)->bytes(); m.cassette().record(TapeFormat::CAS);type(m,"CSAVE\"HELLO\"\r");frames(m,1800); if(m.cassette().snapshot().motor) throw std::runtime_error("Starter CSAVE did not finish"); tape=m.cassette().exportImage(); } for(auto cpu:{Processor::MC6809,Processor::HD6309}) { MachineSession m;auto fdc=std::make_shared(rom);fdc->mount(0,DiskImage::load(disk)); m.cartridges().configure({nullptr,nullptr,nullptr,nullptr,fdc},true,4); m.cassette().mount(tape,TapeFormat::CAS);m.load(system,cpu);m.resume();frames(m,300); expect("DISK EXTENDED");expect("OK"); if(m.cassette().snapshot().mode!=TapeMode::Stopped || m.cassette().snapshot().position!=0) throw std::runtime_error("Starter cassette must remain stopped at launch"); type(m,"CLS\rLIST\r");frames(m,60); if(!m.basicReadSource().empty()) throw std::runtime_error("Starter program was loaded automatically"); type(m,"LOAD\"HELLO\"\r");frames(m,1200); runPlanetStarter(m,'1'); runPlanetStarter(m,'2'); type(m,"NEW\rCLS\r");m.cassette().play();type(m,"CLOAD\"HELLO\"\r");frames(m,3600); runPlanetStarter(m,'1'); runPlanetStarter(m,'2'); if(fdc->image(0)->bytes()!=disk) throw std::runtime_error("Starter disk changed on read"); std::printf("PASS: starter Disk BASIC, no auto-load, disk LOAD/RUN, cassette CLOAD/RUN (%s)\n",cpu==Processor::MC6809?"6809":"6309"); } auto write=[&](const char* name,const std::vector& data) { std::ofstream f(std::string(output)+"/"+name,std::ios::binary|std::ios::trunc); f.write(reinterpret_cast(data.data()),data.size()); if(!f)throw std::runtime_error("Could not write starter media"); }; write("Hello World.dsk",disk);write("Hello World.cas",tape); } // Authored BASIC-only hard-disk template. Reserved OS-9 space is zero-filled; // no downloaded disk contents or operating system are redistributed. void CreateHardBasicMedia(const char* systemPath, const char* rgbPath, const char* floppyPath, const char* output) { using namespace VCC::Devices; using namespace VCC::Mac; auto read=[](const char* path, std::size_t size) { std::ifstream f(path,std::ios::binary); std::vector data(size+1); f.read(reinterpret_cast(data.data()),data.size()); if(std::size_t(f.gcount())!=size) throw std::runtime_error("Unexpected hard BASIC input size"); data.resize(size);return data; }; auto expect=[](const char* text) { if(screen().find(text)==std::string::npos || screen().find("ERROR")!=std::string::npos) throw std::runtime_error(std::string("Hard BASIC verification failed: ")+text+"\n"+screen()); }; const auto system=read(systemPath,32768), rgb=read(rgbPath,8192), floppy=read(floppyPath,161280); constexpr std::size_t offset=0x5a000*256, volume=161280; std::vector image(offset+256*volume,0), blank, hello; { MachineSession m;auto hd=std::make_shared();hd->mount(0,HardDiskImage(image)); auto fd=std::make_shared(rgb);fd->mount(0,DiskImage::load(floppy)); m.cartridges().configure({nullptr,hd,nullptr,nullptr,fd},true,4); m.load(system);m.resume();frames(m,600);expect("HARD DISK BASIC"); type(m,"CLS\rDSKINI0\r");frames(m,60);type(m,"Y");frames(m,1200);expect("OK"); const auto& formatted=hd->image(0)->bytes(); blank.assign(formatted.begin()+offset,formatted.begin()+offset+volume); installPlanetSource(m); type(m,"SAVE\"HELLO\"\r");frames(m,1200);expect("OK"); const auto& saved=hd->image(0)->bytes(); hello.assign(saved.begin()+offset,saved.begin()+offset+volume); std::copy(hello.begin(),hello.end(),image.begin()+offset); for(unsigned i=1;i<256;++i) std::copy(blank.begin(),blank.end(),image.begin()+offset+i*volume); } for(auto cpu:{Processor::MC6809,Processor::HD6309}) { MachineSession m;auto hd=std::make_shared();hd->mount(0,HardDiskImage(image)); auto fd=std::make_shared(rgb);fd->mount(0,DiskImage::load(floppy)); m.cartridges().configure({nullptr,hd,nullptr,nullptr,fd},true,4); m.load(system,cpu);m.resume();frames(m,600);expect("HARD DISK BASIC"); type(m,"CLS\rLIST\r");frames(m,60);expect("OK"); if(!m.basicReadSource().empty()) throw std::runtime_error("Hard BASIC must not load/run a program at boot"); const auto hdReads = hd->readEvents(0); const auto fdReads = fd->readEvents(0); type(m,"DIR\rLOAD\"HELLO\"\r");frames(m,600);expect("OK"); runPlanetStarter(m,'1'); runPlanetStarter(m,'2'); if(hd->readEvents(0)==hdReads || fd->readEvents(0)!=fdReads) throw std::runtime_error("RGBDOS did not read the starter from HD0"); const auto afterHD = hd->readEvents(0); type(m,"NEW\rDRIVE OFF\rLOAD\"HELLO\"\r");frames(m,1200);expect("OK"); runPlanetStarter(m,'1'); if(fd->readEvents(0)==fdReads || hd->readEvents(0)!=afterHD) throw std::runtime_error("DRIVE OFF did not read the starter from FD0"); const auto afterFD = fd->readEvents(0); type(m,"NEW\rDRIVE ON\rLOAD\"HELLO\"\r");frames(m,600); runPlanetStarter(m,'2'); if(hd->readEvents(0)==afterHD || fd->readEvents(0)!=afterFD) throw std::runtime_error("DRIVE ON did not return to HD0"); if(fd->image(0)->bytes()!=floppy || hd->image(0)->bytes()!=image) throw std::runtime_error("Reading starters altered their images"); type(m,"CLS\rDRIVE255\rSAVE\"CHECK\"\r");frames(m,600);expect("OK"); auto saved=hd->image(0)->bytes();hd->eject(0,true);hd->mount(0,HardDiskImage(saved)); type(m,"NEW\rLOAD\"CHECK\"\r");frames(m,600); runPlanetStarter(m,'2'); if(saved==image || !std::equal(image.begin(),image.begin()+offset+255*volume,saved.begin())) throw std::runtime_error("Last virtual disk SAVE altered other volumes"); std::printf("PASS: Hard BASIC boot/no auto-load, HD/floppy LOAD/RUN, DRIVE OFF/ON, volume 255 SAVE/reopen (%s)\n",cpu==Processor::MC6809?"6809":"6309"); } auto write=[&](const char* name,const std::vector& data) { std::ofstream f(std::string(output)+"/"+name,std::ios::binary|std::ios::trunc); f.write(reinterpret_cast(data.data()),data.size()); if(!f)throw std::runtime_error("Could not write authored hard BASIC template"); }; write("Blank Disk.dsk",blank);write("Hello Hard Disk.dsk",hello); } void CheckClipboardRom(const char* path) { std::ifstream file(path,std::ios::binary); std::vector rom((std::istreambuf_iterator(file)),{}); for(auto cpu:{VCC::Mac::Processor::MC6809,VCC::Mac::Processor::HD6309}) { VCC::Mac::MachineSession m;m.load(rom,cpu);m.resume();frames(m,300); auto paste=[&](const std::string& text) { std::vector keys; for(char c:text) { auto chord=key(c); if(c>='A' && c<='Z')chord|=std::uint64_t{1}<<55; // Native clipboard uppercase mapping. keys.push_back(chord); } m.paste(keys); for(unsigned n=0;m.pasteRemaining() && n<20000;++n)m.advance(); if(m.pasteRemaining())throw std::runtime_error("Paste did not finish"); frames(m,30); }; paste("10 PRINT 6*7\r20 PRINT 7*7\r"); paste("20 PRINT 8*8\rCLS\rRUN\r"); auto text=m.copyScreenText(); if(text.find(" 42")==std::string::npos || text.find(" 64")==std::string::npos || text.find(" 49")!=std::string::npos) throw std::runtime_error("BASIC merge/copy did not preserve or replace lines: "+text); paste("NEW\r10 PRINT 9*9\rCLS\rRUN\r");text=m.copyScreenText(); if(text.find(" 81")==std::string::npos || text.find(" 42")!=std::string::npos) throw std::runtime_error("BASIC replacement/copy failed: "+text); // Verify the safe entry limit through the actual interpreter. std::string line="30 REM "+std::string(242,'A'); // exactly 249 characters paste(line+"\r"); paste("CLS\rLIST 30\r");text=m.copyScreenText(); if(text.find("30 REM")==std::string::npos || std::count(text.begin(),text.end(),'A')!=242) throw std::runtime_error("Boundary BASIC line failed: "+text); std::printf("PASS: paced BASIC merge/replace, 249-character boundary, copy screen (%s)\n",cpu==VCC::Mac::Processor::MC6809?"6809":"6309"); } } // Optional private-ROM integration; generated PCM is a local listening artifact. void CheckOrchestraRom(const char* systemPath, const char* orchestraPath, const char* outputPrefix) { auto read = [](const char* path, std::size_t size) { std::ifstream f(path, std::ios::binary); std::vector b(size+1); f.read(reinterpret_cast(b.data()), b.size()); if (std::size_t(f.gcount()) != size) throw std::runtime_error("Unexpected Orchestra reference ROM size"); b.resize(size); return b; }; auto system = read(systemPath,32768), rom = read(orchestraPath,8192); for (auto cpu : {VCC::Mac::Processor::MC6809, VCC::Mac::Processor::HD6309}) { VCC::Mac::MachineSession m; m.cartridges().configure({nullptr,std::make_shared(rom),nullptr,nullptr,nullptr},true,1); m.load(system,cpu); m.resume(); frames(m,300); type(m," "); frames(m,60); // Dismiss the copyright screen, per manual page 8. if (screen().find("WILLIAM TELL") == std::string::npos) { std::fprintf(stderr,"%s",screen().c_str()); throw std::runtime_error("Orchestra ROM did not reach its demonstration screen"); } type(m,"S\r"); frames(m,600); m.takeAudio(); type(m,"P\r"); m.takeAudio(); std::vector pcm; for (unsigned i=0;i<3600;++i) { m.advance(); auto part=m.takeAudio(); pcm.insert(pcm.end(),part.begin(),part.end()); } float minL=1,maxL=-1,minR=1,maxR=-1; unsigned different=0; for(auto v:pcm) { minL=std::min(minL,v.left);maxL=std::max(maxL,v.left);minR=std::min(minR,v.right);maxR=std::max(maxR,v.right);different+=v.left!=v.right; } if(maxL-minL<.05f || maxR-minR<.05f || different<1000) throw std::runtime_error("Demonstration did not produce independent stereo music"); std::string path=std::string(outputPrefix)+(cpu==VCC::Mac::Processor::MC6809 ? "-6809.wav" : "-6309.wav"); if (pcm.size() > (std::numeric_limits::max() - 36u) / 4u) throw std::runtime_error("Listening capture exceeds the WAV size limit"); const auto dataBytes = static_cast(pcm.size() * 4u); std::ofstream out(path,std::ios::binary); auto word=[&](std::uint32_t v,unsigned n){while(n--){out.put(v&255);v>>=8;}}; out.write("RIFF",4);word(36u+dataBytes,4);out.write("WAVEfmt ",8);word(16,4);word(1,2);word(2,2); word(44100,4);word(44100*4,4);word(4,2);word(16,2);out.write("data",4);word(dataBytes,4); for(auto v:pcm) {word(std::uint16_t(std::int16_t(v.left*32767)),2);word(std::uint16_t(std::int16_t(v.right*32767)),2);} if(!out)throw std::runtime_error("Could not write private listening capture"); std::printf("Orchestra %s: %zu stereo samples, %u differing channels, ranges %.3f/%.3f; %s\n",cpu==VCC::Mac::Processor::MC6809 ? "6809" : "6309",pcm.size(),different,maxL-minL,maxR-minR,path.c_str()); } } // Authored guest sends documented SSC command bytes, polling BUSY before each. void CheckSpeechSoundRom(const char* systemPath, const char* picPath, const char* speechPath, const char* outputPrefix) { using namespace VCC::Mac; auto read=[](const char* path,std::size_t size){ std::ifstream f(path,std::ios::binary);std::vector b(size+1); f.read(reinterpret_cast(b.data()),b.size()); if(std::size_t(f.gcount())!=size)throw std::runtime_error("Unexpected Speech/Sound reference ROM size"); b.resize(size);return b; }; auto system=read(systemPath,32768),rom=read(picPath,4096),speech=read(speechPath,2048); rom.insert(rom.end(),speech.begin(),speech.end()); for(auto cpu:{Processor::MC6809,Processor::HD6309}) { MachineSession m;auto device=std::make_shared(rom); m.cartridges().configure({nullptr,nullptr,device,nullptr,nullptr},true,4); m.load(system,cpu);m.resume();frames(m,60);m.takeAudio(); auto send=[&](const std::vector& bytes){ const auto end=0x3040+bytes.size(); // Authored guest explicitly selects cartridge SND and enables the // analog mux, just as physical Speech/Sound software must do. std::vector code{0xb6,0xff,0x01,0x84,0xf7,0xb7,0xff,0x01, 0xb6,0xff,0x03,0x8a,0x08,0xb7,0xff,0x03, 0xb6,0xff,0x23,0x8a,0x08,0xb7,0xff,0x23,0x8e,0x30,0x40,0x8c,std::uint8_t(end>>8),std::uint8_t(end), 0x27,12,0xe6,0x80,0xb6,0xff,0x7e,0x2a,0xfb,0xf7,0xff,0x7e,0x20,0xef,0x20,0xfe}; code.resize(64,0x12);code.insert(code.end(),bytes.begin(),bytes.end()); std::vector binary{0,0,std::uint8_t(code.size()),0x30,0}; binary.insert(binary.end(),code.begin(),code.end());binary.insert(binary.end(),{0xff,0,0,0x30,0}); m.quickLoad(binary); }; std::string phrase="HELLO FROM YOUR COLOR COMPUTER. \r"; send({phrase.begin(),phrase.end()}); std::vector pcm; for(unsigned i=0;i<600;++i){m.advance();auto part=m.takeAudio();pcm.insert(pcm.end(),part.begin(),part.end());} if(device->diagnostics().CommandCount()!=phrase.size() || device->diagnostics().AldSpeechCount()<5 || device->diagnostics().CpuIllegalCount()!=0) throw std::runtime_error("Speech firmware failed command/phoneme execution"); float low=1,high=-1; for(auto sample:pcm){low=std::min(low,sample.left);high=std::max(high,sample.left);if(sample.left!=sample.right)throw std::runtime_error("SSC must be mono on both output channels");} if(high-low<.05f)throw std::runtime_error("Speech produced no signal"); std::string path=std::string(outputPrefix)+(cpu==Processor::MC6809?"-6809.wav":"-6309.wav"); if (pcm.size() > (std::numeric_limits::max() - 36u) / 4u) throw std::runtime_error("Listening capture exceeds the WAV size limit"); const auto dataBytes = static_cast(pcm.size() * 4u); std::ofstream out(path,std::ios::binary); auto word=[&](std::uint32_t v,unsigned n){while(n--){out.put(v&255);v>>=8;}}; out.write("RIFF",4);word(36u+dataBytes,4);out.write("WAVEfmt ",8);word(16,4);word(1,2);word(2,2); word(44100,4);word(44100*4,4);word(4,2);word(16,2);out.write("data",4);word(dataBytes,4); // Capture the actual signed native PCM without introducing a DC offset. for(auto v:pcm){word(std::uint16_t(std::int16_t(std::clamp(v.left,-1.0f,1.0f)*32767)),2);word(std::uint16_t(std::int16_t(std::clamp(v.right,-1.0f,1.0f)*32767)),2);} if(!out)throw std::runtime_error("Could not write speech listening capture"); const auto ay=device->diagnostics().AyWriteCount(); send({0xaf,0,100,1,0,7,62,8,15,0xff}); float musicLow=1,musicHigh=-1; for(unsigned i=0;i<60;++i){m.advance();for(auto v:m.takeAudio()){musicLow=std::min(musicLow,v.left);musicHigh=std::max(musicHigh,v.left);}} if(device->diagnostics().AyWriteCount()diagnostics().AldSpeechCount(),high-low,musicHigh-musicLow,path.c_str()); } } // Direct-memory BASIC transfers must match bytes produced by the ROM itself. void CheckBasicProgramRom(const char* systemPath, const char* diskPath) { auto read = [](const char* path) { std::ifstream stream(path, std::ios::binary); if (!stream) throw std::runtime_error("Unable to open BASIC integration ROM"); return std::vector((std::istreambuf_iterator(stream)), {}); }; const auto system=read(systemPath), disk=read(diskPath); for (auto cpu : {VCC::Mac::Processor::MC6809,VCC::Mac::Processor::HD6309}) { VCC::Mac::MachineSession m; auto floppy = std::make_shared(disk); m.cartridges().configure({nullptr,nullptr,nullptr,nullptr,floppy},true,4); m.load(system,cpu);m.resume();frames(m,300); type(m,"10 PRINT 6*7\r20 END\r");frames(m,30); auto program = [&] { unsigned start=(SafeMemRead8(0x19)<<8)|SafeMemRead8(0x1a), end=(SafeMemRead8(0x1b)<<8)|SafeMemRead8(0x1c); std::vector bytes; for(unsigned p=start;pmount(0,VCC::Devices::DiskImage::load(exportedDisk)); type(m,"NEW\rCLS\rLOAD\"HELLO\"\r");frames(m,1200); if(m.basicReadSource()!=exportSource) throw std::runtime_error("Exported DSK did not LOAD identical BASIC source: "+screen()); type(m,"RUN\r");frames(m,60); if(screen().find(" 42 ")==std::string::npos)throw std::runtime_error("Exported DSK program did not execute"); type(m,"NEW\rCLS\r");m.cassette().mount(exportedTape,VCC::Mac::TapeFormat::CAS);m.cassette().setFast(false);m.cassette().play(); type(m,"CLOAD\"HELLO\"\r");frames(m,3600); if(m.basicReadSource()!=exportSource)throw std::runtime_error("Exported CAS did not CLOAD identical BASIC source: "+screen()); type(m,"RUN\r");frames(m,60); if(screen().find(" 42 ")==std::string::npos)throw std::runtime_error("Exported CAS program did not execute"); const auto stable=m.basicReadSource(); bool rejected=false; try {m.basicLoadSource("10 PRINT1\n10 PRINT2",true);}catch(const std::exception&){rejected=true;} if(!rejected || m.basicReadSource()!=stable) throw std::runtime_error("Rejected BASIC source changed RAM"); m.basicLoadSource("10 GOTO10",true);frames(m,10); rejected=false;try{m.basicLoadSource("10 END",true);}catch(const std::exception&){rejected=true;} if(!rejected || m.basicReadSource()!="10 GOTO10\n") throw std::runtime_error("Running BASIC program was replaced"); std::printf("PASS: BASIC token oracle, paused Load/Read/Run, disk LOAD/cassette CLOAD multi-block exports, output, and atomic rejection (%s)\n",cpu==VCC::Mac::Processor::MC6809?"6809":"6309"); } } void CheckGraphicsPreviewRom(const char* systemPath, const char* diskPath, const char* programPath) { using namespace VCC::Mac; using Mode = DebugGraphicsMode; auto read = [](const char* path) { std::ifstream input(path, std::ios::binary); if (!input) throw std::runtime_error(std::string("Unable to open ") + path); return std::vector((std::istreambuf_iterator(input)), {}); }; const auto system = read(systemPath), disk = read(diskPath), sourceBytes = read(programPath); const std::string program(sourceBytes.begin(), sourceBytes.end()); auto expect = [](bool condition, const char* message) { if (!condition) throw std::runtime_error(message); }; for (auto cpu : {Processor::MC6809, Processor::HD6309}) { MachineSession machine; auto floppy = std::make_shared(disk); machine.cartridges().configure({nullptr, nullptr, nullptr, nullptr, floppy}, true, 4); machine.load(system, cpu); machine.resume(); frames(machine, 300); machine.basicLoadSource(program, true, {40, 50, 60}); auto stop = [&](int line) { for (unsigned i = 0; i < 300 && machine.snapshot().state == RunState::Running; ++i) machine.advance(); const auto stopped = machine.snapshot(); if (stopped.basicStoppedLine != line || stopped.state != RunState::Paused || !stopped.frame) throw std::runtime_error("Two-page program did not stop before line " + std::to_string(line)); return stopped; }; auto current = [&] { return machine.debuggerGraphicsPreview(true, false, 0, Mode::SG4); }; // These are Disk BASIC's legacy pages 1 and 3 in the default memory map. // A PMODE 1 bitmap occupies two consecutive 1,536-byte pages. constexpr unsigned first = 0x0e00, second = 0x1a00; auto mapped = [&](unsigned address) { return machine.debuggerGraphicsPreview(false, false, address, Mode::PMODE1); }; auto compareToFrame = [&](const DebugGraphicsPreview& preview, const Snapshot& stopped) { expect(preview.width == 128 && preview.height == 96 && preview.bytesPerRow == 32 && preview.pixels.size() == 128*96 && preview.modeName == "PMODE 1", "Wrong active PMODE 1 layout"); const auto& frame = *stopped.frame; const auto r = frame.activeArea; expect(r.width == 512 && r.height == 384, "Unexpected live PMODE raster geometry"); for (unsigned y = 0; y < 96; ++y) for (unsigned x = 0; x < 128; ++x) expect(frame.pixels[(r.y+y*4)*Frame::width + r.x+x*4] == preview.pixels[y*128+x], "Current-page colors differ from the actual published video frame"); }; const auto beforeDraw = stop(40); const auto front = current(), hiddenBlank = mapped(second); compareToFrame(front, beforeDraw); expect(front.physical && front.pixels == mapped(first).pixels && front.bytes == mapped(first).bytes, "Mapped page 1 does not match displayed RAM"); expect(front.pixels[10*128+10] != front.pixels[0], "First page is missing its square"); expect(std::all_of(hiddenBlank.bytes.begin(), hiddenBlank.bytes.end(), [](auto b) { return b == 0xff; }), "The selected drawing page should still be clear"); machine.basicContinue(program); const auto beforeFlip = stop(50); const auto stillFront = current(), hiddenDrawing = mapped(second); compareToFrame(stillFront, beforeFlip); expect(front.pixels == stillFront.pixels && front.address == stillFront.address && beforeDraw.frame->pixels == beforeFlip.frame->pixels, "Drawing to hidden page affected the displayed screen"); for (unsigned y = 0; y < 96; ++y) { for (unsigned x = 0; x < 128; ++x) { const bool changed = hiddenDrawing.pixels[y*128+x] != hiddenBlank.pixels[y*128+x]; expect(changed == (x >= 80 && x < 112 && y >= 32 && y < 64), "The hidden square must occupy exactly its BASIC coordinates"); } } const auto selected = hiddenDrawing.pixelInfo(81, 32); expect(hiddenDrawing.byteAddresses[selected.byteIndex] == 0x1e14 && hiddenDrawing.bytes[selected.byteIndex] == 0xaa && selected.mask == 0x30, "Wrong mapped byte or bits for a pixel in the hidden drawing"); expect(machine.snapshot().cycles == beforeFlip.cycles && machine.snapshot().cpu.PC == beforeFlip.cpu.PC, "Preview moved the stopped CPU"); machine.basicContinue(program); const auto afterFlip = stop(60); const auto displayed = current(); compareToFrame(displayed, afterFlip); expect(displayed.address == front.address + second - first && displayed.bytes == hiddenDrawing.bytes && displayed.pixels == hiddenDrawing.pixels && mapped(first).pixels == front.pixels, "SCREEN did not reveal the untouched second page"); std::printf("PASS: PMODE 1 two-page preview, 1,024 hidden changed pixels, $1E14 bits 5–4, " "and SCREEN page flip (%s)\n", cpu == Processor::MC6809 ? "6809" : "6309"); } }