
#include "app.h"

unsigned short *EmuScreen=NULL;
int EmuWidth=0,EmuHeight=0;

int EmuInit()
{
  int len=0;

  //PicoInit();
  //PsndRate=44100; PsndLen=DSoundSeg;

  // Allocate screen:
  EmuWidth=640; EmuHeight=480;
  len=EmuWidth*EmuHeight; len<<=1;
  EmuScreen=(unsigned short *)malloc(len); if (EmuScreen==NULL) return 1;
  memset(EmuScreen,0,len);

  return 0;
}

void EmuExit()
{
  RomFree();
  free(EmuScreen); EmuScreen=NULL; // Deallocate screen
  EmuWidth=EmuHeight=0;

  PicoExit();
}

// Megadrive scanline callback:
int EmuScan(unsigned int num,unsigned short *pal,unsigned char *sdata)
{
  unsigned short *pd=NULL,*end=NULL;
  unsigned char *ps=NULL;

  if (num>=(unsigned int)EmuHeight) return 0;

  // Copy scanline to screen buffer:
  pd=EmuScreen+(num<<8)+(num<<6); end=pd+320;
  ps=sdata;

  do { *pd++=pal[*ps++]; } while (pd<end);
  
  return 0;
}

int EmuFrame()
{




  /*
  char map[8]={0,1,2,3,8,9,10,4};  // Joypads, format is SACB RLDU
  int a=0,input=0;
 
  // Set Megadrive buttons:
  for (a=0;a<8;a++)
  {
    int m=map[a];
    if (m>=0) if (Inp.button[m]>30) input|=1<<a;
  }

  PicoPad[0]=input;

  PicoScan=EmuScan;
  PsndOut=DSoundNext; PicoFrame(); PsndOut=NULL;
  PicoScan=NULL;
  */

  return 0;
}
