OSDN Git Service

[VM][FMTOWNS][CDROM] CDROM checks DMAC's mask as running DMAC CH.3.
[csp-qt/common_source_project-fm7.git] / source / src / vm / fmtowns / fmtowns.cpp
index bdd55b6..99fec7d 100644 (file)
@@ -5,7 +5,7 @@
        Date   : 2016.12.28 -
 
        [ virtual machine ]
-       History: 
+       History:
                2016-12-28 Copy from eFMR-50.
 */
 
@@ -20,6 +20,7 @@
 #include "../i8259.h"
 
 #include "../i386_np21.h"
+//#include "../i386.h"
 
 #include "../io.h"
 #include "../mb8877.h"
 #include "../pcm1bit.h"
 #include "../harddisk.h"
 #include "../scsi_hdd.h"
-#include "./towns_scsi_host.h"
+#include "../scsi_host.h"
+//#include "./towns_scsi_host.h"
 #include "../upd71071.h"
 
-#include "towns_cdrom.h"
-#include "towns_crtc.h"
-#include "towns_dictionary.h"
-#include "towns_dmac.h"
-#include "towns_memory.h"
-#include "towns_sprite.h"
-#include "towns_sysrom.h"
-#include "towns_vram.h"
+#include "./cdrom.h"
+#include "./cmos.h"
+#include "./crtc.h"
+#include "./dictionary.h"
+#include "./dmac.h"
+#include "./towns_memory.h"
+#include "./sprite.h"
+#include "./sysrom.h"
+#include "./vram.h"
+
 // Electric Volume
-//#include "mb87078.h"
+#include "mb87078.h"
 //YM-2612 "OPN2"
 //#include "../ym2612.h"
 //RF5C68 PCM
 #endif
 
 #include "./adpcm.h"
-#include "./cdc.h"
+//#include "./cdc.h"
 #include "./floppy.h"
 #include "./fontroms.h"
 #include "./joystick.h"
+#include "./joypad_2btn.h"
+#include "./joypad_6btn.h"
 #include "./keyboard.h"
+#include "./mouse.h"
 #include "./msdosrom.h"
 #include "./scsi.h"
 #include "./serialrom.h"
 #include "./timer.h"
+#include "./iccard.h"
+
+#include "./planevram.h"
 
 // ----------------------------------------------------------------------------
 // initialize
 // ----------------------------------------------------------------------------
 using FMTOWNS::ADPCM;
-using FMTOWNS::CDC;
+//using FMTOWNS::CDC;
+using FMTOWNS::CMOS;
 using FMTOWNS::DICTIONARY;
 using FMTOWNS::FLOPPY;
 using FMTOWNS::FONT_ROMS;
 using FMTOWNS::JOYSTICK;
+using FMTOWNS::JOYPAD_2BTN;
+using FMTOWNS::JOYPAD_6BTN;
+
 using FMTOWNS::KEYBOARD;
+using FMTOWNS::MOUSE;
 using FMTOWNS::MSDOSROM;
 using FMTOWNS::SCSI;
 using FMTOWNS::SERIAL_ROM;
 using FMTOWNS::SYSROM;
 using FMTOWNS::TIMER;
+using FMTOWNS::TOWNS_ICCARD;
 
 using FMTOWNS::TOWNS_CDROM;
 using FMTOWNS::TOWNS_CRTC;
 using FMTOWNS::TOWNS_DMAC;
 using FMTOWNS::TOWNS_MEMORY;
-using FMTOWNS::TOWNS_SCSI_HOST;
+//using FMTOWNS::TOWNS_SCSI_HOST;
 using FMTOWNS::TOWNS_SPRITE;
 using FMTOWNS::TOWNS_VRAM;
