/*
*    DivaPC ARM C source
*
*    VID.H.WINDEFS  - Definition of ARM-PC interface
*
*    12-08-94 INH  Original - from Vid.c.windrv2
*    09-03-96      for v2.0 - rewrite using HPC
*
* Windrv versions
*
* 200 The start
* ..206 (Lost in the mists of time)
* 207 Added RLEblt functions
* 208 Changed set-palette functions, add 256-clr palette flags
* 209 Changed set-palette again(!), added UpdateColours
* 20A StrBlt foreground/background reordered
* 20B Cursor commands changed
* 20C First HPC version
* 20D HPC Not used anymore
* 20E Write-FIFO interlock flag added
* 20F Mode changing stuff added (ARSED UP)
*/

#define WINDRV_VERSION	0x20E
#define MAX_BUF_LEN     16380
#define BUF_ALLOC_SIZE  16384

#define BUF_LOCKFLAG_OFFSET 16380
#define BUF_LOCKFLAG_WAIT  0
#define BUF_LOCKFLAG_GO    1

/* Flags for Opcode fields */

#define OPC_ROP_MASK    0x000000FFU	/* ROP code */
#define OPC_TRANS_MASK  0x0000FF00U	/* Transparency mask for Scanlines */
#define OPC_UNI_PAT     0x00010000U	/* Set if 1-word pattern is present*/
#define OPC_FULL_PAT    0x00020000U	/* Set if full pattern is present */
#define OPC_DIB_BLIT	0x00040000U	/* Makes mem->dev blits go upwards */
#define OPC_PL_TRANS    0x00080000U	/* Draws Polylines transparently */

/* Win Driver parameter/result structures  ------------------------------ */

struct _common_res
{
  LONG  value;
};

/* ---------- */

struct _ReadModeInfo /* Result */
{
  LONG   Signature;
#define HPC_WIN_SIG 0x644E6957
  WORD   Version;
  WORD   flags;
  INT16  Xtotal;
  INT16  Ytotal;
  INT16  bpp;
  WORD   padword;
  LONG   SharedAddr;
};

/* ---------- */

#define SI_REASON_SET      0 /* try to set mode, but set VGA on fail */
#define SI_REASON_SET_FAIL 1 /* try to set mode, allowing for a fail */
#define SI_REASON_VALID    2 /* i.e. just check mode's validity */

struct _SendInfo /* Command */
{
  WORD   reason; /* see above */
  WORD   xres;
  WORD   yres;
  WORD   bpp;
  WORD   pal;
};

/* ---------- */

struct _StrBlt
{
  LONG   Opcode;
  INT16  DestX;
  INT16  DestY;
  INT16  ExtX;
  INT16  ExtY;
  WORD   LineOffset;
  WORD   spare;
  LONG   Foregnd;
  LONG   Backgnd;
  BYTE   Data[1];
#define STRBLT_MAX_LEN (MAX_BUF_LEN-28)
};

/* ---------- */

struct _BitBlt /* Used for DevBlit, MemBlit, and DevFetch */
{
  LONG   Opcode;
  INT16  DestX;
  INT16  DestY;
  INT16  ExtX;
  INT16  ExtY;
  INT16  SrcX;
  INT16  SrcY;
  LONG   Foregnd;
  LONG   Backgnd;
  int    Pattern[1];
};

/* ---------- */

struct _ScanLine
{
  LONG   Opcode;
  WORD   Count;
  INT16  Yl;
  BYTE	 Pattern_Data[1]; /* Pattern if needed, followed by data */
#define SCANLINE_MAX ((MAX_BUF_LEN-12-256)/4)
};

/* ---------- */

struct _PolyLine
{
  LONG   Opcode;
  WORD   Count;
  WORD   Style;
  LONG   Foregnd;
  LONG   Backgnd;
  WORD   Data[1]; /* Array of X/Y coordinates */
#define POLYLINE_MAX ((MAX_BUF_LEN-20)/8)
};

/* ---------- */

struct _FastBorder
{
  LONG   Opcode;
  INT16  RectX1;
  INT16  RectY1;
  INT16  RectX2;
  INT16  RectY2;
  INT16  Xthickness;
  INT16  Ythickness;
  INT16  ClipX1;
  INT16  ClipY1;
  INT16  ClipX2;
  INT16  ClipY2;
  int    Pattern[1];
};

/* ---------- */

struct _SaveBitmap
{
  LONG   Op; /* 0 = Save, 1 = Restore */
  INT16  DestX;
  INT16  DestY;
  INT16  ExtX;
  INT16  ExtY;
};

