-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqmnes.c
177 lines (157 loc) · 4.72 KB
/
qmnes.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
/*
* qmnes.c
* Copyright (C) 2014 martian <[email protected]>
*
* Distributed under terms of the MIT license.
*/
#include <stdio.h>
#include <unistd.h>
#include <signal.h>
#include <SDL.h>
#include "cpu.h"
#include "rom.h"
#include "ins.h"
#include "ppu.h"
SDL_Window *gWindow = NULL;
SDL_Surface *gScreen = NULL;
SDL_Renderer *gRender = NULL;
SDL_Texture *gTexture = NULL;
void input_handler(void *cpu)
{
SDL_Event event;
struct cpu_6502 *p = (struct cpu_6502 *) cpu;
while(SDL_PollEvent(&event)) {
switch(event.type) {
case SDL_KEYDOWN:
switch (event.key.keysym.sym) {
case SDLK_w:
p->keypad[KEYPAD_UP] = 1;
break;
case SDLK_s:
p->keypad[KEYPAD_DOWN] = 1;
break;
case SDLK_a:
p->keypad[KEYPAD_LEFT] = 1;
break;
case SDLK_d:
p->keypad[KEYPAD_RIGHT] = 1;
break;
case SDLK_u:
p->keypad[KEYPAD_START] = 1;
break;
case SDLK_i:
p->keypad[KEYPAD_SELECT] = 1;
break;
case SDLK_j:
p->keypad[KEYPAD_A] = 1;
break;
case SDLK_k:
p->keypad[KEYPAD_B] = 1;
break;
default:
break;
}
break;
case SDL_KEYUP:
switch (event.key.keysym.sym) {
case SDLK_w:
p->keypad[KEYPAD_UP] = 0;
break;
case SDLK_s:
p->keypad[KEYPAD_DOWN] = 0;
break;
case SDLK_a:
p->keypad[KEYPAD_LEFT] = 0;
break;
case SDLK_d:
p->keypad[KEYPAD_RIGHT] = 0;
break;
case SDLK_u:
p->keypad[KEYPAD_START] = 0;
break;
case SDLK_i:
p->keypad[KEYPAD_SELECT] = 0;
break;
case SDLK_j:
p->keypad[KEYPAD_A] = 0;
break;
case SDLK_k:
p->keypad[KEYPAD_B] = 0;
break;
default:
break;
}
break;
default:
break;
}
}
}
void render(uint32_t *pixels)
{
uint32_t *p;
int pitch;
SDL_LockTexture(gTexture, NULL, (void **)&p, &pitch);
memcpy(p, pixels, sizeof(uint32_t) * 256 * 240);
SDL_UnlockTexture(gTexture);
SDL_RenderCopy(gRender, gTexture, NULL, NULL);
SDL_RenderPresent(gRender);
}
void graphic_init()
{
if (SDL_Init(SDL_INIT_VIDEO) < 0) {
perror("Video cannot be initialized");
exit(1);
}
gWindow = SDL_CreateWindow("qmnes", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 256, 240, SDL_WINDOW_SHOWN);
if (gWindow == NULL) {
perror("Video cannot be initialized");
exit(1);
}
gRender = SDL_CreateRenderer(gWindow, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
gScreen = SDL_GetWindowSurface(gWindow);
gTexture = SDL_CreateTexture(gRender, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_STREAMING, 256, 240);
}
void sighdl(int sig)
{
if (sig == SIGINT) {
SDL_DestroyRenderer(gRender);
SDL_DestroyWindow(gWindow);
gWindow = NULL;
SDL_Quit();
printf("Bye my dear\n");
exit(0);
}
}
int main(int argc, char **argv)
{
if (argc != 2) {
printf("oops\n");
return 1;
}
char *filename = argv[1];
struct rom test_rom;
struct cpu_6502 *cpu = malloc(sizeof(struct cpu_6502));
struct ppu *ppu = malloc(sizeof(struct ppu));
// Init cpu and ppu.
cpu_setup(cpu);
ppu_setup(ppu);
cpu->ppu = ppu;
cpu->handle_input = input_handler;
ppu->r = render;
// Get ready for SDL.
graphic_init();
// Exit at ctrl+c
if (signal(SIGINT, sighdl) == SIG_ERR) {
return 1;
}
// Loads the rom and sets the cpu and ppu memory mapping.
load_rom(&test_rom, filename);
cpu->rom_prg = test_rom.prg_rom_data;
ppu->rom_chr = test_rom.chr_rom_data;
printf("chr rom %d\n", ppu->rom_chr);
ppu->rom_mirroring = test_rom.rom_mirroring;
cpu_reset(cpu);
cpu_run(cpu);
return 0;
}