c62d2810 |
1 | /* FCE Ultra - NES/Famicom Emulator |
2 | * |
3 | * Copyright notice for this file: |
4 | * Copyright (C) 2002 Ben Parnell |
5 | * |
6 | * This program is free software; you can redistribute it and/or modify |
7 | * it under the terms of the GNU General Public License as published by |
8 | * the Free Software Foundation; either version 2 of the License, or |
9 | * (at your option) any later version. |
10 | * |
11 | * This program is distributed in the hope that it will be useful, |
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14 | * GNU General Public License for more details. |
15 | * |
16 | * You should have received a copy of the GNU General Public License |
17 | * along with this program; if not, write to the Free Software |
18 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
19 | */ |
20 | |
21 | #include <string.h> |
22 | #include <stdlib.h> |
23 | #include <stdio.h> /* For GG loading code. */ |
24 | |
25 | #include "types.h" |
26 | #include "version.h" |
27 | #include "fce.h" |
28 | #include "cart.h" |
29 | #include "memory.h" |
30 | |
31 | #include "general.h" |
32 | #include "svga.h" |
33 | |
937bf65b |
34 | /* |
c62d2810 |
35 | This file contains all code for coordinating the mapping in of the |
36 | address space external to the NES. |
37 | It's also (ab)used by the NSF code. |
38 | */ |
39 | |
40 | uint8 *Page[32],*VPage[8]; |
41 | uint8 **VPageR=VPage; |
42 | uint8 *VPageG[8]; |
43 | uint8 *MMC5SPRVPage[8]; |
44 | uint8 *MMC5BGVPage[8]; |
45 | |
46 | /* 16 are (sort of) reserved for UNIF/iNES and 16 to map other stuff. */ |
47 | |
48 | static int CHRram[32]; |
49 | static int PRGram[32]; |
50 | |
51 | uint8 *PRGptr[32]; |
52 | uint8 *CHRptr[32]; |
53 | |
54 | uint32 PRGsize[32]; |
55 | uint32 CHRsize[32]; |
56 | |
57 | uint32 PRGmask2[32]; |
58 | uint32 PRGmask4[32]; |
59 | uint32 PRGmask8[32]; |
60 | uint32 PRGmask16[32]; |
61 | uint32 PRGmask32[32]; |
62 | |
63 | uint32 CHRmask1[32]; |
64 | uint32 CHRmask2[32]; |
65 | uint32 CHRmask4[32]; |
66 | uint32 CHRmask8[32]; |
67 | |
68 | int geniestage=0; |
69 | |
70 | int modcon; |
71 | |
72 | uint8 genieval[3]; |
73 | uint8 geniech[3]; |
74 | |
75 | uint32 genieaddr[3]; |
76 | |
77 | static INLINE void setpageptr(int s, uint32 A, uint8 *p) |
78 | { |
79 | uint32 AB=A>>11; |
80 | int x; |
81 | |
82 | for(x=(s>>1)-1;x>=0;x--) |
83 | Page[AB+x]=p-A; |
84 | } |
85 | |
86 | static char nothing[8192]; |
87 | void ResetCartMapping(void) |
88 | { |
89 | int x; |
90 | |
91 | for(x=0;x<32;x++) |
92 | { |
5232c20c |
93 | Page[x]=(uint8 *)(nothing-x*2048); |
c62d2810 |
94 | PRGptr[x]=CHRptr[x]=0; |
95 | PRGsize[x]=CHRsize[x]=0; |
96 | } |
97 | for(x=0;x<8;x++) |
98 | { |
5232c20c |
99 | MMC5SPRVPage[x]=MMC5BGVPage[x]=VPageR[x]=(uint8 *)(nothing-0x400*x); |
c62d2810 |
100 | } |
101 | |
102 | } |
103 | |
104 | void SetupCartPRGMapping(int chip, uint8 *p, uint32 size, int ram) |
105 | { |
106 | PRGptr[chip]=p; |
107 | PRGsize[chip]=size; |
108 | |
109 | PRGmask2[chip]=(size>>11)-1; |
110 | PRGmask4[chip]=(size>>12)-1; |
111 | PRGmask8[chip]=(size>>13)-1; |
112 | PRGmask16[chip]=(size>>14)-1; |
937bf65b |
113 | PRGmask32[chip]=(size>>15)-1; |
c62d2810 |
114 | |
115 | PRGram[chip]=ram?1:0; |
116 | } |
117 | |
118 | void SetupCartCHRMapping(int chip, uint8 *p, uint32 size, int ram) |
119 | { |
120 | CHRptr[chip]=p; |
121 | CHRsize[chip]=size; |
122 | |
123 | CHRmask1[chip]=(size>>10)-1; |
124 | CHRmask2[chip]=(size>>11)-1; |
125 | CHRmask4[chip]=(size>>12)-1; |
126 | CHRmask8[chip]=(size>>13)-1; |
127 | |
128 | CHRram[chip]=ram; |
129 | } |
130 | |
131 | DECLFR(CartBR) |
132 | { |
133 | return Page[A>>11][A]; |
134 | } |
135 | |
136 | void FASTAPASS(3) GINLINE setprg2r(int r, unsigned int A, unsigned int V) |
137 | { |
138 | if(!PRGptr[r]) return; |
139 | V&=PRGmask2[r]; |
140 | |
141 | setpageptr(2,A,(&PRGptr[r][V<<11])); |
142 | } |
143 | |
144 | void FASTAPASS(2) setprg2(uint32 A, uint32 V) |
145 | { |
146 | setprg2r(0,A,V); |
147 | } |
148 | |
149 | void FASTAPASS(3) GINLINE setprg4r(int r, unsigned int A, unsigned int V) |
150 | { |
151 | if(!PRGptr[r]) return; |
152 | V&=PRGmask4[r]; |
153 | setpageptr(4,A,(&PRGptr[r][V<<12])); |
154 | } |
155 | |
156 | void FASTAPASS(2) setprg4(uint32 A, uint32 V) |
157 | { |
158 | setprg4r(0,A,V); |
159 | } |
160 | |
161 | void FASTAPASS(3) GINLINE setprg8r(int r, unsigned int A, unsigned int V) |
162 | { |
163 | if(!PRGptr[r]) return; |
164 | |
165 | if(PRGsize[r]>=8192) |
166 | { |
167 | V&=PRGmask8[r]; |
168 | setpageptr(8,A,(&PRGptr[r][V<<13])); |
169 | } |
170 | else |
171 | { |
172 | uint32 VA=V<<2; |
173 | int x; |
174 | for(x=0;x<4;x++) |
175 | setpageptr(2,A+(x<<11),(&PRGptr[r][((VA+x)&PRGmask2[r])<<11])); |
176 | } |
177 | } |
178 | |
179 | void FASTAPASS(2) setprg8(uint32 A, uint32 V) |
180 | { |
181 | setprg8r(0,A,V); |
182 | } |
183 | |
184 | void FASTAPASS(3) GINLINE setprg16r(int r, unsigned int A, unsigned int V) |
185 | { |
186 | if(!PRGptr[r]) return; |
187 | |
188 | if(PRGsize[r]>=16384) |
189 | { |
190 | V&=PRGmask16[r]; |
191 | setpageptr(16,A,(&PRGptr[r][V<<14])); |
192 | } |
193 | else |
194 | { |
195 | uint32 VA=V<<3; |
196 | int x; |
197 | |
198 | for(x=0;x<8;x++) |
199 | setpageptr(2,A+(x<<11),(&PRGptr[r][((VA+x)&PRGmask2[r])<<11])); |
200 | } |
201 | } |
202 | |
203 | void FASTAPASS(2) setprg16(uint32 A, uint32 V) |
204 | { |
205 | setprg16r(0,A,V); |
206 | } |
207 | |
208 | void FASTAPASS(3) GINLINE setprg32r(int r,unsigned int A, unsigned int V) |
209 | { |
210 | if(!PRGptr[r]) return; |
211 | if(PRGsize[r]>=32768) |
212 | { |
213 | V&=PRGmask32[r]; |
214 | setpageptr(32,A,(&PRGptr[r][V<<15])); |
215 | } |
216 | else |
217 | { |
218 | uint32 VA=V<<4; |
219 | int x; |
220 | |
221 | for(x=0;x<16;x++) |
222 | setpageptr(2,A+(x<<11),(&PRGptr[r][((VA+x)&PRGmask2[r])<<11])); |
223 | } |
224 | } |
225 | |
226 | void FASTAPASS(2) setprg32(uint32 A, uint32 V) |
227 | { |
228 | setprg32r(0,A,V); |
229 | } |
230 | |
231 | void GINLINE FASTAPASS(3) setchr1r(int r, unsigned int A, unsigned int V) |
232 | { |
233 | if(!CHRptr[r]) return; |
234 | V&=CHRmask1[r]; |
235 | if(CHRram[r]) |
236 | PPUCHRRAM|=(1<<(A>>10)); |
237 | else |
238 | PPUCHRRAM&=~(1<<(A>>10)); |
239 | VPageR[(A)>>10]=&CHRptr[r][(V)<<10]-(A); |
240 | } |
241 | |
242 | void GINLINE FASTAPASS(3) setchr2r(int r, unsigned int A, unsigned int V) |
243 | { |
244 | if(!CHRptr[r]) return; |
245 | V&=CHRmask2[r]; |
246 | VPageR[(A)>>10]=VPageR[((A)>>10)+1]=&CHRptr[r][(V)<<11]-(A); |
247 | if(CHRram[r]) |
248 | PPUCHRRAM|=(3<<(A>>10)); |
249 | else |
250 | PPUCHRRAM&=~(3<<(A>>10)); |
251 | } |
252 | |
253 | void GINLINE FASTAPASS(3) setchr4r(int r, unsigned int A, unsigned int V) |
254 | { |
255 | if(!CHRptr[r]) return; |
256 | V&=CHRmask4[r]; |
257 | VPageR[(A)>>10]=VPageR[((A)>>10)+1]= |
258 | VPageR[((A)>>10)+2]=VPageR[((A)>>10)+3]=&CHRptr[r][(V)<<12]-(A); |
259 | if(CHRram[r]) |
260 | PPUCHRRAM|=(15<<(A>>10)); |
261 | else |
262 | PPUCHRRAM&=~(15<<(A>>10)); |
263 | } |
264 | |
265 | void GINLINE FASTAPASS(2) setchr8r(int r, unsigned int V) |
266 | { |
267 | int x; |
268 | |
269 | if(!CHRptr[r]) return; |
270 | V&=CHRmask8[r]; |
271 | for(x=7;x>=0;x--) |
272 | VPageR[x]=&CHRptr[r][V<<13]; |
273 | if(CHRram[r]) |
274 | PPUCHRRAM|=(255); |
275 | else |
276 | PPUCHRRAM&=~(255); |
277 | } |
278 | |
279 | void FASTAPASS(2) setchr1(unsigned int A, unsigned int V) |
280 | { |
281 | setchr1r(0,A,V); |
282 | } |
283 | |
284 | void FASTAPASS(2) setchr2(unsigned int A, unsigned int V) |
285 | { |
286 | setchr2r(0,A,V); |
287 | } |
288 | |
289 | void FASTAPASS(2) setchr4(unsigned int A, unsigned int V) |
290 | { |
291 | setchr4r(0,A,V); |
292 | } |
293 | |
5232c20c |
294 | void FASTAPASS(2) setchr8(unsigned int V) |
c62d2810 |
295 | { |
296 | setchr8r(0,V); |
297 | } |
298 | |
299 | void FASTAPASS(1) setvram8(uint8 *p) |
300 | { |
301 | int x; |
302 | for(x=7;x>=0;x--) |
303 | VPageR[x]=p; |
304 | PPUCHRRAM|=255; |
305 | } |
306 | |
307 | void FASTAPASS(2) setvram4(uint32 A, uint8 *p) |
308 | { |
309 | int x; |
310 | for(x=3;x>=0;x--) |
311 | VPageR[(A>>10)+x]=p-A; |
312 | PPUCHRRAM|=(15<<(A>>10)); |
313 | } |
314 | |
315 | void FASTAPASS(3) setvramb1(uint8 *p, uint32 A, uint32 b) |
316 | { |
317 | VPageR[A>>10]=p-A+(b<<10); |
318 | PPUCHRRAM|=(1<<(A>>10)); |
319 | } |
320 | |
321 | void FASTAPASS(3) setvramb2(uint8 *p, uint32 A, uint32 b) |
322 | { |
323 | VPageR[(A>>10)]=VPageR[(A>>10)+1]=p-A+(b<<11); |
324 | PPUCHRRAM|=(3<<(A>>10)); |
325 | } |
326 | |
327 | void FASTAPASS(3) setvramb4(uint8 *p, uint32 A, uint32 b) |
328 | { |
329 | int x; |
330 | |
331 | for(x=3;x>=0;x--) |
332 | VPageR[(A>>10)+x]=p-A+(b<<12); |
333 | PPUCHRRAM|=(15<<(A>>10)); |
334 | } |
335 | |
336 | void FASTAPASS(2) setvramb8(uint8 *p, uint32 b) |
337 | { |
338 | int x; |
339 | |
340 | for(x=7;x>=0;x--) |
341 | VPageR[x]=p+(b<<13); |
342 | PPUCHRRAM|=255; |
343 | } |
344 | |
345 | /* This function can be called without calling SetupCartMirroring(). */ |
346 | |
347 | void FASTAPASS(3) setntamem(uint8 *p, int ram, uint32 b) |
348 | { |
349 | vnapage[b]=p; |
350 | PPUNTARAM&=~(1<<b); |
351 | if(ram) |
352 | PPUNTARAM|=1<<b; |
353 | } |
354 | |
355 | static int mirrorhard=0; |
356 | void setmirrorw(int a, int b, int c, int d) |
357 | { |
358 | vnapage[0]=NTARAM+a*0x400; |
359 | vnapage[1]=NTARAM+b*0x400; |
360 | vnapage[2]=NTARAM+c*0x400; |
361 | vnapage[3]=NTARAM+d*0x400; |
362 | } |
363 | |
364 | void FASTAPASS(1) setmirror(int t) |
365 | { |
366 | if(!mirrorhard) |
367 | { |
368 | switch(t) |
369 | { |
370 | case MI_H: |
371 | vnapage[0]=vnapage[1]=NTARAM;vnapage[2]=vnapage[3]=NTARAM+0x400; |
372 | break; |
373 | case MI_V: |
374 | vnapage[0]=vnapage[2]=NTARAM;vnapage[1]=vnapage[3]=NTARAM+0x400; |
375 | break; |
376 | case MI_0: |
377 | vnapage[0]=vnapage[1]=vnapage[2]=vnapage[3]=NTARAM; |
378 | break; |
379 | case MI_1: |
380 | vnapage[0]=vnapage[1]=vnapage[2]=vnapage[3]=NTARAM+0x400; |
381 | break; |
382 | } |
383 | PPUNTARAM=0xF; |
384 | } |
385 | } |
386 | |
387 | void SetupCartMirroring(int m, int hard, uint8 *extra) |
388 | { |
389 | if(m<4) |
390 | setmirror(m); |
391 | else |
392 | { |
393 | vnapage[0]=NTARAM; |
394 | vnapage[1]=NTARAM+0x400; |
395 | vnapage[2]=extra; |
396 | vnapage[3]=extra+0x400; |
397 | PPUNTARAM=0xF; |
398 | } |
399 | mirrorhard=hard; |
400 | } |
401 | |
402 | static uint8 *GENIEROM=0; |
403 | |
404 | void FixGenieMap(void); |
405 | |
406 | /* Called when a game(file) is opened successfully. */ |
407 | void OpenGenie(void) |
408 | { |
409 | FILE *fp; |
410 | int x; |
411 | |
412 | if(!GENIEROM) |
413 | { |
414 | if(!(GENIEROM=FCEU_malloc(4096+1024))) return; |
415 | |
416 | if(!(fp=fopen(FCEU_MakeFName(FCEUMKF_GGROM,0,0),"rb"))) |
417 | { |
418 | FCEU_PrintError("Error opening Game Genie ROM image!"); |
419 | free(GENIEROM); |
420 | GENIEROM=0; |
421 | return; |
422 | } |
423 | if(fread(GENIEROM,1,16,fp)!=16) |
424 | { |
425 | grerr: |
426 | FCEU_PrintError("Error reading from Game Genie ROM image!"); |
427 | free(GENIEROM); |
428 | GENIEROM=0; |
429 | fclose(fp); |
430 | return; |
431 | } |
432 | if(GENIEROM[0]==0x4E) /* iNES ROM image */ |
433 | { |
434 | if(fread(GENIEROM,1,4096,fp)!=4096) |
435 | goto grerr; |
436 | if(fseek(fp,16384-4096,SEEK_CUR)) |
437 | goto grerr; |
438 | if(fread(GENIEROM+4096,1,256,fp)!=256) |
439 | goto grerr; |
440 | } |
441 | else |
442 | { |
443 | if(fread(GENIEROM+16,1,4352-16,fp)!=(4352-16)) |
444 | goto grerr; |
445 | } |
446 | fclose(fp); |
937bf65b |
447 | |
c62d2810 |
448 | /* Workaround for the FCE Ultra CHR page size only being 1KB */ |
449 | for(x=0;x<4;x++) |
450 | memcpy(GENIEROM+4096+(x<<8),GENIEROM+4096,256); |
451 | } |
452 | |
453 | geniestage=1; |
454 | } |
455 | |
456 | /* Called when a game is closed. */ |
457 | void CloseGenie(void) |
458 | { |
459 | /* No good reason to free() the Game Genie ROM image data. */ |
460 | geniestage=0; |
461 | FlushGenieRW(); |
462 | VPageR=VPage; |
463 | } |
464 | |
465 | static DECLFR(GenieRead) |
466 | { |
467 | return GENIEROM[A&4095]; |
468 | } |
469 | |
470 | static DECLFW(GenieWrite) |
471 | { |
472 | switch(A) |
473 | { |
474 | case 0x800c: |
475 | case 0x8008: |
476 | case 0x8004:genieval[((A-4)&0xF)>>2]=V;break; |
477 | |
478 | case 0x800b: |
479 | case 0x8007: |
480 | case 0x8003:geniech[((A-3)&0xF)>>2]=V;break; |
481 | |
482 | case 0x800a: |
483 | case 0x8006: |
484 | case 0x8002:genieaddr[((A-2)&0xF)>>2]&=0xFF00;genieaddr[((A-2)&0xF)>>2]|=V;break; |
485 | |
486 | case 0x8009: |
487 | case 0x8005: |
488 | case 0x8001:genieaddr[((A-1)&0xF)>>2]&=0xFF;genieaddr[((A-1)&0xF)>>2]|=(V|0x80)<<8;break; |
489 | |
490 | case 0x8000:if(!V) |
491 | FixGenieMap(); |
492 | else |
493 | { |
494 | modcon=V^0xFF; |
937bf65b |
495 | if(V==0x71) |
c62d2810 |
496 | modcon=0; |
497 | } |
498 | break; |
499 | } |
500 | } |
501 | |
502 | static readfunc GenieBackup[3]; |
503 | |
504 | static DECLFR(GenieFix1) |
505 | { |
506 | uint8 r=GenieBackup[0](A); |
507 | |
508 | if((modcon>>1)&1) // No check |
509 | return genieval[0]; |
510 | else if(r==geniech[0]) |
511 | return genieval[0]; |
512 | |
513 | return r; |
514 | } |
515 | |
516 | static DECLFR(GenieFix2) |
517 | { |
518 | uint8 r=GenieBackup[1](A); |
519 | |
520 | if((modcon>>2)&1) // No check |
521 | return genieval[1]; |
522 | else if(r==geniech[1]) |
523 | return genieval[1]; |
524 | |
525 | return r; |
526 | } |
527 | |
528 | static DECLFR(GenieFix3) |
529 | { |
530 | uint8 r=GenieBackup[2](A); |
531 | |
532 | if((modcon>>3)&1) // No check |
533 | return genieval[2]; |
534 | else if(r==geniech[2]) |
535 | return genieval[2]; |
536 | |
537 | return r; |
538 | } |
539 | |
540 | |
541 | void FixGenieMap(void) |
542 | { |
543 | int x; |
544 | |
545 | geniestage=2; |
546 | |
547 | for(x=0;x<8;x++) |
548 | VPage[x]=VPageG[x]; |
549 | |
550 | VPageR=VPage; |
551 | FlushGenieRW(); |
937bf65b |
552 | |
c62d2810 |
553 | for(x=0;x<3;x++) |
554 | if((modcon>>(4+x))&1) |
555 | { |
556 | readfunc tmp[3]={GenieFix1,GenieFix2,GenieFix3}; |
557 | GenieBackup[x]=GetReadHandler(genieaddr[x]); |
558 | SetReadHandler(genieaddr[x],genieaddr[x],tmp[x]); |
559 | } |
560 | } |
561 | |
562 | void GeniePower(void) |
563 | { |
564 | uint32 x; |
565 | |
566 | if(!geniestage) |
567 | return; |
568 | |
569 | geniestage=1; |
570 | for(x=0;x<3;x++) |
571 | { |
572 | genieval[x]=0xFF; |
573 | geniech[x]=0xFF; |
574 | genieaddr[x]=0xFFFF; |
575 | } |
576 | modcon=0; |
577 | |
578 | SetWriteHandler(0x8000,0xFFFF,GenieWrite); |
579 | SetReadHandler(0x8000,0xFFFF,GenieRead); |
580 | |
581 | for(x=0;x<8;x++) |
582 | VPage[x]=GENIEROM+4096-0x400*x; |
583 | |
584 | if(AllocGenieRW()) |
585 | VPageR=VPageG; |
586 | else |
587 | geniestage=2; |
588 | } |
589 | |
590 | |