-
Notifications
You must be signed in to change notification settings - Fork 0
/
emulation_c.c
1667 lines (1459 loc) · 45.1 KB
/
emulation_c.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
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/************************************************************************
* emulation_c.c
* Main emulation routines (C part)
*
************************************************************************
*
* Phoinix,
* Nintendo Gameboy(TM) emulator for the Palm OS(R) Computing Platform
*
* (c)2000-2008 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: emulation_c.c,v $
* Revision 1.34 2008/06/08 08:54:08 bodowenzel
* Tracking dead zone for pen cursor
*
* Revision 1.33 2007/02/25 13:15:32 bodowenzel
* Show last state in Load State while emulating
*
* Revision 1.32 2007/02/14 15:23:57 bodowenzel
* Set HDMA5 bit 7 for better compatibility
*
* Revision 1.31 2007/02/11 17:14:36 bodowenzel
* New info control for wall clock
* Multiple saved state feature implemented
*
* Revision 1.30 2006/12/19 18:58:43 bodowenzel
* Prevent both directions (u/d, l/r) simultaneously
*
* Revision 1.29 2006/11/18 16:18:23 bodowenzel
* Capture the Back key of the Fossil Wrist PDA
*
* Revision 1.28 2005/10/11 14:48:23 bodowenzel
* Disable Jot on Wrist PDA
*
* Revision 1.27 2005/06/28 15:58:37 bodowenzel
* GB didn't redraw after relaunch with paused game
*
* Revision 1.26 2005/06/25 06:48:21 bodowenzel
* Redraw of GB screen can't be clipped, delayed now
*
* Revision 1.25 2005/05/06 11:13:25 bodowenzel
* Correct redraw when paused
*
* Revision 1.24 2005/05/03 08:40:12 bodowenzel
* Moved coldReset, ime, im and error into EmulationCpuState
* Show boost state after reset, too
* Adding states dirty flag for fewer error messages
* Reset pause state after reset
*
* Revision 1.23 2005/04/03 14:07:42 bodowenzel
* Open menu with taps in title area also while pausing
* Option to save the state for each game
*
* Revision 1.22 2005/03/01 17:19:57 bodowenzel
* While emulating open menu with taps in title area
*
* Revision 1.21 2005/02/17 19:42:35 bodowenzel
* Added pace control
*
* Revision 1.20 2005/01/30 19:35:26 bodowenzel
* Removed support for VFS mount and unmount
*
* Revision 1.19 2005/01/29 10:25:34 bodowenzel
* Added const qualifier to pointer parameters
*
* Revision 1.18 2004/12/28 13:57:56 bodowenzel
* Disable auto-off with pressed keys
* Menu command to press all keys
* Menu shortcuts allowed
* Split up all C-Code to multi-segmented
*
* Revision 1.17 2004/12/02 20:01:57 bodowenzel
* Utility function MiscGetObjectPointer()
* New configurable screen layout
*
* Revision 1.16 2004/10/24 09:11:05 bodowenzel
* New button mapping
*
* Revision 1.15 2004/10/18 17:48:51 bodowenzel
* Dropped macro LITE, no need to maintain smaller code
*
* Revision 1.14 2004/09/19 12:36:41 bodowenzel
* Enhanced error reporting with string parameter
* New info type (None)
*
* Revision 1.13 2004/06/20 14:20:45 bodowenzel
* Adjustments for volume names, correct RAM name
* VFS mount and unmount support
*
* Revision 1.12 2004/06/11 16:14:19 bodowenzel
* Enhanced error reporting with MiscPostError()
*
* Revision 1.11 2004/06/06 09:25:01 bodowenzel
* Change game's location to RAM on error saving states
*
* Revision 1.10 2004/03/02 19:24:16 bodowenzel
* Changed to new error function MiscShowError()
*
* Revision 1.9 2004/01/11 19:07:14 bodowenzel
* Corrected copyright
*
* Revision 1.8 2003/04/27 09:35:22 bodowenzel
* Support Tungsten T keys (quick-n-dirty)
* Added Lite edition
*
* Revision 1.7 2002/10/23 16:33:28 bodowenzel
* Better handling of system events (alarms and timeouts)
*
* Revision 1.6 2002/10/19 08:08:10 bodowenzel
* History cleanup, Javadoc-style header
*
* Revision 1.5 2002/02/18 17:16:19 bodowenzel
* Crash if autostarting at short memory
*
* Revision 1.4 2002/01/05 10:21:16 bodowenzel
* Bug: delay after switch-on while emulating
*
* Revision 1.3 2001/12/30 18:47:11 bodowenzel
* CVS keyword Log was faulty
*
* Revision 1.2 2001/12/28 18:27:23 bodowenzel
* Disabled shortcut-bar for OS >= 3.5, can't intercept its display
* Bug found, if restarted twice without running (GB-PC was reset)
* Speed display now with TimGetSeconds(), for use with overclockers
*
* Revision 1.1.1.1 2001/12/16 13:38:58 bodowenzel
* Import
*
************************************************************************
* Pre-CVS History (developed as PalmBoy):
*
* 2001-09-16 Bodo Wenzel Saving states now possible
* 2001-07-27 Bodo Wenzel v.4.0beta, changed to C
* 2001-07-01 Bodo Wenzel v.3.3b, Bugfix corrected ;-)
* 2001-06-23 Bodo Wenzel v.3.3, Bug in MBC1 code corrected
* 2001-03-18 Bodo Wenzel Support of more cartridges,
* new stack management, STOP corrected
* 2000-11-21 Bodo Wenzel Support of color devices and
* better joypad emulation
* 2000-11-03 Bodo Wenzel Request of EasterEgg corrected
* 2000-10-22 Bodo Wenzel Interrupts and timer
* 2000-09-30 Bodo Wenzel Scroller in GB code
* 2000-09-13 Bodo Wenzel Some things in separated files
* 2000-08-02 Bodo Wenzel Now nearly full screen emulation
* 2000-05-07 Bodo Wenzel First working version
* 2000-05-06 Bodo Wenzel First working version with built-in games
* 2000-05-01 Bodo Wenzel Got from full source v.0.4 as runmode.asm
************************************************************************
*/
/* IMPORTANT!
* Please make sure that any referenced resource is included!
* Unfortunately there is no automatic!
*/
/* === Includes ======================================================= */
/* Access needed until a function is provided, see below */
#define ALLOW_ACCESS_TO_INTERNALS_OF_MENUS
#include <PalmOS.h>
#include "Phoinix.h"
#ifdef handera
/* Handera H330 support */
#include <HandEra/Vga.h>
#endif
#ifdef palmgamepad
/* GamePad support */
#define UInt8 UInt8 /* they check it with #ifdef :-( */
#include "GPDLib.h"
#endif
#ifdef fossil
/* Fossil WristPDA support */
#include <JotAPI.h>
#include <WristPDA.h>
#endif
#include "emulation.h"
#include "prefs.h"
#include "misc.h"
#include "states.h"
#include "ram.h"
#include "vfs.h"
#include "video.h"
#include "memory.h"
#include "gbemu.h"
/* === Constants ====================================================== */
/* Key bits are inverted: released/pressed = 1/0 */
#define joypadCursorMask (0xef) /* bits: 1 1 1 0 down up left right */
#define joypadCursorDown (0x08)
#define joypadCursorUp (0x04)
#define joypadCursorLeft (0x02)
#define joypadCursorRight (0x01)
#define joypadControlMask (0xdf) /* bits: 1 1 0 1 start select b a */
#define joypadControlStart (0x08)
#define joypadControlSelect (0x04)
#define joypadControlB (0x02)
#define joypadControlA (0x01)
#define joypadAllMask (0x0f)
#define showBatteryPeriod (10) /* seconds */
#define keyHoldFrames (15) /* frames */
#define penTitleHeight (16) /* pen coordinates */
#define penMinDeadZone (10) /* pen coordinates */
/* === Type definitions =============================================== */
typedef enum {
keyHoldFsmOff = 0,
keyHoldFsmIdle,
keyHoldFsmHold1,
keyHoldFsmWait1,
keyHoldFsmHold2,
keyHoldFsmWait2,
} KeyHoldFsmType;
typedef enum {
penStateUp = 0,
penStateTitle,
penStateCursor,
penStateControl,
} PenStateType;
/* === Global and static variables ==================================== */
EventType EmulationTheEvent;
static Boolean CmdBarOpen = false;
static FormType *FormP = NULL;
static ControlType *PauseCtlP;
static ControlType *BoostCtlP;
static ControlType *SelectCtlP;
static RectangleType SelectBounds;
static ControlType *StartCtlP;
static RectangleType StartBounds;
static ControlType *BCtlP;
static RectangleType BBounds;
static ControlType *ACtlP;
static RectangleType ABounds;
static ControlType *TimeCtlP;
static ControlType *BatteryCtlP;
static ControlType *PaceCtlP;
static ControlType *ClockCtlP;
#ifdef palmgamepad
static UInt16 GamePadLibRefNum;
#endif
static MemHandle EmulatorH = NULL;
static Boolean Running = false;
static Boolean ResetRunning;
static Boolean Paused;
static EmulationCpuStateType SavedCpuState;
static Boolean Crashed;
static UInt32 Seconds;
static UInt32 GameTime;
static UInt16 FramesPerSecond[2];
static UInt32 KeyMask[emulationKeyCount];
static KeyHoldFsmType KeyHoldFsmVertical;
static KeyHoldFsmType KeyHoldFsmHorizontal;
static UInt16 KeyAllKeysHoldFrames;
static PenStateType PenState;
static Coord PenCenterX;
static Coord PenCenterY;
static Coord PenDeadZone;
static UInt8 PenJoypadCursor;
static UInt8 PenJoypadControl;
/* Address of emulator kernel; NOTE: This points to combo 0x00 at offset
* 0x8000 because of SInt16 indices!
*/
GbemuComboType *EmulationEmulator;
UInt8 *EmulationJoypadPtr;
UInt8 EmulationJoypad[4];
UInt8 EmulationJoypadOld; /* saved joypad value before user poll */
/* === Function prototypes ============================================ */
#ifdef fossil
static UInt32 Jot(UInt32 feature)
EMULATION_SECTION;
#endif
static void ShowForm(void)
EMULATION_SECTION;
static void ShowInfo(void)
EMULATION_SECTION;
static void LoadStateDialog(void)
EMULATION_SECTION;
static Boolean LoadStateFormHandleEvent(EventType *evtP)
EMULATION_SECTION;
static void SetupKeyHandling(void)
EMULATION_SECTION;
static void HandleHoldFsm(KeyHoldFsmType *holdFsmP, UInt32 *keysP,
UInt32 mask1, UInt32 mask2)
EMULATION_SECTION;
static void SetupScreenLayout(FormType *frmP)
EMULATION_SECTION;
static Boolean PenStartTitle(Coord x, Coord y)
EMULATION_SECTION;
#define PenTrackTitle(x, y) /* not needed */
static Boolean PenStartCursor(Coord x, Coord y)
EMULATION_SECTION;
static void PenTrackCursor(Coord x, Coord y)
EMULATION_SECTION;
#define PenStartControl PenTrackControl
static Boolean PenTrackControl(Coord x, Coord y)
EMULATION_SECTION;
static void PenFinish(Coord x, Coord y)
EMULATION_SECTION;
static void SetupReset(void)
EMULATION_SECTION;
static void SetupGame(void)
EMULATION_SECTION;
/* === User interface ================================================= */
/**
* Retrieves the next event and stores it in the global variable. While
* emulating, events are sampled at another function; on return from
* emulation, the GB is switched if appropriate.
*/
void EmulationGetEvent(void) {
UInt16 initDelayOld, periodOld, doubleTapDelayOld;
Boolean queueAheadOld;
UInt16 initDelay, period, doubleTapDelay;
Boolean queueAhead;
#ifdef fossil
UInt32 jotFeature = 0;
UInt16 fossilBackKeyMode = 0;
#endif
/* track opened command bar on OS 3.5 and greater */
if (CmdBarOpen) {
if (!MenuCmdBarGetButtonData (0, NULL, NULL, NULL, NULL)) {
/* this works only if at least one icon is shown */
CmdBarOpen = false;
}
}
if (CmdBarOpen || !Running) {
/* emulation is not running */
if (FormP != NULL) {
ShowInfo();
}
EvtGetEvent(&EmulationTheEvent,
showBatteryPeriod * SysTicksPerSecond());
return;
}
/* if there are still events, handle them all before emulating */
if (EmulationTheEvent.eType != nilEvent) {
if (EvtEventAvail()) {
EvtGetEvent(&EmulationTheEvent, evtNoWait);
if (EmulationTheEvent.eType != keyDownEvent) {
return;
} else {
/* filter mapped application buttons */
UInt16 modifiers;
modifiers = EmulationTheEvent.data.keyDown.modifiers;
if ((modifiers & commandKeyMask) == 0 ||
(modifiers & poweredOnKeyMask) != 0) {
return;
} else {
UInt16 i;
for (i = 0; i < emulationKeyCount; i++) {
UInt16 chr;
chr = PrefsPreferences.buttonMapping.key[i].chr;
if (chr != prefsButtonMappingNoChr &&
chr == EmulationTheEvent.data.keyDown.chr) {
break;
}
}
if (i >= emulationKeyCount) {
return;
}
}
}
}
}
/* try to reduce the frequency of key events
* (though the docs say it doesn't work really well)
*/
KeyRates(false, &initDelayOld, &periodOld, &doubleTapDelayOld,
&queueAheadOld);
initDelay = slowestKeyDelayRate;
period = slowestKeyPeriodRate;
doubleTapDelay = slowestKeyPeriodRate;
queueAhead = false;
KeyRates(true, &initDelay, &period, &doubleTapDelay, &queueAhead);
#ifdef fossil
/* special handling of the Wrist PDA */
if (MiscDevice == miscDeviceWrist) {
jotFeature = Jot(DISABLE_JOT);
if ((jotFeature & DISABLE_JOT) == 0) {
jotFeature = ENABLE_JOT;
}
fossilBackKeyMode = FossilBackKeyModeGet();
FossilBackKeyModeSet(kFossilBackKeyNoAction);
}
#endif
/* release memory protection */
MemSemaphoreReserve(true);
/* *** HERE IT IS! THE EMULATOR STARTS EXECUTION... ***************** */
EmulationStartKernel();
/* ****************************************************************** */
if (ResetRunning) {
/* the GB reset code ends with an unknown opcode - normally ;-) */
if (EmulationCpuState.error == gbemuRetOpcodeUnknown) {
/* setup the game now */
SetupGame();
/* prevent stopping */
EmulationCpuState.error = gbemuRetUserStop;
EmulationTheEvent.eType = nilEvent;
}
}
/* regain memory protection */
MemSemaphoreRelease(true);
#ifdef fossil
/* restore settings on Wrist PDA */
if (MiscDevice == miscDeviceWrist) {
(void)Jot(jotFeature);
FossilBackKeyModeSet(fossilBackKeyMode);
}
#endif
/* restore key rates */
KeyRates(true, &initDelayOld, &periodOld, &doubleTapDelayOld,
&queueAheadOld);
/* on emulation errors alert and go back to the manager */
if (EmulationCpuState.error != gbemuRetUserStop) {
MiscShowError(miscErrEmulator, miscErrRuntime, NULL);
FrmGotoForm(formIdManager);
/* stop the emulation right NOW! */
PrefsPreferences.running = false;
Running = false;
Crashed = true;
EmulationTheEvent.eType = nilEvent;
}
}
/**
* Handles events in the emulation form.
*
* @param evtP pointer to the event structure.
* @return true if the event is handled.
*/
Boolean EmulationFormHandleEvent(EventType *evtP) {
#ifdef palmgamepad
Err err;
#endif
switch (evtP->eType) {
case frmOpenEvent:
FormP = FrmGetActiveForm();
/* remember pointer to display buttons for ShowInfo() */
TimeCtlP = MiscGetObjectPtr(FormP, buttonIdEmulationTime);
BatteryCtlP = MiscGetObjectPtr(FormP, buttonIdEmulationBattery);
PaceCtlP = MiscGetObjectPtr(FormP, buttonIdEmulationPace);
ClockCtlP = MiscGetObjectPtr(FormP, buttonIdEmulationClock);
/* setup pause checkbox */
PauseCtlP = MiscGetObjectPtr(FormP, checkboxIdEmulationPause);
CtlSetValue(PauseCtlP, Paused ? 1 : 0);
/* setup boost checkbox */
BoostCtlP = MiscGetObjectPtr(FormP, checkboxIdEmulationBoost);
CtlSetValue(BoostCtlP, VideoBoosted ? 1 : 0);
/* setup screen layout */
SetupScreenLayout(FormP);
/* show form, sets draw window */
ShowForm();
/* setup key handling */
SetupKeyHandling();
#ifdef palmgamepad
/* reserve and open GamePad library if present */
err = SysLibFind(GPD_LIB_NAME, &GamePadLibRefNum);
if (err == sysErrLibNotFound) {
err = SysLibLoad('libr', GPD_LIB_CREATOR, &GamePadLibRefNum);
}
if (err == errNone) {
err = GPDOpen(GamePadLibRefNum);
}
if (err != errNone && err != GPDErrGamePadNotPresent) {
GamePadLibRefNum = sysInvalidRefNum;
}
#endif
/* start the GB reset code */
SetupReset();
VideoSetupPaceControl();
/* setup for user interface while emulating */
Seconds = TimGetSeconds();
ShowInfo();
return true;
case frmUpdateEvent:
ShowForm();
return true;
case frmCloseEvent:
/* reset variables for EmulationGetEvent() */
FormP = NULL;
Running = false;
/* stop emulation gracefully */
if (Crashed) {
StatesSaveCurrentStateAsCrashed();
} else if (PrefsPreferences.running ||
(StatesEmulationOptionsP->save && !ResetRunning)) {
StatesSaveCurrentState();
} else {
StatesResetCurrentState();
}
StatesSetPrefsAndStatesDirty();
VideoCleanup();
EmulationCleanup();
/* close states database here so that MemoryCleanup() works correctly
* in case of errors
*/
StatesClosePrefsAndStatesWithShow(MiscPostError, false);
MemoryCleanup();
#ifdef palmgamepad
/* if GamePad library was found, close and release it */
if (GamePadLibRefNum != sysInvalidRefNum) {
UInt32 numUsers;
GPDClose(GamePadLibRefNum, &numUsers);
if (numUsers == 0) {
SysLibRemove(GamePadLibRefNum);
}
}
#endif
return false;
case winExitEvent:
/* stop the emulation while handling the GUI events */
if (evtP->data.winExit.exitWindow == FrmGetWindowHandle(FormP)) {
Running = false;
}
return false;
case winEnterEvent:
/* restart the emulation after handling the GUI events */
if (evtP->data.winEnter.enterWindow == FrmGetWindowHandle(FormP)) {
Running = (ResetRunning || !Paused) && !Crashed;
VideoSetupPaceControl();
/* now we can safely update the GB screen */
MemSemaphoreReserve(true);
VideoCopyScreen();
MemSemaphoreRelease(true);
/* this works also after changing the button mapping */
SetupKeyHandling();
}
return false;
case ctlSelectEvent:
switch (evtP->data.ctlSelect.controlID) {
case checkboxIdEmulationPause:
Paused = evtP->data.ctlSelect.on;
Running = ResetRunning || !Paused;
VideoSetupPaceControl();
/* in case the emulation was stopped, correct the seconds */
Seconds = TimGetSeconds();
break;
case checkboxIdEmulationBoost:
VideoBoosted = evtP->data.ctlSelect.on;
VideoSetupPaceControl();
break;
default:
return false;
}
return true;
case penDownEvent:
if (PenStartTitle(EmulationTheEvent.screenX,
EmulationTheEvent.screenY)) {
PenState = penStateTitle;
} else {
PenState = penStateUp;
}
return false;
case penMoveEvent:
switch (PenState) {
case penStateTitle:
PenTrackTitle(EmulationTheEvent.screenX, EmulationTheEvent.screenY);
break;
default:
break;
}
return false;
case penUpEvent:
if (PenState != penStateUp) {
PenFinish(EmulationTheEvent.screenX, EmulationTheEvent.screenY);
PenState = penStateUp;
}
return false;
case menuCmdBarOpenEvent:
CmdBarOpen = true;
MenuCmdBarAddButton(menuCmdBarOnRight, BarPasteBitmap,
menuCmdBarResultMenuItem,
menuIdEmulationStateLoad, NULL);
MenuCmdBarAddButton(menuCmdBarOnRight, BarCopyBitmap,
menuCmdBarResultMenuItem,
menuIdEmulationStateSave, NULL);
MenuCmdBarAddButton(menuCmdBarOnRight, BarUndoBitmap,
menuCmdBarResultMenuItem,
menuIdEmulationGameReset, NULL);
MenuCmdBarAddButton(menuCmdBarOnRight, BarDeleteBitmap,
menuCmdBarResultMenuItem,
menuIdEmulationGameQuit, NULL);
return false;
case menuEvent:
/* the display of the chosen menu command can't be catched and it will
* get garbled by Phoinix' output, so it has to erased if displayed
*/
MenuEraseStatus(NULL);
switch (evtP->data.menu.itemID) {
case menuIdEmulationGamePressAllKeys:
KeyAllKeysHoldFrames = keyHoldFrames;
break;
case menuIdEmulationGamePause:
/* toggle Paused */
Paused = !Paused;
CtlSetValue(PauseCtlP, Paused ? 1 : 0);
Running = ResetRunning || !Paused;
VideoSetupPaceControl();
/* in case the emulation was stopped, correct the seconds */
Seconds = TimGetSeconds();
break;
case menuIdEmulationGameBoost:
/* toggle Boosted */
VideoBoosted = !VideoBoosted;
CtlSetValue(BoostCtlP, VideoBoosted ? 1 : 0);
VideoSetupPaceControl();
break;
case menuIdEmulationGameReset:
StatesResetCurrentState();
StatesLoadCurrentState();
/* start the GB reset code */
SetupReset();
/* reflect changes on screen */
CtlSetValue(PauseCtlP, Paused ? 1 : 0);
CtlSetValue(BoostCtlP, VideoBoosted ? 1 : 0);
ShowInfo();
break;
case menuIdEmulationGameQuit:
PrefsPreferences.running = false;
FrmGotoForm(formIdManager);
/* frmCloseEvent will come, too... */
break;
case menuIdEmulationStateLoad:
LoadStateDialog();
/* reflect changes on screen */
CtlSetValue(PauseCtlP, Paused ? 1 : 0);
CtlSetValue(BoostCtlP, VideoBoosted ? 1 : 0);
ShowInfo();
break;
case menuIdEmulationStateSave:
if (!StatesCreateStateFromCurrentState()) {
MiscShowError(miscErrEmulator, miscErrStateSave, NULL);
}
break;
case menuIdEmulationOptionsEmulation:
PrefsEmulationOptionsDialog(StatesEmulationOptionsP);
VideoSetupPaceControl();
break;
case menuIdEmulationOptionsButtonMapping:
PrefsButtonMappingDialog(StatesButtonMappingP);
break;
case menuIdEmulationOptionsScreenLayout:
PrefsScreenLayoutDialog(StatesScreenLayoutP);
SetupScreenLayout(FormP);
break;
case menuIdEmulationOptionsTips:
FrmHelp(helpIdEmulation);
break;
case menuIdEmulationOptionsAbout:
MiscShowAbout();
break;
default:
return false;
}
return true;
default:
return false;
}
}
#ifdef fossil
/**
* Calls Jot on Wrist PDA.
*
* @param feature bit mask of feature to switch.
* @return bit mask of currently set features.
*/
static UInt32 Jot(UInt32 feature) {
UInt16 cardNo;
LocalID dbId;
DmSearchStateType searchState;
if (DmGetNextDatabaseByTypeCreator(
true, &searchState, sysFileTApplication, JOTCREATOR, true,
&cardNo, &dbId) == errNone) {
SysAppLaunch(cardNo, dbId, 0, 32801, &feature, NULL);
return feature;
}
return 0;
}
#endif
/**
* Draws the emulation form, on bigger screens with a visible frame for
* the GB screen.
*/
static void ShowForm(void) {
/* show form */
FrmDrawForm(FormP);
#ifdef handera
/* on H330, switch screen mode to draw the full rectangle */
if (MiscDevice == miscDeviceH330) {
VgaSetScreenMode(screenMode1To1, rotateModeNone);
WinSetDrawWindow(WinGetDisplayWindow());
}
#endif
WinDrawRectangleFrame(simpleFrame, &VideoGbScreenBounds);
#ifdef handera
if (MiscDevice == miscDeviceH330) {
VgaSetScreenMode(screenModeScaleToFit, rotateModeNone);
WinSetDrawWindow(FrmGetWindowHandle(FormP));
}
#endif
}
/**
* Draws the info lines.
*/
static void ShowInfo(void) {
MenuBarType *menuBarP;
UInt16 h, m;
UInt8 b;
UInt16 si, sf;
DateTimeType dt;
static Char timeLine[8];
static Char batteryLine[8];
static Char paceLine[8];
static Char clockLine[8];
/* don't draw over an open command bar */
if (CmdBarOpen) {
return;
}
menuBarP = MenuGetActiveMenu();
if (menuBarP != NULL) {
/* this is for OS versions below 3.5 */
/* we need ALLOW_ACCESS_TO_INTERNALS_OF_MENUS defined */
if (menuBarP->attr.commandPending) {
return;
}
}
/* show played time */
m = (UInt16)(GameTime / 60);
h = m / 60;
m %= 60;
StrPrintF(timeLine, "%u:%02u", h, m);
CtlSetLabel(TimeCtlP, timeLine);
/* show current battery power */
(void)SysBatteryInfo(false, NULL, NULL, NULL, NULL, NULL, &b);
StrPrintF(batteryLine, "%u%%", b);
CtlSetLabel(BatteryCtlP, batteryLine);
/* show emulation speed */
sf = (50 * (FramesPerSecond[0] + FramesPerSecond[1])) /
videoFramesPerSec;
si = sf / 100;
sf %= 100;
StrPrintF(paceLine, "%u.%02u\xd7", si, sf);
CtlSetLabel(PaceCtlP, paceLine);
/* show wall clock */
TimSecondsToDateTime(TimGetSeconds(), &dt);
StrPrintF(clockLine, "%u:%02u", dt.hour, dt.minute);
CtlSetLabel(ClockCtlP, clockLine);
}
/**
* Shows the load state form.
*/
static void LoadStateDialog(void) {
FormType *prevFrmP, *frmP;
ListType *lstP;
Int16 index;
/* prepare new form */
prevFrmP = FrmGetActiveForm();
frmP = FrmInitForm(formIdLoadState);
FrmSetActiveForm(frmP);
FrmSetEventHandler(frmP, LoadStateFormHandleEvent);
lstP = MiscGetObjectPtr(frmP, listIdLoadState);
StatesSetupList(frmP, lstP, statesLastState);
/* show form and get selected entry */
FrmDoDialog(frmP);
index = LstGetSelection(lstP);
/* cleanup */
if (prevFrmP != NULL) {
FrmSetActiveForm(prevFrmP);
}
FrmDeleteForm(frmP);
/* the first entry is the default state = cancel */
if (index > 0) {
if (!StatesCopyStateToCurrentState(index)) {
MiscShowError(miscErrEmulator, miscErrStateLoad, NULL);
} else {
StatesLoadCurrentState();
/* start the GB reset code */
SetupReset();
}
}
}
/**
* Handles events in the load state form.
*
* @param evtP pointer to the event structure.
* @return true if the event is handled.
*/
static Boolean LoadStateFormHandleEvent(EventType *evtP) {
FormType *frmP;
ListType *lstP;
frmP = FrmGetActiveForm();
lstP = MiscGetObjectPtr(frmP, listIdLoadState);
switch (evtP->eType) {
case winEnterEvent:
/* apparently we get this event only for our window */
/* there is no frmUpdateEvent for modal forms through FrmDoDialog */
StatesDrawGoButton(frmP, lstP, buttonIdLoadState, labelIdLoadState);
return false;
case lstSelectEvent:
StatesDrawGoButton(frmP, lstP, buttonIdLoadState, labelIdLoadState);
return true;
case appStopEvent:
/* to cancel the first entry must be selected */
LstSetSelection(lstP, 0);
return false;
default:
return false;
}
}
/* === Polling while emulating ======================================== */
/**
* Polls the user interface and the system. When events have to be
* handled, the emulation is stopped.
*
* @return true if the emulation should quit.
*/
Boolean EmulationUserPoll(void) {
Boolean getEvent, quit;
UInt8 joypadCursor, joypadControl;
UInt32 keys;
UInt32 secs;
#ifdef palmgamepad
UInt8 gamepadKeys;
#endif
/* default: don't quit the emulation... */
quit = false;
/* regain memory protection */
MemSemaphoreRelease(true);
/* start with no buttons pressed */
joypadCursor = joypadCursorMask;
joypadControl = joypadControlMask;
/* read and evaluate own hard keys */
keys = KeyCurrentState();
HandleHoldFsm(&KeyHoldFsmVertical, &keys,
KeyMask[offsetButtonMappingUp],
KeyMask[offsetButtonMappingDown]);
HandleHoldFsm(&KeyHoldFsmHorizontal, &keys,
KeyMask[offsetButtonMappingLeft],
KeyMask[offsetButtonMappingRight]);
if ((keys & KeyMask[offsetButtonMappingUp]) != 0) {
joypadCursor &= ~joypadCursorUp;
}
if ((keys & KeyMask[offsetButtonMappingDown]) != 0) {
joypadCursor &= ~joypadCursorDown;
}
if ((keys & KeyMask[offsetButtonMappingLeft]) != 0) {
joypadCursor &= ~joypadCursorLeft;
}
if ((keys & KeyMask[offsetButtonMappingRight]) != 0) {
joypadCursor &= ~joypadCursorRight;
}
if ((keys & KeyMask[offsetButtonMappingSelect]) != 0) {
joypadControl &= ~joypadControlSelect;
}
if ((keys & KeyMask[offsetButtonMappingStart]) != 0) {
joypadControl &= ~joypadControlStart;
}
if ((keys & KeyMask[offsetButtonMappingB]) != 0) {
joypadControl &= ~joypadControlB;
}
if ((keys & KeyMask[offsetButtonMappingA]) != 0) {
joypadControl &= ~joypadControlA;
}