aboutsummaryrefslogtreecommitdiff
path: root/src/kernel/graphics/vesa.c
blob: cab6c7007eb5b75b3cbd624749eedc9e77cd0850 (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
#include "vesa.h"
#include "graphics.h"
#include "../sound/sound.h"
#include "../lib/lib.h"
#include "../acpi/acpi.h"

void switch_to_vga() {
    terminal_initialize();
}

vbe_mode_info *vbe_set_mode(unsigned short mode) {
    regs16_t regs;
    regs.ax = 0x4F02;
    regs.bx = mode | (1 << 14);
    int32(0x10, &regs);

    if (regs.ax == 0x004F) {
        regs.ax = 0x4F01;
        regs.cx = mode;
        regs.di = 0x0000;
        regs.es = 0xA000;
        int32(0x10, &regs);
        if (regs.ax != 0x004F) {
            switch_to_vga();
        }

        vbe_mode_info *vbe_info = (vbe_mode_info *) 0xA0000;

        vbe_width = vbe_info->width;
        vbe_height = vbe_info->height;
        vbe_bpp = vbe_info->bpp / 8;
        vbe_pitch = vbe_info->pitch;

        char *fb = (char *) vbe_info->framebuffer;
        for (int i = 0; i < vbe_width * vbe_height * vbe_bpp; i++) {
            fb[i] = 100;
            fb[i + 1] = 100;
            fb[i + 2] = 100;
        }
        regs.ax = 0x0000;
        int32(0x16, &regs);
        regs.ax = 0x0003;
        int32(0x10, &regs);

        return vbe_info;
    } else {
        switch_to_vga();
    }

    vbe_mode_info vbe_info;
    return &vbe_info;
}

void set_optimal_resolution() {
    extern vbe_info *vbe_init_structure;
    regs16_t regs;
    regs.ax = 0x4F01;
    regs.cx = vbe_init_structure;
    regs.di = 0x0000;
    regs.es = 0xA000;
    int32(0x10, &regs);

    if (regs.ax != 0x004F) {
        switch_to_vga();
    }

    vbe_info *vbe_modes = (vbe_info *) 0xA0000;
    if (strcmp(vbe_modes->signature, "VESA") == 0) {
        loop:
        asm volatile ("hlt");
        goto loop;
    } else {
        acpi_poweroff();
    }
}