summaryrefslogtreecommitdiffstatshomepage
path: root/src/mess/machine/mbee.c
blob: e94dfbcd9f510ffce6604367dc92da2d46f9b345 (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
/***************************************************************************

    microbee.c

    machine driver
    Juergen Buchmueller <pullmoll@t-online.de>, Jan 2000


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

#include "emu.h"
#include "includes/mbee.h"
#include "machine/z80bin.h"

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

    PIO

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

WRITE_LINE_MEMBER( mbee_state::pio_ardy )
{
	m_printer->strobe_w((state) ? 0 : 1);
}

WRITE8_MEMBER( mbee_state::pio_port_a_w )
{
	/* hardware strobe driven by PIO ARDY, bit 7..0 = data */
	m_pio->strobe_a(1); /* needed - otherwise nothing prints */
	m_printer->write(space, 0, data);
};

WRITE8_MEMBER( mbee_state::pio_port_b_w )
{
/*  PIO port B - d5..d2 not emulated
    d7 network interrupt (microbee network for classrooms)
    d6 speaker
    d5 rs232 output (1=mark)
    d4 rs232 input (0=mark)
    d3 rs232 CTS (0=clear to send)
    d2 rs232 clock or DTR
    d1 cass out and (on 256tc) keyboard irq
    d0 cass in */

	m_cassette->output((data & 0x02) ? -1.0 : +1.0);

	speaker_level_w(m_speaker, BIT(data, 6));
};

READ8_MEMBER( mbee_state::pio_port_b_r )
{
	UINT8 data = 0;

	if (m_cassette->input() > 0.03) data |= 1;

	data |= m_clock_pulse;
	data |= m_mbee256_key_available;

	m_clock_pulse = 0;

	return data;
};

const z80pio_interface mbee_z80pio_intf =
{
	DEVCB_CPU_INPUT_LINE("maincpu", INPUT_LINE_IRQ0),
	DEVCB_NULL,
	DEVCB_DRIVER_MEMBER(mbee_state, pio_port_a_w),
	DEVCB_DRIVER_LINE_MEMBER(mbee_state, pio_ardy),
	DEVCB_DRIVER_MEMBER(mbee_state, pio_port_b_r),
	DEVCB_DRIVER_MEMBER(mbee_state, pio_port_b_w),
	DEVCB_NULL
};

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

    Floppy DIsk

    The callback is quite simple, no interrupts are used.
    If either IRQ or DRQ activate, they set bit 7 of inport 0x48.

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

WRITE_LINE_MEMBER( mbee_state::mbee_fdc_intrq_w )
{
	m_fdc_intrq = state ? 0x80 : 0;
}

WRITE_LINE_MEMBER( mbee_state::mbee_fdc_drq_w )
{
	m_fdc_drq = state ? 0x80 : 0;
}

const wd17xx_interface mbee_wd17xx_interface =
{
	DEVCB_NULL,
	DEVCB_DRIVER_LINE_MEMBER(mbee_state, mbee_fdc_intrq_w),
	DEVCB_DRIVER_LINE_MEMBER(mbee_state, mbee_fdc_drq_w),
	{FLOPPY_0, FLOPPY_1, NULL, NULL }
};

READ8_MEMBER( mbee_state::mbee_fdc_status_r )
{
/*  d7 indicate if IRQ or DRQ is occuring (1=happening)
    d6..d0 not used */

	return 0x7f | m_fdc_intrq | m_fdc_drq;
}

WRITE8_MEMBER( mbee_state::mbee_fdc_motor_w )
{
/*  d7..d4 not used
    d3 density (1=MFM)
    d2 side (1=side 1)
    d1..d0 drive select (0 to 3) */

	wd17xx_set_drive(m_fdc, data & 3);
	wd17xx_set_side(m_fdc, BIT(data, 2));
	wd17xx_dden_w(m_fdc, !BIT(data, 3));
		/* no idea what turns the motors on & off, guessing it could be drive select
		commented out because it prevents 128k and 256TC from booting up */
	//floppy_mon_w(floppy_get_device(machine(), data & 3), CLEAR_LINE); // motor on
}

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

    256TC Keyboard

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


