summaryrefslogtreecommitdiff
path: root/Kernel/VGA.cpp
blob: b2571ecdd832a26633c8223d988c3594aabde6c6 (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
#include "types.h"
#include "VGA.h"
#include "i386.h"
#include "IO.h"
#include "StdLib.h"
#include "Task.h"

PRIVATE BYTE *vga_mem = 0L;
PRIVATE BYTE current_attr = 0x07;

void vga_scroll_up()
{
    InterruptDisabler disabler;
    memcpy(vga_mem, vga_mem + 160, 160 * 24);
    memset(vga_mem + (160 * 24), 0, 160);
}

void vga_clear()
{
    InterruptDisabler disabler;
    memset(vga_mem, 0, 160 * 25);
}

void vga_putch_at(byte row, byte column, byte ch)
{
    word cur = (row * 160) + (column * 2);
    vga_mem[cur] = ch;
    vga_mem[cur + 1] = current_attr;
}

void vga_set_attr(BYTE attr)
{
    current_attr = attr;
}

byte vga_get_attr()
{
    return current_attr;
}

void vga_init()
{
    current_attr = 0x07;
    vga_mem = (byte*)0xb8000;

    for (word i = 0; i < (80 * 25); ++i) {
        vga_mem[i*2] = ' ';
        vga_mem[i*2 + 1] = 0x07;
    }

    vga_set_cursor(0);
}

WORD vga_get_cursor()
{
    WORD value;
    IO::out8(0x3d4, 0x0e);
    value = IO::in8(0x3d5) << 8;
    IO::out8(0x3d4, 0x0f);
    value |= IO::in8(0x3d5);
    return value;
}

void vga_set_cursor(WORD value)
{
    if (value >= (80 * 25)) {
        /* XXX: If you try to move the cursor off the screen, I will go reddish pink! */
        vga_set_cursor(0);
        current_attr = 0x0C;
        return;
    }
    IO::out8(0x3d4, 0x0e);
    IO::out8(0x3d5, MSB(value));
    IO::out8(0x3d4, 0x0f);
    IO::out8(0x3d5, LSB(value));
}

void vga_set_cursor(BYTE row, BYTE column)
{
    vga_set_cursor(row * 80 + column);
}