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 using MZ2500::CALENDAR;
61 using MZ2500::INTERRUPT;
62 using MZ2500::JOYSTICK;
63 using MZ2500::KEYBOARD;
70 using MZ2500::PRINTER;
74 // ----------------------------------------------------------------------------
76 // ----------------------------------------------------------------------------
78 VM::VM(EMU_TEMPLATE* parent_emu) : VM_TEMPLATE(parent_emu)
81 first_device = last_device = NULL;
82 dummy = new DEVICE(this, emu); // must be 1st device
83 event = new EVENT(this, emu); // must be 2nd device
84 dummy->set_device_name(_T("1st Dummy"));
86 drec = new DATAREC(this, emu);
87 drec->set_context_noise_play(new NOISE(this, emu));
88 drec->set_context_noise_stop(new NOISE(this, emu));
89 drec->set_context_noise_fast(new NOISE(this, emu));
90 pit = new I8253(this, emu);
91 pio_i = new I8255(this, emu);
92 io = new IO(this, emu);
93 fdc = new MB8877(this, emu);
94 fdc->set_context_noise_seek(new NOISE(this, emu));
95 fdc->set_context_noise_head_down(new NOISE(this, emu));
96 fdc->set_context_noise_head_up(new NOISE(this, emu));
98 // fdc->set_context_debugger(new DEBUGGER(this, emu));
100 pcm = new PCM1BIT(this, emu);
102 // pcm->set_context_debugger(new DEBUGGER(this, emu));
104 rtc = new RP5C01(this, emu); // RP-5C15
105 sasi_host = new SCSI_HOST(this, emu);
106 sasi_hdd = new SASI_HDD(this, emu);
107 sasi_hdd->set_device_name(_T("SASI Hard Disk Drive"));
108 sasi_hdd->scsi_id = 0;
109 sasi_hdd->bytes_per_sec = 32 * 1024; // 32KB/s
110 for(int i = 0; i < USE_HARD_DISK; i++) {
111 sasi_hdd->set_disk_handler(i, new HARDDISK(emu));
113 sasi_hdd->set_context_interface(sasi_host);
114 sasi_host->set_context_target(sasi_hdd);
115 w3100a = new W3100A(this, emu);
116 opn = new YM2203(this, emu);
118 opn->set_context_debugger(new DEBUGGER(this, emu));
120 cpu = new Z80(this, emu);
121 pio = new Z80PIO(this, emu);
122 sio = new Z80SIO(this, emu);
124 pio_i->set_device_name(_T("i8255 PIO(CMT/CRTC)"));
125 pio->set_device_name(_T("Z80 PIO(KEYBOARD/CRTC)"));
126 sio->set_device_name(_T("Z80 SIO(MOUSE)"));
129 calendar = new CALENDAR(this, emu);
130 cmt = new CMT(this, emu);
131 crtc = new CRTC(this, emu);
132 floppy = new FLOPPY(this, emu);
133 interrupt = new INTERRUPT(this, emu);
134 joystick = new JOYSTICK(this, emu);
135 keyboard = new KEYBOARD(this, emu);
136 memory = new MEMORY(this, emu);
137 mouse = new MOUSE(this, emu);
138 mz1e26 = new MZ1E26(this, emu);
139 mz1e30 = new MZ1E30(this, emu);
140 mz1r13 = new MZ1R13(this, emu);
141 mz1r37 = new MZ1R37(this, emu);
142 printer = new PRINTER(this, emu);
143 serial = new SERIAL(this, emu);
144 timer = new TIMER(this, emu);
146 event->set_context_cpu(cpu);
147 event->set_context_sound(opn);
148 event->set_context_sound(pcm);
149 event->set_context_sound(drec);
150 event->set_context_sound(fdc->get_context_noise_seek());
151 event->set_context_sound(fdc->get_context_noise_head_down());
152 event->set_context_sound(fdc->get_context_noise_head_up());
153 event->set_context_sound(drec->get_context_noise_play());
154 event->set_context_sound(drec->get_context_noise_stop());
155 event->set_context_sound(drec->get_context_noise_fast());
157 drec->set_context_ear(cmt, SIG_CMT_OUT, 1);
158 drec->set_context_remote(cmt, SIG_CMT_REMOTE, 1);
159 drec->set_context_end(cmt, SIG_CMT_END, 1);
160 drec->set_context_top(cmt, SIG_CMT_TOP, 1);
162 pit->set_context_ch0(interrupt, SIG_INTERRUPT_I8253, 1);
163 pit->set_context_ch0(pit, SIG_I8253_CLOCK_1, 1);
164 pit->set_context_ch1(pit, SIG_I8253_CLOCK_2, 1);
165 pit->set_constant_clock(0, 31250);
166 pio_i->set_context_port_a(cmt, SIG_CMT_PIO_PA, 0xff, 0);
167 pio_i->set_context_port_c(cmt, SIG_CMT_PIO_PC, 0xff, 0);
168 pio_i->set_context_port_c(crtc, SIG_CRTC_MASK, 0x01, 0);
169 pio_i->set_context_port_c(pcm, SIG_PCM1BIT_SIGNAL, 0x04, 0);
170 // Sound:: Force realtime rendering. This is temporally fix. 20161024 K.O
171 //pcm->set_realtime_render(true);
174 rtc->set_context_alarm(interrupt, SIG_INTERRUPT_RP5C15, 1);
175 rtc->set_context_pulse(opn, SIG_YM2203_PORT_B, 8);
176 sasi_host->set_context_irq(mz1e30, SIG_MZ1E30_IRQ, 1);
177 sasi_host->set_context_drq(mz1e30, SIG_MZ1E30_DRQ, 1);
178 opn->set_context_port_a(floppy, SIG_FLOPPY_REVERSE, 0x02, 0);
179 opn->set_context_port_a(crtc, SIG_CRTC_PALLETE, 0x04, 0);
180 opn->set_context_port_a(mouse, SIG_MOUSE_SEL, 0x08, 0);
181 pio->set_context_port_a(crtc, SIG_CRTC_COLUMN_SIZE, 0x20, 0);
182 pio->set_context_port_a(keyboard, SIG_KEYBOARD_COLUMN, 0x1f, 0);
183 sio->set_context_dtr(1, mouse, SIG_MOUSE_DTR, 1);
185 calendar->set_context_rtc(rtc);
186 cmt->set_context_pio(pio_i);
187 cmt->set_context_drec(drec);
188 crtc->set_context_mem(memory);
189 crtc->set_context_int(interrupt);
190 crtc->set_context_pio(pio_i);
191 crtc->set_vram_ptr(memory->get_vram());
192 crtc->set_tvram_ptr(memory->get_tvram());
193 crtc->set_kanji_ptr(memory->get_kanji());
194 crtc->set_pcg_ptr(memory->get_pcg());
195 floppy->set_context_fdc(fdc);
196 keyboard->set_context_pio_i(pio_i);
197 keyboard->set_context_pio(pio);
198 memory->set_context_cpu(cpu);
199 memory->set_context_crtc(crtc);
200 mouse->set_context_sio(sio);
201 mz1e30->set_context_host(sasi_host);
202 if(config.printer_type == 0) {
203 printer->set_context_prn(new PRNFILE(this, emu));
204 } else if(config.printer_type == 1) {
205 MZ1P17 *mz1p17 = new MZ1P17(this, emu);
206 mz1p17->mode = MZ1P17_MODE_MZ1;
207 printer->set_context_prn(mz1p17);
208 // } else if(config.printer_type == 2) {
209 // printer->set_context_prn(new PCPR201(this, emu));
211 printer->set_context_prn(dummy);
213 serial->set_context_sio(sio);
214 timer->set_context_pit(pit);
217 cpu->set_context_mem(memory);
218 cpu->set_context_io(io);
219 cpu->set_context_intr(pio);
221 cpu->set_context_debugger(new DEBUGGER(this, emu));
224 // z80 family daisy chain
225 pio->set_context_intr(cpu, 0);
226 pio->set_context_child(sio);
227 sio->set_context_intr(cpu, 1);
228 sio->set_context_child(interrupt);
229 interrupt->set_context_intr(cpu, 2);
232 io->set_iomap_range_rw(0x60, 0x63, w3100a);
233 io->set_iomap_range_rw(0xa0, 0xa3, serial);
234 io->set_iomap_range_rw(0xa4, 0xa5, mz1e30);
235 io->set_iomap_range_rw(0xa8, 0xa9, mz1e30);
236 io->set_iomap_range_rw(0xac, 0xad, mz1r37);
237 io->set_iomap_single_w(0xae, crtc);
238 io->set_iomap_range_rw(0xb0, 0xb3, serial);
239 io->set_iomap_range_rw(0xb4, 0xb5, memory);
240 io->set_iomap_range_rw(0xb8, 0xb9, mz1r13);
241 io->set_iomap_range_rw(0xbc, 0xbf, crtc);
242 io->set_iomap_range_w(0xc6, 0xc7, interrupt);
243 io->set_iomap_range_rw(0xc8, 0xc9, opn);
244 io->set_iomap_single_rw(0xca, mz1e26);
245 io->set_iomap_single_rw(0xcc, calendar);
246 io->set_iomap_single_w(0xcd, serial);
247 io->set_iomap_range_w(0xce, 0xcf, memory);
248 io->set_iomap_range_rw(0xd8, 0xdb, fdc);
249 io->set_iomap_range_w(0xdc, 0xdd, floppy);
250 io->set_iomap_range_rw(0xe0, 0xe3, pio_i);
251 io->set_iomap_range_rw(0xe4, 0xe7, pit);
252 io->set_iomap_range_rw(0xe8, 0xeb, pio);
253 io->set_iomap_single_rw(0xef, joystick);
254 io->set_iomap_range_w(0xf0, 0xf3, timer);
255 io->set_iomap_range_rw(0xf4, 0xf7, crtc);
256 io->set_iomap_range_rw(0xfe, 0xff, printer);
258 io->set_iowait_range_rw(0xc8, 0xc9, 1);
259 io->set_iowait_single_rw(0xcc, 3);
260 io->set_iowait_range_rw(0xd8, 0xdf, 1);
261 io->set_iowait_range_rw(0xe8, 0xeb, 1);
263 // initialize all devices
264 #if defined(__GIT_REPO_VERSION)
265 strncpy(_git_revision, __GIT_REPO_VERSION, sizeof(_git_revision) - 1);
267 for(DEVICE* device = first_device; device; device = device->next_device) {
268 device->initialize();
271 for(int drv = 0; drv < MAX_DRIVE; drv++) {
272 // if(config.drive_type) {
273 // fdc->set_drive_type(drv, DRIVE_TYPE_2D);
275 fdc->set_drive_type(drv, DRIVE_TYPE_2DD);
278 for(int drv = 0; drv < USE_HARD_DISK; drv++) {
279 if(!(config.last_hard_disk_path[drv][0] != _T('\0') && FILEIO::IsFileExisting(config.last_hard_disk_path[drv]))) {
280 create_local_path(config.last_hard_disk_path[drv], _MAX_PATH, _T("SASI%d.DAT"), drv);
283 monitor_type = config.monitor_type;
288 // delete all devices
289 for(DEVICE* device = first_device; device;) {
290 DEVICE *next_device = device->next_device;
293 device = next_device;
297 DEVICE* VM::get_device(int id)
299 for(DEVICE* device = first_device; device; device = device->next_device) {
300 if(device->this_device_id == id) {
307 // ----------------------------------------------------------------------------
308 // drive virtual machine
309 // ----------------------------------------------------------------------------
314 for(DEVICE* device = first_device; device; device = device->next_device) {
318 // set initial port status
319 opn->write_signal(SIG_YM2203_PORT_B, (monitor_type & 2) ? 0x77 : 0x37, 0xff);
322 void VM::special_reset(int num)
325 // for(DEVICE* device = first_device; device; device = device->next_device) {
326 // device->special_reset();
328 memory->special_reset(num);
337 double VM::get_frame_rate()
339 return event->get_frame_rate();
342 // ----------------------------------------------------------------------------
344 // ----------------------------------------------------------------------------
347 DEVICE *VM::get_cpu(int index)
356 // ----------------------------------------------------------------------------
358 // ----------------------------------------------------------------------------
360 void VM::draw_screen()
365 // ----------------------------------------------------------------------------
367 // ----------------------------------------------------------------------------
369 void VM::initialize_sound(int rate, int samples)
371 // init sound manager
372 event->initialize_sound(rate, samples);
375 opn->initialize_sound(rate, 2000000, samples, 0, -8);
376 pcm->initialize_sound(rate, 4096);
379 uint16_t* VM::create_sound(int* extra_frames)
381 return event->create_sound(extra_frames);
384 int VM::get_sound_buffer_ptr()
386 return event->get_sound_buffer_ptr();
389 #ifdef USE_SOUND_VOLUME
390 void VM::set_sound_device_volume(int ch, int decibel_l, int decibel_r)
393 opn->set_volume(0, decibel_l, decibel_r);
395 opn->set_volume(1, decibel_l, decibel_r);
397 pcm->set_volume(0, decibel_l, decibel_r);
399 drec->set_volume(0, decibel_l, decibel_r);
401 drec->set_volume(1, decibel_l, decibel_r);
403 fdc->get_context_noise_seek()->set_volume(0, decibel_l, decibel_r);
404 fdc->get_context_noise_head_down()->set_volume(0, decibel_l, decibel_r);
405 fdc->get_context_noise_head_up()->set_volume(0, decibel_l, decibel_r);
407 drec->get_context_noise_play()->set_volume(0, decibel_l, decibel_r);
408 drec->get_context_noise_stop()->set_volume(0, decibel_l, decibel_r);
409 drec->get_context_noise_fast()->set_volume(0, decibel_l, decibel_r);
414 // ----------------------------------------------------------------------------
416 // ----------------------------------------------------------------------------
418 void VM::notify_socket_connected(int ch)
420 w3100a->notify_connected(ch);
423 void VM::notify_socket_disconnected(int ch)
425 w3100a->notify_disconnected(ch);
428 uint8_t* VM::get_socket_send_buffer(int ch, int* size)
430 return w3100a->get_send_buffer(ch, size);
433 void VM::inc_socket_send_buffer_ptr(int ch, int size)
435 w3100a->inc_send_buffer_ptr(ch, size);
438 uint8_t* VM::get_socket_recv_buffer0(int ch, int* size0, int* size1)
440 return w3100a->get_recv_buffer0(ch, size0, size1);
443 uint8_t* VM::get_socket_recv_buffer1(int ch)
445 return w3100a->get_recv_buffer1(ch);
448 void VM::inc_socket_recv_buffer_ptr(int ch, int size)
450 w3100a->inc_recv_buffer_ptr(ch, size);
453 // ----------------------------------------------------------------------------
455 // ----------------------------------------------------------------------------
457 void VM::open_floppy_disk(int drv, const _TCHAR* file_path, int bank)
459 fdc->open_disk(drv, file_path, bank);
461 if(fdc->get_media_type(drv) == MEDIA_TYPE_2DD) {
462 if(fdc->get_drive_type(drv) == DRIVE_TYPE_2D) {
463 fdc->set_drive_type(drv, DRIVE_TYPE_2DD);
465 } else if(fdc->get_media_type(drv) == MEDIA_TYPE_2D) {
466 if(fdc->get_drive_type(drv) == DRIVE_TYPE_2DD) {
467 fdc->set_drive_type(drv, DRIVE_TYPE_2D);
472 void VM::close_floppy_disk(int drv)
474 fdc->close_disk(drv);
477 bool VM::is_floppy_disk_inserted(int drv)
479 return fdc->is_disk_inserted(drv);
482 void VM::is_floppy_disk_protected(int drv, bool value)
484 fdc->is_disk_protected(drv, value);
487 bool VM::is_floppy_disk_protected(int drv)
489 return fdc->is_disk_protected(drv);
492 uint32_t VM::is_floppy_disk_accessed()
494 return fdc->read_signal(0);
497 void VM::open_hard_disk(int drv, const _TCHAR* file_path)
499 if(drv < USE_HARD_DISK) {
500 sasi_hdd->open(drv, file_path, 256);
504 void VM::close_hard_disk(int drv)
506 if(drv < USE_HARD_DISK) {
507 sasi_hdd->close(drv);
511 bool VM::is_hard_disk_inserted(int drv)
513 if(drv < USE_HARD_DISK) {
514 return sasi_hdd->mounted(drv);
519 uint32_t VM::is_hard_disk_accessed()
523 for(int drv = 0; drv < USE_HARD_DISK; drv++) {
524 if(sasi_hdd->accessed(drv)) {
531 void VM::play_tape(int drv, const _TCHAR* file_path)
533 bool remote = drec->get_remote();
534 bool opened = drec->play_tape(file_path);
536 if(opened && remote) {
537 // if machine already sets remote on, start playing now
541 cmt->play_tape(opened);
544 void VM::rec_tape(int drv, const _TCHAR* file_path)
546 bool remote = drec->get_remote();
547 bool opened = drec->rec_tape(file_path);
549 if(opened && remote) {
550 // if machine already sets remote on, start recording now
554 cmt->rec_tape(opened);
557 void VM::close_tape(int drv)
562 drec->set_remote(false);
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)
593 drec->set_remote(false);
595 drec->set_remote(true);
598 void VM::push_stop(int drv)
600 drec->set_remote(false);
603 void VM::push_fast_forward(int drv)
605 drec->set_remote(false);
607 drec->set_remote(true);
610 void VM::push_fast_rewind(int drv)
612 drec->set_remote(false);
613 drec->set_ff_rew(-1);
614 drec->set_remote(true);
617 bool VM::is_frame_skippable()
619 return event->is_frame_skippable();
622 void VM::update_config()
624 for(DEVICE* device = first_device; device; device = device->next_device) {
625 device->update_config();
629 double VM::get_current_usec()
631 if(event == NULL) return 0.0;
632 return event->get_current_usec();
635 uint64_t VM::get_current_clock_uint64()
637 if(event == NULL) return (uint64_t)0;
638 return event->get_current_clock_uint64();
641 #define STATE_VERSION 8
643 bool VM::process_state(FILEIO* state_fio, bool loading)
645 if(!state_fio->StateCheckUint32(STATE_VERSION)) {
648 for(DEVICE* device = first_device; device; device = device->next_device) {
649 // Note: typeid(foo).name is fixed by recent ABI.Not dec 6.
650 // const char *name = typeid(*device).name();
651 // But, using get_device_name() instead of typeid(foo).name() 20181008 K.O
652 const char *name = device->get_device_name();
653 int len = (int)strlen(name);
655 if(!state_fio->StateCheckInt32(len)) {
657 printf("Class name len Error: DEVID=%d EXPECT=%s\n", device->this_device_id, name);
661 if(!state_fio->StateCheckBuffer(name, len, 1)) {
663 printf("Class name Error: DEVID=%d EXPECT=%s\n", device->this_device_id, name);
667 if(!device->process_state(state_fio, loading)) {
669 printf("Data loading Error: DEVID=%d\n", device->this_device_id);
674 // Machine specified.
675 state_fio->StateValue(monitor_type);