11 #define farptr_to_linear(rmaddr) \
12 ((((intptr_t)(rmaddr) >> 12) & 0xffff0) + ((intptr_t)(rmaddr) & 0xffff))
14 static int init(void);
15 static void cleanup(void);
16 static struct vid_modeinfo *find_mode(int mode);
17 static int setmode(int mode);
18 static int getmode(void);
19 static const char *memsize_str(long sz);
20 static int get_mode_info(int mode, struct vbe_mode_info *mi);
21 static int conv_vbeinfo(int mode, struct vid_modeinfo *mi, struct vbe_mode_info *vbemi);
22 static unsigned int calc_mask(int nbits, int pos);
23 static void print_mode_info(int mode, struct vid_modeinfo *mi);
25 static void pack(uint32_t *pix, int r, int g, int b);
26 static void unpack(uint32_t pix, int *r, int *g, int *b);
27 static void clear(uint32_t color);
28 static void blitfb_lfb(void *fb, int pitch);
29 static void blitfb_banked(void *fb, int pitch);
30 static void flip(int vsync);
32 static struct vid_driver drv;
33 static struct vid_drvops drvops = {init, cleanup, setmode, getmode};
34 static unsigned int vbe_ver;
37 static struct vid_modeinfo *cur_mi;
38 static int cur_pgsize;
41 void vid_register_vbe(void)
47 vid_drvlist[vid_numdrv++] = &drv;
53 struct dpmi_regs regs = {0};
55 struct vbe_mode_info vbemi;
56 unsigned short bufseg;
57 uint16_t *vbe_modelist, *modelist;
59 struct vid_modeinfo modeinf;
65 bufseg = (intptr_t)vbe >> 4;
67 /* call VBE function 00 (get controller information) */
68 memcpy(vbe->sig, "VBE2", 4); /* denote we want VBE 2.0 info */
71 dpmi_rmint(0x10, ®s);
72 if((regs.eax & 0xffff) != 0x4f || memcmp(vbe->sig, "VESA", 4) != 0) {
73 errormsg("failed to get VBE controller information\n");
79 infomsg("Found VBE %d.%d\n", VBE_VER_MAJOR(vbe_ver), VBE_VER_MINOR(vbe_ver));
80 infomsg("OEM: %s\n", (char*)farptr_to_linear(vbe->oem_name));
81 if(vbe_ver >= 0x0200) {
82 infomsg("%s - %s (%s)\n", (char*)farptr_to_linear(vbe->vendor),
83 (char*)farptr_to_linear(vbe->product),
84 (char*)farptr_to_linear(vbe->revstr));
86 infomsg("Video RAM: %s\n", memsize_str((long)vbe->vmem_blk * 65536));
88 vbe_modelist = (uint16_t*)farptr_to_linear(vbe->modelist_addr);
90 for(i=0; i<1024; i++) {
91 if(vbe_modelist[i] == 0xffff) break;
95 if(!(modelist = malloc(count * sizeof *modelist))) {
96 errormsg("failed to allocate mode list\n");
99 for(i=0; i<count; i++) {
100 modelist[i] = vbe_modelist[i];
103 if(!(drv.modes = malloc(count * sizeof *drv.modes))) {
104 errormsg("failed to allocate mode list\n");
110 for(i=0; i<count; i++) {
111 if(get_mode_info(modelist[i], &vbemi) == -1) {
114 if(conv_vbeinfo(modelist[i], drv.modes + drv.num_modes, &vbemi) == -1) {
124 static void cleanup(void)
131 static struct vid_modeinfo *find_mode(int mode)
134 for(i=0; i<drv.num_modes; i++) {
135 if(drv.modes[i].modeno == mode) {
136 return drv.modes + i;
142 static int setmode(int mode)
144 struct vid_modeinfo *minf;
145 struct dpmi_regs regs = {0};
147 if((minf = find_mode(mode)) && minf->lfb) {
148 mode |= VBE_MODE_LFB;
154 dpmi_rmint(0x10, ®s);
156 if((regs.eax & 0xffff) != 0x4f) {
157 if(mode & VBE_MODE_LFB) {
158 mode &= ~VBE_MODE_LFB;
165 if(!(cur_mi = minf)) return 0;
167 cur_pgsize = minf->height * minf->pitch;
169 if(mode & VBE_MODE_LFB) {
170 minf->ops.blitfb = blitfb_lfb;
172 minf->ops.blitfb = blitfb_banked;
175 print_mode_info(mode, minf);
179 static int getmode(void)
184 static const char *memsize_str(long sz)
186 static const char *suffix[] = {"bytes", "kb", "mb", "gb", 0};
190 while(sz > 1024 && suffix[cnt + 1]) {
195 sprintf(buf, "%ld %s", sz, suffix[cnt]);
199 static int get_mode_info(int mode, struct vbe_mode_info *mi)
201 struct dpmi_regs regs = {0};
202 struct vbe_mode_info *miptr;
205 miptr = dpmi_lowbuf();
206 bufseg = (intptr_t)miptr >> 4;
211 dpmi_rmint(0x10, ®s);
212 if((regs.eax & 0xffff) != 0x4f) {
220 int vid_setwin(int wid, int pos)
222 struct dpmi_regs regs = {0};
227 dpmi_rmint(0x10, ®s);
228 if((regs.eax & 0xffff) != 0x4f) {
234 static int conv_vbeinfo(int mode, struct vid_modeinfo *mi, struct vbe_mode_info *vbemi)
236 static int gran_shift;
237 static const struct { int width, height, bpp; } stdmode[] = {
238 {640, 400, 8}, /* 100h */
239 {640, 480, 8}, /* 101h */
240 {800, 600, 4}, {800, 600, 8}, /* 102h - 103h */
241 {1024, 768, 4}, {1024, 768, 8}, /* 104h - 105h */
242 {1280, 1024, 4}, {1280, 1024, 8}, /* 106h - 107h */
243 {80, 60, 4}, {132, 25, 4}, {132, 43, 4}, {132, 50, 4}, {132, 60, 4},
244 {320, 200, 15}, {320, 200, 16}, {320, 200, 24}, /* 10dh - 10fh */
245 {640, 480, 15}, {640, 480, 16}, {640, 480, 24}, /* 110h - 112h */
246 {800, 600, 15}, {800, 600, 16}, {800, 600, 24}, /* 113h - 115h */
247 {1024, 768, 15}, {1024, 768, 16}, {1024, 768, 24}, /* 116h - 118h */
248 {1280, 1024, 15}, {1280, 1024, 16}, {1280, 1024, 24} /* 119h - 11bh */
251 if(!(vbemi->attr & VBE_ATTR_AVAIL)) {
252 return -1; /* ignore unsupported modes */
254 if(!(vbemi->attr & VBE_ATTR_GFX)) {
255 return -1; /* ignore text modes */
257 if(vbemi->attr & VBE_ATTR_LFB) {
263 mi->vmem_addr = 0xa0000;
265 if(vbe_ver >= 0x0102) {
266 mi->width = vbemi->xres;
267 mi->height = vbemi->yres;
268 mi->bpp = vbemi->bpp;
269 mi->rshift = vbemi->rpos;
270 mi->gshift = vbemi->gpos;
271 mi->bshift = vbemi->bpos;
272 mi->rmask = calc_mask(vbemi->rsize, vbemi->rpos);
273 mi->gmask = calc_mask(vbemi->gsize, vbemi->gpos);
274 mi->bmask = calc_mask(vbemi->bsize, vbemi->bpos);
275 mi->pages = vbemi->num_img_pages + 1;
277 if(vbe_ver >= 0x0200) {
278 mi->vmem_addr = vbemi->fb_addr;
279 mi->vmem_size = vbemi->scanline_bytes * mi->height * mi->pages;
282 if((mode & 0xff) > 7) {
285 mi->width = stdmode[mode & 0xff].width;
286 mi->height = stdmode[mode & 0xff].height;
287 mi->bpp = stdmode[mode & 0xff].bpp;
289 mi->ncolors = 1 << mi->bpp;
290 mi->pitch = vbemi->scanline_bytes;
291 mi->win_size = vbemi->win_size;
292 mi->win_gran = vbemi->win_gran;
296 if(mi->win_gran > 0 && mi->win_gran < 64) {
297 int gran = mi->win_gran;
302 mi->win_step = 1 << gran_shift;
306 mi->ops.unpack = unpack;
307 mi->ops.setpal = vga_setpal;
308 mi->ops.getpal = vga_getpal;
309 mi->ops.vsync = vid_vsync;
310 mi->ops.clear = clear;
316 static unsigned int calc_mask(int nbits, int pos)
319 unsigned int mask = 0;
321 for(i=0; i<nbits; i++) {
322 mask = (mask << 1) | 1;
327 static void print_mode_info(int mode, struct vid_modeinfo *mi)
329 infomsg("VBE mode %04x\n", mode);
330 infomsg(" %dx%d %d bpp (%d colors)\n", mi->width, mi->height,
331 mi->bpp, mi->ncolors);
332 infomsg(" pitch: %d bytes, %d vmem pages\n", mi->pitch, mi->pages);
335 infomsg(" RGB mask %06x %06x %06x (pos: %d %d %d)\n", (unsigned int)mi->rmask,
336 (unsigned int)mi->gmask, (unsigned int)mi->bmask, mi->rshift,
337 mi->gshift, mi->bshift);
340 if(mode & VBE_MODE_LFB) {
341 infomsg(" LFB address %xh, size: %d\n", (unsigned int)mi->vmem_addr,
344 infomsg(" banked window %d kb, granularity: %d kb, step: %d\n", mi->win_size,
345 mi->win_gran, mi->win_step);
350 static void pack(uint32_t *pix, int r, int g, int b)
352 *pix = (((uint32_t)r << cur_mi->rshift) & cur_mi->rmask) |
353 (((uint32_t)g << cur_mi->gshift) & cur_mi->gmask) |
354 (((uint32_t)b << cur_mi->bshift) & cur_mi->bmask);
357 static void unpack(uint32_t pix, int *r, int *g, int *b)
359 *r = (pix & cur_mi->rmask) >> cur_mi->rshift;
360 *g = (pix & cur_mi->gmask) >> cur_mi->gshift;
361 *b = (pix & cur_mi->bmask) >> cur_mi->bshift;
364 static void clear(uint32_t color)
368 static void blitfb_lfb(void *fb, int pitch)
371 unsigned char *dest, *src;
376 for(i=0; i<cur_mi->height; i++) {
377 memcpy(dest, src, cur_mi->pitch);
378 dest += cur_mi->pitch;
383 static void blitfb_banked(void *fb, int pitch)
385 int sz, offs, pending, winsz;
386 unsigned char *pptr = fb;
388 winsz = cur_mi->win_size << 10;
390 /* assume initial window offset at 0 */
392 pending = cur_pgsize;
394 sz = pending > winsz ? winsz : pending;
395 memcpy((void*)0xa0000, pptr, sz);
398 offs += cur_mi->win_step;
404 static void flip(int vsync)