1 /**
2  ** u_egavga.c ---- common EGA/VGA utilities for video drivers
3  **
4  ** Copyright (c) 1995 Csaba Biegl, 820 Stirrup Dr, Nashville, TN 37221
5  ** [e-mail: csaba@vuse.vanderbilt.edu]
6  **
7  ** This file is part of the GRX graphics library.
8  **
9  ** The GRX graphics library is free software; you can redistribute it
10  ** and/or modify it under some conditions; see the "copying.grx" file
11  ** for details.
12  **
13  ** This library is distributed in the hope that it will be useful,
14  ** but WITHOUT ANY WARRANTY; without even the implied warranty of
15  ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
16  **
17  **/
18 
19 #include "libgrx.h"
20 #include "grdriver.h"
21 #include "int86.h"
22 #include "vesa.h"
23 #include "memfill.h"
24 
_GrViDrvDetectEGAVGA(void)25 int _GrViDrvDetectEGAVGA(void)
26 {
27 	Int86Regs r;
28 	sttzero(&r);
29 	/* check for EGA/VGA by trying to read a palette register */
30 	IREG_AX(r) = 0x1007;
31 	IREG_BX(r) = 0xff00;
32 	int10(&r);
33 	return ( (unsigned char)IREG_BH(r) != (unsigned char)0xff );
34 }
35 
detectvga(void)36 static int detectvga(void)
37 {
38 	Int86Regs r;
39 	sttzero(&r);
40 	/* check for VGA by trying to read a DAC register */
41 	IREG_AX(r) = 0x1015;
42 	IREG_BX(r) = 0;
43 	IREG_CX(r) = 0xffff;
44 	IREG_DX(r) = 0xffff;
45 	int10(&r);
46 	return( (   (unsigned short)(IREG_CX(r) & IREG_DX(r))
47 		 != (unsigned short)0xffff                  ) ? TRUE : FALSE);
48 }
49 
_GrViDrvDetectEGA(void)50 int _GrViDrvDetectEGA(void)
51 {
52 	return((_GrViDrvDetectEGAVGA() && !detectvga()) ? TRUE : FALSE);
53 }
54 
_GrViDrvDetectVGA(void)55 int _GrViDrvDetectVGA(void)
56 {
57 	return((_GrViDrvDetectEGAVGA() && detectvga()) ? TRUE : FALSE);
58 }
59 
_GrViDrvGetCurrentEGAVGAmode(void)60 int _GrViDrvGetCurrentEGAVGAmode(void)
61 {
62 	Int86Regs r;
63 	sttzero(&r);
64 	IREG_AX(r) = VESA_FUNC + VESA_GET_MODE;
65 	int10(&r);
66 	if(IREG_AX(r) == VESA_SUCCESS) {
67 	    int mode = IREG_BX(r) & 0x7fff;
68 	    if(mode > 0x13) return(mode);
69 	}
70 	IREG_AX(r) = 0x0f00;
71 	int10(&r);
72 	return(IREG_AL(r) & 0x7f);
73 }
74 
setmode(int mode,int noclear)75 static int setmode(int mode,int noclear)
76 {
77 	Int86Regs r;
78 	sttzero(&r);
79 	IREG_AX(r) = VESA_FUNC + VESA_SET_MODE;
80 	IREG_BX(r) = (mode & 0x7fff) | (noclear ? 0x8000U : 0);
81 	int10(&r);
82 	if(IREG_AX(r) == VESA_SUCCESS) return(TRUE);
83 	IREG_AX(r) = (mode & 0x7f) | (noclear ? 0x80U : 0);
84 	int10(&r);
85 	return((_GrViDrvGetCurrentEGAVGAmode() == mode) ? TRUE : FALSE);
86 }
87 
_GrViDrvSetEGAVGAmode(GrVideoMode * mp,int noclear)88 int _GrViDrvSetEGAVGAmode(GrVideoMode *mp,int noclear)
89 {
90 	return(setmode(mp->mode,noclear));
91 }
92 
_GrViDrvSetEGAVGAcustomTextMode(GrVideoMode * mp,int noclear)93 int _GrViDrvSetEGAVGAcustomTextMode(GrVideoMode *mp,int noclear)
94 {
95 	if(_GrViDrvSetEGAVGAmode(mp,noclear)) {
96 	    Int86Regs r;
97 	    sttzero(&r);
98 	    /* load 8x8 or 8x14 font */
99 	    IREG_AX(r) = (mp->height >= 50) ? 0x1112 : 0x1111;
100 	    int10(&r);
101 	    return(TRUE);
102 	}
103 	return(FALSE);
104 }
105 
106 static int origmode = (-1);
107 
_GrViDrvInitEGAVGA(char * options)108 int _GrViDrvInitEGAVGA(char *options)
109 {
110 	if(_GrViDrvDetectEGAVGA()) {
111 	    origmode = _GrViDrvGetCurrentEGAVGAmode();
112 	    return(TRUE);
113 	}
114 	return(FALSE);
115 }
116 
_GrViDrvResetEGAVGA(void)117 void _GrViDrvResetEGAVGA(void)
118 {
119 	if((origmode != (-1)) && DRVINFO->moderestore) {
120 	    setmode(origmode,FALSE);
121 	}
122 }
123 
124 GrVideoModeExt _GrViDrvEGAVGAtextModeExt = {
125     GR_frameText,                       /* frame driver */
126     NULL,                               /* frame driver override */
127     MK_FP(0xb800,0),                    /* frame buffer address */
128     { 0, 0, 0 },                        /* color precisions */
129     { 0, 0, 0 },                        /* color component bit positions */
130     0,                                  /* mode flag bits */
131     _GrViDrvSetEGAVGAmode,              /* mode set */
132     NULL,                               /* virtual size set */
133     NULL,                               /* virtual scroll */
134     NULL,                               /* bank set function */
135     NULL,                               /* double bank set function */
136     NULL                                /* color loader */
137 };
138 
139 GrVideoModeExt _GrViDrvEGAVGAcustomTextModeExt = {
140     GR_frameText,                       /* frame driver */
141     NULL,                               /* frame driver override */
142     MK_FP(0xb800,0),                    /* frame buffer address */
143     { 0, 0, 0 },                        /* color precisions */
144     { 0, 0, 0 },                        /* color component bit positions */
145     0,                                  /* mode flag bits */
146     _GrViDrvSetEGAVGAcustomTextMode,    /* mode set */
147     NULL,                               /* virtual size set */
148     NULL,                               /* virtual scroll */
149     NULL,                               /* bank set function */
150     NULL,                               /* double bank set function */
151     NULL                                /* color loader */
152 };
153 
154