root / lab5 / graphics.c @ 104
History | View | Annotate | Download (3.24 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 | 99 | up20180655 | 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(®_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(®_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 | 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 | 103 | up20180655 | phys_bytes get_phys_addr(void) {
|
53 | return vbe_mem_info.PhysBasePtr;
|
||
54 | } |
||
55 | |||
56 | unsigned int get_vram_size(void) { |
||
57 | return vbe_mem_info.XResolution * vbe_mem_info.YResolution * ((vbe_mem_info.BitsPerPixel + 7) >> 3); |
||
58 | } |
||
59 | |||
60 | uint16_t get_XRes(void) {
|
||
61 | return vbe_mem_info.XResolution;
|
||
62 | } |
||
63 | |||
64 | uint16_t get_YRes(void) {
|
||
65 | return vbe_mem_info.YResolution;
|
||
66 | } |
||
67 | |||
68 | uint16_t get_bits_pixel(void) {
|
||
69 | return vbe_mem_info.BitsPerPixel;
|
||
70 | } |
||
71 | |||
72 | int (map_vram)(void) { |
||
73 | 99 | up20180655 | int r;
|
74 | 103 | up20180655 | unsigned int vram_base = get_phys_addr(); |
75 | unsigned int vram_size = get_vram_size(); |
||
76 | 99 | up20180655 | if ((r = get_permission(vram_base, vram_size)))
|
77 | panic("%s: sys_privctl (ADD MEM) failed: %d\n", __func__, r);
|
||
78 | 95 | up20180655 | |
79 | 99 | up20180655 | video_mem = vm_map_phys(SELF, (void *)vram_base, vram_size);
|
80 | 95 | up20180655 | |
81 | if (video_mem == MAP_FAILED)
|
||
82 | 99 | up20180655 | panic("%s: couldn't map video memory.", __func__);
|
83 | 95 | up20180655 | |
84 | 99 | up20180655 | return SUCCESS;
|
85 | } |
||
86 | |||
87 | int (free_memory)(void) { |
||
88 | 103 | up20180655 | return !lm_free(&mem_map);
|
89 | 99 | up20180655 | } |
90 | |||
91 | int (set_graphics_mode)(uint16_t mode) {
|
||
92 | struct reg86 reg_86;
|
||
93 | |||
94 | 103 | up20180655 | memset(®_86, 0, sizeof(struct reg86)); // reset struct |
95 | 96 | up20180655 | |
96 | 99 | up20180655 | // Set Reg86
|
97 | 98 | up20180655 | reg_86.intno = VC_BIOS_SERV; |
98 | reg_86.ah = VBE_CALL; |
||
99 | reg_86.al = SET_VBE_MD; |
||
100 | reg_86.bx = mode | LINEAR_FRAME_BUFFER_MD; |
||
101 | 96 | up20180655 | |
102 | 98 | up20180655 | // BIOS CALL
|
103 | if (sys_int86(®_86)) {
|
||
104 | printf("%s: sys_int86 failed\n", __func__);
|
||
105 | return BIOS_CALL_ERROR;
|
||
106 | } |
||
107 | 99 | up20180655 | |
108 | 95 | up20180655 | return SUCCESS;
|
109 | } |
||
110 | 102 | up20180642 | |
111 | int (vg_draw_hline)(uint16_t x, uint16_t y, uint16_t len, uint32_t color){
|
||
112 | 104 | up20180642 | |
113 | //uint8_t color8 = color;
|
||
114 | uint8_t *p = video_mem; |
||
115 | p += y*1024+x;
|
||
116 | //*(p++) = color8;
|
||
117 | //*(p++) = color8;
|
||
118 | //*(p++) = color8;
|
||
119 | 102 | up20180642 | /*
|
120 | 104 | up20180642 | for(void *p = video_mem; p-video_mem < len; ++p){
|
121 | *p = color;
|
||
122 | }
|
||
123 | */
|
||
124 | /*
|
||
125 | 102 | up20180642 | for(uint16_t l = 0; l < len; ++l){
|
126 | 104 | up20180642 | *((uint8_t*)(video_mem)+y*1024+x+len) = color8;
|
127 | }
|
||
128 | */
|
||
129 | 102 | up20180642 | return 0; |
130 | } |