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