diff --git a/include/PSX/SPU/spu.hpp b/include/PSX/SPU/spu.hpp index 64e60eca..f18f2f63 100644 --- a/include/PSX/SPU/spu.hpp +++ b/include/PSX/SPU/spu.hpp @@ -1,7 +1,32 @@ #pragma once -#include "../jabyengine.hpp" +#include "../System/IOPorts/spu_io.hpp" namespace JabyEngine { namespace SPU { + using SRAM_Adr = uint16_t; + + struct Voice { + size_t get_id() const { + return reinterpret_cast(this); + } + + SRAM_Adr allocate(size_t size) const; + SRAM_Adr allocate(SPU_IO::SampleRate frequency, size_t size) const; + void deallocate() const; + + void set_sample_rate(SPU_IO::SampleRate frequency) const { + SPU_IO::Voice[Voice::get_id()].sampleRate.write(frequency); + } + + void play() const { + SPU_IO::Key::On.write(1 << Voice::get_id()); + } + + void stop() const { + SPU_IO::Key::Off.write(1 << Voice::get_id()); + } + }; + + extern const Voice voice[SPU_IO::VoiceCount]; } } \ No newline at end of file diff --git a/mkfile/psexe.ld b/mkfile/psexe.ld index d02aff2f..8b672b79 100644 --- a/mkfile/psexe.ld +++ b/mkfile/psexe.ld @@ -55,7 +55,7 @@ __stack_start = ORIGIN(ram) + LENGTH(ram); SECTIONS { .zero (NOLOAD) : { - _ZN10JabyEngine2CD4zeroE = .; + _ZN10JabyEngine3SPU5voiceE = .; } > zero .bios (NOLOAD) : { diff --git a/src/Library/src/SPU/spu.cpp b/src/Library/src/SPU/spu.cpp index 40e88543..d01cbf44 100644 --- a/src/Library/src/SPU/spu.cpp +++ b/src/Library/src/SPU/spu.cpp @@ -4,7 +4,28 @@ #include #include +#include + namespace JabyEngine { namespace SPU { + SRAM_Adr Voice :: allocate(size_t size) const { + Voice::stop(); + const auto voice_id = Voice::get_id(); + const auto adr = static_cast(reinterpret_cast(SPU_MMU::allocate(voice_id, size))); + + SPU_IO::Voice[voice_id].adr.write({adr}); + return adr; + } + + SRAM_Adr Voice :: allocate(SPU_IO::SampleRate frequency, size_t size) const { + const auto result = Voice::allocate(size); + Voice::set_sample_rate(frequency); + return result; + } + + void Voice :: deallocate() const { + Voice::stop(); + SPU_MMU::deallocate(Voice::get_id()); + } } } \ No newline at end of file