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