screen.c 4.3 KB

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