summaryrefslogtreecommitdiffstatshomepage
path: root/src/emu/sound/cdp1864.c
blob: 934eaa7aea05a451228efd494538eb67ce63ec15 (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
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
/**********************************************************************

    RCA CDP1864C COS/MOS PAL Compatible Color TV Interface

    Copyright MESS Team.
    Visit http://mamedev.org for licensing and usage restrictions.

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

/*

    TODO:

    - interlace mode
    - PAL output, currently using RGB
    - cpu synchronization

        SC1 and SC0 are used to provide CDP1864C-to-CPU synchronization for a jitter-free display.
        During every horizontal sync the CDP1864C samples SC0 and SC1 for SC0 = 1 and SC1 = 0
        (CDP1800 execute state). Detection of a fetch cycle causes the CDP1864C to skip cycles to
        attain synchronization. (i.e. picture moves 8 pixels to the right)

*/

#include "emu.h"
#include "streams.h"
#include "cdp1864.h"

/***************************************************************************
    PARAMETERS
***************************************************************************/

#define CDP1864_DEFAULT_LATCH		0x35

#define CDP1864_CYCLES_DMA_START	2*8
#define CDP1864_CYCLES_DMA_ACTIVE	8*8
#define CDP1864_CYCLES_DMA_WAIT		6*8

static const int CDP1864_BACKGROUND_COLOR_SEQUENCE[] = { 2, 0, 1, 4 };

/***************************************************************************
    TYPE DEFINITIONS
***************************************************************************/

typedef struct _cdp1864_t cdp1864_t;
struct _cdp1864_t
{
	devcb_resolved_read_line		in_rdata_func;
	devcb_resolved_read_line		in_bdata_func;
	devcb_resolved_read_line		in_gdata_func;
	devcb_resolved_write_line		out_int_func;
	devcb_resolved_write_line		out_dmao_func;
	devcb_resolved_write_line		out_efx_func;

	screen_device *screen;	/* screen */
	bitmap_t *bitmap;				/* bitmap */
	sound_stream *stream;			/* sound output */

	/* video state */
	int disp;						/* display on */
	int dmaout;						/* DMA request active */
	int bgcolor;					/* background color */
	int con;						/* color on */

	/* sound state */
	int aoe;						/* audio on */
	int latch;						/* sound latch */
	INT16 signal;					/* current signal */
	int incr;						/* initial wave state */

	/* timers */
	emu_timer *int_timer;			/* interrupt timer */
	emu_timer *efx_timer;			/* EFx timer */
	emu_timer *dma_timer;			/* DMA timer */

	running_device *cpu;
};

/***************************************************************************
    INLINE FUNCTIONS
***************************************************************************/

INLINE cdp1864_t *get_safe_token(running_device *device)
{
	assert(device != NULL);
	return (cdp1864_t *)downcast<legacy_device_base *>(device)->token();
}

/***************************************************************************
    IMPLEMENTATION
***************************************************************************/

/*-------------------------------------------------
    TIMER_CALLBACK( cdp1864_int_tick )
-------------------------------------------------*/

static TIMER_CALLBACK( cdp1864_int_tick )
{
	running_device *device = (running_device *) ptr;
	cdp1864_t *cdp1864 = get_safe_token(device);

	int scanline = cdp1864->screen->vpos();

	if (scanline == CDP1864_SCANLINE_INT_START)
	{
		if (cdp1864->disp)
		{
			devcb_call_write_line(&cdp1864->out_int_func, ASSERT_LINE);
		}

		timer_adjust_oneshot(cdp1864->int_timer, cdp1864->screen->time_until_pos(CDP1864_SCANLINE_INT_END), 0);
	}
	else
	{
		if (cdp1864->disp)
		{
			devcb_call_write_line(&cdp1864->out_int_func, CLEAR_LINE);
		}

		timer_adjust_oneshot(cdp1864->int_timer, cdp1864->screen->time_until_pos(CDP1864_SCANLINE_INT_START), 0);
	}
}

/*-------------------------------------------------
    TIMER_CALLBACK( cdp1864_efx_tick )
-------------------------------------------------*/

static TIMER_CALLBACK( cdp1864_efx_tick )
{
	running_device *device = (running_device *) ptr;
	cdp1864_t *cdp1864 = get_safe_token(device);

	int scanline = cdp1864->screen->vpos();

	switch (scanline)
	{
	case CDP1864_SCANLINE_EFX_TOP_START:
		devcb_call_write_line(&cdp1864->out_efx_func, ASSERT_LINE);
		timer_adjust_oneshot(cdp1864->efx_timer, cdp1864->screen->time_until_pos(CDP1864_SCANLINE_EFX_TOP_END), 0);
		break;

	case CDP1864_SCANLINE_EFX_TOP_END:
		devcb_call_write_line(&cdp1864->out_efx_func, CLEAR_LINE);
		timer_adjust_oneshot(cdp1864->efx_timer, cdp1864->screen->time_until_pos(CDP1864_SCANLINE_EFX_BOTTOM_START), 0);
		break;

	case CDP1864_SCANLINE_EFX_BOTTOM_START:
		devcb_call_write_line(&cdp1864->out_efx_func, ASSERT_LINE);
		timer_adjust_oneshot(cdp1864->efx_timer, cdp1864->screen->time_until_pos(CDP1864_SCANLINE_EFX_BOTTOM_END), 0);
		break;

	case CDP1864_SCANLINE_EFX_BOTTOM_END:
		devcb_call_write_line(&cdp1864->out_efx_func, CLEAR_LINE);
		timer_adjust_oneshot(cdp1864->efx_timer, cdp1864->screen->time_until_pos(CDP1864_SCANLINE_EFX_TOP_START), 0);
		break;
	}
}

/*-------------------------------------------------
    TIMER_CALLBACK( cdp1864_dma_tick )
-------------------------------------------------*/

static TIMER_CALLBACK( cdp1864_dma_tick )
{
	running_device *device = (running_device *) ptr;
	cdp1864_t *cdp1864 = get_safe_token(device);

	int scanline = cdp1864->screen->vpos();

	if (cdp1864->dmaout)
	{
		if (cdp1864->disp)
		{
			if (scanline >= CDP1864_SCANLINE_DISPLAY_START && scanline < CDP1864_SCANLINE_DISPLAY_END)
			{
				devcb_call_write_line(&cdp1864->out_dmao_func, CLEAR_LINE);
			}
		}

		timer_adjust_oneshot(cdp1864->dma_timer, machine->firstcpu->cycles_to_attotime(CDP1864_CYCLES_DMA_WAIT), 0);

		cdp1864->dmaout = 0;
	}
	else
	{
		if (cdp1864->disp)
		{
			if (scanline >= CDP1864_SCANLINE_DISPLAY_START && scanline < CDP1864_SCANLINE_DISPLAY_END)
			{
				devcb_call_write_line(&cdp1864->out_dmao_func, ASSERT_LINE);
			}
		}

		timer_adjust_oneshot(cdp1864->dma_timer, machine->firstcpu->cycles_to_attotime(CDP1864_CYCLES_DMA_ACTIVE), 0);

		cdp1864->dmaout = 1;
	}
}

/*-------------------------------------------------
    cdp1864_init_palette - initialize palette
-------------------------------------------------*/

static void cdp1864_init_palette(running_device *device, const cdp1864_interface *intf)
{
	int i;

	double res_total = intf->res_r + intf->res_g + intf->res_b + intf->res_bkg;

	int weight_r = (intf->res_r / res_total) * 100;
	int weight_g = (intf->res_g / res_total) * 100;
	int weight_b = (intf->res_b / res_total) * 100;
	int weight_bkg = (intf->res_bkg / res_total) * 100;

	for (i = 0; i < 16; i++)
	{
		int r, g, b, luma = 0;

		luma += (i & 4) ? weight_r : 0;
		luma += (i & 1) ? weight_g : 0;
		luma += (i & 2) ? weight_b : 0;
		luma += (i & 8) ? 0 : weight_bkg;

		luma = (luma * 0xff) / 100;

		r = (i & 4) ? luma : 0;
		g = (i & 1) ? luma : 0;
		b = (i & 2) ? luma : 0;

		palette_set_color_rgb( device->machine, i, r, g, b );
	}
}

/*-------------------------------------------------
    cdp1864_aoe_w - audio output enable
-------------------------------------------------*/

WRITE_LINE_DEVICE_HANDLER( cdp1864_aoe_w )
{
	cdp1864_t *cdp1864 = get_safe_token(device);

	if (!state)
	{
		cdp1864->latch = CDP1864_DEFAULT_LATCH;
	}

	cdp1864->aoe = state;
}

/*-------------------------------------------------
    cdp1864_dispon_r - turn display on
-------------------------------------------------*/

READ8_DEVICE_HANDLER( cdp1864_dispon_r )
{
	cdp1864_t *cdp1864 = get_safe_token(device);

	cdp1864->disp = 1;

	return 0xff;
}

/*-------------------------------------------------
    cdp1864_dispoff_r - turn display off
-------------------------------------------------*/

READ8_DEVICE_HANDLER( cdp1864_dispoff_r )
{
	cdp1864_t *cdp1864 = get_safe_token(device);

	cdp1864->disp = 0;

	devcb_call_write_line(&cdp1864->out_int_func, CLEAR_LINE);
	devcb_call_write_line(&cdp1864->out_dmao_func, CLEAR_LINE);

	return 0xff;
}

/*-------------------------------------------------
    cdp1864_step_bgcolor_w - step background color
-------------------------------------------------*/

WRITE8_DEVICE_HANDLER( cdp1864_step_bgcolor_w )
{
	cdp1864_t *cdp1864 = get_safe_token(device);

	cdp1864->disp = 1;

	if (++cdp1864->bgcolor > 3) cdp1864->bgcolor = 0;
}

/*-------------------------------------------------
    cdp1864_con_w - color on write
-------------------------------------------------*/

WRITE_LINE_DEVICE_HANDLER( cdp1864_con_w )
{
	cdp1864_t *cdp1864 = get_safe_token(device);

	if (!state)
	{
		cdp1864->con = 0;
	}
}

/*-------------------------------------------------
    cdp1864_tone_latch_w - load tone latch
-------------------------------------------------*/

WRITE8_DEVICE_HANDLER( cdp1864_tone_latch_w )
{
	cdp1864_t *cdp1864 = get_safe_token(device);

	cdp1864->latch = data;
}

/*-------------------------------------------------
    cdp1864_dma_w - write DMA byte
-------------------------------------------------*/

WRITE8_DEVICE_HANDLER( cdp1864_dma_w )
{
	cdp1864_t *cdp1864 = get_safe_token(device);

	int rdata = 1, bdata = 1, gdata = 1;
	int sx = cdp1864->screen->hpos() + 4;
	int y = cdp1864->screen->vpos();
	int x;

	if (!cdp1864->con)
	{
		rdata = devcb_call_read_line(&cdp1864->in_rdata_func);
		bdata = devcb_call_read_line(&cdp1864->in_bdata_func);
		gdata = devcb_call_read_line(&cdp1864->in_gdata_func);
	}

	for (x = 0; x < 8; x++)
	{
		int color = CDP1864_BACKGROUND_COLOR_SEQUENCE[cdp1864->bgcolor] + 8;

		if (BIT(data, 7))
		{
			color = (gdata << 2) | (bdata << 1) | rdata;
		}

		*BITMAP_ADDR16(cdp1864->bitmap, y, sx + x) = color;

		data <<= 1;
	}
}

/*-------------------------------------------------
    STREAM_UPDATE( cdp1864_stream_update )
-------------------------------------------------*/

static STREAM_UPDATE( cdp1864_stream_update )
{
	cdp1864_t *cdp1864 = get_safe_token(device);

	INT16 signal = cdp1864->signal;
	stream_sample_t *buffer = outputs[0];

	memset( buffer, 0, samples * sizeof(*buffer) );

	if (cdp1864->aoe)
	{
		double frequency = cdp1864->cpu->unscaled_clock() / 8 / 4 / (cdp1864->latch + 1) / 2;
		int rate = device->machine->sample_rate / 2;

		/* get progress through wave */
		int incr = cdp1864->incr;

		if (signal < 0)
		{
			signal = -0x7fff;
		}
		else
		{
			signal = 0x7fff;
		}

		while( samples-- > 0 )
		{
			*buffer++ = signal;
			incr -= frequency;
			while( incr < 0 )
			{
				incr += rate;
				signal = -signal;
			}
		}

		/* store progress through wave */
		cdp1864->incr = incr;
		cdp1864->signal = signal;
	}
}

/*-------------------------------------------------
    cdp1864_update - update screen
-------------------------------------------------*/

void cdp1864_update(running_device *device, bitmap_t *bitmap, const rectangle *cliprect)
{
	cdp1864_t *cdp1864 = get_safe_token(device);

	if (cdp1864->disp)
	{
		copybitmap(bitmap, cdp1864->bitmap, 0, 0, 0, 0, cliprect);
		bitmap_fill(cdp1864->bitmap, cliprect, CDP1864_BACKGROUND_COLOR_SEQUENCE[cdp1864->bgcolor] + 8);
	}
	else
	{
		bitmap_fill(bitmap, cliprect, get_black_pen(device->machine));
	}
}

/*-------------------------------------------------
    DEVICE_START( cdp1864 )
-------------------------------------------------*/

static DEVICE_START( cdp1864 )
{
	cdp1864_t *cdp1864 = get_safe_token(device);
	const cdp1864_interface *intf = (const cdp1864_interface *) device->baseconfig().static_config();

	/* resolve callbacks */
	devcb_resolve_read_line(&cdp1864->in_rdata_func, &intf->in_rdata_func, device);
	devcb_resolve_read_line(&cdp1864->in_bdata_func, &intf->in_bdata_func, device);
	devcb_resolve_read_line(&cdp1864->in_gdata_func, &intf->in_gdata_func, device);
	devcb_resolve_write_line(&cdp1864->out_int_func, &intf->out_int_func, device);
	devcb_resolve_write_line(&cdp1864->out_dmao_func, &intf->out_dmao_func, device);
	devcb_resolve_write_line(&cdp1864->out_efx_func, &intf->out_efx_func, device);

	/* get the cpu */
	cdp1864->cpu = device->machine->device(intf->cpu_tag);

	/* get the screen device */
	cdp1864->screen = downcast<screen_device *>(device->machine->device(intf->screen_tag));
	assert(cdp1864->screen != NULL);

	/* allocate the temporary bitmap */
	cdp1864->bitmap = auto_bitmap_alloc(device->machine, cdp1864->screen->width(), cdp1864->screen->height(), cdp1864->screen->format());
	bitmap_fill(cdp1864->bitmap, 0, CDP1864_BACKGROUND_COLOR_SEQUENCE[cdp1864->bgcolor] + 8);

	/* initialize the palette */
	cdp1864_init_palette(device, intf);

	/* create sound stream */
	cdp1864->stream = stream_create(device, 0, 1, device->machine->sample_rate, cdp1864, cdp1864_stream_update);

	/* create the timers */
	cdp1864->int_timer = timer_alloc(device->machine, cdp1864_int_tick, (void *)device);
	cdp1864->efx_timer = timer_alloc(device->machine, cdp1864_efx_tick, (void *)device);
	cdp1864->dma_timer = timer_alloc(device->machine, cdp1864_dma_tick, (void *)device);

	/* register for state saving */
	state_save_register_device_item(device, 0, cdp1864->disp);
	state_save_register_device_item(device, 0, cdp1864->dmaout);
	state_save_register_device_item(device, 0, cdp1864->bgcolor);
	state_save_register_device_item(device, 0, cdp1864->con);

	state_save_register_device_item(device, 0, cdp1864->aoe);
	state_save_register_device_item(device, 0, cdp1864->latch);
	state_save_register_device_item(device, 0, cdp1864->signal);
	state_save_register_device_item(device, 0, cdp1864->incr);

	state_save_register_device_item_bitmap(device, 0, cdp1864->bitmap);
}

/*-------------------------------------------------
    DEVICE_RESET( cdp1864 )
-------------------------------------------------*/

static DEVICE_RESET( cdp1864 )
{
	cdp1864_t *cdp1864 = get_safe_token(device);

	timer_adjust_oneshot(cdp1864->int_timer, cdp1864->screen->time_until_pos(CDP1864_SCANLINE_INT_START), 0);
	timer_adjust_oneshot(cdp1864->efx_timer, cdp1864->screen->time_until_pos(CDP1864_SCANLINE_EFX_TOP_START), 0);
	timer_adjust_oneshot(cdp1864->dma_timer, device->machine->firstcpu->cycles_to_attotime(CDP1864_CYCLES_DMA_START), 0);

	cdp1864->disp = 0;
	cdp1864->dmaout = 0;
	cdp1864->bgcolor = 0;
	cdp1864->con = 1;

	devcb_call_write_line(&cdp1864->out_int_func, CLEAR_LINE);
	devcb_call_write_line(&cdp1864->out_dmao_func, CLEAR_LINE);
	devcb_call_write_line(&cdp1864->out_efx_func, CLEAR_LINE);

	cdp1864_aoe_w(device, 0);
}

/*-------------------------------------------------
    DEVICE_GET_INFO( cdp1864 )
-------------------------------------------------*/

DEVICE_GET_INFO( cdp1864 )
{
	switch (state)
	{
		/* --- the following bits of info are returned as 64-bit signed integers --- */
		case DEVINFO_INT_TOKEN_BYTES:					info->i = sizeof(cdp1864_t);						break;
		case DEVINFO_INT_INLINE_CONFIG_BYTES:			info->i = 0;										break;

		/* --- the following bits of info are returned as pointers to data or functions --- */
		case DEVINFO_FCT_START:							info->start = DEVICE_START_NAME(cdp1864);			break;
		case DEVINFO_FCT_STOP:							/* Nothing */										break;
		case DEVINFO_FCT_RESET:							info->reset = DEVICE_RESET_NAME(cdp1864);			break;

		/* --- the following bits of info are returned as NULL-terminated strings --- */
		case DEVINFO_STR_NAME:							strcpy(info->s, "RCA CDP1864");						break;
		case DEVINFO_STR_FAMILY:						strcpy(info->s, "RCA CDP1800");						break;
		case DEVINFO_STR_VERSION:						strcpy(info->s, "1.0");								break;
		case DEVINFO_STR_SOURCE_FILE:					strcpy(info->s, __FILE__);							break;
		case DEVINFO_STR_CREDITS:						strcpy(info->s, "Copyright MESS Team");				break;
	}
}


DEFINE_LEGACY_SOUND_DEVICE(CDP1864, cdp1864);