⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ibmmsdos.c

📁 知名gba模拟器vgba代码
💻 C
📖 第 1 页 / 共 2 页
字号:
  {
   if (extkey)
   {
    if (!keybstatus[code+128])
    {
     keybstatus[code+128]=1;
     PausePressed=0;
    }
   }
   else
   {
    if (!keybstatus[code])
    {
     keybstatus[code]=1;
     if (code==VK_F8)
      PausePressed=(PausePressed)? 0:2;
     else
      if (code==VK_F9)
       PausePressed=(PausePressed)? 0:1;
      else
       PausePressed=0;
     switch (code)
     {
      case VK_F11:
       Adlib_DecreaseVolume ();
       break;
      case VK_F12:
       Adlib_IncreaseVolume ();
       break;
      case VK_Escape:
      case VK_F10:
       CPURunning=0;
       break;
      case VK_F5:
       Adlib_ToggleSound ();
       break;
      case VK_F4:
       Adlib_ToggleChannel (3);
       break;
      case VK_F3:
       Adlib_ToggleChannel (2);
       break;
      case VK_F2:
       Adlib_ToggleChannel (1);
       break;
      case VK_F1:
       Adlib_ToggleChannel (0);
       break;
#ifdef DEBUG
      case VK_1: case VK_2: case VK_3: case VK_4: case VK_5:
      case VK_6: case VK_7: case VK_8: case VK_9:
       nDebugKey=code-VK_1+1;
       break;
#endif
     }
    }
   }
  }
  extkey=0;
 }
 else
  if (code==0xE0)
   extkey=1;
 code=inportb (0x61);           /* acknowledge interrupt */
 outportb (0x61,code | 0x80);
 outportb (0x61,code);
 outportb (0x20,0x20);
}

extern void keyb_interrupt_asm (void);
static int Keyb_Init (void)
{
 _go32_dpmi_get_protected_mode_interrupt_vector (9,&oldirqkeyb);
 newirqkeyb.pm_offset=(int)keyb_interrupt_asm;
 newirqkeyb.pm_selector=_go32_my_cs();
 _go32_dpmi_set_protected_mode_interrupt_vector (9,&newirqkeyb);
 return 1;
}

static void Keyb_Reset (void)
{
 _go32_dpmi_set_protected_mode_interrupt_vector (9,&oldirqkeyb);
}

/* Acknowledge interrupt and check sound updates. This is called about 290
   times per second */
void timer_interrupt (void)
{
 outportb (0x20,0x20);
 Adlib_InterruptRoutine ();
}

typedef struct joyposstruct
{
 int x,y;
} joypos;

static int gotjoy=0;
static joypos joycentre;

/* Get absolute position in jp */
static int _JoyGetPos (joypos *jp)
{
 unsigned tmp;
 tmp=JoyGetPos ();
 jp->x=(unsigned) (tmp&0xFFFF);
 if (jp->x>=10000)
  return 0;
 jp->y=(unsigned) (tmp>>16);
 if (jp->y>=10000)
  return 0;
 return 1;
}

/* Initialise the joystick routines. Return 0 if no joystick found */
static int Joy_Init (void)
{
 joypos jp;
 if (!_JoyGetPos (&jp))
  return 0;
 joycentre.x=jp.x;
 joycentre.y=jp.y;
 gotjoy=1;
 return 1;
}

/* Check joystick and set JoyState accordingly */
static byte Joy_Check (void)
{
 joypos jp;
 unsigned char joystatus;
 static byte JoyState=0xFF;
 static byte count=2;
 if (!gotjoy)
  return JoyState;
 if (--count)
  return JoyState;
 count=2;
 JoyState=0xFF;
 _JoyGetPos (&jp);
 joystatus=inportb (0x201);
 if (jp.x<(joycentre.x/2))
  JoyState&=0xFD;
 else
  if (jp.x>(joycentre.x*3/2))
   JoyState&=0xFE;
 if (jp.y<(joycentre.y/2))
  JoyState&=0xFB;
 else
  if (jp.y>((joycentre.y*3)/2))
   JoyState&=0xF7;
 if ((joystatus&0x10)==0)
  JoyState&=(swapbuttons&1)? 0xDF:0xEF;
 if ((joystatus&0x20)==0) 
  JoyState&=(swapbuttons&1)? 0xEF:0xDF;
 if ((joystatus&0x40)==0)
  JoyState&=0xBF;
 if ((joystatus&0x80)==0)
  JoyState&=0x7F;
 return JoyState;
}

