summaryrefslogtreecommitdiffstatshomepage
path: root/src/emu/machine/6525tpi.c
blob: 12a0f76f952ba7b608382eb348d9b7a6ffdccb82 (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
/***************************************************************************
    mos tri port interface 6525
    mos triple interface adapter 6523

    peter.trauner@jk.uni-linz.ac.at

    used in commodore b series
    used in commodore c1551 floppy disk drive
***************************************************************************/

/*
 mos tpi 6525
 40 pin package
 3 8 bit ports (pa, pb, pc)
 8 registers to pc
 0 port a 0 in low
 1 port a data direction register 1 output
 2 port b
 3 port b ddr
 4 port c
  handshaking, interrupt mode
  0 interrupt 0 input, 1 interrupt enabled
   interrupt set on falling edge
  1 interrupt 1 input
  2 interrupt 2 input
  3 interrupt 3 input
  4 interrupt 4 input
  5 irq output
  6 ca handshake line (read handshake answer on I3 preferred)
  7 cb handshake line (write handshake clear on I4 preferred)
 5 port c ddr

 6 cr configuration register
  0 mc
    0 port c normal input output mode like port a and b
    1 port c used for handshaking and interrupt input
  1 1 interrupt prioritized
  2 i3 configure edge
    1 interrupt set on positive edge
  3 i4 configure edge
  5,4 ca handshake
   00 on read
      rising edge of i3 sets ca high
      read a data from computers sets ca low
   01 pulse output
      1 microsecond low after read a operation
   10 manual output low
   11 manual output high
  7,6 cb handshake
   00 handshake on write
      write b data from computer sets cb 0
      rising edge of i4 sets cb high
   01 pulse output
      1 microsecond low after write b operation
   10 manual output low
   11 manual output high
 7 air active interrupt register
   0 I0 occurred
   1 I1 occurred
   2 I2 occurred
   3 I3 occurred
   4 I4 occurred
   read clears interrupt

 non prioritized interrupts
  interrupt is set when occurred
  read clears all interrupts

 prioritized interrupts
  I4>I3>I2>I1>I0
  highest interrupt can be found in air register
  read clears highest interrupt
*/

#include "emu.h"
#include "6525tpi.h"


/***************************************************************************
    CONSTANTS
***************************************************************************/

#define VERBOSE_LEVEL 0
#define DBG_LOG( MACHINE, N, M, A ) \
	do { \
		if(VERBOSE_LEVEL >= N) \
		{ \
			if( M ) \
				logerror("%11.6f: %-24s", MACHINE.time().as_double(), (char*) M ); \
			logerror A; \
		} \
	} while (0)


#define INTERRUPT_MODE (tpi6525->cr & 1)
#define PRIORIZED_INTERRUPTS (tpi6525->cr & 2)
#define INTERRUPT3_RISING_EDGE (tpi6525->cr & 4)
#define INTERRUPT4_RISING_EDGE (tpi6525->cr & 8)
#define CA_MANUAL_OUT (tpi6525->cr & 0x20)
#define CA_MANUAL_LEVEL ((tpi6525->cr & 0x10) ? 1 : 0)
#define CB_MANUAL_OUT (tpi6525->cr & 0x80)
#define CB_MANUAL_LEVEL ((tpi6525->cr & 0x40) ? 1 : 0)


/***************************************************************************
    TYPE DEFINITIONS
***************************************************************************/

typedef struct _tpi6525_state tpi6525_state;
struct _tpi6525_state
{
	devcb_resolved_write_line	out_irq_func;
	devcb_resolved_read8		in_pa_func;
	devcb_resolved_write8		out_pa_func;
	devcb_resolved_read8		in_pb_func;
	devcb_resolved_write8		out_pb_func;
	devcb_resolved_read8		in_pc_func;
	devcb_resolved_write8		out_pc_func;
	devcb_resolved_write_line	out_ca_func;
	devcb_resolved_write_line	out_cb_func;

	UINT8 port_a, ddr_a, in_a;
	UINT8 port_b, ddr_b, in_b;
	UINT8 port_c, ddr_c, in_c;

	UINT8 ca_level, cb_level, interrupt_level;

	UINT8 cr;
	UINT8 air;

	UINT8 irq_level[5];
};


/*****************************************************************************
    INLINE FUNCTIONS
*****************************************************************************/

INLINE tpi6525_state *get_safe_token(device_t *device)
{
	assert(device != NULL);
	assert(device->type() == TPI6525);

	return (tpi6525_state *)downcast<legacy_device_base *>(device)->token();
}


/*****************************************************************************
    DEVICE INTERFACE
*****************************************************************************/

static DEVICE_START( tpi6525 )
{
	tpi6525_state *tpi6525 = get_safe_token(device);
	const tpi6525_interface *intf = (const tpi6525_interface*)device->static_config();

	// resolve callbacks
	tpi6525->out_irq_func.resolve(intf->out_irq_func, *device);
	tpi6525->in_pa_func.resolve(intf->in_pa_func, *device);
	tpi6525->out_pa_func.resolve(intf->out_pa_func, *device);
	tpi6525->in_pb_func.resolve(intf->in_pb_func, *device);
	tpi6525->out_pb_func.resolve(intf->out_pb_func, *device);
	tpi6525->in_pc_func.resolve(intf->in_pc_func, *device);
	tpi6525->out_pc_func.resolve(intf->out_pc_func, *device);
	tpi6525->out_ca_func.resolve(intf->out_ca_func, *device);
	tpi6525->out_cb_func.resolve(intf->out_cb_func, *device);

	/* verify that we have an interface assigned */
	assert(device->static_config() != NULL);

	/* register for state saving */
	device->save_item(NAME(tpi6525->port_a));
	device->save_item(NAME(tpi6525->ddr_a));
	device->save_item(NAME(tpi6525->in_a));
	device->save_item(NAME(tpi6525->port_b));
	device->save_item(NAME(tpi6525->ddr_b));
	device->save_item(NAME(tpi6525->in_b));
	device->save_item(NAME(tpi6525->port_c));
	device->save_item(NAME(tpi6525->ddr_c));
	device->save_item(NAME(tpi6525->in_c));
	device->save_item(NAME(tpi6525->ca_level));
	device->save_item(NAME(tpi6525->cb_level));
	device->save_item(NAME(tpi6525->interrupt_level));
	device->save_item(NAME(tpi6525->cr));
	device->save_item(NAME(tpi6525->air));
	device->save_item(NAME(tpi6525->irq_level));
}


static DEVICE_RESET( tpi6525 )
{
	tpi6525_state *tpi6525 = get_safe_token(device);

	/* setup some initial values */
	tpi6525->in_a = 0xff;
	tpi6525->in_b = 0xff;
	tpi6525->in_c = 0xff;
}


DEVICE_GET_INFO( tpi6525 )
{
	switch (state)
	{
		/* --- the following bits of info are returned as 64-bit signed integers --- */
		case DEVINFO_INT_TOKEN_BYTES:			info->i = sizeof(tpi6525_state);		break;

		/* --- the following bits of info are returned as pointers to data or functions --- */
		case DEVINFO_FCT_START:					info->start = DEVICE_START_NAME( tpi6525 );			break;
		case DEVINFO_FCT_STOP:					/* Nothing */										break;
		case DEVINFO_FCT_RESET:					info->reset = DEVICE_RESET_NAME( tpi6525 );			break;

		/* --- the following bits of info are returned as NULL-terminated strings --- */
		case DEVINFO_STR_NAME:					strcpy(info->s, "6525 TPI");			break;
		case DEVINFO_STR_FAMILY:				strcpy(info->s, "6525 TPI");			break;
		case DEVINFO_STR_VERSION:				strcpy(info->s, "1.0");					break;
		case DEVINFO_STR_SOURCE_FILE:			strcpy(info->s, __FILE__);				break;
		case DEVINFO_STR_CREDITS:				strcpy(info->s, "Copyright MESS Team");	break;
	}
}


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

static void tpi6525_set_interrupt(device_t *device)
{
	tpi6525_state *tpi6525 = get_safe_token(device);

	if (!tpi6525->interrupt_level && (tpi6525->air != 0))
	{
		tpi6525->interrupt_level = 1;

		DBG_LOG(device->machine(), 3, "tpi6525", ("%s set interrupt\n", device->tag()));

		tpi6525->out_irq_func(tpi6525->interrupt_level);
	}
}


static void tpi6525_clear_interrupt(device_t *device)
{
	tpi6525_state *tpi6525 = get_safe_token(device);

	if (tpi6525->interrupt_level && (tpi6525->air == 0))
	{
		tpi6525->interrupt_level = 0;

		DBG_LOG(device->machine(), 3, "tpi6525", ("%s clear interrupt\n", device->tag()));

		tpi6525->out_irq_func(tpi6525->interrupt_level);
	}
}


WRITE_LINE_DEVICE_HANDLER( tpi6525_i0_w )
{
	tpi6525_state *tpi6525 = get_safe_token(device);

	if (INTERRUPT_MODE && (state != tpi6525->irq_level[0]))
	{
		tpi6525->irq_level[0] = state;

		if ((state == 0) && !(tpi6525->air & 1) && (tpi6525->ddr_c & 1))
		{
			tpi6525->air |= 1;
			tpi6525_set_interrupt(device);
		}
	}
}


WRITE_LINE_DEVICE_HANDLER( tpi6525_i1_w )
{
	tpi6525_state *tpi6525 = get_safe_token(device);

	if (INTERRUPT_MODE && (state != tpi6525->irq_level[1]))
	{
		tpi6525->irq_level[1] = state;

		if ((state == 0) && !(tpi6525->air & 2) && (tpi6525->ddr_c & 2))
		{
			tpi6525->air |= 2;
			tpi6525_set_interrupt(device);
		}
	}
}


WRITE_LINE_DEVICE_HANDLER( tpi6525_i2_w )
{
	tpi6525_state *tpi6525 = get_safe_token(device);

	if (INTERRUPT_MODE && (state != tpi6525->irq_level[2]))
	{
		tpi6525->irq_level[2] = state;

		if ((state == 0) && !(tpi6525->air & 4) && (tpi6525->ddr_c & 4))
		{
			tpi6525->air |= 4;
			tpi6525_set_interrupt(device);
		}
	}
}


WRITE_LINE_DEVICE_HANDLER( tpi6525_i3_w )
{
	tpi6525_state *tpi6525 = get_safe_token(device);

	if (INTERRUPT_MODE && (state != tpi6525->irq_level[3]))
	{
		tpi6525->irq_level[3] = state;

		if (((INTERRUPT3_RISING_EDGE && (state == 1))
			|| (!INTERRUPT3_RISING_EDGE && (state == 0)))
			&& !(tpi6525->air & 8) && (tpi6525->ddr_c & 8))
		{
			tpi6525->air |= 8;
			tpi6525_set_interrupt(device);
		}
	}
}


WRITE_LINE_DEVICE_HANDLER( tpi6525_i4_w )
{
	tpi6525_state *tpi6525 = get_safe_token(device);

	if (INTERRUPT_MODE && (state != tpi6525->irq_level[4]) )
	{
		tpi6525->irq_level[4] = state;

		if (((INTERRUPT4_RISING_EDGE && (state == 1))
			||(!INTERRUPT4_RISING_EDGE&&(state == 0)))
			&& !(tpi6525->air & 0x10) && (tpi6525->ddr_c & 0x10))
		{
			tpi6525->air |= 0x10;
			tpi6525_set_interrupt(device);
		}
	}
}


READ8_DEVICE_HANDLER( tpi6525_porta_r )
{
	tpi6525_state *tpi6525 = get_safe_token(device);
	UINT8 data = tpi6525->in_a;

	if (!tpi6525->in_pa_func.isnull())
		data = tpi6525->in_pa_func(offset);

	data = (data & ~tpi6525->ddr_a) | (tpi6525->ddr_a & tpi6525->port_a);

	return data;
}


WRITE8_DEVICE_HANDLER( tpi6525_porta_w )
{
	tpi6525_state *tpi6525 = get_safe_token(device);

	tpi6525->in_a = data;
}


READ8_DEVICE_HANDLER( tpi6525_portb_r )
{
	tpi6525_state *tpi6525 = get_safe_token(device);
	UINT8 data = tpi6525->in_b;

	if (!tpi6525->in_pb_func.isnull())
		data = tpi6525->in_pb_func(offset);

	data = (data & ~tpi6525->ddr_b) | (tpi6525->ddr_b & tpi6525->port_b);

	return data;
}


WRITE8_DEVICE_HANDLER( tpi6525_portb_w )
{
	tpi6525_state *tpi6525 = get_safe_token(device);

	tpi6525->in_b = data;
}


READ8_DEVICE_HANDLER( tpi6525_portc_r )
{
	tpi6525_state *tpi6525 = get_safe_token(device);
	UINT8 data = tpi6525->in_c;

	if (!tpi6525->in_pc_func.isnull())
		data &= tpi6525->in_pc_func(offset);

	data = (data & ~tpi6525->ddr_c) | (tpi6525->ddr_c & tpi6525->port_c);

	return data;
}


WRITE8_DEVICE_HANDLER( tpi6525_portc_w )
{
	tpi6525_state *tpi6525 = get_safe_token(device);

	tpi6525->in_c = data;
}


READ8_DEVICE_HANDLER( tpi6525_r )
{
	tpi6525_state *tpi6525 = get_safe_token(device);
	UINT8 data = 0xff;

	switch (offset & 7)
	{
	case 0:
		data = tpi6525->in_a;

		if (!tpi6525->in_pa_func.isnull())
			data &= tpi6525->in_pa_func(0);

		data = (data & ~tpi6525->ddr_a) | (tpi6525->ddr_a & tpi6525->port_a);

		break;

	case 1:
		data = tpi6525->in_b;

		if (!tpi6525->in_pb_func.isnull())
			data &= tpi6525->in_pb_func(0);

		data = (data & ~tpi6525->ddr_b) | (tpi6525->ddr_b & tpi6525->port_b);

		break;

	case 2:
		if (INTERRUPT_MODE)
		{
			data = 0;

			if (tpi6525->irq_level[0]) data |= 0x01;
			if (tpi6525->irq_level[1]) data |= 0x02;
			if (tpi6525->irq_level[2]) data |= 0x04;
			if (tpi6525->irq_level[3]) data |= 0x08;
			if (tpi6525->irq_level[4]) data |= 0x10;
			if (!tpi6525->interrupt_level) data |= 0x20;
			if (tpi6525->ca_level) data |= 0x40;
			if (tpi6525->cb_level) data |= 0x80;
		}
		else
		{
			data = tpi6525->in_c;

			if (!tpi6525->in_pc_func.isnull())
				data &= tpi6525->in_pc_func(0);

			data = (data & ~tpi6525->ddr_c) | (tpi6525->ddr_c & tpi6525->port_c);
		}

		DBG_LOG(device->machine(), 2, "tpi6525", ("%s read %.2x %.2x\n", device->tag(), offset, data));
		break;

	case 3:
		data = tpi6525->ddr_a;
		break;

	case 4:
		data = tpi6525->ddr_b;
		break;

	case 5:
		data = tpi6525->ddr_c;
		break;

	case 6:
		data = tpi6525->cr;
		break;

	case 7: /* air */
		if (PRIORIZED_INTERRUPTS)
		{
			if (tpi6525->air & 0x10)
			{
				data = 0x10;
				tpi6525->air &= ~0x10;
			}
			else if (tpi6525->air & 8)
			{
				data = 8;
				tpi6525->air &= ~8;
			}
			else if (tpi6525->air & 4)
			{
				data = 4;
				tpi6525->air &= ~4;
			}
			else if (tpi6525->air & 2)
			{
				data = 2;
				tpi6525->air &= ~2;
			}
			else if (tpi6525->air & 1)
			{
				data = 1;
				tpi6525->air &= ~1;
			}
		}
		else
		{
			data = tpi6525->air;
			tpi6525->air = 0;
		}

		tpi6525_clear_interrupt(device);
		break;

	}

	DBG_LOG(device->machine(), 3, "tpi6525", ("%s read %.2x %.2x\n", device->tag(), offset, data));

	return data;
}


WRITE8_DEVICE_HANDLER( tpi6525_w )
{
	tpi6525_state *tpi6525 = get_safe_token(device);

	DBG_LOG(device->machine(), 2, "tpi6525", ("%s write %.2x %.2x\n", device->tag(), offset, data));

	switch (offset & 7)
	{
	case 0:
		tpi6525->port_a = data;
		tpi6525->out_pa_func(0, (tpi6525->port_a & tpi6525->ddr_a) | (tpi6525->ddr_a ^ 0xff));
		break;

	case 1:
		tpi6525->port_b = data;
		tpi6525->out_pb_func(0, (tpi6525->port_b & tpi6525->ddr_b) | (tpi6525->ddr_b ^ 0xff));
		break;

	case 2:
		tpi6525->port_c = data;

		if (!INTERRUPT_MODE)
			tpi6525->out_pc_func(0, (tpi6525->port_c & tpi6525->ddr_c) | (tpi6525->ddr_c ^ 0xff));
		break;

	case 3:
		tpi6525->ddr_a = data;
		tpi6525->out_pa_func(0, (tpi6525->port_a & tpi6525->ddr_a) | (tpi6525->ddr_a ^ 0xff));
		break;

	case 4:
		tpi6525->ddr_b = data;
		tpi6525->out_pb_func(0, (tpi6525->port_b & tpi6525->ddr_b) | (tpi6525->ddr_b ^ 0xff));
		break;

	case 5:
		tpi6525->ddr_c = data;

		if (!INTERRUPT_MODE)
			tpi6525->out_pc_func(0, (tpi6525->port_c & tpi6525->ddr_c) | (tpi6525->ddr_c ^ 0xff));
		break;

	case 6:
		tpi6525->cr = data;

		if (INTERRUPT_MODE)
		{
			if (CA_MANUAL_OUT)
			{
				if (tpi6525->ca_level != CA_MANUAL_LEVEL)
				{
					tpi6525->ca_level = CA_MANUAL_LEVEL;
					tpi6525->out_ca_func(tpi6525->ca_level);
				}
			}
			if (CB_MANUAL_OUT)
			{
				if (tpi6525->cb_level != CB_MANUAL_LEVEL)
				{
					tpi6525->cb_level = CB_MANUAL_LEVEL;
					tpi6525->out_cb_func(tpi6525->cb_level);
				}
			}
		}

		break;

	case 7:
		/* tpi6525->air = data; */
		break;
	}
}


/* this should probably be done better, needed for amigacd.c */

UINT8 tpi6525_get_ddr_a(device_t *device)
{
	tpi6525_state *tpi6525 = get_safe_token(device);
	return tpi6525->ddr_a;
}

UINT8 tpi6525_get_ddr_b(device_t *device)
{
	tpi6525_state *tpi6525 = get_safe_token(device);
	return tpi6525->ddr_b;
}

UINT8 tpi6525_get_ddr_c(device_t *device)
{
	tpi6525_state *tpi6525 = get_safe_token(device);
	return tpi6525->ddr_c;
}

DEFINE_LEGACY_DEVICE(TPI6525, tpi6525);