TIMER_CALLBACK_MEMBER(mbee_state::mbee256_kbd)
{
	/* Keyboard scanner is a Mostek M3870 chip. Its speed of operation is determined by a 15k resistor on
	pin 2 (XTL2) and is therefore unknown. If a key change is detected (up or down), the /strobe
	line activates, sending a high to bit 1 of port 2 (one of the pio input lines). The next read of
	port 18 will clear this line, and read the key scancode. It will also signal the 3870 that the key
	data has been read, on pin 38 (/extint). The 3870 can cache up to 9 keys. With no rom dump
	available, the following is a guess.

	The 3870 (MK3870) 8-bit microcontroller is a single chip implementation of Fairchild F8 (Mostek 3850).
	It includes up to 4 KB of mask-programmable ROM, 64 bytes of scratchpad RAM and up to 64 bytes
	of executable RAM. The MCU also integrates 32-bit I/O and a programmable timer. */

	UINT8 i, j;
	UINT8 pressed[15];


	/* see what is pressed */
	pressed[0] = m_io_x0->read();
	pressed[1] = m_io_x1->read();
	pressed[2] = m_io_x2->read();
	pressed[3] = m_io_x3->read();
	pressed[4] = m_io_x4->read();
	pressed[5] = m_io_x5->read();
	pressed[6] = m_io_x6->read();
	pressed[7] = m_io_x7->read();
	pressed[8] = m_io_x8->read();
	pressed[9] = m_io_x9->read();
	pressed[10] = m_io_x10->read();
	pressed[11] = m_io_x11->read();
	pressed[12] = m_io_x12->read();
	pressed[13] = m_io_x13->read();
	pressed[14] = m_io_x14->read();

	/* find what has changed */
	for (i = 0; i < 15; i++)
	{
		if (pressed[i] != m_mbee256_was_pressed[i])
		{
			/* get scankey value */
			for (j = 0; j < 8; j++)
			{
				if (BIT(pressed[i]^m_mbee256_was_pressed[i], j))
				{
					/* put it in the queue */
					m_mbee256_q[m_mbee256_q_pos] = (i << 3) | j | (BIT(pressed[i], j) ? 0x80 : 0);
					if (m_mbee256_q_pos < 19) m_mbee256_q_pos++;
				}
			}
			m_mbee256_was_pressed[i] = pressed[i];
		}
	}

	/* if anything queued, cause an interrupt */
	if (m_mbee256_q_pos)
		m_mbee256_key_available = 2; // set irq
}

READ8_MEMBER( mbee_state::mbee256_18_r )
{
	UINT8 i, data = m_mbee256_q[0]; // get oldest key

	if (m_mbee256_q_pos)
	{
		m_mbee256_q_pos--;
		for (i = 0; i < m_mbee256_q_pos; i++) m_mbee256_q[i] = m_mbee256_q[i+1]; // ripple queue
	}

	m_mbee256_key_available = 0; // clear irq
	return data;
}


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

    256TC Change CPU speed

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

READ8_MEMBER( mbee_state::mbee256_speed_low_r )
{
	m_maincpu->set_unscaled_clock(3375000);
	return 0xff;
}

READ8_MEMBER( mbee_state::mbee256_speed_high_r )
{
	m_maincpu->set_unscaled_clock(6750000);
	return 0xff;
}



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

    256TC Real Time Clock

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

WRITE8_MEMBER( mbee_state::mbee_04_w )  // address
{
	m_rtc->write(space, 0, data);
}

WRITE8_MEMBER( mbee_state::mbee_06_w )  // write
{
	m_rtc->write(space, 1, data);
}

READ8_MEMBER( mbee_state::mbee_07_r )   // read
{
	return m_rtc->read(space, 1);
}

