summaryrefslogtreecommitdiffstatshomepage
path: root/src/emu/machine/mos6551.c
blob: a20e1aebc9b75127519d502f79decc13ba2e40ae (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
/**********************************************************************

    MOS Technology 6551 Asynchronous Communication Interface Adapter

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

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

/*

    TODO:

    - receiver disable
    - IRQ on DCD/DSR change
    - parity
    - framing error

*/

#include "mos6551.h"



//**************************************************************************
//  MACROS / CONSTANTS
//**************************************************************************

#define LOG 0


const int mos6551_device::brg_divider[] = {
	0, 2304, 1536, 1048, 856, 768, 384, 192, 96, 64, 48, 32, 24, 16, 12, 6
};



//**************************************************************************
//  DEVICE DEFINITIONS
//**************************************************************************

// device type definition
const device_type MOS6551 = &device_creator<mos6551_device>;



//**************************************************************************
//  LIVE DEVICE
//**************************************************************************

//-------------------------------------------------
//  mos6551_device - constructor
//-------------------------------------------------

mos6551_device::mos6551_device(const machine_config &mconfig, const char *tag, device_t *owner, UINT32 clock)
	: device_t(mconfig, MOS6551, "MOS6551", tag, owner, clock),
		device_serial_interface(mconfig, *this),
		m_irq_handler(*this),
		m_ctrl(0),
		m_cmd(CMD_RIE),
		m_st(ST_TDRE),
		m_ext_rxc(0)
{
}


//-------------------------------------------------
//  device_start - device-specific startup
//-------------------------------------------------

void mos6551_device::device_start()
{
	m_irq_handler.resolve_safe();

	transmit_register_reset();
	receive_register_reset();
}


//-------------------------------------------------
//  device_reset - device-specific reset
//-------------------------------------------------

void mos6551_device::device_reset()
{
	m_ctrl = 0;
	m_cmd = CMD_RIE;

	update_serial();
}


//-------------------------------------------------
//  tra_complete -
//-------------------------------------------------

void mos6551_device::tra_complete()
{
	if (!(m_st & ST_TDRE))
	{
		transmit_register_setup(m_tdr);
		m_st |= ST_TDRE;

		if ((m_cmd & CMD_TC_MASK) == CMD_TC_TIE_RTS_LO)
		{
			m_st |= ST_IRQ;
			m_irq_handler(ASSERT_LINE);
		}
	}
}


//-------------------------------------------------
//  rcv_complete -
//-------------------------------------------------

void mos6551_device::rcv_complete()
{
	if (m_st & ST_RDRF)
	{
		m_st |= ST_OR;
	}

	m_st &= ~(ST_FE | ST_PE);

	m_st |= ST_RDRF;

	if (!(m_cmd & CMD_RIE))
	{
		m_st |= ST_IRQ;
		m_irq_handler(ASSERT_LINE);
	}
}


//-------------------------------------------------
//  input_callback -
//-------------------------------------------------

void mos6551_device::input_callback(UINT8 state)
{
	m_input_state = state;
}


//-------------------------------------------------
//  update_serial -
//-------------------------------------------------

void mos6551_device::update_serial()
{
	int brg = m_ctrl & CTRL_BRG_MASK;

	if (brg == CTRL_BRG_16X_EXTCLK)
	{
		set_rcv_rate(m_ext_rxc / 16);
		set_tra_rate(m_ext_rxc / 16);
	}
	else
	{
		int baud = clock() / brg_divider[brg] / 16;

		set_tra_rate(baud);

		if (m_ctrl & CTRL_RXC_BRG)
		{
			set_rcv_rate(baud);
		}
		else
		{
			set_rcv_rate(m_ext_rxc / 16);
		}

		int num_data_bits = 8;
		int stop_bit_count = 1;
		int parity_code = SERIAL_PARITY_NONE;

		switch (m_ctrl & CTRL_WL_MASK)
		{
		case CTRL_WL_8: num_data_bits = 8; break;
		case CTRL_WL_7: num_data_bits = 7; break;
		case CTRL_WL_6: num_data_bits = 6; break;
		case CTRL_WL_5: num_data_bits = 5; break;
		}

		set_data_frame(num_data_bits, stop_bit_count, parity_code);
	}

	if (m_cmd & CMD_DTR)
		m_connection_state |= SERIAL_STATE_DTR;
	else
		m_connection_state &= ~SERIAL_STATE_DTR;

	if ((m_cmd & CMD_TC_MASK) == CMD_TC_RTS_HI)
		m_connection_state &= ~SERIAL_STATE_RTS;
	else
		m_connection_state |= SERIAL_STATE_RTS;

	serial_connection_out();
}


//-------------------------------------------------
//  read -
//-------------------------------------------------

READ8_MEMBER( mos6551_device::read )
{
	UINT8 data = 0;

	switch (offset & 0x03)
	{
	case 0:
		if (is_receive_register_full())
		{
			receive_register_extract();
			data = get_received_char();
		}

		m_st &= ~(ST_RDRF | ST_OR | ST_FE | ST_PE);
		break;

	case 1:
		data = m_st;
		m_st &= ~ST_IRQ;
		m_irq_handler(CLEAR_LINE);
		break;

	case 2:
		data = m_cmd;
		break;

	case 3:
		data = m_ctrl;
		break;
	}

	return data;
}


//-------------------------------------------------
//  write -
//-------------------------------------------------

WRITE8_MEMBER( mos6551_device::write )
{
	switch (offset & 0x03)
	{
	case 0:
		m_tdr = data;
		m_st &= ~ST_TDRE;

		if (is_transmit_register_empty())
		{
			transmit_register_setup(m_tdr);
			m_st |= ST_TDRE;

			if ((m_cmd & CMD_TC_MASK) == CMD_TC_TIE_RTS_LO)
			{
				m_st |= ST_IRQ;
				m_irq_handler(ASSERT_LINE);
			}
		}
		break;

	case 1:
		// programmed reset
		m_cmd = (m_cmd & 0xe0) | CMD_RIE;
		m_st &= ~ST_OR;
		update_serial();
		break;

	case 2:
		m_cmd = data;
		update_serial();
		break;

	case 3:
		m_ctrl = data;
		update_serial();
		break;
	}
}


//-------------------------------------------------
//  set_rxc - set external receiver clock
//-------------------------------------------------

void mos6551_device::set_rxc(int clock)
{
	m_ext_rxc = clock;

	update_serial();
}