vga.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. /* VGA mode 13h / mode 03h via direct register programming.
  2. Saves and restores the 8x16 font from plane 2 so text mode survives. */
  3. #include "vga.h"
  4. #define VGA_FB ((volatile unsigned char *)0xA0000)
  5. static inline void outb(unsigned short port, unsigned char v)
  6. {
  7. __asm__ volatile ("outb %0, %1" : : "a"(v), "Nd"(port));
  8. }
  9. static inline unsigned char inb(unsigned short port)
  10. {
  11. unsigned char v;
  12. __asm__ volatile ("inb %1, %0" : "=a"(v) : "Nd"(port));
  13. return v;
  14. }
  15. /* ---- low-level register helpers ---- */
  16. static void write_seq(unsigned char idx, unsigned char val)
  17. {
  18. outb(0x3C4, idx);
  19. outb(0x3C5, val);
  20. }
  21. static void write_crtc(unsigned char idx, unsigned char val)
  22. {
  23. outb(0x3D4, idx);
  24. outb(0x3D5, val);
  25. }
  26. static void write_gc(unsigned char idx, unsigned char val)
  27. {
  28. outb(0x3CE, idx);
  29. outb(0x3CF, val);
  30. }
  31. static void crtc_unlock(void)
  32. {
  33. outb(0x3D4, 0x11);
  34. outb(0x3D5, inb(0x3D5) & 0x7F);
  35. }
  36. /* ---- register tables ---- */
  37. /* Mode 13h: 320x200, 256 colors, chain-4, framebuffer at 0xA0000 */
  38. static const unsigned char misc_13 = 0x63;
  39. static const unsigned char seq_13[] = { 0x03, 0x01, 0x0F, 0x00, 0x0E };
  40. static const unsigned char crtc_13[] = {
  41. 0x5F, 0x4F, 0x50, 0x82, 0x54, 0x80, 0xBF, 0x1F,
  42. 0x00, 0x41, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  43. 0x9C, 0x0E, 0x8F, 0x28, 0x40, 0x96, 0xB9, 0xA3, 0xFF
  44. };
  45. static const unsigned char gc_13[] = {
  46. 0x00, 0x00, 0x00, 0x00, 0x00, 0x40, 0x05, 0x0F, 0xFF
  47. };
  48. static const unsigned char ac_13[] = {
  49. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  50. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
  51. 0x41, 0x00, 0x0F, 0x00, 0x00
  52. };
  53. /* Mode 03h: 80x25, 16-color text, framebuffer at 0xB8000 */
  54. static const unsigned char misc_03 = 0x67;
  55. static const unsigned char seq_03[] = { 0x03, 0x00, 0x03, 0x00, 0x02 };
  56. static const unsigned char crtc_03[] = {
  57. 0x5F, 0x4F, 0x50, 0x82, 0x55, 0x81, 0xBF, 0x1F,
  58. 0x00, 0x4F, 0x20, 0x0E, 0x00, 0x00, 0x00, 0x00,
  59. 0x9C, 0x0E, 0x8F, 0x28, 0x1F, 0x96, 0xB9, 0xA3, 0xFF
  60. };
  61. static const unsigned char gc_03[] = {
  62. 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x0E, 0x00, 0xFF
  63. };
  64. static const unsigned char ac_03[] = {
  65. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x14, 0x07,
  66. 0x38, 0x39, 0x3A, 0x3B, 0x3C, 0x3D, 0x3E, 0x3F,
  67. 0x0C, 0x00, 0x0F, 0x08, 0x00
  68. };
  69. /* ---- font buffer: 256 chars x 32 bytes/char ---- */
  70. #define FONT_SIZE 8192
  71. static unsigned char font_buf[FONT_SIZE];
  72. /* ---- set all VGA registers for a mode ---- */
  73. static void set_regs(unsigned char misc,
  74. const unsigned char *seq, const unsigned char *crtc,
  75. const unsigned char *gc, const unsigned char *ac)
  76. {
  77. unsigned int i;
  78. /* Misc Output */
  79. outb(0x3C2, misc);
  80. /* Sequencer: sync reset, write 1-4, release */
  81. write_seq(0, 0x01);
  82. for (i = 1; i < 5; i++)
  83. write_seq((unsigned char)i, seq[i]);
  84. write_seq(0, seq[0]);
  85. /* CRTC: unlock then write all 25 */
  86. crtc_unlock();
  87. for (i = 0; i < 25; i++)
  88. write_crtc((unsigned char)i, crtc[i]);
  89. /* Graphics Controller */
  90. for (i = 0; i < 9; i++)
  91. write_gc((unsigned char)i, gc[i]);
  92. /* Attribute Controller */
  93. (void)inb(0x3DA); /* reset flip-flop */
  94. for (i = 0; i < 21; i++) {
  95. outb(0x3C0, (unsigned char)i);
  96. outb(0x3C0, ac[i]);
  97. }
  98. /* Re-enable display (PAS bit) */
  99. (void)inb(0x3DA);
  100. outb(0x3C0, 0x20);
  101. }
  102. /* ---- font save / restore via plane 2 ---- */
  103. static void font_plane_access(void)
  104. {
  105. write_seq(0, 0x01); /* sync reset */
  106. write_seq(2, 0x04); /* map mask: plane 2 only */
  107. write_seq(4, 0x06); /* sequential access, extended memory */
  108. write_seq(0, 0x03); /* release reset */
  109. write_gc(4, 0x02); /* read map select: plane 2 */
  110. write_gc(5, 0x00); /* mode: read/write mode 0 */
  111. write_gc(6, 0x00); /* misc: A0000, 128K window */
  112. }
  113. static void text_plane_access(void)
  114. {
  115. write_seq(0, 0x01); /* sync reset */
  116. write_seq(2, 0x03); /* map mask: planes 0 + 1 */
  117. write_seq(4, 0x02); /* odd/even, extended memory */
  118. write_seq(0, 0x03); /* release reset */
  119. write_gc(4, 0x00); /* read map select: plane 0 */
  120. write_gc(5, 0x10); /* odd/even mode */
  121. write_gc(6, 0x0E); /* B8000, 32K window */
  122. }
  123. static void save_font(void)
  124. {
  125. unsigned int i;
  126. font_plane_access();
  127. for (i = 0; i < FONT_SIZE; i++)
  128. font_buf[i] = VGA_FB[i];
  129. }
  130. static void restore_font(void)
  131. {
  132. unsigned int i;
  133. font_plane_access();
  134. for (i = 0; i < FONT_SIZE; i++)
  135. VGA_FB[i] = font_buf[i];
  136. text_plane_access();
  137. }
  138. /* ---- public API ---- */
  139. void vga_set_mode_13(void)
  140. {
  141. save_font();
  142. set_regs(misc_13, seq_13, crtc_13, gc_13, ac_13);
  143. }
  144. void vga_set_mode_text(void)
  145. {
  146. set_regs(misc_03, seq_03, crtc_03, gc_03, ac_03);
  147. restore_font();
  148. }
  149. void vga_put_pixel(int x, int y, unsigned char color)
  150. {
  151. if (x >= 0 && x < VGA_GFX_WIDTH && y >= 0 && y < VGA_GFX_HEIGHT)
  152. VGA_FB[y * VGA_GFX_WIDTH + x] = color;
  153. }
  154. void vga_clear(unsigned char color)
  155. {
  156. unsigned int i;
  157. unsigned int n = VGA_GFX_WIDTH * VGA_GFX_HEIGHT;
  158. for (i = 0; i < n; i++)
  159. VGA_FB[i] = color;
  160. }