#include <dos.h>
#include <stdio.h>
#include <malloc.h>

typedef unsigned int    (far * VIDBUF);
#define CGABUF          ( (VIDBUF) (0xB800L << 16) )
#define MGABUF          ( (VIDBUF) (0xB000L << 16) )
#define CGASTAT         0x3da
#define FALSE  0

static unsigned int   far *buffrecs[20] = {
                                             {0},{0},{0},{0},{0},
                                             {0},{0},{0},{0},{0},
                                             {0},{0},{0},{0},{0},
                                             {0},{0},{0},{0},{0}
                                          } ;

pushscreen(buff, retrace)
int  buff, retrace;
{
   VIDBUF scr_ofs;
   unsigned int  far *bp;
   int i;
   register int c;

   if (buffrecs[buff] == NULL)
      if ( (buffrecs[buff] = (unsigned int far *) _fmalloc(4000)) == NULL)
         return 0;

   bp = buffrecs[buff];
   if (vidmode() == 7)
   {
      scr_ofs = MGABUF;
      retrace = FALSE;
   }
   else
      scr_ofs = CGABUF;

   for (i = 0; i < 2000; i++)
   {
      c = *scr_ofs++;
      if (retrace)
      {
         while ( (inp(CGASTAT) & 1) != 0);
         while ( (inp(CGASTAT) & 1) == 0);
      }
      *bp++ = c;
   }

   return 1;
}

popscreen(buff, retrace)
int  buff, retrace;
{
   VIDBUF scr_ofs;
   unsigned int  far *bp;
   int i;
   register int c;

   if (buffrecs[buff] == NULL)
      return 0;

   bp = buffrecs[buff];
   if (vidmode() == 7)
   {
      scr_ofs = MGABUF;
      retrace = FALSE;
   }
   scr_ofs = CGABUF;

   for (i = 0; i < 2000; i++)
   {
      c = *bp++;
      if (retrace)
      {
         while ( (inp(CGASTAT) & 1) != 0);
         while ( (inp(CGASTAT) & 1) == 0);
      }
      *scr_ofs++ = c;
   }

   return 1;
}
