screen.c 4.6 KB

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