-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
97 lines (77 loc) · 1.95 KB
/
main.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
#include<stdio.h>
#include"GL/glut.h"
#include"cpu.h"
#include"ppu.h"
#include"memory.h"
#include"controller.h"
#include "window.h"
#define WINDOW_WIDTH 256
#define WINDOW_HEIGHT 240
void reshape(int w, int h);
void open_file(char *filename);
int main(int argc, char *argv[]) {
if(argc < 2) {
printf("write filename\n");
exit(1);
}
glutInit(&argc, argv);
glutInitWindowSize(WINDOW_WIDTH * 3, WINDOW_HEIGHT * 3);
glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE);
glutCreateWindow("NES EMU");
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutIdleFunc(cpu);
glutSpecialFunc(special_key_down);
glutSpecialUpFunc(special_key_up);
glutKeyboardFunc(key_down);
glutKeyboardUpFunc(key_up);
glutJoystickFunc(joystick, 10);
glClearColor(1.0, 1.0, 1.0, 1.0);
init_window(WINDOW_POWER_ON);
init_cpu();
init_ppu();
open_file(argv[1]);
glutMainLoop();
return 0;
}
void open_file(char *filename) {
FILE *fp;
unsigned char header[16];
int prg_size, chr_size;
int i, j, k = 0;
fp = fopen(filename, "rb");
if(fp == NULL) {
fprintf(stderr, "%s is not opened\n", filename);
exit(1);
}
fread(header, sizeof(unsigned char), 16, fp);
prg_size = header[4] * 16384;
chr_size = header[5] * 8192;
nes_flag6 = header[6];
for(i = 0; i < 0x8000; i += prg_size) {
if(fseek(fp, 16, SEEK_SET) != 0) {
fprintf(stderr, "seek error\n");
exit(1);
}
for(j = 0; j < prg_size; j++, k++) {
unsigned char data;
fread(&data, sizeof(unsigned char), 1, fp);
memory[0x8000 + k] = data;
}
}
if(fseek(fp, 16 + prg_size, SEEK_SET) != 0) {
fprintf(stderr, "seek error in second\n");
exit(1);
}
for(i = 0; i < chr_size; i++) {
unsigned char data;
fread(&data, sizeof(unsigned char), 1, fp);
vram[i] = data;
}
fclose(fp);
}
void reshape(int w, int h) {
glViewport(0, 0, w, h);
glLoadIdentity();
glOrtho(0, WINDOW_WIDTH, WINDOW_HEIGHT, 0, -200, 200);
}