summaryrefslogtreecommitdiffstatshomepage
path: root/src/mame/video/jedi.c
blob: b50fa026c06493336cffddb24417bd35976f2985 (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
/***************************************************************************

    Atari Return of the Jedi hardware

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

#include "driver.h"
#include "jedi.h"


/* globals */
UINT8 *jedi_backgroundram;
size_t jedi_backgroundram_size;
UINT8 *jedi_PIXIRAM;


/* local variables */
static UINT32 jedi_vscroll;
static UINT32 jedi_hscroll;
static UINT32 jedi_alpha_bank;
static UINT8 video_off, smooth_table;
static UINT8 *fgdirty, *bgdirty;
static mame_bitmap *fgbitmap, *mobitmap, *bgbitmap, *bgexbitmap;



/*************************************
 *
 *  Video startup
 *
 *************************************/

static void jedi_postload(void)
{
	memset(fgdirty, 1, videoram_size);
	memset(bgdirty, 1, jedi_backgroundram_size);
}


VIDEO_START( jedi )
{
	/* allocate dirty buffer for the foreground characters */
	fgdirty = dirtybuffer = auto_malloc(videoram_size);
	memset(fgdirty, 1, videoram_size);

	/* allocate an 8bpp bitmap for the raw foreground characters */
	fgbitmap = auto_bitmap_alloc(machine->screen[0].width, machine->screen[0].height, machine->screen[0].format);

	/* allocate an 8bpp bitmap for the motion objects */
	mobitmap = auto_bitmap_alloc(machine->screen[0].width, machine->screen[0].height, machine->screen[0].format);
	fillbitmap(mobitmap, 0, &machine->screen[0].visarea);

	/* allocate dirty buffer for the background characters */
	bgdirty = auto_malloc(jedi_backgroundram_size);
	memset(bgdirty, 1, jedi_backgroundram_size);

	/* the background area is 256x256, doubled by the hardware*/
	bgbitmap = auto_bitmap_alloc(256, 256, machine->screen[0].format);

	/* the expanded background area is 512x512 */
	bgexbitmap = auto_bitmap_alloc(512, 512, machine->screen[0].format);

	/* reserve color 1024 for black (disabled display) */
	palette_set_color(machine, 1024, MAKE_RGB(0, 0, 0));

	/* register for saving */
	state_save_register_global(jedi_vscroll);
	state_save_register_global(jedi_hscroll);
	state_save_register_global(jedi_alpha_bank);
	state_save_register_global(video_off);
	state_save_register_global(smooth_table);
	state_save_register_func_postload(jedi_postload);
}



/*************************************
 *
 *  Palette RAM
 *
 *************************************
 *
 *  Color RAM format
 *  Color RAM is 1024x12
 *
 *  RAM address: A0..A3 = Playfield color code
 *      A4..A7 = Motion object color code
 *      A8..A9 = Alphanumeric color code
 *
 *  RAM data:
 *      0..2 = Blue
 *      3..5 = Green
 *      6..8 = Blue
 *      9..11 = Intensity
 *
 *  Output resistor values:
 *      bit 0 = 22K
 *      bit 1 = 10K
 *      bit 2 = 4.7K
 *
 *************************************/

WRITE8_HANDLER( jedi_paletteram_w )
{
    int r, g, b, bits, intensity;
    UINT32 color;

	paletteram[offset] = data;
	color = paletteram[offset & 0x3FF] | (paletteram[offset | 0x400] << 8);

	intensity = (color >> 9) & 7;
	bits = (color >> 6) & 7;
	r = 5 * bits * intensity;
	bits = (color >> 3) & 7;
	g = 5 * bits * intensity;
	bits = (color >> 0) & 7;
	b = 5 * bits * intensity;

	palette_set_color(Machine, offset & 0x3ff, MAKE_RGB(r, g, b));
}



/*************************************
 *
 *  Background access
 *
 *************************************/

WRITE8_HANDLER( jedi_backgroundram_w )
{
	if (jedi_backgroundram[offset] != data)
	{
		bgdirty[offset] = 1;
		jedi_backgroundram[offset] = data;
	}
}



/*************************************
 *
 *  Foreground banking
 *
 *************************************/

WRITE8_HANDLER( jedi_alpha_banksel_w )
{
	if (jedi_alpha_bank != 2 * (data & 0x80))
	{
		jedi_alpha_bank = 2 * (data & 0x80);
		memset(fgdirty, 1, videoram_size);
	}
}



/*************************************
 *
 *  Scroll offsets
 *
 *************************************/

WRITE8_HANDLER( jedi_vscroll_w )
{
    jedi_vscroll = data | (offset << 8);
}


WRITE8_HANDLER( jedi_hscroll_w )
{
    jedi_hscroll = data | (offset << 8);
}



/*************************************
 *
 *  Video control
 *
 *************************************/

WRITE8_HANDLER( jedi_video_off_w )
{
	video_off = data;
}


WRITE8_HANDLER( jedi_PIXIRAM_w )
{
	smooth_table = data & 0x03;
	memset(bgdirty, 1, jedi_backgroundram_size);
}



/*************************************
 *
 *  Background smoothing
 *
 *************************************/

static void update_smoothing(int bgtilerow, int first, int last)
{
	UINT8 *prom = memory_region(REGION_PROMS) + smooth_table * 0x100;
	UINT8 bgscan[2][256];
	UINT8 *bgcurr = bgscan[0], *bglast = bgscan[1];
	int xstart, xstop, x, y;

	/*
        smoothing notes:
            * even scanlines blend the previous (Y-1) and current (Y) line
            * odd scanlines are just taken from the current line (Y)
            * therefore, if we modify source scanlines 8-15, we must update dest scanlines 16-32

            * even pixels are just taken from the current pixel (X)
            * odd pixels blend the current (X) and next (X+1) pixels
            * therefore, if we modify source pixels 8-15, we must update dest pixels 15-31
    */

	/* compute x start/stop in destination coordinates */
	xstart = first * 16 - 1;
	xstop = last * 16 + 15;

	/* extract the previous bg scanline */
	extract_scanline8(bgbitmap, 0, ((bgtilerow * 16 - 1) & 0x1ff) / 2, 256, bgcurr);

	/* loop over height */
	for (y = 0; y <= 16; y++)
	{
		int curry = (bgtilerow * 16 + y) & 0x1ff;

		/* swap background buffers */
		UINT8 *bgtemp = bgcurr;
		bgcurr = bglast;
		bglast = bgtemp;

		/* extract current bg scanline */
		extract_scanline8(bgbitmap, 0, curry / 2, 256, bgcurr);

		/* loop over columns */
		for (x = xstart; x <= xstop; x++)
		{
			int tr = bglast[((x + 1) & 0x1ff) / 2];
			int br = bgcurr[((x + 1) & 0x1ff) / 2];

			/* smooth pixels */
			if (x & 1)
			{
				int tl = bglast[(x & 0x1ff) / 2];
				int bl = bgcurr[(x & 0x1ff) / 2];
				int mixt = prom[16 * tl + tr];
				int mixb = prom[16 * bl + br];
				*BITMAP_ADDR16(bgexbitmap, curry, x & 0x1ff) = prom[0x400 + 16 * mixt + mixb];
			}
			else
				*BITMAP_ADDR16(bgexbitmap, curry, x & 0x1ff) = prom[0x400 + 16 * tr + br];
		}
	}
}



/*************************************
 *
 *  Core video refresh
 *
 *************************************/

VIDEO_UPDATE( jedi )
{
	int bgexdirty[32][2];
	int offs;


	/* if no video, clear it all to black */
	if (video_off)
	{
		fillbitmap(bitmap, machine->pens[1024], &machine->screen[0].visarea);
		return 0;
	}

	/* Return of the Jedi has a peculiar playfield/motion object priority system. That */
	/* is, there is no priority system ;-) The color of the pixel which appears on */
	/* screen depends on all three of the foreground, background and motion objects. The */
	/* 1024 colors palette is appropriately set up by the program to "emulate" a */
	/* priority system, but it can also be used to display completely different */
	/* colors (see the palette test in service mode) */

    /* update foreground bitmap as a raw bitmap*/
    for (offs = videoram_size - 1; offs >= 0; offs--)
		if (fgdirty[offs])
		{
			int sx = offs % 64;
			int sy = offs / 64;

			fgdirty[offs] = 0;

			drawgfx(fgbitmap, machine->gfx[0], videoram[offs] + jedi_alpha_bank,
					0, 0, 0, 8*sx, 8*sy, &machine->screen[0].visarea, TRANSPARENCY_NONE_RAW, 0);
		}

	/* reset the expanded dirty array */
	for (offs = 0; offs < 32; offs++)
		bgexdirty[offs][0] = bgexdirty[offs][1] = -1;

    /* update background bitmap as a raw bitmap */
	for (offs = jedi_backgroundram_size / 2 - 1; offs >= 0; offs--)
		if (bgdirty[offs] || bgdirty[offs + 0x400])
		{
			int sx = offs % 32;
			int sy = offs / 32;
			int code = (jedi_backgroundram[offs] & 0xFF);
			int bank = (jedi_backgroundram[offs + 0x400] & 0x0F);

			/* shuffle the bank bits in */
			code |= (bank & 0x01) << 8;
			code |= (bank & 0x08) << 6;
			code |= (bank & 0x02) << 9;

			bgdirty[offs] = bgdirty[offs + 0x400] = 0;

			/* update expanded dirty status (assumes we go right-to-left) */
			if (bgexdirty[sy][1] == -1)
				bgexdirty[sy][1] = sx;
			bgexdirty[sy][0] = sx;

			drawgfx(bgbitmap, machine->gfx[1], code,
					0, bank & 0x04, 0, 8*sx, 8*sy, 0, TRANSPARENCY_NONE_RAW, 0);
		}

	/* update smoothed version of background */
	for (offs = 0; offs < 32; offs++)
		if (bgexdirty[offs][1] != -1)
			update_smoothing(offs, bgexdirty[offs][0], bgexdirty[offs][1]);

	/* draw the motion objects */
    for (offs = 0; offs < 0x30; offs++)
	{
		/* coordinates adjustments made to match screenshot */
		int x = spriteram[offs + 0x100] + ((spriteram[offs + 0x40] & 0x01) << 8) - 2;
		int y = 240 - spriteram[offs + 0x80] + 1;
		int flipx = spriteram[offs + 0x40] & 0x10;
		int flipy = spriteram[offs + 0x40] & 0x20;
		int tall = spriteram[offs + 0x40] & 0x08;
		int code, bank;

		/* shuffle the bank bits in */
		bank  = ((spriteram[offs + 0x40] & 0x02) >> 1);
		bank |= ((spriteram[offs + 0x40] & 0x40) >> 5);
		bank |=  (spriteram[offs + 0x40] & 0x04);
		code = spriteram[offs] + (bank * 256);

		/* adjust for double-height */
		if (tall)
			code |= 1;

		/* draw motion object */
		drawgfx(mobitmap, machine->gfx[2], code,
				0, flipx, flipy, x, y, &machine->screen[0].visarea, TRANSPARENCY_PEN_RAW, 0);

		/* handle double-height */
		if (tall)
			drawgfx(mobitmap, machine->gfx[2], code - 1,
					0, flipx, flipy, x, y - 16, &machine->screen[0].visarea, TRANSPARENCY_PEN_RAW, 0);
    }

	/* compose the three layers */
	{
		int xscroll = -jedi_hscroll;
		int yscroll = -jedi_vscroll;
		copyscrollbitmap(bitmap, bgexbitmap, 1, &xscroll, 1, &yscroll, &machine->screen[0].visarea, TRANSPARENCY_NONE, 0);
		copybitmap(bitmap, mobitmap, 0, 0, 0, 0, &machine->screen[0].visarea, TRANSPARENCY_BLEND_RAW, 4);
		copybitmap(bitmap, fgbitmap, 0, 0, 0, 0, &machine->screen[0].visarea, TRANSPARENCY_BLEND, 8);
	}

	/* erase the motion objects */
    for (offs = 0; offs < 0x30; offs++)
	{
		/* coordinates adjustments made to match screenshot */
		int x = spriteram[offs + 0x100] + ((spriteram[offs + 0x40] & 0x01) << 8) - 2;
		int y = 240 - spriteram[offs + 0x80] + 1;
		int tall = spriteram[offs + 0x40] & 0x08;
		rectangle bounds;

		/* compute the bounds */
		bounds.min_x = x;
		bounds.max_x = x + 15;
		bounds.min_y = tall ? (y - 16) : y;
		bounds.max_y = y + (tall ? 31 : 15);
		fillbitmap(mobitmap, 0, &bounds);
    }
	return 0;
}