-
Notifications
You must be signed in to change notification settings - Fork 0
/
emulator.cpp
443 lines (370 loc) · 9.5 KB
/
emulator.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
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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
#include "emulator.h"
Emulator::Emulator()
{
cpu.init(&memory);
display.init(&memory);
}
// Íà÷àëî ýìóëÿöèè CPU
void Emulator::run()
{
sf::Time time;
while (display.window.isOpen())
{
// CPU cycles to emulate per frame draw
float cycles_per_frame = cpu.CLOCK_SPEED / framerate;
float time_between_frames = 1000 / framerate;
// Òåêóùèé öèêë â êàäðå
int current_cycle = 0;
handle_events();
while (current_cycle < cycles_per_frame)
{
Opcode code = memory.read(cpu.reg_PC);
cpu.parse_opcode(code);
current_cycle += cpu.num_cycles;
update_timers(cpu.num_cycles);
update_scanline(cpu.num_cycles);
do_interrupts();
cpu.num_cycles = 0;
}
//display.render();
//cout << "frame " << current_cycle << endl;
current_cycle = 0;
int frame_time = time.asMilliseconds();
float sleep_time = time_between_frames - frame_time;
if (frame_time < time_between_frames)
sf::sleep(sf::milliseconds(sleep_time));
time = time.Zero;
//cout << display.scanlines_rendered << endl;
display.scanlines_rendered = 0;
}
}
// Îáðàáîòêà ñîáûòèé îêíà è ââîäà-âûâîäà
void Emulator::handle_events()
{
sf::Event event;
while (display.window.pollEvent(event))
{
switch (event.type)
{
case sf::Event::Closed:
display.window.close();
break;
case sf::Event::KeyPressed:
key_pressed(event.key.code);
break;
case sf::Event::KeyReleased:
key_released(event.key.code);
break;
}
}
}
void Emulator::key_pressed(Key key)
{
// Ôóíêöèîíàëüíûå êëàâèøè F1-F12
if (key >= 85 && key <= 96)
{
int id = key - 84;
if (sf::Keyboard::isKeyPressed(Key::LShift))
save_state(id);
else
load_state(id);
return;
}
if (key == Key::Space)
{
cpu.CLOCK_SPEED *= 100;
return;
}
int key_id = get_key_id(key);
if (key_id < 0)
return;
bool directional = false;
if (key == Key::Up || key == Key::Down || key == Key::Left || key == Key::Right)
{
directional = true;
}
Byte joypad = (directional) ? memory.joypad_arrows : memory.joypad_buttons;
bool unpressed = is_bit_set(joypad, key_id);
if (!unpressed)
return;
if (directional)
memory.joypad_arrows = clear_bit(joypad, key_id);
else
memory.joypad_buttons = clear_bit(joypad, key_id);
request_interrupt(INTERRUPT_JOYPAD);
}
void Emulator::key_released(Key key)
{
if (key == Key::Space)
{
cpu.CLOCK_SPEED /= 100;
}
int key_id = get_key_id(key);
if (key_id < 0)
return;
bool directional = false;
if (key == Key::Up || key == Key::Down || key == Key::Left || key == Key::Right)
{
directional = true;
}
Byte joypad = (directional) ? memory.joypad_arrows : memory.joypad_buttons;
bool unpressed = is_bit_set(joypad, key_id);
if (unpressed)
return;
if (directional)
memory.joypad_arrows = set_bit(joypad, key_id);
else
memory.joypad_buttons = set_bit(joypad, key_id);
}
int Emulator::get_key_id(Key key)
{
switch (key)
{
case Key::A:
case Key::Right:
return BIT_0;
case Key::S: // B
case Key::Left:
return BIT_1;
case Key::X: // select
case Key::Up:
return BIT_2;
case Key::Z:
case Key::Down:
return BIT_3;
default:
return -1;
}
}
void Emulator::update_divider(int cycles)
{
divider_counter += cycles;
if (divider_counter >= 256) // 16384 Hz
{
divider_counter = 0;
memory.DIV.set(memory.DIV.get() + 1);
}
}
// ×èñëî öèêëîâ opcode ìîæåò ïîòðåáîâàòü êîððåêöèè, èñïîëüçóþòñÿ çíà÷åíèÿ Nintendo
void Emulator::update_timers(int cycles)
{
update_divider(cycles);
// Ýòî ìîæíî îïòèìèçèðîâàòü ïðè íåîáõîäèìîñòè
Byte new_freq = get_timer_frequency();
if (timer_frequency != new_freq)
{
set_timer_frequency();
timer_frequency = new_freq;
}
if (timer_enabled())
{
timer_counter -= cycles;
// äîñòàòî÷íî òàêòîâûõ öèêëîâ ïðîøëî äëÿ îáíîâëåíèÿ òàéìåðà
if (timer_counter <= 0)
{
Byte timer_value = memory.TIMA.get();
set_timer_frequency();
// Òàéìåð ïåðåïîëíèòñÿ, ãåíåðèðîâàòü ïðåðûâàíèå
if (timer_value == 255)
{
memory.TIMA.set(memory.TMA.get());
request_interrupt(INTERRUPT_TIMER);
}
else
{
memory.TIMA.set(timer_value + 1);
}
}
}
}
bool Emulator::timer_enabled()
{
return memory.TAC.is_bit_set(BIT_2);
}
Byte Emulator::get_timer_frequency()
{
return (memory.TAC.get() & 0x3);
}
void Emulator::set_timer_frequency()
{
Byte frequency = get_timer_frequency();
timer_frequency = frequency;
switch (frequency)
{
// timer_counter calculated by (Clock Speed / selected frequency)
case 0: timer_counter = 1024; break; // 4096 Hz
case 1: timer_counter = 16; break; // 262144 Hz
case 2: timer_counter = 64; break; // 65536 Hz
case 3: timer_counter = 256; break; // 16384 Hz
}
}
void Emulator::request_interrupt(Byte id)
{
memory.IF.set_bit(id);
}
void Emulator::do_interrupts()
{
// Åñëè åñòü óñòàíîâëåííûå ïðåðûâàíèÿ
if (memory.IF.get() > 0)
{
// Âîçîáíîâèòü ñîñòîÿíèå CPU, åñëè îíî ïðèîñòàíîâëåíî, è åñòü îæèäàþùèå ïðåðûâàíèÿ
if (memory.IE.get() > 0)
{
if (cpu.halted)
{
cpu.halted = false;
cpu.reg_PC += 1;
}
}
// Ïåðåáèðàåì êàæäûé áèò è âûçûâàåì ïðåðûâàíèå äëÿ óñòàíîâëåííûõ áèòîâ ñ íàèìåíüøèì è íàèâûñøèì ïðèîðèòåòîì
for (int i = 0; i < 5; i++)
{
if (memory.IF.is_bit_set(i))
{
if (memory.IE.is_bit_set(i))
{
// IME òîëüêî îòêëþ÷àåò îáñëóæèâàíèå ïðåðûâàíèé,
// à íå âñþ ôóíêöèîíàëüíîñòü ïðåðûâàíèÿ
if (cpu.interrupt_master_enable)
{
service_interrupt(i);
}
}
}
}
}
}
void Emulator::service_interrupt(Byte id)
{
cpu.interrupt_master_enable = false;
memory.IF.clear_bit(id);
// Ïîìåùàåì òåêóùèé àäðåñ âûïîëíåíèÿ â ñòåê
memory.write(--cpu.reg_SP, high_byte(cpu.reg_PC));
memory.write(--cpu.reg_SP, low_byte(cpu.reg_PC));
switch (id)
{
case INTERRUPT_VBLANK: cpu.reg_PC = 0x40; break;
case INTERRUPT_LCDC: cpu.reg_PC = 0x48; break;
case INTERRUPT_TIMER: cpu.reg_PC = 0x50; break;
case INTERRUPT_SERIAL: cpu.reg_PC = 0x58; break;
case INTERRUPT_JOYPAD: cpu.reg_PC = 0x60; break;
}
}
void Emulator::set_lcd_status()
{
Byte status = memory.STAT.get();
Byte current_line = memory.LY.get();
// èçâëå÷ü òåêóùèé ðåæèì LCD
Byte current_mode = status & 0x03;
Byte mode = 0;
bool do_interrupt = false;
// â VBLANK óñòàíàâëèâàåì ðåæèì 1
if (current_line >= 144)
{
mode = 1; //  ïåðèîäå âåðòèêàëüíîé ðàçâ¸ðòêè
// 1 â äâîè÷íîì âèäå
status = set_bit(status, BIT_0);
status = clear_bit(status, BIT_1);
do_interrupt = is_bit_set(status, BIT_4);
}
else
{
int mode2_threshold = 456 - 80;
int mode3_threshold = mode2_threshold - 172;
if (scanline_counter >= mode2_threshold)
{
mode = 2; // Ïîèñê â OAM RAM
// 2 â äâîè÷íîì âèäå
status = set_bit(status, BIT_1);
status = clear_bit(status, BIT_0);
do_interrupt = is_bit_set(status, BIT_5);
}
else if (scanline_counter >= mode3_threshold)
{
mode = 3; // Ïåðåäà÷à äàííûõ â äðàéâåð LCD
// 3 â äâîè÷íîì âèäå
status = set_bit(status, BIT_1);
status = set_bit(status, BIT_0);
}
else
{
mode = 0; // ÖÏ èìååò äîñòóï êî âñåé îïåðàòèâíîé ïàìÿòè äèñïëåÿ
// Åñëè ïåðâûé ðàç âñòðå÷àåòñÿ H-blank, îáíîâëÿåì ñòðîêó ñêàíèðîâàíèÿ
if (current_mode != mode)
{
// íàðèñîâàòü òåêóùóþ ñòðîêó ñêàíèðîâàíèÿ íà ýêðàíå
if (current_line < 144 && display.scanlines_rendered <= 144)
display.update_scanline(current_line);
}
// 0 â äâîè÷íîì âèäå
status = clear_bit(status, BIT_1);
status = clear_bit(status, BIT_0);
do_interrupt = is_bit_set(status, BIT_3);
}
}
// Âîøëè â íîâûé ðåæèì, çàïðîñèòü ïðåðûâàíèå
if (do_interrupt && (mode != current_mode))
request_interrupt(INTERRUPT_LCDC);
// ïðîâåðèòü ôëàã ñîâïàäåíèÿ, óñòàíîâèòü áèò 2, åñëè îí ñîâïàäàåò
if (memory.LY.get() == memory.LYC.get())
{
status = set_bit(status, BIT_2);
if (is_bit_set(status, BIT_6))
request_interrupt(INTERRUPT_LCDC);
}
// ñáðîñèòü áèò 2, åñëè íåò
else
status = clear_bit(status, BIT_2);
memory.STAT.set(status);
memory.video_mode = mode;
}
void Emulator::update_scanline(int cycles)
{
scanline_counter -= cycles;
set_lcd_status();
if (memory.LY.get() > 153)
memory.LY.clear();
// Ïðîøëî äîñòàòî÷íî âðåìåíè äëÿ îòðèñîâêè ñëåäóþùåé ñòðîêè ñêàíèðîâàíèÿ
if (scanline_counter <= 0)
{
Byte current_scanline = memory.LY.get();
// èíêðåìåíòèðîâàòü ñòðîêó ñêàíèðîâàíèÿ è ñáðîñèòü ñ÷¸ò÷èê
memory.LY.set(++current_scanline);
scanline_counter = 456;
// Âîøëè â ïåðèîä VBLANK
if (current_scanline == 144)
{
request_interrupt(INTERRUPT_VBLANK);
if (display.scanlines_rendered <= 144)
display.render();
}
// Ñáðîñèòü ñ÷¸ò÷èê, åñëè ïðåâûøåíî ìàêñèìàëüíîå çíà÷åíèå
else if (current_scanline > 153)
memory.LY.clear();
}
}
void Emulator::save_state(int id)
{
ofstream file;
string filename = "./saves/" + memory.rom_name + "_" + to_string(id) + ".sav";
file.open(filename, ios::binary | ios::trunc);
if (!file.bad())
{
cpu.save_state(file);
memory.save_state(file);
file.close();
cout << "çàïèñàíî ñîñòîÿíèå ñîõðàíåíèÿ " << id << endl;
}
}
void Emulator::load_state(int id)
{
string filename = "./saves/" + memory.rom_name + "_" + to_string(id) + ".sav";
ifstream file(filename, ios::binary);
if (file.is_open())
{
cpu.load_state(file);
memory.load_state(file);
file.close();
cout << "çàãðóæåíî ñîñòîÿíèå " << id << endl;
}
}