summaryrefslogtreecommitdiff
path: root/src/mesa/drivers/dos/vesa.c
blob: 3fdd3e25db4f8d1fa7afd56286c110a400ccfcbb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
/*
 * Mesa 3-D graphics library
 * Version:  4.1
 * 
 * Copyright (C) 1999  Brian Paul   All Rights Reserved.
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included
 * in all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

/*
 * DOS/DJGPP device driver for Mesa
 *
 *  Author: Daniel Borca
 *  Email : dborca@users.sourceforge.net
 *  Web   : http://www.geocities.com/dborca
 */


#include <dpmi.h>
#include <pc.h>
#include <stdlib.h>
#include <stubinfo.h>
#include <sys/exceptn.h>
#include <sys/segments.h>
#include <sys/farptr.h>
#include <sys/movedata.h>

#include "video.h"
#include "vesa.h"


static vl_mode modes[128];

static word16 vesa_ver;
static int banked_selector, linear_selector;
static int oldmode = -1;

static int vesa_color_precision = 6;

static word16 *vesa_pmcode;
unsigned int vesa_gran_mask, vesa_gran_shift;


/*
 * VESA info
 */
#define V_SIGN     0
#define V_MINOR    4
#define V_MAJOR    5
#define V_OEM_OFS  6
#define V_OEM_SEG  8
#define V_MODE_OFS 14
#define V_MODE_SEG 16
#define V_MEMORY   18

/*
 * mode info
 */
#define M_ATTR     0
#define M_GRAN     4
#define M_SCANLEN  16
#define M_XRES     18
#define M_YRES     20
#define M_BPP      25
#define M_RED      31
#define M_GREEN    33
#define M_BLUE     35
#define M_PHYS_PTR 40

/*
 * VESA 3.0 CRTC timings structure
 */
typedef struct CRTCInfoBlock {
    unsigned short HorizontalTotal;
    unsigned short HorizontalSyncStart;
    unsigned short HorizontalSyncEnd;
    unsigned short VerticalTotal;
    unsigned short VerticalSyncStart;
    unsigned short VerticalSyncEnd;
    unsigned char  Flags;
    unsigned long  PixelClock;	/* units of Hz */
    unsigned short RefreshRate;	/* units of 0.01 Hz */
    unsigned char  reserved[40];
} __PACKED__ CRTCInfoBlock;

#define HNEG         (1 << 2)
#define VNEG         (1 << 3)
#define DOUBLESCAN   (1 << 0)


/* Desc: Attempts to detect VESA, check video modes and create selectors.
 *
 * In  : -
 * Out : mode array
 *
 * Note: -
 */
