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
53 // ----------------------------------------------------------------------------
55 // ----------------------------------------------------------------------------
57 VM::VM(EMU* parent_emu) : VM_TEMPLATE(parent_emu)
60 first_device = last_device = NULL;
61 dummy = new DEVICE(this, emu); // must be 1st device
62 event = new EVENT(this, emu); // must be 2nd device
63 dummy->set_device_name(_T("1st Dummy"));
65 drec = new DATAREC(this, emu);
66 drec->set_context_noise_play(new NOISE(this, emu));
67 drec->set_context_noise_stop(new NOISE(this, emu));
68 drec->set_context_noise_fast(new NOISE(this, emu));
69 pit = new I8253(this, emu);
70 pio_i = new I8255(this, emu);
71 io = new IO(this, emu);
72 fdc = new MB8877(this, emu);
73 fdc->set_context_noise_seek(new NOISE(this, emu));
74 fdc->set_context_noise_head_down(new NOISE(this, emu));
75 fdc->set_context_noise_head_up(new NOISE(this, emu));
76 pcm = new PCM1BIT(this, emu);
77 cpu = new Z80(this, emu);
78 pio = new Z80PIO(this, emu);
79 pio_i->set_device_name(_T("i8255 PIO(CMT/CRTC)"));
80 pio->set_device_name(_T("Z80 PIO(KEYBOARD/CRTC)"));
82 cmt = new CMT(this, emu);
83 floppy = new FLOPPY(this, emu);
84 keyboard = new KEYBOARD(this, emu);
85 memory = new MEMORY(this, emu);
86 mz1r12 = new MZ1R12(this, emu);
87 mz1r13 = new MZ1R13(this, emu);
88 printer = new PRINTER(this, emu);
89 timer = new TIMER(this, emu);
91 #ifdef SUPPORT_QUICK_DISK
92 sio = new Z80SIO(this, emu);
93 qd = new QUICKDISK(this, emu);
94 sio->set_device_name(_T("Z80 SIO(QD)"));
97 #ifdef SUPPORT_16BIT_BOARD
98 pio_to16 = new Z80PIO(this, emu);
99 cpu_16 = new I286(this, emu); // 8088
100 pic_16 = new I8259(this, emu);
101 mz1m01 = new MZ1M01(this, emu);
102 pio_to16->set_device_name(_T("Z80 PIO(16BIT BOARD)"));
103 cpu_16->set_device_name(_T("CPU i286(16BIT BOARD)"));
104 pic_16->set_device_name(_T("i8259 PIC(16BIT BOARD)"));
108 event->set_context_cpu(cpu, config.cpu_type ? CPU_CLOCKS_HIGH : CPU_CLOCKS);
109 #ifdef SUPPORT_16BIT_BOARD
110 event->set_context_cpu(cpu_16, 5000000);
112 event->set_context_sound(pcm);
113 event->set_context_sound(drec);
114 event->set_context_sound(fdc->get_context_noise_seek());
115 event->set_context_sound(fdc->get_context_noise_head_down());
116 event->set_context_sound(fdc->get_context_noise_head_up());
117 event->set_context_sound(drec->get_context_noise_play());
118 event->set_context_sound(drec->get_context_noise_stop());
119 event->set_context_sound(drec->get_context_noise_fast());
121 drec->set_context_ear(cmt, SIG_CMT_OUT, 1);
122 drec->set_context_remote(cmt, SIG_CMT_REMOTE, 1);
123 drec->set_context_end(cmt, SIG_CMT_END, 1);
124 drec->set_context_top(cmt, SIG_CMT_TOP, 1);
126 pit->set_context_ch0(pit, SIG_I8253_CLOCK_1, 1);
127 pit->set_context_ch1(pit, SIG_I8253_CLOCK_2, 1);
128 pit->set_constant_clock(0, 31250);
129 pio_i->set_context_port_a(cmt, SIG_CMT_PIO_PA, 0xff, 0);
130 pio_i->set_context_port_a(memory, SIG_CRTC_REVERSE, 0x10, 0);
131 pio_i->set_context_port_c(memory, SIG_CRTC_VGATE, 0x01, 0);
132 pio_i->set_context_port_c(cmt, SIG_CMT_PIO_PC, 0xff, 0);
133 pio_i->set_context_port_c(pcm, SIG_PCM1BIT_SIGNAL, 0x04, 0);
134 // Sound:: Force realtime rendering. This is temporally fix. 20161024 K.O
135 //pcm->set_realtime_render(true);
137 pio->set_context_port_a(memory, SIG_MEMORY_VRAM_SEL, 0xc0, 0);
138 pio->set_context_port_a(memory, SIG_CRTC_WIDTH80, 0x20, 0);
139 pio->set_context_port_a(keyboard, SIG_KEYBOARD_COLUMN, 0x1f, 0);
141 cmt->set_context_pio(pio_i);
142 cmt->set_context_drec(drec);
143 floppy->set_context_fdc(fdc);
144 keyboard->set_context_pio_i(pio_i);
145 keyboard->set_context_pio(pio);
146 memory->set_context_cpu(cpu);
147 memory->set_context_pio(pio_i);
148 if(config.printer_type == 0) {
149 printer->set_context_prn(new PRNFILE(this, emu));
150 } else if(config.printer_type == 1) {
151 MZ1P17 *mz1p17 = new MZ1P17(this, emu);
152 mz1p17->mode = MZ1P17_MODE_MZ1;
153 printer->set_context_prn(mz1p17);
154 } else if(config.printer_type == 2) {
155 MZ1P17 *mz1p17 = new MZ1P17(this, emu);
156 mz1p17->mode = MZ1P17_MODE_MZ3;
157 printer->set_context_prn(mz1p17);
159 printer->set_context_prn(dummy);
161 timer->set_context_pit(pit);
163 #ifdef SUPPORT_QUICK_DISK
164 // Z80SIO:RTSA -> QD:WRGA
165 sio->set_context_rts(0, qd, QUICKDISK_SIO_RTSA, 1);
166 // Z80SIO:DTRB -> QD:MTON
167 sio->set_context_dtr(1, qd, QUICKDISK_SIO_DTRB, 1);
168 // Z80SIO:SENDA -> QD:RECV
169 sio->set_context_sync(0, qd, QUICKDISK_SIO_SYNC, 1);
170 sio->set_context_rxdone(0, qd, QUICKDISK_SIO_RXDONE, 1);
171 sio->set_context_send(0, qd, QUICKDISK_SIO_DATA);
172 sio->set_context_break(0, qd, QUICKDISK_SIO_BREAK, 1);
173 // Z80SIO:CTSA <- QD:PROTECT
174 // Z80SIO:DCDA <- QD:INSERT
175 // Z80SIO:DCDB <- QD:HOE
176 qd->set_context_sio(sio);
178 sio->set_tx_clock(0, 101562.5);
179 sio->set_rx_clock(0, 101562.5);
180 sio->set_tx_clock(1, 101562.5);
181 sio->set_rx_clock(1, 101562.5);
184 #ifdef SUPPORT_16BIT_BOARD
185 pio_to16->set_context_port_a(mz1m01, SIG_MZ1M01_PORT_A, 0xff, 0);
186 pio_to16->set_context_port_b(mz1m01, SIG_MZ1M01_PORT_B, 0x80, 0);
187 pio_to16->set_context_ready_a(pic_16, SIG_I8259_IR0, 1);
188 pio_to16->set_context_ready_b(pic_16, SIG_I8259_IR1, 1);
189 pio_to16->set_context_port_b(pic_16, SIG_I8259_IR2, 0x80, 0);
190 pio_to16->set_hand_shake(0, true);
191 pio_to16->set_hand_shake(1, true);
192 pic_16->set_context_cpu(cpu_16);
193 cpu_16->set_context_mem(mz1m01);
194 cpu_16->set_context_io(mz1m01);
195 cpu_16->set_context_intr(pic_16);
197 cpu_16->set_context_debugger(new DEBUGGER(this, emu));
200 mz1m01->set_context_cpu(cpu_16);
201 mz1m01->set_context_pic(pic_16);
202 mz1m01->set_context_pio(pio_to16);
206 cpu->set_context_mem(memory);
207 cpu->set_context_io(io);
209 // z80 family daisy chain
210 #ifdef SUPPORT_16BIT_BOARD
211 // FIXME: Z80PIO on MZ-1M01 is not daisy-chained to other Z80 family !!!
212 cpu->set_context_intr(pio_to16);
213 pio_to16->set_context_intr(cpu, 0);
214 pio_to16->set_context_child(pio);
216 cpu->set_context_intr(pio);
218 pio->set_context_intr(cpu, 1);
219 #ifdef SUPPORT_QUICK_DISK
220 pio->set_context_child(sio);
221 sio->set_context_intr(cpu, 2);
224 cpu->set_context_debugger(new DEBUGGER(this, emu));
228 io->set_iomap_range_rw(0xb8, 0xbb, mz1r13);
229 #ifdef SUPPORT_QUICK_DISK
230 io->set_iomap_alias_rw(0xd0, sio, 0);
231 io->set_iomap_alias_rw(0xd1, sio, 2);
232 io->set_iomap_alias_rw(0xd2, sio, 1);
233 io->set_iomap_alias_rw(0xd3, sio, 3);
235 #ifdef SUPPORT_16BIT_BOARD
236 io->set_iomap_range_rw(0xd4, 0xd7, pio_to16);
239 io->set_iomap_range_w(0xb4, 0xb4, memory);
241 io->set_iomap_range_rw(0xd8, 0xdb, fdc);
242 io->set_iomap_range_w(0xdc, 0xdd, floppy);
243 io->set_iomap_range_rw(0xe0, 0xe3, pio_i);
244 io->set_iomap_range_rw(0xe4, 0xe7, pit);
245 io->set_iomap_range_rw(0xe8, 0xeb, pio);
246 io->set_iomap_range_w(0xf0, 0xf3, timer);
247 io->set_iomap_range_w(0xf4, 0xf7, memory);
248 io->set_iomap_range_rw(0xf8, 0xfa, mz1r12);
249 io->set_iomap_range_rw(0xfe, 0xff, printer);
251 io->set_iowait_range_rw(0xd8, 0xdf, 1);
252 io->set_iowait_range_rw(0xe8, 0xeb, 1);
254 // initialize all devices
255 #if defined(__GIT_REPO_VERSION)
256 strncpy(_git_revision, __GIT_REPO_VERSION, sizeof(_git_revision) - 1);
258 for(DEVICE* device = first_device; device; device = device->next_device) {
259 device->initialize();
266 // delete all devices
267 for(DEVICE* device = first_device; device;) {
268 DEVICE *next_device = device->next_device;
271 device = next_device;
273 for(int drv = 0; drv < MAX_DRIVE; drv++) {
274 // if(config.drive_type) {
275 // fdc->set_drive_type(drv, DRIVE_TYPE_2D);
277 fdc->set_drive_type(drv, DRIVE_TYPE_2DD);
282 DEVICE* VM::get_device(int id)
284 for(DEVICE* device = first_device; device; device = device->next_device) {
285 if(device->this_device_id == id) {
292 // ----------------------------------------------------------------------------
293 // drive virtual machine
294 // ----------------------------------------------------------------------------
299 for(DEVICE* device = first_device; device; device = device->next_device) {
304 void VM::special_reset()
307 // for(DEVICE* device = first_device; device; device = device->next_device) {
308 // device->special_reset();
310 memory->special_reset();
312 #ifdef SUPPORT_16BIT_BOARD
325 // ----------------------------------------------------------------------------
327 // ----------------------------------------------------------------------------
330 DEVICE *VM::get_cpu(int index)
334 #ifdef SUPPORT_16BIT_BOARD
335 } else if(index == 1) {
343 // ----------------------------------------------------------------------------
345 // ----------------------------------------------------------------------------
347 void VM::draw_screen()
349 memory->draw_screen();
352 // ----------------------------------------------------------------------------
354 // ----------------------------------------------------------------------------
356 void VM::initialize_sound(int rate, int samples)
358 // init sound manager
359 event->initialize_sound(rate, samples);
362 pcm->initialize_sound(rate, 8000);
365 uint16_t* VM::create_sound(int* extra_frames)
367 return event->create_sound(extra_frames);
370 int VM::get_sound_buffer_ptr()
372 return event->get_sound_buffer_ptr();
375 #ifdef USE_SOUND_VOLUME
376 void VM::set_sound_device_volume(int ch, int decibel_l, int decibel_r)
379 pcm->set_volume(0, decibel_l, decibel_r);
381 drec->set_volume(0, decibel_l, decibel_r);
383 fdc->get_context_noise_seek()->set_volume(0, decibel_l, decibel_r);
384 fdc->get_context_noise_head_down()->set_volume(0, decibel_l, decibel_r);
385 fdc->get_context_noise_head_up()->set_volume(0, decibel_l, decibel_r);
387 drec->get_context_noise_play()->set_volume(0, decibel_l, decibel_r);
388 drec->get_context_noise_stop()->set_volume(0, decibel_l, decibel_r);
389 drec->get_context_noise_fast()->set_volume(0, decibel_l, decibel_r);
394 // ----------------------------------------------------------------------------
396 // ----------------------------------------------------------------------------
398 void VM::open_floppy_disk(int drv, const _TCHAR* file_path, int bank)
400 fdc->open_disk(drv, file_path, bank);
402 if(fdc->get_media_type(drv) == MEDIA_TYPE_2DD) {
403 if(fdc->get_drive_type(drv) == DRIVE_TYPE_2D) {
404 fdc->set_drive_type(drv, DRIVE_TYPE_2DD);
406 } else if(fdc->get_media_type(drv) == MEDIA_TYPE_2D) {
407 if(fdc->get_drive_type(drv) == DRIVE_TYPE_2DD) {
408 fdc->set_drive_type(drv, DRIVE_TYPE_2D);
413 void VM::close_floppy_disk(int drv)
415 fdc->close_disk(drv);
418 bool VM::is_floppy_disk_inserted(int drv)
420 return fdc->is_disk_inserted(drv);
423 void VM::is_floppy_disk_protected(int drv, bool value)
425 fdc->is_disk_protected(drv, value);
428 bool VM::is_floppy_disk_protected(int drv)
430 return fdc->is_disk_protected(drv);
433 uint32_t VM::is_floppy_disk_accessed()
435 return fdc->read_signal(0);
438 #ifdef SUPPORT_QUICK_DISK
439 void VM::open_quick_disk(int drv, const _TCHAR* file_path)
442 qd->open_disk(file_path);
446 void VM::close_quick_disk(int drv)
453 bool VM::is_quick_disk_inserted(int drv)
456 return qd->is_disk_inserted();
462 uint32_t VM::is_quick_disk_accessed()
464 return qd->read_signal(0);
468 void VM::play_tape(int drv, const _TCHAR* file_path)
470 if(check_file_extension(file_path, _T(".dat"))) {
471 memory->load_dat_image(file_path);
473 } else if(check_file_extension(file_path, _T(".mzt")) || check_file_extension(file_path, _T(".mzf"))) {
474 if(config.direct_load_mzt[0] && memory->load_mzt_image(file_path)) {
477 } else if(check_file_extension(file_path, _T(".mtw"))) {
478 memory->load_mzt_image(file_path);
480 bool value = drec->play_tape(file_path);
482 cmt->play_tape(value);
485 void VM::rec_tape(int drv, const _TCHAR* file_path)
487 bool value = drec->rec_tape(file_path);
489 cmt->rec_tape(value);
492 void VM::close_tape(int drv)
500 bool VM::is_tape_inserted(int drv)
502 return drec->is_tape_inserted();
505 bool VM::is_tape_playing(int drv)
507 return drec->is_tape_playing();
510 bool VM::is_tape_recording(int drv)
512 return drec->is_tape_recording();
515 int VM::get_tape_position(int drv)
517 return drec->get_tape_position();
520 const _TCHAR* VM::get_tape_message(int drv)
522 return drec->get_message();
525 void VM::push_play(int drv)
528 drec->set_remote(true);
531 void VM::push_stop(int drv)
533 drec->set_remote(false);
536 void VM::push_fast_forward(int drv)
539 drec->set_remote(true);
542 void VM::push_fast_rewind(int drv)
544 drec->set_ff_rew(-1);
545 drec->set_remote(true);
548 bool VM::is_frame_skippable()
550 return event->is_frame_skippable();
553 void VM::update_config()
555 for(DEVICE* device = first_device; device; device = device->next_device) {
556 device->update_config();
560 #define STATE_VERSION 4
562 #include "../../statesub.h"
563 #include "../../qt/gui/csp_logger.h"
564 extern CSP_Logger DLL_PREFIX_I *csp_logger;
566 void VM::decl_state(void)
568 state_entry = new csp_state_utils(STATE_VERSION, 0, (_TCHAR *)(_T("CSP::MZ80B_SERIES_HEAD")), csp_logger);
569 for(DEVICE* device = first_device; device; device = device->next_device) {
570 device->decl_state();
574 void VM::save_state(FILEIO* state_fio)
576 //state_fio->FputUint32(STATE_VERSION);
577 if(state_entry != NULL) {
578 state_entry->save_state(state_fio);
580 for(DEVICE* device = first_device; device; device = device->next_device) {
581 device->save_state(state_fio);
585 bool VM::load_state(FILEIO* state_fio)
587 //if(state_fio->FgetUint32() != STATE_VERSION) {
591 if(state_entry != NULL) {
592 mb = state_entry->load_state(state_fio);
594 if(!mb) return false;
595 for(DEVICE* device = first_device; device; device = device->next_device) {
596 if(!device->load_state(state_fio)) {