vidmodes.c 11.5 KB
Newer Older
1 2
/* $Id$ */

deuce's avatar
deuce committed
3
#include <stdlib.h>
4
#include <stdbool.h>
deuce's avatar
deuce committed
5

6 7 8
/* xpdev: */
#include <gen_defs.h>	/* FREE_AND_NULL */

9
#include "vidmodes.h"
deuce's avatar
deuce committed
10

11
struct video_params vparams[49] = {
12
	/* BW 40x25 */
13
	{BW40, GREYSCALE_PALETTE, 40, 25, 14, 15, 16, 8, 1},
14
	/* CO 40x25 */
15
	{C40, COLOUR_PALETTE, 40, 25, 14, 15, 16, 8, 1},
16
	/* BW 80x25 */
17
	{BW80, GREYSCALE_PALETTE, 80, 25, 14, 15, 16, 8, 1},
18
	/* CO 80x25 */
19
	{C80, COLOUR_PALETTE, 80, 25, 14, 15, 16, 8, 1},
20
	/* MONO */
21
	{MONO, 0, 80, 25, 14, 15, 16, 8, 1},
22
	/* CO 40x14 */
23
	{C40X14, COLOUR_PALETTE, 40, 14, 14, 15, 16, 8, 1},
24
	/* CO 40x21 */
25
	{C40X21, COLOUR_PALETTE, 40, 21, 14, 15, 16, 8, 1},
26
	/* CO 40x28 */
27
	{C40X28, COLOUR_PALETTE, 40, 28, 12, 13, 14, 8, 1},
28
	/* CO 40x43 */
29
	{C40X43, COLOUR_PALETTE, 40, 43, 7, 7, 8, 8, 1},
30
	/* CO 40x50 */
31
	{C40X50, COLOUR_PALETTE, 40, 50, 7, 7, 8, 8, 1},
32
	/* CO 40x60 */
33
	{C40X60, COLOUR_PALETTE, 40, 60, 7, 7, 8, 8, 1},
34
	/* CO 80x14 */
35
	{C80X14, COLOUR_PALETTE, 80, 14, 14, 15, 16, 8, 1},
36
	/* CO 80x21 */
37
	{C80X21, COLOUR_PALETTE, 80, 21, 14, 15, 16, 8, 1},
38
	/* CO 80x28 */
39
	{C80X28, COLOUR_PALETTE, 80, 28, 12, 13, 14, 8, 1},
40
	/* CO 80x43 */
41
	{C80X43, COLOUR_PALETTE, 80, 43, 7, 7, 8, 8, 1},
42
	/* CO 80x50 */
43
	{C80X50, COLOUR_PALETTE, 80, 50, 7, 7, 8, 8, 1},
44
	/* CO 80x60 */
45
	{C80X60, COLOUR_PALETTE, 80, 60, 7, 7, 8, 8, 1},
46
	/* B 40x14 */
47
	{BW40X14, GREYSCALE_PALETTE, 40, 14, 14, 15, 16, 8, 1},
48
	/* BW 40x21 */
49
	{BW40X21, GREYSCALE_PALETTE, 40, 21, 14, 15, 16, 8, 1},
50
	/* BW 40x28 */
51
	{BW40X28, GREYSCALE_PALETTE, 40, 28, 12, 13, 14, 8, 1},
52
	/* BW 40x43 */
53
	{BW40X43, GREYSCALE_PALETTE, 40, 43, 7, 7, 8, 8, 1},
54
	/* BW 40x50 */
55
	{BW40X50, GREYSCALE_PALETTE, 40, 50, 7, 7, 8, 8, 1},
56
	/* BW 40x60 */
57
	{BW40X60, GREYSCALE_PALETTE, 40, 60, 7, 7, 8, 8, 1},
58
	/* BW 80x14 */
59
	{BW80X14, GREYSCALE_PALETTE, 80, 14, 14, 15, 16, 8, 1},
60
	/* BW 80x21 */
61
	{BW80X21, GREYSCALE_PALETTE, 80, 21, 14, 15, 16, 8, 1},
62
	/* BW 80x28 */
63
	{BW80X28, GREYSCALE_PALETTE, 80, 28, 12, 13, 14, 8, 1},
64
	/* BW 80x43 */
65
	{BW80X43, GREYSCALE_PALETTE, 80, 43, 7, 7, 8, 8, 1},
66
	/* BW 80x50 */
67
	{BW80X50, GREYSCALE_PALETTE, 80, 50, 7, 7, 8, 8, 1},
68
	/* BW 80x60 */
69
	{BW80X60, GREYSCALE_PALETTE, 80, 60, 7, 7, 8, 8, 1},
70
	/* MONO 80x14 */
71
	{MONO14, MONO_PALETTE, 80, 14, 14, 15, 16, 8, 1},
72
	/* MONO 80x21 */
73
	{MONO21, MONO_PALETTE, 80, 21, 14, 15, 16, 8, 1},
74
	/* MONO 80x28 */
75
	{MONO28, MONO_PALETTE, 80, 28, 12, 13, 14, 8, 1},
76
	/* MONO 80x43 */
77
	{MONO43, MONO_PALETTE, 80, 43, 7, 7, 8, 8, 1},
78
	/* MONO 80x50 */
79
	{MONO50, MONO_PALETTE, 80, 50, 7, 7, 8, 8, 1},
80
	/* MONO 80x60 */
81
	{MONO60, MONO_PALETTE, 80, 60, 7, 7, 8, 8, 1},
82
	/* Magical C4350 Mode */
83
	{C4350, COLOUR_PALETTE, 80, 50, 7, 7, 8, 8, 1},
84
	/* Commodore 64 40x25 mode */
85
	{C64_40X25, C64_PALETTE, 40, 25, 0, 7, 8, 8, 1},
86
	/* Commodore 128 40x25 mode */
87
	{C128_40X25, COLOUR_PALETTE, 40, 25, 0, 7, 8, 8, 1},
88
	/* Commodore 128 80x25 mode */
89
	{C128_80X25, COLOUR_PALETTE, 80, 25, 0, 7, 8, 8, 2},
90
	/* Atari 800 40x24 mode */
91
	{ATARI_40X24, ATARI_PALETTE, 40, 24, 0, 7, 8, 8, 1},
92
	/* Atari 800 XEP80 80x25 mode */
93
	{ATARI_80X25, GREYSCALE_PALETTE, 80, 25, 0, 15, 16, 8, 1},
deuce's avatar
deuce committed
94
	/* VESA 21x132 mode */
95
	{VESA_132X21, COLOUR_PALETTE, 132, 21, 14, 15, 16, 8, 1},
deuce's avatar
deuce committed
96
	/* VESA 25x132 mode */
97
	{VESA_132X25, COLOUR_PALETTE, 132, 25, 14, 15, 16, 8, 1},
deuce's avatar
deuce committed
98
	/* VESA 28x132 mode */
99
	{VESA_132X28, COLOUR_PALETTE, 132, 28, 12, 13, 14, 8, 1},
deuce's avatar
deuce committed
100
	/* VESA 30x132 mode */
101
	{VESA_132X30, COLOUR_PALETTE, 132, 30, 14, 15, 16, 8, 1},
deuce's avatar
deuce committed
102
	/* VESA 34x132 mode */
103
	{VESA_132X34, COLOUR_PALETTE, 132, 34, 12, 13, 14, 8, 1},
deuce's avatar
deuce committed
104
	/* VESA 43x132 mode */
105
	{VESA_132X43, COLOUR_PALETTE, 132, 43, 7, 7, 8, 8, 1},
deuce's avatar
deuce committed
106
	/* VESA 50x132 mode */
107
	{VESA_132X50, COLOUR_PALETTE, 132, 50, 7, 7, 8, 8, 1},
deuce's avatar
deuce committed
108
	/* VESA 60x132 mode */
109
	{VESA_132X60, COLOUR_PALETTE, 132, 60, 7, 7, 8, 8, 1},
110 111
};

