-
Notifications
You must be signed in to change notification settings - Fork 0
/
Phoinix.c
298 lines (265 loc) · 8.62 KB
/
Phoinix.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
/************************************************************************
* Phoinix.c
* Main module
*
************************************************************************
*
* Phoinix,
* Nintendo Gameboy(TM) emulator for the Palm OS(R) Computing Platform
*
* (c)2000-2005 Bodo Wenzel
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
************************************************************************
* History:
*
* $Log: Phoinix.c,v $
* Revision 1.19 2005/04/03 14:08:33 bodowenzel
* Option to save the state for each game
*
* Revision 1.18 2005/01/30 19:35:26 bodowenzel
* Removed support for VFS mount and unmount
*
* Revision 1.17 2005/01/29 10:25:34 bodowenzel
* Added const qualifier to pointer parameters
*
* Revision 1.16 2005/01/28 17:35:13 bodowenzel
* Manager form uses a scrollable table now for the list of games
*
* Revision 1.15 2004/12/28 13:56:35 bodowenzel
* Split up all C-Code to multi-segmented
*
* Revision 1.14 2004/10/18 17:48:51 bodowenzel
* Dropped macro LITE, no need to maintain smaller code
*
* Revision 1.13 2004/09/19 12:35:54 bodowenzel
* Enhanced error reporting with string parameter
*
* Revision 1.12 2004/06/20 14:23:20 bodowenzel
* VFS mount and unmount support
*
* Revision 1.11 2004/06/11 16:14:19 bodowenzel
* Enhanced error reporting with MiscPostError()
*
* Revision 1.10 2004/04/05 21:56:55 bodowenzel
* VFS review and its consequences
*
* Revision 1.9 2004/03/02 19:24:16 bodowenzel
* Changed to new error function MiscShowError()
*
* Revision 1.8 2004/01/11 19:05:58 bodowenzel
* Start of VFS review and correction
*
* Revision 1.7 2003/04/27 09:36:18 bodowenzel
* Added Lite edition
*
* Revision 1.6 2002/12/07 08:57:43 bodowenzel
* OS5 check removed
*
* Revision 1.5 2002/11/02 15:53:41 bodowenzel
* Check for OS 5 added
*
* Revision 1.4 2002/10/19 08:08:10 bodowenzel
* History cleanup, Javadoc-style header
*
* Revision 1.3 2002/05/10 15:17:39 bodowenzel
* Till's VFS contribution
*
* Revision 1.2 2001/12/30 18:47:11 bodowenzel
* CVS keyword Log was faulty
*
* Revision 1.1.1.1 2001/12/16 13:38:30 bodowenzel
* Import
*
************************************************************************
* Pre-CVS History (developed as PalmBoy):
*
* 2001-07-23 Bodo Wenzel v.4.0, Many sources converted to C,
* compiler/assembler now GCC
* 2001-07-01 Bodo Wenzel v.3.3b, Bugfix corrected ;-)
* 2001-06-23 Bodo Wenzel v.3.3, Bug in MBC1 code corrected
* 2001-02-22 Bodo Wenzel v.3.2 (v.3.14159), Hsync interrupts and
* support of more cartridges
* 2000-11-18 Bodo Wenzel v.3.1, Support of color devices
* 2000-11-03 Bodo Wenzel v.3.0a, No more "Visor crash"
* 2000-10-16 Bodo Wenzel v.3.0, Interrupts and timer
* 2000-09-30 Bodo Wenzel v.2.1, Scroller in GB code
* 2000-09-11 Bodo Wenzel v.2.0, Nearly full screen emulation
* 2000-05-07 Bodo Wenzel v.1.0, First working version
* 2000-05-06 Bodo Wenzel v.0.9, First working version with built-in
* games
* 2000-05-01 Bodo Wenzel v.0.4, Quick'n'dirty screen conversion,
* splitting in modules, and usage of more
* registers
* 2000-04-25 Bodo Wenzel v.0.3, Errors fixed with Palm IIIC, V
* 2000-04-19 Bodo Wenzel v.0.2, Creation of user interface
* 2000-04-15 Bodo Wenzel v.0.1, Creation of user interface without
* gray image
* 2000-03-28 Bodo Wenzel v.0.0, Creation of user interface without
* emulator mock-up and gray image
************************************************************************
*/
/* IMPORTANT!
* Please make sure that any referenced resource is included!
* Unfortunately there is no automatic!
*/
/* === Includes ======================================================= */
#include <PalmOS.h>
#include <VFSMgr.h>
#include "Phoinix.h"
#include "sections.h"
#include "misc.h"
#include "prefs.h"
#include "manager.h"
#include "states.h"
#include "vfs.h"
#include "emulation.h"
#include "video.h"
/* === Function prototypes ============================================ */
static Err StartApplication(UInt16 launchFlags)
PHOINIX_SECTION;
static void StopApplication(void)
PHOINIX_SECTION;
static void EventLoop(void)
PHOINIX_SECTION;
static Boolean ApplicationHandleEvent(const EventType *evtP)
PHOINIX_SECTION;
/* === M A I N ======================================================== */
/**
* Main function of the application, returns an error code.
*
* @param cmd launch code.
* @param cmdPBP pointer to the parameter block.
* @param launchFlags several flags from the launcher.
* @return error code, errNone if all is OK.
*/
UInt32 PilotMain(UInt16 cmd, void *cmdPBP, UInt16 launchFlags) {
Err ret;
ret = errNone;
switch (cmd) {
case sysAppLaunchCmdNormalLaunch:
/* normal launch */
ret = StartApplication(launchFlags);
if (ret == errNone) {
EventLoop();
StopApplication();
}
default:
break;
}
return ret;
}
/* === Start and stop ================================================= */
/**
* Initializes the application, run once before the event loop.
*
* @param launchFlags several flags from the launcher.
* @return error code, errNone if all is OK.
*/
static Err StartApplication(UInt16 launchFlags) {
Err err;
/* check for adequate OS version */
err = MiscRomVersionCompatible(
sysMakeROMVersion(3, 0, 0, sysROMStageRelease, 0),
0xFFFFFFFFUL, /* no upper limit yet */
launchFlags);
if (err != errNone) {
return err;
}
/* check the display, if OK switch to gray */
if (!VideoGraySwitchOn()) {
MiscShowError(miscErrInitialization, miscErrScreen, NULL);
return sysErrParamErr;
}
/* set up VFS support */
if (!VfsInit()) {
MiscShowError(miscErrVfs, miscErrMemoryFull, NULL);
}
/* load preferences, after VFS support to get the volume index */
PrefsLoad();
/* try to restart an interrupted game */
if (StatesAutostart()) {
FrmGotoForm(formIdEmulation);
} else {
FrmGotoForm(formIdManager);
}
return errNone;
}
/**
* Cleans up after the event loop quits.
*/
static void StopApplication(void) {
/* cleanup GUI */
FrmCloseAllForms();
/* cleanup databases */
StatesClosePrefsAndStatesWithShow(MiscShowError, true);
ManagerExit();
/* cleanup volumes */
VfsExit();
/* save preferences */
PrefsSave();
/* now display the last error, if any */
MiscShowPendingError();
/* switch back to default video mode */
VideoGraySwitchOff();
}
/* === Event handling ================================================= */
/**
* Retrieves and handles all events in all standard cases.
*/
static void EventLoop(void) {
Err err;
do {
EmulationGetEvent();
if (!SysHandleEvent(&EmulationTheEvent)) {
if (!MenuHandleEvent(NULL, &EmulationTheEvent, &err)) {
if (!ApplicationHandleEvent(&EmulationTheEvent)) {
FrmDispatchEvent(&EmulationTheEvent);
}
}
}
} while (EmulationTheEvent.eType != appStopEvent);
}
/**
* Handles globally used events.
*
* @param evtP pointer to the event structure.
* @return true when the event is handled.
*/
static Boolean ApplicationHandleEvent(const EventType *evtP) {
FormType *frmP;
switch (evtP->eType) {
case frmLoadEvent:
frmP = FrmInitForm(evtP->data.frmLoad.formID);
FrmSetActiveForm(frmP);
switch (evtP->data.frmLoad.formID) {
case formIdManager:
FrmSetEventHandler(frmP, ManagerFormHandleEvent);
break;
case formIdEmulation:
FrmSetEventHandler(frmP, EmulationFormHandleEvent);
break;
default:
break;
}
return true;
case phoinixErrorEvent:
MiscShowPendingError();
return true;
default:
return false;
}
}
/* === The end ======================================================== */