Clear MainVolume and improve port code

This commit is contained in:
jaby 2022-09-02 11:02:56 +02:00
parent 5c15b66873
commit dac33a77c6
3 changed files with 95 additions and 70 deletions

View File

@ -52,13 +52,13 @@ public:
template<typename S>
constexpr IOPort<T>& clear_bit(S bit) {
this->value = bit::set(this->value, static_cast<size_t>(bit));
this->value = bit::clear(this->value, static_cast<size_t>(bit));
return *this;
}
template<typename S>
constexpr void clear_bit(S bit) volatile {
this->value = bit::set(this->value, static_cast<size_t>(bit));
this->value = bit::clear(this->value, static_cast<size_t>(bit));
}
template<typename S>
@ -164,4 +164,10 @@ static constexpr uintptr_t IO_Base_Mask = 0xF0000000;
static constexpr uintptr_t IO_Base_Adr = 0x10000000;
#define __declare_io_port_global(type, name, adr) static __always_inline auto& name = *reinterpret_cast<IOPort<type>*>((IO_Base_Adr + (adr & ~IO_Base_Mask)))
#define __io_port_inherit(name) \
using IOPort::operator=; \
constexpr name() = default; \
constexpr name(IOPort value) : IOPort(value) { \
}
#endif //!__JABYENGINE_IOPORT_HPP__

View File

