game.c

来自「ESS3890+SL原代码(1*16内存)」· C语言 代码 · 共 1,099 行 · 第 1/3 页

C
1,099
字号
/* Copyright 2001, ESS Technology, Inc.                                 *//* SCCSID @(#)game.c	4.6 10/17/03 *//*Set up on 04/11/2002 */#ifdef GAMEBOY#include "common.h"#include "memmap.h"#include "constvar.h"#include "display.h"#include "vp.h"#include "mvd.h"#include "util.h"#include "vcxi.h"#include "play.h"#include "config.h"#include "low.h"#include "buffer.h"#include "const.h"#include "micro.h"#include "keydef.h"#ifndef MEMORY_DEVICE#include "servo.h"#endif#ifdef ECHO#include "echo.h"#endif#include "game.h"#if (defined VGB_CD || defined INES_CD) typedef unsigned char uch;static void VGASetColor(int N,int R,int G,int B, int *pallete);#ifdef INES_CDstatic void GM_SetColor(uch *xpal,uch N,uch R,uch G,uch B,int *pallete);#endifvoid SyncSND();extern short game_list_misc_def;extern short game_list_buffer_size;extern int is_ess_game;extern int GmScnScale;extern int NES_U_Period;/** VGASetColor() ********************************************//** Set color N (0..255) to a given RGB value.              **//*************************************************************/inline void VGASetColor(int N,int R,int G,int B, int *pallete){ pallete[N] = (R<<16)|(G<<8)|(B);}#ifdef INES_CDinline void GM_SetColor(uch *xpal, uch N,uch R,uch G,uch B, int *pallete){  VGASetColor(xpal[N]=N+1,R,G,B,pallete);}#endifvoid CDGM_hscale(int width){    mvd[vid_scn_incval] = 256*width/45; /* (4096*width)/720 */    mvd[vid_scn_y_wordsize] = width/4 - 1;    mvd[vid_scn_uv_wordsize] = width/8;    mvd[vid_scn_hend] = vcx_scn_hstart + 725;    GmScnScale = 1;}void CDGM_hscale_recover(void){    GmScnScale = 0;    mvd[vid_scn_incval] = 0x800;    mvd[vid_scn_y_wordsize] = 352/4 - 1;    mvd[vid_scn_uv_wordsize] = 352/8;    mvd[vid_scn_hend] = vcx_scn_hstart + 725;}void CDGM_init_disp(){    int tmp;    if (VP_need_to_restore_ucode & VP_RESTORE_GAM_UCODE) {	VP_load_ucode(rgamnonpage);	VP_need_to_restore_ucode &= ~VP_RESTORE_GAM_UCODE;	VP_need_to_restore_ucode |= 	    (VP_RESTORE_MPG_UCODE|VP_RESTORE_EQU_UCODE|VP_RESTORE_MP3_UCODE|\	     VP_RESTORE_KSC_UCODE|VP_RESTORE_JPG_UCODE|VP_RESTORE_GSL_UCODE|\	     VP_RESTORE_RVB_UCODE);    }     if(gmby_name_id == 0x60) tmp = 716;    else tmp = 0;    CD_gm_startY = (int*)dram(DISP_info->startY)+tmp;    if(vcx_scn_height==288) CD_gm_startY += 2112;    CD_gm_startU = (int*)dram(DISP_info->startU)+tmp/2;    if(vcx_scn_height==288) CD_gm_startU += 1056;    CD_gm_startV = (int*)dram(DISP_info->startV)+tmp/2;    if(vcx_scn_height==288) CD_gm_startV += 1056;    CD_yuv_pal = (int *)dram(GMBY_YUVPAL_start);    DISP_count = 0;    DISP_frame = 0;}void CDGM_ld_yuvpal(uch *Palette, uch *XPal){    int i, j, k;    int r, g, b, y, cb, cr, tmp;    int *gm_pal;    /* cr=0.299, cg=0.587,cb=0.114,                    */    /* cu=1/(2*(1-cb))=0.5643, cv=1/(2*(1-cr))= 0.7132 */    /* Y = cr*R + cg*G + cb*B                          */    /* U = (B-Y) * Cu                                  */    /* V = (R-Y) * Cv                                  */    /*     * Layout in DP is like this:     *          0       0x80     *          1       0x81     *          2       0x82     * ...     */    gm_pal = (int *)dram_cached(GMBY_RGB_start);#ifdef VGB_CD    if(gmby_name_id == 0x60){#endif#ifdef INES_CD    for(j=0; j<64; j++)       GM_SetColor(XPal,j,Palette[j*3],Palette[j*3+1],Palette[j*3+2],gm_pal);#endif#ifdef VGB_CD    } else {       for(j=0;j<256;j++)          VGASetColor(j,(j>>6)*255/3,((j>>3)&0x07)*255/7,(j&0x07)*255/7,gm_pal);    }#endif    for (i = 0; i < 128; i++) {        j = 2 * i;        for (k = 0; k <= 128; k += 128) {            CD_yuv_pal[j] = 0x0;            tmp = gm_pal[i + k];            r = (tmp >> 16) & 0xff;            g = (tmp >>  8) & 0xff;            b = tmp & 0xff;            /* Actually this is not YCbCr, it's YUV! */            y = ((299 * r + 587 * g + 114 * b) / 1000);            cb = ((b - y) * 5643) / 10000;            cr = ((r - y) * 7132) / 10000;            /* let luminance be 70% of maximum value */#if 0            y = 16 + (((y - 16) * 70) / 100);#endif            tmp = y ^ 0x80;            tmp = ((tmp << 8) | (cb & 0xff));            tmp = ((tmp << 8) | (cr & 0xff));            CD_yuv_pal[j] = tmp;            j++;        }    }    /*Transfer color pallete to VP*/    VP_xfer(NCMDQ_VP_xfer, VPCMD_W_DP, 0, 0, ((int)CD_yuv_pal >> 2), 256, 1);}#ifdef INES_CD/** VGAPutImage() ********************************************//** Put an image on the screen.                             **//*************************************************************/void VGAPutImage(){    /*divide the rgb buffer to be 32*144 block*/    register int j, k, dramAddr;    int *pU0, *pV0, *pY0, *pY1;    int pU0_bb, pV0_bb, pY0_bb, pY1_bb;    register int D_Source;    DISP_frame = 0;    pY0 = CD_gm_startY;    pY1 = pY0 + 88;    pU0 = CD_gm_startU;    pV0 = CD_gm_startV;    D_Source = GMBY_RGB_start;    UTIL_flushcache(0x30, 8192);    LOW_set_buscon_dma_width3(66);    for(j=0;j<4; j++) {       VP_xfer(NCMDQ_VP_xfer, VPCMD_W_DP, 0, 0, ((int)CD_yuv_pal >> 2), 256, 1);       /*Transfer color pallete to VP*/       pY0_bb = (int)pY0>>2;       pY1_bb = (int)pY1>>2;       pU0_bb = (int)pU0>>2;       pV0_bb = (int)pV0>>2;       dramAddr = D_Source;       for(k=0; k<112; k++){/*process two lines for each time*/          VP_cmdq_wait(9);          VP_xfer(0,VPCMD_W_DPCM,GM_RGB4_START,VPDMA_WIDTH3,(int)dramAddr,16,2);          dramAddr += 132;          VP_data(0, gmby_no_deflicker);          VP_ucode(0, deflicker);          wait4l_only; wait4l_only;          /* Read YUV data from DM */          VP_cmdq_wait(16);          VP_xfer(0,VPCMD_R_DPCM,GM_Y1_START,0,pY0_bb,16,1);          VP_xfer(0,VPCMD_R_DPCM,GM_Y2_START,0,pY1_bb,16,1);          VP_xfer(0,VPCMD_R_DPCM,GM_U420_START,0,pU0_bb,8,1);          VP_xfer(0,VPCMD_R_DPCM,GM_V420_START,0,pV0_bb,8,1);          pY0_bb += 176;/*jump two lines for Y*/          pY1_bb += 176;          pU0_bb += 88;          pV0_bb += 88;         }      pY0 += 16;      pY1 += 16;      pU0 += 8;      pV0 += 8;      D_Source += 16;#if (defined(ECHO) && defined(SOUND))    MIC_service();#endif    }}#endif /*INES_CD*//*-----------------------------------------------*//*          GAME PAD                             *//*-----------------------------------------------*/extern unsigned short codeIR;#ifdef IR_GAMEPADvolatile int GB_ir_key;#endifvoid CDGM_init_pad(){    CLEAR_GM_LATCH0;    SET_GM_CLK0;    SET_GM_DATA0;    TRI_GM_DATA0;#ifdef GB_TWO_PAD    SET_GM_DATA1;    TRI_GM_DATA1;#endif}unsigned int  CDGM_key_process(){    int ii, i;    unsigned int tt, ret;    ret = tt = 0 ;#ifdef IR_GAMEPAD    if(GB_ir_key) {         ret = GB_ir_key ;         return (ret);     }#endif/*For HQ, we will define ESS_NEW_PAD */#ifdef ESS_NEW_PAD  /*Below is just for one pad connected physically             */  if(!(tt&0xFF)) {        SET_GM_LATCH0;        risc_sleep_a_bit(60);        CLEAR_GM_LATCH0;        risc_sleep_a_bit(20);        TRI_GM_DATA0;        for(ii=1; ii<=8; ii++){            tt <<= 1;            CLEAR_GM_CLK0;            if(!GM_DATA0_HIGH) tt |= 1;            risc_sleep_a_bit(40);            SET_GM_CLK0;            risc_sleep_a_bit(40);         }        /* The bit0-bit3 is the direction, since we cannot press */        /* 4 dirctions at the same time.  i just use it to make  */        /* the joypad not go crazy.                              */        if((tt&0xF) == 0xF) tt = 0;      }#ifdef GB_TWO_PAD  /* I just check two pad connection, i think it is because we  */  /* just share the same clock pin for two pad, so that the 2nd */  /* pad is connected or not will affect the 1st pad's clock,   */  /* for this case i just use two kinds of clock for pad        */  /*Below is for TWO pads connected physically                  */  if(! tt){    SET_GM_LATCH0;    risc_sleep_a_bit(300);    CLEAR_GM_LATCH0;    risc_sleep_a_bit(300);    TRI_GM_DATA0;    TRI_GM_DATA1;    for(ii=1; ii<=8; ii++){        tt <<= 1;        CLEAR_GM_CLK0;        if(!GM_DATA0_HIGH) tt |= 1;        if(!GM_DATA1_HIGH) tt |= 0x100;        risc_sleep_a_bit(100);        SET_GM_CLK0;        risc_sleep_a_bit(100);     }     if( (tt&0xF) == 0xF ) tt &=0xFF00;     if( (tt&0x0F00) == 0x0F00) tt &= 0xFF;   }#endif#else /*ESS_HK_PAD, no define ESS_NEW_PAD, by default*//*For custmer, we use the old pad codes. but it seems we should *//*Change to different clock setting for different kind of pad   */    SET_GM_LATCH0;    risc_sleep_a_bit(262);    CLEAR_GM_LATCH0;    risc_sleep_a_bit(262);    TRI_GM_DATA0;#ifdef GB_TWO_PAD    TRI_GM_DATA1;#endif    for(ii=1; ii<=8; ii++){        tt <<= 1;        CLEAR_GM_CLK0;        if(!GM_DATA0_HIGH) tt |= 1;#ifdef GB_TWO_PAD        if(!GM_DATA1_HIGH) tt |= 0x100;#endif        risc_sleep_a_bit(262);        SET_GM_CLK0;        risc_sleep_a_bit(262);     }#endif /*ESS_HK_PAD*/#ifdef VGB_CD    if(gmby_name_id == 0x60){#endif     if ( tt ) {        /*1st start key should not pressed when function keys are pressed*/        /*Just make sure there is no pause action when playing game      */        if((tt&0xFF) != 0x10 && (tt&0xFF) != 0x30) tt &= 0xEFEF;        ret |= (tt&0x0101)<<7; /* L */        ret |= (tt&0x0202)<<5; /*R */        ret |= (tt&0x0404)<<3; /*UP*/        ret |= (tt&0x0808)<<1; /*DOWN*/        ret |= (tt&0x1010)>>1; /*START*/        ret |= (tt&0x2020)>>3; /*SELECT*/        ret |= (tt&0xC0C0)>>6; /*AB*/

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?