12 #define REALPTR(s, o) (void*)(((uint32_t)(s) << 4) + (uint32_t)(o))
13 #define VBEPTR(x) REALPTR(((x) & 0xffff0000) >> 16, (x) & 0xffff)
14 #define VBEPTR_SEG(x) (((x) & 0xffff0000) >> 16)
15 #define VBEPTR_OFF(x) ((x) & 0xffff)
17 #define SAME_BPP(a, b) \
18 ((a) == (b) || (a) == 16 && (b) == 15 || (a) == 15 && (b) == 16 || (a) == 32 && (b) == 24 || (a) == 24 && (b) == 32)
20 static unsigned int make_mask(int sz, int pos);
22 static struct vbe_info *vbe_info;
23 static struct vbe_mode_info *mode_info;
24 static int pal_bits = 6;
26 void *set_video_mode(int xsz, int ysz, int bpp)
29 uint16_t *modes, best = 0;
32 /* check for VBE2 support and output some info */
34 if(!(vbe_info = vbe_get_info())) {
35 fprintf(stderr, "VESA BIOS Extensions not available\n");
39 printlog("VBE Version: %x.%x\n", vbe_info->version >> 8, vbe_info->version & 0xff);
40 if(vbe_info->version < 0x200) {
41 fprintf(stderr, "This program requires VBE 2.0 or greater. Try running UniVBE\n");
45 printlog("Graphics adapter: %s, %s (%s)\n", VBEPTR(vbe_info->oem_vendor_name_ptr),
46 VBEPTR(vbe_info->oem_product_name_ptr), VBEPTR(vbe_info->oem_product_rev_ptr));
47 printlog("Video memory: %dkb\n", vbe_info->total_mem << 6);
49 modes = VBEPTR(vbe_info->vid_mode_ptr);
52 for(i=0; i<1024; i++) { /* impose an upper limit to avoid inf-loops */
53 if(modes[i] == 0xffff) {
54 break; /* reached the end */
57 mode_info = vbe_get_mode_info(modes[i] | VBE_MODE_LFB);
58 if(!mode_info || mode_info->xres != xsz || mode_info->yres != ysz) {
61 if(SAME_BPP(mode_info->bpp, bpp)) {
67 mode_info = vbe_get_mode_info(best);
69 fprintf(stderr, "Requested video mode (%dx%d %dbpp) is unavailable\n", xsz, ysz, bpp);
73 if(vbe_set_mode(best | VBE_MODE_LFB) == -1) {
74 fprintf(stderr, "Failed to set video mode %dx%d %dbpp\n", mode_info->xres, mode_info->yres, mode_info->bpp);
78 /* attempt to set 8 bits of color per component in palettized modes */
80 pal_bits = vbe_set_palette_bits(8);
81 printlog("palette bits per color primary: %d\n", pal_bits);
85 fbsize = xsz * ysz * mode_info->num_img_pages * (bpp / CHAR_BIT);
86 return (void*)dpmi_mmap(mode_info->fb_addr, fbsize);
89 int set_text_mode(void)
95 int get_color_depth(void)
100 return mode_info->bpp;
103 int get_color_bits(int *rbits, int *gbits, int *bbits)
108 *rbits = mode_info->rmask_size;
109 *gbits = mode_info->gmask_size;
110 *bbits = mode_info->bmask_size;
114 int get_color_mask(unsigned int *rmask, unsigned int *gmask, unsigned int *bmask)
119 *rmask = make_mask(mode_info->rmask_size, mode_info->rpos);
120 *gmask = make_mask(mode_info->gmask_size, mode_info->gpos);
121 *bmask = make_mask(mode_info->bmask_size, mode_info->bpos);
125 int get_color_shift(int *rshift, int *gshift, int *bshift)
130 *rshift = mode_info->rpos;
131 *gshift = mode_info->gpos;
132 *bshift = mode_info->bpos;
136 void set_palette(int idx, int r, int g, int b)
142 vbe_set_palette(idx, col, 1, pal_bits);
145 void wait_vsync(void)
160 static unsigned int make_mask(int sz, int pos)
162 unsigned int i, mask = 0;
164 for(i=0; i<sz; i++) {