-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
153 lines (124 loc) · 3.88 KB
/
main.cpp
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
#include <iostream>
#include <fstream>
#include <vector>
#include <stdexcept>
#include <chrono>
#include <thread>
#include <SDL2/SDL.h>
#include "cpu.cpp"
SDL_Renderer* renderer;
SDL_Window* window;
SDL_Texture* texture;
const int keymap[16] = {
SDL_SCANCODE_X,
SDL_SCANCODE_1,
SDL_SCANCODE_2,
SDL_SCANCODE_3,
SDL_SCANCODE_Q,
SDL_SCANCODE_W,
SDL_SCANCODE_E,
SDL_SCANCODE_A,
SDL_SCANCODE_S,
SDL_SCANCODE_D,
SDL_SCANCODE_Z,
SDL_SCANCODE_C,
SDL_SCANCODE_4,
SDL_SCANCODE_R,
SDL_SCANCODE_F,
SDL_SCANCODE_V
};
Cpu cpu;
void init() {
if (SDL_Init(SDL_INIT_EVERYTHING) < 0) { throw std::runtime_error("SDL INITALIZATION FAILED."); }
// window with 64x32 ratio
window = SDL_CreateWindow("eightmulator", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 1024, 512, SDL_WINDOW_VULKAN);
if (window == NULL) { throw std::runtime_error("WINDOW CREATION FAILED."); }
renderer = SDL_CreateRenderer(window, -1, 0);
if (renderer == NULL) { throw std::runtime_error("RENDERER CREATION FAILED."); }
// 64 x 32 texture
texture = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_STREAMING, 64, 32);
if (texture == NULL) { throw std::runtime_error("TEXTURE CREATION FAILED."); }
cpu.init(true);
}
void deinit() {
cpu.deinit();
SDL_DestroyWindow(window);
SDL_Quit();
}
void loadROM(char *filename) {
std::ifstream file(filename, std::ios::in | std::ios::binary);
if (file.is_open()) {
char c;
int check = 0x200;
for (int i = 0x200; file.get(c); i++) {
if (check >= 4096) {
throw std::runtime_error("ROM TOO LARGE.");
}
cpu.memory[i] = (uint8_t) c;
check++;
}
}
}
int main(int argc, char *argv[]) {
try {
init();
char* filename = argv[1];
if (filename == NULL) {
printf("No ROM argument present.\n");
exit(-1);
}
loadROM(filename);
// Main loop
bool keepOpen = true;
while (keepOpen) {
cpu.cycle();
// SDL Events
SDL_Event e;
while(SDL_PollEvent(&e) > 0) {
switch (e.type) {
case SDL_QUIT:
keepOpen = false;
break;
case SDL_KEYDOWN:
for(int i = 0; i < 16; i++) {
if (e.key.keysym.scancode == keymap[i]) {
cpu.keys[i] = 1;
}
}
break;
case SDL_KEYUP:
for(int i = 0; i < 16; i++) {
if (e.key.keysym.scancode == keymap[i]) {
cpu.keys[i] = 0;
}
}
}
}
SDL_RenderClear(renderer);
uint32_t* bytes = new uint32_t[64 * 32];
int pitch = 0;
SDL_LockTexture(texture, NULL, (void**) &bytes, &pitch);
for (int y = 0; y < 32; y++) {
for (int x = 0; x < 64; x++) {
bytes[y * 64 + x] = (cpu.graphics[y * 64 + x] == 1) ? 0xFFFFFF : (uint32_t) 0x000000;
}
}
SDL_UnlockTexture(texture);
SDL_RenderCopy(renderer, texture, NULL, NULL);
SDL_RenderPresent(renderer);
// sleep for 16 milliseconds for 16Hz
std::this_thread::sleep_for(std::chrono::milliseconds(16));
}
// Close
deinit();
exit(0);
} catch(const std::runtime_error& error) {
std::cout << error.what() << "\n";
deinit();
exit(-1);
} catch (...) {
std::cout << "an error ocurred." << "\n";
deinit();
exit(-1);
}
}