From 6e99221ab6e914e530b508129e71008f90d5e603 Mon Sep 17 00:00:00 2001 From: Jaby Date: Sat, 1 Apr 2023 15:23:32 +0200 Subject: [PATCH] Fix CD pause issue --- .../internal-include/CD/cd_internal.hpp | 2 +- src/Library/src/BootLoader/gpu_boot.cpp | 2 +- src/Library/src/CD/cd.cpp | 72 ++++++++----------- .../src/File/Processor/cd_file_processor.cpp | 5 +- 4 files changed, 34 insertions(+), 47 deletions(-) diff --git a/src/Library/internal-include/CD/cd_internal.hpp b/src/Library/internal-include/CD/cd_internal.hpp index 5a370b4b..7c555d27 100644 --- a/src/Library/internal-include/CD/cd_internal.hpp +++ b/src/Library/internal-include/CD/cd_internal.hpp @@ -47,7 +47,7 @@ namespace JabyEngine { }; void read_file(FileInfo file_info, const SectorBufferAllocator& buffer_allocator); - bool continue_reading(); + void continue_reading(); } } } diff --git a/src/Library/src/BootLoader/gpu_boot.cpp b/src/Library/src/BootLoader/gpu_boot.cpp index d311e54d..5f38475e 100644 --- a/src/Library/src/BootLoader/gpu_boot.cpp +++ b/src/Library/src/BootLoader/gpu_boot.cpp @@ -43,7 +43,7 @@ namespace JabyEngine { // Upload SplashScreen picture auto state = FileProcessor::create(&__boot_loader_end, SimpleTIM(32, 0, 0, 0)); - while(state.process(bytes_ready) == Progress::InProgress); + state.process(bytes_ready); Display::enable(); } diff --git a/src/Library/src/CD/cd.cpp b/src/Library/src/CD/cd.cpp index c407727e..963a98f3 100644 --- a/src/Library/src/CD/cd.cpp +++ b/src/Library/src/CD/cd.cpp @@ -23,7 +23,8 @@ namespace JabyEngine { static constexpr auto DataSectorMode = Mode_t::from(Mode_t::DoubleSpeed, Mode_t::DataSector); static SectorBufferAllocator sector_allocator; - static uint16_t sectors_left; + static uint16_t cur_lba; + static uint16_t dst_lba; static void pause_cd() { CD_IO::PortIndex0::change_to(); @@ -31,9 +32,14 @@ namespace JabyEngine { } // Requires Index0 - static void start_reading_cd() { + static void read_cd(uint16_t lba) { + const auto loc = CDTimeStamp::from(lba); + + Command::send_wait(CD_IO::Command::SetLoc, loc.get_min_cd(), loc.get_sec_cd(), loc.get_sector_cd()); Command::send(CD_IO::Command::ReadN); current_state = State::Reading; + + printf("Now reading: %i(%i:%i:%i)\n", lba, loc.get_min_cd(), loc.get_sec_cd(), loc.get_sector_cd()); } static void read_sector_dma(CD_IO::DataSector& sector) { @@ -66,23 +72,6 @@ namespace JabyEngine { // Doesn't seem to important when we can use DMA } - 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; @@ -93,8 +82,20 @@ namespace JabyEngine { CD_IO::Interrupt::ack(CD_IO::PortIndex1::InterruptFlag); if(cur_irq == CD_IO::Interrupt::DataReady) { - //Obtain sector content here - if(!try_read_sector()) { + // Obtain sector content here + auto* sector = sector_allocator.allocate_sector(); + if(sector) { + //Now obtain sector + read_sector_to(*sector); + + cur_lba++; + if(cur_lba == dst_lba) { + current_state = State::Done; + pause_cd(); + } + } + + else { current_state = State::BufferFull; pause_cd(); } @@ -128,35 +129,20 @@ namespace JabyEngine { }; void read_file(FileInfo file_info, const SectorBufferAllocator& buffer_allocator) { + cur_lba = file_info.lba; + dst_lba = file_info.lba + file_info.sectors; sector_allocator = buffer_allocator; - sectors_left = file_info.sectors; CD_IO::PortIndex0::change_to(); Command::send_wait(CD_IO::Command::SetMode, DataSectorMode); - const auto loc = CDTimeStamp::from(file_info); - Command::send_wait(CD_IO::Command::SetLoc, loc.get_min_cd(), loc.get_sec_cd(), loc.get_sector_cd()); - start_reading_cd(); - - printf("Now reading: %i(%i:%i:%i)\n", file_info.lba, loc.get_min_cd(), loc.get_sec_cd(), loc.get_sector_cd()); + read_cd(file_info.lba); } - bool continue_reading() { - if(current_state != State::BufferFull) { - return true; - } - - printf(">>> Trying to read buffer from continue_reading\n"); - CD_IO::PortIndex1::change_to(); - if(!try_read_sector()) { - return false; - } - - if(current_state != State::Done) { - CD_IO::PortIndex0::change_to(); - start_reading_cd(); - } - return true; + void continue_reading() { + if(current_state == State::BufferFull) { + read_cd(cur_lba); + } } } } diff --git a/src/Library/src/File/Processor/cd_file_processor.cpp b/src/Library/src/File/Processor/cd_file_processor.cpp index 684c8858..ac4e0a31 100644 --- a/src/Library/src/File/Processor/cd_file_processor.cpp +++ b/src/Library/src/File/Processor/cd_file_processor.cpp @@ -3,7 +3,7 @@ #include namespace JabyEngine { - static constexpr auto NormalCircularBufferSize = 64; + static constexpr auto NormalCircularBufferSize = 5; void CDFileProcessor :: start_cur_job() { using CD::internal::FileInfo; @@ -89,7 +89,8 @@ namespace JabyEngine { case CD::internal::State::BufferFull: /* We processd data and unpause the CD drive */ - return CD::internal::continue_reading() ? Progress::InProgress : Progress::Error; + CD::internal::continue_reading(); + return Progress::InProgress; case CD::internal::State::Reading: return Progress::InProgress;