Project

General

Profile

Statistics
| Revision:

root / lab5 / graphics.c @ 135

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