still reverting r167 sound change
[fceu.git] / ppu098.c
1 /* FCE Ultra - NES/Famicom Emulator
2  *
3  * Copyright notice for this file:
4  *  Copyright (C) 1998 BERO
5  *  Copyright (C) 2003 Xodnizel
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
24 #include  "types.h"
25 #include  "x6502.h"
26 #include  "fce.h"
27 #include  "ppu098.h"
28 #include  "nsf.h"
29 #include  "sound.h"
30 #include  "memory.h"
31
32 #include  "cart.h"
33 #include  "palette.h"
34 #include  "video.h"
35 #include  "input.h"
36
37 #ifdef GP2X
38 #include        "drivers/gp2x/asmutils.h"
39 #endif
40
41 #define Pal     (PALRAM)
42
43 static void FetchSpriteData098(void);
44 static void FASTAPASS(1) RefreshLine098(int lastpixel);
45 static void RefreshSprites098(void);
46 static void CopySprites098(uint8 *target);
47
48 static void Fixit1(void);
49 static uint32 ppulut1[256];
50 static uint32 ppulut2[256];
51 static uint32 ppulut3[128];
52
53 static void makeppulut(void)
54 {
55  int x;
56  int y;
57
58  for(x=0;x<256;x++)
59  {
60   ppulut1[x]=0;
61   for(y=0;y<8;y++)
62    ppulut1[x]|=((x>>(7-y))&1)<<(y*4);
63   ppulut2[x]=ppulut1[x]<<1;
64  }
65
66  {
67
68   int cc,xo,pixel;
69
70   for(cc=0;cc<16;cc++)
71   {
72    for(xo=0;xo<8;xo++)
73    {
74     ppulut3[xo|(cc<<3)]=0;
75     for(pixel=0;pixel<8;pixel++)
76     {
77      int shiftr;
78       shiftr=(pixel+xo)/8;
79       shiftr*=2;
80       ppulut3[xo|(cc<<3)]|=(( cc>>shiftr )&3)<<(2+pixel*4);
81     }
82 //    printf("%08x\n",ppulut3[xo|(cc<<3)]);
83    }
84   }
85
86  }
87 }
88
89 #if defined(ASM_6502) && !defined(DEBUG_ASM_6502)
90 static void asmcpu_update(int32 cycles)
91 {
92  // some code from x6502.c
93  fhcnt-=cycles;
94  if(fhcnt<=0)
95  {
96   FrameSoundUpdate();
97   fhcnt+=fhinc;
98  }
99
100  if(PCMIRQCount>0)
101  {
102   PCMIRQCount-=cycles;
103   if(PCMIRQCount<=0)
104   {
105    vdis=1;
106    if((PSG[0x10]&0x80) && !(PSG[0x10]&0x40))
107    {
108     extern uint8 SIRQStat;
109     SIRQStat|=0x80;
110     X6502_IRQBegin(FCEU_IQDPCM);
111    }
112   }
113  }
114 }
115 #endif
116
117 extern int ppudead;
118 extern int kook;
119
120 /* Color deemphasis emulation.  Joy... */
121 static uint8 deemp=0;
122 static int deempcnt[8];
123
124 extern int maxsprites;
125
126 extern uint8 PPUSPL;
127 extern uint8 SPRAM[0x100];
128 extern uint8 SPRBUF[0x100];
129
130
131 #define MMC5SPRVRAMADR(V)      &MMC5SPRVPage[(V)>>10][(V)]
132 #define MMC5BGVRAMADR(V)      &MMC5BGVPage[(V)>>10][(V)]
133 #define VRAMADR(V)      &VPage[(V)>>10][(V)]
134
135
136 static DECLFR(A2002)
137 {
138                         uint8 ret;
139
140                         FCEUPPU_LineUpdate098();
141                         ret = PPU_status;
142                         ret|=PPUGenLatch&0x1F;
143
144                         {
145                          vtoggle=0;
146                          PPU_status&=0x7F;
147                          PPUGenLatch=ret;
148                         }
149                         return ret;
150 }
151
152 static DECLFR(A200x)  /* Not correct for $2004 reads. */
153 {
154                         FCEUPPU_LineUpdate098();
155                         return PPUGenLatch;
156 }
157
158 /*
159 static DECLFR(A2004)
160 {
161                         uint8 ret;
162
163                         FCEUPPU_LineUpdate098();
164                         ret = SPRAM[PPU[3]];
165
166                         if(PPUSPL>=8)
167                         {
168                          if(PPU[3]>=8)
169                           ret = SPRAM[PPU[3]];
170                         }
171                         else
172                         {
173                          //printf("$%02x:$%02x\n",PPUSPL,V);
174                          ret = SPRAM[PPUSPL];
175                         }
176                         PPU[3]++;
177                         PPUSPL++;
178                         PPUGenLatch = ret;
179                         printf("%d, %02x\n",scanline,ret);
180                         return(ret);
181 }
182 */
183 static DECLFR(A2007)
184 {
185                         uint8 ret;
186                         uint32 tmp=RefreshAddr&0x3FFF;
187
188                         FCEUPPU_LineUpdate098();
189
190                         ret=VRAMBuffer;
191
192                         {
193                          if(PPU_hook) PPU_hook(tmp);
194                          PPUGenLatch=VRAMBuffer;
195                          if(tmp<0x2000)
196                          {
197                           VRAMBuffer=VPage[tmp>>10][tmp];
198                          }
199                          else
200                          {
201                           VRAMBuffer=vnapage[(tmp>>10)&0x3][tmp&0x3FF];
202                          }
203                         }
204                         {
205        if(INC32) RefreshAddr+=32;
206                          else RefreshAddr++;
207                          if(PPU_hook) PPU_hook(RefreshAddr&0x3fff);
208                         }
209                         return ret;
210 }
211
212 static DECLFW(B2000)
213 {
214 //    FCEU_printf("%04x:%02x, (%d) %02x, %02x\n",A,V,scanline,PPU[0],PPU_status);
215
216                 FCEUPPU_LineUpdate098();
217                 PPUGenLatch=V;
218                 if(!(PPU[0]&0x80) && (V&0x80) && (PPU_status&0x80))
219                 {
220 //     FCEU_printf("Trigger NMI, %d, %d\n",timestamp,ppudead);
221 //                 TriggerNMI2();
222                  TriggerNMI(); // TODO?
223                 }
224                 PPU[0]=V;
225                 TempAddr&=0xF3FF;
226                 TempAddr|=(V&3)<<10;
227 }
228
229 static DECLFW(B2001)
230 {
231                 //printf("%04x:$%02x, %d\n",A,V,scanline);
232                 FCEUPPU_LineUpdate098();
233                 PPUGenLatch=V;
234                 PPU[1]=V;
235                 if(V&0xE0)
236                  deemp=V>>5;
237 }
238
239 static DECLFW(B2002)
240 {
241                 PPUGenLatch=V;
242 }
243
244 static DECLFW(B2003)
245 {
246                 //printf("$%04x:$%02x, %d, %d\n",A,V,timestamp,scanline);
247                 PPUGenLatch=V;
248                 PPU[3]=V;
249                 PPUSPL=V&0x7;
250 }
251
252 static DECLFW(B2004)
253 {
254                 //printf("Wr: %04x:$%02x\n",A,V);
255
256                 PPUGenLatch=V;
257                 if(PPUSPL>=8)
258                 {
259                  if(PPU[3]>=8)
260                   SPRAM[PPU[3]]=V;
261                 }
262                 else
263                 {
264                  //printf("$%02x:$%02x\n",PPUSPL,V);
265                  SPRAM[PPUSPL]=V;
266                 }
267                 PPU[3]++;
268                 PPUSPL++;
269
270 }
271
272 static DECLFW(B2005)
273 {
274                 uint32 tmp=TempAddr;
275                 FCEUPPU_LineUpdate098();
276                 PPUGenLatch=V;
277     if(!vtoggle)
278                 {
279                  tmp&=0xFFE0;
280                  tmp|=V>>3;
281                  XOffset=V&7;
282                 }
283                 else
284                 {
285                  tmp&=0x8C1F;
286                  tmp|=((V&~0x7)<<2);
287                  tmp|=(V&7)<<12;
288                 }
289                 TempAddr=tmp;
290                 vtoggle^=1;
291 }
292
293
294 static DECLFW(B2006)
295 {
296                 FCEUPPU_LineUpdate098();
297
298                 PPUGenLatch=V;
299                 if(!vtoggle)
300                 {
301                  TempAddr&=0x00FF;
302                  TempAddr|=(V&0x3f)<<8;
303                 }
304                 else
305                 {
306                  TempAddr&=0xFF00;
307                  TempAddr|=V;
308
309                  RefreshAddr=TempAddr;
310                  if(PPU_hook)
311                   PPU_hook(RefreshAddr);
312                  //printf("%d, %04x\n",scanline,RefreshAddr);
313                 }
314                 vtoggle^=1;
315 }
316
317 static DECLFW(B2007)
318 {
319                         uint32 tmp=RefreshAddr&0x3FFF;
320                         PPUGenLatch=V;
321                         if(tmp>=0x3F00)
322                         {
323                          // hmmm....
324                          if(!(tmp&0xf))
325                           PALRAM[0x00]=PALRAM[0x04]=PALRAM[0x08]=PALRAM[0x0C]=V&0x3F;
326                          else if(tmp&3) PALRAM[(tmp&0x1f)]=V&0x3f;
327                         }
328                         else if(tmp<0x2000)
329                         {
330                           if(PPUCHRRAM&(1<<(tmp>>10)))
331                             VPage[tmp>>10][tmp]=V;
332                         }
333                         else
334                         {
335                          if(PPUNTARAM&(1<<((tmp&0xF00)>>10)))
336                           vnapage[((tmp&0xF00)>>10)][tmp&0x3FF]=V;
337                         }
338 //      FCEU_printf("ppu (%04x) %04x:%04x %d, %d\n",X.PC,RefreshAddr,PPUGenLatch,scanline,timestamp);
339       if(INC32) RefreshAddr+=32;
340                         else RefreshAddr++;
341                         if(PPU_hook) PPU_hook(RefreshAddr&0x3fff);
342 }
343
344 static DECLFW(B4014)
345 {
346         uint32 t=V<<8;
347         int x;
348
349         //for(x=0;x<256;x++)
350         // X6502_DMW(0x2004,X6502_DMR(t+x));
351         for(x=0;x<256;x++)
352          B2004(0x2004,X.DB=ARead[t+x](t+x));
353         X6502_AddCycles(512);
354 }
355
356 #define PAL(c)  ((c)+cc)
357
358 #define GETLASTPIXEL    (PAL?((timestamp*48-linestartts)/15) : ((timestamp*48-linestartts)>>4) )
359
360 static uint8 *Pline=0,*Plinef;
361 static int firsttile;
362 static int linestartts;
363 static int tofix=0;
364
365 static void ResetRL(uint8 *target)
366 {
367  FCEU_dwmemset(target,0xffffffff,256);
368  if(InputScanlineHook)
369   InputScanlineHook(0,0,0,0);
370  Plinef=target;
371  Pline=target;
372  firsttile=0;
373  linestartts=timestamp*48+X.count;
374  tofix=0;
375  FCEUPPU_LineUpdate098();
376  tofix=1;
377 }
378
379 extern uint8 sprlinebuf[256+8];
380
381 void FCEUPPU_LineUpdate098(void)
382 {
383  if(Pline)
384  {
385   int l=GETLASTPIXEL;
386   RefreshLine098(l);
387  }
388 }
389
390 static int rendis = 0;
391
392 void FCEUI_SetRenderDisable(int sprites, int bg)
393 {
394  //printf("%d, %d\n",sprites,bg);
395  if(sprites >= 0)
396  {
397   if(sprites == 2) rendis ^= 1;
398   else rendis = (rendis &~1) | sprites?1:0;
399  }
400  if(bg >= 0)
401  {
402   if(bg == 2) rendis ^= 2;
403   else rendis = (rendis &~2) | bg?2:0;
404  }
405 }
406
407 static void CheckSpriteHit(int p);
408
409 static void EndRL(void)
410 {
411  RefreshLine098(272);
412  if(tofix)
413   Fixit1();
414  CheckSpriteHit(272);
415  Pline=0;
416 }
417
418 static int32 sphitx;
419 static uint8 sphitdata;
420
421 static void CheckSpriteHit(int p)
422 {
423  int l=p-16;
424  int x;
425
426  if(sphitx==0x100) return;
427
428  for(x=sphitx;x<(sphitx+8) && x<l;x++)
429  {
430    if((sphitdata&(0x80>>(x-sphitx))) && !(Plinef[x]&64))
431    {
432     PPU_status|=0x40;
433     //printf("Ha:  %d, %d, Hita: %d, %d, %d, %d, %d\n",p,p&~7,scanline,GETLASTPIXEL-16,&Plinef[x],Pline,Pline-Plinef);
434     //printf("%d\n",GETLASTPIXEL-16);
435     //if(Plinef[x] == 0xFF)
436     //printf("PL: %d, %02x\n",scanline, Plinef[x]);
437     sphitx=0x100;
438     break;
439    }
440    }
441 }
442 static int spork=0;     /* spork the world.  Any sprites on this line?
443                            Then this will be set to 1.  Needed for zapper
444                            emulation and *gasp* sprite emulation.
445                         */
446
447 // lasttile is really "second to last tile."
448 static void FASTAPASS(1) RefreshLine098(int lastpixel)
449 {
450         static uint32 pshift[2];
451         static uint32 atlatch;
452         uint32 smorkus=RefreshAddr;
453
454         #define RefreshAddr smorkus
455         uint32 vofs;
456         int X1;
457
458         register uint8 *P=Pline;
459         int lasttile=lastpixel>>3;
460         int numtiles;
461         static int norecurse=0; /* Yeah, recursion would be bad.
462                                     PPU_hook() functions can call
463                                     mirroring/chr bank switching functions,
464                                     which call FCEUPPU_LineUpdate, which call this
465                                     function. */
466         if(norecurse) return;
467
468         if(sphitx != 0x100 && !(PPU_status&0x40))
469         {
470          if((sphitx < (lastpixel-16)) && !(sphitx < ((lasttile - 2)*8)))
471          {
472           //printf("OK: %d\n",scanline);
473           lasttile++;
474          }
475
476         }
477
478         if(lasttile>34) lasttile=34;
479         numtiles=lasttile-firsttile;
480
481         if(numtiles<=0) return;
482
483         P=Pline;
484
485         vofs=0;
486
487         vofs=((PPU[0]&0x10)<<8) | ((RefreshAddr>>12)&7);
488
489         if(!ScreenON && !SpriteON)
490         {
491          uint32 tem;
492          tem=Pal[0]|(Pal[0]<<8)|(Pal[0]<<16)|(Pal[0]<<24);
493          tem|=0x40404040;
494          FCEU_dwmemset(Pline,tem,numtiles*8);
495          P+=numtiles*8;
496          Pline=P;
497
498          firsttile=lasttile;
499
500          #define TOFIXNUM (272-0x4)
501          if(lastpixel>=TOFIXNUM && tofix)
502          {
503           Fixit1();
504           tofix=0;
505          }
506
507          if(InputScanlineHook && (lastpixel-16)>=0)
508          {
509           InputScanlineHook(Plinef,spork?sprlinebuf:0,linestartts,lasttile*8-16);
510          }
511          return;
512         }
513
514         /* Priority bits, needed for sprite emulation. */
515         Pal[0]|=64;
516         Pal[4]|=64;
517         Pal[8]|=64;
518         Pal[0xC]|=64;
519
520         /* This high-level graphics MMC5 emulation code was written
521            for MMC5 carts in "CL" mode.  It's probably not totally
522            correct for carts in "SL" mode.
523         */
524
525         #define PPUT_MMC5
526         if(MMC5Hack && geniestage!=1)
527         {
528          if(MMC5HackCHRMode==0 && (MMC5HackSPMode&0x80))
529          {
530           int tochange=MMC5HackSPMode&0x1F;
531           tochange-=firsttile;
532           for(X1=firsttile;X1<lasttile;X1++)
533           {
534            if((tochange<=0 && MMC5HackSPMode&0x40) || (tochange>0 && !(MMC5HackSPMode&0x40)))
535            {
536             #define PPUT_MMC5SP
537             #include "pputile098.h"
538             #undef PPUT_MMC5SP
539            }
540            else
541            {
542             #include "pputile098.h"
543            }
544            tochange--;
545           }
546          }
547          else if(MMC5HackCHRMode==1 && (MMC5HackSPMode&0x80))
548          {
549           int tochange=MMC5HackSPMode&0x1F;
550           tochange-=firsttile;
551
552           #define PPUT_MMC5SP
553           #define PPUT_MMC5CHR1
554           for(X1=firsttile;X1<lasttile;X1++)
555           {
556            #include "pputile098.h"
557           }
558           #undef PPUT_MMC5CHR1
559           #undef PPUT_MMC5SP
560          }
561          else if(MMC5HackCHRMode==1)
562          {
563           #define PPUT_MMC5CHR1
564           for(X1=firsttile;X1<lasttile;X1++)
565           {
566            #include "pputile098.h"
567           }
568           #undef PPUT_MMC5CHR1
569          }
570          else
571          {
572           for(X1=firsttile;X1<lasttile;X1++)
573           {
574            #include "pputile098.h"
575           }
576          }
577         }
578         #undef PPUT_MMC5
579         else if(PPU_hook)
580         {
581          norecurse=1;
582          #define PPUT_HOOK
583          for(X1=firsttile;X1<lasttile;X1++)
584          {
585           #include "pputile098.h"
586          }
587          #undef PPUT_HOOK
588          norecurse=0;
589         }
590         else
591         {
592          for(X1=firsttile;X1<lasttile;X1++)
593          {
594           #include "pputile098.h"
595          }
596         }
597
598         #undef vofs
599         #undef RefreshAddr
600
601         /* Reverse changes made before. */
602         Pal[0]&=63;
603         Pal[4]&=63;
604         Pal[8]&=63;
605         Pal[0xC]&=63;
606
607         RefreshAddr=smorkus;
608         if(firsttile<=2 && 2<lasttile && !(PPU[1]&2))
609         {
610          uint32 tem;
611          tem=Pal[0]|(Pal[0]<<8)|(Pal[0]<<16)|(Pal[0]<<24);
612          tem|=0x40404040;
613          *(uint32 *)Plinef=*(uint32 *)(Plinef+4)=tem;
614         }
615
616         if(!ScreenON)
617         {
618          uint32 tem;
619          int tstart,tcount;
620          tem=Pal[0]|(Pal[0]<<8)|(Pal[0]<<16)|(Pal[0]<<24);
621          tem|=0x40404040;
622
623          tcount=lasttile-firsttile;
624          tstart=firsttile-2;
625          if(tstart<0)
626          {
627           tcount+=tstart;
628           tstart=0;
629          }
630          if(tcount>0)
631           FCEU_dwmemset(Plinef+tstart*8,tem,tcount*8);
632         }
633
634         if(lastpixel>=TOFIXNUM && tofix)
635         {
636           //puts("Fixed");
637           Fixit1();
638           tofix=0;
639         }
640
641         //CheckSpriteHit(lasttile*8); //lasttile*8); //lastpixel);
642
643   CheckSpriteHit(lastpixel);  /* This only works right because
644                                            of a hack earlier in this function.
645                                         */
646         if(InputScanlineHook && (lastpixel-16)>=0)
647         {
648          InputScanlineHook(Plinef,spork?sprlinebuf:0,linestartts,lasttile*8-16);
649         }
650         Pline=P;
651         firsttile=lasttile;
652 }
653
654 static INLINE void Fixit2(void)
655 {
656    if(ScreenON || SpriteON)
657    {
658     uint32 rad=RefreshAddr;
659     rad&=0xFBE0;
660     rad|=TempAddr&0x041f;
661     RefreshAddr=rad;
662     //PPU_hook(RefreshAddr);
663     //PPU_hook(RefreshAddr,-1);
664    }
665 }
666
667 static void Fixit1(void)
668 {
669    if(ScreenON || SpriteON)
670    {
671     uint32 rad=RefreshAddr;
672
673     if((rad&0x7000)==0x7000)
674     {
675      rad^=0x7000;
676      if((rad&0x3E0)==0x3A0)
677      {
678       rad^=0x3A0;
679       rad^=0x800;
680      }
681      else
682      {
683       if((rad&0x3E0)==0x3e0)
684        rad^=0x3e0;
685       else rad+=0x20;
686      }
687     }
688     else
689      rad+=0x1000;
690     RefreshAddr=rad;
691     //PPU_hook(RefreshAddr); //,-1);
692    }
693 }
694
695 void MMC5_hb(int);     /* Ugh ugh ugh. */
696 static void DoLine(void)
697 {
698 #ifndef GP2X
699  int x;
700 #endif
701  uint8 *target=XBuf+scanline*320+32;
702
703  if(MMC5Hack && (ScreenON || SpriteON)) MMC5_hb(scanline);
704
705  X6502_Run(256);
706  EndRL();
707
708  if(rendis & 2)  /* User asked to not display background data. */
709  {
710   uint32 tem;
711   tem=Pal[0]|(Pal[0]<<8)|(Pal[0]<<16)|(Pal[0]<<24);
712   tem|=0x40404040;
713   FCEU_dwmemset(target,tem,256);
714  }
715
716  if(SpriteON)
717   CopySprites098(target);
718
719 #ifdef GP2X
720  if(ScreenON || SpriteON)  // Yes, very el-cheapo.
721  {
722   if(PPU[1]&0x01)
723    block_and(target, 256, 0x30);
724  }
725  if((PPU[1]>>5)==0x7)
726   block_or(target, 256, 0xc0);
727  else if(PPU[1]&0xE0)
728   block_or(target, 256, 0x40);
729  else
730   block_andor(target, 256, 0x3f, 0x80);
731 #else
732  if(ScreenON || SpriteON)  // Yes, very el-cheapo.
733  {
734   if(PPU[1]&0x01)
735   {
736    for(x=63;x>=0;x--)
737    *(uint32 *)&target[x<<2]=(*(uint32*)&target[x<<2])&0x30303030;
738   }
739  }
740  if((PPU[1]>>5)==0x7)
741  {
742   for(x=63;x>=0;x--)
743    *(uint32 *)&target[x<<2]=((*(uint32*)&target[x<<2])&0x3f3f3f3f)|0xc0c0c0c0;
744  }
745  else if(PPU[1]&0xE0)
746   for(x=63;x>=0;x--)
747    *(uint32 *)&target[x<<2]=(*(uint32*)&target[x<<2])|0x40404040;
748  else
749   for(x=63;x>=0;x--)
750    *(uint32 *)&target[x<<2]=((*(uint32*)&target[x<<2])&0x3f3f3f3f)|0x80808080;
751 #endif
752
753  sphitx=0x100;
754
755  if(ScreenON || SpriteON)
756   FetchSpriteData098();
757
758  if(GameHBIRQHook && (ScreenON || SpriteON) && ((PPU[0]&0x38)!=0x18))
759  {
760   X6502_Run(6);
761   Fixit2();
762   X6502_Run(4);
763   GameHBIRQHook();
764   X6502_Run(85-16-10);
765  }
766  else
767  {
768   X6502_Run(6);  // Tried 65, caused problems with Slalom(maybe others)
769   Fixit2();
770   X6502_Run(85-6-16);
771
772   // A semi-hack for Star Trek: 25th Anniversary
773   if(GameHBIRQHook && (ScreenON || SpriteON) && ((PPU[0]&0x38)!=0x18))
774    GameHBIRQHook();
775  }
776
777  if(SpriteON)
778   RefreshSprites098();
779  if(GameHBIRQHook2 && (ScreenON || SpriteON))
780   GameHBIRQHook2();
781  scanline++;
782  if(scanline<240)
783  {
784   ResetRL(XBuf+scanline*320+32);
785  }
786  X6502_Run(16);
787 }
788
789 #define V_FLIP  0x80
790 #define H_FLIP  0x40
791 #define SP_BACK 0x20
792
793 typedef struct {
794         uint8 y,no,atr,x;
795 } SPR;
796
797 typedef struct {
798         uint8 ca[2],atr,x;
799 } SPRB;
800
801 static uint8 numsprites,SpriteBlurp;
802 static void FetchSpriteData098(void)
803 {
804         uint8 ns,sb;
805         SPR *spr;
806         uint8 H;
807         int n;
808         int vofs;
809         uint8 P0=PPU[0];
810
811         spr=(SPR *)SPRAM;
812         H=8;
813
814         ns=sb=0;
815
816         vofs=(unsigned int)(P0&0x8&(((P0&0x20)^0x20)>>2))<<9;
817         H+=(P0&0x20)>>2;
818
819         if(!PPU_hook)
820          for(n=63;n>=0;n--,spr++)
821          {
822                 if((unsigned int)(scanline-spr->y)>=H) continue;
823                 //printf("%d, %u\n",scanline,(unsigned int)(scanline-spr->y));
824                 if(ns<maxsprites)
825                 {
826                  if(n==63) sb=1;
827
828                  {
829                   SPRB dst;
830                   uint8 *C;
831                   int t;
832                   unsigned int vadr;
833
834                   t = (int)scanline-(spr->y);
835
836       if(Sprite16)
837        vadr = ((spr->no&1)<<12) + ((spr->no&0xFE)<<4);
838       else
839        vadr = (spr->no<<4)+vofs;
840
841       if(spr->atr&V_FLIP)
842                   {
843                         vadr+=7;
844                         vadr-=t;
845                         vadr+=(P0&0x20)>>1;
846                         vadr-=t&8;
847                   }
848                   else
849                   {
850                         vadr+=t;
851                         vadr+=t&8;
852                   }
853
854                   /* Fix this geniestage hack */
855                   if(MMC5Hack && geniestage!=1) C = MMC5SPRVRAMADR(vadr);
856                   else C = VRAMADR(vadr);
857
858
859                   dst.ca[0]=C[0];
860                   dst.ca[1]=C[8];
861                   dst.x=spr->x;
862                   dst.atr=spr->atr;
863
864                   *(uint32 *)&SPRBUF[ns<<2]=*(uint32 *)&dst;
865                  }
866
867                  ns++;
868                 }
869                 else
870                 {
871                   PPU_status|=0x20;
872                   break;
873                 }
874          }
875         else
876          for(n=63;n>=0;n--,spr++)
877          {
878                 if((unsigned int)(scanline-spr->y)>=H) continue;
879
880                 if(ns<maxsprites)
881                 {
882                  if(n==63) sb=1;
883
884                  {
885                   SPRB dst;
886                   uint8 *C;
887                   int t;
888                   unsigned int vadr;
889
890                   t = (int)scanline-(spr->y);
891
892       if(Sprite16)
893        vadr = ((spr->no&1)<<12) + ((spr->no&0xFE)<<4);
894       else
895        vadr = (spr->no<<4)+vofs;
896
897       if(spr->atr&V_FLIP)
898                   {
899                         vadr+=7;
900                         vadr-=t;
901                         vadr+=(P0&0x20)>>1;
902                         vadr-=t&8;
903                   }
904                   else
905                   {
906                         vadr+=t;
907                         vadr+=t&8;
908                   }
909
910                   if(MMC5Hack) C = MMC5SPRVRAMADR(vadr);
911                   else C = VRAMADR(vadr);
912                   dst.ca[0]=C[0];
913                   if(ns<8)
914                   {
915                    PPU_hook(0x2000);
916                    PPU_hook(vadr);
917                   }
918                   dst.ca[1]=C[8];
919                   dst.x=spr->x;
920                   dst.atr=spr->atr;
921
922
923                   *(uint32 *)&SPRBUF[ns<<2]=*(uint32 *)&dst;
924                  }
925
926                  ns++;
927                 }
928                 else
929                 {
930                   PPU_status|=0x20;
931                   break;
932                 }
933          }
934         //if(ns>=7)
935         //printf("%d %d\n",scanline,ns);
936   if(ns>8) PPU_status|=0x20;  /* Handle case when >8 sprites per
937                                            scanline option is enabled. */
938         else if(PPU_hook)
939         {
940          for(n=0;n<(8-ns);n++)
941          {
942                  PPU_hook(0x2000);
943                  PPU_hook(vofs);
944          }
945         }
946         numsprites=ns;
947         SpriteBlurp=sb;
948 }
949
950 static void RefreshSprites098(void)
951 {
952         int n;
953         SPRB *spr;
954
955         spork=0;
956         if(!numsprites) return;
957
958         FCEU_dwmemset(sprlinebuf,0x80808080,256);
959         numsprites--;
960         spr = (SPRB*)SPRBUF+numsprites;
961
962        for(n=numsprites;n>=0;n--,spr--)
963        {
964          //#ifdef C80x86
965          //register uint32 pixdata asm ("eax");
966          //register uint8 J, atr;
967          //#else
968         register uint32 pixdata;
969         register uint8 J,atr;
970          //#endif
971
972         int x=spr->x;
973         uint8 *C;
974         uint8 *VB;
975
976         pixdata=ppulut1[spr->ca[0]]|ppulut2[spr->ca[1]];
977         J=spr->ca[0]|spr->ca[1];
978         atr=spr->atr;
979
980                        if(J)
981                        {
982                         if(n==0 && SpriteBlurp && !(PPU_status&0x40))
983                         {
984                          sphitx=x;
985                          sphitdata=J;
986                          if(atr&H_FLIP)
987                           sphitdata=    ((J<<7)&0x80) |
988                                         ((J<<5)&0x40) |
989                                         ((J<<3)&0x20) |
990                                         ((J<<1)&0x10) |
991                                         ((J>>1)&0x08) |
992                                         ((J>>3)&0x04) |
993                                         ((J>>5)&0x02) |
994                                         ((J>>7)&0x01);
995                         }
996
997          C = sprlinebuf+x;
998          VB = (PALRAM+0x10)+((atr&3)<<2);
999
1000          if(atr&SP_BACK)
1001          {
1002     if(atr&H_FLIP)
1003           {
1004            if(J&0x80) C[7]=VB[pixdata&3]|0x40;
1005            pixdata>>=4;
1006            if(J&0x40) C[6]=VB[pixdata&3]|0x40;
1007            pixdata>>=4;
1008            if(J&0x20) C[5]=VB[pixdata&3]|0x40;
1009            pixdata>>=4;
1010            if(J&0x10) C[4]=VB[pixdata&3]|0x40;
1011            pixdata>>=4;
1012            if(J&0x08) C[3]=VB[pixdata&3]|0x40;
1013            pixdata>>=4;
1014            if(J&0x04) C[2]=VB[pixdata&3]|0x40;
1015            pixdata>>=4;
1016            if(J&0x02) C[1]=VB[pixdata&3]|0x40;
1017            pixdata>>=4;
1018            if(J&0x01) C[0]=VB[pixdata]|0x40;
1019           } else  {
1020            if(J&0x80) C[0]=VB[pixdata&3]|0x40;
1021            pixdata>>=4;
1022            if(J&0x40) C[1]=VB[pixdata&3]|0x40;
1023            pixdata>>=4;
1024            if(J&0x20) C[2]=VB[pixdata&3]|0x40;
1025            pixdata>>=4;
1026            if(J&0x10) C[3]=VB[pixdata&3]|0x40;
1027            pixdata>>=4;
1028            if(J&0x08) C[4]=VB[pixdata&3]|0x40;
1029            pixdata>>=4;
1030            if(J&0x04) C[5]=VB[pixdata&3]|0x40;
1031            pixdata>>=4;
1032            if(J&0x02) C[6]=VB[pixdata&3]|0x40;
1033            pixdata>>=4;
1034            if(J&0x01) C[7]=VB[pixdata]|0x40;
1035           }
1036          } else {
1037     if(atr&H_FLIP)
1038           {
1039            if(J&0x80) C[7]=VB[pixdata&3];
1040            pixdata>>=4;
1041            if(J&0x40) C[6]=VB[pixdata&3];
1042            pixdata>>=4;
1043            if(J&0x20) C[5]=VB[pixdata&3];
1044            pixdata>>=4;
1045            if(J&0x10) C[4]=VB[pixdata&3];
1046            pixdata>>=4;
1047            if(J&0x08) C[3]=VB[pixdata&3];
1048            pixdata>>=4;
1049            if(J&0x04) C[2]=VB[pixdata&3];
1050            pixdata>>=4;
1051            if(J&0x02) C[1]=VB[pixdata&3];
1052            pixdata>>=4;
1053            if(J&0x01) C[0]=VB[pixdata];
1054           }else{
1055            if(J&0x80) C[0]=VB[pixdata&3];
1056            pixdata>>=4;
1057            if(J&0x40) C[1]=VB[pixdata&3];
1058            pixdata>>=4;
1059            if(J&0x20) C[2]=VB[pixdata&3];
1060            pixdata>>=4;
1061            if(J&0x10) C[3]=VB[pixdata&3];
1062            pixdata>>=4;
1063            if(J&0x08) C[4]=VB[pixdata&3];
1064            pixdata>>=4;
1065            if(J&0x04) C[5]=VB[pixdata&3];
1066            pixdata>>=4;
1067            if(J&0x02) C[6]=VB[pixdata&3];
1068            pixdata>>=4;
1069            if(J&0x01) C[7]=VB[pixdata];
1070           }
1071          }
1072         }
1073       }
1074      SpriteBlurp=0;
1075         spork=1;
1076 }
1077
1078 static void CopySprites098(uint8 *target)
1079 {
1080       uint8 n=((PPU[1]&4)^4)<<1;
1081       uint8 *P=target;
1082
1083       if(!spork) return;
1084       spork=0;
1085
1086       if(rendis & 1) return;  /* User asked to not display sprites. */
1087
1088       loopskie:
1089       {
1090        uint32 t=*(uint32 *)(sprlinebuf+n);
1091
1092        if(t!=0x80808080)
1093        {
1094         #ifdef LSB_FIRST
1095         if(!(t&0x80))
1096         {
1097          if(!(t&0x40) || (P[n]&0x40))       // Normal sprite || behind bg sprite
1098           P[n]=sprlinebuf[n];
1099         }
1100
1101         if(!(t&0x8000))
1102         {
1103          if(!(t&0x4000) || (P[n+1]&0x40))       // Normal sprite || behind bg sprite
1104           P[n+1]=(sprlinebuf+1)[n];
1105         }
1106
1107         if(!(t&0x800000))
1108         {
1109          if(!(t&0x400000) || (P[n+2]&0x40))       // Normal sprite || behind bg sprite
1110           P[n+2]=(sprlinebuf+2)[n];
1111         }
1112
1113         if(!(t&0x80000000))
1114         {
1115          if(!(t&0x40000000) || (P[n+3]&0x40))       // Normal sprite || behind bg sprite
1116           P[n+3]=(sprlinebuf+3)[n];
1117         }
1118         #else
1119         /* TODO:  Simplify */
1120         if(!(t&0x80000000))
1121         {
1122          if(!(t&0x40000000))       // Normal sprite
1123           P[n]=sprlinebuf[n];
1124    else if(P[n]&64)  // behind bg sprite
1125           P[n]=sprlinebuf[n];
1126         }
1127
1128         if(!(t&0x800000))
1129         {
1130          if(!(t&0x400000))       // Normal sprite
1131           P[n+1]=(sprlinebuf+1)[n];
1132    else if(P[n+1]&64)  // behind bg sprite
1133           P[n+1]=(sprlinebuf+1)[n];
1134         }
1135
1136         if(!(t&0x8000))
1137         {
1138          if(!(t&0x4000))       // Normal sprite
1139           P[n+2]=(sprlinebuf+2)[n];
1140    else if(P[n+2]&64)  // behind bg sprite
1141           P[n+2]=(sprlinebuf+2)[n];
1142         }
1143
1144         if(!(t&0x80))
1145         {
1146          if(!(t&0x40))       // Normal sprite
1147           P[n+3]=(sprlinebuf+3)[n];
1148    else if(P[n+3]&64)  // behind bg sprite
1149           P[n+3]=(sprlinebuf+3)[n];
1150         }
1151         #endif
1152        }
1153       }
1154       n+=4;
1155       if(n) goto loopskie;
1156 }
1157
1158 void FCEUPPU_Init(void)
1159 {
1160  makeppulut();
1161 }
1162
1163 void FCEUPPU_Reset(void)
1164 {
1165         VRAMBuffer=PPU[0]=PPU[1]=PPU_status=PPU[3]=0;
1166         PPUSPL=0;
1167         PPUGenLatch=0;
1168         RefreshAddr=TempAddr=0;
1169         vtoggle = 0;
1170         ppudead = 2;
1171         kook = 0;
1172
1173 //      XOffset=0;
1174 }
1175
1176 void FCEUPPU_Power(void)
1177 {
1178         int x;
1179
1180         memset(NTARAM,0x00,0x800);
1181         memset(PALRAM,0x00,0x20);
1182         memset(SPRAM,0x00,0x100);
1183         FCEUPPU_Reset();
1184
1185         for(x=0x2000;x<0x4000;x+=8)
1186         {
1187          ARead[x]=A200x;
1188          BWrite[x]=B2000;
1189          ARead[x+1]=A200x;
1190          BWrite[x+1]=B2001;
1191          ARead[x+2]=A2002;
1192          BWrite[x+2]=B2002;
1193          ARead[x+3]=A200x;
1194          BWrite[x+3]=B2003;
1195          ARead[x+4]=A200x; //A2004;
1196          BWrite[x+4]=B2004;
1197          ARead[x+5]=A200x;
1198          BWrite[x+5]=B2005;
1199          ARead[x+6]=A200x;
1200          BWrite[x+6]=B2006;
1201          ARead[x+7]=A2007;
1202          BWrite[x+7]=B2007;
1203         }
1204         BWrite[0x4014]=B4014;
1205 }
1206
1207
1208 void FCEUPPU_Loop(int skip)
1209 {
1210   uint32 scanlines_per_frame = PAL ? 312 : 262;
1211
1212   if(ppudead) /* Needed for Knight Rider, possibly others. */
1213   {
1214    //memset(XBuf, 0x80, 256*240);
1215    //X6502_Run(scanlines_per_frame*(256+85));
1216    int lines;
1217    for (lines=scanlines_per_frame;lines;lines--)
1218      X6502_Run(256+85);
1219    ppudead--;
1220   }
1221   else
1222   {
1223    X6502_Run(256+85);
1224    PPU_status |= 0x80;
1225    PPU[3]=PPUSPL=0;       /* Not sure if this is correct.  According
1226                                   to Matt Conte and my own tests, it is.  Timing is probably
1227                                   off, though.  NOTE:  Not having this here
1228                                   breaks a Super Donkey Kong game. */
1229                                 /* I need to figure out the true nature and length
1230                                    of this delay.
1231                                 */
1232    X6502_Run(12);
1233    if(FCEUGameInfo.type==GIT_NSF)
1234     DoNSFFrame();
1235    else
1236    {
1237     if(VBlankON)
1238      TriggerNMI();
1239    }
1240    // Note: this is needed for asm core
1241    {
1242     int lines;
1243     X6502_Run(256+85-12);
1244     for (lines=scanlines_per_frame-242-1;lines;lines--)
1245      X6502_Run(256+85);
1246    }
1247    PPU_status&=0x1f;
1248    X6502_Run(256);
1249
1250    {
1251     int x;
1252
1253     if(ScreenON || SpriteON)
1254     {
1255      if(GameHBIRQHook && ((PPU[0]&0x38)!=0x18))
1256       GameHBIRQHook();
1257      if(PPU_hook)
1258       for(x=0;x<42;x++) {PPU_hook(0x2000); PPU_hook(0);}
1259      if(GameHBIRQHook2)
1260       GameHBIRQHook2();
1261     }
1262     X6502_Run(85-16);
1263     if(ScreenON || SpriteON)
1264     {
1265      RefreshAddr=TempAddr;
1266      if(PPU_hook) PPU_hook(RefreshAddr&0x3fff);
1267     }
1268
1269     /* Clean this stuff up later. */
1270     spork=numsprites=0;
1271     ResetRL(XBuf+32);
1272
1273     X6502_Run(16-kook);
1274     kook ^= 1;
1275    }
1276    if(FCEUGameInfo.type==GIT_NSF)
1277    {
1278     // run scanlines for asm core to fuction
1279     for(scanline=0;scanline<240;scanline++)
1280       X6502_Run(256+85);
1281    }
1282    #ifdef FRAMESKIP
1283    else if(skip)
1284    {
1285     int y, lines;
1286
1287     y=SPRAM[0];
1288     y++;
1289
1290     PPU_status|=0x20;       // Fixes "Bee 52".  Does it break anything?
1291     if(GameHBIRQHook)
1292     {
1293      X6502_Run(256);
1294      for(scanline=0;scanline<240;scanline++)
1295      {
1296       if(ScreenON || SpriteON)
1297        GameHBIRQHook();
1298       if(scanline==y && SpriteON) PPU_status|=0x40;
1299       X6502_Run((scanline==239)?85:(256+85));
1300      }
1301     }
1302     else if(y<240)
1303     {
1304      for (lines=y;lines;lines--)
1305       X6502_Run(256+85);
1306      if(SpriteON) PPU_status|=0x40; // Quick and very dirty hack.
1307      for (lines=240-y;lines;lines--)
1308       X6502_Run(256+85);
1309     }
1310     else
1311     {
1312      for (lines=240;lines;lines--)
1313       X6502_Run(256+85);
1314     }
1315    }
1316    #endif
1317    else
1318    {
1319     int x,max,maxref;
1320
1321     deemp=PPU[1]>>5;
1322     for(scanline=0;scanline<240;)       //scanline is incremented in  DoLine.  Evil. :/
1323     {
1324      deempcnt[deemp]++;
1325 #ifdef WIN32
1326      if((PPUViewer) && (scanline == PPUViewScanline)) UpdatePPUView(1);
1327 #endif
1328      DoLine();
1329     }
1330     if(MMC5Hack && (ScreenON || SpriteON)) MMC5_hb(scanline);
1331     for(x=1,max=0,maxref=0;x<7;x++)
1332     {
1333      if(deempcnt[x]>max)
1334      {
1335       max=deempcnt[x];
1336       maxref=x;
1337      }
1338      deempcnt[x]=0;
1339     }
1340     //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);
1341     //memset(deempcnt,0,sizeof(deempcnt));
1342     SetNESDeemph(maxref,0);
1343    }
1344   } /* else... to if(ppudead) */
1345 }
1346
1347