2 * COPYRIGHT: GPL - See COPYING in the top level directory
3 * PROJECT: ReactOS Virtual DOS Machine
5 * PURPOSE: VGA hardware emulation
6 * PROGRAMMERS: Aleksandar Andrejevic <theflash AT sdf DOT lonestar DOT org>
9 /* INCLUDES *******************************************************************/
16 /* PRIVATE VARIABLES **********************************************************/
18 static CONST DWORD MemoryBase
[] = { 0xA0000, 0xA0000, 0xB0000, 0xB8000 };
19 static CONST DWORD MemoryLimit
[] = { 0xAFFFF, 0xAFFFF, 0xB7FFF, 0xBFFFF };
21 static BYTE VgaMemory
[VGA_NUM_BANKS
* VGA_BANK_SIZE
];
22 static BYTE VgaMiscRegister
;
23 static BYTE VgaSeqIndex
= VGA_SEQ_RESET_REG
;
24 static BYTE VgaSeqRegisters
[VGA_SEQ_MAX_REG
];
25 static BYTE VgaGcIndex
= VGA_GC_RESET_REG
;
26 static BYTE VgaGcRegisters
[VGA_GC_MAX_REG
];
27 static BYTE VgaCrtcIndex
= VGA_CRTC_HORZ_TOTAL_REG
;
28 static BYTE VgaCrtcRegisters
[VGA_CRTC_MAX_REG
];
29 static BYTE VgaAcIndex
= VGA_AC_PAL_0_REG
;
30 static BOOLEAN VgaAcLatch
= FALSE
;
31 static BYTE VgaAcRegisters
[VGA_AC_MAX_REG
];
32 static BYTE VgaDacIndex
= 0;
33 static BOOLEAN VgaDacReadWrite
= FALSE
;
34 static BYTE VgaDacRegisters
[VGA_PALETTE_SIZE
];
35 static BOOLEAN InVerticalRetrace
= FALSE
;
36 static BOOLEAN InHorizontalRetrace
= FALSE
;
37 static HANDLE TextConsoleBuffer
= NULL
;
38 static HANDLE GraphicsConsoleBuffer
= NULL
;
39 static LPVOID ConsoleFramebuffer
= NULL
;
40 static HANDLE ConsoleMutex
= NULL
;
41 static BOOLEAN NeedsUpdate
= FALSE
;
42 static BOOLEAN ModeChanged
= TRUE
;
43 static BOOLEAN CursorMoved
= FALSE
;
44 static BOOLEAN TextMode
= TRUE
;
45 static SMALL_RECT UpdateRectangle
= { 0, 0, 0, 0 };
47 /* PRIVATE FUNCTIONS **********************************************************/
49 static inline INT
VgaGetAddressSize(VOID
)
51 if (VgaCrtcRegisters
[VGA_CRTC_UNDERLINE_REG
] & VGA_CRTC_UNDERLINE_DWORD
)
53 /* Double-word addressing */
57 if (VgaCrtcRegisters
[VGA_CRTC_MODE_CONTROL_REG
] & VGA_CRTC_MODE_CONTROL_BYTE
)
67 static inline DWORD
VgaTranslateReadAddress(DWORD Address
)
69 DWORD Offset
= Address
- VgaGetVideoBaseAddress();
72 /* Check for chain-4 and odd-even mode */
73 if (VgaSeqRegisters
[VGA_SEQ_MEM_REG
] & VGA_SEQ_MEM_C4
)
75 /* The lowest two bits are the plane number */
79 else if (VgaGcRegisters
[VGA_GC_MODE_REG
] & VGA_GC_MODE_OE
)
81 /* The LSB is the plane number */
87 /* Use the read mode */
88 Plane
= VgaGcRegisters
[VGA_GC_READ_MAP_SEL_REG
] & 0x03;
91 /* Multiply the offset by the address size */
92 Offset
*= VgaGetAddressSize();
94 return Offset
+ Plane
* VGA_BANK_SIZE
;
97 static inline DWORD
VgaTranslateWriteAddress(DWORD Address
)
99 DWORD Offset
= Address
- VgaGetVideoBaseAddress();
101 /* Check for chain-4 and odd-even mode */
102 if (VgaSeqRegisters
[VGA_SEQ_MEM_REG
] & VGA_SEQ_MEM_C4
)
104 /* Shift the offset to the right by 2 */
107 else if (VgaGcRegisters
[VGA_GC_MODE_REG
] & VGA_GC_MODE_OE
)
109 /* Shift the offset to the right by 1 */
113 /* Multiply the offset by the address size */
114 Offset
*= VgaGetAddressSize();
116 /* Return the offset on plane 0 */
120 static inline VOID
VgaMarkForUpdate(SHORT Row
, SHORT Column
)
122 DPRINT("VgaMarkForUpdate: Row %d, Column %d\n", Row
, Column
);
124 /* Check if this is the first time the rectangle is updated */
127 UpdateRectangle
.Left
= UpdateRectangle
.Top
= SHRT_MAX
;
128 UpdateRectangle
.Right
= UpdateRectangle
.Bottom
= SHRT_MIN
;
131 /* Expand the rectangle to include the point */
132 UpdateRectangle
.Left
= min(UpdateRectangle
.Left
, Column
);
133 UpdateRectangle
.Right
= max(UpdateRectangle
.Right
, Column
);
134 UpdateRectangle
.Top
= min(UpdateRectangle
.Top
, Row
);
135 UpdateRectangle
.Bottom
= max(UpdateRectangle
.Bottom
, Row
);
137 /* Set the update request flag */
141 static VOID
VgaWriteSequencer(BYTE Data
)
143 ASSERT(VgaSeqIndex
< VGA_SEQ_MAX_REG
);
146 VgaSeqRegisters
[VgaSeqIndex
] = Data
;
149 static VOID
VgaWriteGc(BYTE Data
)
151 ASSERT(VgaGcIndex
< VGA_GC_MAX_REG
);
154 VgaGcRegisters
[VgaGcIndex
] = Data
;
156 /* Check the index */
159 case VGA_GC_MISC_REG
:
161 /* The GC misc register decides if it's text or graphics mode */
169 static VOID
VgaWriteCrtc(BYTE Data
)
171 ASSERT(VgaGcIndex
< VGA_CRTC_MAX_REG
);
174 VgaCrtcRegisters
[VgaCrtcIndex
] = Data
;
176 /* Check the index */
177 switch (VgaCrtcIndex
)
179 case VGA_CRTC_END_HORZ_DISP_REG
:
180 case VGA_CRTC_VERT_DISP_END_REG
:
181 case VGA_CRTC_OVERFLOW_REG
:
183 /* The video mode has changed */
189 case VGA_CRTC_CURSOR_LOC_LOW_REG
:
190 case VGA_CRTC_CURSOR_LOC_HIGH_REG
:
191 case VGA_CRTC_CURSOR_START_REG
:
192 case VGA_CRTC_CURSOR_END_REG
:
194 /* Set the cursor moved flag */
202 static VOID
VgaWriteDac(BYTE Data
)
205 VgaDacRegisters
[VgaDacIndex
++] = Data
;
206 VgaDacIndex
%= VGA_PALETTE_SIZE
;
208 // TODO: Change the palette!
211 static VOID
VgaWriteAc(BYTE Data
)
213 ASSERT(VgaAcIndex
< VGA_AC_MAX_REG
);
216 VgaAcRegisters
[VgaAcIndex
] = Data
;
219 static BOOL
VgaEnterGraphicsMode(PCOORD Resolution
)
222 CONSOLE_GRAPHICS_BUFFER_INFO GraphicsBufferInfo
;
223 BYTE BitmapInfoBuffer
[VGA_BITMAP_INFO_SIZE
];
224 LPBITMAPINFO BitmapInfo
= (LPBITMAPINFO
)BitmapInfoBuffer
;
225 LPWORD PaletteIndex
= (LPWORD
)(BitmapInfoBuffer
+ sizeof(BITMAPINFOHEADER
));
227 /* Fill the bitmap info header */
228 ZeroMemory(&BitmapInfo
->bmiHeader
, sizeof(BITMAPINFOHEADER
));
229 BitmapInfo
->bmiHeader
.biSize
= sizeof(BITMAPINFOHEADER
);
230 BitmapInfo
->bmiHeader
.biWidth
= Resolution
->X
;
231 BitmapInfo
->bmiHeader
.biHeight
= Resolution
->Y
;
232 BitmapInfo
->bmiHeader
.biBitCount
= 8;
233 BitmapInfo
->bmiHeader
.biPlanes
= 1;
234 BitmapInfo
->bmiHeader
.biCompression
= BI_RGB
;
235 BitmapInfo
->bmiHeader
.biSizeImage
= Resolution
->X
* Resolution
->Y
;
237 /* Fill the palette data */
238 for (i
= 0; i
< (VGA_PALETTE_SIZE
/ 3); i
++) PaletteIndex
[i
] = (WORD
)i
;
240 /* Fill the console graphics buffer info */
241 GraphicsBufferInfo
.dwBitMapInfoLength
= VGA_BITMAP_INFO_SIZE
;
242 GraphicsBufferInfo
.lpBitMapInfo
= BitmapInfo
;
243 GraphicsBufferInfo
.dwUsage
= DIB_PAL_COLORS
;
245 /* Create the buffer */
246 GraphicsConsoleBuffer
= CreateConsoleScreenBuffer(GENERIC_READ
| GENERIC_WRITE
,
247 FILE_SHARE_READ
| FILE_SHARE_WRITE
,
249 CONSOLE_GRAPHICS_BUFFER
,
250 &GraphicsBufferInfo
);
251 if (GraphicsConsoleBuffer
== INVALID_HANDLE_VALUE
) return FALSE
;
253 /* Save the framebuffer address and mutex */
254 ConsoleFramebuffer
= GraphicsBufferInfo
.lpBitMap
;
255 ConsoleMutex
= GraphicsBufferInfo
.hMutex
;
257 /* Clear the framebuffer */
258 ZeroMemory(ConsoleFramebuffer
, BitmapInfo
->bmiHeader
.biSizeImage
);
260 /* Set the active buffer */
261 SetConsoleActiveScreenBuffer(GraphicsConsoleBuffer
);
266 static VOID
VgaLeaveGraphicsMode(VOID
)
268 /* Switch back to the text buffer */
269 SetConsoleActiveScreenBuffer(TextConsoleBuffer
);
271 /* Cleanup the video data */
272 CloseHandle(ConsoleMutex
);
273 CloseHandle(GraphicsConsoleBuffer
);
274 GraphicsConsoleBuffer
= NULL
;
277 static BOOL
VgaEnterTextMode(PCOORD Resolution
)
279 /* Resize the console */
280 SetConsoleScreenBufferSize(TextConsoleBuffer
, *Resolution
);
282 /* Allocate a framebuffer */
283 ConsoleFramebuffer
= HeapAlloc(GetProcessHeap(),
285 Resolution
->X
* Resolution
->Y
286 * sizeof(CHAR_INFO
));
287 if (ConsoleFramebuffer
== NULL
)
289 DisplayMessage(L
"An unexpected error occurred!\n");
297 static VOID
VgaLeaveTextMode(VOID
)
299 /* Free the old framebuffer */
300 HeapFree(GetProcessHeap(), 0, ConsoleFramebuffer
);
301 ConsoleFramebuffer
= NULL
;
304 static VOID
VgaUpdateMode(VOID
)
306 COORD Resolution
= VgaGetDisplayResolution();
310 /* Leave the current graphics mode */
311 VgaLeaveGraphicsMode();
315 /* Leave the current text mode */
319 /* Check if the new mode is alphanumeric */
320 if (!(VgaGcRegisters
[VGA_GC_MISC_REG
] & VGA_GC_MISC_NOALPHA
))
322 /* Enter new text mode */
323 if (!VgaEnterTextMode(&Resolution
)) return;
325 /* Set the text mode flag */
330 /* Enter 8-bit graphics mode */
331 if (!VgaEnterGraphicsMode(&Resolution
)) return;
333 /* Clear the text mode flag */
337 /* Perform a full update */
339 UpdateRectangle
.Left
= 0;
340 UpdateRectangle
.Top
= 0;
341 UpdateRectangle
.Right
= Resolution
.X
;
342 UpdateRectangle
.Bottom
= Resolution
.Y
;
345 static VOID
VgaUpdateFramebuffer(VOID
)
348 COORD Resolution
= VgaGetDisplayResolution();
349 INT AddressSize
= VgaGetAddressSize();
350 DWORD Address
= (VgaCrtcRegisters
[VGA_CRTC_START_ADDR_HIGH_REG
] << 8)
351 + VgaCrtcRegisters
[VGA_CRTC_START_ADDR_LOW_REG
];
352 DWORD ScanlineSize
= (DWORD
)VgaCrtcRegisters
[VGA_CRTC_OFFSET_REG
] * 2;
353 PCHAR_INFO CharBuffer
= (PCHAR_INFO
)ConsoleFramebuffer
;
354 PBYTE GraphicsBuffer
= (PBYTE
)ConsoleFramebuffer
;
356 /* Loop through the scanlines */
357 for (i
= 0; i
< Resolution
.Y
; i
++)
359 /* Check if this is text mode or graphics mode */
360 if (VgaGcRegisters
[VGA_GC_MISC_REG
] & VGA_GC_MISC_NOALPHA
)
364 /* Loop through the pixels */
365 for (j
= 0; j
< Resolution
.X
; j
++)
369 /* Check the shifting mode */
370 if (VgaGcRegisters
[VGA_GC_MODE_REG
] & VGA_GC_MODE_SHIFT256
)
372 /* 4 bits shifted from each plane */
374 /* Check if this is 16 or 256 color mode */
375 if (VgaAcRegisters
[VGA_AC_CONTROL_REG
] & VGA_AC_CONTROL_8BIT
)
377 /* One byte per pixel */
378 PixelData
= VgaMemory
[(j
% VGA_NUM_BANKS
) * VGA_BANK_SIZE
379 + (Address
+ (j
/ VGA_NUM_BANKS
))
384 /* 4-bits per pixel */
386 PixelData
= VgaMemory
[(j
% VGA_NUM_BANKS
) * VGA_BANK_SIZE
387 + (Address
+ (j
/ (VGA_NUM_BANKS
* 2)))
390 /* Check if we should use the highest 4 bits or lowest 4 */
391 if (((j
/ VGA_NUM_BANKS
) % 2) == 0)
403 else if (VgaGcRegisters
[VGA_GC_MODE_REG
] & VGA_GC_MODE_SHIFTREG
)
406 * 2 bits shifted from plane 0 and 2 for the first 4 pixels,
407 * then 2 bits shifted from plane 1 and 3 for the next 4
410 // TODO: NOT IMPLEMENTED!
411 DPRINT1("Interleaved shift mode is not implemented!\n");
415 /* 1 bit shifted from each plane */
417 /* Check if this is 16 or 256 color mode */
418 if (VgaAcRegisters
[VGA_AC_CONTROL_REG
] & VGA_AC_CONTROL_8BIT
)
420 /* 8 bits per pixel, 2 on each plane */
422 for (k
= 0; k
< VGA_NUM_BANKS
; k
++)
424 /* The data is on plane k, 4 pixels per byte */
425 BYTE PlaneData
= VgaMemory
[k
* VGA_BANK_SIZE
426 + (Address
+ (j
/ 4)) * AddressSize
];
428 /* The mask of the first bit in the pair */
429 BYTE BitMask
= 1 << (((3 - (j
% 4)) * 2) + 1);
431 /* Bits 0, 1, 2 and 3 come from the first bit of the pair */
432 if (PlaneData
& BitMask
) PixelData
|= 1 << k
;
434 /* Bits 4, 5, 6 and 7 come from the second bit of the pair */
435 if (PlaneData
& (BitMask
>> 1)) PixelData
|= 1 << (k
+ 4);
440 /* 4 bits per pixel, 1 on each plane */
442 for (k
= 0; k
< VGA_NUM_BANKS
; k
++)
444 BYTE PlaneData
= VgaMemory
[k
* VGA_BANK_SIZE
445 + (Address
+ (j
/ 8)) * AddressSize
];
447 /* If the bit on that plane is set, set it */
448 if (PlaneData
& (1 << (7 - (j
% 8)))) PixelData
|= 1 << k
;
453 /* Now check if the resulting pixel data has changed */
454 if (GraphicsBuffer
[i
* Resolution
.X
+ j
] != PixelData
)
456 /* Yes, write the new value */
457 GraphicsBuffer
[i
* Resolution
.X
+ j
] = PixelData
;
459 /* Mark the specified pixel as changed */
460 VgaMarkForUpdate(i
, j
);
468 /* Loop through the characters */
469 for (j
= 0; j
< Resolution
.X
; j
++)
471 DWORD CurrentAddr
= LOWORD((Address
+ j
) * AddressSize
);
474 /* Plane 0 holds the character itself */
475 CharInfo
.Char
.AsciiChar
= VgaMemory
[CurrentAddr
];
477 /* Plane 1 holds the attribute */
478 CharInfo
.Attributes
= VgaMemory
[CurrentAddr
+ VGA_BANK_SIZE
];
480 /* Now check if the resulting character data has changed */
481 if ((CharBuffer
[i
* Resolution
.X
+ j
].Char
.AsciiChar
!= CharInfo
.Char
.AsciiChar
)
482 || (CharBuffer
[i
* Resolution
.X
+ j
].Attributes
!= CharInfo
.Attributes
))
484 /* Yes, write the new value */
485 CharBuffer
[i
* Resolution
.X
+ j
] = CharInfo
;
487 /* Mark the specified pixel as changed */
488 VgaMarkForUpdate(i
, j
);
493 /* Move to the next scanline */
494 Address
+= ScanlineSize
;
498 static VOID
VgaUpdateTextCursor(VOID
)
501 CONSOLE_CURSOR_INFO CursorInfo
;
502 BYTE CursorStart
= VgaCrtcRegisters
[VGA_CRTC_CURSOR_START_REG
] & 0x3F;
503 BYTE CursorEnd
= VgaCrtcRegisters
[VGA_CRTC_CURSOR_END_REG
] & 0x1F;
504 DWORD ScanlineSize
= (DWORD
)VgaCrtcRegisters
[VGA_CRTC_OFFSET_REG
] * 2;
505 WORD Location
= MAKEWORD(VgaCrtcRegisters
[VGA_CRTC_CURSOR_LOC_LOW_REG
],
506 VgaCrtcRegisters
[VGA_CRTC_CURSOR_LOC_HIGH_REG
]);
508 if (CursorStart
< CursorEnd
)
511 CursorInfo
.bVisible
= TRUE
;
512 CursorInfo
.dwSize
= (100 * (CursorEnd
- CursorStart
)) >> 5;
517 CursorInfo
.bVisible
= FALSE
;
518 CursorInfo
.dwSize
= 0;
521 /* Add the cursor skew to the location */
522 Location
+= (VgaCrtcRegisters
[VGA_CRTC_CURSOR_END_REG
] >> 5) & 3;
524 /* Find the coordinates of the new position */
525 Position
.X
= (WORD
)(Location
% ScanlineSize
);
526 Position
.Y
= (WORD
)(Location
/ ScanlineSize
);
528 /* Update the physical cursor */
529 SetConsoleCursorInfo(TextConsoleBuffer
, &CursorInfo
);
530 SetConsoleCursorPosition(TextConsoleBuffer
, Position
);
533 /* PUBLIC FUNCTIONS ***********************************************************/
535 DWORD
VgaGetVideoBaseAddress(VOID
)
537 return MemoryBase
[(VgaGcRegisters
[VGA_GC_MISC_REG
] >> 2) & 0x03];
540 DWORD
VgaGetVideoLimitAddress(VOID
)
542 return MemoryLimit
[(VgaGcRegisters
[VGA_GC_MISC_REG
] >> 2) & 0x03];
545 COORD
VgaGetDisplayResolution(VOID
)
549 /* The low 8 bits are in the display registers */
550 Resolution
.X
= VgaCrtcRegisters
[VGA_CRTC_END_HORZ_DISP_REG
];
551 Resolution
.Y
= VgaCrtcRegisters
[VGA_CRTC_VERT_DISP_END_REG
];
553 /* Set the top bits from the overflow register */
554 if (VgaCrtcRegisters
[VGA_CRTC_OVERFLOW_REG
] & VGA_CRTC_OVERFLOW_VDE8
)
556 Resolution
.Y
|= 1 << 8;
558 if (VgaCrtcRegisters
[VGA_CRTC_OVERFLOW_REG
] & VGA_CRTC_OVERFLOW_VDE9
)
560 Resolution
.Y
|= 1 << 9;
563 /* Increase the values by 1 */
567 if (VgaGcRegisters
[VGA_GC_MISC_REG
] & VGA_GC_MISC_NOALPHA
)
569 /* Multiply the horizontal resolution by the 9/8 dot mode */
570 Resolution
.X
*= (VgaSeqRegisters
[VGA_SEQ_CLOCK_REG
] & VGA_SEQ_CLOCK_98DM
)
573 /* The horizontal resolution is halved in 8-bit mode */
574 if (VgaAcRegisters
[VGA_AC_CONTROL_REG
] & VGA_AC_CONTROL_8BIT
) Resolution
.X
/= 2;
576 /* Divide the vertical resolution by the maximum scan line */
577 Resolution
.Y
/= ((DWORD
)VgaCrtcRegisters
[VGA_CRTC_MAX_SCAN_LINE_REG
] & 0x1F) + 1;
581 /* Divide the number of scanlines by the font size */
585 /* Return the resolution */
589 VOID
VgaRefreshDisplay(VOID
)
591 COORD Resolution
= VgaGetDisplayResolution();
593 DPRINT("VgaRefreshDisplay\n");
597 /* Change the display mode */
600 /* Reset the mode change flag */
606 /* Change the text cursor location */
607 VgaUpdateTextCursor();
609 /* Reset the cursor move flag */
613 /* Update the contents of the framebuffer */
614 VgaUpdateFramebuffer();
616 /* Set the vertical retrace flag */
617 InVerticalRetrace
= TRUE
;
619 /* Ignore if there's nothing to update */
620 if (!NeedsUpdate
) return;
622 DPRINT("Updating screen rectangle (%d, %d, %d, %d)\n",
623 UpdateRectangle
.Left
,
625 UpdateRectangle
.Right
,
626 UpdateRectangle
.Bottom
);
628 /* Check if this is text mode or graphics mode */
629 if (VgaGcRegisters
[VGA_GC_MISC_REG
] & VGA_GC_MISC_NOALPHA
)
633 /* Redraw the screen */
634 InvalidateConsoleDIBits(GraphicsConsoleBuffer
, &UpdateRectangle
);
639 COORD Origin
= { UpdateRectangle
.Left
, UpdateRectangle
.Top
};
641 /* Write the data to the console */
642 WriteConsoleOutputA(TextConsoleBuffer
,
643 (PCHAR_INFO
)ConsoleFramebuffer
,
650 /* Clear the update flag */
654 VOID
VgaHorizontalRetrace(VOID
)
657 InHorizontalRetrace
= TRUE
;
660 VOID
VgaReadMemory(DWORD Address
, LPBYTE Buffer
, DWORD Size
)
665 DPRINT("VgaReadMemory: Address 0x%08X, Size %lu\n",
669 /* Ignore if video RAM access is disabled */
670 if (!(VgaMiscRegister
& VGA_MISC_RAM_ENABLED
)) return;
672 /* Loop through each byte */
673 for (i
= 0; i
< Size
; i
++)
675 VideoAddress
= VgaTranslateReadAddress(Address
+ i
);
677 /* Copy the value to the buffer */
678 Buffer
[i
] = VgaMemory
[VideoAddress
];
682 VOID
VgaWriteMemory(DWORD Address
, LPBYTE Buffer
, DWORD Size
)
687 DPRINT("VgaWriteMemory: Address 0x%08X, Size %lu\n",
691 /* Ignore if video RAM access is disabled */
692 if (!(VgaMiscRegister
& VGA_MISC_RAM_ENABLED
)) return;
694 /* Also ignore if write access to all planes is disabled */
695 if ((VgaSeqRegisters
[VGA_SEQ_MASK_REG
] & 0x0F) == 0x00) return;
697 /* Loop through each byte */
698 for (i
= 0; i
< Size
; i
++)
700 VideoAddress
= VgaTranslateWriteAddress(Address
+ i
);
702 for (j
= 0; j
< VGA_NUM_BANKS
; j
++)
704 /* Make sure the page is writeable */
705 if (!(VgaSeqRegisters
[VGA_SEQ_MASK_REG
] & (1 << j
))) continue;
707 /* Check if this is chain-4 mode */
708 if (VgaSeqRegisters
[VGA_SEQ_MEM_REG
] & VGA_SEQ_MEM_C4
)
710 if (((Address
+ i
) & 3) != j
)
712 /* This plane will not be accessed */
717 /* Check if this is odd-even mode */
718 if (VgaGcRegisters
[VGA_GC_MODE_REG
] & VGA_GC_MODE_OE
)
720 if (((Address
+ i
) & 1) != (j
& 1))
722 /* This plane will not be accessed */
727 /* Copy the value to the VGA memory */
728 VgaMemory
[VideoAddress
+ j
* VGA_BANK_SIZE
] = Buffer
[i
];
733 BYTE
VgaReadPort(WORD Port
)
735 DPRINT("VgaReadPort: Port 0x%04X\n", Port
);
746 return VgaAcRegisters
[VgaAcIndex
];
756 return VgaSeqRegisters
[VgaSeqIndex
];
759 case VGA_DAC_READ_INDEX
:
761 /* This returns the read/write state */
762 return VgaDacReadWrite
? 0 : 3;
765 case VGA_DAC_WRITE_INDEX
:
772 /* Ignore reads in write mode */
773 if (!VgaDacReadWrite
)
775 BYTE Data
= VgaDacRegisters
[VgaDacIndex
++];
776 VgaDacIndex
%= VGA_PALETTE_SIZE
;
785 return VgaMiscRegister
;
795 return VgaCrtcRegisters
[VgaCrtcIndex
];
805 return VgaGcRegisters
[VgaGcIndex
];
813 /* Reset the AC latch */
816 /* Set a flag if there is a vertical or horizontal retrace */
817 if (InVerticalRetrace
|| InHorizontalRetrace
) Result
|= VGA_STAT_DD
;
819 /* Set an additional flag if there was a vertical retrace */
820 if (InVerticalRetrace
) Result
|= VGA_STAT_VRETRACE
;
822 /* Clear the flags */
823 InHorizontalRetrace
= InVerticalRetrace
= FALSE
;
832 VOID
VgaWritePort(WORD Port
, BYTE Data
)
834 DPRINT("VgaWritePort: Port 0x%04X, Data 0x%02X\n", Port
, Data
);
842 /* Change the index */
843 if (Data
< VGA_AC_MAX_REG
) VgaAcIndex
= Data
;
851 /* Toggle the latch */
852 VgaAcLatch
= !VgaAcLatch
;
859 /* Set the sequencer index register */
860 if (Data
< VGA_SEQ_MAX_REG
) VgaSeqIndex
= Data
;
867 /* Call the sequencer function */
868 VgaWriteSequencer(Data
);
873 case VGA_DAC_READ_INDEX
:
875 VgaDacReadWrite
= FALSE
;
876 VgaDacIndex
= Data
% VGA_PALETTE_SIZE
;
881 case VGA_DAC_WRITE_INDEX
:
883 VgaDacReadWrite
= TRUE
;
884 VgaDacIndex
= Data
% VGA_PALETTE_SIZE
;
891 /* Ignore writes in read mode */
892 if (VgaDacReadWrite
) VgaWriteDac(Data
& 0x3F);
899 VgaMiscRegister
= Data
;
906 /* Set the CRTC index register */
907 if (Data
< VGA_CRTC_MAX_REG
) VgaCrtcIndex
= Data
;
914 /* Call the CRTC function */
922 /* Set the GC index register */
923 if (Data
< VGA_GC_MAX_REG
) VgaGcIndex
= Data
;
929 /* Call the GC function */
937 VOID
VgaInitialize(HANDLE TextHandle
)
943 COORD Origin
= { 0, 0 };
944 SMALL_RECT ScreenRect
;
945 PCHAR_INFO CharBuffer
;
949 /* Set the global handle */
950 TextConsoleBuffer
= TextHandle
;
952 /* Clear the VGA memory */
953 ZeroMemory(VgaMemory
, VGA_NUM_BANKS
* VGA_BANK_SIZE
);
955 /* Set the default video mode */
956 BiosSetVideoMode(BIOS_DEFAULT_VIDEO_MODE
);
961 Resolution
= VgaGetDisplayResolution();
962 CharBuffer
= (PCHAR_INFO
)ConsoleFramebuffer
;
963 AddressSize
= VgaGetAddressSize();
964 ScreenRect
.Left
= ScreenRect
.Top
= 0;
965 ScreenRect
.Right
= Resolution
.X
;
966 ScreenRect
.Bottom
= Resolution
.Y
;
967 ScanlineSize
= (DWORD
)VgaCrtcRegisters
[VGA_CRTC_OFFSET_REG
] * 2;
969 /* Read the data from the console into the framebuffer */
970 ReadConsoleOutputA(TextConsoleBuffer
,
976 /* Loop through the scanlines */
977 for (i
= 0; i
< Resolution
.Y
; i
++)
979 /* Loop through the characters */
980 for (j
= 0; j
< Resolution
.X
; j
++)
982 CurrentAddr
= LOWORD((Address
+ j
) * AddressSize
);
984 /* Store the character in plane 0 */
985 VgaMemory
[CurrentAddr
] = CharBuffer
[i
* Resolution
.X
+ j
].Char
.AsciiChar
;
987 /* Store the attribute in plane 1 */
988 VgaMemory
[CurrentAddr
+ VGA_BANK_SIZE
] = (BYTE
)CharBuffer
[i
* Resolution
.X
+ j
].Attributes
;
991 /* Move to the next scanline */
992 Address
+= ScanlineSize
;