1 /* FCE Ultra - NES/Famicom Emulator
3 * Copyright notice for this file:
4 * Copyright (C) 1998 BERO
5 * Copyright (C) 2003 Xodnizel
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.
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.
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
29 #include "sound.h" // TODO 098?
39 static void FetchSpriteData098(void);
40 static void FASTAPASS(1) RefreshLine098(int lastpixel);
41 static void RefreshSprites098(void);
42 static void CopySprites098(uint8 *target);
44 static void Fixit1(void);
45 static uint32 ppulut1[256];
46 static uint32 ppulut2[256];
47 static uint32 ppulut3[128];
49 static void makeppulut(void)
58 ppulut1[x]|=((x>>(7-y))&1)<<(y*4);
59 ppulut2[x]=ppulut1[x]<<1;
70 ppulut3[xo|(cc<<3)]=0;
71 for(pixel=0;pixel<8;pixel++)
76 ppulut3[xo|(cc<<3)]|=(( cc>>shiftr )&3)<<(2+pixel*4);
78 // printf("%08x\n",ppulut3[xo|(cc<<3)]);
85 // TODO: make this compatible with the new sound code
87 static void asmcpu_update(int32 cycles)
89 // some code from x6502.c
103 if((PSG[0x10]&0x80) && !(PSG[0x10]&0x40))
105 extern uint8 SIRQStat;
107 X6502_IRQBegin(FCEU_IQDPCM);
117 /* Color deemphasis emulation. Joy... */
118 static uint8 deemp=0;
119 static int deempcnt[8];
121 extern int maxsprites;
124 extern uint8 SPRAM[0x100];
125 extern uint8 SPRBUF[0x100];
128 #define MMC5SPRVRAMADR(V) &MMC5SPRVPage[(V)>>10][(V)]
129 #define MMC5BGVRAMADR(V) &MMC5BGVPage[(V)>>10][(V)]
130 #define VRAMADR(V) &VPage[(V)>>10][(V)]
137 FCEUPPU_LineUpdate098();
139 ret|=PPUGenLatch&0x1F;
149 static DECLFR(A200x) /* Not correct for $2004 reads. */
151 FCEUPPU_LineUpdate098();
160 FCEUPPU_LineUpdate098();
170 //printf("$%02x:$%02x\n",PPUSPL,V);
176 printf("%d, %02x\n",scanline,ret);
183 uint32 tmp=RefreshAddr&0x3FFF;
185 FCEUPPU_LineUpdate098();
190 if(PPU_hook) PPU_hook(tmp);
191 PPUGenLatch=VRAMBuffer;
194 VRAMBuffer=VPage[tmp>>10][tmp];
198 VRAMBuffer=vnapage[(tmp>>10)&0x3][tmp&0x3FF];
202 if(INC32) RefreshAddr+=32;
204 if(PPU_hook) PPU_hook(RefreshAddr&0x3fff);
211 // FCEU_printf("%04x:%02x, (%d) %02x, %02x\n",A,V,scanline,PPU[0],PPU_status);
213 FCEUPPU_LineUpdate098();
215 if(!(PPU[0]&0x80) && (V&0x80) && (PPU_status&0x80))
217 // FCEU_printf("Trigger NMI, %d, %d\n",timestamp,ppudead);
228 //printf("%04x:$%02x, %d\n",A,V,scanline);
229 FCEUPPU_LineUpdate098();
243 //printf("$%04x:$%02x, %d, %d\n",A,V,timestamp,scanline);
251 //printf("Wr: %04x:$%02x\n",A,V);
261 //printf("$%02x:$%02x\n",PPUSPL,V);
272 FCEUPPU_LineUpdate098();
293 FCEUPPU_LineUpdate098();
299 TempAddr|=(V&0x3f)<<8;
306 RefreshAddr=TempAddr;
308 PPU_hook(RefreshAddr);
309 //printf("%d, %04x\n",scanline,RefreshAddr);
316 uint32 tmp=RefreshAddr&0x3FFF;
322 PALRAM[0x00]=PALRAM[0x04]=PALRAM[0x08]=PALRAM[0x0C]=V&0x3F;
323 else if(tmp&3) PALRAM[(tmp&0x1f)]=V&0x3f;
327 if(PPUCHRRAM&(1<<(tmp>>10)))
328 VPage[tmp>>10][tmp]=V;
332 if(PPUNTARAM&(1<<((tmp&0xF00)>>10)))
333 vnapage[((tmp&0xF00)>>10)][tmp&0x3FF]=V;
335 // FCEU_printf("ppu (%04x) %04x:%04x %d, %d\n",X.PC,RefreshAddr,PPUGenLatch,scanline,timestamp);
336 if(INC32) RefreshAddr+=32;
338 if(PPU_hook) PPU_hook(RefreshAddr&0x3fff);
347 // X6502_DMW(0x2004,X6502_DMR(t+x));
349 B2004(0x2004,X.DB=ARead[t+x](t+x));
350 X6502_AddCycles(512);
353 #define PAL(c) ((c)+cc)
355 #define GETLASTPIXEL (PAL?((timestamp*48-linestartts)/15) : ((timestamp*48-linestartts)>>4) )
357 static uint8 *Pline=0,*Plinef;
358 static int firsttile;
359 static int linestartts;
362 static void ResetRL(uint8 *target)
364 memset(target,0xFF,256);
365 if(InputScanlineHook)
366 InputScanlineHook(0,0,0,0);
370 linestartts=timestamp*48+X.count;
372 FCEUPPU_LineUpdate098();
376 extern uint8 sprlinebuf[256+8];
378 void FCEUPPU_LineUpdate098(void)
387 static int rendis = 0;
389 void FCEUI_SetRenderDisable(int sprites, int bg)
391 //printf("%d, %d\n",sprites,bg);
394 if(sprites == 2) rendis ^= 1;
395 else rendis = (rendis &~1) | sprites?1:0;
399 if(bg == 2) rendis ^= 2;
400 else rendis = (rendis &~2) | bg?2:0;
404 static void CheckSpriteHit(int p);
406 static void EndRL(void)
416 static uint8 sphitdata;
418 static void CheckSpriteHit(int p)
423 if(sphitx==0x100) return;
425 for(x=sphitx;x<(sphitx+8) && x<l;x++)
427 if((sphitdata&(0x80>>(x-sphitx))) && !(Plinef[x]&64))
430 //printf("Ha: %d, %d, Hita: %d, %d, %d, %d, %d\n",p,p&~7,scanline,GETLASTPIXEL-16,&Plinef[x],Pline,Pline-Plinef);
431 //printf("%d\n",GETLASTPIXEL-16);
432 //if(Plinef[x] == 0xFF)
433 //printf("PL: %d, %02x\n",scanline, Plinef[x]);
439 static int spork=0; /* spork the world. Any sprites on this line?
440 Then this will be set to 1. Needed for zapper
441 emulation and *gasp* sprite emulation.
444 // lasttile is really "second to last tile."
445 static void FASTAPASS(1) RefreshLine098(int lastpixel)
447 static uint32 pshift[2];
448 static uint32 atlatch;
449 uint32 smorkus=RefreshAddr;
451 #define RefreshAddr smorkus
455 register uint8 *P=Pline;
456 int lasttile=lastpixel>>3;
458 static int norecurse=0; /* Yeah, recursion would be bad.
459 PPU_hook() functions can call
460 mirroring/chr bank switching functions,
461 which call FCEUPPU_LineUpdate, which call this
463 if(norecurse) return;
465 if(sphitx != 0x100 && !(PPU_status&0x40))
467 if((sphitx < (lastpixel-16)) && !(sphitx < ((lasttile - 2)*8)))
469 //printf("OK: %d\n",scanline);
475 if(lasttile>34) lasttile=34;
476 numtiles=lasttile-firsttile;
478 if(numtiles<=0) return;
484 vofs=((PPU[0]&0x10)<<8) | ((RefreshAddr>>12)&7);
486 if(!ScreenON && !SpriteON)
489 tem=Pal[0]|(Pal[0]<<8)|(Pal[0]<<16)|(Pal[0]<<24);
491 FCEU_dwmemset(Pline,tem,numtiles*8);
497 #define TOFIXNUM (272-0x4)
498 if(lastpixel>=TOFIXNUM && tofix)
504 if(InputScanlineHook && (lastpixel-16)>=0)
506 InputScanlineHook(Plinef,spork?sprlinebuf:0,linestartts,lasttile*8-16);
511 /* Priority bits, needed for sprite emulation. */
517 /* This high-level graphics MMC5 emulation code was written
518 for MMC5 carts in "CL" mode. It's probably not totally
519 correct for carts in "SL" mode.
523 if(MMC5Hack && geniestage!=1)
525 if(MMC5HackCHRMode==0 && (MMC5HackSPMode&0x80))
527 int tochange=MMC5HackSPMode&0x1F;
529 for(X1=firsttile;X1<lasttile;X1++)
531 if((tochange<=0 && MMC5HackSPMode&0x40) || (tochange>0 && !(MMC5HackSPMode&0x40)))
534 #include "pputile098.h"
539 #include "pputile098.h"
544 else if(MMC5HackCHRMode==1 && (MMC5HackSPMode&0x80))
546 int tochange=MMC5HackSPMode&0x1F;
550 #define PPUT_MMC5CHR1
551 for(X1=firsttile;X1<lasttile;X1++)
553 #include "pputile098.h"
558 else if(MMC5HackCHRMode==1)
560 #define PPUT_MMC5CHR1
561 for(X1=firsttile;X1<lasttile;X1++)
563 #include "pputile098.h"
569 for(X1=firsttile;X1<lasttile;X1++)
571 #include "pputile098.h"
580 for(X1=firsttile;X1<lasttile;X1++)
582 #include "pputile098.h"
589 for(X1=firsttile;X1<lasttile;X1++)
591 #include "pputile098.h"
598 /* Reverse changes made before. */
605 if(firsttile<=2 && 2<lasttile && !(PPU[1]&2))
608 tem=Pal[0]|(Pal[0]<<8)|(Pal[0]<<16)|(Pal[0]<<24);
610 *(uint32 *)Plinef=*(uint32 *)(Plinef+4)=tem;
617 tem=Pal[0]|(Pal[0]<<8)|(Pal[0]<<16)|(Pal[0]<<24);
620 tcount=lasttile-firsttile;
628 FCEU_dwmemset(Plinef+tstart*8,tem,tcount*8);
631 if(lastpixel>=TOFIXNUM && tofix)
638 //CheckSpriteHit(lasttile*8); //lasttile*8); //lastpixel);
640 CheckSpriteHit(lastpixel); /* This only works right because
641 of a hack earlier in this function.
643 if(InputScanlineHook && (lastpixel-16)>=0)
645 InputScanlineHook(Plinef,spork?sprlinebuf:0,linestartts,lasttile*8-16);
651 static INLINE void Fixit2(void)
653 if(ScreenON || SpriteON)
655 uint32 rad=RefreshAddr;
657 rad|=TempAddr&0x041f;
659 //PPU_hook(RefreshAddr);
660 //PPU_hook(RefreshAddr,-1);
664 static void Fixit1(void)
666 if(ScreenON || SpriteON)
668 uint32 rad=RefreshAddr;
670 if((rad&0x7000)==0x7000)
673 if((rad&0x3E0)==0x3A0)
680 if((rad&0x3E0)==0x3e0)
688 //PPU_hook(RefreshAddr); //,-1);
692 void MMC5_hb(int); /* Ugh ugh ugh. */
693 static void DoLine(void)
696 uint8 *target=XBuf+scanline*320+32;
698 if(MMC5Hack && (ScreenON || SpriteON)) MMC5_hb(scanline);
703 if(rendis & 2) /* User asked to not display background data. */
706 tem=Pal[0]|(Pal[0]<<8)|(Pal[0]<<16)|(Pal[0]<<24);
708 FCEU_dwmemset(target,tem,256);
712 CopySprites098(target);
714 if(ScreenON || SpriteON) // Yes, very el-cheapo.
719 *(uint32 *)&target[x<<2]=(*(uint32*)&target[x<<2])&0x30303030;
725 *(uint32 *)&target[x<<2]=((*(uint32*)&target[x<<2])&0x3f3f3f3f)|0xc0c0c0c0;
729 *(uint32 *)&target[x<<2]=(*(uint32*)&target[x<<2])|0x40404040;
732 *(uint32 *)&target[x<<2]=((*(uint32*)&target[x<<2])&0x3f3f3f3f)|0x80808080;
736 if(ScreenON || SpriteON)
737 FetchSpriteData098();
739 if(GameHBIRQHook && (ScreenON || SpriteON) && ((PPU[0]&0x38)!=0x18))
749 X6502_Run(6); // Tried 65, caused problems with Slalom(maybe others)
753 // A semi-hack for Star Trek: 25th Anniversary
754 if(GameHBIRQHook && (ScreenON || SpriteON) && ((PPU[0]&0x38)!=0x18))
760 if(GameHBIRQHook2 && (ScreenON || SpriteON))
765 ResetRL(XBuf+scanline*320+32);
782 static uint8 numsprites,SpriteBlurp;
783 static void FetchSpriteData098(void)
797 vofs=(unsigned int)(P0&0x8&(((P0&0x20)^0x20)>>2))<<9;
801 for(n=63;n>=0;n--,spr++)
803 if((unsigned int)(scanline-spr->y)>=H) continue;
804 //printf("%d, %u\n",scanline,(unsigned int)(scanline-spr->y));
815 t = (int)scanline-(spr->y);
818 vadr = ((spr->no&1)<<12) + ((spr->no&0xFE)<<4);
820 vadr = (spr->no<<4)+vofs;
835 /* Fix this geniestage hack */
836 if(MMC5Hack && geniestage!=1) C = MMC5SPRVRAMADR(vadr);
837 else C = VRAMADR(vadr);
845 *(uint32 *)&SPRBUF[ns<<2]=*(uint32 *)&dst;
857 for(n=63;n>=0;n--,spr++)
859 if((unsigned int)(scanline-spr->y)>=H) continue;
871 t = (int)scanline-(spr->y);
874 vadr = ((spr->no&1)<<12) + ((spr->no&0xFE)<<4);
876 vadr = (spr->no<<4)+vofs;
891 if(MMC5Hack) C = MMC5SPRVRAMADR(vadr);
892 else C = VRAMADR(vadr);
904 *(uint32 *)&SPRBUF[ns<<2]=*(uint32 *)&dst;
916 //printf("%d %d\n",scanline,ns);
917 if(ns>8) PPU_status|=0x20; /* Handle case when >8 sprites per
918 scanline option is enabled. */
921 for(n=0;n<(8-ns);n++)
931 static void RefreshSprites098(void)
937 if(!numsprites) return;
939 FCEU_dwmemset(sprlinebuf,0x80808080,256);
941 spr = (SPRB*)SPRBUF+numsprites;
943 for(n=numsprites;n>=0;n--,spr--)
946 //register uint32 pixdata asm ("eax");
947 //register uint8 J, atr;
949 register uint32 pixdata;
950 register uint8 J,atr;
957 pixdata=ppulut1[spr->ca[0]]|ppulut2[spr->ca[1]];
958 J=spr->ca[0]|spr->ca[1];
963 if(n==0 && SpriteBlurp && !(PPU_status&0x40))
968 sphitdata= ((J<<7)&0x80) |
979 VB = (PALRAM+0x10)+((atr&3)<<2);
985 if(J&0x80) C[7]=VB[pixdata&3]|0x40;
987 if(J&0x40) C[6]=VB[pixdata&3]|0x40;
989 if(J&0x20) C[5]=VB[pixdata&3]|0x40;
991 if(J&0x10) C[4]=VB[pixdata&3]|0x40;
993 if(J&0x08) C[3]=VB[pixdata&3]|0x40;
995 if(J&0x04) C[2]=VB[pixdata&3]|0x40;
997 if(J&0x02) C[1]=VB[pixdata&3]|0x40;
999 if(J&0x01) C[0]=VB[pixdata]|0x40;
1001 if(J&0x80) C[0]=VB[pixdata&3]|0x40;
1003 if(J&0x40) C[1]=VB[pixdata&3]|0x40;
1005 if(J&0x20) C[2]=VB[pixdata&3]|0x40;
1007 if(J&0x10) C[3]=VB[pixdata&3]|0x40;
1009 if(J&0x08) C[4]=VB[pixdata&3]|0x40;
1011 if(J&0x04) C[5]=VB[pixdata&3]|0x40;
1013 if(J&0x02) C[6]=VB[pixdata&3]|0x40;
1015 if(J&0x01) C[7]=VB[pixdata]|0x40;
1020 if(J&0x80) C[7]=VB[pixdata&3];
1022 if(J&0x40) C[6]=VB[pixdata&3];
1024 if(J&0x20) C[5]=VB[pixdata&3];
1026 if(J&0x10) C[4]=VB[pixdata&3];
1028 if(J&0x08) C[3]=VB[pixdata&3];
1030 if(J&0x04) C[2]=VB[pixdata&3];
1032 if(J&0x02) C[1]=VB[pixdata&3];
1034 if(J&0x01) C[0]=VB[pixdata];
1036 if(J&0x80) C[0]=VB[pixdata&3];
1038 if(J&0x40) C[1]=VB[pixdata&3];
1040 if(J&0x20) C[2]=VB[pixdata&3];
1042 if(J&0x10) C[3]=VB[pixdata&3];
1044 if(J&0x08) C[4]=VB[pixdata&3];
1046 if(J&0x04) C[5]=VB[pixdata&3];
1048 if(J&0x02) C[6]=VB[pixdata&3];
1050 if(J&0x01) C[7]=VB[pixdata];
1059 static void CopySprites098(uint8 *target)
1061 uint8 n=((PPU[1]&4)^4)<<1;
1067 if(rendis & 1) return; /* User asked to not display sprites. */
1071 uint32 t=*(uint32 *)(sprlinebuf+n);
1078 if(!(t&0x40) || (P[n]&0x40)) // Normal sprite || behind bg sprite
1084 if(!(t&0x4000) || (P[n+1]&0x40)) // Normal sprite || behind bg sprite
1085 P[n+1]=(sprlinebuf+1)[n];
1090 if(!(t&0x400000) || (P[n+2]&0x40)) // Normal sprite || behind bg sprite
1091 P[n+2]=(sprlinebuf+2)[n];
1096 if(!(t&0x40000000) || (P[n+3]&0x40)) // Normal sprite || behind bg sprite
1097 P[n+3]=(sprlinebuf+3)[n];
1100 /* TODO: Simplify */
1103 if(!(t&0x40000000)) // Normal sprite
1105 else if(P[n]&64) // behind bg sprite
1111 if(!(t&0x400000)) // Normal sprite
1112 P[n+1]=(sprlinebuf+1)[n];
1113 else if(P[n+1]&64) // behind bg sprite
1114 P[n+1]=(sprlinebuf+1)[n];
1119 if(!(t&0x4000)) // Normal sprite
1120 P[n+2]=(sprlinebuf+2)[n];
1121 else if(P[n+2]&64) // behind bg sprite
1122 P[n+2]=(sprlinebuf+2)[n];
1127 if(!(t&0x40)) // Normal sprite
1128 P[n+3]=(sprlinebuf+3)[n];
1129 else if(P[n+3]&64) // behind bg sprite
1130 P[n+3]=(sprlinebuf+3)[n];
1136 if(n) goto loopskie;
1139 void FCEUPPU_Init(void)
1144 void FCEUPPU_Reset(void)
1146 VRAMBuffer=PPU[0]=PPU[1]=PPU_status=PPU[3]=0;
1149 RefreshAddr=TempAddr=0;
1157 void FCEUPPU_Power(void)
1161 memset(NTARAM,0x00,0x800);
1162 memset(PALRAM,0x00,0x20);
1163 memset(SPRAM,0x00,0x100);
1166 for(x=0x2000;x<0x4000;x+=8)
1176 ARead[x+4]=A200x; //A2004;
1185 BWrite[0x4014]=B4014;
1189 void FCEUPPU_Loop(int skip)
1191 uint32 scanlines_per_frame = PAL ? 312 : 262;
1193 if(ppudead) /* Needed for Knight Rider, possibly others. */
1195 //memset(XBuf, 0x80, 256*240);
1196 X6502_Run(scanlines_per_frame*(256+85));
1203 PPU[3]=PPUSPL=0; /* Not sure if this is correct. According
1204 to Matt Conte and my own tests, it is. Timing is probably
1205 off, though. NOTE: Not having this here
1206 breaks a Super Donkey Kong game. */
1207 /* I need to figure out the true nature and length
1211 if(FCEUGameInfo.type==GIT_NSF)
1218 X6502_Run((scanlines_per_frame-242)*(256+85)-12); //-12);
1225 if(ScreenON || SpriteON)
1227 if(GameHBIRQHook && ((PPU[0]&0x38)!=0x18))
1230 for(x=0;x<42;x++) {PPU_hook(0x2000); PPU_hook(0);}
1235 if(ScreenON || SpriteON)
1237 RefreshAddr=TempAddr;
1238 if(PPU_hook) PPU_hook(RefreshAddr&0x3fff);
1241 /* Clean this stuff up later. */
1248 if(FCEUGameInfo.type==GIT_NSF)
1250 X6502_Run((256+85)*240);
1260 PPU_status|=0x20; // Fixes "Bee 52". Does it break anything?
1264 for(scanline=0;scanline<240;scanline++)
1266 if(ScreenON || SpriteON)
1268 if(scanline==y && SpriteON) PPU_status|=0x40;
1269 X6502_Run((scanline==239)?85:(256+85));
1274 X6502_Run((256+85)*y);
1275 if(SpriteON) PPU_status|=0x40; // Quick and very dirty hack.
1276 X6502_Run((256+85)*(240-y));
1279 X6502_Run((256+85)*240);
1287 for(scanline=0;scanline<240;) //scanline is incremented in DoLine. Evil. :/
1291 if((PPUViewer) && (scanline == PPUViewScanline)) UpdatePPUView(1);
1295 if(MMC5Hack && (ScreenON || SpriteON)) MMC5_hb(scanline);
1296 for(x=1,max=0,maxref=0;x<7;x++)
1305 //FCEU_DispMessage("%2x:%2x:%2x:%2x:%2x:%2x:%2x:%2x %d",deempcnt[0],deempcnt[1],deempcnt[2],deempcnt[3],deempcnt[4],deempcnt[5],deempcnt[6],deempcnt[7],maxref);
1306 //memset(deempcnt,0,sizeof(deempcnt));
1307 SetNESDeemph(maxref,0);
1309 } /* else... to if(ppudead) */