2 SHARP MZ-2500 Emulator 'EmuZ-2500'
4 Author : Takeda.Toshiya
11 #include "../../emu.h"
12 #include "../device.h"
15 #include "../datarec.h"
17 #include "../harddisk.h"
21 #include "../mb8877.h"
22 #include "../mz1p17.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"
33 #include "../z80pio.h"
34 #include "../z80sio.h"
37 #include "../debugger.h"
44 #include "interrupt.h"
57 // ----------------------------------------------------------------------------
59 // ----------------------------------------------------------------------------
61 VM::VM(EMU* parent_emu) : VM_TEMPLATE(parent_emu)
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"));
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));
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);
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)"));
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);
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());
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);
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);
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);
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));
185 printer->set_context_prn(dummy);
187 serial->set_context_sio(sio);
188 timer->set_context_pit(pit);
191 cpu->set_context_mem(memory);
192 cpu->set_context_io(io);
193 cpu->set_context_intr(pio);
195 cpu->set_context_debugger(new DEBUGGER(this, emu));
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);
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);
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);
237 // initialize all devices
238 #if defined(__GIT_REPO_VERSION)
239 strncpy(_git_revision, __GIT_REPO_VERSION, sizeof(_git_revision) - 1);
241 for(DEVICE* device = first_device; device; device = device->next_device) {
242 device->initialize();
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);
249 open_hard_disk_tmp(drv, create_local_path(_T("SASI%d.DAT"), drv));
252 monitor_type = config.monitor_type;
258 // delete all devices
259 for(DEVICE* device = first_device; device;) {
260 DEVICE *next_device = device->next_device;
263 device = next_device;
267 DEVICE* VM::get_device(int id)
269 for(DEVICE* device = first_device; device; device = device->next_device) {
270 if(device->this_device_id == id) {
277 // ----------------------------------------------------------------------------
278 // drive virtual machine
279 // ----------------------------------------------------------------------------
284 for(DEVICE* device = first_device; device; device = device->next_device) {
287 for(int i = 0; i < MAX_DRIVE; i++) {
288 if(config.drive_type) {
289 fdc->set_drive_type(i, DRIVE_TYPE_2D);
291 fdc->set_drive_type(i, DRIVE_TYPE_2DD);
295 // set initial port status
296 opn->write_signal(SIG_YM2203_PORT_B, (monitor_type & 2) ? 0x77 : 0x37, 0xff);
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]);
304 close_hard_disk_tmp(drv);
310 void VM::special_reset()
313 // for(DEVICE* device = first_device; device; device = device->next_device) {
314 // device->special_reset();
316 memory->special_reset();
325 double VM::get_frame_rate()
327 return event->get_frame_rate();
330 // ----------------------------------------------------------------------------
332 // ----------------------------------------------------------------------------
335 DEVICE *VM::get_cpu(int index)
344 // ----------------------------------------------------------------------------
346 // ----------------------------------------------------------------------------
348 void VM::draw_screen()
353 // ----------------------------------------------------------------------------
355 // ----------------------------------------------------------------------------
357 void VM::initialize_sound(int rate, int samples)
359 // init sound manager
360 event->initialize_sound(rate, samples);
363 opn->initialize_sound(rate, 2000000, samples, 0, -8);
364 pcm->initialize_sound(rate, 4096);
367 uint16_t* VM::create_sound(int* extra_frames)
369 return event->create_sound(extra_frames);
372 int VM::get_sound_buffer_ptr()
374 return event->get_sound_buffer_ptr();
377 #ifdef USE_SOUND_VOLUME
378 void VM::set_sound_device_volume(int ch, int decibel_l, int decibel_r)
381 opn->set_volume(0, decibel_l, decibel_r);
383 opn->set_volume(1, decibel_l, decibel_r);
385 pcm->set_volume(0, decibel_l, decibel_r);
387 drec->set_volume(0, decibel_l, decibel_r);
389 drec->set_volume(1, decibel_l, decibel_r);
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);
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);
402 // ----------------------------------------------------------------------------
404 // ----------------------------------------------------------------------------
406 void VM::notify_socket_connected(int ch)
408 w3100a->notify_connected(ch);
411 void VM::notify_socket_disconnected(int ch)
413 w3100a->notify_disconnected(ch);
416 uint8_t* VM::get_socket_send_buffer(int ch, int* size)
418 return w3100a->get_send_buffer(ch, size);
421 void VM::inc_socket_send_buffer_ptr(int ch, int size)
423 w3100a->inc_send_buffer_ptr(ch, size);
426 uint8_t* VM::get_socket_recv_buffer0(int ch, int* size0, int* size1)
428 return w3100a->get_recv_buffer0(ch, size0, size1);
431 uint8_t* VM::get_socket_recv_buffer1(int ch)
433 return w3100a->get_recv_buffer1(ch);
436 void VM::inc_socket_recv_buffer_ptr(int ch, int size)
438 w3100a->inc_recv_buffer_ptr(ch, size);
441 // ----------------------------------------------------------------------------
443 // ----------------------------------------------------------------------------
445 void VM::open_floppy_disk(int drv, const _TCHAR* file_path, int bank)
447 fdc->open_disk(drv, file_path, bank);
450 void VM::close_floppy_disk(int drv)
452 fdc->close_disk(drv);
455 bool VM::is_floppy_disk_inserted(int drv)
457 return fdc->is_disk_inserted(drv);
460 void VM::is_floppy_disk_protected(int drv, bool value)
462 fdc->is_disk_protected(drv, value);
465 bool VM::is_floppy_disk_protected(int drv)
467 return fdc->is_disk_protected(drv);
470 uint32_t VM::is_floppy_disk_accessed()
472 return fdc->read_signal(0);
475 void VM::open_hard_disk(int drv, const _TCHAR* file_path)
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);
481 open_hard_disk_tmp(drv, file_path);
486 void VM::close_hard_disk(int drv)
488 if(drv < USE_HARD_DISK) {
489 #if defined(OPEN_HARD_DISK_IN_RESET)
490 hd_file_path[drv][0] = _T('\0');
492 close_hard_disk_tmp(drv);
497 bool VM::is_hard_disk_inserted(int drv)
499 if(drv < USE_HARD_DISK) {
500 #if defined(OPEN_HARD_DISK_IN_RESET)
501 return (hd_file_path[drv][0] != _T('\0'));
503 return is_hard_disk_inserted_tmp(drv);
509 uint32_t VM::is_hard_disk_accessed()
513 for(int drv = 0; drv < USE_HARD_DISK; drv++) {
514 if(sasi_hdd->get_disk_handler(drv)->accessed()) {
521 void VM::open_hard_disk_tmp(int drv, const _TCHAR* file_path)
523 if(drv < USE_HARD_DISK) {
524 sasi_hdd->get_disk_handler(drv)->open(file_path);
528 void VM::close_hard_disk_tmp(int drv)
530 if(drv < USE_HARD_DISK) {
531 sasi_hdd->get_disk_handler(drv)->close();
535 bool VM::is_hard_disk_inserted_tmp(int drv)
537 if(drv < USE_HARD_DISK) {
538 return sasi_hdd->get_disk_handler(drv)->mounted();
544 void VM::play_tape(int drv, const _TCHAR* file_path)
546 bool value = drec->play_tape(file_path);
548 cmt->play_tape(value);
551 void VM::rec_tape(int drv, const _TCHAR* file_path)
553 bool value = drec->rec_tape(file_path);
555 cmt->rec_tape(value);
558 void VM::close_tape(int drv)
566 bool VM::is_tape_inserted(int drv)
568 return drec->is_tape_inserted();
571 bool VM::is_tape_playing(int drv)
573 return drec->is_tape_playing();
576 bool VM::is_tape_recording(int drv)
578 return drec->is_tape_recording();
581 int VM::get_tape_position(int drv)
583 return drec->get_tape_position();
586 const _TCHAR* VM::get_tape_message(int drv)
588 return drec->get_message();
591 void VM::push_play(int drv)
594 drec->set_remote(true);
597 void VM::push_stop(int drv)
599 drec->set_remote(false);
602 void VM::push_fast_forward(int drv)
605 drec->set_remote(true);
608 void VM::push_fast_rewind(int drv)
610 drec->set_ff_rew(-1);
611 drec->set_remote(true);
614 bool VM::is_frame_skippable()
616 return event->is_frame_skippable();
619 void VM::update_config()
621 for(DEVICE* device = first_device; device; device = device->next_device) {
622 device->update_config();
626 #define STATE_VERSION 7
628 #include "../../statesub.h"
629 #include "../../qt/gui/csp_logger.h"
630 extern CSP_Logger DLL_PREFIX_I *csp_logger;
632 void VM::decl_state(void)
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();
641 void VM::save_state(FILEIO* state_fio)
643 //state_fio->FputUint32(STATE_VERSION);
644 if(state_entry != NULL) {
645 state_entry->save_state(state_fio);
647 for(DEVICE* device = first_device; device; device = device->next_device) {
648 device->save_state(state_fio);
651 //state_fio->FputInt32(monitor_type);
654 bool VM::load_state(FILEIO* state_fio)
657 if(state_entry != NULL) {
658 mb = state_entry->load_state(state_fio);
660 if(!mb) return false;
661 //if(state_fio->FgetUint32() != STATE_VERSION) {
664 for(DEVICE* device = first_device; device; device = device->next_device) {
665 if(!device->load_state(state_fio)) {
669 //monitor_type = state_fio->FgetInt32();