terminal.c 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. /* Terminal: prompt, datetime (top right), blinking cursor, echo loop. */
  2. #include "io.h"
  3. #include "rtc.h"
  4. #include "terminal.h"
  5. #include "timer.h"
  6. #define DATETIME_MS 1000
  7. static unsigned int last_datetime_tick;
  8. static char datetime_buf[IO_DATETIME_LEN + 1];
  9. static int cursor_visible;
  10. static unsigned int last_cursor_tick;
  11. static void update_datetime(void)
  12. {
  13. unsigned int t = get_ticks_ms();
  14. if (t - last_datetime_tick >= DATETIME_MS) {
  15. last_datetime_tick = t;
  16. rtc_read_datetime(datetime_buf);
  17. {
  18. unsigned int i;
  19. for (i = 0; i < IO_DATETIME_LEN && datetime_buf[i]; i++)
  20. io_vga_write(IO_DATETIME_POS + i,
  21. (unsigned short)((0x0A << 8) | (unsigned char)datetime_buf[i]));
  22. }
  23. }
  24. }
  25. static void update_cursor_blink(void)
  26. {
  27. unsigned int t = get_ticks_ms();
  28. if (t - last_cursor_tick >= CURSOR_MS) {
  29. last_cursor_tick = t;
  30. cursor_visible = !cursor_visible;
  31. }
  32. if (cursor_visible)
  33. io_cursor_show_block();
  34. else
  35. io_cursor_hide_block();
  36. }
  37. static const char msg[] __attribute__((section(".text"))) = "Nebs OS |o|";
  38. static const char prompt[] __attribute__((section(".text"))) = "\n> ";
  39. void terminal_run(void)
  40. {
  41. last_cursor_tick = get_ticks_ms();
  42. last_datetime_tick = get_ticks_ms();
  43. cursor_visible = 1;
  44. io_putstr(msg);
  45. io_putstr(prompt);
  46. for (;;) {
  47. update_datetime();
  48. update_cursor_blink();
  49. {
  50. char c = io_poll_key();
  51. if (c) {
  52. io_putchar(c);
  53. if (c == '\n')
  54. io_putstr(prompt);
  55. } else {
  56. __asm__ volatile ("hlt");
  57. }
  58. }
  59. }
  60. }