OSDN Git Service

[VM] Fix crash when end of emulation at various(!) VMs.
[csp-qt/common_source_project-fm7.git] / source / src / vm / mz2500 / mz80b.cpp
1 /*
2         SHARP MZ-80B Emulator 'EmuZ-80B'
3         SHARP MZ-2200 Emulator 'EmuZ-2200'
4
5         Author : Takeda.Toshiya
6         Date   : 2013.03.14-
7
8         [ virtual machine ]
9 */
10
11 #include "mz80b.h"
12 #include "../../emu.h"
13 #include "../device.h"
14 #include "../event.h"
15
16 #include "../datarec.h"
17 #include "../disk.h"
18 #include "../i8253.h"
19 #include "../i8255.h"
20 #include "../io.h"
21 #include "../mb8877.h"
22 #include "../mz1p17.h"
23 #include "../noise.h"
24 #include "../pcm1bit.h"
25 #include "../prnfile.h"
26 #include "../z80.h"
27 #include "../z80pio.h"
28
29 #ifdef USE_DEBUGGER
30 #include "../debugger.h"
31 #endif
32
33 #include "cmt.h"
34 #include "floppy.h"
35 #include "keyboard.h"
36 #include "./memory80b.h"
37 #include "mz1r12.h"
38 #include "mz1r13.h"
39 #include "printer.h"
40 #include "timer.h"
41
42 #ifdef SUPPORT_QUICK_DISK
43 #include "../z80sio.h"
44 #include "../mz700/quickdisk.h"
45 #endif
46
47 #ifdef SUPPORT_16BIT_BOARD
48 #include "../i286.h"
49 #include "../i8259.h"
50 #include "mz1m01.h"
51 #endif
52
53 using MZ80B::MEMORY;
54 using MZ2500::CMT;
55 using MZ2500::FLOPPY;
56 using MZ2500::KEYBOARD;
57 using MZ2500::MZ1R12;
58 using MZ2500::MZ1R13;
59 using MZ2500::PRINTER;
60 using MZ2500::TIMER;
61 #ifdef SUPPORT_QUICK_DISK
62 using MZ700::QUICKDISK;
63 #endif
64 #ifdef SUPPORT_16BIT_BOARD
65 using MZ80B::MZ1M01;
66 #endif
67
68 // ----------------------------------------------------------------------------
69 // initialize
70 // ----------------------------------------------------------------------------
71
72 VM::VM(EMU* parent_emu) : VM_TEMPLATE(parent_emu)
73 {
74         // create devices
75         first_device = last_device = NULL;
76         dummy = new DEVICE(this, emu);  // must be 1st device
77         event = new EVENT(this, emu);   // must be 2nd device
78         dummy->set_device_name(_T("1st Dummy"));
79         
80         drec = new DATAREC(this, emu);
81         drec->set_context_noise_play(new NOISE(this, emu));
82         drec->set_context_noise_stop(new NOISE(this, emu));
83         drec->set_context_noise_fast(new NOISE(this, emu));
84         pit = new I8253(this, emu);
85         pio_i = new I8255(this, emu);
86         io = new IO(this, emu);
87         fdc = new MB8877(this, emu);
88         fdc->set_context_noise_seek(new NOISE(this, emu));
89         fdc->set_context_noise_head_down(new NOISE(this, emu));
90         fdc->set_context_noise_head_up(new NOISE(this, emu));
91         pcm = new PCM1BIT(this, emu);
92         cpu = new Z80(this, emu);
93         pio = new Z80PIO(this, emu);
94         pio_i->set_device_name(_T("i8255 PIO(CMT/CRTC)"));
95         pio->set_device_name(_T("Z80 PIO(KEYBOARD/CRTC)"));
96         
97         cmt = new CMT(this, emu);
98         floppy = new FLOPPY(this, emu);
99         keyboard = new KEYBOARD(this, emu);
100         memory = new MEMORY(this, emu);
101         mz1r12 = new MZ1R12(this, emu);
102         mz1r13 = new MZ1R13(this, emu);
103         printer = new PRINTER(this, emu);
104         timer = new TIMER(this, emu);
105         
106 #ifdef SUPPORT_QUICK_DISK
107         sio = new Z80SIO(this, emu);
108         qd = new QUICKDISK(this, emu);
109         sio->set_device_name(_T("Z80 SIO(QD)"));
110 #endif
111         
112 #ifdef SUPPORT_16BIT_BOARD
113         pio_to16 = new Z80PIO(this, emu);
114         cpu_16 = new I286(this, emu);   // 8088
115         pic_16 = new I8259(this, emu);
116         mz1m01 = new MZ1M01(this, emu);
117         pio_to16->set_device_name(_T("Z80 PIO(16BIT BOARD)"));
118         cpu_16->set_device_name(_T("CPU i286(16BIT BOARD)"));
119         pic_16->set_device_name(_T("i8259 PIC(16BIT BOARD)"));
120 #endif
121         
122         // set contexts
123         event->set_context_cpu(cpu, config.cpu_type ? CPU_CLOCKS_HIGH : CPU_CLOCKS);
124 #ifdef SUPPORT_16BIT_BOARD
125         event->set_context_cpu(cpu_16, 5000000);
126 #endif
127         event->set_context_sound(pcm);
128         event->set_context_sound(drec);
129         event->set_context_sound(fdc->get_context_noise_seek());
130         event->set_context_sound(fdc->get_context_noise_head_down());
131         event->set_context_sound(fdc->get_context_noise_head_up());
132         event->set_context_sound(drec->get_context_noise_play());
133         event->set_context_sound(drec->get_context_noise_stop());
134         event->set_context_sound(drec->get_context_noise_fast());
135         
136         drec->set_context_ear(cmt, SIG_CMT_OUT, 1);
137         drec->set_context_remote(cmt, SIG_CMT_REMOTE, 1);
138         drec->set_context_end(cmt, SIG_CMT_END, 1);
139         drec->set_context_top(cmt, SIG_CMT_TOP, 1);
140         
141         pit->set_context_ch0(pit, SIG_I8253_CLOCK_1, 1);
142         pit->set_context_ch1(pit, SIG_I8253_CLOCK_2, 1);
143         pit->set_constant_clock(0, 31250);
144         pio_i->set_context_port_a(cmt, SIG_CMT_PIO_PA, 0xff, 0);
145         pio_i->set_context_port_a(memory, SIG_CRTC_REVERSE, 0x10, 0);
146         pio_i->set_context_port_c(memory, SIG_CRTC_VGATE, 0x01, 0);
147         pio_i->set_context_port_c(cmt, SIG_CMT_PIO_PC, 0xff, 0);
148         pio_i->set_context_port_c(pcm, SIG_PCM1BIT_SIGNAL, 0x04, 0);
149         // Sound:: Force realtime rendering. This is temporally fix. 20161024 K.O
150         //pcm->set_realtime_render(true);
151         
152         pio->set_context_port_a(memory, SIG_MEMORY_VRAM_SEL, 0xc0, 0);
153         pio->set_context_port_a(memory, SIG_CRTC_WIDTH80, 0x20, 0);
154         pio->set_context_port_a(keyboard, SIG_KEYBOARD_COLUMN, 0x1f, 0);
155         
156         cmt->set_context_pio(pio_i);
157         cmt->set_context_drec(drec);
158         floppy->set_context_fdc(fdc);
159         keyboard->set_context_pio_i(pio_i);
160         keyboard->set_context_pio(pio);
161         memory->set_context_cpu(cpu);
162         memory->set_context_pio(pio_i);
163         if(config.printer_type == 0) {  
164                 printer->set_context_prn(new PRNFILE(this, emu));
165         } else if(config.printer_type == 1) {
166                 MZ1P17 *mz1p17 = new MZ1P17(this, emu);
167                 mz1p17->mode = MZ1P17_MODE_MZ1;
168                 printer->set_context_prn(mz1p17);
169         } else if(config.printer_type == 2) {
170                 MZ1P17 *mz1p17 = new MZ1P17(this, emu);
171                 mz1p17->mode = MZ1P17_MODE_MZ3;
172                 printer->set_context_prn(mz1p17);
173         } else {
174                 printer->set_context_prn(dummy);
175         }
176         timer->set_context_pit(pit);
177         
178 #ifdef SUPPORT_QUICK_DISK
179         // Z80SIO:RTSA -> QD:WRGA
180         sio->set_context_rts(0, qd, QUICKDISK_SIO_RTSA, 1);
181         // Z80SIO:DTRB -> QD:MTON
182         sio->set_context_dtr(1, qd, QUICKDISK_SIO_DTRB, 1);
183         // Z80SIO:SENDA -> QD:RECV
184         sio->set_context_sync(0, qd, QUICKDISK_SIO_SYNC, 1);
185         sio->set_context_rxdone(0, qd, QUICKDISK_SIO_RXDONE, 1);
186         sio->set_context_send(0, qd, QUICKDISK_SIO_DATA);
187         sio->set_context_break(0, qd, QUICKDISK_SIO_BREAK, 1);
188         // Z80SIO:CTSA <- QD:PROTECT
189         // Z80SIO:DCDA <- QD:INSERT
190         // Z80SIO:DCDB <- QD:HOE
191         qd->set_context_sio(sio);
192         
193         sio->set_tx_clock(0, 101562.5);
194         sio->set_rx_clock(0, 101562.5);
195         sio->set_tx_clock(1, 101562.5);
196         sio->set_rx_clock(1, 101562.5);
197 #endif
198         
199 #ifdef SUPPORT_16BIT_BOARD
200         pio_to16->set_context_port_a(mz1m01, SIG_MZ1M01_PORT_A, 0xff, 0);
201         pio_to16->set_context_port_b(mz1m01, SIG_MZ1M01_PORT_B, 0x80, 0);
202         pio_to16->set_context_ready_a(pic_16, SIG_I8259_IR0, 1);
203         pio_to16->set_context_ready_b(pic_16, SIG_I8259_IR1, 1);
204         pio_to16->set_context_port_b(pic_16, SIG_I8259_IR2, 0x80, 0);
205         pio_to16->set_hand_shake(0, true);
206         pio_to16->set_hand_shake(1, true);
207         pic_16->set_context_cpu(cpu_16);
208         cpu_16->set_context_mem(mz1m01);
209         cpu_16->set_context_io(mz1m01);
210         cpu_16->set_context_intr(pic_16);
211 #ifdef USE_DEBUGGER
212         cpu_16->set_context_debugger(new DEBUGGER(this, emu));
213 #endif
214         
215         mz1m01->set_context_cpu(cpu_16);
216         mz1m01->set_context_pic(pic_16);
217         mz1m01->set_context_pio(pio_to16);
218 #endif
219         
220         // cpu bus
221         cpu->set_context_mem(memory);
222         cpu->set_context_io(io);
223         
224         // z80 family daisy chain
225 #ifdef SUPPORT_16BIT_BOARD
226         // FIXME: Z80PIO on MZ-1M01 is not daisy-chained to other Z80 family !!!
227         cpu->set_context_intr(pio_to16);
228         pio_to16->set_context_intr(cpu, 0);
229         pio_to16->set_context_child(pio);
230 #else
231         cpu->set_context_intr(pio);
232 #endif
233         pio->set_context_intr(cpu, 1);
234 #ifdef SUPPORT_QUICK_DISK
235         pio->set_context_child(sio);
236         sio->set_context_intr(cpu, 2);
237 #endif
238 #ifdef USE_DEBUGGER
239         cpu->set_context_debugger(new DEBUGGER(this, emu));
240 #endif
241         
242         // i/o bus
243         io->set_iomap_range_rw(0xb8, 0xbb, mz1r13);
244 #ifdef SUPPORT_QUICK_DISK
245         io->set_iomap_alias_rw(0xd0, sio, 0);
246         io->set_iomap_alias_rw(0xd1, sio, 2);
247         io->set_iomap_alias_rw(0xd2, sio, 1);
248         io->set_iomap_alias_rw(0xd3, sio, 3);
249 #endif
250 #ifdef SUPPORT_16BIT_BOARD
251         io->set_iomap_range_rw(0xd4, 0xd7, pio_to16);
252 #endif
253 #ifdef _MZ80B
254         io->set_iomap_range_w(0xb4, 0xb4, memory);
255 #endif
256         io->set_iomap_range_rw(0xd8, 0xdb, fdc);
257         io->set_iomap_range_w(0xdc, 0xdd, floppy);
258         io->set_iomap_range_rw(0xe0, 0xe3, pio_i);
259         io->set_iomap_range_rw(0xe4, 0xe7, pit);
260         io->set_iomap_range_rw(0xe8, 0xeb, pio);
261         io->set_iomap_range_w(0xf0, 0xf3, timer);
262         io->set_iomap_range_w(0xf4, 0xf7, memory);
263         io->set_iomap_range_rw(0xf8, 0xfa, mz1r12);
264         io->set_iomap_range_rw(0xfe, 0xff, printer);
265         
266         io->set_iowait_range_rw(0xd8, 0xdf, 1);
267         io->set_iowait_range_rw(0xe8, 0xeb, 1);
268
269         // initialize all devices
270 #if defined(__GIT_REPO_VERSION)
271         strncpy(_git_revision, __GIT_REPO_VERSION, sizeof(_git_revision) - 1);
272 #endif
273         for(DEVICE* device = first_device; device; device = device->next_device) {
274                 device->initialize();
275         }
276         for(int drv = 0; drv < MAX_DRIVE; drv++) {
277 //              if(config.drive_type) {
278 //                      fdc->set_drive_type(drv, DRIVE_TYPE_2D);
279 //              } else {
280                         fdc->set_drive_type(drv, DRIVE_TYPE_2DD);
281 //              }
282         }
283 }
284
285 VM::~VM()
286 {
287         // delete all devices
288         for(DEVICE* device = first_device; device;) {
289                 DEVICE *next_device = device->next_device;
290                 device->release();
291                 delete device;
292                 device = next_device;
293         }
294 }
295
296 DEVICE* VM::get_device(int id)
297 {
298         for(DEVICE* device = first_device; device; device = device->next_device) {
299                 if(device->this_device_id == id) {
300                         return device;
301                 }
302         }
303         return NULL;
304 }
305
306 // ----------------------------------------------------------------------------
307 // drive virtual machine
308 // ----------------------------------------------------------------------------
309
310 void VM::reset()
311 {
312         // reset all devices
313         for(DEVICE* device = first_device; device; device = device->next_device) {
314                 device->reset();
315         }
316 }
317
318 void VM::special_reset()
319 {
320         // reset all devices
321 //      for(DEVICE* device = first_device; device; device = device->next_device) {
322 //              device->special_reset();
323 //      }
324         memory->special_reset();
325         cpu->reset();
326 #ifdef SUPPORT_16BIT_BOARD
327         pio_to16->reset();
328         cpu_16->reset();
329         pic_16->reset();
330         mz1m01->reset();
331 #endif
332 }
333
334 void VM::run()
335 {
336         event->drive();
337 }
338
339 // ----------------------------------------------------------------------------
340 // debugger
341 // ----------------------------------------------------------------------------
342
343 #ifdef USE_DEBUGGER
344 DEVICE *VM::get_cpu(int index)
345 {
346         if(index == 0) {
347                 return cpu;
348 #ifdef SUPPORT_16BIT_BOARD
349         } else if(index == 1) {
350                 return cpu_16;
351 #endif
352         }
353         return NULL;
354 }
355 #endif
356
357 // ----------------------------------------------------------------------------
358 // draw screen
359 // ----------------------------------------------------------------------------
360
361 void VM::draw_screen()
362 {
363         memory->draw_screen();
364 }
365
366 // ----------------------------------------------------------------------------
367 // soud manager
368 // ----------------------------------------------------------------------------
369
370 void VM::initialize_sound(int rate, int samples)
371 {
372         // init sound manager
373         event->initialize_sound(rate, samples);
374         
375         // init sound gen
376         pcm->initialize_sound(rate, 8000);
377 }
378
379 uint16_t* VM::create_sound(int* extra_frames)
380 {
381         return event->create_sound(extra_frames);
382 }
383
384 int VM::get_sound_buffer_ptr()
385 {
386         return event->get_sound_buffer_ptr();
387 }
388
389 #ifdef USE_SOUND_VOLUME
390 void VM::set_sound_device_volume(int ch, int decibel_l, int decibel_r)
391 {
392         if(ch == 0) {
393                 pcm->set_volume(0, decibel_l, decibel_r);
394         } else if(ch == 1) {
395                 drec->set_volume(0, decibel_l, decibel_r);
396         } else if(ch == 2) {
397                 fdc->get_context_noise_seek()->set_volume(0, decibel_l, decibel_r);
398                 fdc->get_context_noise_head_down()->set_volume(0, decibel_l, decibel_r);
399                 fdc->get_context_noise_head_up()->set_volume(0, decibel_l, decibel_r);
400         } else if(ch == 3) {
401                 drec->get_context_noise_play()->set_volume(0, decibel_l, decibel_r);
402                 drec->get_context_noise_stop()->set_volume(0, decibel_l, decibel_r);
403                 drec->get_context_noise_fast()->set_volume(0, decibel_l, decibel_r);
404         }
405 }
406 #endif
407
408 // ----------------------------------------------------------------------------
409 // user interface
410 // ----------------------------------------------------------------------------
411
412 void VM::open_floppy_disk(int drv, const _TCHAR* file_path, int bank)
413 {
414         fdc->open_disk(drv, file_path, bank);
415         
416         if(fdc->get_media_type(drv) == MEDIA_TYPE_2DD) {
417                 if(fdc->get_drive_type(drv) == DRIVE_TYPE_2D) {
418                         fdc->set_drive_type(drv, DRIVE_TYPE_2DD);
419                 }
420         } else if(fdc->get_media_type(drv) == MEDIA_TYPE_2D) {
421                 if(fdc->get_drive_type(drv) == DRIVE_TYPE_2DD) {
422                         fdc->set_drive_type(drv, DRIVE_TYPE_2D);
423                 }
424         }
425 }
426
427 void VM::close_floppy_disk(int drv)
428 {
429         fdc->close_disk(drv);
430 }
431
432 bool VM::is_floppy_disk_inserted(int drv)
433 {
434         return fdc->is_disk_inserted(drv);
435 }
436
437 void VM::is_floppy_disk_protected(int drv, bool value)
438 {
439         fdc->is_disk_protected(drv, value);
440 }
441
442 bool VM::is_floppy_disk_protected(int drv)
443 {
444         return fdc->is_disk_protected(drv);
445 }
446
447 uint32_t VM::is_floppy_disk_accessed()
448 {
449         return fdc->read_signal(0);
450 }
451
452 #ifdef SUPPORT_QUICK_DISK
453 void VM::open_quick_disk(int drv, const _TCHAR* file_path)
454 {
455         if(drv == 0) {
456                 qd->open_disk(file_path);
457         }
458 }
459
460 void VM::close_quick_disk(int drv)
461 {
462         if(drv == 0) {
463                 qd->close_disk();
464         }
465 }
466
467 bool VM::is_quick_disk_inserted(int drv)
468 {
469         if(drv == 0) {
470                 return qd->is_disk_inserted();
471         } else {
472                 return false;
473         }
474 }
475
476 uint32_t VM::is_quick_disk_accessed()
477 {
478         return qd->read_signal(0);
479 }
480 #endif
481
482 void VM::play_tape(int drv, const _TCHAR* file_path)
483 {
484         if(check_file_extension(file_path, _T(".dat"))) {
485                 memory->load_dat_image(file_path);
486                 return;
487         } else if(check_file_extension(file_path, _T(".mzt")) || check_file_extension(file_path, _T(".mzf"))) {
488                 if(config.direct_load_mzt[0] && memory->load_mzt_image(file_path)) {
489                         return;
490                 }
491         } else if(check_file_extension(file_path, _T(".mtw"))) {
492                 memory->load_mzt_image(file_path);
493         }
494         bool remote = drec->get_remote();
495         bool opened = drec->play_tape(file_path);
496         
497         if(opened && remote) {
498                 // if machine already sets remote on, start playing now
499                 push_play(drv);
500         }
501         cmt->close_tape();
502         cmt->play_tape(opened);
503 }
504
505 void VM::rec_tape(int drv, const _TCHAR* file_path)
506 {
507         bool remote = drec->get_remote();
508         bool opened = drec->rec_tape(file_path);
509         
510         if(opened && remote) {
511                 // if machine already sets remote on, start recording now
512                 push_play(drv);
513         }
514         cmt->close_tape();
515         cmt->rec_tape(opened);
516 }
517
518 void VM::close_tape(int drv)
519 {
520         emu->lock_vm();
521         drec->close_tape();
522         emu->unlock_vm();
523         drec->set_remote(false);
524         cmt->close_tape();
525 }
526
527 bool VM::is_tape_inserted(int drv)
528 {
529         return drec->is_tape_inserted();
530 }
531
532 bool VM::is_tape_playing(int drv)
533 {
534         return drec->is_tape_playing();
535 }
536
537 bool VM::is_tape_recording(int drv)
538 {
539         return drec->is_tape_recording();
540 }
541
542 int VM::get_tape_position(int drv)
543 {
544         return drec->get_tape_position();
545 }
546
547 const _TCHAR* VM::get_tape_message(int drv)
548 {
549         return drec->get_message();
550 }
551
552 void VM::push_play(int drv)
553 {
554         drec->set_remote(false);
555         drec->set_ff_rew(0);
556         drec->set_remote(true);
557 }
558
559 void VM::push_stop(int drv)
560 {
561         drec->set_remote(false);
562 }
563
564 void VM::push_fast_forward(int drv)
565 {
566         drec->set_remote(false);
567         drec->set_ff_rew(1);
568         drec->set_remote(true);
569 }
570
571 void VM::push_fast_rewind(int drv)
572 {
573         drec->set_remote(false);
574         drec->set_ff_rew(-1);
575         drec->set_remote(true);
576 }
577
578 bool VM::is_frame_skippable()
579 {
580         return event->is_frame_skippable();
581 }
582
583 void VM::update_config()
584 {
585         for(DEVICE* device = first_device; device; device = device->next_device) {
586                 device->update_config();
587         }
588 }
589
590 #define STATE_VERSION   4
591
592 bool VM::process_state(FILEIO* state_fio, bool loading)
593 {
594         if(!state_fio->StateCheckUint32(STATE_VERSION)) {
595                 return false;
596         }
597         for(DEVICE* device = first_device; device; device = device->next_device) {
598                 // Note: typeid(foo).name is fixed by recent ABI.Not dec 6.
599                 // const char *name = typeid(*device).name();
600                 //       But, using get_device_name() instead of typeid(foo).name() 20181008 K.O
601                 const char *name = device->get_device_name();
602                 int len = strlen(name);
603                 
604                 if(!state_fio->StateCheckInt32(len)) {
605                         if(loading) {
606                                 printf("Class name len Error: DEVID=%d EXPECT=%s\n", device->this_device_id, name);
607                         }
608                         return false;
609                 }
610                 if(!state_fio->StateCheckBuffer(name, len, 1)) {
611                         if(loading) {
612                                 printf("Class name Error: DEVID=%d EXPECT=%s\n", device->this_device_id, name);
613                         }
614                         return false;
615                 }
616                 if(!device->process_state(state_fio, loading)) {
617                         if(loading) {
618                                 printf("Data loading Error: DEVID=%d\n", device->this_device_id);
619                         }
620                         return false;
621                 }
622         }
623         return true;
624 }