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
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 #define asmcpu_update(c) \
88 FCEU_SoundCPUHook098((((c) >> 4) * 43) >> 7)
94 /* Color deemphasis emulation. Joy... */
96 static int deempcnt[8];
98 extern int maxsprites;
101 extern uint8 SPRAM[0x100];
102 extern uint8 SPRBUF[0x100];
105 #define MMC5SPRVRAMADR(V) &MMC5SPRVPage[(V)>>10][(V)]
106 #define MMC5BGVRAMADR(V) &MMC5BGVPage[(V)>>10][(V)]
107 #define VRAMADR(V) &VPage[(V)>>10][(V)]
114 FCEUPPU_LineUpdate098();
116 ret|=PPUGenLatch&0x1F;
126 static DECLFR(A200x) /* Not correct for $2004 reads. */
128 FCEUPPU_LineUpdate098();
137 FCEUPPU_LineUpdate098();
147 //printf("$%02x:$%02x\n",PPUSPL,V);
153 printf("%d, %02x\n",scanline,ret);
160 uint32 tmp=RefreshAddr&0x3FFF;
162 FCEUPPU_LineUpdate098();
167 if(PPU_hook) PPU_hook(tmp);
168 PPUGenLatch=VRAMBuffer;
171 VRAMBuffer=VPage[tmp>>10][tmp];
175 VRAMBuffer=vnapage[(tmp>>10)&0x3][tmp&0x3FF];
179 if(INC32) RefreshAddr+=32;
181 if(PPU_hook) PPU_hook(RefreshAddr&0x3fff);
188 // FCEU_printf("%04x:%02x, (%d) %02x, %02x\n",A,V,scanline,PPU[0],PPU_status);
190 FCEUPPU_LineUpdate098();
192 if(!(PPU[0]&0x80) && (V&0x80) && (PPU_status&0x80))
194 // FCEU_printf("Trigger NMI, %d, %d\n",timestamp,ppudead);
196 TriggerNMI(); // TODO
205 //printf("%04x:$%02x, %d\n",A,V,scanline);
206 FCEUPPU_LineUpdate098();
220 //printf("$%04x:$%02x, %d, %d\n",A,V,timestamp,scanline);
228 //printf("Wr: %04x:$%02x\n",A,V);
238 //printf("$%02x:$%02x\n",PPUSPL,V);
249 FCEUPPU_LineUpdate098();
270 FCEUPPU_LineUpdate098();
276 TempAddr|=(V&0x3f)<<8;
283 RefreshAddr=TempAddr;
285 PPU_hook(RefreshAddr);
286 //printf("%d, %04x\n",scanline,RefreshAddr);
293 uint32 tmp=RefreshAddr&0x3FFF;
299 PALRAM[0x00]=PALRAM[0x04]=PALRAM[0x08]=PALRAM[0x0C]=V&0x3F;
300 else if(tmp&3) PALRAM[(tmp&0x1f)]=V&0x3f;
304 if(PPUCHRRAM&(1<<(tmp>>10)))
305 VPage[tmp>>10][tmp]=V;
309 if(PPUNTARAM&(1<<((tmp&0xF00)>>10)))
310 vnapage[((tmp&0xF00)>>10)][tmp&0x3FF]=V;
312 // FCEU_printf("ppu (%04x) %04x:%04x %d, %d\n",X.PC,RefreshAddr,PPUGenLatch,scanline,timestamp);
313 if(INC32) RefreshAddr+=32;
315 if(PPU_hook) PPU_hook(RefreshAddr&0x3fff);
324 // X6502_DMW(0x2004,X6502_DMR(t+x));
326 B2004(0x2004,X.DB=ARead[t+x](t+x));
327 X6502_AddCycles(512);
330 #define PAL(c) ((c)+cc)
332 #define GETLASTPIXEL (PAL?((timestamp*48-linestartts)/15) : ((timestamp*48-linestartts)>>4) )
334 static uint8 *Pline=0,*Plinef;
335 static int firsttile;
336 static int linestartts;
339 static void ResetRL(uint8 *target)
341 memset(target,0xFF,256);
342 if(InputScanlineHook)
343 InputScanlineHook(0,0,0,0);
347 linestartts=timestamp*48+X.count;
349 FCEUPPU_LineUpdate098();
353 extern uint8 sprlinebuf[256+8];
355 void FCEUPPU_LineUpdate098(void)
364 static int rendis = 0;
366 void FCEUI_SetRenderDisable(int sprites, int bg)
368 //printf("%d, %d\n",sprites,bg);
371 if(sprites == 2) rendis ^= 1;
372 else rendis = (rendis &~1) | sprites?1:0;
376 if(bg == 2) rendis ^= 2;
377 else rendis = (rendis &~2) | bg?2:0;
381 static void CheckSpriteHit(int p);
383 static void EndRL(void)
393 static uint8 sphitdata;
395 static void CheckSpriteHit(int p)
400 if(sphitx==0x100) return;
402 for(x=sphitx;x<(sphitx+8) && x<l;x++)
404 if((sphitdata&(0x80>>(x-sphitx))) && !(Plinef[x]&64))
407 //printf("Ha: %d, %d, Hita: %d, %d, %d, %d, %d\n",p,p&~7,scanline,GETLASTPIXEL-16,&Plinef[x],Pline,Pline-Plinef);
408 //printf("%d\n",GETLASTPIXEL-16);
409 //if(Plinef[x] == 0xFF)
410 //printf("PL: %d, %02x\n",scanline, Plinef[x]);
416 static int spork=0; /* spork the world. Any sprites on this line?
417 Then this will be set to 1. Needed for zapper
418 emulation and *gasp* sprite emulation.
421 // lasttile is really "second to last tile."
422 static void FASTAPASS(1) RefreshLine098(int lastpixel)
424 static uint32 pshift[2];
425 static uint32 atlatch;
426 uint32 smorkus=RefreshAddr;
428 #define RefreshAddr smorkus
432 register uint8 *P=Pline;
433 int lasttile=lastpixel>>3;
435 static int norecurse=0; /* Yeah, recursion would be bad.
436 PPU_hook() functions can call
437 mirroring/chr bank switching functions,
438 which call FCEUPPU_LineUpdate, which call this
440 if(norecurse) return;
442 if(sphitx != 0x100 && !(PPU_status&0x40))
444 if((sphitx < (lastpixel-16)) && !(sphitx < ((lasttile - 2)*8)))
446 //printf("OK: %d\n",scanline);
452 if(lasttile>34) lasttile=34;
453 numtiles=lasttile-firsttile;
455 if(numtiles<=0) return;
461 vofs=((PPU[0]&0x10)<<8) | ((RefreshAddr>>12)&7);
463 if(!ScreenON && !SpriteON)
466 tem=Pal[0]|(Pal[0]<<8)|(Pal[0]<<16)|(Pal[0]<<24);
468 FCEU_dwmemset(Pline,tem,numtiles*8);
474 #define TOFIXNUM (272-0x4)
475 if(lastpixel>=TOFIXNUM && tofix)
481 if(InputScanlineHook && (lastpixel-16)>=0)
483 InputScanlineHook(Plinef,spork?sprlinebuf:0,linestartts,lasttile*8-16);
488 /* Priority bits, needed for sprite emulation. */
494 /* This high-level graphics MMC5 emulation code was written
495 for MMC5 carts in "CL" mode. It's probably not totally
496 correct for carts in "SL" mode.
500 if(MMC5Hack && geniestage!=1)
502 if(MMC5HackCHRMode==0 && (MMC5HackSPMode&0x80))
504 int tochange=MMC5HackSPMode&0x1F;
506 for(X1=firsttile;X1<lasttile;X1++)
508 if((tochange<=0 && MMC5HackSPMode&0x40) || (tochange>0 && !(MMC5HackSPMode&0x40)))
511 #include "pputile098.h"
516 #include "pputile098.h"
521 else if(MMC5HackCHRMode==1 && (MMC5HackSPMode&0x80))
523 int tochange=MMC5HackSPMode&0x1F;
527 #define PPUT_MMC5CHR1
528 for(X1=firsttile;X1<lasttile;X1++)
530 #include "pputile098.h"
535 else if(MMC5HackCHRMode==1)
537 #define PPUT_MMC5CHR1
538 for(X1=firsttile;X1<lasttile;X1++)
540 #include "pputile098.h"
546 for(X1=firsttile;X1<lasttile;X1++)
548 #include "pputile098.h"
557 for(X1=firsttile;X1<lasttile;X1++)
559 #include "pputile098.h"
566 for(X1=firsttile;X1<lasttile;X1++)
568 #include "pputile098.h"
575 /* Reverse changes made before. */
582 if(firsttile<=2 && 2<lasttile && !(PPU[1]&2))
585 tem=Pal[0]|(Pal[0]<<8)|(Pal[0]<<16)|(Pal[0]<<24);
587 *(uint32 *)Plinef=*(uint32 *)(Plinef+4)=tem;
594 tem=Pal[0]|(Pal[0]<<8)|(Pal[0]<<16)|(Pal[0]<<24);
597 tcount=lasttile-firsttile;
605 FCEU_dwmemset(Plinef+tstart*8,tem,tcount*8);
608 if(lastpixel>=TOFIXNUM && tofix)
615 //CheckSpriteHit(lasttile*8); //lasttile*8); //lastpixel);
617 CheckSpriteHit(lastpixel); /* This only works right because
618 of a hack earlier in this function.
620 if(InputScanlineHook && (lastpixel-16)>=0)
622 InputScanlineHook(Plinef,spork?sprlinebuf:0,linestartts,lasttile*8-16);
628 static INLINE void Fixit2(void)
630 if(ScreenON || SpriteON)
632 uint32 rad=RefreshAddr;
634 rad|=TempAddr&0x041f;
636 //PPU_hook(RefreshAddr);
637 //PPU_hook(RefreshAddr,-1);
641 static void Fixit1(void)
643 if(ScreenON || SpriteON)
645 uint32 rad=RefreshAddr;
647 if((rad&0x7000)==0x7000)
650 if((rad&0x3E0)==0x3A0)
657 if((rad&0x3E0)==0x3e0)
665 //PPU_hook(RefreshAddr); //,-1);
669 void MMC5_hb(int); /* Ugh ugh ugh. */
670 static void DoLine(void)
673 uint8 *target=XBuf+scanline*320+32;
675 if(MMC5Hack && (ScreenON || SpriteON)) MMC5_hb(scanline);
680 if(rendis & 2) /* User asked to not display background data. */
683 tem=Pal[0]|(Pal[0]<<8)|(Pal[0]<<16)|(Pal[0]<<24);
685 FCEU_dwmemset(target,tem,256);
689 CopySprites098(target);
691 if(ScreenON || SpriteON) // Yes, very el-cheapo.
696 *(uint32 *)&target[x<<2]=(*(uint32*)&target[x<<2])&0x30303030;
702 *(uint32 *)&target[x<<2]=((*(uint32*)&target[x<<2])&0x3f3f3f3f)|0xc0c0c0c0;
706 *(uint32 *)&target[x<<2]=(*(uint32*)&target[x<<2])|0x40404040;
709 *(uint32 *)&target[x<<2]=((*(uint32*)&target[x<<2])&0x3f3f3f3f)|0x80808080;
713 if(ScreenON || SpriteON)
714 FetchSpriteData098();
716 if(GameHBIRQHook && (ScreenON || SpriteON) && ((PPU[0]&0x38)!=0x18))
726 X6502_Run(6); // Tried 65, caused problems with Slalom(maybe others)
730 // A semi-hack for Star Trek: 25th Anniversary
731 if(GameHBIRQHook && (ScreenON || SpriteON) && ((PPU[0]&0x38)!=0x18))
737 if(GameHBIRQHook2 && (ScreenON || SpriteON))
742 ResetRL(XBuf+scanline*320+32);
759 static uint8 numsprites,SpriteBlurp;
760 static void FetchSpriteData098(void)
774 vofs=(unsigned int)(P0&0x8&(((P0&0x20)^0x20)>>2))<<9;
778 for(n=63;n>=0;n--,spr++)
780 if((unsigned int)(scanline-spr->y)>=H) continue;
781 //printf("%d, %u\n",scanline,(unsigned int)(scanline-spr->y));
792 t = (int)scanline-(spr->y);
795 vadr = ((spr->no&1)<<12) + ((spr->no&0xFE)<<4);
797 vadr = (spr->no<<4)+vofs;
812 /* Fix this geniestage hack */
813 if(MMC5Hack && geniestage!=1) C = MMC5SPRVRAMADR(vadr);
814 else C = VRAMADR(vadr);
822 *(uint32 *)&SPRBUF[ns<<2]=*(uint32 *)&dst;
834 for(n=63;n>=0;n--,spr++)
836 if((unsigned int)(scanline-spr->y)>=H) continue;
848 t = (int)scanline-(spr->y);
851 vadr = ((spr->no&1)<<12) + ((spr->no&0xFE)<<4);
853 vadr = (spr->no<<4)+vofs;
868 if(MMC5Hack) C = MMC5SPRVRAMADR(vadr);
869 else C = VRAMADR(vadr);
881 *(uint32 *)&SPRBUF[ns<<2]=*(uint32 *)&dst;
893 //printf("%d %d\n",scanline,ns);
894 if(ns>8) PPU_status|=0x20; /* Handle case when >8 sprites per
895 scanline option is enabled. */
898 for(n=0;n<(8-ns);n++)
908 static void RefreshSprites098(void)
914 if(!numsprites) return;
916 FCEU_dwmemset(sprlinebuf,0x80808080,256);
918 spr = (SPRB*)SPRBUF+numsprites;
920 for(n=numsprites;n>=0;n--,spr--)
923 //register uint32 pixdata asm ("eax");
924 //register uint8 J, atr;
926 register uint32 pixdata;
927 register uint8 J,atr;
934 pixdata=ppulut1[spr->ca[0]]|ppulut2[spr->ca[1]];
935 J=spr->ca[0]|spr->ca[1];
940 if(n==0 && SpriteBlurp && !(PPU_status&0x40))
945 sphitdata= ((J<<7)&0x80) |
956 VB = (PALRAM+0x10)+((atr&3)<<2);
962 if(J&0x80) C[7]=VB[pixdata&3]|0x40;
964 if(J&0x40) C[6]=VB[pixdata&3]|0x40;
966 if(J&0x20) C[5]=VB[pixdata&3]|0x40;
968 if(J&0x10) C[4]=VB[pixdata&3]|0x40;
970 if(J&0x08) C[3]=VB[pixdata&3]|0x40;
972 if(J&0x04) C[2]=VB[pixdata&3]|0x40;
974 if(J&0x02) C[1]=VB[pixdata&3]|0x40;
976 if(J&0x01) C[0]=VB[pixdata]|0x40;
978 if(J&0x80) C[0]=VB[pixdata&3]|0x40;
980 if(J&0x40) C[1]=VB[pixdata&3]|0x40;
982 if(J&0x20) C[2]=VB[pixdata&3]|0x40;
984 if(J&0x10) C[3]=VB[pixdata&3]|0x40;
986 if(J&0x08) C[4]=VB[pixdata&3]|0x40;
988 if(J&0x04) C[5]=VB[pixdata&3]|0x40;
990 if(J&0x02) C[6]=VB[pixdata&3]|0x40;
992 if(J&0x01) C[7]=VB[pixdata]|0x40;
997 if(J&0x80) C[7]=VB[pixdata&3];
999 if(J&0x40) C[6]=VB[pixdata&3];
1001 if(J&0x20) C[5]=VB[pixdata&3];
1003 if(J&0x10) C[4]=VB[pixdata&3];
1005 if(J&0x08) C[3]=VB[pixdata&3];
1007 if(J&0x04) C[2]=VB[pixdata&3];
1009 if(J&0x02) C[1]=VB[pixdata&3];
1011 if(J&0x01) C[0]=VB[pixdata];
1013 if(J&0x80) C[0]=VB[pixdata&3];
1015 if(J&0x40) C[1]=VB[pixdata&3];
1017 if(J&0x20) C[2]=VB[pixdata&3];
1019 if(J&0x10) C[3]=VB[pixdata&3];
1021 if(J&0x08) C[4]=VB[pixdata&3];
1023 if(J&0x04) C[5]=VB[pixdata&3];
1025 if(J&0x02) C[6]=VB[pixdata&3];
1027 if(J&0x01) C[7]=VB[pixdata];
1036 static void CopySprites098(uint8 *target)
1038 uint8 n=((PPU[1]&4)^4)<<1;
1044 if(rendis & 1) return; /* User asked to not display sprites. */
1048 uint32 t=*(uint32 *)(sprlinebuf+n);
1055 if(!(t&0x40) || (P[n]&0x40)) // Normal sprite || behind bg sprite
1061 if(!(t&0x4000) || (P[n+1]&0x40)) // Normal sprite || behind bg sprite
1062 P[n+1]=(sprlinebuf+1)[n];
1067 if(!(t&0x400000) || (P[n+2]&0x40)) // Normal sprite || behind bg sprite
1068 P[n+2]=(sprlinebuf+2)[n];
1073 if(!(t&0x40000000) || (P[n+3]&0x40)) // Normal sprite || behind bg sprite
1074 P[n+3]=(sprlinebuf+3)[n];
1077 /* TODO: Simplify */
1080 if(!(t&0x40000000)) // Normal sprite
1082 else if(P[n]&64) // behind bg sprite
1088 if(!(t&0x400000)) // Normal sprite
1089 P[n+1]=(sprlinebuf+1)[n];
1090 else if(P[n+1]&64) // behind bg sprite
1091 P[n+1]=(sprlinebuf+1)[n];
1096 if(!(t&0x4000)) // Normal sprite
1097 P[n+2]=(sprlinebuf+2)[n];
1098 else if(P[n+2]&64) // behind bg sprite
1099 P[n+2]=(sprlinebuf+2)[n];
1104 if(!(t&0x40)) // Normal sprite
1105 P[n+3]=(sprlinebuf+3)[n];
1106 else if(P[n+3]&64) // behind bg sprite
1107 P[n+3]=(sprlinebuf+3)[n];
1113 if(n) goto loopskie;
1116 void FCEUPPU_Init(void)
1121 void FCEUPPU_Reset(void)
1123 VRAMBuffer=PPU[0]=PPU[1]=PPU_status=PPU[3]=0;
1126 RefreshAddr=TempAddr=0;
1134 void FCEUPPU_Power(void)
1138 memset(NTARAM,0x00,0x800);
1139 memset(PALRAM,0x00,0x20);
1140 memset(SPRAM,0x00,0x100);
1143 for(x=0x2000;x<0x4000;x+=8)
1153 ARead[x+4]=A200x; //A2004;
1162 BWrite[0x4014]=B4014;
1166 void FCEUPPU_Loop(int skip)
1168 uint32 scanlines_per_frame = PAL ? 312 : 262;
1170 if(ppudead) /* Needed for Knight Rider, possibly others. */
1172 //memset(XBuf, 0x80, 256*240);
1173 //X6502_Run(scanlines_per_frame*(256+85));
1175 for (lines=scanlines_per_frame;lines;lines--)
1183 PPU[3]=PPUSPL=0; /* Not sure if this is correct. According
1184 to Matt Conte and my own tests, it is. Timing is probably
1185 off, though. NOTE: Not having this here
1186 breaks a Super Donkey Kong game. */
1187 /* I need to figure out the true nature and length
1191 if(FCEUGameInfo.type==GIT_NSF)
1198 // Note: this is needed for asm core
1201 X6502_Run(256+85-12);
1202 for (lines=scanlines_per_frame-242-1;lines;lines--)
1211 if(ScreenON || SpriteON)
1213 if(GameHBIRQHook && ((PPU[0]&0x38)!=0x18))
1216 for(x=0;x<42;x++) {PPU_hook(0x2000); PPU_hook(0);}
1221 if(ScreenON || SpriteON)
1223 RefreshAddr=TempAddr;
1224 if(PPU_hook) PPU_hook(RefreshAddr&0x3fff);
1227 /* Clean this stuff up later. */
1234 if(FCEUGameInfo.type==GIT_NSF)
1236 // run scanlines for asm core to fuction
1237 for(scanline=0;scanline<240;scanline++)
1248 PPU_status|=0x20; // Fixes "Bee 52". Does it break anything?
1252 for(scanline=0;scanline<240;scanline++)
1254 if(ScreenON || SpriteON)
1256 if(scanline==y && SpriteON) PPU_status|=0x40;
1257 X6502_Run((scanline==239)?85:(256+85));
1262 for (lines=y;lines;lines--)
1264 if(SpriteON) PPU_status|=0x40; // Quick and very dirty hack.
1265 for (lines=240-y;lines;lines--)
1270 for (lines=240;lines;lines--)
1280 for(scanline=0;scanline<240;) //scanline is incremented in DoLine. Evil. :/
1284 if((PPUViewer) && (scanline == PPUViewScanline)) UpdatePPUView(1);
1288 if(MMC5Hack && (ScreenON || SpriteON)) MMC5_hb(scanline);
1289 for(x=1,max=0,maxref=0;x<7;x++)
1298 //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);
1299 //memset(deempcnt,0,sizeof(deempcnt));
1300 SetNESDeemph(maxref,0);
1302 } /* else... to if(ppudead) */