summaryrefslogtreecommitdiffstatshomepage
path: root/src/emu/machine/6821new.c
blob: 4273e83230d6238ba31e64aa97a8179fddd130a7 (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
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
/**********************************************************************

    Motorola 6821 PIA interface and emulation

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

#include "6821new.h"


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

typedef struct _pia6821 pia6821;
struct _pia6821
{
	UINT8 in_a;
	UINT8 in_ca1;
	UINT8 in_ca2;
	UINT8 out_a;
	UINT8 out_ca2;
	UINT8 port_a_z_mask;
	UINT8 ddr_a;
	UINT8 ctl_a;
	UINT8 irq_a1;
	UINT8 irq_a2;
	UINT8 irq_a_state;

	UINT8 in_b;
	UINT8 in_cb1;
	UINT8 in_cb2;
	UINT8 out_b;
	UINT8 out_cb2;
	UINT8 last_out_cb2_z;
	UINT8 ddr_b;
	UINT8 ctl_b;
	UINT8 irq_b1;
	UINT8 irq_b2;
	UINT8 irq_b_state;

	/* variables that indicate if access a line externally -
       used to for logging purposes ONLY */
	UINT8 in_a_pushed;
	UINT8 out_a_needs_pulled;
	UINT8 in_ca1_pushed;
	UINT8 in_ca2_pushed;
	UINT8 out_ca2_needs_pulled;
	UINT8 in_b_pushed;
	UINT8 out_b_needs_pulled;
	UINT8 in_cb1_pushed;
	UINT8 in_cb2_pushed;
	UINT8 out_cb2_needs_pulled;
	UINT8 logged_port_a_not_connected;
	UINT8 logged_port_b_not_connected;
	UINT8 logged_ca1_not_connected;
	UINT8 logged_ca2_not_connected;
	UINT8 logged_cb1_not_connected;
	UINT8 logged_cb2_not_connected;
};


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

#define VERBOSE 0

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

#define PIA_IRQ1				(0x80)
#define PIA_IRQ2				(0x40)

#define IRQ1_ENABLED(c)			( (((c) >> 0) & 0x01))
#define C1_LOW_TO_HIGH(c)		( (((c) >> 1) & 0x01))
#define C1_HIGH_TO_LOW(c)		(!(((c) >> 1) & 0x01))
#define OUTPUT_SELECTED(c)		( (((c) >> 2) & 0x01))
#define IRQ2_ENABLED(c)			( (((c) >> 3) & 0x01))
#define STROBE_E_RESET(c)		( (((c) >> 3) & 0x01))
#define STROBE_C1_RESET(c)		(!(((c) >> 3) & 0x01))
#define C2_SET(c)				( (((c) >> 3) & 0x01))
#define C2_LOW_TO_HIGH(c)		( (((c) >> 4) & 0x01))
#define C2_HIGH_TO_LOW(c)		(!(((c) >> 4) & 0x01))
#define C2_SET_MODE(c)			( (((c) >> 4) & 0x01))
#define C2_STROBE_MODE(c)		(!(((c) >> 4) & 0x01))
#define C2_OUTPUT(c)			( (((c) >> 5) & 0x01))
#define C2_INPUT(c)				(!(((c) >> 5) & 0x01))


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

INLINE pia6821 *get_token(const device_config *device)
{
	assert(device != NULL);
	assert((device->type == PIA6821) || (device->type == PIA6822));
	return (pia6821 *) device->token;
}


INLINE const pia6821_interface *get_interface(const device_config *device)
{
	assert(device != NULL);
	assert((device->type == PIA6821) || (device->type == PIA6822));
	return (const pia6821_interface *) device->static_config;
}


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

/*-------------------------------------------------
    DEVICE_START( pia )
-------------------------------------------------*/

