summaryrefslogtreecommitdiff
path: root/kernel/main.c
blob: a4a905c63eb97c19dc25f6d48bda52b2455ab506 (plain)
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
#include <stdint.h>
#include "assert.h"
#include "util.h"

#define VGA_WIDTH 80
#define VGA_HEIGHT 25

struct e820_entry {
	uint64_t base;
	uint64_t length;
	uint32_t type;
	uint32_t ext_attr;
};

STATIC_ASSERT(sizeof(struct e820_entry) == 24, e820_entry);

struct vga_term {
	uint16_t *buf;
	uint16_t column;
	uint16_t line;
};

static void
vga_term_scroll(const struct vga_term *const term)
{
	for (int i = 0; i < VGA_HEIGHT-1; ++i)
		for (int j = 0; j < VGA_WIDTH; ++j)
			term->buf[i*VGA_WIDTH+j] = term->buf[(i+1)*VGA_WIDTH+j];
	for (int i = 0; i < VGA_WIDTH; ++i)
		term->buf[(VGA_HEIGHT-1)*VGA_WIDTH+i] = 0x0f20;
}

static void
vga_term_newline(struct vga_term *const term)
{
	if (term->line < VGA_HEIGHT-1)
		++term->line;
	else
		vga_term_scroll(term);

	term->column = 0;

	return;
}

static void
vga_term_putc(struct vga_term *const term, const char c, const uint8_t color)
{
	if (c == '\n') {
		vga_term_newline(term);
		return;
	}

	if (term->column >= VGA_WIDTH)
		vga_term_newline(term);

	term->buf[term->line*VGA_WIDTH+(term->column++)] = (color<<8)|(c);
}

static void
vga_term_puts(struct vga_term *const term, const char *const str, const uint8_t color)
{
	for (int i = 0; str[i]; ++i)
		vga_term_putc(term, str[i], color);
}

static void
vga_term_print_hex(struct vga_term *const term, uint64_t num, const uint8_t color)
{
	vga_term_putc(term, '0', color);
	vga_term_putc(term, 'x', color);

	for (int i = 0; i < 16; ++i) {
		char c;

		c = (num >> 60);
		c = (c < 0xa) ? (c + '0') : (c + 'a' - 0xa);

		vga_term_putc(term, c, color);

		num <<= 4;
	}
}

void
kmain(void)
{
	vga_cursor_hide();

	struct vga_term term = {(uint16_t *)0xb8000, 0, 0};

	for (int i = 0; i < VGA_WIDTH*VGA_HEIGHT; ++i)
		term.buf[i] = 0x0f20;

	vga_term_puts(&term, "ProvidenceOS v0.1\n\ne820 memory map:\n", 0x0f);

	const uint16_t e820_size = *(uint16_t *)0xf000;
	const struct e820_entry *const e820_mmap = (struct e820_entry *)0xf008;

	for (uint16_t i = 0; i < e820_size; ++i) {
		vga_term_print_hex(&term, e820_mmap[i].base, 0x0f);
		vga_term_putc(&term, '-', 0x0f);

		vga_term_print_hex(&term, e820_mmap[i].base + e820_mmap[i].length - 1, 0x0f);
		vga_term_putc(&term, ' ', 0x0f);

		vga_term_puts(&term, e820_mmap[i].type == 1 ? "available\n" : "reserved\n", 0x0f);
	}

	halt();
}