summaryrefslogtreecommitdiff
path: root/src/kernel/core/monitor.c
blob: 0d0f5eb85ce97dfa5381ab3cb48ea2048cc4ba90 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#include "monitor.h"
#include "sys.h"

static int cursor_x = 0, cursor_y = 0;
static uint16_t *video_memory = (uint16_t*)0xE00B8000;

static uint8_t attribute = 0x07; // 0 = background = black, 7 = foreground = white

/*	For internal use only. Tells hardware to move the cursor at (cursor_x, cursor_y). */
static void move_cursor() {
	uint16_t cursor_location = cursor_y * 80 + cursor_x;
	outb(0x3D4, 14);	//Sending high cursor byte
	outb(0x3D5, cursor_location >> 8);
	outb(0x3D4, 15);	//Sending high cursor byte
	outb(0x3D5, cursor_location);
}

/*	For internal use only. Scrolls everything up one line. */
static void scroll() {
	uint16_t blank = (attribute << 8) | 0x20;

	if (cursor_y >= 25) {
		int i;
		for (i = 0; i < 80*24; i++) {
			video_memory[i] = video_memory[i+80];
		}
		for (i = 80*24; i < 80*25; i++) {
			video_memory[i] = blank;
		}
		cursor_y = 24;
	}
}

/*	Put one character on the screen. This function handles special characters \b, \t, \r and  \n. */
void monitor_put(char c) {
	if (c == '\b' && cursor_x) {	//Backspace
		cursor_x--;
	} else if (c == '\t') {	//Tab
		cursor_x = (cursor_x + 8) & ~(8 - 1);
	} else if (c == '\r') {	//Carriage return
		cursor_x = 0;
	} else if (c == '\n') {	//New line
		cursor_x = 0;
		cursor_y++;
	} else if (c >= ' ') {	//Any printable character
		video_memory[cursor_y * 80 + cursor_x] = c | (attribute << 8);
		cursor_x++;
	}
	if (cursor_x >= 80) {
		cursor_x = 0;
		cursor_y++;
	}

	scroll();
	move_cursor();
}

/* Clears the screen and moves cursor to (0,0) (top left corner) */
void monitor_clear() {
	uint16_t blank = (attribute << 8) | 0x20;

	int i;

	for (i = 0; i < 80*25; i++) {
		video_memory[i] = blank;
	}

	cursor_x = 0; cursor_y = 0;
	move_cursor();
}

/* Writes a string to the monitor */
void monitor_write(char *s) {
	while (*s) {
		monitor_put(*(s++));
	}
}

/* Writes a number in hexadecimal notation to the monitor */
void monitor_writeHex(uint32_t v) {
	int i;

	monitor_put('0'); monitor_put('x');
	char hexdigits[] = "0123456789abcdef";

	for (i = 0; i < 8; i++) {
		monitor_put(hexdigits[v >> 28]);
		v = v << 4;
	}
}

/* Writes a number in decimal notation to the monitor */
void monitor_writeDec(uint32_t v) {
	if (v == 0) {
		monitor_put('0');
		return;
	}

	char numbers[] = "0123456789";
	while (v > 0) {
		int order = 1, no = 1;
		while (v / order > 0) order *= 10;
		order /= 10;
		monitor_put(numbers[v / order]);
		v = v - (v / order * order);
		while (v / no > 0) no *= 10;
		while (no < order) {
			monitor_put('0');
			no *= 10;
		}
	}
}