112
uint32_t palettes[5][16] = {
113 114 115 116 117 118 119 120 121
	/* Mono */
	{ 0x00, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07,
	  0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07
	},
	/* Black and White */
	{ 0x00, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07,
	  0x08, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f
	},
	/* Colour */
122 123
	{ 0x00, 0x04, 0x02, 0x06, 0x01, 0x05, 0x03, 0x07, 
	  0x08, 0x0c, 0x0a, 0x0e, 0x09, 0x0d, 0x0b, 0x0f
deuce's avatar
deuce committed
124
	},
125
	/* C64 */
126 127
	{ 0x100, 0x101, 0x102, 0x103, 0x104, 0x105, 0x106, 0x107, 
	  0x108, 0x109, 0x10a, 0x10b, 0x10c, 0x10d, 0x10e, 0x10f
128
	},
deuce's avatar
deuce committed
129
	/* Atari */
130 131
	{ 0x110, 0x111, 0x111, 0x111, 0x111, 0x111, 0x111, 0x111,
	  0x111, 0x111, 0x111, 0x111, 0x111, 0x111, 0x111, 0x111
deuce's avatar
deuce committed
132
	},
133 134
};

135
struct dac_colors dac_default[TOTAL_DAC_SIZE] = {
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216
	/* "System" colours */
	{0, 0, 0},       // Black
	{168, 0, 0},     // Red
	{0, 168, 0},     // Green
	{168, 84, 0},    // Brown
	{0, 0, 168},     // Blue
	{168, 0, 168},   // Magenta
	{0, 168, 168},   // Cyan
	{168, 168, 168}, // Light Gray

