palette ram reading fix for old ppu

This commit is contained in:
CaH4e3 2013-03-14 12:55:36 +00:00
parent 37b0f97f1d
commit 9d778993b7
1 changed files with 150 additions and 134 deletions

View File

@ -1,64 +1,63 @@
/* FCE Ultra - NES/Famicom Emulator
*
* Copyright notice for this file:
* Copyright (C) 1998 BERO
* Copyright (C) 2003 Xodnizel
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
*
* Copyright notice for this file:
* Copyright (C) 1998 BERO
* Copyright (C) 2003 Xodnizel
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include "types.h"
#include "x6502.h"
#include "fceu.h"
#include "ppu.h"
#include "nsf.h"
#include "sound.h"
#include "file.h"
#include "utils/endian.h"
#include "utils/memory.h"
#include "types.h"
#include "x6502.h"
#include "fceu.h"
#include "ppu.h"
#include "nsf.h"
#include "sound.h"
#include "file.h"
#include "utils/endian.h"
#include "utils/memory.h"
#include "cart.h"
#include "palette.h"
#include "state.h"
#include "video.h"
#include "input.h"
#include "driver.h"
#include "debug.h"
#include "cart.h"
#include "palette.h"
#include "state.h"
#include "video.h"
#include "input.h"
#include "driver.h"
#include "debug.h"
#define VBlankON (PPU[0] & 0x80) //Generate VBlank NMI
#define Sprite16 (PPU[0] & 0x20) //Sprites 8x16/8x8
#define BGAdrHI (PPU[0] & 0x10) //BG pattern adr $0000/$1000
#define SpAdrHI (PPU[0] & 0x08) //Sprite pattern adr $0000/$1000
#define INC32 (PPU[0] & 0x04) //auto increment 1/32
#define VBlankON (PPU[0] & 0x80) //Generate VBlank NMI
#define Sprite16 (PPU[0] & 0x20) //Sprites 8x16/8x8
#define BGAdrHI (PPU[0] & 0x10) //BG pattern adr $0000/$1000
#define SpAdrHI (PPU[0] & 0x08) //Sprite pattern adr $0000/$1000
#define INC32 (PPU[0] & 0x04) //auto increment 1/32
#define SpriteON (PPU[1] & 0x10) //Show Sprite
#define ScreenON (PPU[1] & 0x08) //Show screen
#define PPUON (PPU[1] & 0x18) //PPU should operate
#define GRAYSCALE (PPU[1] & 0x01) //Grayscale (AND palette entries with 0x30)
#define SpriteON (PPU[1] & 0x10) //Show Sprite
#define ScreenON (PPU[1] & 0x08) //Show screen
#define PPUON (PPU[1] & 0x18) //PPU should operate
#define GRAYSCALE (PPU[1] & 0x01) //Grayscale (AND palette entries with 0x30)
#define SpriteLeft8 (PPU[1] & 0x04)
#define BGLeft8 (PPU[1] & 0x02)
#define SpriteLeft8 (PPU[1] & 0x04)
#define BGLeft8 (PPU[1] & 0x02)
#define PPU_status (PPU[2])
#define PPU_status (PPU[2])
#define Pal (PALRAM)
#define Pal (PALRAM)
static void FetchSpriteData(void);
static void RefreshLine(int lastpixel);
@ -146,21 +145,21 @@ uint8 idleSynch = 1;
//uses the internal counters concept at http://nesdev.icequake.net/PPU%20addressing.txt
struct PPUREGS {
//normal clocked regs. as the game can interfere with these at any time, they need to be savestated
uint32 fv; //3
uint32 v; //1
uint32 h; //1
uint32 vt; //5
uint32 ht; //5
uint32 fv; //3
uint32 v; //1
uint32 h; //1
uint32 vt; //5
uint32 ht; //5
//temp unlatched regs (need savestating, can be written to at any time)
uint32 _fv, _v, _h, _vt, _ht;
//other regs that need savestating
uint32 fh; //3 (horz scroll)
uint32 s; //1 ($2000 bit 4: "Background pattern table address (0: $0000; 1: $1000)")
uint32 fh; //3 (horz scroll)
uint32 s; //1 ($2000 bit 4: "Background pattern table address (0: $0000; 1: $1000)")
//other regs that don't need saving
uint32 par; //8 (sort of a hack, just stored in here, but not managed by this system)
uint32 par; //8 (sort of a hack, just stored in here, but not managed by this system)
//cached state data. these are always reset at the beginning of a frame and don't need saving
//but just to be safe, we're gonna save it
@ -207,8 +206,8 @@ struct PPUREGS {
fv++;
int fv_overflow = (fv >> 3);
vt += fv_overflow;
vt &= 31; //fixed tecmo super bowl
if (vt == 30 && fv_overflow == 1) { //caution here (only do it at the exact instant of overflow) fixes p'radikus conflict
vt &= 31; //fixed tecmo super bowl
if (vt == 30 && fv_overflow == 1) { //caution here (only do it at the exact instant of overflow) fixes p'radikus conflict
v++;
vt = 0;
}
@ -266,7 +265,6 @@ struct PPUREGS {
}
} ppur;
static void makeppulut(void) {
int x;
int y;
@ -275,23 +273,19 @@ static void makeppulut(void) {
for (x = 0; x < 256; x++) {
ppulut1[x] = 0;
for (y = 0; y < 8; y++) {
for (y = 0; y < 8; y++)
ppulut1[x] |= ((x >> (7 - y)) & 1) << (y * 4);
}
ppulut2[x] = ppulut1[x] << 1;
}
for (cc = 0; cc < 16; cc++) {
for (xo = 0; xo < 8; xo++) {
ppulut3[ xo | (cc << 3) ] = 0;
ppulut3[xo | (cc << 3)] = 0;
for (pixel = 0; pixel < 8; pixel++) {
int shiftr;
shiftr = (pixel + xo) / 8;
shiftr *= 2;
ppulut3[ xo | (cc << 3) ] |= ((cc >> shiftr) & 3) << (2 + pixel * 4);
ppulut3[xo | (cc << 3)] |= ((cc >> shiftr) & 3) << (2 + pixel * 4);
}
}
}
@ -345,8 +339,8 @@ static uint32 scanlines_per_frame;
uint8 PPU[4];
uint8 PPUSPL;
uint8 NTARAM[0x800], PALRAM[0x20], SPRAM[0x100], SPRBUF[0x100];
uint8 UPALRAM[0x03]; //for 0x4/0x8/0xC addresses in palette, the ones in
//0x20 are 0 to not break fceu rendering.
uint8 UPALRAM[0x03];//for 0x4/0x8/0xC addresses in palette, the ones in
//0x20 are 0 to not break fceu rendering.
#define MMC5SPRVRAMADR(V) &MMC5SPRVPage[(V) >> 10][(V)]
@ -357,7 +351,7 @@ uint8 UPALRAM[0x03]; //for 0x4/0x8/0xC addresses in palette, the ones in
//in mmc5 docs
uint8 * MMC5BGVRAMADR(uint32 V) {
if (!Sprite16) {
extern uint8 mmc5ABMode; /* A=0, B=1 */
extern uint8 mmc5ABMode; /* A=0, B=1 */
if (mmc5ABMode == 0)
return MMC5SPRVRAMADR(V);
else
@ -374,7 +368,7 @@ uint8* FCEUPPU_GetCHR(uint32 vadr, uint32 refreshaddr) {
if (MMC5HackCHRMode == 1) {
uint8 *C = MMC5HackVROMPTR;
C += (((MMC5HackExNTARAMPtr[refreshaddr & 0x3ff]) & 0x3f & MMC5HackVROMMask) << 12) + (vadr & 0xfff);
C += (MMC50x5130 & 0x3) << 18; //11-jun-2009 for kuja_killer
C += (MMC50x5130 & 0x3) << 18; //11-jun-2009 for kuja_killer
return C;
} else {
return MMC5BGVRAMADR(vadr);
@ -527,7 +521,7 @@ static DECLFR(A2004) {
// to 0xFF
if (ppur.status.cycle < 64)
return spr_read.ret = 0xFF;
else{
else {
for (int i = spr_read.last;
i != ppur.status.cycle; ++i) {
if (i < 256) {
@ -649,7 +643,7 @@ static DECLFR(A2004) {
}
}
static DECLFR(A200x) { /* Not correct for $2004 reads. */
static DECLFR(A200x) { /* Not correct for $2004 reads. */
FCEUPPU_LineUpdate();
return PPUGenLatch;
}
@ -699,20 +693,41 @@ static DECLFR(A2007) {
} else {
FCEUPPU_LineUpdate();
ret = VRAMBuffer;
if (tmp >= 0x3F00) { // Palette RAM tied directly to the output data, without VRAM buffer
if (!(tmp & 3)) {
if (!(tmp & 0xC))
ret = PALRAM[0x00];
else
ret = UPALRAM[((tmp & 0xC) >> 2) - 1];
} else
ret = PALRAM[tmp & 0x1F];
if (GRAYSCALE)
ret &= 0x30;
#ifdef FCEUDEF_DEBUGGER
if (!fceuindbg)
#endif
{
if ((tmp - 0x1000) < 0x2000)
VRAMBuffer = VPage[(tmp - 0x1000) >> 10][tmp - 0x1000];
else
VRAMBuffer = vnapage[((tmp - 0x1000) >> 10) & 0x3][(tmp - 0x1000) & 0x3FF];
if (PPU_hook) PPU_hook(tmp);
}
} else {
ret = VRAMBuffer;
#ifdef FCEUDEF_DEBUGGER
if (!fceuindbg)
#endif
{
if (PPU_hook) PPU_hook(tmp);
PPUGenLatch = VRAMBuffer;
if (tmp < 0x2000) {
if (debug_loggingCD)
LogAddress = GetCHRAddress(tmp);
VRAMBuffer = VPage[tmp >> 10][tmp];
} else if (tmp < 0x3F00) {
VRAMBuffer = vnapage[(tmp >> 10) & 0x3][tmp & 0x3FF];
#ifdef FCEUDEF_DEBUGGER
if (!fceuindbg)
#endif
{
if (PPU_hook) PPU_hook(tmp);
PPUGenLatch = VRAMBuffer;
if (tmp < 0x2000) {
if (debug_loggingCD)
LogAddress = GetCHRAddress(tmp);
VRAMBuffer = VPage[tmp >> 10][tmp];
} else if (tmp < 0x3F00)
VRAMBuffer = vnapage[(tmp >> 10) & 0x3][tmp & 0x3FF];
}
}
#ifdef FCEUDEF_DEBUGGER
@ -866,19 +881,20 @@ static DECLFW(B2007) {
RefreshAddr = ppur.get_2007access();
} else {
PPUGenLatch = V;
if (tmp >= 0x3F00) {
// hmmm....
if (!(tmp & 0xf))
PALRAM[0x00] = PALRAM[0x04] = PALRAM[0x08] = PALRAM[0x0C] = V & 0x3F;
else
if (tmp & 3)
PALRAM[(tmp & 0x1f)] = V & 0x3f;
} else if (tmp < 0x2000) {
if (tmp < 0x2000) {
if (PPUCHRRAM & (1 << (tmp >> 10)))
VPage[tmp >> 10][tmp] = V;
} else {
} else if (tmp < 0x3F00) {
if (PPUNTARAM & (1 << ((tmp & 0xF00) >> 10)))
vnapage[((tmp & 0xF00) >> 10)][tmp & 0x3FF] = V;
} else {
if (!(tmp & 3)) {
if (!(tmp & 0xC))
PALRAM[0x00] = PALRAM[0x04] = PALRAM[0x08] = PALRAM[0x0C] = V & 0x3F;
else
UPALRAM[((tmp & 0xC) >> 2) - 1] = V & 0x3F;
} else
PALRAM[tmp & 0x1F] = V & 0x3F;
}
if (INC32)
RefreshAddr += 32;
@ -903,7 +919,7 @@ static DECLFW(B4014) {
static uint8 *Pline, *Plinef;
static int firsttile;
int linestartts; //no longer static so the debugger can see it
int linestartts; //no longer static so the debugger can see it
static int tofix = 0;
static void ResetRL(uint8 *target) {
@ -1172,7 +1188,7 @@ static void Fixit1(void) {
}
}
void MMC5_hb(int); //Ugh ugh ugh.
void MMC5_hb(int); //Ugh ugh ugh.
static void DoLine(void) {
int x;
uint8 *target = XBuf + (scanline << 8);
@ -1182,7 +1198,7 @@ static void DoLine(void) {
X6502_Run(256);
EndRL();
if (!renderbg) { // User asked to not display background data.
if (!renderbg) {// User asked to not display background data.
uint32 tem;
uint8 col;
if (gNoBGFillColor == 0xFF)
@ -1196,7 +1212,7 @@ static void DoLine(void) {
if (SpriteON)
CopySprites(target);
if (ScreenON || SpriteON) { // Yes, very el-cheapo.
if (ScreenON || SpriteON) { // Yes, very el-cheapo.
if (PPU[1] & 0x01) {
for (x = 63; x >= 0; x--)
*(uint32*)&target[x << 2] = (*(uint32*)&target[x << 2]) & 0x30303030;
@ -1224,7 +1240,7 @@ static void DoLine(void) {
GameHBIRQHook();
X6502_Run(85 - 16 - 10);
} else {
X6502_Run(6); // Tried 65, caused problems with Slalom(maybe others)
X6502_Run(6); // Tried 65, caused problems with Slalom(maybe others)
Fixit2();
X6502_Run(85 - 6 - 16);
@ -1495,7 +1511,7 @@ static void RefreshSprites(void) {
if (J & 0x02) C[1] = VB[pixdata & 3];
pixdata >>= 4;
if (J & 0x01) C[0] = VB[pixdata];
} else{
} else {
if (J & 0x80) C[0] = VB[pixdata & 3];
pixdata >>= 4;
if (J & 0x40) C[1] = VB[pixdata & 3];
@ -1526,7 +1542,7 @@ static void CopySprites(uint8 *target) {
if (!spork) return;
spork = 0;
if (!rendersprites) return; //User asked to not display sprites.
if (!rendersprites) return; //User asked to not display sprites.
loopskie:
{
@ -1535,51 +1551,51 @@ static void CopySprites(uint8 *target) {
if (t != 0x80808080) {
#ifdef LSB_FIRST
if (!(t & 0x80)) {
if (!(t & 0x40) || (P[n] & 0x40)) // Normal sprite || behind bg sprite
if (!(t & 0x40) || (P[n] & 0x40)) // Normal sprite || behind bg sprite
P[n] = sprlinebuf[n];
}
if (!(t & 0x8000)) {
if (!(t & 0x4000) || (P[n + 1] & 0x40)) // Normal sprite || behind bg sprite
if (!(t & 0x4000) || (P[n + 1] & 0x40)) // Normal sprite || behind bg sprite
P[n + 1] = (sprlinebuf + 1)[n];
}
if (!(t & 0x800000)) {
if (!(t & 0x400000) || (P[n + 2] & 0x40)) // Normal sprite || behind bg sprite
if (!(t & 0x400000) || (P[n + 2] & 0x40)) // Normal sprite || behind bg sprite
P[n + 2] = (sprlinebuf + 2)[n];
}
if (!(t & 0x80000000)) {
if (!(t & 0x40000000) || (P[n + 3] & 0x40)) // Normal sprite || behind bg sprite
if (!(t & 0x40000000) || (P[n + 3] & 0x40)) // Normal sprite || behind bg sprite
P[n + 3] = (sprlinebuf + 3)[n];
}
#else
/* TODO: Simplify */
if (!(t & 0x80000000)) {
if (!(t & 0x40000000)) // Normal sprite
if (!(t & 0x40000000)) // Normal sprite
P[n] = sprlinebuf[n];
else if (P[n] & 64) // behind bg sprite
else if (P[n] & 64) // behind bg sprite
P[n] = sprlinebuf[n];
}
if (!(t & 0x800000)) {
if (!(t & 0x400000)) // Normal sprite
if (!(t & 0x400000)) // Normal sprite
P[n + 1] = (sprlinebuf + 1)[n];
else if (P[n + 1] & 64) // behind bg sprite
else if (P[n + 1] & 64) // behind bg sprite
P[n + 1] = (sprlinebuf + 1)[n];
}
if (!(t & 0x8000)) {
if (!(t & 0x4000)) // Normal sprite
if (!(t & 0x4000)) // Normal sprite
P[n + 2] = (sprlinebuf + 2)[n];
else if (P[n + 2] & 64) // behind bg sprite
else if (P[n + 2] & 64) // behind bg sprite
P[n + 2] = (sprlinebuf + 2)[n];
}
if (!(t & 0x80)) {
if (!(t & 0x40)) // Normal sprite
if (!(t & 0x40)) // Normal sprite
P[n + 3] = (sprlinebuf + 3)[n];
else if (P[n + 3] & 64) // behind bg sprite
else if (P[n + 3] & 64) // behind bg sprite
P[n + 3] = (sprlinebuf + 3)[n];
}
#endif
@ -1642,7 +1658,7 @@ void FCEUPPU_Power(void) {
BWrite[x + 2] = B2002;
ARead[x + 3] = A200x;
BWrite[x + 3] = B2003;
ARead[x + 4] = A2004; //A2004;
ARead[x + 4] = A2004; //A2004;
BWrite[x + 4] = B2004;
ARead[x + 5] = A200x;
BWrite[x + 5] = B2005;
@ -1678,11 +1694,11 @@ int FCEUPPU_Loop(int skip) {
X6502_Run(12);
if (GameInfo->type == GIT_NSF)
DoNSFFrame();
else{
else {
if (VBlankON)
TriggerNMI();
}
X6502_Run((scanlines_per_frame - 242) * (256 + 85) - 12); //-12);
X6502_Run((scanlines_per_frame - 242) * (256 + 85) - 12);
PPU_status &= 0x1f;
X6502_Run(256);
@ -1721,7 +1737,7 @@ int FCEUPPU_Loop(int skip) {
y = SPRAM[0];
y++;
PPU_status |= 0x20; // Fixes "Bee 52". Does it break anything?
PPU_status |= 0x20; // Fixes "Bee 52". Does it break anything?
if (GameHBIRQHook) {
X6502_Run(256);
for (scanline = 0; scanline < 240; scanline++) {
@ -1732,17 +1748,17 @@ int FCEUPPU_Loop(int skip) {
}
} else if (y < 240) {
X6502_Run((256 + 85) * y);
if (SpriteON) PPU_status |= 0x40; // Quick and very dirty hack.
if (SpriteON) PPU_status |= 0x40; // Quick and very dirty hack.
X6502_Run((256 + 85) * (240 - y));
} else
X6502_Run((256 + 85) * 240);
}
#endif
else{
else {
int x, max, maxref;
deemp = PPU[1] >> 5;
for (scanline = 0; scanline < 240; ) { //scanline is incremented in DoLine. Evil. :/
for (scanline = 0; scanline < 240; ) { //scanline is incremented in DoLine. Evil. :/
deempcnt[deemp]++;
DEBUG(FCEUD_UpdatePPUView(scanline, 1));
DoLine();
@ -1758,7 +1774,7 @@ int FCEUPPU_Loop(int skip) {
}
SetNESDeemph(maxref, 0);
}
} //else... to if(ppudead)
} //else... to if(ppudead)
#ifdef FRAMESKIP
if (skip) {
@ -1893,7 +1909,7 @@ struct BGData {
}
};
Record main[34]; //one at the end is junk, it can never be rendered
Record main[34]; //one at the end is junk, it can never be rendered
} bgdata;
static inline int PaletteAdjustPixel(int pixel) {
@ -1933,11 +1949,11 @@ int FCEUX_PPU_Loop(int skip) {
//Timing is probably off, though.
//NOTE: Not having this here breaks a Super Donkey Kong game.
PPU[3] = PPUSPL = 0;
const int delay = 20; //fceu used 12 here but I couldnt get it to work in marble madness and pirates.
const int delay = 20; //fceu used 12 here but I couldnt get it to work in marble madness and pirates.
ppur.status.sl = 241; //for sprite reads
ppur.status.sl = 241; //for sprite reads
runppu(delay); //X6502_Run(12);
runppu(delay); //X6502_Run(12);
if (VBlankON) TriggerNMI();
if (PAL)
runppu(70 * (kLineTime) - delay);
@ -1958,7 +1974,7 @@ int FCEUX_PPU_Loop(int skip) {
//if(PPUON)
// ppur.install_latches();
static uint8 oams[2][64][8]; //[7] turned to [8] for faster indexing
static uint8 oams[2][64][8];//[7] turned to [8] for faster indexing
static int oamcounts[2] = { 0, 0 };
static int oamslot = 0;
static int oamcount;
@ -2136,7 +2152,7 @@ int FCEUX_PPU_Loop(int skip) {
uint8* const oam = oams[scanslot][s];
uint32 line = yp - oam[0];
if (oam[2] & 0x80) //vflip
if (oam[2] & 0x80) //vflip
line = spriteHeight - line - 1;
uint32 patternNumber = oam[1];
@ -2181,7 +2197,7 @@ int FCEUX_PPU_Loop(int skip) {
//does not set SpriteON in the beginning but it does
//set the bg on so if using the conditional SpriteON the MMC3 counter
//the counter will never count and no IRQs will be fired so use PPUON
if (((PPU[0] & 0x38) != 0x18) && s == 2 && PPUON) { //SpriteON ) {
if (((PPU[0] & 0x38) != 0x18) && s == 2 && PPUON) {
//(The MMC3 scanline counter is based entirely on PPU A12, triggered on rising edges (after the line remains low for a sufficiently long period of time))
//http://nesdevwiki.org/wiki/index.php/Nintendo_MMC3
//test cases for timing: SMB3, Crystalis
@ -2245,7 +2261,7 @@ int FCEUX_PPU_Loop(int skip) {
//first one on every second frame, then this delay simply doesn't exist.
if (ppur.status.end_cycle == 341)
runppu(1);
} //scanline loop
} //scanline loop
if (MMC5Hack && PPUON) MMC5_hb(240);
@ -2254,7 +2270,7 @@ int FCEUX_PPU_Loop(int skip) {
framectr++;
}
finish:
finish:
FCEU_PutImage();
return 0;