Converted SPU IO

This commit is contained in:
Jaby 2023-03-18 17:13:45 +01:00
parent d185641a2a
commit 1143f73f9b
3 changed files with 126 additions and 117 deletions

View File

@ -52,18 +52,20 @@ namespace JabyEngine {
typedef volatile T Value; typedef volatile T Value;
}; };
#define __declare_new_io_port(name, adr) \ #define __declare_new_named_io_port(type, name, adr) \
static auto& name = *reinterpret_cast<name##_v*>(adr) static inline auto& name = *reinterpret_cast<type##_v*>(adr)
/*constexpr name##_io_base() = default; \ #define __declare_new_io_port(name, adr) \
\ __declare_new_named_io_port(name, name, adr)
constexpr name##_io_base(type value) : raw_value(value) {} \
\*/ #define __declare_new_io_port_array(name, adr, size) \
static inline auto& name = reinterpret_cast<name##_v(&)[size]>(*reinterpret_cast<name##_v*>(adr))
#define __declare_io_type(name, type, ...) \ #define __declare_io_type(name, type, ...) \
template<template<typename> typename T> \ template<template<typename> typename T> \
struct name##_io_base { \ struct name##_io_base { \
T<type>::Value raw_value = 0; \ T<type>::Value raw_value = 0; \
typedef name##_io_base Self; \
\ \
__VA_ARGS__ \ __VA_ARGS__ \
template<typename...ARGS> \ template<typename...ARGS> \
@ -173,12 +175,11 @@ namespace JabyEngine {
}; };
struct __no_align ubus32_t { struct __no_align ubus32_t {
typedef ComplexBitMap<uint16_t> Base16; __declare_io_type(uint16_t, uint16_t,);
uint16_t_v low;
uint16_t_v high;
VolatileBitMapPOD<Base16> low; constexpr ubus32_t(uint32_t value) : low{0}, high{0} {
VolatileBitMapPOD<Base16> high;
constexpr ubus32_t(uint32_t value) {
*this = value; *this = value;
} }
@ -187,12 +188,15 @@ namespace JabyEngine {
} }
constexpr operator uint32_t() const { constexpr operator uint32_t() const {
return ((this->high.read_raw() << 16) | this->low.read_raw()); const uint32_t high = *this->high;
const uint32_t low = *this->low;
return ((high << 16) | low);
} }
constexpr ubus32_t& operator=(uint32_t value) { constexpr ubus32_t& operator=(uint32_t value) {
this->low.write_raw(value & 0xFFFF); this->low = (value & 0xFFFF);
this->high.write_raw(value >> 16); this->high = (value >> 16);
return *this; return *this;
} }

View File

@ -26,59 +26,63 @@ namespace JabyEngine {
typedef uint8_t Step; typedef uint8_t Step;
typedef int16_t SimpleVolume; typedef int16_t SimpleVolume;
typedef volatile int16_t SimpleVolume_v;
struct SampleRate : public ComplexBitMap<uint16_t> { typedef volatile uint16_t Adr_v;
static constexpr SampleRate from_HZ(double freq) { typedef volatile uint16_t DataTransferControl_v;
__declare_io_type(SampleRate, uint16_t,
static constexpr Self from_HZ(double freq) {
//4096 == 44100Hz //4096 == 44100Hz
constexpr double Base = (4096.0 / 44100.0); constexpr double Base = (4096.0 / 44100.0);
return {static_cast<uint16_t>((freq*Base))}; return {static_cast<uint16_t>((freq*Base))};
} }
}; );
struct SweepVolume : public ComplexBitMap<int16_t> { __declare_io_type(SweepVolume, int16_t,
// For Volume Mode // For Volume Mode
static constexpr auto SweepEnable = Bit<int16_t>(15); static constexpr auto SweepEnable = IOBitSet(15);
static constexpr auto VolumeEnable = !SweepEnable; static constexpr auto VolumeEnable = !SweepEnable;
static constexpr auto Volume = BitRange<int16_t>::from_to(0, 14); static constexpr auto Volume = IOValueSet::from_to(0, 14);
// For Sweep Mode // For Sweep Mode
static constexpr auto SweepMode = Bit<Mode>(14); static constexpr auto SweepMode = IOBitSet(14);
static constexpr auto SweepDirection = Bit<Direction>(13); static constexpr auto SweepDirection = IOBitSet(13);
static constexpr auto SweepPhase = Bit<Phase>(12); static constexpr auto SweepPhase = IOBitSet(12);
static constexpr auto SweepShift = BitRange<Shift>::from_to(2, 6); static constexpr auto SweepShift = IOValueSet::from_to(2, 6);
static constexpr auto SweepStep = BitRange<Step>::from_to(0, 1); static constexpr auto SweepStep = IOValueSet::from_to(0, 1);
);
__declare_io_type(SR, uint16_t,
static constexpr auto SustainMode = IOBitSet(31 - 16);
static constexpr auto SustainDirection = IOBitSet(30 - 16);
static constexpr auto SustainShift = IOValueSet::from_to((24 - 16), (28 - 16));
static constexpr auto SustainStep = IOValueSet::from_to((22 - 16), (23 - 16));
static constexpr auto ReleaseMode = IOBitSet(21 - 16);
static constexpr auto ReleaseShift = IOValueSet::from_to((16 - 16), (20 - 16));
);
__declare_io_type(AD, uint16_t,
static constexpr auto AttackMode = IOBitSet(15);
static constexpr auto AttackShift = IOValueSet::from_to(10, 14);
static constexpr auto AttackStep = IOValueSet::from_to(8, 9);
static constexpr auto DecayShift = IOValueSet::from_to(4, 7);
static constexpr auto SustainLevel = IOValueSet::from_to(0, 3);
);
struct __no_align Voice_v {
SweepVolume_v volumeLeft; //Offset: 0x0
SweepVolume_v volumeRight; //Offset: 0x2
SampleRate_v sampleRate; //Offset: 0x4;
Adr_v adr; //Offset: 0x6
AD_v ad; //Offset: 0x8
SR_v sr; //Offset: 0xA
SimpleVolume_v currentVolume; //Offset: 0xC
Adr_v repeatAdr; //Offset: 0xE
}; };
struct SR : public ComplexBitMap<uint16_t> { __declare_io_type(ControlRegister, uint16_t,
static constexpr auto SustainMode = Bit<Mode>(31 - 16);
static constexpr auto SustainDirection = Bit<Direction>(30 - 16);
static constexpr auto SustainShift = BitRange<Shift>::from_to((24 - 16), (28 - 16));
static constexpr auto SustainStep = BitRange<Step>::from_to((22 - 16), (23 - 16));
static constexpr auto ReleaseMode = Bit<Mode>(21 - 16);
static constexpr auto ReleaseShift = BitRange<Shift>::from_to((16 - 16), (20 - 16));
};
struct AD : public ComplexBitMap<uint16_t> {
static constexpr auto AttackMode = Bit<Mode>(15);
static constexpr auto AttackShift = BitRange<Shift>::from_to(10, 14);
static constexpr auto AttackStep = BitRange<Step>::from_to(8, 9);
static constexpr auto DecayShift = BitRange<Shift>::from_to(4, 7);
static constexpr auto SustainLevel = BitRange<uint16_t>::from_to(0, 3);
};
struct __no_align Voice {
VolatileBitMapPOD<SweepVolume> volumeLeft; //Offset: 0x0
VolatileBitMapPOD<SweepVolume> volumeRight; //Offset: 0x2
VolatileBitMapPOD<SampleRate> sampleRate; //Offset: 0x4;
VolatilePOD<uint16_t> adr; //Offset: 0x6
VolatileBitMapPOD<AD> ad; //Offset: 0x8
VolatileBitMapPOD<SR> sr; //Offset: 0xA
VolatilePOD<SimpleVolume> currentVolume; //Offset: 0xC
VolatilePOD<uint16_t> repeatAdr; //Offset: 0xE
};
struct ControlRegister : public ComplexBitMap<uint16_t> {
enum RAMTransferMode { enum RAMTransferMode {
Stop = 0, Stop = 0,
ManualWrite = 1, ManualWrite = 1,
@ -86,30 +90,30 @@ namespace JabyEngine {
DMARead = 3 DMARead = 3
}; };
static constexpr auto Enable = Bit<uint16_t>(15); static constexpr auto Enable = IOBitSet(15);
static constexpr auto Unmute = Bit<uint16_t>(14); static constexpr auto Unmute = IOBitSet(14);
static constexpr auto NoiseFrequcenyShift = BitRange<Shift>::from_to(10, 13); static constexpr auto NoiseFrequcenyShift = IOValueSet::from_to(10, 13);
static constexpr auto NoiseFrequcenyStep = BitRange<Step>::from_to(8, 9); static constexpr auto NoiseFrequcenyStep = IOValueSet::from_to(8, 9);
static constexpr auto ReverbMasterEnable = Bit<uint16_t>(7); static constexpr auto ReverbMasterEnable = IOBitSet(7);
static constexpr auto IRQ9Enable = Bit<uint16_t>(6); static constexpr auto IRQ9Enable = IOBitSet(6);
static constexpr auto TransferMode = BitRange<RAMTransferMode>::from_to(4, 5); static constexpr auto TransferMode = IOValueSet::from_to(4, 5);
static constexpr auto ExternalAudioReverb = Bit<uint16_t>(3); static constexpr auto ExternalAudioReverb = IOBitSet(3);
static constexpr auto CDAudioReverb = Bit<uint16_t>(2); static constexpr auto CDAudioReverb = IOBitSet(2);
static constexpr auto ExternalAudioEnable = Bit<uint16_t>(1); static constexpr auto ExternalAudioEnable = IOBitSet(1);
static constexpr auto CDAudioEnable = Bit<uint16_t>(0); static constexpr auto CDAudioEnable = IOBitSet(0);
}; );
struct PitchModFlags : public ComplexBitMap<uint16_t> { __declare_io_type(PMON, uint16_t,
static constexpr BitRange<uint16_t> EnableBits = BitRange<uint16_t>::from_to(1, 23); static constexpr auto EnableBits = IOValueSet::from_to(1, 23);
}; );
struct NoiseGenerator : public ComplexBitMap<uint16_t> { __declare_io_type(NON, uint16_t,
static constexpr BitRange<uint16_t> NoiseBits = BitRange<uint16_t>::from_to(0, 23); static constexpr auto NoiseBits = IOValueSet::from_to(0, 23);
}; );
struct EchoOn : public ComplexBitMap<uint16_t> { __declare_io_type(EON, uint16_t,
static constexpr BitRange<uint16_t> EchoBits = BitRange<uint16_t>::from_to(0, 23); static constexpr auto EchoBits = IOValueSet::from_to(0, 23);
}; );
static constexpr size_t VoiceCount = 24; static constexpr size_t VoiceCount = 24;
@ -120,35 +124,35 @@ namespace JabyEngine {
}; };
struct MainVolume { struct MainVolume {
__declare_io_port_member(SweepVolume, Left, 0x1F801D80); __declare_new_named_io_port(SweepVolume, Left, 0x1F801D80);
__declare_io_port_member(SweepVolume, Right, 0x1F801D82); __declare_new_named_io_port(SweepVolume, Right, 0x1F801D82);
}; };
struct CDVolume { struct CDVolume {
__declare_io_port_member_simple(SimpleVolume, Left, 0x1F801DB0); __declare_new_named_io_port(SimpleVolume, Left, 0x1F801DB0);
__declare_io_port_member_simple(SimpleVolume, Right, 0x1F801DB2); __declare_new_named_io_port(SimpleVolume, Right, 0x1F801DB2);
}; };
struct ExternalAudioInputVolume { struct ExternalAudioInputVolume {
__declare_io_port_member_simple(SimpleVolume, Left, 0x1F801DB4); __declare_new_named_io_port(SimpleVolume, Left, 0x1F801DB4);
__declare_io_port_member_simple(SimpleVolume, Right, 0x1F801DB6); __declare_new_named_io_port(SimpleVolume, Right, 0x1F801DB6);
}; };
struct Reverb { struct Reverb {
struct Volume { struct Volume {
__declare_io_port_member_simple(SimpleVolume, Left, 0x1F801D84); __declare_new_named_io_port(SimpleVolume, Left, 0x1F801D84);
__declare_io_port_member_simple(SimpleVolume, Right, 0x1F801D86); __declare_new_named_io_port(SimpleVolume, Right, 0x1F801D86);
}; };
__declare_io_port_member_simple(uint16_t, WorkAreaAdr, 0x1F801DA2); __declare_new_named_io_port(Adr, WorkAreaAdr, 0x1F801DA2);
}; };
__declare_io_port_global(ControlRegister, Control, 0x1F801DAA); __declare_new_io_port(ControlRegister, 0x1F801DAA);
__declare_io_port_global_simple(uint16_t, DataTransferControl, 0x1F801DAC); __declare_new_io_port(DataTransferControl, 0x1F801DAC);
__declare_io_port_global(PitchModFlags, PMON, 0x1F801D90); __declare_new_io_port(PMON, 0x1F801D90);
__declare_io_port_global(NoiseGenerator, NON, 0x1F801D94); __declare_new_io_port(NON, 0x1F801D94);
__declare_io_port_global(EchoOn, EON, 0x1F801D98); __declare_new_io_port(EON, 0x1F801D98);
__declare_io_port_global_array(struct Voice, Voice, 0x1F801C00, VoiceCount); __declare_new_io_port_array(Voice, 0x1F801C00, VoiceCount);
} }
} }
#endif //!__JABYENGINE_SPU_IO_HPP__ #endif //!__JABYENGINE_SPU_IO_HPP__