/* Flush stdout because it is buffered */
static void _printf (char *szMessage)
{
 printf (szMessage);
 fflush (stdout);
}

/** InitMachine **********************************************/
/** Allocate resources needed by MS-DOS dependent code.     **/
/*************************************************************/
int InitMachine()
{
  int tmp,i;
  char GifPalette[768];
  char *pGifBytes;
  pGifBytes=0;
  DosSelector=_dos_ds;
  cs_alias=_my_ds();
  GetSBInfo ();
  if (Verbose)
   _printf ("Allocating stack space... ");
  i=InitStacks (NUM_STACKS,STACK_SIZE);
  if (Verbose)
   _printf ((i)? "OK\n":"Failed\n");
  if (!i)
   return 0;
  if(Verbose) _printf("Allocating screen buffer...");
  XBuf=(byte *)malloc(sizeof(byte)*HEIGHT*WIDTH*4);
  if (Verbose)
   printf ((XBuf)? "OK\n":"FAILED\n");
  if(!XBuf)
   return(0);
  memset (XBuf,0,sizeof(byte)*HEIGHT*WIDTH);
  for (i=0;i<16;++i)
   XPal[i]=i+1;
  if (soundmode==1)
  {
   if (Verbose)
    _printf ("Detecting sound card...");
   tmp=Adlib_Init ();
   if (Verbose)
    if (tmp)
    {
     switch (tmp&0x70000)
     {
      case 0:
       printf ("OPL2");
       break;
      case 0x10000:
       printf ("OPL3");
       break;
      case 0x20000:
       printf ("Sound Blaster Pro");
       break;
      case 0x30000:
       printf ("Sound Blaster Pro 2");
       break;
      default:
       printf ("Sound Blaster 16");
       break;
     }
     printf (" found at 0x%x\n",tmp&0xFFFF);
    }
    else
     printf ("Not found\n");
  }
  else
   soundmode=0;
  if (Verbose)
   _printf ("Detecting joystick...");
  tmp=Joy_Init ();
  if (Verbose)
   printf ((tmp)? "Found\n" : "Not found\n");
  if (Verbose) printf ("Initializing timer...\n");
  install_timer_int ();
  if (IntSync)
  {
   ReadTimerMin=1192380/IFreq;
   OldTimer=ReadTimer ();
  }
  pGifBytes=0;
  if (videomode==0 || videomode==4)
  {
   if (Verbose)
    _printf ("Allocating GIF decoding space...");
   pGifBytes=malloc(64000);
   if (Verbose)
    printf ((pGifBytes)? "OK\n":"FAILED\n");
   if (pGifBytes)
   {
    if (!LoadGif (szGifFile,GifPalette,pGifBytes))
    {
     free (pGifBytes);
     pGifBytes=0;
    }
   }
  }
  if (Verbose)
  {
   fprintf (stderr,"Press any key to run ROM code...");
   getch ();
  }
  VGA_Init (GifPalette,pGifBytes);
  if (pGifBytes)
   free (pGifBytes);
  /* Parse keyboard mapping string */
  sscanf (szKeys,"%02X%02X%02X%02X%02X%02X%02X%02X",
          &KEY_LEFT,&KEY_RIGHT,&KEY_UP,&KEY_DOWN,
          &KEY_BUTTONA,&KEY_BUTTONB,&KEY_START,&KEY_SELECT);
  Keyb_Init ();
  return(1);
}

