Files
kernel3/kernel/screen.c
T

122 lines
3.0 KiB
C

#include "../includes/screen.h"
#include "../build_numbers.c"
unsigned char cursor_x = 0;
unsigned char cursor_y = 0;
unsigned char screen_width = 80;
char column = -1;
unsigned char row = 0;
void print_banner(){
unsigned short *buffer = 0x000B8000;
unsigned short attr = 0x1800;
unsigned char* banner = "Project Code Name: kernel3";
char* version = KERNEL_VERSION_NUMBER;
unsigned char banner_length = (80 - strlen(banner)) / 2;
// Add 14 for the string "Kernel Version: ".
unsigned char version_length = (80 - (strlen(version) + 16)) / 2;
for(unsigned char i = 0; i < screen_width; i++){
*buffer++ = ' ';
*buffer++ = attr;
}
for(unsigned char i = 0; i < screen_width; i++){
if(i == banner_length){
for(unsigned short j = 0; j < strlen(banner); j++){
unsigned short *where = (unsigned short*) 0x000B8000 + j + i;
*where = banner[j] | attr;
}
}
if(i == version_length){
unsigned short offset = 0;
char* tmp = "Kernel Version: ";
for(unsigned char j = 0; j < strlen(tmp); j++){
unsigned short *where = (unsigned short*) 0x000B8000 + j + 80 + i;
*where = tmp[j] | attr;
offset++;
}
for(unsigned char j = 0; j < strlen(version); j++){
unsigned short *where = (unsigned short*) 0x000B8000 + j + 80 + offset + i;
*where = version[j] | attr;
}
}
}
cursor_y = 3;
}
//index = (y_value * width_of_screen) + x_value;
void draw_rect(int start_x, int start_y, int end_x, int end_y, unsigned char fg, unsigned char bg){
unsigned char y = end_y - start_y;
unsigned char x = end_x - start_x;
int attr = ((bg << 4) | (fg & 0x0F));
unsigned short *buffer = (unsigned short*) 0x000B8000 + (start_y * 80) + start_x;
for(unsigned char i = 0; i < y; i++){
for(unsigned char j = 0; j < x; j++){
unsigned short *where = (unsigned short*) buffer + (i * 80) + j;
*where = ' ' | (attr << 8);
}
}
}
void draw_string(char* string, unsigned char fg, unsigned char bg)
{
unsigned short *video_buffer = 0x000B8000;
int attr = ((bg << 4) | (fg & 0x0F));
int length = strlen(string);
for(int i = 0; i < length; i++)
{
unsigned char current_char = string[i];
unsigned short *where = (unsigned short *) video_buffer + (cursor_y * 80) + i + cursor_x;
if(current_char == '\n'){
cursor_y++;
cursor_x = 0;
continue;
}
*where = string[i] | attr << 8;
}
if(cursor_x != 0) cursor_x = cursor_x + length + 1;
}
void set_cursor(unsigned char x, unsigned char y){
cursor_x = x;
cursor_y = y;
}
void move_cursor(unsigned char x, unsigned char y){
if(column != -1) draw_rect(cursor_x, cursor_y, cursor_x + 1, cursor_y + 1, 0x2, 0x8);
cursor_x = x;
cursor_y = y;
draw_rect(x, y, x + 1, y + 1, 0x0, 0xF);
}
void clear_screen(){
char *video_buffer = 0x000B8000;
for(unsigned char i = 0; i < 25; i++){
for(unsigned char j = 0; j < 80; j++){
*video_buffer++ = ' ';//0x20 | (0x0F << 8);
*video_buffer++ = 0x82;
}
}
}
int strlen(char* string)
{
int len = 0;
for(int i = 0; string[i] != '\0'; i++) len++;
return len;
}