Project

General

Profile

Statistics
| Revision:

root / lab5 / graphics.c @ 126

History | View | Annotate | Download (5.79 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
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
    virtual_addr->VbeSignature[0] = 'V';
63
    virtual_addr->VbeSignature[1] = 'B';
64
    virtual_addr->VbeSignature[2] = 'E';
65
    virtual_addr->VbeSignature[3] = '2';
66

    
67

    
68
    reg_86.intno = VC_BIOS_SERV;
69
    reg_86.ah = VBE_CALL;
70
    reg_86.al = VBE_CTRL_INFO;
71
    reg_86.es = PB2BASE(controller_map.phys);
72
    reg_86.di = PB2OFF(controller_map.phys);
73
    // BIOS CALL
74
    if (sys_int86(&reg_86)) {
75
        printf("%s: sys_int86 failed\n", __func__);
76
        if (free_memory()) {
77
            printf("%s: lm_free failed\n", __func__);
78
            return LCF_ERROR;
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

    
100

    
101

    
102

    
103

    
104

    
105

    
106
    return SUCCESS;
107
}
108

    
109
phys_bytes get_phys_addr(void) {
110
    return vbe_mem_info.PhysBasePtr;
111
}
112

    
113
unsigned int get_vram_size(void) {
114
    return vbe_mem_info.XResolution * vbe_mem_info.YResolution * ((vbe_mem_info.BitsPerPixel + 7) >> 3);
115
}
116

    
117
uint16_t get_XRes(void) {
118
    return vbe_mem_info.XResolution;
119
}
120

    
121
uint16_t get_YRes(void) {
122
    return vbe_mem_info.YResolution;
123
}
124

    
125
uint16_t get_bits_pixel(void) {
126
    return vbe_mem_info.BitsPerPixel;
127
}
128

    
129
uint16_t get_bytes_pixel(void) {
130
    return (vbe_mem_info.BitsPerPixel + 7) >> 3;
131
}
132

    
133
uint16_t get_RedMaskSize  (void){ return vbe_mem_info.RedMaskSize  ; }
134
uint16_t get_GreenMaskSize(void){ return vbe_mem_info.GreenMaskSize; }
135
uint16_t get_BlueMaskSize (void){ return vbe_mem_info.BlueMaskSize ; }
136

    
137
int (map_vram)(void) {
138
    int r;
139
    unsigned int vram_base = get_phys_addr();
140
    unsigned int vram_size = get_vram_size();
141
    if ((r = get_permission(vram_base, vram_size)))
142
        panic("%s: sys_privctl (ADD MEM) failed: %d\n", __func__, r);
143

    
144
    video_mem = vm_map_phys(SELF, (void *)vram_base, vram_size);
145

    
146
    if (video_mem == MAP_FAILED)
147
        panic("%s: couldn't map video memory.", __func__);
148

    
149
    return SUCCESS;
150
}
151

    
152
int (free_memory)(void) {
153
    return !lm_free(&mem_map);
154
}
155

    
156
int (set_pixel)(uint16_t x, uint16_t y, uint32_t color) {
157
    if (x >= vbe_mem_info.XResolution || y >= vbe_mem_info.YResolution) {
158
        printf("%s: invalid pixel.\n", __func__);
159
        return OUT_OF_RANGE;
160
    }
161
    unsigned int pos = (x + y * vbe_mem_info.XResolution) * get_bytes_pixel();
162
    memcpy((void*)((unsigned int)video_mem + pos), &color, get_bytes_pixel());
163
    return SUCCESS;
164
}
165

    
166
int (set_graphics_mode)(uint16_t mode) {
167
    struct reg86 reg_86;
168

    
169
    memset(&reg_86, 0, sizeof(struct reg86)); // reset struct
170

    
171
    // Set Reg86
172
    reg_86.intno = VC_BIOS_SERV;
173
    reg_86.ah = VBE_CALL;
174
    reg_86.al = SET_VBE_MD;
175
    reg_86.bx = mode | LINEAR_FRAME_BUFFER_MD;
176

    
177
    // BIOS CALL
178
    if (sys_int86(&reg_86)) {
179
        printf("%s: sys_int86 failed\n", __func__);
180
        return BIOS_CALL_ERROR;
181
    }
182

    
183
    return SUCCESS;
184
}
185

    
186
int (draw_hline)(uint16_t x, uint16_t y, uint16_t len, uint32_t color){
187
    int r;
188
    for (uint16_t i = 0; i < len; i++)
189
        if ((r = set_pixel(x + i, y, color))) return r;
190
    return SUCCESS;
191
}
192
int (vg_draw_hline)(uint16_t x, uint16_t y, uint16_t len, uint32_t color){
193
    return draw_hline(x,y,len,color);
194
}
195

    
196
int (draw_rectangle)(uint16_t x, uint16_t y,uint16_t width, uint16_t height, uint32_t color)        {
197
    int r;
198
    for (uint16_t i = 0; i < height; i++)
199
        if ((r = draw_hline(x, y + i, width, color))) return r;
200
    return SUCCESS;
201
}
202
int (vg_draw_rectangle)(uint16_t x, uint16_t y,uint16_t width, uint16_t height, uint32_t color){
203
    return draw_rectangle(x,y,width,height, color);
204
}