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) : emu(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 for(DEVICE* device = first_device; device; device = device->next_device) {
256 device->initialize();
263 // delete all devices
264 for(DEVICE* device = first_device; device;) {
265 DEVICE *next_device = device->next_device;
268 device = next_device;
272 DEVICE* VM::get_device(int id)
274 for(DEVICE* device = first_device; device; device = device->next_device) {
275 if(device->this_device_id == id) {
282 // ----------------------------------------------------------------------------
283 // drive virtual machine
284 // ----------------------------------------------------------------------------
289 for(DEVICE* device = first_device; device; device = device->next_device) {
292 for(int i = 0; i < MAX_DRIVE; i++) {
293 if(config.drive_type) {
294 fdc->set_drive_type(i, DRIVE_TYPE_2DD);
296 fdc->set_drive_type(i, DRIVE_TYPE_2D);
301 void VM::special_reset()
304 // for(DEVICE* device = first_device; device; device = device->next_device) {
305 // device->special_reset();
307 memory->special_reset();
309 #ifdef SUPPORT_16BIT_BOARD
322 // ----------------------------------------------------------------------------
324 // ----------------------------------------------------------------------------
327 DEVICE *VM::get_cpu(int index)
331 #ifdef SUPPORT_16BIT_BOARD
332 } else if(index == 1) {
340 // ----------------------------------------------------------------------------
342 // ----------------------------------------------------------------------------
344 void VM::draw_screen()
346 memory->draw_screen();
349 // ----------------------------------------------------------------------------
351 // ----------------------------------------------------------------------------
353 void VM::initialize_sound(int rate, int samples)
355 // init sound manager
356 event->initialize_sound(rate, samples);
359 pcm->initialize_sound(rate, 8000);
362 uint16_t* VM::create_sound(int* extra_frames)
364 return event->create_sound(extra_frames);
367 int VM::get_sound_buffer_ptr()
369 return event->get_sound_buffer_ptr();
372 #ifdef USE_SOUND_VOLUME
373 void VM::set_sound_device_volume(int ch, int decibel_l, int decibel_r)
376 pcm->set_volume(0, decibel_l, decibel_r);
378 drec->set_volume(0, decibel_l, decibel_r);
380 fdc->get_context_noise_seek()->set_volume(0, decibel_l, decibel_r);
381 fdc->get_context_noise_head_down()->set_volume(0, decibel_l, decibel_r);
382 fdc->get_context_noise_head_up()->set_volume(0, decibel_l, decibel_r);
384 drec->get_context_noise_play()->set_volume(0, decibel_l, decibel_r);
385 drec->get_context_noise_stop()->set_volume(0, decibel_l, decibel_r);
386 drec->get_context_noise_fast()->set_volume(0, decibel_l, decibel_r);
391 // ----------------------------------------------------------------------------
393 // ----------------------------------------------------------------------------
395 void VM::open_floppy_disk(int drv, const _TCHAR* file_path, int bank)
397 fdc->open_disk(drv, file_path, bank);
400 void VM::close_floppy_disk(int drv)
402 fdc->close_disk(drv);
405 bool VM::is_floppy_disk_inserted(int drv)
407 return fdc->is_disk_inserted(drv);
410 void VM::is_floppy_disk_protected(int drv, bool value)
412 fdc->is_disk_protected(drv, value);
415 bool VM::is_floppy_disk_protected(int drv)
417 return fdc->is_disk_protected(drv);
420 uint32_t VM::is_floppy_disk_accessed()
422 return fdc->read_signal(0);
425 #ifdef SUPPORT_QUICK_DISK
426 void VM::open_quick_disk(int drv, const _TCHAR* file_path)
429 qd->open_disk(file_path);
433 void VM::close_quick_disk(int drv)
440 bool VM::is_quick_disk_inserted(int drv)
443 return qd->is_disk_inserted();
449 uint32_t VM::is_quick_disk_accessed()
451 return qd->read_signal(0);
455 void VM::play_tape(int drv, const _TCHAR* file_path)
457 if(check_file_extension(file_path, _T(".dat"))) {
458 memory->load_dat_image(file_path);
460 } else if(check_file_extension(file_path, _T(".mzt")) || check_file_extension(file_path, _T(".mzf"))) {
461 if(config.direct_load_mzt[0] && memory->load_mzt_image(file_path)) {
464 } else if(check_file_extension(file_path, _T(".mtw"))) {
465 memory->load_mzt_image(file_path);
467 bool value = drec->play_tape(file_path);
469 cmt->play_tape(value);
472 void VM::rec_tape(int drv, const _TCHAR* file_path)
474 bool value = drec->rec_tape(file_path);
476 cmt->rec_tape(value);
479 void VM::close_tape(int drv)
487 bool VM::is_tape_inserted(int drv)
489 return drec->is_tape_inserted();
492 bool VM::is_tape_playing(int drv)
494 return drec->is_tape_playing();
497 bool VM::is_tape_recording(int drv)
499 return drec->is_tape_recording();
502 int VM::get_tape_position(int drv)
504 return drec->get_tape_position();
507 const _TCHAR* VM::get_tape_message(int drv)
509 return drec->get_message();
512 void VM::push_play(int drv)
515 drec->set_remote(true);
518 void VM::push_stop(int drv)
520 drec->set_remote(false);
523 void VM::push_fast_forward(int drv)
526 drec->set_remote(true);
529 void VM::push_fast_rewind(int drv)
531 drec->set_ff_rew(-1);
532 drec->set_remote(true);
535 bool VM::is_frame_skippable()
537 return event->is_frame_skippable();
540 void VM::update_config()
542 for(DEVICE* device = first_device; device; device = device->next_device) {
543 device->update_config();
547 #define STATE_VERSION 4
549 #include "../../statesub.h"
550 #include "../../qt/gui/csp_logger.h"
551 extern CSP_Logger DLL_PREFIX_I *csp_logger;
553 void VM::decl_state(void)
555 state_entry = new csp_state_utils(STATE_VERSION, 0, (_TCHAR *)(_T("CSP::MZ80B_SERIES_HEAD")), csp_logger);
556 for(DEVICE* device = first_device; device; device = device->next_device) {
557 device->decl_state();
561 void VM::save_state(FILEIO* state_fio)
563 //state_fio->FputUint32(STATE_VERSION);
564 if(state_entry != NULL) {
565 state_entry->save_state(state_fio);
567 for(DEVICE* device = first_device; device; device = device->next_device) {
568 device->save_state(state_fio);
572 bool VM::load_state(FILEIO* state_fio)
574 //if(state_fio->FgetUint32() != STATE_VERSION) {
578 if(state_entry != NULL) {
579 mb = state_entry->load_state(state_fio);
581 if(!mb) return false;
582 for(DEVICE* device = first_device; device; device = device->next_device) {
583 if(!device->load_state(state_fio)) {