static vl_mode *
vesa_init (void)
{
    __dpmi_regs r;
    word16 *p;
    vl_mode *q;
    char vesa_info[512], tmp[512];
    int maxsize = 0;
    word32 linearfb = 0;

    if (vesa_ver) {
	return modes;
    }

    _farpokel(_stubinfo->ds_selector, 0, 0x32454256);
    r.x.ax = 0x4f00;
    r.x.di = 0;
    r.x.es = _stubinfo->ds_segment;
    __dpmi_int(0x10, &r);
    movedata(_stubinfo->ds_selector, 0, _my_ds(), (unsigned)vesa_info, 512);
    if ((r.x.ax != 0x004f) || ((_32_ vesa_info[V_SIGN]) != 0x41534556)) {
	return NULL;
    }

    p = (word16 *)(((_16_ vesa_info[V_MODE_SEG]) << 4) + (_16_ vesa_info[V_MODE_OFS]));
    q = modes;
    do {
	if ((q->mode = _farpeekw(__djgpp_dos_sel, (unsigned long)(p++))) == 0xffff) {
	    break;
	}

	r.x.ax = 0x4f01;
	r.x.cx = q->mode;
	r.x.di = 512;
	r.x.es = _stubinfo->ds_segment;
	__dpmi_int(0x10, &r);
	movedata(_stubinfo->ds_selector, 512, _my_ds(), (unsigned)tmp, 256);
	switch (tmp[M_BPP]) {
	    case 16:
		q->bpp = tmp[M_RED] + tmp[M_GREEN] + tmp[M_BLUE];
		break;
	    case 8:
	    case 15:
	    case 24:
	    case 32:
		q->bpp = tmp[M_BPP];
		break;
	    default:
		q->bpp = 0;
	}
	if ((r.x.ax == 0x004f) && ((tmp[M_ATTR] & 0x11) == 0x11) && q->bpp) {
	    q->xres = _16_ tmp[M_XRES];
	    q->yres = _16_ tmp[M_YRES];
	    q->scanlen = _16_ tmp[M_SCANLEN];
	    q->gran = (_16_ tmp[M_GRAN]) << 10;
	    if (tmp[M_ATTR] & 0x80) {
		vl_mode *q1 = q + 1;
		*q1 = *q++;
		linearfb = _32_ tmp[M_PHYS_PTR];
		q->mode |= 0x4000;
	    }
	    if (maxsize < (q->scanlen * q->yres)) {
		maxsize = q->scanlen * q->yres;
	    }
	    q++;
	}
    } while (TRUE);

    if (q == modes) {
	return NULL;
    }
    if (_create_selector(&banked_selector, 0xa0000, modes[0].gran)) {
	return NULL;
    }
    if (linearfb) {
	maxsize = ((maxsize + 0xfffUL) & ~0xfffUL);
	if (_create_selector(&linear_selector, linearfb, maxsize)) {
	    linear_selector = banked_selector;
	}
    }

    for (q = modes; q->mode != 0xffff; q++) {
	q->sel = banked_selector;
	if (q->mode & 0x4000) {
	    if (linear_selector != banked_selector) {
		q->sel = linear_selector;
	    } else {
		q->mode &= ~0x4000;
	    }
	}
    }

    if (vesa_info[V_MAJOR] >= 2) {
	r.x.ax = 0x4f0a;
	r.x.bx = 0;
	__dpmi_int(0x10, &r);
	if (r.x.ax == 0x004f) {
	    vesa_pmcode = (word16 *)malloc(r.x.cx);
	    if (vesa_pmcode != NULL) {
		movedata(__djgpp_dos_sel, (r.x.es << 4) + r.x.di, _my_ds(), (unsigned)vesa_pmcode, r.x.cx);
		if (vesa_pmcode[3]) {
		    p = (word16 *)((long)vesa_pmcode + vesa_pmcode[3]);
		    while (*p++ != 0xffff) {
		    }
		} else {
		    p = NULL;
		}
		if (p && (*p != 0xffff)) {
		    free(vesa_pmcode);
		    vesa_pmcode = NULL;
		} else {
		    vesa_swbank = (void *)((long)vesa_pmcode + vesa_pmcode[0]);
		}
	    }
	}
    }

    vesa_ver = _16_ vesa_info[V_MINOR];
    return modes;
}


/* Desc: Frees all resources allocated by VESA init code.
 *
 * In  : -
 * Out : -
 *
 * Note: -
 */
static void
vesa_fini (void)
{
    if (vesa_ver) {
	_remove_selector(&linear_selector);
	_remove_selector(&banked_selector);
	if (vesa_pmcode != NULL) {
	    free(vesa_pmcode);
	    vesa_pmcode = NULL;
	}
    }
}


/* Desc: Uses VESA 3.0 function 0x4F0B to find the closest pixel clock to the requested value.
 *
 * In  : mode, clock
 * Out : desired clock
 *
 * Note: -
 */
static unsigned long
_closest_pixclk (int mode_no, unsigned long vclk)
{
    __dpmi_regs r;

    r.x.ax = 0x4F0B;
    r.h.bl = 0;
    r.d.ecx = vclk;
    r.x.dx = mode_no;
    __dpmi_int(0x10, &r);

    return (r.x.ax == 0x004f) ? r.d.ecx : 0;
}


/* Desc: Calculates CRTC mode timings.
 *
 * In  : crtc block, geometry, adjust
 * Out :
 *
 * Note:
 */
static void
_crtc_timing (CRTCInfoBlock *crtc, int xres, int yres, int xadjust, int yadjust)
{
    int HTotal, VTotal;
    int HDisp, VDisp;
    int HSS, VSS;
    int HSE, VSE;
    int HSWidth, VSWidth;
    int SS, SE;
    int doublescan = FALSE;

    if (yres < 400) {
	doublescan = TRUE;
	yres *= 2;
    }

    HDisp = xres;
    HTotal = (int)(HDisp * 1.27) & ~0x7;
    HSWidth = (int)((HTotal - HDisp) / 5) & ~0x7;
    HSS = HDisp + 16;
    HSE = HSS + HSWidth;
    VDisp = yres;
    VTotal = VDisp * 1.07;
    VSWidth = (VTotal / 100) + 1;
    VSS = VDisp + ((int)(VTotal - VDisp) / 5) + 1;
    VSE = VSS + VSWidth;

    SS = HSS + xadjust;
    SE = HSE + xadjust;

    if (xadjust < 0) {
	if (SS < (HDisp + 8)) {
	    SS = HDisp + 8;
	    SE = SS + HSWidth;
	}
    } else {
	if ((HTotal - 24) < SE) {
	    SE = HTotal - 24;
	    SS = SE - HSWidth;
	}
    }

    HSS = SS;
    HSE = SE;

    SS = VSS + yadjust;
    SE = VSE + yadjust;

    if (yadjust < 0) {
	if (SS < (VDisp + 3)) {
	    SS = VDisp + 3;
	    SE = SS + VSWidth;
	}
    } else {
	if ((VTotal - 4) < SE) {
	    SE = VTotal - 4;
	    SS = SE - VSWidth;
	}
    }

    VSS = SS;
    VSE = SE;

    crtc->HorizontalTotal     = HTotal;
    crtc->HorizontalSyncStart = HSS;
    crtc->HorizontalSyncEnd   = HSE;
    crtc->VerticalTotal       = VTotal;
    crtc->VerticalSyncStart   = VSS;
    crtc->VerticalSyncEnd     = VSE;
    crtc->Flags               = HNEG | VNEG;

    if (doublescan) {
	crtc->Flags |= DOUBLESCAN;
    }
}


