Project

General

Profile

Revision 168

stuff changed

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 (graph_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 (graph_get_phys_addr)    (void){ return vbe_mem_info.PhysBasePtr; }
127
unsigned   (graph_get_vram_size)    (void){ return vbe_mem_info.XResolution * vbe_mem_info.YResolution * ((vbe_mem_info.BitsPerPixel + 7) >> 3); }
128
uint16_t   (graph_get_XRes)         (void){ return vbe_mem_info.XResolution; }
129
uint16_t   (graph_get_YRes)         (void){ return vbe_mem_info.YResolution; }
130
uint16_t   (graph_get_bits_pixel)   (void){ return vbe_mem_info.BitsPerPixel; }
131
uint16_t   (graph_get_bytes_pixel)  (void){ return (vbe_mem_info.BitsPerPixel + 7) >> 3; }
132
uint16_t   (graph_get_RedMaskSize)  (void){ return vbe_mem_info.RedMaskSize  ; }
133
uint16_t   (graph_get_GreenMaskSize)(void){ return vbe_mem_info.GreenMaskSize; }
134
uint16_t   (graph_get_BlueMaskSize) (void){ return vbe_mem_info.BlueMaskSize ; }
135

  
136
int (graph_map_vram)(void) {
137
    int r;
138
    unsigned int vram_base = graph_get_phys_addr();
139
    unsigned int vram_size = graph_get_vram_size();
140
    if ((r = get_permission(vram_base, vram_size))) {
141
        if (graph_free_memory_map()) {
142
            printf("%s: lm_free failed\n", __func__);
143
        }
144
        panic("%s: sys_privctl (ADD MEM) failed: %d\n", __func__, r);
145
    }
146

  
147
    video_mem = vm_map_phys(SELF, (void *)vram_base, vram_size);
148

  
149
    if (video_mem == MAP_FAILED) {
150
        if (graph_free_memory_map()) {
151
            printf("%s: lm_free failed\n", __func__);
152
        }
153
        panic("%s: couldn't map video memory.", __func__);
154
    }
155

  
156
    return SUCCESS;
157
}
158

  
159
int (graph_free_memory_map)(void) {
160
    return !lm_free(&mem_map);
161
}
162

  
163
int (graph_set_mode)(uint16_t mode) {
164
    struct reg86 reg_86;
165

  
166
    memset(&reg_86, 0, sizeof(struct reg86)); // reset struct
167

  
168
    // Set Reg86
169
    reg_86.intno = VC_BIOS_SERV;
170
    reg_86.ah = VBE_CALL;
171
    reg_86.al = SET_VBE_MD;
172
    reg_86.bx = mode | LINEAR_FRAME_BUFFER_MD;
173

  
174
    // BIOS CALL
175
    if (sys_int86(&reg_86) || reg_86.ah != AH_SUCCESS) {
176
        printf("%s: sys_int86 failed\n", __func__);
177
        return BIOS_CALL_ERROR;
178
    }
179

  
180
    return SUCCESS;
181
}
182

  
183
int (graph_set_pixel)(uint16_t x, uint16_t y, uint32_t color) {
184
    if (x >= vbe_mem_info.XResolution || y >= vbe_mem_info.YResolution) {
185
        printf("%s: invalid pixel.\n", __func__);
186
        return OUT_OF_RANGE;
187
    }
188
    unsigned int pos = (x + y * vbe_mem_info.XResolution) * graph_get_bytes_pixel();
189
    memcpy((void*)((unsigned int)video_mem + pos), &color, graph_get_bytes_pixel());
190
    return SUCCESS;
191
}
192
int (graph_set_pixel_alpha)(uint16_t x, uint16_t y, uint32_t color, uint8_t alpha){
193
    unsigned int pos = (x + y * vbe_mem_info.XResolution) * graph_get_bytes_pixel();
194
    uint32_t color_;
195
    memcpy(&color_, (void*)((unsigned int)video_mem + pos), graph_get_bytes_pixel());
196
    float a = 1.0-(alpha&0xFF)/(float)0xFF;
197
    uint8_t r = GET_RED(color)*a + GET_RED(color_)*(1.0-a);
198
    uint8_t g = GET_GRE(color)*a + GET_GRE(color_)*(1.0-a);
199
    uint8_t b = GET_BLU(color)*a + GET_BLU(color_)*(1.0-a);
200
    return graph_set_pixel(x,y,SET_RGB(r,g,b));
201
    //return set_pixel(x,y,color);
202
}
203

  
204
int (graph_draw_hline)(uint16_t x, uint16_t y, uint16_t len, uint32_t color){
205
    int r;
206
    for (uint16_t i = 0; i < len; i++)
207
        if ((r = graph_set_pixel(x + i, y, color))) return r;
208
    return SUCCESS;
209
}
210
int (vg_draw_hline)(uint16_t x, uint16_t y, uint16_t len, uint32_t color){
211
    return graph_draw_hline(x,y,len,color);
212
}
213

  
214
int (graph_draw_rectangle)(uint16_t x, uint16_t y,uint16_t width, uint16_t height, uint32_t color)	{
215
    int r;
216
    for (uint16_t i = 0; i < height; i++)
217
        if ((r = graph_draw_hline(x, y + i, width, color))) return r;
218
    return SUCCESS;
219
}
220
int (vg_draw_rectangle)(uint16_t x, uint16_t y,uint16_t width, uint16_t height, uint32_t color){
221
    return graph_draw_rectangle(x,y,width,height, color);
222
}
223

  
224
int (graph_paint_screen)(uint32_t color){
225
    return graph_draw_rectangle(0,0,graph_get_XRes(),graph_get_YRes(),color);
226
}
227

  
228
int (graph_clear_screen)(void){
229
    return graph_paint_screen(BLACK);
230
}
231

  
232
int (graph_draw)(void){
233
    return 0;
234
}
235 0

  
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_ALP(n)          (0xFF & ((n) >> 24))
8
#define GET_RED(n)          (0xFF & ((n) >> 16))
9
#define GET_GRE(n)          (0xFF & ((n) >>  8))
10
#define GET_BLU(n)          (0xFF & (n      ))
11
#define SET_ALP(n)          (((n)&0xFF) << 24)
12
#define SET_RED(n)          (((n)&0xFF) << 16)
13
#define SET_GRE(n)          (((n)&0xFF) <<  8)
14
#define SET_BLU(n)          (((n)&0xFF)      )
15
#define SET_RGB(r,g,b)      (             SET_RED(r) | SET_GRE(g) | SET_BLU(b))
16
#define SET_ARGB(a,r,g,b)   (SET_ALP(a) | SET_RED(r) | SET_GRE(g) | SET_BLU(b))
17
#define FAR2PHYS(n)         ((((n)>>12) & 0xFFFFFFF0) + ((n) & 0x0000FFFF))
18

  
19
typedef struct __attribute__((packed)) {
20

  
21
    char        VbeSignature[4]     ;
22
    uint16_t    VbeVersion          ;
23
    uint32_t    OemStringPtr        ;
24
    uint8_t     Capabilities[4]     ;
25
    uint32_t    VideoModePtr        ;
26
    uint16_t    TotalMemory         ;
27

  
28
    uint16_t    OemSoftwareRev      ;
29
    uint32_t    OemVendorNamePtr    ;
30
    uint32_t    OemProductNamePtr   ;
31
    uint32_t    OemProductRevPtr    ;
32
    char        Reserved[222]       ;
33

  
34
    char        OemData[256]        ;
35
} VbeInfoBlock;
36

  
37
int (get_permission)(unsigned int base_addr, unsigned int size);
38

  
39
//int (get_permissions_first_mbyte)(void);
40

  
41
int (vbe_get_mode_information)(uint16_t mode);
42

  
43
int (vbe_get_controller_information)(vg_vbe_contr_info_t *info_p);
44

  
45
phys_bytes (graph_get_phys_addr)    (void);
46
unsigned   (graph_get_vram_size)    (void);
47
uint16_t   (graph_get_XRes)         (void);
48
uint16_t   (graph_get_YRes)         (void);
49
uint16_t   (graph_get_bits_pixel)   (void);
50
uint16_t   (graph_get_bytes_pixel)  (void);
51
uint16_t   (graph_get_RedMaskSize)  (void);
52
uint16_t   (graph_get_GreenMaskSize)(void);
53
uint16_t   (graph_get_BlueMaskSize) (void);
54

  
55
int (graph_map_vram)(void);
56

  
57
int (graph_free_memory_map)(void);
58

  
59
int (graph_set_pixel)      (uint16_t x, uint16_t y, uint32_t color);
60
int (graph_set_pixel_alpha)(uint16_t x, uint16_t y, uint32_t color, uint8_t alpha);
61

  
62
/**
63
 * @brief
64
 * @param mode
65
 * @return
66
 */
67
int (graph_set_mode)(uint16_t mode);
68

  
69
int (graph_draw_hline)(uint16_t x, uint16_t y, uint16_t len, uint32_t color);
70

  
71
int (graph_draw_rectangle)(uint16_t x, uint16_t y,uint16_t width, uint16_t height, uint32_t color);
72

  
73
int (graph_paint_screen)(uint32_t color);
74

  
75
int (graph_clear_screen)(void);
76

  
77
int (graph_draw)(void);
78

  
79
#endif /* end of include guard: GRAPHICS_H_INCLUDED */
80 0

  
proj/graphics_macros.h
1
#ifndef GRAPHICS_MACROS_H_INCLUDED
2
#define GRAPHICS_MACROS_H_INCLUDED
3

  
4
#define VC_BIOS_SERV  0x10 /** @brief Interrupt service of video card */
5
#define VBE_CALL      0x4F /** @brief VBE call function specifier */
6

  
7
#define MBYTE_BASE  0x0         /** @brief Base address (zero address) */
8
#define MBYTE_SIZE  0xFFFFF     /** @brief Size of a mebibyte */
9

  
10
// Graphics Functions
11
#define VBE_CTRL_INFO       0x00    /** @brief Get VBE Controller Information */
12
#define VBE_MD_INFO         0x01    /** @brief Get VBE Mode Information */
13
#define SET_VBE_MD          0x02    /** @brief Set VBE Mode */
14

  
15
// Error codes (AH)
16
#define AH_SUCCESS          0x00    /** @brief Success code on BIOS call */
17
#define AH_FUNC_CALL_FAIL   0x01    /** @brief Function call failed */
18
#define AH_FUNC_NOT_SUPP    0x02    /** @brief Function call is not supported in current HW configuration */
19
#define AH_FUNC_INVALID     0x03    /** @brief Invalid function in current video mode */
20

  
21
// Graphics modes
22
#define INDEXED_1024_768        0x105
23
#define DIRECT_640_480          0x110
24
#define DIRECT_800_600          0x115
25
#define DIRECT_1280_1024_565    0x11A
26
#define DIRECT_1280_1024_888    0x11B
27
#define LINEAR_FRAME_BUFFER_MD  BIT(14)
28

  
29
// Colors in RBG (8 bit)
30
#define BLACK               0x000000
31

  
32
#endif /* end of include guard: GRAPHICS_MACROS_H_INCLUDED */
33 0

  
proj/DR.mk
1 1
PROG=proj
2 2

  
3
SRCS= proj.c graph.c sprite.c kbc.c keyboard.c mouse.c utils.c timer.c interrupts_func.c proj_func.c
3
SRCS= proj.c graph.c sprite.c kbc.c keyboard.c mouse.c utils.c timer.c interrupts_func.c proj_func.c fast_math.c
4 4

  
5
CPPFLAGS += -pedantic -I./bmp -I./xpm -D DIOGO # -D __LCOM_OPTIMIZED_
5
CPPFLAGS += -pedantic -I./bmp -I./xpm -D DIOGO -D __LCOM_OPTIMIZED_
6 6

  
7 7
DPADD += ${LIBLCF}
8 8
LDADD += -llcf
proj/TB.mk
1 1
PROG=proj
2 2

  
3
SRCS= proj.c graph.c sprite.c kbc.c keyboard.c mouse.c utils.c timer.c interrupts_func.c proj_func.c
3
SRCS= proj.c graph.c sprite.c kbc.c keyboard.c mouse.c utils.c timer.c interrupts_func.c proj_func.c fast_math.c
4 4

  
5 5
CPPFLAGS += -pedantic -I./bmp -I./xpm -D TELMO #-D __LCOM_OPTIMIZED_
6 6

  
proj/fast_math.c
1
#include "fast_math.h"
2

  
3
#include <math.h>
4

  
5
double fm_sin(double x){
6
    if(x < 0.0)         return -fm_sin(-x);
7
    if(x > 2.0*M_PI) return fm_sin(x-2.0*M_PI);
8
    if(x > M_PI)      return -fm_sin(x-M_PI);
9
    if(x > 0.5*M_PI) x = M_PI - x;
10
    double x2 = x*x;
11
    double x3 = x*x2;
12
    double x5 = x3*x2;
13
    //double x7 = x5*x2;
14
    return x-x3*0.1666666666666666+x5*0.008333333333333333;//-x7*0.0001984126984127;
15
}
16

  
17
double fm_cos(double x){
18
    if(x < 0.0)         x = -x;
19
    if(x > 2.0*M_PI) return fm_cos(x-2.0*M_PI);
20
    if(x > M_PI)      x = 2.0*M_PI-x;
21
    if(x > 0.5*M_PI) return -fm_cos(M_PI-x);
22
    double x2 = x*x;
23
    double x4 = x2*x2;
24
    double x6 = x4*x2;
25
    //double x8 = x4*x4;
26
    return 1.0-x2*0.5+x4*0.041666666666666666-x6*0.00138888888888888888;//+x8*0.000024801587;
27
}
0 28

  
proj/fast_math.h
1
#ifndef FAST_MATH_H_INCLUDED
2
#define FAST_MATH_H_INCLUDED
3

  
4
double fm_sin(double x);
5
double fm_cos(double x);
6

  
7
#endif //FAST_MATH_H_INCLUDED
0 8

  
proj/graph.c
1
#include "graph.h"
2
#include "graph_macros.h"
3
#include "errors.h"
4

  
5
#include <lcom/lcf.h>
6

  
7
#include <stdio.h>
8

  
9
static void *video_mem = NULL; /** @brief Frame-buffer VM address. */
10
static uint8_t *video_buf = NULL; /** @brief Primary buffer for drawing before copying to video_mem. */
11
static vbe_mode_info_t vbe_mem_info;
12
static mmap_t mem_map;
13

  
14
int (get_permission)(unsigned int base_addr, unsigned int size) {
15
    struct minix_mem_range mmr;
16
    mmr.mr_base = base_addr;
17
    mmr.mr_limit = base_addr + size;
18
    return sys_privctl(SELF, SYS_PRIV_ADD_MEM, &mmr);
19
}
20

  
21
//int (get_permissions_first_mbyte)(void) {
22
//    return get_permission(MBYTE_BASE, MBYTE_SIZE);
23
//}
24

  
25
int (graph_init)(uint16_t mode){
26
    if (vbe_get_mode_information(mode)) {
27
        printf("%s: failed to get information for mode %x.\n", __func__, mode);
28
        return 1;
29
    }
30

  
31
    graph_map_vram(); // if function fails it aborts program
32

  
33
    if (graph_set_mode(mode)) {
34
        printf("%s: failed to set graphic mode %x.\n", __func__, mode);
35
        return 1;
36
    };
37
    return SUCCESS;
38
}
39

  
40
int (graph_cleanup)(void){
41
    int r = SUCCESS;
42
    if ((r = vg_exit()))
43
        printf("%s: vg_exit failed to exit to text mode.\n", __func__);
44
    if ((r = graph_free_memory()))
45
        printf("%s: lm_free failed\n", __func__);
46
    return r;
47
}
48

  
49
int (vbe_get_mode_information)(uint16_t mode) {
50
    memset(&vbe_mem_info, 0, sizeof(vbe_mode_info_t)); // reset values
51

  
52
    struct reg86 reg_86;
53
    memset(&reg_86, 0, sizeof(struct reg86)); // reset struct
54

  
55
    vbe_mode_info_t *virtual_addr = lm_alloc(sizeof(vbe_mode_info_t), &mem_map);
56

  
57
    reg_86.intno = VC_BIOS_SERV;
58
    reg_86.ah = VBE_CALL;
59
    reg_86.al = VBE_MD_INFO;
60
    reg_86.cx = mode;
61
    reg_86.es = PB2BASE(mem_map.phys);
62
    reg_86.di = PB2OFF(mem_map.phys);
63
    // BIOS CALL
64
    if (sys_int86(&reg_86) || reg_86.ah != AH_SUCCESS) {
65
        printf("%s: sys_int86 failed\n", __func__);
66
        if (graph_free_memory()) {
67
            printf("%s: lm_free failed\n", __func__);
68
        }
69
        return BIOS_CALL_ERROR;
70
    }
71

  
72
    memcpy((void*)&vbe_mem_info, (void*)virtual_addr, mem_map.size);
73
    return SUCCESS;
74
}
75

  
76
int (vbe_get_controller_information)(vg_vbe_contr_info_t *info_p) {
77
    memset(info_p, 0, sizeof(vg_vbe_contr_info_t)); // reset values
78

  
79
    mmap_t controller_map;
80

  
81
    struct reg86 reg_86;
82
    memset(&reg_86, 0, sizeof(struct reg86)); // reset struct
83

  
84
    VbeInfoBlock *virtual_addr = lm_alloc(sizeof(VbeInfoBlock), &controller_map);
85

  
86
    uint32_t virtual_base = (uint32_t)(virtual_addr) - controller_map.phys;
87

  
88
    virtual_addr->VbeSignature[0] = 'V';
89
    virtual_addr->VbeSignature[1] = 'B';
90
    virtual_addr->VbeSignature[2] = 'E';
91
    virtual_addr->VbeSignature[3] = '2';
92

  
93

  
94
    reg_86.intno = VC_BIOS_SERV;
95
    reg_86.ah = VBE_CALL;
96
    reg_86.al = VBE_CTRL_INFO;
97
    reg_86.es = PB2BASE(controller_map.phys);
98
    reg_86.di = PB2OFF(controller_map.phys);
99
    // BIOS CALL
100
    if (sys_int86(&reg_86) || reg_86.ah != AH_SUCCESS) {
101
        printf("%s: sys_int86 failed\n", __func__);
102
        if (!lm_free(&controller_map)) {
103
            printf("%s: lm_free failed\n", __func__);
104
        }
105
        return BIOS_CALL_ERROR;
106
    }
107

  
108
    info_p->VBESignature[0] = virtual_addr->VbeSignature[0];
109
    info_p->VBESignature[1] = virtual_addr->VbeSignature[1];
110
    info_p->VBESignature[2] = virtual_addr->VbeSignature[2];
111
    info_p->VBESignature[3] = virtual_addr->VbeSignature[3];
112

  
113
    uint8_t lsb, msb;
114
    util_get_LSB(virtual_addr->VbeVersion, &lsb);
115
    util_get_MSB(virtual_addr->VbeVersion, &msb);
116
    info_p->VBEVersion[0] = lsb;
117
    info_p->VBEVersion[1] = msb;
118

  
119
    info_p->TotalMemory = (virtual_addr->TotalMemory << 6);
120

  
121
    // Convert Far Far Pointer to Virtual Address
122

  
123
    uint32_t phys_ptr = FAR2PHYS(virtual_addr->OemStringPtr);
124
    uint32_t virtual_ptr = phys_ptr + virtual_base;
125
    info_p->OEMString = (char*)(virtual_ptr);
126

  
127
    phys_ptr = FAR2PHYS(virtual_addr->VideoModePtr);
128
    virtual_ptr = phys_ptr + virtual_base;
129
    info_p->VideoModeList = (uint16_t*)(virtual_ptr);
130

  
131
    phys_ptr = FAR2PHYS(virtual_addr->OemVendorNamePtr);
132
    virtual_ptr = phys_ptr + virtual_base;
133
    info_p->OEMVendorNamePtr = (char*)(virtual_ptr);
134

  
135
    phys_ptr = FAR2PHYS(virtual_addr->OemProductNamePtr);
136
    virtual_ptr = phys_ptr + virtual_base;
137
    info_p->OEMProductNamePtr = (char*)(virtual_ptr);
138

  
139
    phys_ptr = FAR2PHYS(virtual_addr->OemProductRevPtr);
140
    virtual_ptr = phys_ptr + virtual_base;
141
    info_p->OEMProductRevPtr = (char*)(virtual_ptr);
142

  
143
    if (!lm_free(&controller_map)) {
144
        printf("%s: lm_free failed\n", __func__);
145
        return LCF_ERROR;
146
    }
147

  
148
    return SUCCESS;
149
}
150

  
151
phys_bytes (graph_get_phys_addr)    (void){ return vbe_mem_info.PhysBasePtr; }
152
unsigned   (graph_get_vram_size)    (void){ return vbe_mem_info.XResolution * vbe_mem_info.YResolution * graph_get_bytes_pixel(); }
153
uint16_t   (graph_get_XRes)         (void){ return vbe_mem_info.XResolution; }
154
uint16_t   (graph_get_YRes)         (void){ return vbe_mem_info.YResolution; }
155
uint16_t   (graph_get_bits_pixel)   (void){ return vbe_mem_info.BitsPerPixel; }
156
uint16_t   (graph_get_bytes_pixel)  (void){ return (vbe_mem_info.BitsPerPixel + 7) >> 3; }
157
uint16_t   (graph_get_RedMaskSize)  (void){ return vbe_mem_info.RedMaskSize  ; }
158
uint16_t   (graph_get_GreenMaskSize)(void){ return vbe_mem_info.GreenMaskSize; }
159
uint16_t   (graph_get_BlueMaskSize) (void){ return vbe_mem_info.BlueMaskSize ; }
160

  
161
int (graph_map_vram)(void) {
162
    int r;
163
    const unsigned vram_base = graph_get_phys_addr();
164
    const unsigned vram_size = graph_get_vram_size();
165
    if ((r = get_permission(vram_base, vram_size))) {
166
        if (graph_free_memory()) {
167
            printf("%s: lm_free failed\n", __func__);
168
        }
169
        panic("%s: sys_privctl (ADD MEM) failed: %d\n", __func__, r);
170
    }
171

  
172
    video_mem = vm_map_phys(SELF, (void *)vram_base, vram_size);
173

  
174
    if (video_mem == MAP_FAILED) {
175
        if (graph_free_memory()) {
176
            printf("%s: lm_free failed\n", __func__);
177
        }
178
        panic("%s: couldn't map video memory.", __func__);
179
    }
180

  
181
    video_buf = malloc(vram_size);
182

  
183
    return SUCCESS;
184
}
185

  
186
int (graph_free_memory)(void) {
187
    int r = SUCCESS;
188
    free(video_buf); video_buf = NULL;
189
    r = !lm_free(&mem_map);
190
    return r;
191
}
192

  
193
int (graph_set_mode)(uint16_t mode) {
194
    struct reg86 reg_86;
195

  
196
    memset(&reg_86, 0, sizeof(struct reg86)); // reset struct
197

  
198
    // Set Reg86
199
    reg_86.intno = VC_BIOS_SERV;
200
    reg_86.ah = VBE_CALL;
201
    reg_86.al = SET_VBE_MD;
202
    reg_86.bx = mode | LINEAR_FRAME_BUFFER_MD;
203

  
204
    // BIOS CALL
205
    if (sys_int86(&reg_86) || reg_86.ah != AH_SUCCESS) {
206
        printf("%s: sys_int86 failed\n", __func__);
207
        return BIOS_CALL_ERROR;
208
    }
209

  
210
    return SUCCESS;
211
}
212

  
213
int (graph_set_pixel)(uint16_t x, uint16_t y, uint32_t color) {
214
    if (x >= vbe_mem_info.XResolution || y >= vbe_mem_info.YResolution) {
215
        printf("%s: invalid pixel.\n", __func__);
216
        return OUT_OF_RANGE;
217
    }
218
    unsigned int pos = (x + y * vbe_mem_info.XResolution) * graph_get_bytes_pixel();
219
    memcpy(video_buf + pos, &color, graph_get_bytes_pixel());
220
    return SUCCESS;
221
}
222
int (graph_set_pixel_alpha)(uint16_t x, uint16_t y, uint32_t color, uint8_t alpha){
223
    unsigned int pos = (x + y * vbe_mem_info.XResolution) * graph_get_bytes_pixel();
224
    uint32_t color_;
225
    memcpy(&color_, video_buf + pos, graph_get_bytes_pixel());
226
    float a = 1.0-(alpha&0xFF)/(float)0xFF;
227
    uint8_t r = GET_RED(color)*a + GET_RED(color_)*(1.0-a);
228
    uint8_t g = GET_GRE(color)*a + GET_GRE(color_)*(1.0-a);
229
    uint8_t b = GET_BLU(color)*a + GET_BLU(color_)*(1.0-a);
230
    return graph_set_pixel(x,y,SET_RGB(r,g,b));
231
    //return set_pixel(x,y,color);
232
}
233

  
234
int (graph_draw_hline)(uint16_t x, uint16_t y, uint16_t len, uint32_t color){
235
    int r;
236
    for (uint16_t i = 0; i < len; i++)
237
        if ((r = graph_set_pixel(x + i, y, color))) return r;
238
    return SUCCESS;
239
}
240
int (vg_draw_hline)(uint16_t x, uint16_t y, uint16_t len, uint32_t color){
241
    return graph_draw_hline(x,y,len,color);
242
}
243

  
244
int (graph_draw_rectangle)(uint16_t x, uint16_t y,uint16_t width, uint16_t height, uint32_t color)	{
245
    int r;
246
    for (uint16_t i = 0; i < height; i++)
247
        if ((r = graph_draw_hline(x, y + i, width, color))) return r;
248
    return SUCCESS;
249
}
250
int (vg_draw_rectangle)(uint16_t x, uint16_t y,uint16_t width, uint16_t height, uint32_t color){
251
    return graph_draw_rectangle(x,y,width,height, color);
252
}
253

  
254
int (graph_paint_screen)(uint32_t color){
255
    return graph_draw_rectangle(0,0,graph_get_XRes(),graph_get_YRes(),color);
256
}
257

  
258
int (graph_clear_screen)(void){
259
    //return graph_paint_screen(BLACK);
260
    memset(video_buf, 0, graph_get_vram_size());
261
    return SUCCESS;
262
}
263

  
264
int (graph_draw)(void){
265
    memcpy((uint8_t*)video_mem, video_buf, graph_get_vram_size());
266
    return 0;
267
}
0 268

  
proj/graph.h
1
#ifndef GRAPH_H_INCLUDED
2
#define GRAPH_H_INCLUDED
3

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

  
7
#define GET_ALP(n)          (0xFF & ((n) >> 24))
8
#define GET_RED(n)          (0xFF & ((n) >> 16))
9
#define GET_GRE(n)          (0xFF & ((n) >>  8))
10
#define GET_BLU(n)          (0xFF & (n      ))
11
#define SET_ALP(n)          (((n)&0xFF) << 24)
12
#define SET_RED(n)          (((n)&0xFF) << 16)
13
#define SET_GRE(n)          (((n)&0xFF) <<  8)
14
#define SET_BLU(n)          (((n)&0xFF)      )
15
#define SET_RGB(r,g,b)      (             SET_RED(r) | SET_GRE(g) | SET_BLU(b))
16
#define SET_ARGB(a,r,g,b)   (SET_ALP(a) | SET_RED(r) | SET_GRE(g) | SET_BLU(b))
17
#define FAR2PHYS(n)         ((((n)>>12) & 0xFFFFFFF0) + ((n) & 0x0000FFFF))
18

  
19
typedef struct __attribute__((packed)) {
20

  
21
    char        VbeSignature[4]     ;
22
    uint16_t    VbeVersion          ;
23
    uint32_t    OemStringPtr        ;
24
    uint8_t     Capabilities[4]     ;
25
    uint32_t    VideoModePtr        ;
26
    uint16_t    TotalMemory         ;
27

  
28
    uint16_t    OemSoftwareRev      ;
29
    uint32_t    OemVendorNamePtr    ;
30
    uint32_t    OemProductNamePtr   ;
31
    uint32_t    OemProductRevPtr    ;
32
    char        Reserved[222]       ;
33

  
34
    char        OemData[256]        ;
35
} VbeInfoBlock;
36

  
37
int (get_permission)(unsigned int base_addr, unsigned int size);
38

  
39
//int (get_permissions_first_mbyte)(void);
40

  
41
int (graph_init)(uint16_t mode);
42
int (graph_cleanup)(void);
43

  
44
int (vbe_get_mode_information)(uint16_t mode);
45

  
46
int (vbe_get_controller_information)(vg_vbe_contr_info_t *info_p);
47

  
48
phys_bytes (graph_get_phys_addr)    (void);
49
unsigned   (graph_get_vram_size)    (void);
50
uint16_t   (graph_get_XRes)         (void);
51
uint16_t   (graph_get_YRes)         (void);
52
uint16_t   (graph_get_bits_pixel)   (void);
53
uint16_t   (graph_get_bytes_pixel)  (void);
54
uint16_t   (graph_get_RedMaskSize)  (void);
55
uint16_t   (graph_get_GreenMaskSize)(void);
56
uint16_t   (graph_get_BlueMaskSize) (void);
57

  
58
int (graph_map_vram)(void);
59

  
60
int (graph_free_memory)(void);
61

  
62
int (graph_set_pixel)      (uint16_t x, uint16_t y, uint32_t color);
63
int (graph_set_pixel_alpha)(uint16_t x, uint16_t y, uint32_t color, uint8_t alpha);
64

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

  
72
int (graph_draw_hline)(uint16_t x, uint16_t y, uint16_t len, uint32_t color);
73

  
74
int (graph_draw_rectangle)(uint16_t x, uint16_t y,uint16_t width, uint16_t height, uint32_t color);
75

  
76
int (graph_paint_screen)(uint32_t color);
77

  
78
int (graph_clear_screen)(void);
79

  
80
int (graph_draw)(void);
81

  
82
#endif /* end of include guard: GRAPH_H_INCLUDED */
0 83

  
proj/graph_macros.h
1
#ifndef GRAPHICS_MACROS_H_INCLUDED
2
#define GRAPHICS_MACROS_H_INCLUDED
3

  
4
#define VC_BIOS_SERV  0x10 /** @brief Interrupt service of video card */
5
#define VBE_CALL      0x4F /** @brief VBE call function specifier */
6

  
7
#define MBYTE_BASE  0x0         /** @brief Base address (zero address) */
8
#define MBYTE_SIZE  0xFFFFF     /** @brief Size of a mebibyte */
9

  
10
// Graphics Functions
11
#define VBE_CTRL_INFO       0x00    /** @brief Get VBE Controller Information */
12
#define VBE_MD_INFO         0x01    /** @brief Get VBE Mode Information */
13
#define SET_VBE_MD          0x02    /** @brief Set VBE Mode */
14

  
15
// Error codes (AH)
16
#define AH_SUCCESS          0x00    /** @brief Success code on BIOS call */
17
#define AH_FUNC_CALL_FAIL   0x01    /** @brief Function call failed */
18
#define AH_FUNC_NOT_SUPP    0x02    /** @brief Function call is not supported in current HW configuration */
19
#define AH_FUNC_INVALID     0x03    /** @brief Invalid function in current video mode */
20

  
21
// Graphics modes
22
#define INDEXED_1024_768        0x105
23
#define DIRECT_640_480          0x110
24
#define DIRECT_800_600          0x115
25
#define DIRECT_1280_1024_565    0x11A
26
#define DIRECT_1280_1024_888    0x11B
27
#define LINEAR_FRAME_BUFFER_MD  BIT(14)
28

  
29
// Colors in RBG (8 bit)
30
#define BLACK               0x000000
31

  
32
#endif /* end of include guard: GRAPHICS_MACROS_H_INCLUDED */
0 33

  
proj/proj.c
22 22
#include "interrupts_func.h"
23 23
#include "proj_func.h"
24 24

  
25
#include "fast_math.h"
26
#include <math.h>
27

  
25 28
#ifdef DIOGO
26 29
    #include "shooter.h"
27 30
    #include "pistol.xpm"
......
50 53
    }
