summaryrefslogtreecommitdiffstatshomepage
path: root/src/devices/machine/ym3802.cpp
blob: 39008a2ea15ff0bc53e43acf7b7355ecccbd4129 (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
// license:BSD-3-Clause
// copyright-holders:Barry Rodewald
/*
 * ym3802.c - Yamaha MCS MIDI Communication and Service Controller
 *
 * TODO:
 *   - Receive serial data
 *   - Transmit Idle detection
 *   - IRx/ITx (used for MIDI system messages)
 *   - FSK modulation
 *   - Timers (MIDI clock timer and Click counter are working but not guaranteed to be perfectly accurate)
 *   - Interrupts (except for Tx Buffer Empty, MIDI clock detect, Click Counter)
 */

#include "emu.h"
#include "ym3802.h"


DEFINE_DEVICE_TYPE(YM3802, ym3802_device, "ym3802", "Yamaha YM3802 MCS MIDI Communication and Service Controller")

ym3802_device::ym3802_device(const machine_config &mconfig, const char *tag, device_t *owner, uint32_t clock)
	: device_t(mconfig, YM3802, tag, owner, clock)
	, device_serial_interface(mconfig, *this)
	, m_irq_handler(*this)
	, m_txd_handler(*this)
	, m_rxd_handler(*this, 0xff)
	, m_reg(REG_MAX)
	, m_wdr(0)
	, m_irq_status(0)
	, m_vector(0)
	, m_clkm_rate(500000)  // TODO: make these configurable
	, m_clkf_rate(614400)
{
}

void ym3802_device::device_start()
{
	m_midi_timer = timer_alloc(FUNC(ym3802_device::transmit_clk), this);
	m_midi_counter_timer = timer_alloc(FUNC(ym3802_device::midi_clk), this);
	save_item(NAME(m_reg));
}

void ym3802_device::device_reset()
{
	m_reg.clear();
	reset_irq(0xff);
	transmit_register_reset();
	receive_register_reset();
	reset_midi_timer();
	set_comms_mode();
}

void ym3802_device::set_irq(uint8_t irq)
{
	uint8_t x;

	m_irq_status |= (irq & m_reg[REG_IER]);
	for(x=0;x<8;x++)
	{
		if(m_irq_status & (1 << x))
			break;
	}
	m_vector = (m_reg[REG_IOR] & 0xe0) | (x << 1);
	if(m_irq_status != 0)
		m_irq_handler(ASSERT_LINE);
}

void ym3802_device::reset_irq(uint8_t irq)
{
	m_irq_status &= ~irq;
	if(m_irq_status == 0)
		m_irq_handler(CLEAR_LINE);
}

TIMER_CALLBACK_MEMBER(ym3802_device::transmit_clk)
{
	if(m_reg[REG_TCR] & 0x01) // Tx Enable
	{
		if(!m_tx_fifo.empty())
		{
			if (is_transmit_register_empty())
			{
				transmit_register_setup(m_tx_fifo.front());  // start to send first byte in FIFO
				m_tx_fifo.pop();                                // and remove it from the FIFO
				if(m_tx_fifo.empty())
					set_irq(IRQ_FIFOTX_EMPTY);
			}
		}
		/* if diserial has bits to send, make them so */
		if (!is_transmit_register_empty())
		{
			uint8_t data = transmit_register_get_data_bit();
			m_tx_busy = true;
			m_txd_handler(data);
		}
		if (m_tx_fifo.empty() && is_transmit_register_empty())
			m_tx_busy = false;
	}
}

TIMER_CALLBACK_MEMBER(ym3802_device::midi_clk)
{
	if(m_midi_counter_base > 1)  // counter is not guaranteed to work if set to 0 or 1.
	{
		if(m_midi_counter == 0)
		{
			m_midi_counter = m_midi_counter_base;  // reload timer
			if(m_reg[REG_IMR] & 0x08)  // if IRQ1 is set to MIDI clock detect
				set_irq(IRQ_MIDI_CLK);
			if(m_click_counter_base != 0)
			{
				m_click_counter--;
				if(m_click_counter == 0)
				{
					m_click_counter = m_click_counter_base;
					if(!(m_reg[REG_IMR] & 0x08))  // if IRQ1 is set to click counter
						set_irq(IRQ_CLICK);
				}
			}
		}
		else
			m_midi_counter--;
	}
}

void ym3802_device::reset_midi_timer()
{
	uint32_t rate;
	uint8_t divisor = m_reg[REG_TRR] & 0x1f;

	if(!(divisor & 0x10))
	{
		if(divisor & 0x08)
			rate = m_clkm_rate / 32;
		else
			rate = m_clkm_rate / 16;
	}
	else
	{
		if(!(divisor & 0x08))
			rate = m_clkf_rate / 32;
		else
			rate = m_clkf_rate / (64 << (divisor & 0x07));
	}

	if(rate != m_prev_rate)
		m_midi_timer->adjust(attotime::from_hz(rate),0,attotime::from_hz(rate));
	m_prev_rate = rate;
	logerror("MIDI Timer rate set to %iHz\n",rate);
}

void ym3802_device::set_comms_mode()
{
	uint8_t data_bits = (m_reg[REG_TMR] & 0x20) ? 7 : 8;
	parity_t parity;
	stop_bits_t stop_bits = (m_reg[REG_TMR] & 0x02) ? STOP_BITS_2 : STOP_BITS_1;

	if(!(m_reg[REG_TMR] & 0x10))  // parity enable
		parity = PARITY_NONE;
	else
	{
		if(m_reg[REG_TMR] & 0x04)
			parity = PARITY_ODD;
		else
			parity = PARITY_EVEN;
		// TODO: 4-bit parity
	}

	set_data_frame(1, data_bits, parity, stop_bits);
	logerror("MIDI comms set to 1 start bit, %i data bits, %s, parity = %i\n",data_bits, (stop_bits == STOP_BITS_2) ? "2 stop bits" : "1 stop bit", parity);
}

uint8_t ym3802_device::read(offs_t offset)
{
	if(offset < 4)
	{
		if(offset == 3)
			return m_wdr;
		if(offset == 2)
			return m_irq_status;
		if(offset == 0)
			return m_vector;
		return m_reg[offset];
	}
	else
	{
		uint8_t bank = m_reg[REG_RGR] & 0x0f;
		uint8_t ret = 0;

		if(bank > 9)
			return m_wdr;

		switch(offset + (bank * 10))
		{
			case REG_TSR:
				if(m_tx_fifo.empty())
					ret |= 0x80;
				if(m_tx_fifo.size() < 16)
					ret |= 0x40;
				if(m_tx_busy)
					ret |= 0x01;
				break;
			default:
				ret = m_reg[offset + (bank * 10)];
		}
		return ret;
	}
}

void ym3802_device::write(offs_t offset, uint8_t data)
{
	m_wdr = data;
	if(offset == 1)
	{
		m_reg[REG_RGR] = data & 0x0f;
		if(data & 0x80)
			device_reset();
		logerror("MIDI: writing %02x to reg %i\n",data,offset);
	}
	if(offset == 3)
		reset_irq(data);
	if(offset > 4)
	{
		uint8_t bank = m_reg[REG_RGR] & 0x0f;

		if(bank > 9)
			return;

		m_reg[offset + (bank * 10)] = data;
		logerror("MIDI: writing %02x to reg %i\n",data,offset + (bank * 10));

		switch(offset + (bank * 10))
		{
			case REG_IOR:
				logerror("IOR vector write %02\n",data);
				break;
			case REG_IER:
				logerror("IER set to %02x\n",data);
				break;
			case REG_DCR:
				if(data & 0x20)
				{
					if((data & 0x07) == 2)
					{
						const double rate = (m_reg[REG_CCR] & 0x02) ? m_clkm_rate / 4 : m_clkm_rate / 8;

						// start message to click counter
						m_midi_counter_timer->adjust(attotime::from_hz(rate),0,attotime::from_hz(rate));
					}
					if((data & 0x07) == 3)
					{
						// stop message to click counter
						m_midi_counter_timer->adjust(attotime::zero,0,attotime::never);
					}
				}
				break;
			case REG_TMR:
				set_comms_mode();
				break;
			case REG_TCR:
				if(data & 0x01)
					reset_midi_timer();
				break;
			case REG_TDR:
				m_tx_fifo.push(data);
				reset_irq(IRQ_FIFOTX_EMPTY);
				break;
			case REG_GTR_LOW:
				m_general_counter = (m_general_counter & 0xff00) | data;
				//popmessage("General counter set to %i\n",m_general_counter);
				break;
			case REG_GTR_HIGH:
				m_general_counter = (m_general_counter & 0x00ff) | ((data & 0x3f) << 8);
				//popmessage("General counter set to %i\n",m_general_counter);
				break;
			case REG_MTR_LOW:
				m_midi_counter_base = (m_midi_counter & 0xff00) | data;
				m_midi_counter = m_midi_counter_base;
				//popmessage("MIDI counter set to %i\n",m_midi_counter);
				break;
			case REG_MTR_HIGH:
				m_midi_counter_base = (m_midi_counter & 0x00ff) | ((data & 0x3f) << 8);
				m_midi_counter = m_midi_counter_base;
				//popmessage("MIDI counter set to %i\n",m_midi_counter);
				break;
			case REG_CDR:
				m_click_counter_base = data & 0x7f;
				m_click_counter = m_click_counter_base;
				break;
		}
	}
}