static DEVICE_START( pia )
{
	pia6821 *p = get_token(device);
	memset(p, 0, sizeof(*p));

	state_save_register_device_item(device, 0, p->in_a);
	state_save_register_device_item(device, 0, p->in_ca1);
	state_save_register_device_item(device, 0, p->in_ca2);
	state_save_register_device_item(device, 0, p->out_a);
	state_save_register_device_item(device, 0, p->out_ca2);
	state_save_register_device_item(device, 0, p->port_a_z_mask);
	state_save_register_device_item(device, 0, p->ddr_a);
	state_save_register_device_item(device, 0, p->ctl_a);
	state_save_register_device_item(device, 0, p->irq_a1);
	state_save_register_device_item(device, 0, p->irq_a2);
	state_save_register_device_item(device, 0, p->irq_a_state);
	state_save_register_device_item(device, 0, p->in_b);
	state_save_register_device_item(device, 0, p->in_cb1);
	state_save_register_device_item(device, 0, p->in_cb2);
	state_save_register_device_item(device, 0, p->out_b);
	state_save_register_device_item(device, 0, p->out_cb2);
	state_save_register_device_item(device, 0, p->last_out_cb2_z);
	state_save_register_device_item(device, 0, p->ddr_b);
	state_save_register_device_item(device, 0, p->ctl_b);
	state_save_register_device_item(device, 0, p->irq_b1);
	state_save_register_device_item(device, 0, p->irq_b2);
	state_save_register_device_item(device, 0, p->irq_b_state);
	state_save_register_device_item(device, 0, p->in_a_pushed);
	state_save_register_device_item(device, 0, p->out_a_needs_pulled);
	state_save_register_device_item(device, 0, p->in_ca1_pushed);
	state_save_register_device_item(device, 0, p->in_ca2_pushed);
	state_save_register_device_item(device, 0, p->out_ca2_needs_pulled);
	state_save_register_device_item(device, 0, p->in_b_pushed);
	state_save_register_device_item(device, 0, p->out_b_needs_pulled);
	state_save_register_device_item(device, 0, p->in_cb1_pushed);
	state_save_register_device_item(device, 0, p->in_cb2_pushed);
	state_save_register_device_item(device, 0, p->out_cb2_needs_pulled);

	return DEVICE_START_OK;
}


/*-------------------------------------------------
    DEVICE_RESET( pia )
-------------------------------------------------*/

static DEVICE_RESET( pia )
{
	pia6821 *p = get_token(device);
	const pia6821_interface *intf = get_interface(device);

	memset(p, 0, sizeof(*p));

	/*
     * set default read values.
     *
     * ports A,CA1,CA2 default to 1
     * ports B,CB1,CB2 are three-state and undefined (set to 0)
     */
	p->in_a = 0xff;
	p->in_ca1 = TRUE;
	p->in_ca2 = TRUE;

	/* clear the IRQs */
	if (intf->irq_a_func) (*intf->irq_a_func)(device, FALSE);
	if (intf->irq_b_func) (*intf->irq_b_func)(device, FALSE);
}


/*-------------------------------------------------
    update_interrupts
-------------------------------------------------*/

static void update_interrupts(const device_config *device)
{
	pia6821 *p = get_token(device);
	const pia6821_interface *intf = get_interface(device);
	int new_state;

	/* start with IRQ A */
	new_state = (p->irq_a1 && IRQ1_ENABLED(p->ctl_a)) || (p->irq_a2 && IRQ2_ENABLED(p->ctl_a));

	if (new_state != p->irq_a_state)
	{
		p->irq_a_state = new_state;

		if (intf->irq_a_func)
			(*intf->irq_a_func)(device, p->irq_a_state);
	}

	/* then do IRQ B */
	new_state = (p->irq_b1 && IRQ1_ENABLED(p->ctl_b)) || (p->irq_b2 && IRQ2_ENABLED(p->ctl_b));

	if (new_state != p->irq_b_state)
	{
		p->irq_b_state = new_state;

		if (intf->irq_b_func)
			(*intf->irq_b_func)(device, p->irq_b_state);
	}
}


/*-------------------------------------------------
    get_in_a_value
-------------------------------------------------*/

static UINT8 get_in_a_value(const device_config *device)
{
	pia6821 *p = get_token(device);
	const pia6821_interface *intf = get_interface(device);
	UINT8 port_a_data = 0;
	UINT8 ret;

	/* update the input */
	if (intf->in_a_func)
		port_a_data = (*intf->in_a_func)(device, 0);
	else
	{
		if (p->in_a_pushed)
			port_a_data = p->in_a;
		else
		{
			/* mark all pins disconnected */
			p->port_a_z_mask = 0xff;

			if (!p->logged_port_a_not_connected && (p->ddr_a != 0xff))
			{
				logerror("PIA #%s: Warning! No port A read handler. Assuming pins 0x%02X not connected\n", device->tag, p->ddr_a ^ 0xff);
				p->logged_port_a_not_connected = TRUE;
			}
		}
	}

	/* - connected pins are always read
       - disconnected pins read the output buffer in output mode
       - disconnected pins are HI in input mode */
	ret = (~p->port_a_z_mask             & port_a_data) |
	      ( p->port_a_z_mask &  p->ddr_a & p->out_a) |
	      ( p->port_a_z_mask & ~p->ddr_a);

	return ret;
}


