summaryrefslogtreecommitdiffstatshomepage
path: root/src/mame/video/redalert.c
blob: db0686603cbbeff8c9bc87cf087cf840a08ddf2a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
/***************************************************************************

    Irem Red Alert hardware

    If you have any questions about how this driver works, don't hesitate to
    ask.  - Mike Balfour (mab22@po.cwru.edu)

****************************************************************************/

#include "emu.h"
#include "video/resnet.h"
#include "includes/redalert.h"


#define NUM_CHARMAP_PENS	0x200
#define NUM_BITMAP_PENS		8


/*************************************
 *
 *  Bitmap videoram write handler
 *
 *************************************/

WRITE8_HANDLER( redalert_bitmap_videoram_w )
{
	redalert_state *state = space->machine->driver_data<redalert_state>();
	state->bitmap_videoram[offset     ] = data;
	state->bitmap_colorram[offset >> 3] = *state->bitmap_color & 0x07;
}



/*************************************
 *
 *  Color generation
 *
 *************************************/

static void get_pens(running_machine *machine, pen_t *pens)
{
	static const int resistances_bitmap[]     = { 100 };
	static const int resistances_charmap_rg[] = { 390, 220, 180 };
	static const int resistances_charmap_b[]  = { 220, 100 };
	static const int resistances_back_r[]     = { 1000 + 100 };
	static const int resistances_back_gb[]    = { 100 + 470 };

	offs_t offs;
	double scaler;
	double bitmap_weight[2];
	double charmap_rg_weights[3];
	double charmap_b_weights[2];
	double back_r_weight[1];
	double back_gb_weight[1];
	const UINT8 *prom = machine->region("proms")->base();

	scaler = compute_resistor_weights(0, 0xff, -1,
									  1, resistances_bitmap,     bitmap_weight,      470, 0,
									  3, resistances_charmap_rg, charmap_rg_weights, 470, 0,
									  2, resistances_charmap_b,  charmap_b_weights,  470, 0);

			 compute_resistor_weights(0, 0xff, scaler,
									  1, resistances_back_r,     back_r_weight,      470, 0,
									  1, resistances_back_gb,    back_gb_weight,     470, 0,
									  0, 0, 0, 0, 0);

	/* the character layer colors come from the PROM */
	for (offs = 0; offs < NUM_CHARMAP_PENS; offs++)
	{
		UINT8 data = prom[offs];

		/* very strange mapping */
		UINT8 r0_bit = (data >> 2) & 0x01;
		UINT8 r1_bit = (data >> 6) & 0x01;
		UINT8 r2_bit = (data >> 4) & 0x01;
		UINT8 g0_bit = (data >> 1) & 0x01;
		UINT8 g1_bit = (data >> 3) & 0x01;
		UINT8 g2_bit = (data >> 5) & 0x01;
		UINT8 b0_bit = (data >> 0) & 0x01;
		UINT8 b1_bit = (data >> 7) & 0x01;

		UINT8 r = combine_3_weights(charmap_rg_weights, r0_bit, r1_bit, r2_bit);
		UINT8 g = combine_3_weights(charmap_rg_weights, g0_bit, g1_bit, g2_bit);
		UINT8 b = combine_2_weights(charmap_b_weights,  b0_bit, b1_bit);

		pens[offs] = MAKE_RGB(r, g, b);
	}

	/* the bitmap layer colors are directly mapped */
	for (offs = 0; offs < NUM_BITMAP_PENS; offs++)
	{
		UINT8 r = bitmap_weight[(offs >> 2) & 0x01];
		UINT8 g = bitmap_weight[(offs >> 1) & 0x01];
		UINT8 b = bitmap_weight[(offs >> 0) & 0x01];

		pens[NUM_CHARMAP_PENS + offs] = MAKE_RGB(r, g, b);
	}

	/* background color */
	pens[NUM_CHARMAP_PENS + NUM_BITMAP_PENS] = MAKE_RGB(back_r_weight[0], back_gb_weight[0], back_gb_weight[0]);
}