/* returns 0 = failed, 1 = OK */

/* ---------- */

struct _PutPixel
{
  LONG   Opcode;
  INT16  Xp;
  INT16  Yp;
  LONG   Colour;
};

/* ---------- */

struct _GetPixel
{
  INT16  Xp;
  INT16  Yp;
};

/* returns result = colour value */

/* ---------- */

struct _ScanLR
{
  LONG   How;
  INT16  Xp;
  INT16  Yp;
  LONG   Colour;
};

/* returns result = xval */

/* ---------- */

struct _WriteXY
{
  INT16 DestX;
  INT16 DestY;
  WORD  Xext;
  WORD  spare;
  int   Data[1];
};

/* ---------- */

struct _BeginDraw
{
  INT16 X1;
  INT16 Y1;
  INT16 X2;  /* Exclusive! */
  INT16 Y2;
};

/* ---------- */

struct _UpdateColours
{
  INT16 DestX;
  INT16 DestY;
  INT16 ExtX;
  INT16 ExtY;
  BYTE  Data[256];  /* 256->256 lookup table */
};

/* ---------- */

struct _PointerShape
{
  WORD   PtrWidth;
  WORD   PtrHeight;
  BYTE	 Data[1];  /* AND and XOR masks */
};

/* ---------- */

struct _ShowPointer
{
  INT16  Xp;
  INT16  Yp;
};


/* ---------- */

struct _SetPalette
{
  WORD  first;
  WORD  n_clrs;
  LONG  rgb_data[1];
};

/* ---------- */

struct _WriteLine
{
  WORD  nlines;
  WORD  LineOffset;
  int	Data[1];
};
#define WRITELINE_MAX (MAX_BUF_LEN-8)

/* ---------- */

struct _WriteLineMono
{
  WORD  nlines;
  WORD  LineOffset;
  BYTE	Data[1];
};

/* ---------- */

struct _ReadLine /* Result */
{
  int	Data[1];
};

/* ---------- */

struct _ReadLineMono /* Result */
{
  BYTE	Data[1];
};

/* ---------- */

struct _ReadModeList /* parameter + result */
{
  WORD  skip;
  char  mode[34];
};

/* ---------- */

typedef union
{
  struct _common_res	result;
  struct _ReadModeInfo	readmodeinfo;
  struct _StrBlt        strblt;
  struct _BitBlt        bitblt;
  struct _ScanLine      scanline;
  struct _PolyLine      polyline;
  struct _FastBorder    fastborder;
  struct _SaveBitmap    savebitmap;
  struct _PutPixel      putpixel;
  struct _GetPixel      getpixel;
  struct _ScanLR        scanlr;
  struct _WriteXY       writexy;
  struct _BeginDraw     begindraw;
  struct _UpdateColours updatecolours;
  struct _PointerShape  pointershape;
  struct _ShowPointer   showpointer;
  struct _SetPalette    setpalette;
  struct _WriteLine	writeline;
  struct _WriteLineMono	writelinemono;
  struct _ReadLine	readline;
  struct _ReadLineMono	readlinemono;
  struct _SendInfo      sendinfo;
  struct _ReadModeList  readmodelist;
}
  WIN_PARAM_BLK;


/* Options for BitBlt commands */