/*-------------------------------------------------
    get_in_b_value
-------------------------------------------------*/

static UINT8 get_in_b_value(const device_config *device)
{
	pia6821 *p = get_token(device);
	const pia6821_interface *intf = get_interface(device);
	UINT8 ret;

	if (p->ddr_b == 0xff)
		/* all output, just return buffer */
		ret = p->out_b;
	else
	{
		UINT8 port_b_data;

		/* update the input */
		if (intf->in_b_func)
			port_b_data = (*intf->in_b_func)(device, 0);
		else
		{
			if (p->in_b_pushed)
				port_b_data = p->in_b;
			else
			{
				if (!p->logged_port_b_not_connected && (p->ddr_b != 0xff))
				{
					logerror("PIA #%s: Error! No port B read handler. Three-state pins 0x%02X are undefined\n", device->tag, p->ddr_b ^ 0xff);
					p->logged_port_b_not_connected = TRUE;
				}

				/* undefined -- need to return something */
				port_b_data = 0x00;
			}
		}

		/* the DDR determines if the pin or the output buffer is read */
		ret = (p->out_b & p->ddr_b) | (port_b_data & ~p->ddr_b);
	}

	return ret;
}


/*-------------------------------------------------
    get_out_a_value
-------------------------------------------------*/

static UINT8 get_out_a_value(const device_config *device)
{
	pia6821 *p = get_token(device);
	UINT8 ret;

	if (p->ddr_a == 0xff)
		/* all output */
		ret = p->out_a;
	else
		/* input pins don't change */
		ret = (p->out_a & p->ddr_a) | (get_in_a_value(device) & ~p->ddr_a);

	return ret;
}


/*-------------------------------------------------
    get_out_b_value
-------------------------------------------------*/

static UINT8 get_out_b_value(const device_config *device)
{
	pia6821 *p = get_token(device);

	/* input pins are high-impedance - we just send them as zeros for backwards compatibility */
	return p->out_b & p->ddr_b;
}


/*-------------------------------------------------
    set_out_ca2
-------------------------------------------------*/

static void set_out_ca2(const device_config *device, int data)
{
	pia6821 *p = get_token(device);
	const pia6821_interface *intf = get_interface(device);

	if (data != p->out_ca2)
	{
		p->out_ca2 = data;

		/* send to output function */
		if (intf->out_ca2_func)
			(*intf->out_ca2_func)(device, 0, p->out_ca2);
		else
		{
			if (p->out_ca2_needs_pulled)
				logerror("PIA #%s: Warning! No port CA2 write handler. Previous value has been lost!\n", device->tag);

			p->out_ca2_needs_pulled = TRUE;
		}
	}
}


/*-------------------------------------------------
    set_out_cb2
-------------------------------------------------*/

static void set_out_cb2(const device_config *device, int data)
{
	pia6821 *p = get_token(device);
	const pia6821_interface *intf = get_interface(device);

	int z = pianew_get_output_cb2_z(device);

	if ((data != p->out_cb2) || (z != p->last_out_cb2_z))
	{
		p->out_cb2 = data;
		p->last_out_cb2_z = z;

		/* send to output function */
		if (intf->out_cb2_func)
			(*intf->out_cb2_func)(device, 0, p->out_cb2);
		else
		{
			if (p->out_cb2_needs_pulled)
				logerror("PIA #%s: Warning! No port CB2 write handler. Previous value has been lost!\n", device->tag);

			p->out_cb2_needs_pulled = TRUE;
		}
	}
}


/*-------------------------------------------------
    port_a_r
-------------------------------------------------*/

static UINT8 port_a_r(const device_config *device)
{
	pia6821 *p = get_token(device);

	UINT8 ret = get_in_a_value(device);

	/* IRQ flags implicitly cleared by a read */
	p->irq_a1 = FALSE;
	p->irq_a2 = FALSE;
	update_interrupts(device);

	/* CA2 is configured as output and in read strobe mode */
	if (C2_OUTPUT(p->ctl_a) && C2_STROBE_MODE(p->ctl_a))
	{
		/* this will cause a transition low */
		set_out_ca2(device, FALSE);

		/* if the CA2 strobe is cleared by the E, reset it right away */
		if (STROBE_E_RESET(p->ctl_a))
			set_out_ca2(device, TRUE);
	}

	LOG(("PIA #%s: port A read = %02X\n", device->tag, ret));

	return ret;
}


/*-------------------------------------------------
    ddr_a_r
-------------------------------------------------*/

