Style nits

This commit is contained in:
twinaphex 2015-02-17 18:30:55 +01:00
parent 884fb4229a
commit faf40ea0aa
6 changed files with 327 additions and 285 deletions

View File

@ -19,134 +19,153 @@
#include "duty_unit.h"
#include <algorithm>
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;
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;
}
static inline unsigned toPeriod(const unsigned freq) {
static inline unsigned toPeriod(const unsigned freq)
{
return (2048 - freq) << 1;
}
namespace gambatte {
namespace gambatte
{
void DutyUnit::updatePos(const unsigned long cc) {
if (cc >= nextPosUpdate) {
const unsigned long inc = (cc - nextPosUpdate) / period + 1;
nextPosUpdate += period * inc;
pos += inc;
pos &= 7;
}
}
void DutyUnit::updatePos(const unsigned long cc)
{
if (cc >= nextPosUpdate)
{
const unsigned long inc = (cc - nextPosUpdate) / period + 1;
nextPosUpdate += period * inc;
pos += inc;
pos &= 7;
}
}
void DutyUnit::setDuty(const unsigned nr1) {
duty = nr1 >> 6;
high = toOutState(duty, pos);
}
void DutyUnit::setDuty(const unsigned nr1)
{
duty = nr1 >> 6;
high = toOutState(duty, pos);
}
void DutyUnit::setCounter() {
static const unsigned char nextStateDistance[4 * 8] = {
6, 5, 4, 3, 2, 1, 0, 0,
0, 5, 4, 3, 2, 1, 0, 1,
0, 3, 2, 1, 0, 3, 2, 1,
0, 5, 4, 3, 2, 1, 0, 1
};
if (enableEvents && nextPosUpdate != COUNTER_DISABLED)
counter = nextPosUpdate + period * nextStateDistance[(duty * 8) | pos];
else
counter = COUNTER_DISABLED;
}
void DutyUnit::setCounter()
{
static const unsigned char nextStateDistance[4 * 8] = {
6, 5, 4, 3, 2, 1, 0, 0,
0, 5, 4, 3, 2, 1, 0, 1,
0, 3, 2, 1, 0, 3, 2, 1,
0, 5, 4, 3, 2, 1, 0, 1
};
void DutyUnit::setFreq(const unsigned newFreq, const unsigned long cc) {
updatePos(cc);
period = toPeriod(newFreq);
setCounter();
}
if (enableEvents && nextPosUpdate != COUNTER_DISABLED)
counter = nextPosUpdate + period * nextStateDistance[(duty * 8) | pos];
else
counter = COUNTER_DISABLED;
}
void DutyUnit::event() {
unsigned inc = period << duty;
if (duty == 3)
inc -= period * 2;
if (!(high ^= true))
inc = period * 8 - inc;
counter += inc;
}
void DutyUnit::setFreq(const unsigned newFreq, const unsigned long cc)
{
updatePos(cc);
period = toPeriod(newFreq);
setCounter();
}
void DutyUnit::nr1Change(const unsigned newNr1, const unsigned long cc) {
updatePos(cc);
setDuty(newNr1);
setCounter();
}
void DutyUnit::event()
{
unsigned inc = period << duty;
void DutyUnit::nr3Change(const unsigned newNr3, const unsigned long cc) {
setFreq((getFreq() & 0x700) | newNr3, cc);
}
if (duty == 3)
inc -= period * 2;
void DutyUnit::nr4Change(const unsigned newNr4, const unsigned long cc) {
setFreq((newNr4 << 8 & 0x700) | (getFreq() & 0xFF), cc);
if (newNr4 & 0x80) {
nextPosUpdate = (cc & ~1) + period;
setCounter();
}
}
if (!(high ^= true))
inc = period * 8 - inc;
DutyUnit::DutyUnit() :
nextPosUpdate(COUNTER_DISABLED),
period(4096),
pos(0),
duty(0),
high(false),
enableEvents(true)
{}
counter += inc;
}
void DutyUnit::reset() {
pos = 0;
high = toOutState(duty, pos);
nextPosUpdate = COUNTER_DISABLED;
setCounter();
}
void DutyUnit::nr1Change(const unsigned newNr1, const unsigned long cc)
{
updatePos(cc);
setDuty(newNr1);
setCounter();
}
void DutyUnit::saveState(SaveState::SPU::Duty &dstate, const unsigned long cc) {
updatePos(cc);
dstate.nextPosUpdate = nextPosUpdate;
dstate.nr3 = getFreq() & 0xFF;
dstate.pos = pos;
}
void DutyUnit::nr3Change(const unsigned newNr3, const unsigned long cc)
{
setFreq((getFreq() & 0x700) | newNr3, 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);
period = toPeriod((nr4 << 8 & 0x700) | dstate.nr3);
enableEvents = true;
setCounter();
}
void DutyUnit::nr4Change(const unsigned newNr4, const unsigned long cc)
{
setFreq((newNr4 << 8 & 0x700) | (getFreq() & 0xFF), cc);
void DutyUnit::resetCounters(const unsigned long oldCc) {
if (nextPosUpdate == COUNTER_DISABLED)
return;
updatePos(oldCc);
nextPosUpdate -= COUNTER_MAX;
SoundUnit::resetCounters(oldCc);
}
if (newNr4 & 0x80)
{
nextPosUpdate = (cc & ~1) + period;
setCounter();
}
}
void DutyUnit::killCounter() {
enableEvents = false;
setCounter();
}
DutyUnit::DutyUnit() :
nextPosUpdate(COUNTER_DISABLED),
period(4096),
pos(0),
duty(0),
high(false),
enableEvents(true)
{}
void DutyUnit::reviveCounter(const unsigned long cc) {
updatePos(cc);
high = toOutState(duty, pos);
enableEvents = true;
setCounter();
}
void DutyUnit::reset()
{
pos = 0;
high = toOutState(duty, pos);
nextPosUpdate = COUNTER_DISABLED;
setCounter();
}
void DutyUnit::saveState(SaveState::SPU::Duty &dstate, const unsigned long cc)
{
updatePos(cc);
dstate.nextPosUpdate = nextPosUpdate;
dstate.nr3 = getFreq() & 0xFF;
dstate.pos = pos;
}
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);
period = toPeriod((nr4 << 8 & 0x700) | dstate.nr3);
enableEvents = true;
setCounter();
}
void DutyUnit::resetCounters(const unsigned long oldCc)
{
if (nextPosUpdate == COUNTER_DISABLED)
return;
updatePos(oldCc);
nextPosUpdate -= COUNTER_MAX;
SoundUnit::resetCounters(oldCc);
}
void DutyUnit::killCounter()
{
enableEvents = false;
setCounter();
}
void DutyUnit::reviveCounter(const unsigned long cc)
{
updatePos(cc);
high = toOutState(duty, pos);
enableEvents = true;
setCounter();
}
}