/* this uses the same color hook-up between bitmap and chars. */
/* TODO: clean me up */
static void get_panther_pens(running_machine *machine, pen_t *pens)
{
	static const int resistances_bitmap[]     = { 100 };
	static const int resistances_charmap_rg[] = { 390, 220, 180 };
	static const int resistances_charmap_b[]  = { 220, 100 };
	static const int resistances_back_r[]     = { 1000 + 100 };
	static const int resistances_back_gb[]    = { 100 + 470 };

	offs_t offs;
	double scaler;
	double bitmap_weight[2];
	double charmap_rg_weights[3];
	double charmap_b_weights[2];
	double back_r_weight[1];
	double back_gb_weight[1];
	const UINT8 *prom = machine->region("proms")->base();

	scaler = compute_resistor_weights(0, 0xff, -1,
									  1, resistances_bitmap,     bitmap_weight,      470, 0,
									  3, resistances_charmap_rg, charmap_rg_weights, 470, 0,
									  2, resistances_charmap_b,  charmap_b_weights,  470, 0);

			 compute_resistor_weights(0, 0xff, scaler,
									  1, resistances_back_r,     back_r_weight,      470, 0,
									  1, resistances_back_gb,    back_gb_weight,     470, 0,
									  0, 0, 0, 0, 0);

	/* the character layer colors come from the PROM */
	for (offs = 0; offs < NUM_CHARMAP_PENS; offs++)
	{
		UINT8 data = prom[offs];

		UINT8 r = bitmap_weight[(~data >> 2) & 0x01];
		UINT8 g = bitmap_weight[(~data >> 1) & 0x01];
		UINT8 b = bitmap_weight[(~data >> 0) & 0x01];

		pens[offs] = MAKE_RGB(r, g, b);
	}

	/* the bitmap layer colors are directly mapped */
	for (offs = 0; offs < NUM_BITMAP_PENS; offs++)
	{
		UINT8 r = bitmap_weight[(offs >> 2) & 0x01];
		UINT8 g = bitmap_weight[(offs >> 1) & 0x01];
		UINT8 b = bitmap_weight[(offs >> 0) & 0x01];

		pens[NUM_CHARMAP_PENS + offs] = MAKE_RGB(r, g, b);
	}

	/* background color */
	pens[NUM_CHARMAP_PENS + NUM_BITMAP_PENS] = MAKE_RGB(back_r_weight[0], back_gb_weight[0], back_gb_weight[0]);
}

/*************************************
 *
 *  Video hardware start
 *
 *************************************/

static VIDEO_START( redalert )
{
	redalert_state *state = machine->driver_data<redalert_state>();
	state->bitmap_colorram = auto_alloc_array(machine, UINT8, 0x0400);

	state->save_pointer(NAME(state->bitmap_colorram), 0x0400);

	state->control_xor = 0x00;
}

static VIDEO_START( ww3 )
{
	redalert_state *state = machine->driver_data<redalert_state>();
	VIDEO_START_CALL( redalert );

	state->control_xor = 0x04;
}


/*************************************
 *
 *  Red Alert video update
 *
 *************************************/

static SCREEN_UPDATE( redalert )
{
	redalert_state *state = screen->machine->driver_data<redalert_state>();
	pen_t pens[NUM_CHARMAP_PENS + NUM_BITMAP_PENS + 1];
	offs_t offs;

	get_pens(screen->machine, pens);

	for (offs = 0; offs < 0x2000; offs++)
	{
		int i;
		UINT8 charmap_data_1;
		UINT8 charmap_data_2;

		UINT8 y = offs & 0xff;
		UINT8 x = (~offs >> 8) << 3;

		UINT8 bitmap_data = state->bitmap_videoram[offs];
		UINT8 bitmap_color = state->bitmap_colorram[offs >> 3];

		UINT8 charmap_code = state->charmap_videoram[0x0000 | (offs >> 3)];
		offs_t charmap_data_base = ((charmap_code & 0x7f) << 3) | (offs & 0x07);

		/* D7 of the char code selects the char set to use */
		if (charmap_code & 0x80)
		{
			charmap_data_1 = state->charmap_videoram[0x0400 | charmap_data_base];
			charmap_data_2 = state->charmap_videoram[0x0c00 | charmap_data_base];
		}
		else
		{
			charmap_data_1 = 0; /* effectively disables A0 of the color PROM */
			charmap_data_2 = state->charmap_videoram[0x0800 | charmap_data_base];
		}

		for (i = 0; i < 8; i++)
		{
			pen_t pen;

			int bitmap_bit = bitmap_data & 0x80;
			UINT8 color_prom_a0_a1 = ((charmap_data_2 & 0x80) >> 6) | ((charmap_data_1 & 0x80) >> 7);

			/* determine priority */
			if ((color_prom_a0_a1 == 0) || (bitmap_bit && ((charmap_code & 0xc0) == 0xc0)))
				pen = bitmap_bit ? pens[NUM_CHARMAP_PENS + bitmap_color] : pens[NUM_CHARMAP_PENS + NUM_BITMAP_PENS];
			else
				pen = pens[((charmap_code & 0xfe) << 1) | color_prom_a0_a1];

			if ((*state->video_control ^ state->control_xor) & 0x04)
				*BITMAP_ADDR32(bitmap, y, x) = pen;
			else
				*BITMAP_ADDR32(bitmap, y ^ 0xff, x ^ 0xff) = pen;

			/* next pixel */
			x = x + 1;

			bitmap_data    = bitmap_data    << 1;
			charmap_data_1 = charmap_data_1 << 1;
			charmap_data_2 = charmap_data_2 << 1;
		}
	}

	return 0;
}