/** TrashMachine *********************************************/  
/** Deallocate all resources taken by InitMachine().        **/
/*************************************************************/
void TrashMachine()
{
  VGA_Reset ();
  if(Verbose) printf("Shutting down...\n");
  restore_timer_int ();
  Keyb_Reset ();
  Adlib_Reset ();
  ExitStacks ();
  fflush (stdin);       /* Clear keyboard buffer */
}

/** PutImage *************************************************/
/** Put an image on the screen.                             **/
/*************************************************************/
void PutImage()
{
 switch (videomode)
 {
  case 3:
  case 2:  PutImage_FullScreen ();
           break;
  default: PutImage_Standard ();
           break;
 }
}

/** Joystick *************************************************/
/** Return the current joystick state.                      **/
/*************************************************************/
byte Joystick(void)
{
  byte JoyState;
#ifdef DEBUG
  ParseDebugKey ();
#endif
  JoyState=Joy_Check ();
  if (keybstatus[KEY_START])
   JoyState&=0x7F;
  if (keybstatus[KEY_SELECT])
   JoyState&=0xBF;
  if (keybstatus[KEY_DOWN])
   JoyState&=0xF7;
  if (keybstatus[KEY_UP])
   JoyState&=0xFB;
  if (keybstatus[KEY_LEFT])
   JoyState&=0xFD;
  if (keybstatus[KEY_RIGHT])
   JoyState&=0xFE;
  if (keybstatus[KEY_BUTTONA])
   JoyState&=(swapbuttons&2)? 0xDF:0xEF;
  if (keybstatus[KEY_BUTTONB])
   JoyState&=(swapbuttons&2)? 0xEF:0xDF;
  if (keybstatus[VK_Ctrl] && keybstatus[VK_Ctrl+128] &&
      keybstatus[VK_Alt] && keybstatus[VK_Alt+128])
   ResetGB ();
  return(JoyState);
}

static int NumRefreshSkipped=0;
int CheckRefresh (void)
{
 int DoRefresh=0;
 do
  NewTimer=ReadTimer ();
 while ((NewTimer-OldTimer)<0);
 OldTimer+=ReadTimerMin;
 if ((OldTimer-NewTimer)>0)
  DoRefresh=1;
 else
  if (++NumRefreshSkipped>10)
  {
   DoRefresh=1;
   OldTimer=ReadTimer();
  }
 if (DoRefresh)
 {
  NumRefreshSkipped=0;
 }
 return DoRefresh;
}

/* Returns:
   0 - Do not refresh screen
   1 - Refresh screen
   2 - Refresh screen according to the -uperiod parameter
   Return value is ignored if uperiod!=0 */
int MachineInterrupt (void)
{
 int tmp;
 if (PausePressed)
 {
  Adlib_SoundOff ();
  tmp=0;
  if (PausePressed==2)          /* Blank screen */
  {
   tmp=1;
   inportb (0x3BA);
   inportb (0x3DA);
   outportb (0x3C0,0);
  }
  while (PausePressed);
  if (tmp)
  {
   inportb (0x3BA);
   inportb (0x3DA);
   outportb (0x3C0,0x20);
  }
  Adlib_SoundOn ();
  if (IntSync)
   OldTimer=ReadTimer ();
 }
 if (IntSync)
  return CheckRefresh();
 return 2;
}

/*** SIOSend ****************************************************/
/*** Send a byte onto the serial line.                        ***/
/****************************************************************/
byte SIOSend(register byte V) { return(0); }


/*** SIOReceive *************************************************/
/*** Receive a byte from the serial line. Returns 1 on        ***/
/*** success, 0 otherwise.                                    ***/
/****************************************************************/
byte SIOReceive(register byte *V) { return(0); }


/****************************************************************/
/*** Write value into sound chip register (Reg #0 at FF10h).  ***/
/****************************************************************/
void Sound(byte R,byte V)
{
 Adlib_WriteSoundReg (R,V);
}

/** Common.h ****************************************************/
/** Parts of the drivers common for Unix/X and MSDOS.          **/
/****************************************************************/
#include "Common.h"

⌨️ 快捷键说明

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