-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChip8Emulator.cpp
More file actions
462 lines (409 loc) · 15.9 KB
/
Copy pathChip8Emulator.cpp
File metadata and controls
462 lines (409 loc) · 15.9 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
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
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
/**
* Chip-8 emulator
*
* Chip-8 is a simple, interpreted, programming language which was first used on some do-it-yourself computer systems
* in the late 1970s and early 1980s. The COSMAC VIP, DREAM 6800, and ETI 660 computers are a few examples.
* These computers typically were designed to use a television as a display, had between 1 and 4K of RAM, and used a 16-key hexadecimal keypad for input.
* The interpreter took up only 512 bytes of memory, and programs, which were entered into the computer in hexadecimal, were even smaller.
*
* In the early 1990s, the Chip-8 language was revived by a man named Andreas Gustafsson. He created a Chip-8 interpreter for the HP48 graphing calculator,
* called Chip-48. The HP48 was lacking a way to easily make fast games at the time, and Chip-8 was the answer. Chip-48 later begat Super Chip-48,
* a modification of Chip-48 which allowed higher resolution graphics, as well as other graphical enhancements.
*
* Chip-48 inspired a whole new crop of Chip-8 interpreters for various platforms, including MS-DOS, Windows 3.1, Amiga, HP48, MSX, Adam, and ColecoVision.
*
* Technical Reference(used for this emulator): http://devernay.
.fr/hacks/chip8/C8TECH10.HTM
*
* More information: https://en.wikipedia.org/wiki/CHIP-8
*
* @author James Kozlowski
* @version April 2, 2017
*/
#include <stdio.h>
#include <SFML/Graphics.hpp>
#include <SFML/Audio.hpp>
#include <iostream>
#include <sstream>
#include <string.h>
#include <iomanip>
#include "Chip8.h"
#include "Chip8Emulator.h"
#include "Chip8Disassembler.h"
#include "Chip8Assembler.h"
using namespace std;
//the chip-8 object
Chip8CPU mychip8;
//factor each pixel by this value
const int modifier = 10;
//set screen width and height
const int width = 64 * modifier + 650;
const int height = 32 * modifier + 300;
//if true emulator is running, if false emulator is paused
bool run = true;
//used for displaying the memory in the debugger
int displayMemLocation;
//holds the breakpoint
int breakpoint = -1;
using namespace std;
int main(int argc, char **argv)
{
if (argc < 2)
{
PrintHelp();
return 0;
}
//assemble a file
if (strcmp(argv[1], "-a") == 0)
{
if (argc < 4)
PrintHelp();
else if (!Chip8AssProcessFile(argv[2], argv[3]))
{
cout << endl << "Error reading file" << endl;
PrintHelp();
}
//we are done so exit
return 0;
}
//disassemble a file
if (strcmp(argv[1], "-d") == 0)
{
if (argc < 4)
PrintHelp();
else if (!Chip8DisProcessFile (argv[2], argv[3]))
{
cout << endl << "Error reading file" << endl;
PrintHelp();
}
//we are done so exit
return 0;
}
//otherwise we must want to play a game;
//reset the CPU
Chip8Reset(&mychip8);
//try to load the rom file
if (Chip8LoadRom(&mychip8, argv[1]) == false)
{
cout << endl << "Error loading file" << endl;
PrintHelp();
return 0;
}
//setup and open a window
sf::ContextSettings settings;
settings.depthBits = 0;
settings.stencilBits = 0;
settings.antialiasingLevel = 0;
settings.majorVersion = 3;
settings.minorVersion = 0;
sf::RenderWindow window(sf::VideoMode(width, height), "Chip8 Emu", sf::Style::Default, settings);
window.setVerticalSyncEnabled(false);
//window.setFramerateLimit(60);
//load a font
sf::Font font;
if (!font.loadFromFile("DroidSansMono.ttf"))
{
cout << "Error loading Font (DroidSansMono.ttf)";
}
//main loop
while (window.isOpen())
{
sf::Event event;
while (window.pollEvent(event))
{
if (event.type == sf::Event::Closed)
window.close();
//check for key presses
else if (event.type == sf::Event::KeyPressed)
{
//program/debugger keys
if (event.key.code == sf::Keyboard::Escape)
exit(0);
else if (event.key.code == sf::Keyboard::Space)
run = !run;
else if (!run && event.key.code == sf::Keyboard::N)
{
Chip8EmulateCycle(&mychip8);
displayMemLocation = mychip8.pc;
}
else if (!run && event.key.code == sf::Keyboard::Down && displayMemLocation < 4074)
displayMemLocation += 2;
else if (!run && event.key.code == sf::Keyboard::Up && displayMemLocation > 22)
displayMemLocation -= 2;
else if (!run && event.key.code == sf::Keyboard::PageDown && displayMemLocation > 36 )
displayMemLocation += 16;
else if (!run && event.key.code == sf::Keyboard::PageUp && displayMemLocation < 4080 )
displayMemLocation -= 16;
else if (!run && event.key.code == sf::Keyboard::B)
{
if (breakpoint == displayMemLocation)
breakpoint = -1;
else
breakpoint = displayMemLocation;
}
else if (!run && event.key.code == sf::Keyboard::M)
{
mychip8.pc = displayMemLocation;
run = true;
}
//load state keys
else if (event.key.code == sf::Keyboard::F1)
Chip8SaveState(&mychip8, (char*)"state.c8");
else if (event.key.code == sf::Keyboard::F2)
Chip8LoadState(&mychip8, (char*)"state.c8");
//gamepad keys
else if (event.key.code == sf::Keyboard::Num1)
mychip8.key[0x1] = 1;
else if (event.key.code == sf::Keyboard::Num2)
mychip8.key[0x2] = 1;
else if (event.key.code == sf::Keyboard::Num3)
mychip8.key[0x3] = 1;
else if (event.key.code == sf::Keyboard::Num4)
mychip8.key[0xC] = 1;
else if (event.key.code == sf::Keyboard::Q)
mychip8.key[0x4] = 1;
else if (event.key.code == sf::Keyboard::W)
mychip8.key[0x5] = 1;
else if (event.key.code == sf::Keyboard::E)
mychip8.key[0x6] = 1;
else if (event.key.code == sf::Keyboard::R)
mychip8.key[0xD] = 1;
else if (event.key.code == sf::Keyboard::A)
mychip8.key[0x7] = 1;
else if (event.key.code == sf::Keyboard::S)
mychip8.key[0x8] = 1;
else if (event.key.code == sf::Keyboard::D)
mychip8.key[0x9] = 1;
else if (event.key.code == sf::Keyboard::F)
mychip8.key[0xE] = 1;
else if (event.key.code == sf::Keyboard::Z)
mychip8.key[0xA] = 1;
else if (event.key.code == sf::Keyboard::X)
mychip8.key[0x0] = 1;
else if (event.key.code == sf::Keyboard::C)
mychip8.key[0xB] = 1;
else if (event.key.code == sf::Keyboard::V)
mychip8.key[0xF] = 1;
}
//key up events to clear gamepad keys
else if (event.type == sf::Event::KeyReleased)
{
if (event.key.code == sf::Keyboard::Num1)
mychip8.key[0x1] = 0;
else if (event.key.code == sf::Keyboard::Num2)
mychip8.key[0x2] = 0;
else if (event.key.code == sf::Keyboard::Num3)
mychip8.key[0x3] = 0;
else if (event.key.code == sf::Keyboard::Num4)
mychip8.key[0xC] = 0;
else if (event.key.code == sf::Keyboard::Q)
mychip8.key[0x4] = 0;
else if (event.key.code == sf::Keyboard::W)
mychip8.key[0x5] = 0;
else if (event.key.code == sf::Keyboard::E)
mychip8.key[0x6] = 0;
else if (event.key.code == sf::Keyboard::R)
mychip8.key[0xD] = 0;
else if (event.key.code == sf::Keyboard::A)
mychip8.key[0x7] = 0;
else if (event.key.code == sf::Keyboard::S)
mychip8.key[0x8] = 0;
else if (event.key.code == sf::Keyboard::D)
mychip8.key[0x9] = 0;
else if (event.key.code == sf::Keyboard::F)
mychip8.key[0xE] = 0;
else if (event.key.code == sf::Keyboard::Z)
mychip8.key[0xA] = 0;
else if (event.key.code == sf::Keyboard::X)
mychip8.key[0x0] = 0;
else if (event.key.code == sf::Keyboard::C)
mychip8.key[0xB] = 0;
else if (event.key.code == sf::Keyboard::V)
mychip8.key[0xF] = 0;
}
}
//if the emulator is not paused
if(run)
{
//if we are about to process the breakpoint line
if (mychip8.pc == breakpoint - 2)
run = false;
Chip8EmulateCycle(&mychip8);
displayMemLocation = mychip8.pc;
}
//display the UI and game screen
window.clear();
DrawUI(&window, &font);
DrawGameScreen(&window);
window.display();
//play a beep if needed (not done yet)
if (mychip8.playBeep)
{
mychip8.playBeep = false;
}
}
return 0;
}
/**
* Draws the UI
*
* @param window the SF::RenderWindow
* @return none
*/
void DrawUI(sf::RenderWindow *window, sf::Font *font)
{
//print out box around screen
sf::RectangleShape rectangle(sf::Vector2f(648, 330));
rectangle.setFillColor(sf::Color::Black);
rectangle.setOutlineThickness(2);
rectangle.setOutlineColor(sf::Color::Red);
rectangle.setPosition(2, 2);
//print out registers
stringstream os;
os << "REGISTERS" << endl;
os << "Reg " << "Value " << endl << "------------"<< endl;
os << "PC: " << setfill('0') << setw(4) << hex << (int)mychip8.pc << endl;
os << "I: " << setfill('0') << setw(4) << hex << (int)mychip8.I << endl << endl;
for (int i = 0; i < 16; i++)
os << "V" << hex << (int) i << ": " << hex << (int)mychip8.V[i] << endl;
os << endl;
os << "DT: " << hex << (int)mychip8.delayTimer << endl;
os << "ST: " << hex << (int)mychip8.soundTimer << endl;
sf::Text text;
text.setFont(*font);
text.setString(os.str());
text.setCharacterSize(20);
text.setColor(sf::Color(173, 173, 173));
text.setPosition(690, 20);
//print out memory
stringstream mem;
mem << "MEMORY" << endl;
mem << "B Loc " << " Value " << " Opcode" << endl << "---------------------------------"<< endl;
char buffer[50];
for(int i = displayMemLocation - 20; i <= displayMemLocation + 20; i += 2)
{
int value = ((int)mychip8.memory[i] << 8) | (int)mychip8.memory[i + 1];
Chip8Disassemble(value, buffer);
//if this line is set as a break display the *
if (i == breakpoint)
{
mem << "* " << setfill('0') << setw(4) << hex << (int)i << ":\t";
mem << setfill('0') << setw(2) << hex << (int)mychip8.memory[i];
mem << setfill('0') << setw(2) << hex << (int)mychip8.memory[i + 1];
mem << " " << buffer << endl;
}
else
{
mem << " " << setfill('0') << setw(4) << hex << (int)i << ":\t";
mem << setfill('0') << setw(2) << hex << (int)mychip8.memory[i];
mem << setfill('0') << setw(2) << hex << (int)mychip8.memory[i + 1];
mem << " " << buffer << endl;
}
}
sf::Text memText;
memText.setFont(*font);
memText.setString(mem.str());
memText.setCharacterSize(20);
memText.setColor(sf::Color(173, 173, 173));
memText.setPosition(865, 20);
//current memory marker
sf::RectangleShape memrectangle(sf::Vector2f(400, 20));
memrectangle.setFillColor(sf::Color(1, 112, 10));
//if the line is a break mark it red
if (displayMemLocation == breakpoint)
memrectangle.setFillColor(sf::Color::Red);
memrectangle.setPosition(860, 323);
//printout keypad
stringstream keypadstream;
keypadstream << "KEYPAD INPUT" << endl;
keypadstream << "-----------------" << endl;
keypadstream << "| 1 | 2 | 3 | 4 |" << endl;
keypadstream << "| Q | W | E | R |" << endl;
keypadstream << "| A | S | D | F |" << endl;
keypadstream << "| Z | X | C | V |" << endl;
keypadstream << "-----------------" << endl;
sf::Text keypadText;
keypadText.setFont(*font);
keypadText.setString(keypadstream.str());
keypadText.setCharacterSize(14);
keypadText.setColor(sf::Color(173, 173, 173));
keypadText.setPosition(20, 350);
//print out instructions
stringstream instructions;
instructions << "INSTRUCTIONS" << endl;
instructions << "---------------------" << endl;
instructions << "ESC: Quit" << endl;
instructions << "F1: Save State" << endl;
instructions << "F2: Load State" << endl << endl;
instructions << "SPACE: Pause/Run Emulation" << endl;
instructions << "N: Step forward" << endl << endl;
instructions << "While paused:" << endl;
instructions << "Up/Down: Move memory location" << endl;
instructions << "PgUp/PgDn: Move memory location page " << endl;
instructions << "B: Set break point" << endl;
instructions << "M: Run from here (Not recomended)" << endl;
sf::Text instructionsText;
instructionsText.setFont(*font);
instructionsText.setString(instructions.str());
instructionsText.setCharacterSize(14);
instructionsText.setColor(sf::Color(173, 173, 173));
instructionsText.setPosition(250, 350);
//draw eash object to the screen
window->draw(keypadText);
window->draw(instructionsText);
window->draw(memrectangle);
window->draw(memText);
window->draw(text);
window->draw(rectangle);
}
/**
* Draws the game screen in the game screen area
*
* @param window the SF::RenderWindow
* @return none
*/
void DrawGameScreen(sf::RenderWindow *window)
{
int screeny = 32;
int screenx = 64;
int modifierFactor = modifier;
if (mychip8.extendedGraphicsMode == true)
{
screeny = 64;
screenx = 128;
modifierFactor = modifierFactor / 2;
}
sf::Uint8 screen[screeny * screenx * 4];
memset (screen, 0, screeny * screenx * 4);
for(int y = 0; y < screeny; y++)
for(int x = 0; x < screenx; x++)
if(mychip8.videoMemory[(y*screenx) + x] == 1)
{
screen[(x * 4) + (y * screenx * 4) + 0] = 255;
screen[(x * 4) + (y * screenx * 4) + 1] = 255;
screen[(x * 4) + (y * screenx * 4) + 2] = 255;
screen[(x * 4) + (y * screenx * 4) + 3] = 255;
}
sf::Image image;
image.create(screenx, screeny, screen);
sf::Texture texture;
texture.create(screenx, screeny);
texture.update(image);
sf::Sprite sprite;
sprite.setTexture(texture);
sprite.setScale(modifierFactor,modifierFactor);
sprite.setPosition(4,4);
window->draw(sprite);
}
/**
* prints out how to use the program
*
* @return none
*/
void PrintHelp()
{
cout << endl << "Error, please use one of the following commands" << endl;
cout << "to play a game: Chip8Emu gamefile.c8" << endl;
cout << "To assemble a file: Chip8Emu -a filenamein.ca filename out.c8" << endl;
cout << "To disassemble a file: Chip8Emu -d filenamein.ca filename out.c8" << endl << endl;
}