-
Notifications
You must be signed in to change notification settings - Fork 0
/
chip8.cpp
60 lines (48 loc) · 1.43 KB
/
chip8.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
#include "unistd.h"
#include "chip8.h"
/**
* 0x000-0x1FF Chip 8 interpreter
* 0x050-0x0A0 4x5 pixel font set
* 0x200-0xFFF Program ROM and work RAM
*/
namespace Chip8 {
Chip8::Chip8() {
SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO);
_graphics.reset((new Graphics));
_memory.reset((new Memory));
_cpu.reset((new CPU(_graphics.get(), _memory.get())));
}
Chip8::~Chip8() {
SDL_Quit();
}
void Chip8::load_program(std::ifstream& program) {
program.seekg(0, std::ios::end);
int size = program.tellg();
program.seekg(0, std::ios::beg);
for (int i=0; i<size; i++) {
_memory->putByte(0x200 + i, program.get());
}
}
void Chip8::run() {
bool quit = false;
SDL_Event event;
while (!quit) {
_cpu->run_cycle();
if (_graphics->dirty())
_graphics->refresh();
while (SDL_PollEvent(&event)) {
if (event.type == SDL_QUIT)
quit = true;
if (event.type == SDL_KEYDOWN) {
if (event.key.keysym.sym == SDLK_s) {
_cpu->run_cycle();
_graphics->refresh();
}
if (event.key.keysym.sym == SDLK_q) {
quit = true;
}
}
}
}
}
} // namespace Chip8