From 59f8f7d1f0c4e2c073f0ebdb57d253cc8ea3f692 Mon Sep 17 00:00:00 2001
From: kub <derkub@gmail.com>
Date: Wed, 5 Feb 2025 20:33:56 +0100
Subject: [PATCH] platform, add test menu for SC-3000 tape saving

---
 platform/common/emu.c       | 14 +++++++++
 platform/common/emu.h       |  1 +
 platform/common/menu_pico.c | 57 ++++++++++++++++++++++++++++---------
 platform/common/menu_pico.h |  1 +
 4 files changed, 60 insertions(+), 13 deletions(-)

diff --git a/platform/common/emu.c b/platform/common/emu.c
index 9c6f942c..6b544091 100644
--- a/platform/common/emu.c
+++ b/platform/common/emu.c
@@ -616,6 +616,19 @@ int emu_play_tape(const char *fname)
 	return 1;
 }
 
+int emu_record_tape(const char *ext)
+{
+	int ret;
+
+	fname_ext(static_buff, sizeof(static_buff), "tape"PATH_SEP, ext, rom_fname_loaded);
+	ret = PicoRecordTape(static_buff);
+	if (ret != 0) {
+		menu_update_msg("recording tape failed");
+		return 0;
+	}
+	return 1;
+}
+
 // <base dir><end>
 void emu_make_path(char *buff, const char *end, int size)
 {
@@ -1461,6 +1474,7 @@ void emu_init(void)
 	mkdir_path(path, pos, "mds");
 	mkdir_path(path, pos, "srm");
 	mkdir_path(path, pos, "brm");
+	mkdir_path(path, pos, "tape");
 	mkdir_path(path, pos, "cfg");
 
 	pprof_init();
diff --git a/platform/common/emu.h b/platform/common/emu.h
index ff9f8441..30ab20f1 100644
--- a/platform/common/emu.h
+++ b/platform/common/emu.h
@@ -140,6 +140,7 @@ void  emu_loop(void);
 int   emu_reload_rom(const char *rom_fname_in);
 int   emu_swap_cd(const char *fname);
 int   emu_play_tape(const char *fname);
+int   emu_record_tape(const char *ext);
 int   emu_save_load_game(int load, int sram);
 void  emu_reset_game(void);
 
diff --git a/platform/common/menu_pico.c b/platform/common/menu_pico.c
index d5b435e7..480d8cfe 100644
--- a/platform/common/menu_pico.c
+++ b/platform/common/menu_pico.c
@@ -1415,20 +1415,53 @@ static void menu_main_draw_status(void)
 			bp[(w - i) + g_menuscreen_pp * u] = menu_text_color;
 }
 
-static int menu_load_sc3000_tape(void)
+static menu_entry e_menu_main[];
+
+static int tape_record_bit;
+static const char *tape_record_exts[] = { ".wav", ".bit" };
+
+static const char *mgn_loadtape(int id, int *offs)
 {
-	static const char *rom_exts[] = { "bit", "wav", NULL };
-	const char *ret_name;
+	return "";
+}
 
-	ret_name = menu_loop_romsel_d(rom_fname_loaded,
-			sizeof(rom_fname_loaded), rom_exts, NULL, menu_draw_prep);
-	if (ret_name == NULL)
+static int mh_loadtape(int id, int keys)
+{
+	if (keys & (PBTN_LEFT|PBTN_RIGHT)) { // multi choice
+		int x = me_id2offset(e_menu_main, MA_MAIN_SAVE_TAPE);
+		e_menu_main[x].enabled = !e_menu_main[x].enabled;
 		return 0;
+	}
+	if (keys & PBTN_MOK) {
+		static const char *rom_exts[] = { "bit", "wav", NULL };
+		const char *ret_name;
+
+		ret_name = menu_loop_romsel_d(rom_fname_loaded,
+				sizeof(rom_fname_loaded), rom_exts, NULL, menu_draw_prep);
+		if (ret_name == NULL)
+			return 0;
 
-	return emu_play_tape(ret_name);
+		return emu_play_tape(ret_name);
+	}
+	return 1;
 }
 
