Skip to content

Commit

Permalink
Merge branch 'master' of github.com:fleroviux/NanoBoyAdvance into acc…
Browse files Browse the repository at this point in the history
…uracy/prefetch-rewrite
  • Loading branch information
fleroviux committed Oct 5, 2023
2 parents b7cec79 + 5f92598 commit 92fe4ca
Showing 1 changed file with 21 additions and 8 deletions.
29 changes: 21 additions & 8 deletions src/nba/src/hw/timer/timer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -171,16 +171,29 @@ void Timer::OnControlWritten(u64 chan_id) {
channel.mask = g_ticks_mask[control.frequency];

if(control.enable) {
if(!enable_previous) {
channel.counter = channel.reload;
}
// How many cycles are we away from the next prescaler tick?
const int prescaler_offset = scheduler.GetTimestampNow() & channel.mask;

if(!control.cascade) {
auto late = (scheduler.GetTimestampNow() & channel.mask);
if(!enable_previous) {
late -= 1;
if(enable_previous) {
if(!control.cascade) {
StartChannel(channel, prescaler_offset);
}
} else {
if(control.cascade) {
channel.counter = channel.reload;
} else if(channel.counter == 0xFFFF && prescaler_offset == 0) {
/**
* After enabling a timer, it takes one cycle to load the reload value into the counter.
* During this cycle, the timer can tick and may even overflow.
* We handle this edge-case here.
*
* See: https://github.com/nba-emu/NanoBoyAdvance/issues/331
*/
StartChannel(channel, 0);
} else {
channel.counter = channel.reload;
StartChannel(channel, prescaler_offset - 1);
}
StartChannel(channel, late);
}
}
}
Expand Down

0 comments on commit 92fe4ca

Please sign in to comment.