-
Notifications
You must be signed in to change notification settings - Fork 6
/
CookieboyDividerTimer.h
73 lines (57 loc) · 1.09 KB
/
CookieboyDividerTimer.h
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
71
72
73
#ifndef COOKIEBOYDIVIDER_H
#define COOKIEBOYDIVIDER_H
#include "CookieboyDefs.h"
namespace Cookieboy
{
/*
Very simple timer. Clocked at 16384 Hz. Doesn't generate any interrupts. Loops from 0 to 255.
*/
class DividerTimer
{
public:
DividerTimer(const bool &_CGB, const bool &_CGBDoubleSpeed) : CGB(_CGB), CGBDoubleSpeed(_CGBDoubleSpeed)
{
Reset();
}
void Step(DWORD clockDelta)
{
//In double speed mode DIV operates twice as fast
if (CGB && CGBDoubleSpeed)
{
clockDelta *= 2;
}
ClockCounter += clockDelta;
//Divider increments every 256 ticks
if (ClockCounter >= 256)
{
int passedPeriods = ClockCounter / 256;
ClockCounter %= 256;
DIV += passedPeriods;
}
}
void Reset()
{
ClockCounter = 0;
DIV = 0;
}
void EmulateBIOS()
{
Reset();
}
void DIVChanged(BYTE value)
{
DIV = 0;
}
BYTE GetDIV()
{
return DIV;
}
private:
const bool &CGB;
const bool &CGBDoubleSpeed;
DWORD ClockCounter;
BYTE DIV; //Divider Register (R/W)
//This register is incremented 16384 (~16779 on SGB) times a second. Writing any value sets it to $00
};
}
#endif