TIMER_CALLBACK_MEMBER(mbee_state::mbee_rtc_irq)
{
	UINT8 data = m_rtc->read(m_maincpu->space(AS_PROGRAM), 12);
	if (data) m_clock_pulse = 0x80;
}


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

    256TC Memory Banking

    Bits 0, 1 and 5 select which bank goes into 0000-7FFF.
    Bit 2 disables ROM, replacing it with RAM.
    Bit 3 disables Video, replacing it with RAM.
    Bit 4 switches the video circuits between F000-FFFF and
          8000-8FFF.

    In case of a clash, video overrides ROM which overrides RAM.

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

WRITE8_MEMBER( mbee_state::mbee256_50_w )
{
	address_space &mem = m_maincpu->space(AS_PROGRAM);

	// primary low banks
	m_boot->set_entry((data & 3) | ((data & 0x20) >> 3));
	m_bank1->set_entry((data & 3) | ((data & 0x20) >> 3));

	// 9000-EFFF
	m_bank9->set_entry((data & 4) ? 1 : 0);

	// 8000-8FFF, F000-FFFF
	mem.unmap_readwrite (0x8000, 0x87ff);
	mem.unmap_readwrite (0x8800, 0x8fff);
	mem.unmap_readwrite (0xf000, 0xf7ff);
	mem.unmap_readwrite (0xf800, 0xffff);

	switch (data & 0x1c)
	{
		case 0x00:
			mem.install_read_bank (0x8000, 0x87ff, "bank8l");
			mem.install_read_bank (0x8800, 0x8fff, "bank8h");
			mem.install_readwrite_handler (0xf000, 0xf7ff, read8_delegate(FUNC(mbee_state::mbeeppc_low_r), this), write8_delegate(FUNC(mbee_state::mbeeppc_low_w), this));
			mem.install_readwrite_handler (0xf800, 0xffff, read8_delegate(FUNC(mbee_state::mbeeppc_high_r), this), write8_delegate(FUNC(mbee_state::mbeeppc_high_w), this));
			m_bank8l->set_entry(0); // rom
			m_bank8h->set_entry(0); // rom
			break;
		case 0x04:
			mem.install_read_bank (0x8000, 0x87ff, "bank8l");
			mem.install_read_bank (0x8800, 0x8fff, "bank8h");
			mem.install_readwrite_handler (0xf000, 0xf7ff, read8_delegate(FUNC(mbee_state::mbeeppc_low_r), this), write8_delegate(FUNC(mbee_state::mbeeppc_low_w), this));
			mem.install_readwrite_handler (0xf800, 0xffff, read8_delegate(FUNC(mbee_state::mbeeppc_high_r), this), write8_delegate(FUNC(mbee_state::mbeeppc_high_w), this));
			m_bank8l->set_entry(1); // ram
			m_bank8h->set_entry(1); // ram
			break;
		case 0x08:
		case 0x18:
			mem.install_read_bank (0x8000, 0x87ff, "bank8l");
			mem.install_read_bank (0x8800, 0x8fff, "bank8h");
			mem.install_read_bank (0xf000, 0xf7ff, "bankfl");
			mem.install_read_bank (0xf800, 0xffff, "bankfh");
			m_bank8l->set_entry(0); // rom
			m_bank8h->set_entry(0); // rom
			m_bankfl->set_entry(0); // ram
			m_bankfh->set_entry(0); // ram
			break;
		case 0x0c:
		case 0x1c:
			mem.install_read_bank (0x8000, 0x87ff, "bank8l");
			mem.install_read_bank (0x8800, 0x8fff, "bank8h");
			mem.install_read_bank (0xf000, 0xf7ff, "bankfl");
			mem.install_read_bank (0xf800, 0xffff, "bankfh");
			m_bank8l->set_entry(1); // ram
			m_bank8h->set_entry(1); // ram
			m_bankfl->set_entry(0); // ram
			m_bankfh->set_entry(0); // ram
			break;
		case 0x10:
		case 0x14:
			mem.install_readwrite_handler (0x8000, 0x87ff, read8_delegate(FUNC(mbee_state::mbeeppc_low_r), this), write8_delegate(FUNC(mbee_state::mbeeppc_low_w), this));
			mem.install_readwrite_handler (0x8800, 0x8fff, read8_delegate(FUNC(mbee_state::mbeeppc_high_r), this), write8_delegate(FUNC(mbee_state::mbeeppc_high_w), this));
			mem.install_read_bank (0xf000, 0xf7ff, "bankfl");
			mem.install_read_bank (0xf800, 0xffff, "bankfh");
			m_bankfl->set_entry(0); // ram
			m_bankfh->set_entry(0); // ram
			break;
	}
}

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

    128k Memory Banking

    The only difference to the 256TC is that bit 5 switches
    between rom2 and rom3. Since neither of these is dumped,
    this bit is not emulated. If it was, this scheme is used:

    Low - rom2 occupies C000-FFFF
    High - ram = C000-DFFF, rom3 = E000-FFFF.

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

