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