static UINT8 ddr_a_r(const device_config *device)
{
	pia6821 *p = get_token(device);

	UINT8 ret = p->ddr_a;

	LOG(("PIA #%s: DDR A read = %02X\n", device->tag, ret));

	return ret;
}


/*-------------------------------------------------
    port_b_r
-------------------------------------------------*/

static UINT8 port_b_r(const device_config *device)
{
	pia6821 *p = get_token(device);

	UINT8 ret = get_in_b_value(device);

	/* This read will implicitly clear the IRQ B1 flag.  If CB2 is in write-strobe
       mode with CB1 restore, and a CB1 active transition set the flag,
       clearing it will cause CB2 to go high again.  Note that this is different
       from what happens with port A. */
	if (p->irq_b1 && C2_STROBE_MODE(p->ctl_b) && STROBE_C1_RESET(p->ctl_b))
		set_out_cb2(device, TRUE);

	/* IRQ flags implicitly cleared by a read */
	p->irq_b1 = FALSE;
	p->irq_b2 = FALSE;
	update_interrupts(device);

	LOG(("PIA #%s: port B read = %02X\n", device->tag, ret));

	return ret;
}


/*-------------------------------------------------
    ddr_b_r
-------------------------------------------------*/

static UINT8 ddr_b_r(const device_config *device)
{
	pia6821 *p = get_token(device);

	UINT8 ret = p->ddr_b;

	LOG(("PIA #%s: DDR B read = %02X\n", device->tag, ret));

	return ret;
}


/*-------------------------------------------------
    control_a_r
-------------------------------------------------*/

static UINT8 control_a_r(const device_config *device)
{
	pia6821 *p = get_token(device);
	const pia6821_interface *intf = get_interface(device);
	UINT8 ret;

	/* update CA1 & CA2 if callback exists, these in turn may update IRQ's */
	if (intf->in_ca1_func)
		pia_ca1_w(device, 0, (*intf->in_ca1_func)(device, 0));
	else if (!p->logged_ca1_not_connected && (!p->in_ca1_pushed))
	{
		logerror("PIA #%s: Warning! No CA1 read handler. Assuming pin not connected\n", device->tag);
		p->logged_ca1_not_connected = TRUE;
	}

	if (intf->in_ca2_func)
		pia_ca2_w(device, 0, (*intf->in_ca2_func)(device, 0));
	else if ( !p->logged_ca2_not_connected && C2_INPUT(p->ctl_a) && !p->in_ca2_pushed)
	{
		logerror("PIA #%s: Warning! No CA2 read handler. Assuming pin not connected\n", device->tag);
		p->logged_ca2_not_connected = TRUE;
	}

	/* read control register */
	ret = p->ctl_a;

	/* set the IRQ flags if we have pending IRQs */
	if (p->irq_a1)
		ret |= PIA_IRQ1;

	if (p->irq_a2 && C2_INPUT(p->ctl_a))
		ret |= PIA_IRQ2;

	LOG(("PIA #%s: control A read = %02X\n", device->tag, ret));

	return ret;
}


/*-------------------------------------------------
    control_b_r
-------------------------------------------------*/

static UINT8 control_b_r(const device_config *device)
{
	pia6821 *p = get_token(device);
	const pia6821_interface *intf = get_interface(device);
	UINT8 ret;

	/* update CB1 & CB2 if callback exists, these in turn may update IRQ's */
	if (intf->in_cb1_func)
		pia_cb1_w(device, 0, (*intf->in_cb1_func)(device, 0));
	else if (!p->logged_cb1_not_connected && !p->in_cb1_pushed)
	{
		logerror("PIA #%s: Error! no CB1 read handler. Three-state pin is undefined\n", device->tag);
		p->logged_cb1_not_connected = TRUE;
	}

	if (intf->in_cb2_func)
		pia_cb2_w(device, 0, (*intf->in_cb2_func)(device, 0));
	else if (!p->logged_cb2_not_connected && C2_INPUT(p->ctl_b) && !p->in_cb2_pushed)
	{
		logerror("PIA #%s: Error! No CB2 read handler. Three-state pin is undefined\n", device->tag);
		p->logged_cb2_not_connected = TRUE;
	}

	/* read control register */
	ret = p->ctl_b;

	/* set the IRQ flags if we have pending IRQs */
	if (p->irq_b1)
		ret |= PIA_IRQ1;

	if (p->irq_b2 && C2_INPUT(p->ctl_b))
		ret |= PIA_IRQ2;

	LOG(("PIA #%s: control B read = %02X\n", device->tag, ret));

	return ret;
}


/*-------------------------------------------------
    pia_r
-------------------------------------------------*/

