// SPDX-License-Identifier: GPL-3.0-or-later // Optional local-media compatibility probe. No ROM or game bytes are bundled. // Usage: sdc-validation SYSTEM_ROM SDC_ROM GAME_FOLDER OUTPUT_FOLDER [6809|6309|starter] #include "../../src/core/MachineSession.h" #include "../../src/devices/SDCDevice.h" #include "../../src/core/tcc1014mmu.h" #include #include #include #include #include #include #include #include using namespace VCC::Mac; using namespace VCC::Devices; namespace fs = std::filesystem; namespace { std::vector read(const fs::path& path) { std::ifstream f(path, std::ios::binary); if (!f) throw std::runtime_error("Cannot read " + path.string()); return {std::istreambuf_iterator(f), std::istreambuf_iterator()}; } 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 = 46; else if (c == '/') bit = 47; else if (c == ':') bit = 42; 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 throw std::logic_error("Unsupported probe character"); return std::uint64_t{1} << bit; } std::string screen() { std::string result; for (unsigned n=0;n<512;++n) { unsigned c=SafeMemRead8(0x400+n)&63; result+=char(c<32?c+64:c); if(n%32==31)result+='\n'; } return result; } void picture(const MachineSession& machine,const fs::path& path) { const auto frame=machine.snapshot().frame; if(!frame)throw std::runtime_error("No rendered frame"); std::ofstream out(path,std::ios::binary); out<<"P6\n"<pixels) { out.put(char(pixel>>16));out.put(char(pixel>>8));out.put(char(pixel)); } if(!out)throw std::runtime_error("Cannot save screenshot"); } struct Probe { MachineSession machine; std::shared_ptr device; std::set seen; std::uint64_t audible=0; unsigned frames=0; void advance(unsigned count) { while(count--) { machine.advance();++frames; for(const auto sample:machine.takeAudio())if(sample.left!=0 || sample.right!=0)++audible; const auto state=device->snapshot(); for(const auto& drive:state.drives)if(drive.mounted && seen.insert(drive.name).second) std::cout<<"MOUNT frame="<(rom,root.string(),true); p.machine.cartridges().configure({nullptr,nullptr,nullptr,nullptr,p.device},true,4); MachineConfiguration settings;settings.processor=cpu;settings.ram=1;settings.frameInterval=6; p.machine.configure(settings); p.machine.load(system,cpu);p.machine.resume();p.advance(300); if(screen().find("OK")==std::string::npos)throw std::runtime_error("Starter BASIC did not boot"); if(!p.machine.basicReadSource().empty())throw std::runtime_error("Starter program ran automatically"); if(p.device->snapshot().drives[0].name!="/HELLO.DSK")throw std::runtime_error("startup.cfg did not mount HELLO.DSK"); p.type("DIR 0\r");p.advance(60); if(screen().find("HELLO")==std::string::npos)throw std::runtime_error("Starter directory did not list HELLO"); picture(p.machine,output/(name+"-starter-directory.ppm")); p.type("LOAD \"HELLO:0\"\r");p.advance(60); for(char choice : {'1','2'}) { p.type("RUN\r");p.advance(60); if(p.machine.copyScreenText().find("What planet do you live on?\n1) Earth\n2) Mars")==std::string::npos) throw std::runtime_error("SDC starter did not display the planet menu"); p.type(std::string(1, choice));p.advance(30); const auto greeting = p.machine.copyScreenText(); const auto message = greeting.find(choice=='1' ? "Hello Nice Blue Dot World!" : "Hello Cold Red Dot World!"); if(message==std::string::npos || greeting.find("OK",message)==std::string::npos) throw std::runtime_error("SDC starter did not greet the player and return to BASIC:\n"+greeting); picture(p.machine,output/(name+(choice=='1' ? "-starter-earth.ppm" : "-starter-mars.ppm"))); } p.type("DRIVE 0,\"/HELLO.DSK\"\r");p.advance(60); const auto final=p.device->snapshot(); if(!final.error.empty() || final.drives[0].name!="/HELLO.DSK")throw std::runtime_error("Starter remount command failed"); if(!final.drives[0].reads || final.drives[0].writes)throw std::runtime_error("Unexpected starter transfer counts"); std::cout<<"PASS "<& system,const std::vector& rom, const fs::path& root,const fs::path& output,Processor cpu) { const std::string name=cpu==Processor::MC6809?"6809":"6309"; std::cout<<"BEGIN "<snapshot().drives[0].mounted)throw std::runtime_error(name+": DRIVE did not mount disk 1"); p.type("RUN \"AUTOEXEC:0\"\r"); bool graphics = false; for(unsigned elapsed=0;elapsed<30000;elapsed+=60) { p.advance(60); try { (void)p.machine.copyScreenText(); } catch(const std::logic_error&) { graphics=true;break; } if(elapsed%3000==0)std::cout<<"LOADING frame="<snapshot(); std::uint64_t reads=0,writes=0; for(const auto& drive:final.drives){reads+=drive.reads;writes+=drive.writes;} std::cout<<"RESULT "<6)throw std::runtime_error("Usage: sdc-validation SYSTEM_ROM SDC_ROM GAME_FOLDER OUTPUT_FOLDER [6809|6309|starter]"); if(argc==6 && std::string(argv[5])!="6809" && std::string(argv[5])!="6309" && std::string(argv[5])!="starter")throw std::runtime_error("Mode must be 6809, 6309 or starter"); auto system=read(argv[1]),rom=read(argv[2]); if(system.size()!=32768 || (rom.size()!=8192 && rom.size()!=16384))throw std::runtime_error("Supply a 32 KiB system ROM and raw 8/16 KiB SDC ROM"); const fs::path folder=fs::canonical(argv[3]),output=argv[4];fs::create_directories(output); std::map> before; for(const auto& entry:fs::directory_iterator(folder))if(entry.is_regular_file())before.emplace(entry.path(),read(entry.path())); for(auto cpu:{Processor::MC6809,Processor::HD6309}) { if(argc==6 && std::string(argv[5])!="starter" && std::string(argv[5])!=(cpu==Processor::MC6809?"6809":"6309"))continue; if(argc==6 && std::string(argv[5])=="starter")runStarter(system,rom,folder,output,cpu); else run(system,rom,folder,output,cpu); } for(const auto& [path,bytes]:before)if(read(path)!=bytes)throw std::runtime_error("Source media changed: "+path.string()); std::cout<<"PASS: all source media bytes unchanged"<