@ -4,83 +4,92 @@
#include <limits.h>
namespace SPU {
enum struct SweepMode {
Linear = 0,
Exponential = 1,
};
namespace Port {
enum struct SweepMode {
Linear = 0,
Exponential = 1,
};
enum struct SweepDirection {
Increase = 0,
Decrease = 1,
};
enum struct SweepDirection {
Increase = 0,
Decrease = 1,
};
enum struct SweepPhase {
Posititve = 0,
Negative = 1,
};
enum struct SweepPhase {
Posititve = 0,
Negative = 1,
};
//0..0x1F = Fast..Slow
typedef uint8_t SweepShift;
//0..0x1F = Fast..Slow
typedef uint8_t SweepShift;
//0..3 = +7, +6, +5, +4 or -6, -7, -6, -5
typedef uint8_t SweepStep;
//0..3 = +7, +6, +5, +4 or -6, -7, -6, -5
typedef uint8_t SweepStep;
struct __no_align SampleRate : public IOPort<uint16_t> {
using IOPort<uint16_t>::IOPort;
struct __no_align SampleRate : public IOPort<uint16_t> {
__io_port_inherit(SampleRate);
static constexpr SampleRate from_HZ(double freq) {
//4096 == 44100Hz
constexpr double Base = (4096.0 / 44100.0);
static constexpr SampleRate from_HZ(double freq) {
//4096 == 44100Hz
constexpr double Base = (4096.0 / 44100.0);
return static_cast<uint16_t>((freq*Base));
return IOPort(static_cast<uint16_t>((freq*Base)));
}
};
struct __no_align SweepVolume : public IOPort<int16_t> {
__io_port_inherit(SweepVolume);
// For Volume Mode
static constexpr Bit<int16_t> ModeBit = 15; // 0 Volume Mode; 1 Sweep Mode
static constexpr BitRange<int16_t> VolumeRange = BitRange<int16_t>::from_to(0, 14);
// For Sweep Mode
static constexpr Bit<SweepMode> SweepModeBit = 14;
static constexpr Bit<SweepDirection> SweepDirectionBit = 13;
static constexpr Bit<SweepPhase> SweepPhaseBit = 12;
static constexpr BitRange<SweepShift> SweepShiftRange = BitRange<SweepShift>::from_to(2, 6);
static constexpr BitRange<SweepStep> SweepStepRange = BitRange<SweepStep>::from_to(0, 1);
};
struct __no_align SR : public IOPort<uint16_t> {
static constexpr Bit<SweepMode> SustainModeBit = (31 - 16);
static constexpr Bit<SweepDirection> SustainDirectionBit = (30 - 16);
static constexpr BitRange<SweepShift> SustainShiftRange = BitRange<SweepShift>::from_to((24 - 16), (28 - 16));
static constexpr BitRange<SweepStep> SustainStepRange = BitRange<SweepStep>::from_to((22 - 16), (23 - 16));
static constexpr Bit<SweepMode> ReleaseModeBit = (21 - 16);
static constexpr BitRange<SweepShift> ReleaseShiftRange = BitRange<SweepShift>::from_to((16 - 16), (20 - 16));
};
struct __no_align AD : public IOPort<uint16_t> {
static constexpr Bit<SweepMode> AttackModeBit = 15;
static constexpr BitRange<SweepShift> AttackShiftRange = BitRange<SweepShift>::from_to(10, 14);
static constexpr BitRange<SweepStep> AttackStepRange = BitRange<SweepStep>::from_to(8, 9);
static constexpr BitRange<SweepShift> DecayShiftRange = BitRange<SweepShift>::from_to(4, 7);
static constexpr BitRange<uint16_t> SustainLevelRange = BitRange<uint16_t>::from_to(0, 3);
};
struct __no_align Voice {
SweepVolume volumeLeft;
SweepVolume volumeRight;
SampleRate sampleRate;
IOPort<uint16_t> adr;
IOPort<AD> ad;
IOPort<SR> sr;
IOPort<SweepVolume> currentVolume; //Not used
IOPort<uint16_t> repeatAdr;
};
namespace Key {
__declare_io_port_global(ubus32_t, on, 0x1F801D88);
__declare_io_port_global(ubus32_t, off, 0x1F801D8C);
__declare_io_port_global(ubus32_t, status, 0x1F801D9C);
}
};
struct __no_align SweepVolume : public IOPort<int16_t> {
// For Volume Mode
static constexpr Bit<int16_t> ModeBit = 15; // 0 Volume Mode; 1 Sweep Mode
static constexpr BitRange<int16_t> VolumeRange = BitRange<int16_t>::from_to(0, 14);
// For Sweep Mode
static constexpr Bit<SweepMode> SweepModeBit = 14;
static constexpr Bit<SweepDirection> SweepDirectionBit = 13;
static constexpr Bit<SweepPhase> SweepPhaseBit = 12;
static constexpr BitRange<SweepShift> SweepShiftRange = BitRange<SweepShift>::from_to(2, 6);
static constexpr BitRange<SweepStep> SweepStepRange = BitRange<SweepStep>::from_to(0, 1);
};
struct __no_align SR : public IOPort<uint16_t> {
static constexpr Bit<SweepMode> SustainModeBit = (31 - 16);
static constexpr Bit<SweepDirection> SustainDirectionBit = (30 - 16);
static constexpr BitRange<SweepShift> SustainShiftRange = BitRange<SweepShift>::from_to((24 - 16), (28 - 16));
static constexpr BitRange<SweepStep> SustainStepRange = BitRange<SweepStep>::from_to((22 - 16), (23 - 16));
static constexpr Bit<SweepMode> ReleaseModeBit = (21 - 16);
static constexpr BitRange<SweepShift> ReleaseShiftRange = BitRange<SweepShift>::from_to((16 - 16), (20 - 16));
};
struct __no_align AD : public IOPort<uint16_t> {
static constexpr Bit<SweepMode> AttackModeBit = 15;
static constexpr BitRange<SweepShift> AttackShiftRange = BitRange<SweepShift>::from_to(10, 14);
static constexpr BitRange<SweepStep> AttackStepRange = BitRange<SweepStep>::from_to(8, 9);
static constexpr BitRange<SweepShift> DecayShiftRange = BitRange<SweepShift>::from_to(4, 7);
static constexpr BitRange<uint16_t> SustainLevelRange = BitRange<uint16_t>::from_to(0, 3);
};
struct __no_align Voice {
SweepVolume volumeLeft;
SweepVolume volumeRight;
SampleRate sampleRate;
IOPort<uint16_t> adr;
IOPort<AD> ad;
IOPort<SR> sr;
IOPort<SweepVolume> currentVolume; //Not used
IOPort<uint16_t> repeatAdr;
};
namespace Key {
__declare_io_port_global(ubus32_t, on, 0x1F801D88);
__declare_io_port_global(ubus32_t, off, 0x1F801D8C);
__declare_io_port_global(ubus32_t, status, 0x1F801D9C);
namespace MainVolume {
__declare_io_port_global(SweepVolume, left, 0x1F801D80);
__declare_io_port_global(SweepVolume, right, 0x1F801D82);
}
}
}

View File

@ -2,11 +2,21 @@
#include <PSX/System/IOPorts/IOPort.hpp>
namespace SPU {
using namespace Port;
static void clear_key() {
Key::off.write(UI32_MAX);
}
static void clear_main_volume() {
static constexpr auto StartVol = SweepVolume().clear_bit(SweepVolume::ModeBit).set_value(static_cast<int16_t>(I16_MAX >> 2), SweepVolume::VolumeRange);
MainVolume::left.write(StartVol);
MainVolume::right.write(StartVol);
}
void setup() {
clear_key();
clear_main_volume();
}
}