Path: blob/master/libgambatte/src/sound/length_counter.cpp
2 views
/***************************************************************************1* Copyright (C) 2007 by Sindre Aamås *2* [email protected] *3* *4* This program is free software; you can redistribute it and/or modify *5* it under the terms of the GNU General Public License version 2 as *6* published by the Free Software Foundation. *7* *8* This program is distributed in the hope that it will be useful, *9* but WITHOUT ANY WARRANTY; without even the implied warranty of *10* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *11* GNU General Public License version 2 for more details. *12* *13* You should have received a copy of the GNU General Public License *14* version 2 along with this program; if not, write to the *15* Free Software Foundation, Inc., *16* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *17***************************************************************************/18#include "length_counter.h"19#include "master_disabler.h"20#include <algorithm>2122namespace gambatte {2324LengthCounter::LengthCounter(MasterDisabler &disabler, const unsigned mask) :25disableMaster(disabler),26lengthMask(mask)27{28init(false);29nr1Change(0, 0, 0);30}3132void LengthCounter::event() {33counter = COUNTER_DISABLED;34lengthCounter = 0;35disableMaster();36}3738void LengthCounter::nr1Change(const unsigned newNr1, const unsigned nr4, const unsigned long cycleCounter) {39lengthCounter = (~newNr1 & lengthMask) + 1;40counter = (nr4 & 0x40) ?( (cycleCounter >> 13) + lengthCounter) << 13 : static_cast<unsigned long>(COUNTER_DISABLED);41}4243void LengthCounter::nr4Change(const unsigned oldNr4, const unsigned newNr4, const unsigned long cycleCounter) {44if (counter != COUNTER_DISABLED)45lengthCounter = (counter >> 13) - (cycleCounter >> 13);4647{48unsigned dec = 0;4950if (newNr4 & 0x40) {51dec = ~cycleCounter >> 12 & 1;5253if (!(oldNr4 & 0x40) && lengthCounter) {54if (!(lengthCounter -= dec))55disableMaster();56}57}5859if ((newNr4 & 0x80) && !lengthCounter)60lengthCounter = lengthMask + 1 - dec;61}6263if ((newNr4 & 0x40) && lengthCounter)64counter = ((cycleCounter >> 13) + lengthCounter) << 13;65else66counter = COUNTER_DISABLED;67}6869/*void LengthCounter::reset() {70counter = COUNTER_DISABLED;7172if (cgb)73lengthCounter = lengthMask + 1;74}*/7576void LengthCounter::init(const bool cgb) {77this->cgb = cgb;78}7980void LengthCounter::loadState(const SaveState::SPU::LCounter &lstate, const unsigned long cc) {81counter = std::max(lstate.counter, cc);82lengthCounter = lstate.lengthCounter;83}8485SYNCFUNC(LengthCounter)86{87NSS(counter);88NSS(lengthCounter);89NSS(cgb);90}9192}939495