Project

General

Profile

Statistics
| Revision:

root / lab5 / graphics.c @ 360

History | View | Annotate | Download (7.1 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) || reg_86.ah != AH_SUCCESS) {
40
        printf("%s: sys_int86 failed\n", __func__);
41
        if (free_memory_map()) {
42
            printf("%s: lm_free failed\n", __func__);
43
        }
44
        return BIOS_CALL_ERROR;
45
    }
46

    
47
    memcpy((void*)&vbe_mem_info, (void*)virtual_addr, mem_map.size);
48
    return SUCCESS;
49
}
50

    
51
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
    uint32_t virtual_base = (uint32_t)(virtual_addr) - controller_map.phys;
62

    
63
    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
    if (sys_int86(&reg_86) || reg_86.ah != AH_SUCCESS) {
76
        printf("%s: sys_int86 failed\n", __func__);
77
        if (!lm_free(&controller_map)) {
78
            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
    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

    
102
    phys_ptr = FAR2PHYS(virtual_addr->VideoModePtr);
103
    virtual_ptr = phys_ptr + virtual_base;
104
    info_p->VideoModeList = (uint16_t*)(virtual_ptr);
105

    
106
    phys_ptr = FAR2PHYS(virtual_addr->OemVendorNamePtr);
107
    virtual_ptr = phys_ptr + virtual_base;
108
    info_p->OEMVendorNamePtr = (char*)(virtual_ptr);
109

    
110
    phys_ptr = FAR2PHYS(virtual_addr->OemProductNamePtr);
111
    virtual_ptr = phys_ptr + virtual_base;
112
    info_p->OEMProductNamePtr = (char*)(virtual_ptr);
113

    
114
    phys_ptr = FAR2PHYS(virtual_addr->OemProductRevPtr);
115
    virtual_ptr = phys_ptr + virtual_base;
116
    info_p->OEMProductRevPtr = (char*)(virtual_ptr);
117

    
118
    if (!lm_free(&controller_map)) {
119
        printf("%s: lm_free failed\n", __func__);
120
        return LCF_ERROR;
121
    }
122

    
123
    return SUCCESS;
124
}
125

    
126
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
uint16_t get_bytes_pixel(void) {
147
    return (vbe_mem_info.BitsPerPixel + 7) >> 3;
148
}
149

    
150
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

    
154
int (map_vram)(void) {
155
    int r;
156
    unsigned int vram_base = get_phys_addr();
157
    unsigned int vram_size = get_vram_size();
158
    if ((r = get_permission(vram_base, vram_size))) {
159
        if (free_memory_map()) {
160
            printf("%s: lm_free failed\n", __func__);
161
        }
162
        panic("%s: sys_privctl (ADD MEM) failed: %d\n", __func__, r);
163
    }
164

    
165
    video_mem = vm_map_phys(SELF, (void *)vram_base, vram_size);
166

    
167
    if (video_mem == MAP_FAILED) {
168
        if (free_memory_map()) {
169
            printf("%s: lm_free failed\n", __func__);
170
        }
171
        panic("%s: couldn't map video memory.", __func__);
172
    }
173

    
174
    return SUCCESS;
175
}
176

    
177
int (free_memory_map)(void) {
178
    return !lm_free(&mem_map);
179
}
180

    
181
int (set_graphics_mode)(uint16_t mode) {
182
    struct reg86 reg_86;
183

    
184
    memset(&reg_86, 0, sizeof(struct reg86)); // reset struct
185

    
186
    // Set Reg86
187
    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

    
192
    // BIOS CALL
193
    if (sys_int86(&reg_86) || reg_86.ah != AH_SUCCESS) {
194
        printf("%s: sys_int86 failed\n", __func__);
195
        return BIOS_CALL_ERROR;
196
    }
197

    
198
    return SUCCESS;
199
}
200

    
201

    
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
int (draw_hline)(uint16_t x, uint16_t y, uint16_t len, uint32_t color){
213
    int r;
214
    for (uint16_t i = 0; i < len; i++)
215
        if ((r = set_pixel(x + i, y, color))) return r;
216
    return SUCCESS;
217
}
218
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

    
222
int (draw_rectangle)(uint16_t x, uint16_t y,uint16_t width, uint16_t height, uint32_t color)        {
223
    int r;
224
    for (uint16_t i = 0; i < height; i++)
225
        if ((r = draw_hline(x, y + i, width, color))) return r;
226
    return SUCCESS;
227
}
228
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

    
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
    return paint_screen(BLACK);
238
}