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