jabyengine/src/Library/src/BootLoader/spu_boot.cpp

102 lines
3.2 KiB
C++

#include <PSX/System/IOPorts/spu_io.hpp>
#include <stdio.h>
#include <limits.h>
namespace JabyEngine {
namespace boot {
namespace SPU {
using namespace JabyEngine;
using namespace SPU_IO;
static void clear_main_volume() {
static constexpr auto StartVol = SweepVolume_t::from(SweepVolume_t::VolumeEnable, SweepVolume_t::Volume.with(static_cast<int16_t>(I16_MAX >> 2)));
MainVolume::Left = StartVol;
MainVolume::Right = StartVol;
}
static void clear_cd_and_ext_audio_volume() {
CDVolume::Left = 0;
CDVolume::Right = 0;
ExternalAudioInputVolume::Left = 0;
ExternalAudioInputVolume::Right = 0;
}
static void clear_control_register() {
ControlRegister = 0;
}
static void clear_voice() {
for(auto& voice : SPU_IO::Voice) {
voice.volumeLeft = SweepVolume_t();
voice.volumeRight = SweepVolume_t();
voice.sampleRate = SampleRate_t();
voice.ad = AD_t();
voice.sr = SR_t();
voice.currentVolume = 0;
voice.adr = 0x200;
voice.repeatAdr = 0x200;
}
}
static void clear_pmon() {
SPU_IO::PMON = PMON_t();
}
static void clear_noise_and_echo() {
SPU_IO::NON = NON_t();
SPU_IO::EON = EON_t();
}
static void clear_reverb() {
Reverb::Volume::Left = 0;
Reverb::Volume::Right = 0;
Reverb::WorkAreaAdr = 0;
}
static void setup_control_register() {
static constexpr auto SetupValue = ControlRegister_t::from(ControlRegister_t::Enable, ControlRegister_t::Unmute, ControlRegister_t::CDAudioEnable);
SPU_IO::ControlRegister = SetupValue;
}
static void setup_data_transfer_control() {
static constexpr uint16_t RequiredValue = (2 << 1);
DataTransferControl = RequiredValue;
}
static void wait_voices() {
static constexpr int16_t Treshhold = (I16_MAX*0.03);
try_again:
for(const auto& voice : SPU_IO::Voice) {
if(voice.currentVolume > Treshhold) {
goto try_again;
}
}
}
void stop_voices() {
SPU_IO::Key::Off.write({UI32_MAX});
}
void setup() {
wait_voices();
clear_main_volume();
clear_cd_and_ext_audio_volume();
clear_control_register();
clear_voice();
clear_pmon();
clear_noise_and_echo();
clear_reverb();
setup_data_transfer_control();
setup_control_register();
}
}
}
}