-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvga256.c
2649 lines (2442 loc) · 79.7 KB
/
vga256.c
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
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*------------------------------------------------------------------------------------------------------- */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <conio.h>
#include <malloc.h>
#include <dos.h>
#include <io.h>
#include <fcntl.h>
#include <mem.h>
#include "vga256.h"
/*------------------------------------------------------------------------------------------------------- */
#pragma pack (push, 1)
#define VBE2SIGNATURE "VBE2"
#define VBE3SIGNATURE "VBE3"
static struct rminfo
{
unsigned long EDI;
unsigned long ESI;
unsigned long EBP;
unsigned long reserved_by_system;
unsigned long EBX;
unsigned long EDX;
unsigned long ECX;
unsigned long EAX;
unsigned short flags;
unsigned short ES,DS,FS,GS,IP,CS,SP,SS;
} RMI;
/*
* Structures that hold the preallocated DOS-Memory Aeras
* and their translated near-pointers!
*/
static struct DPMI_PTR VbeInfoPool = {0,0};
static struct DPMI_PTR VbeModePool = {0,0};
static struct VBE_VbeInfoBlock *VBE_Controller_Info_Pointer;
static struct VBE_ModeInfoBlock *VBE_ModeInfo_Pointer;
static void *LastPhysicalMapping = NULL;
/*
* Structures, that hold the Informations needed to invoke
* PM-Interrupts
*/
static union REGS regs;
static struct SREGS sregs;
/*
* Some informations 'bout the last mode which was set
* These informations are required to compensate some differencies
* between the normal and direct PM calling methods
*/
static signed short vbelastbank = -1;
static unsigned long BytesPerScanline;
static unsigned char BitsPerPixel;
/*
* Protected Mode Direct Call Informations
*/
void VBE_InitPM (void);
static void * pm_setwindowcall;
static void * pm_setdisplaystartcall;
static void * pmcode = NULL;
/*
* This function pointers will be initialized after you called
* VBE_Init. It'll be set to the realmode or protected mode call
* code
*/
tagSetDisplayStartType VBE_SetDisplayStart;
tagSetBankType VBE_SetBank;
void Mystique_ModeInformation(short Mode, struct VBE_ModeInfoBlock * a);
void Mystique_SetMode(short Mode, int linear, int clear);
int Mystique_Detect(void);
int Mystique_FindMode(int xres, int yres, char bpp);
static int mga_maxmode;
static int mga_detect = 0;
static char *mgabase1; /* 16k register address space */
#pragma pack (pop)
static void PrepareRegisters (void)
{
memset(&RMI,0,sizeof(RMI));
memset(&sregs,0,sizeof(sregs));
memset(®s,0,sizeof(regs));
}
static void RMIRQ (char irq)
{
memset (®s, 0, sizeof (regs));
regs.w.ax = 0x0300; // Simulate Real-Mode interrupt
regs.h.bl = irq;
sregs.es = FP_SEG(&RMI);
regs.x.edi = FP_OFF(&RMI);
int386x(0x31, ®s, ®s, &sregs);
}
void DPMI_AllocDOSMem (short int paras, struct DPMI_PTR *p)
{
/* DPMI call 100h allocates DOS memory */
PrepareRegisters();
regs.w.ax=0x0100;
regs.w.bx=paras;
int386x( 0x31, ®s, ®s, &sregs);
p->segment=regs.w.ax;
p->selector=regs.w.dx;
}
void DPMI_FreeDOSMem (struct DPMI_PTR *p)
{
/* DPMI call 101h free DOS memory */
memset(&sregs,0,sizeof(sregs));
regs.w.ax=0x0101;
regs.w.dx=p->selector;
int386x( 0x31, ®s, ®s, &sregs);
}
void DPMI_UNMAP_PHYSICAL (void *p)
{
/* DPMI call 800h map physical memory*/
PrepareRegisters();
regs.w.ax=0x0801;
regs.w.bx=(unsigned short) (((unsigned long)p)>>16);
regs.w.cx=(unsigned short) (((unsigned long)p)&0xffff);
int386x( 0x31, ®s, ®s, &sregs);
}
void * DPMI_MAP_PHYSICAL (void *p, unsigned long size)
{
/* DPMI call 800h map physical memory*/
PrepareRegisters();
regs.w.ax=0x0800;
regs.w.bx=(unsigned short) (((unsigned long)p)>>16);
regs.w.cx=(unsigned short) (((unsigned long)p)&0xffff);
regs.w.si=(unsigned short) (((unsigned long)size)>>16);
regs.w.di=(unsigned short) (((unsigned long)size)&0xffff);
int386x( 0x31, ®s, ®s, &sregs);
return (void *) ((regs.w.bx << 16) + regs.w.cx);
}
void VBE_Controller_Information (struct VBE_VbeInfoBlock * a)
{
memcpy (a, VBE_Controller_Info_Pointer, sizeof (struct VBE_VbeInfoBlock));
}
void VBE_Mode_Information (short Mode, struct VBE_ModeInfoBlock * a)
{
if ( (mga_detect) && (Mode>mga_maxmode)) {
Mystique_ModeInformation (Mode, a);
return;
}
PrepareRegisters();
RMI.EAX=0x00004f01; // Get SVGA-Mode Information
RMI.ECX=Mode;
RMI.ES=VbeModePool.segment; // Segment of realmode data
RMI.EDI=0; // offset of realmode data
RMIRQ (0x10);
memcpy (a, VBE_ModeInfo_Pointer, sizeof (struct VBE_ModeInfoBlock));
}
int VBE_IsModeLinear (short Mode)
{
struct VBE_ModeInfoBlock a;
if ( Mode==0x13 ) {
return 1;
}
VBE_Mode_Information (Mode, &a);
return ((a.ModeAttributes & 128)==128);
}
void asmSDS (short lowaddr, short hiaddr);
#pragma aux asmSDS = "mov ax, 0x4f07" \
"xor ebx, ebx" \
"call [pm_setdisplaystartcall]" \
parm[cx][dx] modify [eax ebx ecx edx esi edi];
static void PM_SetDisplayStart (short x, short y)
{
unsigned long addr = (y*BytesPerScanline+x);
unsigned short loaddr = addr & 0xffff;
unsigned short hiaddr = (addr>>16);
asmSDS(loaddr, hiaddr);
}
void asmSB (short bnk);
#pragma aux asmSB = "mov ax, 0x4f05" \
"mov bx, 0" \
"call [pm_setwindowcall]" \
parm [dx] modify [eax ebx ecx edx esi edi];
static void PM_SetBank (short bnk)
{
if ( bnk==vbelastbank ) return;
vbelastbank=bnk;
asmSB(bnk);
}
static void RM_SetDisplayStart (short x, short y)
{
PrepareRegisters();
RMI.EAX=0x00004f07;
RMI.EBX=0;
RMI.ECX=x;
RMI.EDX=y;
RMIRQ (0x10);
}
void setbiosmode (unsigned short c);
#pragma aux setbiosmode = "int 0x10" parm[ax] modify[eax ebx ecx edx esi edi];
void VBE_SetMode (short Mode, int linear, int clear)
{
struct VBE_ModeInfoBlock a;
int rawmode;
/* Is it a normal textmode? if so set it directly! */
if ( Mode == 3 ) {
setbiosmode (Mode);
return;
}
if ( Mode == 0x13 ) {
setbiosmode (Mode);
return;
}
if ( (mga_detect) && (Mode>mga_maxmode)) {
Mystique_SetMode (Mode, linear, clear);
return;
}
rawmode = Mode & 0x01ff;
if ( linear) rawmode |= 1<<14;
if (!clear) rawmode |= 1<<15;
if ( linear && clear ) {
/*
* in case of a S3 card the following bugfix prevents a system-crash
* when a mode with lfb+clearscreen will be set. We simulate the same
* behaviour doing the clearscreen without LFB and setting the LFB
* without clearscreen afterwards (a little bit messy...)
*/
PrepareRegisters();
RMI.EAX=0x00004f02;
RMI.EBX=Mode & 0x01ff; // mode without LFB but clear
RMIRQ (0x10);
PrepareRegisters();
RMI.EAX=0x00004f02;
RMI.EBX=(Mode & 0x01ff)|(1<<14)|(1<<15); // mode with LFB but without clear
RMIRQ (0x10);
} else {
PrepareRegisters();
RMI.EAX=0x00004f02;
RMI.EBX=rawmode;
RMIRQ (0x10);
}
}
int VBE_Test (void)
{
return (VBE_Controller_Info_Pointer->vbeVersion.hi>=0x2);
}
unsigned int VBE_VideoMemory (void)
{
return (VBE_Controller_Info_Pointer->TotalMemory*1024*64);
}
int VBE_FindMode (int xres, int yres, char bpp)
{
int i;
struct VBE_ModeInfoBlock Info;
// First try to find the mode in the ControllerInfoBlock:
for (i=0; VBE_Controller_Info_Pointer->VideoModePtr[i]!=0xffff; i++ ) {
VBE_Mode_Information (VBE_Controller_Info_Pointer->VideoModePtr[i], &Info);
if ((xres == Info.XResolution) &&
(yres == Info.YResolution) &&
(bpp == Info.BitsPerPixel)) {
return VBE_Controller_Info_Pointer->VideoModePtr[i];
}
}
if ( mga_detect ) {
return Mystique_FindMode (xres, yres, bpp);
}
return -1;
}
char * VBE_GetVideoPtr (short mode)
{
void *phys;
struct VBE_ModeInfoBlock ModeInfo;
if ( mode==13 ) return (char *) 0xa0000;
VBE_Mode_Information (mode, &ModeInfo);
/* Unmap the last physical mapping (if there is one...) */
if ( LastPhysicalMapping ) {
DPMI_UNMAP_PHYSICAL (LastPhysicalMapping);
LastPhysicalMapping = NULL;
}
LastPhysicalMapping = DPMI_MAP_PHYSICAL ((void *) ModeInfo.PhysBasePtr,
(long)(VBE_Controller_Info_Pointer->TotalMemory)*64*1024);
return (char *) LastPhysicalMapping;
}
void RM_SetBank (short bnk)
{
if ( bnk==vbelastbank ) return;
PrepareRegisters();
RMI.EAX=0x00004f05;
RMI.EBX=0;
RMI.EDX=bnk;
RMIRQ (0x10);
vbelastbank=bnk;
}
short VBE_MaxBytesPerScanline (void)
{
PrepareRegisters();
RMI.EAX=0x00004f06;
RMI.EBX=3;
RMIRQ (0x10);
return regs.w.bx;
}
void VBE_SetPixelsPerScanline (short Pixels)
{
PrepareRegisters();
RMI.EAX=0x00004f06;
RMI.EBX=0;
RMI.ECX=Pixels;
RMIRQ (0x10);
BytesPerScanline = (Pixels*BitsPerPixel/8);
}
void VBE_SetDACWidth (char bits)
{
PrepareRegisters();
RMI.EAX=0x00004f08;
RMI.EBX=bits<<8;
RMIRQ (0x10);
}
int VBE_8BitDAC (void)
{
return (VBE_Controller_Info_Pointer->Capabilities & 1 );
}
void VBE_InitPM (void)
{
unsigned short *pm_pointer;
VBE_SetDisplayStart = RM_SetDisplayStart;
VBE_SetBank = RM_SetBank;
PrepareRegisters();
RMI.EAX=0x00004f0a;
RMIRQ (0x10);
if ( (RMI.EAX) == 0x004f ) {
if (pmcode) free (pmcode);
// get some memory to copy the stuff.
pmcode = malloc (RMI.ECX & 0x0000ffff);
pm_pointer = (unsigned short *) (((unsigned long )RMI.ES<<4)|(RMI.EDI));
memcpy (pmcode, pm_pointer, (RMI.ECX & 0x0000ffff));
pm_pointer = (unsigned short *) pmcode;
pm_setwindowcall = (void *) (((unsigned long )RMI.ES<<4)|(RMI.EDI+pm_pointer[0]));
pm_setdisplaystartcall = (void *) (((unsigned long )RMI.ES<<4)|(RMI.EDI+pm_pointer[1]));
VBE_SetDisplayStart = PM_SetDisplayStart;
VBE_SetBank = PM_SetBank;
}
}
void VBE_Init (void)
{
/* Allocate the Dos Memory for Mode and Controller Information Blocks */
/* and translate their pointers into flat memory space */
DPMI_AllocDOSMem (512/16, &VbeInfoPool);
DPMI_AllocDOSMem (256/16, &VbeModePool);
VBE_ModeInfo_Pointer = (struct VBE_ModeInfoBlock *) (VbeModePool.segment*16);
VBE_Controller_Info_Pointer = (struct VBE_VbeInfoBlock *) (VbeInfoPool.segment*16);
/* Get Controller Information Block only once and copy this block on */
/* all requests */
memset (VBE_Controller_Info_Pointer, 0, sizeof (struct VBE_VbeInfoBlock));
memcpy (VBE_Controller_Info_Pointer->vbeSignature, VBE2SIGNATURE, 4);
PrepareRegisters();
RMI.EAX=0x00004f00; // Get SVGA-Information
RMI.ES=VbeInfoPool.segment; // Segment of realmode data
RMI.EDI=0; // offset of realmode data
RMIRQ (0x10);
// Translate the Realmode Pointers into flat-memory address space
VBE_Controller_Info_Pointer->OemStringPtr=(char*)((((unsigned long)VBE_Controller_Info_Pointer->OemStringPtr>>16)<<4)+(unsigned short)VBE_Controller_Info_Pointer->OemStringPtr);
VBE_Controller_Info_Pointer->VideoModePtr=(unsigned short*)((((unsigned long)VBE_Controller_Info_Pointer->VideoModePtr>>16)<<4)+(unsigned short)VBE_Controller_Info_Pointer->VideoModePtr);
VBE_Controller_Info_Pointer->OemVendorNamePtr=(char*)((((unsigned long)VBE_Controller_Info_Pointer->OemVendorNamePtr>>16)<<4)+(unsigned short)VBE_Controller_Info_Pointer->OemVendorNamePtr);
VBE_Controller_Info_Pointer->OemProductNamePtr=(char*)((((unsigned long)VBE_Controller_Info_Pointer->OemProductNamePtr>>16)<<4)+(unsigned short)VBE_Controller_Info_Pointer->OemProductNamePtr);
VBE_Controller_Info_Pointer->OemProductRevPtr=(char*)((((unsigned long)VBE_Controller_Info_Pointer->OemProductRevPtr>>16)<<4)+(unsigned short)VBE_Controller_Info_Pointer->OemProductRevPtr);
VBE_InitPM();
Mystique_Detect ();
}
void VBE_Done (void)
{
if ( LastPhysicalMapping ) {
DPMI_UNMAP_PHYSICAL (LastPhysicalMapping);
}
DPMI_FreeDOSMem (&VbeModePool);
DPMI_FreeDOSMem (&VbeInfoPool);
if (pmcode) free (pmcode);
}
unsigned long PCI_ReadDword (unsigned char id, short offset)
{
PrepareRegisters();
RMI.EAX=0x0000b10a;
RMI.EBX=id<<3;
RMI.EDI=offset;
RMIRQ (0x1a);
return RMI.ECX;
}
int Mystique_Detect (void)
{
int i;
unsigned long value;
/*-----------------05-25-97 04:05pm-----------------
* Check for the PCI BIOS extionsions
* --------------------------------------------------*/
mga_detect = 0;
PrepareRegisters();
RMI.EAX=0x0000b101;
RMIRQ (0x1a);
if (!((RMI.EDX == 0x20494350) && ((RMI.flags &1 )==0))) return 0;
for ( i=0; i<32; i++ ) {
value = PCI_ReadDword (i, 0);
if (( (value & 0xffff) == 0x102b ) && ((value >> 16) == 0x051a))
{
/* Matrox Mystique Detect successfull */
mga_detect = 1;
/* Get Mystique Memory Mapped IO-Area */
mgabase1 = (char *) DPMI_MAP_PHYSICAL((void *)(PCI_ReadDword (i, 0x10) & 0xfffffff0), 16*1024);
/* Scan the BIOS Mode List. Find the highest mode-number */
mga_maxmode = 0;
for (i=0; VBE_Controller_Info_Pointer->VideoModePtr[i]!=0xffff; i++ )
if ( VBE_Controller_Info_Pointer->VideoModePtr[i]>mga_maxmode)
mga_maxmode = VBE_Controller_Info_Pointer->VideoModePtr[i];
mga_maxmode+=10;
return 1;
}
}
return 0;
}
void Mystique_Zoom (int activate)
/*-----------------05-25-97 04:06pm-----------------
* Enable or Disable the Matrox Mystique Hardware Zoom
* --------------------------------------------------*/
{
char val;
if ( activate ) {
// Zoom X Enable (DAC Control)
mgabase1[0x3c00]=0x38;
mgabase1[0x3c0a]=1;
// Zoom Y Enable (CRTCX Control)
mgabase1[0x1fd4]=0x09;
val= mgabase1[0x1fd5];
mgabase1[0x1fd4]=0x09;
mgabase1[0x1fd5]=val|128;
} else {
// Zoom X Disable (DAC Control)
mgabase1[0x3c00]=0x38;
mgabase1[0x3c0a]=0;
// Zoom Y Disable (CRTCX Control)
mgabase1[0x1fd4]=0x09;
val= mgabase1[0x1fd5];
mgabase1[0x1fd4]=0x09;
mgabase1[0x1fd5]=val &~128;
}
}
int Mystique_FindMode (int xres, int yres, char bpp)
{
int usemode=-1;
if ( !mga_detect ) return -1;
switch ( bpp ) {
case 8:
if ((xres==320) && (yres==240)) usemode = mga_maxmode+1;
if ((xres==400) && (yres==300)) usemode = mga_maxmode+2;
if ((xres==512) && (yres==384)) usemode = mga_maxmode+3;
break;
case 15:
if ((xres==320) && (yres==200)) usemode = mga_maxmode+4;
if ((xres==320) && (yres==240)) usemode = mga_maxmode+5;
if ((xres==400) && (yres==300)) usemode = mga_maxmode+6;
if ((xres==512) && (yres==384)) usemode = mga_maxmode+7;
break;
case 16:
if ((xres==320) && (yres==200)) usemode = mga_maxmode+8;
if ((xres==320) && (yres==240)) usemode = mga_maxmode+9;
if ((xres==400) && (yres==300)) usemode = mga_maxmode+10;
if ((xres==512) && (yres==384)) usemode = mga_maxmode+11;
break;
case 32:
if ((xres==320) && (yres==200)) usemode = mga_maxmode+12;
if ((xres==320) && (yres==240)) usemode = mga_maxmode+13;
if ((xres==400) && (yres==300)) usemode = mga_maxmode+14;
if ((xres==512) && (yres==384)) usemode = mga_maxmode+15;
break;
}
return usemode;
}
int Mystique_TranslateMode (short mode)
{
int usemode = -1;
if (mode == mga_maxmode+1) usemode = VBE_FindMode (640,480,8);
if (mode == mga_maxmode+2) usemode = VBE_FindMode (800,600,8);
if (mode == mga_maxmode+3) usemode = VBE_FindMode (1024,768,8);
if (mode == mga_maxmode+4) usemode = VBE_FindMode (640,400,15);
if (mode == mga_maxmode+5) usemode = VBE_FindMode (640,480,15);
if (mode == mga_maxmode+6) usemode = VBE_FindMode (800,600,15);
if (mode == mga_maxmode+7) usemode = VBE_FindMode (1024,768,15);
if (mode == mga_maxmode+8) usemode = VBE_FindMode (640,400,16);
if (mode == mga_maxmode+9) usemode = VBE_FindMode (640,480,16);
if (mode == mga_maxmode+10) usemode = VBE_FindMode (800,600,16);
if (mode == mga_maxmode+11) usemode = VBE_FindMode (1024,768,16);
if (mode == mga_maxmode+12) usemode = VBE_FindMode (640,400,32);
if (mode == mga_maxmode+13) usemode = VBE_FindMode (640,480,32);
if (mode == mga_maxmode+14) usemode = VBE_FindMode (800,600,32);
if (mode == mga_maxmode+15) usemode = VBE_FindMode (1024,768,32);
return usemode;
}
void Mystique_ModeInformation (short Mode, struct VBE_ModeInfoBlock * a)
{
int vbemode;
vbemode = Mystique_TranslateMode (Mode);
if ( vbemode!=-1 ) {
/* Create a faked Vesa Mode Information Block */
PrepareRegisters();
RMI.EAX=0x00004f01;
RMI.ECX=vbemode;
RMI.ES=VbeModePool.segment;
RMI.EDI=0;
RMIRQ (0x10);
memcpy (a, VBE_ModeInfo_Pointer, sizeof (struct VBE_ModeInfoBlock));
a->XResolution >>= 1;
a->YResolution >>= 1;
a->BytesPerScanline >>= 1;
}
}
void Mystique_SetBytesPerScanline (int nbytes)
{
char lo, hi, val;
nbytes>>=4;
lo=nbytes&0xff;
hi=(nbytes>>8)&3;
mgabase1[0x1fd4]=0x13;
mgabase1[0x1fd5]=lo;
mgabase1[0x1fde]=0x00;
val = mgabase1[0x1fdf];
val&=0xcf; val|= (hi<<4);
mgabase1[0x1fde]=0x00;
mgabase1[0x1fdf]=val;
}
void Mystique_SetMode (short Mode, int linear, int clear)
{
struct VBE_ModeInfoBlock a;
int rawmode;
if ( Mode> mga_maxmode ) {
rawmode = Mystique_TranslateMode (Mode & 0x01ff);
} else {
rawmode = (Mode & 0x01ff);
}
if ( linear) rawmode |= 1<<14;
if (!clear) rawmode |= 1<<15;
PrepareRegisters();
RMI.EAX=0x00004f02; // Get SVGA-Mode Information
RMI.EBX=rawmode;
RMIRQ (0x10);
VBE_Mode_Information (Mode, &a);
BytesPerScanline = a.BytesPerScanline;
BitsPerPixel = a.BitsPerPixel;
if (Mode>mga_maxmode) Mystique_Zoom (1); else Mystique_Zoom (0);
Mystique_SetBytesPerScanline (a.BytesPerScanline);
}
#if defined(VGA256_MODE_320X200X8)
#define VGA256OFFSET(pVideo, x, y) (((unsigned char *) pVideo)[(y << 8) + (y << 6) + x])
#elif defined(VGA256_MODE_640X480X8)
#define VGA256OFFSET(pVideo, x, y) (((unsigned char *) pVideo)[(y << 9) + (y << 7) + x])
#elif defined(VGA256_MODE_1024X768X8)
#define VGA256OFFSET(pVideo, x, y) (((unsigned char *) pVideo)[(y << 10) + x])
#endif
/*------------------------------------------------------------------------------------------------------- */
#define peekb(s,o) (*((unsigned char *) MK_FP((s),(o))))
#define peekw(s,o) (*((unsigned short *) MK_FP((s),(o))))
#define peekl(s,o) (*((unsigned long *) MK_FP((s),(o))))
#define pokeb(s,o,x) (*((unsigned char *) MK_FP((s),(o))) = (unsigned char)(x))
#define pokew(s,o,x) (*((unsigned short *) MK_FP((s),(o))) = (unsigned short)(x))
#define pokel(s,o,x) (*((unsigned long *) MK_FP((s),(o))) = (unsigned long)(x))
/*------------------------------------------------------------------------------------------------------- */
void VGA256PutPixel(void *pVideo, unsigned int x, unsigned int y, unsigned int color)
{
VGA256OFFSET(pVideo, x, y) = color;
}
/*------------------------------------------------------------------------------------------------------- */
unsigned int VGA256GetPixel(void *pVideo, unsigned int x, unsigned int y)
{
return(VGA256OFFSET(pVideo, x, y));
}
/*------------------------------------------------------------------------------------------------------- */
void VGA256LineH(void *pVideo, unsigned int x, unsigned int y, unsigned int width, unsigned int color)
{
memset(&VGA256OFFSET(pVideo, x, y), color, width);
}
/*------------------------------------------------------------------------------------------------------- */
void VGA256LineV(void *pVideo, unsigned int x, unsigned int y, unsigned int height, unsigned int color)
{
unsigned int h;
unsigned char *offset = (unsigned char *) &VGA256OFFSET(pVideo, x, y);
for (h = 0; h < height; h++)
{
*offset = color;
offset += VGA256_WIDTH;
}
}
/*------------------------------------------------------------------------------------------------------- */
void VGA256DrawBox(void *pVideo, unsigned int x, unsigned int y, unsigned int width, unsigned int height, unsigned int color)
{
VGA256LineV(pVideo, x, y, width, color);
VGA256LineV(pVideo, x + width, y, height, color);
VGA256LineH(pVideo, x, y, width + 1, color);
VGA256LineH(pVideo, x, y + height, width + 1, color);
}
/*------------------------------------------------------------------------------------------------------- */
void VGA256FillBox(void *pVideo, unsigned int x, unsigned int y, unsigned int width, unsigned int height, unsigned int color)
{
unsigned int h;
for(h = 0; h < height; h++)
{
VGA256LineH(pVideo, x, y+h, width, color);
}
}
/*------------------------------------------------------------------------------------------------------- */
void VGA256ClearScreen(void *pVideo, unsigned int color)
{
memset(&VGA256OFFSET(pVideo, 0, 0), color, VGA256_WIDTH * VGA256_HEIGHT);
}
/*------------------------------------------------------------------------------------------------------- */
void VGA256PutSprite(void *pVideo, void *pcSource, unsigned int piX, unsigned int piY, unsigned int piWidth, unsigned int piHeight)
{
unsigned int h;
unsigned char *offset = (unsigned char *) &VGA256OFFSET(pVideo, piX, piY);
for(h = 0; h < piHeight; h++)
{
_VGA256MemCpy0((void *) offset, (void *) pcSource, (size_t) piWidth);
offset += VGA256_WIDTH;
(unsigned char *) pcSource += piWidth;
}
}
/*------------------------------------------------------------------------------------------------------- */
void VGA256PutImage(void* pVideo, void* pcSource, unsigned int piX, unsigned int piY, unsigned int piWidth, unsigned int piHeight)
{
unsigned int h;
unsigned char* offset = (unsigned char*)&VGA256OFFSET(pVideo, piX, piY);
for (h = 0; h < piHeight; h++)
{
memcpy((void*) offset, (void*) pcSource, (size_t) piWidth);
offset += VGA256_WIDTH;
(unsigned char*)pcSource += piWidth;
}
}
/*------------------------------------------------------------------------------------------------------- */
void VGA256GetImage(void* pVideo, void* pcSource, unsigned int piX, unsigned int piY, unsigned int piWidth, unsigned int piHeight)
{
unsigned int h;
unsigned char* offset = (unsigned char*)&VGA256OFFSET(pVideo, piX, piY);
for (h = 0; h < piHeight; h++)
{
memcpy((void*) pcSource, (void*) offset, (size_t) piWidth);
offset += VGA256_WIDTH;
(unsigned char*) pcSource += piWidth;
}
}
/*------------------------------------------------------------------------------------------------------- */
void VGA256ScaleImage(unsigned char* pDest, unsigned char* pSource, unsigned int widthd, unsigned int heightd, unsigned int widths, unsigned int heights)
{
unsigned int x, y;
unsigned int ywidthd, yheightsheightdy;
for (y = 0; y < heightd; y++)
{
ywidthd = y * widthd;
yheightsheightdy = y * heights / heightd;
for (x = 0; x < widthd; x++)
{
//pDest[y * widthd + x] = pSource[(y * heights / heightd) * widths + (x * widths / widthd)];
pDest[ywidthd + x] = pSource[(yheightsheightdy) * widths + (x * widths / widthd)];
}
}
}
void VGA256ScaleImageKO2(unsigned char* pDest, unsigned char* pSource, unsigned int widthd, unsigned int heightd, unsigned int widths, unsigned int heights)
{
unsigned int h, w;
unsigned char pixel;
unsigned int runw, runh;
unsigned int carryw, carryh;
div_t widthr, heightr;
heightr = div(heightd, heights);
widthr = div(widthd, widths);
runh = 0;
carryh = 0;
for (h = 0; h < heights; h++)
{
runh += heightr.quot;
carryh += heightr.rem;
if (carryh >= heights)
{
carryh = 0;
runh++;
}
if (runh > 0)
{
runw = 0;
carryw = 0;
for (w = 0; w < widths; w++)
{
runw += widthr.quot;
carryw += widthr.rem;
if (carryw >= widths)
{
carryw = 0;
runw++;
}
pixel = *pSource;
pSource++;
while (runw > 0)
{
*pDest = pixel;
pDest++;
runw--;
}
}
runh--;
while (runh > 0)
{
memcpy(pDest, pDest - widthd, widthd);
pDest += widthd;
runh--;
}
}
else
{
pSource += widths;
}
}
}
/*------------------------------------------------------------------------------------------------------- */
void VGA256ScaleImageKO(unsigned char* pDest, unsigned char* pSource, unsigned int widthd, unsigned int heightd, unsigned int widths, unsigned int heights)
{
unsigned int widthrun, widthtarget;
unsigned int heightrun, heighttarget;
unsigned int h, w;
unsigned char pixel;
heightrun = 0;
heighttarget = 0;
for (h = 0; h < heights; h++)
{
widthrun = 0;
widthtarget = 0;
for (w = 0; w < widths; w++)
{
widthtarget += widthd;
pixel = *pSource;
pSource++;
while (widthrun < widthtarget)
{
*pDest = pixel;
pDest++;
widthrun += widths;
}
}
heighttarget += heightd;
while (heightrun < heighttarget)
{
memcpy(pDest, pDest - widthd, widthd);
pDest += widthd;
heightrun += heights;
}
}
}
/*------------------------------------------------------------------------------------------------------- */
void VGA256ScaleImage05x(unsigned char* pDest, unsigned char* pSource, unsigned int widths, unsigned int heights)
{
unsigned int h, w;
unsigned char c;
for (h = 0; h < heights; h += 2)
{
for (w = 0; w < widths; w += 2)
{
*pDest = *pSource;
pSource += 2;
pDest++;
}
pSource += widths;
}
}
/*------------------------------------------------------------------------------------------------------- */
void VGA256ScaleImage025x(unsigned char* pDest, unsigned char* pSource, unsigned int widths, unsigned int heights)
{
unsigned int h, w;
unsigned char c;
for (h = 0; h < heights; h += 4)
{
for (w = 0; w < widths; w += 4)
{
*pDest = *pSource;
pSource += 4;
pDest++;
}
pSource += widths * 3;
}
}
/*------------------------------------------------------------------------------------------------------- */
void VGA256ScaleImage2x(unsigned char* pDest, unsigned char* pSource, unsigned int widths, unsigned int heights)
{
unsigned int h, w;
unsigned int widthd = widths << 1;
unsigned short* d;
unsigned char c;
d = (unsigned short*)pDest;
for (h = 0; h < heights; h++)
{
for (w = 0; w < widths; w++)
{
c = *pSource;
pSource++;
*d = (c << 8) | c;
d++;
}
memcpy(d, d - widths, widthd);
d += widths;
}
}
/*------------------------------------------------------------------------------------------------------- */
void VGA256ScaleImage4x(unsigned char* pDest, unsigned char* pSource, unsigned int widths, unsigned int heights)
{
unsigned int h, w;
unsigned int widthd = widths << 2;
unsigned int* d;
unsigned char c;
d = (unsigned int*)pDest;
for (h = 0; h < heights; h++)
{
for (w = 0; w < widths; w++)
{
c = *pSource;
pSource++;
*d = (c << 24) | (c << 16) | (c << 8) | c;
d++;
}
memcpy(d, d - widths, widthd);
d += widths;
memcpy(d, d - widths, widthd);
d += widths;
memcpy(d, d - widths, widthd);
d += widths;
}
}
/*------------------------------------------------------------------------------------------------------- */
void VGA256RotateImage(unsigned char* pDest, unsigned char* pSource, unsigned int width, unsigned int height, int angle)
{
unsigned int hwidth = width >> 1;
unsigned int hheight = height >> 1;
int sinma = VGA256SinDeg[angle];
int cosma = VGA256CosDeg[angle];
int ys, ys_cosma, ys_sinma;
int xt, xs;
unsigned int x, y;
for (y = 0; y < height; y++)
{
ys = y - hheight;
ys_cosma = ys * cosma;
ys_sinma = ys * sinma;
for (x = 0; x < width; x++)
{
xt = x - hwidth;
xs = (cosma * xt - ys_sinma) >> 14;
xs += hwidth;
ys = (ys_cosma + sinma * xt) >> 14;
ys += hheight;
if ((unsigned int)xs < width && (unsigned int)ys < height)
{
pDest[y * width + x] = pSource[ys * width + xs];
}
else
{
pDest[y * width + x] = 0;
}
}
}
}
/*------------------------------------------------------------------------------------------------------- */
void VGA256RotateImageOK(unsigned char* pDest, unsigned char* pSource, unsigned int width, unsigned int height, int angle)
{
unsigned int hwidth = width >> 1;
unsigned int hheight = height >> 1;
int sinma = VGA256SinDeg[angle];
int cosma = VGA256CosDeg[angle];
int xt, yt, xs, ys;
unsigned int x, y;
for (x = 0; x < width; x++)
{
xt = x - hwidth;
for (y = 0; y < height; y++)
{
yt = y - hheight;
xs = (cosma * xt - sinma * yt) >> 14;
xs += hwidth;
ys = (sinma * xt + cosma * yt) >> 14;
ys += hheight;
if (xs >= 0 && xs < (unsigned int) width && ys >= 0 && ys < (unsigned int) height)
{
pDest[y*width+x]=pSource[ys*width+xs];