screen.c 4.6 KB

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