2 SHARP MZ-700 Emulator 'EmuZ-700'
3 SHARP MZ-800 Emulator 'EmuZ-800'
4 SHARP MZ-1500 Emulator 'EmuZ-1500'
6 Author : Takeda.Toshiya
13 #include "../../emu.h"
14 #include "../device.h"
18 #include "../datarec.h"
22 #include "../pcm1bit.h"
26 #include "../debugger.h"
36 #if defined(_MZ800) || defined(_MZ1500)
38 #include "../mb8877.h"
40 #include "../sn76489an.h"
41 #include "../z80pio.h"
42 #include "../z80sio.h"
45 #include "../mz1p17.h"
46 #include "../prnfile.h"
49 #include "quickdisk.h"
52 // ----------------------------------------------------------------------------
54 // ----------------------------------------------------------------------------
56 VM::VM(EMU* parent_emu) : emu(parent_emu)
59 boot_mode = config.boot_mode;
63 first_device = last_device = NULL;
64 dummy = new DEVICE(this, emu); // must be 1st device
65 event = new EVENT(this, emu); // must be 2nd device
67 dummy->set_device_name(_T("1st Dummy"));
68 event->set_device_name(_T("EVENT"));
70 and_int = new AND(this, emu);
71 drec = new DATAREC(this, emu);
72 pit = new I8253(this, emu);
73 pio = new I8255(this, emu);
74 io = new IO(this, emu);
75 pcm = new PCM1BIT(this, emu);
76 cpu = new Z80(this, emu);
78 and_int->set_device_name(_T("AND GATE(INTERRUPT)"));
79 cpu->set_device_name(_T("CPU(Z80)"));
82 // cmos = new CMOS(this, emu);
83 emm = new EMM(this, emu);
84 kanji = new KANJI(this, emu);
85 keyboard = new KEYBOARD(this, emu);
86 memory = new MEMORY(this, emu);
87 ramfile = new RAMFILE(this, emu);
89 emm->set_device_name(_T("EMM"));
90 kanji->set_device_name(_T("KANJI ROM"));
91 keyboard->set_device_name(_T("KEYBOARD I/F"));
92 memory->set_device_name(_T("MEMORY"));
93 ramfile->set_device_name(_T("RAM FILES"));
95 #if defined(_MZ800) || defined(_MZ1500)
96 and_snd = new AND(this, emu);
98 and_snd->set_device_name(_T("AND GATE(SOUND)"));
100 fdc = new MB8877(this, emu); // mb8876
102 not_pit = new NOT(this, emu);
104 not_pit->set_device_name(_T("NOT GATE(PIT)"));
106 psg = new SN76489AN(this, emu);
107 #elif defined(_MZ1500)
108 if(config.printer_device_type == 0) {
109 printer = new PRNFILE(this, emu);
110 } else if(config.printer_device_type == 1) {
111 printer = new MZ1P17(this, emu);
115 not_reset = new NOT(this, emu);
116 not_strobe = new NOT(this, emu);
117 psg_l = new SN76489AN(this, emu);
118 psg_r = new SN76489AN(this, emu);
120 not_reset->set_device_name(_T("NOT GATE(RESET)"));
121 not_strobe->set_device_name(_T("NOT GATE(PRINTER STROBE)"));
122 psg_l->set_device_name(_T("SN76489AN PSG(LEFT)"));
123 psg_r->set_device_name(_T("SN76489AN PSG(RIGHT)"));
126 pio_int = new Z80PIO(this, emu);
127 sio_rs = new Z80SIO(this, emu);
128 sio_qd = new Z80SIO(this, emu);
130 floppy = new FLOPPY(this, emu);
132 floppy->set_device_name(_T("FLOPPY I/F"));
135 psg = new PSG(this, emu);
137 psg->set_device_name(_T("PSG I/F"));
140 qd = new QUICKDISK(this, emu);
142 qd->set_device_name(_T("QUICKDISK I/F"));
147 event->set_context_cpu(cpu);
148 event->set_context_sound(pcm);
150 event->set_context_sound(psg);
151 #elif defined(_MZ1500)
152 event->set_context_sound(psg_l);
153 event->set_context_sound(psg_r);
155 event->set_context_sound(drec);
157 #if defined(USE_SOUND_FILES)
158 #if defined(_MZ800) || defined(_MZ1500)
159 if(fdc->load_sound_data(MB8877_SND_TYPE_SEEK, _T("FDDSEEK.WAV"))) {
160 event->set_context_sound(fdc);
162 //if(qd->load_sound_data(MB8877_SND_TYPE_SEEK, _T("QD_SEEK.WAV"))) {
163 // event->set_context_sound(qd);
166 drec->load_sound_data(DATAREC_SNDFILE_EJECT, _T("CMTEJECT.WAV"));
167 //drec->load_sound_data(DATAREC_SNDFILE_PLAY, _T("CMTPLAY.WAV"));
168 //drec->load_sound_data(DATAREC_SNDFILE_STOP, _T("CMTSTOP.WAV"));
171 drec->load_sound_data(DATAREC_SNDFILE_RELAY_ON, _T("RELAY_ON.WAV"));
172 drec->load_sound_data(DATAREC_SNDFILE_RELAY_OFF, _T("RELAYOFF.WAV"));
174 drec->load_sound_data(DATAREC_SNDFILE_RELAY_ON, _T("CMTPLAY.WAV"));
175 drec->load_sound_data(DATAREC_SNDFILE_RELAY_OFF, _T("CMTSTOP.WAV"));
180 memory->set_context_cpu(cpu);
183 memory->set_context_pio(pio);
184 memory->set_context_pit(pit);
188 psg->set_context_psg_l(psg_l);
189 psg->set_context_psg_r(psg_r);
193 // 8253:CLK#0 <- 1.10MHz
194 pit->set_constant_clock(0, 1100000);
196 // 8253:CLK#0 <- 895KHz
197 pit->set_constant_clock(0, CPU_CLOCKS / 4);
200 #if defined(_MZ800) || defined(_MZ1500)
201 // 8253:OUT#0 AND 8255:PC0 -> SPEAKER
202 pit->set_context_ch0(and_snd, SIG_AND_BIT_0, 1);
203 pio->set_context_port_c(and_snd, SIG_AND_BIT_1, 1, 0);
204 and_snd->set_context_out(pcm, SIG_PCM1BIT_SIGNAL, 1);
205 and_snd->set_mask(SIG_AND_BIT_0 | SIG_AND_BIT_1);
207 // 8253:OUT#0 -> SPEAKER
208 pit->set_context_ch0(pcm, SIG_PCM1BIT_SIGNAL, 1);
211 // 8253:OUT#0 -> NOT -> Z80PIO:PA4
212 pit->set_context_ch0(not_pit, SIG_NOT_INPUT, 1);
213 not_pit->set_context_out(pio_int, SIG_Z80PIO_PORT_A, 0x10);
214 #elif defined(_MZ1500)
215 // 8253:OUT#0 -> Z80PIO:PA4
216 pit->set_context_ch0(pio_int, SIG_Z80PIO_PORT_A, 0x10);
219 // 8253:CLK#1 <- 15.7KHz
220 pit->set_constant_clock(1, CPU_CLOCKS / 228);
222 // 8253:OUT#1 -> 8253:CLK#2
223 pit->set_context_ch1(pit, SIG_I8253_CLOCK_2, 1);
225 // 8253:OUT#2 (N)AND 8255:PC2 -> Z80:INT
226 pit->set_context_ch2(and_int, SIG_AND_BIT_0, 1);
227 pio->set_context_port_c(and_int, SIG_AND_BIT_1, 4, 0);
228 and_int->set_context_out(cpu, SIG_CPU_IRQ, 1);
229 and_int->set_mask(SIG_AND_BIT_0 | SIG_AND_BIT_1);
232 // 8253:OUT#2 -> Z80PIO:PA5
233 pit->set_context_ch2(pio_int, SIG_Z80PIO_PORT_A, 0x20);
236 // 8255:PA0-3 -> KEYBOARD:STROBE
237 pio->set_context_port_a(keyboard, SIG_KEYBOARD_COLUMN, 0x0f, 0);
239 // 8255:PA4 -> JOYSTICK #1
240 // 8255:PA5 -> JOYSTICK #2
242 // 8255:PA7 -> 556 RESET
244 // 8255:PB0-7 <- KEYBOARD:DATA
245 keyboard->set_context_pio(pio);
247 // 8255:PC0 -> AND -> SPEAKER
248 // 8255:PC1 -> DATA RECORDER:WRITE DATA
249 pio->set_context_port_c(drec, SIG_DATAREC_MIC, 0x02, 0);
250 // 8255:PC2 -> (N)AND -> Z80:INT
251 // 8255:PC3 -> DATA RECORDER:MOTOR ON/OFF
252 pio->set_context_port_c(drec, SIG_DATAREC_TRIG, 0x08, 0);
253 // 8255:PC4 <- DATA RECORDER:MOTOR REMOTE
254 drec->set_context_remote(pio, SIG_I8255_PORT_C, 0x10);
255 // 8255:PC5 <- DATA RECORDER:READ DATA
256 drec->set_context_ear(pio, SIG_I8255_PORT_C, 0x20);
257 // 8255:PC6 <- MEMORY:556 OUT (1.5KHz)
258 // 8255:PC7 <- MEMORY:VBLANK
260 #if defined(_MZ800) || defined(_MZ1500)
264 // Z80PIO:PA4 <- NOT <- 8253:OUT#0
265 // Z80PIO:PA5 <- HBLANK
266 memory->set_context_pio_int(pio_int);
267 #elif defined(_MZ1500)
268 // Z80PIO:PA0 <- PRINTER:RDA (BUSY)
269 // Z80PIO:PA1 <- PRINTER:STA (PE)
270 if(config.printer_device_type == 0) {
271 PRNFILE *prnfile = (PRNFILE *)printer;
272 prnfile->set_context_busy(pio_int, SIG_Z80PIO_PORT_A, 0x01);
273 } else if(config.printer_device_type == 1) {
274 MZ1P17 *mz1p17 = (MZ1P17 *)printer;
275 mz1p17->mode = MZ1P17_MODE_MZ2;
276 mz1p17->set_context_busy(pio_int, SIG_Z80PIO_PORT_A, 0x01);
278 // Z80PIO:PA4 <- 8253:OUT#0
279 // Z80PIO:PA5 <- 8253:OUT#2
280 // Z80PIO:PA6 -> NOT -> PRINTER:IRT (RESET)
281 // Z80PIO:PA7 -> NOT -> PRINTER:RDP (STROBE)
282 // Z80PIO:PB -> PRINTER:DATA
283 pio_int->set_context_port_a(not_reset, SIG_NOT_INPUT, 0x40, 0);
284 not_reset->set_context_out(printer, SIG_PRINTER_RESET, 0x01);
285 pio_int->set_context_port_a(not_strobe, SIG_NOT_INPUT, 0x80, 0);
286 not_strobe->set_context_out(printer, SIG_PRINTER_STROBE, 0x01);
287 pio_int->set_context_port_b(printer, SIG_PRINTER_DATA, 0xff, 0);
291 #if defined(_MZ800) || defined(_MZ1500)
292 // Z80SIO:RTSA -> QD:WRGA
293 sio_qd->set_context_rts(0, qd, QUICKDISK_SIO_RTSA, 1);
294 // Z80SIO:DTRB -> QD:MTON
295 sio_qd->set_context_dtr(1, qd, QUICKDISK_SIO_DTRB, 1);
296 // Z80SIO:SENDA -> QD:RECV
297 sio_qd->set_context_sync(0, qd, QUICKDISK_SIO_SYNC, 1);
298 sio_qd->set_context_rxdone(0, qd, QUICKDISK_SIO_RXDONE, 1);
299 sio_qd->set_context_send(0, qd, QUICKDISK_SIO_DATA);
300 sio_qd->set_context_break(0, qd, QUICKDISK_SIO_BREAK, 1);
301 // Z80SIO:CTSA <- QD:PROTECT
302 // Z80SIO:DCDA <- QD:INSERT
303 // Z80SIO:DCDB <- QD:HOE
304 qd->set_context_sio(sio_qd);
306 sio_rs->set_tx_clock(0, 1200 * 16); // 1200 baud
307 sio_rs->set_rx_clock(0, 1200 * 16); // baud-rate can be changed by jumper pin
308 sio_rs->set_tx_clock(1, 1200 * 16);
309 sio_rs->set_rx_clock(1, 1200 * 16);
311 sio_qd->set_tx_clock(0, 101562.5);
312 sio_qd->set_rx_clock(0, 101562.5);
313 sio_qd->set_tx_clock(1, 101562.5);
314 sio_qd->set_rx_clock(1, 101562.5);
317 floppy->set_context_cpu(cpu);
318 floppy->set_context_fdc(fdc);
319 fdc->set_context_drq(floppy, SIG_FLOPPY_DRQ, 1);
323 cpu->set_context_mem(memory);
324 cpu->set_context_io(io);
325 #if defined(_MZ800) || defined(_MZ1500)
326 cpu->set_context_intr(pio_int);
327 // z80 family daisy chain
329 pio_int->set_context_intr(cpu, 1);
330 pio_int->set_context_child(sio_rs);
331 sio_rs->set_context_intr(cpu, 2);
332 sio_rs->set_context_child(sio_qd);
333 sio_qd->set_context_intr(cpu, 3);
335 cpu->set_context_intr(dummy);
338 cpu->set_context_debugger(new DEBUGGER(this, emu));
342 io->set_iomap_range_rw(0x00, 0x03, emm);
344 io->set_iomap_range_rw(0xb8, 0xb9, kanji);
346 io->set_iomap_range_rw(0xea, 0xeb, ramfile);
348 // io->set_iomap_range_rw(0xf8, 0xfa, cmos);
352 io->set_iomap_range_rw(0xd0, 0xd3, pio);
353 io->set_iomap_range_rw(0xd4, 0xd7, pit);
356 #if defined(_MZ800) || defined(_MZ1500)
358 io->set_iomap_range_rw(0xd8, 0xdb, fdc);
359 io->set_iomap_range_w(0xdc, 0xdf, floppy);
364 io->set_iomap_range_r(0xe0, 0xe1, memory);
365 io->set_iomap_range_w(0xe0, 0xe6, memory);
366 #elif defined(_MZ1500)
367 io->set_iomap_range_w(0xe0, 0xe6, memory);
369 io->set_iomap_range_w(0xe0, 0xe4, memory);
374 io->set_iomap_range_w(0xcc, 0xcf, memory);
375 io->set_iomap_single_r(0xce, memory);
377 io->set_iomap_single_w(0xf0, memory);
378 #elif defined(_MZ1500)
380 io->set_iomap_range_w(0xf0, 0xf1, memory);
385 // io->set_iomap_range_r(0xf0, 0xf1, joystick);
390 io->set_iomap_single_w(0xf2, psg);
391 #elif defined(_MZ1500)
392 io->set_iomap_single_w(0xe9, psg);
393 io->set_iomap_single_w(0xf2, psg_l);
394 io->set_iomap_single_w(0xf3, psg_r);
397 #if defined(_MZ800) || defined(_MZ1500)
399 // z80pio and z80sio*2
400 static const int z80_sio_addr[4] = {0, 2, 1, 3};
401 static const int z80_pio_addr[4] = {1, 3, 0, 2};
402 for(int i = 0; i < 4; i++) {
403 io->set_iomap_alias_rw(0xb0 + i, sio_rs, z80_sio_addr[i]);
404 io->set_iomap_alias_rw(0xf4 + i, sio_qd, z80_sio_addr[i]);
405 io->set_iomap_alias_rw(0xfc + i, pio_int, z80_pio_addr[i]);
409 io->set_iovalue_single_r(0xfe, 0xc0);
412 // initialize all devices
413 for(DEVICE* device = first_device; device; device = device->next_device) {
414 device->initialize();
416 #if defined(_MZ800) || defined(_MZ1500)
417 for(int i = 0; i < MAX_DRIVE; i++) {
418 fdc->set_drive_type(i, DRIVE_TYPE_2DD);
425 // delete all devices
426 for(DEVICE* device = first_device; device;) {
427 DEVICE *next_device = device->next_device;
430 device = next_device;
434 DEVICE* VM::get_device(int id)
436 for(DEVICE* device = first_device; device; device = device->next_device) {
437 if(device->this_device_id == id) {
444 // ----------------------------------------------------------------------------
445 // drive virtual machine
446 // ----------------------------------------------------------------------------
451 for(DEVICE* device = first_device; device; device = device->next_device) {
454 and_int->write_signal(SIG_AND_BIT_0, 0, 1); // CLOCK = L
455 and_int->write_signal(SIG_AND_BIT_1, 1, 1); // INTMASK = H
456 #if defined(_MZ800) || defined(_MZ1500)
457 and_snd->write_signal(SIG_AND_BIT_1, 1, 1); // SNDMASK = H
460 pio_int->write_signal(SIG_Z80PIO_PORT_A, 0x02, 0x03); // BUSY = L, PE = H
469 // ----------------------------------------------------------------------------
471 // ----------------------------------------------------------------------------
474 DEVICE *VM::get_cpu(int index)
483 // ----------------------------------------------------------------------------
485 // ----------------------------------------------------------------------------
487 void VM::draw_screen()
489 memory->draw_screen();
492 #if defined(_MZ800) || defined(_MZ1500)
493 uint32_t VM::get_access_lamp_status()
495 uint32_t status = fdc->read_signal(0) | qd->read_signal(0);
496 return (status & (1 | 4)) ? 1 : (status & (2 | 8)) ? 2 : 0;
500 // ----------------------------------------------------------------------------
502 // ----------------------------------------------------------------------------
504 void VM::initialize_sound(int rate, int samples)
506 // init sound manager
507 event->initialize_sound(rate, samples);
510 pcm->initialize_sound(rate, 8000);
512 psg->initialize_sound(rate, 3579545, 8000);
513 #elif defined(_MZ1500)
514 psg_l->initialize_sound(rate, 3579545, 8000);
515 psg_r->initialize_sound(rate, 3579545, 8000);
519 uint16_t* VM::create_sound(int* extra_frames)
521 return event->create_sound(extra_frames);
524 int VM::get_sound_buffer_ptr()
526 return event->get_sound_buffer_ptr();
529 #ifdef USE_SOUND_VOLUME
530 void VM::set_sound_device_volume(int ch, int decibel_l, int decibel_r)
534 psg->set_volume(0, decibel_l, decibel_r);
536 #elif defined(_MZ1500)
538 psg_l->set_volume(0, decibel_l, decibel_r);
539 } else if(ch-- == 0) {
540 psg_r->set_volume(0, decibel_l, decibel_r);
544 pcm->set_volume(0, decibel_l, decibel_r);
545 } else if(ch-- == 0) {
546 drec->set_volume(0, decibel_l, decibel_r);
548 #if defined(USE_SOUND_FILES)
549 #if defined(_MZ1500) || defined(_MZ800)
551 fdc->set_volume(MB8877_SND_TYPE_SEEK, decibel_l, decibel_r);
555 drec->set_volume(2 + DATAREC_SNDFILE_RELAY_ON, decibel_l, decibel_r);
556 drec->set_volume(2 + DATAREC_SNDFILE_RELAY_OFF, decibel_l, decibel_r);
557 drec->set_volume(2 + DATAREC_SNDFILE_EJECT, decibel_l, decibel_r);
563 // ----------------------------------------------------------------------------
565 // ----------------------------------------------------------------------------
567 void VM::play_tape(const _TCHAR* file_path)
569 drec->play_tape(file_path);
570 drec->set_remote(true);
573 void VM::rec_tape(const _TCHAR* file_path)
575 drec->rec_tape(file_path);
576 drec->set_remote(true);
579 void VM::close_tape()
581 #if defined(USE_SOUND_FILES)
582 drec->write_signal(SIG_SOUNDER_ADD + DATAREC_SNDFILE_EJECT, 1, 1);
588 drec->set_remote(false);
591 bool VM::is_tape_inserted()
593 return drec->is_tape_inserted();
596 bool VM::is_tape_playing()
598 return drec->is_tape_playing();
601 bool VM::is_tape_recording()
603 return drec->is_tape_recording();
606 int VM::get_tape_position()
608 return drec->get_tape_position();
614 drec->set_remote(true);
619 drec->set_remote(false);
622 void VM::push_fast_forward()
625 drec->set_remote(true);
628 void VM::push_fast_rewind()
630 drec->set_ff_rew(-1);
631 drec->set_remote(true);
634 #if defined(_MZ800) || defined(_MZ1500)
635 void VM::open_quick_disk(int drv, const _TCHAR* file_path)
638 qd->open_disk(file_path);
642 void VM::close_quick_disk(int drv)
649 bool VM::is_quick_disk_inserted(int drv)
652 return qd->is_disk_inserted();
658 void VM::open_floppy_disk(int drv, const _TCHAR* file_path, int bank)
660 fdc->open_disk(drv, file_path, bank);
663 void VM::close_floppy_disk(int drv)
665 fdc->close_disk(drv);
668 bool VM::is_floppy_disk_inserted(int drv)
670 return fdc->is_disk_inserted(drv);
673 void VM::is_floppy_disk_protected(int drv, bool value)
675 fdc->is_disk_protected(drv, value);
678 bool VM::is_floppy_disk_protected(int drv)
680 return fdc->is_disk_protected(drv);
684 bool VM::is_frame_skippable()
686 return event->is_frame_skippable();
689 void VM::update_config()
692 if(boot_mode != config.boot_mode) {
693 // boot mode is changed !!!
694 boot_mode = config.boot_mode;
698 for(DEVICE* device = first_device; device; device = device->next_device) {
699 device->update_config();
706 #define STATE_VERSION 1
708 void VM::save_state(FILEIO* state_fio)
710 state_fio->FputUint32(STATE_VERSION);
712 for(DEVICE* device = first_device; device; device = device->next_device) {
713 device->save_state(state_fio);
716 state_fio->FputInt32(boot_mode);
720 bool VM::load_state(FILEIO* state_fio)
722 if(state_fio->FgetUint32() != STATE_VERSION) {
725 for(DEVICE* device = first_device; device; device = device->next_device) {
726 if(!device->load_state(state_fio)) {
731 boot_mode = state_fio->FgetInt32();