OSDN Git Service

d7e86ea8463a94dc001f6999289211f695a48bdc
[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) : VM_TEMPLATE(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 #if defined(__GIT_REPO_VERSION)
239         strncpy(_git_revision, __GIT_REPO_VERSION, sizeof(_git_revision) - 1);
240 #endif
241         for(DEVICE* device = first_device; device; device = device->next_device) {
242                 device->initialize();
243         }
244
245         for(int drv = 0; drv < USE_HARD_DISK; drv++) {
246 #if defined(OPEN_HARD_DISK_IN_RESET)
247                 create_local_path(hd_file_path[drv], _MAX_PATH, _T("SASI%d.DAT"), drv);
248 #else
249                 open_hard_disk_tmp(drv, create_local_path(_T("SASI%d.DAT"), drv));
250 #endif
251         }
252         monitor_type = config.monitor_type;
253         decl_state();
254 }
255
256 VM::~VM()
257 {
258         // delete all devices
259         for(DEVICE* device = first_device; device;) {
260                 DEVICE *next_device = device->next_device;
261                 device->release();
262                 delete device;
263                 device = next_device;
264         }
265 }
266
267 DEVICE* VM::get_device(int id)
268 {
269         for(DEVICE* device = first_device; device; device = device->next_device) {
270                 if(device->this_device_id == id) {
271                         return device;
272                 }
273         }
274         return NULL;
275 }
276
277 // ----------------------------------------------------------------------------
278 // drive virtual machine
279 // ----------------------------------------------------------------------------
280
281 void VM::reset()
282 {
283         // reset all devices
284         for(DEVICE* device = first_device; device; device = device->next_device) {
285                 device->reset();
286         }
287         for(int i = 0; i < MAX_DRIVE; i++) {
288                 if(config.drive_type) {
289                         fdc->set_drive_type(i, DRIVE_TYPE_2D);
290                 } else {
291                         fdc->set_drive_type(i, DRIVE_TYPE_2DD);
292                 }
293         }
294         
295         // set initial port status
296         opn->write_signal(SIG_YM2203_PORT_B, (monitor_type & 2) ? 0x77 : 0x37, 0xff);
297         
298 #if defined(OPEN_HARD_DISK_IN_RESET)
299         // open/close hard disk images
300         for(int drv = 0; drv < USE_HARD_DISK; drv++) {
301                 if(hd_file_path[drv][0] != _T('\0')) {
302                         open_hard_disk_tmp(drv, hd_file_path[drv]);
303                 } else {
304                         close_hard_disk_tmp(drv);
305                 }
306         }
307 #endif
308 }
309
310 void VM::special_reset()
311 {
312         // reset all devices
313 //      for(DEVICE* device = first_device; device; device = device->next_device) {
314 //              device->special_reset();
315 //      }
316         memory->special_reset();
317         cpu->reset();
318 }
319
320 void VM::run()
321 {
322         event->drive();
323 }
324
325 double VM::get_frame_rate()
326 {
327         return event->get_frame_rate();
328 }
329
330 // ----------------------------------------------------------------------------
331 // debugger
332 // ----------------------------------------------------------------------------
333
334 #ifdef USE_DEBUGGER
335 DEVICE *VM::get_cpu(int index)
336 {
337         if(index == 0) {
338                 return cpu;
339         }
340         return NULL;
341 }
342 #endif
343
344 // ----------------------------------------------------------------------------
345 // draw screen
346 // ----------------------------------------------------------------------------
347
348 void VM::draw_screen()
349 {
350         crtc->draw_screen();
351 }
352
353 // ----------------------------------------------------------------------------
354 // soud manager
355 // ----------------------------------------------------------------------------
356
357 void VM::initialize_sound(int rate, int samples)
358 {
359         // init sound manager
360         event->initialize_sound(rate, samples);
361         
362         // init sound gen
363         opn->initialize_sound(rate, 2000000, samples, 0, -8);
364         pcm->initialize_sound(rate, 4096);
365 }
366
367 uint16_t* VM::create_sound(int* extra_frames)
368 {
369         return event->create_sound(extra_frames);
370 }
371
372 int VM::get_sound_buffer_ptr()
373 {
374         return event->get_sound_buffer_ptr();
375 }
376
377 #ifdef USE_SOUND_VOLUME
378 void VM::set_sound_device_volume(int ch, int decibel_l, int decibel_r)
379 {
380         if(ch == 0) {
381                 opn->set_volume(0, decibel_l, decibel_r);
382         } else if(ch == 1) {
383                 opn->set_volume(1, decibel_l, decibel_r);
384         } else if(ch == 2) {
385                 pcm->set_volume(0, decibel_l, decibel_r);
386         } else if(ch == 3) {
387                 drec->set_volume(0, decibel_l, decibel_r);
388         } else if(ch == 4) {
389                 drec->set_volume(1, decibel_l, decibel_r);
390         } else if(ch == 5) {
391                 fdc->get_context_noise_seek()->set_volume(0, decibel_l, decibel_r);
392                 fdc->get_context_noise_head_down()->set_volume(0, decibel_l, decibel_r);
393                 fdc->get_context_noise_head_up()->set_volume(0, decibel_l, decibel_r);
394         } else if(ch == 6) {
395                 drec->get_context_noise_play()->set_volume(0, decibel_l, decibel_r);
396                 drec->get_context_noise_stop()->set_volume(0, decibel_l, decibel_r);
397                 drec->get_context_noise_fast()->set_volume(0, decibel_l, decibel_r);
398         }
399 }
400 #endif
401
402 // ----------------------------------------------------------------------------
403 // socket
404 // ----------------------------------------------------------------------------
405
406 void VM::notify_socket_connected(int ch)
407 {
408         w3100a->notify_connected(ch);
409 }
410
411 void VM::notify_socket_disconnected(int ch)
412 {
413         w3100a->notify_disconnected(ch);
414 }
415
416 uint8_t* VM::get_socket_send_buffer(int ch, int* size)
417 {
418         return w3100a->get_send_buffer(ch, size);
419 }
420
421 void VM::inc_socket_send_buffer_ptr(int ch, int size)
422 {
423         w3100a->inc_send_buffer_ptr(ch, size);
424 }
425
426 uint8_t* VM::get_socket_recv_buffer0(int ch, int* size0, int* size1)
427 {
428         return w3100a->get_recv_buffer0(ch, size0, size1);
429 }
430
431 uint8_t* VM::get_socket_recv_buffer1(int ch)
432 {
433         return w3100a->get_recv_buffer1(ch);
434 }
435
436 void VM::inc_socket_recv_buffer_ptr(int ch, int size)
437 {
438         w3100a->inc_recv_buffer_ptr(ch, size);
439 }
440
441 // ----------------------------------------------------------------------------
442 // user interface
443 // ----------------------------------------------------------------------------
444
445 void VM::open_floppy_disk(int drv, const _TCHAR* file_path, int bank)
446 {
447         fdc->open_disk(drv, file_path, bank);
448 }
449
450 void VM::close_floppy_disk(int drv)
451 {
452         fdc->close_disk(drv);
453 }
454
455 bool VM::is_floppy_disk_inserted(int drv)
456 {
457         return fdc->is_disk_inserted(drv);
458 }
459
460 void VM::is_floppy_disk_protected(int drv, bool value)
461 {
462         fdc->is_disk_protected(drv, value);
463 }
464
465 bool VM::is_floppy_disk_protected(int drv)
466 {
467         return fdc->is_disk_protected(drv);
468 }
469
470 uint32_t VM::is_floppy_disk_accessed()
471 {
472         return fdc->read_signal(0);
473 }
474
475 void VM::open_hard_disk(int drv, const _TCHAR* file_path)
476 {
477         if(drv < USE_HARD_DISK) {
478 #if defined(OPEN_HARD_DISK_IN_RESET)
479                 my_tcscpy_s(hd_file_path[drv], _MAX_PATH, file_path);
480 #else
481                 open_hard_disk_tmp(drv, file_path);
482 #endif
483         }
484 }
485
486 void VM::close_hard_disk(int drv)
487 {
488         if(drv < USE_HARD_DISK) {
489 #if defined(OPEN_HARD_DISK_IN_RESET)
490                 hd_file_path[drv][0] = _T('\0');
491 #else
492                 close_hard_disk_tmp(drv);
493 #endif
494         }
495 }
496
497 bool VM::is_hard_disk_inserted(int drv)
498 {
499         if(drv < USE_HARD_DISK) {
500 #if defined(OPEN_HARD_DISK_IN_RESET)
501                 return (hd_file_path[drv][0] != _T('\0'));
502 #else
503                 return is_hard_disk_inserted_tmp(drv);
504 #endif
505         }
506         return false;
507 }
508
509 uint32_t VM::is_hard_disk_accessed()
510 {
511         uint32_t status = 0;
512         
513         for(int drv = 0; drv < USE_HARD_DISK; drv++) {
514                 if(sasi_hdd->get_disk_handler(drv)->accessed()) {
515                         status |= 1 << drv;
516                 }
517         }
518         return status;
519 }
520
521 void VM::open_hard_disk_tmp(int drv, const _TCHAR* file_path)
522 {
523         if(drv < USE_HARD_DISK) {
524                 sasi_hdd->get_disk_handler(drv)->open(file_path);
525         }
526 }
527
528 void VM::close_hard_disk_tmp(int drv)
529 {
530         if(drv < USE_HARD_DISK) {
531                 sasi_hdd->get_disk_handler(drv)->close();
532         }
533 }
534
535 bool VM::is_hard_disk_inserted_tmp(int drv)
536 {
537         if(drv < USE_HARD_DISK) {
538                 return sasi_hdd->get_disk_handler(drv)->mounted();
539         }
540         return false;
541 }
542
543
544 void VM::play_tape(int drv, const _TCHAR* file_path)
545 {
546         bool value = drec->play_tape(file_path);
547         cmt->close_tape();
548         cmt->play_tape(value);
549 }
550
551 void VM::rec_tape(int drv, const _TCHAR* file_path)
552 {
553         bool value = drec->rec_tape(file_path);
554         cmt->close_tape();
555         cmt->rec_tape(value);
556 }
557
558 void VM::close_tape(int drv)
559 {
560         emu->lock_vm();
561         drec->close_tape();
562         emu->unlock_vm();
563         cmt->close_tape();
564 }
565
566 bool VM::is_tape_inserted(int drv)
567 {
568         return drec->is_tape_inserted();
569 }
570
571 bool VM::is_tape_playing(int drv)
572 {
573         return drec->is_tape_playing();
574 }
575
576 bool VM::is_tape_recording(int drv)
577 {
578         return drec->is_tape_recording();
579 }
580
581 int VM::get_tape_position(int drv)
582 {
583         return drec->get_tape_position();
584 }
585
586 const _TCHAR* VM::get_tape_message(int drv)
587 {
588         return drec->get_message();
589 }
590
591 void VM::push_play(int drv)
592 {
593         drec->set_ff_rew(0);
594         drec->set_remote(true);
595 }
596
597 void VM::push_stop(int drv)
598 {
599         drec->set_remote(false);
600 }
601
602 void VM::push_fast_forward(int drv)
603 {
604         drec->set_ff_rew(1);
605         drec->set_remote(true);
606 }
607
608 void VM::push_fast_rewind(int drv)
609 {
610         drec->set_ff_rew(-1);
611         drec->set_remote(true);
612 }
613
614 bool VM::is_frame_skippable()
615 {
616         return event->is_frame_skippable();
617 }
618
619 void VM::update_config()
620 {
621         for(DEVICE* device = first_device; device; device = device->next_device) {
622                 device->update_config();
623         }
624 }
625
626 #define STATE_VERSION   7
627
628 #include "../../statesub.h"
629 #include "../../qt/gui/csp_logger.h"
630 extern CSP_Logger DLL_PREFIX_I *csp_logger;
631
632 void VM::decl_state(void)
633 {
634         state_entry = new csp_state_utils(STATE_VERSION, 0, (_TCHAR *)(_T("CSP::MZ2500_SERIES_HEAD")), csp_logger);
635         DECL_STATE_ENTRY_BOOL(monitor_type);
636         for(DEVICE* device = first_device; device; device = device->next_device) {
637                 device->decl_state();
638         }
639 }
640
641 void VM::save_state(FILEIO* state_fio)
642 {
643         //state_fio->FputUint32(STATE_VERSION);
644         if(state_entry != NULL) {
645                 state_entry->save_state(state_fio);
646         }
647         for(DEVICE* device = first_device; device; device = device->next_device) {
648                 device->save_state(state_fio);
649         }
650
651         //state_fio->FputInt32(monitor_type);
652 }
653
654 bool VM::load_state(FILEIO* state_fio)
655 {
656         bool mb = false;
657         if(state_entry != NULL) {
658                 mb = state_entry->load_state(state_fio);
659         }
660         if(!mb) return false;
661         //if(state_fio->FgetUint32() != STATE_VERSION) {
662         //      return false;
663         //}
664         for(DEVICE* device = first_device; device; device = device->next_device) {
665                 if(!device->load_state(state_fio)) {
666                         return false;
667                 }
668         }
669         //monitor_type = state_fio->FgetInt32();
670         return true;
671 }
672