#ifndef _VBA3__H_
#define _VBA3__H_

#include <Symbol.h>

#define COLOR_BIT1  1
#define COLOR_BIT4  4
#define COLOR_BIT8  8
#define COLOR_BIT15 15
#define COLOR_BIT16 16
#define COLOR_BIT24 24
#define COLOR_BIT32 32

//57             | 640x480  800x600  1024x768 1280x1024
//58 ------------+-------------------------------------
//59   08B - 256 |  0x101    0x103    0x105    0x107   
//60   15B - 32k |  0x110    0x113    0x116    0x119   
//61   16B - 64k |  0x111    0x114    0x117    0x11A   
//62   24B - 16M |  0x112    0x115    0x118    0x11B

#define VBE_AL_CODE_SUCCESS    0x4F
#define VBE_AH_CODE_SUCCESS    0 
#define VBE_AH_CODE_FAIL       1 
#define VBE_AH_CODE_NOTSUPPORT 2 
#define VBE_AH_CODE_INVALID    3 

#define RGB(r, g, b) \
	((DWORD)(((BYTE)(r)| \
	((DWORD)((BYTE)(g))<<8))| \
	(((DWORD)(BYTE)(b))<<16)))
;
#define RGB_16Bit(r, g, b)  \
	(((((WORD)(BYTE)(b))>>3) <<  0)|  \
	 ((((WORD)(BYTE)(g))>>2) <<  5)|  \
	 ((((WORD)(BYTE)(r))>>3) << 11))
;
#define RGB_16Bit_B(c)  (((((WORD)(c)) >>  0) & 0x1F) << 3)
#define RGB_16Bit_G(c)  (((((WORD)(c)) >>  5) & 0x3F) << 2)
#define RGB_16Bit_R(c)  (((((WORD)(c)) >> 11) & 0x1F) << 3)

#define RGB_24Bit(r, g, b)  \
	((DWORD)(((BYTE)(r)| \
	((DWORD)((BYTE)(g))<<8))| \
	(((DWORD)(BYTE)(b))<<16)))
;
#define VBE_COORDINATE320(x,y) (((DWORd)y<<8L)+((DWORD)y<<6L)+((DWORD)x))
#define VBE_COORDINATE512(x,y) (((DWORD)y<<9L)+((DWORD)x))
#define VBE_COORDINATE640(x,y) (((DWORD)y<<9L)+((DWORD)y<<7L)+((DWORD)x))
#define VBE_COORDINATE800(x,y) (((DWORD)y<<9L)+((DWORD)y<<8L)+((DWORD)y<<5L)+((DWORD)x))
#define VBE_COORDINATE1024(x,y) (((DWORD)y<<10L)+((DWORD)x))
#define VBE_COORDINATE1280(x,y) (((DWORD)y<<10L)+((DWORD)y<<8L)((DWORD)x))
;
typedef struct tagVBEPALETTE
{
	BYTE byBlue;
	BYTE byGreen;
	BYTE byRed;
	BYTE byAlignment;
}VBEPALETTE, *LPVBEPALETTE;

