3 Copyright (c) 1998-2001 Klaus P. Gerlicher
11 VGA HW dependent draw routines
20 Reactos Port by Eugene Ingerman
25 15-Nov-2000: general cleanup of source files
29 This file may be distributed under the terms of the GNU Public License.
33 ////////////////////////////////////////////////////
40 //#include <linux/ctype.h>
43 ////////////////////////////////////////////////////
46 extern void pice_save_current_registers(void);
47 extern void pice_restore_current_registers(void);
48 extern void pice_set_mode_3_80x50(void);
49 extern void pice_set_mode_3_80x25(void);
51 extern UCHAR cGraphTable
[8*256];
53 // storage for original VGA font
54 UCHAR cGraphTable2
[16*256];
56 ////////////////////////////////////////////////////
59 #define VGA_EXTENDED // define this for 80x50 console mode
62 #define SCREEN_BUFFER_SIZE (80*25*2)
64 #define SCREEN_BUFFER_SIZE (80*50*2)
67 /* Port addresses of control regs */
68 #define MISCOUTPUT 0x3c2
69 #define FEATURECONTROL 0x3da
70 #define SEQUENCER 0x3c4
72 #define GRAPHICS 0x3ce
73 #define ATTRIBS 0x03c0
74 #define PELADDRESSWRITE 0x3c8
75 #define PELDATAREG 0x3c9
77 /* Number of regs on the various controllers */
82 #define MAXATTRIB 0x015
84 ////////////////////////////////////////////////////
87 // used for HERCULES text and VGA text mode
103 #endif // VGA_EXTENDED
105 PUCHAR pScreenBufferVga
;
106 PUCHAR pScreenBufferSaveVga
= NULL
;
107 PUCHAR pScreenBufferTempVga
;
108 PUCHAR pScreenBufferHardwareVga
;
109 PUCHAR pFontBufferVga
= NULL
;
112 UCHAR offset_c
= 0,offset_d
= 0;
113 UCHAR offset_e
= 0,offset_f
= 0;
130 unsigned char oldgraphicsmode
;
131 unsigned char oldgraphicsmisc
;
132 unsigned char oldsqregmapmask
;
133 unsigned char oldsqregmemory
;
134 unsigned char oldgraphicssetresetenable
;
135 unsigned char oldgraphicsreadmapsel
;
137 unsigned char read_vga_reg(int port
, int reg
)
140 return(inportb(port
+1));
143 void write_vga_reg(int port
, unsigned char reg
, unsigned char value
)
146 outportb(port
+1,value
);
149 /* Registers within controllers */
151 #define GRREGSETRESET 0
152 #define GRREGENABLESETRESET 1
153 #define GRREGREADMAPSEL 4
154 #define SQREGMAPMASK 2
155 #define SQREGMEMORY 4
156 #define GRREGWRMODE 5
159 void map_font_memory(void)
161 oldgraphicssetresetenable
= read_vga_reg(GRAPHICS
, GRREGENABLESETRESET
);
162 oldgraphicsmode
= read_vga_reg(GRAPHICS
, GRREGWRMODE
);
163 oldgraphicsmisc
= read_vga_reg(GRAPHICS
, GRREGMISC
);
164 oldgraphicsreadmapsel
= read_vga_reg(GRAPHICS
, GRREGREADMAPSEL
);
165 oldsqregmapmask
= read_vga_reg(SEQUENCER
, SQREGMAPMASK
);
166 oldsqregmemory
= read_vga_reg(SEQUENCER
, SQREGMEMORY
);
169 /* Make sure set/reset enable is off */
170 write_vga_reg(GRAPHICS
,GRREGENABLESETRESET
,0);
171 /* Select read plane 2 */
172 write_vga_reg(GRAPHICS
,GRREGREADMAPSEL
,0x02);
173 /* Make sure write and read mode = 0 */
174 write_vga_reg(GRAPHICS
,GRREGWRMODE
,0x00);
175 /* Set mapping to 64K at a000:0 & turn off odd/even at the graphics reg */
176 write_vga_reg(GRAPHICS
,GRREGMISC
, 0x04);
177 /* Set sequencer plane to 2 */
178 write_vga_reg(SEQUENCER
,SQREGMAPMASK
, 0x04);
179 /* Turn off odd/even at the sequencer */
180 write_vga_reg(SEQUENCER
,SQREGMEMORY
, 0x07);
183 void unmap_font_memory(void)
185 write_vga_reg(GRAPHICS
,GRREGENABLESETRESET
,oldgraphicssetresetenable
);
186 write_vga_reg(GRAPHICS
,GRREGWRMODE
,oldgraphicsmode
);
187 write_vga_reg(GRAPHICS
,GRREGREADMAPSEL
,oldgraphicsreadmapsel
);
188 write_vga_reg(GRAPHICS
,GRREGMISC
, oldgraphicsmisc
);
189 write_vga_reg(SEQUENCER
,SQREGMAPMASK
, oldsqregmapmask
);
190 write_vga_reg(SEQUENCER
,SQREGMEMORY
, oldsqregmemory
);
193 /* Font and palette constants */
194 #define BYTESPERFONT 8
195 #define FONTENTRIES 256
196 #define FONTBUFFERSIZE 8192
198 void save_font(UCHAR
* graph_table
)
200 PUCHAR FontBase
= pFontBufferVga
;
204 for (i
=0; i
< FONTENTRIES
; i
++)
205 for (j
=0; j
< 16; j
++)
206 graph_table
[i
*16+j
] = FontBase
[i
*32+j
];
211 void load_font(UCHAR
* graph_table
,int bEnter
)
213 PUCHAR FontBase
= pFontBufferVga
;
220 for (i
=0; i
< FONTENTRIES
; i
++)
221 for (j
=0; j
< 8; j
++)
222 FontBase
[i
*32+j
] = graph_table
[i
*BYTESPERFONT
+j
];
223 #else // VGA_EXTENDED
224 for (i
=0; i
< FONTENTRIES
; i
++)
225 for (j
=0; j
< 16; j
++)
226 FontBase
[i
*32+j
] = graph_table
[i
*BYTESPERFONT
+(j
/2)] << (j
&1);
227 #endif // VGA_EXTENDED
231 for (i
=0; i
< FONTENTRIES
; i
++)
232 for (j
=0; j
< 16; j
++)
233 FontBase
[i
*32+j
] = graph_table
[i
*16+j
];
239 //*************************************************************************
240 // SetForegroundColorVga()
242 //*************************************************************************
243 void SetForegroundColorVga(ECOLORS col
)
245 attr
.u
.bits
.fgcol
= col
;
246 attr
.u
.bits
.blink
= 0;
249 //*************************************************************************
250 // SetBackgroundColorVga()
252 //*************************************************************************
253 void SetBackgroundColorVga(ECOLORS col
)
255 attr
.u
.bits
.bkcol
= col
;
256 attr
.u
.bits
.blink
= 0;
259 //*************************************************************************
262 //*************************************************************************
263 void PrintGrafVga(ULONG x
,ULONG y
,UCHAR c
)
265 ((PUSHORT
)pScreenBufferVga
)[y
*GLOBAL_SCREEN_WIDTH
+ x
] = (USHORT
)((attr
.u
.Asuchar
<<8)|c
);
268 //*************************************************************************
271 // show hardware cursor
272 //*************************************************************************
273 void ShowCursorVga(void)
280 outb_p(inb_p(0x3d5)&~0x20,0x3d5);
285 //*************************************************************************
288 // hide hardware cursor
289 //*************************************************************************
290 void HideCursorVga(void)
293 bCursorEnabled
=FALSE
;
296 outb_p(inb_p(0x3d5)|0x20,0x3d5);
301 //*************************************************************************
304 // copy a line from src to dest
305 //*************************************************************************
306 void CopyLineToVga(USHORT dest
,USHORT src
)
308 PUSHORT p
= (PUSHORT
)pScreenBufferVga
;
312 PICE_memcpy(&p
[dest
*GLOBAL_SCREEN_WIDTH
],&p
[src
*GLOBAL_SCREEN_WIDTH
],GLOBAL_SCREEN_WIDTH
*sizeof(USHORT
));
317 //*************************************************************************
320 // invert a line on the screen
321 //*************************************************************************
322 void InvertLineVga(ULONG line
)
325 PUSHORT p
= (PUSHORT
)pScreenBufferVga
;
328 if(line
< GLOBAL_SCREEN_HEIGHT
)
330 attr
= p
[line
*GLOBAL_SCREEN_WIDTH
]>>8;
331 attr
= ((attr
& 0x07)<<4) | ((attr
& 0xF0)>>4);
333 for(i
=0;i
<GLOBAL_SCREEN_WIDTH
;i
++)
334 p
[line
*GLOBAL_SCREEN_WIDTH
+ i
] = (p
[line
*GLOBAL_SCREEN_WIDTH
+ i
] & 0x00FF) | attr
;
338 //*************************************************************************
341 // hatches a line on the screen
342 //*************************************************************************
343 void HatchLineVga(ULONG line
)
346 PUSHORT p
= (PUSHORT
)pScreenBufferVga
;
348 if(line
< GLOBAL_SCREEN_HEIGHT
)
350 for(i
=0;i
<GLOBAL_SCREEN_WIDTH
;i
++)
351 p
[line
*GLOBAL_SCREEN_WIDTH
+ i
] = (p
[line
*GLOBAL_SCREEN_WIDTH
+ i
] & 0xF0FF) | 0x0c00;
355 //*************************************************************************
358 // clear a line on the screen
359 //*************************************************************************
360 void ClrLineVga(ULONG line
)
363 PUSHORT p
= (PUSHORT
)pScreenBufferVga
;
365 if(line
< GLOBAL_SCREEN_HEIGHT
)
367 for(i
=0;i
<GLOBAL_SCREEN_WIDTH
;i
++)
368 p
[line
*GLOBAL_SCREEN_WIDTH
+ i
] = (USHORT
)((attr
.u
.Asuchar
<<8) | 0x20);
372 //*************************************************************************
375 //*************************************************************************
376 void PrintLogoVga(BOOLEAN bShow
)
381 //*************************************************************************
384 // emulate a blinking cursor block
385 //*************************************************************************
386 void PrintCursorVga(BOOLEAN bForce
)
388 static ULONG count
=0;
391 ULONG x
=wWindow
[OUTPUT_WINDOW
].usCurX
,y
=wWindow
[OUTPUT_WINDOW
].y
+wWindow
[OUTPUT_WINDOW
].usCurY
;
397 charoffset
= (y
* GLOBAL_SCREEN_WIDTH
+ x
);
400 data
=(UCHAR
)((charoffset
>>8)&0xFF);
404 data
=(UCHAR
)(charoffset
& 0xFF);
409 //*************************************************************************
412 //*************************************************************************
413 void SaveGraphicsStateVga(void)
418 pice_save_current_registers();
420 // unprotect crtc regs 0-7
423 outb_p(data
& 0x7F,0x3d5);
426 save_font(cGraphTable2
);
428 // restore original regs
430 pice_set_mode_3_80x50();
432 pice_set_mode_3_80x25();
436 load_font(cGraphTable
,1);
438 // copy the screen content to temp area
439 PICE_memcpy(pScreenBufferTempVga
,pScreenBufferHardwareVga
,SCREEN_BUFFER_SIZE
);
440 // copy the console to the screen
441 PICE_memcpy(pScreenBufferHardwareVga
,pScreenBufferVga
,SCREEN_BUFFER_SIZE
);
442 // save original pointer
443 pScreenBufferSaveVga
= pScreenBufferVga
;
444 // pScreenBufferVga now points to screen
445 pScreenBufferVga
= pScreenBufferHardwareVga
;
448 //*************************************************************************
449 // RestoreGraphicsStateVga()
451 //*************************************************************************
452 void RestoreGraphicsStateVga(void)
456 // unprotect crtc regs 0-7
459 outb_p(data
& 0x7F,0x3d5);
461 // restore original regs
462 pice_restore_current_registers();
465 load_font(cGraphTable2
,0);
467 pScreenBufferVga
= pScreenBufferSaveVga
;
468 // copy screen to the console
469 PICE_memcpy(pScreenBufferVga
,pScreenBufferHardwareVga
,SCREEN_BUFFER_SIZE
);
470 // copy the temp area to the screen
471 PICE_memcpy(pScreenBufferHardwareVga
,pScreenBufferTempVga
,SCREEN_BUFFER_SIZE
);
474 //*************************************************************************
477 // init terminal screen
478 //*************************************************************************
479 BOOLEAN
ConsoleInitVga(void)
481 BOOLEAN bResult
= FALSE
;
483 PHYSICAL_ADDRESS FrameBuffer
;
484 PHYSICAL_ADDRESS FontBuffer
;
489 ohandlers
.CopyLineTo
= CopyLineToVga
;
490 ohandlers
.PrintGraf
= PrintGrafVga
;
491 ohandlers
.ClrLine
= ClrLineVga
;
492 ohandlers
.InvertLine
= InvertLineVga
;
493 ohandlers
.HatchLine
= HatchLineVga
;
494 ohandlers
.PrintLogo
= PrintLogoVga
;
495 ohandlers
.PrintCursor
= PrintCursorVga
;
496 ohandlers
.SaveGraphicsState
= SaveGraphicsStateVga
;
497 ohandlers
.RestoreGraphicsState
= RestoreGraphicsStateVga
;
498 ohandlers
.ShowCursor
= ShowCursorVga
;
499 ohandlers
.HideCursor
= HideCursorVga
;
500 ohandlers
.SetForegroundColor
= SetForegroundColorVga
;
501 ohandlers
.SetBackgroundColor
= SetBackgroundColorVga
;
503 ihandlers
.GetKeyPolled
= KeyboardGetKeyPolled
;
504 ihandlers
.FlushKeyboardQueue
= KeyboardFlushKeyboardQueue
;
506 SetWindowGeometry(wWindowVga
);
508 GLOBAL_SCREEN_WIDTH
= 80;
510 GLOBAL_SCREEN_HEIGHT
= 25;
511 #else // VGA_EXTENDED
512 GLOBAL_SCREEN_HEIGHT
= 50;
513 #endif // VGA_EXTENDED
515 attr
.u
.Asuchar
= 0x07;
517 // the real framebuffer
518 FrameBuffer
.u
.LowPart
= 0xB8000;
519 pScreenBufferHardwareVga
= MmMapIoSpace(FrameBuffer
,SCREEN_BUFFER_SIZE
,MmNonCached
);
521 //The real font buffer
522 FontBuffer
.u
.LowPart
= 0xA0000;
523 pFontBufferVga
= MmMapIoSpace(FontBuffer
,FONTBUFFERSIZE
,MmNonCached
);
526 pScreenBufferVga
= PICE_malloc(SCREEN_BUFFER_SIZE
,NONPAGEDPOOL
);
528 pScreenBufferTempVga
= PICE_malloc(SCREEN_BUFFER_SIZE
,NONPAGEDPOOL
);
532 DPRINT((0,"VGA memory phys. 0x000b0000 mapped to virt. 0x%x\n",pScreenBufferVga
));
536 p
= (PUSHORT
)pScreenBufferVga
;
538 PICE_memset(pScreenBufferVga
,0x0,SCREEN_BUFFER_SIZE
);
540 DPRINT((0,"VGA memory cleared!\n"));
544 DPRINT((0,"ConsoleInitVga() SUCCESS!\n"));
552 //*************************************************************************
553 // ConsoleShutdownVga()
555 // exit terminal screen
556 //*************************************************************************
557 void ConsoleShutdownVga(void)
563 PICE_free(pScreenBufferVga
);
564 PICE_free(pScreenBufferTempVga
);
565 MmUnmapIoSpace(pScreenBufferHardwareVga
,SCREEN_BUFFER_SIZE
);
566 MmUnmapIoSpace(pFontBufferVga
,FONTBUFFERSIZE
);