/
main.c
407 lines (312 loc) · 11.4 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
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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
/**
** \file
** \brief The main program file
** \author Sandor Zsuga (Jubatian)
** \copyright 2013 - 2015, GNU GPLv3 (version 3 of the GNU General Public
** License) extended as RRPGEvt (temporary version of the RRPGE
** License): see LICENSE.GPLv3 and LICENSE.RRPGEvt in the project
** root.
** \date 2015.09.16
*/
#include "host/types.h"
#include "host/screen.h"
#include "host/audio.h"
#include "host/filels.h"
#include "iface/render.h"
#include "librrpge/rrpge.h"
#include "version.h"
#include <SDL/SDL.h>
#include <errno.h>
/* Prototype for binary load kernel task */
static void main_loadbin(rrpge_object_t* hnd, rrpge_iuint tsh, const void* par);
/* Temporary areas */
static uint8 main_tdata[128U * 1024U];
/* File handle for the application (must be open while emulating) */
static FILE* main_app;
/* Application name string */
static char const* main_appname = "RRPGE simple SDL emulator. Version: " EMULATOR_VERSION;
static char const* main_appicon = "RRPGE";
/* Other elements */
static char const* main_appauth = "By: Sandor Zsuga (Jubatian)\n";
static char const* main_copyrig = "Copyright: 2013 - 2015, GNU GPLv3 (version 3 of the GNU General Public\nLicense) extended as RRPGEvt (temporary version of the RRPGE License):\nsee LICENSE.GPLv3 and LICENSE.RRPGEvt in the project root.\n";
/* Subroutines */
static const rrpge_cbd_sub_t main_cbsub[1] = {
{ RRPGE_CB_SETPAL, &render_pal }
};
/* Tasks */
static const rrpge_cbd_tsk_t main_cbtsk[1] = {
{ RRPGE_CB_LOADBIN, &main_loadbin }
};
/* Functions */
/* static const rrpge_cbd_fun_t main_cbfun[0] = { */
/* }; */
/* Callback structure for the emulator. Only line rendering for now. */
static const rrpge_cbpack_t main_cbpack={
&render_line,
1, /* Task callbacks */
&main_cbtsk[0],
1, /* Subroutine callbacks */
&main_cbsub[0],
0, /* Function callbacks */
NULL
};
/* Loads binary page, kernel task callback */
static void main_loadbin(rrpge_object_t* hnd, rrpge_iuint tsh, const void* par)
{
const rrpge_cbp_loadbin_t* p = (const rrpge_cbp_loadbin_t*)(par);
/* No async task loading, neither check stuff, just run it. Prototype...
** Note that p->scw can be at most 65536. */
if (main_app != NULL){
filels_read(main_app, (p->sow) << 1, (p->scw) << 1, &main_tdata[0]);
}
rrpge_conv_b2w(&main_tdata[0], (p->buf), (p->scw) << 1);
rrpge_taskend(hnd, tsh, 0x8000U);
}
/* Wrapper for malloc to fix type */
static void* main_malloc(rrpge_iuint siz)
{
return malloc(siz);
}
/* Prints error message according to the passed RRPGE error code */
static void main_printrerr(auint e)
{
printf("RRPGE error: ");
if (e == 0){
printf("No error\n");
}else if (e == RRPGE_ERR_WAIT){
printf("RRPGE_ERR_WAIT\n");
}else if (e == RRPGE_ERR_UNK){
printf("RRPGE_ERR_UNK\n");
}else if (e == RRPGE_ERR_INI){
printf("RRPGE_ERR_INI\n");
}else if (e == RRPGE_ERR_VER){
printf("RRPGE_ERR_VER\n");
}else if ((e & 0xF000U) == RRPGE_ERR_STA){
printf("RRPGE_ERR_STA, location: 0x%03X\n", e & 0xFFFU);
}else if ((e & 0xF000U) == RRPGE_ERR_UNS){
printf("RRPGE_ERR_UNS, location: 0x%03X\n", e & 0xFFFU);
}else if ((e & 0xF000U) == RRPGE_ERR_DSC){
printf("RRPGE_ERR_DSC, location: 0x%03X\n", e & 0xFFFU);
}else{
printf("Unknown, code: 0x%04X\n", e);
}
}
/* Prints halt causes */
static void main_printhalt(auint h)
{
printf("RRPGE halt:");
if (h & RRPGE_HLT_AUDIO) { printf(" AUDIO"); }
if (h & RRPGE_HLT_EXIT) { printf(" EXIT"); }
if (h & RRPGE_HLT_FRAME) { printf(" FRAME"); }
if (h & RRPGE_HLT_CALLBACK){ printf(" CALLBACK"); }
if (h & RRPGE_HLT_BREAK) { printf(" BREAK"); }
if (h & RRPGE_HLT_INVOP) { printf(" INVOP"); }
if (h & RRPGE_HLT_INVKCALL){ printf(" INVKCALL"); }
if (h & RRPGE_HLT_STACK) { printf(" STACK"); }
if (h & RRPGE_HLT_DETACHED){ printf(" DETACHED"); }
if (h & RRPGE_HLT_WAIT) { printf(" WAIT"); }
if (h & RRPGE_HLT_FAULT) { printf(" FAULT"); }
if (h == 0) { printf(" No halt"); }
printf("\n");
}
/* Prints some emulator state */
static void main_printstats(rrpge_object_t* obj)
{
auint bp;
/* CPU Registers */
printf("A...: %04X; B...: %04X; C...: %04X; D...: %04X\n",
rrpge_get_state(obj, RRPGE_STA_VARS + 0x0U),
rrpge_get_state(obj, RRPGE_STA_VARS + 0x1U),
rrpge_get_state(obj, RRPGE_STA_VARS + 0x2U),
rrpge_get_state(obj, RRPGE_STA_VARS + 0x3U));
printf("X0..: %04X; X1..: %04X; X2..: %04X; X3..: %04X\n",
rrpge_get_state(obj, RRPGE_STA_VARS + 0x4U),
rrpge_get_state(obj, RRPGE_STA_VARS + 0x5U),
rrpge_get_state(obj, RRPGE_STA_VARS + 0x6U),
rrpge_get_state(obj, RRPGE_STA_VARS + 0x7U));
printf("XM..: %04X; XH..: %04X\n",
rrpge_get_state(obj, RRPGE_STA_VARS + 0x8U),
rrpge_get_state(obj, RRPGE_STA_VARS + 0x9U));
printf("BP..: %04X; SP..: %04X\n",
rrpge_get_state(obj, RRPGE_STA_VARS + 0xCU),
rrpge_get_state(obj, RRPGE_STA_VARS + 0xBU));
printf("PC..: %04X\n",
rrpge_get_state(obj, RRPGE_STA_VARS + 0xAU));
/* Stack */
bp = (auint)(rrpge_get_state(obj, RRPGE_STA_VARS + 0xCU));
printf("BP+0: %04X; %04X; %04X; %04X; %04X; %04X; %04X; %04X\n",
rrpge_get_stack(obj, bp + 0x0U), rrpge_get_stack(obj, bp + 0x1U),
rrpge_get_stack(obj, bp + 0x2U), rrpge_get_stack(obj, bp + 0x3U),
rrpge_get_stack(obj, bp + 0x4U), rrpge_get_stack(obj, bp + 0x5U),
rrpge_get_stack(obj, bp + 0x6U), rrpge_get_stack(obj, bp + 0x7U));
printf("BP+8: %04X; %04X; %04X; %04X; %04X; %04X; %04X; %04X\n",
rrpge_get_stack(obj, bp + 0x8U), rrpge_get_stack(obj, bp + 0x9U),
rrpge_get_stack(obj, bp + 0xAU), rrpge_get_stack(obj, bp + 0xBU),
rrpge_get_stack(obj, bp + 0xCU), rrpge_get_stack(obj, bp + 0xDU),
rrpge_get_stack(obj, bp + 0xEU), rrpge_get_stack(obj, bp + 0xFU));
}
/* Prints exit error message */
static void main_errexit(auint h, rrpge_object_t* obj)
{
printf("Emulation halted because of error in source program!\n");
main_printhalt(h);
main_printstats(obj);
}
int main(int argc, char** argv)
{
auint j;
auint t;
auint cdi = 0U;
auint auc = 0U;
uint16* lpt;
uint16* rpt;
uint16 edt; /* Event data (just one) */
auint mid; /* Mouse device id */
SDL_Event event; /* The event got from the queue */
rrpge_object_t* emu = NULL;
/* Init message */
printf("\n");
printf("%s", main_appname);
printf("\n\n");
printf("%s", main_appauth);
printf("%s", main_copyrig);
printf("\n");
/* Check arguments: need an application */
if (argc <= 1){
printf("Error: need an application to run as parameter!\n");
exit(1);
}else{
printf("Opening %s...\n", argv[1]);
main_app = fopen(argv[1], "rb");
if (main_app == NULL){
perror("Failed to open file");
exit(1);
}
}
/* Initialize emulator library */
rrpge_init_lib(&main_malloc, &free);
/* Attempt to initialize the emulator. Note that the app. binary load
** callback is blocking, so no need to implement any waiting here using
** rrpge_init_run(). */
emu = rrpge_new_emu(&main_cbpack);
if (emu == NULL){
printf("Failed to allocate emulator state\n");
goto loadfault;
}
t = rrpge_init_run(emu, RRPGE_INI_RESET);
if (t != RRPGE_ERR_OK){
printf("Failed to initialize emulator\n");
main_printrerr(t);
goto loadfault;
}
mid = rrpge_dev_add(emu, RRPGE_DEV_POINT); /* Add mouse (pointing device) */
/* Initialize renderer */
render_reset(emu);
/* Initialize SDL */
if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO)!=0) return -1;
/* Try to set up the screen */
if (screen_set()!=0) return -1;
SDL_WM_SetCaption(main_appname, main_appicon);
/* Set up audio */
if (audio_set(2048U) != 0U) return -1;
/* OK, let's go! */
printf("Entering emulation\n");
/* The main loop: It is more straightforward to exit from it with a suitable
** break at the proper place */
while(1){
/* Wait for some event... */
if (SDL_WaitEvent(&event)==0) return -1; /*Error?*/
/* An event is here: do something about it! */
if (event.type==SDL_USEREVENT){
if (event.user.code==AUDIO_EVENT){
t = 0U; /* No halt */
while (audio_needservice()){ /* Waiting for audio data from emulator */
do{
j = rrpge_run(emu, RRPGE_RUN_FREE);
t = rrpge_gethaltcause(emu);
if (t & RRPGE_HLT_AUDIO){ cdi++; }
if (cdi >= 10){
cdi = 0U;
printf("Audio events: %08d\n", auc);
printf("Cycles: %08d\n", j);
main_printstats(emu);
main_printhalt(t);
}
/* Need proper exit point... */
}while ((t & (RRPGE_HLT_AUDIO |
RRPGE_HLT_EXIT |
RRPGE_HLT_STACK |
RRPGE_HLT_INVKCALL |
RRPGE_HLT_INVOP |
RRPGE_HLT_FAULT |
RRPGE_HLT_DETACHED |
RRPGE_HLT_WAIT)) == 0U);
if ((t & RRPGE_HLT_AUDIO) != 0U){ /* Audio data produced by emulator */
audio_getbufptrs(&lpt, &rpt);
rrpge_getaudio(emu, lpt, rpt);
auc++;
}
if ((t & (RRPGE_HLT_EXIT |
RRPGE_HLT_STACK |
RRPGE_HLT_INVKCALL |
RRPGE_HLT_INVOP |
RRPGE_HLT_FAULT |
RRPGE_HLT_DETACHED |
RRPGE_HLT_WAIT)) != 0U){ /* Errors & Exit */
break;
}
} /* End of audio wait loop */
if ((t & RRPGE_HLT_EXIT) != 0U){ break; } /* Exit program */
if ((t & (RRPGE_HLT_STACK |
RRPGE_HLT_INVKCALL |
RRPGE_HLT_INVOP |
RRPGE_HLT_FAULT |
RRPGE_HLT_DETACHED |
RRPGE_HLT_WAIT)) != 0U){
main_errexit(t, emu);
break; /* Also exit, with error */
}
}
}else if (event.type==SDL_KEYDOWN){
/* Some key was pressed. Which? */
if (event.key.keysym.sym==SDLK_ESCAPE) break; /* Exit program */
}else if (event.type==SDL_KEYUP){
}else if (event.type==SDL_QUIT){ /* Exit program */
break;
}else if (event.type==SDL_MOUSEBUTTONDOWN){
edt = (uint16)(event.button.x);
rrpge_dev_push(emu, mid, 2U, 1U, &edt);
edt = (uint16)(event.button.y);
rrpge_dev_push(emu, mid, 3U, 1U, &edt);
if (event.button.button == SDL_BUTTON_LEFT){ edt = 1U; rrpge_dev_push(emu, mid, 0U, 1U, &edt); }
else if (event.button.button == SDL_BUTTON_RIGHT){ edt = 2U; rrpge_dev_push(emu, mid, 0U, 1U, &edt); }
else if (event.button.button == SDL_BUTTON_MIDDLE){ edt = 3U; rrpge_dev_push(emu, mid, 0U, 1U, &edt); }
else {}
}else if (event.type==SDL_MOUSEBUTTONUP){
edt = (uint16)(event.button.x);
rrpge_dev_push(emu, mid, 2U, 1U, &edt);
edt = (uint16)(event.button.y);
rrpge_dev_push(emu, mid, 3U, 1U, &edt);
if (event.button.button == SDL_BUTTON_LEFT){ edt = 1U; rrpge_dev_push(emu, mid, 1U, 1U, &edt); }
else if (event.button.button == SDL_BUTTON_RIGHT){ edt = 2U; rrpge_dev_push(emu, mid, 1U, 1U, &edt); }
else if (event.button.button == SDL_BUTTON_MIDDLE){ edt = 3U; rrpge_dev_push(emu, mid, 1U, 1U, &edt); }
else {}
}else if (event.type==SDL_MOUSEMOTION){
edt = (uint16)(event.motion.x);
rrpge_dev_push(emu, mid, 2U, 1U, &edt);
edt = (uint16)(event.motion.y);
rrpge_dev_push(emu, mid, 3U, 1U, &edt);
}
}
printf("Trying to exit\n");
rrpge_delete(emu);
audio_free();
screen_free();
SDL_Quit();
fclose(main_app);
exit(0);
loadfault:
if (emu != NULL) free(emu);
fclose(main_app);
exit(1);
}