WRITE8_MEMBER( mbee_state::mbee128_50_w )
{
	address_space &mem = m_maincpu->space(AS_PROGRAM);

	// primary low banks
	m_boot->set_entry((data & 3));
	m_bank1->set_entry((data & 3));

	// 9000-EFFF
	m_bank9->set_entry((data & 4) ? 1 : 0);

	// 8000-8FFF, F000-FFFF
	mem.unmap_readwrite (0x8000, 0x87ff);
	mem.unmap_readwrite (0x8800, 0x8fff);
	mem.unmap_readwrite (0xf000, 0xf7ff);
	mem.unmap_readwrite (0xf800, 0xffff);

	switch (data & 0x1c)
	{
		case 0x00:
			mem.install_read_bank (0x8000, 0x87ff, "bank8l");
			mem.install_read_bank (0x8800, 0x8fff, "bank8h");
			mem.install_readwrite_handler (0xf000, 0xf7ff, read8_delegate(FUNC(mbee_state::mbeeppc_low_r),this), write8_delegate(FUNC(mbee_state::mbeeppc_low_w),this));
			mem.install_readwrite_handler (0xf800, 0xffff, read8_delegate(FUNC(mbee_state::mbeeppc_high_r),this), write8_delegate(FUNC(mbee_state::mbeeppc_high_w),this));
			m_bank8l->set_entry(0); // rom
			m_bank8h->set_entry(0); // rom
			break;
		case 0x04:
			mem.install_read_bank (0x8000, 0x87ff, "bank8l");
			mem.install_read_bank (0x8800, 0x8fff, "bank8h");
			mem.install_readwrite_handler (0xf000, 0xf7ff, read8_delegate(FUNC(mbee_state::mbeeppc_low_r),this), write8_delegate(FUNC(mbee_state::mbeeppc_low_w),this));
			mem.install_readwrite_handler (0xf800, 0xffff, read8_delegate(FUNC(mbee_state::mbeeppc_high_r),this), write8_delegate(FUNC(mbee_state::mbeeppc_high_w),this));
			m_bank8l->set_entry(1); // ram
			m_bank8h->set_entry(1); // ram
			break;
		case 0x08:
		case 0x18:
			mem.install_read_bank (0x8000, 0x87ff, "bank8l");
			mem.install_read_bank (0x8800, 0x8fff, "bank8h");
			mem.install_read_bank (0xf000, 0xf7ff, "bankfl");
			mem.install_read_bank (0xf800, 0xffff, "bankfh");
			m_bank8l->set_entry(0); // rom
			m_bank8h->set_entry(0); // rom
			m_bankfl->set_entry(0); // ram
			m_bankfh->set_entry(0); // ram
			break;
		case 0x0c:
		case 0x1c:
			mem.install_read_bank (0x8000, 0x87ff, "bank8l");
			mem.install_read_bank (0x8800, 0x8fff, "bank8h");
			mem.install_read_bank (0xf000, 0xf7ff, "bankfl");
			mem.install_read_bank (0xf800, 0xffff, "bankfh");
			m_bank8l->set_entry(1); // ram
			m_bank8h->set_entry(1); // ram
			m_bankfl->set_entry(0); // ram
			m_bankfh->set_entry(0); // ram
			break;
		case 0x10:
		case 0x14:
			mem.install_readwrite_handler (0x8000, 0x87ff, read8_delegate(FUNC(mbee_state::mbeeppc_low_r),this), write8_delegate(FUNC(mbee_state::mbeeppc_low_w),this));
			mem.install_readwrite_handler (0x8800, 0x8fff, read8_delegate(FUNC(mbee_state::mbeeppc_high_r),this), write8_delegate(FUNC(mbee_state::mbeeppc_high_w),this));
			mem.install_read_bank (0xf000, 0xf7ff, "bankfl");
			mem.install_read_bank (0xf800, 0xffff, "bankfh");
			m_bankfl->set_entry(0); // ram
			m_bankfh->set_entry(0); // ram
			break;
	}
}


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

    64k Memory Banking

    Bit 2 disables ROM, replacing it with RAM.

    Due to lack of documentation, it is not possible to know
    if other bits are used.

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