/* Desc: Attempts to choose a suitable blitter.
 *
 * In  : ptr to mode structure, software framebuffer bits
 * Out : blitter funciton, or NULL
 *
 * Note: -
 */
static BLTFUNC
_choose_blitter (vl_mode *p, int fbbits)
{
    BLTFUNC blitter;

    if (p->mode & 0x4000) {
	blitter = _can_mmx() ? vesa_l_dump_virtual_mmx : vesa_l_dump_virtual;
	    switch (p->bpp) {
		case 8:
		    switch (fbbits) {
			case 8:
			    break;
			case 16:
			    blitter = vesa_l_dump_16_to_8;
			    break;
			case 24:
			    blitter = vesa_l_dump_24_to_8;
			    break;
			case 32:
			    blitter = vesa_l_dump_32_to_8;
			    break;
			case 15:
			default:
			    return NULL;
		    }
		    break;
		case 15:
		    switch (fbbits) {
			case 16:
			    blitter = vesa_l_dump_16_to_15;
			    break;
			case 32:
			    blitter = vesa_l_dump_32_to_15;
			    break;
			case 8:
			case 15:
			case 24:
			default:
			    return NULL;
		    }
		    break;
		case 16:
		    switch (fbbits) {
			case 16:
			    break;
			case 32:
			    blitter = vesa_l_dump_32_to_16;
			    break;
			case 8:
			case 15:
			case 24:
			default:
			    return NULL;
		    }
		    break;
		case 24:
		    switch (fbbits) {
			case 24:
			    break;
			case 32:
			    blitter = vesa_l_dump_32_to_24;
			    break;
			case 8:
			case 15:
			case 16:
			default:
			    return NULL;
		    }
		    break;
		case 32:
		    switch (fbbits) {
			case 24:
			    blitter = vesa_l_dump_24_to_32;
			    break;
			case 32:
			    break;
			case 8:
			case 15:
			case 16:
			default:
			    return NULL;
		    }
		    break;
	    }
    } else {
	blitter = vesa_b_dump_virtual;
	    switch (p->bpp) {
		case 8:
		    switch (fbbits) {
			case 8:
			    break;
			case 16:
			    blitter = vesa_b_dump_16_to_8;
			    break;
			case 24:
			    blitter = vesa_b_dump_24_to_8;
			    break;
			case 32:
			    blitter = vesa_b_dump_32_to_8;
			    break;
			case 15:
			default:
			    return NULL;
		    }
		    break;
		case 15:
		    switch (fbbits) {
			case 16:
			    blitter = vesa_b_dump_16_to_15;
			    break;
			case 32:
			    blitter = vesa_b_dump_32_to_15;
			    break;
			case 8:
			case 15:
			case 24:
			default:
			    return NULL;
		    }
		    break;
		case 16:
		    switch (fbbits) {
			case 16:
			    break;
			case 32:
			    blitter = vesa_b_dump_32_to_16;
			    break;
			case 8:
			case 15:
			case 24:
			default:
			    return NULL;
		    }
		    break;
		case 24:
		    switch (fbbits) {
			case 24:
			    break;
			case 32:
			    blitter = vesa_b_dump_32_to_24;
			    break;
			case 8:
			case 15:
			case 16:
			default:
			    return NULL;
		    }
		    break;
		case 32:
		    switch (fbbits) {
			case 24:
			    blitter = vesa_b_dump_24_to_32;
			    break;
			case 32:
			    break;
			case 8:
			case 15:
			case 16:
			default:
			    return NULL;
		    }
		    break;
	    }
    }

    return blitter;
}


/* Desc: Attempts to enter specified video mode.
 *
 * In  : ptr to mode structure, refresh rate
 * Out : 0 if success
 *
 * Note: -
 */
