From d9f8396c6712323c3e47b2d2db591d0ea55b6b12 Mon Sep 17 00:00:00 2001
From: retro-wertz <retro-wertz@users.noreply.github.com>
Date: Tue, 6 Aug 2019 00:34:13 +0800
Subject: [PATCH] Cleanup

---
 src/gb/gb.h                           |   7 ++
 src/gb/gbSound.cpp                    |   2 +-
 src/gba/Flash.cpp                     | 134 +++++++++++++-------------
 src/gba/GBA-arm.cpp                   |   1 +
 src/gba/GBA.h                         |   3 +
 src/gba/Sound.cpp                     |   2 +
 src/libretro/Makefile.common          |   3 +-
 src/libretro/libretro.cpp             | 104 ++++++++++----------
 src/libretro/{scrc32.cpp => scrc32.h} |   8 --
 9 files changed, 137 insertions(+), 127 deletions(-)
 rename src/libretro/{scrc32.cpp => scrc32.h} (98%)

diff --git a/src/gb/gb.h b/src/gb/gb.h
index 527c41c4..4ef81090 100644
--- a/src/gb/gb.h
+++ b/src/gb/gb.h
@@ -1,6 +1,11 @@
 #ifndef GB_H
 #define GB_H
 
+#define gbWidth   160
+#define gbHeight  144
+#define sgbWidth  256
+#define sgbHeight 224
+
 const int GB_C_FLAG = 0x10;
 const int GB_H_FLAG = 0x20;
 const int GB_N_FLAG = 0x40;
@@ -55,6 +60,8 @@ void setColorizerHack(bool value);
 bool allowColorizerHack(void);
 
 extern int gbHardware;
+extern int gbRomType; // gets type from header 0x147
+extern int gbBattery; // enabled when gbRamSize != 0
 
 extern struct EmulatedSystem GBSystem;
 
diff --git a/src/gb/gbSound.cpp b/src/gb/gbSound.cpp
index 2420f346..c77f7b89 100644
--- a/src/gb/gbSound.cpp
+++ b/src/gb/gbSound.cpp
@@ -246,6 +246,7 @@ static char dummy_state[735 * 2];
         &name, sizeof(type) \
     }
 