WRITE8_MEMBER( mbee_state::mbee64_50_w )
{
	if BIT(data, 2)
	{
		m_boot->set_entry(0);
		m_bankl->set_entry(0);
		m_bankh->set_entry(0);
	}
	else
	{
		m_bankl->set_entry(1);
		m_bankh->set_entry(1);
	}
}


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

    ROM Banking on older models

    Set A to 0 or 1 then read the port to switch between the
    halves of the Telcom ROM.

    Output the PAK number to choose an optional PAK ROM.

    The bios will support 256 PAKs, although normally only
    8 are available in hardware. Each PAK is normally a 4K
    ROM. If 8K ROMs are used, the 2nd half becomes PAK+8,
    thus 16 PAKs in total. This is used in the PC85 models.

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

READ8_MEMBER( mbee_state::mbeeic_0a_r )
{
	return m_0a;
}

WRITE8_MEMBER( mbee_state::mbeeic_0a_w )
{
	m_0a = data;
	m_pak->set_entry(data & 15);
}

READ8_MEMBER( mbee_state::mbeepc_telcom_low_r )
{
/* Read of port 0A - set Telcom rom to first half */
	m_telcom->set_entry(0);
	return m_0a;
}

READ8_MEMBER( mbee_state::mbeepc_telcom_high_r )
{
/* Read of port 10A - set Telcom rom to 2nd half */
	m_telcom->set_entry(1);
	return m_0a;
}


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

    Machine

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

/*
  On reset or power on, a circuit forces rom 8000-8FFF to appear at 0000-0FFF, while ram is disabled.
  It gets set back to normal on the first attempt to write to memory. (/WR line goes active).
*/


/* after the first 4 bytes have been read from ROM, switch the ram back in */
TIMER_CALLBACK_MEMBER(mbee_state::mbee_reset)
{
	m_boot->set_entry(0);
}

void mbee_state::machine_reset_common_disk()
{
	/* These values need to be fine tuned or the fdc repaired */
	wd17xx_set_pause_time(m_fdc, 45);       /* default is 40 usec if not set */
//  wd17xx_set_complete_command_delay(m_fdc, 50);   /* default is 12 usec if not set */
}

MACHINE_RESET_MEMBER(mbee_state,mbee)
{
	m_boot->set_entry(1);
	machine().scheduler().timer_set(attotime::from_usec(4), timer_expired_delegate(FUNC(mbee_state::mbee_reset),this));
}

MACHINE_RESET_MEMBER(mbee_state,mbee56)
{
	machine_reset_common_disk();
	m_boot->set_entry(1);
	machine().scheduler().timer_set(attotime::from_usec(4), timer_expired_delegate(FUNC(mbee_state::mbee_reset),this));
}

MACHINE_RESET_MEMBER(mbee_state,mbee64)
{
	machine_reset_common_disk();
	m_boot->set_entry(1);
	m_bankl->set_entry(1);
	m_bankh->set_entry(1);
}

MACHINE_RESET_MEMBER(mbee_state,mbee128)
{
	address_space &mem = m_maincpu->space(AS_PROGRAM);
	machine_reset_common_disk();
	mbee128_50_w(mem,0,0); // set banks to default
	m_boot->set_entry(4); // boot time
}

