179 lines
10 KiB
C++
179 lines
10 KiB
C++
// 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 <algorithm>
|
|
#include <filesystem>
|
|
#include <fstream>
|
|
#include <iostream>
|
|
#include <map>
|
|
#include <set>
|
|
#include <stdexcept>
|
|
#include <string>
|
|
using namespace VCC::Mac;
|
|
using namespace VCC::Devices;
|
|
namespace fs = std::filesystem;
|
|
namespace {
|
|
std::vector<std::uint8_t> 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<char>(f), std::istreambuf_iterator<char>()};
|
|
}
|
|
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"<<Frame::width<<' '<<Frame::height<<"\n255\n";
|
|
for(auto pixel:frame->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<SDCDevice> device;
|
|
std::set<std::string> 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="<<frames<<" name="<<drive.name<<std::endl;
|
|
}
|
|
}
|
|
void type(const std::string& text) {
|
|
for(char c:text) { machine.setKeyboard(key(c));advance(4);machine.setKeyboard(0);advance(4); }
|
|
}
|
|
void press(std::uint64_t mask,unsigned count) { machine.setKeyboard(mask);advance(count);machine.setKeyboard(0);advance(4); }
|
|
};
|
|
void runStarter(const std::vector<std::uint8_t>& system,const std::vector<std::uint8_t>& rom,
|
|
const fs::path& root,const fs::path& output,Processor cpu) {
|
|
const std::string name=cpu==Processor::MC6809?"6809":"6309";
|
|
Probe p;
|
|
p.device=std::make_shared<SDCDevice>(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 "<<name<<": 512 KB, startup mount, DIR, LOAD, RUN and absolute-path remount; no autorun or writes.\n"<<p.machine.copyScreenText()<<std::endl;
|
|
}
|
|
void run(const std::vector<std::uint8_t>& system,const std::vector<std::uint8_t>& rom,
|
|
const fs::path& root,const fs::path& output,Processor cpu) {
|
|
const std::string name=cpu==Processor::MC6809?"6809":"6309";
|
|
std::cout<<"BEGIN "<<name<<" RAM=2MB read-only card="<<root<<std::endl;
|
|
Probe p;
|
|
p.device=std::make_shared<SDCDevice>(rom,root.string(),true);
|
|
p.machine.cartridges().configure({nullptr,nullptr,nullptr,nullptr,p.device},true,4);
|
|
MachineConfiguration settings;settings.processor=cpu;settings.ram=2;settings.frameInterval=6;
|
|
p.machine.configure(settings);p.machine.setVideoOutput(VideoOutput::RGB);
|
|
p.machine.load(system,cpu);p.machine.resume();p.advance(300);
|
|
std::cout<<"BOOT\n"<<screen()<<std::endl;
|
|
picture(p.machine,output/(name+"-boot.ppm"));
|
|
if(screen().find("OK")==std::string::npos)throw std::runtime_error(name+": BASIC did not boot");
|
|
p.type("DRIVE 0,\"DDRAGON1.DSK\"\r");p.advance(60);
|
|
if(!p.device->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="<<p.frames<<"\n"<<screen()<<std::endl;
|
|
}
|
|
if(!graphics)throw std::runtime_error(name+": game never reached a graphics screen");
|
|
p.advance(300);
|
|
std::cout<<"AFTER LOAD\n"<<screen()<<std::endl;
|
|
picture(p.machine,output/(name+"-title.ppm"));
|
|
// Z starts the game. Movement and actions are ordinary emulated keys.
|
|
p.press(key('Z'),12);p.advance(600);
|
|
picture(p.machine,output/(name+"-gameplay.ppm"));
|
|
for(unsigned n=0;n<12;++n) {
|
|
p.press((std::uint64_t{1}<<30)|key('Z'),90); // CoCo right arrow and punch.
|
|
p.press(key('X'),30);p.press(key('C'),30);
|
|
}
|
|
picture(p.machine,output/(name+"-after-input.ppm"));
|
|
const auto final=p.device->snapshot();
|
|
std::uint64_t reads=0,writes=0;
|
|
for(const auto& drive:final.drives){reads+=drive.reads;writes+=drive.writes;}
|
|
std::cout<<"RESULT "<<name<<" frames="<<p.frames<<" mounted-images="<<p.seen.size()
|
|
<<" reads="<<reads<<" writes="<<writes<<" nonzero-audio-samples="<<p.audible
|
|
<<" PC="<<std::hex<<p.machine.snapshot().cpu.PC<<std::dec<<" error="<<final.error<<std::endl;
|
|
for(unsigned disk=1;disk<=9;++disk) {
|
|
const auto expected = "DDRAGON"+std::to_string(disk)+".DSK";
|
|
if(std::none_of(p.seen.begin(),p.seen.end(),[&](const auto& path){return fs::path(path).filename()==expected;}))
|
|
throw std::runtime_error(name+": game did not mount "+expected);
|
|
}
|
|
if(writes)throw std::runtime_error(name+": read-only validation unexpectedly wrote media");
|
|
if(p.audible==0)throw std::runtime_error(name+": game produced no audio");
|
|
if(!final.error.empty())throw std::runtime_error(name+": SDC error: "+final.error);
|
|
std::cout<<"PASS "<<name<<": boot, guest disk switching, audio and scripted input. Inspect captures for visual/gameplay correctness; this is not a full playthrough."<<std::endl;
|
|
}
|
|
}
|
|
int main(int argc,char** argv) {
|
|
try {
|
|
if(argc<5 || argc>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<fs::path,std::vector<std::uint8_t>> 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"<<std::endl;
|
|
return 0;
|
|
}catch(const std::exception& error){std::cerr<<"FAIL: "<<error.what()<<std::endl;return 1;}
|
|
}
|