Use the faster span read/write template for 16bpp
[mesa.git] / src / mesa / drivers / dos / vesa.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.6 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 <dpmi.h>
35 #include <pc.h>
36 #include <stdlib.h>
37 #include <stubinfo.h>
38 #include <sys/exceptn.h>
39 #include <sys/segments.h>
40 #include <sys/farptr.h>
41 #include <sys/movedata.h>
42
43 #include "video.h"
44 #include "vesa.h"
45
46
47 static vl_mode modes[128];
48
49 static word16 vesa_ver;
50 static int banked_selector, linear_selector;
51 static int oldmode = -1;
52
53 static int vesa_color_precision = 6;
54
55 static word16 *vesa_pmcode;
56 unsigned int vesa_gran_mask, vesa_gran_shift;
57
58
59 /*
60 * VESA info
61 */
62 #define V_SIGN 0
63 #define V_MINOR 4
64 #define V_MAJOR 5
65 #define V_OEM_OFS 6
66 #define V_OEM_SEG 8
67 #define V_MODE_OFS 14
68 #define V_MODE_SEG 16
69 #define V_MEMORY 18
70
71 /*
72 * mode info
73 */
74 #define M_ATTR 0
75 #define M_GRAN 4
76 #define M_SCANLEN 16
77 #define M_XRES 18
78 #define M_YRES 20
79 #define M_BPP 25
80 #define M_RED 31
81 #define M_GREEN 33
82 #define M_BLUE 35
83 #define M_PHYS_PTR 40
84
85 /*
86 * VESA 3.0 CRTC timings structure
87 */
88 typedef struct CRTCInfoBlock {
89 unsigned short HorizontalTotal;
90 unsigned short HorizontalSyncStart;
91 unsigned short HorizontalSyncEnd;
92 unsigned short VerticalTotal;
93 unsigned short VerticalSyncStart;
94 unsigned short VerticalSyncEnd;
95 unsigned char Flags;
96 unsigned long PixelClock; /* units of Hz */
97 unsigned short RefreshRate; /* units of 0.01 Hz */
98 unsigned char reserved[40];
99 } __PACKED__ CRTCInfoBlock;
100
101 #define HNEG (1 << 2)
102 #define VNEG (1 << 3)
103 #define DOUBLESCAN (1 << 0)
104
105
106 /* Desc: Attempts to detect VESA, check video modes and create selectors.
107 *
108 * In : -
109 * Out : mode array
110 *
111 * Note: -
112 */
113 static vl_mode *
114 vesa_init (void)
115 {
116 __dpmi_regs r;
117 word16 *p;
118 vl_mode *q;
119 char vesa_info[512], tmp[512];
120 int maxsize = 0;
121 word32 linearfb = 0;
122
123 if (vesa_ver) {
124 return modes;
125 }
126
127 _farpokel(_stubinfo->ds_selector, 0, 0x32454256);
128 r.x.ax = 0x4f00;
129 r.x.di = 0;
130 r.x.es = _stubinfo->ds_segment;
131 __dpmi_int(0x10, &r);
132 movedata(_stubinfo->ds_selector, 0, _my_ds(), (unsigned)vesa_info, 512);
133 if ((r.x.ax != 0x004f) || ((_32_ vesa_info[V_SIGN]) != 0x41534556)) {
134 return NULL;
135 }
136
137 p = (word16 *)(((_16_ vesa_info[V_MODE_SEG])<<4) + (_16_ vesa_info[V_MODE_OFS]));
138 q = modes;
139 do {
140 if ((q->mode=_farpeekw(__djgpp_dos_sel, (unsigned long)(p++))) == 0xffff) {
141 break;
142 }
143
144 r.x.ax = 0x4f01;
145 r.x.cx = q->mode;
146 r.x.di = 512;
147 r.x.es = _stubinfo->ds_segment;
148 __dpmi_int(0x10, &r);
149 movedata(_stubinfo->ds_selector, 512, _my_ds(), (unsigned)tmp, 256);
150 switch (tmp[M_BPP]) {
151 case 16:
152 q->bpp = tmp[M_RED] + tmp[M_GREEN] + tmp[M_BLUE];
153 break;
154 case 8:
155 case 15:
156 case 24:
157 case 32:
158 q->bpp = tmp[M_BPP];
159 break;
160 default:
161 q->bpp = 0;
162 }
163 if ((r.x.ax == 0x004f) && ((tmp[M_ATTR] & 0x11) == 0x11) && q->bpp) {
164 q->xres = _16_ tmp[M_XRES];
165 q->yres = _16_ tmp[M_YRES];
166 q->scanlen = _16_ tmp[M_SCANLEN];
167 q->gran = (_16_ tmp[M_GRAN])<<10;
168 if (tmp[M_ATTR] & 0x80) {
169 vl_mode *q1 = q + 1;
170 *q1 = *q++;
171 linearfb = _32_ tmp[M_PHYS_PTR];
172 q->mode |= 0x4000;
173 }
174 if (maxsize < (q->scanlen * q->yres)) {
175 maxsize = q->scanlen * q->yres;
176 }
177 q++;
178 }
179 } while (TRUE);
180
181 if (q == modes) {
182 return NULL;
183 }
184 if (linearfb) {
185 maxsize = (maxsize + 0xfffUL) & ~0xfffUL;
186 if (_create_selector(&linear_selector, linearfb, maxsize)) {
187 return NULL;
188 }
189 }
190 if (_create_selector(&banked_selector, 0xa0000, modes[0].gran)) {
191 _remove_selector(&linear_selector);
192 return NULL;
193 }
194
195 for (q = modes; q->mode != 0xffff; q++) {
196 q->sel = (q->mode & 0x4000) ? linear_selector : banked_selector;
197 }
198
199 if (vesa_info[V_MAJOR] >= 2) {
200 r.x.ax = 0x4f0a;
201 r.x.bx = 0;
202 __dpmi_int(0x10, &r);
203 if (r.x.ax == 0x004f) {
204 vesa_pmcode = (word16 *)malloc(r.x.cx);
205 if (vesa_pmcode != NULL) {
206 movedata(__djgpp_dos_sel, (r.x.es << 4) + r.x.di, _my_ds(), (unsigned)vesa_pmcode, r.x.cx);
207 if (vesa_pmcode[3]) {
208 p = (word16 *)((long)vesa_pmcode + vesa_pmcode[3]);
209 while (*p++ != 0xffff) {
210 }
211 } else {
212 p = NULL;
213 }
214 if (p && (*p != 0xffff)) {
215 free(vesa_pmcode);
216 vesa_pmcode = NULL;
217 } else {
218 vesa_swbank = (void *)((long)vesa_pmcode + vesa_pmcode[0]);
219 }
220 }
221 }
222 }
223
224 vesa_ver = _16_ vesa_info[V_MINOR];
225 return modes;
226 }
227
228
229 /* Desc: Frees all resources allocated by VESA init code.
230 *
231 * In : -
232 * Out : -
233 *
234 * Note: -
235 */
236 static void
237 vesa_fini (void)
238 {
239 if (vesa_ver) {
240 _remove_selector(&linear_selector);
241 _remove_selector(&banked_selector);
242 if (vesa_pmcode != NULL) {
243 free(vesa_pmcode);
244 vesa_pmcode = NULL;
245 }
246 }
247 }
248
249
250 /* Desc: Uses VESA 3.0 function 0x4F0B to find the closest pixel clock to the requested value.
251 *
252 * In : mode, clock
253 * Out : desired clock
254 *
255 * Note: -
256 */
257 static unsigned long
258 _closest_pixclk (int mode_no, unsigned long vclk)
259 {
260 __dpmi_regs r;
261
262 r.x.ax = 0x4F0B;
263 r.h.bl = 0;
264 r.d.ecx = vclk;
265 r.x.dx = mode_no;
266 __dpmi_int(0x10, &r);
267
268 return (r.x.ax == 0x004f) ? r.d.ecx : 0;
269 }
270
271
272 /* Desc: Calculates CRTC mode timings.
273 *
274 * In : crtc block, geometry, adjust
275 * Out :
276 *
277 * Note:
278 */
279 static void
280 _crtc_timing (CRTCInfoBlock *crtc, int xres, int yres, int xadjust, int yadjust)
281 {
282 int HTotal, VTotal;
283 int HDisp, VDisp;
284 int HSS, VSS;
285 int HSE, VSE;
286 int HSWidth, VSWidth;
287 int SS, SE;
288 int doublescan = FALSE;
289
290 if (yres < 400) {
291 doublescan = TRUE;
292 yres *= 2;
293 }
294
295 HDisp = xres;
296 HTotal = (int)(HDisp * 1.27) & ~0x7;
297 HSWidth = (int)((HTotal - HDisp) / 5) & ~0x7;
298 HSS = HDisp + 16;
299 HSE = HSS + HSWidth;
300 VDisp = yres;
301 VTotal = VDisp * 1.07;
302 VSWidth = (VTotal / 100) + 1;
303 VSS = VDisp + ((int)(VTotal - VDisp) / 5) + 1;
304 VSE = VSS + VSWidth;
305
306 SS = HSS + xadjust;
307 SE = HSE + xadjust;
308
309 if (xadjust < 0) {
310 if (SS < (HDisp + 8)) {
311 SS = HDisp + 8;
312 SE = SS + HSWidth;
313 }
314 } else {
315 if ((HTotal - 24) < SE) {
316 SE = HTotal - 24;
317 SS = SE - HSWidth;
318 }
319 }
320
321 HSS = SS;
322 HSE = SE;
323
324 SS = VSS + yadjust;
325 SE = VSE + yadjust;
326
327 if (yadjust < 0) {
328 if (SS < (VDisp + 3)) {
329 SS = VDisp + 3;
330 SE = SS + VSWidth;
331 }
332 } else {
333 if ((VTotal - 4) < SE) {
334 SE = VTotal - 4;
335 SS = SE - VSWidth;
336 }
337 }
338
339 VSS = SS;
340 VSE = SE;
341
342 crtc->HorizontalTotal = HTotal;
343 crtc->HorizontalSyncStart = HSS;
344 crtc->HorizontalSyncEnd = HSE;
345 crtc->VerticalTotal = VTotal;
346 crtc->VerticalSyncStart = VSS;
347 crtc->VerticalSyncEnd = VSE;
348 crtc->Flags = HNEG | VNEG;
349
350 if (doublescan) {
351 crtc->Flags |= DOUBLESCAN;
352 }
353 }
354
355
356 /* Desc: Attempts to enter specified video mode.
357 *
358 * In : ptr to mode structure, refresh rate
359 * Out : 0 if success
360 *
361 * Note: -
362 */
363 static int
364 vesa_entermode (vl_mode *p, int refresh)
365 {
366 __dpmi_regs r;
367
368 if (p->mode & 0x4000) {
369 VESA.blit = _can_mmx() ? vesa_l_dump_virtual_mmx : vesa_l_dump_virtual;
370 } else {
371 VESA.blit = vesa_b_dump_virtual;
372 { int n; for (vesa_gran_shift=0, n=p->gran; n; vesa_gran_shift++, n>>=1) ; }
373 vesa_gran_mask = (1 << (--vesa_gran_shift)) - 1;
374 if ((unsigned)p->gran != (vesa_gran_mask+1)) {
375 return !0;
376 }
377 }
378
379 if (oldmode == -1) {
380 r.x.ax = 0x4f03;
381 __dpmi_int(0x10, &r);
382 oldmode = r.x.bx;
383 }
384
385 r.x.ax = 0x4f02;
386 r.x.bx = p->mode;
387
388 if (refresh && ((vesa_ver>>8) >= 3)) {
389 /* VESA 3.0 stuff for controlling the refresh rate */
390 CRTCInfoBlock crtc;
391 unsigned long vclk;
392 double f0;
393
394 _crtc_timing(&crtc, p->xres, p->yres, 0, 0);
395
396 vclk = (double)crtc.HorizontalTotal * crtc.VerticalTotal * refresh;
397 vclk = _closest_pixclk(p->mode, vclk);
398
399 if (vclk != 0) {
400 f0 = (double)vclk / (crtc.HorizontalTotal * crtc.VerticalTotal);
401 /*_current_refresh_rate = (int)(f0 + 0.5);*/
402
403 crtc.PixelClock = vclk;
404 crtc.RefreshRate = refresh * 100;
405
406 movedata(_my_ds(), (unsigned)&crtc, _stubinfo->ds_selector, 0, sizeof(crtc));
407
408 r.x.di = 0;
409 r.x.es = _stubinfo->ds_segment;
410 r.x.bx |= 0x0800;
411 }
412 }
413
414 __dpmi_int(0x10, &r);
415 if (r.x.ax != 0x004f) {
416 return !0;
417 }
418
419 if (p->bpp == 8) {
420 r.x.ax = 0x4f08;
421 r.x.bx = 0x0800;
422 __dpmi_int(0x10, &r);
423 if (r.x.ax == 0x004f) {
424 r.x.ax = 0x4f08;
425 r.h.bl = 0x01;
426 __dpmi_int(0x10, &r);
427 vesa_color_precision = r.h.bh;
428 }
429 }
430
431 return 0;
432 }
433
434
435 /* Desc: Restores to the mode prior to first call to vesa_entermode.
436 *
437 * In : -
438 * Out : -
439 *
440 * Note: -
441 */
442 static void
443 vesa_restore (void)
444 {
445 __dpmi_regs r;
446
447 if (oldmode != -1) {
448 r.x.ax = 0x4f02;
449 r.x.bx = oldmode;
450 __dpmi_int(0x10, &r);
451 oldmode = -1;
452 }
453 }
454
455
456 /* Desc: set one palette entry
457 *
458 * In : color index, R, G, B
459 * Out : -
460 *
461 * Note: uses integer values
462 */
463 static void
464 vesa_setCI_i (int index, int red, int green, int blue)
465 {
466 #if 0
467 __asm("\n\
468 movw $0x1010, %%ax \n\
469 movb %1, %%dh \n\
470 movb %2, %%ch \n\
471 int $0x10 \n\
472 "::"b"(index), "m"(red), "m"(green), "c"(blue):"%eax", "%edx");
473 #else
474 outportb(0x03C8, index);
475 outportb(0x03C9, red);
476 outportb(0x03C9, green);
477 outportb(0x03C9, blue);
478 #endif
479 }
480
481
482 /* Desc: set one palette entry
483 *
484 * In : color index, R, G, B
485 * Out : -
486 *
487 * Note: uses normalized values
488 */
489 static void
490 vesa_setCI_f (int index, float red, float green, float blue)
491 {
492 float max = (1 << vesa_color_precision) - 1;
493
494 vesa_setCI_i(index, (int)(red * max), (int)(green * max), (int)(blue * max));
495 }
496
497
498 /* Desc: state retrieval
499 *
500 * In : parameter name, ptr to storage
501 * Out : 0 if request successfully processed
502 *
503 * Note: -
504 */
505 static int
506 vesa_get (int pname, int *params)
507 {
508 switch (pname) {
509 case VL_GET_CI_PREC:
510 params[0] = vesa_color_precision;
511 break;
512 default:
513 return -1;
514 }
515 return 0;
516 }
517
518
519 /*
520 * the driver
521 */
522 vl_driver VESA = {
523 vesa_init,
524 vesa_entermode,
525 NULL,
526 vesa_setCI_f,
527 vesa_setCI_i,
528 vesa_get,
529 vesa_restore,
530 vesa_fini
531 };