Set mode and improve on allocation callback

This commit is contained in:
Jaby
2023-02-18 10:56:46 +01:00
committed by Jaby
parent 672ad04c83
commit 6febdae88e
4 changed files with 50 additions and 30 deletions

View File

@@ -1,6 +1,7 @@
#ifndef __JABYENGINE_CD_INTERNAL_HPP__
#define __JABYENGINE_CD_INTERNAL_HPP__
#include <PSX/System/IOPorts/cd_io.hpp>
#include <PSX/AutoLBA/auto_lba.hpp>
namespace JabyEngine {
namespace CD {
@@ -14,53 +15,46 @@ namespace JabyEngine {
Error,
};
extern VolatilePOD<CD_IO::Interrupt::Type> last_interrupt;
extern VolatilePOD<State> state;
struct DataSector {
static constexpr size_t SizeBytes = 2048;
static constexpr size_t SizeWords = (SizeBytes/sizeof(uint32_t));
struct Sector {
uint32_t data[512];
uint32_t data[SizeWords];
template<typename T>
static constexpr T words_to_sectors(T size) {
return (size + static_cast<T>(DataSector::SizeWords - 1))/static_cast<T>(DataSector::SizeWords);
}
};
struct FileInfo {
uint16_t lba;
uint16_t sectors;
constexpr FileInfo(uint16_t lba, uint16_t sectors) : lba(lba), sectors(sectors) {
static constexpr FileInfo from_auto_lba(const AutoLBAEntry& entry) {
return FileInfo{entry.lba, DataSector::words_to_sectors(entry.size_words)};
}
};
class SectorBufferAllocator {
private:
struct Dummy {
};
typedef Sector* (*SimpleAllocator)();
typedef Sector* (Dummy::*ComplexAllocator)();
typedef DataSector* (*AllocatorFunction)(void* ctx);
private:
Dummy* this_ctx = nullptr;
union {
SimpleAllocator allocate;
ComplexAllocator this_allocate;
};
void* ctx = nullptr;
AllocatorFunction allocate = nullptr;
SectorBufferAllocator(SimpleAllocator allocate) : this_ctx(nullptr), allocate(allocate) {
}
SectorBufferAllocator(Dummy* ctx, ComplexAllocator this_allocate) : this_ctx(ctx), this_allocate(this_allocate) {
constexpr SectorBufferAllocator(void* ctx, AllocatorFunction function) : ctx(ctx), allocate(function) {
}
public:
static SectorBufferAllocator create(SimpleAllocator allocate) {
return SectorBufferAllocator(allocate);
}
template<typename T>
static SectorBufferAllocator create(T &obj, Sector* (T::*obj_allocate)()) {
return SectorBufferAllocator(reinterpret_cast<Dummy*>(&obj), reinterpret_cast<ComplexAllocator>(obj_allocate));
static constexpr SectorBufferAllocator create(void* obj, AllocatorFunction function) {
return SectorBufferAllocator(obj, function);
}
};
extern VolatilePOD<CD_IO::Interrupt::Type> last_interrupt;
struct Command {
static void wait_until(CD_IO::Interrupt::Type irq) {
while(last_interrupt.read() != irq);
@@ -81,7 +75,7 @@ namespace JabyEngine {
}
};
State start_reading(FileInfo file_info, const SectorBufferAllocator& buffer_allocator);
State read_file(FileInfo file_info, const SectorBufferAllocator& buffer_allocator);
}
}
}