/*
 *  TEX Device Driver  ver 2.02-
 *  copyright(c) 1988,89 by TSG, 1990-93 SHIMA
 *
 *  PREVIEWER ver 2.02-
 *  device.c : previewer module
 *             4th edition
 *
 *	very slightly modified against warnings by Oh-Yeah? 25 May 1992
 *  support EMS by SHIMA, Dec 1992
 *  modified for DOS/V by hero.h, Jan. 1993
 *
 *  Of course, this module is device-dependent.
 */

#define SIZE64KB	65536L
#define ONEBYTE     8
#define V_BIOS      0x10	/* Video Bios Int. NO. */
#define BIOS_WORK   0x0040	/* BIOS work data area segment address */
#define RESUME_BASE 0x00F0	/* resume information area address */

#undef LOOK_SMOOTH	/* To look smooth in hard scrolling */
#define	LOOK_SMOOTH
#define WEIGHT 900	/* Display weight to look smooth */

/* Display resolution */
#define RES_640_480 0
#define RES_800_600 1
#define RES_1024_768 2
#define RES_1280_1024 3

/* Video chip */
#define CHIP_VGA     0
#define CHIP_ET4000  1
#define CHIP_86C911  2
#define CHIP_T8900   3
#define CHIP_WD90C00 4
#define CHIP_WD100X  5
#define CHIP_MACH	 6
#define CHIP_VBE 	 9

/* Video Mode information */
#define VMODE_VGA         0x12	/* 640 x 480 16 color */
#define VMODE_ET4000_800  0x29	/* 800 x 600 16 color */
#define VMODE_WD90C00_800 0x58
#define VMODE_T8900_800   0x5B
#define VMODE_86C911_800  0x6A
#define VMODE_MACH_800    0x54

#define VMODE_ET4000_1K   0x37	/* 1024 x 768 16 color */
#define VMODE_WD90C00_1K  0x5D
#define VMODE_T8900_1K    0x5F
#define VMODE_86C911_1K   0x104
#define VMODE_MACH_1K	  0x55

#define VMODE_HITEXT      0x72	/* HiText driver */
#define VMODE_ERR_VESA    0xFFFD
#define VMODE_ERR_HIRESO  0xFFFE
#define VMODE_UNKNOWN     0xFFFF

/* VGA regieters */
#define START_ADDRESS_HIGH  0X0C
#define START_ADDRESS_LOW   0X0D
#define AC_INDEX           0X3C0
#define AC_HPP             0X13 | 0X20
#define OFFSET_REG         0X013
#define INPUT_STATUS1      0x3DA
#define V_RETRACE          0x08

/* SVGA registers */
#define ET4000_BANK_PORT  0x3CD
#define ET4000_DISP_INDEX 0x33
#define WD90C00_GRC_PORT   0x3CE
#define WD90C00_BANK_PORT  0x3CE
#define WD90C00_BANK_INDEX 0x09
#define WD90C00_DISP_INDEX 0x0D
#define T8900_BANK_PORT  0x3C4
#define T8900_BANK_INDEX 0x0E
#define T8900_DISP_INDEX 0x1E
#define S86C911_BANK_INDEX 0x35
#define S86C911_DISP_INDEX 0x31
#define S86C80X_BANK_INDEX 0x50
#define S86C80X_DISP_INDEX 0x51
#define S86C911_S3_LOCK    0x38
#define S86C911_SC_LOCK    0x39
#define MACH_CMD_PORT 0x1ce

#define G_TOP       0xa0000000L
#define G_SEG       0xa000

#define G_WIDTH_B_MAX  200
#define G_WIDTH_B_1280 160
#define G_WIDTH_B_1024 128
#define G_WIDTH_B_800  100
#define G_WIDTH_B_640  80

#define G_HEIGHT_MAX   1200
#define G_HEIGHT_1024  1024
#define G_HEIGHT_768   768
#define G_HEIGHT_600   600
#define G_HEIGHT_480   480

#define X_ORG_B_640 5
#define X_ORG_B_800 3
#define X_ORG_B_1024 1
#define X_ORG_B_1280 1

#define Y_ORG_480 2
#define Y_ORG_600 2
#define Y_ORG_768 2
#define Y_ORG_1024 2

#define DIV_F1_HI   2	/* 1/2 view */
#define DIV_F2_HI   4	/* 1/4 */
#define SHIFT_F1_HI 1	/* 1/2 view */
#define SHIFT_F2_HI  2	/* 1/4 */

#define DIV_F1_LOW   4	/* 1/4 view */
#define DIV_F2_LOW   8	/* 1/8 */
#define SHIFT_F1_LOW 2	/* 1/4 view */
#define SHIFT_F2_LOW 3	/* 1/8 */