/*************************************
 *
 *  Demoneye-X video update
 *
 *************************************/

static SCREEN_UPDATE( demoneye )
{
	redalert_state *state = screen->machine->driver_data<redalert_state>();
	pen_t pens[NUM_CHARMAP_PENS + NUM_BITMAP_PENS + 1];
	offs_t offs;

	get_pens(screen->machine, pens);

	for (offs = 0; offs < 0x2000; offs++)
	{
		int i;
		UINT8 charmap_data_1;
		UINT8 charmap_data_2;

		UINT8 y = offs & 0xff;
		UINT8 x = (~offs >> 8) << 3;

		UINT8 bitmap_data = state->bitmap_videoram[offs];
		UINT8 bitmap_color = state->bitmap_colorram[offs >> 3];

		UINT8 charmap_code = state->charmap_videoram[0x1000 | (offs >> 3)];
		offs_t charmap_data_base = ((charmap_code & 0x7f) << 3) | (offs & 0x07);

		/* D7 of the char code selects the char set to use */
		if (charmap_code & 0x80)
		{
			charmap_data_1 = state->charmap_videoram[0x0400 | charmap_data_base];
			charmap_data_2 = state->charmap_videoram[0x0c00 | charmap_data_base];
		}
		else
		{
			charmap_data_1 = state->charmap_videoram[0x0000 | charmap_data_base];
			charmap_data_2 = state->charmap_videoram[0x0800 | charmap_data_base];
		}

		/* this is the mapping of the 3rd char set */
		//charmap_data_1 = state->charmap_videoram[0x1400 | charmap_data_base];
		//charmap_data_2 = state->charmap_videoram[0x1c00 | charmap_data_base];

		for (i = 0; i < 8; i++)
		{
			pen_t pen;

			int bitmap_bit = bitmap_data & 0x80;
			UINT8 color_prom_a0_a1 = ((charmap_data_2 & 0x80) >> 6) | ((charmap_data_1 & 0x80) >> 7);

			/* determine priority */
			if ((color_prom_a0_a1 == 0) || (bitmap_bit && ((charmap_code & 0xc0) == 0xc0)))
				pen = bitmap_bit ? pens[NUM_CHARMAP_PENS + bitmap_color] : pens[NUM_CHARMAP_PENS + NUM_BITMAP_PENS];
			else
				pen = pens[((charmap_code & 0xfe) << 1) | color_prom_a0_a1];

			if (*state->video_control & 0x04)
				*BITMAP_ADDR32(bitmap, y ^ 0xff, x ^ 0xff) = pen;
			else
				*BITMAP_ADDR32(bitmap, y, x) = pen;

			/* next pixel */
			x = x + 1;

			bitmap_data    = bitmap_data    << 1;
			charmap_data_1 = charmap_data_1 << 1;
			charmap_data_2 = charmap_data_2 << 1;
		}
	}

	return 0;
}

/*************************************
 *
 *  Panther video update
 *
 *************************************/