View File

@ -19,88 +19,97 @@
#include "envelope_unit.h"
#include <algorithm>
namespace gambatte {
EnvelopeUnit::VolOnOffEvent EnvelopeUnit::nullEvent;
void EnvelopeUnit::event() {
const unsigned long period = nr2 & 7;
if (period) {
unsigned newVol = volume;
if (nr2 & 8)
++newVol;
else
--newVol;
if (newVol < 0x10U) {
volume = newVol;
if (volume < 2)
volOnOffEvent(counter);
counter += period << 15;
} else
counter = COUNTER_DISABLED;
} else
counter += 8ul << 15;
}
bool EnvelopeUnit::nr2Change(const unsigned newNr2) {
if (!(nr2 & 7) && counter != COUNTER_DISABLED)
++volume;
else if (!(nr2 & 8))
volume += 2;
if ((nr2 ^ newNr2) & 8)
volume = 0x10 - volume;
volume &= 0xF;
nr2 = newNr2;
return !(newNr2 & 0xF8);
}
bool EnvelopeUnit::nr4Init(const unsigned long cc) {
{
unsigned long period = nr2 & 7;
if (!period)
period = 8;
if (!(cc & 0x7000))
++period;
counter = cc - ((cc - 0x1000) & 0x7FFF) + period * 0x8000;
}
volume = nr2 >> 4;
return !(nr2 & 0xF8);
}
EnvelopeUnit::EnvelopeUnit(VolOnOffEvent &volOnOffEvent)
: volOnOffEvent(volOnOffEvent),
nr2(0),
volume(0)
namespace gambatte
{
}
void EnvelopeUnit::reset() {
counter = COUNTER_DISABLED;
}
EnvelopeUnit::VolOnOffEvent EnvelopeUnit::nullEvent;
void EnvelopeUnit::saveState(SaveState::SPU::Env &estate) const {
estate.counter = counter;
estate.volume = volume;
}
void EnvelopeUnit::event()
{
const unsigned long period = nr2 & 7;
void EnvelopeUnit::loadState(const SaveState::SPU::Env &estate, const unsigned nr2, const unsigned long cc) {
counter = std::max(estate.counter, cc);
volume = estate.volume;
this->nr2 = nr2;
}
if (period)
{
unsigned newVol = volume;
if (nr2 & 8)
++newVol;
else
--newVol;
if (newVol < 0x10U)
{
volume = newVol;
if (volume < 2)
volOnOffEvent(counter);
counter += period << 15;
}
else
counter = COUNTER_DISABLED;
}
else
counter += 8ul << 15;
}
bool EnvelopeUnit::nr2Change(const unsigned newNr2)
{
if (!(nr2 & 7) && counter != COUNTER_DISABLED)
++volume;
else if (!(nr2 & 8))
volume += 2;
if ((nr2 ^ newNr2) & 8)
volume = 0x10 - volume;
volume &= 0xF;
nr2 = newNr2;
return !(newNr2 & 0xF8);
}
bool EnvelopeUnit::nr4Init(const unsigned long cc)
{
unsigned long period = nr2 & 7;
if (!period)
period = 8;
if (!(cc & 0x7000))
++period;
counter = cc - ((cc - 0x1000) & 0x7FFF) + period * 0x8000;
volume = nr2 >> 4;
return !(nr2 & 0xF8);
}
EnvelopeUnit::EnvelopeUnit(VolOnOffEvent &volOnOffEvent)
: volOnOffEvent(volOnOffEvent),
nr2(0),
volume(0)
{
}
void EnvelopeUnit::reset()
{
counter = COUNTER_DISABLED;
}
void EnvelopeUnit::saveState(SaveState::SPU::Env &estate) const
{
estate.counter = counter;
estate.volume = volume;
}
void EnvelopeUnit::loadState(const SaveState::SPU::Env &estate, const unsigned nr2, const unsigned long cc)
{
counter = std::max(estate.counter, cc);
volume = estate.volume;
this->nr2 = nr2;
}
}