/* In general, BitBlt is done by whichever side the destination is
   on - the PC does it for mono & colour bitmaps, and the ARM does
   it onto the device. This means we have to support the following:

   Device       ->Device blt
   Mono bitmap  ->Device blt
   Colour bitmap->Device blt

   and we need to assist the PC by doing

   Device->Mono bitmap fetch
   Device->Colour bitmap fetch

   It is a general rule that the side fetching the source data will
   shift it so as to align it with the destination. For bitmap-to-device
   transfers, the PC aligns the data to the destination before sending
   it out to the ARM. For device->bitmap transfers, the ARM aligns
   the data as it fetches it from the screen. The commands and parameters
   to do this (see _BitBlt structure below) are as follows:

DevBlit:
   Execute device-to-device blt:
     Opcode : raster op in A-format
     SrcX, SrcY : co-ords of src rectangle on screen
     DstX, DstY : co-ords of dst rectangle on screen
     ExtX, ExtY : x/y size

MemBlit:
   Setup for memory bitmap->Device blt:
     Opcode : raster op in A-format
     SrcX, SrcY : not used - PC does source alignment
     DstX, DstY : co-ords of dst rectangle on screen
     ExtX, ExtY : x/y size
     ForeGnd/BackGnd : will be used if it's a mono->device blt

   This will be followed by one or more WriteLine or WriteLineMono
    commands, which is used to write the bitmap line-by-line.


WriteLineMono:
   Do 1 line of mono bitmap->Device blt:
     The PC data buffer must previously have been loaded with
     mono bitmap data - it should be aligned with the destination
     so that the first mono bit corresponds to the first
     destination word (i.e. it's on an 8-pixel boundary for 4bpp
     modes, a 4-pixel boundary for 8bpp, and so on).

WriteLine:
   Do 1 line of colour bitmap->Device blt:
     The PC data buffer must have been loaded with one line of
     colour bitmap data. The first pixel in this data must be
     aligned with the first destination word on the device (an
     8-pixel boundary for 4bpp modes, etc).

DevFetch:
   Setup for Device-to-memory blts, for which we do src fetches.
     Opcode : ignored
     SrcX, SrcY : source rectangle on the device
     DstX, DstY : DstX is used to determine the alignment
     ExtX, ExtY : x/y size
     BackGnd : will be used if it's a device->mono blt

   This will be followed by one or more ReadLine / ReadLineMono calls


ReadLineMono:
   Fetch 1 line from device & convert it to mono. The first pixel
   fetched from the device will be aligned to be the (dstX & 7)th
   pixel in the resultant data left in the PC data buffer.

ReadLine:
   Fetch 1 line from device, & align it so that the first pixel
   fetched from the device will be aligned to the (dstX & 7)th
   pixel in the PC data buffer. (the 7 is for 4-bpp modes; in 8bpp
   this will be 3, in 16bpp it will be 1 and 32bpp will be 0.

StrBlt:
   Used for the ExtTextOut function - this is essentially a multi-line
   mono-bitmap-to-screen blit. The line buffer is filled with as
   many lines of mono bitmap data as will fit, and this is then
   blitted onto the screen. The ExtY parameter gives the number
   of lines which will be put in the buffer, and LineOffset gives
   the gap in bytes between each one. Currently, this will support
   three opcodes - OPC_SRCMOVE (write given bits in background/
   foreground fashion to the screen) OPC_SRCXOR (xor given bits
   expanded to foreground colour with the screen) and OPC_SRCPAINT
   (set bits set in source to foreground colour in dest).

*/

#define CMD_READINFO	0x00
#define CMD_ENABLE	0x01
#define CMD_DISABLE	0x02
#define CMD_STRBLT	0x03
#define CMD_DEVBLIT	0x04
#define CMD_MEMBLIT	0x05
#define CMD_DEVFETCH	0x06
#define CMD_SCANLINE	0x07
#define CMD_POLYLINE	0x08
#define CMD_FASTBORDER	0x09
#define CMD_SAVEBITMAP	0x0A
#define CMD_PUTPIXEL	0x0B
#define CMD_GETPIXEL	0x0C
#define CMD_SCANLR	0x0D
#define CMD_WRITEXY	0x0E
#define CMD_BEGINDRAW	0x0F
#define CMD_ENDDRAW	0x10
#define CMD_UPDATECOLOURS 0x11
#define CMD_POINTERSHAPE 0x12
#define CMD_SHOWPOINTER	0x13
#define CMD_HIDEPOINTER	0x14
#define CMD_SETPALETTE	0x15
#define CMD_WRITELINE	0x16
#define CMD_WRITELINEMONO 0x17
#define CMD_READLINE	0x18
#define CMD_READLINEMONO 0x19
#define CMD_SENDINFO    0x1A
#define CMD_READMODELIST 0x1B

#define CMD_LAST	0x1B

#define CMD_WRITE_IO	0x80
#define CMD_READ_IO	0x81


/* Port definitions ------- */

#define WIN_PORT_FIRST  0x308
#define WIN_PORT_LAST   (WIN_PORT_FIRST+3)
#define WIN_CMD_PORT    (WIN_PORT_FIRST+0)
#define WIN_DATA_PORT   (WIN_PORT_FIRST+2)
#define WIN_STATUS_PORT (WIN_PORT_FIRST+0)

#define ST_READY	0x40
#define ST_BUSY		0x80
#define ST_LAST_OK	0x01
#define ST_LAST_ERR	0x02


/* for ValidateMode function; return these values direct to Windows */

#define VALMODE_YES 0
#define VALMODE_NO_NOMEM 2
#define VALMODE_NO_WRONGDRV 1


