-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchip8.cpp
More file actions
160 lines (135 loc) · 4.96 KB
/
chip8.cpp
File metadata and controls
160 lines (135 loc) · 4.96 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
#include <cstdio>
#include <cstdlib>
#include <stdexcept>
#include "chip8screen.hpp"
#include "chip8timer.hpp"
#include "chip8keyboard.hpp"
#include "chip8cpu.hpp"
class Chip8Emu {
private:
unsigned char* memory;
Chip8Screen* screen;
Chip8Cpu* cpu;
Chip8Timer* delay_timer;
Chip8Timer* sound_timer;
Chip8Keyboard* keyboard;
public:
Chip8Emu(Chip8Screen& screen, Chip8Cpu& cpu, Chip8Timer& delay_timer, Chip8Timer& sound_timer, Chip8Keyboard& keyboard) {
// allocate memory
this->memory = new unsigned char [4096];
// check for allocation errors
if (this->memory == NULL)
throw std::runtime_error("Failed to allocate memory!");
// erase memory (init)
for (size_t i = 0; i < 4096; i++)
this->memory[i] = 0;
char fonts [] = { 0xF0, 0x90, 0x90, 0x90, 0xF0,
0x20, 0x60, 0x20, 0x20, 0x70,
0xF0, 0x10, 0xF0, 0x80, 0xF0,
0xF0, 0x10, 0xF0, 0x10, 0xF0,
0x90, 0x90, 0xF0, 0x10, 0x10,
0xF0, 0x80, 0xF0, 0x10, 0xF0,
0xF0, 0x80, 0xF0, 0x90, 0xF0,
0xF0, 0x10, 0x20, 0x40, 0x40,
0xF0, 0x90, 0xF0, 0x90, 0xF0,
0xF0, 0x90, 0xF0, 0x10, 0xF0,
0xF0, 0x90, 0xF0, 0x90, 0x90,
0xE0, 0x90, 0x90, 0x90, 0xE0,
0xF0, 0x80, 0xF0, 0x80, 0xF0,
0xF0, 0x80, 0xF0, 0x80, 0x80};
for (size_t i = 0; i < 80; i++)
this->memory[i] = fonts[i];
this->screen = &screen;
this->cpu = &cpu;
this->delay_timer = &delay_timer;
this->sound_timer = &sound_timer;
this->keyboard = &keyboard;
}
~Chip8Emu() {
// cleanup
delete[] this->memory;
}
bool play(bool debug) {
unsigned long ticks = SDL_GetTicks();
unsigned short int tick_delta = 0;
SDL_Event event;
while (true) {
tick_delta = SDL_GetTicks() - ticks;
if (SDL_PollEvent(&event) && event.type == SDL_QUIT)
break;
else if (event.type == SDL_KEYDOWN || event.type == SDL_KEYUP)
this->keyboard->notify(event);
if (debug) {
if (event.type == SDL_KEYDOWN && event.key.keysym.sym == SDLK_n)
this->cpu->cycle(this->memory, this->screen, this->delay_timer, this->sound_timer, this->keyboard);
} else {
this->cpu->cycle(this->memory, this->screen, this->delay_timer, this->sound_timer, this->keyboard);
}
if (tick_delta >= 17) {
tick_delta = 0;
ticks = SDL_GetTicks();
this->delay_timer->tick();
this->sound_timer->tick();
}
// this->print_screen_memory();
// SDL_Delay(500);
}
return true;
}
bool load_game(const char* filename) {
// open the specified file
if (FILE* game_file = fopen(filename, "rb")) {
// calculate file size
fseek(game_file, 0, SEEK_END);
size_t file_size = ftell(game_file);
// if the file wouldn't fit in memory (4096 - 512 - 256 - 96)
if (file_size > 3232) {
printf("* File too large! (%d)\n", file_size);
return false;
}
// read the file and copy it to memory
rewind(game_file);
size_t result = fread(this->memory + 512, 1, file_size, game_file);
if (result != file_size) {
printf("* File reading failed! (%d/%d)\n", result, file_size);
return false;
}
return true;
} else {
printf("* Unable to open file! (%s)\n", filename);
return false;
}
}
void print_memory() {
for (size_t i = 0; i < 4096; i += 2) {
if (i % 16 == 0)
printf("\n%08x: ", i);
printf("%02x%02x ", this->memory[i], this->memory[i+1]);
}
printf("\n");
}
void print_screen_memory() {
for (size_t i = 0xF00; i < 0xFFF; i += 2) {
if (i % 16 == 0)
printf("\n%08x: ", i);
printf("%02x%02x ", this->memory[i], this->memory[i+1]);
}
printf("\n");
}
};
int main() {
try {
Chip8Screen screen = Chip8Screen(8);
Chip8Cpu cpu = Chip8Cpu(400);
Chip8Timer delay_timer = Chip8Timer();
Chip8Timer sound_timer = Chip8Timer();
Chip8Keyboard keyboard = Chip8Keyboard();
Chip8Emu emu = Chip8Emu(screen, cpu, delay_timer, sound_timer, keyboard);
if (emu.load_game("spaceinvaders.ch8"))
emu.play(false);
return 0;
} catch (const std::exception& e) {
printf("* Runtime error! %s\n", e.what());
return 1;
}
}