diff --git a/include/PSX/System/IOPorts/cd_io.hpp b/include/PSX/System/IOPorts/cd_io.hpp index e6d2c36c..bab6d9ba 100644 --- a/include/PSX/System/IOPorts/cd_io.hpp +++ b/include/PSX/System/IOPorts/cd_io.hpp @@ -52,6 +52,14 @@ namespace JabyEngine { __declare_io_type(Request, uint8_t, static constexpr auto WantCommandStartIRQ = Bit(5); static constexpr auto WantData = Bit(7); + + void want_data() { + this->raw_value = Self::set(Self::WantData); + } + + void reset() { + this->raw_value = 0; + } ); __declare_io_type(SoundMapCoding, uint8_t, diff --git a/include/PSX/System/IOPorts/dma_io.hpp b/include/PSX/System/IOPorts/dma_io.hpp index c17b54b9..7f7a1e69 100644 --- a/include/PSX/System/IOPorts/dma_io.hpp +++ b/include/PSX/System/IOPorts/dma_io.hpp @@ -12,6 +12,11 @@ namespace JabyEngine { struct SyncMode0 { static constexpr auto NumberOfWords = BitRange::from_to(0, 15); static constexpr auto CD_OneBlock = Bit(16); + + static constexpr Self for_cd() { + // v Should be replaced with a named constant + return Self::from(SyncMode0::CD_OneBlock, SyncMode0::NumberOfWords.with(512)); + } }; struct SyncMode1 { diff --git a/src/Library/src/BootLoader/cd_boot.cpp b/src/Library/src/BootLoader/cd_boot.cpp index 37016a89..24da5b48 100644 --- a/src/Library/src/BootLoader/cd_boot.cpp +++ b/src/Library/src/BootLoader/cd_boot.cpp @@ -25,11 +25,10 @@ namespace JabyEngine { CD_IO::Interrupt::ack_extended(CD_IO::PortIndex1::InterruptFlag); CD_IO::Interrupt::enable(CD_IO::PortIndex1::InterruptEnable); - Interrupt::ack_irq(Interrupt::CDROM); Interrupt::enable_irq(Interrupt::CDROM); + Interrupt::ack_irq(Interrupt::CDROM); __syscall_ExitCriticalSection(); - CD_IO::PortIndex0::change_to(); Command::send_wait(CD_IO::PortIndex0::CommandFifo, CD_IO::PortIndex0::ParameterFifo, CD_IO::Command::GetStat); diff --git a/src/Library/src/BootLoader/start_boot.cpp b/src/Library/src/BootLoader/start_boot.cpp index 94cc51ee..ee4bf32d 100644 --- a/src/Library/src/BootLoader/start_boot.cpp +++ b/src/Library/src/BootLoader/start_boot.cpp @@ -7,10 +7,10 @@ namespace JabyEngine { namespace boot { - namespace Start { + namespace Start { + //This should become part of the bootloader later static void enable_DMA() { - const auto dpcr = DMA_IO::DPCR_t(DMA_IO::DPCR).set(DMA_IO::DPCR_t::SPUEnable).set(DMA_IO::DPCR_t::GPUEnable); - DMA_IO::DPCR = dpcr; + DMA_IO::DPCR = DMA_IO::DPCR_t(DMA_IO::DPCR).set(DMA_IO::DPCR_t::SPUEnable).set(DMA_IO::DPCR_t::GPUEnable).set(DMA_IO::DPCR_t::CDROMEnable); } JabyEngine::NextRoutine setup() { diff --git a/src/Library/src/CD/cd.cpp b/src/Library/src/CD/cd.cpp index f64c7edf..ba1ca60c 100644 --- a/src/Library/src/CD/cd.cpp +++ b/src/Library/src/CD/cd.cpp @@ -1,4 +1,5 @@ #include "../../internal-include/CD/cd_internal.hpp" +#include #include #include @@ -24,6 +25,36 @@ namespace JabyEngine { static SectorBufferAllocator sector_allocator; static uint16_t sectors_left; + static void read_sector_dma(CD_IO::DataSector& sector) { + static const auto WaitSectorReady = []() { + while(!CD_IO::IndexStatus.is_set(CD_IO::IndexStatus_t::HasDataFifoData)); + }; + + static const auto ReadSector = [](uint32_t* dst) { + DMA_IO::CDROM.set_adr(reinterpret_cast(dst)); + DMA_IO::CDROM.block_ctrl = DMA_IO::BCR_t::SyncMode0::for_cd(); + DMA_IO::CDROM.channel_ctrl = DMA_IO::CHCHR_t::StartCDROM(); + + DMA_IO::CDROM.wait(); + + CD_IO::PortIndex0::Request.reset(); + }; + + WaitSectorReady(); + ReadSector(sector.data); + } + + static void read_sector_to(CD_IO::DataSector& sector) { + CD_IO::PortIndex0::change_to(); + CD_IO::PortIndex0::Request.want_data(); + + // We only support DMA rn + read_sector_dma(sector); + + // Do we ever want to support reading via IO Port? + // Doesn't seem to important when we can use DMA + } + static InterruptVerifierResult interrupt_verifier() { static const auto pause = []() { CD_IO::PortIndex0::change_to(); @@ -43,12 +74,7 @@ namespace JabyEngine { auto* sector = sector_allocator.allocate_sector(); if(sector) { //Now obtain sector - static const char SimpleStr[] = "CD-Drive: This is a test string"; - - char* test_str = reinterpret_cast(sector->data); - for(size_t n = 0; n < sizeof(SimpleStr); n++) { - test_str[n] = SimpleStr[n]; - } + //read_sector_to(*sector); sectors_left--; if(sectors_left == 0) { @@ -102,7 +128,7 @@ namespace JabyEngine { current_state = State::Reading; - printf("Now reading: %i\n", file_info.lba); + printf("Now reading: %i(%i:%i:%i)\n", file_info.lba, loc.get_min_cd(), loc.get_sec_cd(), loc.get_sector_cd()); } } } diff --git a/src/Library/src/File/Processor/cd_file_processor.cpp b/src/Library/src/File/Processor/cd_file_processor.cpp index 58a9dd52..7ba57171 100644 --- a/src/Library/src/File/Processor/cd_file_processor.cpp +++ b/src/Library/src/File/Processor/cd_file_processor.cpp @@ -54,6 +54,7 @@ namespace JabyEngine { case CD::internal::State::Error: // Error for real! + test_print(); return Progress::Error; }