OSDN Git Service

[VM][STATE] Update state feature with some VMs, some devices.
[csp-qt/common_source_project-fm7.git] / source / src / vm / mz2500 / mz2500.cpp
1 /*
2         SHARP MZ-2500 Emulator 'EmuZ-2500'
3
4         Author : Takeda.Toshiya
5         Date   : 2006.11.24 -
6
7         [ virtual machine ]
8 */
9
10 #include "mz2500.h"
11 #include "../../emu.h"
12 #include "../device.h"
13 #include "../event.h"
14
15 #include "../datarec.h"
16 #include "../disk.h"
17 #include "../harddisk.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 "../pcpr201.h"
26 #include "../prnfile.h"
27 #include "../rp5c01.h"
28 #include "../scsi_hdd.h"
29 #include "../scsi_host.h"
30 #include "../w3100a.h"
31 #include "../ym2203.h"
32 #include "../z80.h"
33 #include "../z80pio.h"
34 #include "../z80sio.h"
35
36 #ifdef USE_DEBUGGER
37 #include "../debugger.h"
38 #endif
39
40 #include "calendar.h"
41 #include "cmt.h"
42 #include "crtc.h"
43 #include "floppy.h"
44 #include "interrupt.h"
45 #include "joystick.h"
46 #include "keyboard.h"
47 #include "memory.h"
48 #include "mouse.h"
49 #include "mz1e26.h"
50 #include "mz1e30.h"
51 #include "mz1r13.h"
52 #include "mz1r37.h"
53 #include "printer.h"
54 #include "serial.h"
55 #include "timer.h"
56
57 // ----------------------------------------------------------------------------
58 // initialize
59 // ----------------------------------------------------------------------------
60
61 VM::VM(EMU* parent_emu) : emu(parent_emu)
62 {
63         // create devices
64         first_device = last_device = NULL;
65         dummy = new DEVICE(this, emu);  // must be 1st device
66         event = new EVENT(this, emu);   // must be 2nd device
67         dummy->set_device_name(_T("1st Dummy"));
68         
69         drec = new DATAREC(this, emu);
70         drec->set_context_noise_play(new NOISE(this, emu));
71         drec->set_context_noise_stop(new NOISE(this, emu));
72         drec->set_context_noise_fast(new NOISE(this, emu));
73         pit = new I8253(this, emu);
74         pio_i = new I8255(this, emu);
75         io = new IO(this, emu);
76         fdc = new MB8877(this, emu);
77         fdc->set_context_noise_seek(new NOISE(this, emu));
78         fdc->set_context_noise_head_down(new NOISE(this, emu));
79         fdc->set_context_noise_head_up(new NOISE(this, emu));
80         pcm = new PCM1BIT(this, emu);
81         rtc = new RP5C01(this, emu);    // RP-5C15
82         sasi_host = new SCSI_HOST(this, emu);
83         sasi_hdd = new SCSI_HDD(this, emu);
84         sasi_hdd->set_device_name(_T("SASI Hard Disk Drive"));
85         sasi_hdd->scsi_id = 0;
86         sasi_hdd->bytes_per_sec = 32 * 1024; // 32KB/s
87         for(int i = 0; i < USE_HARD_DISK; i++) {
88                 sasi_hdd->set_disk_handler(i, new HARDDISK(emu));
89         }
90         sasi_hdd->set_context_interface(sasi_host);
91         sasi_host->set_context_target(sasi_hdd);
92         w3100a = new W3100A(this, emu);
93         opn = new YM2203(this, emu);
94         cpu = new Z80(this, emu);
95         pio = new Z80PIO(this, emu);
96         sio = new Z80SIO(this, emu);
97
98         pio_i->set_device_name(_T("i8255 PIO(CMT/CRTC)"));
99         pio->set_device_name(_T("Z80 PIO(KEYBOARD/CRTC)"));
100         sio->set_device_name(_T("Z80 SIO(MOUSE)"));
101
102         
103         calendar = new CALENDAR(this, emu);
104         cmt = new CMT(this, emu);
105         crtc = new CRTC(this, emu);
106         floppy = new FLOPPY(this, emu);
107         interrupt = new INTERRUPT(this, emu);
108         joystick = new JOYSTICK(this, emu);
109         keyboard = new KEYBOARD(this, emu);
110         memory = new MEMORY(this, emu);
111         mouse = new MOUSE(this, emu);
112         mz1e26 = new MZ1E26(this, emu);
113         mz1e30 = new MZ1E30(this, emu);
114         mz1r13 = new MZ1R13(this, emu);
115         mz1r37 = new MZ1R37(this, emu);
116         printer = new PRINTER(this, emu);
117         serial = new SERIAL(this, emu);
118         timer = new TIMER(this, emu);
119         // set contexts
120         event->set_context_cpu(cpu);
121         event->set_context_sound(opn);
122         event->set_context_sound(pcm);
123         event->set_context_sound(drec);
124         event->set_context_sound(fdc->get_context_noise_seek());
125         event->set_context_sound(fdc->get_context_noise_head_down());
126         event->set_context_sound(fdc->get_context_noise_head_up());
127         event->set_context_sound(drec->get_context_noise_play());
128         event->set_context_sound(drec->get_context_noise_stop());
129         event->set_context_sound(drec->get_context_noise_fast());
130         
131         drec->set_context_ear(cmt, SIG_CMT_OUT, 1);
132         drec->set_context_remote(cmt, SIG_CMT_REMOTE, 1);
133         drec->set_context_end(cmt, SIG_CMT_END, 1);
134         drec->set_context_top(cmt, SIG_CMT_TOP, 1);
135         
136         pit->set_context_ch0(interrupt, SIG_INTERRUPT_I8253, 1);
137         pit->set_context_ch0(pit, SIG_I8253_CLOCK_1, 1);
138         pit->set_context_ch1(pit, SIG_I8253_CLOCK_2, 1);
139         pit->set_constant_clock(0, 31250);
140         pio_i->set_context_port_a(cmt, SIG_CMT_PIO_PA, 0xff, 0);
141         pio_i->set_context_port_c(cmt, SIG_CMT_PIO_PC, 0xff, 0);
142         pio_i->set_context_port_c(crtc, SIG_CRTC_MASK, 0x01, 0);
143         pio_i->set_context_port_c(pcm, SIG_PCM1BIT_SIGNAL, 0x04, 0);
144         // Sound:: Force realtime rendering. This is temporally fix. 20161024 K.O
145         //pcm->set_realtime_render(true);
146
147         
148         rtc->set_context_alarm(interrupt, SIG_INTERRUPT_RP5C15, 1);
149         rtc->set_context_pulse(opn, SIG_YM2203_PORT_B, 8);
150         sasi_host->set_context_irq(mz1e30, SIG_MZ1E30_IRQ, 1);
151         sasi_host->set_context_drq(mz1e30, SIG_MZ1E30_DRQ, 1);
152         opn->set_context_port_a(floppy, SIG_FLOPPY_REVERSE, 0x02, 0);
153         opn->set_context_port_a(crtc, SIG_CRTC_PALLETE, 0x04, 0);
154         opn->set_context_port_a(mouse, SIG_MOUSE_SEL, 0x08, 0);
155         pio->set_context_port_a(crtc, SIG_CRTC_COLUMN_SIZE, 0x20, 0);
156         pio->set_context_port_a(keyboard, SIG_KEYBOARD_COLUMN, 0x1f, 0);
157         sio->set_context_dtr(1, mouse, SIG_MOUSE_DTR, 1);
158         
159         calendar->set_context_rtc(rtc);
160         cmt->set_context_pio(pio_i);
161         cmt->set_context_drec(drec);
162         crtc->set_context_mem(memory);
163         crtc->set_context_int(interrupt);
164         crtc->set_context_pio(pio_i);
165         crtc->set_vram_ptr(memory->get_vram());
166         crtc->set_tvram_ptr(memory->get_tvram());
167         crtc->set_kanji_ptr(memory->get_kanji());
168         crtc->set_pcg_ptr(memory->get_pcg());
169         floppy->set_context_fdc(fdc);
170         keyboard->set_context_pio_i(pio_i);
171         keyboard->set_context_pio(pio);
172         memory->set_context_cpu(cpu);
173         memory->set_context_crtc(crtc);
174         mouse->set_context_sio(sio);
175         mz1e30->set_context_host(sasi_host);
176         if(config.printer_type == 0) {  
177                 printer->set_context_prn(new PRNFILE(this, emu));
178         } else if(config.printer_type == 1) {
179                 MZ1P17 *mz1p17 = new MZ1P17(this, emu);
180                 mz1p17->mode = MZ1P17_MODE_MZ1;
181                 printer->set_context_prn(mz1p17);
182 //      } else if(config.printer_type == 2) {
183 //              printer->set_context_prn(new PCPR201(this, emu));
184         } else {
185                 printer->set_context_prn(dummy);
186         }
187         serial->set_context_sio(sio);
188         timer->set_context_pit(pit);
189         
190         // cpu bus
191         cpu->set_context_mem(memory);
192         cpu->set_context_io(io);
193         cpu->set_context_intr(pio);
194 #ifdef USE_DEBUGGER
195         cpu->set_context_debugger(new DEBUGGER(this, emu));
196 #endif
197         
198         // z80 family daisy chain
199         pio->set_context_intr(cpu, 0);
200         pio->set_context_child(sio);
201         sio->set_context_intr(cpu, 1);
202         sio->set_context_child(interrupt);
203         interrupt->set_context_intr(cpu, 2);
204         
205         // i/o bus
206         io->set_iomap_range_rw(0x60, 0x63, w3100a);
207         io->set_iomap_range_rw(0xa0, 0xa3, serial);
208         io->set_iomap_range_rw(0xa4, 0xa5, mz1e30);
209         io->set_iomap_range_rw(0xa8, 0xa9, mz1e30);
210         io->set_iomap_range_rw(0xac, 0xad, mz1r37);
211         io->set_iomap_single_w(0xae, crtc);
212         io->set_iomap_range_rw(0xb0, 0xb3, serial);
213         io->set_iomap_range_rw(0xb4, 0xb5, memory);
214         io->set_iomap_range_rw(0xb8, 0xb9, mz1r13);
215         io->set_iomap_range_rw(0xbc, 0xbf, crtc);
216         io->set_iomap_range_w(0xc6, 0xc7, interrupt);
217         io->set_iomap_range_rw(0xc8, 0xc9, opn);
218         io->set_iomap_single_rw(0xca, mz1e26);
219         io->set_iomap_single_rw(0xcc, calendar);
220         io->set_iomap_single_w(0xcd, serial);
221         io->set_iomap_range_w(0xce, 0xcf, memory);
222         io->set_iomap_range_rw(0xd8, 0xdb, fdc);
223         io->set_iomap_range_w(0xdc, 0xdd, floppy);
224         io->set_iomap_range_rw(0xe0, 0xe3, pio_i);
225         io->set_iomap_range_rw(0xe4, 0xe7, pit);
226         io->set_iomap_range_rw(0xe8, 0xeb, pio);
227         io->set_iomap_single_rw(0xef, joystick);
228         io->set_iomap_range_w(0xf0, 0xf3, timer);
229         io->set_iomap_range_rw(0xf4, 0xf7, crtc);
230         io->set_iomap_range_rw(0xfe, 0xff, printer);
231         
232         io->set_iowait_range_rw(0xc8, 0xc9, 1);
233         io->set_iowait_single_rw(0xcc, 3);
234         io->set_iowait_range_rw(0xd8, 0xdf, 1);
235         io->set_iowait_range_rw(0xe8, 0xeb, 1);
236         
237         // initialize all devices
238         for(DEVICE* device = first_device; device; device = device->next_device) {
239                 device->initialize();
240         }
241         for(int drv = 0; drv < USE_HARD_DISK; drv++) {
242 #if defined(OPEN_HARD_DISK_IN_RESET)
243                 create_local_path(hd_file_path[drv], _MAX_PATH, _T("SASI%d.DAT"), drv);
244 #else
245                 open_hard_disk_tmp(drv, create_local_path(_T("SASI%d.DAT"), drv));
246 #endif
247         }
248         monitor_type = config.monitor_type;
249 }
250
251 VM::~VM()
252 {
253         // delete all devices
254         for(DEVICE* device = first_device; device;) {
255                 DEVICE *next_device = device->next_device;
256                 device->release();
257                 delete device;
258                 device = next_device;
259         }
260 }
261
262 DEVICE* VM::get_device(int id)
263 {
264         for(DEVICE* device = first_device; device; device = device->next_device) {
265                 if(device->this_device_id == id) {
266                         return device;
267                 }
268         }
269         return NULL;
270 }
271
272 // ----------------------------------------------------------------------------
273 // drive virtual machine
274 // ----------------------------------------------------------------------------
275
276 void VM::reset()
277 {
278         // reset all devices
279         for(DEVICE* device = first_device; device; device = device->next_device) {
280                 device->reset();
281         }
282         for(int i = 0; i < MAX_DRIVE; i++) {
283                 if(config.drive_type) {
284                         fdc->set_drive_type(i, DRIVE_TYPE_2D);
285                 } else {
286                         fdc->set_drive_type(i, DRIVE_TYPE_2DD);
287                 }
288         }
289         
290         // set initial port status
291         opn->write_signal(SIG_YM2203_PORT_B, (monitor_type & 2) ? 0x77 : 0x37, 0xff);
292         
293 #if defined(OPEN_HARD_DISK_IN_RESET)
294         // open/close hard disk images
295         for(int drv = 0; drv < USE_HARD_DISK; drv++) {
296                 if(hd_file_path[drv][0] != _T('\0')) {
297                         open_hard_disk_tmp(drv, hd_file_path[drv]);
298                 } else {
299                         close_hard_disk_tmp(drv);
300                 }
301         }
302 #endif
303 }
304
305 void VM::special_reset()
306 {
307         // reset all devices
308 //      for(DEVICE* device = first_device; device; device = device->next_device) {
309 //              device->special_reset();
310 //      }
311         memory->special_reset();
312         cpu->reset();
313 }
314
315 void VM::run()
316 {
317         event->drive();
318 }
319
320 double VM::get_frame_rate()
321 {
322         return event->get_frame_rate();
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         }
335         return NULL;
336 }
337 #endif
338
339 // ----------------------------------------------------------------------------
340 // draw screen
341 // ----------------------------------------------------------------------------
342
343 void VM::draw_screen()
344 {
345         crtc->draw_screen();
346 }
347
348 // ----------------------------------------------------------------------------
349 // soud manager
350 // ----------------------------------------------------------------------------
351
352 void VM::initialize_sound(int rate, int samples)
353 {
354         // init sound manager
355         event->initialize_sound(rate, samples);
356         
357         // init sound gen
358         opn->initialize_sound(rate, 2000000, samples, 0, -8);
359         pcm->initialize_sound(rate, 4096);
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                 opn->set_volume(0, decibel_l, decibel_r);
377         } else if(ch == 1) {
378                 opn->set_volume(1, decibel_l, decibel_r);
379         } else if(ch == 2) {
380                 pcm->set_volume(0, decibel_l, decibel_r);
381         } else if(ch == 3) {
382                 drec->set_volume(0, decibel_l, decibel_r);
383         } else if(ch == 4) {
384                 drec->set_volume(1, decibel_l, decibel_r);
385         } else if(ch == 5) {
386                 fdc->get_context_noise_seek()->set_volume(0, decibel_l, decibel_r);
387                 fdc->get_context_noise_head_down()->set_volume(0, decibel_l, decibel_r);
388                 fdc->get_context_noise_head_up()->set_volume(0, decibel_l, decibel_r);
389         } else if(ch == 6) {
390                 drec->get_context_noise_play()->set_volume(0, decibel_l, decibel_r);
391                 drec->get_context_noise_stop()->set_volume(0, decibel_l, decibel_r);
392                 drec->get_context_noise_fast()->set_volume(0, decibel_l, decibel_r);
393         }
394 }
395 #endif
396
397 // ----------------------------------------------------------------------------
398 // socket
399 // ----------------------------------------------------------------------------
400
401 void VM::notify_socket_connected(int ch)
402 {
403         w3100a->notify_connected(ch);
404 }
405
406 void VM::notify_socket_disconnected(int ch)
407 {
408         w3100a->notify_disconnected(ch);
409 }
410
411 uint8_t* VM::get_socket_send_buffer(int ch, int* size)
412 {
413         return w3100a->get_send_buffer(ch, size);
414 }
415
416 void VM::inc_socket_send_buffer_ptr(int ch, int size)
417 {
418         w3100a->inc_send_buffer_ptr(ch, size);
419 }
420
421 uint8_t* VM::get_socket_recv_buffer0(int ch, int* size0, int* size1)
422 {
423         return w3100a->get_recv_buffer0(ch, size0, size1);
424 }
425
426 uint8_t* VM::get_socket_recv_buffer1(int ch)
427 {
428         return w3100a->get_recv_buffer1(ch);
429 }
430
431 void VM::inc_socket_recv_buffer_ptr(int ch, int size)
432 {
433         w3100a->inc_recv_buffer_ptr(ch, size);
434 }
435
436 // ----------------------------------------------------------------------------
437 // user interface
438 // ----------------------------------------------------------------------------
439
440 void VM::open_floppy_disk(int drv, const _TCHAR* file_path, int bank)
441 {
442         fdc->open_disk(drv, file_path, bank);
443 }
444
445 void VM::close_floppy_disk(int drv)
446 {
447         fdc->close_disk(drv);
448 }
449
450 bool VM::is_floppy_disk_inserted(int drv)
451 {
452         return fdc->is_disk_inserted(drv);
453 }
454
455 void VM::is_floppy_disk_protected(int drv, bool value)
456 {
457         fdc->is_disk_protected(drv, value);
458 }
459
460 bool VM::is_floppy_disk_protected(int drv)
461 {
462         return fdc->is_disk_protected(drv);
463 }
464
465 uint32_t VM::is_floppy_disk_accessed()
466 {
467         return fdc->read_signal(0);
468 }
469
470 void VM::open_hard_disk(int drv, const _TCHAR* file_path)
471 {
472         if(drv < USE_HARD_DISK) {
473 #if defined(OPEN_HARD_DISK_IN_RESET)
474                 my_tcscpy_s(hd_file_path[drv], _MAX_PATH, file_path);
475 #else
476                 open_hard_disk_tmp(drv, file_path);
477 #endif
478         }
479 }
480
481 void VM::close_hard_disk(int drv)
482 {
483         if(drv < USE_HARD_DISK) {
484 #if defined(OPEN_HARD_DISK_IN_RESET)
485                 hd_file_path[drv][0] = _T('\0');
486 #else
487                 close_hard_disk_tmp(drv);
488 #endif
489         }
490 }
491
492 bool VM::is_hard_disk_inserted(int drv)
493 {
494         if(drv < USE_HARD_DISK) {
495 #if defined(OPEN_HARD_DISK_IN_RESET)
496                 return (hd_file_path[drv][0] != _T('\0'));
497 #else
498                 return is_hard_disk_inserted_tmp(drv);
499 #endif
500         }
501         return false;
502 }
503
504 uint32_t VM::is_hard_disk_accessed()
505 {
506         uint32_t status = 0;
507         
508         for(int drv = 0; drv < USE_HARD_DISK; drv++) {
509                 if(sasi_hdd->get_disk_handler(drv)->accessed()) {
510                         status |= 1 << drv;
511                 }
512         }
513         return status;
514 }
515
516 void VM::open_hard_disk_tmp(int drv, const _TCHAR* file_path)
517 {
518         if(drv < USE_HARD_DISK) {
519                 sasi_hdd->get_disk_handler(drv)->open(file_path);
520         }
521 }
522
523 void VM::close_hard_disk_tmp(int drv)
524 {
525         if(drv < USE_HARD_DISK) {
526                 sasi_hdd->get_disk_handler(drv)->close();
527         }
528 }
529
530 bool VM::is_hard_disk_inserted_tmp(int drv)
531 {
532         if(drv < USE_HARD_DISK) {
533                 return sasi_hdd->get_disk_handler(drv)->mounted();
534         }
535         return false;
536 }
537
538
539 void VM::play_tape(int drv, const _TCHAR* file_path)
540 {
541         bool value = drec->play_tape(file_path);
542         cmt->close_tape();
543         cmt->play_tape(value);
544 }
545
546 void VM::rec_tape(int drv, const _TCHAR* file_path)
547 {
548         bool value = drec->rec_tape(file_path);
549         cmt->close_tape();
550         cmt->rec_tape(value);
551 }
552
553 void VM::close_tape(int drv)
554 {
555         emu->lock_vm();
556         drec->close_tape();
557         emu->unlock_vm();
558         cmt->close_tape();
559 }
560
561 bool VM::is_tape_inserted(int drv)
562 {
563         return drec->is_tape_inserted();
564 }
565
566 bool VM::is_tape_playing(int drv)
567 {
568         return drec->is_tape_playing();
569 }
570
571 bool VM::is_tape_recording(int drv)
572 {
573         return drec->is_tape_recording();
574 }
575
576 int VM::get_tape_position(int drv)
577 {
578         return drec->get_tape_position();
579 }
580
581 const _TCHAR* VM::get_tape_message(int drv)
582 {
583         return drec->get_message();
584 }
585
586 void VM::push_play(int drv)
587 {
588         drec->set_ff_rew(0);
589         drec->set_remote(true);
590 }
591
592 void VM::push_stop(int drv)
593 {
594         drec->set_remote(false);
595 }
596
597 void VM::push_fast_forward(int drv)
598 {
599         drec->set_ff_rew(1);
600         drec->set_remote(true);
601 }
602
603 void VM::push_fast_rewind(int drv)
604 {
605         drec->set_ff_rew(-1);
606         drec->set_remote(true);
607 }
608
609 bool VM::is_frame_skippable()
610 {
611         return event->is_frame_skippable();
612 }
613
614 void VM::update_config()
615 {
616         for(DEVICE* device = first_device; device; device = device->next_device) {
617                 device->update_config();
618         }
619 }
620
621 #define STATE_VERSION   7
622
623 #include "../../statesub.h"
624
625 void VM::decl_state(void)
626 {
627         state_entry = new csp_state_utils(STATE_VERSION, 0, (_TCHAR *)(_T("CSP::MZ700_SERIES_HEAD")));
628         DECL_STATE_ENTRY_BOOL(monitor_type);
629         for(DEVICE* device = first_device; device; device = device->next_device) {
630                 device->decl_state();
631         }
632 }
633
634 void VM::save_state(FILEIO* state_fio)
635 {
636         //state_fio->FputUint32(STATE_VERSION);
637         if(state_entry != NULL) {
638                 state_entry->save_state(state_fio);
639         }
640         for(DEVICE* device = first_device; device; device = device->next_device) {
641                 device->save_state(state_fio);
642         }
643         //state_fio->FputInt32(monitor_type);
644 }
645
646 bool VM::load_state(FILEIO* state_fio)
647 {
648         bool mb = false;
649         if(state_entry != NULL) {
650                 mb = state_entry->save_state(state_fio);
651         }
652         if(!mb) return false;
653         //if(state_fio->FgetUint32() != STATE_VERSION) {
654         //      return false;
655         //}
656         for(DEVICE* device = first_device; device; device = device->next_device) {
657                 if(!device->load_state(state_fio)) {
658                         return false;
659                 }
660         }
661         //monitor_type = state_fio->FgetInt32();
662         //return true;
663 }
664