Syncboot/arch/i386/tty.c

88 lines
2.2 KiB
C
Executable File

#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <string.h>
#include <kernel/tty.h>
#include <kernel/utils.h>
static const size_t TERM_WIDTH=80;
static const size_t TERM_HEIGHT=25;
static size_t terminal_row;
static size_t terminal_column;
static uint8_t current_color;
static uint16_t* term_buffer;
static uint16_t* const vga_buffer = (uint16_t*) 0xB8000;
void screen_initialize(void) {
terminal_row = 0;
terminal_column = 0;
current_color = vga_color_set(LIGHT_GREY, BLACK);
term_buffer = vga_buffer;
for (size_t y = 0; y < TERM_HEIGHT; y++) {
for(size_t x = 0; x < TERM_WIDTH; x++) {
const size_t offset = y * TERM_WIDTH + x;
term_buffer[offset] = vga_entry(' ', current_color);
}
}
}
void term_setcolor(enum vga_colors color) {
current_color = color;
}
void term_putentryat (char c, uint8_t color, size_t x, size_t y) {
const size_t offset = y * TERM_WIDTH + x;
term_buffer[offset] = vga_entry(c, color);
}
void term_putchar(char c) {
unsigned char uc = c;
term_putentryat(uc, current_color, terminal_column, terminal_row);
if(++terminal_column == TERM_WIDTH)
{
terminal_column = 0;
if(++terminal_row == TERM_HEIGHT){
term_scroll();
terminal_row = 0;
}
}
}
void term_write(const char* data, size_t size) {
for(size_t i = 0; i < size; i++)
term_putchar(data[i]);
}
void puts(const char* string) {
term_write(string, strlen(string));
term_putchar('\n');
}
void term_scroll() {
int current_pos = 160; //Start of the second line.
unsigned char* term_buffer = (unsigned char*) vga_buffer;
while (current_pos <= 4000) {
term_buffer[current_pos - 160 /*The character immediately below it*/] = vga_buffer[current_pos]; //Move each character up a line
term_buffer[current_pos - 159 /*The color of the character below*/] = vga_buffer[current_pos + 1]; //As well as its color
current_pos += 2; //Move to next char
}
current_pos = 3840; //Start of the last line
int i; //Offset for framebuffer
//Wipe out the bottom line
for(i = 1920; i <= 2000; i++) {
term_buffer[current_pos] = 0;
current_pos += 2;
}
i = 3840;
terminal_row = 24; //Prepare the screen to scroll
}