+using FMTOWNS::PLANEVRAM;
 
 
-VM::VM(EMU* parent_emu) : VM_TEMPLATE(parent_emu)
+VM::VM(EMU_TEMPLATE* parent_emu) : VM_TEMPLATE(parent_emu)
 {
 /*
        Machine ID & CPU ID
@@ -109,15 +126,15 @@ VM::VM(EMU* parent_emu) : VM_TEMPLATE(parent_emu)
        80386SX         0x03
        80486           0x02
 */
-       
+
        // create devices
-       first_device = last_device = NULL;
+       //first_device = last_device = nullptr;
        dummy = new DEVICE(this, emu);  // must be 1st device
        event = new EVENT(this, emu);   // must be 2nd device
 #if defined(_USE_QT)
        dummy->set_device_name(_T("1st Dummy"));
        event->set_device_name(_T("EVENT"));
-#endif 
+#endif
 
        cpu = new I386(this, emu);
 #if defined(_USE_QT)
@@ -128,39 +145,55 @@ VM::VM(EMU* parent_emu) : VM_TEMPLATE(parent_emu)
   #elif defined(HAS_PENTIUM)
        cpu->set_device_name(_T("CPU(Pentium)"));
   #endif
-#endif 
+#endif
 
        io = new IO(this, emu);
-       
+       io->space = _IO_SPACE;
+       io->bus_width = _IO_BUS_WIDTH;
+
        crtc = new TOWNS_CRTC(this, emu);
-       cdc  = new CDC(this, emu);
-       cdc_scsi = new TOWNS_SCSI_HOST(this, emu);
-//     cdc_scsi = new SCSI_HOST(this, emu);
        cdrom = new TOWNS_CDROM(this, emu);
 
        memory = new TOWNS_MEMORY(this, emu);
+       //memory->space = _MEMORY_SPACE;
+       //memory->bank_size = _MEMORY_BANK_SIZE;
+       //memory->bus_width = _MEMORY_BUS_WIDTH;
+
        vram = new TOWNS_VRAM(this, emu);
        sprite = new TOWNS_SPRITE(this, emu);
        sysrom = new SYSROM(this, emu);
        msdosrom = new MSDOSROM(this, emu);
        fontrom = new FONT_ROMS(this, emu);
        dictionary = new DICTIONARY(this, emu);
+       cmos     = new CMOS(this, emu);
+#ifdef USE_DEBUGGER
+       cmos->set_context_debugger(new DEBUGGER(this, emu));
+#endif
+
 #if defined(HAS_20PIX_FONTS)
        fontrom_20pix = new FONT_ROM_20PIX(this, emu);
 #endif
        serialrom = new SERIAL_ROM(this, emu);
+
        adpcm = new ADPCM(this, emu);
 //     mixer = new MIXER(this, emu); // Pseudo mixer.
-               
+
+       planevram = new PLANEVRAM(this, emu);
+
        adc = new AD7820KR(this, emu);
        rf5c68 = new RF5C68(this, emu);
-//     e_volume[0] = new MB87878(this, emu);
-//     e_volume[1] = new MB87878(this, emu);
-       
+       e_volumes[0] = new MB87078(this, emu);
+       e_volumes[1] = new MB87078(this, emu);
+
        sio = new I8251(this, emu);
        pit0 = new I8253(this, emu);
+       pit0->device_model = INTEL_8253;
+
        pit1 = new I8253(this, emu);
+       pit1->device_model = INTEL_8253;
+
        pic = new I8259(this, emu);
+       pic->num_chips = 2;
        fdc = new MB8877(this, emu);
        rtc = new MSM58321(this, emu);
        beep = new PCM1BIT(this, emu);
@@ -169,13 +202,13 @@ VM::VM(EMU* parent_emu) : VM_TEMPLATE(parent_emu)
        seek_sound = new NOISE(this, emu);
        head_up_sound = new NOISE(this, emu);
        head_down_sound = new NOISE(this, emu);
-       
+
 //     scsi_host = new TOWNS_SCSI_HOST(this, emu);
        scsi_host = new SCSI_HOST(this, emu);
-       
+
        for(int i = 0; i < 7; i++) {
-               scsi_hdd[i] = NULL;
-       }       
+               scsi_hdd[i] = nullptr;
+       }
 #if defined(USE_HARD_DISK)
        for(int i = 0; i < USE_HARD_DISK; i++) {
                scsi_hdd[i] = new SCSI_HDD(this, emu);
@@ -196,7 +229,19 @@ VM::VM(EMU* parent_emu) : VM_TEMPLATE(parent_emu)
        joystick = new JOYSTICK(this, emu);
        scsi = new SCSI(this, emu);
        timer = new TIMER(this, emu);
-       
+
+       iccard1 = new TOWNS_ICCARD(this, emu);
+#if 0
+       iccard2 = new TOWNS_ICCARD(this, emu);
+#else
+       iccard2 = nullptr;
+#endif
+       for(int i = 0; i < 2; i++) {
+               joypad_2btn[i] = new JOYPAD_2BTN(this, emu);
+               joypad_6btn[i] = new JOYPAD_6BTN(this, emu);
+               mouse[i] = new MOUSE(this, emu);
+       }
+
        uint16_t machine_id = 0x0100; // FM-Towns1
        uint16_t cpu_id = 0x0001;     // i386DX
        uint32_t cpu_clock = 16000 * 1000; // 16MHz
@@ -226,6 +271,10 @@ VM::VM(EMU* parent_emu) : VM_TEMPLATE(parent_emu)
        machine_id = 0x0700; // OK?
        cpu_id = 0x0002;      // i486SX
        cpu_clock = 20000 * 1000; // 20MHz
+#elif defined(_FMTOWNS_UR_VARIANTS)
+       machine_id = 0x0900;  // UR10/20/40/80
+       cpu_id = 0x0002;      // i486DX
+       cpu_clock = 20000 * 1000; // ToDo: Correct frequency.
 #elif defined(_FMTOWNS_MA_VARIANTS)
        machine_id = 0x0b00; // OK?
        cpu_id = 0x0002;      // i486SX
@@ -242,17 +291,21 @@ VM::VM(EMU* parent_emu) : VM_TEMPLATE(parent_emu)
        machine_id = 0x0c00; // OK?
        cpu_id = 0x0002;      // i486DX (With FPU?)
        cpu_clock = 66000 * 1000; // 66MHz
-#elif defined(_FMTOWNS_MX_VARIANTS)
-       machine_id = 0x0c00; // OK?
-       cpu_id = 0x0002;      // i486DX (With FPU?)
-       cpu_clock = 66000 * 1000; // 66MHz
+#elif defined(_FMTOWNS_HC_VARIANTS)
+       // 20210227 K.O
+       // From FMTowns::MachineID()  of TSUGARU,
+       // git 83d4ec2309ac9fcbb8c01f26061ff0d49c5321e4.
+       machine_id = 0x1100; // OK?
+       cpu_id = 0x0002;      // Pentium (With FPU?)
+       cpu_clock = 50000 * 1000; // ToDo: Correctness frequency.
 #else
        // ToDo: Pentium Model (After HB).
 
 #endif
+
        event->set_frames_per_sec(FRAMES_PER_SEC);
        event->set_lines_per_frame(LINES_PER_FRAME);
-       
+
        set_machine_type(machine_id, cpu_id);
        // set contexts
        event->set_context_cpu(cpu, cpu_clock);
@@ -263,131 +316,192 @@ VM::VM(EMU* parent_emu) : VM_TEMPLATE(parent_emu)
        mic_in_ch = -1;
 
        // Use pseudo mixer instead of event.Due to using ADC.
-#if 0
-       line_mix_ch = -1;
-       modem_mix_ch = -1;
-       mic_mix_ch = -1;
-       //line_mix_ch = mixer->set_context_sound(line_in);
-       //modem_mix_ch = mixer->set_context_sound(modem_in);
-       //mic_mix_ch = mixer->set_context_sound(mic_in);
-       beep_mix_ch = mixer->set_context_sound(beep);
-       pcm_mix_ch  = mixer->set_context_sound(rf5c68);
-       opn2_mix_ch = mixer->set_context_sound(opn2);
-       cdc_mix_ch = mixer->set_context_sound(cdc);
-       mixer->set_interpolate_filter_freq(pcm_mix_ch, 4000); // channel, freq; disable if freq <= 0.
-       event->set_context_sound(mixer);
-#else
        // Temporally not use mixer.
        event->set_context_sound(beep);
        event->set_context_sound(opn2);
        event->set_context_sound(rf5c68);
        event->set_context_sound(cdrom);
-#endif
+
        fdc->set_context_noise_seek(seek_sound);
        fdc->set_context_noise_head_down(head_down_sound);
        fdc->set_context_noise_head_up(head_up_sound);
        event->set_context_sound(seek_sound);
        event->set_context_sound(head_down_sound);
        event->set_context_sound(head_up_sound);
-       
+
+#ifdef USE_DEBUGGER
+       pit0->set_context_debugger(new DEBUGGER(this, emu));
+       pit1->set_context_debugger(new DEBUGGER(this, emu));
+#endif
        pit0->set_context_ch0(timer, SIG_TIMER_CH0, 1);
        pit0->set_context_ch1(timer, SIG_TIMER_CH1, 1);
-       pit0->set_context_ch2(beep, SIG_PCM1BIT_SIGNAL, 1);
+       pit0->set_context_ch2(beep,  SIG_PCM1BIT_SIGNAL, 1);
        pit0->set_constant_clock(0, 307200);
        pit0->set_constant_clock(1, 307200);
        pit0->set_constant_clock(2, 307200);
-       pit1->set_constant_clock(1, 1228800);
-//     pic->set_context_cpu(cpu);
-       pic->set_context_cpu(memory);
+       pit1->set_constant_clock(0, 1229900);
+       pit1->set_constant_clock(1, 1229900);
+       pit1->set_constant_clock(2, 1229900);
+       pic->set_context_cpu(cpu);
+       //pic->set_context_cpu(memory);
+
        fdc->set_context_irq(floppy, SIG_FLOPPY_IRQ, 1);
+       //fdc->set_context_irq(dma, SIG_TOWNS_DMAC_EOT_CH0, 1);
+
        rtc->set_context_data(timer, SIG_TIMER_RTC, 0x0f, 0);
        rtc->set_context_busy(timer, SIG_TIMER_RTC_BUSY, 0x80);
        scsi_host->set_context_irq(scsi, SIG_SCSI_IRQ, 1);
        scsi_host->set_context_drq(scsi, SIG_SCSI_DRQ, 1);
+       scsi_host->set_context_drq(keyboard, SIG_KEYBOARD_BOOTSEQ_END, 1);
+
+#ifdef USE_DEBUGGER
+       dma->set_context_debugger(new DEBUGGER(this, emu));
+       extra_dma->set_context_debugger(new DEBUGGER(this, emu));
+#endif
+       // Note: DMAC may set wait value to CPU. 20230409 K.O
+       dma->set_context_cpu(NULL);
+       //dma->set_context_cpu(cpu);
        dma->set_context_memory(memory);
+       // BASE CLOCK is 1MHz * 4.
+       dma->set_dmac_clock(4 * 1000 * 1000, 4);
        dma->set_context_ch0(fdc);
+       // This is workaround for FM-Towns's SCSI.
+       dma->set_force_16bit_transfer(1, false);
        dma->set_context_ch1(scsi_host);
        //dma->set_context_ch2(printer);
-       //dma->set_context_ch3(cdc);
-       dma->set_context_ch3(cdc_scsi);
-       dma->set_context_ube1(scsi_host, SIG_SCSI_16BIT_BUS, 0x02);
-
+       dma->set_context_ch3(cdrom);
+       dma->set_context_mask_bit(cdrom, SIG_TOWNS_CDROM_DMAMASK, 3);
+       //extra_dma->set_context_cpu(cpu);
+       extra_dma->set_context_cpu(NULL);
+       extra_dma->set_context_memory(memory);
+       // BASE CLOCK is 1MHz * 4.
+       extra_dma->set_dmac_clock(4 * 1000 * 1000, 4);
+
+       //dma->set_context_tc1(scsi, SIG_SCSI_EOT, 0xffffffff);
+       dma->set_context_tc3(cdrom, SIG_TOWNS_CDROM_DMAINT, 0xffffffff);
+
+       dma->set_context_ube(1, scsi_host, SIG_SCSI_16BIT_BUS, 0x02);
+       //dma->set_context_ack(1, scsi_host, SIG_SCSI_ACK, 0xffffffff);
+       //dma->set_context_ack(3, cdrom, SIG_TOWNS_CDROM_DMAACK, 0xffffffff);
        dma->set_context_child_dma(extra_dma);
-       
+
        floppy->set_context_fdc(fdc);
-       
-       sprite->set_context_vram(vram); 
-       sprite->set_context_font(fontrom);
+
+       sprite->set_context_vram(vram);
+//     sprite->set_context_font(fontrom);
+//     sprite->set_context_crtc(crtc);
 #ifdef USE_DEBUGGER
        sprite->set_context_debugger(new DEBUGGER(this, emu));
 #endif
-       
-       vram->set_context_sprite(sprite);
-       vram->set_context_crtc(crtc);
-       
+
+       planevram->set_context_vram(vram);
+       planevram->set_context_sprite(sprite);
+       planevram->set_context_crtc(crtc);
+
        crtc->set_context_sprite(sprite);
        crtc->set_context_vram(vram);
        crtc->set_context_font(fontrom);
 
-       //e_volume[0]->set_context_ch0(line_in, MB87878_VOLUME_LEFT);
-       //e_volume[0]->set_context_ch1(line_in, MB87878_VOLUME_RIGHT);
-       //e_volume[0]->set_context_ch2(NULL, MB87878_VOLUME_LEFT);
-       //e_volume[0]->set_context_ch3(NULL, MB87878_VOLUME_RIGHT);
-//     e_volume[1]->set_context_ch0(cdc, MB87878_VOLUME_LEFT);
-//     e_volume[1]->set_context_ch1(cdc, MB87878_VOLUME_RIGHT);
-       //e_volume[1]->set_context_ch2(mic, MB87878_VOLUME_LEFT | MB87878_VOLUME_RIGHT);
-       //e_volume[1]->set_context_ch3(modem, MB87878_VOLUME_LEFT | MB87878_VOLUME_RIGHT);
-       
+//     e_volumes[0]->set_context_device(0, line_in, 0,
+//                                                                      MB87078_TYPE_MASK_LEFT);
+//     e_volumes[0]->set_context_device(1, line_in, 0,
+//                                                                      MB87078_TYPE_MASK_RIGHT);
+       e_volumes[1]->set_context_device(0, cdrom, 0,
+                                                                        MB87078_TYPE_SET_LEFT, SIG_TOWNS_CDROM_MUTE_L,
+                                                                        0xffffffff,
+                                                                        0xffffffff,
+                                                                        false
+               );
+       e_volumes[1]->set_context_device(1, cdrom, 0,
+                                                                        MB87078_TYPE_SET_RIGHT, SIG_TOWNS_CDROM_MUTE_R,
+                                                                        0xffffffff,
+                                                                        0xffffffff,
+                                                                        false
+               );
+
        memory->set_context_cpu(cpu);
        memory->set_context_dmac(dma);
        memory->set_context_vram(vram);
+       memory->set_context_planevram(planevram);
        memory->set_context_crtc(crtc);
        memory->set_context_system_rom(sysrom);
        memory->set_context_msdos(msdosrom);
        memory->set_context_dictionary(dictionary);
+       memory->set_context_cmos(cmos);
        memory->set_context_font_rom(fontrom);
-       memory->set_context_beep(beep);
+       memory->set_context_timer(timer);
        memory->set_context_serial_rom(serialrom);
        memory->set_context_sprite(sprite);
-       memory->set_context_pcm(rf5c68);
-       
-       cdrom->scsi_id = 0;
-       cdrom->set_context_interface(cdc_scsi);
-       cdrom->set_context_completed(cdc, SIG_TOWNS_CDC_TRANSFER_COMPLETE, 0xffffffff);
-       //cdrom->set_context_next_sector(cdc, SIG_TOWNS_CDC_NEXT_SECTOR, 0xffffffff);
-       //cdrom->set_context_done(cdc, SIG_TOWNS_CDC_TRANSFER_COMPLETE, 1);
-       
-       cdc->set_context_scsi_host(cdc_scsi);
-       cdc_scsi->set_context_target(cdrom);
-       
-       cdc->set_context_cdrom(cdrom);
-       cdc->set_context_dmac(dma);
-       cdc_scsi->set_device_name(_T("CDROM controller's PSEUDO SCSI"));
-       
+       memory->set_context_pcm(adpcm);
+       memory->set_context_iccard(iccard1, 0);
+       memory->set_context_iccard(iccard2, 1);
+
        adpcm->set_context_opn2(opn2);
        adpcm->set_context_rf5c68(rf5c68);
        adpcm->set_context_adc(adc);
 
        rf5c68->set_context_interrupt_boundary(adpcm, SIG_ADPCM_WRITE_INTERRUPT, 0xffffffff);
+#ifdef USE_DEBUGGER
+       rf5c68->set_context_debugger(new DEBUGGER(this, emu));
+#endif
        opn2->set_context_irq(adpcm, SIG_ADPCM_OPX_INTR, 0xffffffff);
-       
+
        adc->set_sample_rate(19200);
        adc->set_sound_bank(-1);
-       adc->set_context_interrupt(adpcm, SIG_ADPCM_ADC_INTR, 0xffffffff); 
-       
+       adc->set_context_interrupt(adpcm, SIG_ADPCM_ADC_INTR, 0xffffffff);
+
        scsi->set_context_dma(dma);
        scsi->set_context_host(scsi_host);
        scsi->set_context_pic(pic);
        timer->set_context_pcm(beep);
        timer->set_context_rtc(rtc);
-       timer->set_context_halt_line(cpu, SIG_CPU_HALTREQ, 0xffffffff);
-       
+       //timer->set_context_halt_line(cpu, SIG_CPU_HALTREQ, 0xffffffff);
+       timer->set_context_halt_line(cpu, SIG_CPU_BUSREQ, 0xffffffff);
+
+       for(int i = 0; i < 2; i++) {
+               // type =
+               // 0: Towns PAD 2buttons
+               // 1: Towns PAD 6buttons
+               // 2: Towns MOUSE
+               // 3: Analog Pad (reserved)
+               // 4: Libble Rabble stick (reserved)
+               joystick->set_context_joystick(i, joypad_2btn[i]);
+               joystick->set_context_joystick(i, joypad_6btn[i]);
+               joystick->set_context_joystick(i, mouse[i]);
+       }
+#ifdef USE_DEBUGGER
+       joystick->set_context_debugger(new DEBUGGER(this, emu));
+#endif
+       // ToDo: Selective by config.
+       for(int i = 0; i < 2; i++) {
+               joypad_2btn[i]->set_context_pad_num(i);
+               joypad_2btn[i]->set_context_parent_port(i, joystick, 0, 0xff);
+               joypad_2btn[i]->set_negative_logic(true);
+               joypad_2btn[i]->set_enable(true);
+       }
+       for(int i = 0; i < 2; i++) {
+               joypad_6btn[i]->set_context_pad_num(i);
+               joypad_6btn[i]->set_context_parent_port(i, joystick, 0, 0xff);
+               joypad_6btn[i]->set_negative_logic(true);
+               joypad_6btn[i]->set_enable(false);
+       }
+       for(int i = 0; i < 2; i++) {
+               mouse[i]->set_context_pad_num(i);
+               mouse[i]->set_context_parent_port(i, joystick, 0, 0xff);
+               mouse[i]->set_negative_logic(true);
+               mouse[i]->set_enable(false);
+       }
+       joystick->set_using_pad(0, -1);
+       joystick->set_using_pad(1, -1);
+
        // cpu bus
        cpu->set_context_mem(memory);
        cpu->set_context_io(io);
        cpu->set_context_intr(pic);
+#ifdef SINGLE_MODE_DMA
        cpu->set_context_dma(dma);
-       cpu->set_context_bios(NULL);
+#endif
+       cpu->set_context_bios(nullptr);
        cpu->set_context_extreset(memory, SIG_FMTOWNS_NOTIFY_RESET, 0xffffffff);
 #ifdef USE_DEBUGGER
        cpu->set_context_debugger(new DEBUGGER(this, emu));
@@ -404,45 +518,58 @@ VM::VM(EMU* parent_emu) : VM_TEMPLATE(parent_emu)
        timer->set_context_intr_line(pic, SIG_I8259_CHIP0 | SIG_I8259_IR0, 0xffffffff);
        keyboard->set_context_intr_line(pic, SIG_I8259_CHIP0 | SIG_I8259_IR1, 0xffffffff);
        floppy->set_context_intr_line(pic, SIG_I8259_CHIP0 | SIG_I8259_IR6, 0xffffffff);
-       
+
        // IRQ8  : SCSI (-> scsi.cpp)
-       // IRQ9  : CDC
+       // IRQ9  : CDC/CDROM
        // IRQ10 : EXTRA I/O (Maybe not implement)
        // IRQ11 : VSYNC
        // IRQ12 : PRINTER (ToDo)
        // IRQ13 : ADPCM AND OPN2 (Route to adpcm.cpp)
        // IRQ14 : EXTRA I/O (Maybe not implement)
        // IRQ15 : RESERVED.
-       cdc->set_context_dmaint_line(pic, SIG_I8259_CHIP1 | SIG_I8259_IR1, 0xffffffff);
-       cdc->set_context_mpuint_line(pic, SIG_I8259_CHIP1 | SIG_I8259_IR1, 0xffffffff);
+       cdrom->set_context_mpuint_line(pic, SIG_I8259_CHIP1 | SIG_I8259_IR1, 0xffffffff);
        crtc->set_context_vsync(pic, SIG_I8259_CHIP1 | SIG_I8259_IR3, 0xffffffff);
+       crtc->set_context_vsync(sprite, SIG_TOWNS_SPRITE_VSYNC, 0xffffffff);
+
        adpcm->set_context_intr_line(pic, SIG_I8259_CHIP1 | SIG_I8259_IR5, 0xffffffff);
 
        // DMA0  : FDC/DRQ
        // DMA1  : SCSI (-> scsi.cpp)
        // DMA2  : PRINTER (ToDo)
-       // DMA3  : CDC
+       // DMA3  : CDC/CDROM
        // EXTRA DMA0 : EXTRA SLOT (Maybe not implement)
        // EXTRA DMA1 : Reserved
        // EXTRA DMA2 : Reserved
        // EXTRA DMA3 : Reserved
        fdc->set_context_drq(dma, SIG_UPD71071_CH0, 1);
-       cdc->set_context_dmareq_line(dma, SIG_UPD71071_CH3, 0xff);
+       fdc->set_context_drq(keyboard, SIG_KEYBOARD_BOOTSEQ_END, 1);
+       cdrom->set_context_drq_line(dma, SIG_UPD71071_CH3, 0xff);
+       cdrom->set_context_drq_line(keyboard, SIG_KEYBOARD_BOOTSEQ_END, 1);
 
        // NMI0 : KEYBOARD (RAS)
        // NMI1 : Extra SLOT (Maybe not implement)
        keyboard->set_context_nmi_line(memory, SIG_CPU_NMI, 0xffffffff);
-       
+
+       cdrom->set_context_dmac(dma);
+       // For Debugging, will remove 20200822 K.O
+       cdrom->set_context_cpu(cpu);
+       //cdrom->set_context_eot_line(dma, SIG_TOWNS_DMAC_EOT_CH3, 0xffffffff);
+
        // i/o bus
+       io->set_iowait_range_rw(0x0000, 0xffff, 6); // ToDo: May variable wait.
+
        io->set_iomap_alias_rw (0x0000, pic, I8259_ADDR_CHIP0 | 0);
        io->set_iomap_alias_rw (0x0002, pic, I8259_ADDR_CHIP0 | 1);
        io->set_iomap_alias_rw (0x0010, pic, I8259_ADDR_CHIP1 | 0);
        io->set_iomap_alias_rw (0x0012, pic, I8259_ADDR_CHIP1 | 1);
-       
-       io->set_iomap_range_rw (0x0020, 0x0025, memory);
-       io->set_iomap_range_rw (0x0026, 0x0027, timer);  // Freerun counter
-       io->set_iomap_single_rw(0x0028, memory);
-       
+
+       io->set_iomap_single_rw(0x0020, memory); // RESET REASON / POWER CONTROL (by software)
+       io->set_iomap_single_rw(0x0022, memory); // POWER CONTROL
+       io->set_iomap_single_rw(0x0024, memory); // MISC3 / DMA WRAP (AFTER MA/MX/ME)
+       io->set_iomap_single_r (0x0025, memory); // MISC4
+       io->set_iomap_range_r  (0x0026, 0x0027, timer);  // Freerun counter
+       io->set_iomap_single_rw(0x0028, memory);         // NMI MASK
+
        io->set_iomap_range_r  (0x0030, 0x0031, memory);        // cpu id / machine id
        io->set_iomap_single_rw(0x0032, memory);        // serial rom (routed from memory)
        io->set_iomap_single_r (0x0034, scsi);  // ENABLE/ UNABLE to WORD DMA for SCSI
@@ -455,145 +582,206 @@ VM::VM(EMU* parent_emu) : VM_TEMPLATE(parent_emu)
        io->set_iomap_alias_rw(0x0052, pit1, 1);
        io->set_iomap_alias_rw(0x0054, pit1, 2);
        io->set_iomap_alias_rw(0x0056, pit1, 3);
-       
+
        io->set_iomap_single_rw(0x0060, timer); // Beep and interrupts register
-       io->set_iomap_single_rw(0x0068, timer); // Interval timer register2 (after Towns 10F).
-       io->set_iomap_single_rw(0x006a, timer); // Interval timer register2 (after Towns 10F).
-       io->set_iomap_single_rw(0x006b, timer); // Interval timer register2 (after Towns 10F).
+       io->set_iomap_single_rw(0x0068, timer); // Interval timer register2 : CONTROL (after Towns 10F).
+       io->set_iomap_range_rw (0x006a, 0x006b, timer); // Interval timer register2 : DATA (after Towns 10F).
        io->set_iomap_single_rw(0x006c, timer); // 1uS wait register (after Towns 10F).
-       
+
        io->set_iomap_single_rw(0x0070, timer); // RTC DATA
        io->set_iomap_single_w (0x0080, timer); // RTC COMMAND
-       
+
        io->set_iomap_range_rw (0x00a0, 0x00af, dma);
        io->set_iomap_range_rw (0x00b0, 0x00bf, extra_dma);
-       
+
+       io->set_iomap_single_rw(0x00c0, memory);   // CACHE CONTROLLER   (after HR, i486)
+       io->set_iomap_single_rw(0x00c2, memory);   // CACHE DIAGNNOSTICS (after HR, i486)
+
        io->set_iomap_alias_rw (0x0200, fdc, 0);  // STATUS/COMMAND
        io->set_iomap_alias_rw (0x0202, fdc, 1);  // TRACK
        io->set_iomap_alias_rw (0x0204, fdc, 2);  // SECTOR
        io->set_iomap_alias_rw (0x0206, fdc, 3);  // DATA
        io->set_iomap_single_rw(0x0208, floppy);  // DRIVE STATUS / DRIVE CONTROL
        io->set_iomap_single_rw(0x020c, floppy);  // DRIVE SELECT
+       io->set_iomap_single_r (0x020d, floppy);  // FDDVEXT (after HG/HR).
        io->set_iomap_single_rw(0x020e, floppy);  // Towns drive SW
-       
-       io->set_iomap_range_rw (0x0400, 0x0404, memory); // System Status
-       io->set_iomap_range_rw (0x0406, 0x403f, memory); // Reserved
-       
-       io->set_iomap_range_rw(0x0440, 0x0443, crtc); // CRTC
-       io->set_iomap_range_rw(0x0448, 0x044f, crtc); // VIDEO OUT (CRTC)
-       
-       io->set_iomap_range_rw(0x0450, 0x0452, sprite); // SPRITE
-       
+
+       io->set_iomap_single_r (0x0400, memory); // RESOLUTION
+       io->set_iomap_single_rw(0x0402, memory); // RESERVED (??)
+       io->set_iomap_single_rw(0x0404, memory); // SYSTEM STATUS
+//     io->set_iomap_range_rw (0x0406, 0x043f, memory); // Reserved
+
+       io->set_iomap_single_rw(0x0440, crtc); // CRTC ADDRESS INDEX
+       io->set_iomap_range_rw (0x0442, 0x0443, crtc); // CRTC DATA
+       io->set_iomap_single_rw(0x0448, crtc); // VIDEO OUT ADDRESS INDEX
+       io->set_iomap_single_rw(0x044a, crtc); // VIDEO OUT DATA
+       io->set_iomap_single_r (0x044c, crtc); // DIGITAL PALLETTE STATUS, SPRITE STATUS
+
+
+       io->set_iomap_alias_rw(0x0450, sprite, 0); // SPRITE
+       io->set_iomap_alias_rw(0x0452, sprite, 2); // SPRITE
+
        io->set_iomap_single_rw(0x0458, vram);         // VRAM ACCESS CONTROLLER (ADDRESS)
-       io->set_iomap_range_rw (0x045a, 0x045f, vram); // VRAM ACCESS CONTROLLER (DATA)
-       
-       io->set_iomap_single_rw(0x0480, sysrom); //  MEMORY REGISTER
-       io->set_iomap_single_rw(0x0484, dictionary); // Dictionary
-       
-       //io->set_iomap_alias_r(0x48a, memory_card, 0); //
+       io->set_iomap_range_rw (0x045a, 0x045b, vram); // VRAM ACCESS CONTROLLER (DATA)
+
+       //io->set_iomap_single_r (0x0470, crtc); // HIGH RESOLUTION (after MX)
+       //io->set_iomap_single_r (0x0471, vram); // VRAM CAPACITY  (after MX, by MBytes)
+       //io->set_iomap_range_rw (0x0472, 0x0473, crtc); // VIDEO OUT REGS ADDRESS (after MX, by MBytes)
+       //io->set_iomap_range_rw (0x0474, 0x0477, crtc); // VIDEO OUT REGS DATA (after MX, by MBytes)
+
+       io->set_iomap_single_rw(0x0480, memory); //  MEMORY REGISTER
+       io->set_iomap_single_rw(0x0484, dictionary); // Dictionary BANK
+
+       io->set_iomap_alias_r(0x48a, iccard1, 0); //
        //io->set_iomap_alias_rw(0x490, memory_card); // After Towns2
        //io->set_iomap_alias_rw(0x491, memory_card); // After Towns2
-       
-       io->set_iomap_range_rw(0x04c0, 0x04cf, cdc); // CDROM
+
+       //io->set_iomap_single_r(0x4b0, cdrom); // CDROM functions (after MX)
+       io->set_iomap_single_rw(0x04c0, cdrom); // CDROM: MASTER STATUS/MASTER CONTROL
+       io->set_iomap_single_rw(0x04c2, cdrom); // CDROM: STATUS(FIFO) / COMMAND
+       io->set_iomap_single_rw(0x04c4, cdrom); // CDROM: DATA (BUFFER~ / PARAMETER (FIFO)
+       io->set_iomap_single_w (0x04c6, cdrom); // CDROM TRANSFER CONTROL
+       io->set_iomap_single_rw(0x04c8, cdrom); // CDROM CACHE CONTROL (after HR)
+       io->set_iomap_range_r  (0x04cc, 0x04cd, cdrom); // CDROM SUBCODE DATA
        // PAD, Sound
-#if 1
-       io->set_iomap_single_r(0x04d0, joystick); // Pad1
-       io->set_iomap_single_r(0x04d2, joystick); // Pad 2
-       io->set_iomap_single_w(0x04d6, joystick); // Pad out
-#endif 
-       io->set_iomap_single_rw(0x04d5, adpcm); // mute 
+
+       io->set_iomap_single_r (0x04d0, joystick); // Pad1
+       io->set_iomap_single_r (0x04d2, joystick); // Pad 2
+       io->set_iomap_single_rw(0x04d5, adpcm); // mute
+       io->set_iomap_single_w (0x04d6, joystick); // Pad out
+
        // OPN2(YM2612)
        io->set_iomap_alias_rw(0x04d8, opn2, 0); // STATUS(R)/Addrreg 0(W)
        io->set_iomap_alias_w (0x04da, opn2, 1);  // Datareg 0(W)
        io->set_iomap_alias_w (0x04dc, opn2, 2);  // Addrreg 1(W)
        io->set_iomap_alias_w (0x04de, opn2, 3);  // Datareg 1(W)
        // Electrical volume
-//     io->set_iomap_alias_rw(0x04e0, e_volume[0], 0);
-//     io->set_iomap_alias_rw(0x04e1, e_volume[0], 1);
-//     io->set_iomap_alias_rw(0x04e2, e_volume[1], 0);
-//     io->set_iomap_alias_rw(0x04e3, e_volume[1], 1);
+       io->set_iomap_alias_rw(0x04e0, e_volumes[0], 0);
+       io->set_iomap_alias_rw(0x04e1, e_volumes[0], 1);
+       io->set_iomap_alias_rw(0x04e2, e_volumes[1], 0);
+       io->set_iomap_alias_rw(0x04e3, e_volumes[1], 1);
 
        // ADPCM
-       io->set_iomap_range_rw(0x04e7, 0x04ef, adpcm); // A/D SAMPLING DATA REG 
-       io->set_iomap_range_rw(0x04f0, 0x04ff, adpcm); // A/D SAMPLING DATA REG 
+       io->set_iomap_single_r (0x04e7, adpcm); // A/D SAMPLING DATA REG
+       io->set_iomap_single_rw(0x04e8, adpcm); // A/D SAMPLING FLAG
+       io->set_iomap_single_rw(0x04e9, adpcm); // OPN2/PCM INTERRUPT (INT13) REGISTER
+       io->set_iomap_single_rw(0x04ea, adpcm); // PCM INTERRUPT MASK
+       io->set_iomap_single_r (0x04eb, adpcm); // PCM INTERRUPT STATUS
+       io->set_iomap_single_w (0x04ec, adpcm); // PCM LED/MUTE
+       io->set_iomap_range_w (0x04f0, 0x04f8, adpcm); // PCM CONTROL REGS (WO?)
+
+       //io->set_iomap_single_rw(0x510, newpcm); // PCM BANK (after MX)
+       //io->set_iomap_single_rw(0x511, newpcm); // PCM DMA STATUS(after MX)
+       //io->set_iomap_range_rw (0x512, 0x0513, newpcm); // PCM DMA COUNTER(after MX)
+       //io->set_iomap_range_rw (0x514, 0x0517, newpcm); // PCM DMA ADDRESS(after MX)
+       //io->set_iomap_single_rw(0x518, newpcm); // PCM CLOCK (after MX)
+       //io->set_iomap_single_rw(0x519, newpcm); // PCM MODE (after MX)
+       //io->set_iomap_single_rw(0x51a, newpcm); // PCM SYSTEM CONTROL (after MX)
+       //io->set_iomap_single_rw(0x51b, newpcm); // PCM BUFFER STATUS/CONTROL (after MX)
+       //io->set_iomap_single_rw(0x51c, newpcm); // PCM REC/PLAY (after MX)
+       //io->set_iomap_single_rw(0x51d, newpcm); // PCM PEAK MONITOR / TRIGGER (after MX)
+       //io->set_iomap_range_rw (0x51e, 0x051f, newpcm); // PCM LEVEL / SOFTWARE DATA (after MX)
 
        io->set_iomap_single_rw(0x05c0, memory); // NMI MASK
        io->set_iomap_single_r (0x05c2, memory);  // NMI STATUS
-       io->set_iomap_single_r (0x05c8, sprite); // TVRAM EMULATION
+       io->set_iomap_alias_r  (0x05c8, sprite, 8); // TVRAM EMULATION
        io->set_iomap_single_w (0x05ca, crtc); // VSYNC INTERRUPT
-       
-       io->set_iomap_single_rw(0x05e8, memory); // RAM capacity register.(later Towns1H/2H/1F/2F).
+
+       io->set_iomap_single_rw(0x05e0, memory); // Hidden MEMORY WAIT REGISTER from AB.COM (Towns 1/2)
+       io->set_iomap_single_rw(0x05e2, memory); // Hidden MEMORY WAIT REGISTER from AB.COM (After Towns 1H/1F/2H/2F )
+       io->set_iomap_single_rw(0x05e6, memory); // Hidden VRAM WAIT REGISTER from TSUGARU (Maybe after Towns 10H/10F/20H/20F )
+       io->set_iomap_single_r (0x05e8, memory); // RAM capacity register.(later Towns1H/2H/1F/2F).
        io->set_iomap_single_rw(0x05ec, memory); // RAM Wait register , ofcially after Towns2, but exists after Towns1H.
-       
+       io->set_iomap_single_r (0x05ed, memory); // Maximum clock register (After HR/HG).
+       io->set_iomap_single_rw(0x05ee, vram);   // VRAM CACHE CONTROLLER (After HR, i486)
+
        io->set_iomap_single_rw(0x0600, keyboard);
        io->set_iomap_single_rw(0x0602, keyboard);
        io->set_iomap_single_rw(0x0604, keyboard);
+       //io->set_iomap_single_r (0x606, keyboard); // BUFFER FULL (for TUNER)
 
        //io->set_iomap_single_rw(0x0800, printer);
        //io->set_iomap_single_rw(0x0802, printer);
        //io->set_iomap_single_rw(0x0804, printer);
-       
+
        io->set_iomap_alias_rw (0x0a00, sio, 0);
        io->set_iomap_alias_rw (0x0a02, sio, 1);
 //     io->set_iomap_single_r (0x0a04, serial);
 //     io->set_iomap_single_r (0x0a06, serial);
 //     io->set_iomap_single_w (0x0a08, serial);
 //     io->set_iomap_single_rw(0x0a0a, modem);
-       
+//     io->set_iomap_single_rw(0x0a0c, uart_fifo); // USART HAVE FIFO (after MA)
+//     io->set_iomap_single_rw(0x0a0d, uart_fifo); // USART FIFO STATUS (after MA)
+//     io->set_iomap_single_rw(0x0a0e, uart_fifo); // USART FIFO STATUS (after MA)
+
        io->set_iomap_single_rw(0x0c30, scsi);
        io->set_iomap_single_rw(0x0c32, scsi);
+       io->set_iomap_single_r (0x0c34, scsi);
 
-       
-       io->set_iomap_range_rw(0x3000, 0x3fff, dictionary); // CMOS
-       
-       io->set_iomap_range_rw(0xfd90, 0xfda2, crtc);   // Palette and CRTC
-       io->set_iomap_single_rw(0xfda4, memory);        // memory
-       io->set_iomap_range_rw(0xff80, 0xffff, vram);   // MMIO
+       // ToDo: Implement debugging I/Os to 2000h - 2FFFh.
 
-       // Vram allocation may be before initialize().
-       /*
-       bool alloc_failed = false;
-       for(int bank = 0; bank < 2; bank++) {
-               if(alloc_failed) break;
-               for(int layer = 0; layer < 2; layer++) {
-                       d_renderbuffer[bank][layer] = NULL;
-                       renderbuffer_size[bank][layer] = 0;
-                       
-                       uint32_t initial_width = 640;
-                       uint32_t initial_height = 480;
-                       uint32_t initial_stride = 640;
-                       uint32_t __size = initial_stride * initial_height * sizeof(scrntype_t);
-                       scrntype_t *p = (scrntype_t*)malloc(__size);
-                       if(p == NULL) {
-                               alloc_faled = true;
-                               break;
-                       } else {
-                               memset(p, 0x00, __size);
-                               renderbuffer_size[bank][layer] = __size;
-                               d_renderbuffer[bank][layer] = p;
-//                             d_vram->set_context_renderbuffer(p, layer, bank, width, height, stride);
-                       }
-               }
-       }
-       if(alloc_failed) {
-               for(int bank = 0; bank < 2; bank++) {
-                       for(int layer = 0; layer < 2; layer++) {
-                               renderbuffer_size[bank][layer] = 0;
-                               if(d_renderbuffer[bank][layer] != NULL) {
-                                       free((void *)(d_renderbuffer[bank][layer]));
-                               }
-                               d_renderbuffer[bank][layer] = NULL;
-                               d_vram->set_context_renderbuffer(NULL, layer, bank, 0, 0, 0);
-                       }
-               }
+       for(uint32_t addr = 0x3000; addr < 0x4000; addr += 2) {
+               io->set_iomap_single_rw (addr, cmos); // CMOS
        }
-       */
+       io->set_iomap_range_rw (0xfd90, 0xfd91, crtc);  // PALETTE INDEX
+       io->set_iomap_range_rw (0xfd92, 0xfd93, crtc);  // PALETTE DATA BLUE
+       io->set_iomap_range_rw (0xfd94, 0xfd95, crtc);  // PALETTE DATA RED
+       io->set_iomap_single_rw(0xfd96, crtc);  // PALETTE DATA GREEN
+       io->set_iomap_range_rw (0xfd98, 0xfd9f, crtc);  // DIGITAL PALETTE REGISTERS(EMULATED)
+
+       io->set_iomap_single_rw(0xfda0, crtc);  // CRTC: VSYNC, HSYNC / OUTPUT CONTROL
+       io->set_iomap_single_r (0xfda2, crtc);  // CRTC OUT (after UG)
+       io->set_iomap_single_rw(0xfda4, memory);        // CRTC: READ COMPATIBLE (after UG)
+
+       io->set_iomap_range_rw (0xff81, 0xff83, planevram);     // MMIO changed from Tsugaru.
+       io->set_iomap_single_r (0xff84, planevram);     // MMIO
+       io->set_iomap_single_r (0xff86, planevram);     // MMIO
+       io->set_iomap_single_rw(0xff88, memory);        // MMIO
+
+       io->set_iomap_range_rw (0xff94, 0xff99, memory);        // MMIO
+       io->set_iomap_range_r  (0xff9c, 0xff9d, memory);        // MMIO
+       io->set_iomap_single_rw(0xff9e, memory);        // MMIO
+       io->set_iomap_single_rw(0xffa0, planevram);     // MMIO
+
+       // Vram allocation may be before initialize().
        // initialize all devices
 #if defined(__GIT_REPO_VERSION)
-       strncpy(_git_revision, __GIT_REPO_VERSION, sizeof(_git_revision) - 1);
+       set_git_repo_version(__GIT_REPO_VERSION);
 #endif
        // ToDo : Use config framework
-       memory->set_extra_ram_size(6);
+       int exram_size = config.current_ram_size;
+       if(exram_size < 1) {
+               switch((machine_id & 0xff00) >> 8) {
+               case 0x00:
+               case 0x01: // Towns 1/2 : Not Supported.
+                       exram_size = 2; // UP TO 5MB.
+                       break;
+               case 0x03: // TOWNS2 UX
+               case 0x06: // TOWNS2 UG
+                       exram_size = 4; // UP TO 9MB.
+                       break;
+               case 0x02: // TOWNS 2F/2H
+               case 0x04: // TOWNS 10F/10H/20F/20H
+                       exram_size = 6; // MAYBE UP TO 7MB.
+                       break;
+               case 0x05: // TOWNS II CX
+               case 0x08: // TOWNS II HG
+                       exram_size = 8; // UP TO 15MB.
+                       break;
+               case 0x07: // Towns II HR
+               case 0x09: // Towns II UR
+                       exram_size = 24; // UP TO 31MB.
+                       break;
+               default:
+                       exram_size = 31; // UP TO 127MB.
+                       break;
+               }
+       }
+       if(exram_size < MIN_RAM_SIZE) {
+               exram_size = MIN_RAM_SIZE;
+       }
+
+       memory->set_extra_ram_size(exram_size);
 
 #if defined(WITH_I386SX)
        cpu->device_model = INTEL_80386;
@@ -606,62 +794,60 @@ VM::VM(EMU* parent_emu) : VM_TEMPLATE(parent_emu)
 #else
        // I386
        cpu->device_model = INTEL_80386;
-#endif 
+#endif
 
-       for(DEVICE* device = first_device; device; device = device->next_device) {
-               device->initialize();
-       }
+       initialize_devices();
 //     cpu->set_address_mask(0xffffffff);
 }
 
 VM::~VM()
 {
        // delete all devices
-       for(DEVICE* device = first_device; device;) {
-               DEVICE *next_device = device->next_device;
-//             printf("DEVID=%d\n", device->this_device_id);
-               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;
+       release_devices();
 }
 
 void VM::set_machine_type(uint16_t machine_id, uint16_t cpu_id)
 {
-       if(memory != NULL) {
+       if(memory != nullptr) {
                memory->set_cpu_id(cpu_id);
                memory->set_machine_id(machine_id);
        }
-       if(crtc != NULL) {
+       if(crtc != nullptr) {
                crtc->set_cpu_id(cpu_id);
                crtc->set_machine_id(machine_id);
        }
-       if(timer != NULL) {
+       if(timer != nullptr) {
                timer->set_cpu_id(cpu_id);
                timer->set_machine_id(machine_id);
        }
-       if(scsi != NULL) {
+       if(cdrom != nullptr) {
+               cdrom->set_cpu_id(cpu_id);
+               cdrom->set_machine_id(machine_id);
+       }
+       if(scsi != nullptr) {
                scsi->set_cpu_id(cpu_id);
                scsi->set_machine_id(machine_id);
        }
+       if(serialrom != nullptr) {
+               serialrom->set_cpu_id(cpu_id);
+               serialrom->set_machine_id(machine_id);
+       }
+       if(floppy != nullptr) {
+               floppy->set_cpu_id(cpu_id);
+               floppy->set_machine_id(machine_id);
+       }
 #if defined(HAS_20PIX_FONTS)
-       if(fontrom_20pix != NULL) {
+       if(fontrom_20pix != nullptr) {
                fontrom_20pix->set_cpu_id(cpu_id);
                fontrom_20pix->set_machine_id(machine_id);
        }
 #endif
-       
-}              
+       if(vram != nullptr) {
+               vram->set_cpu_id(cpu_id);
+               vram->set_machine_id(machine_id);
+       }
+
+}
 
 
 // ----------------------------------------------------------------------------
@@ -671,15 +857,39 @@ void VM::set_machine_type(uint16_t machine_id, uint16_t cpu_id)
 void VM::reset()
 {
        // reset all devices
-       for(DEVICE* device = first_device; device; device = device->next_device) {
-               device->reset();
-       }
+       boot_seq = false;
+       VM_TEMPLATE::reset();
 //     cpu->set_address_mask(0xffffffff);
 }
 
+void VM::special_reset(int num)
+{
+       boot_seq = true;
+
+       // reset all devices
+       VM_TEMPLATE::reset();
+       //      cpu->set_address_mask(0xffffffff);
+       __LIKELY_IF(keyboard != nullptr) {
+               keyboard->special_reset(num);
+       }
+}
+
 void VM::run()
 {
-       event->drive();
+       __LIKELY_IF(event != nullptr) {
+               event->drive();
+       }
+}
+void VM::process_boot_sequence(uint32_t val)
+{
+       if(boot_seq) {
+               if(val != 0) {
+                       if(keyboard != nullptr) {
+                               keyboard->write_signal(SIG_KEYBOARD_BOOTSEQ_END, 0xffffffff, 0xffffffff);
+                       }
+                       boot_seq = false;
+               }
+       }
 }
 
 // ----------------------------------------------------------------------------
@@ -692,7 +902,7 @@ DEVICE *VM::get_cpu(int index)
        if(index == 0) {
                return cpu;
        }
-       return NULL;
+       return nullptr;
 }
 #endif
 
@@ -702,13 +912,11 @@ DEVICE *VM::get_cpu(int index)
 
 void VM::draw_screen()
 {
-       crtc->draw_screen();
+       __LIKELY_IF(crtc != nullptr) {
+               crtc->draw_screen();
+       }
 }
 
-uint32_t VM::is_floppy_disk_accessed()
-{
-       return fdc->read_signal(0);
-}
 
 // ----------------------------------------------------------------------------
 // soud manager
@@ -716,26 +924,41 @@ uint32_t VM::is_floppy_disk_accessed()
 
 void VM::initialize_sound(int rate, int samples)
 {
-       emu->lock_vm();
+//     emu->lock_vm();
        // init sound manager
        event->initialize_sound(rate, samples);
-       
+
        // init sound gen
        beep->initialize_sound(rate, 8000);
 
        // init OPN2
+       #if 0
        // MASTER CLOCK MAYBE 600KHz * 12 = 7200KHz .
        // From FM-Towns Technical Databook (Rev.2), Page 201
-       opn2->initialize_sound(rate, (int)(600.0e3 * 12.0) , samples, 0.0, 0.0); 
-
+       // opn2->initialize_sound(rate, (int)(600.0e3 * 12.0) , samples, 0.0, 0.0);
+       #else
+       // But...20230330 K.O:
+       // From src/ym2612/ym2612.h of Tsugaru below quote.
+       // Thanks to YS-11 San.
+       //
+       // What exactly is the master clock given to YM2612 of TOWNS?
+       // FM Towns Technical Databook tells internal clock is 600KHz on page 201.
+       // However, after calibrating the tone to match real Towns, it must be 690KHz.  Is it correct?
+       // Master clock must be the internal clock times pre scaler.  But, we don't know the default pre scalar value.
+       // Let's say it is 3.  Then, 690KHz times 3=2070KHz.  Sounds reasonable.
+       // But, now FM77AV's YM2203C uses master clock frequency of 1228.8KHz.
+       // And after calibration we know the ratio between the two.
+       // From there, 1228.8*1698/1038=1999.46.  2MHz.  Makes more sense.
+       opn2->initialize_sound(rate, (int)(((1228.8e3 * 1698.0) / 1038.0) * 4.0) , samples, 0.0, 0.0);
+       #endif
        // init PCM
        rf5c68->initialize_sound(rate, samples);
-       
+
        // add_sound_in_source() must add after per initialize_sound().
        adc_in_ch = event->add_sound_in_source(rate, samples, 2);
        adc->set_sample_rate(19200);
        adc->set_sound_bank(adc_in_ch);
-#if 0  
+#if 0
        mixer->set_context_out_line(adc_in_ch);
        mixer->set_context_sample_out(adc_in_ch, rate, samples); // Must be 2ch.
        // ToDo: Check recording sample rate & channels.
@@ -745,21 +968,29 @@ void VM::initialize_sound(int rate, int samples)
        line_in_ch = event->add_sound_in_source(rate, samples, 2);
        mixer->set_context_line_in(line_in_ch, rate, samples);
 #endif
-       emu->unlock_vm();
+//     emu->unlock_vm();
 }
 
 uint16_t* VM::create_sound(int* extra_frames)
 {
-       return event->create_sound(extra_frames);
+       __LIKELY_IF(event != nullptr) {
+               return event->create_sound(extra_frames);
+       }
+       return VM_TEMPLATE::create_sound(extra_frames);
 }
 
 int VM::get_sound_buffer_ptr()
 {
-       return event->get_sound_buffer_ptr();
+       __LIKELY_IF(event != nullptr) {
+               return event->get_sound_buffer_ptr();
+       }
+       return VM_TEMPLATE::get_sound_buffer_ptr();
 }
 
 void VM::clear_sound_in()
 {
+       __UNLIKELY_IF(event == nullptr) return;
+
        event->clear_sound_in_source(adc_in_ch);
        event->clear_sound_in_source(mic_in_ch);
        event->clear_sound_in_source(line_in_ch);
@@ -768,7 +999,7 @@ void VM::clear_sound_in()
 
 int VM::get_sound_in_data(int ch, int32_t* dst, int expect_samples, int expect_rate, int expect_channels)
 {
-       if(dst == NULL) return 0;
+       if(dst == nullptr) return 0;
        if(expect_samples <= 0) return 0;
        int n_ch = -1;
        switch(ch) {
@@ -783,7 +1014,10 @@ int VM::get_sound_in_data(int ch, int32_t* dst, int expect_samples, int expect_r
                break;
        }
        if(n_ch < 0) return 0;
-       int samples = event->get_sound_in_data(n_ch, dst, expect_samples, expect_rate, expect_channels);
+       int samples = 0;
+       if(event != nullptr) {
+               samples = event->get_sound_in_data(n_ch, dst, expect_samples, expect_rate, expect_channels);
+       }
        return samples;
 }
 
@@ -806,11 +1040,10 @@ int VM::sound_in(int ch, int32_t* src, int samples)
        if(n_ch < 0) return 0;
 
        int ss = 0;
-       {
+       if(event != nullptr) {
                emu->lock_vm();
                ss =  event->write_sound_in_buffer(n_ch, src, samples);
                emu->unlock_vm();
-
        }
        return ss;
 }
@@ -819,7 +1052,7 @@ int VM::sound_in(int ch, int32_t* src, int samples)
 void VM::open_hard_disk(int drv, const _TCHAR* file_path)
 {
        if((drv < USE_HARD_DISK) && (drv < 8) && (drv >= 0)) {
-               if(scsi_hdd[drv] != NULL) {
+               if(scsi_hdd[drv] != nullptr) {
                        scsi_hdd[drv]->open(0, file_path, 512);
                }
        }
@@ -828,7 +1061,7 @@ void VM::open_hard_disk(int drv, const _TCHAR* file_path)
 void VM::close_hard_disk(int drv)
 {
        if((drv < USE_HARD_DISK) && (drv < 8) && (drv >= 0)) {
-               if(scsi_hdd[drv] != NULL) {
+               if(scsi_hdd[drv] != nullptr) {
                        scsi_hdd[drv]->close(0);
                }
        }
@@ -837,7 +1070,7 @@ void VM::close_hard_disk(int drv)
 bool VM::is_hard_disk_inserted(int drv)
 {
        if((drv < USE_HARD_DISK) && (drv < 8) && (drv >= 0)) {
-               if(scsi_hdd[drv] != NULL) {
+               if(scsi_hdd[drv] != nullptr) {
                        return scsi_hdd[drv]->mounted(0);
                }
        }
@@ -847,36 +1080,44 @@ bool VM::is_hard_disk_inserted(int drv)
 uint32_t VM::is_hard_disk_accessed()
 {
        uint32_t status = 0;
-       
+
        for(int drv = 0; drv < USE_HARD_DISK; drv++) {
-               if(scsi_hdd[drv] != NULL) {
+               if(scsi_hdd[drv] != nullptr) {
                        if(scsi_hdd[drv]->accessed(0)) {
                                status |= 1 << drv;
                        }
                }
        }
+       process_boot_sequence(status);
        return status;
 }
 #endif // USE_HARD_DISK
 
 void VM::open_compact_disc(int drv, const _TCHAR* file_path)
 {
-       cdrom->open(file_path);
+       if(cdrom != nullptr) {
+               cdrom->open(file_path);
+       }
 }
 
 void VM::close_compact_disc(int drv)
 {
-       cdrom->close();
+       if(cdrom != nullptr) {
+               cdrom->close();
+       }
 }
 
 bool VM::is_compact_disc_inserted(int drv)
 {
+       if(cdrom == nullptr) return false;
        return cdrom->mounted();
 }
 
 uint32_t VM::is_compact_disc_accessed()
 {
-       return cdrom->accessed();
+       uint32_t status = cdrom->accessed();
+       process_boot_sequence(status);
+       return status;
 }
 
 #ifdef USE_SOUND_VOLUME
@@ -894,61 +1135,39 @@ void VM::set_sound_device_volume(int ch, int decibel_l, int decibel_r)
 #ifndef HAS_2ND_ADPCM
 //     if(ch >= 10) ch++;
 #endif
-#if 0  
        if(ch == 0) { // BEEP
-               mixer->set_volume(beep_mix_ch, decibel_l, decibel_r);
+               if(beep != nullptr) {
+                       beep->set_volume(0, decibel_l, decibel_r);
+               }
        }
        else if(ch == 1) { // CD-ROM
-               //e_volume[1]->set_volume(0, decibel_l);
-               //e_volume[1]->set_volume(1, decibel_r);
-               mixer->set_volume(cdc_mix_ch, decibel_l, decibel_r);
-       }
-       else if(ch == 2) { // OPN2
-               mixer->set_volume(opn2_mix_ch, decibel_l, decibel_r);
-       }
-       else if(ch == 3) { // ADPCM
-               mixer->set_volume(pcm_mix_ch, decibel_l, decibel_r);
-       }
-       else if(ch == 4) { // LINE IN
-               //mixer->set_volume(line_mix_ch, decibel_l, decibel_r);
-       } 
-       else if(ch == 5) { // MIC
-               //mic->set_volume(0, (decibel_l + decibel_r) / 2);
-       } 
-       else if(ch == 6) { // MODEM
-               //modem->set_volume(0, (decibel_l + decibel_r) / 2);
-       }
-#ifdef HAS_2ND_ADPCM
-       else if(ch == 7) { // ADPCM
-               adpcm2->set_volume(0, decibel_l, decibel_r);
-       }
-#endif
-       else if(ch == 8) { // FDD
-               fdc->set_volume(0, decibel_l, decibel_r);
-       }
-       else if(ch == 9) { // HDD(ToDo)
-               fdc->set_volume(0, decibel_l, decibel_r);
-       }       
-#else
-       if(ch == 0) { // BEEP
-               beep->set_volume(0, decibel_l, decibel_r);
+               if(e_volumes[1] != nullptr) {
+                       e_volumes[1]->set_volumes(0, decibel_l, 1, decibel_r);
+               } else if(cdrom != nullptr) {
+                       cdrom->set_volume(0, decibel_l, decibel_r);
+               }
        }
-       else if(ch == 1) { // CD-ROM
-               cdrom->set_volume(0, decibel_l, decibel_r);
-       }       
        else if(ch == 2) { // OPN2
-               opn2->set_volume(0, decibel_l, decibel_r);
+               if(opn2 != nullptr) {
+                       opn2->set_volume(0, decibel_l, decibel_r);
+               }
        }
        else if(ch == 3) { // ADPCM
-               rf5c68->set_volume(0, decibel_l, decibel_r);
+               if(rf5c68 != nullptr) {
+                       rf5c68->set_volume(0, decibel_l, decibel_r);
+               }
        }
        else if(ch == 4) { // SEEK, HEAD UP / DOWN
-               seek_sound->set_volume(0, decibel_l, decibel_r);
-               head_up_sound->set_volume(0, decibel_l, decibel_r);
-               head_down_sound->set_volume(0, decibel_l, decibel_r);
+               if(seek_sound != nullptr) {
+                       seek_sound->set_volume(0, decibel_l, decibel_r);
+               }
+               if(head_up_sound != nullptr) {
+                       head_up_sound->set_volume(0, decibel_l, decibel_r);
+               }
+               if(head_down_sound != nullptr) {
+                       head_down_sound->set_volume(0, decibel_l, decibel_r);
+               }
        }
-       
-#endif
 }
 #endif
 
@@ -958,92 +1177,154 @@ void VM::set_sound_device_volume(int ch, int decibel_l, int decibel_r)
 
 void VM::key_down(int code, bool repeat)
 {
-       keyboard->key_down(code);
+       __LIKELY_IF(keyboard != nullptr) {
+               keyboard->key_down(code);
+       }
 }
 
 void VM::key_up(int code)
 {
-       keyboard->key_up(code);
+       __LIKELY_IF(keyboard != nullptr) {
+               keyboard->key_up(code);
+       }
 }
 
 // ----------------------------------------------------------------------------
 // user interface
 // ----------------------------------------------------------------------------
+void VM::open_cart(int drv, const _TCHAR* file_path)
+{
+       switch(drv) {
+       case 0:
+               if(iccard1 != nullptr) {
+                       iccard1->open_cart(file_path);
+               }
+               break;
+       case 1:
+               if(iccard2 != nullptr) {
+                       iccard2->open_cart(file_path);
+               }
+               break;
+       }
+}
+
+void VM::close_cart(int drv)
+{
+       switch(drv) {
+       case 0:
+               if(iccard1 != nullptr) {
+                       iccard1->close_cart();
+               }
+               break;
+       case 1:
+               if(iccard2 != nullptr) {
+                       iccard2->close_cart();
+               }
+               break;
+       }
+}
+
+bool VM::is_cart_inserted(int drv)
+{
+       switch(drv) {
+       case 0:
+               if(iccard1 != nullptr) {
+                       return iccard1->is_cart_inserted();
+               }
+               break;
+       case 1:
+               if(iccard2 != nullptr) {
+                       return iccard2->is_cart_inserted();
+               }
+               break;
+       }
+       return false;
+}
 
 void VM::open_floppy_disk(int drv, const _TCHAR* file_path, int bank)
 {
-       fdc->open_disk(drv, file_path, bank);
-//     floppy->change_disk(drv);
+
+       if(fdc != nullptr) {
+               fdc->open_disk(drv, file_path, bank);
+               floppy->change_disk(drv);
+       }
 }
 
 void VM::close_floppy_disk(int drv)
 {
-       fdc->close_disk(drv);
+       if(fdc != nullptr) {
+               fdc->close_disk(drv);
+       //      floppy->change_disk(drv);
+       }
+}
+uint32_t VM::is_floppy_disk_accessed()
+{
+       uint32_t val = fdc->read_signal(0);
+       process_boot_sequence(val);
+       return val;
 }
 
 bool VM::is_floppy_disk_inserted(int drv)
 {
-       return fdc->is_disk_inserted(drv);
+       if(fdc != nullptr) {
+               return fdc->is_disk_inserted(drv);
+       }
+       return VM_TEMPLATE::is_floppy_disk_inserted(drv);
 }
 
 void VM::is_floppy_disk_protected(int drv, bool value)
 {
-       fdc->is_disk_protected(drv, value);
+       __LIKELY_IF(fdc != nullptr) {
+               fdc->is_disk_protected(drv, value);
+       }
 }
 
 bool VM::is_floppy_disk_protected(int drv)
 {
-       return fdc->is_disk_protected(drv);
+       __LIKELY_IF(fdc != nullptr) {
+               return fdc->is_disk_protected(drv);
+       }
+       return VM_TEMPLATE::is_floppy_disk_protected(drv);
 }
 
 bool VM::is_frame_skippable()
 {
-       return event->is_frame_skippable();
+       __LIKELY_IF(event != nullptr) {
+               return event->is_frame_skippable();
+       }
+       return VM_TEMPLATE::is_frame_skippable();
 }
 
-void VM::update_config()
+
+double VM::get_current_usec()
 {
-       for(DEVICE* device = first_device; device; device = device->next_device) {
-               device->update_config();
+       __LIKELY_IF(event != nullptr) {
+               return event->get_current_usec();
        }
+       return VM_TEMPLATE::get_current_usec();
 }
 
-#define STATE_VERSION  3
+uint64_t VM::get_current_clock_uint64()
+{
+       __LIKELY_IF(event != nullptr) {
+               return event->get_current_clock_uint64();
+       }
+       return VM_TEMPLATE::get_current_clock_uint64();
+}
+
+#define STATE_VERSION  4
 
 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 decr. 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)) {
-                       return false;
-               }
-               if(!state_fio->StateCheckBuffer(name, len, 1)) {
-                       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(!(VM_TEMPLATE::process_state_core(state_fio, loading, STATE_VERSION))) {
+               return false;
+       }
+
        // Machine specified.
-       state_fio->StateValue(beep_mix_ch);
-       state_fio->StateValue(cdc_mix_ch);
-       state_fio->StateValue(opn2_mix_ch);
-       state_fio->StateValue(pcm_mix_ch);
-       state_fio->StateValue(line_mix_ch);
-       state_fio->StateValue(modem_mix_ch);
-       state_fio->StateValue(mic_mix_ch);
-
-       
+       state_fio->StateValue(boot_seq);
+
+       if(loading) {
+               update_config();
+       }
        return true;
 }
-