view cos/kernel/video.c @ 23:5dd47d6eebac

Added ubersimple malloc algorithm
author windel
date Thu, 01 Dec 2011 21:42:59 +0100
parents 6129643f5c34
children d8627924d40d
line wrap: on
line source

#include "kernel.h"

#define VIDMEM              0xB8000

#define NUM_COLS 80
#define NUM_ROWS 25
#define SCREEN_SIZE (NUM_COLS * NUM_ROWS)

static int row, col;
uint8_t video_color = 0x1F;

void move_cursor()
{
   uint16_t cursorLocation = row * 80 + col;
   outb(0x3D4, 14);
   outb(0x3D5, cursorLocation >> 8);
   outb(0x3D4, 15);
   outb(0x3D5, cursorLocation & 0xFF);
}

void
clear_screen()
{
	unsigned char *vidmem = (unsigned char *) VIDMEM;
	int loop;

	for (loop = 0; loop < SCREEN_SIZE; loop++) {
		*vidmem++ = 0x20;
		*vidmem++ = video_color;
	}
}

void
init_screen()
{
	row = col = 0;
	clear_screen();
   move_cursor();
}


static void
scroll_screen()
{
	unsigned short* v;
	int i;
	int n = SCREEN_SIZE;
	
	for (v = (unsigned short*) VIDMEM, i = 0; i < n; i++ ) {
		*v = *(v + NUM_COLS);
		++v;
	}

	for (v = (unsigned short*) VIDMEM + n, i = 0; i < NUM_COLS; i++) {
		*v++ = (video_color << 8) & 0x20;
	}
}

static void
new_line()
{
	++row;
	col = 0;
	if (row == NUM_ROWS) {
		scroll_screen();
		row = NUM_ROWS - 1;
	}
}

static void
clear_to_EOL()
{
	int loop;
	unsigned char *v = (unsigned char *) ((uint64_t)(VIDMEM + row * NUM_COLS * 2 + col * 2));

	for (loop = col; loop < NUM_COLS; loop++) {
		*v++ = ' ';
		*v++ = video_color;
	}
}

static void
print_char(int c)
{
	unsigned char *v = (unsigned char *) ((uint64_t)(VIDMEM + row * NUM_COLS * 2 + col * 2));

	switch(c) {
	case '\n':
		clear_to_EOL();
		new_line();
		break;
	default:
		*v++ = (unsigned char) c;
		*v   = video_color;
		
		if (col < NUM_COLS - 1)
			++col;
		else
			new_line();
	}
   move_cursor();
}

void
print_string(const char *s)
{
	while (*s != '\0') {
		print_char(*s++);
	}
}