tty.c (2080B)
1 #include "oxytoxin.h" 2 3 size_t tty_row; 4 size_t tty_column; 5 uint8_t tty_color; 6 uint16_t* tty_buffer = (uint16_t*)VGA_START; 7 8 void tty_set_color(uint8_t color) { 9 tty_color = color; 10 } 11 12 void tty_putentryat(char c, uint8_t color, size_t x, size_t y) { 13 size_t index; 14 15 index = y * VGA_WIDTH + x; 16 tty_buffer[index] = vga_entry(c, color); 17 } 18 19 void tty_clear(void) { 20 size_t x; 21 size_t y; 22 size_t index; 23 24 tty_row = 0; 25 tty_column = 0; 26 27 for (y = 0; y < VGA_HEIGHT; y++) { 28 for (x = 0; x < VGA_WIDTH; x++) { 29 index = y * VGA_WIDTH + x; 30 tty_buffer[index] = vga_entry(' ', tty_color); 31 } 32 } 33 vga_cursor_update(tty_column, tty_row); 34 } 35 36 void tty_init(void) { 37 vga_cursor_enable(14, 15); 38 tty_color = vga_entry_color(VGA_COLOR_LIGHT_GREY, VGA_COLOR_BLACK); 39 tty_clear(); 40 } 41 42 void tty_scroll(void) { 43 int row; 44 int col; 45 size_t src_index; 46 size_t dst_index; 47 uint16_t blank; 48 size_t index; 49 50 /* move lines to line-1 */ 51 for (row = 1; row < VGA_HEIGHT; row++) { 52 for (col = 0; col < VGA_WIDTH; col++) { 53 src_index = row * VGA_WIDTH + col; 54 dst_index = (row - 1) * VGA_WIDTH + col; 55 tty_buffer[dst_index] = tty_buffer[src_index]; 56 } 57 } 58 /* clear last line*/ 59 blank = ' ' | (tty_color << 8); 60 for (col = 0; col < VGA_WIDTH; col++) { 61 index = (VGA_HEIGHT - 1) * VGA_WIDTH + col; 62 tty_buffer[index] = blank; 63 } 64 } 65 66 void tty_putchar(char c) { 67 if(c == '\n') { 68 tty_row++; 69 tty_column = 0; 70 71 if (tty_row >= VGA_HEIGHT) { 72 tty_scroll(); 73 tty_row = VGA_HEIGHT - 1; 74 } 75 } else if(c == '\b') { 76 if (tty_column > 0) { 77 tty_column--; 78 tty_putentryat(' ', tty_color, tty_column, tty_row); 79 } 80 } else { 81 tty_putentryat(c, tty_color, tty_column, tty_row); 82 if (++tty_column == VGA_WIDTH) { 83 tty_column = 0; 84 if (++tty_row == VGA_HEIGHT) { 85 tty_scroll(); 86 tty_row = VGA_HEIGHT - 1; 87 } 88 } 89 } 90 91 vga_cursor_update(tty_column, tty_row); 92 } 93 94 void tty_write(const char* data, size_t size) { 95 size_t i; 96 97 for (i = 0; i < size; i++) { 98 tty_putchar(data[i]); 99 } 100 }