Working TTY

This commit is contained in:
Gitea 2020-12-01 21:00:28 -06:00
parent 094f93bcd6
commit 832aa58bb7
3 changed files with 127 additions and 0 deletions

82
arch/i386/tty.c Executable file
View File

@ -0,0 +1,82 @@
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <string.h>
#include <kernel/tty.h>
#include "vga.h"
static const size_t VGA_WIDTH = 80;
static const size_t VGA_HEIGHT = 24;
static uint16_t* const VGA_MEMORY = (uint16_t*) 0xB8000 ;
static size_t term_row;
static size_t term_col;
static uint8_t term_color;
static uint16_t* term_buf;
void term_init(void) {
term_row = 0;
term_col = 0;
term_color = vga_entry_color(VGA_COLOR_LIGHT_GREY, VGA_COLOR_BLACK);
term_buf = VGA_MEMORY;
for(size_t y = 0; y < VGA_HEIGHT; y++) {
for(size_t x = 0; x < VGA_WIDTH; x++) {
const size_t ind = y * VGA_WIDTH + x;
term_buf[ind] = vga_entry(' ', term_color);
}
}
}
void term_scroll(void) {
for(size_t y = 0; y < VGA_HEIGHT; y++) {
for(size_t x = 0; x < VGA_WIDTH; x++) {
const size_t ind = y * VGA_WIDTH + x;
term_buf[ind] = term_buf[(y+1) * VGA_WIDTH + x];
}
}
for(size_t x = 0; x < VGA_WIDTH; x++) {
const size_t ind = VGA_HEIGHT * VGA_WIDTH + x;
term_buf[ind] = vga_entry(' ', term_color);
}
}
void term_setcolor(uint8_t color) {
term_color = color;
}
void term_putentryat(unsigned char c, uint8_t color, size_t x, size_t y) {
const size_t ind = y * VGA_WIDTH + x;
term_buf[ind] = vga_entry(c, color);
}
void term_putc(char c) {
unsigned char uc = c;
if(term_row > VGA_HEIGHT) {
term_scroll();
term_row--;
}
switch(uc) {
case '\n': term_row++; term_col = 0; break;
default: term_putentryat(uc, term_color, term_col, term_row); break;
}
if(uc != '\n' && ++term_col == VGA_WIDTH) {
term_col = 0;
if(++term_row == VGA_HEIGHT) {
term_scroll();
}
}
}
void term_write(const char* str, size_t size) {
for(size_t i = 0; i < size; i++) {
term_putc(str[i]);
}
}
void term_writestr(const char* str) {
term_write(str, strlen(str));
}

33
arch/i386/vga.h Executable file
View File

@ -0,0 +1,33 @@
#ifndef _ARCH_I386_VGA_H
#define _ARCH_I386_VGA_H
#include <stdint.h>
enum vga_color {
VGA_COLOR_BLACK = 0,
VGA_COLOR_BLUE= 1,
VGA_COLOR_GREEN = 2,
VGA_COLOR_CYAN = 3,
VGA_COLOR_RED = 4,
VGA_COLOR_MAGENTA = 5,
VGA_COLOR_BROWN = 6,
VGA_COLOR_LIGHT_GREY = 7,
VGA_COLOR_DARK_GREY = 8,
VGA_COLOR_LIGHT_BLUE = 9,
VGA_COLOR_LIGHT_GREEN = 10,
VGA_COLOR_LIGHT_CYAN = 11,
VGA_COLOR_LIGHT_RED = 12,
VGA_COLOR_LIGHT_MAGENTA = 13,
VGA_COLOR_LIGHT_BROWN = 14,
VGA_COLOR_WHITE = 15,
};
static inline uint8_t vga_entry_color(enum vga_color fg, enum vga_color bg) {
return fg | bg << 4;
}
static inline uint16_t vga_entry(unsigned char uc, uint8_t color) {
return (uint16_t) uc | (uint16_t) color << 8;
}
#endif

12
include/kernel/tty.h Executable file
View File

@ -0,0 +1,12 @@
#ifndef _KERN_TTY_H
#define _KERN_TTY_H
#include <stddef.h>
void term_init(void);
void term_scroll(void);
void term_putc(char c);
void term_write(const char* str, size_t size);
void term_writestr(const char* str);
#endif