READ8_DEVICE_HANDLER(pia_r)
{
	pia6821 *p = get_token(device);
	UINT8 ret;

	switch (offset & 0x03)
	{
		default: /* impossible */
		case 0x00:
			if (OUTPUT_SELECTED(p->ctl_a))
				ret = port_a_r(device);
			else
				ret = ddr_a_r(device);
			break;

		case 0x01:
			ret = control_a_r(device);
			break;

		case 0x02:
			if (OUTPUT_SELECTED(p->ctl_b))
				ret = port_b_r(device);
			else
				ret = ddr_b_r(device);
			break;

		case 0x03:
			ret = control_b_r(device);
			break;
	}

	return ret;
}


/*-------------------------------------------------
    pia_alt_r
-------------------------------------------------*/

READ8_DEVICE_HANDLER(pia_alt_r)
{
	return pia_r(device, ((offset << 1) & 0x02) | ((offset >> 1) & 0x01));
}


/*-------------------------------------------------
    pianew_get_port_b_z_mask
-------------------------------------------------*/

UINT8 pianew_get_port_b_z_mask(const device_config *device)
{
	pia6821 *p = get_token(device);
	return ~p->ddr_b;
}


/*-------------------------------------------------
    send_to_out_a_func
-------------------------------------------------*/

static void send_to_out_a_func(const device_config *device, const char* message)
{
	pia6821 *p = get_token(device);
	const pia6821_interface *intf = get_interface(device);

	/* input pins are pulled high */
	UINT8 data = get_out_a_value(device);

	LOG(("PIA #%s: %s = %02X\n", device->tag, message, data));

	if (intf->out_a_func)
		(*intf->out_a_func)(device, 0, data);
	else
	{
		if (p->out_a_needs_pulled)
			logerror("PIA #%s: Warning! No port A write handler. Previous value has been lost!\n", device->tag);

		p->out_a_needs_pulled = TRUE;
	}
}


/*-------------------------------------------------
    send_to_out_b_func
-------------------------------------------------*/

static void send_to_out_b_func(const device_config *device, const char* message)
{
	pia6821 *p = get_token(device);
	const pia6821_interface *intf = get_interface(device);

	/* input pins are high-impedance - we just send them as zeros for backwards compatibility */
	UINT8 data = get_out_b_value(device);

	LOG(("PIA #%s: %s = %02X\n", device->tag, message, data));

	if (intf->out_b_func)
		(*intf->out_b_func)(device, 0, data);
	else
	{
		if (p->out_b_needs_pulled)
			logerror("PIA #%s: Warning! No port B write handler. Previous value has been lost!\n", device->tag);

		p->out_b_needs_pulled = TRUE;
	}
}


/*-------------------------------------------------
    port_a_w
-------------------------------------------------*/

static void port_a_w(const device_config *device, UINT8 data)
{
	pia6821 *p = get_token(device);

	/* buffer the output value */
	p->out_a = data;

	send_to_out_a_func(device, "port A write");
}


/*-------------------------------------------------
    ddr_a_w
-------------------------------------------------*/

static void ddr_a_w(const device_config *device, UINT8 data)
{
	pia6821 *p = get_token(device);

	if (data == 0x00)
		LOG(("PIA #%s: DDR A write = %02X (input mode)\n", device->tag, data));
	else if (data == 0xff)
		LOG(("PIA #%s: DDR A write = %02X (output mode)\n", device->tag, data));
	else
		LOG(("PIA #%s: DDR A write = %02X (mixed mode)\n", device->tag, data));

	if (p->ddr_a != data)
	{
		/* DDR changed, call the callback again */
		p->ddr_a = data;
		p->logged_port_a_not_connected = FALSE;
		send_to_out_a_func(device, "port A write due to DDR change");
	}
}


/*-------------------------------------------------
    port_b_w
-------------------------------------------------*/

static void port_b_w(const device_config *device, UINT8 data)
{
	pia6821 *p = get_token(device);

	/* buffer the output value */
	p->out_b = data;

	send_to_out_b_func(device, "port B write");

	/* CB2 in write strobe mode */
	if (C2_STROBE_MODE(p->ctl_b))
	{
		/* this will cause a transition low */
		set_out_cb2(device, FALSE);

		/* if the CB2 strobe is cleared by the E, reset it right away */
		if (STROBE_E_RESET(p->ctl_b))
			set_out_cb2(device, TRUE);
	}
}


/*-------------------------------------------------
    ddr_b_w
-------------------------------------------------*/