-static menu_entry e_menu_main[];
+static const char *mgn_savetape(int id, int *offs)
+{
+	return tape_record_exts[!!tape_record_bit];
+}
+
+static int mh_savetape(int id, int keys)
+{
+	if (keys & (PBTN_LEFT|PBTN_RIGHT)) { // multi choice
+		tape_record_bit = !tape_record_bit;
+		return 0;
+	}
+	if (keys & PBTN_MOK) {
+		return emu_record_tape(tape_record_exts[!!tape_record_bit]);
+	}
+	return 1;
+}
 
 static int main_menu_handler(int id, int keys)
 {
@@ -1474,10 +1507,6 @@ static int main_menu_handler(int id, int keys)
 			return 1;
 		}
 		break;
-	case MA_MAIN_LOAD_TAPE:
-		if (PicoIn.AHW & PAHW_SC)
-			return menu_load_sc3000_tape();
-		break;
 	case MA_MAIN_CREDITS:
 		draw_menu_message(credits, draw_frame_credits);
 		in_menu_wait(PBTN_MOK|PBTN_MBACK, NULL, 70);
@@ -1574,7 +1603,8 @@ static menu_entry e_menu_main[] =
 	mee_handler_id("Reset game",         MA_MAIN_RESET_GAME,  main_menu_handler),
 	mee_handler_id("Change CD",          MA_MAIN_CHANGE_CD,   main_menu_handler),
 	mee_cust_s_h  ("Storyware page",     MA_MAIN_PICO_PAGE, 0,mh_picopage, mgn_picopage, NULL),
-	mee_handler_id("Load tape",          MA_MAIN_LOAD_TAPE,   main_menu_handler),
+	mee_cust_s_h  ("Load tape",          MA_MAIN_LOAD_TAPE, 0,mh_loadtape, mgn_loadtape, NULL),
+	mee_cust_s_h  ("Save tape",          MA_MAIN_SAVE_TAPE, 0,mh_savetape, mgn_savetape, NULL),
 	mee_handler_id("Patches / GameGenie",MA_MAIN_PATCHES,     main_menu_handler),
 	mee_handler_id("Load new game",      MA_MAIN_LOAD_ROM,    main_menu_handler),
 	mee_handler   ("Change options",                          menu_loop_options),
@@ -1596,6 +1626,7 @@ void menu_loop(void)
 	me_enable(e_menu_main, MA_MAIN_RESET_GAME,  PicoGameLoaded);
 	me_enable(e_menu_main, MA_MAIN_CHANGE_CD,   PicoIn.AHW & PAHW_MCD);
 	me_enable(e_menu_main, MA_MAIN_LOAD_TAPE,   PicoIn.AHW & PAHW_SC);
+	me_enable(e_menu_main, MA_MAIN_SAVE_TAPE,   0);
 	me_enable(e_menu_main, MA_MAIN_PICO_PAGE,   PicoIn.AHW & PAHW_PICO);
 	me_enable(e_menu_main, MA_MAIN_PATCHES,     PicoPatches != NULL);
 	me_enable(e_menu_main, MA_OPT_SAVECFG_GAME, PicoGameLoaded);
diff --git a/platform/common/menu_pico.h b/platform/common/menu_pico.h
index cd6edc68..644fca71 100644
--- a/platform/common/menu_pico.h
+++ b/platform/common/menu_pico.h
@@ -13,6 +13,7 @@ typedef enum
 	MA_MAIN_LOAD_ROM,
 	MA_MAIN_CHANGE_CD,
 	MA_MAIN_LOAD_TAPE,
+	MA_MAIN_SAVE_TAPE,
 	MA_MAIN_PICO_PAGE,
 	MA_MAIN_CONTROLS,
 	MA_MAIN_CREDITS,
-- 
2.39.5