removed 098 snd due to problems with it
[fceu.git] / ppu098.c
CommitLineData
6244011f 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"
77887306 29#include "sound.h"
6244011f 30#include "memory.h"
31
32#include "cart.h"
33#include "palette.h"
34#include "video.h"
35#include "input.h"
36
77887306 37#ifdef GP2X
38#include "drivers/gp2x/asmutils.h"
39#endif
40
6244011f 41#define Pal (PALRAM)
42
43static void FetchSpriteData098(void);
44static void FASTAPASS(1) RefreshLine098(int lastpixel);
45static void RefreshSprites098(void);
46static void CopySprites098(uint8 *target);
47
48static void Fixit1(void);
49static uint32 ppulut1[256];
50static uint32 ppulut2[256];
51static uint32 ppulut3[128];
52
53static 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
6244011f 89#ifdef ASM_6502
77887306 90static 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}
6244011f 115#endif
116
117extern int ppudead;
118extern int kook;
119
120/* Color deemphasis emulation. Joy... */
121static uint8 deemp=0;
122static int deempcnt[8];
123
124extern int maxsprites;
125
126extern uint8 PPUSPL;
127extern uint8 SPRAM[0x100];
128extern 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
136static 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
152static DECLFR(A200x) /* Not correct for $2004 reads. */
153{
154 FCEUPPU_LineUpdate098();
155 return PPUGenLatch;
156}
157
158/*
159static 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*/
183static 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
212static 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();
77887306 222 TriggerNMI(); // TODO?
6244011f 223 }
224 PPU[0]=V;
225 TempAddr&=0xF3FF;
226 TempAddr|=(V&3)<<10;
227}
228
229static 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
239static DECLFW(B2002)
240{
241 PPUGenLatch=V;
242}
243
244static 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
252static 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
272static 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
294static 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
317static 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
344static 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
360static uint8 *Pline=0,*Plinef;
361static int firsttile;
362static int linestartts;
363static int tofix=0;
364
365static void ResetRL(uint8 *target)
366{
77887306 367 FCEU_dwmemset(target,0xffffffff,256);
6244011f 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
379extern uint8 sprlinebuf[256+8];
380
381void FCEUPPU_LineUpdate098(void)
382{
383 if(Pline)
384 {
385 int l=GETLASTPIXEL;
386 RefreshLine098(l);
387 }
388}
389
390static int rendis = 0;
391
392void 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
407static void CheckSpriteHit(int p);
408
409static void EndRL(void)
410{
411 RefreshLine098(272);
412 if(tofix)
413 Fixit1();
414 CheckSpriteHit(272);
415 Pline=0;
416}
417
418static int32 sphitx;
419static uint8 sphitdata;
420
421static 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}
442static 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."
448static 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
654static 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
667static 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
695void MMC5_hb(int); /* Ugh ugh ugh. */
696static void DoLine(void)
697{
698 int x;
699 uint8 *target=XBuf+scanline*320+32;
700
701 if(MMC5Hack && (ScreenON || SpriteON)) MMC5_hb(scanline);
702
703 X6502_Run(256);
704 EndRL();
705
706 if(rendis & 2) /* User asked to not display background data. */
707 {
708 uint32 tem;
709 tem=Pal[0]|(Pal[0]<<8)|(Pal[0]<<16)|(Pal[0]<<24);
710 tem|=0x40404040;
711 FCEU_dwmemset(target,tem,256);
712 }
713
714 if(SpriteON)
715 CopySprites098(target);
716
77887306 717#ifdef GP2X
718 if(ScreenON || SpriteON) // Yes, very el-cheapo.
719 {
720 if(PPU[1]&0x01)
721 block_and(target, 256, 0x30);
722 }
723 if((PPU[1]>>5)==0x7)
724 block_or(target, 256, 0xc0);
725 else if(PPU[1]&0xE0)
726 block_or(target, 256, 0x40);
727 else
728 block_andor(target, 256, 0x3f, 0x80);
729#else
6244011f 730 if(ScreenON || SpriteON) // Yes, very el-cheapo.
731 {
732 if(PPU[1]&0x01)
733 {
734 for(x=63;x>=0;x--)
735 *(uint32 *)&target[x<<2]=(*(uint32*)&target[x<<2])&0x30303030;
736 }
737 }
738 if((PPU[1]>>5)==0x7)
739 {
740 for(x=63;x>=0;x--)
741 *(uint32 *)&target[x<<2]=((*(uint32*)&target[x<<2])&0x3f3f3f3f)|0xc0c0c0c0;
742 }
743 else if(PPU[1]&0xE0)
744 for(x=63;x>=0;x--)
745 *(uint32 *)&target[x<<2]=(*(uint32*)&target[x<<2])|0x40404040;
746 else
747 for(x=63;x>=0;x--)
748 *(uint32 *)&target[x<<2]=((*(uint32*)&target[x<<2])&0x3f3f3f3f)|0x80808080;
77887306 749#endif
6244011f 750
751 sphitx=0x100;
752
753 if(ScreenON || SpriteON)
754 FetchSpriteData098();
755
756 if(GameHBIRQHook && (ScreenON || SpriteON) && ((PPU[0]&0x38)!=0x18))
757 {
758 X6502_Run(6);
759 Fixit2();
760 X6502_Run(4);
761 GameHBIRQHook();
762 X6502_Run(85-16-10);
763 }
764 else
765 {
766 X6502_Run(6); // Tried 65, caused problems with Slalom(maybe others)
767 Fixit2();
768 X6502_Run(85-6-16);
769
770 // A semi-hack for Star Trek: 25th Anniversary
771 if(GameHBIRQHook && (ScreenON || SpriteON) && ((PPU[0]&0x38)!=0x18))
772 GameHBIRQHook();
773 }
774
775 if(SpriteON)
776 RefreshSprites098();
777 if(GameHBIRQHook2 && (ScreenON || SpriteON))
778 GameHBIRQHook2();
779 scanline++;
780 if(scanline<240)
781 {
782 ResetRL(XBuf+scanline*320+32);
783 }
784 X6502_Run(16);
785}
786
787#define V_FLIP 0x80
788#define H_FLIP 0x40
789#define SP_BACK 0x20
790
791typedef struct {
792 uint8 y,no,atr,x;
793} SPR;
794
795typedef struct {
796 uint8 ca[2],atr,x;
797} SPRB;
798
799static uint8 numsprites,SpriteBlurp;
800static void FetchSpriteData098(void)
801{
802 uint8 ns,sb;
803 SPR *spr;
804 uint8 H;
805 int n;
806 int vofs;
807 uint8 P0=PPU[0];
808
809 spr=(SPR *)SPRAM;
810 H=8;
811
812 ns=sb=0;
813
814 vofs=(unsigned int)(P0&0x8&(((P0&0x20)^0x20)>>2))<<9;
815 H+=(P0&0x20)>>2;
816
817 if(!PPU_hook)
818 for(n=63;n>=0;n--,spr++)
819 {
820 if((unsigned int)(scanline-spr->y)>=H) continue;
821 //printf("%d, %u\n",scanline,(unsigned int)(scanline-spr->y));
822 if(ns<maxsprites)
823 {
824 if(n==63) sb=1;
825
826 {
827 SPRB dst;
828 uint8 *C;
829 int t;
830 unsigned int vadr;
831
832 t = (int)scanline-(spr->y);
833
834 if(Sprite16)
835 vadr = ((spr->no&1)<<12) + ((spr->no&0xFE)<<4);
836 else
837 vadr = (spr->no<<4)+vofs;
838
839 if(spr->atr&V_FLIP)
840 {
841 vadr+=7;
842 vadr-=t;
843 vadr+=(P0&0x20)>>1;
844 vadr-=t&8;
845 }
846 else
847 {
848 vadr+=t;
849 vadr+=t&8;
850 }
851
852 /* Fix this geniestage hack */
853 if(MMC5Hack && geniestage!=1) C = MMC5SPRVRAMADR(vadr);
854 else C = VRAMADR(vadr);
855
856
857 dst.ca[0]=C[0];
858 dst.ca[1]=C[8];
859 dst.x=spr->x;
860 dst.atr=spr->atr;
861
862 *(uint32 *)&SPRBUF[ns<<2]=*(uint32 *)&dst;
863 }
864
865 ns++;
866 }
867 else
868 {
869 PPU_status|=0x20;
870 break;
871 }
872 }
873 else
874 for(n=63;n>=0;n--,spr++)
875 {
876 if((unsigned int)(scanline-spr->y)>=H) continue;
877
878 if(ns<maxsprites)
879 {
880 if(n==63) sb=1;
881
882 {
883 SPRB dst;
884 uint8 *C;
885 int t;
886 unsigned int vadr;
887
888 t = (int)scanline-(spr->y);
889
890 if(Sprite16)
891 vadr = ((spr->no&1)<<12) + ((spr->no&0xFE)<<4);
892 else
893 vadr = (spr->no<<4)+vofs;
894
895 if(spr->atr&V_FLIP)
896 {
897 vadr+=7;
898 vadr-=t;
899 vadr+=(P0&0x20)>>1;
900 vadr-=t&8;
901 }
902 else
903 {
904 vadr+=t;
905 vadr+=t&8;
906 }
907
908 if(MMC5Hack) C = MMC5SPRVRAMADR(vadr);
909 else C = VRAMADR(vadr);
910 dst.ca[0]=C[0];
911 if(ns<8)
912 {
913 PPU_hook(0x2000);
914 PPU_hook(vadr);
915 }
916 dst.ca[1]=C[8];
917 dst.x=spr->x;
918 dst.atr=spr->atr;
919
920
921 *(uint32 *)&SPRBUF[ns<<2]=*(uint32 *)&dst;
922 }
923
924 ns++;
925 }
926 else
927 {
928 PPU_status|=0x20;
929 break;
930 }
931 }
932 //if(ns>=7)
933 //printf("%d %d\n",scanline,ns);
934 if(ns>8) PPU_status|=0x20; /* Handle case when >8 sprites per
935 scanline option is enabled. */
936 else if(PPU_hook)
937 {
938 for(n=0;n<(8-ns);n++)
939 {
940 PPU_hook(0x2000);
941 PPU_hook(vofs);
942 }
943 }
944 numsprites=ns;
945 SpriteBlurp=sb;
946}
947
948static void RefreshSprites098(void)
949{
950 int n;
951 SPRB *spr;
952
953 spork=0;
954 if(!numsprites) return;
955
956 FCEU_dwmemset(sprlinebuf,0x80808080,256);
957 numsprites--;
958 spr = (SPRB*)SPRBUF+numsprites;
959
960 for(n=numsprites;n>=0;n--,spr--)
961 {
962 //#ifdef C80x86
963 //register uint32 pixdata asm ("eax");
964 //register uint8 J, atr;
965 //#else
966 register uint32 pixdata;
967 register uint8 J,atr;
968 //#endif
969
970 int x=spr->x;
971 uint8 *C;
972 uint8 *VB;
973
974 pixdata=ppulut1[spr->ca[0]]|ppulut2[spr->ca[1]];
975 J=spr->ca[0]|spr->ca[1];
976 atr=spr->atr;
977
978 if(J)
979 {
980 if(n==0 && SpriteBlurp && !(PPU_status&0x40))
981 {
982 sphitx=x;
983 sphitdata=J;
984 if(atr&H_FLIP)
985 sphitdata= ((J<<7)&0x80) |
986 ((J<<5)&0x40) |
987 ((J<<3)&0x20) |
988 ((J<<1)&0x10) |
989 ((J>>1)&0x08) |
990 ((J>>3)&0x04) |
991 ((J>>5)&0x02) |
992 ((J>>7)&0x01);
993 }
994
995 C = sprlinebuf+x;
996 VB = (PALRAM+0x10)+((atr&3)<<2);
997
998 if(atr&SP_BACK)
999 {
1000 if(atr&H_FLIP)
1001 {
1002 if(J&0x80) C[7]=VB[pixdata&3]|0x40;
1003 pixdata>>=4;
1004 if(J&0x40) C[6]=VB[pixdata&3]|0x40;
1005 pixdata>>=4;
1006 if(J&0x20) C[5]=VB[pixdata&3]|0x40;
1007 pixdata>>=4;
1008 if(J&0x10) C[4]=VB[pixdata&3]|0x40;
1009 pixdata>>=4;
1010 if(J&0x08) C[3]=VB[pixdata&3]|0x40;
1011 pixdata>>=4;
1012 if(J&0x04) C[2]=VB[pixdata&3]|0x40;
1013 pixdata>>=4;
1014 if(J&0x02) C[1]=VB[pixdata&3]|0x40;
1015 pixdata>>=4;
1016 if(J&0x01) C[0]=VB[pixdata]|0x40;
1017 } else {
1018 if(J&0x80) C[0]=VB[pixdata&3]|0x40;
1019 pixdata>>=4;
1020 if(J&0x40) C[1]=VB[pixdata&3]|0x40;
1021 pixdata>>=4;
1022 if(J&0x20) C[2]=VB[pixdata&3]|0x40;
1023 pixdata>>=4;
1024 if(J&0x10) C[3]=VB[pixdata&3]|0x40;
1025 pixdata>>=4;
1026 if(J&0x08) C[4]=VB[pixdata&3]|0x40;
1027 pixdata>>=4;
1028 if(J&0x04) C[5]=VB[pixdata&3]|0x40;
1029 pixdata>>=4;
1030 if(J&0x02) C[6]=VB[pixdata&3]|0x40;
1031 pixdata>>=4;
1032 if(J&0x01) C[7]=VB[pixdata]|0x40;
1033 }
1034 } else {
1035 if(atr&H_FLIP)
1036 {
1037 if(J&0x80) C[7]=VB[pixdata&3];
1038 pixdata>>=4;
1039 if(J&0x40) C[6]=VB[pixdata&3];
1040 pixdata>>=4;
1041 if(J&0x20) C[5]=VB[pixdata&3];
1042 pixdata>>=4;
1043 if(J&0x10) C[4]=VB[pixdata&3];
1044 pixdata>>=4;
1045 if(J&0x08) C[3]=VB[pixdata&3];
1046 pixdata>>=4;
1047 if(J&0x04) C[2]=VB[pixdata&3];
1048 pixdata>>=4;
1049 if(J&0x02) C[1]=VB[pixdata&3];
1050 pixdata>>=4;
1051 if(J&0x01) C[0]=VB[pixdata];
1052 }else{
1053 if(J&0x80) C[0]=VB[pixdata&3];
1054 pixdata>>=4;
1055 if(J&0x40) C[1]=VB[pixdata&3];
1056 pixdata>>=4;
1057 if(J&0x20) C[2]=VB[pixdata&3];
1058 pixdata>>=4;
1059 if(J&0x10) C[3]=VB[pixdata&3];
1060 pixdata>>=4;
1061 if(J&0x08) C[4]=VB[pixdata&3];
1062 pixdata>>=4;
1063 if(J&0x04) C[5]=VB[pixdata&3];
1064 pixdata>>=4;
1065 if(J&0x02) C[6]=VB[pixdata&3];
1066 pixdata>>=4;
1067 if(J&0x01) C[7]=VB[pixdata];
1068 }
1069 }
1070 }
1071 }
1072 SpriteBlurp=0;
1073 spork=1;
1074}
1075
1076static void CopySprites098(uint8 *target)
1077{
1078 uint8 n=((PPU[1]&4)^4)<<1;
1079 uint8 *P=target;
1080
1081 if(!spork) return;
1082 spork=0;
1083
1084 if(rendis & 1) return; /* User asked to not display sprites. */
1085
1086 loopskie:
1087 {
1088 uint32 t=*(uint32 *)(sprlinebuf+n);
1089
1090 if(t!=0x80808080)
1091 {
1092 #ifdef LSB_FIRST
1093 if(!(t&0x80))
1094 {
1095 if(!(t&0x40) || (P[n]&0x40)) // Normal sprite || behind bg sprite
1096 P[n]=sprlinebuf[n];
1097 }
1098
1099 if(!(t&0x8000))
1100 {
1101 if(!(t&0x4000) || (P[n+1]&0x40)) // Normal sprite || behind bg sprite
1102 P[n+1]=(sprlinebuf+1)[n];
1103 }
1104
1105 if(!(t&0x800000))
1106 {
1107 if(!(t&0x400000) || (P[n+2]&0x40)) // Normal sprite || behind bg sprite
1108 P[n+2]=(sprlinebuf+2)[n];
1109 }
1110
1111 if(!(t&0x80000000))
1112 {
1113 if(!(t&0x40000000) || (P[n+3]&0x40)) // Normal sprite || behind bg sprite
1114 P[n+3]=(sprlinebuf+3)[n];
1115 }
1116 #else
1117 /* TODO: Simplify */
1118 if(!(t&0x80000000))
1119 {
1120 if(!(t&0x40000000)) // Normal sprite
1121 P[n]=sprlinebuf[n];
1122 else if(P[n]&64) // behind bg sprite
1123 P[n]=sprlinebuf[n];
1124 }
1125
1126 if(!(t&0x800000))
1127 {
1128 if(!(t&0x400000)) // Normal sprite
1129 P[n+1]=(sprlinebuf+1)[n];
1130 else if(P[n+1]&64) // behind bg sprite
1131 P[n+1]=(sprlinebuf+1)[n];
1132 }
1133
1134 if(!(t&0x8000))
1135 {
1136 if(!(t&0x4000)) // Normal sprite
1137 P[n+2]=(sprlinebuf+2)[n];
1138 else if(P[n+2]&64) // behind bg sprite
1139 P[n+2]=(sprlinebuf+2)[n];
1140 }
1141
1142 if(!(t&0x80))
1143 {
1144 if(!(t&0x40)) // Normal sprite
1145 P[n+3]=(sprlinebuf+3)[n];
1146 else if(P[n+3]&64) // behind bg sprite
1147 P[n+3]=(sprlinebuf+3)[n];
1148 }
1149 #endif
1150 }
1151 }
1152 n+=4;
1153 if(n) goto loopskie;
1154}
1155
1156void FCEUPPU_Init(void)
1157{
1158 makeppulut();
1159}
1160
1161void FCEUPPU_Reset(void)
1162{
1163 VRAMBuffer=PPU[0]=PPU[1]=PPU_status=PPU[3]=0;
1164 PPUSPL=0;
1165 PPUGenLatch=0;
1166 RefreshAddr=TempAddr=0;
1167 vtoggle = 0;
1168 ppudead = 2;
1169 kook = 0;
1170
1171// XOffset=0;
1172}
1173
1174void FCEUPPU_Power(void)
1175{
1176 int x;
1177
1178 memset(NTARAM,0x00,0x800);
1179 memset(PALRAM,0x00,0x20);
1180 memset(SPRAM,0x00,0x100);
1181 FCEUPPU_Reset();
1182
1183 for(x=0x2000;x<0x4000;x+=8)
1184 {
1185 ARead[x]=A200x;
1186 BWrite[x]=B2000;
1187 ARead[x+1]=A200x;
1188 BWrite[x+1]=B2001;
1189 ARead[x+2]=A2002;
1190 BWrite[x+2]=B2002;
1191 ARead[x+3]=A200x;
1192 BWrite[x+3]=B2003;
1193 ARead[x+4]=A200x; //A2004;
1194 BWrite[x+4]=B2004;
1195 ARead[x+5]=A200x;
1196 BWrite[x+5]=B2005;
1197 ARead[x+6]=A200x;
1198 BWrite[x+6]=B2006;
1199 ARead[x+7]=A2007;
1200 BWrite[x+7]=B2007;
1201 }
1202 BWrite[0x4014]=B4014;
1203}
1204
1205
1206void FCEUPPU_Loop(int skip)
1207{
1208 uint32 scanlines_per_frame = PAL ? 312 : 262;
1209
1210 if(ppudead) /* Needed for Knight Rider, possibly others. */
1211 {
1212 //memset(XBuf, 0x80, 256*240);
a384bf44 1213 //X6502_Run(scanlines_per_frame*(256+85));
1214 int lines;
1215 for (lines=scanlines_per_frame;lines;lines--)
1216 X6502_Run(256+85);
6244011f 1217 ppudead--;
1218 }
1219 else
1220 {
1221 X6502_Run(256+85);
1222 PPU_status |= 0x80;
1223 PPU[3]=PPUSPL=0; /* Not sure if this is correct. According
1224 to Matt Conte and my own tests, it is. Timing is probably
1225 off, though. NOTE: Not having this here
1226 breaks a Super Donkey Kong game. */
1227 /* I need to figure out the true nature and length
1228 of this delay.
1229 */
1230 X6502_Run(12);
1231 if(FCEUGameInfo.type==GIT_NSF)
1232 DoNSFFrame();
1233 else
1234 {
1235 if(VBlankON)
1236 TriggerNMI();
1237 }
a384bf44 1238 // Note: this is needed for asm core
1239 {
1240 int lines;
1241 X6502_Run(256+85-12);
1242 for (lines=scanlines_per_frame-242-1;lines;lines--)
1243 X6502_Run(256+85);
1244 }
6244011f 1245 PPU_status&=0x1f;
1246 X6502_Run(256);
1247
1248 {
1249 int x;
1250
1251 if(ScreenON || SpriteON)
1252 {
1253 if(GameHBIRQHook && ((PPU[0]&0x38)!=0x18))
1254 GameHBIRQHook();
1255 if(PPU_hook)
1256 for(x=0;x<42;x++) {PPU_hook(0x2000); PPU_hook(0);}
1257 if(GameHBIRQHook2)
1258 GameHBIRQHook2();
1259 }
1260 X6502_Run(85-16);
1261 if(ScreenON || SpriteON)
1262 {
1263 RefreshAddr=TempAddr;
1264 if(PPU_hook) PPU_hook(RefreshAddr&0x3fff);
1265 }
1266
1267 /* Clean this stuff up later. */
1268 spork=numsprites=0;
1269 ResetRL(XBuf+32);
1270
1271 X6502_Run(16-kook);
1272 kook ^= 1;
1273 }
1274 if(FCEUGameInfo.type==GIT_NSF)
1275 {
a384bf44 1276 // run scanlines for asm core to fuction
1277 for(scanline=0;scanline<240;scanline++)
1278 X6502_Run(256+85);
6244011f 1279 }
1280 #ifdef FRAMESKIP
1281 else if(skip)
1282 {
a384bf44 1283 int y, lines;
6244011f 1284
1285 y=SPRAM[0];
1286 y++;
1287
1288 PPU_status|=0x20; // Fixes "Bee 52". Does it break anything?
1289 if(GameHBIRQHook)
1290 {
1291 X6502_Run(256);
1292 for(scanline=0;scanline<240;scanline++)
1293 {
1294 if(ScreenON || SpriteON)
1295 GameHBIRQHook();
1296 if(scanline==y && SpriteON) PPU_status|=0x40;
1297 X6502_Run((scanline==239)?85:(256+85));
1298 }
1299 }
1300 else if(y<240)
1301 {
a384bf44 1302 for (lines=y;lines;lines--)
1303 X6502_Run(256+85);
6244011f 1304 if(SpriteON) PPU_status|=0x40; // Quick and very dirty hack.
a384bf44 1305 for (lines=240-y;lines;lines--)
1306 X6502_Run(256+85);
6244011f 1307 }
1308 else
a384bf44 1309 {
1310 for (lines=240;lines;lines--)
1311 X6502_Run(256+85);
1312 }
6244011f 1313 }
1314 #endif
1315 else
1316 {
1317 int x,max,maxref;
1318
1319 deemp=PPU[1]>>5;
1320 for(scanline=0;scanline<240;) //scanline is incremented in DoLine. Evil. :/
1321 {
1322 deempcnt[deemp]++;
1323#ifdef WIN32
1324 if((PPUViewer) && (scanline == PPUViewScanline)) UpdatePPUView(1);
1325#endif
1326 DoLine();
1327 }
1328 if(MMC5Hack && (ScreenON || SpriteON)) MMC5_hb(scanline);
1329 for(x=1,max=0,maxref=0;x<7;x++)
1330 {
1331 if(deempcnt[x]>max)
1332 {
1333 max=deempcnt[x];
1334 maxref=x;
1335 }
1336 deempcnt[x]=0;
1337 }
1338 //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);
1339 //memset(deempcnt,0,sizeof(deempcnt));
1340 SetNESDeemph(maxref,0);
1341 }
1342 } /* else... to if(ppudead) */
1343}
1344
1345