kernel.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. /* Kernel: entered from bootloader in 32-bit protected mode at 0x7E00.
  2. * Must be linked so this function is at 0x7E00 (see kernel.ld).
  3. * Simple terminal: keyboard echo. */
  4. #define VGA_BASE ((volatile unsigned short *)0xB8000)
  5. #define VGA_ATTR 0x0A /* green on black */
  6. #define VGA_COLS 80
  7. #define VGA_ROWS 25
  8. #define VGA_SIZE (VGA_COLS * VGA_ROWS)
  9. #define DEBUG_ROW 24 /* last row: debug line (input vs echo) */
  10. #define CONTENT_ROWS (DEBUG_ROW) /* rows 0..23 for terminal */
  11. #define CONTENT_SIZE (CONTENT_ROWS * VGA_COLS)
  12. /* Port I/O */
  13. static inline unsigned char inb(unsigned short port)
  14. {
  15. unsigned char v;
  16. __asm__ volatile ("inb %1, %0" : "=a"(v) : "Nd"(port));
  17. return v;
  18. }
  19. /* PS/2 keyboard: data port 0x60, status 0x64 (bit 0 = output buffer full) */
  20. #define KB_DATA 0x60
  21. #define KB_STATUS 0x64
  22. /* Scancode set 1 (XT) -> ASCII. 0 = ignore. Set 1 break = make | 0x80, we ignore those. */
  23. static const unsigned char scancode_to_ascii[256] __attribute__((section(".rodata"))) = {
  24. [0x02] = '1', [0x03] = '2', [0x04] = '3', [0x05] = '4', [0x06] = '5',
  25. [0x07] = '6', [0x08] = '7', [0x09] = '8', [0x0A] = '9', [0x0B] = '0',
  26. [0x0C] = '-', [0x0D] = '=', [0x0E] = '\b', [0x0F] = '\t',
  27. [0x10] = 'q', [0x11] = 'w', [0x12] = 'e', [0x13] = 'r', [0x14] = 't',
  28. [0x15] = 'y', [0x16] = 'u', [0x17] = 'i', [0x18] = 'o', [0x19] = 'p',
  29. [0x1A] = '[', [0x1B] = ']', [0x1C] = '\n',
  30. [0x1E] = 'a', [0x1F] = 's', [0x20] = 'd', [0x21] = 'f', [0x22] = 'g',
  31. [0x23] = 'h', [0x24] = 'j', [0x25] = 'k', [0x26] = 'l', [0x27] = ';',
  32. [0x28] = '\'', [0x29] = '`',
  33. [0x2B] = '\\', [0x2C] = 'z', [0x2D] = 'x', [0x2E] = 'c', [0x2F] = 'v',
  34. [0x30] = 'b', [0x31] = 'n', [0x32] = 'm', [0x33] = ',', [0x34] = '.',
  35. [0x35] = '/', [0x39] = ' ',
  36. };
  37. /* Set 1: left shift make 0x2A break 0xAA, right shift make 0x36 break 0xB6 */
  38. static int shift_pressed;
  39. static unsigned int cursor; /* linear position 0 .. CONTENT_SIZE-1 */
  40. static unsigned char last_scancode; /* set by getchar for debug line */
  41. #define BUSY_POS (DEBUG_ROW * VGA_COLS + 79) /* bottom-right: never overwritten by echo */
  42. static unsigned int busy_tick;
  43. /* Explicit ASCII so no encoding/escape issues: - \ | / */
  44. static const unsigned char busy_chars[4] = { 0x2D, 0x5C, 0x7C, 0x2F };
  45. static void putchar(char c)
  46. {
  47. if (c == '\n') {
  48. cursor = (cursor / VGA_COLS + 1) * VGA_COLS;
  49. if (cursor >= CONTENT_SIZE) {
  50. /* Scroll: move lines up, clear bottom of content area */
  51. unsigned int i;
  52. for (i = 0; i < (CONTENT_ROWS - 1) * VGA_COLS; i++)
  53. VGA_BASE[i] = VGA_BASE[i + VGA_COLS];
  54. for (i = (CONTENT_ROWS - 1) * VGA_COLS; i < CONTENT_SIZE; i++)
  55. VGA_BASE[i] = (unsigned short)(VGA_ATTR << 8);
  56. cursor = (CONTENT_ROWS - 1) * VGA_COLS;
  57. }
  58. return;
  59. }
  60. if (c == '\b') {
  61. if (cursor > 0) {
  62. cursor--;
  63. VGA_BASE[cursor] = (unsigned short)(VGA_ATTR << 8);
  64. }
  65. return;
  66. }
  67. if (cursor < CONTENT_SIZE) {
  68. VGA_BASE[cursor] = (unsigned short)((VGA_ATTR << 8) | (unsigned char)c);
  69. cursor++;
  70. }
  71. }
  72. /* Write one hex byte at VGA position; returns next position. */
  73. static unsigned int puthex(unsigned int pos, unsigned char byte)
  74. {
  75. static const char hex[] = "0123456789ABCDEF";
  76. VGA_BASE[pos++] = (unsigned short)((VGA_ATTR << 8) | hex[byte >> 4]);
  77. VGA_BASE[pos++] = (unsigned short)((VGA_ATTR << 8) | hex[byte & 0x0F]);
  78. return pos;
  79. }
  80. /* Update bottom line with last scancode and decoded char. Leaves col 79 for busy. */
  81. static void debug_line(unsigned char sc, char c)
  82. {
  83. unsigned int i, pos = DEBUG_ROW * VGA_COLS;
  84. for (i = 0; i < 79; i++)
  85. VGA_BASE[pos + i] = (unsigned short)(VGA_ATTR << 8);
  86. /* "SC:XX CH:YY 'c'" */
  87. VGA_BASE[pos++] = (unsigned short)((VGA_ATTR << 8) | 'S');
  88. VGA_BASE[pos++] = (unsigned short)((VGA_ATTR << 8) | 'C');
  89. VGA_BASE[pos++] = (unsigned short)((VGA_ATTR << 8) | ':');
  90. pos = puthex(pos, sc);
  91. VGA_BASE[pos++] = (unsigned short)((VGA_ATTR << 8) | ' ');
  92. VGA_BASE[pos++] = (unsigned short)((VGA_ATTR << 8) | 'C');
  93. VGA_BASE[pos++] = (unsigned short)((VGA_ATTR << 8) | 'H');
  94. VGA_BASE[pos++] = (unsigned short)((VGA_ATTR << 8) | ':');
  95. pos = puthex(pos, (unsigned char)c);
  96. VGA_BASE[pos++] = (unsigned short)((VGA_ATTR << 8) | ' ');
  97. VGA_BASE[pos++] = (unsigned short)((VGA_ATTR << 8) | '\'');
  98. VGA_BASE[pos++] = (unsigned short)((VGA_ATTR << 8) | (c >= 0x20 && c < 0x7F ? (unsigned char)c : '.'));
  99. VGA_BASE[pos] = (unsigned short)((VGA_ATTR << 8) | '\'');
  100. }
  101. static void putstr(const char *s)
  102. {
  103. while (*s) putchar(*s++);
  104. }
  105. /* Apply shift to get US layout shifted character. Returns c unchanged if no shift variant. */
  106. static char apply_shift(char c)
  107. {
  108. if (!shift_pressed) return c;
  109. if (c >= 'a' && c <= 'z') return (char)(c - 32);
  110. switch ((unsigned char)c) {
  111. case '1': return '!'; case '2': return '@'; case '3': return '#';
  112. case '4': return '$'; case '5': return '%'; case '6': return '^';
  113. case '7': return '&'; case '8': return '*'; case '9': return '(';
  114. case '0': return ')'; case '-': return '_'; case '=': return '+';
  115. case '[': return '{'; case ']': return '}'; case ';': return ':';
  116. case '\'': return '"'; case '`': return '~'; case '\\': return '|';
  117. case ',': return '<'; case '.': return '>'; case '/': return '?';
  118. default: return c;
  119. }
  120. }
  121. /* Draw busy indicator at bottom-right so we know the event loop runs. */
  122. static void update_busy(void)
  123. {
  124. busy_tick++;
  125. VGA_BASE[BUSY_POS] = (unsigned short)((VGA_ATTR << 8) | busy_chars[busy_tick % 4]);
  126. }
  127. /* String in .text so it loads with the kernel */
  128. static const char msg[] __attribute__((section(".text"))) = "Hello from C (protected mode)";
  129. static const char prompt[] __attribute__((section(".text"))) = "\n> ";
  130. /* Entry point: must be first in .text so bootloader jump to 0x7E00 hits it */
  131. void kernel_main(void) __attribute__((section(".text.entry")));
  132. void kernel_main(void)
  133. {
  134. unsigned i;
  135. /* Clear screen */
  136. for (i = 0; i < VGA_SIZE; i++)
  137. VGA_BASE[i] = (unsigned short)(VGA_ATTR << 8);
  138. cursor = 0;
  139. putstr(msg);
  140. putstr(prompt);
  141. /* Drain keyboard buffer so we don't echo leftover/garbage bytes */
  142. while (inb(KB_STATUS) & 1)
  143. (void)inb(KB_DATA);
  144. /* Terminal echo loop: always update busy indicator, then poll keyboard */
  145. for (;;) {
  146. update_busy();
  147. if (inb(KB_STATUS) & 1) {
  148. unsigned char sc = inb(KB_DATA);
  149. if (sc & 0x80) {
  150. /* Set 1 break: release shift */
  151. if (sc == 0xAA || sc == 0xB6) shift_pressed = 0;
  152. continue;
  153. }
  154. if (sc == 0x2A || sc == 0x36) {
  155. shift_pressed = 1;
  156. continue;
  157. }
  158. {
  159. char c = (char)scancode_to_ascii[sc];
  160. if (c) {
  161. c = apply_shift(c);
  162. last_scancode = sc;
  163. debug_line(last_scancode, c);
  164. putchar(c);
  165. if (c == '\n')
  166. putstr(prompt);
  167. }
  168. }
  169. }
  170. }
  171. }