From 6febdae88efb6da37d906bb925f9c0e2c3379e81 Mon Sep 17 00:00:00 2001 From: Jaby Date: Sat, 18 Feb 2023 10:56:46 +0100 Subject: [PATCH] Set mode and improve on allocation callback --- include/PSX/System/IOPorts/cd_io.hpp | 1 + src/Library/include/CD/cd_internal.hpp | 48 ++++++++----------- src/Library/src/CD/cd.cpp | 22 +++++++-- .../src/File/Processor/cd_file_processor.cpp | 9 ++++ 4 files changed, 50 insertions(+), 30 deletions(-) diff --git a/include/PSX/System/IOPorts/cd_io.hpp b/include/PSX/System/IOPorts/cd_io.hpp index 270999e2..8b36d7a9 100644 --- a/include/PSX/System/IOPorts/cd_io.hpp +++ b/include/PSX/System/IOPorts/cd_io.hpp @@ -109,6 +109,7 @@ namespace JabyEngine { static constexpr Info GetStat{0x1, Interrupt::Type::Acknowledge}; static constexpr Info Init{0xA, Interrupt::Type::Complete}; + static constexpr Info SetMode{0xE, Interrupt::Type::Acknowledge}; }; static constexpr auto IORegister1Adr = 0x1F801801; diff --git a/src/Library/include/CD/cd_internal.hpp b/src/Library/include/CD/cd_internal.hpp index 3b47036c..b5eb316b 100644 --- a/src/Library/include/CD/cd_internal.hpp +++ b/src/Library/include/CD/cd_internal.hpp @@ -1,6 +1,7 @@ #ifndef __JABYENGINE_CD_INTERNAL_HPP__ #define __JABYENGINE_CD_INTERNAL_HPP__ #include +#include namespace JabyEngine { namespace CD { @@ -14,53 +15,46 @@ namespace JabyEngine { Error, }; - extern VolatilePOD last_interrupt; - extern VolatilePOD 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 + static constexpr T words_to_sectors(T size) { + return (size + static_cast(DataSector::SizeWords - 1))/static_cast(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 - static SectorBufferAllocator create(T &obj, Sector* (T::*obj_allocate)()) { - return SectorBufferAllocator(reinterpret_cast(&obj), reinterpret_cast(obj_allocate)); + static constexpr SectorBufferAllocator create(void* obj, AllocatorFunction function) { + return SectorBufferAllocator(obj, function); } }; + extern VolatilePOD 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); } } } diff --git a/src/Library/src/CD/cd.cpp b/src/Library/src/CD/cd.cpp index 39c8d26c..998e3f0e 100644 --- a/src/Library/src/CD/cd.cpp +++ b/src/Library/src/CD/cd.cpp @@ -7,7 +7,19 @@ namespace JabyEngine { namespace CD { namespace internal { - VolatilePOD last_interrupt{CD_IO::Interrupt::Type::None}; + struct Mode : public ComplexBitMap { + static constexpr auto DoubleSpeed = Bit(7); + static constexpr auto SingleSpeed = !DoubleSpeed; + static constexpr auto XADPCM = Bit(6); + static constexpr auto WholeSector = Bit(5); + static constexpr auto DataSector = !WholeSector; + static constexpr auto UseXAFilter = Bit(3); + static constexpr auto AudioPlayIRQ = Bit(2); + static constexpr auto AutoPauseTrack = Bit(1); + static constexpr auto CDDA = Bit(0); + }; + + static constexpr auto DataSectorMode = Mode::with(Mode::DoubleSpeed, Mode::DataSector); static InterruptVerifierResult interrupt_verifier() { if(Interrupt::is_irq(Interrupt::CDROM)) { @@ -32,14 +44,18 @@ namespace JabyEngine { __syscall_ReturnFromException(); } + VolatilePOD last_interrupt{CD_IO::Interrupt::Type::None}; InterrupCallback callback = { .next = nullptr, .handler_function = reinterpret_cast(interrupt_handler), .verifier_function = interrupt_verifier }; - State start_reading(FileInfo file_info, const SectorBufferAllocator& buffer_allocator) { - printf("I'm not implemented! %s\n", __FUNCTION__); + State read_file(FileInfo file_info, const SectorBufferAllocator& buffer_allocator) { + CD_IO::PortIndex0::change_to(); + Command::send_wait(CD_IO::PortIndex0::CommandFifo, CD_IO::PortIndex0::ParameterFifo, CD_IO::Command::SetMode, DataSectorMode); + + printf("I'm not fully implemented! %s\n", __FUNCTION__); return State::Error; } } diff --git a/src/Library/src/File/Processor/cd_file_processor.cpp b/src/Library/src/File/Processor/cd_file_processor.cpp index 13275049..89bc4ee6 100644 --- a/src/Library/src/File/Processor/cd_file_processor.cpp +++ b/src/Library/src/File/Processor/cd_file_processor.cpp @@ -1,10 +1,19 @@ #include +#include #include namespace JabyEngine { void CDFileProcessor :: start_cur_job() { + using CD::internal::FileInfo; + using CD::internal::SectorBufferAllocator; + const auto& cur_lba = this->lba[this->jobs.files->rel_lba_idx]; + CD::internal::read_file(FileInfo::from_auto_lba(cur_lba), SectorBufferAllocator::create(this, [](void* ctx) -> CD::internal::DataSector* { + printf("Blubb?!\n"); + return nullptr; + })); + printf(">>> CD needs to load LBA: %i -> %i\n", cur_lba.lba, cur_lba.size_words); }