5a6447dd87ba9f6fc77b68801636b2e1d7d0f5db
[profile/ivi/mesa.git] / src / mesa / drivers / dos / vga.c
1 /*
2  * Mesa 3-D graphics library
3  * Version:  4.1
4  * 
5  * Copyright (C) 1999  Brian Paul   All Rights Reserved.
6  * 
7  * Permission is hereby granted, free of charge, to any person obtaining a
8  * copy of this software and associated documentation files (the "Software"),
9  * to deal in the Software without restriction, including without limitation
10  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11  * and/or sell copies of the Software, and to permit persons to whom the
12  * Software is furnished to do so, subject to the following conditions:
13  * 
14  * The above copyright notice and this permission notice shall be included
15  * in all copies or substantial portions of the Software.
16  * 
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
20  * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
21  * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23  */
24
25 /*
26  * DOS/DJGPP device driver for Mesa
27  *
28  *  Author: Daniel Borca
29  *  Email : dborca@users.sourceforge.net
30  *  Web   : http://www.geocities.com/dborca
31  */
32
33
34 #include <pc.h>
35 #include <stdlib.h>
36
37 #include "video.h"
38 #include "vga.h"
39
40
41 static vl_mode modes[] = {
42    {
43     /* .xres    = */ 320,
44     /* .yres    = */ 200,
45     /* .bpp     = */ 8,
46     /* .mode    = */ 0x13 | 0x4000,
47     /* .scanlen = */ 320,
48     /* .sel     = */ -1,
49     /* .gran    = */ 320*200
50    },
51    {
52     /* .xres    = */ -1,
53     /* .yres    = */ -1,
54     /* .bpp     = */ -1,
55     /* .mode    = */ 0xffff,
56     /* .scanlen = */ -1,
57     /* .sel     = */ -1,
58     /* .gran    = */ -1
59    }
60 };
61
62 static word16 vga_ver;
63 static int linear_selector;
64 static int oldmode = -1;
65
66 #define vga_color_precision 6
67
68
69 /* Desc: Attempts to detect VGA, check video modes and create selectors.
70  *
71  * In  : -
72  * Out : mode array
73  *
74  * Note: -
75  */
76 static vl_mode *
77 vga_init (void)
78 {
79    int rv = 0;
80
81    if (vga_ver) {
82       return modes;
83    }
84
85    __asm("\n\
86                 movw    $0x1a00, %%ax   \n\
87                 int     $0x10           \n\
88                 cmpb    $0x1a, %%al     \n\
89                 jne     0f              \n\
90                 cmpb    $0x07, %%bl     \n\
91                 jb      0f              \n\
92                 andl    $0xff, %%ebx    \n\
93                 movl    %%ebx, %0       \n\
94    0:":"=g"(rv)::"%eax", "%ebx");
95    if (rv == 0) {
96       return NULL;
97    }
98
99    if (_create_selector(&linear_selector, 0xa0000, 0x10000)) {
100       return NULL;
101    }
102
103    modes[0].sel = linear_selector;
104
105    vga_ver = rv;
106    return modes;
107 }
108
109
110 /* Desc: Frees all resources allocated by VGA init code.
111  *
112  * In  : -
113  * Out : -
114  *
115  * Note: -
116  */
117 static void
118 vga_fini (void)
119 {
120    if (vga_ver) {
121       _remove_selector(&linear_selector);
122    }
123 }
124
125
126 /* Desc: Attempts to choose a suitable blitter.
127  *
128  * In  : ptr to mode structure, software framebuffer bits
129  * Out : blitter funciton, or NULL
130  *
131  * Note: -
132  */
133 static BLTFUNC
134 _choose_blitter (vl_mode *p, int fbbits)
135 {
136     BLTFUNC blitter;
137
138     switch (fbbits) {
139         case 8:
140             blitter = _can_mmx() ? vesa_l_dump_virtual_mmx : vesa_l_dump_virtual;
141             break;
142         case 16:
143             blitter = vesa_l_dump_16_to_8;
144             break;
145         case 24:
146             blitter = vesa_l_dump_24_to_8;
147             break;
148         case 32:
149             blitter = vesa_l_dump_32_to_8;
150             break;
151         default:
152             return NULL;
153     }
154
155     return blitter;
156
157     (void)p;
158 }
159
160
161 /* Desc: Attempts to enter specified video mode.
162  *
163  * In  : ptr to mode structure, refresh rate
164  * Out : 0 if success
165  *
166  * Note: -
167  */
168 static int
169 vga_entermode (vl_mode *p, int refresh, int fbbits)
170 {
171     if (!(p->mode & 0x4000)) {
172         return -1;
173     }
174
175     VGA.blit = _choose_blitter(p, fbbits);
176     if (VGA.blit == NULL) {
177         return !0;
178     }
179
180     if (oldmode == -1) {
181         __asm("\n\
182                 movb    $0x0f, %%ah     \n\
183                 int     $0x10           \n\
184                 andl    $0xff, %%eax    \n\
185                 movl    %%eax, %0       \n\
186         ":"=g"(oldmode)::"%eax", "%ebx");
187     }
188
189     __asm("int $0x10"::"a"(p->mode&0xff));
190
191     return 0;
192
193     (void)refresh; /* silence compiler warning */
194 }
195
196
197 /* Desc: Restores to the mode prior to first call to vga_entermode.
198  *
199  * In  : -
200  * Out : -
201  *
202  * Note: -
203  */
204 static void
205 vga_restore (void)
206 {
207    if (oldmode != -1) {
208       __asm("int $0x10"::"a"(oldmode));
209       oldmode = -1;
210    }
211 }
212
213
214 /* Desc: set one palette entry
215  *
216  * In  : color index, R, G, B
217  * Out : -
218  *
219  * Note: uses integer values
220  */
221 static void
222 vga_setCI_i (int index, int red, int green, int blue)
223 {
224 #if 0
225    __asm("\n\
226                 movw    $0x1010, %%ax   \n\
227                 movb    %1, %%dh        \n\
228                 movb    %2, %%ch        \n\
229                 int     $0x10           \n\
230    "::"b"(index), "m"(red), "m"(green), "c"(blue):"%eax", "%edx");
231 #else
232    outportb(0x03C8, index);
233    outportb(0x03C9, red);
234    outportb(0x03C9, green);
235    outportb(0x03C9, blue);
236 #endif
237 }
238
239
240 /* Desc: set one palette entry
241  *
242  * In  : color index, R, G, B
243  * Out : -
244  *
245  * Note: uses normalized values
246  */
247 static void
248 vga_setCI_f (int index, float red, float green, float blue)
249 {
250     float max = (1 << vga_color_precision) - 1;
251
252     vga_setCI_i(index, (int)(red * max), (int)(green * max), (int)(blue * max));
253 }
254
255
256 /* Desc: state retrieval
257  *
258  * In  : parameter name, ptr to storage
259  * Out : 0 if request successfully processed
260  *
261  * Note: -
262  */
263 static int
264 vga_get (int pname, int *params)
265 {
266    switch (pname) {
267       case VL_GET_CI_PREC:
268          params[0] = vga_color_precision;
269          break;
270       default:
271          return -1;
272    }
273    return 0;
274 }
275
276
277 /*
278  * the driver
279  */
280 vl_driver VGA = {
281    vga_init,
282    vga_entermode,
283    NULL,
284    vga_setCI_f,
285    vga_setCI_i,
286    vga_get,
287    vga_restore,
288    vga_fini
289 };