MACHINE_RESET_MEMBER(mbee_state,mbee256)
{
	UINT8 i;
	address_space &mem = m_maincpu->space(AS_PROGRAM);
	machine_reset_common_disk();
	for (i = 0; i < 15; i++) m_mbee256_was_pressed[i] = 0;
	m_mbee256_q_pos = 0;
	mbee256_50_w(mem,0,0); // set banks to default
	m_boot->set_entry(8); // boot time
	machine().scheduler().timer_set(attotime::from_usec(4), timer_expired_delegate(FUNC(mbee_state::mbee_reset),this));
}

MACHINE_RESET_MEMBER(mbee_state,mbeett)
{
	UINT8 i;
	for (i = 0; i < 15; i++) m_mbee256_was_pressed[i] = 0;
	m_mbee256_q_pos = 0;
	m_boot->set_entry(1);
	machine().scheduler().timer_set(attotime::from_usec(4), timer_expired_delegate(FUNC(mbee_state::mbee_reset),this));
}

INTERRUPT_GEN_MEMBER(mbee_state::mbee_interrupt)
{
// Due to the uncertainly and hackage here, this is commented out for now - Robbbert - 05-Oct-2010
#if 0

	//address_space &space = m_maincpu->space(AS_PROGRAM);
	/* The printer status connects to the pio ASTB pin, and the printer changing to not
	    busy should signal an interrupt routine at B61C, (next line) but this doesn't work.
	    The line below does what the interrupt should be doing. */
	/* But it would break any program loaded to that area of memory, such as CP/M programs */

	//m_z80pio->strobe_a(centronics_busy_r(m_printer)); /* signal int when not busy (L->H) */
	//space.write_byte(0x109, centronics_busy_r(m_printer));


	/* once per frame, pulse the PIO B bit 7 - it is in the schematic as an option,
	but need to find out what it does */
	m_clock_pulse = 0x80;
	irq0_line_hold(device);

#endif
}

DRIVER_INIT_MEMBER(mbee_state,mbee)
{
	UINT8 *RAM = memregion("maincpu")->base();
	m_boot->configure_entries(0, 2, &RAM[0x0000], 0x8000);
	m_size = 0x4000;
}

DRIVER_INIT_MEMBER(mbee_state,mbeeic)
{
	UINT8 *RAM = memregion("maincpu")->base();
	m_boot->configure_entries(0, 2, &RAM[0x0000], 0x8000);

	RAM = memregion("pakrom")->base();
	m_pak->configure_entries(0, 16, &RAM[0x0000], 0x2000);

	m_pak->set_entry(0);
	m_size = 0x8000;
}

DRIVER_INIT_MEMBER(mbee_state,mbeepc)
{
	UINT8 *RAM = memregion("maincpu")->base();
	m_boot->configure_entries(0, 2, &RAM[0x0000], 0x8000);

	RAM = memregion("telcomrom")->base();
	m_telcom->configure_entries(0, 2, &RAM[0x0000], 0x1000);

	RAM = memregion("pakrom")->base();
	m_pak->configure_entries(0, 16, &RAM[0x0000], 0x2000);

	m_pak->set_entry(0);
	m_telcom->set_entry(0);
	m_size = 0x8000;
}

DRIVER_INIT_MEMBER(mbee_state,mbeepc85)
{
	UINT8 *RAM = memregion("maincpu")->base();
	m_boot->configure_entries(0, 2, &RAM[0x0000], 0x8000);

	RAM = memregion("telcomrom")->base();
	m_telcom->configure_entries(0, 2, &RAM[0x0000], 0x1000);

	RAM = memregion("pakrom")->base();
	m_pak->configure_entries(0, 16, &RAM[0x0000], 0x2000);

	m_pak->set_entry(5);
	m_telcom->set_entry(0);
	m_size = 0x8000;
}

