summaryrefslogtreecommitdiffstatshomepage
path: root/src/emu/machine/i8251.c
blob: f06d800d9ebaad1031af1f28ca82494c841c68af (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
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
/*********************************************************************

    i8251.c

    Intel 8251 Universal Synchronous/Asynchronous Receiver Transmitter code

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

#include "emu.h"
#include "i8251.h"


/***************************************************************************
    MACROS
***************************************************************************/

#define VERBOSE 0

#define LOG(x)	do { if (VERBOSE) logerror x; } while (0)

/***************************************************************************
    GLOBAL VARIABLES
***************************************************************************/

const i8251_interface default_i8251_interface = { DEVCB_NULL, DEVCB_NULL, DEVCB_NULL, DEVCB_NULL, DEVCB_NULL, DEVCB_NULL, DEVCB_NULL, DEVCB_NULL, DEVCB_NULL };


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

/*-------------------------------------------------
    i8251_in_callback
-------------------------------------------------*/

void i8251_device::input_callback(UINT8 state)
{
	int changed = m_input_state^state;

	m_input_state = state;

	/* did cts change state? */
	if (changed & SERIAL_STATE_CTS)
	{
		/* yes */
		/* update tx ready */
		/* update_tx_ready(); */
	}
}

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

const device_type I8251 = &device_creator<i8251_device>;

//-------------------------------------------------
//  i8251_device - constructor
//-------------------------------------------------

i8251_device::i8251_device(const machine_config &mconfig, const char *tag, device_t *owner, UINT32 clock)
    : device_t(mconfig, I8251, "I8251", tag, owner, clock),
	  device_serial_interface(mconfig, *this)
{
}

//-------------------------------------------------
//  device_config_complete - perform any
//  operations now that the configuration is
//  complete
//-------------------------------------------------

void i8251_device::device_config_complete()
{
	// inherit a copy of the static data
	const i8251_interface *intf = reinterpret_cast<const i8251_interface *>(static_config());
	if (intf != NULL)
		*static_cast<i8251_interface *>(this) = *intf;

	// or initialize to defaults if none provided
	else
	{
		memset(&m_in_rxd_cb, 0, sizeof(m_in_rxd_cb));
		memset(&m_out_txd_cb, 0, sizeof(m_out_txd_cb));
		memset(&m_in_dsr_cb, 0, sizeof(m_in_dsr_cb));
		memset(&m_out_dtr_cb, 0, sizeof(m_out_dtr_cb));
		memset(&m_out_rts_cb, 0, sizeof(m_out_rts_cb));
		memset(&m_out_rxrdy_cb, 0, sizeof(m_out_rxrdy_cb));
		memset(&m_out_txrdy_cb, 0, sizeof(m_out_txrdy_cb));
		memset(&m_out_txempty_cb, 0, sizeof(m_out_txempty_cb));
		memset(&m_out_syndet_cb, 0, sizeof(m_out_syndet_cb));
	}
}

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

void i8251_device::device_start()
{
	// resolve callbacks
	m_out_rxrdy_func.resolve(m_out_rxrdy_cb, *this);
	m_out_txrdy_func.resolve(m_out_txrdy_cb, *this);
	m_out_txempty_func.resolve(m_out_txempty_cb, *this);

	m_input_state = 0;
}




/*-------------------------------------------------
    update_rx_ready
-------------------------------------------------*/

void i8251_device::update_rx_ready()
{
	int state;

	state = m_status & I8251_STATUS_RX_READY;

	/* masked? */
	if ((m_command & (1<<2))==0)
	{
		state = 0;
	}

	m_out_rxrdy_func(state != 0);
}



/*-------------------------------------------------
    receive_clock
-------------------------------------------------*/

void i8251_device::receive_clock()
{
	/* receive enable? */
	if (m_command & (1<<2))
	{
		//logerror("I8251\n");
		/* get bit received from other side and update receive register */
		receive_register_update_bit(get_in_data_bit());

		if (is_receive_register_full())
		{
			receive_register_extract();
			receive_character(get_received_char());
		}
	}
}



/*-------------------------------------------------
    transmit_clock
-------------------------------------------------*/

void i8251_device::transmit_clock()
{
	/* transmit enable? */
	if (m_command & (1<<0))
	{

		/* transmit register full? */
		if ((m_status & I8251_STATUS_TX_READY)==0)
		{
			/* if transmit reg is empty */
			if (is_transmit_register_empty())
			{
				/* set it up */
				transmit_register_setup(m_data);
				/* i8251 transmit reg now empty */
				m_status |=I8251_STATUS_TX_EMPTY;
				/* ready for next transmit */
				m_status |=I8251_STATUS_TX_READY;

				update_tx_empty();
				update_tx_ready();
			}
		}

		/* if transmit is not empty... transmit data */
		if (!is_transmit_register_empty())
		{
	//      logerror("I8251\n");
			transmit_register_send_bit();
		}
	}

#if 0
	/* hunt mode? */
	/* after each bit has been shifted in, it is compared against the current sync byte */
	if (m_command & (1<<7))
	{
		/* data matches sync byte? */
		if (m_data == m_sync_bytes[m_sync_byte_offset])
		{
			/* sync byte matches */
			/* update for next sync byte? */
			m_sync_byte_offset++;

			/* do all sync bytes match? */
			if (m_sync_byte_offset == m_sync_byte_count)
			{
				/* ent hunt mode */
				m_command &=~(1<<7);
			}
		}
		else
		{
			/* if there is no match, reset */
			m_sync_byte_offset = 0;
		}
	}
#endif
}



/*-------------------------------------------------
    update_tx_ready
-------------------------------------------------*/

void i8251_device::update_tx_ready()
{
	/* clear tx ready state */
	int tx_ready;

	/* tx ready output is set if:
        DB Buffer Empty &
        CTS is set &
        Transmit enable is 1
    */

	tx_ready = 0;

	/* transmit enable? */
	if ((m_command & (1<<0))!=0)
	{
		/* other side has rts set (comes in as CTS at this side) */
		if (m_input_state & SERIAL_STATE_CTS)
		{
			if (m_status & I8251_STATUS_TX_EMPTY)
			{
				/* enable transfer */
				tx_ready = 1;
			}
		}
	}

	m_out_txrdy_func(tx_ready);
}



/*-------------------------------------------------
    update_tx_empty
-------------------------------------------------*/

void i8251_device::update_tx_empty()
{
	if (m_status & I8251_STATUS_TX_EMPTY)
	{
		/* tx is in marking state (high) when tx empty! */
		set_out_data_bit(1);
		serial_connection_out();
	}

	m_out_txempty_func((m_status & I8251_STATUS_TX_EMPTY) != 0);
}



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

void i8251_device::device_reset()
{
	LOG(("I8251: Reset\n"));

	/* what is the default setup when the 8251 has been reset??? */

	/* i8251 datasheet explains the state of tx pin at reset */
	/* tx is set to 1 */
	set_out_data_bit(1);

	/* assumption, rts is set to 1 */
	m_connection_state &= ~SERIAL_STATE_RTS;
	serial_connection_out();

	transmit_register_reset();
	receive_register_reset();
	/* expecting mode byte */
	m_flags |= I8251_EXPECTING_MODE;
	/* not expecting a sync byte */
	m_flags &= ~I8251_EXPECTING_SYNC_BYTE;

	/* no character to read by cpu */
	/* transmitter is ready and is empty */
	m_status = I8251_STATUS_TX_EMPTY | I8251_STATUS_TX_READY;
	m_mode_byte = 0;
	m_command = 0;

	/* update tx empty pin output */
	update_tx_empty();
	/* update rx ready pin output */
	update_rx_ready();
	/* update tx ready pin output */
	update_tx_ready();
}



/*-------------------------------------------------
    control_w
-------------------------------------------------*/

WRITE8_MEMBER(i8251_device::control_w)
{
	if (m_flags & I8251_EXPECTING_MODE)
	{
		if (m_flags & I8251_EXPECTING_SYNC_BYTE)
		{
			LOG(("I8251: Sync byte\n"));

			LOG(("Sync byte: %02x\n", data));
			/* store sync byte written */
			m_sync_bytes[m_sync_byte_offset] = data;
			m_sync_byte_offset++;

			if (m_sync_byte_offset == m_sync_byte_count)
			{
				/* finished transfering sync bytes, now expecting command */
				m_flags &= ~(I8251_EXPECTING_MODE | I8251_EXPECTING_SYNC_BYTE);
				m_sync_byte_offset = 0;
			//  m_status = I8251_STATUS_TX_EMPTY | I8251_STATUS_TX_READY;
			}
		}
		else
		{
			LOG(("I8251: Mode byte\n"));

			m_mode_byte = data;

			/* Synchronous or Asynchronous? */
			if ((data & 0x03)!=0)
			{
				/*  Asynchronous

                    bit 7,6: stop bit length
                        0 = inhibit
                        1 = 1 bit
                        2 = 1.5 bits
                        3 = 2 bits
                    bit 5: parity type
                        0 = parity odd
                        1 = parity even
                    bit 4: parity test enable
                        0 = disable
                        1 = enable
                    bit 3,2: character length
                        0 = 5 bits
                        1 = 6 bits
                        2 = 7 bits
                        3 = 8 bits
                    bit 1,0: baud rate factor
                        0 = defines command byte for synchronous or asynchronous
                        1 = x1
                        2 = x16
                        3 = x64
                */

				LOG(("I8251: Asynchronous operation\n"));

				LOG(("Character length: %d\n", (((data>>2) & 0x03)+5)));

				if (data & (1<<4))
				{
					LOG(("enable parity checking\n"));
				}
				else
				{
					LOG(("parity check disabled\n"));
				}

				if (data & (1<<5))
				{
					LOG(("even parity\n"));
				}
				else
				{
					LOG(("odd parity\n"));
				}

				{
					UINT8 stop_bit_length;

					stop_bit_length = (data>>6) & 0x03;

					switch (stop_bit_length)
					{
						case 0:
						{
							/* inhibit */
							LOG(("stop bit: inhibit\n"));
						}
						break;

						case 1:
						{
							/* 1 */
							LOG(("stop bit: 1 bit\n"));
						}
						break;

						case 2:
						{
							/* 1.5 */
							LOG(("stop bit: 1.5 bits\n"));
						}
						break;

						case 3:
						{
							/* 2 */
							LOG(("stop bit: 2 bits\n"));
						}
						break;
					}
				}

				int word_length = ((data>>2) & 0x03)+5;
				int parity = SERIAL_PARITY_NONE;
				int stop_bit_count = 1;
				switch ((data>>6) & 0x03)
				{
					case 0:
					case 1:
						stop_bit_count =  1;
						break;
					case 2:
					case 3:
						stop_bit_count =  2;
						break;
				}
				set_data_frame(word_length,stop_bit_count,parity);

#if 0
				/* data bits */
				m_receive_char_length = (((data>>2) & 0x03)+5);

				if (data & (1<<4))
				{
					/* parity */
					m_receive_char_length++;
				}

				/* stop bits */
				m_receive_char_length++;

				m_receive_flags &=~I8251_TRANSFER_RECEIVE_SYNCHRONISED;
				m_receive_flags |= I8251_TRANSFER_RECEIVE_WAITING_FOR_START_BIT;
#endif
				/* not expecting mode byte now */
				m_flags &= ~I8251_EXPECTING_MODE;
//              m_status = I8251_STATUS_TX_EMPTY | I8251_STATUS_TX_READY;
			}
			else
			{
				/*  bit 7: Number of sync characters
                        0 = 1 character
                        1 = 2 character
                    bit 6: Synchronous mode
                        0 = Internal synchronisation
                        1 = External synchronisation
                    bit 5: parity type
                        0 = parity odd
                        1 = parity even
                    bit 4: parity test enable
                        0 = disable
                        1 = enable
                    bit 3,2: character length
                        0 = 5 bits
                        1 = 6 bits
                        2 = 7 bits
                        3 = 8 bits
                    bit 1,0 = 0
                */
				LOG(("I8251: Synchronous operation\n"));

				/* setup for sync byte(s) */
				m_flags |= I8251_EXPECTING_SYNC_BYTE;
				m_sync_byte_offset = 0;
				if (data & 0x07)
				{
					m_sync_byte_count = 1;
				}
				else
				{
					m_sync_byte_count = 2;
				}

			}
		}
	}
	else
	{
		/* command */
		LOG(("I8251: Command byte\n"));

		m_command = data;

		LOG(("Command byte: %02x\n", data));

		if (data & (1<<7))
		{
			LOG(("hunt mode\n"));
		}

		if (data & (1<<5))
		{
			LOG(("/rts set to 0\n"));
		}
		else
		{
			LOG(("/rts set to 1\n"));
		}

		if (data & (1<<2))
		{
			LOG(("receive enable\n"));
		}
		else
		{
			LOG(("receive disable\n"));
		}

		if (data & (1<<1))
		{
			LOG(("/dtr set to 0\n"));
		}
		else
		{
			LOG(("/dtr set to 1\n"));
		}

		if (data & (1<<0))
		{
			LOG(("transmit enable\n"));
		}
		else
		{
			LOG(("transmit disable\n"));
		}


		/*  bit 7:
                0 = normal operation
                1 = hunt mode
            bit 6:
                0 = normal operation
                1 = internal reset
            bit 5:
                0 = /RTS set to 1
                1 = /RTS set to 0
            bit 4:
                0 = normal operation
                1 = reset error flag
            bit 3:
                0 = normal operation
                1 = send break character
            bit 2:
                0 = receive disable
                1 = receive enable
            bit 1:
                0 = /DTR set to 1
                1 = /DTR set to 0
            bit 0:
                0 = transmit disable
                1 = transmit enable
        */

		m_connection_state &=~SERIAL_STATE_RTS;
		if (data & (1<<5))
		{
			/* rts set to 0 */
			m_connection_state |= SERIAL_STATE_RTS;
		}

		m_connection_state &=~SERIAL_STATE_DTR;
		if (data & (1<<1))
		{
			m_connection_state |= SERIAL_STATE_DTR;
		}

		if ((data & (1<<0))==0)
		{
			/* held in high state when transmit disable */
			set_out_data_bit(1);
		}


		/* refresh outputs */
		serial_connection_out();

		if (data & (1<<4))
		{
			m_status &= ~(I8251_STATUS_PARITY_ERROR | I8251_STATUS_OVERRUN_ERROR | I8251_STATUS_FRAMING_ERROR);
		}

		if (data & (1<<6))
		{
			reset();
		}

		update_rx_ready();
		update_tx_ready();

	}
}



/*-------------------------------------------------
    status_r
-------------------------------------------------*/

READ8_MEMBER(i8251_device::status_r)
{
	LOG(("status: %02x\n", m_status));
	return m_status;
}



/*-------------------------------------------------
    data_w
-------------------------------------------------*/

WRITE8_MEMBER(i8251_device::data_w)
{
	m_data = data;

	logerror("write data: %02x\n",data);

	/* writing clears */
	m_status &=~I8251_STATUS_TX_READY;

	/* if transmitter is active, then tx empty will be signalled */

	update_tx_ready();
}



/*-------------------------------------------------
    receive_character - called when last
    bit of data has been received
-------------------------------------------------*/

void i8251_device::receive_character(UINT8 ch)
{
	logerror("i8251 receive char: %02x\n",ch);

	m_data = ch;

	/* char has not been read and another has arrived! */
	if (m_status & I8251_STATUS_RX_READY)
	{
		m_status |= I8251_STATUS_OVERRUN_ERROR;
	}
	m_status |= I8251_STATUS_RX_READY;

	update_rx_ready();
}



/*-------------------------------------------------
    data_r - read data
-------------------------------------------------*/

READ8_MEMBER(i8251_device::data_r)
{
	logerror("read data: %02x, STATUS=%02x\n",m_data,m_status);
	/* reading clears */
	m_status &= ~I8251_STATUS_RX_READY;

	update_rx_ready();
	return m_data;
}