static void ddr_b_w(const device_config *device, UINT8 data)
{
	pia6821 *p = get_token(device);

	if (data == 0x00)
		LOG(("PIA #%s: DDR B write = %02X (input mode)\n", device->tag, data));
	else if (data == 0xff)
		LOG(("PIA #%s: DDR B write = %02X (output mode)\n", device->tag, data));
	else
		LOG(("PIA #%s: DDR B write = %02X (mixed mode)\n", device->tag, data));

	if (p->ddr_b != data)
	{
		/* DDR changed, call the callback again */
		p->ddr_b = data;
		p->logged_port_b_not_connected = FALSE;
		send_to_out_b_func(device, "port B write due to DDR change");
	}
}


/*-------------------------------------------------
    control_a_w
-------------------------------------------------*/

static void control_a_w(const device_config *device, UINT8 data)
{
	pia6821 *p = get_token(device);

	/* bit 7 and 6 are read only */
	data &= 0x3f;

	LOG(("PIA #%s: control A write = %02X\n", device->tag, data));

	/* update the control register */
	p->ctl_a = data;

	/* CA2 is configured as output */
	if (C2_OUTPUT(p->ctl_a))
	{
		int temp;

		if (C2_SET_MODE(p->ctl_a))
			/* set/reset mode - bit value determines the new output */
			temp = C2_SET(p->ctl_a);
		else
			/* strobe mode - output is always high unless strobed */
			temp = TRUE;

		set_out_ca2(device, temp);
	}

	/* update externals */
	update_interrupts(device);
}


/*-------------------------------------------------
    control_b_w
-------------------------------------------------*/

static void control_b_w(const device_config *device, UINT8 data)
{
	pia6821 *p = get_token(device);
	int temp;

	/* bit 7 and 6 are read only */
	data &= 0x3f;

	LOG(("PIA #%s: control B write = %02X\n", device->tag, data));

	/* update the control register */
	p->ctl_b = data;

	if (C2_SET_MODE(p->ctl_b))
		/* set/reset mode - bit value determines the new output */
		temp = C2_SET(p->ctl_b);
	else
		/* strobe mode - output is always high unless strobed */
		temp = TRUE;

	set_out_cb2(device, temp);

	/* update externals */
	update_interrupts(device);
}


/*-------------------------------------------------
    pia_w
-------------------------------------------------*/

WRITE8_DEVICE_HANDLER(pia_w)
{
	pia6821 *p = get_token(device);

	switch (offset & 0x03)
	{
		default: /* impossible */
		case 0x00:
			if (OUTPUT_SELECTED(p->ctl_a))
				port_a_w(device, data);
			else
				ddr_a_w(device, data);
			break;

		case 0x01:
			control_a_w(device, data);
			break;

		case 0x02:
			if (OUTPUT_SELECTED(p->ctl_b))
				port_b_w(device, data);
			else
				ddr_b_w(device, data);
			break;

		case 0x03:
			control_b_w(device, data);
			break;
	}
}


/*-------------------------------------------------
    pia_alt_w
-------------------------------------------------*/

WRITE8_DEVICE_HANDLER(pia_alt_w)
{
	pia_w(device, ((offset << 1) & 0x02) | ((offset >> 1) & 0x01), data);
}


/*-------------------------------------------------
    pianew_set_port_a_z_mask
-------------------------------------------------*/

void pianew_set_port_a_z_mask(const device_config *device, UINT8 data)
{
	pia6821 *p = get_token(device);

	p->port_a_z_mask = data;
}


/*-------------------------------------------------
    pia_porta_r
-------------------------------------------------*/

READ8_DEVICE_HANDLER(pia_porta_r)
{
	pia6821 *p = get_token(device);

	return p->in_a;
}


/*-------------------------------------------------
    pianew_set_input_a
-------------------------------------------------*/

void pianew_set_input_a(const device_config *device, UINT8 data, UINT8 z_mask)
{
	pia6821 *p = get_token(device);
	const pia6821_interface *intf = get_interface(device);

	assert_always(intf->in_a_func == NULL, "pia_porta_w() called when in_a_func implemented");

	LOG(("PIA #%s: set input port A = %02X\n", device->tag, data));

	p->in_a = data;
	p->port_a_z_mask = z_mask;
	p->in_a_pushed = TRUE;
}


/*-------------------------------------------------
    pia_porta_w
-------------------------------------------------*/

WRITE8_DEVICE_HANDLER(pia_porta_w)
{
	pianew_set_input_a(device, data, 0);
}


/*-------------------------------------------------
    pianew_get_output_a
-------------------------------------------------*/

