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