2021-05-02 17:38:18 +00:00
|
|
|
#include "kernel.h"
|
|
|
|
#include "kterm.h"
|
2021-05-02 16:02:00 +00:00
|
|
|
|
|
|
|
|
2021-05-02 17:38:18 +00:00
|
|
|
/**
|
|
|
|
* simple delay function
|
|
|
|
**/
|
|
|
|
void delay(int t){
|
|
|
|
volatile int i,j;
|
|
|
|
for(i=0;i<t;i++)
|
|
|
|
for(j=0;j<25000;j++)
|
|
|
|
asm("NOP");
|
|
|
|
}
|
|
|
|
|
|
|
|
void kernel_main (void) {
|
|
|
|
/** initialize terminal interface */
|
|
|
|
kterm_init();
|
|
|
|
|
|
|
|
/** Wrtite stuff to the screen to test the terminal**/
|
|
|
|
kterm_writestring("Hello world!\n");
|
|
|
|
kterm_writestring("We got newline support!\n");
|
|
|
|
|
|
|
|
for(;;){
|
|
|
|
delay(500);
|
|
|
|
kterm_writestring("We have implemented terminal scrolling!\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* KTerm stuff
|
|
|
|
**/
|
2021-05-02 16:02:00 +00:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2021-05-02 17:38:18 +00:00
|
|
|
void kterm_init () {
|
2021-05-02 16:02:00 +00:00
|
|
|
kterm_row = 0;
|
|
|
|
kterm_column = 0;
|
|
|
|
kterm_color = vga_entry_color ( VGA_COLOR_LIGHT_GREY , VGA_COLOR_BLACK);
|
|
|
|
kterm_buffer = (uint16_t*) 0xB8000;
|
|
|
|
for (size_t y = 0; y < VGA_HEIGHT; y++ ){
|
|
|
|
for( size_t x = 0; x < VGA_WIDTH; x++){
|
|
|
|
const size_t index = y * VGA_WIDTH + x;
|
|
|
|
kterm_buffer[index] = vga_entry(' ', kterm_color);
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-05-02 17:38:18 +00:00
|
|
|
void kterm_resetcolor(){
|
|
|
|
kterm_color = vga_entry_color ( VGA_COLOR_LIGHT_GREY , VGA_COLOR_BLACK);
|
|
|
|
}
|
|
|
|
|
2021-05-02 16:02:00 +00:00
|
|
|
void kterm_setcolor(uint8_t color){
|
|
|
|
kterm_color = color;
|
|
|
|
}
|
|
|
|
|
|
|
|
void kterm_putat (char c, uint8_t color, size_t x, size_t y ) {
|
|
|
|
const size_t index = y * VGA_WIDTH + x;
|
|
|
|
|
|
|
|
kterm_buffer[index] = vga_entry(c, color);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2021-05-02 17:14:31 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* With the help from:
|
|
|
|
* https://whiteheadsoftware.dev/operating-systems-development-for-dummies/
|
|
|
|
**/
|
|
|
|
void kterm_scrollup(){
|
|
|
|
size_t i ;
|
|
|
|
for(i=0; i < (VGA_WIDTH * VGA_HEIGHT - VGA_WIDTH); i++)
|
|
|
|
kterm_buffer[i] = kterm_buffer[i+VGA_WIDTH];
|
|
|
|
|
|
|
|
for( i=0; i< VGA_WIDTH; i++)
|
|
|
|
kterm_buffer[(VGA_HEIGHT -1) * VGA_WIDTH + i ] = vga_entry(' ', kterm_color);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2021-05-02 16:02:00 +00:00
|
|
|
void kterm_put (char c) {
|
2021-05-02 17:14:31 +00:00
|
|
|
|
|
|
|
if(++kterm_column == VGA_WIDTH || c == '\n' ) {
|
2021-05-02 16:04:15 +00:00
|
|
|
kterm_column = 0;
|
2021-05-02 17:14:31 +00:00
|
|
|
if(kterm_row == VGA_HEIGHT-1) {
|
|
|
|
kterm_scrollup();
|
|
|
|
} else {
|
|
|
|
kterm_row ++;
|
|
|
|
}
|
|
|
|
|
2021-05-02 16:04:15 +00:00
|
|
|
}
|
2021-05-02 17:14:31 +00:00
|
|
|
|
|
|
|
if(c == '\n') return;
|
2021-05-02 16:02:00 +00:00
|
|
|
kterm_putat ( c, kterm_color, kterm_column, kterm_row);
|
2021-05-02 17:14:31 +00:00
|
|
|
|
2021-05-02 16:02:00 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void kterm_write(const char* data, size_t size) {
|
|
|
|
for(size_t i = 0; i < size; i++){
|
|
|
|
kterm_put(data[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void kterm_writestring(const char* data ){
|
2021-05-02 17:38:18 +00:00
|
|
|
AS_KERNEL();
|
2021-05-02 16:02:00 +00:00
|
|
|
kterm_write(data, strlen(data));
|
2021-05-02 17:38:18 +00:00
|
|
|
}
|