// SPDX-License-Identifier: GPL-3.0-or-later #include "CoreChecks.h" #include "../../src/core/BasicProgram.h" #include "../../src/core/MachineSession.h" #include #include #include using namespace VCC::Mac; namespace { void require(bool yes, const char* why) { if (!yes) throw std::runtime_error(why); } void rejects(const std::function& call) { try { call(); } catch (const std::exception&) { return; } throw std::runtime_error("Malformed BASIC input was accepted"); } std::vector dictionary() { return {{"GO",0x81},{"REM",0x82},{"'",0x83},{"ELSE",0x84},{"IF",0x85},{"DATA",0x86},{"PRINT",0x87}, {"END",0x8a},{"TO",0xa5},{"THEN",0xa7},{"+",0xab},{"*",0xad},{"=",0xb3}, {"HSCREEN",0xe4},{"LEFT$",0x9c,true}}; } } void CheckBasicProgramTokens() { const auto dict = dictionary(); const std::string source = "50 HSCREEN 0\n30 ? LEFT$(\"PRINT:REM\",2)\n10 REM PRINT remains text\n20 DATA PRINT,\"X:Y\":PRINT6*7\n40 IF A=1 THEN ?1 ELSE ?2 'PRINT\n60 XPRINT=1\n"; const auto encoded = BasicProgram::encode(source, 0x2601, 0x7000, dict); const auto decoded = BasicProgram::decode(encoded, 0x2601, dict); require(decoded.find("10 REM PRINT remains text\n20 DATA PRINT,\"X:Y\":PRINT6*7") == 0, "BASIC sorting, REM or DATA changed"); require(decoded.find("30 PRINT LEFT$(\"PRINT:REM\",2)") != std::string::npos, "Function/PRINT shortcut changed"); require(decoded.find("40 IF A=1 THEN PRINT1 ELSE PRINT2 'PRINT") != std::string::npos, "ELSE/apostrophe did not roundtrip"); require(decoded.find("60 XPRINT=1") != std::string::npos, "Embedded keyword in identifier changed"); require(BasicProgram::encode(decoded,0x2601,0x7000,dict) == encoded, "BASIC decode/encode is not byte stable"); std::vector locations; require(BasicProgram::encode(source,0x2601,0x7000,dict,&locations) == encoded, "Debug mapping changed program bytes"); require(!locations.empty(), "Missing BASIC source map"); for (const auto& location : locations) { require(location.address >= 0x2605 && location.address < 0x2601 + encoded.size(), "Source map address outside program"); require(location.length > 0 && location.offset + location.length <= source.size(), "Source map span outside editor"); const auto token = encoded[location.address - 0x2601]; if (token == 0x87) require(source.substr(location.offset, location.length) == "PRINT" || source.substr(location.offset, location.length) == "?", "PRINT map misses original spelling"); } require(BasicProgram::decode(BasicProgram::encode("\n\r\n",0x2601,0x7000,dict),0x2601,dict).empty(), "Empty BASIC program failed"); require(BasicProgram::decode(BasicProgram::encode("10 print \"Mixed Case\"\r\n",0x2601,0x7000,dict),0x2601,dict) == "10 PRINT \"Mixed Case\"\n", "Case normalization changed a string"); } void CheckBasicProgramValidation() { const auto dict = dictionary(); for (const auto& bad : {"PRINT 1", "64000 PRINT1", "10 PRINT1\n10 PRINT2", "10 PRINT\x01", "10 PRINT \xc3\xa9"}) rejects([&] { BasicProgram::encode(bad,0x2601,0x7000,dict); }); rejects([&] { BasicProgram::encode("10 REM " + std::string(250,'X'),0x2601,0x7000,dict); }); rejects([&] { BasicProgram::encode("10 PRINT1",0x2601,0x2608,dict); }); auto encoded = BasicProgram::encode("10 PRINT1\n20 END",0x2601,0x7000,dict); auto bad = encoded; bad[0]=0;bad[1]=1; rejects([&] { BasicProgram::decode(bad,0x2601,dict); }); bad=encoded;bad.back()=1; rejects([&] { BasicProgram::decode(bad,0x2601,dict); }); bad=encoded;bad[4]=0xfe; rejects([&] { BasicProgram::decode(bad,0x2601,dict); }); const auto disk = BasicProgram::exportImage(encoded,"hello",BasicExportFormat::Disk); require(disk.size()==161280, "BASIC export disk geometry differs"); constexpr unsigned fat=(17*18+1)*256, entry=(17*18+2)*256; require(std::string(disk.begin()+entry,disk.begin()+entry+11)=="HELLO BAS" && disk[entry+11]==0 && disk[entry+12]==0, "BASIC export directory metadata differs"); require(disk[0]==0xff && ((unsigned(disk[1])<<8)|disk[2])==encoded.size() && std::equal(encoded.begin(),encoded.end(),disk.begin()+3), "BASIC disk file differs from SAVE binary layout"); require(disk[fat]==0xc1 && disk[fat+1]==0xff && disk[entry+15]==encoded.size()+3, "BASIC disk allocation differs"); // Exercise exact-sector size and a file spanning multiple granules. for (std::size_t size : {std::size_t(253),std::size_t(4608)}) { std::vector payload(size,0); auto image=BasicProgram::exportImage(payload,"BOUNDARY",BasicExportFormat::Disk); const auto length=size+3, count=(length+2303)/2304; require(image[entry+14]*256+image[entry+15]==(length-1)%256+1, "Last-sector length is wrong"); for(std::size_t g=0;g recovered; std::size_t at=0; unsigned blocks=0; bool eof=false; while(at(0x5900,0),"HELLO",BasicExportFormat::Disk);}); rejects([&]{BasicProgram::exportImage({0,1},"HELLO",BasicExportFormat::Cassette);}); rejects([&]{BasicProgram::exportImage(encoded,"HELLO",static_cast(99));}); // Decoder guardrails use synthetic RAM, independent of proprietary ROMs. std::vector memory(0x8000,0); auto word = [&](unsigned at, unsigned v) { memory[at] = v >> 8; memory[at+1] = v & 255; }; word(0x19,0x2601); word(0x1b,0x2610); word(0x1d,0x2617); word(0x1f,0x2617); word(0x21,0x7000); memory[0x2610]='A'; memory[0x2612]=0x81; memory[0x2613]=0xc0; // -1.5 require(BasicProgram::inspect(memory).at(0).value=="-1.5", "BASIC numeric format differs"); memory[0x2611]=0x80; memory[0x2612]=3; word(0x2614,0x4000); memory[0x4000]='a'; memory[0x4001]=0; memory[0x4002]=255; require(BasicProgram::inspect(memory).at(0).value=="\"a\\x00\\xFF\"", "String escaping or flag differs"); word(0x2614,0xff00); require(BasicProgram::inspect(memory).at(0).value.find("Unavailable") == 0,"String device address accepted"); memory[0x2612]=0; require(BasicProgram::inspect(memory).at(0).value=="\"\"", "Empty string dereferenced an unused pointer"); auto saved=memory; for (unsigned ptr : {0x1bu,0x1du,0x1fu,0x21u}) { word(ptr,0xffff); rejects([&]{ BasicProgram::inspect(memory); }); memory=saved; } word(0x1d,0x2618); rejects([&]{ BasicProgram::inspect(memory); }); memory=saved; // Array descriptor: two dimensions (source order 2,3), six elements. word(0x1f,0x2617+39); memory[0x2617]='M'; word(0x2619,39); memory[0x261b]=2; word(0x261c,3); word(0x261e,2); auto array=BasicProgram::inspect(memory).at(1); require(array.dimensions==std::vector({2,3}) && array.elements.size()==6,"Array dimensions are not in source order"); saved=memory; for(unsigned badsize : {0u,8u,38u,40u,65535u}) { word(0x2619,badsize); rejects([&]{ BasicProgram::inspect(memory); }); memory=saved; } for(unsigned rank : {0u,3u,255u}) { memory[0x261b]=rank; rejects([&]{ BasicProgram::inspect(memory); }); memory=saved; } for(unsigned length : {0u,65535u}) { word(0x261c,length); rejects([&]{ BasicProgram::inspect(memory); }); memory=saved; } rejects([&]{ BasicProgram::inspect(std::vector(2)); }); MachineSession machine; rejects([&] { machine.basicReadSource(); }); rejects([&] { machine.basicLoadSource("10 PRINT1",true); }); machine.loadDiagnostic(); const auto before=machine.snapshot(); rejects([&] { machine.basicLoadSource("10 PRINT1",true); }); require(machine.snapshot().cpu.PC == before.cpu.PC && machine.snapshot().cycles == before.cycles, "Rejected BASIC load changed diagnostic execution"); }