summaryrefslogtreecommitdiffstatshomepage
path: root/src/mame/video/exidy440.cpp
blob: f1173334a47f451636ac97379f1463f9abe96d38 (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
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
// license:BSD-3-Clause
// copyright-holders:Aaron Giles
/***************************************************************************

    Exidy 440 video system

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

#include "emu.h"
#include "includes/exidy440.h"


#define PIXEL_CLOCK         (EXIDY440_MASTER_CLOCK / 2)
#define HTOTAL              (0x1a0)
#define HBEND               (0x000)
#define HBSTART             (0x140)
#define HSEND               (0x178)
#define HSSTART             (0x160)
#define VTOTAL              (0x104)
#define VBEND               (0x000)
#define VBSTART             (0x0f0)
#define VSEND               (0x0f8)
#define VSSTART             (0x0f0)

/* Top Secret has a larger VBLANK area */
#define TOPSECEX_VBSTART    (0x0ec)

#define SPRITE_COUNT        (0x28)

/*************************************
 *
 *  Initialize the video system
 *
 *************************************/

void exidy440_state::video_start()
{
	/* reset the system */
	m_firq_enable = 0;
	m_firq_select = 0;
	m_palettebank_io = 0;
	m_palettebank_vis = 0;
	m_firq_vblank = 0;
	m_firq_beam = 0;
	m_beam_firq_count = 0;

	/* allocate a buffer for VRAM */
	m_local_videoram = std::make_unique<uint8_t[]>(256 * 256 * 2);
	memset(m_local_videoram.get(), 0, 256 * 256 * 2);

	/* allocate a buffer for palette RAM */
	m_local_paletteram = std::make_unique<uint8_t[]>(512 * 2);
	memset(m_local_paletteram.get(), 0, 512 * 2);

	m_beam_firq_timer = machine().scheduler().timer_alloc(timer_expired_delegate(FUNC(exidy440_state::beam_firq_callback), this));
	m_collide_firq_timer = machine().scheduler().timer_alloc(timer_expired_delegate(FUNC(exidy440_state::collide_firq_callback), this));
}


void topsecex_state::video_start()
{
	exidy440_state::video_start();

	m_topsecex_yscroll = 0;
}



/*************************************
 *
 *  Video RAM read/write
 *
 *************************************/

READ8_MEMBER(exidy440_state::exidy440_videoram_r)
{
	uint8_t *base = &m_local_videoram[(*m_scanline * 256 + offset) * 2];

	/* combine the two pixel values into one byte */
	return (base[0] << 4) | base[1];
}


WRITE8_MEMBER(exidy440_state::exidy440_videoram_w)
{
	uint8_t *base = &m_local_videoram[(*m_scanline * 256 + offset) * 2];

	/* expand the two pixel values into two bytes */
	base[0] = (data >> 4) & 15;
	base[1] = data & 15;
}



/*************************************
 *
 *  Palette RAM read/write
 *
 *************************************/

READ8_MEMBER(exidy440_state::exidy440_paletteram_r)
{
	return m_local_paletteram[m_palettebank_io * 512 + offset];
}


WRITE8_MEMBER(exidy440_state::exidy440_paletteram_w)
{
	/* update palette ram in the I/O bank */
	m_local_paletteram[m_palettebank_io * 512 + offset] = data;

	/* if we're modifying the active palette, change the color immediately */
	if (m_palettebank_io == m_palettebank_vis)
	{
		int word;

		/* combine two bytes into a word */
		offset = m_palettebank_vis * 512 + (offset & 0x1fe);
		word = (m_local_paletteram[offset] << 8) + m_local_paletteram[offset + 1];

		/* extract the 5-5-5 RGB colors */
		m_palette->set_pen_color(offset / 2, pal5bit(word >> 10), pal5bit(word >> 5), pal5bit(word >> 0));
	}
}



/*************************************
 *
 *  Horizontal/vertical positions
 *
 *************************************/

READ8_MEMBER(exidy440_state::exidy440_horizontal_pos_r)
{
	/* clear the FIRQ on a read here */
	m_firq_beam = 0;
	exidy440_update_firq();

	/* according to the schems, this value is only latched on an FIRQ
	 * caused by collision or beam */
	return m_latched_x;
}


READ8_MEMBER(exidy440_state::exidy440_vertical_pos_r)
{
	int result;

	/* according to the schems, this value is latched on any FIRQ
	 * caused by collision or beam, ORed together with CHRCLK,
	 * which probably goes off once per scanline; for now, we just
	 * always return the current scanline */
	result = m_screen->vpos();
	return (result < 255) ? result : 255;
}



/*************************************
 *
 *  Sprite RAM handler
 *
 *************************************/

WRITE8_MEMBER(exidy440_state::exidy440_spriteram_w)
{
	m_screen->update_partial(m_screen->vpos());
	m_spriteram[offset] = data;
}



/*************************************
 *
 *  Interrupt and I/O control regs
 *
 *************************************/

WRITE8_MEMBER(exidy440_state::exidy440_control_w)
{
	int oldvis = m_palettebank_vis;

	/* extract the various bits */
	exidy440_bank_select(data >> 4);
	m_firq_enable = (data >> 3) & 1;
	m_firq_select = (data >> 2) & 1;
	m_palettebank_io = (data >> 1) & 1;
	m_palettebank_vis = data & 1;

	/* update the FIRQ in case we enabled something */
	exidy440_update_firq();

	/* if we're swapping palettes, change all the colors */
	if (oldvis != m_palettebank_vis)
	{
		int i;

		/* pick colors from the visible bank */
		offset = m_palettebank_vis * 512;
		for (i = 0; i < 256; i++, offset += 2)
		{
			/* extract a word and the 5-5-5 RGB components */
			int word = (m_local_paletteram[offset] << 8) + m_local_paletteram[offset + 1];
			m_palette->set_pen_color(i, pal5bit(word >> 10), pal5bit(word >> 5), pal5bit(word >> 0));
		}
	}
}


WRITE8_MEMBER(exidy440_state::exidy440_interrupt_clear_w)
{
	/* clear the VBLANK FIRQ on a write here */
	m_firq_vblank = 0;
	exidy440_update_firq();
}



/*************************************
 *
 *  Interrupt generators
 *
 *************************************/

void exidy440_state::exidy440_update_firq()
{
	if (m_firq_vblank || (m_firq_enable && m_firq_beam))
		m_maincpu->set_input_line(1, ASSERT_LINE);
	else
		m_maincpu->set_input_line(1, CLEAR_LINE);
}


WRITE_LINE_MEMBER(exidy440_state::vblank_interrupt_w)
{
	/* set the FIRQ line on a VBLANK */
	if (state)
	{
		m_firq_vblank = 1;
		exidy440_update_firq();
	}
}



/*************************************
 *
 *  IRQ callback handlers
 *
 *************************************/

TIMER_CALLBACK_MEMBER(exidy440_state::beam_firq_callback)
{
	/* generate the interrupt, if we're selected */
	if (m_firq_select && m_firq_enable)
	{
		m_firq_beam = 1;
		exidy440_update_firq();
	}

	/* round the x value to the nearest byte */
	uint8_t rounded_x = (param + 1) / 2;

	/* latch the x value; this convolution comes from the read routine */
	m_latched_x = (rounded_x + 3) ^ 2;

	if (m_beam_firq_count++ < 12)
		m_beam_firq_timer->adjust(m_screen->scan_period(), param);
}


TIMER_CALLBACK_MEMBER(exidy440_state::collide_firq_callback)
{
	/* generate the interrupt, if we're selected */
	if (!m_firq_select && m_firq_enable)
	{
		m_firq_beam = 1;
		exidy440_update_firq();
	}

	/* round the x value to the nearest byte */
	param = (param + 1) / 2;

	/* latch the x value; this convolution comes from the read routine */
	m_latched_x = (param + 3) ^ 2;
}



/*************************************
 *
 *  Sprite drawing
 *
 *************************************/

void exidy440_state::draw_sprites(screen_device &screen, bitmap_ind16 &bitmap, const rectangle &cliprect, int scroll_offset, int check_collision)
{
	/* get a pointer to the palette to look for collision flags */
	uint8_t *palette = &m_local_paletteram[m_palettebank_vis * 512];
	int count = 0;

	/* draw the sprite images, checking for collisions along the way */
	uint8_t *sprite = m_spriteram + (SPRITE_COUNT - 1) * 4;

	for (int i = 0; i < SPRITE_COUNT; i++, sprite -= 4)
	{
		int image = (~sprite[3] & 0x3f);
		int xoffs = (~((sprite[1] << 8) | sprite[2]) & 0x1ff);
		int yoffs = (~sprite[0] & 0xff) + 1;
		int x, y, sy;
		uint8_t *src;

		/* skip if out of range */
		if (yoffs < cliprect.top() || yoffs >= cliprect.bottom() + 16)
			continue;

		/* get a pointer to the source image */
		src = &m_imageram[image * 128];

		/* account for large positive offsets meaning small negative values */
		if (xoffs >= 0x1ff - 16)
			xoffs -= 0x1ff;

		/* loop over y */
		sy = yoffs + scroll_offset;
		for (y = 0; y < 16; y++, yoffs--, sy--)
		{
			/* wrap at the top and bottom of the screen */
			if (sy >= VBSTART)
				sy -= (VBSTART - VBEND);
			else if (sy < VBEND)
				sy += (VBSTART - VBEND);

			/* stop if we get before the current scanline */
			if (yoffs < cliprect.top())
				break;

			/* only draw scanlines that are in this cliprect */
			if (yoffs <= cliprect.bottom())
			{
				uint8_t *old = &m_local_videoram[sy * 512 + xoffs];
				int currx = xoffs;

				/* loop over x */
				for (x = 0; x < 8; x++, old += 2)
				{
					int ipixel = *src++;
					int left = ipixel & 0xf0;
					int right = (ipixel << 4) & 0xf0;
					int pen;

					/* left pixel */
					if (left && currx >= HBEND && currx < HBSTART)
					{
						/* combine with the background */
						pen = left | old[0];
						bitmap.pix16(yoffs, currx) = pen;

						/* check the collisions bit */
						if (check_collision && (palette[2 * pen] & 0x80) && (count++ < 128))
							m_collide_firq_timer->adjust(screen.time_until_pos(yoffs, currx), currx);
					}
					currx++;

					/* right pixel */
					if (right && currx >= HBEND && currx < HBSTART)
					{
						/* combine with the background */
						pen = right | old[1];
						bitmap.pix16(yoffs, currx) = pen;

						/* check the collisions bit */
						if (check_collision && (palette[2 * pen] & 0x80) && (count++ < 128))
							m_collide_firq_timer->adjust(screen.time_until_pos(yoffs, currx), currx);
					}
					currx++;
				}
			}
			else
				src += 8;
		}
	}
}



/*************************************
 *
 *  Core refresh routine
 *
 *************************************/

void exidy440_state::update_screen(screen_device &screen, bitmap_ind16 &bitmap, const rectangle &cliprect,  int scroll_offset, int check_collision)
{
	/* draw any dirty scanlines from the VRAM directly */
	int sy = scroll_offset + cliprect.top();
	for (int y = cliprect.top(); y <= cliprect.bottom(); y++, sy++)
	{
		/* wrap at the bottom of the screen */
		if (sy >= VBSTART)
			sy -= (VBSTART - VBEND);

		/* draw line */
		draw_scanline8(bitmap, 0, y, (HBSTART - HBEND), &m_local_videoram[sy * 512], nullptr);
	}

	/* draw the sprites */
	draw_sprites(screen, bitmap, cliprect, scroll_offset, check_collision);
}



/*************************************
 *
 *  Standard screen refresh callback
 *
 *************************************/

uint32_t exidy440_state::screen_update_exidy440(screen_device &screen, bitmap_ind16 &bitmap, const rectangle &cliprect)
{
	/* redraw the screen */
	update_screen(screen, bitmap, cliprect, 0, true);

	/* generate an interrupt once/frame for the beam */
	if (cliprect.bottom() == screen.visible_area().bottom())
	{
		int beamx = ((ioport("AN0")->read() & 0xff) * (HBSTART - HBEND)) >> 8;
		int beamy = ((ioport("AN1")->read() & 0xff) * (VBSTART - VBEND)) >> 8;

		/* The timing of this FIRQ is very important. The games look for an FIRQ
		    and then wait about 650 cycles, clear the old FIRQ, and wait a
		    very short period of time (~130 cycles) for another one to come in.
		    From this, it appears that they are expecting to get beams over
		    a 12 scanline period, and trying to pick roughly the middle one.
		    This is how it is implemented. */
		attotime increment = screen.scan_period();
		attotime time = screen.time_until_pos(beamy, beamx) - increment * 6;

		m_beam_firq_count = 0;
		m_beam_firq_timer->adjust(time, beamx);
	}

	return 0;
}


uint32_t topsecex_state::screen_update_topsecex(screen_device &screen, bitmap_ind16 &bitmap, const rectangle &cliprect)
{
	/* redraw the screen */
	update_screen(screen, bitmap, cliprect, m_topsecex_yscroll, false);

	return 0;
}



/*************************************
 *
 *  Machine drivers
 *
 *************************************/

void exidy440_state::exidy440_video(machine_config &config)
{
	PALETTE(config, m_palette).set_entries(512);

	SCREEN(config, m_screen, SCREEN_TYPE_RASTER);
	m_screen->set_video_attributes(VIDEO_ALWAYS_UPDATE);
	m_screen->set_raw(PIXEL_CLOCK, HTOTAL, HBEND, HBSTART, VTOTAL, VBEND, VBSTART);
	m_screen->set_screen_update(FUNC(exidy440_state::screen_update_exidy440));
	m_screen->set_palette(m_palette);
	m_screen->screen_vblank().set(FUNC(exidy440_state::vblank_interrupt_w));
	m_screen->screen_vblank().append(m_custom, FUNC(exidy440_sound_device::sound_interrupt_w));
}


void topsecex_state::topsecex_video(machine_config &config)
{
	m_screen->set_video_attributes(0);
	m_screen->set_raw(PIXEL_CLOCK, HTOTAL, HBEND, HBSTART, VTOTAL, VBEND, TOPSECEX_VBSTART);
	m_screen->set_screen_update(FUNC(topsecex_state::screen_update_topsecex));
}