summaryrefslogtreecommitdiffstatshomepage
path: root/src/mess/machine/pet.c
blob: c5b9f4aa0ffd9d78b3ded3c536691c99c65d470b (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
/***************************************************************************
    commodore pet series computer

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

#include "emu.h"
#include "cpu/m6502/m6502.h"
#include "cpu/m6809/m6809.h"

#include "machine/6821pia.h"
#include "machine/6522via.h"
#include "machine/ieee488.h"

#include "includes/pet.h"

#include "imagedev/cartslot.h"
#include "imagedev/cassette.h"
#include "machine/ram.h"

#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)



/* pia at 0xe810
   port a
    7 sense input (low for diagnostics)
    6 ieee eoi in
    5 cassette 2 switch in
    4 cassette 1 switch in
    3..0 keyboard line select

  ca1 cassette 1 read
  ca2 ieee eoi out

  cb1 video sync in
  cb2 cassette 1 motor out
*/
READ8_MEMBER(pet_state::pia0_pa_r)
{
	/*

        bit     description

        PA0     KEY A
        PA1     KEY B
        PA2     KEY C
        PA3     KEY D
        PA4     #1 CASS SWITCH
        PA5     #2 CASS SWITCH
        PA6     _EOI IN
        PA7     DIAG JUMPER

    */

	UINT8 data = 0;

	/* key */
	data |= m_keyline_select;

	/* #1 cassette switch */
	data |= ((machine().device<cassette_image_device>(CASSETTE_TAG)->get_state() & CASSETTE_MASK_UISTATE) == CASSETTE_STOPPED) << 4;

	/* #2 cassette switch */
	data |= ((machine().device<cassette_image_device>(CASSETTE2_TAG)->get_state() & CASSETTE_MASK_UISTATE) == CASSETTE_STOPPED) << 5;

	/* end or identify in */
	data |= m_ieee->eoi_r() << 6;

	/* diagnostic jumper */
	data |= 0x80;

	return data;
}

WRITE8_MEMBER(pet_state::pia0_pa_w)
{
	/*

        bit     description

        PA0     KEY A
        PA1     KEY B
        PA2     KEY C
        PA3     KEY D
        PA4     #1 CASS SWITCH
        PA5     #2 CASS SWITCH
        PA6     _EOI IN
        PA7     DIAG JUMPER

    */

	/* key */
	m_keyline_select = data & 0x0f;
}

/* Keyboard reading/handling for regular keyboard */
READ8_MEMBER(pet_state::kin_r)
{
	/*

        bit     description

        PB0     KIN0
        PB1     KIN1
        PB2     KIN2
        PB3     KIN3
        PB4     KIN4
        PB5     KIN5
        PB6     KIN6
        PB7     KIN7

    */

	UINT8 data = 0xff;
	static const char *const keynames[] = {
		"ROW0", "ROW1", "ROW2", "ROW3", "ROW4",
		"ROW5", "ROW6", "ROW7", "ROW8", "ROW9"
	};

	if (m_keyline_select < 10)
	{
		data = machine().root_device().ioport(keynames[m_keyline_select])->read();
		/* Check for left-shift lock */
		if ((m_keyline_select == 8) && (machine().root_device().ioport("SPECIAL")->read() & 0x80))
			data &= 0xfe;
	}
	return data;
}

/* Keyboard handling for business keyboard */
READ8_MEMBER(pet_state::petb_kin_r)
{
	UINT8 data = 0xff;
	static const char *const keynames[] = {
		"ROW0", "ROW1", "ROW2", "ROW3", "ROW4",
		"ROW5", "ROW6", "ROW7", "ROW8", "ROW9"
	};

	if (m_keyline_select < 10)
	{
		data = machine().root_device().ioport(keynames[m_keyline_select])->read();
		/* Check for left-shift lock */
		/* 2008-05 FP: For some reason, superpet read it in the opposite way!! */
		/* While waiting for confirmation from docs, we add a workaround here. */
		if (m_superpet)
		{
			if ((m_keyline_select == 6) && !(machine().root_device().ioport("SPECIAL")->read() & 0x80))
				data &= 0xfe;
		}
		else
		{
			if ((m_keyline_select == 6) && (machine().root_device().ioport("SPECIAL")->read() & 0x80))
				data &= 0xfe;
		}
	}
	return data;
}

READ8_MEMBER(pet_state::cass1_r)
{
	// cassette 1 read
	return (machine().device<cassette_image_device>(CASSETTE_TAG)->input() > +0.0) ? 1 : 0;
}

WRITE8_MEMBER(pet_state::cass1_motor_w)
{
	if (!data)
	{
		machine().device<cassette_image_device>(CASSETTE_TAG)->change_state(CASSETTE_MOTOR_ENABLED,CASSETTE_MASK_MOTOR);
		m_datasette1_timer->adjust(attotime::zero, 0, attotime::from_hz(48000));	// I put 48000 because I was given some .wav with this freq
	}
	else
	{
		machine().device<cassette_image_device>(CASSETTE_TAG)->change_state(CASSETTE_MOTOR_DISABLED ,CASSETTE_MASK_MOTOR);
		m_datasette1_timer->reset();
	}
}

WRITE_LINE_MEMBER(pet_state::pia0_irq_w)
{
	m_pia0_irq = state;
	int level = (m_pia0_irq | m_pia1_irq | m_via_irq) ? ASSERT_LINE : CLEAR_LINE;

	machine().firstcpu->set_input_line(INPUT_LINE_IRQ0, level);
}

const pia6821_interface pet_pia0 =
{
	DEVCB_DRIVER_MEMBER(pet_state,pia0_pa_r),		/* in_a_func */
	DEVCB_DRIVER_MEMBER(pet_state,kin_r),			/* in_b_func */
	DEVCB_DRIVER_MEMBER(pet_state,cass1_r),			/* in_ca1_func */
	DEVCB_NULL,						/* in_cb1_func */
	DEVCB_NULL,						/* in_ca2_func */
	DEVCB_NULL,						/* in_cb2_func */
	DEVCB_DRIVER_MEMBER(pet_state,pia0_pa_w),		/* out_a_func */
	DEVCB_NULL,						/* out_b_func */
	DEVCB_DEVICE_LINE_MEMBER(IEEE488_TAG, ieee488_device, eoi_w),			/* out_ca2_func */
	DEVCB_DRIVER_MEMBER(pet_state,cass1_motor_w),	/* out_cb2_func */
	DEVCB_DRIVER_LINE_MEMBER(pet_state,pia0_irq_w),			/* irq_a_func */
	DEVCB_DRIVER_LINE_MEMBER(pet_state,pia0_irq_w)			/* irq_b_func */
};

const pia6821_interface petb_pia0 =
{
	DEVCB_DRIVER_MEMBER(pet_state,pia0_pa_r),		/* in_a_func */
	DEVCB_DRIVER_MEMBER(pet_state,petb_kin_r),		/* in_b_func */
	DEVCB_DRIVER_MEMBER(pet_state,cass1_r),			/* in_ca1_func */
	DEVCB_NULL,						/* in_cb1_func */
	DEVCB_NULL,						/* in_ca2_func */
	DEVCB_NULL,						/* in_cb2_func */
	DEVCB_DRIVER_MEMBER(pet_state,pia0_pa_w),		/* out_a_func */
	DEVCB_NULL,						/* out_b_func */
	DEVCB_DEVICE_LINE_MEMBER(IEEE488_TAG, ieee488_device, eoi_w),			/* out_ca2_func */
	DEVCB_DRIVER_MEMBER(pet_state,cass1_motor_w),	/* out_cb2_func */
	DEVCB_DRIVER_LINE_MEMBER(pet_state,pia0_irq_w),			/* irq_a_func */
	DEVCB_DRIVER_LINE_MEMBER(pet_state,pia0_irq_w)			/* irq_b_func */
};

/* pia at 0xe820 (ieee488)
   port a data in
   port b data out
  ca1 atn in
  ca2 ndac out
  cb1 srq in
  cb2 dav out
 */

WRITE_LINE_MEMBER(pet_state::pia1_irq_w)
{
	m_pia1_irq = state;
	int level = (m_pia0_irq | m_pia1_irq | m_via_irq) ? ASSERT_LINE : CLEAR_LINE;

	machine().firstcpu->set_input_line(INPUT_LINE_IRQ0, level);
}

const pia6821_interface pet_pia1 =
{
	DEVCB_DEVICE_MEMBER(IEEE488_TAG, ieee488_device, dio_r),/* in_a_func */
	DEVCB_NULL,								/* in_b_func */
	DEVCB_DEVICE_LINE_MEMBER(IEEE488_TAG, ieee488_device, atn_r),	/* in_ca1_func */
	DEVCB_DEVICE_LINE_MEMBER(IEEE488_TAG, ieee488_device, srq_r),	/* in_cb1_func */
	DEVCB_NULL,								/* in_ca2_func */
	DEVCB_NULL,								/* in_cb2_func */
	DEVCB_NULL,								/* out_a_func */
	DEVCB_DEVICE_MEMBER(IEEE488_TAG, ieee488_device, dio_w),					/* out_b_func */
	DEVCB_DEVICE_LINE_MEMBER(IEEE488_TAG, ieee488_device, ndac_w),					/* out_ca2_func */
	DEVCB_DEVICE_LINE_MEMBER(IEEE488_TAG, ieee488_device, dav_w),					/* out_cb2_func */
	DEVCB_DRIVER_LINE_MEMBER(pet_state,pia1_irq_w),					/* irq_a_func */
	DEVCB_DRIVER_LINE_MEMBER(pet_state,pia1_irq_w)					/* irq_b_func */
};

/* userport, cassettes, rest ieee488
   ca1 userport
   ca2 character rom address line 11
   pa user port

   pb0 ieee ndac in
   pb1 ieee nrfd out
   pb2 ieee atn out
   pb3 userport/cassettes
   pb4 cassettes
   pb5 userport/???
   pb6 ieee nrfd in
   pb7 ieee dav in

   cb1 cassettes
   cb2 user port
 */
READ8_MEMBER(pet_state::via_pb_r)
{
	/*

        bit     description

        PB0     _NDAC IN
        PB1     _NRFD OUT
        PB2     _ATN OUT
        PB3     CASS WRITE
        PB4     #2 CASS MOTOR
        PB5     SYNC IN
        PB6     _NRFD IN
        PB7     _DAV IN

    */

	UINT8 data = 0;

	/* not data accepted in */
	data |= m_ieee->ndac_r();

	/* sync in */

	/* not ready for data in */
	data |= m_ieee->nrfd_r() << 6;

	/* data valid in */
	data |= m_ieee->dav_r() << 7;

	return data;
}

READ_LINE_MEMBER(pet_state::cass2_r)
{
	// cassette 2 read
	return (machine().device<cassette_image_device>(CASSETTE2_TAG)->input() > +0.0) ? 1 : 0;
}

WRITE8_MEMBER(pet_state::via_pb_w)
{
	/*

        bit     description

        PB0     _NDAC IN
        PB1     _NRFD OUT
        PB2     _ATN OUT
        PB3     CASS WRITE
        PB4     #2 CASS MOTOR
        PB5     SYNC IN
        PB6     _NRFD IN
        PB7     _DAV IN

    */

	/* not ready for data out */
	m_ieee->nrfd_w(BIT(data, 1));

	/* attention out */
	m_ieee->atn_w(BIT(data, 2));

	/* cassette write */
	machine().device<cassette_image_device>(CASSETTE_TAG)->output(BIT(data, 3) ? -(0x5a9e >> 1) : +(0x5a9e >> 1));
	machine().device<cassette_image_device>(CASSETTE2_TAG)->output(BIT(data, 3) ? -(0x5a9e >> 1) : +(0x5a9e >> 1));

	/* #2 cassette motor */
	if (BIT(data, 4))
	{
		machine().device<cassette_image_device>(CASSETTE2_TAG)->change_state(CASSETTE_MOTOR_ENABLED, CASSETTE_MASK_MOTOR);
		m_datasette2_timer->adjust(attotime::zero, 0, attotime::from_hz(48000));	// I put 48000 because I was given some .wav with this freq
	}
	else
	{
		machine().device<cassette_image_device>(CASSETTE2_TAG)->change_state(CASSETTE_MOTOR_DISABLED, CASSETTE_MASK_MOTOR);
		m_datasette2_timer->reset();
	}
}

WRITE_LINE_MEMBER(pet_state::gb_w)
{
	DBG_LOG(machine(), 1, "address line", ("%d\n", state));
	if (state) m_font |= 1;
	else m_font &= ~1;
}

WRITE_LINE_MEMBER(pet_state::via_irq_w)
{
	m_via_irq = state;
	int level = (m_pia0_irq | m_pia1_irq | m_via_irq) ? ASSERT_LINE : CLEAR_LINE;

	machine().firstcpu->set_input_line(INPUT_LINE_IRQ0, level);
}

const via6522_interface pet_via =
{
	DEVCB_NULL,					/* in_a_func */
	DEVCB_DRIVER_MEMBER(pet_state,via_pb_r),	/* in_b_func */
	DEVCB_NULL,					/* in_ca1_func */
	DEVCB_DRIVER_LINE_MEMBER(pet_state,cass2_r),		/* in_cb1_func */
	DEVCB_NULL,					/* in_ca2_func */
	DEVCB_NULL,					/* in_cb2_func */
	DEVCB_NULL,					/* out_a_func */
	DEVCB_DRIVER_MEMBER(pet_state,via_pb_w),	/* out_b_func */
	DEVCB_NULL,					/* out_ca1_func */
	DEVCB_DRIVER_LINE_MEMBER(pet_state,gb_w),			/* out_ca2_func */
	DEVCB_NULL,					/* out_ca2_func */
	DEVCB_NULL,					/* out_cb2_func */
	DEVCB_DRIVER_LINE_MEMBER(pet_state,via_irq_w)		/* out_irq_func */
};


static WRITE8_HANDLER( cbm8096_io_w )
{
	via6522_device *via_0 = space.machine().device<via6522_device>("via6522_0");
	pia6821_device *pia_0 = space.machine().device<pia6821_device>("pia_0");
	pia6821_device *pia_1 = space.machine().device<pia6821_device>("pia_1");
	mc6845_device *mc6845 = space.machine().device<mc6845_device>("crtc");

	if (offset < 0x10) ;
	else if (offset < 0x14) pia_0->write(space, offset & 3, data);
	else if (offset < 0x20) ;
	else if (offset < 0x24) pia_1->write(space, offset & 3, data);
	else if (offset < 0x40) ;
	else if (offset < 0x50) via_0->write(space, offset & 0xf, data);
	else if (offset < 0x80) ;
	else if (offset == 0x80) mc6845->address_w(space, 0, data);
	else if (offset == 0x81) mc6845->register_w(space, 0, data);
}

static READ8_HANDLER( cbm8096_io_r )
{
	via6522_device *via_0 = space.machine().device<via6522_device>("via6522_0");
	pia6821_device *pia_0 = space.machine().device<pia6821_device>("pia_0");
	pia6821_device *pia_1 = space.machine().device<pia6821_device>("pia_1");
	mc6845_device *mc6845 = space.machine().device<mc6845_device>("crtc");

	int data = 0xff;
	if (offset < 0x10) ;
	else if (offset < 0x14) data = pia_0->read(space, offset & 3);
	else if (offset < 0x20) ;
	else if (offset < 0x24) data = pia_1->read(space, offset & 3);
	else if (offset < 0x40) ;
	else if (offset < 0x50) data = via_0->read(space, offset & 0xf);
	else if (offset < 0x80) ;
	else if (offset == 0x81) data = mc6845->register_r(space, 0);
	return data;
}

static WRITE8_HANDLER( pet80_bank1_w )
{
	pet_state *state = space.machine().driver_data<pet_state>();
	state->m_pet80_bank1_base[offset] = data;
}

/*
65520        8096 memory control register
        bit 0    1=write protect $8000-BFFF
        bit 1    1=write protect $C000-FFFF
        bit 2    $8000-BFFF bank select
        bit 3    $C000-FFFF bank select
        bit 5    1=screen peek through
        bit 6    1=I/O peek through
        bit 7    1=enable expansion memory

*/
WRITE8_HANDLER( cbm8096_w )
{
	pet_state *state = space.machine().driver_data<pet_state>();
	if (data & 0x80)
	{
		if (data & 0x40)
		{
			space.install_legacy_read_handler(0xe800, 0xefff, FUNC(cbm8096_io_r));
			space.install_legacy_write_handler(0xe800, 0xefff, FUNC(cbm8096_io_w));
		}
		else
		{
			space.install_read_bank(0xe800, 0xefff, "bank7");
			if (!(data & 2))
				space.install_write_bank(0xe800, 0xefff, "bank7");
			else
				space.nop_write(0xe800, 0xefff);
		}


		if ((data & 2) == 0) {
			space.install_write_bank(0xc000, 0xe7ff, "bank6");
			space.install_write_bank(0xf000, 0xffef, "bank8");
			space.install_write_bank(0xfff1, 0xffff, "bank9");
		} else {
			space.nop_write(0xc000, 0xe7ff);
			space.nop_write(0xf000, 0xffef);
			space.nop_write(0xfff1, 0xffff);
		}

		if (data & 0x20)
		{
			state->m_pet80_bank1_base = state->m_memory + 0x8000;
			state->membank("bank1")->set_base(state->m_pet80_bank1_base);
			space.install_legacy_write_handler(0x8000, 0x8fff, FUNC(pet80_bank1_w));
		}
		else
		{
			if (!(data & 1))
				space.install_write_bank(0x8000, 0x8fff, "bank1");
			else
				space.nop_write(0x8000, 0x8fff);
		}

		if ((data & 1) == 0 ){
			space.install_write_bank(0x9000, 0x9fff, "bank2");
			space.install_write_bank(0xa000, 0xafff, "bank3");
			space.install_write_bank(0xb000, 0xbfff, "bank4");
		} else {
			space.nop_write(0x9000, 0x9fff);
			space.nop_write(0xa000, 0xafff);
			space.nop_write(0xb000, 0xbfff);
		}

		if (data & 4)
		{
			if (!(data & 0x20))
			{
				state->m_pet80_bank1_base = state->m_memory + 0x14000;
				state->membank("bank1")->set_base(state->m_pet80_bank1_base);
			}
			state->membank("bank2")->set_base(state->m_memory + 0x15000);
			state->membank("bank3")->set_base(state->m_memory + 0x16000);
			state->membank("bank4")->set_base(state->m_memory + 0x17000);
		}
		else
		{
			if (!(data & 0x20))
			{
				state->m_pet80_bank1_base = state->m_memory + 0x10000;
				state->membank("bank1")->set_base(state->m_pet80_bank1_base);
			}
			state->membank("bank2")->set_base(state->m_memory + 0x11000);
			state->membank("bank3")->set_base(state->m_memory + 0x12000);
			state->membank("bank4")->set_base(state->m_memory + 0x13000);
		}

		if (data & 8)
		{
			if (!(data & 0x40))
			{
				state->membank("bank7")->set_base(state->m_memory + 0x1e800);
			}
			state->membank("bank6")->set_base(state->m_memory + 0x1c000);
			state->membank("bank8")->set_base(state->m_memory + 0x1f000);
			state->membank("bank9")->set_base(state->m_memory + 0x1fff1);
		}
		else
		{
			if (!(data & 0x40))
			{
				state->membank("bank7")->set_base(state->m_memory+ 0x1a800);
			}
			state->membank("bank6")->set_base(state->m_memory + 0x18000);
			state->membank("bank8")->set_base(state->m_memory + 0x1b000);
			state->membank("bank9")->set_base(state->m_memory + 0x1bff1);
		}
	}
	else
	{
		state->m_pet80_bank1_base = state->m_memory + 0x8000;
		state->membank("bank1")->set_base(state->m_pet80_bank1_base );
		space.install_legacy_write_handler(0x8000, 0x8fff, FUNC(pet80_bank1_w));

		state->membank("bank2")->set_base(state->m_memory + 0x9000);
		space.unmap_write(0x9000, 0x9fff);

		state->membank("bank3")->set_base(state->m_memory + 0xa000);
		space.unmap_write(0xa000, 0xafff);

		state->membank("bank4")->set_base(state->m_memory + 0xb000);
		space.unmap_write(0xb000, 0xbfff);

		state->membank("bank6")->set_base(state->m_memory + 0xc000);
		space.unmap_write(0xc000, 0xe7ff);

		space.install_legacy_read_handler(0xe800, 0xefff, FUNC(cbm8096_io_r));
		space.install_legacy_write_handler(0xe800, 0xefff, FUNC(cbm8096_io_w));

		state->membank("bank8")->set_base(state->m_memory + 0xf000);
		space.unmap_write(0xf000, 0xffef);

		state->membank("bank9")->set_base(state->m_memory + 0xfff1);
		space.unmap_write(0xfff1, 0xffff);
	}
}

READ8_HANDLER( superpet_r )
{
	return 0xff;
}

WRITE8_HANDLER( superpet_w )
{
	pet_state *state = space.machine().driver_data<pet_state>();
	switch (offset)
	{
		case 0:
		case 1:
		case 2:
		case 3:
			/* 3: 1 pull down diagnostic pin on the userport
               1: 1 if jumpered programable ram r/w
               0: 0 if jumpered programable m6809, 1 m6502 selected */
			break;

		case 4:
		case 5:
			state->m_spet.bank = data & 0xf;
			state->membank("bank1")->configure_entries(0, 16, state->m_supermemory, 0x1000);
			state->membank("bank1")->set_entry(state->m_spet.bank);
			/* 7 low writeprotects systemlatch */
			break;

		case 6:
		case 7:
			state->m_spet.rom = data & 1;
			break;
	}
}

TIMER_CALLBACK_MEMBER(pet_state::pet_interrupt)
{
	pia6821_device *pia_0 = machine().device<pia6821_device>("pia_0");

	pia_0->cb1_w(m_pia_level);
	m_pia_level = !m_pia_level;
}


TIMER_CALLBACK_MEMBER(pet_state::pet_tape1_timer)
{
	pia6821_device *pia_0 = machine().device<pia6821_device>("pia_0");
//  cassette 1
	UINT8 data = (machine().device<cassette_image_device>(CASSETTE_TAG)->input() > +0.0) ? 1 : 0;
	pia_0->ca1_w(data);
}

TIMER_CALLBACK_MEMBER(pet_state::pet_tape2_timer)
{
	via6522_device *via_0 = machine().device<via6522_device>("via6522_0");
//  cassette 2
	UINT8 data = (machine().device<cassette_image_device>(CASSETTE2_TAG)->input() > +0.0) ? 1 : 0;
	via_0->write_cb1(data);
}


static void pet_common_driver_init( running_machine &machine )
{
	int i;
	pet_state *state = machine.driver_data<pet_state>();

	state->m_font = 0;

	state->m_pet_basic1 = 0;
	state->m_superpet = 0;
	state->m_cbm8096 = 0;

	machine.device("maincpu")->memory().space(AS_PROGRAM).install_readwrite_bank(0x0000, machine.device<ram_device>(RAM_TAG)->size() - 1, "bank10");
	state->membank("bank10")->set_base(state->m_memory);

	if (machine.device<ram_device>(RAM_TAG)->size() < 0x8000)
	{
		machine.device("maincpu")->memory().space(AS_PROGRAM).nop_readwrite(machine.device<ram_device>(RAM_TAG)->size(), 0x7FFF);
	}

	/* 2114 poweron ? 64 x 0xff, 64x 0, and so on */
	for (i = 0; i < machine.device<ram_device>(RAM_TAG)->size(); i += 0x40)
	{
		memset (state->m_memory + i, i & 0x40 ? 0 : 0xff, 0x40);
	}

	/* pet clock */
	machine.scheduler().timer_pulse(attotime::from_msec(10), timer_expired_delegate(FUNC(pet_state::pet_interrupt),state));

	/* datasette */
	state->m_datasette1_timer = machine.scheduler().timer_alloc(timer_expired_delegate(FUNC(pet_state::pet_tape1_timer),state));
	state->m_datasette2_timer = machine.scheduler().timer_alloc(timer_expired_delegate(FUNC(pet_state::pet_tape2_timer),state));
}


DRIVER_INIT_MEMBER(pet_state,pet2001)
{
	m_memory.set_target(machine().device<ram_device>(RAM_TAG)->pointer(),m_memory.bytes());
	pet_common_driver_init(machine());
	m_pet_basic1 = 1;
	pet_vh_init(machine());
}

DRIVER_INIT_MEMBER(pet_state,pet)
{
	m_memory.set_target(machine().device<ram_device>(RAM_TAG)->pointer(),m_memory.bytes());
	pet_common_driver_init(machine());
	pet_vh_init(machine());
}

DRIVER_INIT_MEMBER(pet_state,pet80)
{
	m_memory.set_target(memregion("maincpu")->base(),m_memory.bytes());

	pet_common_driver_init(machine());
	m_cbm8096 = 1;
	m_videoram.set_target(&m_memory[0x8000],m_videoram.bytes());
	pet80_vh_init(machine());

}

DRIVER_INIT_MEMBER(pet_state,superpet)
{
	m_memory.set_target(machine().device<ram_device>(RAM_TAG)->pointer(),m_memory.bytes());
	pet_common_driver_init(machine());
	m_superpet = 1;

	m_supermemory = auto_alloc_array(machine(), UINT8, 0x10000);

	membank("bank1")->configure_entries(0, 16, m_supermemory, 0x1000);
	membank("bank1")->set_entry(0);

	superpet_vh_init(machine());
}

void pet_state::machine_reset()
{

	if (m_superpet)
	{
		m_spet.rom = 0;
		if (machine().root_device().ioport("CFG")->read() & 0x04)
		{
			machine().device("maincpu")->execute().set_input_line(INPUT_LINE_HALT, 1);
			machine().device("maincpu")->execute().set_input_line(INPUT_LINE_HALT, 0);
			m_font = 2;
		}
		else
		{
			machine().device("maincpu")->execute().set_input_line(INPUT_LINE_HALT, 0);
			machine().device("maincpu")->execute().set_input_line(INPUT_LINE_HALT, 1);
			m_font = 0;
		}
	}

	if (m_cbm8096)
	{
		if (machine().root_device().ioport("CFG")->read() & 0x08)
		{
			machine().device("maincpu")->memory().space(AS_PROGRAM).install_legacy_write_handler(0xfff0, 0xfff0, FUNC(cbm8096_w));
		}
		else
		{
			machine().device("maincpu")->memory().space(AS_PROGRAM).nop_write(0xfff0, 0xfff0);
		}
		cbm8096_w(machine().device("maincpu")->memory().space(AS_PROGRAM), 0, 0);
	}

//removed   cbm_drive_0_config (machine().root_device().ioport("CFG")->read() & 2 ? IEEE : 0, 8);
//removed   cbm_drive_1_config (machine().root_device().ioport("CFG")->read() & 1 ? IEEE : 0, 9);
	machine().device("maincpu")->reset();

	m_ieee->ren_w(0);
	m_ieee->ifc_w(0);
	m_ieee->ifc_w(1);
}


INTERRUPT_GEN_MEMBER(pet_state::pet_frame_interrupt)
{
	if (m_superpet)
	{
		if (ioport("CFG")->read() & 0x04)
		{
			device.execute().set_input_line(INPUT_LINE_HALT, 1);
			device.execute().set_input_line(INPUT_LINE_HALT, 0);
			m_font |= 2;
		}
		else
		{
			device.execute().set_input_line(INPUT_LINE_HALT, 0);
			device.execute().set_input_line(INPUT_LINE_HALT, 1);
			m_font &= ~2;
		}
	}

	set_led_status (machine(),1, machine().root_device().ioport("SPECIAL")->read() & 0x80 ? 1 : 0);		/* Shift Lock */
}


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

    PET Cartridges

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

static DEVICE_IMAGE_LOAD(pet_cart)
{
	pet_state *state = image.device().machine().driver_data<pet_state>();
	UINT32 size = image.length();
	const char *filetype = image.filetype();
	int address = 0;

	/* Assign loading address according to extension */
	if (!mame_stricmp(filetype, "90"))
		address = 0x9000;
	else if (!mame_stricmp(filetype, "a0"))
		address = 0xa000;
	else if (!mame_stricmp(filetype, "b0"))
		address = 0xb000;

	logerror("Loading cart %s at %.4x size:%.4x\n", image.filename(), address, size);

	image.fread(state->m_memory + address, size);

	return IMAGE_INIT_PASS;
}

MACHINE_CONFIG_FRAGMENT(pet_cartslot)
	MCFG_CARTSLOT_ADD("cart1")
	MCFG_CARTSLOT_EXTENSION_LIST("90,a0,b0")
	MCFG_CARTSLOT_NOT_MANDATORY
	MCFG_CARTSLOT_LOAD(pet_cart)

	MCFG_CARTSLOT_ADD("cart2")
	MCFG_CARTSLOT_EXTENSION_LIST("90,a0,b0")
	MCFG_CARTSLOT_NOT_MANDATORY
	MCFG_CARTSLOT_LOAD(pet_cart)
MACHINE_CONFIG_END

// 2010-08, FP: this is used by CBM40 & CBM80, and I actually have only found .prg files for these... does cart dumps exist?
MACHINE_CONFIG_FRAGMENT(pet4_cartslot)
	MCFG_CARTSLOT_MODIFY("cart1")
	MCFG_CARTSLOT_EXTENSION_LIST("a0")
	MCFG_CARTSLOT_NOT_MANDATORY
	MCFG_CARTSLOT_LOAD(pet_cart)

	MCFG_CARTSLOT_MODIFY("cart2")
	MCFG_CARTSLOT_EXTENSION_LIST("a0")
	MCFG_CARTSLOT_NOT_MANDATORY
	MCFG_CARTSLOT_LOAD(pet_cart)
MACHINE_CONFIG_END