//VBE信息结构
typedef struct tagVBEINFO
{
	BYTE  byVESASignature[4];  //VBE 签名，四个标识字节，一般为"VESA"
	WORD  wVESAVersion;        //VBE 版本，BCD值，高字节指定了主版本号，低字节指定副版本号
	char FAR *pOEMString;      //OEM设备制造商名字的地址
	DWORD dwCapabilities;      //视频环境能力，B5-B31位保留
		//D0    = 0        DAC is fixed width, with 6 bits per primary color
		//		= 1        DAC width is switchable to 8 bits per primary color
		//D1    = 0        Controller is VGA compatible
		//		= 1        Controller is not VGA compatible
		//D2    = 0        Normal RAMDAC operation
		//		= 1        When programming large blocks of information to the RAMDAC,
        //			        use the blank bit in Function 09h.
		//D3    = 0        No hardware stereoscopic signaling support
		//		= 1        Hardware stereoscopic signaling supported by controller
		//D4    = 0        Stereo signaling supported via external VESA stereo connector
		//		= 1        Stereo signaling supported via VESA EVC connector
		//D5-31 = Reserved

	BYTE FAR *pVideoModePtr;   //支持显示模式的模式号列表指针，每个模式号占两个字节，该列表以0FFFFh 结尾
	WORD  wTotalMemory;        //内存块的数目（VBE2以上），以64K字节为单位的显示存贮器的容量，并非所有的视频模式都能寻址整个内存

	WORD  wOemSoftwareRev;      //VBE 执行程序版本，BCD值，高字节指定了主版本号，低字节指定副版本号
	DWORD dwOemVendorNamePtr;    //指向供应商名称字符串的远指针
	DWORD dwOemProductNamePtr;   //指向产品名称字符串的远指针
	DWORD dwOemProductRevPtr;    //指向产品版本字符串的远指针

	BYTE  Reserved[222];       //保留
	BYTE  OemData[256];        //OEM 字符串数据区

}VBEINFO, *LPVBEINFO;


//VBE模式信息结构
typedef struct tagVBEMODEINFO 
{
	WORD  wModeAttributes;       //显示方式属性，D5-D15保留
		//D0  =  指明该图形模式是否被支持
		//    0 =  不支持
		//    1 =  支持
		//D1 = 1 (Reserved)
		//D2  =  指示 视频 BIOS 是否支持诸如：TTY 输出、卷屏 等等
		//    0 =  不支持
		//    1 =  支持
		//D3  =  指示彩色模式
		//    0 =  单色模式
		//    1 =  采色模式
		//D4  = 指示图形模式
		//    0 =  文本
		//    1 =  图形
		//D5  =  指示模式使用的寄存器 、I/O端口是否和标准VGA兼容
		//    0 =  是
		//    1 =  否
		//D6  =  VGA compatible windowed memory mode is available
		//    0 =  是
		//    1 =  否
		//D7  =  指示线性帧缓冲区内存模型的存在
		//    0 =  否
		//    1 =  是
		//D8  =  Double scan mode is available
		//    0 =  否
		//    1 =  是
		//D9  =  Interlaced mode is available
		//    0 =  否
		//    1 =  是
		//D10 =  Hardware triple buffering support
		//    0 =  否
		//    1 =  是
		//D11 =  Hardware stereoscopic display support
		//    0 =  否
		//    1 =  是
		//D12 =  Dual display start address support
		//    0 =  否
		//    1 =  是
		//D13-D15 = Reserved

	BYTE  byWinAAttributes;      //显示存贮器窗口A属性（注：VESA支持两个窗口）
		//D0   =  指示可再定位窗口支持      
		//     0 =  Single non-relocatable window only
		//     1 =  Relocatable window(s) are supported
		//D1   = Window readable
		//     0 =  Window is not readable
		//     1 =  Window is readable
		//D2   = Window writeable
		//     0 =  Window is not writeable
		//     1 =  Window is writeable
		//D3-D7= Reserved

	BYTE  byWinBAttributes;      //显示存贮器窗口B属性（注：VESA支持两个窗口）
	WORD  wWinGranularity;       //视频窗口的粒度，单位为KB字节
	WORD  wWinSize;              //视频窗口的大小，单位为KB字节
	WORD  wWinASegment;          //视频窗口A的起始地址
	WORD  wWinBSegment;          //视频窗口B的起始地址
	void (FAR *WinFuncPtr)(void);//窗口函数调用入口地址
	WORD  wBytesPerScanLine;     //每条扫描线上的字节数（ banked模式时）

	//Mandatory information for VBE 1.2 and above
	WORD  wXResolution;           //水平分辩率
	WORD  wYResolution;           //垂直分辩率
	BYTE  byXCharSize;            //字符宽度
	BYTE  byYCharSize;            //字符高度
	BYTE  byNumberOfPlanes;       //显示存储器的位平面个数
	BYTE  byBitsPerPixel;         //每象素对应的二进制位数
	BYTE  byNumberOfBanks;        //Bank的数量
	BYTE  byMemoryModel;          //显示存储器的组织模式
	BYTE  byBankSize;             //显示存储块的长度
	BYTE  byNumberOfImagePages;   //图象数
	BYTE  byReserved_page;        //

	//Direct Color fields
	BYTE  byRedMaskSize;          // 
	BYTE  byRedFieldPosition;       //
	BYTE  byGreenMaskSize;        //
	BYTE  byGreenFieldPosition;    //
	BYTE  byBlueMaskSize;         //
	BYTE  byBlueFieldPosition;     //
	BYTE  byReservedMaskSize;     //
	BYTE  byReservedMaskPosition; //
	BYTE  byDirectColorModeInfo;  //

	//Mandatory information for VBE2.0 and above
	DWORD dwPhysBasePtr;         //
	DWORD dwOffScreenMemOffset;  //
	WORD  wOffScreenMemSize;     //

	//mandatory information for VBE3.0 and above
	WORD  wLinBytesPerScanLine;  //
	BYTE  byBnkNumberOfImagePages;//
	BYTE  byLinNumberOfImagePages;//
	BYTE  byLinRedMaskSize;       //
	BYTE  byLinRedFieldPosition;  //
	BYTE  byLinGreenMaskSize;     //
	BYTE  byLinGreenFieldPosition;//
	BYTE  byLinBlueMaskSize;      //
	BYTE  byLinBlueFieldPosition; //
	BYTE  byLinRsvdMaskSize;      //
	BYTE  byLinRsveFieldPosition; //
	DWORD dwMaxPixelClock;        //

	BYTE  byReserved[189];        //
	BYTE  byReserved2[1];         //
}VBEMODEINFO, *LPVBEMODEINFO;