	{84, 84, 84},    // Dark Gray
	{255, 84, 84},   // Light Red
	{84, 255, 84},   // Light Green
	{255, 255, 84},  // Yellow
	{84, 84, 255},   // Light Blue
	{255, 84, 255},  // Light Magenta
	{84, 255, 255},  // Light Cyan
	{255, 255, 255}, // White

	/* XTerm 256 colour palette */
	{0, 0, 0}, {0, 0, 95}, {0, 0, 135}, {0, 0, 175},
	{0, 0, 215}, {0, 0, 255}, {0, 95, 0}, {0, 95, 95},
	{0, 95, 135}, {0, 95, 175}, {0, 95, 215}, {0, 95, 255},
	{0, 135, 0}, {0, 135, 95}, {0, 135, 135}, {0, 135, 175},
	{0, 135, 215}, {0, 135, 255}, {0, 175, 0}, {0, 175, 95},
	{0, 175, 135}, {0, 175, 175}, {0, 175, 215}, {0, 175, 255},
	{0, 215, 0}, {0, 215, 95}, {0, 215, 135}, {0, 215, 175},
	{0, 215, 215}, {0, 215, 255}, {0, 255, 0}, {0, 255, 95},
	{0, 255, 135}, {0, 255, 175}, {0, 255, 215}, {0, 255, 255},
	{95, 0, 0}, {95, 0, 95}, {95, 0, 135}, {95, 0, 175},
	{95, 0, 215}, {95, 0, 255}, {95, 95, 0}, {95, 95, 95},
	{95, 95, 135}, {95, 95, 175}, {95, 95, 215}, {95, 95, 255},
	{95, 135, 0}, {95, 135, 95}, {95, 135, 135}, {95, 135, 175},
	{95, 135, 215}, {95, 135, 255}, {95, 175, 0}, {95, 175, 95},
	{95, 175, 135}, {95, 175, 175}, {95, 175, 215}, {95, 175, 255},
	{95, 215, 0}, {95, 215, 95}, {95, 215, 135}, {95, 215, 175},
	{95, 215, 215}, {95, 215, 255}, {95, 255, 0}, {95, 255, 95},
	{95, 255, 135}, {95, 255, 175}, {95, 255, 215}, {95, 255, 255},
	{135, 0, 0}, {135, 0, 95}, {135, 0, 135}, {135, 0, 175},
	{135, 0, 215}, {135, 0, 255}, {135, 95, 0}, {135, 95, 95},
	{135, 95, 135}, {135, 95, 175}, {135, 95, 215}, {135, 95, 255},
	{135, 135, 0}, {135, 135, 95}, {135, 135, 135}, {135, 135, 175},
	{135, 135, 215}, {135, 135, 255}, {135, 175, 0}, {135, 175, 95},
	{135, 175, 135}, {135, 175, 175}, {135, 175, 215}, {135, 175, 255},
	{135, 215, 0}, {135, 215, 95}, {135, 215, 135}, {135, 215, 175},
	{135, 215, 215}, {135, 215, 255}, {135, 255, 0}, {135, 255, 95},
	{135, 255, 135}, {135, 255, 175}, {135, 255, 215}, {135, 255, 255},
	{175, 0, 0}, {175, 0, 95}, {175, 0, 135}, {175, 0, 175},
	{175, 0, 215}, {175, 0, 255}, {175, 95, 0}, {175, 95, 95},
	{175, 95, 135}, {175, 95, 175}, {175, 95, 215}, {175, 95, 255},
	{175, 135, 0}, {175, 135, 95}, {175, 135, 135}, {175, 135, 175},
	{175, 135, 215}, {175, 135, 255}, {175, 175, 0}, {175, 175, 95},
	{175, 175, 135}, {175, 175, 175}, {175, 175, 215}, {175, 175, 255},
	{175, 215, 0}, {175, 215, 95}, {175, 215, 135}, {175, 215, 175},
	{175, 215, 215}, {175, 215, 255}, {175, 255, 0}, {175, 255, 95},
	{175, 255, 135}, {175, 255, 175}, {175, 255, 215}, {175, 255, 255},
	{215, 0, 0}, {215, 0, 95}, {215, 0, 135}, {215, 0, 175},
	{215, 0, 215}, {215, 0, 255}, {215, 95, 0}, {215, 95, 95},
	{215, 95, 135}, {215, 95, 175}, {215, 95, 215}, {215, 95, 255},
	{215, 135, 0}, {215, 135, 95}, {215, 135, 135}, {215, 135, 175},
	{215, 135, 215}, {215, 135, 255}, {215, 175, 0}, {215, 175, 95},
	{215, 175, 135}, {215, 175, 175}, {215, 175, 215}, {215, 175, 255},
	{215, 215, 0}, {215, 215, 95}, {215, 215, 135}, {215, 215, 175},
	{215, 215, 215}, {215, 215, 255}, {215, 255, 0}, {215, 255, 95},
	{215, 255, 135}, {215, 255, 175}, {215, 255, 215}, {215, 255, 255},
	{255, 0, 0}, {255, 0, 95}, {255, 0, 135}, {255, 0, 175},
	{255, 0, 215}, {255, 0, 255}, {255, 95, 0}, {255, 95, 95},
	{255, 95, 135}, {255, 95, 175}, {255, 95, 215}, {255, 95, 255},
	{255, 135, 0}, {255, 135, 95}, {255, 135, 135}, {255, 135, 175},
	{255, 135, 215}, {255, 135, 255}, {255, 175, 0}, {255, 175, 95},
	{255, 175, 135}, {255, 175, 175}, {255, 175, 215}, {255, 175, 255},
	{255, 215, 0}, {255, 215, 95}, {255, 215, 135}, {255, 215, 175},
	{255, 215, 215}, {255, 215, 255}, {255, 255, 0}, {255, 255, 95},
	{255, 255, 135}, {255, 255, 175}, {255, 255, 215}, {255, 255, 255},
	{8, 8, 8}, {18, 18, 18}, {28, 28, 28}, {38, 38, 38},
	{48, 48, 48}, {58, 58, 58}, {68, 68, 68}, {78, 78, 78},
	{88, 88, 88}, {98, 98, 98}, {108, 108, 108}, {118, 118, 118},
	{128, 128, 128}, {138, 138, 138}, {148, 148, 148}, {158, 158, 158},
	{168, 168, 168}, {178, 178, 178}, {188, 188, 188}, {198, 198, 198},
	{208, 208, 208}, {218, 218, 218}, {228, 228, 228}, {238, 238, 238}, 

217 218 219
	/* C64 colours */
	/* Black, White, Red, Cyan, Purple, Green, Blue, Yellow */
	/* Orange, Brown, Lt Red, Dk Grey, Grey, Lt Green, Lt Blue, Lt Grey */
220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236
	/* Taken from CTerm source */
	{0x00, 0x00, 0x00},	// Black
	{0xFD, 0xFE, 0xFC},	// White
	{0xBE, 0x1A, 0x24},	// Red
	{0x30, 0xE6, 0xC6},	// Cyan
	{0xB4, 0x1A, 0xE2},	// Purple
	{0x1F, 0xD2, 0x1E},	// Green
	{0x21, 0x1B, 0xAE},	// Blue
	{0xDF, 0xF6, 0x0A},	// Yellow
	{0xB8, 0x41, 0x04},	// Orange
	{0x6A, 0x33, 0x04},	// Brown
	{0xFE, 0x4A, 0x57},	// Light Red
	{0x42, 0x45, 0x40},	// Dark Grey
	{0x70, 0x74, 0x6F},	// Grey
	{0x59, 0xFE, 0x59},	// Light Green
	{0x5F, 0x53, 0xFE},	// Light Blue
	{0xA4, 0xA7, 0xA2},	// Light Grey
deuce's avatar
deuce committed
237 238
	/* Atari Colours */
	/* BG, FG */
deuce's avatar
deuce committed
239
	{0, 81, 129}, {96, 183, 231},
240 241 242 243 244 245 246 247 248 249 250 251 252
};

