OSDN Git Service

[General] Merge Upstream 2018-12-27.
[csp-qt/common_source_project-fm7.git] / source / src / vm / pc6001 / memory.cpp
index f78d49a..ad4d63f 100644 (file)
@@ -21,7 +21,7 @@
        [ memory ]
 */
 
-#include "memory.h"
+#include "./memory.h"
 #include "timer.h"
 
 #define RAM            (MEMORY_BASE + RAM_BASE)
@@ -39,6 +39,8 @@
 #define SYSTEMROM2     (MEMORY_BASE + SYSTEMROM2_BASE)
 #define CGROM6         (MEMORY_BASE + CGROM6_BASE)
 
+namespace PC6001 {
+
 void MEMORY::initialize()
 {
        FILEIO* fio = new FILEIO();
@@ -163,8 +165,8 @@ void MEMORY::initialize()
        palette_pc[15] = RGB_COLOR(0xFF,0xFF,0xFF); // COL080                   = FFFFFF                        ;mk2\81\94\92
        
        // register event
-       register_vline_event(this);
 #endif
+       register_vline_event(this);
 }
 
 void MEMORY::reset()
@@ -251,9 +253,9 @@ void MEMORY::reset()
        portF1 = 0xdd;
        CRTMode1 = CRTMode2 = CRTMode3 = 0;
        CSS3=CSS2=CSS1=0;
-       CGSW93 = CRTKILL = 0;
        CurKANJIROM = KANJIROM;
 #endif
+       CGSW93 = CRTKILL = 0;
 }
 
 void MEMORY::write_data8(uint32_t addr, uint32_t data)
@@ -279,6 +281,68 @@ uint32_t MEMORY::read_data8(uint32_t addr)
        return(RdMem[addr >> 13][addr & 0x1FFF]);
 }
 
+void MEMORY::write_data8w(uint32_t addr, uint32_t data, int *wait)
+{
+#ifdef _PC6001
+       *wait = addr < 0x8000 ? 1 : 0;
+#else
+       bool is_rom;
+       uint32_t portF3 = d_timer->read_io8(0xf3);
+#if defined(_PC6601SR) || defined(_PC6001MK2SR)
+       if (static_cast<VM *>(vm)->sr_mode) {
+               is_rom = (port60[8 + (addr >> 13)] & 0xf0) > 0x20 ? true: false;
+       } else
+#endif
+       {
+               is_rom = EnWrite[addr >> 14] ? false : true;
+       }
+       *wait = is_rom && (portF3 & 0x40) || !is_rom && (portF3 & 0x20) ? 1 : 0;
+#endif
+       write_data8(addr, data);
+}
+
+uint32_t MEMORY::read_data8w(uint32_t addr, int *wait)
+{
+#ifdef _PC6001
+       *wait = addr < 0x8000 ? 1 : 0;
+#else
+       bool is_rom;
+       uint32_t portF3 = d_timer->read_io8(0xf3);
+#if defined(_PC6601SR) || defined(_PC6001MK2SR)
+       if (static_cast<VM *>(vm)->sr_mode) {
+               is_rom = (port60[addr >> 13] & 0xf0) > 0x20 ? true : false;
+       } else
+#endif
+       {
+               if (CGSW93 && 0x6000 <= addr && addr < 0x8000) {
+                       is_rom = true;
+               } else if (addr < 0x4000) {
+                       is_rom = (portF0 & 0x0f) == 0x0d || (portF0 & 0x0f) == 0x0e ? false : true;
+               } else if (addr < 0x8000) {
+                       is_rom = (portF0 & 0xf0) == 0xd0 || (portF0 & 0xf0) == 0xe0 ? false : true;
+               } else if (addr < 0xc000) {
+                       is_rom = (portF1 & 0x0f) == 0x0d || (portF1 & 0x0f) == 0x0e ? false : true;
+               } else {
+                       is_rom = (portF1 & 0xf0) == 0xd0 || (portF1 & 0xf0) == 0xe0 ? false : true;
+               }
+       }
+       *wait = is_rom && (portF3 & 0x40) || !is_rom && (portF3 & 0x20) ? 1 : 0;
+#endif
+       return read_data8(addr);
+}
+
+uint32_t MEMORY::fetch_op(uint32_t addr, int *wait)
+{
+#ifndef _PC6001
+       uint32_t portF3 = d_timer->read_io8(0xf3);
+       if ((portF3 & 0x80) == 0) {
+               return read_data8w(addr, wait);
+       }
+#endif
+       *wait = 1;
+       return read_data8(addr);
+}
+
 void MEMORY::write_io8(uint32_t addr, uint32_t data)
 {
        unsigned int VRAMHead[2][4] = {
@@ -601,6 +665,19 @@ uint32_t MEMORY::read_io8(uint32_t addr)
        }
        return(Value);
 }
