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);
86 vmptr->bpp = vmptr->rbits + vmptr->gbits + vmptr->bbits;
88 if(minf.attr & VBE_ATTR_LFB) {
89 vmptr->fb_addr = minf.fb_addr;
91 vmptr->bank_size = (uint32_t)minf.bank_size * 1024;
92 if(!vmptr->bank_size) {
93 vmptr->bank_size = 65536;
96 vmptr->max_pages = minf.num_img_pages;
98 printf("%04x: ", vbe.modes[i]);
99 vbe_print_mode_info(stdout, &minf);
103 vbe_init_ver = VBE_VER_MAJOR(vbe.ver);
107 void cleanup_video(void)
112 struct video_mode *video_modes(void)
117 int num_video_modes(void)
122 int match_video_mode(int xsz, int ysz, int bpp)
125 struct video_mode *vm;
127 for(i=0; i<num_vmodes; i++) {
129 if(vm->xsz != xsz || vm->ysz != ysz) continue;
130 if(SAME_BPP(vm->bpp, bpp)) {
133 if(vm->bpp == bpp) break;
137 fprintf(stderr, "failed to find video mode %dx%d %d bpp)\n", xsz, ysz, bpp);
143 int find_video_mode(int mode)
146 struct video_mode *vm;
149 for(i=0; i<num_vmodes; i++) {
150 if(vm->mode == mode) return i;
155 void *set_video_mode(int idx, int nbuf)
158 struct video_mode *vm = vmodes + idx;
160 printf("setting video mode %x (%dx%d %d bpp)\n", (unsigned int)vm->mode,
161 vm->xsz, vm->ysz, vm->bpp);
164 mode = vm->mode | VBE_MODE_LFB;
165 if(vbe_setmode(mode) == -1) {
167 if(vbe_setmode(mode) == -1) {
168 fprintf(stderr, "failed to set video mode %x\n", (unsigned int)vm->mode);
171 printf("Warning: failed to get a linear framebuffer. falling back to banked mode\n");
175 if(nbuf < 1) nbuf = 1;
176 if(nbuf > 2) nbuf = 2;
177 pgcount = nbuf > vm->max_pages ? vm->max_pages : nbuf;
178 pgsize = vm->ysz * vm->pitch;
179 fbsize = pgcount * pgsize;
181 printf("pgcount: %d, pgsize: %d, fbsize: %d\n", pgcount, pgsize, fbsize);
182 printf("phys addr: %p\n", (void*)vm->fb_addr);
186 vpgaddr[0] = (void*)dpmi_mmap(vm->fb_addr, fbsize);
188 fprintf(stderr, "failed to map framebuffer (phys: %lx, size: %d)\n",
189 (unsigned long)vm->fb_addr, fbsize);
193 memset(vpgaddr[0], 0xaa, pgsize);
196 vpgaddr[1] = (char*)vpgaddr[0] + pgsize;
198 page_flip(FLIP_NOW); /* start with the second page visible */
200 frontidx = backidx = 0;
204 blit_frame = blit_frame_lfb;
207 vpgaddr[0] = (void*)0xa0000;
210 blit_frame = blit_frame_banked;
215 int set_text_mode(void)
222 void *page_flip(int vsync)
225 /* page flipping not supported */
229 vbe_swap(backidx ? pgsize : 0, vsync ? VBE_SWAP_VBLANK : VBE_SWAP_NOW);
231 backidx = (backidx + 1) & 1;
233 return vpgaddr[backidx];
237 static void blit_frame_lfb(void *pixels, int vsync)
239 if(vsync) wait_vsync();
240 memcpy(vpgaddr[frontidx], pixels, pgsize);
243 static void blit_frame_banked(void *pixels, int vsync)
246 unsigned int pending;
247 unsigned char *pptr = pixels;
249 if(vsync) wait_vsync();
251 /* assume initial window offset at 0 */
255 sz = pending > curmode->bank_size ? curmode->bank_size : pending;
256 memcpy((void*)0xa0000, pptr, sz);
259 vbe_setwin(0, ++offs);
265 static uint32_t calc_mask(int sz, int pos)
270 mask = (mask << 1) | 1;