-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtimer.cpp
More file actions
34 lines (29 loc) · 791 Bytes
/
timer.cpp
File metadata and controls
34 lines (29 loc) · 791 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include "timer.h"
// Static Tables
const std::array<uint8_t, 4> Timer::freq_bits = {9, 3, 5, 7};
// Core Functions
Timer::Timer(Memory &mem_in) : mem(mem_in) {
div = clock >> 8;
// reset timer on DIV write
mem.wmask(0xff04, 0x0);
mem.hook(0xff04, [&](uint8_t) { clock = 0; });
mem.hook(0xff07, [&](uint8_t val) {
on = read1(val, 2);
freq_bit = freq_bits[val & 0x3];
});
}
void Timer::update(unsigned cpu_cycles) {
// catch up to CPU cycles
for (unsigned i = 0; i < cpu_cycles; ++i) {
clock += 4;
if (tima_scheduled) {
tima = tma;
IF = write1(IF, 2, true);
tima_scheduled = false;
}
bool bit = on && read1(clock, freq_bit);
if (last_bit && !bit) tima_scheduled = (++tima == 0);
last_bit = bit;
}
div = clock >> 8;
}