From 864ecf4412d27d8539dc42062ca6836522fd35a6 Mon Sep 17 00:00:00 2001 From: Jaby Date: Wed, 29 Mar 2023 21:59:50 +0200 Subject: [PATCH] Support buffer full handling --- .../internal-include/CD/cd_internal.hpp | 1 + src/Library/src/CD/cd.cpp | 52 ++++++++++++------- .../src/File/Processor/cd_file_processor.cpp | 4 +- 3 files changed, 36 insertions(+), 21 deletions(-) diff --git a/src/Library/internal-include/CD/cd_internal.hpp b/src/Library/internal-include/CD/cd_internal.hpp index 3e05fb69..5a370b4b 100644 --- a/src/Library/internal-include/CD/cd_internal.hpp +++ b/src/Library/internal-include/CD/cd_internal.hpp @@ -47,6 +47,7 @@ namespace JabyEngine { }; void read_file(FileInfo file_info, const SectorBufferAllocator& buffer_allocator); + bool continue_reading(); } } } diff --git a/src/Library/src/CD/cd.cpp b/src/Library/src/CD/cd.cpp index 7b5a30d6..a812c3f4 100644 --- a/src/Library/src/CD/cd.cpp +++ b/src/Library/src/CD/cd.cpp @@ -25,6 +25,11 @@ namespace JabyEngine { static SectorBufferAllocator sector_allocator; static uint16_t sectors_left; + static void pause_cd() { + CD_IO::PortIndex0::change_to(); + Command::send(CD_IO::Command::Pause); + } + static void read_sector_dma(CD_IO::DataSector& sector) { static const auto WaitSectorReady = []() { while(!CD_IO::IndexStatus.is_set(CD_IO::IndexStatus_t::HasDataFifoData)); @@ -55,12 +60,24 @@ namespace JabyEngine { // Doesn't seem to important when we can use DMA } - static InterruptVerifierResult interrupt_verifier() { - static const auto pause = []() { - CD_IO::PortIndex0::change_to(); - Command::send(CD_IO::Command::Pause); - }; + static bool try_read_sector() { + // Obtain sector content here + auto* sector = sector_allocator.allocate_sector(); + if(sector) { + //Now obtain sector + read_sector_to(*sector); + sectors_left--; + if(sectors_left == 0) { + current_state = State::Done; + pause_cd(); + } + return true; + } + return false; + } + + static InterruptVerifierResult interrupt_verifier() { if(Interrupt::is_irq(Interrupt::CDROM)) { const uint8_t old_status = CD_IO::IndexStatus; @@ -71,21 +88,9 @@ namespace JabyEngine { if(cur_irq == CD_IO::Interrupt::DataReady) { //Obtain sector content here - auto* sector = sector_allocator.allocate_sector(); - if(sector) { - //Now obtain sector - read_sector_to(*sector); - - sectors_left--; - if(sectors_left == 0) { - current_state = State::Done; - pause(); - } - } - - else { + if(!try_read_sector()) { current_state = State::BufferFull; - pause(); + pause_cd(); } } @@ -131,6 +136,15 @@ namespace JabyEngine { printf("Now reading: %i(%i:%i:%i)\n", file_info.lba, loc.get_min_cd(), loc.get_sec_cd(), loc.get_sector_cd()); } + + bool continue_reading() { + if(current_state != State::BufferFull) { + return true; + } + + CD_IO::PortIndex1::change_to(); + return try_read_sector(); + } } } } \ No newline at end of file diff --git a/src/Library/src/File/Processor/cd_file_processor.cpp b/src/Library/src/File/Processor/cd_file_processor.cpp index 0b323114..01a198df 100644 --- a/src/Library/src/File/Processor/cd_file_processor.cpp +++ b/src/Library/src/File/Processor/cd_file_processor.cpp @@ -87,8 +87,8 @@ namespace JabyEngine { case CD::internal::State::BufferFull: // We have to process data and unpause the CD drive - // Error case for now - return Progress::Error; + CDFileProcessor::reading_state(cur_job); + return CD::internal::continue_reading() ? CDFileProcessor::process() : Progress::Error; case CD::internal::State::Reading: // Do we have data? Use it!