int find_vmode(int mode)
{
    unsigned i;

	for (i = 0; i < NUMMODES; i++)
		if (vparams[i].mode == mode)
			return i;

	return -1;
}

deuce's avatar
deuce committed
253 254 255 256 257 258 259 260 261 262 263 264 265
struct vstat_vmem *get_vmem(struct video_stats *vs)
{
	vs->vmem->refcount++;
	return vs->vmem;
}

void release_vmem(struct vstat_vmem *vm)
{
	if (vm == NULL)
		return;
	vm->refcount--;
	if (vm->refcount == 0) {
		FREE_AND_NULL(vm->vmem);
266 267
		FREE_AND_NULL(vm->fgvmem);
		FREE_AND_NULL(vm->bgvmem);
deuce's avatar
deuce committed
268 269 270 271
		FREE_AND_NULL(vm);
	}
}

272
static struct vstat_vmem *new_vmem(int cols, int rows, bool palette)
deuce's avatar
deuce committed
273 274 275 276 277 278 279 280 281 282 283
{
	struct vstat_vmem *ret = malloc(sizeof(struct vstat_vmem));

	if (ret == NULL)
		return ret;
	ret->refcount = 1;
	ret->vmem = (unsigned short *)malloc(cols*rows*sizeof(unsigned short));
	if (ret->vmem == NULL) {
		free(ret);
		return NULL;
	}
284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302
	if (palette) {
		ret->fgvmem = malloc(cols*rows*sizeof(ret->fgvmem[0]));
		if (ret->fgvmem == NULL) {
			free(ret->vmem);
			free(ret);
			return NULL;
		}
		ret->bgvmem = malloc(cols*rows*sizeof(ret->bgvmem[0]));
		if (ret->bgvmem == NULL) {
			free(ret->fgvmem);
			free(ret->vmem);
			free(ret);
			return NULL;
		}
	}
	else {
		ret->fgvmem = NULL;
		ret->bgvmem = NULL;
	}
deuce's avatar
deuce committed
303 304 305
	return ret;
}

