Project

General

Profile

Revision 146

add files

View differences:

proj/graphics.c
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
}
0 239

  
proj/graphics.h
1
#ifndef GRAPHICS_H_INCLUDED
2
#define GRAPHICS_H_INCLUDED
3

  
4
#include <lcom/lcf.h>
5
#include <stdint.h>
6

  
7
#define GET_RED(n)          (0xFF & ((n) >> 16))
8
#define GET_GRE(n)          (0xFF & ((n) >>  8))
9
#define GET_BLU(n)          (0xFF & (n      ))
10
#define SET_RED(n)          (((n)&0xFF) << 16)
11
#define SET_GRE(n)          (((n)&0xFF) <<  8)
12
#define SET_BLU(n)          (((n)&0xFF)      )
13
#define SET_COLOR(r,g,b)    (SET_RED(r) | SET_GRE(g) | SET_BLU(b))
14
#define FAR2PHYS(n)         ((((n)>>12) & 0xFFFFFFF0) + ((n) & 0x0000FFFF))
15

  
16
typedef struct __attribute__((packed)) {
17

  
18
    char        VbeSignature[4]     ;
19
    uint16_t    VbeVersion          ;
20
    uint32_t    OemStringPtr        ;
21
    uint8_t     Capabilities[4]     ;
22
    uint32_t    VideoModePtr        ;
23
    uint16_t    TotalMemory         ;
24

  
25
    uint16_t    OemSoftwareRev      ;
26
    uint32_t    OemVendorNamePtr    ;
27
    uint32_t    OemProductNamePtr   ;
28
    uint32_t    OemProductRevPtr    ;
29
    char        Reserved[222]       ;
30

  
31
    char        OemData[256]        ;
32
} VbeInfoBlock;
33

  
34
int (get_permission)(unsigned int base_addr, unsigned int size);
35

  
36
//int (get_permissions_first_mbyte)(void);
37

  
38
int (vbe_get_mode_information)(uint16_t mode);
39

  
40
int (vbe_get_controller_information)(vg_vbe_contr_info_t *info_p);
41

  
42
phys_bytes get_phys_addr(void);
43

  
44
unsigned int get_vram_size(void);
45

  
46
uint16_t get_XRes(void);
47

  
48
uint16_t get_YRes(void);
49

  
50
uint16_t get_bits_pixel(void);
51

  
52
uint16_t get_bytes_pixel(void);
53

  
54
uint16_t get_RedMaskSize(void);
55

  
56
uint16_t get_GreenMaskSize(void);
57

  
58
uint16_t get_BlueMaskSize(void);
59

  
60
int (map_vram)(void);
61

  
62
int (free_memory_map)(void);
63

  
64
int (set_pixel)(uint16_t row, uint16_t col, uint32_t color);
65

  
66
/**
67
 * @brief
68
 * @param mode
69
 * @return
70
 */
71
int (set_graphics_mode)(uint16_t mode);
72

  
73
int (draw_hline)(uint16_t x, uint16_t y, uint16_t len, uint32_t color);
74

  
75
int (draw_rectangle)(uint16_t x, uint16_t y,uint16_t width, uint16_t height, uint32_t color);
76

  
77
int (paint_screen)(uint32_t color);
78

  
79
int (clear_screen)();
80

  
81
#endif /* end of include guard: GRAPHICS_H_INCLUDED */
0 82

  
proj/timer.c
1
#include <lcom/lcf.h>
2

  
3
#include "timer.h"
4

  
5
#include "i8254.h"
6

  
7
int (subscribe_timer_interrupt)(uint8_t interrupt_bit, int *interrupt_id) {
8
    if (interrupt_id == NULL) return 1;
9
    *interrupt_id = interrupt_bit;
10
    return (sys_irqsetpolicy(TIMER0_IRQ, IRQ_REENABLE, interrupt_id));
11
}
12

  
13
uint32_t no_interrupts = 0;
14
void (timer_int_handler)() {
15
    no_interrupts++;
16
}
0 17

  
proj/timer.h
1
/**
2
 * This file concerns everything related to the timer
3
 */
4

  
5
#ifndef TIMER_H_INCLUDED
6
#define TIMER_H_INCLUDED
7

  
8
#include <stdint.h>
9

  
10
int (subscribe_timer_interrupt)(uint8_t interrupt_bit, int *interrupt_id);
11

  
12
uint32_t no_interrupts;
13

  
14
#endif //TIMER_H_INCLUDED
0 15

  

Also available in: Unified diff