/* Display offset (Byte) */
#define X_ORG_B     5
#define Y_ORG_B     2

#define ON          1
#define OFF         0
#define ESC         0x1b
#define V_SHIFT     1

#define GRAM_BUF    uchar far

#define	MAX_VIEW	16	/* 縮小画面数 */

#define DIVSCAN_MAX 17	/* 画面分割バッファ数 */

struct VIEW {
	int current;	/* current screen to be saved	*/
	int total;	/* total screen saved			*/
	int page[MAX_VIEW];	/* page numbers for screens		*/
};

struct SCROLL {
	/* output parameter. send to scroll-functions and other device-output-
     * functions
     */
	ulong offset;
	/* Screen Display offset address (OFFSET) */
	ulong gaddr;
	/* the current position in video buffer */
	int o_width, o_height;
	/* width(bytes) and height(pixels) of output screen */
	int x, y;
	/* the current positioin in bitmap. */
	int gw_act, gh_act;
	/* width and height to output on screen */
	BOOL x_lock, y_lock;
	/* permition to scroll horizontal and vertical direction. */
	HUGE_BUF *mptr;
	/* the current position of bitmap pointer */
	int h_spc, v_spc;
	/* h_spc and v_spc is to put at the center of the screen */

};

/* light color element */
struct COLOR_ELEMENT {
	uchar r;
	uchar g;
	uchar b;
};

struct PALETTE {
	struct COLOR_ELEMENT fg;
	struct COLOR_ELEMENT bg;
};

typedef enum {
	BackSpace, SpaceKey,	/* PreviousPart, NextPart   */
	Key_P, Key_N,	/* PreviousPage, NextPage   */
	CaridgeReturn,	/* NextPageDirection        */
	EscapeKey,	/* EscapeQuit               */
	Key_Q,	/* Same as above            */
	Key_B,	/* Another Previous Page	*/
	Key_V,	/* View outline			*/
	Key_C,	/* Comressed View		*/
	Key_R,	/* Reverse			*/
	Key_H,	/* HELP 			*/
	HELP,	/* Same as above		*/
	Key_M,	/* Move to a page		*/
	Key_G,	/* Same as above		*/
	ArrowDown, ArrowUp, ArrowLeft, ArrowRight,
	ShiftDown, ShiftUp, ShiftLeft, ShiftRight, LoopEnd
} KeyInput;

/* VESA BIOS Extention information structure */
/* VBE information */
struct VBE_INFO {
	char sign[4];
	uchar ver_lo;
	uchar ver_hi;
	char far *oemstrp;
	ulong capa;
	uint far *modesp;
};

/* VBE Mode information */
struct VBE_MODE_INFO {
	uint mode_attr;
	uchar win_a_attr;
	uchar win_b_attr;
	uint win_granula;
	uint win_size;
	uint win_a_seg;
	uint win_b_seg;
	void (far * win_func) (void);
	uint win_bps;
	/* Optional information */
	uint x_reso;
	uint y_reso;
	uchar x_char_size;
	uchar y_char_size;
	uchar number_of_planes;
	uchar bits_per_pixel;
	uchar number_of_banks;
	uchar memory_model;
	uchar bank_size;
	uchar image_pages;
	uchar reserve00;
	/* VBE 1.2+ */
	uchar red_mask_size;
	uchar red_field_pos;
	uchar green_mask_size;
	uchar green_field_pos;
	uchar blue_mask_size;
	uchar blue_field_pos;
	uchar reserved_mask_size;
	uchar reserved_field_pos;
	uchar direct_color_mode_info;
};

/* VBE モード動作時に使用する情報の構造 */
struct VBE_ACT_INFO {
	ulong granula;
	ulong int size;
	uint width;
	uint rd_win;
	uint wr_win;
	BUFFER far *rd_top;
	BUFFER far *wr_top;
};

/* チップ <=> 画面モード・対応テーブル構造 */
struct CHIP_MODES {
	uint chip;
	uint vmode;
};

/* 解像度 <=> VESA 画面モード・対応テーブル構造 */
struct VESA_MODES {
	int reso;
	uint vmode;
};

/* 画面分割テーブル構造 */
struct DIVSCAN {
	BOOL div;	/* スキャンライン分割フラグ */
	uint lines;	/* 同一バンクのライン数 */
	uint len1;	/* スキャンライン・バイト数(分割:前半、非分割:全長) */
	uint len2;	/* 後半スキャンライン・バイト数 */
};

/* オプション=>実装名 変換テーブル構造 */
static struct OPT_NAMES {
	int opt;	/* オプション値 */
	char *name;	/* 実装名文字列 */
};
