menu
[eradicate] / 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
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))
12
13 void (*blit_frame)(void*, int);
14
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);
18
19 static struct video_mode *vmodes;
20 static int num_vmodes;
21
22 static int vbe_init_ver;
23 static struct vbe_info vbe;
24
25 /* current mode */
26 static struct video_mode *curmode;
27 static void *vpgaddr[2];
28 static int frontidx, backidx;
29 static int pgcount, pgsize, fbsize;
30
31
32 int init_video(void)
33 {
34         int i, num, max_modes;
35         struct video_mode *vmptr;
36
37         if(vbe_info(&vbe) == -1) {
38                 fprintf(stderr, "failed to retrieve VBE information\n");
39                 return -1;
40         }
41         vbe_print_info(stdout, &vbe);
42
43         num_vmodes = 0;
44         max_modes = 64;
45         if(!(vmodes = malloc(max_modes * sizeof *vmodes))) {
46                 fprintf(stderr, "failed to allocate video modes list\n");
47                 return -1;
48         }
49
50         num = vbe_num_modes(&vbe);
51         for(i=0; i<num; i++) {
52                 struct vbe_mode_info minf;
53
54                 if(vbe_mode_info(vbe.modes[i], &minf) == -1) {
55                         continue;
56                 }
57
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);
62                                 free(vmodes);
63                                 return -1;
64                         }
65                         vmodes = vmptr;
66                         max_modes = newmax;
67                 }
68
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;
87                 }
88                 if(minf.attr & VBE_ATTR_LFB) {
89                         vmptr->fb_addr = minf.fb_addr;
90                 } else {
91                         vmptr->bank_size = (uint32_t)minf.bank_size * 1024;
92                         if(!vmptr->bank_size) {
93                                 vmptr->bank_size = 65536;
94                         }
95                 }
96                 vmptr->max_pages = minf.num_img_pages;
97
98                 printf("%04x: ", vbe.modes[i]);
99                 vbe_print_mode_info(stdout, &minf);
100         }
101         fflush(stdout);
102
103         vbe_init_ver = VBE_VER_MAJOR(vbe.ver);
104         return 0;
105 }
106
107 void cleanup_video(void)
108 {
109         free(vmodes);
110 }
111
112 struct video_mode *video_modes(void)
113 {
114         return vmodes;
115 }
116
117 int num_video_modes(void)
118 {
119         return num_vmodes;
120 }
121
122 int match_video_mode(int xsz, int ysz, int bpp)
123 {
124         int i, best = -1;
125         struct video_mode *vm;
126
127         for(i=0; i<num_vmodes; i++) {
128                 vm = vmodes + i;
129                 if(vm->xsz != xsz || vm->ysz != ysz) continue;
130                 if(SAME_BPP(vm->bpp, bpp)) {
131                         best = i;
132                 }
133                 if(vm->bpp == bpp) break;
134         }
135
136         if(best == -1) {
137                 fprintf(stderr, "failed to find video mode %dx%d %d bpp)\n", xsz, ysz, bpp);
138                 return -1;
139         }
140         return best;
141 }
142
143 int find_video_mode(int mode)
144 {
145         int i;
146         struct video_mode *vm;
147
148         vm = vmodes;
149         for(i=0; i<num_vmodes; i++) {
150                 if(vm->mode == mode) return i;
151         }
152         return -1;
153 }
154
155 void *set_video_mode(int idx, int nbuf)
156 {
157         unsigned int mode;
158         struct video_mode *vm = vmodes + idx;
159
160         printf("setting video mode %x (%dx%d %d bpp)\n", (unsigned int)vm->mode,
161                         vm->xsz, vm->ysz, vm->bpp);
162         fflush(stdout);
163
164         mode = vm->mode | VBE_MODE_LFB;
165         if(vbe_setmode(mode) == -1) {
166                 mode = vm->mode;
167                 if(vbe_setmode(mode) == -1) {
168                         fprintf(stderr, "failed to set video mode %x\n", (unsigned int)vm->mode);
169                         return 0;
170                 }
171                 printf("Warning: failed to get a linear framebuffer. falling back to banked mode\n");
172         }
173
174         curmode = vm;
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;
180
181         printf("pgcount: %d, pgsize: %d, fbsize: %d\n", pgcount, pgsize, fbsize);
182         printf("phys addr: %p\n", (void*)vm->fb_addr);
183         fflush(stdout);
184
185         if(vm->fb_addr) {
186                 vpgaddr[0] = (void*)dpmi_mmap(vm->fb_addr, fbsize);
187                 if(!vpgaddr[0]) {
188                         fprintf(stderr, "failed to map framebuffer (phys: %lx, size: %d)\n",
189                                         (unsigned long)vm->fb_addr, fbsize);
190                         set_text_mode();
191                         return 0;
192                 }
193                 memset(vpgaddr[0], 0xaa, pgsize);
194
195                 if(pgcount > 1) {
196                         vpgaddr[1] = (char*)vpgaddr[0] + pgsize;
197                         backidx = 1;
198                         page_flip(FLIP_NOW);    /* start with the second page visible */
199                 } else {
200                         frontidx = backidx = 0;
201                         vpgaddr[1] = 0;
202                 }
203
204                 blit_frame = blit_frame_lfb;
205
206         } else {
207                 vpgaddr[0] = (void*)0xa0000;
208                 vpgaddr[1] = 0;
209
210                 blit_frame = blit_frame_banked;
211         }
212         return vpgaddr[0];
213 }
214
215 int set_text_mode(void)
216 {
217         vga_setmode(3);
218         curmode = 0;
219         return 0;
220 }
221
222 void *page_flip(int vsync)
223 {
224         if(!vpgaddr[1]) {
225                 /* page flipping not supported */
226                 return vpgaddr[0];
227         }
228
229         vbe_swap(backidx ? pgsize : 0, vsync ? VBE_SWAP_VBLANK : VBE_SWAP_NOW);
230         frontidx = backidx;
231         backidx = (backidx + 1) & 1;
232
233         return vpgaddr[backidx];
234 }
235
236
237 static void blit_frame_lfb(void *pixels, int vsync)
238 {
239         if(vsync) wait_vsync();
240         memcpy(vpgaddr[frontidx], pixels, pgsize);
241 }
242
243 static void blit_frame_banked(void *pixels, int vsync)
244 {
245         int i, sz, offs;
246         unsigned int pending;
247         unsigned char *pptr = pixels;
248
249         if(vsync) wait_vsync();
250
251         /* assume initial window offset at 0 */
252         offs = 0;
253         pending = pgsize;
254         while(pending > 0) {
255                 sz = pending > curmode->bank_size ? curmode->bank_size : pending;
256                 memcpy((void*)0xa0000, pptr, sz);
257                 pptr += sz;
258                 pending -= sz;
259                 vbe_setwin(0, ++offs);
260         }
261
262         vbe_setwin(0, 0);
263 }
264
265 static uint32_t calc_mask(int sz, int pos)
266 {
267         int i;
268         uint32_t mask = 0;
269         while(sz-- > 0) {
270                 mask = (mask << 1) | 1;
271         }
272         return mask << pos;
273 }