Use the faster span read/write template for 16bpp
[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 v1.7 for Mesa
27 *
28 * Copyright (C) 2002 - Borca Daniel
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 enter specified video mode.
127 *
128 * In : ptr to mode structure, refresh rate
129 * Out : 0 if success
130 *
131 * Note: -
132 */
133 static int
134 vga_entermode (vl_mode *p, int refresh)
135 {
136 if (!(p->mode & 0x4000)) {
137 return -1;
138 }
139 VGA.blit = _can_mmx() ? vesa_l_dump_virtual_mmx : vesa_l_dump_virtual;
140
141 if (oldmode == -1) {
142 __asm("\n\
143 movb $0x0f, %%ah \n\
144 int $0x10 \n\
145 andl $0xff, %%eax \n\
146 movl %%eax, %0 \n\
147 ":"=g"(oldmode)::"%eax", "%ebx");
148 }
149
150 __asm("int $0x10"::"a"(p->mode&0xff));
151
152 return 0;
153
154 (void)refresh; /* silence compiler warning */
155 }
156
157
158 /* Desc: Restores to the mode prior to first call to vga_entermode.
159 *
160 * In : -
161 * Out : -
162 *
163 * Note: -
164 */
165 static void
166 vga_restore (void)
167 {
168 if (oldmode != -1) {
169 __asm("int $0x10"::"a"(oldmode));
170 oldmode = -1;
171 }
172 }
173
174
175 /* Desc: set one palette entry
176 *
177 * In : color index, R, G, B
178 * Out : -
179 *
180 * Note: uses integer values
181 */
182 static void
183 vga_setCI_i (int index, int red, int green, int blue)
184 {
185 #if 0
186 __asm("\n\
187 movw $0x1010, %%ax \n\
188 movb %1, %%dh \n\
189 movb %2, %%ch \n\
190 int $0x10 \n\
191 "::"b"(index), "m"(red), "m"(green), "c"(blue):"%eax", "%edx");
192 #else
193 outportb(0x03C8, index);
194 outportb(0x03C9, red);
195 outportb(0x03C9, green);
196 outportb(0x03C9, blue);
197 #endif
198 }
199
200
201 /* Desc: set one palette entry
202 *
203 * In : color index, R, G, B
204 * Out : -
205 *
206 * Note: uses normalized values
207 */
208 static void
209 vga_setCI_f (int index, float red, float green, float blue)
210 {
211 float max = (1 << vga_color_precision) - 1;
212
213 vga_setCI_i(index, (int)(red * max), (int)(green * max), (int)(blue * max));
214 }
215
216
217 /* Desc: state retrieval
218 *
219 * In : parameter name, ptr to storage
220 * Out : 0 if request successfully processed
221 *
222 * Note: -
223 */
224 static int
225 vga_get (int pname, int *params)
226 {
227 switch (pname) {
228 case VL_GET_CI_PREC:
229 params[0] = vga_color_precision;
230 break;
231 default:
232 return -1;
233 }
234 return 0;
235 }
236
237
238 /*
239 * the driver
240 */
241 vl_driver VGA = {
242 vga_init,
243 vga_entermode,
244 NULL,
245 vga_setCI_f,
246 vga_setCI_i,
247 vga_get,
248 vga_restore,
249 vga_fini
250 };