306 307 308 309 310 311 312
int load_vmode(struct video_stats *vs, int mode)
{
	int i;

	i=find_vmode(mode);
	if(i==-1)
		return(-1);
deuce's avatar
deuce committed
313
	release_vmem(vs->vmem);
314
	vs->vmem=new_vmem(vparams[i].cols, vparams[i].rows, vs->flags & VIDMODES_FLAG_PALETTE_VMEM);
deuce's avatar
deuce committed
315 316
	if (vs->vmem == NULL)
		return -1;
317 318 319 320 321 322
	vs->rows=vparams[i].rows;
	vs->cols=vparams[i].cols;
	vs->curs_start=vparams[i].curs_start;
	vs->curs_end=vparams[i].curs_end;
	vs->default_curs_start=vparams[i].curs_start;
	vs->default_curs_end=vparams[i].curs_end;
323 324 325 326 327
	vs->curs_blink=1;
	vs->curs_visible=1;
	vs->bright_background=0;
	vs->no_bright=0;
	vs->bright_altcharset=0;
328 329
	vs->no_blink=0;
	vs->blink_altcharset=0;
330 331 332 333 334 335 336 337
	if(vs->curs_row < 0)
		vs->curs_row=0;
	if(vs->curs_row >= vparams[i].rows)
		vs->curs_row=vparams[i].rows-1;
	if(vs->curs_col < 0)
		vs->curs_col=0;
	if(vs->curs_col >= vparams[i].cols)
		vs->curs_col=vparams[i].cols-1;
338
	memcpy(vs->palette, palettes[vparams[i].palette], sizeof(vs->palette));
339 340 341
	vs->charheight=vparams[i].charheight;
	vs->charwidth=vparams[i].charwidth;
	vs->mode=mode;
342
	vs->vmultiplier=vparams[i].vmultiplier;
343 344
	return(0);
}