screen.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. #include "screen.h"
  2. #include "../cpu/ports.h"
  3. #include "../libc/mem.h"
  4. #include <stdint.h>
  5. /* Declaration of private functions */
  6. int get_cursor_offset();
  7. void set_cursor_offset(int offset);
  8. int print_char(char c, int col, int row, char attr);
  9. int get_offset(int col, int row);
  10. int get_offset_row(int offset);
  11. int get_offset_col(int offset);
  12. /**********************************************************
  13. * Public Kernel API functions *
  14. **********************************************************/
  15. /**
  16. * Print a message on the specified location
  17. * If col, row, are negative, we will use the current offset
  18. */
  19. void kprint_at(char *message, int col, int row) {
  20. /* Set cursor if col/row are negative */
  21. int offset;
  22. if (col >= 0 && row >= 0)
  23. offset = get_offset(col, row);
  24. else {
  25. offset = get_cursor_offset();
  26. row = get_offset_row(offset);
  27. col = get_offset_col(offset);
  28. }
  29. /* Loop through message and print it */
  30. int i = 0;
  31. while (message[i] != 0) {
  32. offset = print_char(message[i++], col, row, WHITE_ON_BLACK);
  33. /* Compute row/col for next iteration */
  34. row = get_offset_row(offset);
  35. col = get_offset_col(offset);
  36. }
  37. }
  38. void kprint(char *message) {
  39. kprint_at(message, -1, -1);
  40. }
  41. void kprint_backspace() {
  42. int offset = get_cursor_offset()-2;
  43. int row = get_offset_row(offset);
  44. int col = get_offset_col(offset);
  45. print_char(0x08, col, row, WHITE_ON_BLACK);
  46. }
  47. /**********************************************************
  48. * Private kernel functions *
  49. **********************************************************/
  50. /**
  51. * Innermost print function for our kernel, directly accesses the video memory
  52. *
  53. * If 'col' and 'row' are negative, we will print at current cursor location
  54. * If 'attr' is zero it will use 'white on black' as default
  55. * Returns the offset of the next character
  56. * Sets the video cursor to the returned offset
  57. */
  58. int print_char(char c, int col, int row, char attr) {
  59. uint8_t *vidmem = (uint8_t*) VIDEO_ADDRESS;
  60. if (!attr) attr = WHITE_ON_BLACK;
  61. /* Error control: print a red 'E' if the coords aren't right */
  62. if (col >= MAX_COLS || row >= MAX_ROWS) {
  63. vidmem[2*(MAX_COLS)*(MAX_ROWS)-2] = 'E';
  64. vidmem[2*(MAX_COLS)*(MAX_ROWS)-1] = RED_ON_WHITE;
  65. return get_offset(col, row);
  66. }
  67. int offset;
  68. if (col >= 0 && row >= 0) offset = get_offset(col, row);
  69. else offset = get_cursor_offset();
  70. if (c == '\n') {
  71. row = get_offset_row(offset);
  72. offset = get_offset(0, row+1);
  73. } else if (c == 0x08) { /* Backspace */
  74. vidmem[offset] = ' ';
  75. vidmem[offset+1] = attr;
  76. } else {
  77. vidmem[offset] = c;
  78. vidmem[offset+1] = attr;
  79. offset += 2;
  80. }
  81. /* Check if the offset is over screen size and scroll */
  82. if (offset >= MAX_ROWS * MAX_COLS * 2) {
  83. int i;
  84. for (i = 1; i < MAX_ROWS; i++)
  85. memory_copy((uint8_t*)(get_offset(0, i) + VIDEO_ADDRESS),
  86. (uint8_t*)(get_offset(0, i-1) + VIDEO_ADDRESS),
  87. MAX_COLS * 2);
  88. /* Blank last line */
  89. char *last_line = (char*) (get_offset(0, MAX_ROWS-1) + (uint8_t*) VIDEO_ADDRESS);
  90. for (i = 0; i < MAX_COLS * 2; i++) last_line[i] = 0;
  91. offset -= 2 * MAX_COLS;
  92. }
  93. set_cursor_offset(offset);
  94. return offset;
  95. }
  96. int get_cursor_offset() {
  97. /* Use the VGA ports to get the current cursor position
  98. * 1. Ask for high byte of the cursor offset (data 14)
  99. * 2. Ask for low byte (data 15)
  100. */
  101. port_byte_out(REG_SCREEN_CTRL, 14);
  102. int offset = port_byte_in(REG_SCREEN_DATA) << 8; /* High byte: << 8 */
  103. port_byte_out(REG_SCREEN_CTRL, 15);
  104. offset += port_byte_in(REG_SCREEN_DATA);
  105. return offset * 2; /* Position * size of character cell */
  106. }
  107. void set_cursor_offset(int offset) {
  108. /* Similar to get_cursor_offset, but instead of reading we write data */
  109. offset /= 2;
  110. port_byte_out(REG_SCREEN_CTRL, 14);
  111. port_byte_out(REG_SCREEN_DATA, (uint8_t)(offset >> 8));
  112. port_byte_out(REG_SCREEN_CTRL, 15);
  113. port_byte_out(REG_SCREEN_DATA, (uint8_t)(offset & 0xff));
  114. }
  115. void clear_screen() {
  116. int screen_size = MAX_COLS * MAX_ROWS;
  117. int i;
  118. uint8_t *screen = (uint8_t*) VIDEO_ADDRESS;
  119. for (i = 0; i < screen_size; i++) {
  120. screen[i*2] = ' ';
  121. screen[i*2+1] = WHITE_ON_BLACK;
  122. }
  123. set_cursor_offset(get_offset(0, 0));
  124. }
  125. int get_offset(int col, int row) { return 2 * (row * MAX_COLS + col); }
  126. int get_offset_row(int offset) { return offset / (2 * MAX_COLS); }
  127. int get_offset_col(int offset) { return (offset - (get_offset_row(offset)*2*MAX_COLS))/2; }