Skip to content

Commit 2b88c71

Browse files
committed
Convert more magic numbers to enums
1 parent 2bfda78 commit 2b88c71

File tree

2 files changed

+60
-41
lines changed

2 files changed

+60
-41
lines changed

src/spu/interface.h

Lines changed: 32 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -83,26 +83,43 @@ class impl final : public SPUInterface {
8383
void waitForGoal(uint32_t goal) override { m_audioOut.waitForGoal(goal); }
8484

8585
private:
86+
struct ADSRFlags {
87+
enum : uint16_t {
88+
AttackMode = 1 << 15, // 15 0=Linear, 1=Exponential
89+
AttackShiftMask = 0x7c00, // 14-10 0..1Fh = Fast..Slow
90+
AttackStepMask = 0x300, // 9-8 0..3 = "+7,+6,+5,+4"
91+
DecayShiftMask = 0xf0, // 7-4 0..0Fh = Fast..Slow
92+
SustainLevelMask = 0xf, // 3-0 0..0Fh ;Level=(N+1)*800h
93+
// Flags for upper 16-bits of reg, shifted right 16-bits
94+
SustainMode = 1 << 15, // 31 0=Linear, 1=Exponential
95+
SustainDirection = 1 << 14, // 30 0=Increase, 1=Decrease (until Key OFF flag)
96+
SustainShiftMask = 0x1f00, // 28-24 0..1Fh = Fast..Slow
97+
SustainStepMask = 0xc0, // 23-22 0..3 = "+7,+6,+5,+4" or "-8,-7,-6,-5") (inc/dec)
98+
ReleaseMode = 1 << 5, // 21 0=Linear, 1=Exponential
99+
ReleaseShiftMask = 0x1f // 20-16 0..1Fh = Fast..Slow
100+
};
101+
};
102+
86103
struct ControlFlags {
87104
enum : uint16_t {
88-
CDAudioEnable = 1 << 0, // 0 0x0001 (0=Off, 1=On) (for CD-DA and XA-ADPCM)
89-
ExternalAudioEnable = 1 << 1, // 1 0x0002 (0=Off, 1=On)
90-
CDReverbEnable = 1 << 2, // 2 0x0004 (0=Off, 1=On) (for CD-DA and XA-ADPCM)
91-
ExternalReverbEnable = 1 << 3, // 3 0x0008 (0=Off, 1=On)
92-
RAMTransferMode = 0x0030, // 5-4 0x0030 RAM Transfer Mode (0=Stop, 1=ManualWrite, 2=DMAwrite, 3=DMAread)
93-
IRQEnable = 1 << 6, // 6 0x0040 (0=Disabled/Acknowledge, 1=Enabled; only when Bit15=1)
94-
ReverbMasterEnable = 1 << 7, // 7 0x0080 (0=Disabled, 1=Enabled)
95-
NoiseStep = 0x0300, // 9-8 0x0300 Noise Frequency Step (0..03h = Step "4,5,6,7")
96-
NoiseShift = 0x3c00, // 13-10 0x3c00 Noise Frequency Shift (0..0Fh = Low .. High Frequency)
97-
Mute = 1 << 14, // 14 0x4000 (0=Mute, 1=Unmute)
98-
Enable = 1 << 15 // 15 0x8000 (0=Off, 1=On)
105+
CDAudioEnable = 1 << 0, // 0 0=Off, 1=On (for CD-DA and XA-ADPCM)
106+
ExternalAudioEnable = 1 << 1, // 1 0=Off, 1=On
107+
CDReverbEnable = 1 << 2, // 20=Off, 1=On (for CD-DA and XA-ADPCM)
108+
ExternalReverbEnable = 1 << 3, // 3 0=Off, 1=On
109+
RAMTransferModeMask = 0x0030, // 5-4 0=Stop, 1=ManualWrite, 2=DMAwrite, 3=DMAread
110+
IRQEnable = 1 << 6, // 6 0=Disabled/Acknowledge, 1=Enabled; only when Bit15=1
111+
ReverbMasterEnable = 1 << 7, // 7 0=Disabled, 1=Enabled
112+
NoiseStepMask = 0x0300, // 9-8 0..03h = Step "4,5,6,7"
113+
NoiseShiftMask = 0x3c00, // 13-10 0..0Fh = Low .. High Frequency
114+
Mute = 1 << 14, // 14 0=Mute, 1=Unmute
115+
Enable = 1 << 15 // 15 0=Off, 1=On
99116
};
100117
};
101118

102119
struct StatusFlags {
103120
enum : uint16_t {
104-
SPUMode = 0x3f, // 5-0 Current SPU Mode(same as SPUCNT.Bit5 - 0, but, applied a bit delayed)
105-
IRQFlag = 1 << 6, // 6 0x0040 IRQ9 Flag (0=No, 1=Interrupt Request)
121+
SPUModeMask = 0x3f, // 5-0 Current SPU Mode(same as SPUCNT.Bit5 - 0, but, applied a bit delayed)
122+
IRQFlag = 1 << 6, // 6 IRQ9 Flag (0=No, 1=Interrupt Request)
106123
DMARWRequest = 1 << 7, // 7 Data Transfer DMA Read/Write Request seems to be same as SPUCNT.Bit5
107124
DMAWriteRequest = 1 << 8, // 8 Data Transfer DMA Write Request (0=No, 1=Yes)
108125
DMAReadRequest = 1 << 9, // 9 Data Transfer DMA Read Request (0=No, 1=Yes)
@@ -119,8 +136,8 @@ class impl final : public SPUInterface {
119136
SweepDirection = 1 << 13, // 13 0=Increase, 1=Decrease
120137
SweepPhase = 1 << 12, // 12 0=Positive, 1=Negative
121138
Unknown = 0xf80, // 7-11 Not used? (should be zero)
122-
SweepShift = 0x7c, // 6-2 0..1Fh = Fast..Slow
123-
SweepStep = 0x3 // 1-0 0..3 = "+7,+6,+5,+4" or "-8,-7,-6,-5") (inc/dec)
139+
SweepShiftMask = 0x7c, // 6-2 0..1Fh = Fast..Slow
140+
SweepStepMask = 0x3 // 1-0 0..3 = "+7,+6,+5,+4" or "-8,-7,-6,-5") (inc/dec)
124141

125142
};
126143
};

src/spu/registers.cc

Lines changed: 28 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -90,16 +90,17 @@ void PCSX::SPU::impl::writeRegister(uint32_t reg, uint16_t val) {
9090
break;
9191
case 8: { // Attack/Decay/Sustain/Release (ADSR)
9292
//---------------------------------------------//
93-
s_chan[ch].ADSRX.get<exAttackModeExp>().value = (val & 0x8000) ? 1 : 0;
94-
s_chan[ch].ADSRX.get<exAttackRate>().value = (val >> 8) & 0x007f;
95-
s_chan[ch].ADSRX.get<exDecayRate>().value = (val >> 4) & 0x000f;
96-
s_chan[ch].ADSRX.get<exSustainLevel>().value = val & 0x000f;
93+
s_chan[ch].ADSRX.get<exAttackModeExp>().value = (val & ADSRFlags::AttackMode) ? 1 : 0;
94+
s_chan[ch].ADSRX.get<exAttackRate>().value =
95+
(val & (ADSRFlags::AttackShiftMask | ADSRFlags::AttackStepMask)) >> 8;
96+
s_chan[ch].ADSRX.get<exDecayRate>().value = (val & ADSRFlags::DecayShiftMask) >> 4;
97+
s_chan[ch].ADSRX.get<exSustainLevel>().value = val & ADSRFlags::SustainLevelMask;
9798
//---------------------------------------------// stuff below is only for debug mode
9899

99-
s_chan[ch].ADSR.get<AttackModeExp>().value = (val & 0x8000) ? 1 : 0; // 0x007f
100+
s_chan[ch].ADSR.get<AttackModeExp>().value = (val & ADSRFlags::AttackMode) ? 1 : 0;
100101

101-
uint32_t lx = (((val >> 8) & 0x007f) >> 2); // attack time to run from 0 to 100% volume
102-
lx = std::min(31U, lx); // no overflow on shift!
102+
uint32_t lx = (val & ADSRFlags::AttackShiftMask) >> 10; // attack time to run from 0 to 100% volume
103+
lx = std::min(31U, lx); // no overflow on shift!
103104
if (lx) {
104105
lx = (1 << lx);
105106
if (lx < 2147483) {
@@ -114,10 +115,10 @@ void PCSX::SPU::impl::writeRegister(uint32_t reg, uint16_t val) {
114115
s_chan[ch].ADSR.get<AttackTime>().value = lx;
115116

116117
// our adsr vol runs from 0 to 1024, so scale the sustain level
117-
s_chan[ch].ADSR.get<SustainLevel>().value = (1024 * (val & 0xf)) / 15;
118+
s_chan[ch].ADSR.get<SustainLevel>().value = (1024 * (val & ADSRFlags::SustainLevelMask)) / 15;
118119

119-
lx = (val >> 4) & 0x000f; // decay:
120-
if (lx) // our const decay value is time it takes from 100% to 0% of volume
120+
lx = (val & ADSRFlags::DecayShiftMask) >> 4; // decay:
121+
if (lx) // our const decay value is time it takes from 100% to 0% of volume
121122
{
122123
lx = ((1 << (lx)) * DECAY_MS) / 10000L;
123124
if (!lx) {
@@ -131,21 +132,22 @@ void PCSX::SPU::impl::writeRegister(uint32_t reg, uint16_t val) {
131132
//------------------------------------------------// adsr times with pre-calcs
132133
case 10: {
133134
//----------------------------------------------//
134-
s_chan[ch].ADSRX.get<exSustainModeExp>().value = (val & 0x8000) ? 1 : 0;
135-
s_chan[ch].ADSRX.get<exSustainIncrease>().value = (val & 0x4000) ? 0 : 1;
136-
s_chan[ch].ADSRX.get<exSustainRate>().value = (val >> 6) & 0x007f;
137-
s_chan[ch].ADSRX.get<exReleaseModeExp>().value = (val & 0x0020) ? 1 : 0;
138-
s_chan[ch].ADSRX.get<exReleaseRate>().value = val & 0x001f;
135+
s_chan[ch].ADSRX.get<exSustainModeExp>().value = (val & ADSRFlags::SustainMode) ? 1 : 0;
136+
s_chan[ch].ADSRX.get<exSustainIncrease>().value = (val & ADSRFlags::SustainDirection) ? 0 : 1;
137+
s_chan[ch].ADSRX.get<exSustainRate>().value =
138+
(val & (ADSRFlags::SustainShiftMask | ADSRFlags::SustainStepMask)) >> 6;
139+
s_chan[ch].ADSRX.get<exReleaseModeExp>().value = (val & ADSRFlags::ReleaseMode) ? 1 : 0;
140+
s_chan[ch].ADSRX.get<exReleaseRate>().value = val & ADSRFlags::ReleaseShiftMask;
139141
//----------------------------------------------// stuff below is only for debug mode
140142

141-
s_chan[ch].ADSR.get<SustainModeExp>().value = (val & 0x8000) ? 1 : 0;
142-
s_chan[ch].ADSR.get<ReleaseModeExp>().value = (val & 0x0020) ? 1 : 0;
143+
s_chan[ch].ADSR.get<SustainModeExp>().value = (val & ADSRFlags::SustainMode) ? 1 : 0;
144+
s_chan[ch].ADSR.get<ReleaseModeExp>().value = (val & ADSRFlags::ReleaseMode) ? 1 : 0;
143145

144-
uint32_t lx = ((((val >> 6) & 0x007f) >> 2)); // sustain time... often very high
145-
lx = std::min(31U, lx); // values are used to hold the volume
146-
if (lx) // until a sound stop occurs
147-
{ // the highest value we reach (due to
148-
lx = (1 << lx); // overflow checking) is:
146+
uint32_t lx = (val & ADSRFlags::SustainShiftMask) >> 8; // sustain time... often very high
147+
lx = std::min(31U, lx); // values are used to hold the volume
148+
if (lx) // until a sound stop occurs
149+
{ // the highest value we reach (due to
150+
lx = (1 << lx); // overflow checking) is:
149151
if (lx < 2147483) {
150152
lx = (lx * SUSTAIN_MS) / 10000L; // 94704 seconds = 1578 minutes = 26 hours...
151153
} else {
@@ -157,7 +159,7 @@ void PCSX::SPU::impl::writeRegister(uint32_t reg, uint16_t val) {
157159
}
158160
s_chan[ch].ADSR.get<SustainTime>().value = lx;
159161

160-
lx = (val & 0x001f);
162+
lx = (val & ADSRFlags::ReleaseShiftMask);
161163
s_chan[ch].ADSR.get<ReleaseVal>().value = lx;
162164
if (lx) // release time from 100% to 0%
163165
{ // note: the release time will be
@@ -212,7 +214,7 @@ void PCSX::SPU::impl::writeRegister(uint32_t reg, uint16_t val) {
212214

213215
case H_SPUctrl:
214216
spuCtrl = val;
215-
m_noiseClock = (spuCtrl & (ControlFlags::NoiseShift | ControlFlags::NoiseStep)) >> 8;
217+
m_noiseClock = (spuCtrl & (ControlFlags::NoiseShiftMask | ControlFlags::NoiseStepMask)) >> 8;
216218
break;
217219

218220
case H_SPUstat:
@@ -472,7 +474,7 @@ uint16_t PCSX::SPU::impl::readRegister(uint32_t reg) {
472474
return spuCtrl;
473475

474476
case H_SPUstat:
475-
return (spuStat & ~StatusFlags::SPUMode) | (spuCtrl & StatusFlags::SPUMode);
477+
return (spuStat & ~StatusFlags::SPUModeMask) | (spuCtrl & StatusFlags::SPUModeMask);
476478

477479
case H_SPUaddr:
478480
return (uint16_t)(spuAddr >> 3);
@@ -557,7 +559,7 @@ void PCSX::SPU::impl::SetVolumeL(uint8_t ch, int16_t vol) // LEFT VOLUME
557559
}
558560
if (vol & VolumeFlags::SweepPhase) {
559561
// Negative Phase
560-
vol ^= 0xffff; // -> mmm... phase inverted? have to investigate this
562+
vol ^= 0xffff; // -> mmm... phase inverted? have to investigate this
561563
}
562564
vol = ((vol & 0x7f) + 1) / 2; // -> sweep: 0..127 -> 0..64
563565
vol += vol / (2 * sInc); // -> HACK: we don't sweep right now, so we just raise/lower the volume by the half!

0 commit comments

Comments
 (0)