eobish

annotate src/dos/vbegfx.c @ 7:6a350c554e46

started DOS port
author John Tsiombikas <nuclear@member.fsf.org>
date Mon, 19 Jan 2015 15:49:14 +0200
parents
children
rev   line source
nuclear@7 1 #ifndef GFX_H_
nuclear@7 2 #define GFX_H_
nuclear@7 3
nuclear@7 4 #include <stdio.h>
nuclear@7 5 #include <stdlib.h>
nuclear@7 6 #include <string.h>
nuclear@7 7 #include "vbe.h"
nuclear@7 8 #include "logger.h"
nuclear@7 9
nuclear@7 10 #define REALPTR(s, o) (void*)(((uint32_t)(s) << 4) + (uint32_t)(o))
nuclear@7 11 #define VBEPTR(x) REALPTR(((x) & 0xffff0000) >> 16, (x) & 0xffff)
nuclear@7 12 #define VBEPTR_SEG(x) (((x) & 0xffff0000) >> 16)
nuclear@7 13 #define VBEPTR_OFF(x) ((x) & 0xffff)
nuclear@7 14
nuclear@7 15 #define SAME_BPP(a, b) \
nuclear@7 16 ((a) == (b) || (a) == 16 && (b) == 15 || (a) == 15 && (b) == 16 || (a) == 32 && (b) == 24 || (a) == 24 && (b) == 32)
nuclear@7 17
nuclear@7 18 static unsigned int make_mask(int sz, int pos);
nuclear@7 19
nuclear@7 20 static struct vbe_info *vbe_info;
nuclear@7 21 static struct vbe_mode_info *mode_info;
nuclear@7 22 static int pal_bits = 6;
nuclear@7 23
nuclear@7 24 void *set_video_mode(int xsz, int ysz, int bpp)
nuclear@7 25 {
nuclear@7 26 int i;
nuclear@7 27 uint16_t *modes, best = 0;
nuclear@7 28 unsigned int fbsize;
nuclear@7 29
nuclear@7 30 /* check for VBE2 support and output some info */
nuclear@7 31 if(!vbe_info) {
nuclear@7 32 if(!(vbe_info = vbe_get_info())) {
nuclear@7 33 fprintf(stderr, "VESA BIOS Extensions not available\n");
nuclear@7 34 return 0;
nuclear@7 35 }
nuclear@7 36
nuclear@7 37 printlog("VBE Version: %x.%x\n", vbe_info->version >> 8, vbe_info->version & 0xff);
nuclear@7 38 if(vbe_info->version < 0x200) {
nuclear@7 39 fprintf(stderr, "This program requires VBE 2.0 or greater. Try running UniVBE\n");
nuclear@7 40 return 0;
nuclear@7 41 }
nuclear@7 42
nuclear@7 43 printlog("Graphics adapter: %s, %s (%s)\n", VBEPTR(vbe_info->oem_vendor_name_ptr),
nuclear@7 44 VBEPTR(vbe_info->oem_product_name_ptr), VBEPTR(vbe_info->oem_product_rev_ptr));
nuclear@7 45 printlog("Video memory: %dmb\n", vbe_info->total_mem << 6);
nuclear@7 46
nuclear@7 47 modes = VBEPTR(vbe_info->vid_mode_ptr);
nuclear@7 48 }
nuclear@7 49
nuclear@7 50 for(i=0; i<1024; i++) { /* impose an upper limit to avoid inf-loops */
nuclear@7 51 if(modes[i] == 0xffff) {
nuclear@7 52 break; /* reached the end */
nuclear@7 53 }
nuclear@7 54
nuclear@7 55 mode_info = vbe_get_mode_info(modes[i] | VBE_MODE_LFB);
nuclear@7 56 if(!mode_info || mode_info->xres != xsz || mode_info->yres != ysz) {
nuclear@7 57 continue;
nuclear@7 58 }
nuclear@7 59 if(SAME_BPP(mode_info->bpp, bpp)) {
nuclear@7 60 best = modes[i];
nuclear@7 61 }
nuclear@7 62 }
nuclear@7 63
nuclear@7 64 if(best) {
nuclear@7 65 mode_info = vbe_get_mode_info(best);
nuclear@7 66 } else {
nuclear@7 67 fprintf(stderr, "Requested video mode (%dx%d %dbpp) is unavailable\n", xsz, ysz, bpp);
nuclear@7 68 return 0;
nuclear@7 69 }
nuclear@7 70
nuclear@7 71 if(vbe_set_mode(best | VBE_MODE_LFB) == -1) {
nuclear@7 72 fprintf(stderr, "Failed to set video mode %dx%d %dbpp\n", mode_info->xres, mode_info->yres, mode_info->bpp);
nuclear@7 73 return 0;
nuclear@7 74 }
nuclear@7 75
nuclear@7 76 /* attempt to set 8 bits of color per component in palettized modes */
nuclear@7 77 if(bpp <= 8) {
nuclear@7 78 pal_bits = vbe_set_palette_bits(8);
nuclear@7 79 printlog("palette bits per color primary: %d\n", pal_bits);
nuclear@7 80 }
nuclear@7 81
nuclear@7 82 fbsize = xsz * ysz * mode_info->num_img_pages * (bpp / CHAR_BIT);
nuclear@7 83 return (void*)dpmi_mmap(mode_info->fb_addr, fbsize);
nuclear@7 84 }
nuclear@7 85
nuclear@7 86 int set_text_mode(void)
nuclear@7 87 {
nuclear@7 88 vbe_set_mode(0x3);
nuclear@7 89 return 0;
nuclear@7 90 }
nuclear@7 91
nuclear@7 92 int get_color_depth(void)
nuclear@7 93 {
nuclear@7 94 if(!mode_info) {
nuclear@7 95 return -1;
nuclear@7 96 }
nuclear@7 97 return mode_info->bpp;
nuclear@7 98 }
nuclear@7 99
nuclear@7 100 int get_color_bits(int *rbits, int *gbits, int *bbits)
nuclear@7 101 {
nuclear@7 102 if(!mode_info) {
nuclear@7 103 return -1;
nuclear@7 104 }
nuclear@7 105 *rbits = mode_info->rmask_size;
nuclear@7 106 *gbits = mode_info->gmask_size;
nuclear@7 107 *bbits = mode_info->bmask_size;
nuclear@7 108 return 0;
nuclear@7 109 }
nuclear@7 110
nuclear@7 111 int get_color_mask(unsigned int *rmask, unsigned int *gmask, unsigned int *bmask)
nuclear@7 112 {
nuclear@7 113 if(!mode_info) {
nuclear@7 114 return -1;
nuclear@7 115 }
nuclear@7 116 *rmask = make_mask(mode_info->rmask_size, mode_info->rpos);
nuclear@7 117 *gmask = make_mask(mode_info->gmask_size, mode_info->gpos);
nuclear@7 118 *bmask = make_mask(mode_info->bmask_size, mode_info->bpos);
nuclear@7 119 return 0;
nuclear@7 120 }
nuclear@7 121
nuclear@7 122 int get_color_shift(int *rshift, int *gshift, int *bshift)
nuclear@7 123 {
nuclear@7 124 if(!mode_info) {
nuclear@7 125 return -1;
nuclear@7 126 }
nuclear@7 127 *rshift = mode_info->rpos;
nuclear@7 128 *gshift = mode_info->gpos;
nuclear@7 129 *bshift = mode_info->bpos;
nuclear@7 130 return 0;
nuclear@7 131 }
nuclear@7 132
nuclear@7 133 void set_palette(int idx, int r, int g, int b)
nuclear@7 134 {
nuclear@7 135 int col[3];
nuclear@7 136 col[0] = r;
nuclear@7 137 col[1] = g;
nuclear@7 138 col[2] = b;
nuclear@7 139 vbe_set_palette(idx, col, 1, pal_bits);
nuclear@7 140 }
nuclear@7 141
nuclear@7 142 void wait_vsync(void)
nuclear@7 143 {
nuclear@7 144 /* TODO */
nuclear@7 145 }
nuclear@7 146
nuclear@7 147 static unsigned int make_mask(int sz, int pos)
nuclear@7 148 {
nuclear@7 149 unsigned int i, mask = 0;
nuclear@7 150
nuclear@7 151 for(i=0; i<sz; i++) {
nuclear@7 152 mask |= 1 << i;
nuclear@7 153 }
nuclear@7 154 return mask << pos;
nuclear@7 155 }
nuclear@7 156
nuclear@7 157
nuclear@7 158 #endif /* GFX_H_ */