OSDN Git Service

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