OSDN Git Service

[VM][WIP] Use namespace to devices per VMs.
[csp-qt/common_source_project-fm7.git] / source / src / vm / mz700 / mz700.cpp
index 9199bef..2015793 100644 (file)
-/*\r
-       SHARP MZ-700 Emulator 'EmuZ-700'\r
-       SHARP MZ-800 Emulator 'EmuZ-800'\r
-       SHARP MZ-1500 Emulator 'EmuZ-1500'\r
-\r
-       Author : Takeda.Toshiya\r
-       Date   : 2008.06.05 -\r
-\r
-       [ virtual machine ]\r
-*/\r
-\r
-#include "mz700.h"\r
-#include "../../emu.h"\r
-#include "../device.h"\r
-#include "../event.h"\r
-\r
-#include "../and.h"\r
-#include "../datarec.h"\r
-#include "../i8253.h"\r
-#include "../i8255.h"\r
-#include "../io.h"\r
-#include "../pcm1bit.h"\r
-#include "../z80.h"\r
-\r
-#ifdef USE_DEBUGGER\r
-#include "../debugger.h"\r
-#endif\r
-\r
-//#include "cmos.h"\r
-#include "emm.h"\r
-#include "keyboard.h"\r
-#include "memory.h"\r
-#include "ramfile.h"\r
-\r
-#if defined(_MZ800) || defined(_MZ1500)\r
-#include "../disk.h"\r
-#include "../mb8877.h"\r
-#if defined(_MZ800)\r
-#include "../not.h"\r
-#endif\r
-#include "../sn76489an.h"\r
-#include "../z80pio.h"\r
-#include "../z80sio.h"\r
-#include "floppy.h"\r
-#if defined(_MZ1500)\r
-#include "psg.h"\r
-#endif\r
-#include "quickdisk.h"\r
-#endif\r
-\r
-// ----------------------------------------------------------------------------\r
-// initialize\r
-// ----------------------------------------------------------------------------\r
-\r
-VM::VM(EMU* parent_emu) : emu(parent_emu)\r
-{\r
-#if defined(_MZ800)\r
-       boot_mode = config.boot_mode;\r
-#endif\r
-       \r
-       // create devices\r
-       first_device = last_device = NULL;\r
-       dummy = new DEVICE(this, emu);  // must be 1st device\r
-       event = new EVENT(this, emu);   // must be 2nd device\r
-       \r
-       and_int = new AND(this, emu);\r
-       drec = new DATAREC(this, emu);\r
-       pit = new I8253(this, emu);\r
-       pio = new I8255(this, emu);\r
-       io = new IO(this, emu);\r
-       pcm = new PCM1BIT(this, emu);\r
-       cpu = new Z80(this, emu);\r
-       \r
-//     cmos = new CMOS(this, emu);\r
-       emm = new EMM(this, emu);\r
-       keyboard = new KEYBOARD(this, emu);\r
-       memory = new MEMORY(this, emu);\r
-       ramfile = new RAMFILE(this, emu);\r
-       \r
-#if defined(_MZ800) || defined(_MZ1500)\r
-       and_snd = new AND(this, emu);\r
-       fdc = new MB8877(this, emu);    // mb8876\r
-#if defined(_MZ800)\r
-       not_pit = new NOT(this, emu);\r
-       psg = new SN76489AN(this, emu);\r
-#elif defined(_MZ1500)\r
-       psg_l = new SN76489AN(this, emu);\r
-       psg_r = new SN76489AN(this, emu);\r
-#endif\r
-       pio_int = new Z80PIO(this, emu);\r
-       sio_rs = new Z80SIO(this, emu);\r
-       sio_qd = new Z80SIO(this, emu);\r
-       \r
-       floppy = new FLOPPY(this, emu);\r
-#if defined(_MZ1500)\r
-       psg = new PSG(this, emu);\r
-#endif\r
-       qd = new QUICKDISK(this, emu);\r
-#endif\r
-       \r
-       // set contexts\r
-       event->set_context_cpu(cpu);\r
-       event->set_context_sound(pcm);\r
-#if defined(_MZ800)\r
-       event->set_context_sound(psg);\r
-#elif defined(_MZ1500)\r
-       event->set_context_sound(psg_l);\r
-       event->set_context_sound(psg_r);\r
-#endif\r
-       \r
-       // VRAM/PCG wait\r
-       memory->set_context_cpu(cpu);\r
-       \r
-       // memory mapped I/O\r
-       memory->set_context_pio(pio);\r
-       memory->set_context_pit(pit);\r
-       \r
-#if defined(_MZ1500)\r
-       // psg mixer\r
-       psg->set_context_psg_l(psg_l);\r
-       psg->set_context_psg_r(psg_r);\r
-#endif\r
-       \r
-#if defined(_MZ800)\r
-       // 8253:CLK#0 <- 1.10MHz\r
-       pit->set_constant_clock(0, 1100000);\r
-#else\r
-       // 8253:CLK#0 <- 895KHz\r
-       pit->set_constant_clock(0, CPU_CLOCKS / 4);\r
-#endif\r
-       \r
-#if defined(_MZ800) || defined(_MZ1500)\r
-       // 8253:OUT#0 AND 8255:PC0 -> SPEAKER\r
-       pit->set_context_ch0(and_snd, SIG_AND_BIT_0, 1);\r
-       pio->set_context_port_c(and_snd, SIG_AND_BIT_1, 1, 0);\r
-       and_snd->set_context_out(pcm, SIG_PCM1BIT_SIGNAL, 1);\r
-       and_snd->set_mask(SIG_AND_BIT_0 | SIG_AND_BIT_1);\r
-#else\r
-       // 8253:OUT#0 -> SPEAKER\r
-       pit->set_context_ch0(pcm, SIG_PCM1BIT_SIGNAL, 1);\r
-#endif\r
-#if defined(_MZ800)\r
-       // 8253:OUT#0 -> NOT -> Z80PIO:PA4\r
-       pit->set_context_ch0(not_pit, SIG_NOT_INPUT, 1);\r
-       not_pit->set_context_out(pio_int, SIG_Z80PIO_PORT_A, 0x10);\r
-#elif defined(_MZ1500)\r
-       // 8253:OUT#0 -> Z80PIO:PA4\r
-       pit->set_context_ch0(pio_int, SIG_Z80PIO_PORT_A, 0x10);\r
-#endif\r
-       \r
-       // 8253:CLK#1 <- 15.7KHz\r
-       pit->set_constant_clock(1, CPU_CLOCKS / 228);\r
-       \r
-       // 8253:OUT#1 -> 8253:CLK#2\r
-       pit->set_context_ch1(pit, SIG_I8253_CLOCK_2, 1);\r
-       \r
-       // 8253:OUT#2 (N)AND 8255:PC2 -> Z80:INT\r
-       pit->set_context_ch2(and_int, SIG_AND_BIT_0, 1);\r
-       pio->set_context_port_c(and_int, SIG_AND_BIT_1, 4, 0);\r
-       and_int->set_context_out(cpu, SIG_CPU_IRQ, 1);\r
-       and_int->set_mask(SIG_AND_BIT_0 | SIG_AND_BIT_1);\r
-       \r
-#if defined(_MZ1500)\r
-       // 8253:OUT#2 -> Z80PIO:PA5\r
-       pit->set_context_ch2(pio_int, SIG_Z80PIO_PORT_A, 0x20);\r
-#endif\r
-       \r
-       // 8255:PA0-3 -> KEYBOARD:STROBE\r
-       pio->set_context_port_a(keyboard, SIG_KEYBOARD_COLUMN, 0x0f, 0);\r
-#if defined(_MZ800)\r
-       // 8255:PA4 -> JOYSTICK #1\r
-       // 8255:PA5 -> JOYSTICK #2\r
-#endif\r
-       // 8255:PA7 -> 556 RESET\r
-       \r
-       // 8255:PB0-7 <- KEYBOARD:DATA\r
-       keyboard->set_context_pio(pio);\r
-       \r
-       // 8255:PC0 -> AND -> SPEAKER\r
-       // 8255:PC1 -> DATA RECORDER:WRITE DATA\r
-       pio->set_context_port_c(drec, SIG_DATAREC_OUT, 0x02, 0);\r
-       // 8255:PC2 -> (N)AND -> Z80:INT\r
-       // 8255:PC3 -> DATA RECORDER:MOTOR ON/OFF\r
-       pio->set_context_port_c(drec, SIG_DATAREC_TRIG, 0x08, 0);\r
-       // 8255:PC4 <- DATA RECORDER:MOTOR REMOTE\r
-       drec->set_context_remote(pio, SIG_I8255_PORT_C, 0x10);\r
-       // 8255:PC5 <- DATA RECORDER:READ DATA\r
-       drec->set_context_out(pio, SIG_I8255_PORT_C, 0x20);\r
-       // 8255:PC6 <- MEMORY:556 OUT (1.5KHz)\r
-       // 8255:PC7 <- MEMORY:VBLANK\r
-       \r
-#if defined(_MZ800) || defined(_MZ1500)\r
-       // Z80PIO:PA0 <- PRINTER:RDA\r
-       // Z80PIO:PA1 <- PRINTER:STA\r
-       // Z80PIO:PA2 <- GND\r
-       // Z80PIO:PA3 <- GND\r
-#if defined(_MZ800)\r
-       // Z80PIO:PA4 <- NOT <- 8253:OUT#0\r
-       // Z80PIO:PA5 <- HBLANK\r
-       memory->set_context_pio_int(pio_int);\r
-#elif defined(_MZ1500)\r
-       // Z80PIO:PA4 <- 8253:OUT#0\r
-       // Z80PIO:PA5 <- 8253:OUT#2\r
-#endif\r
-       // Z80PIO:PA6 -> PRINTER:IRT\r
-       // Z80PIO:PA7 -> PRINTER:RDP\r
-#endif\r
-       \r
-#if defined(_MZ800) || defined(_MZ1500)\r
-       // Z80SIO:RTSA -> QD:WRGA\r
-       sio_qd->set_context_rts0(qd, QUICKDISK_SIO_RTSA, 1);\r
-       // Z80SIO:DTRB -> QD:MTON\r
-       sio_qd->set_context_dtr1(qd, QUICKDISK_SIO_DTRB, 1);\r
-       // Z80SIO:SENDA -> QD:RECV\r
-       sio_qd->set_context_sync0(qd, QUICKDISK_SIO_SYNC, 1);\r
-       sio_qd->set_context_rxdone0(qd, QUICKDISK_SIO_RXDONE, 1);\r
-       sio_qd->set_context_send0(qd, QUICKDISK_SIO_DATA);\r
-       sio_qd->set_context_break0(qd, QUICKDISK_SIO_BREAK, 1);\r
-       // Z80SIO:CTSA <- QD:PROTECT\r
-       // Z80SIO:DCDA <- QD:INSERT\r
-       // Z80SIO:DCDB <- QD:HOE\r
-       qd->set_context_sio(sio_qd);\r
-       \r
-       // floppy drives\r
-       floppy->set_context_cpu(cpu);\r
-       floppy->set_context_fdc(fdc);\r
-       fdc->set_context_drq(floppy, SIG_FLOPPY_DRQ, 1);\r
-#endif\r
-       \r
-       // cpu bus\r
-       cpu->set_context_mem(memory);\r
-       cpu->set_context_io(io);\r
-#if defined(_MZ800) || defined(_MZ1500)\r
-       cpu->set_context_intr(pio_int);\r
-       // z80 family daisy chain\r
-       // 0=8253:OUT2\r
-       pio_int->set_context_intr(cpu, 1);\r
-       pio_int->set_context_child(sio_rs);\r
-       sio_rs->set_context_intr(cpu, 2);\r
-       sio_rs->set_context_child(sio_qd);\r
-       sio_qd->set_context_intr(cpu, 3);\r
-#else\r
-       cpu->set_context_intr(dummy);\r
-#endif\r
-#ifdef USE_DEBUGGER\r
-       cpu->set_context_debugger(new DEBUGGER(this, emu));\r
-#endif\r
-       \r
-       // emm\r
-       io->set_iomap_range_rw(0x00, 0x03, emm);\r
-       // ramfile\r
-       io->set_iomap_range_rw(0xea, 0xeb, ramfile);\r
-       // cmos\r
-//     io->set_iomap_range_rw(0xf8, 0xfa, cmos);\r
-       \r
-#if defined(_MZ800)\r
-       // 8255/8253\r
-       io->set_iomap_range_rw(0xd0, 0xd3, pio);\r
-       io->set_iomap_range_rw(0xd4, 0xd7, pit);\r
-#endif\r
-       \r
-#if defined(_MZ800) || defined(_MZ1500)\r
-       // floppy drives\r
-       io->set_iomap_range_rw(0xd8, 0xdb, fdc);\r
-       io->set_iomap_range_w(0xdc, 0xdf, floppy);\r
-#endif\r
-       \r
-       // memory mapper\r
-#if defined(_MZ800)\r
-       io->set_iomap_range_r(0xe0, 0xe1, memory);\r
-       io->set_iomap_range_w(0xe0, 0xe6, memory);\r
-#elif defined(_MZ1500)\r
-       io->set_iomap_range_w(0xe0, 0xe6, memory);\r
-#else\r
-       io->set_iomap_range_w(0xe0, 0xe4, memory);\r
-#endif\r
-       \r
-#if defined(_MZ800)\r
-       // crtc\r
-       io->set_iomap_range_w(0xcc, 0xcf, memory);\r
-       io->set_iomap_single_r(0xce, memory);\r
-       // palette\r
-       io->set_iomap_single_w(0xf0, memory);\r
-#elif defined(_MZ1500)\r
-       // palette\r
-       io->set_iomap_range_w(0xf0, 0xf1, memory);\r
-#endif\r
-       \r
-#if defined(_MZ800)\r
-       // joystick\r
-//     io->set_iomap_range_r(0xf0, 0xf1, joystick);\r
-#endif\r
-       \r
-       // psg\r
-#if defined(_MZ800)\r
-       io->set_iomap_single_w(0xf2, psg);\r
-#elif defined(_MZ1500)\r
-       io->set_iomap_single_w(0xe9, psg);\r
-       io->set_iomap_single_w(0xf2, psg_l);\r
-       io->set_iomap_single_w(0xf3, psg_r);\r
-#endif\r
-       \r
-#if defined(_MZ800) || defined(_MZ1500)\r
-       // z80pio/sio\r
-       // z80pio and z80sio*2\r
-       static const int z80_sio_addr[4] = {0, 2, 1, 3};\r
-       static const int z80_pio_addr[4] = {1, 3, 0, 2};\r
-       for(int i = 0; i < 4; i++) {\r
-               io->set_iomap_alias_rw(0xb0 + i, sio_rs, z80_sio_addr[i]);\r
-               io->set_iomap_alias_rw(0xf4 + i, sio_qd, z80_sio_addr[i]);\r
-               io->set_iomap_alias_rw(0xfc + i, pio_int, z80_pio_addr[i]);\r
-       }\r
-#else\r
-       // printer\r
-       io->set_iovalue_single_r(0xfe, 0xc0);\r
-#endif\r
-\r
-       // initialize all devices\r
-       for(DEVICE* device = first_device; device; device = device->next_device) {\r
-               device->initialize();\r
-       }\r
-#if defined(_MZ800) || defined(_MZ1500)\r
-       for(int i = 0; i < MAX_DRIVE; i++) {\r
-               fdc->set_drive_type(i, DRIVE_TYPE_2DD);\r
-       }\r
-#endif\r
-}\r
-\r
-VM::~VM()\r
-{\r
-       // delete all devices\r
-       for(DEVICE* device = first_device; device;) {\r
-               DEVICE *next_device = device->next_device;\r
-               device->release();\r
-               delete device;\r
-               device = next_device;\r
-       }\r
-}\r
-\r
-DEVICE* VM::get_device(int id)\r
-{\r
-       for(DEVICE* device = first_device; device; device = device->next_device) {\r
-               if(device->this_device_id == id) {\r
-                       return device;\r
-               }\r
-       }\r
-       return NULL;\r
-}\r
-\r
-// ----------------------------------------------------------------------------\r
-// drive virtual machine\r
-// ----------------------------------------------------------------------------\r
-\r
-void VM::reset()\r
-{\r
-       // reset all devices\r
-       for(DEVICE* device = first_device; device; device = device->next_device) {\r
-               device->reset();\r
-       }\r
-       and_int->write_signal(SIG_AND_BIT_0, 0, 1);     // CLOCK = L\r
-       and_int->write_signal(SIG_AND_BIT_1, 1, 1);     // INTMASK = H\r
-#if defined(_MZ800) || defined(_MZ1500)\r
-       and_snd->write_signal(SIG_AND_BIT_1, 1, 1);     // SNDMASK = H\r
-#endif\r
-}\r
-\r
-void VM::run()\r
-{\r
-       event->drive();\r
-}\r
-\r
-// ----------------------------------------------------------------------------\r
-// debugger\r
-// ----------------------------------------------------------------------------\r
-\r
-#ifdef USE_DEBUGGER\r
-DEVICE *VM::get_cpu(int index)\r
-{\r
-       if(index == 0) {\r
-               return cpu;\r
-       }\r
-       return NULL;\r
-}\r
-#endif\r
-\r
-// ----------------------------------------------------------------------------\r
-// draw screen\r
-// ----------------------------------------------------------------------------\r
-\r
-void VM::draw_screen()\r
-{\r
-       memory->draw_screen();\r
-}\r
-\r
-#if defined(_MZ800) || defined(_MZ1500)\r
-int VM::access_lamp()\r
-{\r
-       uint32 status = fdc->read_signal(0) | qd->read_signal(0);\r
-       return (status & (1 | 4)) ? 1 : (status & (2 | 8)) ? 2 : 0;\r
-}\r
-#endif\r
-\r
-// ----------------------------------------------------------------------------\r
-// soud manager\r
-// ----------------------------------------------------------------------------\r
-\r
-void VM::initialize_sound(int rate, int samples)\r
-{\r
-       // init sound manager\r
-       event->initialize_sound(rate, samples);\r
-       \r
-       // init sound gen\r
-       pcm->init(rate, 8000);\r
-#if defined(_MZ800)\r
-       psg->init(rate, 3579545, 8000);\r
-#elif defined(_MZ1500)\r
-       psg_l->init(rate, 3579545, 8000);\r
-       psg_r->init(rate, 3579545, 8000);\r
-#endif\r
-}\r
-\r
-uint16* VM::create_sound(int* extra_frames)\r
-{\r
-       return event->create_sound(extra_frames);\r
-}\r
-\r
-int VM::sound_buffer_ptr()\r
-{\r
-       return event->sound_buffer_ptr();\r
-}\r
-\r
-// ----------------------------------------------------------------------------\r
-// user interface\r
-// ----------------------------------------------------------------------------\r
-\r
-void VM::play_tape(_TCHAR* file_path)\r
-{\r
-       drec->play_tape(file_path);\r
-       drec->write_signal(SIG_DATAREC_REMOTE, 1, 1);\r
-}\r
-\r
-void VM::rec_tape(_TCHAR* file_path)\r
-{\r
-       drec->rec_tape(file_path);\r
-       drec->write_signal(SIG_DATAREC_REMOTE, 1, 1);\r
-}\r
-\r
-void VM::close_tape()\r
-{\r
-       drec->close_tape();\r
-       drec->write_signal(SIG_DATAREC_REMOTE, 0, 0);\r
-}\r
-\r
-bool VM::tape_inserted()\r
-{\r
-       return drec->tape_inserted();\r
-}\r
-\r
-void VM::push_play()\r
-{\r
-       drec->write_signal(SIG_DATAREC_REMOTE, 1, 1);\r
-}\r
-\r
-void VM::push_stop()\r
-{\r
-       drec->write_signal(SIG_DATAREC_REMOTE, 0, 0);\r
-}\r
-\r
-#if defined(_MZ800) || defined(_MZ1500)\r
-void VM::open_quickdisk(int drv, _TCHAR* file_path)\r
-{\r
-       if(drv == 0) {\r
-               qd->open_disk(file_path);\r
-       }\r
-}\r
-\r
-void VM::close_quickdisk(int drv)\r
-{\r
-       if(drv == 0) {\r
-               qd->close_disk();\r
-       }\r
-}\r
-\r
-bool VM::quickdisk_inserted(int drv)\r
-{\r
-       if(drv == 0) {\r
-               return qd->disk_inserted();\r
-       } else {\r
-               return false;\r
-       }\r
-}\r
-\r
-void VM::open_disk(int drv, _TCHAR* file_path, int offset)\r
-{\r
-       fdc->open_disk(drv, file_path, offset);\r
-}\r
-\r
-void VM::close_disk(int drv)\r
-{\r
-       fdc->close_disk(drv);\r
-}\r
-\r
-bool VM::disk_inserted(int drv)\r
-{\r
-       return fdc->disk_inserted(drv);\r
-}\r
-\r
-#endif\r
-\r
-bool VM::now_skip()\r
-{\r
-       return event->now_skip();\r
-}\r
-\r
-void VM::update_config()\r
-{\r
-#if defined(_MZ800)\r
-       if(boot_mode != config.boot_mode) {\r
-               // boot mode is changed !!!\r
-               boot_mode = config.boot_mode;\r
-               reset();\r
-       } else {\r
-#endif\r
-               for(DEVICE* device = first_device; device; device = device->next_device) {\r
-                       device->update_config();\r
-               }\r
-#if defined(_MZ800)\r
-       }\r
-#endif\r
-}\r
-\r
+/*
+       SHARP MZ-700 Emulator 'EmuZ-700'
+       SHARP MZ-800 Emulator 'EmuZ-800'
+       SHARP MZ-1500 Emulator 'EmuZ-1500'
+
+       Author : Takeda.Toshiya
+       Date   : 2008.06.05 -
+
+       [ virtual machine ]
+*/
+
+#include "mz700.h"
+#include "../../emu.h"
+#include "../device.h"
+#include "../event.h"
+
+#include "../and.h"
+#include "../datarec.h"
+#include "../i8253.h"
+#include "../i8255.h"
+#include "../io.h"
+#include "../noise.h"
+#include "../pcm1bit.h"
+#include "../z80.h"
+
+#ifdef USE_DEBUGGER
+#include "../debugger.h"
+#endif
+
+//#include "cmos.h"
+#include "emm.h"
+#include "kanji.h"
+#include "keyboard.h"
+#include "./memory.h"
+#include "ramfile.h"
+
+#if defined(_MZ800) || defined(_MZ1500)
+#include "../disk.h"
+#include "../mb8877.h"
+#include "../not.h"
+#include "../sn76489an.h"
+#include "../z80pio.h"
+#include "../z80sio.h"
+#include "floppy.h"
+#if defined(_MZ1500)
+#include "../mz1p17.h"
+#include "../prnfile.h"
+#include "psg.h"
+#endif
+#include "quickdisk.h"
+#endif
+
+//using        MZ700::CMOS* cmos;
+using MZ700::EMM;
+using MZ700::KANJI;
+using MZ700::KEYBOARD;
+using MZ700::MEMORY;
+using MZ700::RAMFILE;
+#if defined(_MZ800) || defined(_MZ1500)
+using MZ700::FLOPPY;
+  #if defined(_MZ1500)
+using MZ700::PSG;
+  #endif
+using MZ700::QUICKDISK;
+#endif
+// ----------------------------------------------------------------------------
+// initialize
+// ----------------------------------------------------------------------------
+
+VM::VM(EMU* parent_emu) : VM_TEMPLATE(parent_emu)
+{
+#if defined(_MZ800)
+       boot_mode = config.boot_mode;
+#endif
+       
+       // create devices
+       first_device = last_device = NULL;
+       dummy = new DEVICE(this, emu);  // must be 1st device
+       event = new EVENT(this, emu);   // must be 2nd device
+
+       dummy->set_device_name(_T("1st Dummy"));
+
+       and_int = new AND(this, emu);
+       and_int->set_device_name(_T("AND Gate (IRQ)"));
+   
+       drec = new DATAREC(this, emu);
+       drec->set_context_noise_play(new NOISE(this, emu));
+       drec->set_context_noise_stop(new NOISE(this, emu));
+       drec->set_context_noise_fast(new NOISE(this, emu));
+       pit = new I8253(this, emu);
+       pio = new I8255(this, emu);
+       io = new IO(this, emu);
+       pcm = new PCM1BIT(this, emu);
+       cpu = new Z80(this, emu);
+
+//     cmos = new CMOS(this, emu);
+       emm = new EMM(this, emu);
+       kanji = new KANJI(this, emu);
+       keyboard = new KEYBOARD(this, emu);
+       memory = new MEMORY(this, emu);
+       ramfile = new RAMFILE(this, emu);
+
+#if defined(_MZ800) || defined(_MZ1500)
+       and_snd = new AND(this, emu);
+       and_snd->set_device_name(_T("AND Gate (Sound)"));
+       fdc = new MB8877(this, emu);    // mb8876
+       fdc->set_context_noise_seek(new NOISE(this, emu));
+       fdc->set_context_noise_head_down(new NOISE(this, emu));
+       fdc->set_context_noise_head_up(new NOISE(this, emu));
+#if defined(_MZ800)
+       not_pit = new NOT(this, emu);
+       not_pit->set_device_name(_T("NOT Gate (PIT)"));
+       psg = new SN76489AN(this, emu);
+#elif defined(_MZ1500)
+       if(config.printer_type == 0) {
+               printer = new PRNFILE(this, emu);
+       } else if(config.printer_type == 1) {
+               printer = new MZ1P17(this, emu);
+       } else {
+               printer = dummy;
+       }
+       not_reset = new NOT(this, emu);
+       not_reset->set_device_name(_T("NOT Gate (Reset)"));
+       not_strobe = new NOT(this, emu);
+       not_strobe->set_device_name(_T("NOT Gate (Prinet Strobe)"));
+       psg_l = new SN76489AN(this, emu);
+       psg_l->set_device_name(_T("SN76489AN PSG (Left)"));
+       psg_r = new SN76489AN(this, emu);
+       psg_r->set_device_name(_T("SN76489AN PSG (Right)"));
+#endif
+       pio_int = new Z80PIO(this, emu);
+       pio_int->set_device_name(_T("Z80 PIO(Interrupt)"));
+       sio_rs = new Z80SIO(this, emu);
+       sio_rs->set_device_name(_T("Z80 SIO(RS-232C)"));
+       sio_qd = new Z80SIO(this, emu);
+       sio_qd->set_device_name(_T("Z80 SIO(Quick Disk)"));
+       
+       floppy = new FLOPPY(this, emu);
+#if defined(_MZ1500)
+       psg = new PSG(this, emu);
+#endif
+       qd = new QUICKDISK(this, emu);
+#endif
+       
+       // set contexts
+       event->set_context_cpu(cpu);
+       event->set_context_sound(pcm);
+#if defined(_MZ800)
+       event->set_context_sound(psg);
+#elif defined(_MZ1500)
+       event->set_context_sound(psg_l);
+       event->set_context_sound(psg_r);
+#endif
+       event->set_context_sound(drec);
+#if defined(_MZ800) || defined(_MZ1500)
+       event->set_context_sound(fdc->get_context_noise_seek());
+       event->set_context_sound(fdc->get_context_noise_head_down());
+       event->set_context_sound(fdc->get_context_noise_head_up());
+#endif
+       event->set_context_sound(drec->get_context_noise_play());
+       event->set_context_sound(drec->get_context_noise_stop());
+       event->set_context_sound(drec->get_context_noise_fast());
+       
+       // VRAM/PCG wait
+       memory->set_context_cpu(cpu);
+       
+       // memory mapped I/O
+       memory->set_context_pio(pio);
+       memory->set_context_pit(pit);
+       
+#if defined(_MZ1500)
+       // psg mixer
+       psg->set_context_psg_l(psg_l);
+       psg->set_context_psg_r(psg_r);
+#endif
+       
+#if defined(_MZ800)
+       // 8253:CLK#0 <- 1.10MHz
+       pit->set_constant_clock(0, 1100000);
+#else
+       // 8253:CLK#0 <- 895KHz
+       pit->set_constant_clock(0, CPU_CLOCKS / 4);
+#endif
+       
+#if defined(_MZ800) || defined(_MZ1500)
+       // 8253:OUT#0 AND 8255:PC0 -> SPEAKER
+       pit->set_context_ch0(and_snd, SIG_AND_BIT_0, 1);
+       pio->set_context_port_c(and_snd, SIG_AND_BIT_1, 1, 0);
+       and_snd->set_context_out(pcm, SIG_PCM1BIT_SIGNAL, 1);
+       and_snd->set_mask(SIG_AND_BIT_0 | SIG_AND_BIT_1);
+       // Sound:: Force realtime rendering. This is temporally fix. 20161024 K.O
+#else
+       // 8253:OUT#0 -> SPEAKER
+       pit->set_context_ch0(pcm, SIG_PCM1BIT_SIGNAL, 1);
+       // Sound:: Force realtime rendering. This is temporally fix. 20161024 K.O
+#endif
+#if defined(_MZ800)
+       // 8253:OUT#0 -> NOT -> Z80PIO:PA4
+       pit->set_context_ch0(not_pit, SIG_NOT_INPUT, 1);
+       not_pit->set_context_out(pio_int, SIG_Z80PIO_PORT_A, 0x10);
+#elif defined(_MZ1500)
+       // 8253:OUT#0 -> Z80PIO:PA4
+       pit->set_context_ch0(pio_int, SIG_Z80PIO_PORT_A, 0x10);
+#endif
+       
+       // 8253:CLK#1 <- 15.7KHz
+       pit->set_constant_clock(1, CPU_CLOCKS / 228);
+       
+       // 8253:OUT#1 -> 8253:CLK#2
+       pit->set_context_ch1(pit, SIG_I8253_CLOCK_2, 1);
+       
+       // 8253:OUT#2 (N)AND 8255:PC2 -> Z80:INT
+       pit->set_context_ch2(and_int, SIG_AND_BIT_0, 1);
+       pio->set_context_port_c(and_int, SIG_AND_BIT_1, 4, 0);
+       and_int->set_context_out(cpu, SIG_CPU_IRQ, 1);
+       and_int->set_mask(SIG_AND_BIT_0 | SIG_AND_BIT_1);
+       
+#if defined(_MZ1500)
+       // 8253:OUT#2 -> Z80PIO:PA5
+       pit->set_context_ch2(pio_int, SIG_Z80PIO_PORT_A, 0x20);
+#endif
+       
+       // 8255:PA0-3 -> KEYBOARD:STROBE
+       pio->set_context_port_a(keyboard, SIG_KEYBOARD_COLUMN, 0x0f, 0);
+#if defined(_MZ800)
+       // 8255:PA4 -> JOYSTICK #1
+       // 8255:PA5 -> JOYSTICK #2
+#endif
+       // 8255:PA7 -> 556 RESET
+       
+       // 8255:PB0-7 <- KEYBOARD:DATA
+       keyboard->set_context_pio(pio);
+       
+       // 8255:PC0 -> AND -> SPEAKER
+       // 8255:PC1 -> DATA RECORDER:WRITE DATA
+       pio->set_context_port_c(drec, SIG_DATAREC_MIC, 0x02, 0);
+       // 8255:PC2 -> (N)AND -> Z80:INT
+       // 8255:PC3 -> DATA RECORDER:MOTOR ON/OFF
+       pio->set_context_port_c(drec, SIG_DATAREC_TRIG, 0x08, 0);
+       // 8255:PC4 <- DATA RECORDER:MOTOR REMOTE
+       drec->set_context_remote(pio, SIG_I8255_PORT_C, 0x10);
+       // 8255:PC5 <- DATA RECORDER:READ DATA
+       drec->set_context_ear(pio, SIG_I8255_PORT_C, 0x20);
+       // 8255:PC6 <- MEMORY:556 OUT (1.5KHz)
+       // 8255:PC7 <- MEMORY:VBLANK
+       
+#if defined(_MZ800) || defined(_MZ1500)
+       // Z80PIO:PA2 <- GND
+       // Z80PIO:PA3 <- GND
+#if defined(_MZ800)
+       // Z80PIO:PA4 <- NOT <- 8253:OUT#0
+       // Z80PIO:PA5 <- HBLANK
+       memory->set_context_pio_int(pio_int);
+#elif defined(_MZ1500)
+       // Z80PIO:PA0 <- PRINTER:RDA (BUSY)
+       // Z80PIO:PA1 <- PRINTER:STA (PE)
+       if(config.printer_type == 0) {
+               PRNFILE *prnfile = (PRNFILE *)printer;
+               prnfile->set_context_busy(pio_int, SIG_Z80PIO_PORT_A, 0x01);
+       } else if(config.printer_type == 1) {
+               MZ1P17 *mz1p17 = (MZ1P17 *)printer;
+               mz1p17->mode = MZ1P17_MODE_MZ2;
+               mz1p17->set_context_busy(pio_int, SIG_Z80PIO_PORT_A, 0x01);
+       }
+       // Z80PIO:PA4 <- 8253:OUT#0
+       // Z80PIO:PA5 <- 8253:OUT#2
+       // Z80PIO:PA6 -> NOT -> PRINTER:IRT (RESET)
+       // Z80PIO:PA7 -> NOT -> PRINTER:RDP (STROBE)
+       // Z80PIO:PB  -> PRINTER:DATA
+       pio_int->set_context_port_a(not_reset, SIG_NOT_INPUT, 0x40, 0);
+       not_reset->set_context_out(printer, SIG_PRINTER_RESET, 0x01);
+       pio_int->set_context_port_a(not_strobe, SIG_NOT_INPUT, 0x80, 0);
+       not_strobe->set_context_out(printer, SIG_PRINTER_STROBE, 0x01);
+       pio_int->set_context_port_b(printer, SIG_PRINTER_DATA, 0xff, 0);
+#endif
+#endif
+       
+#if defined(_MZ800) || defined(_MZ1500)
+       // Z80SIO:RTSA -> QD:WRGA
+       sio_qd->set_context_rts(0, qd, QUICKDISK_SIO_RTSA, 1);
+       // Z80SIO:DTRB -> QD:MTON
+       sio_qd->set_context_dtr(1, qd, QUICKDISK_SIO_DTRB, 1);
+       // Z80SIO:SENDA -> QD:RECV
+       sio_qd->set_context_sync(0, qd, QUICKDISK_SIO_SYNC, 1);
+       sio_qd->set_context_rxdone(0, qd, QUICKDISK_SIO_RXDONE, 1);
+       sio_qd->set_context_send(0, qd, QUICKDISK_SIO_DATA);
+       sio_qd->set_context_break(0, qd, QUICKDISK_SIO_BREAK, 1);
+       // Z80SIO:CTSA <- QD:PROTECT
+       // Z80SIO:DCDA <- QD:INSERT
+       // Z80SIO:DCDB <- QD:HOE
+       qd->set_context_sio(sio_qd);
+       
+       sio_rs->set_tx_clock(0, 1200 * 16);     // 1200 baud
+       sio_rs->set_rx_clock(0, 1200 * 16);     // baud-rate can be changed by jumper pin
+       sio_rs->set_tx_clock(1, 1200 * 16);
+       sio_rs->set_rx_clock(1, 1200 * 16);
+       
+       sio_qd->set_tx_clock(0, 101562.5);
+       sio_qd->set_rx_clock(0, 101562.5);
+       sio_qd->set_tx_clock(1, 101562.5);
+       sio_qd->set_rx_clock(1, 101562.5);
+       
+       // floppy drives
+       floppy->set_context_cpu(cpu);
+       floppy->set_context_fdc(fdc);
+       fdc->set_context_drq(floppy, SIG_FLOPPY_DRQ, 1);
+#endif
+       
+       // cpu bus
+       cpu->set_context_mem(memory);
+       cpu->set_context_io(io);
+#if defined(_MZ800) || defined(_MZ1500)
+       cpu->set_context_intr(pio_int);
+       // z80 family daisy chain
+       // 0=8253:OUT2
+       pio_int->set_context_intr(cpu, 1);
+       pio_int->set_context_child(sio_rs);
+       sio_rs->set_context_intr(cpu, 2);
+       sio_rs->set_context_child(sio_qd);
+       sio_qd->set_context_intr(cpu, 3);
+#else
+       cpu->set_context_intr(dummy);
+#endif
+#ifdef USE_DEBUGGER
+       cpu->set_context_debugger(new DEBUGGER(this, emu));
+#endif
+       
+       // emm
+       io->set_iomap_range_rw(0x00, 0x03, emm);
+       // kanji
+       io->set_iomap_range_rw(0xb8, 0xb9, kanji);
+       // ramfile
+       io->set_iomap_range_rw(0xea, 0xeb, ramfile);
+       // cmos
+//     io->set_iomap_range_rw(0xf8, 0xfa, cmos);
+       
+#if defined(_MZ800)
+       // 8255/8253
+       io->set_iomap_range_rw(0xd0, 0xd3, pio);
+       io->set_iomap_range_rw(0xd4, 0xd7, pit);
+#endif
+       
+#if defined(_MZ800) || defined(_MZ1500)
+       // floppy drives
+       io->set_iomap_range_rw(0xd8, 0xdb, fdc);
+       io->set_iomap_range_w(0xdc, 0xdf, floppy);
+#endif
+       
+       // memory mapper
+#if defined(_MZ800)
+       io->set_iomap_range_r(0xe0, 0xe1, memory);
+       io->set_iomap_range_w(0xe0, 0xe6, memory);
+#elif defined(_MZ1500)
+       io->set_iomap_range_w(0xe0, 0xe6, memory);
+#else
+       io->set_iomap_range_w(0xe0, 0xe4, memory);
+#endif
+       
+#if defined(_MZ800)
+       // crtc
+       io->set_iomap_range_w(0xcc, 0xcf, memory);
+       io->set_iomap_single_r(0xce, memory);
+       // palette
+       io->set_iomap_single_w(0xf0, memory);
+#elif defined(_MZ1500)
+       // palette
+       io->set_iomap_range_w(0xf0, 0xf1, memory);
+#endif
+       
+#if defined(_MZ800)
+       // joystick
+//     io->set_iomap_range_r(0xf0, 0xf1, joystick);
+#endif
+       
+       // psg
+#if defined(_MZ800)
+       io->set_iomap_single_w(0xf2, psg);
+#elif defined(_MZ1500)
+       io->set_iomap_single_w(0xe9, psg);
+       io->set_iomap_single_w(0xf2, psg_l);
+       io->set_iomap_single_w(0xf3, psg_r);
+#endif
+       
+#if defined(_MZ800) || defined(_MZ1500)
+       // z80pio/sio
+       // z80pio and z80sio*2
+       static const int z80_sio_addr[4] = {0, 2, 1, 3};
+       static const int z80_pio_addr[4] = {1, 3, 0, 2};
+       for(int i = 0; i < 4; i++) {
+               io->set_iomap_alias_rw(0xb0 + i, sio_rs, z80_sio_addr[i]);
+               io->set_iomap_alias_rw(0xf4 + i, sio_qd, z80_sio_addr[i]);
+               io->set_iomap_alias_rw(0xfc + i, pio_int, z80_pio_addr[i]);
+       }
+#else
+       // printer
+       io->set_iovalue_single_r(0xfe, 0xc0);
+#endif
+       
+       // initialize all devices
+#if defined(__GIT_REPO_VERSION)
+       strncpy(_git_revision, __GIT_REPO_VERSION, sizeof(_git_revision) - 1);
+#endif
+       for(DEVICE* device = first_device; device; device = device->next_device) {
+               device->initialize();
+       }
+#if defined(_MZ800) || defined(_MZ1500)
+       for(int drv = 0; drv < MAX_DRIVE; drv++) {
+//             if(config.drive_type) {
+                       fdc->set_drive_type(drv, DRIVE_TYPE_2DD);
+//             } else {
+//                     fdc->set_drive_type(drv, DRIVE_TYPE_2D);
+//             }
+       }
+#endif
+}
+
+VM::~VM()
+{
+       // delete all devices
+       for(DEVICE* device = first_device; device;) {
+               DEVICE *next_device = device->next_device;
+               device->release();
+               delete device;
+               device = next_device;
+       }
+}
+
+DEVICE* VM::get_device(int id)
+{
+       for(DEVICE* device = first_device; device; device = device->next_device) {
+               if(device->this_device_id == id) {
+                       return device;
+               }
+       }
+       return NULL;
+}
+
+// ----------------------------------------------------------------------------
+// drive virtual machine
+// ----------------------------------------------------------------------------
+
+void VM::reset()
+{
+       // reset all devices
+       for(DEVICE* device = first_device; device; device = device->next_device) {
+               device->reset();
+       }
+       and_int->write_signal(SIG_AND_BIT_0, 0, 1);     // CLOCK = L
+       and_int->write_signal(SIG_AND_BIT_1, 1, 1);     // INTMASK = H
+#if defined(_MZ800) || defined(_MZ1500)
+       and_snd->write_signal(SIG_AND_BIT_1, 1, 1);     // SNDMASK = H
+#endif
+#if defined(_MZ1500)
+       pio_int->write_signal(SIG_Z80PIO_PORT_A, 0x02, 0x03);   // BUSY = L, PE = H
+#endif
+}
+
+void VM::run()
+{
+       event->drive();
+}
+
+// ----------------------------------------------------------------------------
+// debugger
+// ----------------------------------------------------------------------------
+
+#ifdef USE_DEBUGGER
+DEVICE *VM::get_cpu(int index)
+{
+       if(index == 0) {
+               return cpu;
+       }
+       return NULL;
+}
+#endif
+
+// ----------------------------------------------------------------------------
+// draw screen
+// ----------------------------------------------------------------------------
+
+void VM::draw_screen()
+{
+       memory->draw_screen();
+}
+
+// ----------------------------------------------------------------------------
+// soud manager
+// ----------------------------------------------------------------------------
+
+void VM::initialize_sound(int rate, int samples)
+{
+       // init sound manager
+       event->initialize_sound(rate, samples);
+       
+       // init sound gen
+       pcm->initialize_sound(rate, 8000);
+#if defined(_MZ800)
+       psg->initialize_sound(rate, 3579545, 8000);
+#elif defined(_MZ1500)
+       psg_l->initialize_sound(rate, 3579545, 8000);
+       psg_r->initialize_sound(rate, 3579545, 8000);
+#endif
+}
+
+uint16_t* VM::create_sound(int* extra_frames)
+{
+       return event->create_sound(extra_frames);
+}
+
+int VM::get_sound_buffer_ptr()
+{
+       return event->get_sound_buffer_ptr();
+}
+
+#ifdef USE_SOUND_VOLUME
+void VM::set_sound_device_volume(int ch, int decibel_l, int decibel_r)
+{
+#if defined(_MZ800)
+       if(ch-- == 0) {
+               psg->set_volume(0, decibel_l, decibel_r);
+       } else
+#elif defined(_MZ1500)
+       if(ch-- == 0) {
+               psg_l->set_volume(0, decibel_l, decibel_r);
+       } else if(ch-- == 0) {
+               psg_r->set_volume(0, decibel_l, decibel_r);
+       } else
+#endif
+       if(ch-- == 0) {
+               pcm->set_volume(0, decibel_l, decibel_r);
+       } else if(ch-- == 0) {
+               drec->set_volume(0, decibel_l, decibel_r);
+#if defined(_MZ800) || defined(_MZ1500)
+       } else if(ch-- == 0) {
+               fdc->get_context_noise_seek()->set_volume(0, decibel_l, decibel_r);
+               fdc->get_context_noise_head_down()->set_volume(0, decibel_l, decibel_r);
+               fdc->get_context_noise_head_up()->set_volume(0, decibel_l, decibel_r);
+#endif
+       } else if(ch-- == 0) {
+               drec->get_context_noise_play()->set_volume(0, decibel_l, decibel_r);
+               drec->get_context_noise_stop()->set_volume(0, decibel_l, decibel_r);
+               drec->get_context_noise_fast()->set_volume(0, decibel_l, decibel_r);
+       }
+}
+#endif
+
+// ----------------------------------------------------------------------------
+// user interface
+// ----------------------------------------------------------------------------
+
+void VM::play_tape(int drv, const _TCHAR* file_path)
+{
+       drec->play_tape(file_path);
+//     drec->set_remote(true);
+}
+
+void VM::rec_tape(int drv, const _TCHAR* file_path)
+{
+       drec->rec_tape(file_path);
+//     drec->set_remote(true);
+}
+
+void VM::close_tape(int drv)
+{
+       emu->lock_vm();
+       drec->close_tape();
+       emu->unlock_vm();
+
+       //drec->set_remote(false);
+}
+
+bool VM::is_tape_inserted(int drv)
+{
+       return drec->is_tape_inserted();
+}
+
+bool VM::is_tape_playing(int drv)
+{
+       return drec->is_tape_playing();
+}
+
+bool VM::is_tape_recording(int drv)
+{
+       return drec->is_tape_recording();
+}
+
+int VM::get_tape_position(int drv)
+{
+       return drec->get_tape_position();
+}
+
+const _TCHAR* VM::get_tape_message(int drv)
+{
+       return drec->get_message();
+}
+
+void VM::push_play(int drv)
+{
+       drec->set_ff_rew(0);
+       drec->set_remote(true);
+}
+
+void VM::push_stop(int drv)
+{
+       drec->set_remote(false);
+}
+
+void VM::push_fast_forward(int drv)
+{
+       drec->set_ff_rew(1);
+       drec->set_remote(true);
+}
+
+void VM::push_fast_rewind(int drv)
+{
+       drec->set_ff_rew(-1);
+       drec->set_remote(true);
+}
+
+#if defined(_MZ800) || defined(_MZ1500)
+void VM::open_quick_disk(int drv, const _TCHAR* file_path)
+{
+       if(drv == 0) {
+               qd->open_disk(file_path);
+       }
+}
+
+void VM::close_quick_disk(int drv)
+{
+       if(drv == 0) {
+               qd->close_disk();
+       }
+}
+
+bool VM::is_quick_disk_inserted(int drv)
+{
+       if(drv == 0) {
+               return qd->is_disk_inserted();
+       } else {
+               return false;
+       }
+}
+
+uint32_t VM::is_quick_disk_accessed()
+{
+       return qd->read_signal(0);
+}
+
+void VM::open_floppy_disk(int drv, const _TCHAR* file_path, int bank)
+{
+       fdc->open_disk(drv, file_path, bank);
+       
+       if(fdc->get_media_type(drv) == MEDIA_TYPE_2DD) {
+               if(fdc->get_drive_type(drv) == DRIVE_TYPE_2D) {
+                       fdc->set_drive_type(drv, DRIVE_TYPE_2DD);
+               }
+       } else if(fdc->get_media_type(drv) == MEDIA_TYPE_2D) {
+               if(fdc->get_drive_type(drv) == DRIVE_TYPE_2DD) {
+                       fdc->set_drive_type(drv, DRIVE_TYPE_2D);
+               }
+       }
+}
+
+void VM::close_floppy_disk(int drv)
+{
+       fdc->close_disk(drv);
+}
+
+bool VM::is_floppy_disk_inserted(int drv)
+{
+       return fdc->is_disk_inserted(drv);
+}
+
+void VM::is_floppy_disk_protected(int drv, bool value)
+{
+       fdc->is_disk_protected(drv, value);
+}
+
+bool VM::is_floppy_disk_protected(int drv)
+{
+       return fdc->is_disk_protected(drv);
+}
+
+uint32_t VM::is_floppy_disk_accessed()
+{
+       return fdc->read_signal(0);
+}
+#endif
+
+bool VM::is_frame_skippable()
+{
+       return event->is_frame_skippable();
+}
+
+void VM::update_config()
+{
+#if defined(_MZ800)
+       if(boot_mode != config.boot_mode) {
+               // boot mode is changed !!!
+               boot_mode = config.boot_mode;
+               reset();
+       } else {
+#endif
+               for(DEVICE* device = first_device; device; device = device->next_device) {
+                       device->update_config();
+               }
+#if defined(_MZ800)
+       }
+#endif
+}
+
+#define STATE_VERSION  3
+
+
+bool VM::process_state(FILEIO* state_fio, bool loading)
+{
+       if(!state_fio->StateCheckUint32(STATE_VERSION)) {
+               return false;
+       }
+       for(DEVICE* device = first_device; device; device = device->next_device) {
+               // Note: typeid(foo).name is fixed by recent ABI.Not dec 6.
+               // const char *name = typeid(*device).name();
+               //       But, using get_device_name() instead of typeid(foo).name() 20181008 K.O
+               const char *name = device->get_device_name();
+               int len = strlen(name);
+               
+               if(!state_fio->StateCheckInt32(len)) {
+                       if(loading) {
+                               printf("Class name len Error: DEVID=%d EXPECT=%s\n", device->this_device_id, name);
+                       }
+                       return false;
+               }
+               if(!state_fio->StateCheckBuffer(name, len, 1)) {
+                       if(loading) {
+                               printf("Class name Error: DEVID=%d EXPECT=%s\n", device->this_device_id, name);
+                       }
+                       return false;
+               }
+               if(!device->process_state(state_fio, loading)) {
+                       if(loading) {
+                               printf("Data loading Error: DEVID=%d\n", device->this_device_id);
+                       }
+                       return false;
+               }
+       }
+#if defined(_MZ800)
+       state_fio->StateInt32(boot_mode);
+#endif
+       return true;
+}