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

void switch_to_vga() {
    vesa_available = 0;
    regs16_t regs;
    regs.ax = 0x0003;
    int32(0x10, &regs);
}

struct vbe_mode_info *vbe_set_mode(unsigned short mode) {
    vesa_available = 0;
    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();
        }

        struct vbe_mode_info *vbe_info = (struct 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);

        vesa_available = 1;

        return vbe_info;
    } else {
        switch_to_vga();
    }

    struct vbe_mode_info vbe_info;
    return &vbe_info;
}

void set_optimal_resolution() {
    vesa_available = 0;
    struct vbe_info *info = (struct vbe_info *) 0x2000;
    struct vbe_mode_info *mode_info = (struct vbe_mode_info *) 0x3000;

    memory_copy(info->signature, "VBE2", 4);

    regs16_t regs;
    regs.ax = 0x4F00;
    regs.es = 0;
    regs.di = info;
    int32(0x10, &regs);

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

    uint16_t *mode_ptr = get_ptr(info->video_modes);
    uint16_t mode;
    uint16_t highest = 0x11B;
    uint16_t highest_width = 0;

    while ((mode = *mode_ptr++) != 0xFFFF) {
        mode &= 0x1FF;
        regs16_t regs2;
        regs2.ax = 0x4F01;
        regs2.cx = mode;
        regs2.es = get_segment(mode_info);
        regs2.di = get_offset(mode_info);
        int32(0x10, &regs2);

        if ((mode_info->attributes & 0x90) != 0x90) continue;

        if (mode_info->width >= highest_width && (float) mode_info->width / (float) mode_info->height < 2.0) {
            highest = mode;
            highest_width = mode_info->width;
        }
    }

    vbe_set_mode(highest);
}