//保护模式信息
typedef struct tagVBEPMINFO
{
	BYTE bySignature[4];  //PMID,  PM info Block Signature
	WORD wEntryPoint;     //??????,Offset of PM entry point within BIOS
	WORD wPMInitialize;   //??????,Offset of PM initizlization entry point
	WORD wBIOSDaaSel;     //000000,Selector to BIOS data area emulation block
	WORD wA0000Sel;       //A0000H,Selector to access A0000H physical mem
	WORD wB0000Sel;       //B0000H,Selector to access B0000H physical mem
	WORD wB8000Sel;       //B8000H,Selector to access B8000H physical mem
	WORD wCodeSegSel;     //C0000H,Selector to access Code segment as data
	BYTE byInProtectMode; //000000,Set to 1 when in protcted mode
	BYTE byCheckSum;      //??????,Checksum byte for structure
}VBEPMINFO, *LPVBEPMINFO;




typedef enum
{
	memTX   = 0, //Text mode
	memCGA  = 1, //CGA graphics
	memHC   = 2, //Hercules graphics
	memPL   = 3, //Planar
	memPK   = 4, //Packed pixel
	memNC   = 5, //Non-chain 4,256 color
	memRGB  = 6, //Direct color
	memYUV  = 7  //YUV
}memModels;




//CRTC结构，用于刷新屏率设置，VBE3及以上有效

#define CRTCINFO_SYNC_DOUBLE_SCAN             (1 << 0)
#define CRTCINFO_SYNC_INTERLACED              (1 << 1)
#define CRTCINFO_SYNC_HORIZONTAL_POSITIVE     (0 << 2)
#define CRTCINFO_SYNC_HORIZONTAL_NEGATIVE     (1 << 2)
#define CRTCINFO_SYNC_VERTICAL_POSITIVE       (0 << 3)
#define CRTCINFO_SYNC_VERTICAL_NEGATIVE       (1 << 3)

