OSDN Git Service

[VM][State] Apply new state framework to some VMs.
[csp-qt/common_source_project-fm7.git] / source / src / vm / babbage2nd / babbage2nd.cpp
index 22c781e..94f0abe 100644 (file)
 #include "../z80ctc.h"
 #include "../z80pio.h"
 
+#ifdef USE_DEBUGGER
+#include "../debugger.h"
+#endif
+
 #include "display.h"
 #include "keyboard.h"
 
@@ -25,7 +29,7 @@
 // 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;
@@ -37,11 +41,19 @@ VM::VM(EMU* parent_emu) : emu(parent_emu)
        cpu = new Z80(this, emu);
        ctc = new Z80CTC(this, emu);
        pio1 = new Z80PIO(this, emu);
+       pio1->set_device_name(_T("Z80 PIO (LEDs)"));
        pio2 = new Z80PIO(this, emu);
+       pio2->set_device_name(_T("Z80 PIO (7-Seg/Keyboard)"));
        
        display = new DISPLAY(this, emu);
        keyboard = new KEYBOARD(this, emu);
-       
+
+       // Set names
+#if defined(_USE_QT)
+       dummy->set_device_name(_T("1st Dummy"));
+       pio1->set_device_name(_T("Z80 PIO(LEDs)"));
+       pio2->set_device_name(_T("Z80 PIO(7SEG/KEYBOARD)"));
+#endif
        // set contexts
        event->set_context_cpu(cpu);
        
@@ -58,6 +70,9 @@ VM::VM(EMU* parent_emu) : emu(parent_emu)
        cpu->set_context_mem(memory);
        cpu->set_context_io(io);
        cpu->set_context_intr(ctc);
+#ifdef USE_DEBUGGER
+       cpu->set_context_debugger(new DEBUGGER(this, emu));
+#endif
        
        // z80 family daisy chain
        ctc->set_context_intr(cpu, 0);
@@ -81,6 +96,9 @@ VM::VM(EMU* parent_emu) : emu(parent_emu)
        io->set_iomap_range_rw(0x20, 0x23, pio2);
        
        // 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();
        }
@@ -125,6 +143,20 @@ void VM::run()
 }
 
 // ----------------------------------------------------------------------------
+// debugger
+// ----------------------------------------------------------------------------
+
+#ifdef USE_DEBUGGER
+DEVICE *VM::get_cpu(int index)
+{
+       if(index == 0) {
+               return cpu;
+       }
+       return NULL;
+}
+#endif
+
+// ----------------------------------------------------------------------------
 // draw screen
 // ----------------------------------------------------------------------------
 
@@ -197,3 +229,43 @@ void VM::update_config()
        }
 }
 
+#define STATE_VERSION  2
+
+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;
+               }
+       }
+       // Machine specified.
+       state_fio->StateBuffer(ram, sizeof(ram), 1);
+       if(loading) {
+               update_config();
+       }
+       return true;
+}