root / proj / src / graph.c @ 178
History | View | Annotate | Download (9.49 KB)
1 | 178 | up20180642 | #include <lcom/lcf.h> |
---|---|---|---|
2 | |||
3 | 168 | up20180642 | #include "graph.h" |
4 | #include "graph_macros.h" |
||
5 | #include "errors.h" |
||
6 | |||
7 | #include <stdio.h> |
||
8 | |||
9 | 174 | up20180642 | /// MACROS
|
10 | #define FAR2PHYS(n) ((((n)>>12) & 0xFFFFFFF0) + ((n) & 0x0000FFFF)) |
||
11 | |||
12 | /// STRUCT
|
||
13 | typedef struct __attribute__((packed)) { |
||
14 | |||
15 | char VbeSignature[4] ; |
||
16 | uint16_t VbeVersion ; |
||
17 | uint32_t OemStringPtr ; |
||
18 | uint8_t Capabilities[4] ;
|
||
19 | uint32_t VideoModePtr ; |
||
20 | uint16_t TotalMemory ; |
||
21 | |||
22 | uint16_t OemSoftwareRev ; |
||
23 | uint32_t OemVendorNamePtr ; |
||
24 | uint32_t OemProductNamePtr ; |
||
25 | uint32_t OemProductRevPtr ; |
||
26 | char Reserved[222] ; |
||
27 | |||
28 | char OemData[256] ; |
||
29 | } VbeInfoBlock; |
||
30 | |||
31 | 168 | up20180642 | static vbe_mode_info_t vbe_mem_info;
|
32 | |||
33 | 174 | up20180642 | /// PUBLIC GET
|
34 | uint16_t (graph_get_XRes) (void){ return vbe_mem_info.XResolution; } |
||
35 | uint16_t (graph_get_YRes) (void){ return vbe_mem_info.YResolution; } |
||
36 | |||
37 | /// PRIVATE GET
|
||
38 | static uint16_t (graph_get_bits_pixel) (void){ return vbe_mem_info.BitsPerPixel; } |
||
39 | static uint16_t (graph_get_bytes_pixel) (void){ return (graph_get_bits_pixel() + 7) >> 3; } |
||
40 | static phys_bytes (graph_get_phys_addr) (void){ return vbe_mem_info.PhysBasePtr; } |
||
41 | static unsigned (graph_get_vram_size) (void){ return vbe_mem_info.XResolution * vbe_mem_info.YResolution * graph_get_bytes_pixel(); } |
||
42 | //static uint16_t (graph_get_RedMaskSize) (void){ return vbe_mem_info.RedMaskSize ; }
|
||
43 | //static uint16_t (graph_get_GreenMaskSize)(void){ return vbe_mem_info.GreenMaskSize; }
|
||
44 | //static uint16_t (graph_get_BlueMaskSize) (void){ return vbe_mem_info.BlueMaskSize ; }
|
||
45 | |||
46 | static int (get_permission)(unsigned int base_addr, unsigned int size) { |
||
47 | 168 | up20180642 | struct minix_mem_range mmr;
|
48 | mmr.mr_base = base_addr; |
||
49 | mmr.mr_limit = base_addr + size; |
||
50 | return sys_privctl(SELF, SYS_PRIV_ADD_MEM, &mmr);
|
||
51 | } |
||
52 | |||
53 | 174 | up20180642 | //static int (get_permissions_first_mbyte)(void) {
|
54 | 168 | up20180642 | // return get_permission(MBYTE_BASE, MBYTE_SIZE);
|
55 | //}
|
||
56 | |||
57 | 174 | up20180642 | /// MEMORY
|
58 | 178 | up20180642 | static uint8_t *video_mem = NULL; /** @brief Frame-buffer VM address. */ |
59 | 174 | up20180642 | static uint8_t *video_buf = NULL; /** @brief Primary buffer for drawing before copying to video_mem. */ |
60 | static mmap_t mem_map;
|
||
61 | static int (graph_free_memory)(void) { |
||
62 | int r = SUCCESS;
|
||
63 | free(video_buf); video_buf = NULL;
|
||
64 | r = !lm_free(&mem_map); |
||
65 | return r;
|
||
66 | } |
||
67 | static int (graph_map_vram)(void) { |
||
68 | int r;
|
||
69 | const unsigned vram_base = graph_get_phys_addr(); |
||
70 | const unsigned vram_size = graph_get_vram_size(); |
||
71 | if ((r = get_permission(vram_base, vram_size))) {
|
||
72 | if (graph_free_memory()) {
|
||
73 | printf("%s: lm_free failed\n", __func__);
|
||
74 | } |
||
75 | panic("%s: sys_privctl (ADD MEM) failed: %d\n", __func__, r);
|
||
76 | 168 | up20180642 | } |
77 | |||
78 | 174 | up20180642 | video_mem = vm_map_phys(SELF, (void *)vram_base, vram_size);
|
79 | 168 | up20180642 | |
80 | 174 | up20180642 | if (video_mem == MAP_FAILED) {
|
81 | if (graph_free_memory()) {
|
||
82 | printf("%s: lm_free failed\n", __func__);
|
||
83 | } |
||
84 | panic("%s: couldn't map video memory.", __func__);
|
||
85 | } |
||
86 | |||
87 | video_buf = malloc(vram_size); |
||
88 | |||
89 | 168 | up20180642 | return SUCCESS;
|
90 | } |
||
91 | |||
92 | 174 | up20180642 | /// INFO GET
|
93 | static int (vbe_get_mode_information)(uint16_t mode) { |
||
94 | 168 | up20180642 | memset(&vbe_mem_info, 0, sizeof(vbe_mode_info_t)); // reset values |
95 | |||
96 | struct reg86 reg_86;
|
||
97 | memset(®_86, 0, sizeof(struct reg86)); // reset struct |
||
98 | |||
99 | vbe_mode_info_t *virtual_addr = lm_alloc(sizeof(vbe_mode_info_t), &mem_map);
|
||
100 | |||
101 | reg_86.intno = VC_BIOS_SERV; |
||
102 | reg_86.ah = VBE_CALL; |
||
103 | reg_86.al = VBE_MD_INFO; |
||
104 | reg_86.cx = mode; |
||
105 | reg_86.es = PB2BASE(mem_map.phys); |
||
106 | reg_86.di = PB2OFF(mem_map.phys); |
||
107 | // BIOS CALL
|
||
108 | if (sys_int86(®_86) || reg_86.ah != AH_SUCCESS) {
|
||
109 | printf("%s: sys_int86 failed\n", __func__);
|
||
110 | if (graph_free_memory()) {
|
||
111 | printf("%s: lm_free failed\n", __func__);
|
||
112 | } |
||
113 | return BIOS_CALL_ERROR;
|
||
114 | } |
||
115 | |||
116 | memcpy((void*)&vbe_mem_info, (void*)virtual_addr, mem_map.size); |
||
117 | return SUCCESS;
|
||
118 | } |
||
119 | 174 | up20180642 | /*
|
120 | static int (vbe_get_controller_information)(vg_vbe_contr_info_t *info_p) {
|
||
121 | 168 | up20180642 | memset(info_p, 0, sizeof(vg_vbe_contr_info_t)); // reset values
|
122 | |||
123 | mmap_t controller_map;
|
||
124 | |||
125 | struct reg86 reg_86;
|
||
126 | memset(®_86, 0, sizeof(struct reg86)); // reset struct
|
||
127 | |||
128 | VbeInfoBlock *virtual_addr = lm_alloc(sizeof(VbeInfoBlock), &controller_map);
|
||
129 | |||
130 | uint32_t virtual_base = (uint32_t)(virtual_addr) - controller_map.phys;
|
||
131 | |||
132 | virtual_addr->VbeSignature[0] = 'V';
|
||
133 | virtual_addr->VbeSignature[1] = 'B';
|
||
134 | virtual_addr->VbeSignature[2] = 'E';
|
||
135 | virtual_addr->VbeSignature[3] = '2';
|
||
136 | |||
137 | |||
138 | reg_86.intno = VC_BIOS_SERV;
|
||
139 | reg_86.ah = VBE_CALL;
|
||
140 | reg_86.al = VBE_CTRL_INFO;
|
||
141 | reg_86.es = PB2BASE(controller_map.phys);
|
||
142 | reg_86.di = PB2OFF(controller_map.phys);
|
||
143 | // BIOS CALL
|
||
144 | if (sys_int86(®_86) || reg_86.ah != AH_SUCCESS) {
|
||
145 | printf("%s: sys_int86 failed\n", __func__);
|
||
146 | if (!lm_free(&controller_map)) {
|
||
147 | printf("%s: lm_free failed\n", __func__);
|
||
148 | }
|
||
149 | return BIOS_CALL_ERROR;
|
||
150 | }
|
||
151 | |||
152 | info_p->VBESignature[0] = virtual_addr->VbeSignature[0];
|
||
153 | info_p->VBESignature[1] = virtual_addr->VbeSignature[1];
|
||
154 | info_p->VBESignature[2] = virtual_addr->VbeSignature[2];
|
||
155 | info_p->VBESignature[3] = virtual_addr->VbeSignature[3];
|
||
156 | |||
157 | uint8_t lsb, msb;
|
||
158 | util_get_LSB(virtual_addr->VbeVersion, &lsb);
|
||
159 | util_get_MSB(virtual_addr->VbeVersion, &msb);
|
||
160 | info_p->VBEVersion[0] = lsb;
|
||
161 | info_p->VBEVersion[1] = msb;
|
||
162 | |||
163 | info_p->TotalMemory = (virtual_addr->TotalMemory << 6);
|
||
164 | |||
165 | // Convert Far Far Pointer to Virtual Address
|
||
166 | |||
167 | uint32_t phys_ptr = FAR2PHYS(virtual_addr->OemStringPtr);
|
||
168 | uint32_t virtual_ptr = phys_ptr + virtual_base;
|
||
169 | info_p->OEMString = (char*)(virtual_ptr);
|
||
170 | |||
171 | phys_ptr = FAR2PHYS(virtual_addr->VideoModePtr);
|
||
172 | virtual_ptr = phys_ptr + virtual_base;
|
||
173 | info_p->VideoModeList = (uint16_t*)(virtual_ptr);
|
||
174 | |||
175 | phys_ptr = FAR2PHYS(virtual_addr->OemVendorNamePtr);
|
||
176 | virtual_ptr = phys_ptr + virtual_base;
|
||
177 | info_p->OEMVendorNamePtr = (char*)(virtual_ptr);
|
||
178 | |||
179 | phys_ptr = FAR2PHYS(virtual_addr->OemProductNamePtr);
|
||
180 | virtual_ptr = phys_ptr + virtual_base;
|
||
181 | info_p->OEMProductNamePtr = (char*)(virtual_ptr);
|
||
182 | |||
183 | phys_ptr = FAR2PHYS(virtual_addr->OemProductRevPtr);
|
||
184 | virtual_ptr = phys_ptr + virtual_base;
|
||
185 | info_p->OEMProductRevPtr = (char*)(virtual_ptr);
|
||
186 | |||
187 | if (!lm_free(&controller_map)) {
|
||
188 | printf("%s: lm_free failed\n", __func__);
|
||
189 | return LCF_ERROR;
|
||
190 | }
|
||
191 | |||
192 | return SUCCESS;
|
||
193 | }
|
||
194 | 174 | up20180642 | */
|
195 | 168 | up20180642 | |
196 | 174 | up20180642 | /// INIT
|
197 | /**
|
||
198 | * @brief
|
||
199 | * @param mode
|
||
200 | * @return
|
||
201 | */
|
||
202 | static int (graph_set_mode)(uint16_t mode) { |
||
203 | 168 | up20180642 | struct reg86 reg_86;
|
204 | |||
205 | memset(®_86, 0, sizeof(struct reg86)); // reset struct |
||
206 | |||
207 | // Set Reg86
|
||
208 | reg_86.intno = VC_BIOS_SERV; |
||
209 | reg_86.ah = VBE_CALL; |
||
210 | reg_86.al = SET_VBE_MD; |
||
211 | reg_86.bx = mode | LINEAR_FRAME_BUFFER_MD; |
||
212 | |||
213 | // BIOS CALL
|
||
214 | if (sys_int86(®_86) || reg_86.ah != AH_SUCCESS) {
|
||
215 | printf("%s: sys_int86 failed\n", __func__);
|
||
216 | return BIOS_CALL_ERROR;
|
||
217 | } |
||
218 | |||
219 | return SUCCESS;
|
||
220 | } |
||
221 | 174 | up20180642 | int (graph_init)(uint16_t mode){
|
222 | if (vbe_get_mode_information(mode)) {
|
||
223 | printf("%s: failed to get information for mode %x.\n", __func__, mode);
|
||
224 | return 1; |
||
225 | } |
||
226 | 168 | up20180642 | |
227 | 174 | up20180642 | graph_map_vram(); // if function fails it aborts program
|
228 | |||
229 | if (graph_set_mode(mode)) {
|
||
230 | printf("%s: failed to set graphic mode %x.\n", __func__, mode);
|
||
231 | return 1; |
||
232 | }; |
||
233 | return SUCCESS;
|
||
234 | } |
||
235 | |||
236 | /// CLEANUP
|
||
237 | int (graph_cleanup)(void){ |
||
238 | int r = SUCCESS;
|
||
239 | if ((r = vg_exit()))
|
||
240 | printf("%s: vg_exit failed to exit to text mode.\n", __func__);
|
||
241 | if ((r = graph_free_memory()))
|
||
242 | printf("%s: lm_free failed\n", __func__);
|
||
243 | return r;
|
||
244 | } |
||
245 | |||
246 | /// PIXEL DRAWING
|
||
247 | 168 | up20180642 | int (graph_set_pixel)(uint16_t x, uint16_t y, uint32_t color) {
|
248 | if (x >= vbe_mem_info.XResolution || y >= vbe_mem_info.YResolution) {
|
||
249 | 173 | up20180655 | //printf("%s: invalid pixel.\n", __func__);
|
250 | 168 | up20180642 | return OUT_OF_RANGE;
|
251 | } |
||
252 | unsigned int pos = (x + y * vbe_mem_info.XResolution) * graph_get_bytes_pixel(); |
||
253 | memcpy(video_buf + pos, &color, graph_get_bytes_pixel()); |
||
254 | return SUCCESS;
|
||
255 | } |
||
256 | int (graph_set_pixel_alpha)(uint16_t x, uint16_t y, uint32_t color, uint8_t alpha){
|
||
257 | 173 | up20180655 | if (x >= vbe_mem_info.XResolution || y >= vbe_mem_info.YResolution) {
|
258 | //printf("%s: invalid pixel.\n", __func__);
|
||
259 | return OUT_OF_RANGE;
|
||
260 | } |
||
261 | 168 | up20180642 | unsigned int pos = (x + y * vbe_mem_info.XResolution) * graph_get_bytes_pixel(); |
262 | uint32_t color_; |
||
263 | memcpy(&color_, video_buf + pos, graph_get_bytes_pixel()); |
||
264 | float a = 1.0-(alpha&0xFF)/(float)0xFF; |
||
265 | uint8_t r = GET_RED(color)*a + GET_RED(color_)*(1.0-a); |
||
266 | uint8_t g = GET_GRE(color)*a + GET_GRE(color_)*(1.0-a); |
||
267 | uint8_t b = GET_BLU(color)*a + GET_BLU(color_)*(1.0-a); |
||
268 | return graph_set_pixel(x,y,SET_RGB(r,g,b));
|
||
269 | //return set_pixel(x,y,color);
|
||
270 | } |
||
271 | |||
272 | 174 | up20180642 | int (graph_clear_screen)(void){ |
273 | //return graph_paint_screen(BLACK);
|
||
274 | memset(video_buf, 0, graph_get_vram_size());
|
||
275 | return SUCCESS;
|
||
276 | } |
||
277 | |||
278 | int (graph_draw)(void){ |
||
279 | 178 | up20180642 | memcpy(video_mem, video_buf, graph_get_vram_size()); |
280 | 174 | up20180642 | return 0; |
281 | } |
||
282 | |||
283 | /// RECTANGLE
|
||
284 | 168 | up20180642 | int (graph_draw_hline)(uint16_t x, uint16_t y, uint16_t len, uint32_t color){
|
285 | int r;
|
||
286 | for (uint16_t i = 0; i < len; i++) |
||
287 | if ((r = graph_set_pixel(x + i, y, color))) return r; |
||
288 | return SUCCESS;
|
||
289 | } |
||
290 | |||
291 | int (graph_draw_rectangle)(uint16_t x, uint16_t y,uint16_t width, uint16_t height, uint32_t color) {
|
||
292 | int r;
|
||
293 | for (uint16_t i = 0; i < height; i++) |
||
294 | if ((r = graph_draw_hline(x, y + i, width, color))) return r; |
||
295 | return SUCCESS;
|
||
296 | } |