home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 6 / AACD06.ISO / AACD / Emulation / AmigaVGB / Common.h < prev    next >
C/C++ Source or Header  |  2000-07-06  |  9KB  |  272 lines

  1. /** VGB: portable GameBoy emulator ***************************/
  2. /**                                                         **/
  3. /**                        Common.h                         **/
  4. /**                                                         **/
  5. /** This file contains parts of the drivers which are       **/
  6. /** common for both Unix/X and MSDOS.                       **/
  7. /**                                                         **/
  8. /** Copyright (C) Marat Fayzullin 1995,1996                 **/
  9. /**               John Stiles     1996                      **/
  10. /**               Marcel de Kogel 1996                      **/
  11. /**     You are not allowed to distribute this software     **/
  12. /**     commercially. Please, notify me, if you make any    **/   
  13. /**     changes to this file.                               **/
  14. /*************************************************************/
  15.  
  16. /** USE_XPAL *************************************************/
  17. /** When defined, it makes Common.h drivers use XPal[] to   **/
  18. /** obtain color numbers (when there is no 1:1              **/
  19. /** correspondence, for example in X11).                    **/
  20. /*************************************************************/
  21. /* #define USE_XPAL */
  22.  
  23. #ifdef USE_XPAL
  24. #define XPAL(N) XPal[N]
  25. #else
  26. #define XPAL(N) N
  27. #endif
  28.  
  29. /****************************************************************/
  30. /*** Refresh a line.                                          ***/
  31. /****************************************************************/
  32. void RefreshLine(byte Y)
  33. {
  34.   register byte Offset,*P,*T,*R,*Z,Z0,D0,X1,X2,Shift;
  35.   unsigned int D1;
  36.  
  37. #ifdef USE_XPAL
  38. #define PAL(N)  Pal[N]
  39. #define WPAL(N) WPal[N]
  40.   byte Pal[4],WPal[4];
  41.   Pal[0]=XPal[BPal[0]];Pal[1]=XPal[BPal[1]];
  42.   Pal[2]=XPal[BPal[2]];Pal[3]=XPal[BPal[3]];
  43.   WPal[0]=(XPal+8)[BPal[0]];WPal[1]=(XPal+8)[BPal[1]];
  44.   WPal[2]=(XPal+8)[BPal[2]];WPal[3]=(XPal+8)[BPal[3]];
  45. #else
  46. #define PAL(N)  BPal[N]
  47. #define WPAL(N) BPal[N]+8
  48. #endif
  49.  
  50.   D1=WIDTH*Y+(WIDTH-160)/2;
  51.   R=XBuf+D1;Z=ZBuf+(D1>>3);
  52.  
  53.   if((LCDCONT&0x81)!=0x81)
  54.   {
  55.     /* Clearing screen buffer */
  56.     memset(R,XPAL(0),160);
  57.     /* Clearing Z-buffer */
  58.     memset(Z,0x00,160/8);
  59.     memset(Z+WIDTH*HEIGHT/8,0x00,160/8);
  60.   }
  61.   else
  62.   {
  63.     Offset=Y+SCROLLY;
  64.     T=BgdTab+((int)(Offset&0xF8)<<2);
  65.     Offset=(Offset&0x07)<<1;
  66.  
  67.     Shift=SCROLLX&0x07;
  68.     R-=Shift;Z--;
  69.     X1=SCROLLX>>3;
  70.  
  71.     /* Determine how much background is shown */
  72.     X2=(WNDPOSX>7)? WNDPOSX-7:0;
  73.     if((~LCDCONT&0x20)||(WNDPOSY>Y)||(X2>159)) X2=160;
  74.     X2=X1+((X2+7)>>3);
  75.  
  76.     Z[0]=0x00;
  77.  
  78.     for(;X1<=X2;X1++)
  79.     {
  80.       D0=*(T+(X1&0x1F));
  81.       if(~LCDCONT&0x10) D0+=0x80;
  82.       P=ChrGen+(D0<<4)+Offset;
  83.       D0=*P;
  84.  
  85.       /* Modifying Z-buffer */
  86.       D1=(int)(D0|*(P+1))<<Shift;
  87.       Z[0]|=D1>>8;
  88.       Z[1]=D1&0xFF;
  89.       Z[WIDTH*HEIGHT/8]=0x00;
  90.  
  91.       /* Modifying screen buffer */
  92.       D1=(int)*(P+1)<<1;
  93.       D1=(D1&0xAAA)|((D1&0x555)<<7)|(D0&0x55)|((int)(D0&0xAA)<<7);
  94.       R[0]=PAL((D1&0xC000)>>14);
  95.       R[1]=PAL((D1&0x00C0)>>6 );
  96.       R[2]=PAL((D1&0x3000)>>12);
  97.       R[3]=PAL((D1&0x0030)>>4 );
  98.       R[4]=PAL((D1&0x0C00)>>10);
  99.       R[5]=PAL((D1&0x000C)>>2 );
  100.       R[6]=PAL((D1&0x0300)>>8 );
  101.       R[7]=PAL( D1&0x003      );
  102.  
  103.       /* Moving pointers */
  104.       R+=8;Z++;
  105.     }
  106.   }
  107.  
  108.   X1=(WNDPOSX>7)? WNDPOSX-7:0;
  109.   if(((LCDCONT&0xA0)==0xA0)&&(WNDPOSY<=Y)&&(X1<160))
  110.   {
  111.     D1=WIDTH*Y+(WIDTH-160)/2+X1;
  112.     R=XBuf+D1;Z=ZBuf+(D1>>3);
  113.  
  114.     Offset=Y-WNDPOSY;
  115.     T=WndTab+((int)(Offset&0xF8)<<2);
  116.     Offset=(Offset&0x07)<<1;
  117.  
  118.     D0=0xFF>>(X1&0x07);
  119.     if(LCDCONT&0x02) Z[WIDTH*HEIGHT/8]=Z0=0x00;
  120.     else { Z[WIDTH*HEIGHT/8]=D0;Z0=0xFF; }
  121.     Z[0]|=D0;Z++;
  122.  
  123.     for(X1>>=3;X1<(160>>3);X1++)
  124.     {
  125.       D0=*(T++)+0x80;
  126.       P=RAM+0x8800+((int)D0<<4)+Offset;
  127.  
  128.       /* Modifying Z-buffer */
  129.       Z[WIDTH*HEIGHT/8]=Z0;Z[0]|=0xFF;
  130.  
  131.       /* Modifying screen buffer */
  132.       D0=*P;D1=(int)*(P+1)<<1;
  133.       D1=(D1&0xAAA)|((D1&0x555)<<7)|(D0&0x55)|((int)(D0&0xAA)<<7);
  134.       R[0]=WPAL((D1&0xC000)>>14);
  135.       R[1]=WPAL((D1&0x00C0)>>6 );
  136.       R[2]=WPAL((D1&0x3000)>>12);
  137.       R[3]=WPAL((D1&0x0030)>>4 );
  138.       R[4]=WPAL((D1&0x0C00)>>10);
  139.       R[5]=WPAL((D1&0x000C)>>2 );
  140.       R[6]=WPAL((D1&0x0300)>>8 );
  141.       R[7]=WPAL( D1&0x0003     );
  142.  
  143.       /* Moving pointers */
  144.       R+=8;Z++;
  145.     }
  146.   }
  147. }
  148.  
  149.  
  150. /****************************************************************/
  151. /*** Refresh sprites.                                         ***/
  152. /****************************************************************/
  153. void RefreshSprites(void)
  154. {
  155.   /* Conversion matrix where result has bits */
  156.   /* flipped over and inverted.              */
  157.   static byte Conv[256] =
  158.   {
  159.     0xFF,0x7F,0xBF,0x3F,0xDF,0x5F,0x9F,0x1F,0xEF,0x6F,0xAF,0x2F,
  160.     0xCF,0x4F,0x8F,0x0F,0xF7,0x77,0xB7,0x37,0xD7,0x57,0x97,0x17,
  161.     0xE7,0x67,0xA7,0x27,0xC7,0x47,0x87,0x07,0xFB,0x7B,0xBB,0x3B,
  162.     0xDB,0x5B,0x9B,0x1B,0xEB,0x6B,0xAB,0x2B,0xCB,0x4B,0x8B,0x0B,
  163.     0xF3,0x73,0xB3,0x33,0xD3,0x53,0x93,0x13,0xE3,0x63,0xA3,0x23,
  164.     0xC3,0x43,0x83,0x03,0xFD,0x7D,0xBD,0x3D,0xDD,0x5D,0x9D,0x1D,
  165.     0xED,0x6D,0xAD,0x2D,0xCD,0x4D,0x8D,0x0D,0xF5,0x75,0xB5,0x35,
  166.     0xD5,0x55,0x95,0x15,0xE5,0x65,0xA5,0x25,0xC5,0x45,0x85,0x05,
  167.     0xF9,0x79,0xB9,0x39,0xD9,0x59,0x99,0x19,0xE9,0x69,0xA9,0x29,
  168.     0xC9,0x49,0x89,0x09,0xF1,0x71,0xB1,0x31,0xD1,0x51,0x91,0x11,
  169.     0xE1,0x61,0xA1,0x21,0xC1,0x41,0x81,0x01,0xFE,0x7E,0xBE,0x3E,
  170.     0xDE,0x5E,0x9E,0x1E,0xEE,0x6E,0xAE,0x2E,0xCE,0x4E,0x8E,0x0E,
  171.     0xF6,0x76,0xB6,0x36,0xD6,0x56,0x96,0x16,0xE6,0x66,0xA6,0x26,
  172.     0xC6,0x46,0x86,0x06,0xFA,0x7A,0xBA,0x3A,0xDA,0x5A,0x9A,0x1A,
  173.     0xEA,0x6A,0xAA,0x2A,0xCA,0x4A,0x8A,0x0A,0xF2,0x72,0xB2,0x32,
  174.     0xD2,0x52,0x92,0x12,0xE2,0x62,0xA2,0x22,0xC2,0x42,0x82,0x02,
  175.     0xFC,0x7C,0xBC,0x3C,0xDC,0x5C,0x9C,0x1C,0xEC,0x6C,0xAC,0x2C,
  176.     0xCC,0x4C,0x8C,0x0C,0xF4,0x74,0xB4,0x34,0xD4,0x54,0x94,0x14,
  177.     0xE4,0x64,0xA4,0x24,0xC4,0x44,0x84,0x04,0xF8,0x78,0xB8,0x38,
  178.     0xD8,0x58,0x98,0x18,0xE8,0x68,0xA8,0x28,0xC8,0x48,0x88,0x08,
  179.     0xF0,0x70,0xB0,0x30,0xD0,0x50,0x90,0x10,0xE0,0x60,0xA0,0x20,
  180.     0xC0,0x40,0x80,0x00
  181.   };
  182.  
  183.   byte *P,*T,*S,*R,*Z,I,J,K,N,D0;
  184.   unsigned int D1;
  185.   int Shift,DY;
  186.  
  187. #undef PAL
  188. #ifdef USE_XPAL
  189. #define PAL(N) Pal[N]
  190.   byte Pal[4];
  191. #else
  192. #define PAL(N) R[N]+4
  193. #endif
  194.  
  195.   N=LCDCONT&0x04? 16:8;
  196.  
  197.   for(S=RAM+0xFE9C,J=40;J;S-=4,J--)
  198.     if(S[0]&&(S[0]<160)&&S[1]&&(S[1]<168))
  199.     {
  200.       if(S[0]<16) { D0=0;K=16-S[0];I=(K<N)? N-K:0; }
  201.       else        { D0=S[0]-16;K=0;I=(160-S[0]<N)? 160-S[0]:N; }
  202.       Shift=S[1]-(S[3]&0x20? 1:8);
  203.       D1=D0*WIDTH+(WIDTH-160)/2+Shift;
  204.       P=XBuf+D1;Z=ZBuf+(D1>>3);
  205.       Shift&=0x07;
  206.       DY=2;
  207.       if(S[3]&0x40) { DY=-DY;K=N-K-1; }
  208.       R=S[3]&0x10? SPal1:SPal0;
  209. #ifdef USE_XPAL
  210.       Pal[0]=(XPal+4)[R[0]];Pal[1]=(XPal+4)[R[1]];
  211.       Pal[2]=(XPal+4)[R[2]];Pal[3]=(XPal+4)[R[3]];
  212. #endif
  213.       T=RAM+0x8000+((long)((N>8)? S[2]&0xFE:S[2])<<4)+(K<<1);
  214.  
  215.       /* If on top of background, clip only against the window */
  216.       if(~S[3]&0x80) Z+=WIDTH*HEIGHT/8;
  217.  
  218.       if(S[3]&0x20)
  219.       {
  220.         Shift=7-Shift;
  221.         for(;I;I--,P+=WIDTH,Z+=WIDTH/8,T+=DY)
  222.         {
  223.           D0=*T;D1=*(T+1);
  224.           K=(((int)Z[-1]<<8)|Z[0])>>Shift;
  225.           K=(D0|D1)&Conv[K];
  226.           if(K)
  227.           {
  228.             D1<<=1;
  229.             D1=(D1&0xAAA)|((D1&0x555)<<7)|(D0&0x55)|((int)(D0&0xAA)<<7);
  230.             if(K&0x80) P[0]=PAL((D1&0xC000)>>14);
  231.             if(K&0x40) P[-1]=PAL((D1&0x00C0)>>6 );
  232.             if(K&0x20) P[-2]=PAL((D1&0x3000)>>12);
  233.             if(K&0x10) P[-3]=PAL((D1&0x0030)>>4 );
  234.             if(K&0x08) P[-4]=PAL((D1&0x0C00)>>10);
  235.             if(K&0x04) P[-5]=PAL((D1&0x000C)>>2 );
  236.             if(K&0x02) P[-6]=PAL((D1&0x0300)>>8 );
  237.             if(K&0x01) P[-7]=PAL( D1&0x0003     );
  238.           }
  239.         }
  240.       }
  241.       else
  242.       {
  243.         Shift=8-Shift;
  244.         for(;I;I--,P+=WIDTH,Z+=WIDTH/8,T+=DY)
  245.         {
  246.           D0=*T;D1=*(T+1);
  247.           K=(((int)Z[0]<<8)|Z[1])>>Shift;
  248.           K=(D0|D1)&~K;
  249.           if(K)
  250.           {
  251.             D1<<=1;
  252.             D1=(D1&0xAAA)|((D1&0x555)<<7)|(D0&0x55)|((int)(D0&0xAA)<<7);
  253.             if(K&0x80) P[0]=PAL((D1&0xC000)>>14);
  254.             if(K&0x40) P[1]=PAL((D1&0x00C0)>>6 );
  255.             if(K&0x20) P[2]=PAL((D1&0x3000)>>12);
  256.             if(K&0x10) P[3]=PAL((D1&0x0030)>>4 );
  257.             if(K&0x08) P[4]=PAL((D1&0x0C00)>>10);
  258.             if(K&0x04) P[5]=PAL((D1&0x000C)>>2 );
  259.             if(K&0x02) P[6]=PAL((D1&0x0300)>>8 );
  260.             if(K&0x01) P[7]=PAL( D1&0x0003     );
  261.           }
  262.         }
  263.       }
  264.     }
  265. }
  266.  
  267.  
  268. /****************************************************************/
  269. /*** Display the screen buffer.                               ***/
  270. /****************************************************************/
  271. void RefreshScreen() { PutImage(); }
  272.