[libgens] Moved auto-detect code from gens-qt4 to SysVersion.
authorDavid Korth <gerbilsoft@gerbilsoft.com>
Sun, 3 Apr 2016 02:17:14 +0000 (22:17 -0400)
committerDavid Korth <gerbilsoft@gerbilsoft.com>
Sun, 3 Apr 2016 02:17:14 +0000 (22:17 -0400)
This will allow the auto-detect code to be reused by gens-sdl.

[gens-qt4] EmuManager.cpp:
- TODO: Remove GetLgRegionCode().

src/gens-qt4/Config/ConfigStore.cpp
src/gens-qt4/EmuManager.cpp
src/libgens/CMakeLists.txt
src/libgens/EmuContext/SysVersion.cpp [new file with mode: 0644]
src/libgens/EmuContext/SysVersion.hpp

index 8c4c1bc..4875279 100644 (file)
@@ -30,6 +30,8 @@
 #include "libgens/lg_main.hpp"
 #include "libgens/macros/common.h"
 #include "libgens/macros/log_msg.h"
+#include "libgens/EmuContext/SysVersion.hpp"
+using LibGens::SysVersion;
 
 // Qt includes.
 #include <QtCore/QSettings>
@@ -79,13 +81,6 @@ class ConfigStorePrivate
 
        public:
                /**
-                * Check if a region code order is valid.
-                * @param regionCodeOrder Region code order to check.
-                * @return True if valid; false if invalid.
-                */
-               static bool IsRegionCodeOrderValid(uint16_t regionCodeOrder);
-
-               /**
                 * Validate a property.
                 * @param key Property name.
                 * @param value Property value. (May be edited for validation.)
@@ -163,30 +158,6 @@ ConfigStorePrivate::~ConfigStorePrivate()
 }
 
 /**
- * Check if a region code order is valid.
- * @param regionCodeOrder Region code order to check.
- * @return True if valid; false if invalid.
- */
-bool ConfigStorePrivate::IsRegionCodeOrderValid(uint16_t regionCodeOrder)
-{
-       static const uint16_t RegionCodeOrder_tbl[24] =
-       {
-               0x4812, 0x4821, 0x4182, 0x4128, 0x4281, 0x4218, 
-               0x8412, 0x8421, 0x8124, 0x8142, 0x8241, 0x8214,
-               0x1482, 0x1428, 0x1824, 0x1842, 0x1248, 0x1284,
-               0x2481, 0x2418, 0x2814, 0x2841, 0x2148, 0x2184
-       };
-
-       for (size_t i = 0; i < ARRAY_SIZE(RegionCodeOrder_tbl); i++) {
-               if (regionCodeOrder == RegionCodeOrder_tbl[i])
-                       return true;
-       }
-
-       // Region code order is not valid.
-       return false;
-}
-
-/**
  * Validate a property.
  * @param key Property name.
  * @param value Property value. (May be edited for validation.)
@@ -231,7 +202,7 @@ QVariant ConfigStorePrivate::Validate(const QString &name, const QVariant &value
                        if (!value.canConvert(QVariant::UInt))
                                return QVariant();
                        uint16_t rc_order = (uint16_t)value.toString().toUInt(nullptr, 0);
-                       if (!IsRegionCodeOrderValid(rc_order))
+                       if (!SysVersion::IsRegionCodeOrderValid(rc_order))
                                return QVariant();
                        return QVariant(rc_order);
                }
index e1353f2..cae658a 100644 (file)
@@ -33,8 +33,10 @@ using LibGens::Rom;
 
 #include "libgens/EmuContext/EmuContext.hpp"
 #include "libgens/EmuContext/EmuContextFactory.hpp"
+#include "libgens/EmuContext/SysVersion.hpp"
 using LibGens::EmuContext;
 using LibGens::EmuContextFactory;
+using LibGens::SysVersion;
 
 // LibGens Sound Manager.
 // Needed for LibGens::SoundMgr::MAX_SAMPLING_RATE.
@@ -404,13 +406,13 @@ int EmuManager::loadRom_int(LibGens::Rom *rom)
        }
 
        // Determine the system region code.
-       const LibGens::SysVersion::RegionCode_t cfg_region =
-                               (LibGens::SysVersion::RegionCode_t)gqt4_cfg->getInt(QLatin1String("System/regionCode"));
-       const LibGens::SysVersion::RegionCode_t lg_region = GetLgRegionCode(
-                               cfg_region, rom->regionCode(),
-                               (uint16_t)gqt4_cfg->getUInt(QLatin1String("System/regionCodeOrder")));
+       const SysVersion::RegionCode_t cfg_region =
+                       (SysVersion::RegionCode_t)gqt4_cfg->getInt(QLatin1String("System/regionCode"));
+       const SysVersion::RegionCode_t lg_region = GetLgRegionCode(
+                       cfg_region, rom->regionCode(),
+                       (uint16_t)gqt4_cfg->getUInt(QLatin1String("System/regionCodeOrder")));
 
-       if (cfg_region == LibGens::SysVersion::REGION_AUTO) {
+       if (cfg_region == SysVersion::REGION_AUTO) {
                // Print the auto-detected region.
                const QString detect_str = LgRegionCodeStr(lg_region);
                if (!detect_str.isEmpty()) {
@@ -513,43 +515,19 @@ int EmuManager::loadRom_int(LibGens::Rom *rom)
  * @param regionCodeOrder Region code order for auto-detection. (MSN == highest priority)
  * @return LibGens region code to use.
  */
