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