-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcycle.c
306 lines (263 loc) · 8.57 KB
/
cycle.c
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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
#include <cycle.h>
#include <opcodes.h>
#include <ppu.h>
#include <interrupts.h>
const uint8_t m_ticks_by_opcode[256] = {
2, 6, 4, 4, 2, 2, 4, 4, 10, 4, 4, 4, 2, 2, 4, 4, // 0x0_
2, 6, 4, 4, 2, 2, 4, 4, 4, 4, 4, 4, 2, 2, 4, 4, // 0x1_
0, 6, 4, 4, 2, 2, 4, 2, 0, 4, 4, 4, 2, 2, 4, 2, // 0x2_
4, 6, 4, 4, 6, 6, 6, 2, 0, 4, 4, 4, 2, 2, 4, 2, // 0x3_
2, 2, 2, 2, 2, 2, 4, 2, 2, 2, 2, 2, 2, 2, 4, 2, // 0x4_
2, 2, 2, 2,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8, 8, 12, 8, // 0x7_
8, 8, 8, 8, 8, 8, 12, 8, 8, 8, 8, 8, 8, 8, 12, 8, // 0x8_
8, 8, 8, 8, 8, 8, 12, 8, 8, 8, 8, 8, 8, 8, 12, 8, // 0x9_
8, 8, 8, 8, 8, 8, 12, 8, 8, 8, 8, 8, 8, 8, 12, 8, // 0xa_
8, 8, 8, 8, 8, 8, 12, 8, 8, 8, 8, 8, 8, 8, 12, 8, // 0xb_
8, 8, 8, 8, 8, 8, 12, 8, 8, 8, 8, 8, 8, 8, 12, 8, // 0xc_
8, 8, 8, 8, 8, 8, 12, 8, 8, 8, 8, 8, 8, 8, 12, 8, // 0xd_
8, 8, 8, 8, 8, 8, 12, 8, 8, 8, 8, 8, 8, 8, 12, 8, // 0xe_
8, 8, 8, 8, 8, 8, 12, 8, 8, 8, 8, 8, 8, 8, 12, 8 // 0xf_
};
void m_init_registers(m_dmg_t *m_dmg)
{
m_dmg->m_cpu = (m_dmg_cpu*) malloc(sizeof(m_dmg_cpu));
m_dmg->m_cpu->m_registers = (gb_registers_t*) malloc(sizeof(gb_registers_t));
m_dmg->m_cpu->interrupts = (m_interrupts*) malloc(sizeof(m_interrupts));
// If BootROM isn't loaded, bootstrap manually into cart area
if (!m_dmg->m_memory->m_in_bootrom)
{
PC = 0x100;
m_af_reg_set(m_dmg, 0x01B0);
BC = 0x0013;
DE = 0x00D8;
HL = 0x014D;
SP = 0xFFFE;
WRITEB(0xFF05, 0);
WRITEB(0xFF06, 0);
WRITEB(0xFF07, 0);
WRITEB(0xFF10, 0x80);
WRITEB(0xFF11, 0xBF);
WRITEB(0xFF12, 0xF3);
WRITEB(0xFF14, 0xBF);
WRITEB(0xFF16, 0x3F);
WRITEB(0xFF17, 0x00);
WRITEB(0xFF19, 0xBF);
WRITEB(0xFF1A, 0x7A);
WRITEB(0xFF1B, 0xFF);
WRITEB(0xFF1C, 0x9F);
WRITEB(0xFF1E, 0xBF);
WRITEB(0xFF20, 0xFF);
WRITEB(0xFF21, 0x00);
WRITEB(0xFF22, 0x00);
WRITEB(0xFF23, 0xBF);
WRITEB(0xFF24, 0x77);
WRITEB(0xFF25, 0xF3);
WRITEB(0xFF26, 0xF1);
WRITEB(0xFF40, 0x91);
WRITEB(0xFF42, 0x00);
WRITEB(0xFF43, 0x00);
WRITEB(0xFF45, 0x00);
WRITEB(0xFF47, 0xFC);
WRITEB(0xFF48, 0xFF);
WRITEB(0xFF49, 0xFF);
WRITEB(0xFF4A, 0x00);
WRITEB(0xFF4B, 0x00);
WRITEB(0xFFFF, 0x00);
}
else
{
m_af_reg_set(m_dmg, 0);
BC = 0;
DE = 0;
HL = 0;
PC = 0;
SP = 0;
}
m_dmg->m_cpu->m_registers->m_flags.zero = 0;
m_dmg->m_cpu->m_registers->m_flags.ngtv = 0;
m_dmg->m_cpu->m_registers->m_flags.half = 0;
m_dmg->m_cpu->m_registers->m_flags.crry = 0;
// Set the CPU Ticks to 0
m_dmg->m_cpu->m_cpu_ticks = 0;
// Setup the PPU
m_dmg->ppu->m_scanline = 0;
m_dmg->ppu->m_verticalscroll = 0;
m_dmg->ppu->m_horitzontalscroll = 0;
// Set the current GPU Mode to HBLANK
m_dmg->ppu->m_lcdc = 0;
m_dmg->ppu->m_stat = M_GPU_HBLANK;
// Setup Interrupts State (Disable Interrupts and Clear Interrupt's Flags)
m_dmg->m_cpu->interrupts->m_master = 0;
m_dmg->m_cpu->interrupts->m_enabled = 0;
m_dmg->m_cpu->interrupts->m_flags = 0;
}
uint8_t m_fetch(m_dmg_t *m_dmg)
{
if (m_dmg->m_memory->m_in_bootrom && PC >= 0xFF)
{
m_dmg->m_memory->m_in_bootrom = 0;
}
return (uint8_t) READB(PC);
}
uint8_t m_fetchopbyte(m_dmg_t *m_dmg)
{
return (uint8_t) READB(PC + 1);
}
uint16_t m_fetchopword(m_dmg_t *m_dmg)
{
uint8_t m_rb = READB(PC + 1);
uint8_t m_lb = READB(PC + 2);
uint16_t m_w = (((m_lb & 0xFF) << 8) | (m_rb & 0xFF));
return (uint16_t) m_w;
}
#if defined(PREC23)
bool m_is_bit_set(uint8_t m_register, size_t m_bit)
{
return 1 == ((m_register >> m_bit) & 1);
}
#endif
size_t m_exec(m_dmg_t *m_dmg)
{
m_dmg->m_cpu->m_opcode = m_fetch(m_dmg);
#ifdef OPCODE_DEBUG
printf("Current opcode: 0x%02X\n", m_dmg->m_cpu->m_opcode);
#endif
m_dmg->m_cpu->m_boperand = 0;
m_dmg->m_cpu->m_woperand = 0;
if (m_gb_instr[m_dmg->m_cpu->m_opcode].m_operand == 1)
{
m_dmg->m_cpu->m_boperand = m_fetchopbyte(m_dmg);
} else if (m_gb_instr[m_dmg->m_cpu->m_opcode].m_operand == 2) {
m_dmg->m_cpu->m_woperand = m_fetchopword(m_dmg);
}
switch(m_gb_instr[m_dmg->m_cpu->m_opcode].m_operand)
{
case 0:
if (m_gb_instr[m_dmg->m_cpu->m_opcode].m_funct == NULL)
{
printf("Unimplemented Opcode 0x%02X\n", m_dmg->m_cpu->m_opcode);
m_printregs(m_dmg);
exit(EXIT_FAILURE);
} else {
((void (*)(m_dmg_t *m_dmg))m_gb_instr[m_dmg->m_cpu->m_opcode].m_funct)(m_dmg);
}
break;
case 1:
if (m_gb_instr[m_dmg->m_cpu->m_opcode].m_funct == NULL)
{
printf("Unimplemented Opcode 0x%02X\n", m_dmg->m_cpu->m_opcode);
m_printregs(m_dmg);
exit(EXIT_FAILURE);
} else {
if ((m_dmg->m_cpu->m_opcode == 0x18) | (m_dmg->m_cpu->m_opcode == 0x20) | (m_dmg->m_cpu->m_opcode == 0x28))
{
((void (*)(m_dmg_t *m_dmg, uint8_t))m_gb_instr[m_dmg->m_cpu->m_opcode].m_funct)(m_dmg, (int8_t) m_dmg->m_cpu->m_boperand);
} else {
((void (*)(m_dmg_t *m_dmg, uint8_t))m_gb_instr[m_dmg->m_cpu->m_opcode].m_funct)(m_dmg, (uint8_t) m_dmg->m_cpu->m_boperand);
}
}
break;
case 2:
if (m_gb_instr[m_dmg->m_cpu->m_opcode].m_funct == NULL)
{
printf("Unimplemented Opcode 0x%02X\n", m_dmg->m_cpu->m_opcode);
m_printregs(m_dmg);
exit(EXIT_FAILURE);
} else {
((void (*)(m_dmg_t *m_dmg, uint16_t))m_gb_instr[m_dmg->m_cpu->m_opcode].m_funct)(m_dmg, (uint16_t) m_dmg->m_cpu->m_woperand);
}
break;
default:
break;
}
if (m_dmg->m_cpu->m_opcode == 0xCB)
{
if (!m_dmg->m_speedhack) { m_dmg->m_cpu->m_cpu_ticks += m_ticks_by_cbopcode[m_fetchopbyte(m_dmg)]; } else { m_dmg->m_cpu->m_cpu_ticks = m_ticks_by_cbopcode[m_fetchopbyte(m_dmg)]; }
return m_ticks_by_cbopcode[m_fetchopbyte(m_dmg)];
}
else
{
if (!m_dmg->m_speedhack) { m_dmg->m_cpu->m_cpu_ticks += m_ticks_by_opcode[m_dmg->m_cpu->m_opcode]; } else { m_dmg->m_cpu->m_cpu_ticks = m_ticks_by_opcode[m_dmg->m_cpu->m_opcode]; }
return m_ticks_by_opcode[m_dmg->m_cpu->m_opcode];
}
}
void m_printregs(m_dmg_t *m_dmg)
{
printf("\n\033[1;31mGeneral-Purpose Registers:\033[0m\n");
printf("\033[0;35mA:\033[0m 0x%02X, \033[0;35mF:\033[0m 0x%02X; \033[0;35mAF:\033[0m 0x%04X\n", A_REG, F, AF);
printf("\033[0;35mB:\033[0m 0x%02X, \033[0;35mC:\033[0m 0x%02X; \033[0;35mBC:\033[0m 0x%04X\n", B_REG, C, BC);
printf("\033[0;35mD:\033[0m 0x%02X, \033[0;35mE:\033[0m 0x%02X; \033[0;35mDE:\033[0m 0x%04X\n", D, E, DE);
printf("\033[0;35mH:\033[0m 0x%02X, \033[0;35mL:\033[0m 0x%02X; \033[0;35mHL:\033[0m 0x%04X\n\n", H, L, HL);
printf("\033[1;33mSegment Registers:\033[0:0m\n");
printf("\033[1;35mSP:\033[0m 0x%04X\n\n", SP);
printf("\033[1;32mProgram Flow Registers:\033[0:0m\n");
printf("\033[1;35mPC:\033[0m 0x%04X\n\n", PC);
printf("\033[1;34mInterrupts: \033[0m%d\n\n", (int) m_dmg->m_cpu->interrupts->m_master);
printf("\033[1;35mFlags:\033[0:0m\n");
printf("\033[1;35mZ: ");
#if defined(PREC23) || !defined(USE_GCC)
if (FLAG_CHECK(ZERO))
#else
if (m_is_bit_set(FLAGS, ZERO))
#endif
{
printf("\033[0m1 ");
} else {
printf("\033[0m0 ");
}
printf("\033[1;35mN: ");
#if defined(PREC23) || !defined(USE_GCC)
if (FLAG_CHECK(NGTV))
#else
if (m_is_bit_set(FLAGS, NGTV))
#endif
{
printf("\033[0m1 \n");
} else {
printf("\033[0m0 \n");
}
printf("\033[1;35mH: ");
#if defined(PREC23) || !defined(USE_GCC)
if (FLAG_CHECK(HALF))
#else
if (m_is_bit_set(FLAGS, HALF))
#endif
{
printf("\033[0m1 ");
} else {
printf("\033[0m0 ");
}
printf("\033[1;35mC: ");
#if defined(PREC23) || !defined(USE_GCC)
if (FLAG_CHECK(CRRY))
#else
if (m_is_bit_set(FLAGS, CRRY))
#endif
{
printf("\033[0m1 \n\n");
} else {
printf("\033[0m0 \n\n");
}
printf("\033[1;35mCurrent CPU Ticks:\033[0m %lu\n", m_dmg->m_cpu->m_cpu_ticks);
printf("\033[1;35mCurrent GPU Ticks:\033[0m %lu\n", m_dmg->ppu->m_ticks);
}