Sync with trunk.
[reactos.git] / subsystems / ntvdm / vga.c
1 /*
2 * COPYRIGHT: GPL - See COPYING in the top level directory
3 * PROJECT: ReactOS Virtual DOS Machine
4 * FILE: vga.c
5 * PURPOSE: VGA hardware emulation
6 * PROGRAMMERS: Aleksandar Andrejevic <theflash AT sdf DOT lonestar DOT org>
7 */
8
9 /* INCLUDES *******************************************************************/
10
11 #define NDEBUG
12
13 #include "vga.h"
14 #include "bios.h"
15
16 /* PRIVATE VARIABLES **********************************************************/
17
18 static CONST DWORD MemoryBase[] = { 0xA0000, 0xA0000, 0xB0000, 0xB8000 };
19 static CONST DWORD MemoryLimit[] = { 0xAFFFF, 0xAFFFF, 0xB7FFF, 0xBFFFF };
20
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 };
46
47 /* PRIVATE FUNCTIONS **********************************************************/
48
49 static inline INT VgaGetAddressSize(VOID)
50 {
51 if (VgaCrtcRegisters[VGA_CRTC_UNDERLINE_REG] & VGA_CRTC_UNDERLINE_DWORD)
52 {
53 /* Double-word addressing */
54 return 4;
55 }
56
57 if (VgaCrtcRegisters[VGA_CRTC_MODE_CONTROL_REG] & VGA_CRTC_MODE_CONTROL_BYTE)
58 {
59 /* Byte addressing */
60 return 1;
61 }
62
63 /* Word addressing */
64 return 2;
65 }
66
67 static inline DWORD VgaTranslateReadAddress(DWORD Address)
68 {
69 DWORD Offset = Address - VgaGetVideoBaseAddress();
70 BYTE Plane;
71
72 /* Check for chain-4 and odd-even mode */
73 if (VgaSeqRegisters[VGA_SEQ_MEM_REG] & VGA_SEQ_MEM_C4)
74 {
75 /* The lowest two bits are the plane number */
76 Plane = Offset & 3;
77 Offset >>= 2;
78 }
79 else if (VgaGcRegisters[VGA_GC_MODE_REG] & VGA_GC_MODE_OE)
80 {
81 /* The LSB is the plane number */
82 Plane = Offset & 1;
83 Offset >>= 1;
84 }
85 else
86 {
87 /* Use the read mode */
88 Plane = VgaGcRegisters[VGA_GC_READ_MAP_SEL_REG] & 0x03;
89 }
90
91 /* Multiply the offset by the address size */
92 Offset *= VgaGetAddressSize();
93
94 return Offset + Plane * VGA_BANK_SIZE;
95 }
96
97 static inline DWORD VgaTranslateWriteAddress(DWORD Address)
98 {
99 DWORD Offset = Address - VgaGetVideoBaseAddress();
100
101 /* Check for chain-4 and odd-even mode */
102 if (VgaSeqRegisters[VGA_SEQ_MEM_REG] & VGA_SEQ_MEM_C4)
103 {
104 /* Shift the offset to the right by 2 */
105 Offset >>= 2;
106 }
107 else if (VgaGcRegisters[VGA_GC_MODE_REG] & VGA_GC_MODE_OE)
108 {
109 /* Shift the offset to the right by 1 */
110 Offset >>= 1;
111 }
112
113 /* Multiply the offset by the address size */
114 Offset *= VgaGetAddressSize();
115
116 /* Return the offset on plane 0 */
117 return Offset;
118 }
119
120 static inline VOID VgaMarkForUpdate(SHORT Row, SHORT Column)
121 {
122 DPRINT("VgaMarkForUpdate: Row %d, Column %d\n", Row, Column);
123
124 /* Check if this is the first time the rectangle is updated */
125 if (!NeedsUpdate)
126 {
127 UpdateRectangle.Left = UpdateRectangle.Top = SHRT_MAX;
128 UpdateRectangle.Right = UpdateRectangle.Bottom = SHRT_MIN;
129 }
130
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);
136
137 /* Set the update request flag */
138 NeedsUpdate = TRUE;
139 }
140
141 static VOID VgaWriteSequencer(BYTE Data)
142 {
143 ASSERT(VgaSeqIndex < VGA_SEQ_MAX_REG);
144
145 /* Save the value */
146 VgaSeqRegisters[VgaSeqIndex] = Data;
147 }
148
149 static VOID VgaWriteGc(BYTE Data)
150 {
151 ASSERT(VgaGcIndex < VGA_GC_MAX_REG);
152
153 /* Save the value */
154 VgaGcRegisters[VgaGcIndex] = Data;
155
156 /* Check the index */
157 switch (VgaGcIndex)
158 {
159 case VGA_GC_MISC_REG:
160 {
161 /* The GC misc register decides if it's text or graphics mode */
162 ModeChanged = TRUE;
163
164 break;
165 }
166 }
167 }
168
169 static VOID VgaWriteCrtc(BYTE Data)
170 {
171 ASSERT(VgaGcIndex < VGA_CRTC_MAX_REG);
172
173 /* Save the value */
174 VgaCrtcRegisters[VgaCrtcIndex] = Data;
175
176 /* Check the index */
177 switch (VgaCrtcIndex)
178 {
179 case VGA_CRTC_END_HORZ_DISP_REG:
180 case VGA_CRTC_VERT_DISP_END_REG:
181 case VGA_CRTC_OVERFLOW_REG:
182 {
183 /* The video mode has changed */
184 ModeChanged = TRUE;
185
186 break;
187 }
188
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:
193 {
194 /* Set the cursor moved flag */
195 CursorMoved = TRUE;
196
197 break;
198 }
199 }
200 }
201
202 static VOID VgaWriteDac(BYTE Data)
203 {
204 /* Set the value */
205 VgaDacRegisters[VgaDacIndex++] = Data;
206 VgaDacIndex %= VGA_PALETTE_SIZE;
207
208 // TODO: Change the palette!
209 }
210
211 static VOID VgaWriteAc(BYTE Data)
212 {
213 ASSERT(VgaAcIndex < VGA_AC_MAX_REG);
214
215 /* Save the value */
216 VgaAcRegisters[VgaAcIndex] = Data;
217 }
218
219 static BOOL VgaEnterGraphicsMode(PCOORD Resolution)
220 {
221 DWORD i;
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));
226
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;
236
237 /* Fill the palette data */
238 for (i = 0; i < (VGA_PALETTE_SIZE / 3); i++) PaletteIndex[i] = (WORD)i;
239
240 /* Fill the console graphics buffer info */
241 GraphicsBufferInfo.dwBitMapInfoLength = VGA_BITMAP_INFO_SIZE;
242 GraphicsBufferInfo.lpBitMapInfo = BitmapInfo;
243 GraphicsBufferInfo.dwUsage = DIB_PAL_COLORS;
244
245 /* Create the buffer */
246 GraphicsConsoleBuffer = CreateConsoleScreenBuffer(GENERIC_READ | GENERIC_WRITE,
247 FILE_SHARE_READ | FILE_SHARE_WRITE,
248 NULL,
249 CONSOLE_GRAPHICS_BUFFER,
250 &GraphicsBufferInfo);
251 if (GraphicsConsoleBuffer == INVALID_HANDLE_VALUE) return FALSE;
252
253 /* Save the framebuffer address and mutex */
254 ConsoleFramebuffer = GraphicsBufferInfo.lpBitMap;
255 ConsoleMutex = GraphicsBufferInfo.hMutex;
256
257 /* Clear the framebuffer */
258 ZeroMemory(ConsoleFramebuffer, BitmapInfo->bmiHeader.biSizeImage);
259
260 /* Set the active buffer */
261 SetConsoleActiveScreenBuffer(GraphicsConsoleBuffer);
262
263 return TRUE;
264 }
265
266 static VOID VgaLeaveGraphicsMode(VOID)
267 {
268 /* Release the console framebuffer mutex if needed */
269 ReleaseMutex(ConsoleMutex);
270
271 /* Switch back to the text buffer */
272 SetConsoleActiveScreenBuffer(TextConsoleBuffer);
273
274 /* Cleanup the video data */
275 CloseHandle(ConsoleMutex);
276 ConsoleMutex = NULL;
277 CloseHandle(GraphicsConsoleBuffer);
278 GraphicsConsoleBuffer = NULL;
279 }
280
281 static BOOL VgaEnterTextMode(PCOORD Resolution)
282 {
283 /* Resize the console */
284 SetConsoleScreenBufferSize(TextConsoleBuffer, *Resolution);
285
286 /* Allocate a framebuffer */
287 ConsoleFramebuffer = HeapAlloc(GetProcessHeap(),
288 HEAP_ZERO_MEMORY,
289 Resolution->X * Resolution->Y
290 * sizeof(CHAR_INFO));
291 if (ConsoleFramebuffer == NULL)
292 {
293 DisplayMessage(L"An unexpected error occurred!\n");
294 VdmRunning = FALSE;
295 return FALSE;
296 }
297
298 return TRUE;
299 }
300
301 static VOID VgaLeaveTextMode(VOID)
302 {
303 /* Free the old framebuffer */
304 HeapFree(GetProcessHeap(), 0, ConsoleFramebuffer);
305 ConsoleFramebuffer = NULL;
306 }
307
308 static VOID VgaUpdateMode(VOID)
309 {
310 COORD Resolution = VgaGetDisplayResolution();
311
312 if (!TextMode)
313 {
314 /* Leave the current graphics mode */
315 VgaLeaveGraphicsMode();
316 }
317 else
318 {
319 /* Leave the current text mode */
320 VgaLeaveTextMode();
321 }
322
323 /* Check if the new mode is alphanumeric */
324 if (!(VgaGcRegisters[VGA_GC_MISC_REG] & VGA_GC_MISC_NOALPHA))
325 {
326 /* Enter new text mode */
327 if (!VgaEnterTextMode(&Resolution)) return;
328
329 /* Set the text mode flag */
330 TextMode = TRUE;
331 }
332 else
333 {
334 /* Enter 8-bit graphics mode */
335 if (!VgaEnterGraphicsMode(&Resolution)) return;
336
337 /* Clear the text mode flag */
338 TextMode = FALSE;
339 }
340
341 /* Perform a full update */
342 NeedsUpdate = TRUE;
343 UpdateRectangle.Left = 0;
344 UpdateRectangle.Top = 0;
345 UpdateRectangle.Right = Resolution.X;
346 UpdateRectangle.Bottom = Resolution.Y;
347 }
348
349 static VOID VgaUpdateFramebuffer(VOID)
350 {
351 INT i, j, k;
352 COORD Resolution = VgaGetDisplayResolution();
353 INT AddressSize = VgaGetAddressSize();
354 DWORD Address = (VgaCrtcRegisters[VGA_CRTC_START_ADDR_HIGH_REG] << 8)
355 + VgaCrtcRegisters[VGA_CRTC_START_ADDR_LOW_REG];
356 DWORD ScanlineSize = (DWORD)VgaCrtcRegisters[VGA_CRTC_OFFSET_REG] * 2;
357
358 /* Check if this is text mode or graphics mode */
359 if (VgaGcRegisters[VGA_GC_MISC_REG] & VGA_GC_MISC_NOALPHA)
360 {
361 /* Graphics mode */
362 PBYTE GraphicsBuffer = (PBYTE)ConsoleFramebuffer;
363
364 /*
365 * Synchronize access to the graphics framebuffer
366 * with the console framebuffer mutex.
367 */
368 WaitForSingleObject(ConsoleMutex, INFINITE);
369
370 /* Loop through the scanlines */
371 for (i = 0; i < Resolution.Y; i++)
372 {
373 /* Loop through the pixels */
374 for (j = 0; j < Resolution.X; j++)
375 {
376 BYTE PixelData = 0;
377
378 /* Check the shifting mode */
379 if (VgaGcRegisters[VGA_GC_MODE_REG] & VGA_GC_MODE_SHIFT256)
380 {
381 /* 4 bits shifted from each plane */
382
383 /* Check if this is 16 or 256 color mode */
384 if (VgaAcRegisters[VGA_AC_CONTROL_REG] & VGA_AC_CONTROL_8BIT)
385 {
386 /* One byte per pixel */
387 PixelData = VgaMemory[(j % VGA_NUM_BANKS) * VGA_BANK_SIZE
388 + (Address + (j / VGA_NUM_BANKS))
389 * AddressSize];
390 }
391 else
392 {
393 /* 4-bits per pixel */
394
395 PixelData = VgaMemory[(j % VGA_NUM_BANKS) * VGA_BANK_SIZE
396 + (Address + (j / (VGA_NUM_BANKS * 2)))
397 * AddressSize];
398
399 /* Check if we should use the highest 4 bits or lowest 4 */
400 if (((j / VGA_NUM_BANKS) % 2) == 0)
401 {
402 /* Highest 4 */
403 PixelData >>= 4;
404 }
405 else
406 {
407 /* Lowest 4 */
408 PixelData &= 0x0F;
409 }
410 }
411 }
412 else if (VgaGcRegisters[VGA_GC_MODE_REG] & VGA_GC_MODE_SHIFTREG)
413 {
414 /*
415 * 2 bits shifted from plane 0 and 2 for the first 4 pixels,
416 * then 2 bits shifted from plane 1 and 3 for the next 4
417 */
418
419 // TODO: NOT IMPLEMENTED!
420 DPRINT1("Interleaved shift mode is not implemented!\n");
421 }
422 else
423 {
424 /* 1 bit shifted from each plane */
425
426 /* Check if this is 16 or 256 color mode */
427 if (VgaAcRegisters[VGA_AC_CONTROL_REG] & VGA_AC_CONTROL_8BIT)
428 {
429 /* 8 bits per pixel, 2 on each plane */
430
431 for (k = 0; k < VGA_NUM_BANKS; k++)
432 {
433 /* The data is on plane k, 4 pixels per byte */
434 BYTE PlaneData = VgaMemory[k * VGA_BANK_SIZE
435 + (Address + (j / 4)) * AddressSize];
436
437 /* The mask of the first bit in the pair */
438 BYTE BitMask = 1 << (((3 - (j % 4)) * 2) + 1);
439
440 /* Bits 0, 1, 2 and 3 come from the first bit of the pair */
441 if (PlaneData & BitMask) PixelData |= 1 << k;
442
443 /* Bits 4, 5, 6 and 7 come from the second bit of the pair */
444 if (PlaneData & (BitMask >> 1)) PixelData |= 1 << (k + 4);
445 }
446 }
447 else
448 {
449 /* 4 bits per pixel, 1 on each plane */
450
451 for (k = 0; k < VGA_NUM_BANKS; k++)
452 {
453 BYTE PlaneData = VgaMemory[k * VGA_BANK_SIZE
454 + (Address + (j / 8)) * AddressSize];
455
456 /* If the bit on that plane is set, set it */
457 if (PlaneData & (1 << (7 - (j % 8)))) PixelData |= 1 << k;
458 }
459 }
460 }
461
462 /* Now check if the resulting pixel data has changed */
463 if (GraphicsBuffer[i * Resolution.X + j] != PixelData)
464 {
465 /* Yes, write the new value */
466 GraphicsBuffer[i * Resolution.X + j] = PixelData;
467
468 /* Mark the specified pixel as changed */
469 VgaMarkForUpdate(i, j);
470 }
471 }
472
473 /* Move to the next scanline */
474 Address += ScanlineSize;
475 }
476
477 /*
478 * Release the console framebuffer mutex
479 * so that we allow for repainting.
480 */
481 ReleaseMutex(ConsoleMutex);
482 }
483 else
484 {
485 /* Text mode */
486 PCHAR_INFO CharBuffer = (PCHAR_INFO)ConsoleFramebuffer;
487
488 /* Loop through the scanlines */
489 for (i = 0; i < Resolution.Y; i++)
490 {
491 /* Loop through the characters */
492 for (j = 0; j < Resolution.X; j++)
493 {
494 DWORD CurrentAddr = LOWORD((Address + j) * AddressSize);
495 CHAR_INFO CharInfo;
496
497 /* Plane 0 holds the character itself */
498 CharInfo.Char.AsciiChar = VgaMemory[CurrentAddr];
499
500 /* Plane 1 holds the attribute */
501 CharInfo.Attributes = VgaMemory[CurrentAddr + VGA_BANK_SIZE];
502
503 /* Now check if the resulting character data has changed */
504 if ((CharBuffer[i * Resolution.X + j].Char.AsciiChar != CharInfo.Char.AsciiChar)
505 || (CharBuffer[i * Resolution.X + j].Attributes != CharInfo.Attributes))
506 {
507 /* Yes, write the new value */
508 CharBuffer[i * Resolution.X + j] = CharInfo;
509
510 /* Mark the specified pixel as changed */
511 VgaMarkForUpdate(i, j);
512 }
513 }
514
515 /* Move to the next scanline */
516 Address += ScanlineSize;
517 }
518 }
519 }
520
521 static VOID VgaUpdateTextCursor(VOID)
522 {
523 COORD Position;
524 CONSOLE_CURSOR_INFO CursorInfo;
525 BYTE CursorStart = VgaCrtcRegisters[VGA_CRTC_CURSOR_START_REG] & 0x3F;
526 BYTE CursorEnd = VgaCrtcRegisters[VGA_CRTC_CURSOR_END_REG] & 0x1F;
527 DWORD ScanlineSize = (DWORD)VgaCrtcRegisters[VGA_CRTC_OFFSET_REG] * 2;
528 BYTE TextSize = 1 + (VgaCrtcRegisters[VGA_CRTC_MAX_SCAN_LINE_REG] & 0x1F);
529 WORD Location = MAKEWORD(VgaCrtcRegisters[VGA_CRTC_CURSOR_LOC_LOW_REG],
530 VgaCrtcRegisters[VGA_CRTC_CURSOR_LOC_HIGH_REG]);
531
532 if (CursorStart < CursorEnd)
533 {
534 /* Visible cursor */
535 CursorInfo.bVisible = TRUE;
536 CursorInfo.dwSize = (100 * (CursorEnd - CursorStart)) / TextSize;
537 }
538 else
539 {
540 /* No cursor */
541 CursorInfo.bVisible = FALSE;
542 CursorInfo.dwSize = 0;
543 }
544
545 /* Add the cursor skew to the location */
546 Location += (VgaCrtcRegisters[VGA_CRTC_CURSOR_END_REG] >> 5) & 3;
547
548 /* Find the coordinates of the new position */
549 Position.X = (WORD)(Location % ScanlineSize);
550 Position.Y = (WORD)(Location / ScanlineSize);
551
552 /* Update the physical cursor */
553 SetConsoleCursorInfo(TextConsoleBuffer, &CursorInfo);
554 SetConsoleCursorPosition(TextConsoleBuffer, Position);
555 }
556
557 /* PUBLIC FUNCTIONS ***********************************************************/
558
559 DWORD VgaGetVideoBaseAddress(VOID)
560 {
561 return MemoryBase[(VgaGcRegisters[VGA_GC_MISC_REG] >> 2) & 0x03];
562 }
563
564 DWORD VgaGetVideoLimitAddress(VOID)
565 {
566 return MemoryLimit[(VgaGcRegisters[VGA_GC_MISC_REG] >> 2) & 0x03];
567 }
568
569 COORD VgaGetDisplayResolution(VOID)
570 {
571 COORD Resolution;
572 BYTE TextSize = 1 + (VgaCrtcRegisters[VGA_CRTC_MAX_SCAN_LINE_REG] & 0x1F);
573
574 /* The low 8 bits are in the display registers */
575 Resolution.X = VgaCrtcRegisters[VGA_CRTC_END_HORZ_DISP_REG];
576 Resolution.Y = VgaCrtcRegisters[VGA_CRTC_VERT_DISP_END_REG];
577
578 /* Set the top bits from the overflow register */
579 if (VgaCrtcRegisters[VGA_CRTC_OVERFLOW_REG] & VGA_CRTC_OVERFLOW_VDE8)
580 {
581 Resolution.Y |= 1 << 8;
582 }
583 if (VgaCrtcRegisters[VGA_CRTC_OVERFLOW_REG] & VGA_CRTC_OVERFLOW_VDE9)
584 {
585 Resolution.Y |= 1 << 9;
586 }
587
588 /* Increase the values by 1 */
589 Resolution.X++;
590 Resolution.Y++;
591
592 if (VgaGcRegisters[VGA_GC_MISC_REG] & VGA_GC_MISC_NOALPHA)
593 {
594 /* Multiply the horizontal resolution by the 9/8 dot mode */
595 Resolution.X *= (VgaSeqRegisters[VGA_SEQ_CLOCK_REG] & VGA_SEQ_CLOCK_98DM)
596 ? 8 : 9;
597
598 /* The horizontal resolution is halved in 8-bit mode */
599 if (VgaAcRegisters[VGA_AC_CONTROL_REG] & VGA_AC_CONTROL_8BIT) Resolution.X /= 2;
600
601 /* Divide the vertical resolution by the maximum scan line */
602 Resolution.Y /= ((DWORD)VgaCrtcRegisters[VGA_CRTC_MAX_SCAN_LINE_REG] & 0x1F) + 1;
603 }
604 else
605 {
606 /* Divide the number of scanlines by the font size */
607 Resolution.Y /= TextSize;
608 }
609
610 /* Return the resolution */
611 return Resolution;
612 }
613
614 VOID VgaRefreshDisplay(VOID)
615 {
616 COORD Resolution = VgaGetDisplayResolution();
617
618 DPRINT("VgaRefreshDisplay\n");
619
620 if (ModeChanged)
621 {
622 /* Change the display mode */
623 VgaUpdateMode();
624
625 /* Reset the mode change flag */
626 ModeChanged = FALSE;
627 }
628
629 if (CursorMoved)
630 {
631 /* Change the text cursor location */
632 VgaUpdateTextCursor();
633
634 /* Reset the cursor move flag */
635 CursorMoved = FALSE;
636 }
637
638 /* Update the contents of the framebuffer */
639 VgaUpdateFramebuffer();
640
641 /* Set the vertical retrace flag */
642 InVerticalRetrace = TRUE;
643
644 /* Ignore if there's nothing to update */
645 if (!NeedsUpdate) return;
646
647 DPRINT("Updating screen rectangle (%d, %d, %d, %d)\n",
648 UpdateRectangle.Left,
649 UpdateRectangle.Top,
650 UpdateRectangle.Right,
651 UpdateRectangle.Bottom);
652
653 /* Check if this is text mode or graphics mode */
654 if (VgaGcRegisters[VGA_GC_MISC_REG] & VGA_GC_MISC_NOALPHA)
655 {
656 /* Graphics mode */
657
658 /* Redraw the screen */
659 InvalidateConsoleDIBits(GraphicsConsoleBuffer, &UpdateRectangle);
660 }
661 else
662 {
663 /* Text mode */
664 COORD Origin = { UpdateRectangle.Left, UpdateRectangle.Top };
665
666 /* Write the data to the console */
667 WriteConsoleOutputA(TextConsoleBuffer,
668 (PCHAR_INFO)ConsoleFramebuffer,
669 Resolution,
670 Origin,
671 &UpdateRectangle);
672
673 }
674
675 /* Clear the update flag */
676 NeedsUpdate = FALSE;
677 }
678
679 VOID VgaHorizontalRetrace(VOID)
680 {
681 /* Set the flag */
682 InHorizontalRetrace = TRUE;
683 }
684
685 VOID VgaReadMemory(DWORD Address, LPBYTE Buffer, DWORD Size)
686 {
687 DWORD i;
688 DWORD VideoAddress;
689
690 DPRINT("VgaReadMemory: Address 0x%08X, Size %lu\n",
691 Address,
692 Size);
693
694 /* Ignore if video RAM access is disabled */
695 if (!(VgaMiscRegister & VGA_MISC_RAM_ENABLED)) return;
696
697 /* Loop through each byte */
698 for (i = 0; i < Size; i++)
699 {
700 VideoAddress = VgaTranslateReadAddress(Address + i);
701
702 /* Copy the value to the buffer */
703 Buffer[i] = VgaMemory[VideoAddress];
704 }
705 }
706
707 VOID VgaWriteMemory(DWORD Address, LPBYTE Buffer, DWORD Size)
708 {
709 DWORD i, j;
710 DWORD VideoAddress;
711
712 DPRINT("VgaWriteMemory: Address 0x%08X, Size %lu\n",
713 Address,
714 Size);
715
716 /* Ignore if video RAM access is disabled */
717 if (!(VgaMiscRegister & VGA_MISC_RAM_ENABLED)) return;
718
719 /* Also ignore if write access to all planes is disabled */
720 if ((VgaSeqRegisters[VGA_SEQ_MASK_REG] & 0x0F) == 0x00) return;
721
722 /* Loop through each byte */
723 for (i = 0; i < Size; i++)
724 {
725 VideoAddress = VgaTranslateWriteAddress(Address + i);
726
727 for (j = 0; j < VGA_NUM_BANKS; j++)
728 {
729 /* Make sure the page is writeable */
730 if (!(VgaSeqRegisters[VGA_SEQ_MASK_REG] & (1 << j))) continue;
731
732 /* Check if this is chain-4 mode */
733 if (VgaSeqRegisters[VGA_SEQ_MEM_REG] & VGA_SEQ_MEM_C4)
734 {
735 if (((Address + i) & 3) != j)
736 {
737 /* This plane will not be accessed */
738 continue;
739 }
740 }
741
742 /* Check if this is odd-even mode */
743 if (VgaGcRegisters[VGA_GC_MODE_REG] & VGA_GC_MODE_OE)
744 {
745 if (((Address + i) & 1) != (j & 1))
746 {
747 /* This plane will not be accessed */
748 continue;
749 }
750 }
751
752 /* Copy the value to the VGA memory */
753 VgaMemory[VideoAddress + j * VGA_BANK_SIZE] = Buffer[i];
754 }
755 }
756 }
757
758 BYTE VgaReadPort(WORD Port)
759 {
760 DPRINT("VgaReadPort: Port 0x%04X\n", Port);
761
762 switch (Port)
763 {
764 case VGA_AC_INDEX:
765 {
766 return VgaAcIndex;
767 }
768
769 case VGA_AC_READ:
770 {
771 return VgaAcRegisters[VgaAcIndex];
772 }
773
774 case VGA_SEQ_INDEX:
775 {
776 return VgaSeqIndex;
777 }
778
779 case VGA_SEQ_DATA:
780 {
781 return VgaSeqRegisters[VgaSeqIndex];
782 }
783
784 case VGA_DAC_READ_INDEX:
785 {
786 /* This returns the read/write state */
787 return VgaDacReadWrite ? 0 : 3;
788 }
789
790 case VGA_DAC_WRITE_INDEX:
791 {
792 return VgaDacIndex;
793 }
794
795 case VGA_DAC_DATA:
796 {
797 /* Ignore reads in write mode */
798 if (!VgaDacReadWrite)
799 {
800 BYTE Data = VgaDacRegisters[VgaDacIndex++];
801 VgaDacIndex %= VGA_PALETTE_SIZE;
802 return Data;
803 }
804
805 break;
806 }
807
808 case VGA_MISC_READ:
809 {
810 return VgaMiscRegister;
811 }
812
813 case VGA_CRTC_INDEX:
814 {
815 return VgaCrtcIndex;
816 }
817
818 case VGA_CRTC_DATA:
819 {
820 return VgaCrtcRegisters[VgaCrtcIndex];
821 }
822
823 case VGA_GC_INDEX:
824 {
825 return VgaGcIndex;
826 }
827
828 case VGA_GC_DATA:
829 {
830 return VgaGcRegisters[VgaGcIndex];
831 }
832
833 case VGA_STAT_MONO:
834 case VGA_STAT_COLOR:
835 {
836 BYTE Result = 0;
837
838 /* Reset the AC latch */
839 VgaAcLatch = FALSE;
840
841 /* Set a flag if there is a vertical or horizontal retrace */
842 if (InVerticalRetrace || InHorizontalRetrace) Result |= VGA_STAT_DD;
843
844 /* Set an additional flag if there was a vertical retrace */
845 if (InVerticalRetrace) Result |= VGA_STAT_VRETRACE;
846
847 /* Clear the flags */
848 InHorizontalRetrace = InVerticalRetrace = FALSE;
849
850 return Result;
851 }
852 }
853
854 return 0;
855 }
856
857 VOID VgaWritePort(WORD Port, BYTE Data)
858 {
859 DPRINT("VgaWritePort: Port 0x%04X, Data 0x%02X\n", Port, Data);
860
861 switch (Port)
862 {
863 case VGA_AC_INDEX:
864 {
865 if (!VgaAcLatch)
866 {
867 /* Change the index */
868 if (Data < VGA_AC_MAX_REG) VgaAcIndex = Data;
869 }
870 else
871 {
872 /* Write the data */
873 VgaWriteAc(Data);
874 }
875
876 /* Toggle the latch */
877 VgaAcLatch = !VgaAcLatch;
878
879 break;
880 }
881
882 case VGA_SEQ_INDEX:
883 {
884 /* Set the sequencer index register */
885 if (Data < VGA_SEQ_MAX_REG) VgaSeqIndex = Data;
886
887 break;
888 }
889
890 case VGA_SEQ_DATA:
891 {
892 /* Call the sequencer function */
893 VgaWriteSequencer(Data);
894
895 break;
896 }
897
898 case VGA_DAC_READ_INDEX:
899 {
900 VgaDacReadWrite = FALSE;
901 VgaDacIndex = Data % VGA_PALETTE_SIZE;
902
903 break;
904 }
905
906 case VGA_DAC_WRITE_INDEX:
907 {
908 VgaDacReadWrite = TRUE;
909 VgaDacIndex = Data % VGA_PALETTE_SIZE;
910
911 break;
912 }
913
914 case VGA_DAC_DATA:
915 {
916 /* Ignore writes in read mode */
917 if (VgaDacReadWrite) VgaWriteDac(Data & 0x3F);
918
919 break;
920 }
921
922 case VGA_MISC_WRITE:
923 {
924 VgaMiscRegister = Data;
925
926 break;
927 }
928
929 case VGA_CRTC_INDEX:
930 {
931 /* Set the CRTC index register */
932 if (Data < VGA_CRTC_MAX_REG) VgaCrtcIndex = Data;
933
934 break;
935 }
936
937 case VGA_CRTC_DATA:
938 {
939 /* Call the CRTC function */
940 VgaWriteCrtc(Data);
941
942 break;
943 }
944
945 case VGA_GC_INDEX:
946 {
947 /* Set the GC index register */
948 if (Data < VGA_GC_MAX_REG) VgaGcIndex = Data;
949 break;
950 }
951
952 case VGA_GC_DATA:
953 {
954 /* Call the GC function */
955 VgaWriteGc(Data);
956
957 break;
958 }
959 }
960 }
961
962 VOID VgaInitialize(HANDLE TextHandle)
963 {
964 INT i, j;
965 COORD Resolution;
966 INT AddressSize;
967 DWORD ScanlineSize;
968 COORD Origin = { 0, 0 };
969 SMALL_RECT ScreenRect;
970 PCHAR_INFO CharBuffer;
971 DWORD Address = 0;
972 DWORD CurrentAddr;
973
974 /* Set the global handle */
975 TextConsoleBuffer = TextHandle;
976
977 /* Clear the VGA memory */
978 ZeroMemory(VgaMemory, VGA_NUM_BANKS * VGA_BANK_SIZE);
979
980 /* Set the default video mode */
981 BiosSetVideoMode(BIOS_DEFAULT_VIDEO_MODE);
982 VgaUpdateMode();
983 ModeChanged = FALSE;
984
985 /* Get the data */
986 Resolution = VgaGetDisplayResolution();
987 CharBuffer = (PCHAR_INFO)ConsoleFramebuffer;
988 AddressSize = VgaGetAddressSize();
989 ScreenRect.Left = ScreenRect.Top = 0;
990 ScreenRect.Right = Resolution.X;
991 ScreenRect.Bottom = Resolution.Y;
992 ScanlineSize = (DWORD)VgaCrtcRegisters[VGA_CRTC_OFFSET_REG] * 2;
993
994 /* Read the data from the console into the framebuffer */
995 ReadConsoleOutputA(TextConsoleBuffer,
996 CharBuffer,
997 Resolution,
998 Origin,
999 &ScreenRect);
1000
1001 /* Loop through the scanlines */
1002 for (i = 0; i < Resolution.Y; i++)
1003 {
1004 /* Loop through the characters */
1005 for (j = 0; j < Resolution.X; j++)
1006 {
1007 CurrentAddr = LOWORD((Address + j) * AddressSize);
1008
1009 /* Store the character in plane 0 */
1010 VgaMemory[CurrentAddr] = CharBuffer[i * Resolution.X + j].Char.AsciiChar;
1011
1012 /* Store the attribute in plane 1 */
1013 VgaMemory[CurrentAddr + VGA_BANK_SIZE] = (BYTE)CharBuffer[i * Resolution.X + j].Attributes;
1014 }
1015
1016 /* Move to the next scanline */
1017 Address += ScanlineSize;
1018 }
1019 }
1020
1021 /* EOF */