diff --git a/include/PSX/Auxiliary/math_helper.hpp b/include/PSX/Auxiliary/math_helper.hpp new file mode 100644 index 00000000..9a8d4c9e --- /dev/null +++ b/include/PSX/Auxiliary/math_helper.hpp @@ -0,0 +1,18 @@ +#ifndef __JABYENGINE_MATH_HELPER_HPP__ +#define __JABYENGINE_MATH_HELPER_HPP__ +#include "types.hpp" + +namespace JabyEngine { + template + static constexpr pair div_and_mod(T value, T div) { + const auto result = value/div; + return {static_cast(result), static_cast(value - (result*div))}; + } + + static constexpr uint8_t to_bcd(uint8_t value) { + const auto [tenth, rest] = div_and_mod(value, static_cast(10)); + return (tenth << 4 | rest); + } +} + +#endif //!__JABYENGINE_MATH_HELPER_HPP__ \ No newline at end of file diff --git a/include/PSX/System/IOPorts/cd_io.hpp b/include/PSX/System/IOPorts/cd_io.hpp index 8b36d7a9..1e5721ca 100644 --- a/include/PSX/System/IOPorts/cd_io.hpp +++ b/include/PSX/System/IOPorts/cd_io.hpp @@ -107,9 +107,11 @@ namespace JabyEngine { Interrupt::Type complete_irq; }; - 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 Info GetStat{0x01, Interrupt::Type::Acknowledge}; + static constexpr Info SetLoc{0x02, Interrupt::Type::Acknowledge}; + static constexpr Info ReadN{0x06, Interrupt::Type::DataReady}; + static constexpr Info Init{0x0A, Interrupt::Type::Complete}; + static constexpr Info SetMode{0x0E, 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 b5eb316b..1ea97e44 100644 --- a/src/Library/include/CD/cd_internal.hpp +++ b/src/Library/include/CD/cd_internal.hpp @@ -1,58 +1,11 @@ #ifndef __JABYENGINE_CD_INTERNAL_HPP__ #define __JABYENGINE_CD_INTERNAL_HPP__ +#include "cd_types.hpp" #include -#include namespace JabyEngine { namespace CD { namespace internal { - enum struct State { - Free = 0, - Done = 0, - - Reading, - BufferFull, - Error, - }; - - struct DataSector { - static constexpr size_t SizeBytes = 2048; - static constexpr size_t SizeWords = (SizeBytes/sizeof(uint32_t)); - - 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; - - static constexpr FileInfo from_auto_lba(const AutoLBAEntry& entry) { - return FileInfo{entry.lba, DataSector::words_to_sectors(entry.size_words)}; - } - }; - - class SectorBufferAllocator { - private: - typedef DataSector* (*AllocatorFunction)(void* ctx); - - private: - void* ctx = nullptr; - AllocatorFunction allocate = nullptr; - - constexpr SectorBufferAllocator(void* ctx, AllocatorFunction function) : ctx(ctx), allocate(function) { - } - - public: - static constexpr SectorBufferAllocator create(void* obj, AllocatorFunction function) { - return SectorBufferAllocator(obj, function); - } - }; - extern VolatilePOD last_interrupt; struct Command { diff --git a/src/Library/include/CD/cd_types.hpp b/src/Library/include/CD/cd_types.hpp new file mode 100644 index 00000000..3778f189 --- /dev/null +++ b/src/Library/include/CD/cd_types.hpp @@ -0,0 +1,95 @@ +#ifndef __JABYENGINE_INTERNAL_CD_TYPES_HPP__ +#define __JABYENGINE_INTERNAL_CD_TYPES_HPP__ +#include +#include +#include + +namespace JabyEngine { + namespace CD { + namespace internal { + enum struct State { + Free = 0, + Done = 0, + + Reading, + BufferFull, + Error, + }; + + struct DataSector { + static constexpr size_t SizeBytes = 2048; + static constexpr size_t SizeWords = (SizeBytes/sizeof(uint32_t)); + + 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; + + static constexpr FileInfo from(const AutoLBAEntry& entry) { + return FileInfo{entry.lba, DataSector::words_to_sectors(entry.size_words)}; + } + }; + + class SectorBufferAllocator { + private: + typedef DataSector* (*AllocatorFunction)(void* ctx); + + private: + void* ctx = nullptr; + AllocatorFunction allocate = nullptr; + + constexpr SectorBufferAllocator(void* ctx, AllocatorFunction function) : ctx(ctx), allocate(function) { + } + + public: + constexpr SectorBufferAllocator() = default; + + static constexpr SectorBufferAllocator create(void* obj, AllocatorFunction function) { + return SectorBufferAllocator(obj, function); + } + }; + + struct CDTimeStamp { + static constexpr size_t MaxSector = 75; + static constexpr size_t MaxSeconds = 60; + + uint8_t min; + uint8_t sec; + uint8_t sector; + + static constexpr CDTimeStamp from(uint16_t lba) { + const auto [min, new_lba] = div_and_mod(lba, static_cast(MaxSector*MaxSeconds)); + const auto [sec, sectors] = div_and_mod(new_lba, static_cast(MaxSector)); + + return CDTimeStamp{static_cast(min), static_cast(sec), static_cast(sectors)}; + } + + static constexpr CDTimeStamp from(const FileInfo& file_info) { + // Only for now + const auto lba = file_info.lba + 2*MaxSector; + return CDTimeStamp::from(lba); + } + + constexpr uint8_t get_min_cd() const { + return to_bcd(this->min); + } + + constexpr uint8_t get_sec_cd() const { + return to_bcd(this->sec); + } + + constexpr uint8_t get_sector_cd() const { + return to_bcd(this->sector); + } + }; + } + } +} +#endif //!__JABYENGINE_INTERNAL_CD_TYPES_HPP__ \ No newline at end of file diff --git a/src/Library/src/CD/cd.cpp b/src/Library/src/CD/cd.cpp index 998e3f0e..52c5bc37 100644 --- a/src/Library/src/CD/cd.cpp +++ b/src/Library/src/CD/cd.cpp @@ -20,14 +20,22 @@ namespace JabyEngine { }; static constexpr auto DataSectorMode = Mode::with(Mode::DoubleSpeed, Mode::DataSector); + + static SectorBufferAllocator sector_allocator; + static uint16_t sectors_left; static InterruptVerifierResult interrupt_verifier() { if(Interrupt::is_irq(Interrupt::CDROM)) { const uint8_t old_idx = (CD_IO::IndexStatus.read() & 0x3); CD_IO::PortIndex1::change_to(); + const auto cur_irq = CD_IO::Interrupt::get_type(CD_IO::PortIndex1::InterruptFlagRegister); - last_interrupt.write(CD_IO::Interrupt::get_type(CD_IO::PortIndex1::InterruptFlagRegister)); + if(cur_irq == CD_IO::Interrupt::DataReady) { + printf("CDDrive: Got data!\n"); + } + + last_interrupt.write(cur_irq); CD_IO::Interrupt::ack(CD_IO::PortIndex1::InterruptFlagRegister); CD_IO::IndexStatus.write({old_idx}); @@ -52,9 +60,17 @@ namespace JabyEngine { }; State read_file(FileInfo file_info, const SectorBufferAllocator& buffer_allocator) { + sector_allocator = buffer_allocator; + sectors_left = file_info.sectors; + CD_IO::PortIndex0::change_to(); Command::send_wait(CD_IO::PortIndex0::CommandFifo, CD_IO::PortIndex0::ParameterFifo, CD_IO::Command::SetMode, DataSectorMode); + const auto loc = CDTimeStamp::from(file_info); + Command::send_wait(CD_IO::PortIndex0::CommandFifo, CD_IO::PortIndex0::ParameterFifo, CD_IO::Command::SetLoc, loc.get_min_cd(), loc.get_sec_cd(), loc.get_sector_cd()); + Command::send_wait(CD_IO::PortIndex0::CommandFifo, CD_IO::PortIndex0::ParameterFifo, CD_IO::Command::ReadN); + + printf("Now reading: %i\n", file_info.lba); 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 89bc4ee6..62bc2c8f 100644 --- a/src/Library/src/File/Processor/cd_file_processor.cpp +++ b/src/Library/src/File/Processor/cd_file_processor.cpp @@ -9,7 +9,7 @@ namespace JabyEngine { 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* { + CD::internal::read_file(FileInfo::from(cur_lba), SectorBufferAllocator::create(this, [](void* ctx) -> CD::internal::DataSector* { printf("Blubb?!\n"); return nullptr; })); diff --git a/src/Library/src/startup.cpp b/src/Library/src/startup.cpp index e2c5912b..cb3e9fae 100644 --- a/src/Library/src/startup.cpp +++ b/src/Library/src/startup.cpp @@ -24,5 +24,6 @@ namespace JabyEngine { next_routine = execute(next_routine); } printf("Stop!\n"); + while(true); } } \ No newline at end of file