-
Notifications
You must be signed in to change notification settings - Fork 0
/
eggs.c
634 lines (534 loc) · 16.1 KB
/
eggs.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
/************************************************************************
* eggs.c
* Easter eggs
*
************************************************************************
*
* Phoinix,
* Nintendo Gameboy(TM) emulator for the Palm OS(R) Computing Platform
*
* (c)2000-2006 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
************************************************************************
* Notes:
*
* This source is also compiled and run on the development host to
* generate the default hidden game.
*
* Run length encoding scheme, all values are in hex:
* 80 = start of compressed data
* 1 to n entries = compressed data
* 00 = end of compressed data
* An entry starts with a counter, flagged in the most significant bit:
* [01..7F] 1 to 127 bytes = 1 to 127 bytes uncompressed data
* [81..FF] byte = 1 to 127 bytes compressed data
************************************************************************
* History:
*
* $Log: eggs.c,v $
* Revision 1.4 2006/04/25 18:51:40 bodowenzel
* Corrections around the easter egg
*
* Revision 1.3 2005/05/26 19:35:46 bodowenzel
* Moved game's info into game's entry, removing MemoryMbcInfo
*
* Revision 1.2 2005/04/03 14:08:33 bodowenzel
* Option to save the state for each game
*
* Revision 1.1 2005/02/27 16:38:24 bodowenzel
* Added module Eggs for easter eggs
*
************************************************************************
*/
/* IMPORTANT!
* Please make sure that any referenced resource is included!
* Unfortunately there is no automatic!
*/
#ifdef __palmos__ /******************************************************/
/* === Includes ======================================================= */
#include <PalmOS.h>
#include "eggs.h"
#include "Phoinix.h"
#include "misc.h"
#include "prefs.h"
#include "states.h"
#include "ram.h"
#include "vfs.h"
#include "gbemu.h"
#include "memory.h"
/* === Constants ====================================================== */
#define hiddenGameDbName "Phoinix-HiddenGame"
#else /* defined(__palmos__) ********************************************/
/* === Adjustments ==================================================== */
#define EGGS_SECTION
typedef enum { false = 0, true } Boolean;
typedef unsigned char UInt8;
typedef unsigned short UInt16;
/* === Includes ======================================================= */
#include <stdio.h>
#include <stdlib.h>
#include "gbemu.h"
#endif /* defined(__palmos__) else **************************************/
/* === Constants ====================================================== */
/* the combos are numbered first 0x80 to 0xff, then 0x00 to 0x7f */
#define maxCombo (((gbemuLastUnused ^ 0x80) & 0xFF) + 1)
/* only 32K games are supported */
#define expandedSize (32 * 1024L)
#define compressedSize (gbemuXtSize * maxCombo)
#define maxCount (0x7F)
#define compressedFlag (0x80)
#define endMarker (0x00)
/* === Function prototypes ============================================ */
static Boolean Compress(UInt8 *destP, const UInt8 *srceP)
EGGS_SECTION;
#ifdef __palmos__ /******************************************************/
/* === Type definitions =============================================== */
typedef struct {
const Char *code; /* keys to enter, must be more than 1! */
void (*func)(void); /* function to call on completion */
} FsmType;
/* === Function prototypes ============================================ */
static void RunHiddenGame(void)
EGGS_SECTION;
static Boolean ExtractHiddenGame(void)
EGGS_SECTION;
static Boolean SetupHiddenGame(void)
EGGS_SECTION;
static void SaveAsHiddenGame(void)
EGGS_SECTION;
static Boolean Expand(UInt8 *destP, const UInt8 *srceP)
EGGS_SECTION;
/* === Static variables =============================================== */
static const FsmType Fsm[] = {
{ "run", RunHiddenGame },
{ "save", SaveAsHiddenGame },
};
static UInt16 FsmIndex;
static UInt16 CodeIndex = 0;
/* === Handle keys ==================================================== */
/**
* Handles entered keys and if triggered starts the secret action.
*
* @param key entered key.
*/
void EggsHandleKey(WChar key) {
/* the system global switch has to be "on" */
if (PrefGetPreference(prefAllowEasterEggs) == 0) {
return;
}
/* check if it's the next expected key */
if (CodeIndex != 0) {
if (key == Fsm[FsmIndex].code[CodeIndex]) {
CodeIndex++;
if (Fsm[FsmIndex].code[CodeIndex] == '\0') {
/* code is complete, call function */
CodeIndex = 0;
(*Fsm[FsmIndex].func)();
}
} else {
CodeIndex = 0;
}
}
/* check for a new code */
if (CodeIndex == 0) {
for (FsmIndex = 0;
FsmIndex < sizeof(Fsm) / sizeof(FsmType);
FsmIndex++) {
if (key == Fsm[FsmIndex].code[0]) {
CodeIndex++;
break;
}
}
}
}
/* === Hidden game ==================================================== */
/**
* Runs the hidden game. Aborts silently on errors.
*/
static void RunHiddenGame(void) {
/* extract the hidden game */
if (ExtractHiddenGame()) {
/* if possible, run the hidden game, else refresh the manager form */
if (SetupHiddenGame()) {
FrmGotoForm(formIdEmulation);
} else {
FrmGotoForm(formIdManager);
}
}
}
/**
* Extracts the hidden game.
*
* @return true if successful.
*/
static Boolean ExtractHiddenGame(void) {
MemHandle emulatorH;
/* get and lock the emulator kernel */
emulatorH = DmGetResource('code', codeIdKernel);
if (emulatorH != NULL) {
GbemuComboType *emulator;
LocalID dbId = 0;
DmOpenRef dbP = NULL;
emulator = (GbemuComboType *)MemHandleLock(emulatorH);
/* check that a game is actually stored */
if (emulator[0].xt.code[0] == compressedFlag) {
/* create temporary database for the game */
if (DmCreateDatabase(ramGameDbCardNo, hiddenGameDbName, miscCreator,
miscGameDbType, false) == errNone) {
dbId = DmFindDatabase(ramGameDbCardNo, hiddenGameDbName);
if (dbId != 0) {
dbP = DmOpenDatabase(ramGameDbCardNo, dbId, dmModeReadWrite);
}
}
}
if (dbP != NULL) {
UInt16 recId;
MemHandle recH;
/* make some room for the game */
recId = 0;
recH = DmNewRecord(dbP, &recId, expandedSize + compressedSize);
if (recH != NULL) {
UInt8 *recP;
UInt16 combo;
Boolean ok;
MemHandle newH;
recP = MemHandleLock(recH);
/* expand the compressed data */
for (combo = 0; combo < maxCombo; combo++) {
DmWrite(recP, expandedSize + combo * gbemuXtSize,
emulator[combo].xt.code, gbemuXtSize);
}
MemSemaphoreReserve(true);
ok = Expand(recP, recP + expandedSize);
MemSemaphoreRelease(true);
/* release and free the memory for the compressed data */
MemHandleUnlock(recH);
DmReleaseRecord(dbP, recId, false);
newH = DmResizeRecord(dbP, recId, expandedSize);
if (recId == 0 && ok && newH != NULL) {
/* the hidden game is successfully extracted */
DmCloseDatabase(dbP);
MemHandleUnlock(emulatorH);
DmReleaseResource(emulatorH);
return true;
}
}
DmCloseDatabase(dbP);
DmDeleteDatabase(ramGameDbCardNo, dbId);
}
MemHandleUnlock(emulatorH);
DmReleaseResource(emulatorH);
}
return false;
}
/**
* Sets up the hidden game.
*
* @return true if sucessful.
*/
static Boolean SetupHiddenGame(void) {
/* close state database of old game, if open */
StatesClosePrefsAndStatesWithShow(MiscShowError, true);
/* erase current volume name to flag the hidden game */
PrefsPreferences.volumeName[0] = '\0';
/* create dummy game entry */
StrCopy(PrefsPreferences.game.name, hiddenGameDbName);
PrefsPreferences.game.volIndex = vfsRamVolIndex;
/* set up the emulation */
if (MemoryRetrieveMbcInfo()) {
if (StatesOpenPrefsAndStates()) {
if (EmulationSetupAll()) {
StatesLoadCurrentState();
return true;
}
}
}
return false;
}
/**
* Removes the temporary database with the hidden game.
*/
void EggsRemoveHiddenGame(void) {
LocalID dbId;
dbId = DmFindDatabase(ramGameDbCardNo, hiddenGameDbName);
if (dbId != 0) {
DmDeleteDatabase(ramGameDbCardNo, dbId);
}
}
/**
* Saves the current game as the hidden game. Aborts silently on errors.
*/
static void SaveAsHiddenGame(void) {
MemHandle recH;
/* check for an appropriate game */
if (PrefsPreferences.game.name[0] == '\0') {
return;
}
if (PrefsPreferences.game.mbc <= memoryMbcUnknown) {
return;
}
if (PrefsPreferences.game.romSize > (UInt16)(expandedSize / 1024)) {
return;
}
/* get and lock the image of the current game */
#if expandedSize != 32 * 1024
#error "Adjust saving a new hidden game"
#endif
if (PrefsPreferences.game.volIndex == vfsRamVolIndex) {
recH = RamGetFirstBlock(&PrefsPreferences.game);
} else {
recH = VfsGetFirstBlock(&PrefsPreferences.game);
}
if (recH != NULL) {
UInt8 *expandedP, *compressedP;
expandedP = MemHandleLock(recH);
/* try to compress the current game */
compressedP = MemPtrNew(compressedSize);
if (compressedP != NULL) {
if (Compress(compressedP, expandedP)) {
MemHandle emulatorH;
/* get and lock the emulator kernel */
emulatorH = DmGetResource('code', codeIdKernel);
if (emulatorH != NULL) {
GbemuComboType *emulator;
UInt16 combo;
emulator = (GbemuComboType *)MemHandleLock(emulatorH);
/* copy new hidden game into emulator kernel */
for (combo = 0; combo < maxCombo; combo++) {
DmWrite(emulator, combo * 0x100 + gbemuXtOffset,
compressedP + combo * gbemuXtSize, gbemuXtSize);
}
MemHandleUnlock(emulatorH);
DmReleaseResource(emulatorH);
}
}
MemPtrFree(compressedP);
}
MemHandleUnlock(recH);
MemHandleFree(recH);
}
}
#else /* defined(__palmos__) ********************************************/
/* === Main =========================================================== */
/**
* Generates the macros with the compressed data of a hidden game. Both
* filenames of a game and the destination source file are expected as
* command line arguments. If the game's file doesn't exist, all macros
* contain empty data.
*
* On errors the application aborts. All cleanup is left to the C runtime
* system.
*
* @param argc number of arguments in argv[].
* @param argv pointer to array of pointers to arguments.
* @return EXIT_SUCCESS or EXIT_FAILURE.
*/
int main(int argc, char *argv[]) {
UInt8 *srceP, *destP;
FILE *file;
int offset, macro;
/* check for filename */
if (argc != 3) {
fprintf(stderr, "Usage: %s <gb-file> <dest-file>\n", argv[0]);
return EXIT_FAILURE;
}
/* obtain buffers */
srceP = malloc(expandedSize);
destP = malloc(compressedSize);
if (srceP == NULL || destP == NULL) {
fputs("Memory full!\n", stderr);
return EXIT_FAILURE;
}
memset(destP, endMarker, compressedSize);
/* read hidden game, and compress, if any */
file = fopen(argv[1], "rb");
if (file != NULL) {
/* check length */
fseek(file, 0, SEEK_END);
if (ftell(file) != expandedSize) {
fputs("Wrong size of game file!\n", stderr);
return EXIT_FAILURE;
}
fseek(file, 0, SEEK_SET);
/* read data */
if (fread(srceP, sizeof(UInt8), expandedSize, file) != expandedSize) {
fputs("Read error on game file!\n", stderr);
return EXIT_FAILURE;
}
/* compress data */
if (!Compress(destP, srceP)) {
fputs("Compression error!\n", stderr);
return EXIT_FAILURE;
}
fclose(file);
}
/* write macros */
file = fopen(argv[2], "w");
if (file == NULL) {
fputs("Can't create destination file!\n", stderr);
return EXIT_FAILURE;
}
/* print header */
fprintf(file, "/* Automatically generated, don't edit! */\n\n");
/* generate macros */
offset = 0;
for (macro = 0; macro < maxCombo; macro++) {
int index;
fprintf(file, ".macro\txt_%02x\n"
"xt_%02x:\n", macro ^ 0x80, macro ^ 0x80);
for (index = 0; index < gbemuXtSize; index++) {
fprintf(file, "\t.byte\t0x%02x\n", destP[offset++]);
}
fprintf(file, "\t.endm\n"
"\n");
}
/* print footer */
fprintf(file, "/* The end */\n");
/* check for write errors */
if (ferror(file)) {
fputs("Write error on destination file!\n", stderr);
return EXIT_FAILURE;
}
/* all is well ;-) */
fclose(file);
return EXIT_SUCCESS;
}
#endif /* defined(__palmos__) else **************************************/
/* === Compress and expand a game ===================================== */
/**
* Compresses by run length encoding. The buffer sizes are fixed.
*
* @param destP pointer to the destination buffer.
* @param srceP pointer to the source buffer.
* @return true if successful.
*/
static Boolean Compress(UInt8 *destP, const UInt8 *srceP) {
UInt16 destCount, srceCount;
/* set flag */
*destP++ = compressedFlag;
destCount = 1;
srceCount = 0;
for (;;) {
UInt8 count, *currP, curr;
Boolean runFound;
/* remember start of entry */
currP = destP++;
if (++destCount == compressedSize) {
return false;
}
/* collect uncompressable data */
runFound = false;
for (count = 0; count < maxCount; count++) {
curr = *srceP;
*destP++ = *srceP++;
if (++destCount == compressedSize) {
return false;
}
if (++srceCount == expandedSize) {
*currP = ++count & ~compressedFlag;
*destP = endMarker;
return true;
}
if (srceCount < expandedSize - 1) {
if (curr == srceP[0] && curr == srceP[1]) {
runFound = true;
break;
}
}
}
/* if there are uncompressable data, store count
* then start a new entry
*/
if (count != 0) {
*currP = count & ~compressedFlag;
if (runFound) {
currP = destP - 1;
*destP++ = curr;
if (++destCount == compressedSize) {
return false;
}
}
}
/* if compressable data were found, find its end */
if (runFound) {
for (count = 1; count < maxCount && curr == *srceP; count++) {
srceP++;
if (++srceCount == expandedSize) {
*currP = ++count | compressedFlag;
*destP = endMarker;
return true;
}
}
*currP = count | compressedFlag;
}
}
}
#if defined(__palmos__) /************************************************/
/**
* Expands the run length encoded data. The buffer sizes are fixed.
*
* @param destP pointer to the destination buffer.
* @param srceP pointer to the source buffer.
* @return true if successful.
*/
static Boolean Expand(UInt8 *destP, const UInt8 *srceP) {
UInt16 destCount, srceCount;
/* check for compressed data */
if (*srceP++ != compressedFlag) {
return false;
}
srceCount = 1;
destCount = 0;
for (;;) {
UInt8 count;
if (srceCount++ == compressedSize) {
return false;
}
count = *srceP++;
if (count == endMarker) {
/* end of compressed data reached */
return destCount == expandedSize;
}
if ((count & compressedFlag) == 0) {
/* copy uncompressed data */
do {
if (destCount++ == expandedSize) {
return false;
}
if (srceCount++ == compressedSize) {
return false;
}
*destP++ = *srceP++;
} while (--count != 0);
} else {
/* expand compressed data */
UInt8 data;
if (srceCount++ == compressedSize) {
return false;
}
data = *srceP++;
count &= ~compressedFlag;
do {
if (destCount++ == expandedSize) {
return false;
}
*destP++ = data;
} while (--count != 0);
}
}
}
#endif /* defined(__palmos__) *******************************************/
/* === The end ======================================================== */