Files
BozoEngine/tests/native/library_disc_stream_tests.cpp

60 lines
3.7 KiB
C++

// SPDX-License-Identifier: MIT
// cdvdman's stream (sceCdStInit, StStart, StRead, StSeek, StStat, StStop) answered from the disc image.
#include "bozo/native/compiled_libraries.h"
#include <chrono>
#include <cstring>
#include <filesystem>
#include <fstream>
#include <cstdio>
#include <stdexcept>
using namespace bozo::native::irx;
namespace {
void check(bool v,const char *what){if(!v)throw std::runtime_error(std::string("native disc stream: ")+what);}
constexpr uint32_t entry=0xe000,initCall=0xe800,readCall=0xe808,seekCall=0xe810,startCall=0xe818,statCall=0xe820,
stopCall=0xe828,ring=0x30000,buffer=0x34000,error=0x38000,mode=0x38010;
void unused(Env &){throw std::runtime_error("unbound fixture entry");}
const Entry entries[]={{entry,unused}};
const Registration registration("native-disc-stream",1,0xe000,0x1000,entries,1);
uint32_t call(Env &e,uint32_t function,uint32_t a=0,uint32_t b=0,uint32_t c=0,uint32_t d=0){
e.cpu.gpr[4]=a;e.cpu.gpr[5]=b;e.cpu.gpr[6]=c;e.cpu.gpr[7]=d;e.cpu.gpr[31]=0xe004;
check(e.call(function,0xe004),"unexpected yield");return e.cpu.gpr[2];
}
struct Iso {
std::filesystem::path path=std::filesystem::temp_directory_path()/
("bozo-native-stream-"+std::to_string(std::chrono::steady_clock::now().time_since_epoch().count())+".iso");
std::vector<uint8_t> bytes=std::vector<uint8_t>(32*2048);
Iso(){for(size_t i=0;i<bytes.size();++i)bytes[i]=uint8_t((i*5+i/2048*11)&255);
bytes[16*2048]=1;std::memcpy(bytes.data()+16*2048+1,"CD001",5);
std::ofstream out(path,std::ios::binary);out.write(reinterpret_cast<const char *>(bytes.data()),bytes.size());check(bool(out),"write synthetic ISO");}
~Iso(){std::error_code ec;std::filesystem::remove(path,ec);}
};
bool same(Env &e,uint32_t at,const Iso &iso,uint32_t sector,uint32_t count){
for(uint32_t i=0;i<count*2048;++i)if(e.r8(at+i)!=iso.bytes[sector*2048+i])return false;
return true;
}
}
int main(){try{
Iso iso;bozo::libs::Libraries libraries;CompiledLibraries host{libraries,0x40000};
libraries.cd.imagePath=iso.path.string();
Image image{"native-disc-stream",1,0xe000,entry,0x12340,std::vector<uint8_t>(0x1000)};
image.imports={{initCall,"cdvdman",56},{readCall,"cdvdman",57},{seekCall,"cdvdman",58},{startCall,"cdvdman",59},
{statCall,"cdvdman",60},{stopCall,"cdvdman",61}};
host.addImage(std::move(image));host.useNativeDiscFiles();
host.bindNative(entry,[&](Env &e){const auto ret=e.cpu.gpr[31];
check(call(e,initCall,0x50,5,ring)==1,"init");
check(call(e,statCall)==0,"nothing buffered before the start");
e.w32(error,0x55);
check(call(e,readCall,2,buffer,0,error)==0 && e.r32(error)==0,"no read before the start");
check(call(e,startCall,20,mode)==1 && call(e,statCall)==0x50,"started: the ring is full");
check(call(e,readCall,3,buffer,0,error)==3 && e.r32(error)==0 && same(e,buffer,iso,20,3),"the first sectors");
check(call(e,readCall,2,buffer,1,error)==2 && same(e,buffer,iso,23,2),"the stream goes on");
check(call(e,seekCall,5)==1 && call(e,readCall,1,buffer,0,error)==1 && same(e,buffer,iso,5,1),"a seek moves it");
check(call(e,seekCall,30)==1 && call(e,readCall,4,buffer,0,error)==2 && same(e,buffer,iso,30,2),"cut at the disc's end");
check(call(e,readCall,1,buffer,0,error)==0 && e.r32(error)==0x30,"past the end: an error");
check(call(e,stopCall)==1 && call(e,statCall)==0 && call(e,readCall,1,buffer,0,error)==0,"stopped");
e.cpu.gpr[2]=1;e.cpu.pc=ret;
});
check(host.startImage("native-disc-stream")==1,"completed native entry");
std::puts("native disc stream: init, start, read, seek, the disc's end, stat and stop passed");return 0;
}catch(const std::exception &e){std::fprintf(stderr,"%s\n",e.what());return 1;}}