Project

General

Profile

Statistics
| Revision:

root / lab5 / graphics.c @ 111

History | View | Annotate | Download (3.95 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) {
25
    memset(&vbe_mem_info, 0, sizeof(vbe_mode_info_t)); // reset values
26

    
27
    struct reg86 reg_86;
28
    memset(&reg_86, 0, sizeof(struct reg86)); // 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
    memcpy((void*)&vbe_mem_info, (void*)virtual_addr, mem_map.size);
49
    return SUCCESS;
50
}
51

    
52
phys_bytes get_phys_addr(void) {
53
    return vbe_mem_info.PhysBasePtr;
54
}
55

    
56
unsigned int get_vram_size(void) {
57
    return vbe_mem_info.XResolution * vbe_mem_info.YResolution * ((vbe_mem_info.BitsPerPixel + 7) >> 3);
58
}
59

    
60
uint16_t get_XRes(void) {
61
    return vbe_mem_info.XResolution;
62
}
63

    
64
uint16_t get_YRes(void) {
65
    return vbe_mem_info.YResolution;
66
}
67

    
68
uint16_t get_bits_pixel(void) {
69
    return vbe_mem_info.BitsPerPixel;
70
}
71

    
72
uint16_t get_bytes_pixel(void) {
73
    return (vbe_mem_info.BitsPerPixel + 7) >> 3;
74
}
75

    
76
uint16_t get_RedMaskSize(void){
77
    return vbe_mem_info.RedMaskSize;
78
}
79

    
80
uint16_t get_GreenMaskSize(void){
81
    return vbe_mem_info.GreenMaskSize;
82
}
83

    
84
uint16_t get_BlueMaskSize(void){
85
    return vbe_mem_info.BlueMaskSize;
86
}
87

    
88
int (map_vram)(void) {
89
    int r;
90
    unsigned int vram_base = get_phys_addr();
91
    unsigned int vram_size = get_vram_size();
92
    if ((r = get_permission(vram_base, vram_size)))
93
        panic("%s: sys_privctl (ADD MEM) failed: %d\n", __func__, r);
94

    
95
    video_mem = vm_map_phys(SELF, (void *)vram_base, vram_size);
96

    
97
    if (video_mem == MAP_FAILED)
98
        panic("%s: couldn't map video memory.", __func__);
99

    
100
    return SUCCESS;
101
}
102

    
103
int (free_memory)(void) {
104
    return !lm_free(&mem_map);
105
}
106

    
107
int (set_pixel)(uint16_t row, uint16_t col, uint32_t color) {
108
    if (row >= vbe_mem_info.XResolution || col >= vbe_mem_info.YResolution) {
109
        printf("%s: invalid pixel.\n", __func__);
110
        return OUT_OF_RANGE;
111
    }
112
    unsigned int pos = (row + col * vbe_mem_info.XResolution) * get_bytes_pixel();
113
    memcpy((void*)((unsigned int)video_mem + pos), &color, get_bytes_pixel());
114
    return SUCCESS;
115
}
116

    
117
int (set_graphics_mode)(uint16_t mode) {
118
    struct reg86 reg_86;
119

    
120
    memset(&reg_86, 0, sizeof(struct reg86)); // reset struct
121

    
122
    // Set Reg86
123
    reg_86.intno = VC_BIOS_SERV;
124
    reg_86.ah = VBE_CALL;
125
    reg_86.al = SET_VBE_MD;
126
    reg_86.bx = mode | LINEAR_FRAME_BUFFER_MD;
127

    
128
    // BIOS CALL
129
    if (sys_int86(&reg_86)) {
130
        printf("%s: sys_int86 failed\n", __func__);
131
        return BIOS_CALL_ERROR;
132
    }
133

    
134
    return SUCCESS;
135
}
136

    
137
int (vg_draw_hline)(uint16_t x, uint16_t y, uint16_t len, uint32_t color){
138
    int r;
139
    for (int i = 0; i < len; i++)
140
        if ((r = set_pixel(x + i, y, color))) return r;
141

    
142
    return SUCCESS;
143
}
144

    
145
int (vg_draw_rectangle)(uint16_t x, uint16_t y,uint16_t width, uint16_t height, uint32_t color)        {
146
    int r;
147
    for (int i = 0; i < height; i++)
148
        if ((r = vg_draw_hline(x, y + i, width, color))) return r;
149

    
150
    return SUCCESS;
151
}