OSDN Git Service

[VM][STATE] Apply new framework to some VMs.
[csp-qt/common_source_project-fm7.git] / source / src / vm / mz3500 / mz3500.cpp
index f91d591..dea738d 100644 (file)
@@ -19,6 +19,7 @@
 #include "../io.h"
 #include "../ls244.h"
 #include "../mz1p17.h"
+#include "../noise.h"
 #include "../not.h"
 #include "../pcm1bit.h"
 #include "../prnfile.h"
 #include "../debugger.h"
 #endif
 
-#include "main.h"
-#include "sub.h"
+#include "./main.h"
+#include "./sub.h"
 #include "keyboard.h"
 
 // ----------------------------------------------------------------------------
 // initialize
 // ----------------------------------------------------------------------------
 
-VM::VM(EMU* parent_emu) : emu(parent_emu)
+VM::VM(EMU* parent_emu) : VM_TEMPLATE(parent_emu)
 {
        // 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"));
+
        // for main cpu
-       io = new IO(this, emu);
+       mainio = new IO(this, emu);
        fdc = new UPD765A(this, emu);
-       cpu = new Z80(this, emu);
-       main = new MAIN(this, emu);
-       
+       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));
+       maincpu = new Z80(this, emu);
+       mainbus= new MAIN(this, emu);
+       mainio->set_device_name(_T("I/O Bus (Main)"));
+       maincpu->set_device_name(_T("Z80 CPU (Main)"));
+       mainbus->set_device_name(_T("MAIN BUS"));
        // for sub cpu
