irx, nullptr, threading patch

This commit is contained in:
h4570
2022-08-20 16:38:44 +02:00
parent 57767eee72
commit d251df8704
35 changed files with 306 additions and 115 deletions
+57 -16
View File
@@ -22,17 +22,22 @@
#include <stdio.h>
#include <string>
#include <sstream>
#include <fstream>
#include <utility>
#include <memory>
#define TYRA_LOG(...) Debug::writeLines("LOG: ", ##__VA_ARGS__, "\n")
#define TYRA_WARN(...) Debug::writeLines("==WARN: ", ##__VA_ARGS__, "\n")
#define TYRA_ERROR(...) Debug::writeLines("====ERR: ", ##__VA_ARGS__, "\n")
#define TYRA_TRAP(...) Debug::trap(__FILE__, __LINE__, ##__VA_ARGS__)
#define TYRA_BREAKPOINT() Debug::trap(__FILE__, __LINE__, "Breakpoint")
#include "file/file_utils.hpp"
#include "info/info.hpp"
#define TYRA_LOG(...) TyraDebug::writeLines("LOG: ", ##__VA_ARGS__, "\n")
#define TYRA_WARN(...) TyraDebug::writeLines("==WARN: ", ##__VA_ARGS__, "\n")
#define TYRA_ERROR(...) TyraDebug::writeLines("====ERR: ", ##__VA_ARGS__, "\n")
#define TYRA_TRAP(...) TyraDebug::trap(__FILE__, __LINE__, ##__VA_ARGS__)
#define TYRA_BREAKPOINT() TyraDebug::trap(__FILE__, __LINE__, "Breakpoint")
#define TYRA_ASSERT(condition, ...) \
if (!(condition)) Debug::trap(__FILE__, __LINE__, ##__VA_ARGS__)
if (!(condition)) TyraDebug::trap(__FILE__, __LINE__, ##__VA_ARGS__)
class Debug {
class TyraDebug {
public:
template <typename Arg, typename... Args>
static void writeLines(Arg&& arg, Args&&... args) {
@@ -42,24 +47,54 @@ class Debug {
using expander = int[];
(void)expander{0, (void(ss << std::forward<Args>(args)), 0)...};
printf("%s", ss.str().c_str());
if (Tyra::Info::writeLogsToFile) {
auto* logFile = getLogFile();
*logFile << ss.str();
logFile->flush();
} else {
printf("%s", ss.str().c_str());
}
}
template <typename... Args>
static void trap(const char* file, int line, Args... args) {
printf("\n");
printf("============== TYRA ==============\n");
printf("| Assertion failed!\n");
printf("|\n");
std::stringstream ss1;
ss1 << "\n";
ss1 << "============== TYRA ==============\n";
ss1 << "| Assertion failed!\n";
ss1 << "|\n";
if (Tyra::Info::writeLogsToFile) {
auto* logFile = getLogFile();
*logFile << ss1.str();
logFile->flush();
} else {
printf("%s", ss1.str().c_str());
}
writeAssertLines(args...);
printf("|\n");
printf("| File : %s:%d\n", file, line);
printf("====================================\n\n");
std::stringstream ss2;
ss2 << "|\n";
ss2 << "| File : " << file << ":" << line << "\n";
ss2 << "====================================\n\n";
if (Tyra::Info::writeLogsToFile) {
auto* logFile = getLogFile();
*logFile << ss2.str();
logFile->flush();
} else {
printf("%s", ss2.str().c_str());
}
for (;;) {
}
}
private:
static std::unique_ptr<std::ofstream> logFile;
static std::ofstream* getLogFile();
template <typename Arg, typename... Args>
static void writeAssertLines(Arg&& arg, Args&&... args) {
std::stringstream ss;
@@ -69,7 +104,13 @@ class Debug {
(void)expander{
0, (void(ss << "| " << std::forward<Args>(args) << "\n"), 0)...};
printf("%s", ss.str().c_str());
if (Tyra::Info::writeLogsToFile) {
auto* logfile = getLogFile();
*logfile << ss.str();
logFile->flush();
} else {
printf("%s", ss.str().c_str());
}
}
};
+14 -1
View File
@@ -20,24 +20,37 @@
namespace Tyra {
struct EngineOptions {
/**
* True -> logs will be written to file.
* False -> logs will be displayed in console
*/
bool writeLogsToFile = false;
bool loadUsbDriver = false;
};
class Engine {
public:
Engine();
Engine(const EngineOptions& options);
~Engine();
Renderer renderer;
Pad pad;
Audio audio;
IrxLoader irx;
Info info;
void run(Game* t_game);
private:
IrxLoader irx;
Game* game;
Banner banner;
void realLoop();
void initAll(const bool& loadUsbDriver);
};
} // namespace Tyra
+3 -3
View File
@@ -28,9 +28,9 @@ class FileUtils {
private:
// Argv name+path & just path
char cwd[NAME_MAX];
char elfName[NAME_MAX];
char elfPath[NAME_MAX - 14];
char cwd[255];
char elfName[255];
char elfPath[255 - 14];
void setPathInfo(const char* path);
};
+2
View File
@@ -24,6 +24,8 @@ class Info {
Version version;
static bool writeLogsToFile;
/** Called by engine */
void update();
+9 -7
View File
@@ -19,16 +19,18 @@ class IrxLoader {
IrxLoader();
~IrxLoader();
/** Load's audio and pad driver */
void loadDefaultDrivers();
void loadUSBDriver();
void loadAll(const bool& withUsb, const bool& isLoggingToFile);
private:
static bool isLoaded;
void loadSio2man(const bool& verbose);
void loadPadman(const bool& verbose);
void loadLibsd(const bool& verbose);
void loadUsbModules(const bool& verbose);
void loadAudsrv(const bool& verbose);
int applyRpcPatches();
int loadAudio();
int loadPad();
int loadUsb();
void waitUntilUsbDeviceIsReady();
void delay(int count);
};
+2
View File
@@ -49,6 +49,8 @@ class Color {
explicit Color(const float* v) { copy(this, v); }
void operator=(const Color& v);
void operator+=(const float& v);
void operator-=(const float& v);
void operator*=(const float& v);
void operator/=(const float& v);
+2 -2
View File
@@ -48,7 +48,7 @@ void AudioSong::init() {
void AudioSong::load(const char* t_path) {
if (songLoaded) unloadSong();
wav = fopen(t_path, "rb");
TYRA_ASSERT(wav != NULL, "Failed to open wav file!");
TYRA_ASSERT(wav != nullptr, "Failed to open wav file!");
rewindSongToStart();
songLoaded = true;
}
@@ -109,7 +109,7 @@ void AudioSong::unloadSong() {
/** Fseek on wav. */
void AudioSong::rewindSongToStart() {
if (wav != NULL) fseek(wav, 0x30, SEEK_SET);
if (wav != nullptr) fseek(wav, 0x30, SEEK_SET);
chunkReadStatus = 0;
songFinished = false;
}
+25
View File
@@ -0,0 +1,25 @@
/*
# _____ ____ ___
# | \/ ____| |___|
# | | | \ | |
#-----------------------------------------------------------------------
# Copyright 2022, tyra - https://github.com/h4570/tyra
# Licensed under Apache License 2.0
# Wellington Carvalho <wellcoj@gmail.com>
*/
#include "debug/debug.hpp"
std::unique_ptr<std::ofstream> TyraDebug::logFile;
std::ofstream* TyraDebug::getLogFile() {
if (logFile) {
return logFile.get();
} else {
logFile = std::make_unique<std::ofstream>();
logFile->open(Tyra::FileUtils::fromCwd("log.txt"),
std::ofstream::out | std::ofstream::app);
return logFile.get();
}
} // namespace Tyra
+14 -7
View File
@@ -12,13 +12,11 @@
namespace Tyra {
Engine::Engine() {
srand(time(nullptr));
renderer.init();
banner.show(&renderer);
irx.loadDefaultDrivers();
audio.init();
pad.init();
Engine::Engine() { initAll(false); }
Engine::Engine(const EngineOptions& options) {
info.writeLogsToFile = options.writeLogsToFile;
initAll(options.loadUsbDriver);
}
Engine::~Engine() {}
@@ -37,4 +35,13 @@ void Engine::realLoop() {
info.update();
}
void Engine::initAll(const bool& loadUsbDriver) {
srand(time(nullptr));
irx.loadAll(loadUsbDriver, info.writeLogsToFile);
renderer.init();
banner.show(&renderer);
audio.init();
pad.init();
}
} // namespace Tyra
+3 -3
View File
@@ -34,11 +34,11 @@ void FileUtils::setPathInfo(const char* path) {
strcpy(this->elfPath, path);
ptr = strrchr(this->elfPath, '/');
if (ptr == NULL) {
if (ptr == nullptr) {
ptr = strrchr(this->elfPath, '\\');
if (ptr == NULL) {
if (ptr == nullptr) {
ptr = strrchr(this->elfPath, ':');
if (ptr == NULL) {
if (ptr == nullptr) {
TYRA_TRAP("Did not find path! PATH: ", path);
}
}
+12 -10
View File
@@ -14,6 +14,8 @@
namespace Tyra {
bool Info::writeLogsToFile = false;
Info::Info() {
fps = 0;
fpsDelayer = 0;
@@ -48,21 +50,21 @@ void* Info::allocateLargestFreeRAMBlock(size_t* Size) {
s0 = ~(size_t)0 ^ (~(size_t)0 >> 1);
while (s0 && (p = malloc(s0)) == NULL) s0 >>= 1;
while (s0 && (p = malloc(s0)) == nullptr) s0 >>= 1;
if (p) free(p);
s1 = s0 >> 1;
while (s1) {
if ((p = malloc(s0 + s1)) != NULL) {
if ((p = malloc(s0 + s1)) != nullptr) {
s0 += s1;
free(p);
}
s1 >>= 1;
}
while (s0 && (p = malloc(s0)) == NULL) s0 ^= s0 & -s0;
while (s0 && (p = malloc(s0)) == nullptr) s0 ^= s0 & -s0;
*Size = s0;
return p;
@@ -70,30 +72,30 @@ void* Info::allocateLargestFreeRAMBlock(size_t* Size) {
size_t Info::getFreeRAMSize() {
size_t total = 0;
void* pFirst = NULL;
void* pLast = NULL;
void* pFirst = nullptr;
void* pLast = nullptr;
for (;;) {
size_t largest;
void* p = allocateLargestFreeRAMBlock(&largest);
if (largest < sizeof(void*)) {
if (p != NULL) free(p);
if (p != nullptr) free(p);
break;
}
*(void**)p = NULL;
*(void**)p = nullptr;
total += largest;
if (pFirst == NULL) pFirst = p;
if (pFirst == nullptr) pFirst = p;
if (pLast != NULL) *(void**)pLast = p;
if (pLast != nullptr) *(void**)pLast = p;
pLast = p;
}
while (pFirst != NULL) {
while (pFirst != nullptr) {
void* p = *(void**)pFirst;
free(pFirst);
pFirst = p;
+2
View File
@@ -0,0 +1,2 @@
$PS2SDK/iop/irx/audsrv.irx
audsrv_irx
+86 -46
View File
@@ -16,8 +16,23 @@
#include <kernel.h>
#include <sifrpc.h>
#include <sbv_patches.h>
#include <iopcontrol.h>
#include "file/file_utils.hpp"
// external IRX modules
extern u8 sio2man_irx[];
extern int size_sio2man_irx;
extern u8 padman_irx[];
extern int size_padman_irx;
extern u8 audsrv_irx[];
extern int size_audsrv_irx;
extern u8 libsd_irx[];
extern int size_libsd_irx;
extern u8 bdm_irx[];
extern int size_bdm_irx;
@@ -32,19 +47,41 @@ extern int size_usbmass_bd_irx;
namespace Tyra {
bool IrxLoader::isLoaded = false;
IrxLoader::IrxLoader() {
SifInitRpc(0);
while (!SifIopReset("", 0)) {
};
while (!SifIopSync()) {
};
SifInitRpc(0);
this->applyRpcPatches();
}
IrxLoader::~IrxLoader() {}
void IrxLoader::loadDefaultDrivers() {
this->loadAudio();
this->loadPad();
}
void IrxLoader::loadAll(const bool& withUsb, const bool& isLoggingToFile) {
if (isLoaded) {
TYRA_LOG("IRX modules already loaded!");
return;
}
void IrxLoader::loadUSBDriver() { this->loadUsb(); }
loadSio2man(!isLoggingToFile);
loadPadman(!isLoggingToFile);
loadLibsd(!isLoggingToFile);
if (withUsb) {
loadUsbModules(!isLoggingToFile);
}
loadAudsrv(true);
isLoaded = true;
}
/**
* @brief Apply the SBV LMB patch to allow modules to be loaded from a buffer in
@@ -53,7 +90,6 @@ void IrxLoader::loadUSBDriver() { this->loadUsb(); }
*/
int IrxLoader::applyRpcPatches() {
int ret;
TYRA_LOG("Applying SBV Patches");
ret = sbv_patch_enable_lmb();
TYRA_ASSERT(ret >= 0,
@@ -67,65 +103,69 @@ int IrxLoader::applyRpcPatches() {
ret = sbv_patch_fileio();
TYRA_ASSERT(ret >= 0, "Failed to load Applying SBV Patches sbv_patch_fileio");
TYRA_LOG("SBV Patches applyed ");
return ret;
}
int IrxLoader::loadUsb() {
void IrxLoader::loadLibsd(const bool& verbose) {
if (verbose) TYRA_LOG("IRX: Loading libsd...");
int ret;
TYRA_LOG("Loading USB modules");
SifExecModuleBuffer(&libsd_irx, size_libsd_irx, 0, nullptr, &ret);
TYRA_ASSERT(ret >= 0, "Failed to load module: libsd_irx");
// Load Block Device Manager (BDM)
SifExecModuleBuffer(&bdm_irx, size_bdm_irx, 0, NULL, &ret);
TYRA_ASSERT(ret >= 0, "Failed to load module: usbhdfsd");
// Load FATFS (mass:) driver
SifExecModuleBuffer(&bdmfs_fatfs_irx, size_bdmfs_fatfs_irx, 0, NULL, &ret);
TYRA_ASSERT(ret >= 0, "Failed to load module: usbd");
// Load USB Block Device drivers
SifExecModuleBuffer(&usbd_irx, size_usbd_irx, 0, NULL, &ret);
TYRA_ASSERT(ret >= 0, "Failed to load module: usbd");
SifExecModuleBuffer(&usbmass_bd_irx, size_usbmass_bd_irx, 0, NULL, &ret);
TYRA_ASSERT(ret >= 0, "Failed to load module: usbhdfsd");
this->waitUntilUsbDeviceIsReady();
TYRA_LOG("USB/MASS modules loaded!");
return ret;
if (verbose) TYRA_LOG("IRX: Libsd loaded!");
}
int IrxLoader::loadAudio() {
void IrxLoader::loadUsbModules(const bool& verbose) {
if (verbose) TYRA_LOG("IRX: Loading usb modules...");
int ret;
TYRA_LOG("Modules loading started (LIBSD, AUDSRV)");
ret = SifLoadModule("rom0:LIBSD", 0, NULL);
TYRA_ASSERT(ret != -203, "LIBSD loading failed!");
SifExecModuleBuffer(&usbd_irx, size_usbd_irx, 0, nullptr, &ret);
TYRA_ASSERT(ret >= 0, "Failed to load module: usbd_irx");
ret = SifLoadModule("host:audsrv.irx", 0, NULL);
TYRA_ASSERT(ret != -203, "audsrv.irx loading failed!");
TYRA_LOG("Audio modules loaded");
SifExecModuleBuffer(&bdm_irx, size_bdm_irx, 0, nullptr, &ret);
TYRA_ASSERT(ret >= 0, "Failed to load module: bdm_irx");
return ret;
SifExecModuleBuffer(&bdmfs_fatfs_irx, size_bdmfs_fatfs_irx, 0, nullptr, &ret);
TYRA_ASSERT(ret >= 0, "Failed to load module: bdmfs_fatfs");
SifExecModuleBuffer(&usbmass_bd_irx, size_usbmass_bd_irx, 0, nullptr, &ret);
TYRA_ASSERT(ret >= 0, "Failed to load module: usbmass");
waitUntilUsbDeviceIsReady();
if (verbose) TYRA_LOG("IRX: Usb modules loaded!");
}
int IrxLoader::loadPad() {
void IrxLoader::loadAudsrv(const bool& verbose) {
if (verbose) TYRA_LOG("IRX: Loading audsrv...");
int ret;
SifExecModuleBuffer(&audsrv_irx, size_audsrv_irx, 0, nullptr, &ret);
TYRA_ASSERT(ret >= 0, "Failed to load module: audsrv_irx");
TYRA_LOG("PAD Modules loading started (SIO2MAN, PADMAN)");
if (verbose) TYRA_LOG("IRX: Audsrv loaded!");
}
ret = SifLoadModule("rom0:SIO2MAN", 0, NULL);
TYRA_ASSERT(ret >= 0,
"SifLoadModule (SIO2MAN) failed! Returned value: ", ret);
void IrxLoader::loadSio2man(const bool& verbose) {
if (verbose) TYRA_LOG("IRX: Loading sio2man...");
ret = SifLoadModule("rom0:PADMAN", 0, NULL);
TYRA_ASSERT(ret >= 0, "SifLoadModule (PADMAN) failed! Returned value: ", ret);
int ret;
SifExecModuleBuffer(&sio2man_irx, size_sio2man_irx, 0, nullptr, &ret);
TYRA_ASSERT(ret >= 0, "Failed to load module: sio2man_irx");
TYRA_LOG("Pad modules loaded!");
if (verbose) TYRA_LOG("IRX: Sio2man loaded!");
}
return ret;
void IrxLoader::loadPadman(const bool& verbose) {
if (verbose) TYRA_LOG("IRX: Loading padman...");
int ret;
SifExecModuleBuffer(&padman_irx, size_padman_irx, 0, nullptr, &ret);
TYRA_ASSERT(ret >= 0, "Failed to load module: padman_irx");
if (verbose) TYRA_LOG("IRX: Padman loaded!");
}
void IrxLoader::delay(int count) {
+2
View File
@@ -0,0 +1,2 @@
$PS2SDK/iop/irx/libsd.irx
libsd_irx
+2
View File
@@ -0,0 +1,2 @@
$PS2SDK/iop/irx/padman.irx
padman_irx
+2
View File
@@ -0,0 +1,2 @@
$PS2SDK/iop/irx/sio2man.irx
sio2man_irx
@@ -95,7 +95,7 @@ MeshBuilderData* MD2Loader::load(const char* fullpath,
auto filename = getFilenameFromPath(path);
FILE* file = fopen(fullpath, "rb");
TYRA_ASSERT(file != NULL, "Failed to load: ", filename);
TYRA_ASSERT(file != nullptr, "Failed to load: ", filename);
md2_t header;
fread(reinterpret_cast<char*>(&header), sizeof(md2_t), 1, file);
@@ -55,7 +55,7 @@ void DynPipRenderer::init(RendererCore* t_core,
rendererCore = t_core;
programsRepo = t_programRepo;
dma_channel_initialize(DMA_CHANNEL_VIF1, NULL, 0);
dma_channel_initialize(DMA_CHANNEL_VIF1, nullptr, 0);
setProgramsCache();
@@ -17,7 +17,7 @@ BlockizerProgramsManager::BlockizerProgramsManager() {
context = 0;
vu1BlockData = BlockNotUploaded;
dma_channel_initialize(DMA_CHANNEL_VIF1, NULL, 0);
dma_channel_initialize(DMA_CHANNEL_VIF1, nullptr, 0);
setProgramsCache();
}
@@ -15,7 +15,7 @@
// #define TYRA_RENDERER_VERBOSE_LOG 1
#ifdef TYRA_RENDERER_VERBOSE_LOG
#define Verbose(...) Debug::writeLines("VRB: ", ##__VA_ARGS__, "\n")
#define Verbose(...) TyraDebug::writeLines("VRB: ", ##__VA_ARGS__, "\n")
#else
#define Verbose(...) ((void)0)
#endif
@@ -14,7 +14,7 @@
// #define TYRA_QBUFF_RENDERER_VERBOSE_LOG 1
#ifdef TYRA_QBUFF_RENDERER_VERBOSE_LOG
#define Verbose(...) Debug::writeLines("VRB: ", ##__VA_ARGS__, "\n")
#define Verbose(...) TyraDebug::writeLines("VRB: ", ##__VA_ARGS__, "\n")
#else
#define Verbose(...) ((void)0)
#endif
@@ -98,7 +98,7 @@ void StaPipQBufferRenderer::init(RendererCore* t_core, prim_t* t_prim,
prim = t_prim;
lod = t_lod;
dma_channel_initialize(DMA_CHANNEL_VIF1, NULL, 0);
dma_channel_initialize(DMA_CHANNEL_VIF1, nullptr, 0);
setProgramsCache();
@@ -47,7 +47,7 @@ void RendererCoreGS::init(RendererSettings* t_settings) {
}
void RendererCoreGS::initChannels() {
dma_channel_initialize(DMA_CHANNEL_GIF, NULL, 0);
dma_channel_initialize(DMA_CHANNEL_GIF, nullptr, 0);
}
void RendererCoreGS::allocateBuffers() {
@@ -31,7 +31,7 @@ Path3::~Path3() {
void Path3::init(RendererSettings* t_settings) {
settings = t_settings;
dma_channel_initialize(DMA_CHANNEL_GIF, NULL, 0);
dma_channel_initialize(DMA_CHANNEL_GIF, nullptr, 0);
TYRA_LOG("Path3 initialized");
}
+15
View File
@@ -19,12 +19,27 @@ void Color::copy(Color* out, const float* in) {
}
void Color::operator=(const Color& v) { copy(this, v); }
void Color::operator+=(const float& v) {
r += v;
g += v;
b += v;
a += v;
}
void Color::operator-=(const float& v) {
r -= v;
g -= v;
b -= v;
a -= v;
}
void Color::operator*=(const float& v) {
r *= v;
g *= v;
b *= v;
a *= v;
}
void Color::operator/=(const float& v) {
r /= v;
g /= v;