View File

@ -20,72 +20,79 @@
#include "master_disabler.h"
#include <algorithm>
namespace gambatte {
LengthCounter::LengthCounter(MasterDisabler &disabler, const unsigned mask) :
disableMaster(disabler),
lengthMask(mask)
namespace gambatte
{
init(false);
nr1Change(0, 0, 0);
}
void LengthCounter::event() {
counter = COUNTER_DISABLED;
lengthCounter = 0;
disableMaster();
}
LengthCounter::LengthCounter(MasterDisabler &disabler, const unsigned mask) :
disableMaster(disabler),
lengthMask(mask)
{
init(false);
nr1Change(0, 0, 0);
}
void LengthCounter::nr1Change(const unsigned newNr1, const unsigned nr4, const unsigned long cycleCounter) {
lengthCounter = (~newNr1 & lengthMask) + 1;
counter = (nr4 & 0x40) ?( (cycleCounter >> 13) + lengthCounter) << 13 : static_cast<unsigned long>(COUNTER_DISABLED);
}
void LengthCounter::event()
{
counter = COUNTER_DISABLED;
lengthCounter = 0;
disableMaster();
}
void LengthCounter::nr4Change(const unsigned oldNr4, const unsigned newNr4, const unsigned long cycleCounter) {
if (counter != COUNTER_DISABLED)
lengthCounter = (counter >> 13) - (cycleCounter >> 13);
{
unsigned dec = 0;
if (newNr4 & 0x40) {
dec = ~cycleCounter >> 12 & 1;
if (!(oldNr4 & 0x40) && lengthCounter) {
if (!(lengthCounter -= dec))
disableMaster();
}
}
if ((newNr4 & 0x80) && !lengthCounter)
lengthCounter = lengthMask + 1 - dec;
}
if ((newNr4 & 0x40) && lengthCounter)
counter = ((cycleCounter >> 13) + lengthCounter) << 13;
else
counter = COUNTER_DISABLED;
}
void LengthCounter::nr1Change(const unsigned newNr1, const unsigned nr4, const unsigned long cycleCounter)
{
lengthCounter = (~newNr1 & lengthMask) + 1;
counter = (nr4 & 0x40) ?( (cycleCounter >> 13) + lengthCounter) << 13 : static_cast<unsigned long>(COUNTER_DISABLED);
}
/*void LengthCounter::reset() {
counter = COUNTER_DISABLED;
if (cgb)
lengthCounter = lengthMask + 1;
}*/
void LengthCounter::nr4Change(const unsigned oldNr4, const unsigned newNr4, const unsigned long cycleCounter)
{
unsigned dec = 0;
void LengthCounter::init(const bool cgb) {
this->cgb = cgb;
}
if (counter != COUNTER_DISABLED)
lengthCounter = (counter >> 13) - (cycleCounter >> 13);
void LengthCounter::saveState(SaveState::SPU::LCounter &lstate) const {
lstate.counter = counter;
lstate.lengthCounter = lengthCounter;
}
if (newNr4 & 0x40)
{
dec = ~cycleCounter >> 12 & 1;
void LengthCounter::loadState(const SaveState::SPU::LCounter &lstate, const unsigned long cc) {
counter = std::max(lstate.counter, cc);
lengthCounter = lstate.lengthCounter;
}
if (!(oldNr4 & 0x40) && lengthCounter)
{
if (!(lengthCounter -= dec))
disableMaster();
}
}
if ((newNr4 & 0x80) && !lengthCounter)
lengthCounter = lengthMask + 1 - dec;
if ((newNr4 & 0x40) && lengthCounter)
counter = ((cycleCounter >> 13) + lengthCounter) << 13;
else
counter = COUNTER_DISABLED;
}
/*void LengthCounter::reset() {
counter = COUNTER_DISABLED;
if (cgb)
lengthCounter = lengthMask + 1;
}*/
void LengthCounter::init(const bool cgb)
{
this->cgb = cgb;
}
void LengthCounter::saveState(SaveState::SPU::LCounter &lstate) const
{
lstate.counter = counter;
lstate.lengthCounter = lengthCounter;
}
void LengthCounter::loadState(const SaveState::SPU::LCounter &lstate, const unsigned long cc)
{
counter = std::max(lstate.counter, cc);
lengthCounter = lstate.lengthCounter;
}
}

