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