diff --git a/include/PSX/Auxiliary/io_class_helper.hpp b/include/PSX/Auxiliary/io_class_helper.hpp index bfe26e58..49139694 100644 --- a/include/PSX/Auxiliary/io_class_helper.hpp +++ b/include/PSX/Auxiliary/io_class_helper.hpp @@ -3,12 +3,14 @@ #include "bits.hpp" #define io_class__2option_map(option0, option1, bit_num) \ - constexpr void set_##option0() { \ + constexpr auto& set_##option0() { \ this->raw_value = bit::clear(this->raw_value, bit_num); \ + return *this;\ } \ \ - constexpr void set_##option1() { \ + constexpr auto& set_##option1() { \ this->raw_value = bit::set(this->raw_value, bit_num); \ + return *this; \ } #define io_class__2option_map_getter(type, option0, option1, name, bit_num) \ @@ -20,4 +22,9 @@ return static_cast(bit::is_set(this->raw_value, bit_num)); \ } +template +static constexpr void io_class__volatile_assign(volatile T& dst, const T& src) { + dst.raw_value = src.raw_value; +} + #endif //!__JABYENGINE_IO_CLASS_HELPER_HPP__ \ No newline at end of file diff --git a/include/PSX/SPU/SPU_Ports.hpp b/include/PSX/SPU/SPU_Ports.hpp index dea0597a..4aeea711 100644 --- a/include/PSX/SPU/SPU_Ports.hpp +++ b/include/PSX/SPU/SPU_Ports.hpp @@ -1,14 +1,20 @@ #ifndef __JABYENGINE_SPU_PORTS_HPP__ #define __JABYENGINE_SPU_PORTS_HPP__ #include "../Auxiliary/io_class_helper.hpp" +#include "../../limits.h" namespace SPU { - //SampleRate is defined as 4096 == 44100Hz + enum Mode { + Linear = 0, + Exponential = 1, + }; + struct __no_align SampleRate { uint16_t raw_value = 0; static constexpr SampleRate from_HZ(long double freq) { + //SampleRate is defined as 4096 == 44100Hz constexpr long double Base = (4096.0 / 44100.0); return static_cast(((freq*4096.0)/44100.0)); @@ -16,10 +22,8 @@ namespace SPU { }; struct __no_align SweepVolume { - enum Type { - Linear = 0, - Exponential = 1, - }; + static constexpr int16_t VolumeLevelStep100 = (I16_MAX/100); + static constexpr int16_t VolumeLevelStep1000 = (I16_MAX/1000); enum Direction { Increase = 0, @@ -31,12 +35,17 @@ namespace SPU { Negative = 1, }; - uint16_t raw_value = 0; + int16_t raw_value = 0; constexpr SweepVolume() = default; - constexpr void set_volume(int16_t volume) { - this->raw_value = bit::value::set_normalized(this->raw_value, bit::cast(volume >> 1), __start_end_bit2_start_length(0, 14)); + static constexpr int16_t VolumeLevelPercent(double percent) { + return static_cast((I16_MAX/100.0)*percent); + } + + constexpr auto& set_volume(int16_t volume) { + this->raw_value = bit::value::set_normalized(this->raw_value, static_cast(volume >> 1), __start_end_bit2_start_length(0, 14)); + return *this; } constexpr int16_t get_volume() const { @@ -44,14 +53,15 @@ namespace SPU { } io_class__2option_map(volume_mode, sweep_mode, 15); - io_class__2option_map_getter(Type, linear_sweep_mode, exponential_sweep_mode, sweep_mode_type, 14); + io_class__2option_map_getter(Mode, linear_sweep_mode, exponential_sweep_mode, sweep_mode_type, 14); io_class__2option_map_getter(Direction, increase_sweep_mode, decrease_sweep_mode, sweep_mode_direction, 13); io_class__2option_map_getter(Phase, positive_sweep_phase, negative_sweep_phase, sweep_phase, 12); //Uses only 5bit of shift (0..1F (slow..fast)) - constexpr void set_sweep_shift(uint8_5b shift) { - const uint16_t crop_value = static_cast(shift); //(bit::value::crop_value(shift, 5)); + constexpr auto& set_sweep_shift(uint8_5b shift) { + const int16_t crop_value = static_cast(shift); //(bit::value::crop_value(shift, 5)); this->raw_value = bit::value::set_normalized(this->raw_value, crop_value, __start_end_bit2_start_length(2, 6)); + return *this; } constexpr uint8_5b get_sweep_shift() const { @@ -59,8 +69,9 @@ namespace SPU { } //0..3 maps to => +7, +6, +5, +4 or -8, -7, -6, -5 - constexpr void set_sweep_step(uint8_2b step) { - this->raw_value = bit::value::set_normalized(this->raw_value, static_cast(step), __start_end_bit2_start_length(0, 1)); + constexpr auto& set_sweep_step(uint8_2b step) { + this->raw_value = bit::value::set_normalized(this->raw_value, static_cast(step), __start_end_bit2_start_length(0, 1)); + return *this; } constexpr uint8_2b get_sweep_step() const { @@ -68,15 +79,26 @@ namespace SPU { } }; + struct __no_align ADSR { + uint32_t raw_value = 0; + }; + struct __no_align Voice { + static constexpr size_t Count = 24; + static inline __always_inline auto& Channel = reinterpret_cast(*reinterpret_cast(0x1f801c00)); + SweepVolume volumeLeft; SweepVolume volumeRight; - /*VolatileValue sampleRate; - VolatileValue adr; - VolatileValue ad; //0x...08: 15 - 0 - VolatileValue sr; //0x...0A: 31 - 16 - VolatileValue currentVolume; //Not used - VolatileValue repeatAdr;*/ + SampleRate sampleRate; + uint16_t adr; + ADSR adsr; + SweepVolume currentVolume; //Not used + uint16_t repeatAdr; + }; + + struct MainVolume { + static inline __always_inline auto& Left = *reinterpret_cast(0x1F801D80); + static inline __always_inline auto& Right = *reinterpret_cast(0x1F801D82); }; } diff --git a/include/limits.h b/include/limits.h new file mode 100644 index 00000000..49077b0f --- /dev/null +++ b/include/limits.h @@ -0,0 +1,36 @@ +#ifndef __LIMITS__H +#define __LIMITS__H + +#define CHAR_BIT 8 +#define SCHAR_MIN (-128) +#define SCHAR_MAX 127 +#define UCHAR_MAX 0xff + +#define CHAR_MIN SCHAR_MIN +#define CHAR_MAX SCHAR_MAX + +#define SHRT_MIN (-32768) +#define SHRT_MAX 32767 +#define USHRT_MAX 0xffff + +#define INT_MIN (-2147483647 - 1) +#define INT_MAX 2147483647 +#define UINT_MAX 0xffffffff + +#define LONG_MIN INT_MIN +#define LONG_MAX INT_MAX +#define ULONG_MAX UINT_MAX + +#define I8_MIN SCHAR_MIN +#define I8_MAX SCHAR_MAX +#define UI8_MAX UCHAR_MAX + +#define I16_MIN SHRT_MIN +#define I16_MAX SHRT_MAX +#define UI16_MAX USHRT_MAX + +#define I32_MIN INT_MIN +#define I32_MAX INT_MAX +#define UI32_MAX UINT_MAX + +#endif //!__LIMITS__H \ No newline at end of file diff --git a/src/Library/Makefile b/src/Library/Makefile index 1cc481c8..4965ce0b 100644 --- a/src/Library/Makefile +++ b/src/Library/Makefile @@ -2,6 +2,7 @@ ARTIFACT = libJabyEngine BUILD_DIR = bin CCFLAGS += -I../../include +CCFLAGS += -save-temps=obj include ../../lib/Wildcard.mk SRCS = $(call rwildcard, src, c cpp) diff --git a/src/Library/src/BootLoader/start.cpp b/src/Library/src/BootLoader/start.cpp index da4c3b94..3116a511 100644 --- a/src/Library/src/BootLoader/start.cpp +++ b/src/Library/src/BootLoader/start.cpp @@ -2,10 +2,35 @@ #include namespace JabyEngine { - void start() { - SPU::SweepVolume b; + namespace SPU { + static void clear_voice(::SPU::Voice& voice) { + io_class__volatile_assign(voice.volumeLeft, ::SPU::SweepVolume()); + io_class__volatile_assign(voice.volumeRight, ::SPU::SweepVolume()); - b.set_volume(9); - printf("Hello Planschbecken\n"); + io_class__volatile_assign(voice.sampleRate, ::SPU::SampleRate::from_HZ(0.0)); + io_class__volatile_assign(voice.adsr, ::SPU::ADSR()); + io_class__volatile_assign(voice.currentVolume, ::SPU::SweepVolume()); + + voice.adr = 0x200; + voice.repeatAdr = 0x200; + } + + static void setup() { + static constexpr auto StartVol = ::SPU::SweepVolume().set_volume_mode().set_volume(::SPU::SweepVolume::VolumeLevelPercent(50.0)); + + io_class__volatile_assign(::SPU::MainVolume::Left, StartVol); + io_class__volatile_assign(::SPU::MainVolume::Right, StartVol); + + for(auto& voice : ::SPU::Voice::Channel) { + clear_voice(voice); + } + } + } + + void start() { + printf("Hello Planschbecken\n"); + + SPU::setup(); + printf("Setup done!\n"); } } \ No newline at end of file