OSDN Git Service

[VM][STATE][MZ2500][MZ700][MZ80K] Apply new state framework to some 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 // ----------------------------------------------------------------------------
54 // initialize
55 // ----------------------------------------------------------------------------
56
57 VM::VM(EMU* parent_emu) : emu(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         for(DEVICE* device = first_device; device; device = device->next_device) {
256                 device->initialize();
257         }
258         decl_state();
259 }
260
261 VM::~VM()
262 {
263         // delete all devices
264         for(DEVICE* device = first_device; device;) {
265                 DEVICE *next_device = device->next_device;
266                 device->release();
267                 delete device;
268                 device = next_device;
269         }
270 }
271
272 DEVICE* VM::get_device(int id)
273 {
274         for(DEVICE* device = first_device; device; device = device->next_device) {
275                 if(device->this_device_id == id) {
276                         return device;
277                 }
278         }
279         return NULL;
280 }
281
282 // ----------------------------------------------------------------------------
283 // drive virtual machine
284 // ----------------------------------------------------------------------------
285
286 void VM::reset()
287 {
288         // reset all devices
289         for(DEVICE* device = first_device; device; device = device->next_device) {
290                 device->reset();
291         }
292         for(int i = 0; i < MAX_DRIVE; i++) {
293                 if(config.drive_type) {
294                         fdc->set_drive_type(i, DRIVE_TYPE_2DD);
295                 } else {
296                         fdc->set_drive_type(i, DRIVE_TYPE_2D);
297                 }
298         }
299 }
300
301 void VM::special_reset()
302 {
303         // reset all devices
304 //      for(DEVICE* device = first_device; device; device = device->next_device) {
305 //              device->special_reset();
306 //      }
307         memory->special_reset();
308         cpu->reset();
309 #ifdef SUPPORT_16BIT_BOARD
310         pio_to16->reset();
311         cpu_16->reset();
312         pic_16->reset();
313         mz1m01->reset();
314 #endif
315 }
316
317 void VM::run()
318 {
319         event->drive();
320 }
321
322 // ----------------------------------------------------------------------------
323 // debugger
324 // ----------------------------------------------------------------------------
325
326 #ifdef USE_DEBUGGER
327 DEVICE *VM::get_cpu(int index)
328 {
329         if(index == 0) {
330                 return cpu;
331 #ifdef SUPPORT_16BIT_BOARD
332         } else if(index == 1) {
333                 return cpu_16;
334 #endif
335         }
336         return NULL;
337 }
338 #endif
339
340 // ----------------------------------------------------------------------------
341 // draw screen
342 // ----------------------------------------------------------------------------
343
344 void VM::draw_screen()
345 {
346         memory->draw_screen();
347 }
348
349 // ----------------------------------------------------------------------------
350 // soud manager
351 // ----------------------------------------------------------------------------
352
353 void VM::initialize_sound(int rate, int samples)
354 {
355         // init sound manager
356         event->initialize_sound(rate, samples);
357         
358         // init sound gen
359         pcm->initialize_sound(rate, 8000);
360 }
361
362 uint16_t* VM::create_sound(int* extra_frames)
363 {
364         return event->create_sound(extra_frames);
365 }
366
367 int VM::get_sound_buffer_ptr()
368 {
369         return event->get_sound_buffer_ptr();
370 }
371
372 #ifdef USE_SOUND_VOLUME
373 void VM::set_sound_device_volume(int ch, int decibel_l, int decibel_r)
374 {
375         if(ch == 0) {
376                 pcm->set_volume(0, decibel_l, decibel_r);
377         } else if(ch == 1) {
378                 drec->set_volume(0, decibel_l, decibel_r);
379         } else if(ch == 2) {
380                 fdc->get_context_noise_seek()->set_volume(0, decibel_l, decibel_r);
381                 fdc->get_context_noise_head_down()->set_volume(0, decibel_l, decibel_r);
382                 fdc->get_context_noise_head_up()->set_volume(0, decibel_l, decibel_r);
383         } else if(ch == 3) {
384                 drec->get_context_noise_play()->set_volume(0, decibel_l, decibel_r);
385                 drec->get_context_noise_stop()->set_volume(0, decibel_l, decibel_r);
386                 drec->get_context_noise_fast()->set_volume(0, decibel_l, decibel_r);
387         }
388 }
389 #endif
390
391 // ----------------------------------------------------------------------------
392 // user interface
393 // ----------------------------------------------------------------------------
394
395 void VM::open_floppy_disk(int drv, const _TCHAR* file_path, int bank)
396 {
397         fdc->open_disk(drv, file_path, bank);
398 }
399
400 void VM::close_floppy_disk(int drv)
401 {
402         fdc->close_disk(drv);
403 }
404
405 bool VM::is_floppy_disk_inserted(int drv)
406 {
407         return fdc->is_disk_inserted(drv);
408 }
409
410 void VM::is_floppy_disk_protected(int drv, bool value)
411 {
412         fdc->is_disk_protected(drv, value);
413 }
414
415 bool VM::is_floppy_disk_protected(int drv)
416 {
417         return fdc->is_disk_protected(drv);
418 }
419
420 uint32_t VM::is_floppy_disk_accessed()
421 {
422         return fdc->read_signal(0);
423 }
424
425 #ifdef SUPPORT_QUICK_DISK
426 void VM::open_quick_disk(int drv, const _TCHAR* file_path)
427 {
428         if(drv == 0) {
429                 qd->open_disk(file_path);
430         }
431 }
432
433 void VM::close_quick_disk(int drv)
434 {
435         if(drv == 0) {
436                 qd->close_disk();
437         }
438 }
439
440 bool VM::is_quick_disk_inserted(int drv)
441 {
442         if(drv == 0) {
443                 return qd->is_disk_inserted();
444         } else {
445                 return false;
446         }
447 }
448
449 uint32_t VM::is_quick_disk_accessed()
450 {
451         return qd->read_signal(0);
452 }
453 #endif
454
455 void VM::play_tape(int drv, const _TCHAR* file_path)
456 {
457         if(check_file_extension(file_path, _T(".dat"))) {
458                 memory->load_dat_image(file_path);
459                 return;
460         } else if(check_file_extension(file_path, _T(".mzt")) || check_file_extension(file_path, _T(".mzf"))) {
461                 if(config.direct_load_mzt[0] && memory->load_mzt_image(file_path)) {
462                         return;
463                 }
464         } else if(check_file_extension(file_path, _T(".mtw"))) {
465                 memory->load_mzt_image(file_path);
466         }
467         bool value = drec->play_tape(file_path);
468         cmt->close_tape();
469         cmt->play_tape(value);
470 }
471
472 void VM::rec_tape(int drv, const _TCHAR* file_path)
473 {
474         bool value = drec->rec_tape(file_path);
475         cmt->close_tape();
476         cmt->rec_tape(value);
477 }
478
479 void VM::close_tape(int drv)
480 {
481         emu->lock_vm();
482         drec->close_tape();
483         cmt->close_tape();
484         emu->unlock_vm();
485 }
486
487 bool VM::is_tape_inserted(int drv)
488 {
489         return drec->is_tape_inserted();
490 }
491
492 bool VM::is_tape_playing(int drv)
493 {
494         return drec->is_tape_playing();
495 }
496
497 bool VM::is_tape_recording(int drv)
498 {
499         return drec->is_tape_recording();
500 }
501
502 int VM::get_tape_position(int drv)
503 {
504         return drec->get_tape_position();
505 }
506
507 const _TCHAR* VM::get_tape_message(int drv)
508 {
509         return drec->get_message();
510 }
511
512 void VM::push_play(int drv)
513 {
514         drec->set_ff_rew(0);
515         drec->set_remote(true);
516 }
517
518 void VM::push_stop(int drv)
519 {
520         drec->set_remote(false);
521 }
522
523 void VM::push_fast_forward(int drv)
524 {
525         drec->set_ff_rew(1);
526         drec->set_remote(true);
527 }
528
529 void VM::push_fast_rewind(int drv)
530 {
531         drec->set_ff_rew(-1);
532         drec->set_remote(true);
533 }
534
535 bool VM::is_frame_skippable()
536 {
537         return event->is_frame_skippable();
538 }
539
540 void VM::update_config()
541 {
542         for(DEVICE* device = first_device; device; device = device->next_device) {
543                 device->update_config();
544         }
545 }
546
547 #define STATE_VERSION   4
548
549 #include "../../statesub.h"
550 #include "../../qt/gui/csp_logger.h"
551 extern CSP_Logger DLL_PREFIX_I *csp_logger;
552
553 void VM::decl_state(void)
554 {
555         state_entry = new csp_state_utils(STATE_VERSION, 0, (_TCHAR *)(_T("CSP::MZ80B_SERIES_HEAD")), csp_logger);
556         for(DEVICE* device = first_device; device; device = device->next_device) {
557                 device->decl_state();
558         }
559 }
560
561 void VM::save_state(FILEIO* state_fio)
562 {
563         //state_fio->FputUint32(STATE_VERSION);
564         if(state_entry != NULL) {
565                 state_entry->save_state(state_fio);
566         }
567         for(DEVICE* device = first_device; device; device = device->next_device) {
568                 device->save_state(state_fio);
569         }
570 }
571
572 bool VM::load_state(FILEIO* state_fio)
573 {
574         //if(state_fio->FgetUint32() != STATE_VERSION) {
575         //      return false;
576         //}
577         bool mb = false;
578         if(state_entry != NULL) {
579                 mb = state_entry->load_state(state_fio);
580         }
581         if(!mb) return false;
582         for(DEVICE* device = first_device; device; device = device->next_device) {
583                 if(!device->load_state(state_fio)) {
584                         return false;
585                 }
586         }
587         return true;
588 }
589