22 // New for VGA Library.
30 // Global Variables -----------------------------------------------------------
32 ubyte *vga_screen_addr = NULL;
34 unsigned char * vga_video_memory = (unsigned char *)0xA0000;
36 char vga_pal_default[768];
38 int vga_installed = 0;
40 //extern int gr_palette_realized;
41 int vga_prevent_palette_loading = 0;
43 ubyte * pVideoMode = (volatile ubyte *)0x449;
44 uword * pNumColumns = (volatile uword *)0x44a;
45 ubyte * pNumRows = (volatile ubyte *)0x484;
46 uword * pCharHeight = (volatile uword *)0x485;
47 uword * pCursorPos = (volatile uword *)0x450;
48 uword * pCursorType = (volatile uword *)0x460;
49 uword * pTextMemory = (volatile uword *)0xb8000;
51 ubyte saved_video_mode = 0;
52 uword saved_num_columns = 0;
53 ubyte saved_num_rows = 0;
54 uword saved_char_height = 0;
55 uword saved_cursor_pos = 0;
56 uword saved_cursor_type = 0;
57 ubyte saved_is_graphics = 0;
58 uword * saved_video_memory = NULL;
60 int cga_show_screen_info = 0;
63 // VGA Functions --------------------------------------------------------------
65 ubyte vga_get_screen_mode( void )
70 int386( 0x10, ®s, ®s );
71 return regs.w.ax & 0xff;
74 void vga_set_cellheight( ubyte height )
87 outpw( 0x3c4, 0xE04 ); // enable chain4 mode
88 outpw( 0x3d4, 0x4014 ); // turn on dword mode
89 outpw( 0x3d4, 0xa317 ); // turn off byte mode
94 outpw( 0x3ce, 0x4005 ); // set Shift reg to 1
96 inp( 0x3da ); // dummy input
99 outp( 0x3c0, 0x61 ); // turns on PCS & PCC
101 inp( 0x3da ); // dummy input
107 void vga_turn_screen_off()
114 void vga_turn_screen_on()
121 void vga_set_misc_mode( uword mode )
126 int386( 0x10, ®s, ®s );
130 void gr_set_3dbios_mode( uint mode )
133 memset( ®s, 0, sizeof(regs) );
135 regs.w.bx = 0x3d00 | (mode & 0xff);
136 int386( 0x10, ®s, ®s );
140 void vga_set_text_25()
145 int386( 0x10, ®s, ®s );
149 void vga_set_text_43()
155 int386( 0x10, ®s, ®s );
158 int386( 0x10, ®s, ®s );
162 int386( 0x10, ®s, ®s );
165 void vga_set_text_50()
171 int386( 0x10, ®s, ®s );
174 int386( 0x10, ®s, ®s );
178 int386( 0x10, ®s, ®s );
181 ubyte is_graphics_mode()
184 tmp = inp( 0x3DA ); // Reset flip-flip
185 outp( 0x3C0, 0x30 ); // Select attr register 10
186 tmp = inp( 0x3C1 ); // Get graphics/text bit
194 saved_video_mode = vga_get_screen_mode();
195 saved_num_columns = *pNumColumns;
196 saved_num_rows = *pNumRows+1;
197 saved_char_height = *pCharHeight;
198 saved_cursor_pos = *pCursorPos;
199 saved_cursor_type = *pCursorType;
200 saved_is_graphics = is_graphics_mode();
202 if (saved_is_graphics==0)
204 MALLOC(saved_video_memory,uword, saved_num_columns*saved_num_rows );
206 for (i=0; i < saved_num_columns*saved_num_rows; i++ )
207 saved_video_memory[i] = pTextMemory[i];
210 if (cga_show_screen_info )
212 printf( "Current video mode 0x%x:\n", saved_video_mode );
213 if (saved_is_graphics)
214 printf( "Graphics mode\n" );
216 printf( "Text mode\n" );
218 printf( "( %d columns by %d rows)\n", saved_num_columns, saved_num_rows );
219 printf( "Char height is %d pixel rows\n", saved_char_height );
220 printf( "Cursor of type 0x%x is at (%d, %d)\n", saved_cursor_type, saved_cursor_pos & 0xFF, saved_cursor_pos >> 8 );
231 int386( 0x10, ®s, ®s );
233 if ( regs.h.al != 0x1a ) return 0;
234 if ( (regs.h.bl==7) || (regs.h.bl==8)) return 1;
238 void vga_set_cursor_type( uword ctype )
244 int386( 0x10, ®s, ®s );
247 void vga_enable_default_palette_loading()
252 int386( 0x10, ®s, ®s );
253 vga_prevent_palette_loading = 0;
256 void vga_disable_default_palette_loading()
261 int386( 0x10, ®s, ®s );
262 vga_prevent_palette_loading = 1;
265 void vga_set_cursor_position( uword position )
271 regs.w.dx = position;
272 int386( 0x10, ®s, ®s );
276 void vga_restore_mode()
280 if ( saved_video_mode == 3 )
282 switch( saved_num_rows )
284 case 43: vga_set_text_43(); break;
285 case 50: vga_set_text_50(); break;
286 default: vga_set_text_25(); break;
289 vga_set_misc_mode(saved_video_mode);
292 if (saved_is_graphics==0)
294 for (i=0; i < saved_num_columns*saved_num_rows; i++ )
295 pTextMemory[i]=saved_video_memory[i];
296 vga_set_cursor_type( saved_cursor_type );
297 vga_set_cursor_position( saved_cursor_pos );
304 if (vga_installed==1)
308 if( saved_video_memory )
309 free(saved_video_memory);
315 int LinearSVGABuffer=0;
317 int vga_vesa_setmode( short mode )
321 if (VesaInit (mode)) // if there is an error with linear support
328 retcode=gr_vesa_checkmode( mode ); // do the old banking way
329 if ( retcode ) return retcode;
330 return gr_vesa_setmodea( mode );
333 int VGA_current_mode = SM_ORIGINAL;
337 extern int Virge_enabled;
340 short vga_set_mode(short mode)
343 unsigned int w,h,t,data, r;
360 if (!isvga()) return 1;
361 vga_set_misc_mode(0x13);
362 w = 320; r = 320; h = 200; t=BM_LINEAR; data = 0xA0000;
366 retcode = vga_vesa_setmode( 0x100 );
367 //gr_enable_default_palette_loading();
368 if (retcode !=0 ) return retcode;
369 w = 640; r = 640; h = 400; t=BM_SVGA; data = 0;
373 retcode = vga_vesa_setmode( 0x101 );
374 //gr_enable_default_palette_loading();
375 if (retcode !=0 ) return retcode;
376 w = 640; r = 640; h = 480; t=BM_SVGA; data = 0;
380 retcode = vga_vesa_setmode( 0x103 );
381 //gr_enable_default_palette_loading();
382 if (retcode !=0 ) return retcode;
383 w = 800; r = 800; h = 600; t=BM_SVGA; data = 0;
387 retcode = vga_vesa_setmode( 0x105 );
388 //gr_enable_default_palette_loading();
389 if (retcode !=0 ) return retcode;
390 w = 1024; r = 1024; h = 768; t=BM_SVGA; data = 0;
394 retcode = vga_vesa_setmode( 0x107 );
395 //gr_enable_default_palette_loading();
396 if (retcode !=0 ) return retcode;
397 w = 1280; r = 1280; h = 1024; t=BM_SVGA; data = 0;
401 retcode = vga_vesa_setmode( 0x110 );
402 //gr_enable_default_palette_loading();
403 if (retcode !=0 ) return retcode;
404 w = 640; r = 640*2; h=480; t=BM_SVGA15; data = 0;
408 retcode = vga_vesa_setmode( 0x113 );
409 //gr_enable_default_palette_loading();
410 if (retcode !=0 ) return retcode;
411 w = 800; r = 800*2; h=600; t=BM_SVGA15; data = 0;
414 //super-special code for 3dmax high-res mode
415 case SM_320x400_3DMAX: // 3dmax 320x400
416 if (!isvga()) return 1;
417 gr_set_3dbios_mode(0x31);
418 //w = 320; r = 320/4; h = 400; t=BM_MODEX; data = 0;
419 w = 320; r = 320; h = 400; t=BM_SVGA; data = 0;
423 //@@ if (!isvga()) return 1;
424 //@@ vga_set_misc_mode(0x13);
425 //@@ vga_set_cellheight( 3 );
426 //@@ w = 320; r = 320; h = 200; t=BM_LINEAR; data = 0xA0000;
430 //@@ retcode = vga_vesa_setmode( 0x102 );
431 //@@ //gr_enable_default_palette_loading();
432 //@@ if (retcode !=0 ) return retcode;
433 //@@ vga_16_to_256();
434 //@@ vga_set_linear();
435 //@@ //gr_set_cellheight( 1 );
436 //@@ gr_vesa_setlogical( 400 );
437 //@@ w = 400; r = 400; h = 600; t=BM_SVGA; data = 0;
441 //@@ if (!isvga()) return 1;
442 //@@ vga_set_misc_mode(0xd);
443 //@@ vga_16_to_256();
444 //@@ vga_set_linear();
445 //@@ //gr_set_cellheight( 3 );
446 //@@ w = 160; r = 160; h = 200; t=BM_LINEAR; data = 0xA0000;
460 case SM_376x564U: //mode X modes
462 if (!isvga()) return 1;
463 w = gr_modex_setmode( mode );
464 //gr_enable_default_palette_loading();
465 h = w & 0xffff; w = w >> 16; r = w / 4;t = BM_MODEX; data = 0;
468 default: //unknown mode!!! Very bad!!
469 Error("Unknown mode %d in vga_set_mode()",mode);
472 // if (vga_palette_realized && (vga_prevent_palette_loading==0))
473 // gr_pal_setblock( 0, 256, gr_palette);
477 VGA_current_mode = mode;
478 if (LinearSVGABuffer)
480 mprintf ((0,"GREEEEAT!\n"));
482 vga_screen_addr=(ubyte *)VesaGetPtr();
485 vga_screen_addr=(ubyte *)0xa0000;
488 return gr_init_screen( t,w,h,0,0,r,vga_screen_addr);
494 // Only do this function once!
495 if (vga_installed==1)
501 vga_screen_addr = (ubyte *)0xa0000; //address in memory of screen
503 // Save the current text screen mode
504 if (vga_save_mode()==1)
507 /* if (get_selector( &vga_fade_table, 256*GR_FADE_LEVELS, &vga_fade_table_selector ))
508 Error( "Error allocating fade table selector!" );
510 if (get_selector( &vga_palette, 256*3, &vga_palette_selector ))
511 Error( "Error allocating palette selector!" );
513 if (get_selector( &vga_inverse_table, 32*32*32, &vga_inverse_table_selector ))
514 Error( "Error allocating inverse table selector!" );
517 // Set flags indicating that this is installed.
525 short vga_mode13_checkmode()
534 // 1=No VGA adapter installed
535 // 2=Program doesn't support this VESA granularity
536 // 3=Monitor doesn't support that VESA mode.:
537 // 4=Video card doesn't support that VESA mode.
538 // 5=No VESA driver found.
539 // 6=Bad Status after VESA call/
540 // 7=Not enough DOS memory to call VESA functions.
541 // 8=Error using DPMI.
542 // 9=Error setting logical line width.
543 // 10=Error allocating selector for A0000h
544 // 11=Not a valid mode support by gr.lib
546 short vga_check_mode(short mode)
563 case SM_376x564U: return vga_mode13_checkmode();
564 case SM_640x400V: return gr_vesa_checkmode( 0x100 );
565 case SM_640x480V: return gr_vesa_checkmode( 0x101 );
566 case SM_800x600V: return gr_vesa_checkmode( 0x103 );
567 case SM_1024x768V: return vga_vesa_setmode( 0x105 );
568 case SM_640x480V15: return vga_vesa_setmode( 0x110 );
569 case SM_800x600V15: return vga_vesa_setmode( 0x113 );