DRIVER_INIT_MEMBER(mbee_state,mbeeppc)
{
	UINT8 *RAM = memregion("maincpu")->base();
	m_boot->configure_entry(0, &RAM[0x0000]);

	RAM = memregion("basicrom")->base();
	m_basic->configure_entries(0, 2, &RAM[0x0000], 0x2000);
	m_boot->configure_entry(1, &RAM[0x0000]);

	RAM = memregion("telcomrom")->base();
	m_telcom->configure_entries(0, 2, &RAM[0x0000], 0x1000);

	RAM = memregion("pakrom")->base();
	m_pak->configure_entries(0, 16, &RAM[0x0000], 0x2000);

	m_pak->set_entry(5);
	m_telcom->set_entry(0);
	m_basic->set_entry(0);
	m_size = 0x8000;
}

DRIVER_INIT_MEMBER(mbee_state,mbee56)
{
	UINT8 *RAM = memregion("maincpu")->base();
	m_boot->configure_entries(0, 2, &RAM[0x0000], 0xe000);
	m_size = 0xe000;
}

DRIVER_INIT_MEMBER(mbee_state,mbee64)
{
	UINT8 *RAM = memregion("maincpu")->base();
	m_boot->configure_entry(0, &RAM[0x0000]);
	m_bankl->configure_entry(0, &RAM[0x1000]);
	m_bankl->configure_entry(1, &RAM[0x9000]);
	m_bankh->configure_entry(0, &RAM[0x8000]);

	RAM = memregion("bootrom")->base();
	m_bankh->configure_entry(1, &RAM[0x0000]);
	m_boot->configure_entry(1, &RAM[0x0000]);

	m_size = 0xf000;
}

DRIVER_INIT_MEMBER(mbee_state,mbee128)
{
	UINT8 *RAM = memregion("maincpu")->base();
	m_boot->configure_entries(0, 4, &RAM[0x0000], 0x8000); // standard banks 0000
	m_bank1->configure_entries(0, 4, &RAM[0x1000], 0x8000); // standard banks 1000
	m_bank8l->configure_entry(1, &RAM[0x0000]); // shadow ram
	m_bank8h->configure_entry(1, &RAM[0x0800]); // shadow ram
	m_bank9->configure_entry(1, &RAM[0x1000]); // shadow ram
	m_bankfl->configure_entry(0, &RAM[0xf000]); // shadow ram
	m_bankfh->configure_entry(0, &RAM[0xf800]); // shadow ram

	RAM = memregion("bootrom")->base();
	m_bank9->configure_entry(0, &RAM[0x1000]); // rom
	m_boot->configure_entry(4, &RAM[0x0000]); // rom at boot for 4usec
	m_bank8l->configure_entry(0, &RAM[0x0000]); // rom
	m_bank8h->configure_entry(0, &RAM[0x0800]); // rom

	m_size = 0x8000;
}

DRIVER_INIT_MEMBER(mbee_state,mbee256)
{
	UINT8 *RAM = memregion("maincpu")->base();
	m_boot->configure_entries(0, 8, &RAM[0x0000], 0x8000); // standard banks 0000
	m_bank1->configure_entries(0, 8, &RAM[0x1000], 0x8000); // standard banks 1000
	m_bank8l->configure_entry(1, &RAM[0x0000]); // shadow ram
	m_bank8h->configure_entry(1, &RAM[0x0800]); // shadow ram
	m_bank9->configure_entry(1, &RAM[0x1000]); // shadow ram
	m_bankfl->configure_entry(0, &RAM[0xf000]); // shadow ram
	m_bankfh->configure_entry(0, &RAM[0xf800]); // shadow ram

	RAM = memregion("bootrom")->base();
	m_bank9->configure_entry(0, &RAM[0x1000]); // rom
	m_boot->configure_entry(8, &RAM[0x0000]); // rom at boot for 4usec
	m_bank8l->configure_entry(0, &RAM[0x0000]); // rom
	m_bank8h->configure_entry(0, &RAM[0x0800]); // rom

	machine().scheduler().timer_pulse(attotime::from_hz(1), timer_expired_delegate(FUNC(mbee_state::mbee_rtc_irq),this));   /* timer for rtc */
	machine().scheduler().timer_pulse(attotime::from_hz(25), timer_expired_delegate(FUNC(mbee_state::mbee256_kbd),this));   /* timer for kbd */

	m_size = 0x8000;
}

