-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.cpp
More file actions
84 lines (69 loc) · 2.34 KB
/
Main.cpp
File metadata and controls
84 lines (69 loc) · 2.34 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
#include <iostream>
#include <iomanip> // for std::setfill, std::setw...
#include <assert.h>
#include <fstream>
#include "Chip8.h"
// sdl2 is used for graphics
#include "sdl2/SDL.h"
#undef main
#pragma comment(lib, "SDL2.lib")
#define SDL_FAILURE(msg) std::cout << "SDL: " << msg << " ; error ==" << SDL_GetError() << '\n';
int main(int argc, char** argv)
{
// check if the file fits into the chip8 memory
std::ifstream file("PONG.rom", std::ios::binary | std::ios::ate);
auto fileSize = file.tellg();
if (fileSize > Chip8::WorkingMemory) {
assert(false);
std::cout << "File too big! Expected " << Chip8::WorkingMemory << " bytes, got " << fileSize << " instead.\n";
return EXIT_FAILURE;
}
// rewind to the beginning
file.seekg(0, file.beg);
// memory test
{
std::array<char, Chip8::WorkingMemory> buffer{};
file.read(buffer.data(), buffer.size());
Chip8 chip;
std::memcpy(chip.memory.data() + Chip8::StartAddress, buffer.data(), buffer.size());
std::cout << std::hex;
for (std::size_t n = 0; n < chip.memory.size(); ++n) {
std::cout << "0x" << std::setfill('0') << std::setw(4);;
std::cout << chip.memory[n] << '\n';
}
std::cout << std::dec;
}
if (SDL_Init(SDL_INIT_VIDEO) < 0) {
SDL_FAILURE("Init system");
return EXIT_FAILURE;
}
auto screenWidth = Chip8::ScreenWidth * 100;
auto screenHeight = Chip8::ScreenHeight * 100;
/// TODO replace title with the game title!
auto window = SDL_CreateWindow("Chip8 Emulator", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, screenWidth, screenHeight, SDL_WINDOW_SHOWN);
if (!window) {
SDL_FAILURE("Init window");
return EXIT_FAILURE;
}
auto renderer = SDL_CreateRenderer(window, -1, 0);
if (!renderer) {
SDL_FAILURE("Init renderer");
return EXIT_FAILURE;
}
// test: jump to address 003
{
Chip8 chip;
chip.emulate(0x1003);
assert(chip.PC == 3);
std::cout << chip << '\n';
}
// test: set register v1 = 7
{
Chip8 chip;
chip.emulate(0x6107);
assert(chip.V[V1] == 7);
assert(chip.PC == Chip8::StartAddress + 2); // a 2 byte instruction took place
std::cout << chip << '\n';
}
return EXIT_SUCCESS;
}