51 54

  
52 55
    #ifdef DIOGO
53
        graph_paint_screen(0x777777);
56
        printf("%d\n", 1000000-(int)(1000000*fm_sin(0.5*M_PI)));
57
        printf("%d\n", (int)(1000000*fm_cos(0.5*M_PI)));
58

  
59
        clock_t t = clock();
54 60
        sprite_t *shooter1 = get_shooter(); sprite_set_pos(shooter1, 100, 100);
55
        for(double angle = 0; angle < 6.29; angle += 0.01){
61
        for(double angle = 0; angle <= 6.283185; angle += 0.006283185){
56 62
             sprite_set_angle(shooter1, angle);
57
             //paint_screen(0x777777);
63
             //graph_paint_screen(0x777777);
64
             graph_clear_screen();
58 65
             sprite_draw(shooter1);
59
             tickdelay(micros_to_ticks(10000));
66
             graph_draw();
60 67
        }
68
        t = clock() - t; //printf("%d\n", CLOCKS_PER_SEC);
69
        //double dt = ((double)t)/(double)CLOCKS_PER_SEC;
70
        printf("Time taken: %d/%d \n", t, CLOCKS_PER_SEC);
61 71
        sprite_dtor(shooter1);
62
        graph_draw();
63 72
    #endif
64 73

  
65 74
    /// loop stuff
......
75 84

  
76 85
    /// cycle
77 86
    int good = 1;
87

  
88
    #ifdef DIOGO
89
        good = 0;
90
    #endif
91

  
78 92
    while (good) {
79 93
        /* Get a request message. */
80 94
        if ((r = driver_receive(ANY, &msg, &ipc_status)) != 0) {
proj/sprite.c
4 4

  
5 5
#include "graph.h"
6 6
#include "utils.h"
7
#include "fast_math.h"
7 8
#include <math.h>
8 9

  
9 10
struct sprite{
10 11
    int16_t x, y;
11 12
    uint16_t w, h;
12 13
    int16_t u0, v0;
13
    double theta;
14
    float theta;
14 15
    uint8_t *map;
15 16
};
16 17

  
......
53 54
void (sprite_src2pic)(const sprite_t *p, int16_t x, int16_t y, int16_t *u, int16_t *v){
54 55
    int16_t dx = x - p->x;
55 56
    int16_t dy = y - p->y;
56
    double s = sin(p->theta);
57
    double c = cos(p->theta);
57
    double s = fm_sin(p->theta);
58
    double c = fm_cos(p->theta);
58 59
    *u = dx*c - dy*s + p->u0;
59 60
    *v = dx*s + dy*c + p->v0;
60 61
}

Also available in: Unified diff