-
Notifications
You must be signed in to change notification settings - Fork 0
/
GB.cpp
152 lines (131 loc) · 2.97 KB
/
GB.cpp
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
#include "GB.h"
#include <ctime>
#include <functional>
#include <iostream>
#include <string>
GB::GB(const char* rom_fpath) :
rom_fpath(rom_fpath),
controller(this),
MMU(this, controller),
Cpu(this, MMU, controller),
timers(Cpu, MMU),
graphics(this, MMU, Cpu),
serializer(0x20000),
disassembler(this, MMU, Cpu),
debugger(Cpu, disassembler),
debugLogCallback(nullptr) {
LoadROM(rom_fpath);
}
void GB::AdvanceFrame() {
currentCycles = 0;
while (currentCycles < CYCLES_PER_FRAME) {
debugger.Advance();
int cycles = Cpu.Advance();
timers.Update(cycles);
if (doubleSpeed) cycles /= 2;
graphics.Update(cycles);
currentCycles += cycles;
}
Cpu.HandleInput();
}
void GB::SetCGBMode(bool val) {
CGBMode = val;
}
bool GB::CGBModeEnabled() {
return CGBMode;
}
void GB::SetDoubleSpeed(bool val) {
doubleSpeed = val;
}
bool GB::IsDoubleSpeed() {
return doubleSpeed;
}
std::string GB::GetROMName() {
return ROMName;
}
const char * GB::GetROMPath() {
return rom_fpath;
}
void GB::Reset() {
doubleSpeed = false;
CGBMode = false;
currentCycles = 0;
controller.Reset();
MMU.Reset();
Cpu.Reset();
timers.Reset();
graphics.Reset();
}
void GB::LoadROM(const char * _rom_fpath) {
if (_rom_fpath != nullptr) {
rom_fpath = _rom_fpath;
std::string path(rom_fpath);
auto basename = path.substr(path.find_last_of("/\\") + 1);
auto pos = basename.find_last_of(".");
ROMName = basename.substr(0, pos);
MMU.LoadROM(rom_fpath);
}
Reset();
}
void GB::Serialize(Serializer & s) {
s.Serialize<int>(currentCycles);
s.Serialize<bool>(CGBMode);
s.Serialize<bool>(doubleSpeed);
}
void GB::Deserialize(Serializer & s) {
currentCycles = s.Deserialize<int>();
CGBMode = s.Deserialize<bool>();
doubleSpeed = s.Deserialize<bool>();
}
byte * GB::SaveState(bool save_to_disk) {
controller.Serialize(serializer);
MMU.Serialize(serializer);
Cpu.Serialize(serializer);
timers.Serialize(serializer);
graphics.Serialize(serializer);
Serialize(serializer);
if (save_to_disk) {
serializer.SaveFile(("states/" + ROMName + ".state").c_str());
}
serializer.Reset();
return serializer.GetData();
}
void GB::LoadState(const byte * data) {
if (data != nullptr) {
serializer.SetData(data);
}
else {
serializer.LoadFile(("states/" + ROMName + ".state").c_str());
}
controller.Deserialize(serializer);
MMU.Deserialize(serializer);
Cpu.Deserialize(serializer);
timers.Deserialize(serializer);
graphics.Deserialize(serializer);
Deserialize(serializer);
serializer.Reset();
}
void GB::DebugLog(std::string s) {
if (debugLogCallback != nullptr) {
debugLogCallback(s);
}
else std::cout << s << std::endl;
}
Graphics * GB::GetGraphics() {
return &graphics;
}
CPU * GB::GetCPU() {
return &Cpu;
}
Memory * GB::GetMMU() {
return &MMU;
}
Controller * GB::GetInput() {
return &controller;
}
Debugger * GB::GetDebugger() {
return &debugger;
}
Serializer * GB::GetSerializer() {
return &serializer;
}