+#ifndef __LIBRETRO__
 // Old save state support
 
 static variable_desc gbsound_format[] = {
@@ -355,7 +356,6 @@ enum {
     nr52
 };
 
-#ifndef __LIBRETRO__
 static void gbSoundReadGameOld(int version, gzFile gzFile)
 {
     if (version == 11) {
diff --git a/src/gba/Flash.cpp b/src/gba/Flash.cpp
index bd14a43e..c3c5585a 100644
--- a/src/gba/Flash.cpp
+++ b/src/gba/Flash.cpp
@@ -27,30 +27,6 @@ int flashDeviceID = 0x1b;
 int flashManufacturerID = 0x32;
 int flashBank = 0;
 
-static variable_desc flashSaveData[] = {
-    { &flashState, sizeof(int) },
-    { &flashReadState, sizeof(int) },
-    { &flashSaveMemory[0], SIZE_FLASH512 },
-    { NULL, 0 }
-};
-
-static variable_desc flashSaveData2[] = {
-    { &flashState, sizeof(int) },
-    { &flashReadState, sizeof(int) },
-    { &flashSize, sizeof(int) },
-    { &flashSaveMemory[0], SIZE_FLASH1M },
-    { NULL, 0 }
-};
-
-static variable_desc flashSaveData3[] = {
-    { &flashState, sizeof(int) },
-    { &flashReadState, sizeof(int) },
-    { &flashSize, sizeof(int) },
-    { &flashBank, sizeof(int) },
-    { &flashSaveMemory[0], SIZE_FLASH1M },
-    { NULL, 0 }
-};
-
 void flashInit()
 {
     memset(flashSaveMemory, 0xff, sizeof(flashSaveMemory));
@@ -63,49 +39,6 @@ void flashReset()
     flashBank = 0;
 }
 
-#ifdef __LIBRETRO__
-void flashSaveGame(uint8_t*& data)
-{
-    utilWriteDataMem(data, flashSaveData3);
-}
-
-void flashReadGame(const uint8_t*& data, int)
-{
-    utilReadDataMem(data, flashSaveData3);
-}
-
-#else // !__LIBRETRO__
-void flashSaveGame(gzFile gzFile)
-{
-    utilWriteData(gzFile, flashSaveData3);
-}
-
-void flashReadGame(gzFile gzFile, int version)
-{
-    if (version < SAVE_GAME_VERSION_5)
-        utilReadData(gzFile, flashSaveData);
-    else if (version < SAVE_GAME_VERSION_7) {
-        utilReadData(gzFile, flashSaveData2);
-        flashBank = 0;
-        flashSetSize(flashSize);
-    } else {
-        utilReadData(gzFile, flashSaveData3);
-    }
-}
-
-void flashReadGameSkip(gzFile gzFile, int version)
-{
-    // skip the flash data in a save game
-    if (version < SAVE_GAME_VERSION_5)
-        utilReadDataSkip(gzFile, flashSaveData);
-    else if (version < SAVE_GAME_VERSION_7) {
-        utilReadDataSkip(gzFile, flashSaveData2);
-    } else {
-        utilReadDataSkip(gzFile, flashSaveData3);
-    }
-}
-#endif
-
 void flashSetSize(int size)
 {
     //  log("Setting flash size to %d\n", size);
@@ -279,3 +212,70 @@ void flashWrite(uint32_t address, uint8_t byte)
         break;
     }
 }
+
+static variable_desc flashSaveData3[] = {
+    { &flashState, sizeof(int) },
+    { &flashReadState, sizeof(int) },
+    { &flashSize, sizeof(int) },
+    { &flashBank, sizeof(int) },
+    { &flashSaveMemory[0], SIZE_FLASH1M },
+    { NULL, 0 }
+};
+
+#ifdef __LIBRETRO__
+void flashSaveGame(uint8_t*& data)
+{
+    utilWriteDataMem(data, flashSaveData3);
+}
+
+void flashReadGame(const uint8_t*& data, int)
+{
+    utilReadDataMem(data, flashSaveData3);
+}
+
+#else // !__LIBRETRO__
+static variable_desc flashSaveData[] = {
+    { &flashState, sizeof(int) },
+    { &flashReadState, sizeof(int) },
+    { &flashSaveMemory[0], SIZE_FLASH512 },
+    { NULL, 0 }
+};
+
+static variable_desc flashSaveData2[] = {
+    { &flashState, sizeof(int) },
+    { &flashReadState, sizeof(int) },
+    { &flashSize, sizeof(int) },
+    { &flashSaveMemory[0], SIZE_FLASH1M },
+    { NULL, 0 }
+};
+
+void flashSaveGame(gzFile gzFile)
+{
+    utilWriteData(gzFile, flashSaveData3);
+}
+
+void flashReadGame(gzFile gzFile, int version)
+{
+    if (version < SAVE_GAME_VERSION_5)
+        utilReadData(gzFile, flashSaveData);
+    else if (version < SAVE_GAME_VERSION_7) {
+        utilReadData(gzFile, flashSaveData2);
+        flashBank = 0;
+        flashSetSize(flashSize);
+    } else {
+        utilReadData(gzFile, flashSaveData3);
+    }
+}
+
+void flashReadGameSkip(gzFile gzFile, int version)
+{
+    // skip the flash data in a save game
+    if (version < SAVE_GAME_VERSION_5)
+        utilReadDataSkip(gzFile, flashSaveData);
+    else if (version < SAVE_GAME_VERSION_7) {
+        utilReadDataSkip(gzFile, flashSaveData2);
+    } else {
+        utilReadDataSkip(gzFile, flashSaveData3);
+    }
+}
+#endif
diff --git a/src/gba/GBA-arm.cpp b/src/gba/GBA-arm.cpp
index 2eea3824..5eb2ba04 100644
--- a/src/gba/GBA-arm.cpp
+++ b/src/gba/GBA-arm.cpp
@@ -1235,6 +1235,7 @@ DEFINE_ALU_INSN_C(1F, 3F, MVNS, YES)
     int mult = (opcode & 0x0F);                                        \
     uint32_t rs = reg[(opcode >> 8) & 0x0F].I;                         \
     int acc = (opcode >> 12) & 0x0F; /* or destLo */                   \
+    maybe_unused(acc);                                                 \
     int dest = (opcode >> 16) & 0x0F; /* or destHi */                  \
     OP;                                                                \
     SETCOND;                                                           \
diff --git a/src/gba/GBA.h b/src/gba/GBA.h
index 8649b2e5..bee3ba19 100644
--- a/src/gba/GBA.h
+++ b/src/gba/GBA.h
@@ -19,6 +19,9 @@ const uint64_t TICKS_PER_SECOND = 16777216;
 #define SAVE_GAME_VERSION_10 10
 #define SAVE_GAME_VERSION SAVE_GAME_VERSION_10
 
+#define gbaWidth  240
+#define gbaHeight 160
+
 enum {
     GBA_SAVE_AUTO = 0,
     GBA_SAVE_EEPROM,
diff --git a/src/gba/Sound.cpp b/src/gba/Sound.cpp
index dba4ce01..d6376ff2 100644
--- a/src/gba/Sound.cpp
+++ b/src/gba/Sound.cpp
@@ -588,6 +588,7 @@ static struct {
     int soundDSBValue;
 } state;
 
+#ifndef __LIBRETRO__
 // Old GBA sound state format
 static variable_desc old_gba_state[] = {
     SKIP(int, soundPaused),
@@ -673,6 +674,7 @@ variable_desc old_gba_state2[] = {
     SKIP(int, sound3ForcedOutput),
     { NULL, 0 }
 };
+#endif
 
 // New state format
 static variable_desc gba_state[] = {
diff --git a/src/libretro/Makefile.common b/src/libretro/Makefile.common
index 9776ba71..38e9ad27 100644
--- a/src/libretro/Makefile.common
+++ b/src/libretro/Makefile.common
@@ -6,8 +6,7 @@ SOURCES_CXX :=
 SOURCES_CXX += \
 	$(CORE_DIR)/libretro/libretro.cpp \
 	$(CORE_DIR)/libretro/UtilRetro.cpp \
-	$(CORE_DIR)/libretro/SoundRetro.cpp \
-	$(CORE_DIR)/libretro/scrc32.cpp
+	$(CORE_DIR)/libretro/SoundRetro.cpp
 
 SOURCES_CXX += \
 	$(CORE_DIR)/apu/Gb_Oscs.cpp \
diff --git a/src/libretro/libretro.cpp b/src/libretro/libretro.cpp
index d77f730f..2b5d7f77 100644
--- a/src/libretro/libretro.cpp
+++ b/src/libretro/libretro.cpp
@@ -8,6 +8,7 @@
 #include "SoundRetro.h"
 #include "libretro.h"
 #include "libretro_core_options.h"
+#include "scrc32.h"
 
 #include "../System.h"
 #include "../Util.h"
@@ -40,27 +41,20 @@ static retro_environment_t environ_cb;
 retro_audio_sample_batch_t audio_batch_cb;
 static retro_set_rumble_state_t rumble_cb;
 
-static char retro_system_directory[4096];
+static char retro_system_directory[2048];
 static char biosfile[4096];
 static float sndFiltering = 0.5f;
 static bool sndInterpolation = true;
 static bool can_dupe = false;
 static bool usebios = false;
 static unsigned retropad_device[4] = {0};
-
 static const double FramesPerSecond =  (16777216.0 / 280896.0); // 59.73
 static const long SampleRate = 32768;
-static const int GBWidth = 160;
-static const int GBHeight = 144;
-static const int SGBWidth = 256;
-static const int SGBHeight = 224;
-static const int GBAWidth = 240;
-static const int GBAHeight = 160;
-static unsigned width = 240;
-static unsigned height = 160;
+static unsigned width = gbaWidth;
+static unsigned height = gbaHeight;
 static EmulatedSystem* core = NULL;
 static IMAGE_TYPE type = IMAGE_UNKNOWN;
-static unsigned current_gbPalette;
+static unsigned current_gbPalette = 0;
 static bool opt_colorizer_hack = false;
 
 uint16_t systemColorMap16[0x10000];
@@ -220,9 +214,6 @@ static void set_gbColorCorrection(int value)
     gbColorOption = value;
 }
 
-extern int gbRomType; // gets type from header 0x147
-extern int gbBattery; // enabled when gbRamSize != 0
-
 static bool gb_hasrtc(void)
 {
     switch (gbRomType) {
@@ -745,13 +736,11 @@ static void load_image_preferences(void)
         "NONE"
     };
 
-    char buffer[5];
-
-    buffer[0] = rom[0xac];
-    buffer[1] = rom[0xad];
-    buffer[2] = rom[0xae];
-    buffer[3] = rom[0xaf];
-    buffer[4] = 0;
+    bool found = false;
+    bool hasRumble = false;
+    char buffer[12];
+    unsigned i = 0, found_no = 0;
+    unsigned long romCrc32 = crc32(0, rom, romSize);
 
     cpuSaveType = GBA_SAVE_AUTO;
     flashSize = SIZE_FLASH512;
@@ -759,12 +748,26 @@ static void load_image_preferences(void)
     rtcEnabled = false;
     mirroringEnable = false;
 
-    log("GameID in ROM is: %s\n", buffer);
+    log("File CRC32      : 0x%08X\n", romCrc32);
 
-    bool found = false;
-    int found_no = 0;
+    buffer[0] = 0;
+    for (i = 0; i < 12; i++) {
+        if (rom[0xa0 + i] == 0)
+            break;
+        buffer[i] = rom[0xa0 + i];
+    }
 
-    for (int i = 0; i < 512; i++) {
+    buffer[i] = 0;
+    log("Game Title      : %s\n", buffer);
+
+    buffer[0] = rom[0xac];
+    buffer[1] = rom[0xad];
+    buffer[2] = rom[0xae];
+    buffer[3] = rom[0xaf];
+    buffer[4] = 0;
+    log("Game Code       : %s\n", buffer);
+
+    for (i = 0; i < 512; i++) {
         if (!strcmp(gbaover[i].romid, buffer)) {
             found = true;
             found_no = i;
@@ -773,7 +776,6 @@ static void load_image_preferences(void)
     }
 
     if (found) {
-        log("Found ROM in vba-over list.\n");
         log("Name            : %s\n", gbaover[found_no].romtitle);
 
         rtcEnabled = gbaover[found_no].rtcEnabled;
@@ -789,12 +791,10 @@ static void load_image_preferences(void)
     }
 
     // gameID that starts with 'F' are classic/famicom games
-    mirroringEnable = (buffer[0] == 0x46) ? true : false;
+    mirroringEnable = (buffer[0] == 'F') ? true : false;
 
-    if (!cpuSaveType) {
-        log("Scrapping ROM for save type.\n");
+    if (!cpuSaveType)
         utilGBAFindSave(romSize);
-    }
 
     saveType = cpuSaveType;
 
@@ -802,7 +802,12 @@ static void load_image_preferences(void)
         flashSetSize(flashSize);
 
     rtcEnable(rtcEnabled);
-    rtcEnableRumble(!rtcEnabled);
+
+    // game code starting with 'R' or 'V' has rumble support
+    if ((buffer[0] == 'R') || (buffer[0] == 'V'))
+        hasRumble = true;
+
+    rtcEnableRumble(!rtcEnabled && hasRumble);
 
     doMirroring(mirroringEnable);
 
@@ -811,7 +816,7 @@ static void load_image_preferences(void)
     log("cpuSaveType     : %s.\n", savetype[cpuSaveType]);
     if (cpuSaveType == 3)
         log("flashSize       : %d.\n", flashSize);
-    if (cpuSaveType == 1)
+    else if (cpuSaveType == 1)
         log("eepromSize      : %d.\n", eepromSize);
     log("mirroringEnable : %s.\n", mirroringEnable ? "Yes" : "No");
 }
@@ -854,8 +859,8 @@ static void gba_init(void)
     }
     CPUInit(biosfile, usebios);
 
-    width = GBAWidth;
-    height = GBAHeight;
+    width = gbaWidth;
+    height = gbaHeight;
 
     CPUReset();
 }
@@ -883,13 +888,13 @@ static void gb_init(void)
     gbCPUInit(biosfile, usebios);
 
     if (gbBorderOn) {
-        width = gbBorderLineSkip = SGBWidth;
-        height = SGBHeight;
-        gbBorderColumnSkip = (SGBWidth - GBWidth) >> 1;
-        gbBorderRowSkip = (SGBHeight - GBHeight) >> 1;
+        width = gbBorderLineSkip = sgbWidth;
+        height = sgbHeight;
+        gbBorderColumnSkip = (sgbWidth - gbWidth) >> 1;
+        gbBorderRowSkip = (sgbHeight - gbHeight) >> 1;
     } else {
-        width = gbBorderLineSkip = GBWidth;
-        height = GBHeight;
+        width = gbBorderLineSkip = gbWidth;
+        height = gbHeight;
         gbBorderColumnSkip = gbBorderRowSkip = 0;
     }
 
@@ -1171,7 +1176,7 @@ static void update_variables(bool startup)
 
     if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
     {
-        int lastpal = current_gbPalette;
+        unsigned lastpal = current_gbPalette;
 
         if (!strcmp(var.value, "black and white"))
             current_gbPalette = 0;
@@ -1443,6 +1448,7 @@ void retro_cheat_set(unsigned index, bool enabled, const char* code)
                     memset(codeLine, 0, codeLineSize);
                 }
                 break;
+            default: break;
             }
             if (!code[cursor])
                 break;
@@ -1646,11 +1652,11 @@ void systemFrame(void)
 
 void systemGbBorderOn(void)
 {
-    bool changed = ((width != SGBWidth) || (height != SGBHeight));
-    width = gbBorderLineSkip = SGBWidth;
-    height = SGBHeight;
-    gbBorderColumnSkip = (SGBWidth - GBWidth) >> 1;
-    gbBorderRowSkip = (SGBHeight - GBHeight) >> 1;
+    bool changed = ((width != sgbWidth) || (height != sgbHeight));
+    width = gbBorderLineSkip = sgbWidth;
+    height = sgbHeight;
+    gbBorderColumnSkip = (sgbWidth - gbWidth) >> 1;
+    gbBorderRowSkip = (sgbHeight - gbHeight) >> 1;
 
     struct retro_system_av_info avinfo;
     retro_get_system_av_info(&avinfo);
@@ -1663,9 +1669,9 @@ void systemGbBorderOn(void)
 
 static void systemGbBorderOff(void)
 {
-    bool changed = ((width != GBWidth) || (height != GBHeight));
-    width = gbBorderLineSkip = GBWidth;
-    height = GBHeight;
+    bool changed = ((width != gbWidth) || (height != gbHeight));
+    width = gbBorderLineSkip = gbWidth;
+    height = gbHeight;
     gbBorderColumnSkip = gbBorderRowSkip = 0;
 
     struct retro_system_av_info avinfo;
diff --git a/src/libretro/scrc32.cpp b/src/libretro/scrc32.h
similarity index 98%
rename from src/libretro/scrc32.cpp
rename to src/libretro/scrc32.h
index 0c1204ea..c7599928 100644
--- a/src/libretro/scrc32.cpp
+++ b/src/libretro/scrc32.h
@@ -1,10 +1,6 @@
 #ifndef _S_CRC32_H
 #define _S_CRC32_H
 
-#ifdef __cplusplus
-extern "C" {
-#endif
-
 static const unsigned long crc_table[256] = {
     0x00000000L, 0x77073096L, 0xee0e612cL, 0x990951baL, 0x076dc419L,
     0x706af48fL, 0xe963a535L, 0x9e6495a3L, 0x0edb8832L, 0x79dcb8a4L,
@@ -87,8 +83,4 @@ unsigned long crc32(unsigned long crc, const unsigned char* buf, unsigned int le
     return crc ^ 0xffffffffL;
 }
 
-#ifdef __cplusplus
-}
-#endif
-
 #endif