typedef struct tagVBECRTCINFO
{
	WORD  wHorizontalTotal;
	WORD  wHorizontalSyncStart;
	WORD  wHorizontalSyncEnd;

	WORD  wVerticalTotal;
	WORD  wVerticalSyncStart;
	WORD  wVerticalSyncEnd;

	BYTE  byFlags;
//        Bit0---Double Scan Mode Enable
//               0 -- Graphics mode is not double scanned
//               1 -- Graphics mode is double scanned
//        Bit1--- Interlaced Mode Enable
//               0 -- Graphics mode is non-interlaced
//               1 -- Graphics mode is interlaced
//        Bit2--- Horizontal sync polarity
//               0 -- Horizontal sync is positive(+)
//               1 -- Horizontal sync is negative(-)
//        Bit3--- Vertical sync polarity
//               0 -- Vertical sync is positive(+)
//               1 -- Vertical sync is negative(-)




	DWORD dwPixelClock;
	WORD  wRefreshRate;

	BYTE  byReserved[40];
	BYTE  byAlignment[1];
}VBECRTCINFO, *LPVBECRTCINFO;

typedef struct tagSUPVBEINFO
{
		BYTE bySpVbeSignature[4];
		WORD wSupVbeVersion;
		BYTE bySupVbeSubFunc[8];
		WORD wOemSoftwareRev;
		DWORD dwOemVendorNamePtr;
		DWORD dwOemProductNamePtr;
		DWORD dwOemProductRevPtr;
		DWORD dwOemStringPtr;

		BYTE  byReserved[221];

}SUPVBEINFO, *LPSUPVBEINFO;

//VBE的硬件换页结构
typedef struct  tagVBEENABLE
{
	BYTE  byEnablemode;    //换页之图形模式使能标志
	BYTE  byEnablebackbuf; //换页之后台显存使能标志
	BYTE  byCP;            //可视/后台操作标志
	WORD  wPS;             //换页数量
	WORD  wY1;             //可视视口显存起始映射Y
	WORD  wP1;             //可视视口页数
	WORD  wY2;             //后台视口显存起始映射Y
	WORD  wP2;             //后台视口页数
}VBEENABLE, *LPVBEENABLE;


BOOL VBE_GetControlInfo(VBEINFO* pVBEInfo);
BOOL VBE_GetModeInfo(int nMode, VBEMODEINFO* pVBEModeInfo);
BOOL VBE_SetMode(int nMode);
BOOL VBE_SetModeEx(int nMode, WORD wRefreshRate, int nInterlaced = 0);

unsigned FAR VBE_GetPixel8(int nX, int nY);
unsigned FAR VBE_GetPixel16(int nX, int nY);

void FAR VBE_PutPixel8(int nX, int nY, int nColor);
void FAR VBE_PutPixel16(int nX, int nY, int nColor);

void FAR VBE_Line8(int x1, int y1, int x2, int y2);
void FAR VBE_Line16(int x1, int y1, int x2, int y2);


BOOL VBE_VerityMode(int nMode);
int VBE_ChangeBank(int nPage, int nOldPage);

void FAR VBE_ScreenDC8(BYTE* pMemory, int nLeft, int nTop,
	int nWidth, int nHeight, BOOL bWriteVga);

void FAR VBE_ScreenDC16(BYTE* pMemory, int nLeft, int nTop,
	int nWidth, int nHeight, BOOL bWriteVga);

void FAR VBE_Bar8(int nLeft, int nTop, int nRight, int nBottom);
void FAR VBE_Bar16(int nLeft, int nTop, int nRight, int nBottom);

void VBE_RGBQuad_Wobble(int* pnPaletteNumber,
		RGBQUAD* pPalette_User, int nCount_User,
		RGBQUAD* pPalette_System, int nCount_System, int nStyle);

#endif

