11 #define SAME_BPP(a, b) \
12 ((a) == (b) || ((a) == 16 && (b) == 15) || ((a) == 15 && (b) == 16) || \
13 ((a) == 32 && (b) == 24) || ((a) == 24 && (b) == 32))
15 void (*blit_frame)(void*, int);
17 static void blit_frame_lfb(void *pixels, int vsync);
18 static void blit_frame_banked(void *pixels, int vsync);
19 static uint32_t calc_mask(int sz, int pos);
21 static struct video_mode *vmodes;
22 static int num_vmodes;
24 static int vbe_init_ver;
25 static struct vbe_info vbe;
28 static struct video_mode *curmode;
29 static void *vpgaddr[2];
30 static int frontidx, backidx;
31 static int pgcount, pgsize, fbsize;
36 int i, num, max_modes;
37 struct video_mode *vmptr;
39 if(vbe_info(&vbe) == -1) {
40 fprintf(stderr, "failed to retrieve VBE information\n");
43 vbe_print_info(stdout, &vbe);
47 if(!(vmodes = malloc(max_modes * sizeof *vmodes))) {
48 fprintf(stderr, "failed to allocate video modes list\n");
52 num = vbe_num_modes(&vbe);
53 for(i=0; i<num; i++) {
54 struct vbe_mode_info minf;
56 if(vbe_mode_info(vbe.modes[i], &minf) == -1) {
60 if(num_vmodes >= max_modes) {
61 int newmax = max_modes ? (max_modes << 1) : 16;
62 if(!(vmptr = realloc(vmodes, newmax * sizeof *vmodes))) {
63 fprintf(stderr, "failed to grow video mode list (%d)\n", newmax);
71 vmptr = vmodes + num_vmodes++;
72 memset(vmptr, 0, sizeof *vmptr);
73 vmptr->mode = vbe.modes[i];
74 vmptr->xsz = minf.xres;
75 vmptr->ysz = minf.yres;
76 vmptr->bpp = minf.bpp;
77 vmptr->pitch = minf.scanline_bytes;
78 if(minf.mem_model == VBE_TYPE_DIRECT) {
79 vmptr->rbits = minf.rsize;
80 vmptr->gbits = minf.gsize;
81 vmptr->bbits = minf.bsize;
82 vmptr->rshift = minf.rpos;
83 vmptr->gshift = minf.gpos;
84 vmptr->bshift = minf.bpos;
85 vmptr->rmask = calc_mask(minf.rsize, minf.rpos);
86 vmptr->gmask = calc_mask(minf.gsize, minf.gpos);
87 vmptr->bmask = calc_mask(minf.bsize, minf.bpos);
88 vmptr->bpp = vmptr->rbits + vmptr->gbits + vmptr->bbits;
90 if(minf.attr & VBE_ATTR_LFB) {
91 vmptr->fb_addr = minf.fb_addr;
93 vmptr->bank_size = (uint32_t)minf.bank_size * 1024;
94 if(!vmptr->bank_size) {
95 vmptr->bank_size = 65536;
98 vmptr->max_pages = minf.num_img_pages;
100 printf("%04x: ", vbe.modes[i]);
101 vbe_print_mode_info(stdout, &minf);
105 vbe_init_ver = VBE_VER_MAJOR(vbe.ver);
109 void cleanup_video(void)
114 struct video_mode *video_modes(void)
119 int num_video_modes(void)
124 int match_video_mode(int xsz, int ysz, int bpp)
127 struct video_mode *vm;
129 for(i=0; i<num_vmodes; i++) {
131 if(vm->xsz != xsz || vm->ysz != ysz) continue;
132 if(SAME_BPP(vm->bpp, bpp)) {
135 if(vm->bpp == bpp) break;
139 fprintf(stderr, "failed to find video mode %dx%d %d bpp)\n", xsz, ysz, bpp);
145 int find_video_mode(int mode)
148 struct video_mode *vm;
151 for(i=0; i<num_vmodes; i++) {
152 if(vm->mode == mode) return i;
157 void *set_video_mode(int idx, int nbuf)
160 struct video_mode *vm = vmodes + idx;
162 printf("setting video mode %x (%dx%d %d bpp)\n", (unsigned int)vm->mode,
163 vm->xsz, vm->ysz, vm->bpp);
166 mode = vm->mode | VBE_MODE_LFB;
167 if(vbe_setmode(mode) == -1) {
169 if(vbe_setmode(mode) == -1) {
170 fprintf(stderr, "failed to set video mode %x\n", (unsigned int)vm->mode);
173 printf("Warning: failed to get a linear framebuffer. falling back to banked mode\n");
177 if(nbuf < 1) nbuf = 1;
178 if(nbuf > 2) nbuf = 2;
179 pgcount = nbuf > vm->max_pages ? vm->max_pages : nbuf;
180 pgsize = vm->ysz * vm->pitch;
181 fbsize = pgcount * pgsize;
183 printf("pgcount: %d, pgsize: %d, fbsize: %d\n", pgcount, pgsize, fbsize);
184 printf("phys addr: %p\n", (void*)vm->fb_addr);
188 vpgaddr[0] = (void*)dpmi_mmap(vm->fb_addr, fbsize);
190 fprintf(stderr, "failed to map framebuffer (phys: %lx, size: %d)\n",
191 (unsigned long)vm->fb_addr, fbsize);
195 memset(vpgaddr[0], 0xaa, pgsize);
198 vpgaddr[1] = (char*)vpgaddr[0] + pgsize;
200 page_flip(FLIP_NOW); /* start with the second page visible */
202 frontidx = backidx = 0;
206 blit_frame = blit_frame_lfb;
209 vpgaddr[0] = (void*)0xa0000;
212 blit_frame = blit_frame_banked;
217 int set_text_mode(void)
224 void *page_flip(int vsync)
227 /* page flipping not supported */
231 vbe_swap(backidx ? pgsize : 0, vsync ? VBE_SWAP_VBLANK : VBE_SWAP_NOW);
233 backidx = (backidx + 1) & 1;
235 return vpgaddr[backidx];
239 static void blit_frame_lfb(void *pixels, int vsync)
242 if(vsync) wait_vsync();
243 memcpy64(vpgaddr[frontidx], pixels, pgsize >> 3);
246 static void blit_frame_banked(void *pixels, int vsync)
249 unsigned int pending;
250 unsigned char *pptr = pixels;
254 if(vsync) wait_vsync();
256 /* assume initial window offset at 0 */
260 sz = pending > curmode->bank_size ? curmode->bank_size : pending;
261 memcpy64((void*)0xa0000, pptr, sz >> 3);
264 vbe_setwin(0, ++offs);
270 static uint32_t calc_mask(int sz, int pos)
275 mask = (mask << 1) | 1;