text.h 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. #pragma once
  2. static int txt_debug = 0;
  3. unsigned short * textmemptr = (unsigned short *)0xB8000;
  4. static void placech(unsigned char c, int x, int y, int attr) {
  5. #ifdef EFI_PLATFORM
  6. unsigned short ch;
  7. switch (c) {
  8. case '\030':
  9. ch = L'↑';
  10. break;
  11. case '\031':
  12. ch = L'↓';
  13. break;
  14. case '\032':
  15. ch = L'←';
  16. break;
  17. case '\033':
  18. ch = L'→';
  19. break;
  20. default:
  21. ch = c;
  22. break;
  23. }
  24. uint16_t string[] = {ch, 0};
  25. uefi_call_wrapper(ST->ConOut->SetAttribute, 2, ST->ConOut, attr);
  26. uefi_call_wrapper(ST->ConOut->SetCursorPosition, 3, ST->ConOut, x, y);
  27. uefi_call_wrapper(ST->ConOut->OutputString, 2, ST->ConOut, string);
  28. #else
  29. unsigned short *where;
  30. unsigned att = attr << 8;
  31. where = textmemptr + (y * 80 + x);
  32. *where = c | att;
  33. #endif
  34. }
  35. static int x = 0;
  36. static int y = 0;
  37. static int attr = 0x07;
  38. static void print_(char * str) {
  39. while (*str) {
  40. if (*str == '\n') {
  41. for (; x < 80; ++x) {
  42. placech(' ', x, y, attr);
  43. }
  44. x = 0;
  45. y += 1;
  46. if (y == 24) {
  47. y = 0;
  48. }
  49. } else {
  50. placech(*str, x, y, attr);
  51. x++;
  52. if (x == 80) {
  53. x = 0;
  54. y += 1;
  55. if (y == 24) {
  56. y = 0;
  57. }
  58. }
  59. }
  60. str++;
  61. }
  62. }
  63. static void move_cursor(int _x, int _y) {
  64. x = _x;
  65. y = _y;
  66. }
  67. static void set_attr(int _attr) {
  68. attr = _attr;
  69. }
  70. static void print_banner(char * str) {
  71. if (!str) {
  72. for (int i = 0; i < 80; ++i) {
  73. placech(' ', i, y, attr);
  74. }
  75. y++;
  76. return;
  77. }
  78. int len = 0;
  79. char *c = str;
  80. while (*c) {
  81. len++;
  82. c++;
  83. }
  84. int off = (80 - len) / 2;
  85. for (int i = 0; i < 80; ++i) {
  86. placech(' ', i, y, attr);
  87. }
  88. for (int i = 0; i < len; ++i) {
  89. placech(str[i], i + off, y, attr);
  90. }
  91. y++;
  92. }
  93. #ifdef EFI_PLATFORM
  94. static void print_int_(unsigned int value) {
  95. unsigned int n_width = 1;
  96. unsigned int i = 9;
  97. while (value > i && i < UINT32_MAX) {
  98. n_width += 1;
  99. i *= 10;
  100. i += 9;
  101. }
  102. char buf[n_width+1];
  103. buf[n_width] = 0;
  104. i = n_width;
  105. while (i > 0) {
  106. unsigned int n = value / 10;
  107. int r = value % 10;
  108. buf[i - 1] = r + '0';
  109. i--;
  110. value = n;
  111. }
  112. print_(buf);
  113. }
  114. #endif
  115. static void print_hex_(unsigned int value) {
  116. char out[9] = {0};
  117. for (int i = 7; i > -1; i--) {
  118. out[i] = "0123456789abcdef"[(value >> (4 * (7 - i))) & 0xF];
  119. }
  120. print_(out);
  121. }
  122. static void clear_() {
  123. x = 0;
  124. y = 0;
  125. for (int y = 0; y < 24; ++y) {
  126. for (int x = 0; x < 80; ++x) {
  127. placech(' ', x, y, 0x00);
  128. }
  129. }
  130. }
  131. #define print(s) do {if (txt_debug) {print_(s);}} while(0)
  132. #define clear() do {if (txt_debug) {clear_();}} while(0)
  133. #define print_hex(d) do {if (txt_debug) {print_hex_(d);}} while(0)