-LibGens::SysVersion::RegionCode_t EmuManager::GetLgRegionCode(
-               LibGens::SysVersion::RegionCode_t confRegionCode,
+SysVersion::RegionCode_t EmuManager::GetLgRegionCode(
+               SysVersion::RegionCode_t confRegionCode,
                int mdHexRegionCode, uint16_t regionCodeOrder)
 {
-       // FIXME: Move to LibGens::SysVersion?
-       if (confRegionCode >= LibGens::SysVersion::REGION_JP_NTSC &&
-           confRegionCode <= LibGens::SysVersion::REGION_EU_PAL)
+       // TODO: Remove this function?
+       if (confRegionCode >= SysVersion::REGION_JP_NTSC &&
+           confRegionCode <= SysVersion::REGION_EU_PAL)
        {
-               // Valid regoin code.
+               // Valid region code.
                return confRegionCode;
        }
-       
-       // Attempt to auto-detect the region from the ROM image.
-       int regionMatch = 0;
-       int orderTmp = regionCodeOrder;
-       for (int i = 0; i < 4; i++, orderTmp <<= 4) {
-               int orderN = ((orderTmp >> 12) & 0xF);
-               if (mdHexRegionCode & orderN) {
-                       // Found a match.
-                       regionMatch = orderN;
-                       break;
-               }
-       }
-       
-       if (regionMatch == 0) {
-               // No region matched.
-               // Use the highest-priority region.
-               regionMatch = ((regionCodeOrder >> 12) & 0xF);
-       }
-       
-       switch (regionMatch & 0xF) {
-               default:
-               case 0x4:       return LibGens::SysVersion::REGION_US_NTSC;
-               case 0x8:       return LibGens::SysVersion::REGION_EU_PAL;
-               case 0x1:       return LibGens::SysVersion::REGION_JP_NTSC;
-               case 0x2:       return LibGens::SysVersion::REGION_ASIA_PAL;
-       }
+
+       return SysVersion::DetectRegion(mdHexRegionCode, regionCodeOrder);
 }
 
 /**
index ddcfe93..e005b4e 100644 (file)
@@ -103,6 +103,7 @@ SET(libgens_EMUCONTEXT_SRCS
        # MD
        EmuContext/EmuMD.cpp
        EmuContext/EmuMD_zomg.cpp
+       EmuContext/SysVersion.cpp
        EmuContext/TmssReg.cpp
 
        # Pico
diff --git a/src/libgens/EmuContext/SysVersion.cpp b/src/libgens/EmuContext/SysVersion.cpp
new file mode 100644 (file)
index 0000000..6f4612d
--- /dev/null
@@ -0,0 +1,101 @@
+/***************************************************************************
+ * libgens: Gens Emulation Library.                                        *
+ * SysVersion.cpp: MD System Version Code Register.                        *
+ *                                                                         *
+ * Copyright (c) 1999-2002 by Stéphane Dallongeville.                      *
+ * Copyright (c) 2003-2004 by Stéphane Akhoun.                             *
+ * Copyright (c) 2008-2016 by David Korth.                                 *
+ *                                                                         *
+ * This program is free software; you can redistribute it and/or modify it *
+ * under the terms of the GNU General Public License as published by the   *
+ * Free Software Foundation; either version 2 of the License, or (at your  *
+ * option) any later version.                                              *
+ *                                                                         *
+ * This program is distributed in the hope that it will be useful, but     *
+ * WITHOUT ANY WARRANTY; without even the implied warranty of              *
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the           *
+ * GNU General Public License for more details.                            *
+ *                                                                         *
+ * You should have received a copy of the GNU General Public License along *
+ * with this program; if not, write to the Free Software Foundation, Inc., *
+ * 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.           *
+ ***************************************************************************/
+
+#include "SysVersion.hpp"
+
+#include "macros/common.h"
+
+// C includes. (C++ namespace)
+#include <cassert>
+
+namespace LibGens {
+
+// Valid region code auto-detect orders.
+const uint16_t SysVersion::RegionCodeOrder_tbl[24] = {
+       0x4812, 0x4821, 0x4182, 0x4128, 0x4281, 0x4218, 
+       0x8412, 0x8421, 0x8124, 0x8142, 0x8241, 0x8214,
+       0x1482, 0x1428, 0x1824, 0x1842, 0x1248, 0x1284,
+       0x2481, 0x2418, 0x2814, 0x2841, 0x2148, 0x2184
+};
+
+/** Region code auto-detection. **/
+
+/**
+ * Detect the region code for an MD ROM.
+ * @param mdHexRegionCode MD hex region code. (0x0-0xF)
+ * @param regionCodeOrder Region code order.
+ * @return RegionCode_t. (Returns REGION_AUTO if a parameter is invalid.)
+ */
+SysVersion::RegionCode_t SysVersion::DetectRegion(int mdHexRegionCode, uint16_t regionCodeOrder)
+{
+       assert(mdHexRegionCode >= 0x0 && mdHexRegionCode <= 0xF);
+       if (mdHexRegionCode < 0x0 || mdHexRegionCode > 0xF)
+               return REGION_AUTO;
+
+       assert(IsRegionCodeOrderValid(regionCodeOrder));
+       if (!IsRegionCodeOrderValid(regionCodeOrder))
+               return REGION_AUTO;
+
+       // Attempt to auto-detect the region from the ROM image.
+       int regionMatch = 0;
+       int orderTmp = regionCodeOrder;
+       for (int i = 0; i < 4; i++, orderTmp <<= 4) {
+               int orderN = ((orderTmp >> 12) & 0xF);
+               if (mdHexRegionCode & orderN) {
+                       // Found a match.
+                       regionMatch = orderN;
+                       break;
+               }
+       }
+
+       if (regionMatch == 0) {
+               // No region matched.
+               // Use the highest-priority region.
+               regionMatch = ((regionCodeOrder >> 12) & 0xF);
+       }
+
+       switch (regionMatch & 0xF) {
+               default:
+               case 0x4:       return LibGens::SysVersion::REGION_US_NTSC;
+               case 0x8:       return LibGens::SysVersion::REGION_EU_PAL;
+               case 0x1:       return LibGens::SysVersion::REGION_JP_NTSC;
+               case 0x2:       return LibGens::SysVersion::REGION_ASIA_PAL;
+       }
+}
+
+/**
+ * Is a region code auto-detect order valid?
+ * @param regionCodeOrder Region code order.
+ * @return True if valid; false if not.
+ */
+bool SysVersion::IsRegionCodeOrderValid(uint16_t regionCodeOrder)
+{
+       for (int i = 0; i < ARRAY_SIZE(RegionCodeOrder_tbl); i++) {
+               if (regionCodeOrder == RegionCodeOrder_tbl[i])
+                       return true;
+       }
+
+       return false;
+}
+
+}
index 5b395dc..550c5ba 100644 (file)
@@ -4,7 +4,7 @@
  *                                                                         *
  * Copyright (c) 1999-2002 by Stéphane Dallongeville.                      *
  * Copyright (c) 2003-2004 by Stéphane Akhoun.                             *
- * Copyright (c) 2008-2011 by David Korth.                                 *
+ * Copyright (c) 2008-2016 by David Korth.                                 *
  *                                                                         *
  * This program is free software; you can redistribute it and/or modify it *
  * under the terms of the GNU General Public License as published by the   *
@@ -43,8 +43,7 @@
 // C includes.
 #include <stdint.h>
 
-namespace LibGens
-{
+namespace LibGens {
 
 class SysVersion
 {
@@ -89,6 +88,28 @@ class SysVersion
                uint8_t version(void) const;
                void setVersion(uint8_t newVersion);
 
+       public:
+               /** Region code auto-detection. **/
+
+               /**
+                * Detect the region code for an MD ROM.
+                * @param mdHexRegionCode MD hex region code. (0x0-0xF)
+                * @param regionCodeOrder Region code order.
+                * @return RegionCode_t. (Returns REGION_AUTO if a parameter is invalid.)
+                */
+               static RegionCode_t DetectRegion(int mdHexRegionCode, uint16_t regionCodeOrder);
+
+               /**
+                * Is a region code auto-detect order valid?
+                * @param regionCodeOrder Region code order.
+                * @return True if valid; false if not.
+                */
+               static bool IsRegionCodeOrderValid(uint16_t regionCodeOrder);
+
+       protected:
+               // Valid region code auto-detect orders.
+               static const uint16_t RegionCodeOrder_tbl[24];
+
        private:
                RegionCode_t m_region;
                bool m_disk;            // True if MCD is connected.