UINT8 pianew_get_output_a(const device_config *device)
{
	pia6821 *p = get_token(device);

	p->out_a_needs_pulled = FALSE;

	return get_out_a_value(device);
}


/*-------------------------------------------------
    pia_ca1_r
-------------------------------------------------*/

READ8_DEVICE_HANDLER(pia_ca1_r)
{
	pia6821 *p = get_token(device);

	return p->in_ca1;
}


/*-------------------------------------------------
    pia_ca1_w
-------------------------------------------------*/

WRITE8_DEVICE_HANDLER(pia_ca1_w)
{
	pia6821 *p = get_token(device);

	/* limit the data to 0 or 1 */
	data = data ? TRUE : FALSE;

	LOG(("PIA #%s: set input CA1 = %d\n", device->tag, data));

	/* the new state has caused a transition */
	if ((p->in_ca1 != data) &&
		((data && C1_LOW_TO_HIGH(p->ctl_a)) || (!data && C1_HIGH_TO_LOW(p->ctl_a))))
	{
		LOG(("PIA #%s: CA1 triggering\n", device->tag));

		/* mark the IRQ */
		p->irq_a1 = TRUE;

		/* update externals */
		update_interrupts(device);

		/* CA2 is configured as output and in read strobe mode and cleared by a CA1 transition */
		if (C2_OUTPUT(p->ctl_a) && C2_STROBE_MODE(p->ctl_a) && STROBE_C1_RESET(p->ctl_a))
			set_out_ca2(device, TRUE);
	}

	/* set the new value for CA1 */
	p->in_ca1 = data;
	p->in_ca1_pushed = TRUE;
}


/*-------------------------------------------------
    pia_ca2_r
-------------------------------------------------*/

READ8_DEVICE_HANDLER(pia_ca2_r)
{
	pia6821 *p = get_token(device);

	return p->in_ca2;
}


/*-------------------------------------------------
    pia_ca2_w
-------------------------------------------------*/

WRITE8_DEVICE_HANDLER(pia_ca2_w)
{
	pia6821 *p = get_token(device);

	/* limit the data to 0 or 1 */
	data = data ? 1 : 0;

	LOG(("PIA #%s: set input CA2 = %d\n", device->tag, data));

	/* if input mode and the new state has caused a transition */
	if (C2_INPUT(p->ctl_a) &&
		(p->in_ca2 != data) &&
		((data && C2_LOW_TO_HIGH(p->ctl_a)) || (!data && C2_HIGH_TO_LOW(p->ctl_a))))
	{
		LOG(("PIA #%s: CA2 triggering\n", device->tag));

		/* mark the IRQ */
		p->irq_a2 = TRUE;

		/* update externals */
		update_interrupts(device);
	}

	/* set the new value for CA2 */
	p->in_ca2 = data;
	p->in_ca2_pushed = TRUE;
}


/*-------------------------------------------------
    pianew_get_output_ca2
-------------------------------------------------*/

int pianew_get_output_ca2(const device_config *device)
{
	pia6821 *p = get_token(device);

	p->out_ca2_needs_pulled = FALSE;

	return p->out_ca2;
}


/*-------------------------------------------------
    pianew_get_output_ca2_z - version of
	pianew_get_output_ca2, which takes account of internal
	pullup resistor
-------------------------------------------------*/

int pianew_get_output_ca2_z(const device_config *device)
{
	pia6821 *p = get_token(device);

	p->out_ca2_needs_pulled = FALSE;

	// If it's an output, output the bit, if it's an input, it's
	// pulled up
	return p->out_ca2 |
	       C2_INPUT(p->ctl_a);
}


/*-------------------------------------------------
    pia_portb_r
-------------------------------------------------*/

READ8_DEVICE_HANDLER(pia_portb_r)
{
	pia6821 *p = get_token(device);

	return p->in_b;
}


/*-------------------------------------------------
    pia_portb_w
-------------------------------------------------*/

WRITE8_DEVICE_HANDLER(pia_portb_w)
{
	pia6821 *p = get_token(device);
	const pia6821_interface *intf = get_interface(device);

	assert_always(intf->in_b_func == NULL, "pia_set_input_b() called when in_b_func implemented");

	LOG(("PIA #%s: set input port B = %02X\n", device->tag, data));

	p->in_b = data;
	p->in_b_pushed = TRUE;
}


/*-------------------------------------------------
    pianew_get_output_b
-------------------------------------------------*/

UINT8 pianew_get_output_b(const device_config *device)
{
	pia6821 *p = get_token(device);

	p->out_b_needs_pulled = FALSE;

	return get_out_b_value(device);
}