View File

@ -6,65 +6,66 @@ namespace JabyEngine {
namespace boot { namespace boot {
namespace SPU { namespace SPU {
using namespace JabyEngine; using namespace JabyEngine;
using namespace SPU_IO;
static void clear_main_volume() { static void clear_main_volume() {
static constexpr auto StartVol = SPU_IO::SweepVolume::with(SPU_IO::SweepVolume::VolumeEnable, SPU_IO::SweepVolume::Volume.with(I16_MAX >> 2)); static constexpr auto StartVol = SweepVolume_t::from(SweepVolume_t::VolumeEnable, SweepVolume_t::Volume.with(static_cast<int16_t>(I16_MAX >> 2)));
SPU_IO::MainVolume::Left.write({StartVol}); MainVolume::Left = *StartVol;
SPU_IO::MainVolume::Right.write({StartVol}); MainVolume::Right = *StartVol;
} }
static void clear_cd_and_ext_audio_volume() { static void clear_cd_and_ext_audio_volume() {
SPU_IO::CDVolume::Left.write(0); CDVolume::Left = 0;
SPU_IO::CDVolume::Right.write(0); CDVolume::Right = 0;
SPU_IO::ExternalAudioInputVolume::Left.write(0); ExternalAudioInputVolume::Left = 0;
SPU_IO::ExternalAudioInputVolume::Right.write(0); ExternalAudioInputVolume::Right = 0;
} }
static void clear_control_register() { static void clear_control_register() {
SPU_IO::Control.write(SPU_IO::ControlRegister()); ControlRegister = 0;
} }
static void clear_voice() { static void clear_voice() {
for(auto& voice : SPU_IO::Voice) { for(auto& voice : SPU_IO::Voice) {
voice.volumeLeft.write(SPU_IO::SweepVolume()); voice.volumeLeft = *SweepVolume_t();
voice.volumeRight.write(SPU_IO::SweepVolume()); voice.volumeRight = *SweepVolume_t();
voice.sampleRate.write(SPU_IO::SampleRate()); voice.sampleRate = *SampleRate_t();
voice.ad.write(SPU_IO::AD()); voice.ad = *AD_t();
voice.sr.write(SPU_IO::SR()); voice.sr = *SR_t();
voice.currentVolume.write(SPU_IO::SimpleVolume(0)); voice.currentVolume = 0;
voice.adr.write(0x200); voice.adr = 0x200;
voice.repeatAdr.write(0x200); voice.repeatAdr = 0x200;
} }
} }
static void clear_pmon() { static void clear_pmon() {
SPU_IO::PMON.write(SPU_IO::PitchModFlags()); SPU_IO::PMON = *PMON_t();
} }
static void clear_noise_and_echo() { static void clear_noise_and_echo() {
SPU_IO::NON.write(SPU_IO::NoiseGenerator()); SPU_IO::NON = *NON_t();
SPU_IO::EON.write(SPU_IO::EchoOn()); SPU_IO::EON = *EON_t();
} }
static void clear_reverb() { static void clear_reverb() {
SPU_IO::Reverb::Volume::Left.write(0); Reverb::Volume::Left = 0;
SPU_IO::Reverb::Volume::Right.write(0); Reverb::Volume::Right = 0;
SPU_IO::Reverb::WorkAreaAdr.write(0); Reverb::WorkAreaAdr = 0;
} }
static void setup_control_register() { static void setup_control_register() {
static constexpr auto SetupValue = SPU_IO::ControlRegister::with(SPU_IO::ControlRegister::Enable, SPU_IO::ControlRegister::Unmute, SPU_IO::ControlRegister::CDAudioEnable); static constexpr auto SetupValue = ControlRegister_t::from(ControlRegister_t::Enable, ControlRegister_t::Unmute, ControlRegister_t::CDAudioEnable);
SPU_IO::Control.write({SetupValue}); SPU_IO::ControlRegister = *SetupValue;
} }
static void setup_data_transfer_control() { static void setup_data_transfer_control() {
static constexpr uint16_t RequiredValue = (2 << 1); static constexpr uint16_t RequiredValue = (2 << 1);
SPU_IO::DataTransferControl.write(RequiredValue); DataTransferControl = RequiredValue;
} }
static void wait_voices() { static void wait_voices() {
@ -72,7 +73,7 @@ namespace JabyEngine {
try_again: try_again:
for(const auto& voice : SPU_IO::Voice) { for(const auto& voice : SPU_IO::Voice) {
if(voice.currentVolume.read() > Treshhold) { if(voice.currentVolume > Treshhold) {
goto try_again; goto try_again;
} }
} }