-
Notifications
You must be signed in to change notification settings - Fork 6
/
main.c
255 lines (220 loc) · 8.13 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
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
/*
* main.c
*
* P/EMU - P/ECE Emulator
* Copyright (C) 2003 Naoyuki Sawa
*
* * Mon Apr 14 00:00:00 JST 2003 Naoyuki Sawa
* - 作成開始。
*/
#include "app.h"
//#ifndef PSP
#include "SDL_thread.h"
//#endif
#define SC_CONFIG 100
#define SC_ABOUT 101
#define OC_MIN 20
#define OC_MAX 300
#define OC_DEFAULT 100
#define FPS_MIN 10
#define FPS_MAX 150
#define FPS_DEFAULT 60
#define SEC_CONFIG "config"
#define KEY_OC "oc"
#define KEY_FPS "fps"
#define KEY_NOWAIT "nowait"
//#define KEY_DBG "dbg" //デバッグメッセージ状態は保存しない
/****************************************************************************
* グローバル変数
****************************************************************************/
/****************************************************************************
* ローカル変数
****************************************************************************/
int fullscreen = 0; /* 0:ウインドウモード/1:フルスクリーンモード */
unsigned char fsbuff[DISP_Y * 5][DISP_X * 5]; /* フルスクリーン展開用 */
/****************************************************************************
*
****************************************************************************/
// /* デバッグメッセージの出力 */
// void
// dbg(const char* fmt, ...)
// {
// if(o_dbg) {
// va_list ap;
// va_start(ap, fmt);
// vfprintf(stderr, fmt, ap);
// va_end(ap);
// fprintf(stderr, "\n");
// }
// /* 継続 */
// }
//
// /* 異常終了 */
// void
// die(const char* fmt, ...)
// {
// va_list ap;
// va_start(ap, fmt);
// vfprintf(stderr, fmt, ap);
// va_end(ap);
// fprintf(stderr, "\n");
// abort();
// }
/****************************************************************************
*
****************************************************************************/
void ui_init(PIEMU_CONTEXT *context)
{
context->window = SDL_CreateWindow("P/EMU/SDL", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
128 * 4, 88 * 4, SDL_WINDOW_ALLOW_HIGHDPI);
// 0, 0, SDL_WINDOW_FULLSCREEN_DESKTOP);
context->renderer = SDL_CreateRenderer(context->window, -1, 0);
// SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, "linear"); // make the scaled rendering look smoother.
SDL_RenderSetLogicalSize(context->renderer, 128, 88);
context->texture = SDL_CreateTexture(context->renderer, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_STREAMING, 128,
88);
}
int main(int argc, char *argv[])
{
PIEMU_CONTEXT context;
SDL_Event event;
SDL_Thread *thEmuThread;
memset(&context, 0, sizeof context);
SDL_Init(SDL_INIT_EVERYTHING ^ SDL_INIT_HAPTIC);
SDL_LogSetAllPriority(SDL_LOG_PRIORITY_VERBOSE);
context.pfnSetEmuParameters = SetEmuParameters;
context.pfnLoadFlashImage = LoadFlashImage;
context.pfnUpdateScreen = UpdateScreen;
// printf("=================================\n");
// printf("P/EMU - P/ECE Emulator (%s)\n", VERSION);
// printf("Copyright (C) 2003 Naoyuki Sawa\n");
// printf("=================================\n");
/* 設定復元。 */
context.o_oc = OC_DEFAULT;
context.o_fps = FPS_DEFAULT;
context.o_nowait = 0;
context.o_nowait = context.o_nowait != 0;
memset(context.vbuff, 0, sizeof context.vbuff);
/* UI初期化。 */
ui_init(&context);
/* エミュレータ初期化。 */
emu_init(&context);
context.keystate = (uint8_t *) SDL_GetKeyboardState(NULL);
context.bEndFlag = 0;
thEmuThread = SDL_CreateThread(emu_work, "piemu-core", &context);
if (SDL_NumJoysticks() > 0) {
context.pad = SDL_JoystickOpen(0);
}
unsigned nMSecPerFrame = 1000 / context.o_fps;
while (1) {
unsigned real_org = SDL_GetTicks();
while (SDL_PollEvent(&event)) {
switch (event.type) {
case SDL_QUIT:
goto L_EXIT;
#ifdef PSP
case SDL_JOYBUTTONDOWN:
{
switch(event.jbutton.button)
{
case JOY_CIRCLE: context.keystate[KEY_A] = 1; break;
case JOY_CROSS: context.keystate[KEY_B] = 1; break;
case JOY_START: context.keystate[KEY_START] = 1; break;
case JOY_SELECT: context.keystate[KEY_SELECT] = 1; break;
case JOY_LEFT: context.keystate[KEY_LEFT] = 1; break;
case JOY_RIGHT: context.keystate[KEY_RIGHT] = 1; break;
case JOY_UP: context.keystate[KEY_UP] = 1; break;
case JOY_DOWN: context.keystate[KEY_DOWN] = 1; break;
}
break;
}
case SDL_JOYBUTTONUP:
{
switch(event.jbutton.button)
{
case JOY_CIRCLE: context.keystate[KEY_A] = 0; break;
case JOY_CROSS: context.keystate[KEY_B] = 0; break;
case JOY_START: context.keystate[KEY_START] = 0; break;
case JOY_SELECT: context.keystate[KEY_SELECT] = 0; break;
case JOY_LEFT: context.keystate[KEY_LEFT] = 0; break;
case JOY_RIGHT: context.keystate[KEY_RIGHT] = 0; break;
case JOY_UP: context.keystate[KEY_UP] = 0; break;
case JOY_DOWN: context.keystate[KEY_DOWN] = 0; break;
}
break;
}
#else
case SDL_JOYAXISMOTION:
{
//fprintf(stderr, "[axis:%d]: a%d: %d\n", event.jaxis.which, event.jaxis.axis, event.jaxis.value);
if(event.jaxis.axis == 6) {
context.keystate[KEY_LEFT] = (event.jaxis.value < -8192);
context.keystate[KEY_RIGHT] = (event.jaxis.value > 8192);
}
if(event.jaxis.axis == 7) {
context.keystate[KEY_UP] = (event.jaxis.value < -8192);
context.keystate[KEY_DOWN] = (event.jaxis.value > 8192);
}
break;
}
case SDL_JOYHATMOTION:
{
//fprintf(stderr, "[hat:%d]: h%d: %d\n", event.jhat.which, event.jhat.hat, event.jhat.value);
context.keystate[KEY_LEFT] = (event.jhat.value & SDL_HAT_LEFT);
context.keystate[KEY_RIGHT] = (event.jhat.value & SDL_HAT_RIGHT);
context.keystate[KEY_UP] = (event.jhat.value & SDL_HAT_UP);
context.keystate[KEY_DOWN] = (event.jhat.value & SDL_HAT_DOWN);
break;
}
case SDL_JOYBUTTONDOWN:
{
switch(event.jbutton.button)
{
case 1: context.keystate[KEY_A] = 1; break;
case 0: context.keystate[KEY_B] = 1; break;
case 7: context.keystate[KEY_START] = 1; break;
case 6: context.keystate[KEY_SELECT] = 1; break;
}
break;
}
case SDL_JOYBUTTONUP:
{
switch(event.jbutton.button)
{
case 1: context.keystate[KEY_A] = 0; break;
case 0: context.keystate[KEY_B] = 0; break;
case 7: context.keystate[KEY_START] = 0; break;
case 6: context.keystate[KEY_SELECT] = 0; break;
}
break;
}
#endif
}
}
/* 画面更新。 */
lcdc_conv(&context, context.vbuff);
context.pfnUpdateScreen(&context, context.pUser);
/* 実時間との同期 */
if (!context.o_nowait) {
int nExpectedWait = (real_org + nMSecPerFrame) - SDL_GetTicks();
if (nExpectedWait > 0)
SDL_Delay(nExpectedWait);
}
}
L_EXIT:
context.bEndFlag = 1;
// wake up core
SDL_LockMutex(context.core.mut_halt);
{
context.core.in_halt = 0;
SDL_CondBroadcast(context.core.cond_halt);
}
SDL_UnlockMutex(context.core.mut_halt);
SDL_WaitThread(thEmuThread, NULL);
if (context.pad != NULL && SDL_JoystickGetAttached(context.pad))
SDL_JoystickClose(context.pad);
if(context.audio_device > 0)
SDL_CloseAudioDevice(context.audio_device);
SDL_Quit();
return 0;
}