Project

General

Profile

Statistics
| Revision:

root / lab5 / graphics.c @ 100

History | View | Annotate | Download (2.3 KB)

1
#include "graphics.h"
2
#include "graphics_macros.h"
3
#include "errors.h"
4

    
5
#include <lcom/lcf.h>
6

    
7
#include <stdio.h>
8

    
9
static void *video_mem; /** @brief Frame-buffer VM address */
10
//static vbe_mode_info_t vbe_mem_info;
11
static mmap_t mem_map;
12

    
13
int (get_permission)(unsigned int base_addr, unsigned int size) {
14
    struct minix_mem_range mmr;
15
    mmr.mr_base = base_addr;
16
    mmr.mr_limit = base_addr + size;
17
    return sys_privctl(SELF, SYS_PRIV_ADD_MEM, &mmr);
18
}
19

    
20
int (get_permissions_first_mbyte)(void) {
21
    return get_permission(MBYTE_BASE, MBYTE_SIZE);
22
}
23

    
24
int (vbe_get_mode_information)(uint16_t mode, vbe_mode_info_t *vmi_p) {
25
    memset(vmi_p, 0, sizeof(vbe_mode_info_t)); // reset values
26

    
27
    struct reg86 reg_86;
28
    memset(&reg_86, 0, sizeof(reg_86)); // reset struct
29

    
30
    vbe_mode_info_t *virtual_addr = lm_alloc(sizeof(vbe_mode_info_t), &mem_map);
31

    
32
    reg_86.intno = VC_BIOS_SERV;
33
    reg_86.ah = VBE_CALL;
34
    reg_86.al = VBE_MD_INFO;
35
    reg_86.cx = mode;
36
    reg_86.es = PB2BASE(mem_map.phys);
37
    reg_86.di = PB2OFF(mem_map.phys);
38
    // BIOS CALL
39
    if (sys_int86(&reg_86)) {
40
        printf("%s: sys_int86 failed\n", __func__);
41
        if (free_memory()) {
42
            printf("%s: lm_free failed\n", __func__);
43
            return LCF_ERROR;
44
        }
45
        return BIOS_CALL_ERROR;
46
    }
47

    
48
    vmi_p = memcpy((void*)vmi_p, (void*)virtual_addr, mem_map.size);
49
    return SUCCESS;
50
}
51

    
52
int (map_vram)(unsigned int vram_base, unsigned int vram_size) {
53
    int r;
54
    if ((r = get_permission(vram_base, vram_size)))
55
        panic("%s: sys_privctl (ADD MEM) failed: %d\n", __func__, r);
56

    
57
    video_mem = vm_map_phys(SELF, (void *)vram_base, vram_size);
58

    
59
    if (video_mem == MAP_FAILED)
60
        panic("%s: couldn't map video memory.", __func__);
61

    
62
    return SUCCESS;
63
}
64

    
65
int (free_memory)(void) {
66
    return lm_free(&mem_map);
67
}
68

    
69
int (set_graphics_mode)(uint16_t mode) {
70
    struct reg86 reg_86;
71

    
72
    memset(&reg_86, 0, sizeof(reg_86)); // reset struct
73

    
74
    // Set Reg86
75
    reg_86.intno = VC_BIOS_SERV;
76
    reg_86.ah = VBE_CALL;
77
    reg_86.al = SET_VBE_MD;
78
    reg_86.bx = mode | LINEAR_FRAME_BUFFER_MD;
79

    
80
    // BIOS CALL
81
    if (sys_int86(&reg_86)) {
82
        printf("%s: sys_int86 failed\n", __func__);
83
        return BIOS_CALL_ERROR;
84
    }
85

    
86
    return SUCCESS;
87
}