#define VGA ((unsigned char __far*)0xA0000000L)

/*void outofmemory(void);

unsigned char __far *dbuff;

void init_dbuff(void)
{
  if(!(dbuff = _fmalloc(64000)))
    outofmemory();
}

void put_pixel(unsigned x, unsigned y, unsigned char color)
{
  dbuff[(y<<6)+(y<<8)+x] = color;
}

void fill_screen(unsigned char color)
{
  _fmemset(dbuff, color, 64000);
}

void draw_rectangle(signed x, signed y, unsigned w, unsigned h,
                    unsigned char color)
{
  unsigned n;

  while(h--)
    for(n=0;n<w;n++)
      put_pixel(x+n, y+h, color);
}
*/

void outword(unsigned word, unsigned port);
#pragma aux outword = \
  "out dx, ax" \
  parm [ax] [dx] modify exact [];

unsigned char inbyte(unsigned port);
#pragma aux inbyte = \
  "in al, dx" \
  parm [dx] value [al] modify exact [ax];

void update_screen(void)
{
//  _fmemcpy(VGA, dbuff, 64000);

  unsigned char plane;
  unsigned char *ptr = dbuff;

  while ((inbyte(0x3DA) & 0x08));
  while (!(inbyte(0x3DA) & 0x08));

  for(plane=0;plane<4;plane++)
  {
    outword(0x0002 | (0x800 >> plane), 0x3c4);
    _fmemcpy(VGA, ptr, 8000);
    ptr += 8000;
  }
}

void restore_display_mode(void)
{
  __asm
  {
    mov ax, 0x0003
    int 0x10
  }
}

#define KEY_ENTER 0x0D
#define KEY_ESC 27
#define KEY_UP (72 | 0x80)
#define KEY_DOWN (80 | 0x80)
#define KEY_LEFT (75 | 0x80)
#define KEY_RIGHT (77 | 0x80)
#define KEY_F7 (65 | 0x80)
#define KEY_F8 (66 | 0x80)
#define KEY_F9 (67 | 0x80)

int poll_keycode(void)
{
  int c = getch();
    if(!c)
      return getch() | 0x80;
  return c;
}

#define poll_keypress() poll_keycode()
