screen.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. #include "screen.h"
  2. #include "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. set_cursor_offset(offset);
  71. return offset;
  72. }
  73. int get_cursor_offset() {
  74. /* Use the VGA ports to get the current cursor position
  75. * 1. Ask for high byte of the cursor offset (data 14)
  76. * 2. Ask for low byte (data 15)
  77. */
  78. port_byte_out(REG_SCREEN_CTRL, 14);
  79. int offset = port_byte_in(REG_SCREEN_DATA) << 8; /* High byte: << 8 */
  80. port_byte_out(REG_SCREEN_CTRL, 15);
  81. offset += port_byte_in(REG_SCREEN_DATA);
  82. return offset * 2; /* Position * size of character cell */
  83. }
  84. void set_cursor_offset(int offset) {
  85. /* Similar to get_cursor_offset, but instead of reading we write data */
  86. offset /= 2;
  87. port_byte_out(REG_SCREEN_CTRL, 14);
  88. port_byte_out(REG_SCREEN_DATA, (unsigned char)(offset >> 8));
  89. port_byte_out(REG_SCREEN_CTRL, 15);
  90. port_byte_out(REG_SCREEN_DATA, (unsigned char)(offset & 0xff));
  91. }
  92. void clear_screen() {
  93. int screen_size = MAX_COLS * MAX_ROWS;
  94. int i;
  95. char *screen = VIDEO_ADDRESS;
  96. for (i = 0; i < screen_size; i++) {
  97. screen[i*2] = ' ';
  98. screen[i*2+1] = WHITE_ON_BLACK;
  99. }
  100. set_cursor_offset(get_offset(0, 0));
  101. }
  102. int get_offset(int col, int row) { return 2 * (row * MAX_COLS + col); }
  103. int get_offset_row(int offset) { return offset / (2 * MAX_COLS); }
  104. int get_offset_col(int offset) { return (offset - (get_offset_row(offset)*2*MAX_COLS))/2; }