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);
94 fdc = new MB8877(this, emu);
95 fdc->set_context_noise_seek(new NOISE(this, emu));
96 fdc->set_context_noise_head_down(new NOISE(this, emu));
97 fdc->set_context_noise_head_up(new NOISE(this, emu));
99 // fdc->set_context_debugger(new DEBUGGER(this, emu));
101 pcm = new PCM1BIT(this, emu);
103 // pcm->set_context_debugger(new DEBUGGER(this, emu));
105 rtc = new RP5C01(this, emu); // RP-5C15
106 sasi_host = new SCSI_HOST(this, emu);
107 sasi_hdd = new SASI_HDD(this, emu);
108 sasi_hdd->set_device_name(_T("SASI Hard Disk Drive"));
109 sasi_hdd->scsi_id = 0;
110 // sasi_hdd->bytes_per_sec = 32 * 1024; // 32KB/s
111 sasi_hdd->bytes_per_sec = 3600 / 60 * 256 * 33; // 3600rpm, 256bytes x 33sectors in track (thanks Mr.Sato)
112 for(int i = 0; i < USE_HARD_DISK; i++) {
113 sasi_hdd->set_disk_handler(i, new HARDDISK(emu));
115 sasi_hdd->set_context_interface(sasi_host);
116 sasi_host->set_context_target(sasi_hdd);
117 w3100a = new W3100A(this, emu);
118 opn = new YM2203(this, emu);
120 opn->set_context_debugger(new DEBUGGER(this, emu));
122 cpu = new Z80(this, emu);
123 pio = new Z80PIO(this, emu);
124 sio = new Z80SIO(this, emu);
126 pio_i->set_device_name(_T("i8255 PIO(CMT/CRTC)"));
127 pio->set_device_name(_T("Z80 PIO(KEYBOARD/CRTC)"));
128 sio->set_device_name(_T("Z80 SIO(MOUSE)"));
131 calendar = new CALENDAR(this, emu);
132 cmt = new CMT(this, emu);
133 crtc = new CRTC(this, emu);
134 floppy = new FLOPPY(this, emu);
135 interrupt = new INTERRUPT(this, emu);
136 joystick = new JOYSTICK(this, emu);
137 keyboard = new KEYBOARD(this, emu);
138 memory = new MEMORY(this, emu);
139 mouse = new MOUSE(this, emu);
140 mz1e26 = new MZ1E26(this, emu);
141 mz1e30 = new MZ1E30(this, emu);
142 mz1r13 = new MZ1R13(this, emu);
143 mz1r37 = new MZ1R37(this, emu);
144 printer = new PRINTER(this, emu);
145 serial = new SERIAL(this, emu);
146 timer = new TIMER(this, emu);
148 event->set_context_cpu(cpu, config.boot_mode ? CPU_CLOCKS_LOW : CPU_CLOCKS);
149 event->set_context_sound(opn);
150 event->set_context_sound(pcm);
151 event->set_context_sound(drec);
152 event->set_context_sound(fdc->get_context_noise_seek());
153 event->set_context_sound(fdc->get_context_noise_head_down());
154 event->set_context_sound(fdc->get_context_noise_head_up());
155 event->set_context_sound(drec->get_context_noise_play());
156 event->set_context_sound(drec->get_context_noise_stop());
157 event->set_context_sound(drec->get_context_noise_fast());
159 drec->set_context_ear(cmt, SIG_CMT_OUT, 1);
160 drec->set_context_remote(cmt, SIG_CMT_REMOTE, 1);
161 drec->set_context_end(cmt, SIG_CMT_END, 1);
162 drec->set_context_top(cmt, SIG_CMT_TOP, 1);
164 pit->set_context_ch0(interrupt, SIG_INTERRUPT_I8253, 1);
165 pit->set_context_ch0(pit, SIG_I8253_CLOCK_1, 1);
166 pit->set_context_ch1(pit, SIG_I8253_CLOCK_2, 1);
167 pit->set_constant_clock(0, 31250);
168 pio_i->set_context_port_a(cmt, SIG_CMT_PIO_PA, 0xff, 0);
169 pio_i->set_context_port_c(cmt, SIG_CMT_PIO_PC, 0xff, 0);
170 pio_i->set_context_port_a(crtc, SIG_CRTC_REVERSE, 0x10, 0);
171 pio_i->set_context_port_c(crtc, SIG_CRTC_MASK, 0x01, 0);
172 pio_i->set_context_port_c(pcm, SIG_PCM1BIT_SIGNAL, 0x04, 0);
173 // Sound:: Force realtime rendering. This is temporally fix. 20161024 K.O
174 //pcm->set_realtime_render(true);
177 rtc->set_context_alarm(interrupt, SIG_INTERRUPT_RP5C15, 1);
178 rtc->set_context_pulse(opn, SIG_YM2203_PORT_B, 8);
179 sasi_host->set_context_irq(mz1e30, SIG_MZ1E30_IRQ, 1);
180 sasi_host->set_context_drq(mz1e30, SIG_MZ1E30_DRQ, 1);
181 opn->set_context_port_a(floppy, SIG_FLOPPY_REVERSE, 0x02, 0);
182 opn->set_context_port_a(crtc, SIG_CRTC_PALLETE, 0x04, 0);
183 opn->set_context_port_a(mouse, SIG_MOUSE_SEL, 0x08, 0);
184 pio->set_context_port_a(crtc, SIG_CRTC_COLUMN_SIZE, 0x20, 0);
185 pio->set_context_port_a(keyboard, SIG_KEYBOARD_COLUMN, 0x1f, 0);
186 pio->set_context_port_a(memory, SIG_MEMORY_VRAM_SEL, 0xc0, 0);
187 sio->set_context_dtr(1, mouse, SIG_MOUSE_DTR, 1);
189 calendar->set_context_rtc(rtc);
190 cmt->set_context_pio(pio_i);
191 cmt->set_context_drec(drec);
192 crtc->set_context_mem(memory);
193 crtc->set_context_int(interrupt);
194 crtc->set_context_pio(pio_i);
195 crtc->set_vram_ptr(memory->get_vram());
196 crtc->set_tvram_ptr(memory->get_tvram());
197 crtc->set_kanji_ptr(memory->get_kanji());
198 crtc->set_pcg_ptr(memory->get_pcg());
199 floppy->set_context_fdc(fdc);
200 keyboard->set_context_pio_i(pio_i);
201 keyboard->set_context_pio(pio);
202 memory->set_context_cpu(cpu);
203 memory->set_context_crtc(crtc);
204 mouse->set_context_sio(sio);
205 mz1e30->set_context_host(sasi_host);
206 if(config.printer_type == 0) {
207 printer->set_context_prn(new PRNFILE(this, emu));
208 } else if(config.printer_type == 1) {
209 MZ1P17 *mz1p17 = new MZ1P17(this, emu);
210 mz1p17->mode = MZ1P17_MODE_MZ1;
211 printer->set_context_prn(mz1p17);
212 // } else if(config.printer_type == 2) {
213 // printer->set_context_prn(new PCPR201(this, emu));
215 printer->set_context_prn(dummy);
217 serial->set_context_sio(sio);
218 timer->set_context_pit(pit);
221 cpu->set_context_mem(memory);
222 cpu->set_context_io(io);
223 cpu->set_context_intr(pio);
225 cpu->set_context_debugger(new DEBUGGER(this, emu));
228 // z80 family daisy chain
229 pio->set_context_intr(cpu, 0);
230 pio->set_context_child(sio);
231 sio->set_context_intr(cpu, 1);
232 sio->set_context_child(interrupt);
233 interrupt->set_context_intr(cpu, 2);
236 io->set_iomap_range_rw(0x60, 0x63, w3100a);
237 io->set_iomap_range_rw(0xa0, 0xa3, serial);
238 io->set_iomap_range_rw(0xa4, 0xa5, mz1e30);
239 io->set_iomap_range_rw(0xa8, 0xa9, mz1e30);
240 io->set_iomap_range_rw(0xac, 0xad, mz1r37);
241 io->set_iomap_single_w(0xae, crtc);
242 io->set_iomap_range_rw(0xb0, 0xb3, serial);
243 io->set_iomap_range_rw(0xb4, 0xb5, memory);
244 io->set_iomap_single_w(0xb7, memory);
245 io->set_iomap_range_rw(0xb8, 0xb9, mz1r13);
246 io->set_iomap_range_rw(0xbc, 0xbf, crtc);
247 io->set_iomap_range_w(0xc6, 0xc7, interrupt);
248 io->set_iomap_range_rw(0xc8, 0xc9, opn);
249 io->set_iomap_single_rw(0xca, mz1e26);
250 io->set_iomap_single_rw(0xcc, calendar);
251 io->set_iomap_single_w(0xcd, serial);
252 io->set_iomap_range_w(0xce, 0xcf, memory);
253 io->set_iomap_range_rw(0xd8, 0xdb, fdc);
254 io->set_iomap_range_w(0xdc, 0xdd, floppy);
255 io->set_iomap_range_rw(0xe0, 0xe3, pio_i);
256 io->set_iomap_range_rw(0xe4, 0xe7, pit);
257 io->set_iomap_range_rw(0xe8, 0xeb, pio);
258 io->set_iomap_single_rw(0xef, joystick);
259 io->set_iomap_range_w(0xf0, 0xf3, timer);
260 io->set_iomap_range_rw(0xf4, 0xf7, crtc);
261 io->set_iomap_range_rw(0xfe, 0xff, printer);
263 if(config.boot_mode == 0) {
264 io->set_iowait_range_rw(0xd8, 0xdf, 1); // nfdcs
265 io->set_iowait_range_rw(0xe8, 0xeb, 1); // npioc
267 io->set_iowait_range_rw(0xc8, 0xc9, 1); // nopnc
268 io->set_iowait_single_rw(0xcc, 3); // nrtcs
270 // initialize all devices
271 #if defined(__GIT_REPO_VERSION)
272 set_git_repo_version(__GIT_REPO_VERSION);
274 initialize_devices();
276 for(int drv = 0; drv < MAX_DRIVE; drv++) {
277 // if(config.drive_type) {
278 // fdc->set_drive_type(drv, DRIVE_TYPE_2D);
280 fdc->set_drive_type(drv, DRIVE_TYPE_2DD);
283 for(int drv = 0; drv < USE_HARD_DISK; drv++) {
284 if(!(config.last_hard_disk_path[drv][0] != _T('\0') && FILEIO::IsFileExisting(config.last_hard_disk_path[drv]))) {
285 create_local_path(config.last_hard_disk_path[drv], _MAX_PATH, _T("SASI%d.DAT"), drv);
288 boot_mode = config.boot_mode;
289 monitor_type = config.monitor_type;
294 // delete all devices
295 for(DEVICE* device = first_device; device;) {
296 DEVICE *next_device = device->next_device;
299 device = next_device;
303 DEVICE* VM::get_device(int id)
305 for(DEVICE* device = first_device; device; device = device->next_device) {
306 if(device->this_device_id == id) {
313 // ----------------------------------------------------------------------------
314 // drive virtual machine
315 // ----------------------------------------------------------------------------
320 for(DEVICE* device = first_device; device; device = device->next_device) {
324 // set initial port status
325 uint8_t port_b = 0x37;
328 } else if(boot_mode == 2) {
331 if(monitor_type & 2) {
334 opn->write_signal(SIG_YM2203_PORT_B, port_b, 0xff);
337 void VM::special_reset(int num)
340 // for(DEVICE* device = first_device; device; device = device->next_device) {
341 // device->special_reset();
343 memory->special_reset(num);
344 cpu->special_reset(num);
352 double VM::get_frame_rate()
354 return event->get_frame_rate();
357 // ----------------------------------------------------------------------------
359 // ----------------------------------------------------------------------------
362 DEVICE *VM::get_cpu(int index)
371 // ----------------------------------------------------------------------------
373 // ----------------------------------------------------------------------------
375 void VM::draw_screen()
380 // ----------------------------------------------------------------------------
382 // ----------------------------------------------------------------------------
384 void VM::initialize_sound(int rate, int samples)
386 // init sound manager
387 event->initialize_sound(rate, samples);
390 opn->initialize_sound(rate, 2000000, samples, 0, -8);
391 pcm->initialize_sound(rate, 4096);
394 uint16_t* VM::create_sound(int* extra_frames)
396 return event->create_sound(extra_frames);
399 int VM::get_sound_buffer_ptr()
401 return event->get_sound_buffer_ptr();
404 #ifdef USE_SOUND_VOLUME
405 void VM::set_sound_device_volume(int ch, int decibel_l, int decibel_r)
408 opn->set_volume(0, decibel_l, decibel_r);
410 opn->set_volume(1, decibel_l, decibel_r);
412 pcm->set_volume(0, decibel_l, decibel_r);
414 drec->set_volume(0, decibel_l, decibel_r);
416 drec->set_volume(1, decibel_l, decibel_r);
418 fdc->get_context_noise_seek()->set_volume(0, decibel_l, decibel_r);
419 fdc->get_context_noise_head_down()->set_volume(0, decibel_l, decibel_r);
420 fdc->get_context_noise_head_up()->set_volume(0, decibel_l, decibel_r);
422 drec->get_context_noise_play()->set_volume(0, decibel_l, decibel_r);
423 drec->get_context_noise_stop()->set_volume(0, decibel_l, decibel_r);
424 drec->get_context_noise_fast()->set_volume(0, decibel_l, decibel_r);
429 // ----------------------------------------------------------------------------
431 // ----------------------------------------------------------------------------
433 void VM::notify_socket_connected(int ch)
435 w3100a->notify_connected(ch);
438 void VM::notify_socket_disconnected(int ch)
440 w3100a->notify_disconnected(ch);
443 uint8_t* VM::get_socket_send_buffer(int ch, int* size)
445 return w3100a->get_send_buffer(ch, size);
448 void VM::inc_socket_send_buffer_ptr(int ch, int size)
450 w3100a->inc_send_buffer_ptr(ch, size);
453 uint8_t* VM::get_socket_recv_buffer0(int ch, int* size0, int* size1)
455 return w3100a->get_recv_buffer0(ch, size0, size1);
458 uint8_t* VM::get_socket_recv_buffer1(int ch)
460 return w3100a->get_recv_buffer1(ch);
463 void VM::inc_socket_recv_buffer_ptr(int ch, int size)
465 w3100a->inc_recv_buffer_ptr(ch, size);
468 // ----------------------------------------------------------------------------
470 // ----------------------------------------------------------------------------
472 void VM::open_floppy_disk(int drv, const _TCHAR* file_path, int bank)
474 fdc->open_disk(drv, file_path, bank);
476 if(fdc->get_media_type(drv) == MEDIA_TYPE_2DD) {
477 if(fdc->get_drive_type(drv) == DRIVE_TYPE_2D) {
478 fdc->set_drive_type(drv, DRIVE_TYPE_2DD);
480 } else if(fdc->get_media_type(drv) == MEDIA_TYPE_2D) {
481 if(fdc->get_drive_type(drv) == DRIVE_TYPE_2DD) {
482 fdc->set_drive_type(drv, DRIVE_TYPE_2D);
487 void VM::close_floppy_disk(int drv)
489 fdc->close_disk(drv);
492 bool VM::is_floppy_disk_inserted(int drv)
494 return fdc->is_disk_inserted(drv);
497 void VM::is_floppy_disk_protected(int drv, bool value)
499 fdc->is_disk_protected(drv, value);
502 bool VM::is_floppy_disk_protected(int drv)
504 return fdc->is_disk_protected(drv);
507 uint32_t VM::is_floppy_disk_accessed()
509 return fdc->read_signal(0);
512 void VM::open_hard_disk(int drv, const _TCHAR* file_path)
514 if(drv < USE_HARD_DISK) {
515 sasi_hdd->open(drv, file_path, 256);
519 void VM::close_hard_disk(int drv)
521 if(drv < USE_HARD_DISK) {
522 sasi_hdd->close(drv);
526 bool VM::is_hard_disk_inserted(int drv)
528 if(drv < USE_HARD_DISK) {
529 return sasi_hdd->mounted(drv);
534 uint32_t VM::is_hard_disk_accessed()
538 for(int drv = 0; drv < USE_HARD_DISK; drv++) {
539 if(sasi_hdd->accessed(drv)) {
546 void VM::play_tape(int drv, const _TCHAR* file_path)
548 bool remote = drec->get_remote();
549 bool opened = drec->play_tape(file_path);
551 if(opened && remote) {
552 // if machine already sets remote on, start playing now
556 cmt->play_tape(opened);
559 void VM::rec_tape(int drv, const _TCHAR* file_path)
561 bool remote = drec->get_remote();
562 bool opened = drec->rec_tape(file_path);
564 if(opened && remote) {
565 // if machine already sets remote on, start recording now
569 cmt->rec_tape(opened);
572 void VM::close_tape(int drv)
577 drec->set_remote(false);
581 bool VM::is_tape_inserted(int drv)
583 return drec->is_tape_inserted();
586 bool VM::is_tape_playing(int drv)
588 return drec->is_tape_playing();
591 bool VM::is_tape_recording(int drv)
593 return drec->is_tape_recording();
596 int VM::get_tape_position(int drv)
598 return drec->get_tape_position();
601 const _TCHAR* VM::get_tape_message(int drv)
603 return drec->get_message();
606 void VM::push_play(int drv)
608 drec->set_remote(false);
610 drec->set_remote(true);
613 void VM::push_stop(int drv)
615 drec->set_remote(false);
618 void VM::push_fast_forward(int drv)
620 drec->set_remote(false);
622 drec->set_remote(true);
625 void VM::push_fast_rewind(int drv)
627 drec->set_remote(false);
628 drec->set_ff_rew(-1);
629 drec->set_remote(true);
632 bool VM::is_frame_skippable()
634 return event->is_frame_skippable();
637 void VM::update_config()
639 for(DEVICE* device = first_device; device; device = device->next_device) {
640 device->update_config();
644 double VM::get_current_usec()
646 if(event == NULL) return 0.0;
647 return event->get_current_usec();
650 uint64_t VM::get_current_clock_uint64()
652 if(event == NULL) return (uint64_t)0;
653 return event->get_current_clock_uint64();
656 #define STATE_VERSION 9
658 bool VM::process_state(FILEIO* state_fio, bool loading)
660 if(!(VM_TEMPLATE::process_state_core(state_fio, loading, STATE_VERSION))) {
663 // Machine specified.
664 state_fio->StateValue(boot_mode);
665 state_fio->StateValue(monitor_type);