2 SHARP MZ-80B Emulator 'EmuZ-80B'
3 SHARP MZ-2200 Emulator 'EmuZ-2200'
5 Author : Takeda.Toshiya
12 #include "../../emu.h"
13 #include "../device.h"
16 #include "../datarec.h"
21 #include "../mb8877.h"
22 #include "../mz1p17.h"
24 #include "../pcm1bit.h"
25 #include "../prnfile.h"
27 #include "../z80pio.h"
30 #include "../debugger.h"
36 #include "./memory80b.h"
42 #ifdef SUPPORT_QUICK_DISK
43 #include "../z80sio.h"
44 #include "../mz700/quickdisk.h"
47 #ifdef SUPPORT_16BIT_BOARD
56 using MZ2500::KEYBOARD;
59 using MZ2500::PRINTER;
61 #ifdef SUPPORT_QUICK_DISK
62 using MZ700::QUICKDISK;
64 #ifdef SUPPORT_16BIT_BOARD
68 // ----------------------------------------------------------------------------
70 // ----------------------------------------------------------------------------
72 VM::VM(EMU* parent_emu) : VM_TEMPLATE(parent_emu)
75 first_device = last_device = NULL;
76 dummy = new DEVICE(this, emu); // must be 1st device
77 event = new EVENT(this, emu); // must be 2nd device
78 dummy->set_device_name(_T("1st Dummy"));
80 drec = new DATAREC(this, emu);
81 drec->set_context_noise_play(new NOISE(this, emu));
82 drec->set_context_noise_stop(new NOISE(this, emu));
83 drec->set_context_noise_fast(new NOISE(this, emu));
84 pit = new I8253(this, emu);
85 pio_i = new I8255(this, emu);
86 io = new IO(this, emu);
87 fdc = new MB8877(this, emu);
88 fdc->set_context_noise_seek(new NOISE(this, emu));
89 fdc->set_context_noise_head_down(new NOISE(this, emu));
90 fdc->set_context_noise_head_up(new NOISE(this, emu));
91 pcm = new PCM1BIT(this, emu);
92 cpu = new Z80(this, emu);
93 pio = new Z80PIO(this, emu);
94 pio_i->set_device_name(_T("i8255 PIO(CMT/CRTC)"));
95 pio->set_device_name(_T("Z80 PIO(KEYBOARD/CRTC)"));
97 cmt = new CMT(this, emu);
98 floppy = new FLOPPY(this, emu);
99 keyboard = new KEYBOARD(this, emu);
100 memory = new MEMORY(this, emu);
101 mz1r12 = new MZ1R12(this, emu);
102 mz1r13 = new MZ1R13(this, emu);
103 printer = new PRINTER(this, emu);
104 timer = new TIMER(this, emu);
106 #ifdef SUPPORT_QUICK_DISK
107 sio = new Z80SIO(this, emu);
108 qd = new QUICKDISK(this, emu);
109 sio->set_device_name(_T("Z80 SIO(QD)"));
112 #ifdef SUPPORT_16BIT_BOARD
113 pio_to16 = new Z80PIO(this, emu);
114 cpu_16 = new I286(this, emu); // 8088
115 pic_16 = new I8259(this, emu);
116 mz1m01 = new MZ1M01(this, emu);
117 pio_to16->set_device_name(_T("Z80 PIO(16BIT BOARD)"));
118 cpu_16->set_device_name(_T("CPU i286(16BIT BOARD)"));
119 pic_16->set_device_name(_T("i8259 PIC(16BIT BOARD)"));
123 event->set_context_cpu(cpu, config.cpu_type ? CPU_CLOCKS_HIGH : CPU_CLOCKS);
124 #ifdef SUPPORT_16BIT_BOARD
125 event->set_context_cpu(cpu_16, 5000000);
127 event->set_context_sound(pcm);
128 event->set_context_sound(drec);
129 event->set_context_sound(fdc->get_context_noise_seek());
130 event->set_context_sound(fdc->get_context_noise_head_down());
131 event->set_context_sound(fdc->get_context_noise_head_up());
132 event->set_context_sound(drec->get_context_noise_play());
133 event->set_context_sound(drec->get_context_noise_stop());
134 event->set_context_sound(drec->get_context_noise_fast());
136 drec->set_context_ear(cmt, SIG_CMT_OUT, 1);
137 drec->set_context_remote(cmt, SIG_CMT_REMOTE, 1);
138 drec->set_context_end(cmt, SIG_CMT_END, 1);
139 drec->set_context_top(cmt, SIG_CMT_TOP, 1);
141 pit->set_context_ch0(pit, SIG_I8253_CLOCK_1, 1);
142 pit->set_context_ch1(pit, SIG_I8253_CLOCK_2, 1);
143 pit->set_constant_clock(0, 31250);
144 pio_i->set_context_port_a(cmt, SIG_CMT_PIO_PA, 0xff, 0);
145 pio_i->set_context_port_a(memory, SIG_CRTC_REVERSE, 0x10, 0);
146 pio_i->set_context_port_c(memory, SIG_CRTC_VGATE, 0x01, 0);
147 pio_i->set_context_port_c(cmt, SIG_CMT_PIO_PC, 0xff, 0);
148 pio_i->set_context_port_c(pcm, SIG_PCM1BIT_SIGNAL, 0x04, 0);
149 // Sound:: Force realtime rendering. This is temporally fix. 20161024 K.O
150 //pcm->set_realtime_render(true);
152 pio->set_context_port_a(memory, SIG_MEMORY_VRAM_SEL, 0xc0, 0);
153 pio->set_context_port_a(memory, SIG_CRTC_WIDTH80, 0x20, 0);
154 pio->set_context_port_a(keyboard, SIG_KEYBOARD_COLUMN, 0x1f, 0);
156 cmt->set_context_pio(pio_i);
157 cmt->set_context_drec(drec);
158 floppy->set_context_fdc(fdc);
159 keyboard->set_context_pio_i(pio_i);
160 keyboard->set_context_pio(pio);
161 memory->set_context_cpu(cpu);
162 memory->set_context_pio(pio_i);
163 if(config.printer_type == 0) {
164 printer->set_context_prn(new PRNFILE(this, emu));
165 } else if(config.printer_type == 1) {
166 MZ1P17 *mz1p17 = new MZ1P17(this, emu);
167 mz1p17->mode = MZ1P17_MODE_MZ1;
168 printer->set_context_prn(mz1p17);
169 } else if(config.printer_type == 2) {
170 MZ1P17 *mz1p17 = new MZ1P17(this, emu);
171 mz1p17->mode = MZ1P17_MODE_MZ3;
172 printer->set_context_prn(mz1p17);
174 printer->set_context_prn(dummy);
176 timer->set_context_pit(pit);
178 #ifdef SUPPORT_QUICK_DISK
179 // Z80SIO:RTSA -> QD:WRGA
180 sio->set_context_rts(0, qd, QUICKDISK_SIO_RTSA, 1);
181 // Z80SIO:DTRB -> QD:MTON
182 sio->set_context_dtr(1, qd, QUICKDISK_SIO_DTRB, 1);
183 // Z80SIO:SENDA -> QD:RECV
184 sio->set_context_sync(0, qd, QUICKDISK_SIO_SYNC, 1);
185 sio->set_context_rxdone(0, qd, QUICKDISK_SIO_RXDONE, 1);
186 sio->set_context_send(0, qd, QUICKDISK_SIO_DATA);
187 sio->set_context_break(0, qd, QUICKDISK_SIO_BREAK, 1);
188 // Z80SIO:CTSA <- QD:PROTECT
189 // Z80SIO:DCDA <- QD:INSERT
190 // Z80SIO:DCDB <- QD:HOE
191 qd->set_context_sio(sio);
193 sio->set_tx_clock(0, 101562.5);
194 sio->set_rx_clock(0, 101562.5);
195 sio->set_tx_clock(1, 101562.5);
196 sio->set_rx_clock(1, 101562.5);
199 #ifdef SUPPORT_16BIT_BOARD
200 pio_to16->set_context_port_a(mz1m01, SIG_MZ1M01_PORT_A, 0xff, 0);
201 pio_to16->set_context_port_b(mz1m01, SIG_MZ1M01_PORT_B, 0x80, 0);
202 pio_to16->set_context_ready_a(pic_16, SIG_I8259_IR0, 1);
203 pio_to16->set_context_ready_b(pic_16, SIG_I8259_IR1, 1);
204 pio_to16->set_context_port_b(pic_16, SIG_I8259_IR2, 0x80, 0);
205 pio_to16->set_hand_shake(0, true);
206 pio_to16->set_hand_shake(1, true);
207 pic_16->set_context_cpu(cpu_16);
208 cpu_16->set_context_mem(mz1m01);
209 cpu_16->set_context_io(mz1m01);
210 cpu_16->set_context_intr(pic_16);
212 cpu_16->set_context_debugger(new DEBUGGER(this, emu));
215 mz1m01->set_context_cpu(cpu_16);
216 mz1m01->set_context_pic(pic_16);
217 mz1m01->set_context_pio(pio_to16);
221 cpu->set_context_mem(memory);
222 cpu->set_context_io(io);
224 // z80 family daisy chain
225 #ifdef SUPPORT_16BIT_BOARD
226 // FIXME: Z80PIO on MZ-1M01 is not daisy-chained to other Z80 family !!!
227 cpu->set_context_intr(pio_to16);
228 pio_to16->set_context_intr(cpu, 0);
229 pio_to16->set_context_child(pio);
231 cpu->set_context_intr(pio);
233 pio->set_context_intr(cpu, 1);
234 #ifdef SUPPORT_QUICK_DISK
235 pio->set_context_child(sio);
236 sio->set_context_intr(cpu, 2);
239 cpu->set_context_debugger(new DEBUGGER(this, emu));
243 io->set_iomap_range_rw(0xb8, 0xbb, mz1r13);
244 #ifdef SUPPORT_QUICK_DISK
245 io->set_iomap_alias_rw(0xd0, sio, 0);
246 io->set_iomap_alias_rw(0xd1, sio, 2);
247 io->set_iomap_alias_rw(0xd2, sio, 1);
248 io->set_iomap_alias_rw(0xd3, sio, 3);
250 #ifdef SUPPORT_16BIT_BOARD
251 io->set_iomap_range_rw(0xd4, 0xd7, pio_to16);
254 io->set_iomap_range_w(0xb4, 0xb4, memory);
256 io->set_iomap_range_rw(0xd8, 0xdb, fdc);
257 io->set_iomap_range_w(0xdc, 0xdd, floppy);
258 io->set_iomap_range_rw(0xe0, 0xe3, pio_i);
259 io->set_iomap_range_rw(0xe4, 0xe7, pit);
260 io->set_iomap_range_rw(0xe8, 0xeb, pio);
261 io->set_iomap_range_w(0xf0, 0xf3, timer);
262 io->set_iomap_range_w(0xf4, 0xf7, memory);
263 io->set_iomap_range_rw(0xf8, 0xfa, mz1r12);
264 io->set_iomap_range_rw(0xfe, 0xff, printer);
266 io->set_iowait_range_rw(0xd8, 0xdf, 1);
267 io->set_iowait_range_rw(0xe8, 0xeb, 1);
269 // initialize all devices
270 #if defined(__GIT_REPO_VERSION)
271 strncpy(_git_revision, __GIT_REPO_VERSION, sizeof(_git_revision) - 1);
273 for(DEVICE* device = first_device; device; device = device->next_device) {
274 device->initialize();
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);
287 // delete all devices
288 for(DEVICE* device = first_device; device;) {
289 DEVICE *next_device = device->next_device;
292 device = next_device;
296 DEVICE* VM::get_device(int id)
298 for(DEVICE* device = first_device; device; device = device->next_device) {
299 if(device->this_device_id == id) {
306 // ----------------------------------------------------------------------------
307 // drive virtual machine
308 // ----------------------------------------------------------------------------
313 for(DEVICE* device = first_device; device; device = device->next_device) {
318 void VM::special_reset()
321 // for(DEVICE* device = first_device; device; device = device->next_device) {
322 // device->special_reset();
324 memory->special_reset();
326 #ifdef SUPPORT_16BIT_BOARD
339 // ----------------------------------------------------------------------------
341 // ----------------------------------------------------------------------------
344 DEVICE *VM::get_cpu(int index)
348 #ifdef SUPPORT_16BIT_BOARD
349 } else if(index == 1) {
357 // ----------------------------------------------------------------------------
359 // ----------------------------------------------------------------------------
361 void VM::draw_screen()
363 memory->draw_screen();
366 // ----------------------------------------------------------------------------
368 // ----------------------------------------------------------------------------
370 void VM::initialize_sound(int rate, int samples)
372 // init sound manager
373 event->initialize_sound(rate, samples);
376 pcm->initialize_sound(rate, 8000);
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 pcm->set_volume(0, decibel_l, decibel_r);
395 drec->set_volume(0, decibel_l, decibel_r);
397 fdc->get_context_noise_seek()->set_volume(0, decibel_l, decibel_r);
398 fdc->get_context_noise_head_down()->set_volume(0, decibel_l, decibel_r);
399 fdc->get_context_noise_head_up()->set_volume(0, decibel_l, decibel_r);
401 drec->get_context_noise_play()->set_volume(0, decibel_l, decibel_r);
402 drec->get_context_noise_stop()->set_volume(0, decibel_l, decibel_r);
403 drec->get_context_noise_fast()->set_volume(0, decibel_l, decibel_r);
408 // ----------------------------------------------------------------------------
410 // ----------------------------------------------------------------------------
412 void VM::open_floppy_disk(int drv, const _TCHAR* file_path, int bank)
414 fdc->open_disk(drv, file_path, bank);
416 if(fdc->get_media_type(drv) == MEDIA_TYPE_2DD) {
417 if(fdc->get_drive_type(drv) == DRIVE_TYPE_2D) {
418 fdc->set_drive_type(drv, DRIVE_TYPE_2DD);
420 } else if(fdc->get_media_type(drv) == MEDIA_TYPE_2D) {
421 if(fdc->get_drive_type(drv) == DRIVE_TYPE_2DD) {
422 fdc->set_drive_type(drv, DRIVE_TYPE_2D);
427 void VM::close_floppy_disk(int drv)
429 fdc->close_disk(drv);
432 bool VM::is_floppy_disk_inserted(int drv)
434 return fdc->is_disk_inserted(drv);
437 void VM::is_floppy_disk_protected(int drv, bool value)
439 fdc->is_disk_protected(drv, value);
442 bool VM::is_floppy_disk_protected(int drv)
444 return fdc->is_disk_protected(drv);
447 uint32_t VM::is_floppy_disk_accessed()
449 return fdc->read_signal(0);
452 #ifdef SUPPORT_QUICK_DISK
453 void VM::open_quick_disk(int drv, const _TCHAR* file_path)
456 qd->open_disk(file_path);
460 void VM::close_quick_disk(int drv)
467 bool VM::is_quick_disk_inserted(int drv)
470 return qd->is_disk_inserted();
476 uint32_t VM::is_quick_disk_accessed()
478 return qd->read_signal(0);
482 void VM::play_tape(int drv, const _TCHAR* file_path)
484 if(check_file_extension(file_path, _T(".dat"))) {
485 memory->load_dat_image(file_path);
487 } else if(check_file_extension(file_path, _T(".mzt")) || check_file_extension(file_path, _T(".mzf"))) {
488 if(config.direct_load_mzt[0] && memory->load_mzt_image(file_path)) {
491 } else if(check_file_extension(file_path, _T(".mtw"))) {
492 memory->load_mzt_image(file_path);
494 bool remote = drec->get_remote();
495 bool opened = drec->play_tape(file_path);
497 if(opened && remote) {
498 // if machine already sets remote on, start playing now
502 cmt->play_tape(opened);
505 void VM::rec_tape(int drv, const _TCHAR* file_path)
507 bool remote = drec->get_remote();
508 bool opened = drec->rec_tape(file_path);
510 if(opened && remote) {
511 // if machine already sets remote on, start recording now
515 cmt->rec_tape(opened);
518 void VM::close_tape(int drv)
523 drec->set_remote(false);
527 bool VM::is_tape_inserted(int drv)
529 return drec->is_tape_inserted();
532 bool VM::is_tape_playing(int drv)
534 return drec->is_tape_playing();
537 bool VM::is_tape_recording(int drv)
539 return drec->is_tape_recording();
542 int VM::get_tape_position(int drv)
544 return drec->get_tape_position();
547 const _TCHAR* VM::get_tape_message(int drv)
549 return drec->get_message();
552 void VM::push_play(int drv)
554 drec->set_remote(false);
556 drec->set_remote(true);
559 void VM::push_stop(int drv)
561 drec->set_remote(false);
564 void VM::push_fast_forward(int drv)
566 drec->set_remote(false);
568 drec->set_remote(true);
571 void VM::push_fast_rewind(int drv)
573 drec->set_remote(false);
574 drec->set_ff_rew(-1);
575 drec->set_remote(true);
578 bool VM::is_frame_skippable()
580 return event->is_frame_skippable();
583 void VM::update_config()
585 for(DEVICE* device = first_device; device; device = device->next_device) {
586 device->update_config();
590 #define STATE_VERSION 4
592 bool VM::process_state(FILEIO* state_fio, bool loading)
594 if(!state_fio->StateCheckUint32(STATE_VERSION)) {
597 for(DEVICE* device = first_device; device; device = device->next_device) {
598 // Note: typeid(foo).name is fixed by recent ABI.Not dec 6.
599 // const char *name = typeid(*device).name();
600 // But, using get_device_name() instead of typeid(foo).name() 20181008 K.O
601 const char *name = device->get_device_name();
602 int len = strlen(name);
604 if(!state_fio->StateCheckInt32(len)) {
606 printf("Class name len Error: DEVID=%d EXPECT=%s\n", device->this_device_id, name);
610 if(!state_fio->StateCheckBuffer(name, len, 1)) {
612 printf("Class name Error: DEVID=%d EXPECT=%s\n", device->this_device_id, name);
616 if(!device->process_state(state_fio, loading)) {
618 printf("Data loading Error: DEVID=%d\n", device->this_device_id);