9 #define SAME_BPP(a, b) \
10 ((a) == (b) || ((a) == 16 && (b) == 15) || ((a) == 15 && (b) == 16) || \
11 ((a) == 32 && (b) == 24) || ((a) == 24 && (b) == 32))
13 void (*blit_frame)(void*, int);
15 static void blit_frame_lfb(void *pixels, int vsync);
16 static void blit_frame_banked(void *pixels, int vsync);
17 static uint32_t calc_mask(int sz, int pos);
19 static struct video_mode *vmodes;
20 static int num_vmodes;
22 static int vbe_init_ver;
23 static struct vbe_info vbe;
26 static struct video_mode *curmode;
27 static void *vpgaddr[2];
28 static int frontidx, backidx;
29 static int pgcount, pgsize, fbsize;
34 int i, num, max_modes;
35 struct video_mode *vmptr;
37 if(vbe_info(&vbe) == -1) {
38 fprintf(stderr, "failed to retrieve VBE information\n");
41 vbe_print_info(stdout, &vbe);
45 if(!(vmodes = malloc(max_modes * sizeof *vmodes))) {
46 fprintf(stderr, "failed to allocate video modes list\n");
50 num = vbe_num_modes(&vbe);
51 for(i=0; i<num; i++) {
52 struct vbe_mode_info minf;
54 if(vbe_mode_info(vbe.modes[i], &minf) == -1) {
58 if(num_vmodes >= max_modes) {
59 int newmax = max_modes ? (max_modes << 1) : 16;
60 if(!(vmptr = realloc(vmodes, newmax * sizeof *vmodes))) {
61 fprintf(stderr, "failed to grow video mode list (%d)\n", newmax);
69 vmptr = vmodes + num_vmodes++;
70 memset(vmptr, 0, sizeof *vmptr);
71 vmptr->mode = vbe.modes[i];
72 vmptr->xsz = minf.xres;
73 vmptr->ysz = minf.yres;
74 vmptr->bpp = minf.bpp;
75 vmptr->pitch = minf.scanline_bytes;
76 if(minf.mem_model == VBE_TYPE_DIRECT) {
77 vmptr->rbits = minf.rsize;
78 vmptr->gbits = minf.gsize;
79 vmptr->bbits = minf.bsize;
80 vmptr->rshift = minf.rpos;
81 vmptr->gshift = minf.gpos;
82 vmptr->bshift = minf.bpos;
83 vmptr->rmask = calc_mask(minf.rsize, minf.rpos);
84 vmptr->gmask = calc_mask(minf.gsize, minf.gpos);
85 vmptr->bmask = calc_mask(minf.bsize, minf.bpos);
87 if(minf.attr & VBE_ATTR_LFB) {
88 vmptr->fb_addr = minf.fb_addr;
90 vmptr->bank_size = (uint32_t)minf.bank_size * 1024;
91 if(!vmptr->bank_size) {
92 vmptr->bank_size = 65536;
95 vmptr->max_pages = minf.num_img_pages;
97 printf("%04x: ", vbe.modes[i]);
98 vbe_print_mode_info(stdout, &minf);
102 vbe_init_ver = VBE_VER_MAJOR(vbe.ver);
106 void cleanup_video(void)
111 struct video_mode *video_modes(void)
116 int num_video_modes(void)
121 int match_video_mode(int xsz, int ysz, int bpp)
124 struct video_mode *vm;
126 for(i=0; i<num_vmodes; i++) {
128 if(vm->xsz != xsz || vm->ysz != ysz) continue;
129 if(SAME_BPP(vm->bpp, bpp)) {
132 if(vm->bpp == bpp) break;
136 fprintf(stderr, "failed to find video mode %dx%d %d bpp)\n", xsz, ysz, bpp);
142 int find_video_mode(int mode)
145 struct video_mode *vm;
148 for(i=0; i<num_vmodes; i++) {
149 if(vm->mode == mode) return i;
154 void *set_video_mode(int idx, int nbuf)
157 struct video_mode *vm = vmodes + idx;
159 printf("setting video mode %x (%dx%d %d bpp)\n", (unsigned int)vm->mode,
160 vm->xsz, vm->ysz, vm->bpp);
163 mode = vm->mode | VBE_MODE_LFB;
164 if(vbe_setmode(mode) == -1) {
166 if(vbe_setmode(mode) == -1) {
167 fprintf(stderr, "failed to set video mode %x\n", (unsigned int)vm->mode);
170 printf("Warning: failed to get a linear framebuffer. falling back to banked mode\n");
174 if(nbuf < 1) nbuf = 1;
175 if(nbuf > 2) nbuf = 2;
176 pgcount = nbuf > vm->max_pages ? vm->max_pages : nbuf;
177 pgsize = vm->ysz * vm->pitch;
178 fbsize = pgcount * pgsize;
180 printf("pgcount: %d, pgsize: %d, fbsize: %d\n", pgcount, pgsize, fbsize);
181 printf("phys addr: %p\n", (void*)vm->fb_addr);
185 vpgaddr[0] = (void*)dpmi_mmap(vm->fb_addr, fbsize);
187 fprintf(stderr, "failed to map framebuffer (phys: %lx, size: %d)\n",
188 (unsigned long)vm->fb_addr, fbsize);
192 memset(vpgaddr[0], 0xaa, pgsize);
195 vpgaddr[1] = (char*)vpgaddr[0] + pgsize;
197 page_flip(FLIP_NOW); /* start with the second page visible */
199 frontidx = backidx = 0;
203 blit_frame = blit_frame_lfb;
206 vpgaddr[0] = (void*)0xa0000;
209 blit_frame = blit_frame_banked;
214 int set_text_mode(void)
221 void *page_flip(int vsync)
224 /* page flipping not supported */
228 vbe_swap(backidx ? pgsize : 0, vsync ? VBE_SWAP_VBLANK : VBE_SWAP_NOW);
230 backidx = (backidx + 1) & 1;
232 return vpgaddr[backidx];
236 static void blit_frame_lfb(void *pixels, int vsync)
238 if(vsync) wait_vsync();
239 memcpy(vpgaddr[frontidx], pixels, pgsize);
242 static void blit_frame_banked(void *pixels, int vsync)
245 unsigned int pending;
246 unsigned char *pptr = pixels;
248 if(vsync) wait_vsync();
250 /* assume initial window offset at 0 */
254 sz = pending > curmode->bank_size ? curmode->bank_size : pending;
255 memcpy((void*)0xa0000, pptr, sz);
258 vbe_setwin(0, ++offs);
264 static uint32_t calc_mask(int sz, int pos)
269 mask = (mask << 1) | 1;