OSDN Git Service

[VM][General][Qt][CMAKE] Merge updtream 2016-01-10.
[csp-qt/common_source_project-fm7.git] / source / src / vm / mz700 / mz700.cpp
1 /*
2         SHARP MZ-700 Emulator 'EmuZ-700'
3         SHARP MZ-800 Emulator 'EmuZ-800'
4         SHARP MZ-1500 Emulator 'EmuZ-1500'
5
6         Author : Takeda.Toshiya
7         Date   : 2008.06.05 -
8
9         [ virtual machine ]
10 */
11
12 #include "mz700.h"
13 #include "../../emu.h"
14 #include "../device.h"
15 #include "../event.h"
16
17 #include "../and.h"
18 #include "../datarec.h"
19 #include "../i8253.h"
20 #include "../i8255.h"
21 #include "../io.h"
22 #include "../pcm1bit.h"
23 #include "../z80.h"
24
25 #ifdef USE_DEBUGGER
26 #include "../debugger.h"
27 #endif
28
29 //#include "cmos.h"
30 #include "emm.h"
31 #include "kanji.h"
32 #include "keyboard.h"
33 #include "memory.h"
34 #include "ramfile.h"
35
36 #if defined(_MZ800) || defined(_MZ1500)
37 #include "../disk.h"
38 #include "../mb8877.h"
39 #include "../not.h"
40 #include "../sn76489an.h"
41 #include "../z80pio.h"
42 #include "../z80sio.h"
43 #include "floppy.h"
44 #if defined(_MZ1500)
45 #include "../mz1p17.h"
46 #include "../prnfile.h"
47 #include "psg.h"
48 #endif
49 #include "quickdisk.h"
50 #endif
51
52 // ----------------------------------------------------------------------------
53 // initialize
54 // ----------------------------------------------------------------------------
55
56 VM::VM(EMU* parent_emu) : emu(parent_emu)
57 {
58 #if defined(_MZ800)
59         boot_mode = config.boot_mode;
60 #endif
61         
62         // create devices
63         first_device = last_device = NULL;
64         dummy = new DEVICE(this, emu);  // must be 1st device
65         event = new EVENT(this, emu);   // must be 2nd device
66         
67         and_int = new AND(this, emu);
68         drec = new DATAREC(this, emu);
69         pit = new I8253(this, emu);
70         pio = new I8255(this, emu);
71         io = new IO(this, emu);
72         pcm = new PCM1BIT(this, emu);
73         cpu = new Z80(this, emu);
74         
75 //      cmos = new CMOS(this, emu);
76         emm = new EMM(this, emu);
77         kanji = new KANJI(this, emu);
78         keyboard = new KEYBOARD(this, emu);
79         memory = new MEMORY(this, emu);
80         ramfile = new RAMFILE(this, emu);
81         
82 #if defined(_MZ800) || defined(_MZ1500)
83         and_snd = new AND(this, emu);
84         fdc = new MB8877(this, emu);    // mb8876
85 #if defined(_MZ800)
86         not_pit = new NOT(this, emu);
87         psg = new SN76489AN(this, emu);
88 #elif defined(_MZ1500)
89         if(config.printer_device_type == 0) {
90                 printer = new PRNFILE(this, emu);
91         } else if(config.printer_device_type == 1) {
92                 printer = new MZ1P17(this, emu);
93         } else {
94                 printer = dummy;
95         }
96         not_reset = new NOT(this, emu);
97         not_strobe = new NOT(this, emu);
98         psg_l = new SN76489AN(this, emu);
99         psg_r = new SN76489AN(this, emu);
100 #endif
101         pio_int = new Z80PIO(this, emu);
102         sio_rs = new Z80SIO(this, emu);
103         sio_qd = new Z80SIO(this, emu);
104         
105         floppy = new FLOPPY(this, emu);
106 #if defined(_MZ1500)
107         psg = new PSG(this, emu);
108 #endif
109         qd = new QUICKDISK(this, emu);
110 #endif
111         
112         // set contexts
113         event->set_context_cpu(cpu);
114         event->set_context_sound(pcm);
115 #if defined(_MZ800)
116         event->set_context_sound(psg);
117 #elif defined(_MZ1500)
118         event->set_context_sound(psg_l);
119         event->set_context_sound(psg_r);
120 #endif
121         event->set_context_sound(drec);
122         // VRAM/PCG wait
123         memory->set_context_cpu(cpu);
124         
125         // memory mapped I/O
126         memory->set_context_pio(pio);
127         memory->set_context_pit(pit);
128         
129 #if defined(_MZ1500)
130         // psg mixer
131         psg->set_context_psg_l(psg_l);
132         psg->set_context_psg_r(psg_r);
133 #endif
134         
135 #if defined(_MZ800)
136         // 8253:CLK#0 <- 1.10MHz
137         pit->set_constant_clock(0, 1100000);
138 #else
139         // 8253:CLK#0 <- 895KHz
140         pit->set_constant_clock(0, CPU_CLOCKS / 4);
141 #endif
142         
143 #if defined(_MZ800) || defined(_MZ1500)
144         // 8253:OUT#0 AND 8255:PC0 -> SPEAKER
145         pit->set_context_ch0(and_snd, SIG_AND_BIT_0, 1);
146         pio->set_context_port_c(and_snd, SIG_AND_BIT_1, 1, 0);
147         and_snd->set_context_out(pcm, SIG_PCM1BIT_SIGNAL, 1);
148         and_snd->set_mask(SIG_AND_BIT_0 | SIG_AND_BIT_1);
149 #else
150         // 8253:OUT#0 -> SPEAKER
151         pit->set_context_ch0(pcm, SIG_PCM1BIT_SIGNAL, 1);
152 #endif
153 #if defined(_MZ800)
154         // 8253:OUT#0 -> NOT -> Z80PIO:PA4
155         pit->set_context_ch0(not_pit, SIG_NOT_INPUT, 1);
156         not_pit->set_context_out(pio_int, SIG_Z80PIO_PORT_A, 0x10);
157 #elif defined(_MZ1500)
158         // 8253:OUT#0 -> Z80PIO:PA4
159         pit->set_context_ch0(pio_int, SIG_Z80PIO_PORT_A, 0x10);
160 #endif
161         
162         // 8253:CLK#1 <- 15.7KHz
163         pit->set_constant_clock(1, CPU_CLOCKS / 228);
164         
165         // 8253:OUT#1 -> 8253:CLK#2
166         pit->set_context_ch1(pit, SIG_I8253_CLOCK_2, 1);
167         
168         // 8253:OUT#2 (N)AND 8255:PC2 -> Z80:INT
169         pit->set_context_ch2(and_int, SIG_AND_BIT_0, 1);
170         pio->set_context_port_c(and_int, SIG_AND_BIT_1, 4, 0);
171         and_int->set_context_out(cpu, SIG_CPU_IRQ, 1);
172         and_int->set_mask(SIG_AND_BIT_0 | SIG_AND_BIT_1);
173         
174 #if defined(_MZ1500)
175         // 8253:OUT#2 -> Z80PIO:PA5
176         pit->set_context_ch2(pio_int, SIG_Z80PIO_PORT_A, 0x20);
177 #endif
178         
179         // 8255:PA0-3 -> KEYBOARD:STROBE
180         pio->set_context_port_a(keyboard, SIG_KEYBOARD_COLUMN, 0x0f, 0);
181 #if defined(_MZ800)
182         // 8255:PA4 -> JOYSTICK #1
183         // 8255:PA5 -> JOYSTICK #2
184 #endif
185         // 8255:PA7 -> 556 RESET
186         
187         // 8255:PB0-7 <- KEYBOARD:DATA
188         keyboard->set_context_pio(pio);
189         
190         // 8255:PC0 -> AND -> SPEAKER
191         // 8255:PC1 -> DATA RECORDER:WRITE DATA
192         pio->set_context_port_c(drec, SIG_DATAREC_MIC, 0x02, 0);
193         // 8255:PC2 -> (N)AND -> Z80:INT
194         // 8255:PC3 -> DATA RECORDER:MOTOR ON/OFF
195         pio->set_context_port_c(drec, SIG_DATAREC_TRIG, 0x08, 0);
196         // 8255:PC4 <- DATA RECORDER:MOTOR REMOTE
197         drec->set_context_remote(pio, SIG_I8255_PORT_C, 0x10);
198         // 8255:PC5 <- DATA RECORDER:READ DATA
199         drec->set_context_ear(pio, SIG_I8255_PORT_C, 0x20);
200         // 8255:PC6 <- MEMORY:556 OUT (1.5KHz)
201         // 8255:PC7 <- MEMORY:VBLANK
202         
203 #if defined(_MZ800) || defined(_MZ1500)
204         // Z80PIO:PA2 <- GND
205         // Z80PIO:PA3 <- GND
206 #if defined(_MZ800)
207         // Z80PIO:PA4 <- NOT <- 8253:OUT#0
208         // Z80PIO:PA5 <- HBLANK
209         memory->set_context_pio_int(pio_int);
210 #elif defined(_MZ1500)
211         // Z80PIO:PA0 <- PRINTER:RDA (BUSY)
212         // Z80PIO:PA1 <- PRINTER:STA (PE)
213         if(config.printer_device_type == 0) {
214                 PRNFILE *prnfile = (PRNFILE *)printer;
215                 prnfile->set_context_busy(pio_int, SIG_Z80PIO_PORT_A, 0x01);
216         } else if(config.printer_device_type == 1) {
217                 MZ1P17 *mz1p17 = (MZ1P17 *)printer;
218                 mz1p17->mode = MZ1P17_MODE_MZ2;
219                 mz1p17->set_context_busy(pio_int, SIG_Z80PIO_PORT_A, 0x01);
220         }
221         // Z80PIO:PA4 <- 8253:OUT#0
222         // Z80PIO:PA5 <- 8253:OUT#2
223         // Z80PIO:PA6 -> NOT -> PRINTER:IRT (RESET)
224         // Z80PIO:PA7 -> NOT -> PRINTER:RDP (STROBE)
225         // Z80PIO:PB  -> PRINTER:DATA
226         pio_int->set_context_port_a(not_reset, SIG_NOT_INPUT, 0x40, 0);
227         not_reset->set_context_out(printer, SIG_PRINTER_RESET, 0x01);
228         pio_int->set_context_port_a(not_strobe, SIG_NOT_INPUT, 0x80, 0);
229         not_strobe->set_context_out(printer, SIG_PRINTER_STROBE, 0x01);
230         pio_int->set_context_port_b(printer, SIG_PRINTER_DATA, 0xff, 0);
231 #endif
232 #endif
233         
234 #if defined(_MZ800) || defined(_MZ1500)
235         // Z80SIO:RTSA -> QD:WRGA
236         sio_qd->set_context_rts(0, qd, QUICKDISK_SIO_RTSA, 1);
237         // Z80SIO:DTRB -> QD:MTON
238         sio_qd->set_context_dtr(1, qd, QUICKDISK_SIO_DTRB, 1);
239         // Z80SIO:SENDA -> QD:RECV
240         sio_qd->set_context_sync(0, qd, QUICKDISK_SIO_SYNC, 1);
241         sio_qd->set_context_rxdone(0, qd, QUICKDISK_SIO_RXDONE, 1);
242         sio_qd->set_context_send(0, qd, QUICKDISK_SIO_DATA);
243         sio_qd->set_context_break(0, qd, QUICKDISK_SIO_BREAK, 1);
244         // Z80SIO:CTSA <- QD:PROTECT
245         // Z80SIO:DCDA <- QD:INSERT
246         // Z80SIO:DCDB <- QD:HOE
247         qd->set_context_sio(sio_qd);
248         
249         sio_rs->set_tx_clock(0, 1200 * 16);     // 1200 baud
250         sio_rs->set_rx_clock(0, 1200 * 16);     // baud-rate can be changed by jumper pin
251         sio_rs->set_tx_clock(1, 1200 * 16);
252         sio_rs->set_rx_clock(1, 1200 * 16);
253         
254         sio_qd->set_tx_clock(0, 101562.5);
255         sio_qd->set_rx_clock(0, 101562.5);
256         sio_qd->set_tx_clock(1, 101562.5);
257         sio_qd->set_rx_clock(1, 101562.5);
258         
259         // floppy drives
260         floppy->set_context_cpu(cpu);
261         floppy->set_context_fdc(fdc);
262         fdc->set_context_drq(floppy, SIG_FLOPPY_DRQ, 1);
263 #endif
264         
265         // cpu bus
266         cpu->set_context_mem(memory);
267         cpu->set_context_io(io);
268 #if defined(_MZ800) || defined(_MZ1500)
269         cpu->set_context_intr(pio_int);
270         // z80 family daisy chain
271         // 0=8253:OUT2
272         pio_int->set_context_intr(cpu, 1);
273         pio_int->set_context_child(sio_rs);
274         sio_rs->set_context_intr(cpu, 2);
275         sio_rs->set_context_child(sio_qd);
276         sio_qd->set_context_intr(cpu, 3);
277 #else
278         cpu->set_context_intr(dummy);
279 #endif
280 #ifdef USE_DEBUGGER
281         cpu->set_context_debugger(new DEBUGGER(this, emu));
282 #endif
283         
284         // emm
285         io->set_iomap_range_rw(0x00, 0x03, emm);
286         // kanji
287         io->set_iomap_range_rw(0xb8, 0xb9, kanji);
288         // ramfile
289         io->set_iomap_range_rw(0xea, 0xeb, ramfile);
290         // cmos
291 //      io->set_iomap_range_rw(0xf8, 0xfa, cmos);
292         
293 #if defined(_MZ800)
294         // 8255/8253
295         io->set_iomap_range_rw(0xd0, 0xd3, pio);
296         io->set_iomap_range_rw(0xd4, 0xd7, pit);
297 #endif
298         
299 #if defined(_MZ800) || defined(_MZ1500)
300         // floppy drives
301         io->set_iomap_range_rw(0xd8, 0xdb, fdc);
302         io->set_iomap_range_w(0xdc, 0xdf, floppy);
303 #endif
304         
305         // memory mapper
306 #if defined(_MZ800)
307         io->set_iomap_range_r(0xe0, 0xe1, memory);
308         io->set_iomap_range_w(0xe0, 0xe6, memory);
309 #elif defined(_MZ1500)
310         io->set_iomap_range_w(0xe0, 0xe6, memory);
311 #else
312         io->set_iomap_range_w(0xe0, 0xe4, memory);
313 #endif
314         
315 #if defined(_MZ800)
316         // crtc
317         io->set_iomap_range_w(0xcc, 0xcf, memory);
318         io->set_iomap_single_r(0xce, memory);
319         // palette
320         io->set_iomap_single_w(0xf0, memory);
321 #elif defined(_MZ1500)
322         // palette
323         io->set_iomap_range_w(0xf0, 0xf1, memory);
324 #endif
325         
326 #if defined(_MZ800)
327         // joystick
328 //      io->set_iomap_range_r(0xf0, 0xf1, joystick);
329 #endif
330         
331         // psg
332 #if defined(_MZ800)
333         io->set_iomap_single_w(0xf2, psg);
334 #elif defined(_MZ1500)
335         io->set_iomap_single_w(0xe9, psg);
336         io->set_iomap_single_w(0xf2, psg_l);
337         io->set_iomap_single_w(0xf3, psg_r);
338 #endif
339         
340 #if defined(_MZ800) || defined(_MZ1500)
341         // z80pio/sio
342         // z80pio and z80sio*2
343         static const int z80_sio_addr[4] = {0, 2, 1, 3};
344         static const int z80_pio_addr[4] = {1, 3, 0, 2};
345         for(int i = 0; i < 4; i++) {
346                 io->set_iomap_alias_rw(0xb0 + i, sio_rs, z80_sio_addr[i]);
347                 io->set_iomap_alias_rw(0xf4 + i, sio_qd, z80_sio_addr[i]);
348                 io->set_iomap_alias_rw(0xfc + i, pio_int, z80_pio_addr[i]);
349         }
350 #else
351         // printer
352         io->set_iovalue_single_r(0xfe, 0xc0);
353 #endif
354
355         // initialize all devices
356         for(DEVICE* device = first_device; device; device = device->next_device) {
357                 device->initialize();
358         }
359 #if defined(_MZ800) || defined(_MZ1500)
360         for(int i = 0; i < MAX_DRIVE; i++) {
361                 fdc->set_drive_type(i, DRIVE_TYPE_2DD);
362         }
363 #endif
364 }
365
366 VM::~VM()
367 {
368         // delete all devices
369         for(DEVICE* device = first_device; device;) {
370                 DEVICE *next_device = device->next_device;
371                 device->release();
372                 delete device;
373                 device = next_device;
374         }
375 }
376
377 DEVICE* VM::get_device(int id)
378 {
379         for(DEVICE* device = first_device; device; device = device->next_device) {
380                 if(device->this_device_id == id) {
381                         return device;
382                 }
383         }
384         return NULL;
385 }
386
387 // ----------------------------------------------------------------------------
388 // drive virtual machine
389 // ----------------------------------------------------------------------------
390
391 void VM::reset()
392 {
393         // reset all devices
394         for(DEVICE* device = first_device; device; device = device->next_device) {
395                 device->reset();
396         }
397         and_int->write_signal(SIG_AND_BIT_0, 0, 1);     // CLOCK = L
398         and_int->write_signal(SIG_AND_BIT_1, 1, 1);     // INTMASK = H
399 #if defined(_MZ800) || defined(_MZ1500)
400         and_snd->write_signal(SIG_AND_BIT_1, 1, 1);     // SNDMASK = H
401 #endif
402 #if defined(_MZ1500)
403         pio_int->write_signal(SIG_Z80PIO_PORT_A, 0x02, 0x03);   // BUSY = L, PE = H
404 #endif
405 }
406
407 void VM::run()
408 {
409         event->drive();
410 }
411
412 // ----------------------------------------------------------------------------
413 // debugger
414 // ----------------------------------------------------------------------------
415
416 #ifdef USE_DEBUGGER
417 DEVICE *VM::get_cpu(int index)
418 {
419         if(index == 0) {
420                 return cpu;
421         }
422         return NULL;
423 }
424 #endif
425
426 // ----------------------------------------------------------------------------
427 // draw screen
428 // ----------------------------------------------------------------------------
429
430 void VM::draw_screen()
431 {
432         memory->draw_screen();
433 }
434
435 #if defined(_MZ800) || defined(_MZ1500)
436 int VM::access_lamp()
437 {
438         uint32 status = fdc->read_signal(0) | qd->read_signal(0);
439         return (status & (1 | 4)) ? 1 : (status & (2 | 8)) ? 2 : 0;
440 }
441 #endif
442
443 // ----------------------------------------------------------------------------
444 // soud manager
445 // ----------------------------------------------------------------------------
446
447 void VM::initialize_sound(int rate, int samples)
448 {
449         // init sound manager
450         event->initialize_sound(rate, samples);
451         
452         // init sound gen
453         pcm->init(rate, 8000);
454 #if defined(_MZ800)
455         psg->init(rate, 3579545, 8000);
456 #elif defined(_MZ1500)
457         psg_l->init(rate, 3579545, 8000);
458         psg_r->init(rate, 3579545, 8000);
459 #endif
460 }
461
462 uint16* VM::create_sound(int* extra_frames)
463 {
464         return event->create_sound(extra_frames);
465 }
466
467 int VM::sound_buffer_ptr()
468 {
469         return event->sound_buffer_ptr();
470 }
471
472 // ----------------------------------------------------------------------------
473 // user interface
474 // ----------------------------------------------------------------------------
475
476 void VM::play_tape(const _TCHAR* file_path)
477 {
478         drec->play_tape(file_path);
479         drec->set_remote(true);
480 }
481
482 void VM::rec_tape(const _TCHAR* file_path)
483 {
484         drec->rec_tape(file_path);
485         drec->set_remote(true);
486 }
487
488 void VM::close_tape()
489 {
490         drec->close_tape();
491         drec->write_signal(SIG_DATAREC_REMOTE, 0, 0);
492 }
493
494 bool VM::tape_inserted()
495 {
496         return drec->tape_inserted();
497 }
498
499
500 bool VM::tape_playing()
501 {
502         return drec->tape_playing();
503 }
504
505 bool VM::tape_recording()
506 {
507         return drec->tape_recording();
508 }
509
510 int VM::tape_position()
511 {
512         return drec->tape_position();
513 }
514
515 void VM::push_play()
516 {
517         drec->set_ff_rew(0);
518         drec->write_signal(SIG_DATAREC_REMOTE, 1, 1);
519 }
520
521 void VM::push_stop()
522 {
523         drec->write_signal(SIG_DATAREC_REMOTE, 0, 1);
524 }
525
526 void VM::push_fast_forward()
527 {
528         drec->set_ff_rew(1);
529         drec->write_signal(SIG_DATAREC_REMOTE, 1, 1);
530 }
531
532 void VM::push_fast_rewind()
533 {
534         drec->set_ff_rew(-1);
535         drec->write_signal(SIG_DATAREC_REMOTE, 1, 1);
536 }
537
538 #if defined(_MZ800) || defined(_MZ1500)
539 void VM::open_quickdisk(int drv, const _TCHAR* file_path)
540 {
541         if(drv == 0) {
542                 qd->open_disk(file_path);
543         }
544 }
545
546 void VM::close_quickdisk(int drv)
547 {
548         if(drv == 0) {
549                 qd->close_disk();
550         }
551 }
552
553 bool VM::quickdisk_inserted(int drv)
554 {
555         if(drv == 0) {
556                 return qd->disk_inserted();
557         } else {
558                 return false;
559         }
560 }
561
562 void VM::open_disk(int drv, const _TCHAR* file_path, int bank)
563 {
564         fdc->open_disk(drv, file_path, bank);
565 }
566
567 void VM::close_disk(int drv)
568 {
569         fdc->close_disk(drv);
570 }
571
572 bool VM::disk_inserted(int drv)
573 {
574         return fdc->disk_inserted(drv);
575 }
576
577 void VM::set_disk_protected(int drv, bool value)
578 {
579         fdc->set_disk_protected(drv, value);
580 }
581
582 bool VM::get_disk_protected(int drv)
583 {
584         return fdc->get_disk_protected(drv);
585 }
586 #endif
587
588 bool VM::now_skip()
589 {
590         return event->now_skip();
591 }
592
593 void VM::update_config()
594 {
595 #if defined(_MZ800)
596         if(boot_mode != config.boot_mode) {
597                 // boot mode is changed !!!
598                 boot_mode = config.boot_mode;
599                 reset();
600         } else {
601 #endif
602                 for(DEVICE* device = first_device; device; device = device->next_device) {
603                         device->update_config();
604                 }
605 #if defined(_MZ800)
606         }
607 #endif
608 }
609
610 #define STATE_VERSION   1
611
612 void VM::save_state(FILEIO* state_fio)
613 {
614         state_fio->FputUint32(STATE_VERSION);
615         
616         for(DEVICE* device = first_device; device; device = device->next_device) {
617                 device->save_state(state_fio);
618         }
619 #if defined(_MZ800)
620         state_fio->FputInt32(boot_mode);
621 #endif
622 }
623
624 bool VM::load_state(FILEIO* state_fio)
625 {
626         if(state_fio->FgetUint32() != STATE_VERSION) {
627                 return false;
628         }
629         for(DEVICE* device = first_device; device; device = device->next_device) {
630                 if(!device->load_state(state_fio)) {
631                         return false;
632                 }
633         }
634 #if defined(_MZ800)
635         boot_mode = state_fio->FgetInt32();
636 #endif
637         return true;
638 }
639