|
| 1 | +#include <stdbool.h> |
| 2 | +#include <stddef.h> |
| 3 | +#include <stdint.h> |
| 4 | +#include <string.h> |
| 5 | + |
| 6 | +#include <kernel/text.h> |
| 7 | + |
| 8 | +#include "vga.h" |
| 9 | + |
| 10 | +static const size_t VGA_WIDTH = 80; |
| 11 | +static const size_t VGA_HEIGHT = 25; |
| 12 | +static uint16_t* const VGA_MEMORY = (uint16_t*) 0xB8000; |
| 13 | + |
| 14 | +static size_t terminal_row; |
| 15 | +static size_t terminal_column; |
| 16 | +static uint8_t terminal_color; |
| 17 | +static uint16_t* terminal_buffer; |
| 18 | + |
| 19 | +void terminal_initialize(void) { |
| 20 | + terminal_row = 0; |
| 21 | + terminal_column = 0; |
| 22 | + terminal_color = vga_entry_color(VGA_COLOR_LIGHT_GREY, VGA_COLOR_BLACK); |
| 23 | + terminal_buffer = VGA_MEMORY; |
| 24 | + for (size_t y = 0; y < VGA_HEIGHT; y++) { |
| 25 | + for (size_t x = 0; x < VGA_WIDTH; x++) { |
| 26 | + const size_t index = y * VGA_WIDTH + x; |
| 27 | + terminal_buffer[index] = vga_entry(' ', terminal_color); |
| 28 | + } |
| 29 | + } |
| 30 | +} |
| 31 | + |
| 32 | +void terminal_setcolor(uint8_t color) { |
| 33 | + terminal_color = color; |
| 34 | +} |
| 35 | + |
| 36 | +void terminal_putentryat(unsigned char c, uint8_t color, size_t x, size_t y) { |
| 37 | + const size_t index = y * VGA_WIDTH + x; |
| 38 | + terminal_buffer[index] = vga_entry(c, color); |
| 39 | +} |
| 40 | + |
| 41 | +void terminal_scroll(int line) { |
| 42 | + int loop; |
| 43 | + char c; |
| 44 | + |
| 45 | + for(loop = line * (VGA_WIDTH * 2) + 0xB8000; loop < VGA_WIDTH * 2; loop++) { |
| 46 | + c = *loop; |
| 47 | + *(loop - (VGA_WIDTH * 2)) = c; |
| 48 | + } |
| 49 | +} |
| 50 | + |
| 51 | +void terminal_delete_last_line() { |
| 52 | + int x, *ptr; |
| 53 | + |
| 54 | + for(x = 0; x < VGA_WIDTH * 2; x++) { |
| 55 | + ptr = 0xB8000 + (VGA_WIDTH * 2) * (VGA_HEIGHT - 1) + x; |
| 56 | + *ptr = 0; |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | +void putchar(char c) { |
| 61 | + int line; |
| 62 | + unsigned char uc = c; |
| 63 | + |
| 64 | + terminal_putentryat(uc, terminal_color, terminal_column, terminal_row); |
| 65 | + if (++terminal_column == VGA_WIDTH) { |
| 66 | + terminal_column = 0; |
| 67 | + if (++terminal_row == VGA_HEIGHT) |
| 68 | + { |
| 69 | + for(line = 1; line <= VGA_HEIGHT - 1; line++) |
| 70 | + { |
| 71 | + terminal_scroll(line); |
| 72 | + } |
| 73 | + terminal_delete_last_line(); |
| 74 | + terminal_row = VGA_HEIGHT - 1; |
| 75 | + } |
| 76 | + } |
| 77 | +} |
| 78 | + |
| 79 | +void write(const char* data, size_t size) { |
| 80 | + for (size_t i = 0; i < size; i++) |
| 81 | + terminal_putchar(data[i]); |
| 82 | +} |
| 83 | + |
| 84 | +void writestring(const char* data) { |
| 85 | + terminal_write(data, strlen(data)); |
| 86 | +} |
0 commit comments