diff --git a/examples/PoolBox/application/src/application.cpp b/examples/PoolBox/application/src/application.cpp index 96357e30..f30e25f1 100644 --- a/examples/PoolBox/application/src/application.cpp +++ b/examples/PoolBox/application/src/application.cpp @@ -6,11 +6,44 @@ #include #include //#include +#include #include #include using namespace JabyEngine; +struct XAPlayer { + static constexpr auto MaxChannels = 2; + + uint8_t channel; + + static XAPlayer create() { + return XAPlayer{0}; + } + + void play() { + this->channel = 0; + Assets::XAAudio::play_fox(); + } + + void stop() { + CDXA::stop(); + } + + void change_channel(int8_t step) { + this->channel = (this->channel + step)%MaxChannels; + CDXA::set_channel(this->channel); + } + + void push() { + CDXA::push_play(); + } + + void pop() { + CDXA::pop_play(); + } +}; + struct StateChange { void (*asset_load)(); void (*main)(); @@ -46,8 +79,7 @@ static const auto doener_fish = Make::SPRT( GPU::Color24::Grey() ); -// Do we want this namespace? -// Do we want Paco to be HERE?! +static XAPlayer xa = XAPlayer::create(); static object::Paco paco; static Menu::SimpleMenu menu; static StateChange state_changer; @@ -108,17 +140,29 @@ static void setup() { // const auto [first_track, last_track] = CDDA::get_tracks(); // CDDA::play(first_track); - Assets::XAAudio::play_fox(); + xa.play(); } namespace NormalScene { static void update() { + using DigitalButton = Periphery::GenericController::Button; + static const char Title[] = ">> Pool Box <<"; static const char Version[] = "Ver. 0.8.5"; static constexpr auto TitleLength = DefaultFont::Info.estimate_str_render_length(Title); static constexpr auto VersionLength = DefaultFont::Info.estimate_str_render_length(Version); Periphery::query_controller(); + const auto& controller = Periphery::get_primary_controller_as(); + + if(controller.button.went_down(DigitalButton::R1)) { + xa.change_channel(1); + } + + if(controller.button.went_down(DigitalButton::L1)) { + xa.change_channel(-1); + } + auto cursor = FontWriter::update(Make::PositionI16((GPU::Display::Width-TitleLength)/2, 16)); paco.update(); @@ -165,11 +209,10 @@ namespace LoadingScene { render(); GPU::swap_buffers_vsync(1); - // TODO: Enable push and pop - //CDDA::push_play(); + xa.push(); state_changer.asset_load(); old_state_changer = state_changer; - //CDDA::pop_play(); + xa.pop(); } state_changer.main(); diff --git a/include/PSX/System/threads.hpp b/include/PSX/System/threads.hpp index 1ee97422..8d4ce0eb 100644 --- a/include/PSX/System/threads.hpp +++ b/include/PSX/System/threads.hpp @@ -3,6 +3,10 @@ namespace JabyEngine { struct Thread { + static uintptr_t get_pic() { + return table_of_tables.processes->current_tcb->epc; + } + static constexpr uint32_t idx_from_handle(SysCall::ThreadHandle thread) { return thread & 0xFFFF; } @@ -25,6 +29,10 @@ namespace JabyEngine { }; struct MainThread { + static uintptr_t get_pic() { + return table_of_tables.threads[0].epc; + } + static void prepare_if_main(SysCall::ThreadHandle handle) { if(table_of_tables.processes->current_tcb == &table_of_tables.threads[0]) { Thread::prepare_next(handle); diff --git a/src/Library/internal-include/CD/cd_internal.hpp b/src/Library/internal-include/CD/cd_internal.hpp index e7121a8e..ccd84a4f 100644 --- a/src/Library/internal-include/CD/cd_internal.hpp +++ b/src/Library/internal-include/CD/cd_internal.hpp @@ -50,8 +50,8 @@ namespace JabyEngine { }; namespace IRQ { - void read_sector_to(uint32_t* dst, size_t bytes); - void resume_at(const BCDTimeStamp& cd_time); + void read_sector_to0(uint32_t* dst, size_t bytes); + void resume_at0(const BCDTimeStamp& cd_time); } static State read_current_state() { @@ -67,6 +67,7 @@ namespace JabyEngine { void enable_CDXA(bool double_speed); static void pause() { + CD_IO::PortIndex0::change_to(); Command::send(CD_IO::Command::Pause); } } diff --git a/src/Library/src/Audio/CDXA.cpp b/src/Library/src/Audio/CDXA.cpp index c116637e..ba4b4fa5 100644 --- a/src/Library/src/Audio/CDXA.cpp +++ b/src/Library/src/Audio/CDXA.cpp @@ -18,9 +18,11 @@ namespace JabyEngine { case CD_IO::Interrupt::DataReady: { // The IRQ stack is 0x1000 bytes large so this should fit CD::RawXADataSector xa_file; - CD::IRQ::read_sector_to(reinterpret_cast(&xa_file), sizeof(CD::RawXADataSector)); + + CD_IO::PortIndex0::change_to(); + CD::IRQ::read_sector_to0(reinterpret_cast(&xa_file), sizeof(CD::RawXADataSector)); if(setting.channel == xa_file.sub_header.channel_number) { - CD::IRQ::resume_at(setting.start_loc); + CD::IRQ::resume_at0(setting.start_loc); CD::Command::send(CD_IO::Command::ReadS); } } break; @@ -35,7 +37,8 @@ namespace JabyEngine { setting.start_loc = CD::BCDTimeStamp::from(lba[rel_lba_idx].get_lba()); setting.double_speed = double_speed; - CD::enable_CDXA(double_speed); + CD::current_state = CD::State::XAMode; + CD::enable_CDXA(double_speed); //< Activates PortIndex0 set_channel(channel); CD::Command::wait_completed(); CD::Command::send_wait(CD_IO::Command::SetLoc, setting.start_loc.min, setting.start_loc.sec, setting.start_loc.sector); @@ -43,13 +46,13 @@ namespace JabyEngine { } void stop() { - // TODO: Alias this? CD::pause(); } void set_channel(uint8_t channel) { static constexpr uint8_t File = 1; + CD_IO::PortIndex0::change_to(); CD::Command::send(CD_IO::Command::Filter, File, channel); setting.channel = channel; } @@ -61,7 +64,8 @@ namespace JabyEngine { void pop_play() { CD::Command::wait_completed(); - CD::enable_CDXA(setting.double_speed); + CD::current_state = CD::State::XAMode; + CD::enable_CDXA(setting.double_speed); //< Activates PortIndex0 set_channel(setting.channel); CD::Command::wait_completed(); diff --git a/src/Library/src/BootLoader/cd_boot.cpp b/src/Library/src/BootLoader/cd_boot.cpp index 9af68f7a..71d5f50e 100644 --- a/src/Library/src/BootLoader/cd_boot.cpp +++ b/src/Library/src/BootLoader/cd_boot.cpp @@ -40,12 +40,12 @@ namespace JabyEngine { __debug_boot_color_at(::JabyEngine::GPU::Color24::Red(), DebugX, DebugY, DebugScale); CD_IO::PortIndex0::change_to(); - __debug_boot_color_at(::JabyEngine::GPU::Color24::Green(), DebugX, DebugY, DebugScale); +/* __debug_boot_color_at(::JabyEngine::GPU::Color24::Green(), DebugX, DebugY, DebugScale); Command::send_wait(CD_IO::PortIndex0::CommandFifo, CD_IO::PortIndex0::ParameterFifo, CD_IO::Command::GetStat); __debug_boot_color_at(::JabyEngine::GPU::Color24::Blue(), DebugX, DebugY, DebugScale); Command::send_wait(CD_IO::PortIndex0::CommandFifo, CD_IO::PortIndex0::ParameterFifo, CD_IO::Command::GetStat); __debug_boot_color_at(::JabyEngine::GPU::Color24::Yellow(), DebugX, DebugY, DebugScale); - Command::send_wait(CD_IO::PortIndex0::CommandFifo, CD_IO::PortIndex0::ParameterFifo, CD_IO::Command::Init); + Command::send_wait(CD_IO::PortIndex0::CommandFifo, CD_IO::PortIndex0::ParameterFifo, CD_IO::Command::Init);*/ Command::send(CD_IO::Command::Demute); } diff --git a/src/Library/src/CD/cd.cpp b/src/Library/src/CD/cd.cpp index 9924d6c2..7b758d7b 100644 --- a/src/Library/src/CD/cd.cpp +++ b/src/Library/src/CD/cd.cpp @@ -46,21 +46,21 @@ namespace JabyEngine { .verifier_function = IRQ::verifier }; - static BCDTimeStamp send_read_cmd(uint32_t lba, CD_IO::Command::Desc desc) { - const auto loc = BCDTimeStamp::from(lba); - + static BCDTimeStamp send_read_cmd0(uint32_t lba, CD_IO::Command::Desc desc) { + const auto loc = BCDTimeStamp::from(lba); + Command::send_wait(CD_IO::Command::SetLoc, loc.min, loc.sec, loc.sector); Command::send(desc); return loc; } - static void send_read_n(uint32_t lba) { - send_read_cmd(lba, CD_IO::Command::ReadN); + static void send_read_n0(uint32_t lba) { + send_read_cmd0(lba, CD_IO::Command::ReadN); current_state = State::Reading; } namespace IRQ { - static void read_sector_dma(uint32_t* dst, size_t bytes) { + static void read_sector_dma0(uint32_t* dst, size_t bytes) { static const auto WaitSectorReady = []() { while(!CD_IO::IndexStatus.read().is_set(CD_IO::IndexStatus::HasDataFifoData)); }; @@ -79,23 +79,22 @@ namespace JabyEngine { ReadSector(dst, bytes); } - void read_sector_to(uint32_t* dst, size_t bytes) { + void read_sector_to0(uint32_t* dst, size_t bytes) { CD_IO::PortIndex0::Request.write(CD_IO::Request::want_data()); // We only support DMA rn - read_sector_dma(dst, bytes); + read_sector_dma0(dst, bytes); // Do we ever want to support reading via IO Port? // Doesn't seem to important when we can use DMA } - void resume_at(const BCDTimeStamp& cd_time) { + void resume_at0(const BCDTimeStamp& cd_time) { Command::send(CD_IO::Command::SetLoc, cd_time.min, cd_time.sec, cd_time.sector); CD_IO::PortIndex1::change_to(); while(CD_IO::Interrupt::get_type(CD_IO::PortIndex1::InterruptFlag) != CD_IO::Interrupt::Acknowledge); CD_IO::Interrupt::ack_extended(CD_IO::PortIndex1::InterruptFlag); - CD_IO::PortIndex0::change_to(); } @@ -128,7 +127,7 @@ namespace JabyEngine { auto* sector = sector_allocator.allocate_sector(); if(sector) { //Now obtain sector - read_sector_to(sector->data, CD_IO::DataSector::SizeBytes); + read_sector_to0(sector->data, CD_IO::DataSector::SizeBytes); if(cur_file.done_processing()) { current_state = State::Done; @@ -144,7 +143,7 @@ namespace JabyEngine { case CD_IO::Interrupt::DataEnd: { // TODO: Fix this!! This is a freaking static time - resume_at(BCDTimeStamp{.min = 0x0, .sec = 0x09, .sector = 0x0}); + resume_at0(BCDTimeStamp{.min = 0x0, .sec = 0x09, .sector = 0x0}); Command::send(CD_IO::Command::Play); } break; @@ -172,38 +171,40 @@ namespace JabyEngine { sector_allocator = buffer_allocator; Command::wait_completed(); + CD_IO::PortIndex0::change_to(); Command::send_wait(CD_IO::Command::SetMode, DataSectorMode); - - send_read_n(cur_file.cur_lba); + send_read_n0(cur_file.cur_lba); } void continue_reading() { if(current_state == State::BufferFull) { Command::wait_completed(); - send_read_n(cur_file.cur_lba); + CD_IO::PortIndex0::change_to(); + send_read_n0(cur_file.cur_lba); } } BCDTimeStamp get_loc() { Command::wait_completed(); + CD_IO::PortIndex0::change_to(); Command::send_wait(CD_IO::Command::GetLocP); - const auto track = CD_IO::PortIndex0::ResponseFifo.read().raw; // track number (AAh=Lead-out area) (FFh=unknown, toc, none?) - const auto index = CD_IO::PortIndex0::ResponseFifo.read().raw; // index number (Usually 01h) - const auto mm = CD_IO::PortIndex0::ResponseFifo.read().raw; // minute number within track (00h and up) - const auto ss = CD_IO::PortIndex0::ResponseFifo.read().raw; // second number within track (00h to 59h) - const auto sect = CD_IO::PortIndex0::ResponseFifo.read().raw; // sector number within track (00h to 74h) + const auto track = CD_IO::PortIndex0::ResponseFifo.read().raw; // track number (AAh=Lead-out area) (FFh=unknown, toc, none?) + const auto index = CD_IO::PortIndex0::ResponseFifo.read().raw; // index number (Usually 01h) + const auto mm = CD_IO::PortIndex0::ResponseFifo.read().raw; // minute number within track (00h and up) + const auto ss = CD_IO::PortIndex0::ResponseFifo.read().raw; // second number within track (00h to 59h) + const auto sect = CD_IO::PortIndex0::ResponseFifo.read().raw; // sector number within track (00h to 74h) - return BCDTimeStamp::from_time( - CD_IO::PortIndex0::ResponseFifo.read().raw, // minute number on entire disk (00h and up) - CD_IO::PortIndex0::ResponseFifo.read().raw, // second number on entire disk (00h to 59h) - CD_IO::PortIndex0::ResponseFifo.read().raw // sector number on entire disk (00h to 74h) - ); + const auto min = CD_IO::PortIndex0::ResponseFifo.read().raw; // minute number on entire disk (00h and up) + const auto sec = CD_IO::PortIndex0::ResponseFifo.read().raw; // second number on entire disk (00h to 59h) + const auto sectors = CD_IO::PortIndex0::ResponseFifo.read().raw; // sector number on entire disk (00h to 74h) + return BCDTimeStamp{min, sec, sectors}; } void enable_CDDA() { Command::wait_completed(); + CD_IO::PortIndex0::change_to(); Command::send_wait(CD_IO::Command::SetMode, AudioSectorMode); } @@ -214,8 +215,8 @@ namespace JabyEngine { const uint8_t mode = XAAudioSectorMode.raw | (double_speed ? DoubleSpeedBit : SingleSpeedBit); Command::wait_completed(); + CD_IO::PortIndex0::change_to(); Command::send_wait(CD_IO::Command::SetMode, mode); - current_state = State::XAMode; } } }