+#endif
+
+void MEMORY::write_io8w(uint32_t addr, uint32_t data, int* wait)
+{
+       *wait = (addr & 0xf0) == 0xa0 ? 1 : 0;
+       write_io8(addr, data);
+}
+
+uint32_t MEMORY::read_io8w(uint32_t addr, int* wait)
+{
+       *wait = (addr & 0xf0) == 0xa0 ? 1 : 0;
+       return read_io8(addr);
+}
 
 #define EVENT_HBLANK   1
 
@@ -617,11 +694,18 @@ void MEMORY::event_vline(int v, int clock)
        } else
 #endif
        {
-               if(!CRTKILL) {
-                       if(v < (CRTMode1 ? 200 : 192)) {
+               if (!CRTKILL) {
+#ifdef _PC6001
+                       if (v < 192) {
+                               d_cpu->write_signal(SIG_CPU_BUSREQ, 1, 1);
+                               register_event_by_clock(this, EVENT_HBLANK, (uint64_t)((double)CPU_CLOCKS / FRAMES_PER_SEC / LINES_PER_FRAME * 296 / 455), false, NULL);
+                       }
+#else
+                       if (v < (CRTMode1 ? 200 : 192)) {
                                d_cpu->write_signal(SIG_CPU_BUSREQ, 1, 1);
-                               register_event_by_clock(this, EVENT_HBLANK, CPU_CLOCKS /  FRAMES_PER_SEC / LINES_PER_FRAME * 368 / 456, false, NULL);
+                               register_event_by_clock(this, EVENT_HBLANK, (uint64_t)((double)CPU_CLOCKS / FRAMES_PER_SEC / LINES_PER_FRAME * (CRTMode1 ? 368 : 304) / 456), false, NULL);
                        }
+#endif
                }
        }
 }
@@ -632,7 +716,6 @@ void MEMORY::event_callback(int event_id, int err)
                d_cpu->write_signal(SIG_CPU_BUSREQ, 0, 0);
        }
 }
-#endif
 
 void MEMORY::write_signal(int id, uint32_t data, uint32_t mask)
 {
@@ -649,8 +732,11 @@ void MEMORY::write_signal(int id, uint32_t data, uint32_t mask)
                } else {
                        CGSW93=1; RdMem[3]=CGROM;
                }
-               CRTKILL = (data & 2) ? 0 : 1;
 #endif
+               CRTKILL = (data & 2) ? 0 : 1;
+               if (CRTKILL) {
+                       d_cpu->write_signal(SIG_CPU_BUSREQ, 0, 0);
+               }
        }
 }
 
@@ -685,219 +771,6 @@ void MEMORY::close_cart()
 
 #define STATE_VERSION  1
 
