-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCPU.h
More file actions
273 lines (229 loc) · 5.73 KB
/
CPU.h
File metadata and controls
273 lines (229 loc) · 5.73 KB
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
#ifndef CPU_H_INCLUDED
#define CPU_H_INCLUDED
#include "global.h"
#include "Register.h"
#include "MMU.h"
#define ZERO_VALUE 0x80
#define SUB_VALUE 0x40
#define HALF_VALUE 0x20
#define CARRY_VALUE 0x10
#define DIV_ADDR 0xFF04
#define TIMA_ADDR 0xFF05
#define TMA_ADDR 0xFF06
#define TAC_ADDR 0xFF07
#define DIV_SPEED 16384
#define TAC_0 4096
#define TAC_1 262144
#define TAC_2 65536
#define TAC_3 16384
class CPU
{
private:
// Registers
Register AF; ///< `A` and `F` register pair (Accumulator and Flag registers).
Register BC; ///< `B` and `C` register pair.
Register DE; ///< `D` and `E` register pair.
Register HL; ///< `H` and `L` register pair.
Register PC; ///< Program Counter.
Register SP; ///< Stack Pointer.
bool IME = false; ///< Interrupt Master Enable flag.
// Memory
MMU *mmu; ///< Pointer to MMU object associated with the emulator.
// Timer
int divCounter = 0; ///< Internal counter used to determine the number of CPU cycles passed before incrementing DIV
int timerCounter = 0; ///< Internal counter used to determine the number of CPU cycles passed before incrementing TIMA
public:
/**
* @brief Construct a new `CPU` object.
*
* @param mmu Pointer to MMU instance in `Emulator` class.
*/
CPU(MMU *mmu);
/**
* @brief Destroy the `CPU::CPU` object
*/
~CPU();
// REGISTERS
u16 getSP();
void setSP(u16 value);
u16 getPC();
void setPC(u16 value);
bool getIME();
void setIME(bool value);
// Alt
void pushStackWord(u16 word);
void pushStackByte(u8 byte);
u16 popStackWord();
u8 popStackByte();
// FLAGS
bool getZeroFlag();
void setZeroFlag(bool);
bool getSubFlag();
void setSubFlag(bool);
bool getHCarryFlag();
void setHCarryFlag(bool);
bool getCarryFlag();
void setCarryFlag(bool);
// Interrupts
void requestInterrupt(u8 interrupt);
// Helpers
bool checkHCarry_8(u8 arg1, u8 arg2, u8 res);
bool checkHCarry_16(u16 arg1, u16 arg2, u16 res);
bool checkCarry_8(u8 arg1, u8 arg2);
bool checkCarry_16(u16 arg1, u16 arg2);
// Instructions
u8 getInstruction();
/**
* @brief Given an 8-bit CPU instruction, execute the associated Opcode and update flags as necessary.
*
* @param instruction An 8-bit encoded CPU opcode.
* @return `int` The number of M-cycles taken to execute the opcode.
*/
int executeInstruction(u8 instruction);
/**
* @brief Adds arg to the register A, then stores the result in register A.
*
* @param arg The value to add to register A.
*/
void add_8(u8 arg);
void add_16(u16 arg);
/**
* @brief Subtracts arg to the register A, then stores the result in register A.
*
* @param arg The value to add to register A.
*/
void sub_a(u8 arg);
/**
* @brief these are is not implemented???
*
* @param arg The value to add to register A.
*/
void add_hl(u16 arg);
void add_sp(s8 arg);
void adc();
/**
* @brief Increments an 8 bit register.
*
* @param reg The register to increment
*/
void inc_8(u8 *reg);
/**
* @brief Increments an 16 bit register.
*
* @param reg The register to increment
*/
void inc_16(Register *reg);
/**
* @brief Decrements an 8 bit register.
*
* @param reg The register to decrement.
*/
void dec_8(u8 *reg);
/**
* @brief Decrements an 16 bit register.
*
* @param reg The register to decrement.
*/
void dec_16(Register *reg);
/**
* @brief OR operation between register A and a given argument.
*
* @param arg The value to OR against.
*/
void or_a(u8 arg);
/**
* @brief AND operation between register A and a given argument.
*
* @param arg The value to AND against.
*/
void and_a(u8 arg);
/**
* @brief XOR operation between register A and a given argument.
*
* @param arg The value to XOR against.
*/
void xor_a(u8 arg);
/**
* @brief CP operation between register A and a given argument.
*
* @param arg The value to compare against.
*/
void cp(u8 arg);
/**
* @brief POP operation on the given register. Increments the SP.
*
* @param reg The register in which to store the popped value.
*/
void pop(Register *reg);
/**
* @brief JP operation.
*
*/
void jp();
/**
* @brief JP HL operation. Sets the PC to the value of HL.
*
*/
void jp_hl();
/**
* @brief RET operation. Sets the PC to the value stored in the memory location SP.
*
*/
void ret();
/**
* @brief IDK man.
*
*/
void call();
// Timer
/**
* @brief Get the Divider object
*
* @return u8
*/
u8 getDivider();
/**
* @brief Get the Timer object
*
* @return u8
*/
u8 getTimer();
/**
* @brief Get the Timer Modulo object
*
* @return u8
*/
u8 getTimerModulo();
/**
* @brief Resets the divider.
*
*/
void resetDivider();
/**
* @brief Set the Divider object to the given value.
*
* @param value
*/
void setDivider(u8 value);
/**
* @brief Set the Timer object to the given value.
*
* @param value
*/
void setTimer(u8 value);
/**
* @brief Set the Timer Modulo object.
*
* @param value
*/
void setTimerModulo(u8 value);
/**
* @brief Update the timer to the given cycle.
*
* @param cycles
*/
void updateTimer(int cycles);
// DEBUG
void dumpRegisters();
};
#endif