Project

General

Profile

Statistics
| Revision:

root / lab5 / graphics.c @ 161

History | View | Annotate | Download (7.1 KB)

1 95 up20180655
#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 103 up20180655
static vbe_mode_info_t vbe_mem_info;
11 99 up20180655
static mmap_t mem_map;
12 95 up20180655
13 99 up20180655
int (get_permission)(unsigned int base_addr, unsigned int size) {
14 95 up20180655
    struct minix_mem_range mmr;
15 99 up20180655
    mmr.mr_base = base_addr;
16
    mmr.mr_limit = base_addr + size;
17
    return sys_privctl(SELF, SYS_PRIV_ADD_MEM, &mmr);
18
}
19 95 up20180655
20 121 up20180642
//int (get_permissions_first_mbyte)(void) {
21
//    return get_permission(MBYTE_BASE, MBYTE_SIZE);
22
//}
23 95 up20180655
24 103 up20180655
int (vbe_get_mode_information)(uint16_t mode) {
25
    memset(&vbe_mem_info, 0, sizeof(vbe_mode_info_t)); // reset values
26 95 up20180655
27 99 up20180655
    struct reg86 reg_86;
28 103 up20180655
    memset(&reg_86, 0, sizeof(struct reg86)); // reset struct
29 98 up20180655
30 99 up20180655
    vbe_mode_info_t *virtual_addr = lm_alloc(sizeof(vbe_mode_info_t), &mem_map);
31 98 up20180655
32 99 up20180655
    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 138 up20180655
    if (sys_int86(&reg_86) || reg_86.ah != AH_SUCCESS) {
40 99 up20180655
        printf("%s: sys_int86 failed\n", __func__);
41 133 up20180655
        if (free_memory_map()) {
42 99 up20180655
            printf("%s: lm_free failed\n", __func__);
43
        }
44
        return BIOS_CALL_ERROR;
45 95 up20180655
    }
46
47 103 up20180655
    memcpy((void*)&vbe_mem_info, (void*)virtual_addr, mem_map.size);
48 99 up20180655
    return SUCCESS;
49
}
50 95 up20180655
51 126 up20180655
int (vbe_get_controller_information)(vg_vbe_contr_info_t *info_p) {
52
    memset(info_p, 0, sizeof(vg_vbe_contr_info_t)); // reset values
53
54
    mmap_t controller_map;
55
56
    struct reg86 reg_86;
57
    memset(&reg_86, 0, sizeof(struct reg86)); // reset struct
58
59
    VbeInfoBlock *virtual_addr = lm_alloc(sizeof(VbeInfoBlock), &controller_map);
60
61 128 up20180655
    uint32_t virtual_base = (uint32_t)(virtual_addr) - controller_map.phys;
62
63 126 up20180655
    virtual_addr->VbeSignature[0] = 'V';
64
    virtual_addr->VbeSignature[1] = 'B';
65
    virtual_addr->VbeSignature[2] = 'E';
66
    virtual_addr->VbeSignature[3] = '2';
67
68
69
    reg_86.intno = VC_BIOS_SERV;
70
    reg_86.ah = VBE_CALL;
71
    reg_86.al = VBE_CTRL_INFO;
72
    reg_86.es = PB2BASE(controller_map.phys);
73
    reg_86.di = PB2OFF(controller_map.phys);
74
    // BIOS CALL
75 138 up20180655
    if (sys_int86(&reg_86) || reg_86.ah != AH_SUCCESS) {
76 126 up20180655
        printf("%s: sys_int86 failed\n", __func__);
77 138 up20180655
        if (!lm_free(&controller_map)) {
78 126 up20180655
            printf("%s: lm_free failed\n", __func__);
79
        }
80
        return BIOS_CALL_ERROR;
81
    }
82
83
    info_p->VBESignature[0] = virtual_addr->VbeSignature[0];
84
    info_p->VBESignature[1] = virtual_addr->VbeSignature[1];
85
    info_p->VBESignature[2] = virtual_addr->VbeSignature[2];
86
    info_p->VBESignature[3] = virtual_addr->VbeSignature[3];
87
88
    uint8_t lsb, msb;
89
    util_get_LSB(virtual_addr->VbeVersion, &lsb);
90
    util_get_MSB(virtual_addr->VbeVersion, &msb);
91
    info_p->VBEVersion[0] = lsb;
92
    info_p->VBEVersion[1] = msb;
93
94
    info_p->TotalMemory = (virtual_addr->TotalMemory << 6);
95
96
    // Convert Far Far Pointer to Virtual Address
97
98 128 up20180655
    uint32_t phys_ptr = FAR2PHYS(virtual_addr->OemStringPtr);
99
    uint32_t virtual_ptr = phys_ptr + virtual_base;
100
    info_p->OEMString = (char*)(virtual_ptr);
101 126 up20180655
102 128 up20180655
    phys_ptr = FAR2PHYS(virtual_addr->VideoModePtr);
103
    virtual_ptr = phys_ptr + virtual_base;
104
    info_p->VideoModeList = (uint16_t*)(virtual_ptr);
105 126 up20180655
106 128 up20180655
    phys_ptr = FAR2PHYS(virtual_addr->OemVendorNamePtr);
107
    virtual_ptr = phys_ptr + virtual_base;
108
    info_p->OEMVendorNamePtr = (char*)(virtual_ptr);
109 126 up20180655
110 128 up20180655
    phys_ptr = FAR2PHYS(virtual_addr->OemProductNamePtr);
111
    virtual_ptr = phys_ptr + virtual_base;
112
    info_p->OEMProductNamePtr = (char*)(virtual_ptr);
113 126 up20180655
114 128 up20180655
    phys_ptr = FAR2PHYS(virtual_addr->OemProductRevPtr);
115
    virtual_ptr = phys_ptr + virtual_base;
116
    info_p->OEMProductRevPtr = (char*)(virtual_ptr);
117 126 up20180655
118 138 up20180655
    if (!lm_free(&controller_map)) {
119
        printf("%s: lm_free failed\n", __func__);
120
        return LCF_ERROR;
121
    }
122 126 up20180655
123
    return SUCCESS;
124
}
125
126 103 up20180655
phys_bytes get_phys_addr(void) {
127
    return vbe_mem_info.PhysBasePtr;
128
}
129
130
unsigned int get_vram_size(void) {
131
    return vbe_mem_info.XResolution * vbe_mem_info.YResolution * ((vbe_mem_info.BitsPerPixel + 7) >> 3);
132
}
133
134
uint16_t get_XRes(void) {
135
    return vbe_mem_info.XResolution;
136
}
137
138
uint16_t get_YRes(void) {
139
    return vbe_mem_info.YResolution;
140
}
141
142
uint16_t get_bits_pixel(void) {
143
    return vbe_mem_info.BitsPerPixel;
144
}
145
146 105 up20180655
uint16_t get_bytes_pixel(void) {
147
    return (vbe_mem_info.BitsPerPixel + 7) >> 3;
148
}
149
150 112 up20180642
uint16_t get_RedMaskSize  (void){ return vbe_mem_info.RedMaskSize  ; }
151
uint16_t get_GreenMaskSize(void){ return vbe_mem_info.GreenMaskSize; }
152
uint16_t get_BlueMaskSize (void){ return vbe_mem_info.BlueMaskSize ; }
153 111 up20180642
154 103 up20180655
int (map_vram)(void) {
155 99 up20180655
    int r;
156 103 up20180655
    unsigned int vram_base = get_phys_addr();
157
    unsigned int vram_size = get_vram_size();
158 138 up20180655
    if ((r = get_permission(vram_base, vram_size))) {
159
        if (free_memory_map()) {
160
            printf("%s: lm_free failed\n", __func__);
161
        }
162 99 up20180655
        panic("%s: sys_privctl (ADD MEM) failed: %d\n", __func__, r);
163 138 up20180655
    }
164 95 up20180655
165 99 up20180655
    video_mem = vm_map_phys(SELF, (void *)vram_base, vram_size);
166 95 up20180655
167 138 up20180655
    if (video_mem == MAP_FAILED) {
168
        if (free_memory_map()) {
169
            printf("%s: lm_free failed\n", __func__);
170
        }
171 99 up20180655
        panic("%s: couldn't map video memory.", __func__);
172 138 up20180655
    }
173 95 up20180655
174 99 up20180655
    return SUCCESS;
175
}
176
177 133 up20180655
int (free_memory_map)(void) {
178 103 up20180655
    return !lm_free(&mem_map);
179 99 up20180655
}
180
181
int (set_graphics_mode)(uint16_t mode) {
182
    struct reg86 reg_86;
183
184 103 up20180655
    memset(&reg_86, 0, sizeof(struct reg86)); // reset struct
185 96 up20180655
186 99 up20180655
    // Set Reg86
187 98 up20180655
    reg_86.intno = VC_BIOS_SERV;
188
    reg_86.ah = VBE_CALL;
189
    reg_86.al = SET_VBE_MD;
190
    reg_86.bx = mode | LINEAR_FRAME_BUFFER_MD;
191 96 up20180655
192 98 up20180655
    // BIOS CALL
193 138 up20180655
    if (sys_int86(&reg_86) || reg_86.ah != AH_SUCCESS) {
194 98 up20180655
        printf("%s: sys_int86 failed\n", __func__);
195
        return BIOS_CALL_ERROR;
196
    }
197 99 up20180655
198 95 up20180655
    return SUCCESS;
199
}
200 102 up20180642
201 134 up20180642
202
int (set_pixel)(uint16_t x, uint16_t y, uint32_t color) {
203
    if (x >= vbe_mem_info.XResolution || y >= vbe_mem_info.YResolution) {
204
        printf("%s: invalid pixel.\n", __func__);
205
        return OUT_OF_RANGE;
206
    }
207
    unsigned int pos = (x + y * vbe_mem_info.XResolution) * get_bytes_pixel();
208
    memcpy((void*)((unsigned int)video_mem + pos), &color, get_bytes_pixel());
209
    return SUCCESS;
210
}
211
212 120 up20180642
int (draw_hline)(uint16_t x, uint16_t y, uint16_t len, uint32_t color){
213 109 up20180655
    int r;
214 117 up20180642
    for (uint16_t i = 0; i < len; i++)
215 109 up20180655
        if ((r = set_pixel(x + i, y, color))) return r;
216
    return SUCCESS;
217 102 up20180642
}
218 120 up20180642
int (vg_draw_hline)(uint16_t x, uint16_t y, uint16_t len, uint32_t color){
219
    return draw_hline(x,y,len,color);
220
}
221 109 up20180655
222 119 up20180642
int (draw_rectangle)(uint16_t x, uint16_t y,uint16_t width, uint16_t height, uint32_t color)        {
223 109 up20180655
    int r;
224 117 up20180642
    for (uint16_t i = 0; i < height; i++)
225 120 up20180642
        if ((r = draw_hline(x, y + i, width, color))) return r;
226 109 up20180655
    return SUCCESS;
227
}
228 120 up20180642
int (vg_draw_rectangle)(uint16_t x, uint16_t y,uint16_t width, uint16_t height, uint32_t color){
229
    return draw_rectangle(x,y,width,height, color);
230
}
231 134 up20180642
232
int paint_screen(uint32_t color){
233
    return draw_rectangle(0,0,get_XRes(),get_YRes(),color);
234
}
235
236
int clear_screen(){
237 143 up20180655
    return paint_screen(BLACK);
238 134 up20180642
}