-
Notifications
You must be signed in to change notification settings - Fork 0
/
memory_c.c
599 lines (526 loc) · 17 KB
/
memory_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
/************************************************************************
* memory_c.c
* Memory emulation routines (C part)
*
************************************************************************
*
* 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: memory_c.c,v $
* Revision 1.14 2005/05/26 19:35:47 bodowenzel
* Moved game's info into game's entry, removing MemoryMbcInfo
*
* Revision 1.13 2005/04/03 14:08:34 bodowenzel
* Option to save the state for each game
*
* Revision 1.12 2005/01/29 10:26:08 bodowenzel
* Manager form uses a scrollable table now for the list of games
*
* Revision 1.11 2004/12/28 13:56:35 bodowenzel
* Split up all C-Code to multi-segmented
*
* Revision 1.10 2004/10/18 17:48:51 bodowenzel
* Dropped macro LITE, no need to maintain smaller code
*
* Revision 1.9 2004/04/13 19:44:22 bodowenzel
* Checking bank sizes now at start of game
*
* Revision 1.8 2004/04/05 21:56:55 bodowenzel
* VFS review and its consequences
*
* Revision 1.7 2004/03/02 19:24:16 bodowenzel
* Changed to new error function MiscShowError()
*
* Revision 1.6 2004/01/11 19:10:56 bodowenzel
* Added module 'ram' for on-board memory
* Start of VFS review and correction
*
* Revision 1.5 2003/04/27 09:36:18 bodowenzel
* Added Lite edition
*
* Revision 1.4 2002/10/19 08:08:11 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:10 bodowenzel
* Import
*
************************************************************************
* Pre-CVS History (developed as PalmBoy):
*
* 2001-09-30 Bodo Wenzel Saving states now possible
* 2001-07-21 Bodo Wenzel Separated from emumem.asm, changed to C
* 2001-02-24 Bodo Wenzel Support of more cartridges
* 2000-11-15 Bodo Wenzel Support of color devices
* 2000-10-19 Bodo Wenzel Interrupts and timer
* 2000-09-17 Bodo Wenzel Now nearly full screen emulation
* 2000-09-10 Bodo Wenzel Got from runmode.asm
************************************************************************
*/
/* === Includes ======================================================= */
#include <PalmOS.h>
#include "memory.h"
#include "Phoinix.h"
#include "prefs.h"
#include "states.h"
#include "ram.h"
#include "vfs.h"
#include "emulation.h"
#include "gbemu.h"
/* === Constants ====================================================== */
#define crcSeed (0xb0d0) /* setup for CRC checks */
#define crcBlockSize (0x1000)
/* === Type definitions =============================================== */
typedef struct {
Int16 code; /* byte in GB code */
MemoryMbcType mbc;
} CartridgeType;
typedef struct { /* pointer to functions */
void *ramEnable;
void *romSelectL;
void *romSelectH;
void *ramSelect;
void *modeSelect;
} MbcVectorsType;
/* === Global and static variables ==================================== */
static const CartridgeType CartridgesKnown[] = {
{ 0x00, memoryMbcDefault },
{ 0x01, memoryMbc1 },
{ 0x02, memoryMbc1 },
{ 0x03, memoryMbc1 },
{ 0x05, memoryMbc2 },
{ 0x06, memoryMbc2 },
{ 0x08, memoryMbcDefault },
{ 0x09, memoryMbcDefault },
{ 0x0f, memoryMbc3 },
{ 0x10, memoryMbc3 },
{ 0x11, memoryMbc3 },
{ 0x12, memoryMbc3 },
{ 0x13, memoryMbc3 },
{ 0x19, memoryMbc5 },
{ 0x1a, memoryMbc5 },
{ 0x1b, memoryMbc5 },
{ 0x1c, memoryMbc5 },
{ 0x1d, memoryMbc5 },
{ 0x1e, memoryMbc5 },
{ -1, memoryMbcUnknown }
};
static DmOpenRef GameDbP = NULL;
static MemHandle GameDbRecH[memoryMaxRomPages - 1];
/* NOTE:
* As GB addresses are used as offsets, the addresses for Palm memory
* blocks are offset by the base addresses.
*/
UInt8 *MemoryRomStart;
UInt8 *MemoryRomPages[memoryMaxRomPages];
static UInt16 RomPage;
UInt8 *MemoryRamPages[memoryMaxRamPages];
static UInt8 RamPage;
UInt8 *MemoryBaseRom0;
UInt8 *MemoryBaseRom1;
UInt8 *MemoryBaseRamVid;
UInt8 *MemoryBaseRamExt;
UInt8 *MemoryBaseRamInt;
UInt8 *MemoryBaseRamOam;
static UInt16 ramPageOffset;
UInt16 MemoryMbc1RomLsb;
UInt16 MemoryMbc1RomMsb;
UInt8 MemoryMbc1Mode;
UInt16 MemoryMbc3RtcDivider; /* dividing the frame rate */
MemoryMbc3RtcType MemoryMbc3RtcCurrent;
MemoryMbc3RtcType MemoryMbc3RtcLatched;
UInt8 MemoryMbc3Mode;
UInt8 *MemoryMbc3RtcPtr; /* pointer to current register */
static const MbcVectorsType DefaultVectors = {
MemoryMbcDummy,
MemoryMbcDummy,
MemoryMbcDummy,
MemoryMbcDummy,
MemoryMbcDummy };
static const MbcVectorsType Mbc1VectorsMode168 = {
MemoryMbcDummy,
MemoryMbc1RomSelectL,
MemoryMbc1RomSelectL,
MemoryMbc1RomSelectH,
MemoryMbc1ModeSelect };
static const MbcVectorsType Mbc1VectorsMode432 = {
MemoryMbcDummy,
MemoryMbc1RomSelect,
MemoryMbc1RomSelect,
MemoryMbc1RamSelect,
MemoryMbc1ModeSelect };
static const MbcVectorsType Mbc2Vectors = {
MemoryMbcDummy,
MemoryMbc2RomSelect,
MemoryMbc2RomSelect,
MemoryMbcDummy,
MemoryMbcDummy };
static const MbcVectorsType Mbc3Vectors = {
MemoryMbcDummy,
MemoryMbc3RomSelect,
MemoryMbc3RomSelect,
MemoryMbc3RamSelect,
MemoryMbc3RtcLatch };
static const MbcVectorsType Mbc5Vectors = {
MemoryMbcDummy,
MemoryMbc5RomSelect,
MemoryMbcDummy,
MemoryMbc5RamSelect,
MemoryMbcDummy };
/* === Function prototypes ============================================ */
static void SetupState(MemoryStateType *stateP)
MEMORY_SECTION;
/* === Retrieve MBC information ======================================= */
/**
* Retrieves infos of the current game.
*
* @return true if successful.
*/
Boolean MemoryRetrieveMbcInfo(void) {
MemHandle recH;
UInt8 *recP;
UInt16 crc1, crc2, i;
/* set default value to show error */
PrefsPreferences.game.mbc = memoryMbcError;
/* obtain first record */
if (PrefsPreferences.game.volIndex == vfsRamVolIndex) {
recH = RamGetFirstBlock(&PrefsPreferences.game);
} else {
recH = VfsGetFirstBlock(&PrefsPreferences.game);
}
if (recH == NULL) {
return false;
}
/* check size of first record */
if (MemHandleSize(recH) != 32 * 1024L) {
MemHandleFree(recH);
return false;
}
recP = MemHandleLock(recH);
/* calculate CRCs */
crc1 = Crc16CalcBlock(recP + 0 , crcBlockSize, crcSeed);
crc2 = Crc16CalcBlock(recP + crcBlockSize, crcBlockSize, crcSeed);
PrefsPreferences.game.crc = ((UInt32)crc1 << 16) | crc2;
/* calculate ROM size */
PrefsPreferences.game.romSize = 32 << (recP[0x148] & 0x0f);
if ((recP[0x148] & 0xf0) != 0) {
PrefsPreferences.game.romSize += 32 << (recP[0x148] >> 4);
}
/* scan the list of known MBC types */
for (i = 0; CartridgesKnown[i].code != -1; i++) {
if (recP[0x147] == CartridgesKnown[i].code) {
break;
}
}
PrefsPreferences.game.mbc = CartridgesKnown[i].mbc;
/* calculate RAM size (in bytes, needed for the RAM record!) */
if (PrefsPreferences.game.mbc == memoryMbc2) {
PrefsPreferences.game.ramSize = 512; /* only 4 bits used */
} else if (recP[0x149] == 0) {
PrefsPreferences.game.ramSize = 0;
} else if (recP[0x149] <= 3) {
PrefsPreferences.game.ramSize = 512 << (recP[0x149] << 1);
} else {
PrefsPreferences.game.mbc = memoryMbcError;
}
/* release everything */
MemHandleUnlock(recH);
MemHandleFree(recH);
return PrefsPreferences.game.mbc > memoryMbcUnknown;
}
/* === Setup, cleanup, reset, load and save =========================== */
/**
* Sets up all memory stuff for the emulation.
*
* @return true if successful.
*/
Boolean MemorySetup(void) {
UInt16 numRecs, i, romPages;
if (PrefsPreferences.game.volIndex == vfsRamVolIndex) {
GameDbP = RamGameDbOpen(&PrefsPreferences.game);
} else {
GameDbP = VfsGameDbOpen(&PrefsPreferences.game);
}
if (GameDbP == NULL) {
return false;
}
for (i = 0; i < memoryMaxRomPages - 1; i++) {
GameDbRecH[i] = NULL;
}
numRecs = DmNumRecords(GameDbP);
/* check number of records and sizes of other record but the first */
if (numRecs > memoryMaxRomPages -1 ||
numRecs != (PrefsPreferences.game.romSize / 16) - 1) {
return false;
}
for (i = 1; i < numRecs; i++) {
MemHandle recH;
recH = DmQueryRecord(GameDbP, i);
if (recH == NULL) {
return false;
}
if (MemHandleSize(recH) != 16 * 1024L) {
return false;
}
}
/* all OK, now get all records */
for (i = 0; i < numRecs; i++) {
GameDbRecH[i] = DmQueryRecord(GameDbP, i);
}
/* setup ROM pages, locking all records */
romPages = numRecs + 1;
MemoryRomStart = MemHandleLock(GameDbRecH[0]);
MemoryRomPages[0] = MemoryRomStart + 0x0000 - 0x4000;
MemoryRomPages[1] = MemoryRomStart + 0x4000 - 0x4000;
for (i = 2; i < romPages; i++) {
MemoryRomPages[i] = MemHandleLock(GameDbRecH[i - 1]) - 0x4000;
}
/* most MBCs have a paging "error" */
switch (PrefsPreferences.game.mbc) {
case memoryMbc1:
for (i = 0; i < romPages; i += 32) {
MemoryRomPages[i] = MemoryRomPages[i + 1];
}
break;
case memoryMbc5:
break;
default:
MemoryRomPages[0] = MemoryRomPages[1];
break;
}
for (i = romPages; i < memoryMaxRomPages; i++) {
MemoryRomPages[i] = MemoryRomPages[i - romPages];
}
MemSemaphoreReserve(true);
/* setup RAM access vectors */
if (PrefsPreferences.game.ramSize == 0) {
EmulationEmulator[gbemuJmpRamRdExt].xt.vector.address =
MemoryRamRdExtNone;
EmulationEmulator[gbemuJmpRamWrExt].xt.vector.address =
MemoryRamWrExtNone;
ramPageOffset = 0x0000;
} else if (PrefsPreferences.game.ramSize <= 512) {
EmulationEmulator[gbemuJmpRamRdExt].xt.vector.address =
MemoryRamRdExtMbc2;
EmulationEmulator[gbemuJmpRamWrExt].xt.vector.address =
MemoryRamWrExtMbc2;
ramPageOffset = 0x0000;
} else if (PrefsPreferences.game.ramSize <= 2 * 1024) {
EmulationEmulator[gbemuJmpRamRdExt].xt.vector.address =
MemoryRamRdExt2K;
EmulationEmulator[gbemuJmpRamWrExt].xt.vector.address =
MemoryRamWrExt2K;
ramPageOffset = 0x0000;
} else if (PrefsPreferences.game.ramSize <= 8 * 1024) {
EmulationEmulator[gbemuJmpRamRdExt].xt.vector.address =
MemoryRamRdExt8K;
EmulationEmulator[gbemuJmpRamWrExt].xt.vector.address =
MemoryRamWrExt8K;
ramPageOffset = 0x0000;
} else {
EmulationEmulator[gbemuJmpRamRdExt].xt.vector.address =
MemoryRamRdExt8K;
EmulationEmulator[gbemuJmpRamWrExt].xt.vector.address =
MemoryRamWrExt8K;
ramPageOffset = 0x2000;
}
/* this is for MBC3 which accesses its RTC via RAM adresses */
EmulationEmulator[gbemuJmpRamRdExt].xt.vector.buffer =
EmulationEmulator[gbemuJmpRamRdExt].xt.vector.address;
EmulationEmulator[gbemuJmpRamWrExt].xt.vector.buffer =
EmulationEmulator[gbemuJmpRamWrExt].xt.vector.address;
MemSemaphoreRelease(true);
return true;
}
/**
* Cleans up all memory stuff after emulation.
*/
void MemoryCleanup(void) {
UInt16 i;
/* if game is not open, do nothing */
if (GameDbP == NULL) {
return;
}
/* unlock all records */
for (i = 0; i < memoryMaxRomPages - 1; i++) {
if (GameDbRecH[i] != NULL) {
MemHandleUnlock(GameDbRecH[i]);
}
}
if (PrefsPreferences.game.volIndex == vfsRamVolIndex) {
RamGameDbClose(GameDbP);
} else {
VfsGameDbClose(GameDbP);
}
GameDbP = NULL;
}
/**
* Resets all saved memory stuff.
*
* @param recP pointer to the record.
* @param offset offset into the record.
*/
void MemoryResetState(void *recP, UInt32 offset) {
UInt32 seconds;
DateType date;
DateTimeType dateTime;
UInt32 days;
MemoryMbcStateType mbcState = { 0 };
/* get time from OS */
seconds = TimGetSeconds();
DateSecondsToDate(seconds, &date);
days = DateToDays(date);
TimSecondsToDateTime(seconds, &dateTime);
mbcState.mbc3RtcCurrent.control = (days & 0x100) != 0 ? 0x01 : 0x00;
mbcState.mbc3RtcCurrent.days = (UInt8)days;
mbcState.mbc3RtcCurrent.hours = (UInt8)dateTime.hour;
mbcState.mbc3RtcCurrent.minutes = (UInt8)dateTime.minute;
mbcState.mbc3RtcCurrent.seconds = (UInt8)dateTime.second;
DmWrite(recP, offset + OffsetOf(MemoryStateType, mbcState),
&mbcState, sizeof(MemoryMbcStateType));
}
/**
* Sets up all memory stuff to the given state.
*
* @param stateP pointer to the memory state structure.
*/
void MemoryLoadState(MemoryStateType *stateP) {
RomPage = stateP->mbcState.romPage;
RamPage = stateP->mbcState.ramPage;
MemoryMbc1RomLsb = stateP->mbcState.mbc1RomLsb;
MemoryMbc1RomMsb = stateP->mbcState.mbc1RomMsb;
MemoryMbc1Mode = stateP->mbcState.mbc1Mode;
MemoryMbc3Mode = stateP->mbcState.mbc3Mode;
MemoryMbc3RtcDivider = stateP->mbcState.mbc3RtcDivider;
MemoryMbc3RtcCurrent = stateP->mbcState.mbc3RtcCurrent;
MemoryMbc3RtcLatched = stateP->mbcState.mbc3RtcLatched;
SetupState(stateP);
}
/**
* Saves the memory state in the given record.
*
* @param recP pointer to the record.
* @param offset offset into the record.
*/
void MemorySaveState(void *recP, UInt32 offset) {
MemoryMbcStateType mbcState;
/* look for current ROM page */
for (mbcState.romPage = 0;
mbcState.romPage < memoryMaxRomPages;
mbcState.romPage++) {
if (MemoryRomPages[mbcState.romPage] == MemoryBaseRom1) {
break;
}
}
mbcState.romPage %= memoryMaxRomPages;
/* look for current RAM page */
for (mbcState.ramPage = 0;
mbcState.ramPage < memoryMaxRamPages;
mbcState.ramPage++) {
if (MemoryRamPages[mbcState.ramPage] == MemoryBaseRamExt) {
break;
}
}
mbcState.ramPage %= memoryMaxRamPages;
mbcState.mbc1RomLsb = MemoryMbc1RomLsb;
mbcState.mbc1RomMsb = MemoryMbc1RomMsb;
mbcState.mbc1Mode = MemoryMbc1Mode;
mbcState.mbc3Mode = MemoryMbc3Mode;
mbcState.mbc3RtcDivider = MemoryMbc3RtcDivider;
mbcState.mbc3RtcCurrent = MemoryMbc3RtcCurrent;
mbcState.mbc3RtcLatched = MemoryMbc3RtcLatched;
DmWrite(recP, offset + OffsetOf(MemoryStateType, mbcState),
&mbcState, sizeof(MemoryMbcStateType));
}
/**
* Sets up common memory stuff.
*
* @param stateP pointer to the memory state structure.
*/
static void SetupState(MemoryStateType *stateP) {
UInt16 i;
const MbcVectorsType *mbcVectorsP;
/* setup pointer to the emulated memories */
MemoryBaseRamVid = stateP->ram.vid8000 + (0x10000 - 0x8000);
MemoryBaseRamInt = stateP->ram.intC000 + (0x10000 - 0xc000);
MemoryBaseRamOam = stateP->ram.oamFe00 + (0x10000 - 0xfe00);
for (i = 0; i < memoryMaxRamPages; i++) {
MemoryRamPages[i] = StatesCartridgeRamPtr + i * ramPageOffset +
(0x10000 - 0xa000);
}
/* restore pointer to current ROM and RAM page */
MemoryBaseRom1 = MemoryRomPages[RomPage];
MemoryBaseRamExt = MemoryRamPages[RamPage];
MemSemaphoreReserve(true);
/* setup MBC vectors */
switch (PrefsPreferences.game.mbc) {
case memoryMbc1:
if ((MemoryMbc1Mode & 0x01) == 0) {
mbcVectorsP = &Mbc1VectorsMode168;
} else {
mbcVectorsP = &Mbc1VectorsMode432;
}
break;
case memoryMbc2:
mbcVectorsP = &Mbc2Vectors;
break;
case memoryMbc3:
if ((MemoryMbc3Mode & 0x08) == 0) {
EmulationEmulator[gbemuJmpRamRdExt].xt.vector.address =
EmulationEmulator[gbemuJmpRamRdExt].xt.vector.buffer;
EmulationEmulator[gbemuJmpRamWrExt].xt.vector.address =
EmulationEmulator[gbemuJmpRamWrExt].xt.vector.buffer;
} else {
EmulationEmulator[gbemuJmpRamRdExt].xt.vector.address =
MemoryMbc3RtcRd;
EmulationEmulator[gbemuJmpRamWrExt].xt.vector.address =
MemoryMbc3RtcWr;
MemoryMbc3RtcPtr = (UInt8 *)&MemoryMbc3RtcLatched +
(MemoryMbc3Mode & 0x07);
}
mbcVectorsP = &Mbc3Vectors;
break;
case memoryMbc5:
mbcVectorsP = &Mbc5Vectors;
break;
default:
mbcVectorsP = &DefaultVectors;
break;
}
EmulationEmulator[gbemuJmpRamEnable ].xt.vector.address =
mbcVectorsP->ramEnable;
EmulationEmulator[gbemuJmpRomSelectL].xt.vector.address =
mbcVectorsP->romSelectL;
EmulationEmulator[gbemuJmpRomSelectH].xt.vector.address =
mbcVectorsP->romSelectH;
EmulationEmulator[gbemuJmpRamSelect ].xt.vector.address =
mbcVectorsP->ramSelect;
EmulationEmulator[gbemuJmpModeSelect].xt.vector.address =
mbcVectorsP->modeSelect;
MemSemaphoreRelease(true);
}
/* === The end ======================================================== */