View File

@ -19,15 +19,17 @@
#ifndef MASTER_DISABLER_H
#define MASTER_DISABLER_H
namespace gambatte {
class MasterDisabler {
bool &master;
public:
MasterDisabler(bool &m) : master(m) {}
virtual ~MasterDisabler() {}
virtual void operator()() { master = false; }
};
namespace gambatte
{
class MasterDisabler
{
bool &master;
public:
MasterDisabler(bool &m) : master(m) {}
virtual ~MasterDisabler() {}
virtual void operator()() { master = false; }
};
}
#endif

View File

@ -19,20 +19,22 @@
#ifndef SOUND_UNIT_H
#define SOUND_UNIT_H
namespace gambatte {
namespace gambatte
{
class SoundUnit {
protected:
unsigned long counter;
public:
enum { COUNTER_MAX = 0x80000000u, COUNTER_DISABLED = 0xFFFFFFFFu };
SoundUnit() : counter(COUNTER_DISABLED) {}
virtual ~SoundUnit() {}
virtual void event() = 0;
unsigned long getCounter() const { return counter; }
virtual void resetCounters(unsigned long /*oldCc*/) { if (counter != COUNTER_DISABLED) counter -= COUNTER_MAX; }
};
class SoundUnit
{
protected:
unsigned long counter;
public:
enum { COUNTER_MAX = 0x80000000u, COUNTER_DISABLED = 0xFFFFFFFFu };
SoundUnit() : counter(COUNTER_DISABLED) {}
virtual ~SoundUnit() {}
virtual void event() = 0;
unsigned long getCounter() const { return counter; }
virtual void resetCounters(unsigned long /*oldCc*/) { if (counter != COUNTER_DISABLED) counter -= COUNTER_MAX; }
};
}

View File

@ -21,24 +21,27 @@
#include "envelope_unit.h"
namespace gambatte {
namespace gambatte
{
template<class Channel, class Unit>
class StaticOutputTester : public EnvelopeUnit::VolOnOffEvent {
const Channel &ch;
Unit &unit;
public:
StaticOutputTester(const Channel &ch, Unit &unit) : ch(ch), unit(unit) {}
void operator()(unsigned long cc);
};
template<class Channel, class Unit>
class StaticOutputTester : public EnvelopeUnit::VolOnOffEvent
{
const Channel &ch;
Unit &unit;
public:
StaticOutputTester(const Channel &ch, Unit &unit) : ch(ch), unit(unit) {}
void operator()(unsigned long cc);
};
template<class Channel, class Unit>
void StaticOutputTester<Channel, Unit>::operator()(const unsigned long cc) {
if (ch.soMask && ch.master && ch.envelopeUnit.getVolume())
unit.reviveCounter(cc);
else
unit.killCounter();
}
template<class Channel, class Unit>
void StaticOutputTester<Channel, Unit>::operator()(const unsigned long cc)
{
if (ch.soMask && ch.master && ch.envelopeUnit.getVolume())
unit.reviveCounter(cc);
else
unit.killCounter();
}
}