VBE fix: wrong bpp reported (24) with 32bpp modes
[retrobench] / src / dos / gfx.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include "cdpmi.h"
5 #include "gfx.h"
6 #include "vbe.h"
7 #include "vga.h"
8 #include "util.h"
9
10 #define SAME_BPP(a, b)  \
11         ((a) == (b) || ((a) == 16 && (b) == 15) || ((a) == 15 && (b) == 16) || \
12          ((a) == 32 && (b) == 24) || ((a) == 24 && (b) == 32))
13
14 void (*blit_frame)(void*, int);
15
16 int resizefb(int x, int y, int bpp, int pitch);
17
18 static void blit_frame_lfb(void *pixels, int vsync);
19 static void blit_frame_banked(void *pixels, int vsync);
20 static uint32_t calc_mask(int sz, int pos);
21
22 static struct video_mode *vmodes;
23 static int num_vmodes;
24
25 static int vbe_init_ver;
26 static struct vbe_info vbe;
27
28 /* current mode */
29 static struct video_mode *curmode;
30 static void *vpgaddr[2];
31 static int frontidx, backidx;
32 static int pgcount, pgsize, fbsize;
33
34
35 int init_video(void)
36 {
37         int i, num, max_modes;
38         struct video_mode *vmptr;
39
40         if(vbe_info(&vbe) == -1) {
41                 fprintf(stderr, "failed to retrieve VBE information\n");
42                 return -1;
43         }
44         vbe_print_info(stdout, &vbe);
45
46         num_vmodes = 0;
47         max_modes = 64;
48         if(!(vmodes = malloc(max_modes * sizeof *vmodes))) {
49                 fprintf(stderr, "failed to allocate video modes list\n");
50                 return -1;
51         }
52
53         num = vbe_num_modes(&vbe);
54         for(i=0; i<num; i++) {
55                 struct vbe_mode_info minf;
56
57                 if(vbe_mode_info(vbe.modes[i], &minf) == -1) {
58                         continue;
59                 }
60
61                 if(num_vmodes >= max_modes) {
62                         int newmax = max_modes ? (max_modes << 1) : 16;
63                         if(!(vmptr = realloc(vmodes, newmax * sizeof *vmodes))) {
64                                 fprintf(stderr, "failed to grow video mode list (%d)\n", newmax);
65                                 free(vmodes);
66                                 return -1;
67                         }
68                         vmodes = vmptr;
69                         max_modes = newmax;
70                 }
71
72                 vmptr = vmodes + num_vmodes++;
73                 memset(vmptr, 0, sizeof *vmptr);
74                 vmptr->mode = vbe.modes[i];
75                 vmptr->xsz = minf.xres;
76                 vmptr->ysz = minf.yres;
77                 vmptr->bpp = minf.bpp;
78                 vmptr->pitch = minf.scanline_bytes;
79                 if(minf.mem_model == VBE_TYPE_DIRECT) {
80                         vmptr->rbits = minf.rsize;
81                         vmptr->gbits = minf.gsize;
82                         vmptr->bbits = minf.bsize;
83                         vmptr->rshift = minf.rpos;
84                         vmptr->gshift = minf.gpos;
85                         vmptr->bshift = minf.bpos;
86                         vmptr->rmask = calc_mask(minf.rsize, minf.rpos);
87                         vmptr->gmask = calc_mask(minf.gsize, minf.gpos);
88                         vmptr->bmask = calc_mask(minf.bsize, minf.bpos);
89                         /*vmptr->bpp = vmptr->rbits + vmptr->gbits + vmptr->bbits;*/
90                 }
91                 if(minf.attr & VBE_ATTR_LFB) {
92                         vmptr->fb_addr = minf.fb_addr;
93                 } else {
94                         vmptr->bank_size = (uint32_t)minf.bank_size * 1024;
95                         if(!vmptr->bank_size) {
96                                 vmptr->bank_size = 65536;
97                         }
98                 }
99                 vmptr->max_pages = minf.num_img_pages;
100
101                 printf("%04x: ", vbe.modes[i]);
102                 vbe_print_mode_info(stdout, &minf);
103         }
104         fflush(stdout);
105
106         vbe_init_ver = VBE_VER_MAJOR(vbe.ver);
107         return 0;
108 }
109
110 void cleanup_video(void)
111 {
112         free(vmodes);
113 }
114
115 struct video_mode *video_modes(void)
116 {
117         return vmodes;
118 }
119
120 int num_video_modes(void)
121 {
122         return num_vmodes;
123 }
124
125 struct video_mode *get_video_mode(int idx)
126 {
127         if(idx == VMODE_CURRENT) {
128                 return curmode;
129         }
130         return vmodes + idx;
131 }
132
133 int match_video_mode(int xsz, int ysz, int bpp)
134 {
135         int i, best = -1;
136         struct video_mode *vm;
137
138         for(i=0; i<num_vmodes; i++) {
139                 vm = vmodes + i;
140                 if(vm->xsz != xsz || vm->ysz != ysz) continue;
141                 if(SAME_BPP(vm->bpp, bpp)) {
142                         best = i;
143                 }
144                 if(vm->bpp == bpp) break;
145         }
146
147         if(best == -1) {
148                 fprintf(stderr, "failed to find video mode %dx%d %d bpp)\n", xsz, ysz, bpp);
149                 return -1;
150         }
151         return best;
152 }
153
154 int find_video_mode(int mode)
155 {
156         int i;
157         struct video_mode *vm;
158
159         vm = vmodes;
160         for(i=0; i<num_vmodes; i++) {
161                 if(vm->mode == mode) return i;
162         }
163         return -1;
164 }
165
166 void *set_video_mode(int idx, int nbuf)
167 {
168         unsigned int mode;
169         struct video_mode *vm = vmodes + idx;
170
171         if(curmode == vm) return vpgaddr[0];
172
173         printf("setting video mode %x (%dx%d %d bpp)\n", (unsigned int)vm->mode,
174                         vm->xsz, vm->ysz, vm->bpp);
175         fflush(stdout);
176
177         mode = vm->mode | VBE_MODE_LFB;
178         if(vbe_setmode(mode) == -1) {
179                 mode = vm->mode;
180                 if(vbe_setmode(mode) == -1) {
181                         fprintf(stderr, "failed to set video mode %x\n", (unsigned int)vm->mode);
182                         return 0;
183                 }
184                 printf("Warning: failed to get a linear framebuffer. falling back to banked mode\n");
185         }
186
187         /* unmap previous video memory mapping, if there was one (switching modes) */
188         if(vpgaddr[0] && vpgaddr[0] != (void*)0xa0000) {
189                 dpmi_munmap(vpgaddr[0]);
190                 vpgaddr[0] = vpgaddr[1] = 0;
191         }
192
193         curmode = vm;
194         if(nbuf < 1) nbuf = 1;
195         if(nbuf > 2) nbuf = 2;
196         pgcount = nbuf > vm->max_pages ? vm->max_pages : nbuf;
197         pgsize = vm->ysz * vm->pitch;
198         fbsize = pgcount * pgsize;
199
200         printf("pgcount: %d, pgsize: %d, fbsize: %d\n", pgcount, pgsize, fbsize);
201         printf("phys addr: %p\n", (void*)vm->fb_addr);
202         fflush(stdout);
203
204         if(vm->fb_addr) {
205                 vpgaddr[0] = (void*)dpmi_mmap(vm->fb_addr, fbsize);
206                 if(!vpgaddr[0]) {
207                         fprintf(stderr, "failed to map framebuffer (phys: %lx, size: %d)\n",
208                                         (unsigned long)vm->fb_addr, fbsize);
209                         set_text_mode();
210                         return 0;
211                 }
212                 memset(vpgaddr[0], 0xaa, pgsize);
213
214                 if(pgcount > 1) {
215                         vpgaddr[1] = (char*)vpgaddr[0] + pgsize;
216                         backidx = 1;
217                         page_flip(FLIP_NOW);    /* start with the second page visible */
218                 } else {
219                         frontidx = backidx = 0;
220                         vpgaddr[1] = 0;
221                 }
222
223                 blit_frame = blit_frame_lfb;
224
225         } else {
226                 vpgaddr[0] = (void*)0xa0000;
227                 vpgaddr[1] = 0;
228
229                 blit_frame = blit_frame_banked;
230         }
231
232         /* allocate main memory framebuffer */
233         if(resizefb(vm->xsz, vm->ysz, vm->bpp, vm->pitch) == -1) {
234                 fprintf(stderr, "failed to allocate %dx%d (%d bpp) framebuffer\n", vm->xsz,
235                                 vm->ysz, vm->bpp);
236                 set_text_mode();
237                 return 0;
238         }
239
240         return vpgaddr[0];
241 }
242
243 int set_text_mode(void)
244 {
245         /* unmap previous video memory mapping, if there was one (switching modes) */
246         if(vpgaddr[0] && vpgaddr[0] != (void*)0xa0000) {
247                 dpmi_munmap(vpgaddr[0]);
248                 vpgaddr[0] = vpgaddr[1] = 0;
249         }
250
251         vga_setmode(3);
252         curmode = 0;
253         return 0;
254 }
255
256 void *page_flip(int vsync)
257 {
258         if(!vpgaddr[1]) {
259                 /* page flipping not supported */
260                 return vpgaddr[0];
261         }
262
263         vbe_swap(backidx ? pgsize : 0, vsync ? VBE_SWAP_VBLANK : VBE_SWAP_NOW);
264         frontidx = backidx;
265         backidx = (backidx + 1) & 1;
266
267         return vpgaddr[backidx];
268 }
269
270
271 static void blit_frame_lfb(void *pixels, int vsync)
272 {
273         if(vsync) wait_vsync();
274         memcpy64(vpgaddr[frontidx], pixels, pgsize >> 3);
275 }
276
277 static void blit_frame_banked(void *pixels, int vsync)
278 {
279         int sz, offs;
280         unsigned int pending;
281         unsigned char *pptr = pixels;
282
283         if(vsync) wait_vsync();
284
285         /* assume initial window offset at 0 */
286         offs = 0;
287         pending = pgsize;
288         while(pending > 0) {
289                 sz = pending > curmode->bank_size ? curmode->bank_size : pending;
290                 //memcpy64((void*)0xa0000, pptr, sz >> 3);
291                 memcpy((void*)0xa0000, pptr, sz);
292                 pptr += sz;
293                 pending -= sz;
294                 vbe_setwin(0, ++offs);
295         }
296
297         vbe_setwin(0, 0);
298 }
299
300 static uint32_t calc_mask(int sz, int pos)
301 {
302         uint32_t mask = 0;
303         while(sz-- > 0) {
304                 mask = (mask << 1) | 1;
305         }
306         return mask << pos;
307 }