Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
alexbevi
GitHub Repository: alexbevi/BizHawk
Path: blob/master/libmeteor/source/clock.cpp
2 views
1
// Meteor - A Nintendo Gameboy Advance emulator
2
// Copyright (C) 2009-2011 Philippe Daouadi
3
//
4
// This program is free software: you can redistribute it and/or modify
5
// it under the terms of the GNU General Public License as published by
6
// the Free Software Foundation, either version 3 of the License, or
7
// (at your option) any later version.
8
//
9
// This program is distributed in the hope that it will be useful,
10
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
// GNU General Public License for more details.
13
//
14
// You should have received a copy of the GNU General Public License
15
// along with this program. If not, see <http://www.gnu.org/licenses/>.
16
17
#include "ameteor/clock.hpp"
18
#include "globals.hpp"
19
#include "ameteor.hpp"
20
21
#include "debug.hpp"
22
23
namespace AMeteor
24
{
25
void Clock::Reset ()
26
{
27
// lcd is enabled by default
28
m_first = m_count = m_cycles = m_lcd = m_sound = 0;
29
// timers and battery are disabled by default
30
/*m_battery =*/ m_timer[0] = m_timer[1] = m_timer[2] = m_timer[3] =
31
INT_MAX;
32
}
33
34
void Clock::Commit ()
35
{
36
unsigned short tocommit;
37
38
//m_count += m_cycles;
39
40
// this loop is here because a timer can trigger a dma which will take a
41
// long time, during this time the lcd must draw and the timers continue
42
while (m_cycles >= m_first)
43
{
44
m_count += m_cycles;
45
46
tocommit = m_cycles;
47
m_cycles = 0;
48
49
m_lcd -= tocommit;
50
while (m_lcd <= 0)
51
LCD.TimeEvent();
52
53
m_sound -= tocommit;
54
while (m_sound <= 0)
55
{
56
SOUND.TimeEvent();
57
// XXX freq
58
m_sound += SOUND_PERIOD;
59
}
60
61
#define COMMIT(dev, obj) \
62
if (m_##dev != INT_MAX) \
63
{ \
64
m_##dev -= tocommit; \
65
while (m_##dev <= 0) \
66
obj.TimeEvent(); \
67
}
68
COMMIT(timer[0], TIMER0)
69
COMMIT(timer[1], TIMER1)
70
COMMIT(timer[2], TIMER2)
71
COMMIT(timer[3], TIMER3)
72
//COMMIT(battery, MEM)
73
#undef COMMIT
74
75
SetFirst();
76
}
77
}
78
79
void Clock::WaitForNext ()
80
{
81
m_cycles = m_first;
82
Commit();
83
}
84
85
#define SETFIRST(dev) \
86
if (m_##dev < m_first) \
87
m_first = m_##dev
88
void Clock::SetFirst ()
89
{
90
m_first = m_lcd;
91
SETFIRST(timer[0]);
92
SETFIRST(timer[1]);
93
SETFIRST(timer[2]);
94
SETFIRST(timer[3]);
95
SETFIRST(sound);
96
//SETFIRST(battery);
97
}
98
#undef SETFIRST
99
100
bool Clock::SaveState (std::ostream& stream)
101
{
102
SS_WRITE_VAR(m_cycles);
103
SS_WRITE_VAR(m_first);
104
SS_WRITE_VAR(m_lcd);
105
SS_WRITE_VAR(m_sound);
106
//SS_WRITE_VAR(m_battery);
107
108
SS_WRITE_ARRAY(m_timer);
109
110
return true;
111
}
112
113
bool Clock::LoadState (std::istream& stream)
114
{
115
SS_READ_VAR(m_cycles);
116
SS_READ_VAR(m_first);
117
SS_READ_VAR(m_lcd);
118
SS_READ_VAR(m_sound);
119
//SS_READ_VAR(m_battery);
120
121
SS_READ_ARRAY(m_timer);
122
123
return true;
124
}
125
}
126
127