bios.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. #include <stdbool.h>
  2. #include <stddef.h>
  3. #include <stdint.h>
  4. #include <stdio.h>
  5. #include <string.h>
  6. static void* heap_end;
  7. /* Hardware text mode color constants. */
  8. enum vga_color {
  9. COLOR_BLACK = 0,
  10. COLOR_BLUE = 1,
  11. COLOR_GREEN = 2,
  12. COLOR_CYAN = 3,
  13. COLOR_RED = 4,
  14. COLOR_MAGENTA = 5,
  15. COLOR_BROWN = 6,
  16. COLOR_LIGHT_GREY = 7,
  17. COLOR_DARK_GREY = 8,
  18. COLOR_LIGHT_BLUE = 9,
  19. COLOR_LIGHT_GREEN = 10,
  20. COLOR_LIGHT_CYAN = 11,
  21. COLOR_LIGHT_RED = 12,
  22. COLOR_LIGHT_MAGENTA = 13,
  23. COLOR_LIGHT_BROWN = 14,
  24. COLOR_WHITE = 15,
  25. };
  26. uint8_t make_color(enum vga_color fg, enum vga_color bg) {
  27. return fg | bg << 4;
  28. }
  29. uint16_t make_vgaentry(char c, uint8_t color) {
  30. uint16_t c16 = c;
  31. uint16_t color16 = color;
  32. return c16 | color16 << 8;
  33. }
  34. size_t strlen(const char* str) {
  35. size_t ret = 0;
  36. while ( str[ret] != 0 )
  37. ret++;
  38. return ret;
  39. }
  40. static const size_t VGA_WIDTH = 80;
  41. static const size_t VGA_HEIGHT = 25;
  42. size_t terminal_row;
  43. size_t terminal_column;
  44. uint8_t terminal_color;
  45. uint16_t* terminal_buffer;
  46. void terminal_initialize() {
  47. terminal_row = 0;
  48. terminal_column = 0;
  49. terminal_color = make_color(COLOR_LIGHT_GREY, COLOR_BLACK);
  50. terminal_buffer = (uint16_t*) 0xB8000;
  51. for (size_t y = 0; y < VGA_HEIGHT; y++) {
  52. for (size_t x = 0; x < VGA_WIDTH; x++) {
  53. const size_t index = y * VGA_WIDTH + x;
  54. terminal_buffer[index] = make_vgaentry(' ', terminal_color);
  55. }
  56. }
  57. }
  58. void terminal_setcolor(uint8_t color) {
  59. terminal_color = color;
  60. }
  61. void terminal_putentryat(char c, uint8_t color, size_t x, size_t y) {
  62. const size_t index = y * VGA_WIDTH + x;
  63. terminal_buffer[index] = make_vgaentry(c, color);
  64. }
  65. void terminal_putchar(char c) {
  66. if (c==10) {
  67. terminal_column=0;
  68. terminal_row++;
  69. if (terminal_row == VGA_HEIGHT) {
  70. terminal_initialize();
  71. }
  72. return;
  73. }
  74. terminal_putentryat(c, terminal_color, terminal_column, terminal_row);
  75. if (++terminal_column == VGA_WIDTH) {
  76. terminal_column = 0;
  77. if (++terminal_row == VGA_HEIGHT) {
  78. terminal_row = 0;
  79. }
  80. }
  81. }
  82. void terminal_writestring(const char* data) {
  83. size_t datalen = strlen(data);
  84. for (size_t i = 0; i < datalen; i++)
  85. terminal_putchar(data[i]);
  86. }
  87. int bios_getc(void)
  88. {
  89. uint16_t key_to_rune[] = {
  90. 0,
  91. 0,
  92. '1','2','3','4','5','6','7','8','9','0','/','"',9,
  93. '\t','q','w','e','r','t','z','u','i','o','p','-','+',10,
  94. 0,'a','s','d','f','g','h','j','k','l','(',')',0,0,'*',
  95. 'y','x','c','v','b','n','m',',','.','-',0,
  96. 0,0,' ',
  97. 0,0,0,0,0,0,0,0,0,0,
  98. 0,0,0,0,0,0,0,0,0,0,
  99. 0,0,0,0,0,0,0,0,0,0,
  100. 0,0,0,0,0,0,0,0,0,0,
  101. 0,0,0,0,0,0,0,0,0,0,
  102. 0,0,0,0,0,0,0,0,0,0,
  103. 0,0,0,0,0,0,0,0,0,0,
  104. 0,0,0,0,0,0,0,0,0,0,
  105. 0,0,0,0,0,0,0,0,0,0,
  106. 0,0,0,0,0,0,0,0,0,0
  107. };
  108. int key;
  109. __asm("WaitLoop: in $0x64, %al");
  110. __asm( "and $1, %al");
  111. __asm( "jz WaitLoop");
  112. __asm( "in $0x60, %al");
  113. __asm("mov %%al,%0" : "=m" (key));
  114. if (key>0 && key<0x70) {
  115. return(key_to_rune[key]);
  116. } else {
  117. return 0;
  118. }
  119. }
  120. void uart_puts(char* str) {
  121. terminal_writestring(str);
  122. }
  123. void uart_putc(char c) {
  124. terminal_putchar(c);
  125. }
  126. char uart_getc() {
  127. return bios_getc();
  128. }
  129. int open() {
  130. uart_puts("-- clib open called. stubbed.\r\n");
  131. return 1;
  132. }
  133. int close() {
  134. uart_puts("-- clib close called. stubbed.\r\n");
  135. return 1;
  136. }
  137. int fstat() {
  138. //uart_puts("-- clib fstat called. stubbed.\n");
  139. return 0;
  140. }
  141. int lseek() {
  142. //uart_puts("-- clib lseek called. stubbed.\n");
  143. return 0;
  144. }
  145. ssize_t read(int fildes, void *buf, size_t nbyte) {
  146. //uart_puts("-- clib read called. stubbed.\n");
  147. printf("\r\n");
  148. int i = 0;
  149. int k = 0;
  150. while (k!=10 && i<nbyte) {
  151. k = uart_getc();
  152. if (k>0) {
  153. uart_putc(k);
  154. ((char*)buf)[i] = k;
  155. i++;
  156. }
  157. }
  158. return i;
  159. }
  160. size_t write(int fildes, const void *buf, size_t nbytes) {
  161. uart_puts("-- clib write called:\n");
  162. for (int i=0; i<nbytes; i++) {
  163. uart_putc(((char*)buf)[i]);
  164. }
  165. return nbytes;
  166. }
  167. #define GETLINE_MAX 256
  168. static char getline_buf[GETLINE_MAX];
  169. ssize_t getline(char **lineptr, size_t *n, FILE *stream) {
  170. printf("\r\n");
  171. int i = 0;
  172. int k = 0;
  173. while (k!=10 && i<GETLINE_MAX) {
  174. k = uart_getc();
  175. if (k>0) {
  176. uart_putc(k);
  177. getline_buf[i] = k;
  178. i++;
  179. }
  180. }
  181. *n = i;
  182. *lineptr = getline_buf;
  183. return i;
  184. }
  185. #include "debug_util.c"
  186. #include "libc_glue.c"
  187. int main(int argc, char *argv[]);
  188. void kernel_main() {
  189. terminal_buffer = (uint16_t*)0xB8000;
  190. for (int i=0; i<80*23; i++) {
  191. terminal_buffer[i]='*'|(COLOR_WHITE << 8);
  192. }
  193. /* Initialize terminal interface */
  194. terminal_initialize();
  195. heap_end = (void*)0x200000;
  196. main(0,NULL);
  197. }
  198. /*extern _IO_ssize_t getline(char **__restrict __lineptr, size_t *__restrict __n, FILE *__restrict __stream) {
  199. return 0;
  200. }
  201. */