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"
42 #include "../sn76489an.h"
43 #include "../z80pio.h"
44 #include "../z80sio.h"
49 #include "quickdisk.h"
52 #include "../../fileio.h"
54 // ----------------------------------------------------------------------------
56 // ----------------------------------------------------------------------------
58 VM::VM(EMU* parent_emu) : emu(parent_emu)
61 boot_mode = config.boot_mode;
65 first_device = last_device = NULL;
66 dummy = new DEVICE(this, emu); // must be 1st device
67 event = new EVENT(this, emu); // must be 2nd device
69 and_int = new AND(this, emu);
70 drec = new DATAREC(this, emu);
71 pit = new I8253(this, emu);
72 pio = new I8255(this, emu);
73 io = new IO(this, emu);
74 pcm = new PCM1BIT(this, emu);
75 cpu = new Z80(this, emu);
77 // cmos = new CMOS(this, emu);
78 emm = new EMM(this, emu);
79 kanji = new KANJI(this, emu);
80 keyboard = new KEYBOARD(this, emu);
81 memory = new MEMORY(this, emu);
82 ramfile = new RAMFILE(this, emu);
84 #if defined(_MZ800) || defined(_MZ1500)
85 and_snd = new AND(this, emu);
86 fdc = new MB8877(this, emu); // mb8876
88 not_pit = new NOT(this, emu);
89 psg = new SN76489AN(this, emu);
90 #elif defined(_MZ1500)
91 psg_l = new SN76489AN(this, emu);
92 psg_r = new SN76489AN(this, emu);
94 pio_int = new Z80PIO(this, emu);
95 sio_rs = new Z80SIO(this, emu);
96 sio_qd = new Z80SIO(this, emu);
98 floppy = new FLOPPY(this, emu);
100 psg = new PSG(this, emu);
102 qd = new QUICKDISK(this, emu);
104 event->set_context_sound(drec);
107 event->set_context_cpu(cpu);
108 event->set_context_sound(pcm);
110 event->set_context_sound(psg);
111 #elif defined(_MZ1500)
112 event->set_context_sound(psg_l);
113 event->set_context_sound(psg_r);
115 #if defined(DATAREC_SOUND)
116 event->set_context_sound(drec);
119 memory->set_context_cpu(cpu);
122 memory->set_context_pio(pio);
123 memory->set_context_pit(pit);
127 psg->set_context_psg_l(psg_l);
128 psg->set_context_psg_r(psg_r);
132 // 8253:CLK#0 <- 1.10MHz
133 pit->set_constant_clock(0, 1100000);
135 // 8253:CLK#0 <- 895KHz
136 pit->set_constant_clock(0, CPU_CLOCKS / 4);
139 #if defined(_MZ800) || defined(_MZ1500)
140 // 8253:OUT#0 AND 8255:PC0 -> SPEAKER
141 pit->set_context_ch0(and_snd, SIG_AND_BIT_0, 1);
142 pio->set_context_port_c(and_snd, SIG_AND_BIT_1, 1, 0);
143 and_snd->set_context_out(pcm, SIG_PCM1BIT_SIGNAL, 1);
144 and_snd->set_mask(SIG_AND_BIT_0 | SIG_AND_BIT_1);
146 // 8253:OUT#0 -> SPEAKER
147 pit->set_context_ch0(pcm, SIG_PCM1BIT_SIGNAL, 1);
150 // 8253:OUT#0 -> NOT -> Z80PIO:PA4
151 pit->set_context_ch0(not_pit, SIG_NOT_INPUT, 1);
152 not_pit->set_context_out(pio_int, SIG_Z80PIO_PORT_A, 0x10);
153 #elif defined(_MZ1500)
154 // 8253:OUT#0 -> Z80PIO:PA4
155 pit->set_context_ch0(pio_int, SIG_Z80PIO_PORT_A, 0x10);
158 // 8253:CLK#1 <- 15.7KHz
159 pit->set_constant_clock(1, CPU_CLOCKS / 228);
161 // 8253:OUT#1 -> 8253:CLK#2
162 pit->set_context_ch1(pit, SIG_I8253_CLOCK_2, 1);
164 // 8253:OUT#2 (N)AND 8255:PC2 -> Z80:INT
165 pit->set_context_ch2(and_int, SIG_AND_BIT_0, 1);
166 pio->set_context_port_c(and_int, SIG_AND_BIT_1, 4, 0);
167 and_int->set_context_out(cpu, SIG_CPU_IRQ, 1);
168 and_int->set_mask(SIG_AND_BIT_0 | SIG_AND_BIT_1);
171 // 8253:OUT#2 -> Z80PIO:PA5
172 pit->set_context_ch2(pio_int, SIG_Z80PIO_PORT_A, 0x20);
175 // 8255:PA0-3 -> KEYBOARD:STROBE
176 pio->set_context_port_a(keyboard, SIG_KEYBOARD_COLUMN, 0x0f, 0);
178 // 8255:PA4 -> JOYSTICK #1
179 // 8255:PA5 -> JOYSTICK #2
181 // 8255:PA7 -> 556 RESET
183 // 8255:PB0-7 <- KEYBOARD:DATA
184 keyboard->set_context_pio(pio);
186 // 8255:PC0 -> AND -> SPEAKER
187 // 8255:PC1 -> DATA RECORDER:WRITE DATA
188 pio->set_context_port_c(drec, SIG_DATAREC_OUT, 0x02, 0);
189 // 8255:PC2 -> (N)AND -> Z80:INT
190 // 8255:PC3 -> DATA RECORDER:MOTOR ON/OFF
191 pio->set_context_port_c(drec, SIG_DATAREC_TRIG, 0x08, 0);
192 // 8255:PC4 <- DATA RECORDER:MOTOR REMOTE
193 drec->set_context_remote(pio, SIG_I8255_PORT_C, 0x10);
194 // 8255:PC5 <- DATA RECORDER:READ DATA
195 drec->set_context_out(pio, SIG_I8255_PORT_C, 0x20);
196 // 8255:PC6 <- MEMORY:556 OUT (1.5KHz)
197 // 8255:PC7 <- MEMORY:VBLANK
199 #if defined(_MZ800) || defined(_MZ1500)
200 // Z80PIO:PA0 <- PRINTER:RDA
201 // Z80PIO:PA1 <- PRINTER:STA
205 // Z80PIO:PA4 <- NOT <- 8253:OUT#0
206 // Z80PIO:PA5 <- HBLANK
207 memory->set_context_pio_int(pio_int);
208 #elif defined(_MZ1500)
209 // Z80PIO:PA4 <- 8253:OUT#0
210 // Z80PIO:PA5 <- 8253:OUT#2
212 // Z80PIO:PA6 -> PRINTER:IRT
213 // Z80PIO:PA7 -> PRINTER:RDP
216 #if defined(_MZ800) || defined(_MZ1500)
217 // Z80SIO:RTSA -> QD:WRGA
218 sio_qd->set_context_rts(0, qd, QUICKDISK_SIO_RTSA, 1);
219 // Z80SIO:DTRB -> QD:MTON
220 sio_qd->set_context_dtr(1, qd, QUICKDISK_SIO_DTRB, 1);
221 // Z80SIO:SENDA -> QD:RECV
222 sio_qd->set_context_sync(0, qd, QUICKDISK_SIO_SYNC, 1);
223 sio_qd->set_context_rxdone(0, qd, QUICKDISK_SIO_RXDONE, 1);
224 sio_qd->set_context_send(0, qd, QUICKDISK_SIO_DATA);
225 sio_qd->set_context_break(0, qd, QUICKDISK_SIO_BREAK, 1);
226 // Z80SIO:CTSA <- QD:PROTECT
227 // Z80SIO:DCDA <- QD:INSERT
228 // Z80SIO:DCDB <- QD:HOE
229 qd->set_context_sio(sio_qd);
231 sio_rs->set_tx_clock(0, 1200 * 16); // 1200 baud
232 sio_rs->set_rx_clock(0, 1200 * 16); // baud-rate can be changed by jumper pin
233 sio_rs->set_tx_clock(1, 1200 * 16);
234 sio_rs->set_rx_clock(1, 1200 * 16);
236 sio_qd->set_tx_clock(0, 101562.5);
237 sio_qd->set_rx_clock(0, 101562.5);
238 sio_qd->set_tx_clock(1, 101562.5);
239 sio_qd->set_rx_clock(1, 101562.5);
242 floppy->set_context_cpu(cpu);
243 floppy->set_context_fdc(fdc);
244 fdc->set_context_drq(floppy, SIG_FLOPPY_DRQ, 1);
248 cpu->set_context_mem(memory);
249 cpu->set_context_io(io);
250 #if defined(_MZ800) || defined(_MZ1500)
251 cpu->set_context_intr(pio_int);
252 // z80 family daisy chain
254 pio_int->set_context_intr(cpu, 1);
255 pio_int->set_context_child(sio_rs);
256 sio_rs->set_context_intr(cpu, 2);
257 sio_rs->set_context_child(sio_qd);
258 sio_qd->set_context_intr(cpu, 3);
260 cpu->set_context_intr(dummy);
263 cpu->set_context_debugger(new DEBUGGER(this, emu));
267 io->set_iomap_range_rw(0x00, 0x03, emm);
269 io->set_iomap_range_rw(0xb8, 0xb9, kanji);
271 io->set_iomap_range_rw(0xea, 0xeb, ramfile);
273 // io->set_iomap_range_rw(0xf8, 0xfa, cmos);
277 io->set_iomap_range_rw(0xd0, 0xd3, pio);
278 io->set_iomap_range_rw(0xd4, 0xd7, pit);
281 #if defined(_MZ800) || defined(_MZ1500)
283 io->set_iomap_range_rw(0xd8, 0xdb, fdc);
284 io->set_iomap_range_w(0xdc, 0xdf, floppy);
289 io->set_iomap_range_r(0xe0, 0xe1, memory);
290 io->set_iomap_range_w(0xe0, 0xe6, memory);
291 #elif defined(_MZ1500)
292 io->set_iomap_range_w(0xe0, 0xe6, memory);
294 io->set_iomap_range_w(0xe0, 0xe4, memory);
299 io->set_iomap_range_w(0xcc, 0xcf, memory);
300 io->set_iomap_single_r(0xce, memory);
302 io->set_iomap_single_w(0xf0, memory);
303 #elif defined(_MZ1500)
305 io->set_iomap_range_w(0xf0, 0xf1, memory);
310 // io->set_iomap_range_r(0xf0, 0xf1, joystick);
315 io->set_iomap_single_w(0xf2, psg);
316 #elif defined(_MZ1500)
317 io->set_iomap_single_w(0xe9, psg);
318 io->set_iomap_single_w(0xf2, psg_l);
319 io->set_iomap_single_w(0xf3, psg_r);
322 #if defined(_MZ800) || defined(_MZ1500)
324 // z80pio and z80sio*2
325 static const int z80_sio_addr[4] = {0, 2, 1, 3};
326 static const int z80_pio_addr[4] = {1, 3, 0, 2};
327 for(int i = 0; i < 4; i++) {
328 io->set_iomap_alias_rw(0xb0 + i, sio_rs, z80_sio_addr[i]);
329 io->set_iomap_alias_rw(0xf4 + i, sio_qd, z80_sio_addr[i]);
330 io->set_iomap_alias_rw(0xfc + i, pio_int, z80_pio_addr[i]);
334 io->set_iovalue_single_r(0xfe, 0xc0);
337 // initialize all devices
338 for(DEVICE* device = first_device; device; device = device->next_device) {
339 device->initialize();
341 #if defined(_MZ800) || defined(_MZ1500)
342 for(int i = 0; i < MAX_DRIVE; i++) {
343 fdc->set_drive_type(i, DRIVE_TYPE_2DD);
350 // delete all devices
351 for(DEVICE* device = first_device; device;) {
352 DEVICE *next_device = device->next_device;
355 device = next_device;
359 DEVICE* VM::get_device(int id)
361 for(DEVICE* device = first_device; device; device = device->next_device) {
362 if(device->this_device_id == id) {
369 // ----------------------------------------------------------------------------
370 // drive virtual machine
371 // ----------------------------------------------------------------------------
376 for(DEVICE* device = first_device; device; device = device->next_device) {
379 and_int->write_signal(SIG_AND_BIT_0, 0, 1); // CLOCK = L
380 and_int->write_signal(SIG_AND_BIT_1, 1, 1); // INTMASK = H
381 #if defined(_MZ800) || defined(_MZ1500)
382 and_snd->write_signal(SIG_AND_BIT_1, 1, 1); // SNDMASK = H
391 // ----------------------------------------------------------------------------
393 // ----------------------------------------------------------------------------
396 DEVICE *VM::get_cpu(int index)
405 // ----------------------------------------------------------------------------
407 // ----------------------------------------------------------------------------
409 void VM::draw_screen()
411 memory->draw_screen();
414 #if defined(_MZ800) || defined(_MZ1500)
415 int VM::access_lamp()
417 uint32 status = fdc->read_signal(0) | qd->read_signal(0); // 4 + 1: Nagative
418 return (status & (1 | 4)) ? 1 : (status & (2 | 8)) ? 2 : 0;
422 // ----------------------------------------------------------------------------
424 // ----------------------------------------------------------------------------
426 void VM::initialize_sound(int rate, int samples)
428 // init sound manager
429 event->initialize_sound(rate, samples);
432 pcm->init(rate, 8000);
434 psg->init(rate, 3579545, 8000);
435 #elif defined(_MZ1500)
436 psg_l->init(rate, 3579545, 8000);
437 psg_r->init(rate, 3579545, 8000);
441 uint16* VM::create_sound(int* extra_frames)
443 return event->create_sound(extra_frames);
446 int VM::sound_buffer_ptr()
448 return event->sound_buffer_ptr();
451 // ----------------------------------------------------------------------------
453 // ----------------------------------------------------------------------------
455 void VM::play_tape(_TCHAR* file_path)
457 drec->play_tape(file_path);
458 drec->write_signal(SIG_DATAREC_REMOTE, 1, 1);
461 void VM::rec_tape(_TCHAR* file_path)
463 drec->rec_tape(file_path);
464 drec->write_signal(SIG_DATAREC_REMOTE, 1, 1);
467 void VM::close_tape()
470 drec->write_signal(SIG_DATAREC_REMOTE, 0, 0);
473 bool VM::tape_inserted()
475 return drec->tape_inserted();
478 int VM::get_tape_ptr()
480 return drec->get_tape_ptr();
485 drec->write_signal(SIG_DATAREC_REMOTE, 1, 1);
490 drec->write_signal(SIG_DATAREC_REMOTE, 0, 0);
493 bool VM::get_tape_play(void)
495 return drec->get_tape_play();
498 #if defined(_MZ800) || defined(_MZ1500)
499 void VM::open_quickdisk(int drv, _TCHAR* file_path)
502 qd->open_disk(file_path);
506 void VM::close_quickdisk(int drv)
513 bool VM::quickdisk_inserted(int drv)
516 return qd->disk_inserted();
522 void VM::write_protect_fd(int drv, bool flag)
524 fdc->write_protect_fd(drv, flag);
527 bool VM::is_write_protect_fd(int drv)
529 return fdc->is_write_protect_fd(drv);
532 void VM::open_disk(int drv, _TCHAR* file_path, int bank)
534 fdc->open_disk(drv, file_path, bank);
537 void VM::close_disk(int drv)
539 fdc->close_disk(drv);
542 bool VM::disk_inserted(int drv)
544 return fdc->disk_inserted(drv);
551 return event->now_skip();
554 void VM::update_config()
557 if(boot_mode != config.boot_mode) {
558 // boot mode is changed !!!
559 boot_mode = config.boot_mode;
563 for(DEVICE* device = first_device; device; device = device->next_device) {
564 device->update_config();
571 #define STATE_VERSION 1
573 void VM::save_state(FILEIO* state_fio)
575 state_fio->FputUint32(STATE_VERSION);
577 for(DEVICE* device = first_device; device; device = device->next_device) {
578 device->save_state(state_fio);
581 state_fio->FputInt32(boot_mode);
585 bool VM::load_state(FILEIO* state_fio)
587 if(state_fio->FgetUint32() != STATE_VERSION) {
590 for(DEVICE* device = first_device; device; device = device->next_device) {
591 if(!device->load_state(state_fio)) {
596 boot_mode = state_fio->FgetInt32();