DRIVER_INIT_MEMBER(mbee_state,mbeett)
{
	UINT8 *RAM = memregion("maincpu")->base();
	m_boot->configure_entries(0, 2, &RAM[0x0000], 0x8000);

	RAM = memregion("telcomrom")->base();
	m_telcom->configure_entries(0, 2, &RAM[0x0000], 0x1000);

	RAM = memregion("pakrom")->base();
	m_pak->configure_entries(0, 16, &RAM[0x0000], 0x2000);

	m_pak->set_entry(5);
	m_telcom->set_entry(0);

	machine().scheduler().timer_pulse(attotime::from_hz(1), timer_expired_delegate(FUNC(mbee_state::mbee_rtc_irq),this));   /* timer for rtc */
	machine().scheduler().timer_pulse(attotime::from_hz(25), timer_expired_delegate(FUNC(mbee_state::mbee256_kbd),this));   /* timer for kbd */

	m_size = 0x8000;
}


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

    Quickload

    These load the standard BIN format, as well
    as COM and MWB files.

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

QUICKLOAD_LOAD( mbee )
{
	mbee_state *state = image.device().machine().driver_data<mbee_state>();
	address_space &space = state->m_maincpu->space(AS_PROGRAM);
	UINT16 i, j;
	UINT8 data, sw = state->ioport("CONFIG")->read() & 1;   /* reading the dipswitch: 1 = autorun */

	if (!mame_stricmp(image.filetype(), "mwb"))
	{
		/* mwb files - standard basic files */
		for (i = 0; i < quickload_size; i++)
		{
			j = 0x8c0 + i;

			if (image.fread(&data, 1) != 1)
			{
				image.message("Unexpected EOF");
				return IMAGE_INIT_FAIL;
			}

			if ((j < state->m_size) || (j > 0xefff))
				space.write_byte(j, data);
			else
			{
				image.message("Not enough memory in this microbee");
				return IMAGE_INIT_FAIL;
			}
		}

		if (sw)
		{
			space.write_word(0xa2,0x801e);  /* fix warm-start vector to get around some copy-protections */
			state->m_maincpu->set_pc(0x801e);
		}
		else
			space.write_word(0xa2,0x8517);
	}
	else if (!mame_stricmp(image.filetype(), "com"))
	{
		/* com files - most com files are just machine-language games with a wrapper and don't need cp/m to be present */
		for (i = 0; i < quickload_size; i++)
		{
			j = 0x100 + i;

			if (image.fread(&data, 1) != 1)
			{
				image.message("Unexpected EOF");
				return IMAGE_INIT_FAIL;
			}

			if ((j < state->m_size) || (j > 0xefff))
				space.write_byte(j, data);
			else
			{
				image.message("Not enough memory in this microbee");
				return IMAGE_INIT_FAIL;
			}
		}

		if (sw) state->m_maincpu->set_pc(0x100);
	}

	return IMAGE_INIT_PASS;
}


/*-------------------------------------------------
    QUICKLOAD_LOAD( mbee_z80bin )
-------------------------------------------------*/

QUICKLOAD_LOAD( mbee_z80bin )
{
	mbee_state *state = image.device().machine().driver_data<mbee_state>();
	UINT16 execute_address, start_addr, end_addr;
	int autorun;

	/* load the binary into memory */
	if (z80bin_load_file(&image, file_type, &execute_address, &start_addr, &end_addr) == IMAGE_INIT_FAIL)
		return IMAGE_INIT_FAIL;

	/* is this file executable? */
	if (execute_address != 0xffff)
	{
		/* check to see if autorun is on (I hate how this works) */
		autorun = state->ioport("CONFIG")->read_safe(0xFF) & 1;

		address_space &space = state->m_maincpu->space(AS_PROGRAM);

		space.write_word(0xa6, execute_address);            /* fix the EXEC command */

		if (autorun)
		{
			space.write_word(0xa2, execute_address);        /* fix warm-start vector to get around some copy-protections */
			state->m_maincpu->set_pc(execute_address);
		}
		else
		{
			space.write_word(0xa2, 0x8517);
		}
	}

	return IMAGE_INIT_PASS;
}