-
Notifications
You must be signed in to change notification settings - Fork 0
/
memory.h
463 lines (396 loc) · 12.7 KB
/
memory.h
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
//---------------------------------------------------------------------------
// 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. See also the license.txt file for
// additional informations.
//---------------------------------------------------------------------------
#ifndef _MEMORYH_
#define _MEMORYH_
#ifndef __GP32__
#include "StdAfx.h"
#endif
#include "neopopsound.h"
#include "sound.h"
#include "input.h"
#include "graphics.h"
#include "main.h"
#include "flash.h"
#ifdef DRZ80
#include "DrZ80_support.h"
#else
#ifdef CZ80
#include "cz80_support.h"
#else
#include "z80.h"
#endif
#endif
extern unsigned char mainram[]; // All RAM areas
extern unsigned char mainrom[]; // ROM image area
extern unsigned char cpurom[]; // Bios ROM image area
//extern unsigned long cartAddrMask; // Mask for reading/writing mainrom
/// tlcs 900h memory
//extern unsigned char cpuram[]; // 900h cpu core needs this..
extern unsigned char *cpuram;
unsigned char tlcsMemReadB(unsigned long addr);
unsigned short tlcsMemReadW(unsigned long addr);
unsigned long tlcsMemReadL(unsigned long addr);
void tlcsMemWriteB(unsigned long addr, unsigned char data);
void tlcsMemWriteW(unsigned long addr, unsigned short data);
void tlcsMemWriteL(unsigned long addr, unsigned long data);
void mem_init();
void mem_dump_ram(FILE *output);
//unsigned char *get_address(unsigned long addr);
//unsigned char *get_addressW(unsigned long addr);
/// gameboy memory
extern unsigned char (*gbMemReadB)(unsigned short addr);
extern void (*gbMemWriteB)(unsigned short addr, unsigned char data);
unsigned short gbMemReadW(unsigned short addr);
void gbMemWriteW(unsigned short addr, unsigned short data);
// z80 memory functions
extern unsigned char (*z80MemReadB)(unsigned short addr);
extern unsigned short (*z80MemReadW)(unsigned short addr);
extern void (*z80MemWriteB)(unsigned short addr, unsigned char data);
extern void (*z80MemWriteW)(unsigned short addr, unsigned short data);
extern void (*z80PortWriteB)(unsigned char port, unsigned char data);
extern unsigned char (*z80PortReadB)(unsigned char port);
#if defined(DRZ80) || defined(CZ80)
unsigned char z80ngpMemReadB(unsigned short addr);
unsigned short z80ngpMemReadW(unsigned short addr);
void DrZ80ngpMemWriteB(unsigned char data, unsigned short addr);
void DrZ80ngpMemWriteW(unsigned short data, unsigned short addr);
void DrZ80ngpPortWriteB(unsigned short port, unsigned char data);
unsigned char DrZ80ngpPortReadB(unsigned short port);
#endif
// used internally by z80 emulation for emulation of the memory
// map of the Game Gear
extern unsigned char *ggVRAM;
extern unsigned char *ggCRAM;
//
//
unsigned char z80ggMemReadB(unsigned short addr);
unsigned short z80ggMemReadW(unsigned short addr);
void z80ggMemWriteB(unsigned short addr, unsigned char data);
void z80ggMemWriteW(unsigned short addr, unsigned short data);
void z80ggPortWriteB(unsigned char port, unsigned char data);
unsigned char z80ggPortReadB(unsigned char port);
// definitions for wdc6502 emulation
extern unsigned char (*w65MemReadB)(unsigned short addr);
//extern unsigned short (*w65MemReadW)(unsigned short addr);
unsigned short w65MemReadW(unsigned short addr);
extern void (*w65MemWriteB)(unsigned short addr, unsigned char data);
//extern void (*w65MemWriteW)(unsigned short addr, unsigned short data);
void w65MemWriteW(unsigned short addr, unsigned short data);
extern unsigned char *w65GetAddress(unsigned short addr);
//
// Supervision specific
//
extern unsigned char *svVRAM;
//
// HuC specific defines & externs
//
extern unsigned char hucMMR[8];
//
// i8086 functions
//
extern unsigned char *x86Ports;
unsigned char x86MemReadB(unsigned int addr);
void x86MemWriteB(unsigned int addr, unsigned char data);
unsigned short x86MemReadW(unsigned int addr);
void x86MemWriteW(unsigned int addr, unsigned short data);
unsigned char x86PortReadB(unsigned short port);
void x86PortWriteB(unsigned short port, unsigned char data);
unsigned short x86PortReadW(unsigned short port);
void x86PortWriteW(unsigned short port, unsigned short data);
// used for Wonderswan graphics emulation
extern unsigned char *wsTileMap0;
extern unsigned char *wsTileMap1;
extern unsigned char *wsPatterns;
extern unsigned char *wsSpriteTable;
//
// i8048 functions
//
extern unsigned char *advBios;
extern unsigned char *advVidRAM;
unsigned char i8048ROMReadB(unsigned int addr);
unsigned char i8048ExtReadB(unsigned int addr);
void i8048ExtWriteB(unsigned int addr, unsigned char data);
unsigned char i8048PortReadB(unsigned char port);
void i8048PortWriteB(unsigned char port, unsigned char data);
void i8048SetT1();
unsigned char i8048GetT1();
//
// sm85xx memory functions
//
extern unsigned char (*sm85MemReadB)(unsigned short addr);
extern void (*sm85MemWriteB)(unsigned short addr, unsigned char data);
extern unsigned char realBIOSloaded;
inline unsigned char *get_address(unsigned long addr)
{
addr&= 0x00FFFFFF;
if (addr<0x00200000)
{
if (addr<0x000008a0)
{
//if(((unsigned long)&cpuram[addr] >> 24) == 0xFF)
// dbg_print("1) addr=0x%X returning=0x%X\n", addr, &cpuram[addr]);
return &cpuram[addr];
}
if (addr>0x00003fff && addr<0x00018000)
{
//if((unsigned long)&mainram[addr-0x00004000] >> 24 == 0xFF)
// dbg_print("2) addr=0x%X returning=0x%X\n", addr, &mainram[addr-0x00004000]);
switch (addr) //Thanks Koyote
{
case 0x6F80:
mainram[addr-0x00004000] = 0xFF;
break;
case 0x6F80+1:
mainram[addr-0x00004000] = 0x03;
break;
case 0x6F85:
mainram[addr-0x00004000] = 0x00;
break;
case 0x6F82:
mainram[addr-0x00004000] = ngpInputState;
break;
case 0x6DA2:
mainram[addr-0x00004000] = 0x80;
break;
}
return &mainram[addr-0x00004000];
}
}
else
{
if (addr<0x00400000)
{
return &mainrom[(addr-0x00200000)/*&cartAddrMask*/];
}
if(addr<0x00800000) //Flavor added
{
return 0;
}
if (addr<0x00A00000)
{
return &mainrom[(addr-(0x00800000-0x00200000))/*&cartAddrMask*/];
}
if(addr<0x00FF0000) //Flavor added
{
return 0;
}
//if((unsigned long)&cpurom[addr-0x00ff0000] >> 24 == 0xFF)
// dbg_print("5) addr=0x%X returning=0x%X\n", addr, &cpurom[addr-0x00ff0000]);
return &cpurom[addr-0x00ff0000];
}
//dbg_print("6) addr=0x%X returning=0\n", addr);
return 0; //Flavor ERROR
}
/*inline unsigned char *get_addressW(unsigned long addr)
{
addr&= 0x00FFFFFF;
if (addr<0x00200000)
{
if (addr<0x000008a0)
{
return &cpuram[addr];
}
if (addr>0x00003fff && addr<0x00018000)
return &mainram[addr-0x00004000];
return NULL;
}
else
{
return NULL;
}
return NULL; //Flavor ERROR
}*/
// read a byte from a memory address (addr)
inline unsigned char tlcsMemReadB(unsigned long addr)
{
addr&= 0x00FFFFFF;
if(currentCommand == COMMAND_INFO_READ)
return flashReadInfo(addr);
if (addr < 0x00200000) {
if (addr < 0x000008A0) {
if(addr == 0xBC)
{
ngpSoundExecute();
}
return cpuram[addr];
}
else if (addr > 0x00003FFF && addr < 0x00018000)
{
switch (addr) //Thanks Koyote
{
case 0x6DA2:
return 0x80;
break;
case 0x6F80:
return 0xFF;
break;
case 0x6F80+1:
return 0x03;
break;
case 0x6F85:
return 0x00;
break;
case 0x6F82:
return ngpInputState;
break;
default:
return mainram[addr-0x00004000];
}
return mainram[addr-0x00004000];
}
}
else
{
if (addr<0x00400000)
return mainrom[(addr-0x00200000)/*&cartAddrMask*/];
if (addr<0x00800000)
return 0xFF;
if (addr<0x00a00000)
return mainrom[(addr-(0x00800000-0x00200000))/*&cartAddrMask*/];
if (addr<0x00ff0000)
return 0xFF;
return cpurom[addr-0x00ff0000];
}
return 0xFF;
}
// read a word from a memory address (addr)
inline unsigned short tlcsMemReadW(unsigned long addr)
{
#ifdef TARGET_GP2X
register unsigned short i asm("r0");
register byte *gA asm("r1");
gA = get_address(addr);
if(gA == 0)
return 0;
asm volatile(
"ldrb %0, [%1]\n\t"
"ldrb r2, [%1, #1]\n\t"
"orr %0, %0, r2, asl #8"
: "=r" (i)
: "r" (gA)
: "r2");
return i;
#else
/* unsigned short i;
byte *gA = get_address(addr);
if(gA == 0)
return 0;
i = *(gA++);
i |= (*gA) << 8;
return i;
*/
return tlcsMemReadB(addr) | (tlcsMemReadB(addr+1) << 8);
#endif
}
// read a long word from a memory address (addr)
inline unsigned long tlcsMemReadL(unsigned long addr)
{
#ifdef TARGET_GP2X
register unsigned long i asm("r0");
register byte *gA asm("r4");
gA = get_address(addr);
if(gA == 0)
return 0;
asm volatile(
"bic r1,%1,#3 \n"
"ldmia r1,{r0,r3} \n"
"ands r1,%1,#3 \n"
"movne r2,r1,lsl #3 \n"
"movne r0,r0,lsr r2 \n"
"rsbne r1,r2,#32 \n"
"orrne r0,r0,r3,lsl r1"
: "=r"(i)
: "r"(gA)
: "r1","r2","r3");
return i;
#else
unsigned long i;
byte *gA = get_address(addr);
if(gA == 0)
return 0;
i = *(gA++);
i |= (*(gA++)) << 8;
i |= (*(gA++)) << 16;
i |= (*gA) << 24;
return i;
//return tlcsMemReadB(addr) | (tlcsMemReadB(addr +1) << 8) | (tlcsMemReadB(addr +2) << 16) | (tlcsMemReadB(addr +3) << 24);
#endif
}
// write a byte (data) to a memory address (addr)
inline void tlcsMemWriteB(unsigned long addr, unsigned char data)
{
addr&= 0x00FFFFFF;
if (addr<0x000008a0)
{
switch(addr) {
//case 0x80: // CPU speed
// break;
case 0xA0: // L CH Sound Source Control Register
if (cpuram[0xB8] == 0x55 && cpuram[0xB9] == 0xAA)
Write_SoundChipNoise(data);//Flavor SN76496Write(0, data);
break;
case 0xA1: // R CH Sound Source Control Register
if (cpuram[0xB8] == 0x55 && cpuram[0xB9] == 0xAA)
Write_SoundChipTone(data); //Flavor SN76496Write(0, data);
break;
case 0xA2: // L CH DAC Control Register
ngpSoundExecute();
if (cpuram[0xB8] == 0xAA)
dac_writeL(data); //Flavor DAC_data_w(0,data);
break;
/*case 0xA3: // R CH DAC Control Register //Flavor hack for mono only sound
ngpSoundExecute();
if (cpuram[0xB8] == 0xAA)
dac_writeR(data);//Flavor DAC_data_w(1,data);
break;*/
case 0xB8: // Z80 Reset
// if (data == 0x55) DAC_data_w(0,0);
case 0xB9: // Sourd Source Reset Control Register
switch(data) {
case 0x55:
ngpSoundStart();
break;
case 0xAA:
ngpSoundExecute();
ngpSoundOff();
break;
}
break;
case 0xBA:
ngpSoundExecute();
#if defined(DRZ80) || defined(CZ80)
Z80_Cause_Interrupt(Z80_NMI_INT);
#else
z80Interrupt(Z80NMI);
#endif
break;
}
cpuram[addr] = data;
return;
}
else if (addr>0x00003fff && addr<0x00018000)
{
if (addr == 0x87E2 && mainram[0x47F0] != 0xAA)
return; // disallow writes to GEMODE
/*if((addr >= 0x8800 && addr <= 0x88FF)
|| (addr >= 0x8C00 && addr <= 0x8FFF)
|| (addr >= 0xA000 && addr <= 0xBFFF)
|| addr == 0x00008020
|| addr == 0x00008021)
{
spritesDirty = true;
}*/
mainram[addr-0x00004000] = data;
return;
}
else if (addr>=0x00200000 && addr<0x00400000)
flashChipWrite(addr, data);
else if (addr>=0x00800000 && addr<0x00A00000)
flashChipWrite(addr, data);
}
#endif // _MEMORYH_