Skip to content

Commit

Permalink
fix #23
Browse files Browse the repository at this point in the history
  • Loading branch information
CasualPokePlayer committed Feb 1, 2024
1 parent ebfc7d0 commit 7b4ff85
Show file tree
Hide file tree
Showing 12 changed files with 24 additions and 18 deletions.
9 changes: 7 additions & 2 deletions libgambatte/include/gambatte.h
Original file line number Diff line number Diff line change
Expand Up @@ -423,10 +423,15 @@ class GB {
/** Gets the address the CPU was interrupted at or -1 if stopped normally. */
int getHitInterruptAddress();

/** Returns the current cycle-based time counter as dividers. (2^21/sec) */
/** Returns a cycle-based time counter as dividers. (2^21/sec)
* Note that this is stored in savestates, so this counter may change due to loading a savestate.
* This counter will be reset on a load() or reset() call
*/
unsigned long long timeNow() const;

/** Sets the current cycle-based time counter as dividers. (2^21/sec) */
/** Sets RTC time counter as dividers. (2^21/sec).
* This is NOT the same counter from the timeNow() function.
*/
void setTime(unsigned long long dividers) const;

/** Return a value in range 0-3FFF representing current "position" of internal divider */
Expand Down
2 changes: 2 additions & 0 deletions libgambatte/src/initstate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,8 @@ void gambatte::setInitState(SaveState &state, bool const cgb, bool const sgb, bo
state.mem.lastOamDmaUpdate = disabled_time;
state.mem.unhaltTime = disabled_time;
state.mem.lastCartBusUpdate = 0;
state.mem.totalSamplesEmittedHigh = 0;
state.mem.totalSamplesEmittedLow = 0;
state.mem.minIntTime = 0;
state.mem.rombank = 1;
state.mem.dmaSource = 0;
Expand Down
1 change: 0 additions & 1 deletion libgambatte/src/mem/cartridge.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@ class Cartridge {
}
void setTimeMode(bool useCycles, unsigned long const cc) { time_.setTimeMode(useCycles, cc); }
void setRtcDivisorOffset(long const rtcDivisorOffset) { time_.setRtcDivisorOffset(rtcDivisorOffset); }
unsigned long long timeNow() const { return time_.timeNow(); }
void setTime(unsigned long long dividers) { time_.setTime(dividers); }
void rtcWrite(unsigned data, unsigned long const cc) { rtc_.write(data, cc); }
unsigned char rtcRead() const { return rtc_.activeLatch() ? *rtc_.activeLatch() : 0xFF; }
Expand Down
6 changes: 0 additions & 6 deletions libgambatte/src/mem/huc3_chip.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,6 @@ void HuC3Chip::updateClock(unsigned long const cc) {
io_[0x15] = (days >> 8) & 0x0F;
}

unsigned long long HuC3Chip::timeNow() const {
unsigned long const minutes = (io_[0x10] & 0x0F) | ((io_[0x11] & 0x0F) << 4) | ((io_[0x12] & 0x0F) << 8);
unsigned long const days = (io_[0x13] & 0x0F) | ((io_[0x14] & 0x0F) << 4) | ((io_[0x15] & 0x0F) << 8);
return ((days * 86400 + minutes * 60) * time_.getRtcDivisor() + rtcCycles_) >> 1;
}

void HuC3Chip::setTime(unsigned long long const dividers) {
unsigned long const cycleDivisor = time_.getRtcDivisor() * 60;
rtcCycles_ = dividers * 2 % cycleDivisor;
Expand Down
1 change: 0 additions & 1 deletion libgambatte/src/mem/huc3_chip.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ class HuC3Chip : public Clock {
template<bool isReader>void SyncState(NewState *ns);

virtual void updateClock(unsigned long const cc);
virtual unsigned long long timeNow() const;
virtual void setTime(unsigned long long const dividers);
virtual void setBaseTime(unsigned long long baseTime, unsigned long const cc);

Expand Down
4 changes: 0 additions & 4 deletions libgambatte/src/mem/rtc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,6 @@ void Rtc::updateClock(unsigned long const cc) {
}
}

unsigned long long Rtc::timeNow() const {
return (((((((dataDh_ & 0x01) << 8) | dataDl_) * 86400) + (dataH_ * 3600) + (dataM_ * 60) + dataS_) * time_.getRtcDivisor()) + dataC_) >> 1;
}

void Rtc::setTime(unsigned long long const dividers) {
unsigned long const cycleDivisor = time_.getRtcDivisor();
dataC_ = dividers * 2 % cycleDivisor;
Expand Down
1 change: 0 additions & 1 deletion libgambatte/src/mem/rtc.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@ class Rtc : public Clock {
}

virtual void updateClock(unsigned long const cc);
virtual unsigned long long timeNow() const;
virtual void setTime(unsigned long long const dividers);
virtual void setBaseTime(unsigned long long baseTime, unsigned long const cc);

Expand Down
2 changes: 0 additions & 2 deletions libgambatte/src/mem/time.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ struct SaveState;
class Clock {
public:
virtual void updateClock(unsigned long const cc) = 0;
virtual unsigned long long timeNow() const = 0;
virtual void setTime(unsigned long long const dividers) = 0;
virtual void setBaseTime(unsigned long long baseTime, unsigned long const cc) = 0;
};
Expand All @@ -47,7 +46,6 @@ class Time {
void setTimeMode(bool useCycles, unsigned long cycleCounter);

void set(Clock *clock) { clock_ = clock; }
unsigned long long timeNow() const { return clock_ ? clock_->timeNow() : 0; }
void setTime(unsigned long long const dividers) { if (clock_) clock_->setTime(dividers); }
void setBaseTime(unsigned long long baseTime, unsigned long const cc) { if (clock_) clock_->setBaseTime(baseTime, cc); }

Expand Down
7 changes: 7 additions & 0 deletions libgambatte/src/memory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,8 @@ unsigned long Memory::saveState(SaveState &state, unsigned long cc) {
state.mem.biosMode = biosMode_;
state.mem.stopped = stopped_;
state.mem.lastCartBusUpdate = lastCartBusUpdate_;
state.mem.totalSamplesEmittedHigh = totalSamplesEmitted_ >> 32;
state.mem.totalSamplesEmittedLow = totalSamplesEmitted_ & 0xFFFFFFFF;

intreq_.saveState(state);
cart_.saveState(state, cc);
Expand All @@ -116,6 +118,9 @@ void Memory::loadState(SaveState const &state) {
biosMode_ = state.mem.biosMode;
stopped_ = state.mem.stopped;
lastCartBusUpdate_ = state.mem.lastCartBusUpdate;
totalSamplesEmitted_ = (unsigned long long)state.mem.totalSamplesEmittedHigh << 32;
totalSamplesEmitted_ |= state.mem.totalSamplesEmittedLow;

psg_.loadState(state);
lcd_.loadState(state, state.mem.oamDmaPos < oam_size ? cart_.rdisabledRam() : ioamhram_);
tima_.loadState(state, TimaInterruptRequester(intreq_));
Expand Down Expand Up @@ -1445,6 +1450,7 @@ std::size_t Memory::fillSoundBuffer(unsigned long cc) {
if (cart_.isHuC3())
cart_.accumulateSamples(cc);

totalSamplesEmitted_ += samples;
return samples;
}

Expand Down Expand Up @@ -1535,4 +1541,5 @@ SYNCFUNC(Memory) {
NSS(stopped_);
NSS(linked_);
NSS(linkClockTrigger_);
NSS(totalSamplesEmitted_);
}
5 changes: 4 additions & 1 deletion libgambatte/src/memory.h
Original file line number Diff line number Diff line change
Expand Up @@ -358,7 +358,8 @@ class Memory {
std::memcpy(bios_.get(), buffer.get(), buffer.size());
}

unsigned long long timeNow() const { return cart_.timeNow(); }
unsigned long long timeNow() const { return totalSamplesEmitted_; }

void setTime(unsigned long long dividers) { cart_.setTime(dividers); }

unsigned long getDivLastUpdate() { return divLastUpdate_; }
Expand Down Expand Up @@ -408,6 +409,8 @@ class Memory {
bool linked_;
bool linkClockTrigger_;

unsigned long long totalSamplesEmitted_;

void decEventCycles(IntEventId eventId, unsigned long dec);
void oamDmaInitSetup();
void updateOamDma(unsigned long cycleCounter);
Expand Down
2 changes: 2 additions & 0 deletions libgambatte/src/savestate.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ struct SaveState {
unsigned long minIntTime;
unsigned long unhaltTime;
unsigned long lastCartBusUpdate;
unsigned long totalSamplesEmittedHigh;
unsigned long totalSamplesEmittedLow;
unsigned short rombank;
unsigned short dmaSource;
unsigned short dmaDestination;
Expand Down
2 changes: 2 additions & 0 deletions libgambatte/src/statesaver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,8 @@ SaverList::SaverList()
{ static char const label[] = { m,i,n,i,n,t,t, NUL }; ADD(mem.minIntTime); }
{ static char const label[] = { u,n,h,a,l,t,t, NUL }; ADD(mem.unhaltTime); }
{ static char const label[] = { l,c,b,u,s,u,p, NUL }; ADD(mem.lastCartBusUpdate); }
{ static char const label[] = { t,s,a,m,p,s,h, NUL }; ADD(mem.totalSamplesEmittedHigh); }
{ static char const label[] = { t,s,a,m,p,s,l, NUL }; ADD(mem.totalSamplesEmittedLow); }
{ static char const label[] = { r,o,m,b,a,n,k, NUL }; ADD(mem.rombank); }
{ static char const label[] = { d,m,a,s,r,c, NUL }; ADD(mem.dmaSource); }
{ static char const label[] = { d,m,a,d,s,t, NUL }; ADD(mem.dmaDestination); }
Expand Down

0 comments on commit 7b4ff85

Please sign in to comment.