/***************************************************************************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 "ly_counter.h"19#include "../savestate.h"2021namespace gambatte {2223LyCounter::LyCounter()24: time_(0), lineTime_(0), ly_(0), ds(false)25{26setDoubleSpeed(false);27reset(0, 0);28}2930void LyCounter::doEvent() {31++ly_;3233if (ly_ == 154)34ly_ = 0;3536time_ = time_ + lineTime_;37}3839unsigned long LyCounter::nextLineCycle(const unsigned lineCycle, const unsigned long cycleCounter) const {40unsigned long tmp = time_ + (lineCycle << ds);4142if (tmp - cycleCounter > lineTime_)43tmp -= lineTime_;4445return tmp;46}4748unsigned long LyCounter::nextFrameCycle(const unsigned long frameCycle, const unsigned long cycleCounter) const {49unsigned long tmp = time_ + (((153U - ly()) * 456U + frameCycle) << ds);5051if (tmp - cycleCounter > 70224U << ds)52tmp -= 70224U << ds;5354return tmp;55}5657void LyCounter::reset(const unsigned long videoCycles, const unsigned long lastUpdate) {58ly_ = videoCycles / 456;59time_ = lastUpdate + ((456 - (videoCycles - ly_ * 456ul)) << isDoubleSpeed());60}6162void LyCounter::setDoubleSpeed(const bool ds_in) {63ds = ds_in;64lineTime_ = 456U << ds_in;65}6667SYNCFUNC(LyCounter)68{69NSS(time_);70NSS(lineTime_);71NSS(ly_);72NSS(ds);73}7475}767778