terminal.c 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. /* Terminal: prompt, busy indicator, blinking cursor, echo loop. */
  2. #include "io.h"
  3. #include "terminal.h"
  4. #include "timer.h"
  5. static unsigned int busy_idx;
  6. static unsigned int last_busy_tick;
  7. static const unsigned char busy_chars[4] = { 0x2D, 0x5C, 0x7C, 0x2F };
  8. static int cursor_visible;
  9. static unsigned int last_cursor_tick;
  10. static unsigned int puthex(unsigned int pos, unsigned char byte)
  11. {
  12. static const char hex[] = "0123456789ABCDEF";
  13. unsigned short cell = (unsigned short)(0x0A << 8);
  14. io_vga_write(pos++, (unsigned short)(cell | hex[byte >> 4]));
  15. io_vga_write(pos++, (unsigned short)(cell | hex[byte & 0x0F]));
  16. return pos;
  17. }
  18. static void debug_line(unsigned char sc, char c)
  19. {
  20. unsigned int i, pos = IO_DEBUG_ROW * IO_VGA_COLS;
  21. for (i = 0; i < 79; i++)
  22. io_vga_write(pos + i, (unsigned short)(0x0A << 8));
  23. pos = IO_DEBUG_ROW * IO_VGA_COLS;
  24. io_vga_write(pos++, (unsigned short)((0x0A << 8) | 'S'));
  25. io_vga_write(pos++, (unsigned short)((0x0A << 8) | 'C'));
  26. io_vga_write(pos++, (unsigned short)((0x0A << 8) | ':'));
  27. pos = puthex(pos, sc);
  28. io_vga_write(pos++, (unsigned short)((0x0A << 8) | ' '));
  29. io_vga_write(pos++, (unsigned short)((0x0A << 8) | 'C'));
  30. io_vga_write(pos++, (unsigned short)((0x0A << 8) | 'H'));
  31. io_vga_write(pos++, (unsigned short)((0x0A << 8) | ':'));
  32. pos = puthex(pos, (unsigned char)c);
  33. io_vga_write(pos++, (unsigned short)((0x0A << 8) | ' '));
  34. io_vga_write(pos++, (unsigned short)((0x0A << 8) | '\''));
  35. io_vga_write(pos++, (unsigned short)((0x0A << 8) | (c >= 0x20 && c < 0x7F ? (unsigned char)c : '.')));
  36. io_vga_write(pos, (unsigned short)((0x0A << 8) | '\''));
  37. }
  38. static void update_busy(void)
  39. {
  40. unsigned int t = get_ticks_ms();
  41. if (t - last_busy_tick >= BUSY_MS) {
  42. last_busy_tick = t;
  43. busy_idx = (busy_idx + 1) % 4;
  44. }
  45. io_vga_write(IO_BUSY_POS, (unsigned short)((0x0A << 8) | busy_chars[busy_idx]));
  46. }
  47. static void update_cursor_blink(void)
  48. {
  49. unsigned int t = get_ticks_ms();
  50. if (t - last_cursor_tick >= CURSOR_MS) {
  51. last_cursor_tick = t;
  52. cursor_visible = !cursor_visible;
  53. }
  54. if (cursor_visible)
  55. io_cursor_show_block();
  56. else
  57. io_cursor_hide_block();
  58. }
  59. static const char msg[] __attribute__((section(".text"))) = "Nebs OS |o|";
  60. static const char prompt[] __attribute__((section(".text"))) = "\n> ";
  61. void terminal_run(void)
  62. {
  63. last_cursor_tick = get_ticks_ms();
  64. last_busy_tick = get_ticks_ms();
  65. cursor_visible = 1;
  66. busy_idx = 0;
  67. io_putstr(msg);
  68. io_putstr(prompt);
  69. for (;;) {
  70. update_busy();
  71. update_cursor_blink();
  72. {
  73. char c = io_poll_key();
  74. if (c) {
  75. debug_line(io_last_scancode(), c);
  76. io_putchar(c);
  77. if (c == '\n')
  78. io_putstr(prompt);
  79. } else {
  80. __asm__ volatile ("hlt");
  81. }
  82. }
  83. }
  84. }