From 171fb8cc14d919354254e902b591d4d3d6a15bd3 Mon Sep 17 00:00:00 2001
From: kub <derkub@gmail.com>
Date: Sat, 26 Feb 2022 21:48:18 +0000
Subject: [PATCH] sms, mapper for taiwanese sg-1000 ram extension

---
 pico/pico_int.h                           | 1 +
 pico/sms.c                                | 7 ++++++-
 platform/common/menu_pico.c               | 2 +-
 platform/libretro/libretro.c              | 2 ++
 platform/libretro/libretro_core_options.h | 1 +
 5 files changed, 11 insertions(+), 2 deletions(-)

diff --git a/pico/pico_int.h b/pico/pico_int.h
index 550cb5b5..f56ee553 100644
--- a/pico/pico_int.h
+++ b/pico/pico_int.h
@@ -351,6 +351,7 @@ struct PicoMisc
 #define PMS_MAP_N16K	6
 #define PMS_MAP_JANGGUN	7
 #define PMS_MAP_NEMESIS	8
+#define PMS_MAP_8KBRAM	9
 
 struct PicoMS
 {
diff --git a/pico/sms.c b/pico/sms.c
index dd301b08..dd301f9c 100644
--- a/pico/sms.c
+++ b/pico/sms.c
@@ -456,7 +456,12 @@ static void write_bank_jang(unsigned short a, unsigned char d)
 
 static void write_bank_x8k(unsigned short a, unsigned char d)
 {
+  // 8KB address range @ 0x2000
   if ((a&0xe000) != 0x2000) return;
+  // never autodetected, selectable only via config
+  if (Pico.ms.mapper != PMS_MAP_8KBRAM) return;
+  elprintf(EL_Z80BNK, "bank x8k %04x %02x @ %04x", a, d, z80_pc());
+
   ((unsigned char *)PicoMem.vram)[a+0x6000] = d;
   z80_map_set(z80_read_map,  0x2000, 0x3fff, PicoMem.vram+0x4000, 0);
   z80_map_set(z80_write_map, 0x2000, 0x3fff, PicoMem.vram+0x4000, 0);
@@ -479,6 +484,7 @@ static void xwrite(unsigned int a, unsigned char d)
   case PMS_MAP_N16K:	write_bank_n16k(a, d);  break;
   case PMS_MAP_JANGGUN: write_bank_jang(a, d);  break;
   case PMS_MAP_NEMESIS: write_bank_msxn(a, d);  break;
+  case PMS_MAP_8KBRAM:  write_bank_x8k(a, d);   break;
 
   case PMS_MAP_AUTO:
         // NB the sequence of mappers is crucial for the auto detection
@@ -488,7 +494,6 @@ static void xwrite(unsigned int a, unsigned char d)
         write_bank_codem(a, d);
         write_bank_korea(a, d);
         write_bank_n16k(a, d);
-        write_bank_x8k(a, d);
         break;
   }
 }
diff --git a/platform/common/menu_pico.c b/platform/common/menu_pico.c
index 3ebaa4e3..00da408c 100644
--- a/platform/common/menu_pico.c
+++ b/platform/common/menu_pico.c
@@ -537,7 +537,7 @@ static int menu_loop_32x_options(int id, int keys)
 #ifndef NO_SMS
 
 static const char *sms_hardwares[] = { "auto", "Game Gear", "Master System", NULL };
-static const char *sms_mappers[] = { "auto", "Sega", "Codemasters", "Korea", "Korea MSX", "Korea X-in-1", "Korea 4-Pak", "Korea Janggun", "Korea Nemesis", NULL };
+static const char *sms_mappers[] = { "auto", "Sega", "Codemasters", "Korea", "Korea MSX", "Korea X-in-1", "Korea 4-Pak", "Korea Janggun", "Korea Nemesis", "Taiwan 8K RAM", NULL };
 static const char h_smsfm[] = "FM sound is only supported by few games\nOther games may crash with FM enabled";
 
 static menu_entry e_menu_sms_options[] =
diff --git a/platform/libretro/libretro.c b/platform/libretro/libretro.c
index b24028e8..4bbcb9d7 100644
--- a/platform/libretro/libretro.c
+++ b/platform/libretro/libretro.c
@@ -1526,6 +1526,8 @@ static void update_variables(bool first_run)
          PicoIn.mapper = PMS_MAP_JANGGUN;
       else if (strcmp(var.value, "Korea Nemesis") == 0)
          PicoIn.mapper = PMS_MAP_NEMESIS;
+      else if (strcmp(var.value, "Taiwan 8K RAM") == 0)
+         PicoIn.mapper = PMS_MAP_8KRAM;
       else
          PicoIn.mapper = PMS_MAP_SEGA;
    }
diff --git a/platform/libretro/libretro_core_options.h b/platform/libretro/libretro_core_options.h
index 88b48d42..45edb1c1 100644
--- a/platform/libretro/libretro_core_options.h
+++ b/platform/libretro/libretro_core_options.h
@@ -138,6 +138,7 @@ struct retro_core_option_v2_definition option_defs_us[] = {
          { "Korea 4-Pak",   NULL },
          { "Korea Janggun", NULL },
          { "Korea Nemesis", NULL },
+         { "Taiwan 8K RAM", NULL },
          { NULL, NULL },
        },
       "Auto"
-- 
2.39.5