/*-------------------------------------------------
    pia_cb1_r
-------------------------------------------------*/

READ8_DEVICE_HANDLER(pia_cb1_r)
{
	pia6821 *p = get_token(device);

	return p->in_cb1;
}


/*-------------------------------------------------
    pia_cb1_w
-------------------------------------------------*/

WRITE8_DEVICE_HANDLER(pia_cb1_w)
{
	pia6821 *p = get_token(device);

	/* limit the data to 0 or 1 */
	data = data ? 1 : 0;

	LOG(("PIA #%s: set input CB1 = %d\n", device->tag, data));

	/* the new state has caused a transition */
	if ((p->in_cb1 != data) &&
		((data && C1_LOW_TO_HIGH(p->ctl_b)) || (!data && C1_HIGH_TO_LOW(p->ctl_b))))
	{
		LOG(("PIA #%s: CB1 triggering\n", device->tag));

		/* mark the IRQ */
		p->irq_b1 = 1;

		/* update externals */
		update_interrupts(device);

		/* If CB2 is configured as a write-strobe output which is reset by a CB1
           transition, this reset will only happen when a read from port B implicitly
           clears the IRQ B1 flag.  So we handle the CB2 reset there.  Note that this
           is different from what happens with port A. */
	}

	/* set the new value for CB1 */
	p->in_cb1 = data;
	p->in_cb1_pushed = TRUE;
}


/*-------------------------------------------------
    pia_cb2_r
-------------------------------------------------*/

READ8_DEVICE_HANDLER(pia_cb2_r)
{
	pia6821 *p = get_token(device);

	return p->in_cb2;
}


/*-------------------------------------------------
    pia_cb2_w
-------------------------------------------------*/

WRITE8_DEVICE_HANDLER(pia_cb2_w)
{
	pia6821 *p = get_token(device);

	/* limit the data to 0 or 1 */
	data = data ? 1 : 0;

	LOG(("PIA #%s: set input CB2 = %d\n", device->tag, data));

	/* if input mode and the new state has caused a transition */
	if (C2_INPUT(p->ctl_b) &&
		(p->in_cb2 != data) &&
		((data && C2_LOW_TO_HIGH(p->ctl_b)) || (!data && C2_HIGH_TO_LOW(p->ctl_b))))
	{
		LOG(("PIA #%s: CB2 triggering\n", device->tag));

		/* mark the IRQ */
		p->irq_b2 = 1;

		/* update externals */
		update_interrupts(device);
	}

	/* set the new value for CA2 */
	p->in_cb2 = data;
	p->in_cb2_pushed = TRUE;
}


/*-------------------------------------------------
    pianew_get_output_cb2
-------------------------------------------------*/

int pianew_get_output_cb2(const device_config *device)
{
	pia6821 *p = get_token(device);

	p->out_cb2_needs_pulled = FALSE;

	return p->out_cb2;
}


/*-------------------------------------------------
    pianew_get_output_cb2_z
-------------------------------------------------*/

int pianew_get_output_cb2_z(const device_config *device)
{
	pia6821 *p = get_token(device);

	return !C2_OUTPUT(p->ctl_b);
}


/*-------------------------------------------------
    pianew_get_irq_a
-------------------------------------------------*/

int pianew_get_irq_a(const device_config *device)
{
	pia6821 *p = get_token(device);

	return p->irq_a_state;
}


/*-------------------------------------------------
    pianew_get_irq_b
-------------------------------------------------*/

int pianew_get_irq_b(const device_config *device)
{
	pia6821 *p = get_token(device);

	return p->irq_b_state;
}


/*-------------------------------------------------
    DEVICE_SET_INFO( pia )
-------------------------------------------------*/

static DEVICE_SET_INFO( pia )
{
	switch(state)
	{
	}
}


/*-------------------------------------------------
    DEVICE_GET_INFO( pia6821 )
-------------------------------------------------*/

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

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

		/* --- the following bits of info are returned as NULL-terminated strings --- */
		case DEVINFO_STR_NAME:							strcpy(info->s, "6821 PIA");				break;
		case DEVINFO_STR_FAMILY:						strcpy(info->s, "6821 PIA");				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:						/* Nothing */								break;
	}
}


/*-------------------------------------------------
    DEVICE_GET_INFO( pia6822 )
-------------------------------------------------*/

DEVICE_GET_INFO(pia6822)
{
	switch (state)
	{
		case DEVINFO_STR_NAME:							strcpy(info->s, "6822 PIA");				break;
		default: 										DEVICE_GET_INFO_CALL(pia6821);				break;
	}
}