2 SHARP MZ-80K/C Emulator 'EmuZ-80K'
3 SHARP MZ-1200 Emulator 'EmuZ-1200'
5 Author : Takeda.Toshiya
8 SHARP MZ-80A Emulator 'EmuZ-80A'
16 #include "../../emu.h"
17 #include "../device.h"
20 #if defined(_MZ1200) || defined(_MZ80A)
23 #include "../datarec.h"
27 #include "../mz1p17.h"
29 #include "../pcm1bit.h"
30 #include "../prnfile.h"
34 #include "../debugger.h"
41 #if defined(SUPPORT_MZ80AIF)
44 #include "../mb8877.h"
46 #elif defined(SUPPORT_MZ80FIO)
49 #include "../t3444a.h"
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 #if defined(_MZ1200) || defined(_MZ80A)
66 and_int = new AND(this, emu);
67 and_int->set_device_name(_T("AND Gate (Interrupts)"));
69 drec = new DATAREC(this, emu);
70 drec->set_context_noise_play(new NOISE(this, emu));
71 drec->set_context_noise_stop(new NOISE(this, emu));
72 drec->set_context_noise_fast(new NOISE(this, emu));
73 ctc = new I8253(this, emu);
74 pio = new I8255(this, emu);
75 counter = new LS393(this, emu);
76 pcm = new PCM1BIT(this, emu);
77 cpu = new Z80(this, emu);
78 counter->set_device_name(_T("74LS393 Counter (CTC/Sound)"));
80 keyboard = new KEYBOARD(this, emu);
81 memory = new MEMORY(this, emu);
82 printer = new PRINTER(this, emu);
83 #if defined(SUPPORT_MZ80AIF)
84 io = new IO(this, emu);
85 fdc = new MB8877(this, emu); // mb8866
86 fdc->set_context_noise_seek(new NOISE(this, emu));
87 fdc->set_context_noise_head_down(new NOISE(this, emu));
88 fdc->set_context_noise_head_up(new NOISE(this, emu));
89 mz80aif = new MZ80AIF(this, emu);
90 #elif defined(SUPPORT_MZ80FIO)
91 io = new IO(this, emu);
92 fdc = new T3444A(this, emu); // t3444m
93 fdc->set_context_noise_seek(new NOISE(this, emu));
94 fdc->set_context_noise_head_down(new NOISE(this, emu));
95 fdc->set_context_noise_head_up(new NOISE(this, emu));
96 mz80fio = new MZ80FIO(this, emu);
100 event->set_context_cpu(cpu);
101 event->set_context_sound(pcm);
102 event->set_context_sound(drec);
103 #if defined(SUPPORT_MZ80AIF) || defined(SUPPORT_MZ80FIO)
104 event->set_context_sound(fdc->get_context_noise_seek());
105 event->set_context_sound(fdc->get_context_noise_head_down());
106 event->set_context_sound(fdc->get_context_noise_head_up());
108 event->set_context_sound(drec->get_context_noise_play());
109 event->set_context_sound(drec->get_context_noise_stop());
110 event->set_context_sound(drec->get_context_noise_fast());
112 #if defined(_MZ1200) || defined(_MZ80A)
113 and_int->set_context_out(cpu, SIG_CPU_IRQ, 1);
114 and_int->set_mask(SIG_AND_BIT_0 | SIG_AND_BIT_1);
116 drec->set_context_ear(pio, SIG_I8255_PORT_C, 0x20);
117 drec->set_context_remote(pio, SIG_I8255_PORT_C, 0x10);
118 ctc->set_context_ch0(counter, SIG_LS393_CLK, 1);
119 ctc->set_context_ch1(ctc, SIG_I8253_CLOCK_2, 1);
120 #if defined(_MZ1200) || defined(_MZ80A)
121 ctc->set_context_ch2(and_int, SIG_AND_BIT_0, 1);
123 ctc->set_context_ch2(cpu, SIG_CPU_IRQ, 1);
125 ctc->set_constant_clock(0, 2000000);
126 ctc->set_constant_clock(1, 31250);
127 pio->set_context_port_a(keyboard, SIG_KEYBOARD_COLUMN, 0x0f, 0);
128 pio->set_context_port_c(memory, SIG_MEMORY_VGATE, 1, 0);
129 pio->set_context_port_c(drec, SIG_DATAREC_MIC, 2, 0);
130 #if defined(_MZ1200) || defined(_MZ80A)
131 pio->set_context_port_c(and_int, SIG_AND_BIT_1, 4, 0);
133 pio->set_context_port_c(drec, SIG_DATAREC_TRIG, 8, 0);
134 counter->set_context_1qa(pcm, SIG_PCM1BIT_SIGNAL, 1);
135 // Sound:: Force realtime rendering. This is temporally fix. 20161024 K.O
136 //pcm->set_realtime_render(true);
138 keyboard->set_context_pio(pio);
139 memory->set_context_ctc(ctc);
140 memory->set_context_pio(pio);
142 #if defined(SUPPORT_MZ80AIF)
143 fdc->set_context_irq(memory, SIG_MEMORY_FDC_IRQ, 1);
144 fdc->set_context_drq(memory, SIG_MEMORY_FDC_DRQ, 1);
145 mz80aif->set_context_fdc(fdc);
146 #elif defined(SUPPORT_MZ80FIO)
147 mz80fio->set_context_fdc(fdc);
150 if(config.printer_type == 0) {
151 printer->set_context_prn(new PRNFILE(this, emu));
152 } else if(config.printer_type == 1) {
153 MZ1P17 *mz1p17 = new MZ1P17(this, emu);
154 mz1p17->mode = MZ1P17_MODE_MZ80P4;
155 printer->set_context_prn(mz1p17);
157 printer->set_context_prn(dummy);
161 cpu->set_context_mem(memory);
162 #if defined(SUPPORT_MZ80AIF) || defined(SUPPORT_MZ80FIO)
163 cpu->set_context_io(io);
165 cpu->set_context_io(dummy);
167 cpu->set_context_intr(dummy);
169 cpu->set_context_debugger(new DEBUGGER(this, emu));
173 #if defined(SUPPORT_MZ80AIF)
174 io->set_iomap_range_rw(0xd8, 0xdb, fdc);
175 io->set_iomap_range_w(0xdc, 0xdd, mz80aif);
176 #elif defined(SUPPORT_MZ80FIO)
177 io->set_iomap_range_rw(0xf8, 0xfb, mz80fio);
179 io->set_iomap_range_rw(0xfe, 0xff, printer);
181 // initialize all devices
182 for(DEVICE* device = first_device; device; device = device->next_device) {
183 device->initialize();
190 // delete all devices
191 for(DEVICE* device = first_device; device;) {
192 DEVICE *next_device = device->next_device;
195 device = next_device;
199 DEVICE* VM::get_device(int id)
201 for(DEVICE* device = first_device; device; device = device->next_device) {
202 if(device->this_device_id == id) {
209 // ----------------------------------------------------------------------------
210 // drive virtual machine
211 // ----------------------------------------------------------------------------
216 for(DEVICE* device = first_device; device; device = device->next_device) {
219 #if defined(SUPPORT_MZ80AIF)
220 for(int i = 0; i < MAX_DRIVE; i++) {
221 if(config.drive_type) {
222 fdc->set_drive_type(i, DRIVE_TYPE_2DD);
224 fdc->set_drive_type(i, DRIVE_TYPE_2D);
227 #elif defined(SUPPORT_MZ80FIO)
228 for(int i = 0; i < MAX_DRIVE; i++) {
229 fdc->set_drive_type(i, DRIVE_TYPE_2D);
230 // fdc->set_drive_mfm(i, false);
233 #if defined(_MZ1200) || defined(_MZ80A)
234 and_int->write_signal(SIG_AND_BIT_0, 0, 1); // CLOCK = L
235 and_int->write_signal(SIG_AND_BIT_1, 1, 1); // INTMASK = H
244 // ----------------------------------------------------------------------------
246 // ----------------------------------------------------------------------------
249 DEVICE *VM::get_cpu(int index)
258 // ----------------------------------------------------------------------------
260 // ----------------------------------------------------------------------------
262 void VM::draw_screen()
264 memory->draw_screen();
267 // ----------------------------------------------------------------------------
269 // ----------------------------------------------------------------------------
271 void VM::initialize_sound(int rate, int samples)
273 // init sound manager
274 event->initialize_sound(rate, samples);
277 pcm->initialize_sound(rate, 8000);
280 uint16_t* VM::create_sound(int* extra_frames)
282 return event->create_sound(extra_frames);
285 int VM::get_sound_buffer_ptr()
287 return event->get_sound_buffer_ptr();
290 #ifdef USE_SOUND_VOLUME
291 void VM::set_sound_device_volume(int ch, int decibel_l, int decibel_r)
294 pcm->set_volume(0, decibel_l, decibel_r);
295 } else if(ch-- == 0) {
296 drec->set_volume(0, decibel_l, decibel_r);
297 #if defined(SUPPORT_MZ80AIF) || defined(SUPPORT_MZ80FIO)
298 } else if(ch-- == 0) {
299 fdc->get_context_noise_seek()->set_volume(0, decibel_l, decibel_r);
300 fdc->get_context_noise_head_down()->set_volume(0, decibel_l, decibel_r);
301 fdc->get_context_noise_head_up()->set_volume(0, decibel_l, decibel_r);
303 } else if(ch-- == 0) {
304 drec->get_context_noise_play()->set_volume(0, decibel_l, decibel_r);
305 drec->get_context_noise_stop()->set_volume(0, decibel_l, decibel_r);
306 drec->get_context_noise_fast()->set_volume(0, decibel_l, decibel_r);
311 // ----------------------------------------------------------------------------
313 // ----------------------------------------------------------------------------
315 void VM::key_down(int code, bool repeat)
318 keyboard->key_down(code);
322 void VM::key_up(int code)
324 // keyboard->key_up(code);
327 bool VM::get_caps_locked()
329 return keyboard->get_caps_locked();
332 bool VM::get_kana_locked()
334 return keyboard->get_kana_locked();
337 // ----------------------------------------------------------------------------
339 // ----------------------------------------------------------------------------
341 #if defined(SUPPORT_MZ80AIF) || defined(SUPPORT_MZ80FIO)
342 void VM::open_floppy_disk(int drv, const _TCHAR* file_path, int bank)
344 fdc->open_disk(drv, file_path, bank);
347 void VM::close_floppy_disk(int drv)
349 fdc->close_disk(drv);
352 bool VM::is_floppy_disk_inserted(int drv)
354 return fdc->is_disk_inserted(drv);
357 void VM::is_floppy_disk_protected(int drv, bool value)
359 fdc->is_disk_protected(drv, value);
362 bool VM::is_floppy_disk_protected(int drv)
364 return fdc->is_disk_protected(drv);
367 uint32_t VM::is_floppy_disk_accessed()
369 return fdc->read_signal(0);
374 void VM::play_tape(int drv, const _TCHAR* file_path)
376 drec->play_tape(file_path);
377 // drec->set_remote(true);
380 void VM::rec_tape(int drv, const _TCHAR* file_path)
382 drec->rec_tape(file_path);
383 // drec->set_remote(true);
386 void VM::close_tape(int drv)
391 // drec->set_remote(false);
394 bool VM::is_tape_inserted(int drv)
396 return drec->is_tape_inserted();
399 bool VM::is_tape_playing(int drv)
401 return drec->is_tape_playing();
404 bool VM::is_tape_recording(int drv)
406 return drec->is_tape_recording();
409 int VM::get_tape_position(int drv)
411 return drec->get_tape_position();
414 const _TCHAR* VM::get_tape_message(int drv)
416 return drec->get_message();
419 void VM::push_play(int drv)
422 drec->set_remote(true);
425 void VM::push_stop(int drv)
427 drec->set_remote(false);
430 void VM::push_fast_forward(int drv)
433 drec->set_remote(true);
436 void VM::push_fast_rewind(int drv)
438 drec->set_ff_rew(-1);
439 drec->set_remote(true);
442 bool VM::is_frame_skippable()
444 return event->is_frame_skippable();
447 void VM::update_config()
449 for(DEVICE* device = first_device; device; device = device->next_device) {
450 device->update_config();
454 #define STATE_VERSION 7
456 #include "../../statesub.h"
458 void VM::decl_state(void)
460 state_entry = new csp_state_utils(STATE_VERSION, 0, (_TCHAR *)(_T("CSP::MZ80_SERIES_HEAD")));
462 for(DEVICE* device = first_device; device; device = device->next_device) {
463 device->decl_state();
467 void VM::save_state(FILEIO* state_fio)
469 //state_fio->FputUint32(STATE_VERSION);
470 if(state_entry != NULL) {
471 state_entry->save_state(state_fio);
473 for(DEVICE* device = first_device; device; device = device->next_device) {
474 device->save_state(state_fio);
478 bool VM::load_state(FILEIO* state_fio)
481 if(state_entry != NULL) {
482 mb = state_entry->save_state(state_fio);
484 if(!mb) return false;
485 //if(state_fio->FgetUint32() != STATE_VERSION) {
488 for(DEVICE* device = first_device; device; device = device->next_device) {
489 if(!device->load_state(state_fio)) {