diff --git a/libgambatte/src/sound/duty_unit.cpp b/libgambatte/src/sound/duty_unit.cpp index 0c9480c7..9401dfe8 100644 --- a/libgambatte/src/sound/duty_unit.cpp +++ b/libgambatte/src/sound/duty_unit.cpp @@ -21,7 +21,6 @@ static inline bool toOutState(const unsigned duty, const unsigned pos) { static const unsigned char duties[4] = { 0x80, 0x81, 0xE1, 0x7E }; - return duties[duty] >> pos & 1; } @@ -31,6 +30,16 @@ static inline unsigned toPeriod(const unsigned freq) { namespace gambatte { +DutyUnit::DutyUnit() +: nextPosUpdate(COUNTER_DISABLED) +, period(4096) +, pos(0) +, duty(0) +, high(false) +, enableEvents(true) +{ +} + void DutyUnit::updatePos(const unsigned long cc) { if (cc >= nextPosUpdate) { const unsigned long inc = (cc - nextPosUpdate) / period + 1; @@ -96,15 +105,6 @@ void DutyUnit::nr4Change(const unsigned newNr4, const unsigned long cc) { } } -DutyUnit::DutyUnit() : -nextPosUpdate(COUNTER_DISABLED), -period(4096), -pos(0), -duty(0), -high(false), -enableEvents(true) -{} - void DutyUnit::reset() { pos = 0; high = toOutState(duty, pos); @@ -119,7 +119,8 @@ void DutyUnit::saveState(SaveState::SPU::Duty &dstate, const unsigned long cc) { dstate.pos = pos; } -void DutyUnit::loadState(const SaveState::SPU::Duty &dstate, const unsigned nr1, const unsigned nr4, const unsigned long cc) { +void DutyUnit::loadState(const SaveState::SPU::Duty &dstate, + const unsigned nr1, const unsigned nr4, const unsigned long cc) { nextPosUpdate = std::max(dstate.nextPosUpdate, cc); pos = dstate.pos & 7; setDuty(nr1); diff --git a/libgambatte/src/sound/duty_unit.h b/libgambatte/src/sound/duty_unit.h index d0c61a02..eb8e871c 100644 --- a/libgambatte/src/sound/duty_unit.h +++ b/libgambatte/src/sound/duty_unit.h @@ -26,6 +26,25 @@ namespace gambatte { class DutyUnit : public SoundUnit { +public: + DutyUnit(); + virtual void event(); + bool isHighState() const { return high; } + void nr1Change(unsigned newNr1, unsigned long cc); + void nr3Change(unsigned newNr3, unsigned long cc); + void nr4Change(unsigned newNr4, unsigned long cc); + void reset(); + void saveState(SaveState::SPU::Duty &dstate, unsigned long cc); + void loadState(const SaveState::SPU::Duty &dstate, unsigned nr1, unsigned nr4, unsigned long cc); + virtual void resetCounters(unsigned long oldCc); + void killCounter(); + void reviveCounter(unsigned long cc); + + //intended for use by SweepUnit only. + unsigned getFreq() const { return 2048 - (period >> 1); } + void setFreq(unsigned newFreq, unsigned long cc); + +private: unsigned long nextPosUpdate; unsigned short period; unsigned char pos; @@ -36,31 +55,13 @@ class DutyUnit : public SoundUnit { void setCounter(); void setDuty(unsigned nr1); void updatePos(unsigned long cc); - -public: - DutyUnit(); - void event(); - bool isHighState() const { return high; } - void nr1Change(unsigned newNr1, unsigned long cc); - void nr3Change(unsigned newNr3, unsigned long cc); - void nr4Change(unsigned newNr4, unsigned long cc); - void reset(); - void saveState(SaveState::SPU::Duty &dstate, unsigned long cc); - void loadState(const SaveState::SPU::Duty &dstate, unsigned nr1, unsigned nr4, unsigned long cc); - void resetCounters(unsigned long oldCc); - void killCounter(); - void reviveCounter(unsigned long cc); - - //intended for use by SweepUnit only. - unsigned getFreq() const { return 2048 - (period >> 1); } - void setFreq(unsigned newFreq, unsigned long cc); }; class DutyMasterDisabler : public MasterDisabler { DutyUnit &dutyUnit; public: DutyMasterDisabler(bool &m, DutyUnit &dutyUnit) : MasterDisabler(m), dutyUnit(dutyUnit) {} - void operator()() { MasterDisabler::operator()(); dutyUnit.killCounter(); } + virtual void operator()() { MasterDisabler::operator()(); dutyUnit.killCounter(); } }; }