summaryrefslogtreecommitdiffstatshomepage
path: root/src/mame/audio/cchasm.c
blob: 5d1ea24f94f5b9cf75c95bec5f53c99a3867fccf (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
/***************************************************************************

    Cinematronics Cosmic Chasm hardware

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

#include "emu.h"
#include "streams.h"
#include "cpu/z80/z80.h"
#include "machine/z80ctc.h"
#include "includes/cchasm.h"
#include "sound/ay8910.h"
#include "sound/dac.h"

static int sound_flags;
static int coin_flag;
static const device_config *ctc;

WRITE8_HANDLER( cchasm_reset_coin_flag_w )
{
	if (coin_flag)
	{
		coin_flag = 0;
		z80ctc_trg0_w(ctc, coin_flag);
	}
}

INPUT_CHANGED( cchasm_set_coin_flag )
{
	if (!newval && !coin_flag)
	{
		coin_flag = 1;
		z80ctc_trg0_w(ctc, coin_flag);
	}
}

READ8_HANDLER( cchasm_coin_sound_r )
{
	UINT8 coin = (input_port_read(space->machine, "IN3") >> 4) & 0x7;
	return sound_flags | (coin_flag << 3) | coin;
}

READ8_HANDLER( cchasm_soundlatch2_r )
{
	sound_flags &= ~0x80;
	z80ctc_trg2_w(ctc, 0);
	return soundlatch2_r(space, offset);
}

WRITE8_HANDLER( cchasm_soundlatch4_w )
{
	sound_flags |= 0x40;
	soundlatch4_w(space, offset, data);
	cputag_set_input_line(space->machine, "maincpu", 1, HOLD_LINE);
}

WRITE16_HANDLER( cchasm_io_w )
{
    static int led;

	if (ACCESSING_BITS_8_15)
	{
		data >>= 8;
		switch (offset & 0xf)
		{
		case 0:
			soundlatch_w (space, offset, data);
			break;
		case 1:
			sound_flags |= 0x80;
			soundlatch2_w (space, offset, data);
			z80ctc_trg2_w(ctc, 1);
			cputag_set_input_line(space->machine, "audiocpu", INPUT_LINE_NMI, PULSE_LINE);
			break;
		case 2:
			led = data;
			break;
		}
	}
}

READ16_HANDLER( cchasm_io_r )
{
	switch (offset & 0xf)
	{
	case 0x0:
		return soundlatch3_r (space, offset) << 8;
	case 0x1:
		sound_flags &= ~0x40;
		return soundlatch4_r (space,offset) << 8;
	case 0x2:
		return (sound_flags| (input_port_read(space->machine, "IN3") & 0x07) | 0x08) << 8;
	case 0x5:
		return input_port_read(space->machine, "IN2") << 8;
	case 0x8:
		return input_port_read(space->machine, "IN1") << 8;
	default:
		return 0xff << 8;
	}
}

static int channel_active[2];
static int output[2];

static WRITE_LINE_DEVICE_HANDLER( ctc_timer_1_w )
{
	if (state) /* rising edge */
	{
		output[0] ^= 0x7f;
		channel_active[0] = 1;
		dac_data_w(devtag_get_device(device->machine, "dac1"), output[0]);
	}
}

static WRITE_LINE_DEVICE_HANDLER( ctc_timer_2_w )
{
	if (state) /* rising edge */
	{
		output[1] ^= 0x7f;
		channel_active[1] = 1;
		dac_data_w(devtag_get_device(device->machine, "dac2"), output[0]);
	}
}

Z80CTC_INTERFACE( cchasm_ctc_intf )
{
	0,               /* timer disables */
	DEVCB_CPU_INPUT_LINE("audiocpu", INPUT_LINE_IRQ0),   /* interrupt handler */
	DEVCB_NULL,					/* ZC/TO0 callback */
	DEVCB_LINE(ctc_timer_1_w),	/* ZC/TO1 callback */
	DEVCB_LINE(ctc_timer_2_w)	/* ZC/TO2 callback */
};

SOUND_START( cchasm )
{
	coin_flag = 0;
    sound_flags = 0;
    output[0] = 0; output[1] = 0;

	ctc = devtag_get_device(machine, "ctc");
}