-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathemulator.c
293 lines (253 loc) · 5.16 KB
/
emulator.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
// CHIP-8 Emulator
#include "cpu.h"
#include "emulator.h"
#include "GL/glut.h"
Chip8 cpu_reg;
uint8_t memory[4096];
uint8_t video_buffer[WIDTH * HEIGHT];
uint16_t delay_timer; // Used for timeing of game events
uint16_t sound_timer; // Used for sound effects; beeps when nonzero
uint8_t keys[16]; // key states for hex keypad
int main(int argc, char **argv) {
srand(time(NULL));
initialize_cpu(&cpu_reg);
// Attempt to load ROM file. If file fails to open, terminate program
if (load_program("Tetris.ch8", memory + PROGRAM_START) == -1)
exit(1);
// Initialize GLUT and create the window
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA);
glutInitWindowPosition(200, 200);
glutInitWindowSize(640, 320);
glutCreateWindow("CHIP-8 Emulator");
initGLUT();
// Set callback functions
glutKeyboardFunc(key_down);
glutKeyboardUpFunc(key_up);
glutDisplayFunc(display);
glutIdleFunc(idle);
glutMainLoop();
return 0;
}
/*
* display()
* Inputs: None
* Return Value: None
* Function:
*/
void display() {
// execute one cpu cycle
fde_cycle(&cpu_reg);
draw_screen();
}
/*
* draw_screen()
* Inputs: None
* Return Value: None
* Function: Draw to the window screen
*/
void draw_screen() {
glClear(GL_COLOR_BUFFER_BIT); // clear the screen
glLoadIdentity(); // reset the view
glColor3f(1.0, 1.0, 1.0);
for (int i=0; i < HEIGHT; ++i) {
for (int j=0; j < WIDTH; ++j) {
if (video_buffer[j + i*WIDTH] == 1) {
float x = j * 10;
float y = i * 10;
// draw the pixel
glBegin(GL_QUADS);
glVertex3f(x, y, 0.0);
glVertex3f(x + 10, y, 0.0);
glVertex3f(x + 10, y + 10, 0.0);
glVertex3f(x, y + 10, 0.0);
glEnd();
}
}
}
glutSwapBuffers();
}
/*
* idle()
* Inputs: None
* Return Value: None
* Function: Post a re-paint request to activate display()
*/
void idle() {
glutPostRedisplay();
}
/*
* load_program()
* Inputs: filename - Name of the ROM file to be loaded
* mem_loc - Location in RAM where the ROM is to be read into
* Return Value: Returns 0 if file is read into ROM successfully;
* returns -1 on failure
* Function: Attempts to open the given filename and load it into RAM
*/
int load_program(const char *filename, uint8_t *mem_loc) {
FILE * f;
f = fopen(filename,"r");
if (f != NULL) {
if (fseek(f, 0, SEEK_END) == 0) {
uint32_t file_size = ftell(f);
fseek(f, 0, SEEK_SET); // go back to beginning of file
fread(mem_loc, sizeof(uint16_t), file_size, f);
fclose(f);
return 0;
}
}
return -1;
}
/*
* key_down()
* Inputs: key - ASCII char representing the key pressed in the window
* x - Unused
* y - Unused
* Return Value: None
* Function: Maps a key press to its corresponding hexpad value
*/
void key_down(unsigned char key, int x, int y) {
switch(key) {
case '1':
keys[0x0] = 1;
break;
case '2':
keys[0x1] = 1;
break;
case '3':
keys[0x2] = 1;
break;
case '4':
keys[0x3] = 1;
break;
case 'q':
keys[0x4] = 1;
break;
case 'w':
keys[0x5] = 1;
break;
case 'e':
keys[0x6] = 1;
break;
case 'r':
keys[0x7] = 1;
break;
case 'a':
keys[0x8] = 1;
break;
case 's':
keys[0x9] = 1;
break;
case 'd':
keys[0xA] = 1;
break;
case 'f':
keys[0xB] = 1;
break;
case 'z':
keys[0xC] = 1;
break;
case 'x':
keys[0xD] = 1;
break;
case 'c':
keys[0xE] = 1;
break;
case 'v':
keys[0xF] = 1;
break;
default:
break;
}
}
/*
* key_up()
* Inputs: key - ASCII char representing the key pressed in the window
* x - Unused
* y - Unused
* Return Value: None
* Function: Maps a key release to its corresponding hexpad value
*/
void key_up(unsigned char key, int x, int y) {
switch(key) {
case '1':
keys[0x0] = 0;
break;
case '2':
keys[0x1] = 0;
break;
case '3':
keys[0x2] = 0;
break;
case '4':
keys[0x3] = 0;
break;
case 'q':
keys[0x4] = 0;
break;
case 'w':
keys[0x5] = 0;
break;
case 'e':
keys[0x6] = 0;
break;
case 'r':
keys[0x7] = 0;
break;
case 'a':
keys[0x8] = 0;
break;
case 's':
keys[0x9] = 0;
break;
case 'd':
keys[0xA] = 0;
break;
case 'f':
keys[0xB] = 0;
break;
case 'z':
keys[0xC] = 0;
break;
case 'x':
keys[0xD] = 0;
break;
case 'c':
keys[0xE] = 0;
break;
case 'v':
keys[0xF] = 0;
break;
default:
break;
}
}
/*
* initGLUT()
* Inputs: None
* Return Value: None
* Function: Initializes GLUT for 2D drawing
*/
void initGLUT() {
glClearColor(0.0f, 0.0f, 0.0f, 1.0f); // black and opaque
// sets up GLUT window for 2D drawing
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0, WIDTH * 10, HEIGHT * 10, 0.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
/*****************************************************************/
/************** Helpful debugging tools **************/
/*****************************************************************/
/*
* hex_dump_ROM()
* Inputs: file_size - Size of the *.ch8 file read into ROM
* Return Value: None
* Function: Prints the ROM which is in RAM at location 0x0200
*/
void hex_dump_ROM(int file_size) {
int s = PROGRAM_START;
for (int i=0; i < file_size; i+=2)
printf("%02X%02X\n",memory[s+i],memory[s+i+1]);
}