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 and_int = new AND(this, emu);
68 drec = new DATAREC(this, emu);
69 pit = new I8253(this, emu);
70 pio = new I8255(this, emu);
71 io = new IO(this, emu);
72 pcm = new PCM1BIT(this, emu);
73 cpu = new Z80(this, emu);
75 // cmos = new CMOS(this, emu);
76 emm = new EMM(this, emu);
77 kanji = new KANJI(this, emu);
78 keyboard = new KEYBOARD(this, emu);
79 memory = new MEMORY(this, emu);
80 ramfile = new RAMFILE(this, emu);
82 #if defined(_MZ800) || defined(_MZ1500)
83 and_snd = new AND(this, emu);
84 fdc = new MB8877(this, emu); // mb8876
86 not_pit = new NOT(this, emu);
87 psg = new SN76489AN(this, emu);
88 #elif defined(_MZ1500)
89 if(config.printer_device_type == 0) {
90 printer = new PRNFILE(this, emu);
91 } else if(config.printer_device_type == 1) {
92 printer = new MZ1P17(this, emu);
96 not_reset = new NOT(this, emu);
97 not_strobe = new NOT(this, emu);
98 psg_l = new SN76489AN(this, emu);
99 psg_r = new SN76489AN(this, emu);
101 pio_int = new Z80PIO(this, emu);
102 sio_rs = new Z80SIO(this, emu);
103 sio_qd = new Z80SIO(this, emu);
105 floppy = new FLOPPY(this, emu);
107 psg = new PSG(this, emu);
109 qd = new QUICKDISK(this, emu);
113 event->set_context_cpu(cpu);
114 event->set_context_sound(pcm);
116 event->set_context_sound(psg);
117 #elif defined(_MZ1500)
118 event->set_context_sound(psg_l);
119 event->set_context_sound(psg_r);
121 event->set_context_sound(drec);
123 memory->set_context_cpu(cpu);
126 memory->set_context_pio(pio);
127 memory->set_context_pit(pit);
131 psg->set_context_psg_l(psg_l);
132 psg->set_context_psg_r(psg_r);
136 // 8253:CLK#0 <- 1.10MHz
137 pit->set_constant_clock(0, 1100000);
139 // 8253:CLK#0 <- 895KHz
140 pit->set_constant_clock(0, CPU_CLOCKS / 4);
143 #if defined(_MZ800) || defined(_MZ1500)
144 // 8253:OUT#0 AND 8255:PC0 -> SPEAKER
145 pit->set_context_ch0(and_snd, SIG_AND_BIT_0, 1);
146 pio->set_context_port_c(and_snd, SIG_AND_BIT_1, 1, 0);
147 and_snd->set_context_out(pcm, SIG_PCM1BIT_SIGNAL, 1);
148 and_snd->set_mask(SIG_AND_BIT_0 | SIG_AND_BIT_1);
150 // 8253:OUT#0 -> SPEAKER
151 pit->set_context_ch0(pcm, SIG_PCM1BIT_SIGNAL, 1);
154 // 8253:OUT#0 -> NOT -> Z80PIO:PA4
155 pit->set_context_ch0(not_pit, SIG_NOT_INPUT, 1);
156 not_pit->set_context_out(pio_int, SIG_Z80PIO_PORT_A, 0x10);
157 #elif defined(_MZ1500)
158 // 8253:OUT#0 -> Z80PIO:PA4
159 pit->set_context_ch0(pio_int, SIG_Z80PIO_PORT_A, 0x10);
162 // 8253:CLK#1 <- 15.7KHz
163 pit->set_constant_clock(1, CPU_CLOCKS / 228);
165 // 8253:OUT#1 -> 8253:CLK#2
166 pit->set_context_ch1(pit, SIG_I8253_CLOCK_2, 1);
168 // 8253:OUT#2 (N)AND 8255:PC2 -> Z80:INT
169 pit->set_context_ch2(and_int, SIG_AND_BIT_0, 1);
170 pio->set_context_port_c(and_int, SIG_AND_BIT_1, 4, 0);
171 and_int->set_context_out(cpu, SIG_CPU_IRQ, 1);
172 and_int->set_mask(SIG_AND_BIT_0 | SIG_AND_BIT_1);
175 // 8253:OUT#2 -> Z80PIO:PA5
176 pit->set_context_ch2(pio_int, SIG_Z80PIO_PORT_A, 0x20);
179 // 8255:PA0-3 -> KEYBOARD:STROBE
180 pio->set_context_port_a(keyboard, SIG_KEYBOARD_COLUMN, 0x0f, 0);
182 // 8255:PA4 -> JOYSTICK #1
183 // 8255:PA5 -> JOYSTICK #2
185 // 8255:PA7 -> 556 RESET
187 // 8255:PB0-7 <- KEYBOARD:DATA
188 keyboard->set_context_pio(pio);
190 // 8255:PC0 -> AND -> SPEAKER
191 // 8255:PC1 -> DATA RECORDER:WRITE DATA
192 pio->set_context_port_c(drec, SIG_DATAREC_MIC, 0x02, 0);
193 // 8255:PC2 -> (N)AND -> Z80:INT
194 // 8255:PC3 -> DATA RECORDER:MOTOR ON/OFF
195 pio->set_context_port_c(drec, SIG_DATAREC_TRIG, 0x08, 0);
196 // 8255:PC4 <- DATA RECORDER:MOTOR REMOTE
197 drec->set_context_remote(pio, SIG_I8255_PORT_C, 0x10);
198 // 8255:PC5 <- DATA RECORDER:READ DATA
199 drec->set_context_ear(pio, SIG_I8255_PORT_C, 0x20);
200 // 8255:PC6 <- MEMORY:556 OUT (1.5KHz)
201 // 8255:PC7 <- MEMORY:VBLANK
203 #if defined(_MZ800) || defined(_MZ1500)
207 // Z80PIO:PA4 <- NOT <- 8253:OUT#0
208 // Z80PIO:PA5 <- HBLANK
209 memory->set_context_pio_int(pio_int);
210 #elif defined(_MZ1500)
211 // Z80PIO:PA0 <- PRINTER:RDA (BUSY)
212 // Z80PIO:PA1 <- PRINTER:STA (PE)
213 if(config.printer_device_type == 0) {
214 PRNFILE *prnfile = (PRNFILE *)printer;
215 prnfile->set_context_busy(pio_int, SIG_Z80PIO_PORT_A, 0x01);
216 } else if(config.printer_device_type == 1) {
217 MZ1P17 *mz1p17 = (MZ1P17 *)printer;
218 mz1p17->mode = MZ1P17_MODE_MZ2;
219 mz1p17->set_context_busy(pio_int, SIG_Z80PIO_PORT_A, 0x01);
221 // Z80PIO:PA4 <- 8253:OUT#0
222 // Z80PIO:PA5 <- 8253:OUT#2
223 // Z80PIO:PA6 -> NOT -> PRINTER:IRT (RESET)
224 // Z80PIO:PA7 -> NOT -> PRINTER:RDP (STROBE)
225 // Z80PIO:PB -> PRINTER:DATA
226 pio_int->set_context_port_a(not_reset, SIG_NOT_INPUT, 0x40, 0);
227 not_reset->set_context_out(printer, SIG_PRINTER_RESET, 0x01);
228 pio_int->set_context_port_a(not_strobe, SIG_NOT_INPUT, 0x80, 0);
229 not_strobe->set_context_out(printer, SIG_PRINTER_STROBE, 0x01);
230 pio_int->set_context_port_b(printer, SIG_PRINTER_DATA, 0xff, 0);
234 #if defined(_MZ800) || defined(_MZ1500)
235 // Z80SIO:RTSA -> QD:WRGA
236 sio_qd->set_context_rts(0, qd, QUICKDISK_SIO_RTSA, 1);
237 // Z80SIO:DTRB -> QD:MTON
238 sio_qd->set_context_dtr(1, qd, QUICKDISK_SIO_DTRB, 1);
239 // Z80SIO:SENDA -> QD:RECV
240 sio_qd->set_context_sync(0, qd, QUICKDISK_SIO_SYNC, 1);
241 sio_qd->set_context_rxdone(0, qd, QUICKDISK_SIO_RXDONE, 1);
242 sio_qd->set_context_send(0, qd, QUICKDISK_SIO_DATA);
243 sio_qd->set_context_break(0, qd, QUICKDISK_SIO_BREAK, 1);
244 // Z80SIO:CTSA <- QD:PROTECT
245 // Z80SIO:DCDA <- QD:INSERT
246 // Z80SIO:DCDB <- QD:HOE
247 qd->set_context_sio(sio_qd);
249 sio_rs->set_tx_clock(0, 1200 * 16); // 1200 baud
250 sio_rs->set_rx_clock(0, 1200 * 16); // baud-rate can be changed by jumper pin
251 sio_rs->set_tx_clock(1, 1200 * 16);
252 sio_rs->set_rx_clock(1, 1200 * 16);
254 sio_qd->set_tx_clock(0, 101562.5);
255 sio_qd->set_rx_clock(0, 101562.5);
256 sio_qd->set_tx_clock(1, 101562.5);
257 sio_qd->set_rx_clock(1, 101562.5);
260 floppy->set_context_cpu(cpu);
261 floppy->set_context_fdc(fdc);
262 fdc->set_context_drq(floppy, SIG_FLOPPY_DRQ, 1);
266 cpu->set_context_mem(memory);
267 cpu->set_context_io(io);
268 #if defined(_MZ800) || defined(_MZ1500)
269 cpu->set_context_intr(pio_int);
270 // z80 family daisy chain
272 pio_int->set_context_intr(cpu, 1);
273 pio_int->set_context_child(sio_rs);
274 sio_rs->set_context_intr(cpu, 2);
275 sio_rs->set_context_child(sio_qd);
276 sio_qd->set_context_intr(cpu, 3);
278 cpu->set_context_intr(dummy);
281 cpu->set_context_debugger(new DEBUGGER(this, emu));
285 io->set_iomap_range_rw(0x00, 0x03, emm);
287 io->set_iomap_range_rw(0xb8, 0xb9, kanji);
289 io->set_iomap_range_rw(0xea, 0xeb, ramfile);
291 // io->set_iomap_range_rw(0xf8, 0xfa, cmos);
295 io->set_iomap_range_rw(0xd0, 0xd3, pio);
296 io->set_iomap_range_rw(0xd4, 0xd7, pit);
299 #if defined(_MZ800) || defined(_MZ1500)
301 io->set_iomap_range_rw(0xd8, 0xdb, fdc);
302 io->set_iomap_range_w(0xdc, 0xdf, floppy);
307 io->set_iomap_range_r(0xe0, 0xe1, memory);
308 io->set_iomap_range_w(0xe0, 0xe6, memory);
309 #elif defined(_MZ1500)
310 io->set_iomap_range_w(0xe0, 0xe6, memory);
312 io->set_iomap_range_w(0xe0, 0xe4, memory);
317 io->set_iomap_range_w(0xcc, 0xcf, memory);
318 io->set_iomap_single_r(0xce, memory);
320 io->set_iomap_single_w(0xf0, memory);
321 #elif defined(_MZ1500)
323 io->set_iomap_range_w(0xf0, 0xf1, memory);
328 // io->set_iomap_range_r(0xf0, 0xf1, joystick);
333 io->set_iomap_single_w(0xf2, psg);
334 #elif defined(_MZ1500)
335 io->set_iomap_single_w(0xe9, psg);
336 io->set_iomap_single_w(0xf2, psg_l);
337 io->set_iomap_single_w(0xf3, psg_r);
340 #if defined(_MZ800) || defined(_MZ1500)
342 // z80pio and z80sio*2
343 static const int z80_sio_addr[4] = {0, 2, 1, 3};
344 static const int z80_pio_addr[4] = {1, 3, 0, 2};
345 for(int i = 0; i < 4; i++) {
346 io->set_iomap_alias_rw(0xb0 + i, sio_rs, z80_sio_addr[i]);
347 io->set_iomap_alias_rw(0xf4 + i, sio_qd, z80_sio_addr[i]);
348 io->set_iomap_alias_rw(0xfc + i, pio_int, z80_pio_addr[i]);
352 io->set_iovalue_single_r(0xfe, 0xc0);
355 // initialize all devices
356 for(DEVICE* device = first_device; device; device = device->next_device) {
357 device->initialize();
359 #if defined(_MZ800) || defined(_MZ1500)
360 for(int i = 0; i < MAX_DRIVE; i++) {
361 fdc->set_drive_type(i, DRIVE_TYPE_2DD);
368 // delete all devices
369 for(DEVICE* device = first_device; device;) {
370 DEVICE *next_device = device->next_device;
373 device = next_device;
377 DEVICE* VM::get_device(int id)
379 for(DEVICE* device = first_device; device; device = device->next_device) {
380 if(device->this_device_id == id) {
387 // ----------------------------------------------------------------------------
388 // drive virtual machine
389 // ----------------------------------------------------------------------------
394 for(DEVICE* device = first_device; device; device = device->next_device) {
397 and_int->write_signal(SIG_AND_BIT_0, 0, 1); // CLOCK = L
398 and_int->write_signal(SIG_AND_BIT_1, 1, 1); // INTMASK = H
399 #if defined(_MZ800) || defined(_MZ1500)
400 and_snd->write_signal(SIG_AND_BIT_1, 1, 1); // SNDMASK = H
403 pio_int->write_signal(SIG_Z80PIO_PORT_A, 0x02, 0x03); // BUSY = L, PE = H
412 // ----------------------------------------------------------------------------
414 // ----------------------------------------------------------------------------
417 DEVICE *VM::get_cpu(int index)
426 // ----------------------------------------------------------------------------
428 // ----------------------------------------------------------------------------
430 void VM::draw_screen()
432 memory->draw_screen();
435 #if defined(_MZ800) || defined(_MZ1500)
436 int VM::get_access_lamp_status()
438 uint32 status = fdc->read_signal(0) | qd->read_signal(0);
439 return (status & (1 | 4)) ? 1 : (status & (2 | 8)) ? 2 : 0;
443 // ----------------------------------------------------------------------------
445 // ----------------------------------------------------------------------------
447 void VM::initialize_sound(int rate, int samples)
449 // init sound manager
450 event->initialize_sound(rate, samples);
453 pcm->initialize_sound(rate, 8000);
455 psg->initialize_sound(rate, 3579545, 8000);
456 #elif defined(_MZ1500)
457 psg_l->initialize_sound(rate, 3579545, 8000);
458 psg_r->initialize_sound(rate, 3579545, 8000);
462 uint16* VM::create_sound(int* extra_frames)
464 return event->create_sound(extra_frames);
467 int VM::get_sound_buffer_ptr()
469 return event->get_sound_buffer_ptr();
472 #ifdef USE_SOUND_VOLUME
473 void VM::set_sound_device_volume(int ch, int decibel_l, int decibel_r)
477 psg->set_volume(0, decibel_l, decibel_r);
479 #elif defined(_MZ1500)
481 psg_l->set_volume(0, decibel_l, decibel_r);
482 } else if(ch-- == 0) {
483 psg_r->set_volume(0, decibel_l, decibel_r);
487 pcm->set_volume(0, decibel_l, decibel_r);
488 } else if(ch-- == 0) {
489 drec->set_volume(0, decibel_l, decibel_r);
494 // ----------------------------------------------------------------------------
496 // ----------------------------------------------------------------------------
498 void VM::play_tape(const _TCHAR* file_path)
500 drec->play_tape(file_path);
501 drec->set_remote(true);
504 void VM::rec_tape(const _TCHAR* file_path)
506 drec->rec_tape(file_path);
507 drec->set_remote(true);
510 void VM::close_tape()
513 drec->write_signal(SIG_DATAREC_REMOTE, 0, 0);
516 bool VM::is_tape_inserted()
518 return drec->is_tape_inserted();
522 bool VM::is_tape_playing()
524 return drec->is_tape_playing();
527 bool VM::is_tape_recording()
529 return drec->is_tape_recording();
532 int VM::get_tape_position()
534 return drec->get_tape_position();
540 drec->write_signal(SIG_DATAREC_REMOTE, 1, 1);
545 drec->write_signal(SIG_DATAREC_REMOTE, 0, 1);
548 void VM::push_fast_forward()
551 drec->write_signal(SIG_DATAREC_REMOTE, 1, 1);
554 void VM::push_fast_rewind()
556 drec->set_ff_rew(-1);
557 drec->write_signal(SIG_DATAREC_REMOTE, 1, 1);
560 #if defined(_MZ800) || defined(_MZ1500)
561 void VM::open_quick_disk(int drv, const _TCHAR* file_path)
564 qd->open_disk(file_path);
568 void VM::close_quick_disk(int drv)
575 bool VM::is_quick_disk_inserted(int drv)
578 return qd->is_disk_inserted();
584 void VM::open_floppy_disk(int drv, const _TCHAR* file_path, int bank)
586 fdc->open_disk(drv, file_path, bank);
589 void VM::close_floppy_disk(int drv)
591 fdc->close_disk(drv);
594 bool VM::is_floppy_disk_inserted(int drv)
596 return fdc->is_disk_inserted(drv);
599 void VM::is_floppy_disk_protected(int drv, bool value)
601 fdc->is_disk_protected(drv, value);
604 bool VM::is_floppy_disk_protected(int drv)
606 return fdc->is_disk_protected(drv);
610 bool VM::is_frame_skippable()
612 return event->is_frame_skippable();
615 void VM::update_config()
618 if(boot_mode != config.boot_mode) {
619 // boot mode is changed !!!
620 boot_mode = config.boot_mode;
624 for(DEVICE* device = first_device; device; device = device->next_device) {
625 device->update_config();
632 #define STATE_VERSION 1
634 void VM::save_state(FILEIO* state_fio)
636 state_fio->FputUint32(STATE_VERSION);
638 for(DEVICE* device = first_device; device; device = device->next_device) {
639 device->save_state(state_fio);
642 state_fio->FputInt32(boot_mode);
646 bool VM::load_state(FILEIO* state_fio)
648 if(state_fio->FgetUint32() != STATE_VERSION) {
651 for(DEVICE* device = first_device; device; device = device->next_device) {
652 if(!device->load_state(state_fio)) {
657 boot_mode = state_fio->FgetInt32();