static SCREEN_UPDATE( panther )
{
	redalert_state *state = screen->machine->driver_data<redalert_state>();
	pen_t pens[NUM_CHARMAP_PENS + NUM_BITMAP_PENS + 1];
	offs_t offs;

	get_panther_pens(screen->machine, pens);

	for (offs = 0; offs < 0x2000; offs++)
	{
		int i;
		UINT8 charmap_data_1;
		UINT8 charmap_data_2;

		UINT8 y = offs & 0xff;
		UINT8 x = (~offs >> 8) << 3;

		UINT8 bitmap_data = state->bitmap_videoram[offs];
		UINT8 bitmap_color = state->bitmap_colorram[offs >> 3];

		UINT8 charmap_code = state->charmap_videoram[0x0000 | (offs >> 3)];
		offs_t charmap_data_base = ((charmap_code & 0x7f) << 3) | (offs & 0x07);

		/* D7 of the char code selects the char set to use */
		if (charmap_code & 0x80)
		{
			charmap_data_1 = state->charmap_videoram[0x0400 | charmap_data_base];
			charmap_data_2 = state->charmap_videoram[0x0c00 | charmap_data_base];
		}
		else
		{
			charmap_data_1 = 0; /* effectively disables A0 of the color PROM */
			charmap_data_2 = state->charmap_videoram[0x0800 | charmap_data_base];
		}

		for (i = 0; i < 8; i++)
		{
			pen_t pen;

			int bitmap_bit = bitmap_data & 0x80;
			UINT8 color_prom_a0_a1 = ((charmap_data_2 & 0x80) >> 6) | ((charmap_data_1 & 0x80) >> 7);

			/* determine priority */
			if ((color_prom_a0_a1 == 0) || (bitmap_bit && ((charmap_code & 0xc0) == 0xc0)))
				pen = bitmap_bit ? pens[NUM_CHARMAP_PENS + bitmap_color] : pens[NUM_CHARMAP_PENS + NUM_BITMAP_PENS];
			else
				pen = pens[((charmap_code & 0xfe) << 1) | color_prom_a0_a1];

			if ((*state->video_control ^ state->control_xor) & 0x04)
				*BITMAP_ADDR32(bitmap, y, x) = pen;
			else
				*BITMAP_ADDR32(bitmap, y ^ 0xff, x ^ 0xff) = pen;

			/* next pixel */
			x = x + 1;

			bitmap_data    = bitmap_data    << 1;
			charmap_data_1 = charmap_data_1 << 1;
			charmap_data_2 = charmap_data_2 << 1;
		}
	}

	return 0;
}


/*************************************
 *
 *  Red Alert machine driver
 *
 *************************************/

static MACHINE_CONFIG_FRAGMENT( redalert_video_common )
	MCFG_SCREEN_ADD("screen", RASTER)
	MCFG_SCREEN_FORMAT(BITMAP_FORMAT_RGB32)
	MCFG_SCREEN_REFRESH_RATE(60)
	MCFG_SCREEN_VBLANK_TIME(ATTOSECONDS_IN_USEC(0))
	MCFG_SCREEN_SIZE(32*8, 32*8)
	MCFG_SCREEN_VISIBLE_AREA(0*8, 32*8-1, 1*8, 31*8-1)
	MCFG_SCREEN_UPDATE(redalert)
MACHINE_CONFIG_END

MACHINE_CONFIG_FRAGMENT( redalert_video )

	MCFG_VIDEO_START(redalert)
	MCFG_FRAGMENT_ADD( redalert_video_common )

MACHINE_CONFIG_END

MACHINE_CONFIG_FRAGMENT( ww3_video )

	MCFG_VIDEO_START( ww3 )
	MCFG_FRAGMENT_ADD( redalert_video_common )

MACHINE_CONFIG_END


/*************************************
 *
 *  Demoneye-X machine driver
 *
 *************************************/

MACHINE_CONFIG_FRAGMENT( demoneye_video )
	MCFG_VIDEO_START(redalert)
	
	MCFG_SCREEN_ADD("screen", RASTER)
	MCFG_SCREEN_FORMAT(BITMAP_FORMAT_RGB32)
	MCFG_SCREEN_REFRESH_RATE(60)
	MCFG_SCREEN_VBLANK_TIME(ATTOSECONDS_IN_USEC(0))
	MCFG_SCREEN_SIZE(32*8, 32*8)
	MCFG_SCREEN_VISIBLE_AREA(0*8, 32*8-1, 1*8, 31*8-1)
	MCFG_SCREEN_UPDATE(demoneye)
MACHINE_CONFIG_END


MACHINE_CONFIG_FRAGMENT( panther_video )

	MCFG_VIDEO_START(ww3)
	
	MCFG_SCREEN_ADD("screen", RASTER)
	MCFG_SCREEN_FORMAT(BITMAP_FORMAT_RGB32)
	MCFG_SCREEN_REFRESH_RATE(60)
	MCFG_SCREEN_VBLANK_TIME(ATTOSECONDS_IN_USEC(0))
	MCFG_SCREEN_SIZE(32*8, 32*8)
	MCFG_SCREEN_VISIBLE_AREA(0*8, 32*8-1, 1*8, 31*8-1)
	MCFG_SCREEN_UPDATE(panther)
MACHINE_CONFIG_END