static int
vesa_entermode (vl_mode *p, int refresh, int fbbits)
{
    __dpmi_regs r;

    if (!(p->mode & 0x4000)) {
	{ int n; for (vesa_gran_shift = 0, n = p->gran; n; vesa_gran_shift++, n >>= 1); }
	vesa_gran_mask = (1 << (--vesa_gran_shift)) - 1;
	if ((unsigned)p->gran != (vesa_gran_mask + 1)) {
	    return !0;
	}
    }

    VESA.blit = _choose_blitter(p, fbbits);
    if (VESA.blit == NULL) {
	return !0;
    }

    if (oldmode == -1) {
	r.x.ax = 0x4f03;
	__dpmi_int(0x10, &r);
	oldmode = r.x.bx;
    }

    r.x.ax = 0x4f02;
    r.x.bx = p->mode;

    if (refresh && ((vesa_ver >> 8) >= 3)) {
	/* VESA 3.0 stuff for controlling the refresh rate */
	CRTCInfoBlock crtc;
	unsigned long vclk;
	double f0;

	_crtc_timing(&crtc, p->xres, p->yres, 0, 0);

	vclk = (double)crtc.HorizontalTotal * crtc.VerticalTotal * refresh;
	vclk = _closest_pixclk(p->mode, vclk);

	if (vclk != 0) {
	    f0 = (double)vclk / (crtc.HorizontalTotal * crtc.VerticalTotal);
	    /*_current_refresh_rate = (int)(f0 + 0.5);*/

	    crtc.PixelClock  = vclk;
	    crtc.RefreshRate = refresh * 100;

	    movedata(_my_ds(), (unsigned)&crtc, _stubinfo->ds_selector, 0, sizeof(crtc));

	    r.x.di = 0;
	    r.x.es = _stubinfo->ds_segment;
	    r.x.bx |= 0x0800;
	}
    }

    __dpmi_int(0x10, &r);
    if (r.x.ax != 0x004f) {
	return !0;
    }

    if (p->bpp == 8) {
	r.x.ax = 0x4f08;
	r.x.bx = 0x0800;
	__dpmi_int(0x10, &r);
	if (r.x.ax == 0x004f) {
	    r.x.ax = 0x4f08;
	    r.h.bl = 0x01;
	    __dpmi_int(0x10, &r);
	    vesa_color_precision = r.h.bh;
	}
    }

    return 0;
}


/* Desc: Restores to the mode prior to first call to vesa_entermode.
 *
 * In  : -
 * Out : -
 *
 * Note: -
 */
static void
vesa_restore (void)
{
    __dpmi_regs r;

    if (oldmode != -1) {
	if (oldmode < 0x100) {
	    __asm("int $0x10"::"a"(oldmode));
	} else {
	    r.x.ax = 0x4f02;
	    r.x.bx = oldmode;
	    __dpmi_int(0x10, &r);
	}
	oldmode = -1;
    }
}


/* Desc: set one palette entry
 *
 * In  : color index, R, G, B
 * Out : -
 *
 * Note: uses integer values
 */
static void
vesa_setCI_i (int index, int red, int green, int blue)
{
#if 0
  __asm("\n\
		movw	$0x1010, %%ax	\n\
		movb	%1, %%dh	\n\
		movb	%2, %%ch	\n\
		int	$0x10		\n\
   "::"b"(index), "m"(red), "m"(green), "c"(blue):"%eax", "%edx");
#else
    outportb(0x03C8, index);
    outportb(0x03C9, red);
    outportb(0x03C9, green);
    outportb(0x03C9, blue);
#endif
}


/* Desc: set one palette entry
 *
 * In  : color index, R, G, B
 * Out : -
 *
 * Note: uses normalized values
 */
static void
vesa_setCI_f (int index, float red, float green, float blue)
{
    float max = (1 << vesa_color_precision) - 1;

    vesa_setCI_i(index, (int)(red * max), (int)(green * max), (int)(blue * max));
}


/* Desc: state retrieval
 *
 * In  : parameter name, ptr to storage
 * Out : 0 if request successfully processed
 *
 * Note: -
 */
static int
vesa_get (int pname, int *params)
{
    switch (pname) {
	case VL_GET_CI_PREC:
	    params[0] = vesa_color_precision;
	    break;
	default:
	    return -1;
    }
    return 0;
}


/*
 * the driver
 */
vl_driver VESA = {
    vesa_init,
    vesa_entermode,
    NULL,
    vesa_setCI_f,
    vesa_setCI_i,
    vesa_get,
    vesa_restore,
    vesa_fini
};