serial.c 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. /* Serial port (COM1 0x3F8) debug output for bare-metal diagnostics. */
  2. #include "serial.h"
  3. #include "pci.h"
  4. #define COM1 0x3F8
  5. void serial_init(void)
  6. {
  7. port_outb(COM1 + 1, 0x00); /* disable interrupts */
  8. port_outb(COM1 + 3, 0x80); /* DLAB on */
  9. port_outb(COM1 + 0, 0x0C); /* divisor 12 = 9600 baud */
  10. port_outb(COM1 + 1, 0x00);
  11. port_outb(COM1 + 3, 0x03); /* 8N1, DLAB off */
  12. port_outb(COM1 + 2, 0xC7); /* enable FIFO */
  13. port_outb(COM1 + 4, 0x03); /* RTS + DTR */
  14. }
  15. void serial_putc(char c)
  16. {
  17. while (!(port_inb(COM1 + 5) & 0x20))
  18. ;
  19. port_outb(COM1, (unsigned char)c);
  20. }
  21. void serial_puts(const char *s)
  22. {
  23. while (*s) {
  24. if (*s == '\n')
  25. serial_putc('\r');
  26. serial_putc(*s++);
  27. }
  28. }
  29. void serial_puthex(unsigned int n)
  30. {
  31. static const char hex[] = "0123456789ABCDEF";
  32. int i;
  33. serial_puts("0x");
  34. for (i = 28; i >= 0; i -= 4)
  35. serial_putc(hex[(n >> i) & 0xF]);
  36. }
  37. void serial_putdec(unsigned int n)
  38. {
  39. char buf[12];
  40. char *p = buf + sizeof(buf) - 1;
  41. *p = '\0';
  42. if (n == 0) {
  43. serial_putc('0');
  44. return;
  45. }
  46. while (n) {
  47. *--p = (char)('0' + (n % 10));
  48. n /= 10;
  49. }
  50. serial_puts(p);
  51. }