-       if(config.printer_device_type == 0) {
+       if(config.printer_type == 0) {
                printer = new PRNFILE(this, emu);
-       } else if(config.printer_device_type == 1) {
+       } else if(config.printer_type == 1) {
                printer = new MZ1P17(this, emu);
        } else {
                printer = dummy;
@@ -64,40 +71,56 @@ VM::VM(EMU* parent_emu) : emu(parent_emu)
        pit = new I8253(this, emu);
        pio = new I8255(this, emu);
        subio = new IO(this, emu);
+       subio->set_device_name(_T("I/O Bus (Sub)"));
        ls244 = new LS244(this, emu);
+       not_data0 = new NOT(this, emu);
+       not_data0->set_device_name(_T("NOT Gate (Printer Bit0)"));
        not_data1 = new NOT(this, emu);
+       not_data1->set_device_name(_T("NOT Gate (Printer Bit1)"));
        not_data2 = new NOT(this, emu);
+       not_data2->set_device_name(_T("NOT Gate (Printer Bit2)"));
        not_data3 = new NOT(this, emu);
+       not_data3->set_device_name(_T("NOT Gate (Printer Bit3)"));
        not_data4 = new NOT(this, emu);
+       not_data4->set_device_name(_T("NOT Gate (Printer Bit4)"));
        not_data5 = new NOT(this, emu);
+       not_data5->set_device_name(_T("NOT Gate (Printer Bit5)"));
        not_data6 = new NOT(this, emu);
+       not_data6->set_device_name(_T("NOT Gate (Printer Bit6)"));
        not_data7 = new NOT(this, emu);
-       not_data8 = new NOT(this, emu);
+       not_data7->set_device_name(_T("NOT Gate (Printer Bit7)"));
        not_busy = new NOT(this, emu);
+       not_busy->set_device_name(_T("NOT Gate (Printer Busy)"));
        pcm = new PCM1BIT(this, emu);
        rtc = new UPD1990A(this, emu);
        gdc_chr = new UPD7220(this, emu);
+       gdc_chr->set_device_name(_T("uPD7220 GDC (Character)"));
        gdc_gfx = new UPD7220(this, emu);
+       gdc_gfx->set_device_name(_T("uPD7220 GDC (Graphics)"));
        subcpu = new Z80(this, emu);
-       sub = new SUB(this, emu);
+       subcpu->set_device_name(_T("Z80 CPU (Sub)"));
+       subbus = new SUB(this, emu);
        kbd = new KEYBOARD(this, emu);
        
        // set contexts
-       event->set_context_cpu(cpu, CPU_CLOCKS);
+       event->set_context_cpu(maincpu, CPU_CLOCKS);
        event->set_context_cpu(subcpu, CPU_CLOCKS);
        event->set_context_sound(pcm);
+       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());
        
        // mz3500sm p.59
-       fdc->set_context_irq(main, SIG_MAIN_INTFD, 1);
-       fdc->set_context_drq(main, SIG_MAIN_DRQ, 1);
-       fdc->set_context_index(main, SIG_MAIN_INDEX, 1);
+       fdc->set_context_irq(mainbus, SIG_MAIN_INTFD, 1);
+       fdc->set_context_drq(mainbus, SIG_MAIN_DRQ, 1);
+       fdc->set_context_index(mainbus, SIG_MAIN_INDEX, 1);
        
        // mz3500sm p.78
-       if(config.printer_device_type == 0) {
+       if(config.printer_type == 0) {
                PRNFILE *prnfile = (PRNFILE *)printer;
                prnfile->set_context_busy(not_busy, SIG_NOT_INPUT, 1);
                prnfile->set_context_ack(pio, SIG_I8255_PORT_C, 0x40);
-       } else if(config.printer_device_type == 1) {
+       } else if(config.printer_type == 1) {
                MZ1P17 *mz1p17 = (MZ1P17 *)printer;
                mz1p17->mode = MZ1P17_MODE_MZ1;
                mz1p17->set_context_busy(not_busy, SIG_NOT_INPUT, 1);
@@ -120,22 +143,22 @@ VM::VM(EMU* parent_emu) : emu(parent_emu)
        pit->set_constant_clock(2, 2450760);
        
        // mz3500sm p.78,80,81
-       pio->set_context_port_a(not_data1, SIG_NOT_INPUT, 0x01, 0);
-       pio->set_context_port_a(not_data2, SIG_NOT_INPUT, 0x02, 0);
-       pio->set_context_port_a(not_data3, SIG_NOT_INPUT, 0x04, 0);
-       pio->set_context_port_a(not_data4, SIG_NOT_INPUT, 0x08, 0);
-       pio->set_context_port_a(not_data5, SIG_NOT_INPUT, 0x10, 0);
-       pio->set_context_port_a(not_data6, SIG_NOT_INPUT, 0x20, 0);
-       pio->set_context_port_a(not_data7, SIG_NOT_INPUT, 0x40, 0);
-       pio->set_context_port_a(not_data8, SIG_NOT_INPUT, 0x80, 0);
+       pio->set_context_port_a(not_data0, SIG_NOT_INPUT, 0x01, 0);
+       pio->set_context_port_a(not_data1, SIG_NOT_INPUT, 0x02, 0);
+       pio->set_context_port_a(not_data2, SIG_NOT_INPUT, 0x04, 0);
+       pio->set_context_port_a(not_data3, SIG_NOT_INPUT, 0x08, 0);
+       pio->set_context_port_a(not_data4, SIG_NOT_INPUT, 0x10, 0);
+       pio->set_context_port_a(not_data5, SIG_NOT_INPUT, 0x20, 0);
+       pio->set_context_port_a(not_data6, SIG_NOT_INPUT, 0x40, 0);
+       pio->set_context_port_a(not_data7, SIG_NOT_INPUT, 0x80, 0);
        pio->set_context_port_b(rtc, SIG_UPD1990A_STB, 0x01, 0);
        pio->set_context_port_b(rtc, SIG_UPD1990A_C0,  0x02, 0);
        pio->set_context_port_b(rtc, SIG_UPD1990A_C1,  0x04, 0);
        pio->set_context_port_b(rtc, SIG_UPD1990A_C2,  0x08, 0);
        pio->set_context_port_b(rtc, SIG_UPD1990A_DIN, 0x10, 0);
        pio->set_context_port_b(rtc, SIG_UPD1990A_CLK, 0x20, 0);
-       pio->set_context_port_b(main, SIG_MAIN_SRDY, 0x40, 0);
-//     pio->set_context_port_b(sub, SIG_SUB_PIO_PM, 0x80, 0);  // P/M: CG Selection
+       pio->set_context_port_b(mainbus, SIG_MAIN_SRDY, 0x40, 0);
+//     pio->set_context_port_b(subbus, SIG_SUB_PIO_PM, 0x80, 0);       // P/M: CG Selection
        pio->set_context_port_c(kbd, SIG_KEYBOARD_DC, 0x01, 0);
        pio->set_context_port_c(kbd, SIG_KEYBOARD_STC, 0x02, 0);
        pio->set_context_port_c(kbd, SIG_KEYBOARD_ACKC, 0x04, 0);
@@ -146,58 +169,58 @@ VM::VM(EMU* parent_emu) : emu(parent_emu)
        pio->set_context_port_c(ls244, SIG_LS244_INPUT, 0x80, -6);
        
        // mz3500sm p.78
-       not_data1->set_context_out(printer, SIG_PRINTER_DATA, 0x01);
-       not_data2->set_context_out(printer, SIG_PRINTER_DATA, 0x02);
-       not_data3->set_context_out(printer, SIG_PRINTER_DATA, 0x04);
-       not_data4->set_context_out(printer, SIG_PRINTER_DATA, 0x08);
-       not_data5->set_context_out(printer, SIG_PRINTER_DATA, 0x10);
-       not_data6->set_context_out(printer, SIG_PRINTER_DATA, 0x20);
-       not_data7->set_context_out(printer, SIG_PRINTER_DATA, 0x40);
-       not_data8->set_context_out(printer, SIG_PRINTER_DATA, 0x80);
+       not_data0->set_context_out(printer, SIG_PRINTER_DATA, 0x01);
+       not_data1->set_context_out(printer, SIG_PRINTER_DATA, 0x02);
+       not_data2->set_context_out(printer, SIG_PRINTER_DATA, 0x04);
+       not_data3->set_context_out(printer, SIG_PRINTER_DATA, 0x08);
+       not_data4->set_context_out(printer, SIG_PRINTER_DATA, 0x10);
+       not_data5->set_context_out(printer, SIG_PRINTER_DATA, 0x20);
+       not_data6->set_context_out(printer, SIG_PRINTER_DATA, 0x40);
+       not_data7->set_context_out(printer, SIG_PRINTER_DATA, 0x80);
        not_busy->set_context_out(ls244, SIG_LS244_INPUT, 0x04);
        
        // mz3500sm p.80,81
        rtc->set_context_dout(ls244, SIG_LS244_INPUT, 0x01);
        
-       gdc_chr->set_vram_ptr(sub->get_vram_chr(), 0x2000, 0xfff);
-       sub->set_sync_ptr_chr(gdc_chr->get_sync());
-       sub->set_ra_ptr_chr(gdc_chr->get_ra());
-       sub->set_cs_ptr_chr(gdc_chr->get_cs());
-       sub->set_ead_ptr_chr(gdc_chr->get_ead());
+       gdc_chr->set_vram_ptr(subbus->get_vram_chr(), 0x2000, 0xfff);
+       subbus->set_sync_ptr_chr(gdc_chr->get_sync());
+       subbus->set_ra_ptr_chr(gdc_chr->get_ra());
+       subbus->set_cs_ptr_chr(gdc_chr->get_cs());
+       subbus->set_ead_ptr_chr(gdc_chr->get_ead());
        
-       gdc_gfx->set_vram_ptr(sub->get_vram_gfx(), 0x18000);
-       sub->set_sync_ptr_gfx(gdc_gfx->get_sync());
-       sub->set_ra_ptr_gfx(gdc_gfx->get_ra());
-       sub->set_cs_ptr_gfx(gdc_gfx->get_cs());
-       sub->set_ead_ptr_gfx(gdc_gfx->get_ead());
+       gdc_gfx->set_vram_ptr(subbus->get_vram_gfx(), 0x18000);
+       subbus->set_sync_ptr_gfx(gdc_gfx->get_sync());
+       subbus->set_ra_ptr_gfx(gdc_gfx->get_ra());
+       subbus->set_cs_ptr_gfx(gdc_gfx->get_cs());
+       subbus->set_ead_ptr_gfx(gdc_gfx->get_ead());
        
        kbd->set_context_subcpu(subcpu);
        kbd->set_context_ls244(ls244);
        
        // mz3500sm p.23
-       subcpu->set_context_busack(main, SIG_MAIN_SACK, 1);
+       subcpu->set_context_busack(mainbus, SIG_MAIN_SACK, 1);
        
-       main->set_context_cpu(cpu);
-       main->set_context_subcpu(subcpu);
-       main->set_context_fdc(fdc);
+       mainbus->set_context_maincpu(maincpu);
+       mainbus->set_context_subcpu(subcpu);
+       mainbus->set_context_fdc(fdc);
        
-       sub->set_context_main(main);
-       sub->set_ipl(main->get_ipl());
-       sub->set_common(main->get_common());
+       subbus->set_context_main(mainbus);
+       subbus->set_ipl(mainbus->get_ipl());
+       subbus->set_common(mainbus->get_common());
        
        // mz3500sm p.17
-       io->set_iomap_range_rw(0xec, 0xef, main);       // reset int0
-       io->set_iomap_range_rw(0xf4, 0xf7, fdc);        // fdc: f4h,f6h = status, f5h,f7h = data
-       io->set_iomap_range_rw(0xf8, 0xfb, main);       // mfd interface
-       io->set_iomap_range_rw(0xfc, 0xff, main);       // memory mpaper
+       mainio->set_iomap_range_rw(0xec, 0xef, mainbus);        // reset int0
+       mainio->set_iomap_range_rw(0xf4, 0xf7, fdc);            // fdc: f4h,f6h = status, f5h,f7h = data
+       mainio->set_iomap_range_rw(0xf8, 0xfb, mainbus);        // mfd interface
+       mainio->set_iomap_range_rw(0xfc, 0xff, mainbus);        // memory mpaper
        
        // mz3500sm p.18
-       subio->set_iomap_range_w(0x00, 0x0f, sub);      // int0 to main (set flipflop)
+       subio->set_iomap_range_w(0x00, 0x0f, subbus);           // int0 to main (set flipflop)
        subio->set_iomap_range_rw(0x10, 0x1f, sio);
        subio->set_iomap_range_rw(0x20, 0x2f, pit);
        subio->set_iomap_range_rw(0x30, 0x3f, pio);
-       subio->set_iomap_range_r(0x40, 0x4f, ls244);    // input port
-       subio->set_iomap_range_rw(0x50, 0x5f, sub);     // crt control i/o
+       subio->set_iomap_range_r(0x40, 0x4f, ls244);            // input port
+       subio->set_iomap_range_rw(0x50, 0x5f, subbus);          // crt control i/o
        subio->set_iomap_range_rw(0x60, 0x6f, gdc_gfx);
        subio->set_iomap_range_rw(0x70, 0x7f, gdc_chr);
 #ifdef _IO_DEBUG_LOG
@@ -205,26 +228,29 @@ VM::VM(EMU* parent_emu) : emu(parent_emu)
 #endif
        
        // cpu bus
-       cpu->set_context_mem(main);
-       cpu->set_context_io(io);
-       cpu->set_context_intr(main);
+       maincpu->set_context_mem(mainbus);
+       maincpu->set_context_io(mainio);
+       maincpu->set_context_intr(mainbus);
 #ifdef USE_DEBUGGER
-       cpu->set_context_debugger(new DEBUGGER(this, emu));
+       maincpu->set_context_debugger(new DEBUGGER(this, emu));
 #endif
        
-       subcpu->set_context_mem(sub);
+       subcpu->set_context_mem(subbus);
        subcpu->set_context_io(subio);
-       subcpu->set_context_intr(sub);
+       subcpu->set_context_intr(subbus);
 #ifdef USE_DEBUGGER
        subcpu->set_context_debugger(new DEBUGGER(this, emu));
 #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();
        }
        for(int i = 0; i < 4; i++) {
-               fdc->set_drive_type(i, DRIVE_TYPE_2DD);
+               fdc->set_drive_type(i, DRIVE_TYPE_2D);
        }
        // GDC clock (mz3500sm p.33,34)
        if(config.monitor_type == 0 || config.monitor_type == 1) {
@@ -310,7 +336,7 @@ double VM::get_frame_rate()
 DEVICE *VM::get_cpu(int index)
 {
        if(index == 0) {
-               return cpu;
+               return maincpu;
        } else if(index == 1) {
                return subcpu;
        }
@@ -324,13 +350,7 @@ DEVICE *VM::get_cpu(int index)
 
 void VM::draw_screen()
 {
-       sub->draw_screen();
-}
-
-uint32_t VM::get_access_lamp_status()
-{
-       uint32_t status = fdc->read_signal(0);
-       return (status & (1 | 4)) ? 1 : (status & (2 | 8)) ? 2 : 0;
+       subbus->draw_screen();
 }
 
 // ----------------------------------------------------------------------------
@@ -361,6 +381,10 @@ void VM::set_sound_device_volume(int ch, int decibel_l, int decibel_r)
 {
        if(ch == 0) {
                pcm->set_volume(0, decibel_l, decibel_r);
+       } else if(ch == 1) {
+               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
@@ -379,6 +403,16 @@ void VM::key_up(int code)
        kbd->key_up(code);
 }
 
+bool VM::get_caps_locked()
+{
+       return kbd->get_caps_locked();
+}
+
+bool VM::get_kana_locked()
+{
+       return kbd->get_kana_locked();
+}
+
 // ----------------------------------------------------------------------------
 // user interface
 // ----------------------------------------------------------------------------
@@ -408,6 +442,11 @@ 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);
+}
+
 bool VM::is_frame_skippable()
 {
        return event->is_frame_skippable();
@@ -420,29 +459,40 @@ void VM::update_config()
        }
 }
 
-#define STATE_VERSION  3
+#define STATE_VERSION  5
 
-void VM::save_state(FILEIO* state_fio)
+bool VM::process_state(FILEIO* state_fio, bool loading)
 {
-       state_fio->FputUint32(STATE_VERSION);
-       
-       for(DEVICE* device = first_device; device; device = device->next_device) {
-               device->save_state(state_fio);
-       }
-       state_fio->FputUint8(halt);
-}
-
-bool VM::load_state(FILEIO* state_fio)
-{
-       if(state_fio->FgetUint32() != STATE_VERSION) {
+       if(!state_fio->StateCheckUint32(STATE_VERSION)) {
                return false;
        }
-       for(DEVICE* device = first_device; device; device = device->next_device) {
-               if(!device->load_state(state_fio)) {
+       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;
                }
-       }
-       halt = state_fio->FgetUint8();
+               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;
+               }
+       }
+       // Machine specified.
+       state_fio->StateUint8(halt);
        return true;
 }
-