-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdriver.c
290 lines (234 loc) · 5.02 KB
/
driver.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
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include "pthread.h"
#include <time.h>
#include "chip8core.h"
#include <signal.h>
void windcall(GLFWwindow* win, int width, int height){
glViewport(0,0,width,height);
puts("Window size changing");
}
void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods)
{
char key_table[16] = {'X','1','2','3',
'Q','W','E',
'A','S','D',
'Z','C','4','R','F','V'};
if (action == GLFW_PRESS){
for (int i = 0; i < 16; i++)
{
if (key == key_table[i])
{
keyb = i;
break;
}
}
}
if (action == GLFW_RELEASE)
{
keyb = 0xfe;
}
}
void* delayHandler(void* meh){
while (1){
if (dt > 0){
dt--;
}
if (st > 0){
st--;
}
usleep(16666);
}
return NULL;
}
void handler(int sig){
puts("Invalid Memory Access..");
printf("Offending OpCode: %x\nProgram Counter: %x\n",current_opcode, pc);
puts("Registers: ");
for (int i = 0; i <= 0xf; i++){
printf("V%x: %d\n",i,v[i]);
}
printf("I: %x, PC = %x\n", i, pc);
exit(sig);
}
void initialize(){
//puts("Initializing system..");
memset(v,0, 17*sizeof(unsigned char));
pc = 0x200;
sp = 0;
current_opcode = 0;
i = 0;
dpflag = 0;
keyb = 0;
dt = 0;
st = 0;
}
void loadGame(char* c){
FILE* fp = fopen(c,"rb");
if (!fp){
puts("Game not found... exitting");
exit(1);
return;
}
unsigned char ch = getc(fp);
int size = 0;
for (int i = 512; i < 4096; i++){
memory[i] = ch;
size += 1;
ch = getc(fp);
}
}
void emulateCycle(int cycle_count){
for (int i = 1; i <= cycle_count; i++){
glfwPollEvents();
unsigned short fetch = (memory[pc] << 8) | memory[pc+1];
decodeOp(fetch);
pc += 2;
}
}
void displayScreen(int VBO, int prog){
float xoffset = 2.0/((float) display_x);
float yoffset = 2.0/((float) display_y);
float vertices[] = {-1.0f,1.0f,
-1.0f+xoffset, 1.0f,
-1.0f, 1.0f-yoffset,
-1.0f+xoffset, 1.0f-yoffset};
int y = 0;
while (y < display_y){
int x = 0;
while (x < display_x){
if (display[x][y] == 1){
glBufferData(GL_ARRAY_BUFFER,sizeof(vertices), vertices, GL_STREAM_DRAW);
glDrawArrays(GL_TRIANGLE_STRIP,0,4);
}
int i = 0;
while (i < 8){
vertices[i] += xoffset;
i += 2;
}
x++;
}
int i = 1;
while (i < 8){
vertices[i] = vertices[i] - yoffset;
i += 2;
}
vertices[0] = -1.0;
vertices[2] = -1.0+xoffset;
vertices[4] = -1.0;
vertices[6] = -1.0+xoffset;
y++;
}
}
void getSource(char* filename, int shader){
FILE* f = fopen(filename,"r");
if (!f){
printf("Missing file: %s",filename);
exit(-1);
}
char buffer[512];
char ch = getc(f);
int index = 0;
while (ch != EOF){
buffer[index] = ch;
ch = getc(f);
index++;
}
buffer[index] = 0;
const char* data = buffer;
glShaderSource(shader,1,&data,NULL);
}
void compileSource(int shader){
glCompileShader(shader);
int successStatus, shaderType;
glGetShaderiv(shader,GL_COMPILE_STATUS,&successStatus);
glGetShaderiv(shader,GL_SHADER_TYPE,&shaderType);
if (successStatus == GL_FALSE){
puts("Shader Compilation failed..");
exit(-1);
}
}
int main(int argc, char *argv[]){
signal(SIGSEGV, handler);
initialize();
fillFont();
int clock_cycles = 10;
if (argc < 2){
puts("Syntax: ./programfile <chip8 rom> <clocks per frame (default: 10)");
exit(1);
}
if (argc == 3)
{
int a = strtol(argv[2],NULL,10);
if (a >= 1 && a <= 100)
{
clock_cycles = a;
}
else{
puts("Invalid cycle count, defaulting to 10)");
}
}
puts("Initializing emulated CPU");
srand(time(0));
loadGame(argv[1]);
pthread_t pid, pid2;
pthread_create(&pid,NULL,delayHandler,NULL);
int cycle = 1;
if ((memory[pc] << 8 | memory[pc+1]) == 0x1260){
display_x = 64;
display_y = 64;
pc = 0x2c0 - 2;
}
puts("Initializing OpenGL Renderer");
glfwInit();
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR,4);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR,6);
GLFWwindow* win = glfwCreateWindow(1920,1080,"CHIP8 Emulator",NULL, NULL);
glfwMakeContextCurrent(win);
glewInit();
glfwSetWindowSizeCallback(win, windcall);
glfwSetKeyCallback(win,key_callback);
int vertexShader = glCreateShader(GL_VERTEX_SHADER);
int fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
getSource("vertex.glsl",vertexShader);
getSource("fragment.glsl",fragmentShader);
compileSource(vertexShader);
compileSource(fragmentShader);
int prog = glCreateProgram();
glAttachShader(prog,vertexShader);
glAttachShader(prog,fragmentShader);
glLinkProgram(prog);
int linkStatus;
glGetProgramiv(prog,GL_LINK_STATUS, &linkStatus);
if (linkStatus == GL_FALSE){
puts("Linking failed..");
char data[512];
glGetProgramInfoLog(prog,512,NULL,data);
puts("INFO LOG: ");
printf("%s\n",data);
return -1;
}
puts("Shaders compiled and linked");
int VBO;
glGenBuffers(1,&VBO);
glBindBuffer(GL_ARRAY_BUFFER,VBO);
int VAO;
glGenVertexArrays(1,&VAO);
glBindVertexArray(VAO);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0,2,GL_FLOAT,GL_FALSE, sizeof(float) *2, (void*)0);
glUseProgram(prog);
glfwSetWindowSizeCallback(win, windcall);
while (1){
emulateCycle(clock_cycles);
glClearColor(0.1,0.1,0.1,1.0);
glClear(GL_COLOR_BUFFER_BIT);
//if (dpflag){
displayScreen(VBO,prog);
glfwSwapBuffers(win);
//}
}
//usleep(2000);
}