-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
456 lines (428 loc) · 15.1 KB
/
main.cpp
File metadata and controls
456 lines (428 loc) · 15.1 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
//Code by Guigui220D
//https://github.com/Guigui220D/
#include <iostream>
#include "ChipMachine.h"
#include <SFML/Graphics.hpp>
#include <SFML/System.hpp>
#include <sstream>
#include <string>
//The machine
ChipMachine cm;
//The windows
sf::RenderWindow* debug;
sf::RenderWindow* window;
//For the debugging window
sf::Font font;
sf::Text text;
//If debugmode, debug info is displayed on the debug window
bool debugMode = true;
bool pause = false;
bool step = false;
//For the ram explorer in the debugger
unsigned short explorerPos = 0x200;
bool smallPixels = false;
unsigned colorMode = 2;
const unsigned maxModes = 6;
//Background color
sf::Color bColors[] = {sf::Color::Black, sf::Color::White, sf::Color(0, 50, 0), sf::Color(230, 255, 213), sf::Color::Cyan, sf::Color::Red};
//Foreground (ON pixels) color
sf::Color fColors[] = {sf::Color::White, sf::Color::Black, sf::Color::Green, sf::Color(8, 30, 7), sf::Color::Magenta, sf::Color::Yellow};
sf::Keyboard::Key azertyKeys[16] = {sf::Keyboard::X, sf::Keyboard::Num1, sf::Keyboard::Num2, sf::Keyboard::Num3, sf::Keyboard::A, sf::Keyboard::Z, sf::Keyboard::E, sf::Keyboard::Q, sf::Keyboard::S, sf::Keyboard::D, sf::Keyboard::W, sf::Keyboard::C, sf::Keyboard::Num4, sf::Keyboard::R, sf::Keyboard::F, sf::Keyboard::V};
//Give the name of an op, for debugging
std::stringstream decomp(unsigned short op)
{
std::stringstream s;
switch (op & 0xF000)
{
case (0x0000):
if (op == 0)
{
s << "NOP";
break;
}
if (op == 0x00E0) //CLS
{
s << "CLS";
break;
}
if (op == 0x00EE) //RET
{
s << "RET";
break;
}
s << "UNKNOWN OP";
break;
case (0x1000): //JP addr
s << std::hex << "JP " << (op & 0x0FFF);
break;
case (0x2000): //CALL addr
s << std::hex << "CALL " << (op & 0x0FFF);
break;
case (0x3000): //SE Vx, byte
s << std::hex << "SE V" << ((op & 0x0F00) >> 8) << ", " << (op & 0x00FF);
break;
case (0x4000): //SNE Vx, byte
s << std::hex << "SNE V" << ((op & 0x0F00) >> 8) << ", " << (op & 0x00FF);
break;
case (0x5000): //SE Vx, Vy
s << std::hex << "SE V" << ((op & 0x0F00) >> 8) << ", V" << ((op & 0x00F0) >> 4);
break;
case (0x6000): //LD Vx, byte
s << std::hex << "LD V" << ((op & 0x0F00) >> 8) << ", " << (op & 0x00FF);
break;
case (0x7000): //ADD Vx, byte
s << std::hex << "ADD V" << ((op & 0x0F00) >> 8) << ", " << (op & 0x00FF);
break;
case (0x8000):
switch(op & 0x000F)
{
case (0x0000): //LD Vx, Vy
s << std::hex << "LD V" << ((op & 0x0F00) >> 8) << ", V" << ((op & 0x00F0) >> 4);
break;
case (0x0001): //OR Vx, Vy
s << std::hex << "OR V" << ((op & 0x0F00) >> 8) << ", V" << ((op & 0x00F0) >> 4);
break;
case (0x0002): //AND Vx, Vy
s << std::hex << "AND V" << ((op & 0x0F00) >> 8) << ", V" << ((op & 0x00F0) >> 4);
break;
case (0x0003): //XOR Vx, Vy
s << std::hex << "XOR V" << ((op & 0x0F00) >> 8) << ", V" << ((op & 0x00F0) >> 4);
break;
case (0x0004): //ADD Vx, Vy
s << std::hex << "ADD V" << ((op & 0x0F00) >> 8) << ", V" << ((op & 0x00F0) >> 4);
break;
case (0x0005): //SUB Vx, Vy
s << std::hex << "SUB V" << ((op & 0x0F00) >> 8) << ", V" << ((op & 0x00F0) >> 4);
break;
case (0x0006): //SHR Vx
s << std::hex << "SHR V" << ((op & 0x0F00) >> 8);
break;
case (0x0007): //SUBN Vx, Vy
s << std::hex << "SUBN V" << ((op & 0x0F00) >> 8) << ", V" << ((op & 0x00F0) >> 4);
break;
case (0x000E): //SHL Vx
s << std::hex << "SHL V" << ((op & 0x0F00) >> 8);
break;
default:
s << "UNKNOWN OP";
break;
}
break;
case (0x9000): //SNE Vx, Vy
s << std::hex << "SNE V" << ((op & 0x0F00) >> 8) << ", V" << ((op & 0x00F0) >> 4);
break;
case (0xA000): //LD I, addr
s << std::hex << "LD I, " << (op & 0x0FFF);
break;
case (0xB000): //JP V0, addr
s << std::hex << "LD V0, " << (op & 0x0FFF);
break;
case (0xC000): //RND Vx, byte
s << std::hex << "RND V" << ((op & 0x0F00) >> 8) << ", " << (op & 0x00FF);
break;
case (0xD000): //DRW Vx, Vy, nibble
s << std::hex << "DRW V" << ((op & 0x0F00) >> 8) << ", V" << ((op & 0x00F0) >> 4) << ", " << (op & 0x000F);
break;
case (0xE000):
if ((op & 0x00FF) == 0x009E)
{ //SKP Vx
s << std::hex << "SKP V" << ((op & 0x0F00) >> 8);
break;
}
if ((op & 0x00FF) == 0x00A1)
{ //SKNP Vx
s << std::hex << "SKNP V" << ((op & 0x0F00) >> 8);
break;
}
s << "UNKNOWN OP";
break;
case (0xF000):
unsigned short x = ((op & 0x0F00) >> 8);
switch (op & 0x00FF)
{
case (0x0007): //LD Vx DT
s << std::hex << "LD V" << x << ", DT";
break;
case (0x000A): //LD Vx, K
s << std::hex << "LD V" << x << ", K";
break;
case (0x0015): //LD DT, Vx
s << std::hex << "LD DT, V" << x;
break;
case (0x0018): //LD ST, Vx
s << std::hex << "LD ST, V" << x;
break;
case (0x001E): //ADD I, Vx
s << std::hex << "ADD I, V" << x;
break;
case (0x0029): //LD F, Vx
s << std::hex << "LD F, V" << x;
break;
case (0x0033): //LD B, Vx
s << std::hex << "LD B, V" << x;
break;
case (0x0055): //LD [I], Vx
s << std::hex << "LD [I], V" << x;
break;
case (0x0065): //LD Vx, [I]
s << std::hex << "LD V" << x << ", [I]";
break;
default :
s << "UNKNOWN OP";
break;
}
break;
}
return s;
}
//The game draw
void draw()
{
int pcount = 0;
for (int x = 0; x < 64; x++)
for (int y = 0; y < 32; y++)
{
if (cm.screen[x][y])
pcount++;
}
if (pcount > 0)
{
sf::VertexArray pixels = sf::VertexArray(sf::Quads, pcount * 4);
int i = 0;
for (int x = 0; x < 64; x++)
for (int y = 0; y < 32; y++)
{
if (cm.screen[x][y])
{
pixels[i * 4 + 0].position = sf::Vector2f(x * 10 + smallPixels, y * 10 + smallPixels);
pixels[i * 4 + 1].position = sf::Vector2f(x * 10 + 10 - smallPixels, y * 10 + smallPixels);
pixels[i * 4 + 2].position = sf::Vector2f(x * 10 + 10 - smallPixels, y * 10 + 10 - smallPixels);
pixels[i * 4 + 3].position = sf::Vector2f(x * 10 + smallPixels, y * 10 + 10 - smallPixels);
i++;
}
}
for (int x = 0; x < pcount * 4; x++)
pixels[x].color = fColors[colorMode];
window->draw(pixels);
}
}
//The debug window draw
void drawDebug()
{
if (debugMode)
{
std::stringstream s;
text.setPosition(sf::Vector2f(10, 370));
text.setString("PAUSE : P, STEP : M, RESET : L, DEBUG MODE : K (ON)");
debug->draw(text);
if (pause)
{
text.setPosition(sf::Vector2f(520, 0));
text.setString("PAUSED");
debug->draw(text);
}
text.setPosition(sf::Vector2f(10, 386));
text.setString("CHANGE COLOR MODE : O, CHANGE PIXEL MODE : I");
debug->draw(text);
s << "PC : 0x " << std::hex << cm.getPC();
text.setPosition(sf::Vector2f(10, 0));
text.setString(s.str());
debug->draw(text);
s.str(std::string());
s << "OP : 0x" << std::hex << cm.getOp();
if (pause)
s << " \"" << decomp(cm.getOp()).str() << "\"";
text.setPosition(sf::Vector2f(10, 16));
text.setString(s.str());
debug->draw(text);
s.str(std::string());
s << "I : 0x" << std::hex << cm.getIReg();
text.setPosition(sf::Vector2f(10, 48));
text.setString(s.str());
debug->draw(text);
s.str(std::string());
for (int x = 0; x < 16; x++)
{
s << "V" << x << " : 0x" << std::hex << (int)cm.getVxReg(x);
text.setPosition(sf::Vector2f(10, 74 + x * 16));
text.setString(s.str());
debug->draw(text);
s.str(std::string());
}
text.setPosition(sf::Vector2f(200, 48));
text.setString("KEYS : ");
debug->draw(text);
s.str(std::string());
for (int x = 0; x < 4; x++)
for (int y = 0; y < 4; y++)
{
s << '[' << (x + y * 4) << ":" << std::hex << (int)cm.keys[x + y * 4] << ']';
text.setPosition(sf::Vector2f(x * 64 + 200, y * 16 + 64));
text.setString(s.str());
debug->draw(text);
s.str(std::string());
}
s << "SOUND TIMER : 0x" << std::hex << (int)cm.getSoundTimer();
text.setPosition(sf::Vector2f(200, 144));
text.setString(s.str());
debug->draw(text);
s.str(std::string());
s << "DELAY TIMER : 0x" << std::hex << (int)cm.getTimer();
text.setPosition(sf::Vector2f(200, 160));
text.setString(s.str());
debug->draw(text);
s.str(std::string());
text.setPosition(sf::Vector2f(200, 224));
text.setString("RAM EXPLORER");
debug->draw(text);
s << "AT 0x" << std::hex << explorerPos;
text.setPosition(sf::Vector2f(200, 240));
text.setString(s.str());
debug->draw(text);
s.str(std::string());
s << "<- 0x" << std::hex << cm.getOp(explorerPos) << " -> (\"" << decomp(cm.getOp(explorerPos)).str() << "\")";
text.setPosition(sf::Vector2f(184, 272));
text.setString(s.str());
debug->draw(text);
s.str(std::string());
text.setPosition(sf::Vector2f(200, 304));
text.setString("GOTO PC : G, USE LEFT AND RIGHT");
debug->draw(text);
}
else
{
text.setPosition(sf::Vector2f(10, 0));
text.setString("DEBUG DISABLED");
debug->draw(text);
text.setPosition(sf::Vector2f(10, 32));
text.setString("PRESS K TO SWITCH IT ON");
debug->draw(text);
}
}
void registerAllKeys(sf::Keyboard::Key keys[16])
{
//Register all keys, from 0 to f
if (window->hasFocus() || debug->hasFocus())
{
for (int i = 0; i < 16; i++)
{
cm.keys[i] = sf::Keyboard::isKeyPressed(keys[i]);
}
}
}
int main(int argc, char **argv)
{
if (argc < 2)
{
std::cout << "You must specify a rom to load\n";
system("pause");
return 1;
}
window = new sf::RenderWindow(sf::VideoMode(640, 320), "Chip Eight Emu");
debug = new sf::RenderWindow(sf::VideoMode(600, 432), "Debug");
debug->setPosition(sf::Vector2i());
window->setPosition(sf::Vector2i(600, 0));
//The text for the debugger
font.loadFromFile("lucon.ttf");
text.setFont(font);
text.setFillColor(sf::Color::Black);
text.setCharacterSize(16);
//Init the machine and load the rom
cm = ChipMachine();
cm.init();
cm.loadProgram(argv[1]);
//The cpu runs 540 cycles per second
window->setFramerateLimit(540);
//The main loop
while (window->isOpen())
{
//Register events for the game window
sf::Event event;
while (window->pollEvent(event))
{
if (event.type == sf::Event::Closed)
window->close();
if (event.type == sf::Event::KeyPressed)
{
//Pause
if (event.key.code == sf::Keyboard::P)
pause = !pause;
//Step
if (event.key.code == sf::Keyboard::M && pause)
step = true;
//Reset
if (event.key.code == sf::Keyboard::L)
cm.init();
//Change color mode
if (event.key.code == sf::Keyboard::O)
{
colorMode++;
colorMode %= maxModes;
}
//Change pixel mode
if (event.key.code == sf::Keyboard::I)
{
smallPixels = !smallPixels;
}
}
}
//Same for the debugger
sf::Event devent;
while (debug->pollEvent(devent))
{
if (devent.type == sf::Event::Closed)
window->close();
if (devent.type == sf::Event::KeyPressed)
{
//Pause
if (devent.key.code == sf::Keyboard::P)
pause = !pause;
//Step
if (devent.key.code == sf::Keyboard::M && pause)
step = true;
//Navigate in the ram explorer
if (devent.key.code == sf::Keyboard::Left && debugMode)
explorerPos -= 2;
if (devent.key.code == sf::Keyboard::Right && debugMode)
explorerPos += 2;
//Goto pc in the ram explorer
if (devent.key.code == sf::Keyboard::G && debugMode)
explorerPos = cm.getPC();
//Reset
if (devent.key.code == sf::Keyboard::L)
cm.init();
//Enable/disable the debugger
if (devent.key.code == sf::Keyboard::K)
debugMode = !debugMode;
//Change color mode
if (devent.key.code == sf::Keyboard::O)
{
colorMode++;
colorMode %= maxModes;
}
//Change pixel mode
if (devent.key.code == sf::Keyboard::I)
{
smallPixels = !smallPixels;
}
}
}
if (!pause || step)
{
registerAllKeys(azertyKeys);
cm.emulateCycle();
}
step = false;
//Draw the game
window->clear(bColors[colorMode]);
draw();
//Draw the debugger
debug->clear(sf::Color::White);
drawDebug();
debug->display();
window->display();
}
delete(window);
delete(debug);
return 0;
}