trios/kernel/term.c

64 lines
1.0 KiB
C

#include <stddef.h>
#include <stdint.h>
#include <string.h>
#include "term.h"
static uint16_t* screen = (uint16_t*)0xb8000;
static size_t x = 0;
static size_t y = 0;
static uint8_t color = 0x0f;
static inline uint16_t screen_entry(char c, uint8_t col) {
return (uint16_t)c | ((uint16_t)col << 8);
}
void putc(char c) {
switch(c) {
case '\n':
x = TERM_W;
break;
case '\t':
if(x % 8 == 0) x += 8;
x += 7 - (x % 8);
break;
default:
screen[x + y * TERM_W] = screen_entry(c, color);
x += 1;
break;
}
if(x >= TERM_W) {
x = 0;
y += 1;
}
}
void puts(char* s) {
for(char* c = s; *c != '\0'; c++) {
putc(*c);
}
}
void term_clear(void) {
memset(screen, 0, TERM_W * TERM_H * 2);
}
void term_setpos(size_t new_x, size_t new_y) {
x = new_x;
y = new_y;
}
void term_setcol(uint8_t new_col) {
color = new_col;
}
uint32_t term_save(void) {
return color | ((x + y * TERM_W) << 8);
}
void term_load(uint32_t state) {
color = state & 0xff;
state >>= 8;
x = state % TERM_W;
y = state / TERM_W;
}