From b1a37724a13664ed5eae4bf25a42eeff526a72cd Mon Sep 17 00:00:00 2001
From: beirich <beirich@4bbf4f03-6f44-549e-7e59-35d4b0962838>
Date: Thu, 13 Mar 2014 04:49:39 +0000
Subject: [PATCH] SMS: hook up Export/Japan, NTSC/PAL, BIOS Enable, and US/JP
 Bios selection

---
 .../Database/FirmwareDatabase.cs              | 11 +++---
 .../Consoles/Sega/SMS/Compat.txt              |  5 +++
 .../Consoles/Sega/SMS/SMS.cs                  | 36 +++++++++++++------
 3 files changed, 36 insertions(+), 16 deletions(-)

diff --git a/BizHawk.Emulation.Common/Database/FirmwareDatabase.cs b/BizHawk.Emulation.Common/Database/FirmwareDatabase.cs
index 8c4b9f0169..396f73a271 100644
--- a/BizHawk.Emulation.Common/Database/FirmwareDatabase.cs
+++ b/BizHawk.Emulation.Common/Database/FirmwareDatabase.cs
@@ -106,11 +106,12 @@ namespace BizHawk.Emulation.Common
 			var sms_us_1b = File("29091FF60EF4C22B1EE17AA21E0E75BAC6B36474", "sms_us_1.0b.sms", "SMS BIOS 1.0 (USA) (Proto)");
 			var sms_m404  = File("4A06C8E66261611DCE0305217C42138B71331701", "sms_m404.sms", "SMS BIOS (USA) (M404) (Proto)");
 
-			Firmware("SMS", "SMSBIOS", "SMS Bios");
-			Option("SMS", "SMSBIOS", sms_us_13);
-			Option("SMS", "SMSBIOS", sms_jp_21);
-			Option("SMS", "SMSBIOS", sms_us_1b);
-			Option("SMS", "SMSBIOS", sms_m404);
+			Firmware("SMS", "Export", "SMS Bios (USA/Export)");
+			Firmware("SMS", "Japan", "SMS Bios (Japan)");
+			Option("SMS", "Export", sms_us_13);
+			Option("SMS", "Export", sms_us_1b);
+			Option("SMS", "Export", sms_m404);
+			Option("SMS", "Japan", sms_jp_21);
 		}
 
 		//adds a defined firmware ID to the database
diff --git a/BizHawk.Emulation.Cores/Consoles/Sega/SMS/Compat.txt b/BizHawk.Emulation.Cores/Consoles/Sega/SMS/Compat.txt
index d4fb57bcb9..dbd086c98c 100644
--- a/BizHawk.Emulation.Cores/Consoles/Sega/SMS/Compat.txt
+++ b/BizHawk.Emulation.Cores/Consoles/Sega/SMS/Compat.txt
@@ -1,5 +1,10 @@
 ======= SMS compatibility issues =======
 
+* Several korean TMS games don't work. Possibly mapper issue?
+* Janggun-ui Adeul (Korea).sms - Sprite flip gfx issue? Could be interesting to investigate. Cogwheel does the same; Kega wont even boot the game
+* Jang Pung 3 - boots but totally fuxed?
+* Pretty much everything with (korea) in it needs to be checked. Stupid rare korea shit.
+
 * NBA-Jam SMS Proto: works in PAL mode, which is how it's designed to run.
   In NTSC mode, it is impossible to start a game, however, it is possible on
   other emulators.
diff --git a/BizHawk.Emulation.Cores/Consoles/Sega/SMS/SMS.cs b/BizHawk.Emulation.Cores/Consoles/Sega/SMS/SMS.cs
index 7d7521c6e6..e2c0fe27e5 100644
--- a/BizHawk.Emulation.Cores/Consoles/Sega/SMS/SMS.cs
+++ b/BizHawk.Emulation.Cores/Consoles/Sega/SMS/SMS.cs
@@ -15,6 +15,9 @@ using BizHawk.Emulation.Cores.Components.Z80;
   + Try to clean up the organization of the source code. 
   + Lightgun/Paddle/etc if I get really bored  
   + Mode 1 not implemented in VDP TMS modes. (I dont have a test case in SG1000 or Coleco)
+  + Add Region to GameDB.
+  + Still need a "disable bios for japan-only games when bios is enabled and region is export" functionality
+  + Or a "force region to japan if game is only for japan" thing. Which one is better?
  
 **********************************************************/
 
@@ -142,12 +145,22 @@ namespace BizHawk.Emulation.Cores.Sega.MasterSystem
                 Array.Resize(ref RomData, ((RomData.Length / BankSize) + 1) * BankSize);
             RomBanks = (byte)(RomData.Length / BankSize);
 
-            DisplayType = DisplayType.NTSC;
-            if (game["PAL"]) DisplayType = DisplayType.PAL;
+            DisplayType = SyncSettings.UsePAL ? DisplayType.PAL : DisplayType.NTSC;
+			if (game["PAL"] && DisplayType != DisplayType.PAL)
+			{
+				DisplayType = DisplayType.PAL;
+				Console.WriteLine("Display was forced to PAL mode for game compatibility."); // TODO change to corecomm.notify when it exists
+			}
 			CoreComm.VsyncNum = DisplayType == DisplayType.NTSC ? 60 : 50;
 			CoreComm.VsyncDen = 1;
-            
-            if (game["Japan"]) Region = "Japan";
+
+			Region = SyncSettings.ExportRegion ? "Export" : "Japan";
+			if (game["Japan"] && Region != "Japan")
+			{
+				Region = "Japan";
+				Console.WriteLine("Region was forced to Japan for game compatibility."); // TODO corecomm.notify
+			}
+
             if (game.NotInDatabase || game["FM"] && SyncSettings.EnableFM && !IsGameGear)
                 HasYM2413 = true;
 
@@ -201,14 +214,15 @@ namespace BizHawk.Emulation.Cores.Sega.MasterSystem
 			}
 			else if (game.System == "SMS")
 			{
-				BiosRom = comm.CoreFileProvider.GetFirmware("SMS", "SMSBIOS", false);
-				if (BiosRom != null) // && usebios
-				{
+				BiosRom = comm.CoreFileProvider.GetFirmware("SMS", Region, false);
+				if (BiosRom != null && SyncSettings.UseBIOS)
 					Port3E = 0xF7;
-				}
-			}
 
-            SetupMemoryDomains();
+				if (SyncSettings.UseBIOS && BiosRom == null)
+					Console.WriteLine("BIOS was selected, but rom image not available. BIOS not enabled."); // TODO corecomm.notify
+			}
+            
+			SetupMemoryDomains();
 		}
 
 		public byte ReadPort(ushort port)
@@ -464,7 +478,7 @@ namespace BizHawk.Emulation.Cores.Sega.MasterSystem
 
 		public string BoardName { get { return null; } }
 
-		string region = "Export";
+		string region;
 		public string Region
 		{
 			get { return region; }