screen.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. #include "screen.h"
  2. #include "../drivers/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. /**********************************************************
  40. * Private kernel functions *
  41. **********************************************************/
  42. /**
  43. * Innermost print function for our kernel, directly accesses the video memory
  44. *
  45. * If 'col' and 'row' are negative, we will print at current cursor location
  46. * If 'attr' is zero it will use 'white on black' as default
  47. * Returns the offset of the next character
  48. * Sets the video cursor to the returned offset
  49. */
  50. int print_char(char c, int col, int row, char attr) {
  51. unsigned char *vidmem = (unsigned char*) VIDEO_ADDRESS;
  52. if (!attr) attr = WHITE_ON_BLACK;
  53. /* Error control: print a red 'E' if the coords aren't right */
  54. if (col >= MAX_COLS || row >= MAX_ROWS) {
  55. vidmem[2*(MAX_COLS)*(MAX_ROWS)-2] = 'E';
  56. vidmem[2*(MAX_COLS)*(MAX_ROWS)-1] = RED_ON_WHITE;
  57. return get_offset(col, row);
  58. }
  59. int offset;
  60. if (col >= 0 && row >= 0) offset = get_offset(col, row);
  61. else offset = get_cursor_offset();
  62. if (c == '\n') {
  63. row = get_offset_row(offset);
  64. offset = get_offset(0, row+1);
  65. } else {
  66. vidmem[offset] = c;
  67. vidmem[offset+1] = attr;
  68. offset += 2;
  69. }
  70. /* Check if the offset is over screen size and scroll */
  71. if (offset >= MAX_ROWS * MAX_COLS * 2) {
  72. int i;
  73. for (i = 1; i < MAX_ROWS; i++)
  74. memory_copy(get_offset(0, i) + VIDEO_ADDRESS,
  75. get_offset(0, i-1) + VIDEO_ADDRESS,
  76. MAX_COLS * 2);
  77. /* Blank last line */
  78. char *last_line = get_offset(0, MAX_ROWS-1) + VIDEO_ADDRESS;
  79. for (i = 0; i < MAX_COLS * 2; i++) last_line[i] = 0;
  80. offset -= 2 * MAX_COLS;
  81. }
  82. set_cursor_offset(offset);
  83. return offset;
  84. }
  85. int get_cursor_offset() {
  86. /* Use the VGA ports to get the current cursor position
  87. * 1. Ask for high byte of the cursor offset (data 14)
  88. * 2. Ask for low byte (data 15)
  89. */
  90. port_byte_out(REG_SCREEN_CTRL, 14);
  91. int offset = port_byte_in(REG_SCREEN_DATA) << 8; /* High byte: << 8 */
  92. port_byte_out(REG_SCREEN_CTRL, 15);
  93. offset += port_byte_in(REG_SCREEN_DATA);
  94. return offset * 2; /* Position * size of character cell */
  95. }
  96. void set_cursor_offset(int offset) {
  97. /* Similar to get_cursor_offset, but instead of reading we write data */
  98. offset /= 2;
  99. port_byte_out(REG_SCREEN_CTRL, 14);
  100. port_byte_out(REG_SCREEN_DATA, (unsigned char)(offset >> 8));
  101. port_byte_out(REG_SCREEN_CTRL, 15);
  102. port_byte_out(REG_SCREEN_DATA, (unsigned char)(offset & 0xff));
  103. }
  104. void clear_screen() {
  105. int screen_size = MAX_COLS * MAX_ROWS;
  106. int i;
  107. char *screen = VIDEO_ADDRESS;
  108. for (i = 0; i < screen_size; i++) {
  109. screen[i*2] = ' ';
  110. screen[i*2+1] = WHITE_ON_BLACK;
  111. }
  112. set_cursor_offset(get_offset(0, 0));
  113. }
  114. int get_offset(int col, int row) { return 2 * (row * MAX_COLS + col); }
  115. int get_offset_row(int offset) { return offset / (2 * MAX_COLS); }
  116. int get_offset_col(int offset) { return (offset - (get_offset_row(offset)*2*MAX_COLS))/2; }