-#include "../../statesub.h"
-
-void MEMORY::decl_state()
-{
-       enter_decl_state(STATE_VERSION);
-       
-       DECL_STATE_ENTRY_1D_ARRAY(&(MEMORY_BASE[RAM_BASE]), RAM_SIZE);
-//     state_fio->FputInt32((int)(CGROM - MEMORY_BASE));
-//     state_fio->FputInt32((int)(EXTROM1 - MEMORY_BASE));
-//     state_fio->FputInt32((int)(EXTROM2 - MEMORY_BASE));
-//     for(int i = 0; i < 8; i++) {
-//             state_fio->FputInt32((int)(RdMem[i] - MEMORY_BASE));
-//             state_fio->FputInt32((int)(WrMem[i] - MEMORY_BASE));
-//     }
-//     state_fio->FputInt32((int)(VRAM - MEMORY_BASE));
-       DECL_STATE_ENTRY_1D_ARRAY(EnWrite, sizeof(EnWrite));
-       DECL_STATE_ENTRY_UINT8(CGSW93);
-       DECL_STATE_ENTRY_BOOL(inserted);
-#ifndef _PC6001
-       DECL_STATE_ENTRY_UINT8(CRTKILL);
-//     DECL_STATE_ENTRY_INT32((int)(CurKANJIROM - MEMORY_BASE));
-       DECL_STATE_ENTRY_UINT8(CRTMode1);
-       DECL_STATE_ENTRY_UINT8(CRTMode2);
-       DECL_STATE_ENTRY_UINT8(CRTMode3);
-       DECL_STATE_ENTRY_UINT8(CSS1);
-       DECL_STATE_ENTRY_UINT8(CSS2);
-       DECL_STATE_ENTRY_UINT8(CSS3);
-       DECL_STATE_ENTRY_UINT8(portF0);
-       DECL_STATE_ENTRY_UINT8(portF1);
-#if defined(_PC6601SR) || defined(_PC6001MK2SR)
-       DECL_STATE_ENTRY_INT32(bitmap);
-       DECL_STATE_ENTRY_INT32(cols);
-       DECL_STATE_ENTRY_INT32(rows);
-       DECL_STATE_ENTRY_INT32(lines);
-//     state_fio->FputInt32((int)(TEXTVRAM - MEMORY_BASE));
-//     state_fio->FputInt32((int)(SYSROM2 - MEMORY_BASE));
-       DECL_STATE_ENTRY_1D_ARRAY(&(MEMORY_BASE[EXTRAM_BASE]), EXTRAM_SIZE);
-       DECL_STATE_ENTRY_1D_ARRAY(port60, sizeof(port60));
-       DECL_STATE_ENTRY_UINT8(portC1);
-       DECL_STATE_ENTRY_UINT8(portC8);
-       DECL_STATE_ENTRY_UINT8(portCA);
-       DECL_STATE_ENTRY_UINT8(portCB);
-       DECL_STATE_ENTRY_UINT8(portCC);
-       DECL_STATE_ENTRY_UINT8(portCE);
-       DECL_STATE_ENTRY_UINT8(portCF);
-       DECL_STATE_ENTRY_1D_ARRAY(palet, sizeof(palet) / sizeof(int));
-#endif
-#endif
-       // Pointer Values
-       DECL_STATE_ENTRY_INT32(tmp_cgrom_ptr);
-       DECL_STATE_ENTRY_INT32(tmp_extrom1_ptr);
-       DECL_STATE_ENTRY_INT32(tmp_extrom2_ptr);
-       {
-               DECL_STATE_ENTRY_1D_ARRAY(tmp_rdmem_ptr, 8);
-               DECL_STATE_ENTRY_1D_ARRAY(tmp_wrmem_ptr, 8);
-       }
-       DECL_STATE_ENTRY_INT32(tmp_vram_ptr);
-#ifndef _PC6001
-       DECL_STATE_ENTRY_INT32(tmp_kanjirom_ptr);
-#if defined(_PC6601SR) || defined(_PC6001MK2SR)
-       DECL_STATE_ENTRY_INT32(tmp_textvram_ptr);
-       DECL_STATE_ENTRY_INT32(tmp_sysrom2_ptr);
-#endif
-#endif
-
-       leave_decl_state();
-}
-
-void MEMORY::save_state(FILEIO* state_fio)
-{
-       tmp_cgrom_ptr = (int)(CGROM - MEMORY_BASE);
-       tmp_extrom1_ptr = (int)(EXTROM1 - MEMORY_BASE);
-       tmp_extrom2_ptr = (int)(EXTROM2 - MEMORY_BASE);
-       for(int i = 0; i < 8; i++) {
-               tmp_rdmem_ptr[i] = (int)(RdMem[i] - MEMORY_BASE);
-               tmp_wrmem_ptr[i] = (int)(WrMem[i] - MEMORY_BASE);
-       }
-       tmp_vram_ptr =(int)(VRAM - MEMORY_BASE);
-#ifndef _PC6001
-       tmp_kanjirom_ptr = (int)(CurKANJIROM - MEMORY_BASE);
-#if defined(_PC6601SR) || defined(_PC6001MK2SR)
-       tmp_textvram_ptr = (int)(TEXTVRAM - MEMORY_BASE);
-       tmp_sysrom2_ptr = (int)(SYSROM2 - MEMORY_BASE);
-#endif
-#endif
-
-       if(state_entry != NULL) {
-               state_entry->save_state(state_fio);
-       }
-//     state_fio->FputUint32(STATE_VERSION);
-//     state_fio->FputInt32(this_device_id);
-       
-//     state_fio->Fwrite(RAM, RAM_SIZE, 1);
-//     state_fio->FputInt32((int)(CGROM - MEMORY_BASE));
-//     state_fio->FputInt32((int)(EXTROM1 - MEMORY_BASE));
-//     state_fio->FputInt32((int)(EXTROM2 - MEMORY_BASE));
-//     for(int i = 0; i < 8; i++) {
-//             state_fio->FputInt32((int)(RdMem[i] - MEMORY_BASE));
-//             state_fio->FputInt32((int)(WrMem[i] - MEMORY_BASE));
-//     }
-//     state_fio->FputInt32((int)(VRAM - MEMORY_BASE));
-//     state_fio->Fwrite(EnWrite, sizeof(EnWrite), 1);
-//     state_fio->FputUint8(CGSW93);
-//     state_fio->FputBool(inserted);
-#ifndef _PC6001
-//     state_fio->FputUint8(CRTKILL);
-//     state_fio->FputInt32((int)(CurKANJIROM - MEMORY_BASE));
-//     state_fio->FputUint8(CRTMode1);
-//     state_fio->FputUint8(CRTMode2);
-//     state_fio->FputUint8(CRTMode3);
-//     state_fio->FputUint8(CSS1);
-//     state_fio->FputUint8(CSS2);
-//     state_fio->FputUint8(CSS3);
-//     state_fio->FputUint8(portF0);
-//     state_fio->FputUint8(portF1);
-#if defined(_PC6601SR) || defined(_PC6001MK2SR)
-//     state_fio->FputInt32(bitmap);
-//     state_fio->FputInt32(cols);
-//     state_fio->FputInt32(rows);
-//     state_fio->FputInt32(lines);
-//     state_fio->FputInt32((int)(TEXTVRAM - MEMORY_BASE));
-//     state_fio->FputInt32((int)(SYSROM2 - MEMORY_BASE));
-//     state_fio->Fwrite(EXTRAM, EXTRAM_SIZE, 1);
-//     state_fio->Fwrite(port60, sizeof(port60), 1);
-//     state_fio->FputUint8(portC1);
-//     state_fio->FputUint8(portC8);
-//     state_fio->FputUint8(portCA);
-//     state_fio->FputUint8(portCB);
-//     state_fio->FputUint8(portCC);
-//     state_fio->FputUint8(portCE);
-//     state_fio->FputUint8(portCF);
-//     state_fio->Fwrite(palet, sizeof(palet), 1);
-#endif
-#endif
-}
-
-bool MEMORY::load_state(FILEIO* state_fio)
-{
-       bool mb = false;
-       if(state_entry != NULL) {
-               mb = state_entry->load_state(state_fio);
-       }
-       if(!mb) {
-               return false;
-       }
-//     if(state_fio->FgetUint32() != STATE_VERSION) {
-//             return false;
-//     }
-//     if(state_fio->FgetInt32() != this_device_id) {
-//             return false;
-//     }
-//     state_fio->Fread(RAM, RAM_SIZE, 1);
-//     CGROM = MEMORY_BASE + state_fio->FgetInt32();
-//     EXTROM1 = MEMORY_BASE + state_fio->FgetInt32();
-//     EXTROM2 = MEMORY_BASE + state_fio->FgetInt32();
-//     for(int i = 0; i < 8; i++) {
-//             RdMem[i] = MEMORY_BASE + state_fio->FgetInt32();
-//             WrMem[i] = MEMORY_BASE + state_fio->FgetInt32();
-//     }
-//     VRAM = MEMORY_BASE + state_fio->FgetInt32();
-//     state_fio->Fread(EnWrite, sizeof(EnWrite), 1);
-//     CGSW93 = state_fio->FgetUint8();
-//     inserted = state_fio->FgetBool();
-//#ifndef _PC6001
-//     CRTKILL = state_fio->FgetUint8();
-//     CurKANJIROM = MEMORY_BASE + state_fio->FgetInt32();
-//     CRTMode1 = state_fio->FgetUint8();
-//     CRTMode2 = state_fio->FgetUint8();
-//     CRTMode3 = state_fio->FgetUint8();
-//     CSS1 = state_fio->FgetUint8();
-//     CSS2 = state_fio->FgetUint8();
-//     CSS3 = state_fio->FgetUint8();
-//     portF0 = state_fio->FgetUint8();
-//     portF1 = state_fio->FgetUint8();
-//#if defined(_PC6601SR) || defined(_PC6001MK2SR)
-//     bitmap = state_fio->FgetInt32();
-//     cols = state_fio->FgetInt32();
-//     rows = state_fio->FgetInt32();
-//     lines = state_fio->FgetInt32();
-//     TEXTVRAM = MEMORY_BASE + state_fio->FgetInt32();
-//     SYSROM2 = MEMORY_BASE + state_fio->FgetInt32();
-//     state_fio->Fread(EXTRAM, EXTRAM_SIZE, 1);
-//     state_fio->Fread(port60, sizeof(port60), 1);
-//     portC1 = state_fio->FgetUint8();
-//     portC8 = state_fio->FgetUint8();
-//     portCA = state_fio->FgetUint8();
-//     portCB = state_fio->FgetUint8();
-//     portCC = state_fio->FgetUint8();
-//     portCE = state_fio->FgetUint8();
-//     portCF = state_fio->FgetUint8();
-//     state_fio->Fread(palet, sizeof(palet), 1);
-//#endif
-//#endif
-
-       // Post Process
-       CGROM = MEMORY_BASE + tmp_cgrom_ptr;
-       EXTROM1 = MEMORY_BASE + tmp_extrom1_ptr;
-       EXTROM2 = MEMORY_BASE + tmp_extrom2_ptr;
-       for(int i = 0; i < 8; i++) {
-               RdMem[i] = MEMORY_BASE + tmp_rdmem_ptr[i];
-               WrMem[i] = MEMORY_BASE + tmp_wrmem_ptr[i];
-       }
-       VRAM = MEMORY_BASE + tmp_vram_ptr;
-#ifndef _PC6001
-       CurKANJIROM = MEMORY_BASE + tmp_kanjirom_ptr;
-#if defined(_PC6601SR) || defined(_PC6001MK2SR)
-       TEXTVRAM = MEMORY_BASE + tmp_textvram_ptr;
-       SYSROM2 = MEMORY_BASE + tmp_sysrom2_ptr;
-#endif
-#endif
-       return true;
-}
-
 bool MEMORY::process_state(FILEIO* state_fio, bool loading)
 {
        if(!state_fio->StateCheckUint32(STATE_VERSION)) {
@@ -906,7 +779,7 @@ bool MEMORY::process_state(FILEIO* state_fio, bool loading)
        if(!state_fio->StateCheckInt32(this_device_id)) {
                return false;
        }
-       state_fio->StateBuffer(RAM, RAM_SIZE, 1);
+       state_fio->StateArray(RAM, RAM_SIZE, 1);
        if(loading) {
                CGROM = MEMORY_BASE + state_fio->FgetInt32_LE();
                EXTROM1 = MEMORY_BASE + state_fio->FgetInt32_LE();
@@ -926,29 +799,29 @@ bool MEMORY::process_state(FILEIO* state_fio, bool loading)
                }
                state_fio->FputInt32_LE((int)(VRAM - MEMORY_BASE));
        }
-       state_fio->StateBuffer(EnWrite, sizeof(EnWrite), 1);
-       state_fio->StateUint8(CGSW93);
-       state_fio->StateBool(inserted);
+       state_fio->StateArray(EnWrite, sizeof(EnWrite), 1);
+       state_fio->StateValue(CGSW93);
+       state_fio->StateValue(inserted);
 #ifndef _PC6001
-       state_fio->StateUint8(CRTKILL);
+       state_fio->StateValue(CRTKILL);
        if(loading) {
                CurKANJIROM = MEMORY_BASE + state_fio->FgetInt32_LE();
        } else {
                state_fio->FputInt32_LE((int)(CurKANJIROM - MEMORY_BASE));
        }
-       state_fio->StateUint8(CRTMode1);
-       state_fio->StateUint8(CRTMode2);
-       state_fio->StateUint8(CRTMode3);
-       state_fio->StateUint8(CSS1);
-       state_fio->StateUint8(CSS2);
-       state_fio->StateUint8(CSS3);
-       state_fio->StateUint8(portF0);
-       state_fio->StateUint8(portF1);
+       state_fio->StateValue(CRTMode1);
+       state_fio->StateValue(CRTMode2);
+       state_fio->StateValue(CRTMode3);
+       state_fio->StateValue(CSS1);
+       state_fio->StateValue(CSS2);
+       state_fio->StateValue(CSS3);
+       state_fio->StateValue(portF0);
+       state_fio->StateValue(portF1);
 #if defined(_PC6601SR) || defined(_PC6001MK2SR)
-       state_fio->StateInt32(bitmap);
-       state_fio->StateInt32(cols);
-       state_fio->StateInt32(rows);
-       state_fio->StateInt32(lines);
+       state_fio->StateValue(bitmap);
+       state_fio->StateValue(cols);
+       state_fio->StateValue(rows);
+       state_fio->StateValue(lines);
        if(loading) {
                TEXTVRAM = MEMORY_BASE + state_fio->FgetInt32_LE();
                SYSROM2 = MEMORY_BASE + state_fio->FgetInt32_LE();
@@ -956,17 +829,19 @@ bool MEMORY::process_state(FILEIO* state_fio, bool loading)
                state_fio->FputInt32_LE((int)(TEXTVRAM - MEMORY_BASE));
                state_fio->FputInt32_LE((int)(SYSROM2 - MEMORY_BASE));
        }
-       state_fio->StateBuffer(EXTRAM, EXTRAM_SIZE, 1);
-       state_fio->StateBuffer(port60, sizeof(port60), 1);
-       state_fio->StateUint8(portC1);
-       state_fio->StateUint8(portC8);
-       state_fio->StateUint8(portCA);
-       state_fio->StateUint8(portCB);
-       state_fio->StateUint8(portCC);
-       state_fio->StateUint8(portCE);
-       state_fio->StateUint8(portCF);
-       state_fio->StateBuffer(palet, sizeof(palet), 1);
+       state_fio->StateArray(EXTRAM, EXTRAM_SIZE, 1);
+       state_fio->StateArray(port60, sizeof(port60), 1);
+       state_fio->StateValue(portC1);
+       state_fio->StateValue(portC8);
+       state_fio->StateValue(portCA);
+       state_fio->StateValue(portCB);
+       state_fio->StateValue(portCC);
+       state_fio->StateValue(portCE);
+       state_fio->StateValue(portCF);
+       state_fio->StateArray(palet, sizeof(palet), 1);
 #endif
 #endif
        return true;
 }
+
+}