summaryrefslogtreecommitdiffstatshomepage
path: root/src/emu/machine/pc16552d.c
blob: 1d5857f502bf35417e5280ae23bb7288be53d7e9 (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
/*
    National Semiconductor PC16552D
    Dual Universal Asynchronous Receiver/Transmitter with FIFOs

    Written by Ville Linde
*/

#include "emu.h"
#include "pc16552d.h"

#define REG_RECV_BUFFER         0x0     // Read
#define REG_XMIT_HOLD           0x0     // Write
#define REG_INT_ENABLE          0x1
#define REG_FIFO_CTRL           0x2     // Write
#define REG_LINE_CTRL           0x3
#define REG_MODEL_CTRL          0x4
#define REG_LINE_STATUS         0x5
#define REG_MODEM_STATUS        0x6
#define REG_SCRATCH             0x7
#define REG_DIV_LATCH_LSB       0x0     // When DLAB == 1
#define REG_DIV_LATCH_MSB       0x1     // When DLAB == 1
#define REG_ALT_FUNCTION        0x2     // When DLAB == 1

#define LINE_CTRL_DLAB          0x80

#define IRQ_RX_LINE_STATUS          0x1
#define IRQ_RX_DATA_AVAILABLE       0x2
#define IRQ_CHARACTER_TIMEOUT       0x4
#define IRQ_TX_HOLDING_REG_EMPTY    0x8
#define IRQ_MODEM_STATUS            0x10

#define INT_ENABLE_RX_DATA          0x01
#define INT_ENABLE_TX_EMPTY         0x02
#define INT_ENABLE_RX_LINE_STATUS   0x04
#define INT_ENABLE_MODEM_STATUS     0x08

struct PC16552D_CHANNEL
{
	UINT16 divisor;
	UINT8 reg[8];
	UINT8 rx_fifo[16];
	UINT8 tx_fifo[16];
	int pending_interrupt;
	int rx_fifo_read_ptr;
	int rx_fifo_write_ptr;
	int rx_fifo_num;
	int tx_fifo_read_ptr;
	int tx_fifo_write_ptr;
	int tx_fifo_num;
	emu_timer *tx_fifo_timer;
};

struct PC16552D_REGS
{
	PC16552D_CHANNEL ch[2];
	int frequency;
	void (* irq_handler)(running_machine &machine, int channel, int value);
	void (* tx_callback)(running_machine &machine, int channel, int count, UINT8* data);
};

#define MAX_PC16552D_CHIPS      4

static PC16552D_REGS duart[MAX_PC16552D_CHIPS];



static const int rx_trigger_level[4] = { 1, 4, 8, 14 };


static void check_interrupts(running_machine &machine, int chip, int channel)
{
	PC16552D_CHANNEL *ch = &duart[chip].ch[channel];
	int signal = 0;

	if (ch->pending_interrupt != 0)
	{
		if (((ch->reg[REG_INT_ENABLE] & INT_ENABLE_RX_DATA) && (ch->pending_interrupt & IRQ_RX_DATA_AVAILABLE)) ||
			((ch->reg[REG_INT_ENABLE] & INT_ENABLE_TX_EMPTY) && (ch->pending_interrupt & IRQ_TX_HOLDING_REG_EMPTY)) ||
			((ch->reg[REG_INT_ENABLE] & INT_ENABLE_RX_LINE_STATUS) && (ch->pending_interrupt & IRQ_RX_LINE_STATUS)) ||
			((ch->reg[REG_INT_ENABLE] & INT_ENABLE_MODEM_STATUS) && (ch->pending_interrupt & IRQ_MODEM_STATUS)))
		{
			signal = 1;
		}
	}

	if (duart[chip].irq_handler != NULL)
	{
		duart[chip].irq_handler(machine, channel, signal ? ASSERT_LINE : CLEAR_LINE);
	}
}

static void duart_push_rx_fifo(running_machine &machine, int chip, int channel, UINT8 data)
{
	PC16552D_CHANNEL *ch = &duart[chip].ch[channel];

	if (ch->rx_fifo_num >= 16)
	{
		printf("duart_push_rx_fifo: %d, %d, %02X, FIFO overflow\n", chip, channel, data);
		return;
	}

	ch->rx_fifo[ch->rx_fifo_write_ptr++] = data;
	if (ch->rx_fifo_write_ptr == 16)
	{
		ch->rx_fifo_write_ptr = 0;
	}
	ch->rx_fifo_num++;

	if (ch->rx_fifo_num == rx_trigger_level[(ch->reg[REG_FIFO_CTRL] >> 6) & 3])
	{
		ch->pending_interrupt |= IRQ_RX_DATA_AVAILABLE;     // INT ID: received data available

		check_interrupts(machine, chip, channel);
	}
}

static UINT8 duart_pop_rx_fifo(running_machine &machine, int chip, int channel)
{
	UINT8 r;
	PC16552D_CHANNEL *ch = &duart[chip].ch[channel];

	if (ch->rx_fifo_num == 0)
	{
		printf("duart_pop_rx_fifo: %d, %d, FIFO underflow\n", chip, channel);
		return 0;
	}

	r = ch->rx_fifo[ch->rx_fifo_read_ptr++];
	if (ch->rx_fifo_read_ptr == 16)
	{
		ch->rx_fifo_read_ptr = 0;
	}
	ch->rx_fifo_num--;

	if (ch->rx_fifo_num < rx_trigger_level[(ch->reg[REG_FIFO_CTRL] >> 6) & 3])
	{
		ch->pending_interrupt &= ~IRQ_RX_DATA_AVAILABLE;

		check_interrupts(machine, chip, channel);
	}

	return r;
}

static TIMER_CALLBACK( tx_fifo_timer_callback )
{
	PC16552D_CHANNEL *ch;
	int chip = param >> 1;
	int channel = param & 1;

	ch = &duart[chip].ch[channel];

	if (duart[chip].tx_callback)
		duart[chip].tx_callback(machine, channel, ch->tx_fifo_num, ch->tx_fifo);

	ch->tx_fifo_num = 0;

	// set transmitter empty interrupt
	ch->pending_interrupt |= IRQ_TX_HOLDING_REG_EMPTY;
	check_interrupts(machine, chip, channel);

	duart[chip].ch[channel].tx_fifo_timer->adjust(attotime::never, (chip * 2) + channel);
}

static void duart_push_tx_fifo(int chip, int channel, UINT8 data)
{
	attotime period;
	PC16552D_CHANNEL *ch = &duart[chip].ch[channel];

	ch->tx_fifo[ch->tx_fifo_num] = data;
	ch->tx_fifo_num++;

	period = attotime::from_hz(duart[chip].frequency) * (ch->divisor * 16 * 16 * 8);

	duart[chip].ch[channel].tx_fifo_timer->adjust(period, (chip * 2) + channel);
}

#ifdef UNUSED_FUNCTION
static UINT8 duart_pop_tx_fifo(int chip, int channel, UINT8 data)
{
	return 0;
}
#endif


static UINT8 duart_r(running_machine &machine, int chip, int reg)
{
	int channel = (reg >> 3) & 1;
	PC16552D_CHANNEL *ch = &duart[chip].ch[channel];
	reg &= 7;

//  printf("duart_r: chip %d, ch %d, reg %d\n", chip, channel, reg);

	switch (reg)
	{
		case 0:
		{
			if (ch->reg[REG_LINE_CTRL] & LINE_CTRL_DLAB)
			{
				// Divisor Latch (LSB)
				return ch->divisor & 0xff;
			}
			else
			{
				// Receiver Buffer
				ch->pending_interrupt &= ~IRQ_RX_DATA_AVAILABLE;

				check_interrupts(machine, chip, channel);

				return duart_pop_rx_fifo(machine, chip, channel);
			}
		}
		case 1:
		{
			if (ch->reg[REG_LINE_CTRL] & LINE_CTRL_DLAB)
			{
				// Divisor Latch (MSB)
				return (ch->divisor >> 8) & 0xff;
			}
			else
			{
			}
			break;
		}

		case 2:
		{
			if (ch->reg[REG_LINE_CTRL] & LINE_CTRL_DLAB)
			{
				// Alternate Function
			}
			else
			{
				// Interrupt Identification Register
				int i;
				UINT8 r = 0x01;

				for (i=0; i < 5; i++)
				{
					if (ch->pending_interrupt & (1 << i))
					{
						switch (i)
						{
							case 0: r = 0x06; break;    // Receiver Line Status
							case 1: r = 0x04; break;    // Received Data Available
							case 2: r = 0x0c; break;    // Character Timeout Indication
							case 3: r = 0x02; break;    // Transmitter Holding Register Empty
							case 4: r = 0x00; break;    // MODEM Status
						}
						break;
					}
				}

				if (ch->reg[REG_FIFO_CTRL] & 1)
				{
					r |= 0xc0;
				}

				return r;
			}
			break;
		}

		case 5:     // Line Status Register
		{
			UINT8 r = 0;

			// set Data Ready flag
			if (ch->rx_fifo_num > 0)
			{
				r |= 0x1;
			}

			// set Transmitter Holding Register Empty flag
			if (ch->tx_fifo_num == 0)
			{
				r |= 0x20;
			}

			// set Transmitter Empty flag
			if (ch->tx_fifo_num == 0)
			{
				r |= 0x40;
			}

			return r;
		}
	}

	return ch->reg[reg];
}

static void duart_w(running_machine &machine, int chip, int reg, UINT8 data)
{
	int channel = (reg >> 3) & 1;
	PC16552D_CHANNEL *ch = &duart[chip].ch[channel];
	reg &= 7;

//  printf("duart_w: chip %d, ch %d, reg %d, data %02X\n", chip, channel, reg, data);

	switch (reg)
	{
		case 0:
		{
			if (ch->reg[REG_LINE_CTRL] & LINE_CTRL_DLAB)
			{
				// Divisor Latch (LSB)
				ch->divisor &= 0xff00;
				ch->divisor |= data;

				return;
			}
			else
			{
				// Transmitter Holding Register
				duart_push_tx_fifo(chip, channel, data);

				ch->pending_interrupt &= ~IRQ_TX_HOLDING_REG_EMPTY;
				check_interrupts(machine, chip, channel);

				return;
			}
		}
		case 1:
		{
			if (ch->reg[REG_LINE_CTRL] & LINE_CTRL_DLAB)
			{
				// Divisor Latch (MSB)
				ch->divisor &= 0x00ff;
				ch->divisor |= data << 8;

		//      printf("DUART %d %d bps\n", chip, duart[chip].frequency / (ch->divisor * 16));
				return;
			}
			else
			{
				// Interrupt enable
				ch->reg[REG_INT_ENABLE] = data;

				check_interrupts(machine, chip, channel);
				return;
			}
		}

		case 2:
		{
			if (ch->reg[REG_LINE_CTRL] & LINE_CTRL_DLAB)
			{
				// Alternate Function

				return;
			}
			else
			{
				// FIFO control
				if (data & 0x02)
				{
					ch->rx_fifo_write_ptr = 0;
					ch->rx_fifo_read_ptr = 0;
					ch->rx_fifo_num = 0;
				}
				if (data & 0x04)
				{
					ch->tx_fifo_write_ptr = 0;
					ch->tx_fifo_read_ptr = 0;
					ch->tx_fifo_num = 0;


				}

				/*if (data & 0x1 && (ch->reg[reg] & 0x1) == 0)
				{
				    // cause transmitter empty IRQ
				    ch->pending_interrupt |= IRQ_TX_HOLDING_REG_EMPTY;

				    check_interrupts(machine, chip, channel);
				}
				*/
			}
			break;
		}
	}

	ch->reg[reg] = data;
}

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

void pc16552d_init(running_machine &machine, int chip, int frequency, void (* irq_handler)(running_machine &machine, int channel, int value), void (* tx_callback)(running_machine &machine, int channel, int count, UINT8* data))
{
	memset(&duart[chip], 0, sizeof(PC16552D_REGS));

	duart[chip].frequency = frequency;
	duart[chip].irq_handler = irq_handler;
	duart[chip].tx_callback = tx_callback;

	// clear interrupts
	duart[chip].ch[0].pending_interrupt = 0;
	duart[chip].ch[1].pending_interrupt = 0;

	// allocate transmit timers
	duart[chip].ch[0].tx_fifo_timer = machine.scheduler().timer_alloc(FUNC(tx_fifo_timer_callback));
	duart[chip].ch[0].tx_fifo_timer->adjust(attotime::never, (chip * 2) + 0);

	duart[chip].ch[1].tx_fifo_timer = machine.scheduler().timer_alloc(FUNC(tx_fifo_timer_callback));
	duart[chip].ch[1].tx_fifo_timer->adjust(attotime::never, (chip * 2) + 1);
}

void pc16552d_rx_data(running_machine &machine, int chip, int channel, UINT8 data)
{
	if (duart[chip].ch[channel].reg[REG_FIFO_CTRL] & 0x01)  // RCVR & XMIT FIFO enable
	{
		duart_push_rx_fifo(machine, chip, channel, data);
	}
}

/*****************************************************************************/
/* Read/Write handlers */

READ8_HANDLER(pc16552d_0_r)
{
	return duart_r(space.machine(), 0, offset);
}

WRITE8_HANDLER(pc16552d_0_w)
{
	duart_w(space.machine(), 0, offset, data);
}

READ8_HANDLER(pc16552d_1_r)
{
	return duart_r(space.machine(), 1, offset);
}

WRITE8_HANDLER(pc16552d_1_w)
{
	duart_w(space.machine(), 1, offset, data);
}