summaryrefslogtreecommitdiff
path: root/src/main.c
blob: d93e8081ea5a2d5d3e1c9e350d74fad087b8f454 (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
#include "serial.h"
#include "vga.h"

/* XXX doest not always handle memory overlaps */
static void my_memmove(void *_dest, const void *_src, int n)
{
    char *dest = _dest;
    const char *src = _src;

    for (int i = 0; i < n; ++i) {
        dest[i] = src[i];
    }
}

static void clear_line(void *_addr)
{
    char *addr = _addr;

    for (int i = 0; i < VGA_COLS; ++i) {
        if (i % VGA_BPC == 0) {
            *addr++ = ' ';
        } else {
            *addr++ = 0x7;
        }
    }
}

int printk(const char *str)
{
    static const long int offset = VGA_BASE;
    static char *addr = (char *) VGA_BASE;
    int count = 0;

    for (int i = 0; str[i]; ++i) {
        serial_putchar(str[i]);

        char chr;
        char *new_addr = addr;
        char *addr_line = (char *)(((long)(addr - offset) / VGA_BPR) *
                VGA_BPR) + offset;

        if ((long)addr >= offset + VGA_ROWS * VGA_BPR) {
            /* shift up */
            addr -= VGA_BPR;
            addr_line -= VGA_BPR;
            my_memmove((void *)offset, (void *)offset + VGA_BPR, VGA_BPR
                    * (VGA_ROWS - 1)); clear_line(addr_line);
        }

        switch (str[i]) {
            case '\n':
                new_addr = addr_line + VGA_BPR;
                chr = '\0';
                break;
            default:
                new_addr = addr + VGA_BPC;
                chr = str[i];
                break;
        }

        if (chr)
            *addr = chr;

        addr = new_addr;
        ++count;
    }

    return count;
}

int main(void)
{
    serial_init();

    printk("kfs is booting !\n");

    return 0;
}