-
Notifications
You must be signed in to change notification settings - Fork 0
/
Timer_Gameboy.cc
70 lines (64 loc) · 1.19 KB
/
Timer_Gameboy.cc
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#include "Timer_Gameboy.h"
#include "Bus.h"
void Timer_Gameboy::write(uint16_t adr, uint8_t data)
{
if(adr == 0xFF04)
{
div = 0;
}
if(adr == 0xFF05)
{
tima = data; //TODO:manage glitch here
}
if(adr == 0xFF06)
{
tma = data;
}
if(adr == 0xFF07)
{
tac = data;
}
}
uint8_t Timer_Gameboy::read(uint16_t adr)
{
if(adr == 0xFF04)
{
return div>>8;
}
if(adr == 0xFF05)
{
return tima;
}
if(adr == 0xFF06)
{
return tma;
}
if(adr == 0xFF07)
{
return tac;
}
return 0;
}
void Timer_Gameboy::tick()
{
div++;
//if(div == 0);
uint8_t freq_selec = tac & 3;
uint8_t timer_enable = (tac>>2)&1;
uint8_t bit_num[] = {9,3,5,7};
uint8_t output = (div >> (bit_num[freq_selec]))&1;
output = output & timer_enable;
if(prev_output && !output)
{
//std::cout << "Timer:"<<(int)tima<<std::endl;
tima++;
if(tima == 0)
{
//todo:glitches
//std::cout << "interupt!\n";
tima = tma;
bus->write(0xFF0F,bus->read(0xFF0F)|(1<<2));
}
}
prev_output = output;
}