c62d2810 |
1 | /* FCE Ultra - NES/Famicom Emulator |
2 | * |
3 | * Copyright notice for this file: |
15300263 |
4 | * Copyright (C) 1998 BERO |
c62d2810 |
5 | * Copyright (C) 2002 Ben Parnell |
6 | * |
7 | * This program is free software; you can redistribute it and/or modify |
8 | * it under the terms of the GNU General Public License as published by |
9 | * the Free Software Foundation; either version 2 of the License, or |
10 | * (at your option) any later version. |
11 | * |
12 | * This program is distributed in the hope that it will be useful, |
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
15 | * GNU General Public License for more details. |
16 | * |
17 | * You should have received a copy of the GNU General Public License |
18 | * along with this program; if not, write to the Free Software |
19 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
20 | */ |
21 | |
22 | #include <string.h> |
23 | #include <stdio.h> |
24 | #include <stdlib.h> |
25 | |
26 | #include "types.h" |
27 | #include "x6502.h" |
28 | #include "fce.h" |
29 | #include "sound.h" |
30 | #include "svga.h" |
31 | #include "netplay.h" |
32 | #include "general.h" |
33 | #include "endian.h" |
34 | #include "version.h" |
35 | #include "memory.h" |
36 | |
37 | #include "cart.h" |
38 | #include "nsf.h" |
39 | #include "fds.h" |
40 | #include "ines.h" |
41 | #include "unif.h" |
42 | #include "cheat.h" |
43 | |
44 | #include "state.h" |
45 | #include "video.h" |
46 | #include "input.h" |
47 | #include "file.h" |
48 | #include "crc32.h" |
5232c20c |
49 | #include "ppu.h" |
c62d2810 |
50 | |
92764e62 |
51 | #include "palette.h" |
5a2aa426 |
52 | #include "movie.h" |
53 | |
4fdfab07 |
54 | #include "dprintf.h" |
55 | |
6587f346 |
56 | #ifdef GP2X |
57 | #include "drivers/gp2x/asmutils.h" |
58 | #endif |
59 | |
c62d2810 |
60 | #define Pal (PALRAM) |
61 | |
5232c20c |
62 | |
4a1bf31b |
63 | static void (*RefreshLine)(uint8 *P, uint32 vofs) = NULL; |
c62d2810 |
64 | static void PRefreshLine(void); |
5232c20c |
65 | |
c62d2810 |
66 | static void ResetPPU(void); |
67 | static void PowerPPU(void); |
68 | |
69 | uint64 timestampbase=0; |
70 | |
4fdfab07 |
71 | static int ppudead=1; |
72 | static int kook=0; |
73 | |
c62d2810 |
74 | int MMC5Hack; |
75 | uint32 MMC5HackVROMMask; |
76 | uint8 *MMC5HackExNTARAMPtr; |
77 | uint8 *MMC5HackVROMPTR; |
15300263 |
78 | uint8 MMC5HackCHRMode=0; |
c62d2810 |
79 | uint8 MMC5HackSPMode; |
80 | uint8 MMC5HackSPScroll; |
81 | uint8 MMC5HackSPPage; |
82 | |
83 | uint8 *MMC5SPRVPage[8]; |
84 | uint8 *MMC5BGVPage[8]; |
85 | |
86 | |
87 | uint8 VRAMBuffer,PPUGenLatch; |
88 | |
89 | uint8 *vnapage[4]; |
90 | uint8 PPUNTARAM; |
91 | uint8 PPUCHRRAM; |
92 | |
93 | /* Color deemphasis emulation. Joy... */ |
94 | static uint8 deemp=0; |
95 | static int deempcnt[8]; |
96 | |
c62d2810 |
97 | FCEUGI FCEUGameInfo; |
c4980f9e |
98 | void (*GameInterface)(int h, void *param); |
c62d2810 |
99 | |
100 | void FP_FASTAPASS(1) (*PPU_hook)(uint32 A); |
101 | |
102 | void (*GameStateRestore)(int version); |
d97315ac |
103 | void (*GameHBIRQHook)(void), (*GameHBIRQHook2)(void); |
c62d2810 |
104 | |
105 | readfunc ARead[0x10000]; |
106 | writefunc BWrite[0x10000]; |
107 | static readfunc *AReadG; |
108 | static writefunc *BWriteG; |
109 | static int RWWrap=0; |
110 | |
92e249b1 |
111 | #ifdef ASM_6502 |
112 | static void asmcpu_update(int32 cycles) |
113 | { |
92e249b1 |
114 | // some code from x6502.c |
115 | fhcnt-=cycles; |
116 | if(fhcnt<=0) |
117 | { |
118 | FrameSoundUpdate(); |
119 | fhcnt+=fhinc; |
120 | } |
121 | |
122 | if(PCMIRQCount>0) |
123 | { |
124 | PCMIRQCount-=cycles; |
125 | if(PCMIRQCount<=0) |
126 | { |
127 | vdis=1; |
128 | if((PSG[0x10]&0x80) && !(PSG[0x10]&0x40)) |
129 | { |
130 | extern uint8 SIRQStat; |
131 | SIRQStat|=0x80; |
132 | X6502_IRQBegin(FCEU_IQDPCM); |
133 | } |
134 | } |
135 | } |
136 | } |
137 | |
138 | void asmcpu_unpack(void) |
139 | { |
140 | nes_registers[0] = X.A << 24; |
141 | nes_registers[1] = X.X; |
142 | nes_registers[2] = X.Y; |
143 | pc_base = 0; |
144 | nes_registers[3] = X.PC; |
c0bf6f9f |
145 | X6502_Rebase_a(); |
92e249b1 |
146 | nes_registers[4] = X.S << 24; |
147 | nes_registers[4]|= X.IRQlow << 8; |
8fa5eb33 |
148 | nes_registers[7] = (uint32)X.count << 16; |
92e249b1 |
149 | |
150 | // NVUB DIZC |
151 | nes_registers[4]|= X.P & 0x5d; |
152 | nes_registers[5] = X.P << 24; // N |
153 | if (!(X.P&0x02)) nes_registers[5] |= 1; // Z |
154 | } |
155 | |
156 | void asmcpu_pack(void) |
157 | { |
158 | X.A = nes_registers[0] >> 24; |
159 | X.X = nes_registers[1]; |
160 | X.Y = nes_registers[2]; |
161 | X.PC= nes_registers[3] - pc_base; |
162 | X.S = nes_registers[4] >> 24; |
163 | X.IRQlow = nes_registers[4] >> 8; |
8fa5eb33 |
164 | X.count = (int32) nes_registers[7] >> 16; |
92e249b1 |
165 | |
166 | // NVUB DIZC |
167 | X.P = nes_registers[4] & 0x5d; |
168 | if ( nes_registers[5]&0x80000000) X.P |= 0x80; // N |
169 | if (!(nes_registers[5]&0x000000ff)) X.P |= 0x02; // Z |
170 | } |
171 | #endif |
172 | |
c62d2810 |
173 | DECLFW(BNull) |
174 | { |
175 | |
176 | } |
177 | |
178 | DECLFR(ANull) |
179 | { |
180 | return(X.DB); |
181 | } |
182 | |
183 | int AllocGenieRW(void) |
184 | { |
185 | if(!(AReadG=FCEU_malloc(0x8000*sizeof(readfunc)))) |
186 | return 0; |
187 | if(!(BWriteG=FCEU_malloc(0x8000*sizeof(writefunc)))) |
188 | return 0; |
189 | RWWrap=1; |
190 | return 1; |
191 | } |
192 | |
193 | void FlushGenieRW(void) |
194 | { |
195 | int32 x; |
196 | |
197 | if(RWWrap) |
198 | { |
199 | for(x=0;x<0x8000;x++) |
200 | { |
201 | ARead[x+0x8000]=AReadG[x]; |
202 | BWrite[x+0x8000]=BWriteG[x]; |
203 | } |
b8da43fb |
204 | #ifdef ASM_6502 |
205 | GenieSetPages(1); |
206 | #endif |
c62d2810 |
207 | free(AReadG); |
208 | free(BWriteG); |
209 | AReadG=0; |
210 | BWriteG=0; |
211 | RWWrap=0; |
212 | } |
213 | } |
214 | |
215 | readfunc FASTAPASS(1) GetReadHandler(int32 a) |
216 | { |
217 | if(a>=0x8000 && RWWrap) |
218 | return AReadG[a-0x8000]; |
219 | else |
220 | return ARead[a]; |
221 | } |
222 | |
223 | void FASTAPASS(3) SetReadHandler(int32 start, int32 end, readfunc func) |
224 | { |
225 | int32 x; |
226 | |
227 | if(!func) |
228 | func=ANull; |
229 | |
230 | if(RWWrap) |
231 | for(x=end;x>=start;x--) |
232 | { |
233 | if(x>=0x8000) |
234 | AReadG[x-0x8000]=func; |
235 | else |
236 | ARead[x]=func; |
237 | } |
238 | else |
239 | |
240 | for(x=end;x>=start;x--) |
241 | ARead[x]=func; |
242 | } |
243 | |
244 | writefunc FASTAPASS(1) GetWriteHandler(int32 a) |
245 | { |
246 | if(RWWrap && a>=0x8000) |
247 | return BWriteG[a-0x8000]; |
248 | else |
249 | return BWrite[a]; |
250 | } |
251 | |
252 | void FASTAPASS(3) SetWriteHandler(int32 start, int32 end, writefunc func) |
253 | { |
254 | int32 x; |
255 | |
256 | if(!func) |
257 | func=BNull; |
258 | |
259 | if(RWWrap) |
260 | for(x=end;x>=start;x--) |
261 | { |
262 | if(x>=0x8000) |
263 | BWriteG[x-0x8000]=func; |
264 | else |
265 | BWrite[x]=func; |
266 | } |
267 | else |
268 | for(x=end;x>=start;x--) |
269 | BWrite[x]=func; |
270 | } |
271 | |
272 | uint8 vtoggle=0; |
273 | uint8 XOffset=0; |
274 | |
275 | uint32 TempAddr,RefreshAddr; |
276 | |
c62d2810 |
277 | |
278 | /* scanline is equal to the current visible scanline we're on. */ |
279 | |
280 | int scanline; |
c62d2810 |
281 | |
9115e7d2 |
282 | uint8 GameMemBlock[131072] __attribute__ ((aligned (4))); |
283 | uint8 NTARAM[0x800] __attribute__ ((aligned (4))); |
284 | uint8 PALRAM[0x20] __attribute__ ((aligned (4))); |
92e249b1 |
285 | #if !defined(ASM_6502) || defined(DEBUG_ASM_6502) |
9115e7d2 |
286 | uint8 RAM[0x800] __attribute__ ((aligned (4))); |
af32b6c2 |
287 | #endif |
9115e7d2 |
288 | |
c62d2810 |
289 | uint8 PPU[4]; |
290 | uint8 PPUSPL; |
291 | |
c62d2810 |
292 | uint8 PAL=0; |
293 | |
5232c20c |
294 | |
c62d2810 |
295 | #define MMC5BGVRAMADR(V) &MMC5BGVPage[(V)>>10][(V)] |
296 | #define VRAMADR(V) &VPage[(V)>>10][(V)] |
15300263 |
297 | |
4fdfab07 |
298 | static int linestartts; |
299 | static int tofix=0; |
300 | |
e328100e |
301 | static uint8 *Plinef; |
302 | |
303 | extern uint8 sprlinebuf[256+8]; |
304 | extern int32 sphitx; |
305 | extern uint8 sphitdata; |
306 | |
307 | extern int spork; /* spork the world. Any sprites on this line? |
308 | Then this will be set to 1. Needed for zapper |
309 | emulation and *gasp* sprite emulation. |
310 | */ |
311 | |
312 | static void ResetRL(uint8 *target) |
4fdfab07 |
313 | { |
e328100e |
314 | if(InputScanlineHook) |
315 | InputScanlineHook(0,0,0,0); |
316 | Plinef=target; |
4fdfab07 |
317 | linestartts=timestamp*48+X6502_GetCycleCount(); |
318 | tofix=1; |
319 | } |
320 | |
321 | static INLINE void Fixit1(void); |
322 | |
e328100e |
323 | /* faking FCEUPPU_LineUpdate() from later versions of the emu */ |
324 | static void FakedLineUpdate(void) |
4fdfab07 |
325 | { |
326 | #define TOFIXNUM (272-0x4) |
327 | int lastpixel; |
328 | |
e328100e |
329 | if (scanline >= 240) return; |
330 | |
331 | if (tofix || sphitx != 0x100) |
4fdfab07 |
332 | { |
333 | lastpixel = (timestamp*48-linestartts)>>4; |
334 | if (PAL) lastpixel += lastpixel>>4; |
4fdfab07 |
335 | //printf("lastpixel: %i\n", lastpixel); |
e328100e |
336 | } |
337 | |
338 | if (tofix && lastpixel>=TOFIXNUM) |
339 | { |
340 | Fixit1(); |
341 | tofix=0; |
342 | } |
4fdfab07 |
343 | |
e328100e |
344 | // CheckSpriteHit() |
345 | if(sphitx!=0x100) |
346 | { |
347 | int l=lastpixel-16; |
348 | int x; |
349 | |
350 | for(x=sphitx;x<(sphitx+8) && x<l;x++) |
4fdfab07 |
351 | { |
e328100e |
352 | if((sphitdata&(0x80>>(x-sphitx))) && !(Plinef[x]&64)) |
353 | { |
354 | PPU_status|=0x40; |
355 | sphitx=0x100; |
356 | break; |
357 | } |
4fdfab07 |
358 | } |
359 | } |
360 | } |
361 | |
362 | |
c62d2810 |
363 | static DECLFW(BRAML) |
15300263 |
364 | { |
c62d2810 |
365 | RAM[A]=V; |
366 | } |
367 | |
368 | static DECLFW(BRAMH) |
369 | { |
370 | RAM[A&0x7FF]=V; |
371 | } |
372 | |
373 | static DECLFR(ARAML) |
374 | { |
375 | return RAM[A]; |
376 | } |
377 | |
378 | static DECLFR(ARAMH) |
379 | { |
380 | return RAM[A&0x7FF]; |
381 | } |
382 | |
15300263 |
383 | |
c62d2810 |
384 | static DECLFR(A2002) |
385 | { |
4fdfab07 |
386 | /* merged */ |
c62d2810 |
387 | uint8 ret; |
4fdfab07 |
388 | |
e328100e |
389 | FakedLineUpdate(); |
c62d2810 |
390 | ret = PPU_status; |
4fdfab07 |
391 | ret|=PPUGenLatch&0x1F; |
c62d2810 |
392 | vtoggle=0; |
393 | PPU_status&=0x7F; |
4fdfab07 |
394 | PPUGenLatch=ret; |
ea80a45b |
395 | //dprintf("r [2002] %02x",ret); |
4fdfab07 |
396 | return ret; |
c62d2810 |
397 | } |
398 | |
399 | static DECLFR(A200x) |
400 | { |
4fdfab07 |
401 | /* merged */ |
e328100e |
402 | FakedLineUpdate(); |
c62d2810 |
403 | return PPUGenLatch; |
404 | } |
405 | |
406 | static DECLFR(A2007) |
407 | { |
4fdfab07 |
408 | /* merged */ |
c62d2810 |
409 | uint8 ret; |
410 | uint32 tmp=RefreshAddr&0x3FFF; |
411 | |
e328100e |
412 | FakedLineUpdate(); |
4fdfab07 |
413 | |
414 | ret=VRAMBuffer; |
415 | |
c62d2810 |
416 | if(PPU_hook) PPU_hook(tmp); |
4fdfab07 |
417 | PPUGenLatch=VRAMBuffer; |
15300263 |
418 | if(tmp<0x2000) |
c62d2810 |
419 | { |
420 | VRAMBuffer=VPage[tmp>>10][tmp]; |
421 | } |
15300263 |
422 | else |
c62d2810 |
423 | { |
424 | VRAMBuffer=vnapage[(tmp>>10)&0x3][tmp&0x3FF]; |
425 | } |
426 | |
427 | if (INC32) RefreshAddr+=32; |
428 | else RefreshAddr++; |
429 | if(PPU_hook) PPU_hook(RefreshAddr&0x3fff); |
4fdfab07 |
430 | dprintf("r [2007] %02x",ret); |
c62d2810 |
431 | return ret; |
432 | } |
433 | |
434 | static DECLFW(B2000) |
435 | { |
4fdfab07 |
436 | /* NMI2? */ |
e328100e |
437 | FakedLineUpdate(); |
c62d2810 |
438 | PPUGenLatch=V; |
439 | PPU[0]=V; |
440 | TempAddr&=0xF3FF; |
441 | TempAddr|=(V&3)<<10; |
442 | } |
443 | |
444 | static DECLFW(B2001) |
445 | { |
4fdfab07 |
446 | /* merged */ |
e328100e |
447 | FakedLineUpdate(); |
c62d2810 |
448 | PPUGenLatch=V; |
449 | PPU[1]=V; |
450 | if(V&0xE0) |
451 | deemp=V>>5; |
452 | //printf("$%04x:$%02x, %d\n",X.PC,V,scanline); |
453 | } |
454 | |
455 | static DECLFW(B2002) |
456 | { |
4fdfab07 |
457 | /* merged */ |
c62d2810 |
458 | PPUGenLatch=V; |
459 | } |
460 | |
461 | static DECLFW(B2003) |
462 | { |
4fdfab07 |
463 | /* merged */ |
c62d2810 |
464 | PPUGenLatch=V; |
465 | PPU[3]=V; |
466 | PPUSPL=V&0x7; |
467 | } |
468 | |
469 | static DECLFW(B2004) |
470 | { |
4fdfab07 |
471 | /* merged */ |
c62d2810 |
472 | PPUGenLatch=V; |
4fdfab07 |
473 | if(PPUSPL>=8) |
c62d2810 |
474 | { |
475 | if(PPU[3]>=8) |
476 | SPRAM[PPU[3]]=V; |
477 | } |
478 | else |
479 | { |
480 | //printf("$%02x:$%02x\n",PPUSPL,V); |
481 | SPRAM[PPUSPL]=V; |
c62d2810 |
482 | } |
483 | PPU[3]++; |
4fdfab07 |
484 | PPUSPL++; |
5232c20c |
485 | |
c62d2810 |
486 | } |
487 | |
488 | static DECLFW(B2005) |
489 | { |
4fdfab07 |
490 | /* merged */ |
c62d2810 |
491 | uint32 tmp=TempAddr; |
e328100e |
492 | FakedLineUpdate(); |
c62d2810 |
493 | PPUGenLatch=V; |
494 | if (!vtoggle) |
495 | { |
496 | tmp&=0xFFE0; |
497 | tmp|=V>>3; |
498 | XOffset=V&7; |
499 | } |
500 | else |
501 | { |
502 | tmp&=0x8C1F; |
503 | tmp|=((V&~0x7)<<2); |
504 | tmp|=(V&7)<<12; |
505 | } |
506 | |
507 | TempAddr=tmp; |
508 | vtoggle^=1; |
509 | } |
510 | |
511 | static DECLFW(B2006) |
512 | { |
4fdfab07 |
513 | /* merged */ |
e328100e |
514 | FakedLineUpdate(); |
4fdfab07 |
515 | |
c62d2810 |
516 | PPUGenLatch=V; |
517 | if(!vtoggle) |
518 | { |
519 | TempAddr&=0x00FF; |
520 | TempAddr|=(V&0x3f)<<8; |
521 | } |
522 | else |
523 | { |
524 | TempAddr&=0xFF00; |
525 | TempAddr|=V; |
c62d2810 |
526 | |
4fdfab07 |
527 | RefreshAddr=TempAddr; |
c62d2810 |
528 | if(PPU_hook) |
529 | PPU_hook(RefreshAddr); |
530 | } |
531 | vtoggle^=1; |
532 | } |
533 | |
534 | static DECLFW(B2007) |
15300263 |
535 | { |
4fdfab07 |
536 | /* merged */ |
c62d2810 |
537 | uint32 tmp=RefreshAddr&0x3FFF; |
c62d2810 |
538 | PPUGenLatch=V; |
539 | if(tmp>=0x3F00) |
540 | { |
4fdfab07 |
541 | // hmmm.... |
542 | if(!(tmp&0xf)) |
543 | PALRAM[0x00]=PALRAM[0x04]=PALRAM[0x08]=PALRAM[0x0C]=V&0x3f; |
544 | else if(tmp&3) PALRAM[(tmp&0x1f)]=V&0x3f; |
c62d2810 |
545 | } |
546 | else if(tmp<0x2000) |
547 | { |
548 | if(PPUCHRRAM&(1<<(tmp>>10))) |
549 | VPage[tmp>>10][tmp]=V; |
550 | } |
551 | else |
15300263 |
552 | { |
c62d2810 |
553 | if(PPUNTARAM&(1<<((tmp&0xF00)>>10))) |
554 | vnapage[((tmp&0xF00)>>10)][tmp&0x3FF]=V; |
555 | } |
556 | if (INC32) RefreshAddr+=32; |
557 | else RefreshAddr++; |
558 | if(PPU_hook) PPU_hook(RefreshAddr&0x3fff); |
559 | } |
560 | |
561 | static DECLFW(B4014) |
15300263 |
562 | { |
c62d2810 |
563 | uint32 t=V<<8; |
564 | int x; |
4fdfab07 |
565 | |
c62d2810 |
566 | for(x=0;x<256;x++) |
567 | B2004(0x2004,X.DB=ARead[t+x](t+x)); |
568 | X6502_AddCycles(512); |
569 | } |
570 | |
5232c20c |
571 | void BGRender(uint8 *target) |
c62d2810 |
572 | { |
4a1bf31b |
573 | uint32 tem, vofs; |
574 | vofs=((PPU[0]&0x10)<<8) | ((RefreshAddr>>12)&7); |
575 | |
576 | Pal[0]|=64; |
577 | Pal[4]|=64; |
578 | Pal[8]|=64; |
579 | Pal[0xC]|=64; |
580 | RefreshLine(target-XOffset, vofs); |
581 | Pal[0]&=63; |
582 | Pal[4]&=63; |
583 | Pal[8]&=63; |
584 | Pal[0xC]&=63; |
585 | |
c62d2810 |
586 | if(!(PPU[1]&2)) |
587 | { |
9115e7d2 |
588 | tem=Pal[0]|0x40; |
589 | tem|=tem<<8; |
590 | tem|=tem<<16; |
c62d2810 |
591 | *(uint32 *)target=*(uint32 *)(target+4)=tem; |
592 | } |
593 | } |
594 | |
595 | #ifdef FRAMESKIP |
5232c20c |
596 | int FSkip=0; |
c62d2810 |
597 | void FCEUI_FrameSkip(int x) |
598 | { |
599 | FSkip=x; |
600 | } |
601 | #endif |
602 | |
603 | /* This is called at the beginning of each visible scanline */ |
e328100e |
604 | static void LineUpdate(uint8 *target) |
c62d2810 |
605 | { |
606 | uint32 tem; |
c0623dcf |
607 | int y; |
c62d2810 |
608 | |
078eb4c0 |
609 | /* PRefreshLine() will not get called on skipped frames. This |
610 | * could cause a problem, but the solution would be rather complex, |
611 | * due to the current sprite 0 hit code. |
612 | */ |
c0623dcf |
613 | if(FSkip) |
614 | { |
615 | y=(int)SPRAM[0] + 1; |
616 | if(scanline==y && SpriteON) PPU_status|=0x40; // hack |
617 | return; |
618 | } |
078eb4c0 |
619 | |
620 | if(scanline < FSettings.FirstSLine || scanline > FSettings.LastSLine) |
e328100e |
621 | { |
622 | if(PPU_hook) |
623 | PRefreshLine(); |
c0623dcf |
624 | y=(int)SPRAM[0] + 1; |
625 | if(scanline==y && SpriteON) PPU_status|=0x40; |
e328100e |
626 | } |
627 | else |
628 | { |
629 | if(ScreenON) |
c62d2810 |
630 | { |
21afaa36 |
631 | BGRender(target); |
c62d2810 |
632 | } |
e328100e |
633 | else |
c62d2810 |
634 | { |
e328100e |
635 | tem=Pal[0]|0x40; |
636 | tem|=tem << 8; |
637 | tem|=tem << 16; |
638 | FCEU_dwmemset(target,tem,256); |
c62d2810 |
639 | } |
15300263 |
640 | } |
e328100e |
641 | |
c62d2810 |
642 | if(InputScanlineHook) |
e328100e |
643 | InputScanlineHook(target,spork?sprlinebuf:0,linestartts,256); |
644 | } |
645 | |
646 | |
647 | static void LineUpdateEnd(uint8 *target) |
648 | { |
649 | #ifdef GP2X |
650 | if(ScreenON || SpriteON) // Yes, very el-cheapo. |
651 | { |
652 | if(PPU[1]&0x01) |
653 | block_and(target, 256, 0x30); |
654 | } |
655 | if((PPU[1]>>5)==0x7) |
656 | block_or(target, 256, 0xc0); |
657 | else if(PPU[1]&0xE0) |
658 | block_or(target, 256, 0x40); |
659 | else |
660 | block_andor(target, 256, 0x3f, 0x80); |
661 | #else |
662 | int x; |
663 | |
664 | if(ScreenON || SpriteON) // Yes, very el-cheapo. |
665 | { |
666 | if(PPU[1]&0x01) |
667 | { |
668 | for(x=63;x>=0;x--) |
669 | *(uint32 *)&target[x<<2]=(*(uint32*)&target[x<<2])&0x30303030; |
670 | } |
671 | } |
672 | if((PPU[1]>>5)==0x7) |
673 | { |
674 | for(x=63;x>=0;x--) |
675 | *(uint32 *)&target[x<<2]=((*(uint32*)&target[x<<2])&0x3f3f3f3f)|0xc0c0c0c0; |
676 | } |
677 | else if(PPU[1]&0xE0) |
678 | for(x=63;x>=0;x--) |
679 | *(uint32 *)&target[x<<2]=(*(uint32*)&target[x<<2])|0x40404040; |
680 | else |
681 | for(x=63;x>=0;x--) |
682 | *(uint32 *)&target[x<<2]=((*(uint32*)&target[x<<2])&0x3f3f3f3f)|0x80808080; |
683 | #endif |
684 | |
685 | // black borders |
686 | ((uint32 *)target)[-2]=((uint32 *)target)[-1]=0; |
687 | ((uint32 *)target)[64]=((uint32 *)target)[65]=0; |
c62d2810 |
688 | } |
689 | |
690 | #define PAL(c) ((c)+cc) |
691 | |
692 | |
693 | static void PRefreshLine(void) |
694 | { |
078eb4c0 |
695 | uint32 vofs; |
696 | int X1; |
697 | vofs=((PPU[0]&0x10)<<8) | ((RefreshAddr>>12)&7); |
698 | void (*PPU_hook_)(uint32 A) = PPU_hook; |
c62d2810 |
699 | |
c62d2810 |
700 | |
078eb4c0 |
701 | for(X1=33;X1;X1--) |
702 | { |
703 | uint32 zz2; |
704 | uint32 vadr; |
705 | |
706 | zz2=(RefreshAddr>>10)&3; |
707 | PPU_hook_(0x2000|(RefreshAddr&0xFFF)); |
708 | |
709 | vadr=(vnapage[zz2][RefreshAddr&0x3ff]<<4)+vofs; |
710 | |
711 | PPU_hook_(vadr); |
c62d2810 |
712 | |
c62d2810 |
713 | if((RefreshAddr&0x1f)==0x1f) |
714 | RefreshAddr^=0x41F; |
715 | else |
716 | RefreshAddr++; |
078eb4c0 |
717 | } |
c62d2810 |
718 | } |
719 | |
4a1bf31b |
720 | /* This high-level graphics MMC5 emulation code was written |
721 | for MMC5 carts in "CL" mode. It's probably not totally |
722 | correct for carts in "SL" mode. |
723 | */ |
724 | static void RefreshLine_MMC5Hack1(uint8 *P, uint32 vofs) |
c62d2810 |
725 | { |
4a1bf31b |
726 | int8 tochange, X1; |
c62d2810 |
727 | |
728 | tochange=MMC5HackSPMode&0x1F; |
729 | |
730 | for(X1=33;X1;X1--,P+=8) |
731 | { |
732 | uint8 *C; |
5232c20c |
733 | uint8 cc,zz,zz2; |
c62d2810 |
734 | uint32 vadr; |
735 | |
15300263 |
736 | if((tochange<=0 && MMC5HackSPMode&0x40) || |
c62d2810 |
737 | (tochange>0 && !(MMC5HackSPMode&0x40))) |
738 | { |
739 | uint8 xs,ys; |
740 | |
15300263 |
741 | xs=33-X1; |
c62d2810 |
742 | ys=((scanline>>3)+MMC5HackSPScroll)&0x1F; |
743 | if(ys>=0x1E) ys-=0x1E; |
744 | vadr=(MMC5HackExNTARAMPtr[xs|(ys<<5)]<<4)+(vofs&7); |
745 | |
746 | C = MMC5HackVROMPTR+vadr; |
747 | C += ((MMC5HackSPPage & 0x3f & MMC5HackVROMMask) << 12); |
748 | |
749 | cc=MMC5HackExNTARAMPtr[0x3c0+(xs>>2)+((ys&0x1C)<<1)]; |
750 | cc=((cc >> ((xs&2) + ((ys&0x2)<<1))) &3) <<2; |
751 | } |
752 | else |
753 | { |
754 | zz=RefreshAddr&0x1F; |
755 | zz2=(RefreshAddr>>10)&3; |
756 | vadr=(vnapage[zz2][RefreshAddr&0x3ff]<<4)+vofs; |
757 | C = MMC5BGVRAMADR(vadr); |
758 | cc=vnapage[zz2][0x3c0+(zz>>2)+((RefreshAddr&0x380)>>4)]; |
759 | cc=((cc >> ((zz&2) + ((RefreshAddr&0x40)>>4))) &3) <<2; |
760 | } |
761 | #include "fceline.h" |
762 | |
763 | if((RefreshAddr&0x1f)==0x1f) |
764 | RefreshAddr^=0x41F; |
765 | else |
766 | RefreshAddr++; |
767 | tochange--; |
768 | } |
4a1bf31b |
769 | } |
770 | |
771 | static void RefreshLine_MMC5Hack2(uint8 *P, uint32 vofs) |
772 | { |
773 | int8 tochange, X1; |
c62d2810 |
774 | |
775 | tochange=MMC5HackSPMode&0x1F; |
776 | |
777 | for(X1=33;X1;X1--,P+=8) |
778 | { |
779 | uint8 *C; |
5232c20c |
780 | uint8 cc; |
781 | uint8 zz2; |
c62d2810 |
782 | uint32 vadr; |
783 | |
784 | if((tochange<=0 && MMC5HackSPMode&0x40) || |
785 | (tochange>0 && !(MMC5HackSPMode&0x40))) |
786 | { |
787 | uint8 xs,ys; |
788 | |
15300263 |
789 | xs=33-X1; |
c62d2810 |
790 | ys=((scanline>>3)+MMC5HackSPScroll)&0x1F; |
791 | if(ys>=0x1E) ys-=0x1E; |
792 | vadr=(MMC5HackExNTARAMPtr[xs|(ys<<5)]<<4)+(vofs&7); |
793 | |
794 | C = MMC5HackVROMPTR+vadr; |
795 | C += ((MMC5HackSPPage & 0x3f & MMC5HackVROMMask) << 12); |
796 | |
797 | cc=MMC5HackExNTARAMPtr[0x3c0+(xs>>2)+((ys&0x1C)<<1)]; |
798 | cc=((cc >> ((xs&2) + ((ys&0x2)<<1))) &3) <<2; |
799 | } |
800 | else |
801 | { |
802 | C=MMC5HackVROMPTR; |
803 | zz2=(RefreshAddr>>10)&3; |
804 | vadr = (vnapage[zz2][RefreshAddr & 0x3ff] << 4) + vofs; |
805 | C += (((MMC5HackExNTARAMPtr[RefreshAddr & 0x3ff]) & 0x3f & |
806 | MMC5HackVROMMask) << 12) + (vadr & 0xfff); |
807 | vadr = (MMC5HackExNTARAMPtr[RefreshAddr & 0x3ff] & 0xC0)>> 4; |
808 | cc = vadr; |
809 | } |
810 | #include "fceline.h" |
811 | if((RefreshAddr&0x1f)==0x1f) |
812 | RefreshAddr^=0x41F; |
813 | else |
814 | RefreshAddr++; |
815 | tochange--; |
816 | } |
4a1bf31b |
817 | } |
818 | |
819 | static void RefreshLine_MMC5Hack3(uint8 *P, uint32 vofs) |
820 | { |
821 | int8 X1; |
c62d2810 |
822 | |
c62d2810 |
823 | for(X1=33;X1;X1--,P+=8) |
824 | { |
15300263 |
825 | uint8 *C; |
5232c20c |
826 | uint8 cc; |
827 | uint8 zz2; |
15300263 |
828 | uint32 vadr; |
c62d2810 |
829 | |
830 | C=MMC5HackVROMPTR; |
831 | zz2=(RefreshAddr>>10)&3; |
832 | vadr = (vnapage[zz2][RefreshAddr & 0x3ff] << 4) + vofs; |
15300263 |
833 | C += (((MMC5HackExNTARAMPtr[RefreshAddr & 0x3ff]) & 0x3f & |
c62d2810 |
834 | MMC5HackVROMMask) << 12) + (vadr & 0xfff); |
835 | vadr = (MMC5HackExNTARAMPtr[RefreshAddr & 0x3ff] & 0xC0)>> 4; |
836 | cc = vadr; |
837 | |
838 | #include "fceline.h" |
839 | if((RefreshAddr&0x1f)==0x1f) |
840 | RefreshAddr^=0x41F; |
841 | else |
842 | RefreshAddr++; |
843 | } |
4a1bf31b |
844 | } |
845 | |
846 | static void RefreshLine_MMC5Hack4(uint8 *P, uint32 vofs) |
847 | { |
848 | int8 X1; |
849 | |
c62d2810 |
850 | for(X1=33;X1;X1--,P+=8) |
851 | { |
852 | uint8 *C; |
5232c20c |
853 | uint8 cc,zz,zz2; |
c62d2810 |
854 | uint32 vadr; |
855 | |
856 | zz=RefreshAddr&0x1F; |
857 | zz2=(RefreshAddr>>10)&3; |
858 | vadr=(vnapage[zz2][RefreshAddr&0x3ff]<<4)+vofs; |
859 | C = MMC5BGVRAMADR(vadr); |
860 | cc=vnapage[zz2][0x3c0+(zz>>2)+((RefreshAddr&0x380)>>4)]; |
861 | cc=((cc >> ((zz&2) + ((RefreshAddr&0x40)>>4))) &3) <<2; |
862 | |
863 | #include "fceline.h" |
15300263 |
864 | |
c62d2810 |
865 | if((RefreshAddr&0x1f)==0x1f) |
866 | RefreshAddr^=0x41F; |
867 | else |
868 | RefreshAddr++; |
15300263 |
869 | } |
4a1bf31b |
870 | } |
871 | |
872 | static void RefreshLine_PPU_hook(uint8 *P, uint32 vofs) |
873 | { |
874 | int8 X1; |
078eb4c0 |
875 | void (*PPU_hook_)(uint32 A) = PPU_hook; |
876 | uint32 rfraddr = RefreshAddr; |
877 | uint8 *page = vnapage[(rfraddr>>10)&3]; |
c62d2810 |
878 | |
c62d2810 |
879 | for(X1=33;X1;X1--,P+=8) |
880 | { |
15300263 |
881 | uint8 *C; |
078eb4c0 |
882 | uint8 cc,zz; |
15300263 |
883 | uint32 vadr; |
c62d2810 |
884 | |
078eb4c0 |
885 | zz=rfraddr&0x1F; |
886 | PPU_hook_(0x2000|(rfraddr&0xFFF)); |
887 | cc=page[0x3c0+(zz>>2)+((rfraddr&0x380)>>4)]; |
888 | cc=((cc >> ((zz&2) + ((rfraddr&0x40)>>4))) &3) <<2; |
889 | vadr=(page[rfraddr&0x3ff]<<4)+vofs; |
c62d2810 |
890 | C = VRAMADR(vadr); |
891 | |
892 | #include "fceline.h" |
893 | |
078eb4c0 |
894 | PPU_hook_(vadr); |
c62d2810 |
895 | |
078eb4c0 |
896 | if((rfraddr&0x1f)==0x1f) { |
897 | rfraddr^=0x41F; |
898 | page = vnapage[(rfraddr>>10)&3]; |
899 | } else |
900 | rfraddr++; |
c62d2810 |
901 | } |
078eb4c0 |
902 | RefreshAddr = rfraddr; |
4a1bf31b |
903 | } |
904 | |
9115e7d2 |
905 | static void RefreshLine_normal(uint8 *P, uint32 vofs) // vofs is 0x107 max |
4a1bf31b |
906 | { |
907 | int8 X1; |
9115e7d2 |
908 | uint32 rfraddr = RefreshAddr; |
909 | uint8 *page = vnapage[(rfraddr>>10)&3]; |
910 | uint32 cc2=0; |
4a1bf31b |
911 | |
9115e7d2 |
912 | if ((rfraddr&0xc)!=0) |
913 | cc2=*(uint32 *) (page + ((rfraddr&0x380)>>4) + ((rfraddr&0x10)>>2) + 0x3c0); |
914 | |
915 | for (X1=33;X1;X1--,P+=8) |
c62d2810 |
916 | { |
9115e7d2 |
917 | uint8 cc,*C; |
c62d2810 |
918 | uint32 vadr; |
919 | |
9115e7d2 |
920 | vadr=(page[rfraddr&0x3ff]<<4)+vofs; |
c62d2810 |
921 | C = VRAMADR(vadr); |
9115e7d2 |
922 | if ((rfraddr&0xc)==0) |
923 | cc2=*(uint32 *) (page + ((rfraddr&0x380)>>4) + ((rfraddr&0x10)>>2) + 0x3c0); |
924 | cc=((cc2 >> ((rfraddr&2) + ((rfraddr&0x40)>>4) + ((rfraddr&0xc)<<1))) & 3) << 2; |
4a1bf31b |
925 | |
9115e7d2 |
926 | #include "fceline.h" |
15300263 |
927 | |
9115e7d2 |
928 | if((rfraddr&0x1f)==0x1f) { |
929 | rfraddr^=0x41F; |
930 | page = vnapage[(rfraddr>>10)&3]; |
931 | } else |
932 | rfraddr++; |
c62d2810 |
933 | } |
9115e7d2 |
934 | RefreshAddr = rfraddr; |
4a1bf31b |
935 | } |
c62d2810 |
936 | |
4a1bf31b |
937 | static void SetRefreshLine(void) |
938 | { |
939 | if(MMC5Hack && geniestage!=1) |
940 | { |
941 | if(MMC5HackCHRMode==0 && (MMC5HackSPMode&0x80)) |
942 | { |
943 | if (RefreshLine != RefreshLine_MMC5Hack1) printf("set refr RefreshLine_MMC5Hack1\n"); |
944 | RefreshLine = RefreshLine_MMC5Hack1; |
945 | } |
946 | else if(MMC5HackCHRMode==1 && (MMC5HackSPMode&0x80)) |
947 | { |
948 | if (RefreshLine != RefreshLine_MMC5Hack2) printf("set refr RefreshLine_MMC5Hack2\n"); |
949 | RefreshLine = RefreshLine_MMC5Hack2; |
950 | } |
951 | else if(MMC5HackCHRMode==1) |
952 | { |
953 | if (RefreshLine != RefreshLine_MMC5Hack3) printf("set refr RefreshLine_MMC5Hack3\n"); |
954 | RefreshLine = RefreshLine_MMC5Hack3; |
955 | } |
956 | else |
957 | { |
958 | if (RefreshLine != RefreshLine_MMC5Hack4) printf("set refr RefreshLine_MMC5Hack4\n"); |
959 | RefreshLine = RefreshLine_MMC5Hack4; |
960 | } |
961 | } // End if(MMC5Hack) |
962 | else if(PPU_hook) |
963 | { |
964 | if (RefreshLine != RefreshLine_PPU_hook) printf("set refr RefreshLine_PPU_hook\n"); |
965 | RefreshLine = RefreshLine_PPU_hook; |
966 | } |
967 | else |
968 | { |
969 | if (RefreshLine != RefreshLine_normal) printf("set refr RefreshLine_normal\n"); |
970 | RefreshLine = RefreshLine_normal; |
971 | } |
c62d2810 |
972 | } |
973 | |
4fdfab07 |
974 | static INLINE |
937bf65b |
975 | void Fixit2(void) |
c62d2810 |
976 | { |
977 | if(ScreenON || SpriteON) |
978 | { |
979 | uint32 rad=RefreshAddr; |
980 | rad&=0xFBE0; |
981 | rad|=TempAddr&0x041f; |
982 | RefreshAddr=rad; |
983 | //PPU_hook(RefreshAddr,-1); |
984 | } |
985 | } |
986 | |
4fdfab07 |
987 | static INLINE |
937bf65b |
988 | void Fixit1(void) |
c62d2810 |
989 | { |
990 | if(ScreenON || SpriteON) |
991 | { |
992 | uint32 rad=RefreshAddr; |
993 | |
994 | if((rad&0x7000)==0x7000) |
995 | { |
996 | rad^=0x7000; |
997 | if((rad&0x3E0)==0x3A0) |
998 | { |
999 | rad^=0x3A0; |
1000 | rad^=0x800; |
1001 | } |
1002 | else |
1003 | { |
1004 | if((rad&0x3E0)==0x3e0) |
1005 | rad^=0x3e0; |
1006 | else rad+=0x20; |
1007 | } |
1008 | } |
1009 | else |
1010 | rad+=0x1000; |
1011 | RefreshAddr=rad; |
1012 | //PPU_hook(RefreshAddr,-1); |
1013 | } |
1014 | } |
1015 | |
937bf65b |
1016 | |
5232c20c |
1017 | // ============================// |
1018 | // end of new code |
1019 | // ===========================// |
c62d2810 |
1020 | |
1021 | void ResetMapping(void) |
1022 | { |
1023 | int x; |
1024 | |
1025 | SetReadHandler(0x0000,0xFFFF,ANull); |
1026 | SetWriteHandler(0x0000,0xFFFF,BNull); |
1027 | |
1028 | SetReadHandler(0,0x7FF,ARAML); |
1029 | SetWriteHandler(0,0x7FF,BRAML); |
1030 | |
1031 | SetReadHandler(0x800,0x1FFF,ARAMH); /* Part of a little */ |
1032 | SetWriteHandler(0x800,0x1FFF,BRAMH); /* hack for a small speed boost. */ |
1033 | |
1034 | for(x=0x2000;x<0x4000;x+=8) |
1035 | { |
1036 | ARead[x]=A200x; |
1037 | BWrite[x]=B2000; |
1038 | ARead[x+1]=A200x; |
1039 | BWrite[x+1]=B2001; |
1040 | ARead[x+2]=A2002; |
1041 | BWrite[x+2]=B2002; |
1042 | ARead[x+3]=A200x; |
1043 | BWrite[x+3]=B2003; |
1044 | ARead[x+4]=A200x; |
1045 | BWrite[x+4]=B2004; |
1046 | ARead[x+5]=A200x; |
1047 | BWrite[x+5]=B2005; |
1048 | ARead[x+6]=A200x; |
1049 | BWrite[x+6]=B2006; |
1050 | ARead[x+7]=A2007; |
1051 | BWrite[x+7]=B2007; |
1052 | } |
1053 | |
1054 | BWrite[0x4014]=B4014; |
1055 | SetNESSoundMap(); |
1056 | InitializeInput(); |
1057 | } |
1058 | |
1059 | int GameLoaded=0; |
1060 | void CloseGame(void) |
1061 | { |
c4980f9e |
1062 | FCEUI_StopMovie(); |
c62d2810 |
1063 | if(GameLoaded) |
1064 | { |
1065 | if(FCEUGameInfo.type!=GIT_NSF) |
92764e62 |
1066 | FCEU_FlushGameCheats(0,0); |
c62d2810 |
1067 | #ifdef NETWORK |
1068 | if(FSettings.NetworkPlay) KillNetplay(); |
15300263 |
1069 | #endif |
c4980f9e |
1070 | GameInterface(GI_CLOSE, 0); |
c62d2810 |
1071 | CloseGenie(); |
1072 | GameLoaded=0; |
1073 | } |
1074 | } |
1075 | |
1076 | void ResetGameLoaded(void) |
1077 | { |
1078 | if(GameLoaded) CloseGame(); |
1079 | GameStateRestore=0; |
1080 | PPU_hook=0; |
d97315ac |
1081 | GameHBIRQHook=GameHBIRQHook2=0; |
c62d2810 |
1082 | GameExpSound.Fill=0; |
1083 | GameExpSound.RChange=0; |
1084 | if(GameExpSound.Kill) |
1085 | GameExpSound.Kill(); |
1086 | GameExpSound.Kill=0; |
1087 | MapIRQHook=0; |
1088 | MMC5Hack=0; |
1089 | PAL&=1; |
1090 | pale=0; |
1091 | |
1092 | FCEUGameInfo.name=0; |
1093 | FCEUGameInfo.type=GIT_CART; |
1094 | FCEUGameInfo.vidsys=GIV_USER; |
1095 | FCEUGameInfo.input[0]=FCEUGameInfo.input[1]=-1; |
1096 | FCEUGameInfo.inputfc=-1; |
1097 | } |
1098 | |
5a2aa426 |
1099 | char lastLoadedGameName [2048]; |
989672f4 |
1100 | int LoadGameLastError = 0; |
d97315ac |
1101 | int UNIFLoad(const char *name, int fp); |
1102 | int iNESLoad(const char *name, int fp); |
1103 | int FDSLoad(const char *name, int fp); |
1104 | int NSFLoad(int fp); |
5a2aa426 |
1105 | |
c62d2810 |
1106 | FCEUGI *FCEUI_LoadGame(char *name) |
1107 | { |
5a2aa426 |
1108 | char name2[512]; |
1109 | int have_movie = 0; |
c62d2810 |
1110 | int fp; |
1111 | |
b2b95d2e |
1112 | //Exit=1; |
989672f4 |
1113 | LoadGameLastError = 0; |
c62d2810 |
1114 | ResetGameLoaded(); |
1115 | |
5a2aa426 |
1116 | strncpy(name2, name, sizeof(name2)); |
1117 | name2[sizeof(name2)-1] = 0; |
1118 | |
1119 | fp=FCEU_fopen(name2,"rb"); |
c62d2810 |
1120 | if(!fp) |
1121 | { |
1122 | FCEU_PrintError("Error opening \"%s\"!",name); |
989672f4 |
1123 | LoadGameLastError = 1; |
c62d2810 |
1124 | return 0; |
1125 | } |
1126 | |
5a2aa426 |
1127 | { |
1128 | char *p = name2 + strlen(name2) - 4; |
1129 | if (strcmp(p, ".fcm") == 0) |
1130 | { |
1131 | // movie detected |
1132 | printf("movie detected\n"); |
1133 | FCEU_fclose(fp); |
1134 | *p = 0; |
1135 | fp=FCEU_fopen(name2,"rb"); |
b2b95d2e |
1136 | if (!fp && p - name2 > 2) p[-2] = 0; |
1137 | fp=FCEU_fopen(name2,"rb"); |
5a2aa426 |
1138 | if (!fp) { |
1139 | printf("no ROM for movie\n"); |
989672f4 |
1140 | LoadGameLastError = 2; |
5a2aa426 |
1141 | return 0; |
1142 | } |
1143 | have_movie = 1; |
1144 | } |
1145 | } |
1146 | |
5a2aa426 |
1147 | GetFileBase(name2); |
1148 | if(iNESLoad(name2,fp)) |
c62d2810 |
1149 | goto endlseq; |
1150 | if(NSFLoad(fp)) |
1151 | goto endlseq; |
5a2aa426 |
1152 | if(FDSLoad(name2,fp)) |
c62d2810 |
1153 | goto endlseq; |
5a2aa426 |
1154 | if(UNIFLoad(name2,fp)) |
c62d2810 |
1155 | goto endlseq; |
1156 | |
1157 | FCEU_PrintError("An error occurred while loading the file."); |
1158 | FCEU_fclose(fp); |
989672f4 |
1159 | // format handlers may set LoadGameLastError themselves. |
1160 | if (LoadGameLastError == 0) LoadGameLastError = 3; |
c62d2810 |
1161 | return 0; |
1162 | |
1163 | endlseq: |
1164 | FCEU_fclose(fp); |
15300263 |
1165 | GameLoaded=1; |
c62d2810 |
1166 | |
1167 | FCEU_ResetVidSys(); |
1168 | if(FCEUGameInfo.type!=GIT_NSF) |
1169 | if(FSettings.GameGenie) |
1170 | OpenGenie(); |
1171 | |
1172 | PowerNES(); |
1173 | #ifdef NETWORK |
1174 | if(FSettings.NetworkPlay) InitNetplay(); |
1175 | #endif |
1176 | SaveStateRefresh(); |
1177 | if(FCEUGameInfo.type!=GIT_NSF) |
1178 | { |
92764e62 |
1179 | FCEU_LoadGamePalette(); |
1180 | FCEU_LoadGameCheats(0); |
c62d2810 |
1181 | } |
15300263 |
1182 | |
c62d2810 |
1183 | FCEU_ResetPalette(); |
1184 | Exit=0; |
5a2aa426 |
1185 | |
1186 | if (have_movie) |
1187 | FCEUI_LoadMovie(name, 1); |
989672f4 |
1188 | |
1189 | strcpy(lastLoadedGameName, name2); |
1190 | |
c62d2810 |
1191 | return(&FCEUGameInfo); |
1192 | } |
1193 | |
1194 | |
1195 | void FCEU_ResetVidSys(void) |
1196 | { |
1197 | int w; |
1198 | |
1199 | if(FCEUGameInfo.vidsys==GIV_NTSC) |
1200 | w=0; |
1201 | else if(FCEUGameInfo.vidsys==GIV_PAL) |
1202 | w=1; |
1203 | else |
1204 | w=FSettings.PAL; |
1205 | |
1206 | if(w) |
1207 | { |
1208 | PAL=1; |
c62d2810 |
1209 | FSettings.FirstSLine=FSettings.UsrFirstSLine[1]; |
1210 | FSettings.LastSLine=FSettings.UsrLastSLine[1]; |
1211 | } |
1212 | else |
1213 | { |
1214 | PAL=0; |
c62d2810 |
1215 | FSettings.FirstSLine=FSettings.UsrFirstSLine[0]; |
1216 | FSettings.LastSLine=FSettings.UsrLastSLine[0]; |
1217 | } |
0b65fdb3 |
1218 | printf("ResetVidSys: PAL = %i\n", PAL); |
c62d2810 |
1219 | SetSoundVariables(); |
1220 | } |
1221 | |
1222 | int FCEUI_Initialize(void) |
1223 | { |
1224 | if(!InitVirtualVideo()) |
1225 | return 0; |
1226 | memset(&FSettings,0,sizeof(FSettings)); |
1227 | FSettings.UsrFirstSLine[0]=8; |
1228 | FSettings.UsrFirstSLine[1]=0; |
1229 | FSettings.UsrLastSLine[0]=FSettings.UsrLastSLine[1]=239; |
92764e62 |
1230 | FSettings.SoundVolume=100; |
c62d2810 |
1231 | return 1; |
1232 | } |
1233 | |
ea80a45b |
1234 | void MMC5_hb(int); /* Ugh ugh ugh. */ |
e328100e |
1235 | static void DoLine(void) |
c62d2810 |
1236 | { |
e328100e |
1237 | uint8 *target=XBuf+scanline*320+32; |
1238 | |
1239 | LineUpdate(target); |
c62d2810 |
1240 | |
ea80a45b |
1241 | if(MMC5Hack && (ScreenON || SpriteON)) MMC5_hb(scanline); |
1242 | |
e328100e |
1243 | X6502_Run(256); |
1244 | |
f12b1f31 |
1245 | // check: Battletoads & Double Dragon, Addams Family |
1246 | // sky glitches in SMB1 if done wrong |
e328100e |
1247 | FakedLineUpdate(); |
1248 | |
1249 | #ifdef FRAMESKIP |
1250 | if(!FSkip) |
1251 | #endif |
21afaa36 |
1252 | if(scanline>=FSettings.FirstSLine && scanline<=FSettings.LastSLine) |
1253 | { |
1254 | if(SpriteON && spork) |
1255 | CopySprites(target); |
e328100e |
1256 | |
21afaa36 |
1257 | LineUpdateEnd(target); |
1258 | } |
e328100e |
1259 | sphitx=0x100; |
1260 | |
1261 | if(ScreenON || SpriteON) |
1262 | FetchSpriteData(); |
1263 | |
1264 | // DoHBlank(); |
1265 | if(GameHBIRQHook && (ScreenON || SpriteON) && ((PPU[0]&0x38)!=0x18)) |
15300263 |
1266 | { |
e328100e |
1267 | X6502_Run(6); |
1268 | Fixit2(); |
1269 | X6502_Run(4); |
1270 | GameHBIRQHook(); |
1271 | X6502_Run(85-10-16); |
c62d2810 |
1272 | } |
1273 | else |
1274 | { |
e328100e |
1275 | X6502_Run(6); // Tried 65, caused problems with Slalom(maybe others) |
1276 | Fixit2(); |
1277 | X6502_Run(85-6-16); |
c62d2810 |
1278 | } |
e328100e |
1279 | |
1280 | if(SpriteON) |
1281 | RefreshSprites(); |
1282 | if(GameHBIRQHook2 && (ScreenON || SpriteON)) |
1283 | GameHBIRQHook2(); |
1284 | scanline++; |
1285 | if (scanline<240) |
1286 | ResetRL(XBuf+scanline*320+32); |
1287 | X6502_Run(16); |
c62d2810 |
1288 | } |
c62d2810 |
1289 | |
e328100e |
1290 | |
c62d2810 |
1291 | void EmLoop(void) |
1292 | { |
1293 | for(;;) |
1294 | { |
ea80a45b |
1295 | int x; |
92e249b1 |
1296 | uint32 scanlines_per_frame = PAL ? 312 : 262; |
4fdfab07 |
1297 | UpdateInput(); |
92764e62 |
1298 | FCEU_ApplyPeriodicCheats(); |
4fdfab07 |
1299 | |
1300 | // FCEUPPU_Loop: |
1301 | if(ppudead) /* Needed for Knight Rider, possibly others. */ |
1302 | { |
6587f346 |
1303 | //memset(XBuf, 0, 320*240); |
4fdfab07 |
1304 | X6502_Run(scanlines_per_frame*(256+85)); |
1305 | ppudead--; |
1306 | goto update; |
1307 | } |
ea80a45b |
1308 | |
c62d2810 |
1309 | X6502_Run(256+85); |
1310 | |
1311 | PPU[2]|=0x80; |
1312 | PPU[3]=PPUSPL=0; /* Not sure if this is correct. According |
1313 | to Matt Conte and my own tests, it is. Timing is probably |
1314 | off, though. NOTE: Not having this here |
1315 | breaks a Super Donkey Kong game. */ |
1316 | |
1317 | X6502_Run(12); /* I need to figure out the true nature and length |
15300263 |
1318 | of this delay. |
c62d2810 |
1319 | */ |
1320 | if(FCEUGameInfo.type==GIT_NSF) |
ea80a45b |
1321 | DoNSFFrame(); |
c62d2810 |
1322 | else if(VBlankON) |
1323 | TriggerNMI(); |
1324 | |
655f8df0 |
1325 | // Note: this is needed for asm core |
1326 | // Warning: using 'scanline' var here breaks Castlevania III |
1327 | { |
1328 | int lines; |
1329 | X6502_Run(256+85-12); |
1330 | for (lines=scanlines_per_frame-242-1;lines;lines--) |
1331 | X6502_Run(256+85); |
1332 | } |
1333 | // X6502_Run((scanlines_per_frame-242)*(256+85)-12); |
c62d2810 |
1334 | PPU_status&=0x1f; |
c62d2810 |
1335 | X6502_Run(256); |
ea80a45b |
1336 | |
c62d2810 |
1337 | { |
c62d2810 |
1338 | if(ScreenON || SpriteON) |
d97315ac |
1339 | { |
c62d2810 |
1340 | if(GameHBIRQHook) |
1341 | GameHBIRQHook(); |
ea80a45b |
1342 | if(PPU_hook) |
1343 | for(x=0;x<42;x++) {PPU_hook(0x2000); PPU_hook(0);} // ugh |
d97315ac |
1344 | if(GameHBIRQHook2) |
1345 | GameHBIRQHook2(); |
1346 | } |
c62d2810 |
1347 | |
4fdfab07 |
1348 | X6502_Run(85-16); |
c62d2810 |
1349 | |
4fdfab07 |
1350 | if(ScreenON || SpriteON) |
1351 | { |
1352 | RefreshAddr=TempAddr; |
1353 | if(PPU_hook) PPU_hook(RefreshAddr&0x3fff); |
1354 | } |
e328100e |
1355 | spork=0; |
1356 | ResetRL(XBuf+32); |
4fdfab07 |
1357 | |
1358 | X6502_Run(16-kook); |
1359 | kook ^= 1; |
c62d2810 |
1360 | } |
4fdfab07 |
1361 | |
c62d2810 |
1362 | if(FCEUGameInfo.type==GIT_NSF) |
937bf65b |
1363 | { |
6587f346 |
1364 | // run scanlines for asm core to fuction |
13624c8f |
1365 | for(scanline=0;scanline<240;scanline++) |
1366 | X6502_Run(256+85); |
937bf65b |
1367 | } |
c62d2810 |
1368 | else |
1369 | { |
1370 | int x,max,maxref; |
1371 | |
1372 | deemp=PPU[1]>>5; |
4a1bf31b |
1373 | SetRefreshLine(); |
4fdfab07 |
1374 | for(scanline=0;scanline<240;) // scanline is incremented in DoLine. Evil. :/ |
c62d2810 |
1375 | { |
1376 | deempcnt[deemp]++; |
e328100e |
1377 | DoLine(); |
c62d2810 |
1378 | } |
ea80a45b |
1379 | if(MMC5Hack && (ScreenON || SpriteON)) MMC5_hb(scanline); |
c62d2810 |
1380 | for(x=1,max=0,maxref=0;x<7;x++) |
1381 | { |
1382 | if(deempcnt[x]>max) |
1383 | { |
1384 | max=deempcnt[x]; |
1385 | maxref=x; |
1386 | } |
1387 | deempcnt[x]=0; |
1388 | } |
c62d2810 |
1389 | SetNESDeemph(maxref,0); |
1390 | } |
1391 | |
4fdfab07 |
1392 | update: |
47d0839e |
1393 | if(Exit) |
1394 | { |
1395 | //CloseGame(); |
1396 | break; |
1397 | } |
1398 | |
c62d2810 |
1399 | { |
1400 | int ssize; |
1401 | |
1402 | ssize=FlushEmulateSound(); |
1403 | |
4fdfab07 |
1404 | timestampbase += timestamp; |
1405 | timestamp = 0; |
1406 | |
c62d2810 |
1407 | #ifdef FRAMESKIP |
1408 | if(FSkip) |
1409 | { |
1410 | FCEU_PutImageDummy(); |
1411 | FSkip--; |
5232c20c |
1412 | FCEUD_Update(0,WaveFinalMono,ssize); |
c62d2810 |
1413 | } |
1414 | else |
1415 | #endif |
1416 | { |
1417 | FCEU_PutImage(); |
5232c20c |
1418 | FCEUD_Update(XBuf+8,WaveFinalMono,ssize); |
c62d2810 |
1419 | } |
c62d2810 |
1420 | } |
1421 | |
4fdfab07 |
1422 | } // for |
c62d2810 |
1423 | } |
1424 | |
1425 | #ifdef FPS |
1426 | #include <sys/time.h> |
1427 | uint64 frcount; |
1428 | #endif |
1429 | void FCEUI_Emulate(void) |
1430 | { |
1431 | #ifdef FPS |
1432 | uint64 starttime,end; |
1433 | struct timeval tv; |
1434 | frcount=0; |
1435 | gettimeofday(&tv,0); |
1436 | starttime=((uint64)tv.tv_sec*1000000)+tv.tv_usec; |
1437 | #endif |
1438 | EmLoop(); |
1439 | |
1440 | #ifdef FPS |
1441 | // Probably won't work well on Windows port; for |
1442 | // debugging/speed testing. |
1443 | { |
1444 | uint64 w; |
1445 | int i,frac; |
1446 | gettimeofday(&tv,0); |
1447 | end=((uint64)tv.tv_sec*1000000)+tv.tv_usec; |
1448 | w=frcount*10000000000LL/(end-starttime); |
1449 | i=w/10000; |
1450 | frac=w-i*10000; |
1451 | printf("Average FPS: %d.%04d\n",i,frac); |
1452 | } |
1453 | #endif |
1454 | |
1455 | } |
1456 | |
1457 | void FCEUI_CloseGame(void) |
1458 | { |
1459 | Exit=1; |
1460 | } |
1461 | |
1462 | static void ResetPPU(void) |
1463 | { |
1464 | VRAMBuffer=PPU[0]=PPU[1]=PPU[2]=PPU[3]=0; |
1465 | PPUSPL=0; |
1466 | PPUGenLatch=0; |
1467 | RefreshAddr=TempAddr=0; |
1468 | vtoggle = 0; |
4fdfab07 |
1469 | ppudead = 2; |
1470 | kook = 0; |
c62d2810 |
1471 | } |
1472 | |
1473 | static void PowerPPU(void) |
1474 | { |
1475 | memset(NTARAM,0x00,0x800); |
1476 | memset(PALRAM,0x00,0x20); |
1477 | memset(SPRAM,0x00,0x100); |
1478 | ResetPPU(); |
1479 | } |
1480 | |
1481 | void ResetNES(void) |
1482 | { |
92764e62 |
1483 | if(!GameLoaded) return; |
c4980f9e |
1484 | GameInterface(GI_RESETM2, 0); |
c62d2810 |
1485 | ResetSound(); |
1486 | ResetPPU(); |
1487 | X6502_Reset(); |
1488 | } |
1489 | |
b8da43fb |
1490 | static void FCEU_MemoryRand(uint8 *ptr, uint32 size) |
1491 | { |
1492 | int x=0; |
1493 | while(size) |
1494 | { |
1495 | *ptr=(x&4)?0xFF:0x00; |
1496 | x++; |
1497 | size--; |
1498 | ptr++; |
1499 | } |
1500 | } |
1501 | |
15300263 |
1502 | void PowerNES(void) |
c62d2810 |
1503 | { |
1504 | if(!GameLoaded) return; |
1505 | |
1506 | FCEU_CheatResetRAM(); |
1507 | FCEU_CheatAddRAM(2,0,RAM); |
1508 | |
1509 | GeniePower(); |
1510 | |
b8da43fb |
1511 | #ifndef DEBUG_ASM_6502 |
1512 | FCEU_MemoryRand(RAM,0x800); |
1513 | #else |
c62d2810 |
1514 | memset(RAM,0x00,0x800); |
b8da43fb |
1515 | #endif |
c62d2810 |
1516 | ResetMapping(); |
c4980f9e |
1517 | GameInterface(GI_POWER, 0); |
c62d2810 |
1518 | PowerSound(); |
1519 | PowerPPU(); |
1520 | timestampbase=0; |
b8da43fb |
1521 | #ifdef ASM_6502 |
1522 | if (geniestage) |
1523 | GenieSetPages(0); |
1524 | #endif |
c62d2810 |
1525 | X6502_Power(); |
1526 | } |
1527 | |
890e37ba |
1528 | |
1529 | /* savestate stuff */ |
1530 | uint16 TempAddrT,RefreshAddrT; |
1531 | |
1532 | SFORMAT FCEUPPU_STATEINFO[]={ |
1533 | { NTARAM, 0x800, "NTAR"}, |
1534 | { PALRAM, 0x20, "PRAM"}, |
1535 | { SPRAM, 0x100, "SPRA"}, |
1536 | { PPU, 0x4, "PPUR"}, |
1537 | { &XOffset, 1, "XOFF"}, |
1538 | { &vtoggle, 1, "VTOG"}, |
1539 | { &RefreshAddrT, 2|RLSB, "RADD"}, |
1540 | { &TempAddrT, 2|RLSB, "TADD"}, |
1541 | { &VRAMBuffer, 1, "VBUF"}, |
1542 | { &PPUGenLatch, 1, "PGEN"}, |
1543 | // from 0.98.15 |
1544 | { &kook, 1, "KOOK"}, |
1545 | { &ppudead, 1, "DEAD"}, |
1546 | { &PPUSPL, 1, "PSPL"}, |
1547 | { 0 } |
1548 | }; |
1549 | |
1550 | |