From: notaz <notasas@gmail.com>
Date: Sun, 1 Apr 2007 18:10:07 +0000 (+0000)
Subject: bugfixes, CD swap, autorepeat
X-Git-Url: https://notaz.gp2x.de/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=5e2e14f284d1abbee9f212f75d6766a5825c7ada;p=libpicofe.git

bugfixes, CD swap, autorepeat

git-svn-id: file:///home/notaz/opt/svn/PicoDrive/platform@85 be3aeb3a-fb24-0410-a615-afba39da0efa
---

diff --git a/gp2x/emu.c b/gp2x/emu.c
index 99357b2..c16b658 100644
--- a/gp2x/emu.c
+++ b/gp2x/emu.c
@@ -156,7 +156,7 @@ int find_bios(int region, char **bios_file)
 
 /* checks if romFileName points to valid MegaCD image
  * if so, checks for suitable BIOS */
-static int cd_check(char **bios_file)
+int emu_cd_check(char **bios_file)
 {
 	unsigned char buf[32];
 	pm_file *cd_f;
@@ -177,7 +177,7 @@ static int cd_check(char **bios_file)
 		return 0;
 	}
 
-	/* it seems we have a CD image here. Try to detect region and load a suitable BIOS now.. */
+	/* it seems we have a CD image here. Try to detect region now.. */
 	pm_seek(cd_f, (type == 1) ? 0x100+0x10B : 0x110+0x10B, SEEK_SET);
 	pm_read(buf, 1, cd_f);
 	pm_close(cd_f);
@@ -193,7 +193,9 @@ static int cd_check(char **bios_file)
 		printf("overrided region to %s\n", region != 4 ? (region == 8 ? "EU" : "JAP") : "USA");
 	}
 
-	if(find_bios(region, bios_file))
+	if (bios_file == NULL) return type;
+
+	if (find_bios(region, bios_file))
 		 return type;	// CD and BIOS detected
 
 	return -1;     		// CD detected but load failed
@@ -271,7 +273,7 @@ int emu_ReloadRom(void)
 	}
 
 	// check for MegaCD image
-	cd_state = cd_check(&used_rom_name);
+	cd_state = emu_cd_check(&used_rom_name);
 	if (cd_state > 0) {
 		PicoMCD |= 1;
 		get_ext(used_rom_name, ext);
@@ -381,6 +383,7 @@ int emu_ReloadRom(void)
 
 
 static void emu_msg_cb(const char *msg);
+static void emu_msg_tray_open(void);
 
 void emu_Init(void)
 {
@@ -399,8 +402,8 @@ void emu_Init(void)
 
 	PicoInit();
 	PicoMessage = emu_msg_cb;
-
-//	logf = fopen("log.txt", "w");
+	PicoMCDopenTray = emu_msg_tray_open;
+	PicoMCDcloseTray = menu_loop_tray;
 }
 
 
@@ -528,8 +531,9 @@ int emu_ReadConfig(int game)
 	}
 	scaling_update();
 	// some sanity checks
-	if (currentConfig.CPUclock < 1 || currentConfig.CPUclock > 4096) currentConfig.CPUclock = 200;
+	if (currentConfig.CPUclock < 10 || currentConfig.CPUclock > 4096) currentConfig.CPUclock = 200;
 	if (currentConfig.gamma < 10 || currentConfig.gamma > 300) currentConfig.gamma = 100;
+	if (currentConfig.volume < 0 || currentConfig.volume > 99) currentConfig.volume = 50;
 	// if volume keys are unbound, bind them to volume control
 	if (!currentConfig.KeyBinds[23] && !currentConfig.KeyBinds[22]) {
 		currentConfig.KeyBinds[23] = 1<<29; // vol up
@@ -603,7 +607,6 @@ void emu_Deinit(void)
 	free(framebuff);
 
 	PicoExit();
-//	fclose(logf);
 
 	// restore gamma
 	if (gp2x_old_gamma != 100)
@@ -824,6 +827,12 @@ static void emu_state_cb(const char *str)
 	blit("", str);
 }
 
+static void emu_msg_tray_open(void)
+{
+	strcpy(noticeMsg, "CD tray opened");
+	gettimeofday(&noticeMsgTime, 0);
+}
+
 static void RunEvents(unsigned int which)
 {
 	if(which & 0x1800) { // save or load (but not both)
@@ -974,7 +983,7 @@ static void updateKeys(void)
 	if(events & 0x6000) {
 		int vol = currentConfig.volume;
 		if (events & 0x2000) {
-			if (vol < 90) vol++;
+			if (vol < 99) vol++;
 		} else {
 			if (vol >  0) vol--;
 		}
@@ -1196,7 +1205,7 @@ void emu_Loop(void)
 				if (frames_shown > frames_done) frames_shown = frames_done;
 			}
 		}
-#if 1
+#if 0
 		sprintf(fpsbuff, "%05i", Pico.m.frame_count);
 #endif
 		lim_time = (frames_done+1) * target_frametime;
diff --git a/gp2x/emu.h b/gp2x/emu.h
index b487c51..627c226 100644
--- a/gp2x/emu.h
+++ b/gp2x/emu.h
@@ -13,6 +13,7 @@ enum TPicoGameState {
 	PGS_KeyConfig,
 	PGS_ReloadRom,
 	PGS_Menu,
+	PGS_RestartRun,
 };
 
 typedef struct {
@@ -52,6 +53,7 @@ char *emu_GetSaveFName(int load, int is_sram, int slot);
 int  emu_check_save_file(int slot);
 void emu_set_save_cbs(int gz);
 void emu_forced_frame(void);
+int  emu_cd_check(char **bios_file);
 int  find_bios(int region, char **bios_file);
 void scaling_update(void);
 
diff --git a/gp2x/main.c b/gp2x/main.c
index 23d6241..61579cf 100644
--- a/gp2x/main.c
+++ b/gp2x/main.c
@@ -120,6 +120,9 @@ int main(int argc, char *argv[])
 				}
 				break;
 
+			case PGS_RestartRun:
+				engineState = PGS_Running;
+
 			case PGS_Running:
 				emu_Loop();
 				break;
diff --git a/gp2x/menu.c b/gp2x/menu.c
index f9c8065..881ea98 100644
--- a/gp2x/menu.c
+++ b/gp2x/menu.c
@@ -15,6 +15,7 @@
 #include "menu.h"
 #include "fonts.h"
 #include "usbjoy.h"
+#include "asmutils.h"
 #include "version.h"
 
 #include <Pico/PicoInt.h>
@@ -170,14 +171,16 @@ static int inp_prevjoy = 0;
 static unsigned long wait_for_input(unsigned long interesting)
 {
 	unsigned long ret;
-	static int repeats = 0, wait = 300*1000;
+	static int repeats = 0, wait = 50*1000;
 	int release = 0, i;
 
-	if (repeats == 5 || repeats == 15 || repeats == 30) wait /= 2;
+	if (repeats == 2 || repeats == 4) wait /= 2;
+	if (repeats == 6) wait = 15 * 1000;
 
 	for (i = 0; i < 6 && inp_prev == gp2x_joystick_read(1); i++) {
-		if(i == 0) repeats++;
-		usleep(wait/6);
+		if (i == 0) repeats++;
+		if (wait >= 30*1000) usleep(wait); // usleep sleeps for ~30ms minimum
+		else spend_cycles(wait * currentConfig.CPUclock);
 	}
 
 	while ( !((ret = gp2x_joystick_read(1)) & interesting) ) {
@@ -187,7 +190,7 @@ static unsigned long wait_for_input(unsigned long interesting)
 
 	if (release || ret != inp_prev) {
 		repeats = 0;
-		wait = 300*1000;
+		wait = 50*1000;
 	}
 	inp_prev = ret;
 	inp_prevjoy = 0;
@@ -356,8 +359,10 @@ static char *romsel_loop(char *curr_path)
 		inp = wait_for_input(GP2X_UP|GP2X_DOWN|GP2X_LEFT|GP2X_RIGHT|GP2X_L|GP2X_R|GP2X_B|GP2X_X);
 		if(inp & GP2X_UP  )  { sel--;   if (sel < 0)   sel = n-2; }
 		if(inp & GP2X_DOWN)  { sel++;   if (sel > n-2) sel = 0; }
-		if(inp &(GP2X_LEFT|GP2X_L))  { sel-=10; if (sel < 0)   sel = 0; }
-		if(inp &(GP2X_RIGHT|GP2X_R)) { sel+=10; if (sel > n-2) sel = n-2; }
+		if(inp & GP2X_LEFT)  { sel-=10; if (sel < 0)   sel = 0; }
+		if(inp & GP2X_L)     { sel-=24; if (sel < 0)   sel = 0; }
+		if(inp & GP2X_RIGHT) { sel+=10; if (sel > n-2) sel = n-2; }
+		if(inp & GP2X_R)     { sel+=24; if (sel > n-2) sel = n-2; }
 		if(inp & GP2X_B)     { // enter dir/select
 			again:
 			if (namelist[sel+1]->d_type == DT_REG) {
@@ -872,7 +877,7 @@ static void draw_amenu_options(int menu_sel)
 	gp2x_text_out8(tl_x, (y+=10), "Emulate YM2612 (FM)        %s", (currentConfig.PicoOpt&0x001)?"ON":"OFF"); // 2
 	gp2x_text_out8(tl_x, (y+=10), "Emulate SN76496 (PSG)      %s", (currentConfig.PicoOpt&0x002)?"ON":"OFF"); // 3
 	gp2x_text_out8(tl_x, (y+=10), "gzip savestates            %s", (currentConfig.EmuOpt &0x008)?"ON":"OFF"); // 4
-	gp2x_text_out8(tl_x, (y+=10), "Don't save config on exit  %s", (currentConfig.EmuOpt &0x020)?"ON":"OFF"); // 5
+	gp2x_text_out8(tl_x, (y+=10), "Don't save last used ROM   %s", (currentConfig.EmuOpt &0x020)?"ON":"OFF"); // 5
 	gp2x_text_out8(tl_x, (y+=10), "needs restart:");
 	gp2x_text_out8(tl_x, (y+=10), "craigix's RAM timings      %s", (currentConfig.EmuOpt &0x100)?"ON":"OFF"); // 7
 	gp2x_text_out8(tl_x, (y+=10), "squidgehack (now %s %s",   mms, (currentConfig.EmuOpt &0x010)?"ON":"OFF"); // 8
@@ -1256,7 +1261,11 @@ static void menu_loop_root(void)
 		if(inp & GP2X_B   )  {
 			switch (menu_sel) {
 				case 0: // resume game
-					if (rom_data) { engineState = PGS_Running; return; }
+					if (rom_data) {
+						while (gp2x_joystick_read(1) & GP2X_B) usleep(50*1000);
+						engineState = PGS_Running;
+						return;
+					}
 					break;
 				case 1: // save state
 					if (rom_data) {
@@ -1355,3 +1364,85 @@ void menu_loop(void)
 
 	menuErrorMsg[0] = 0;
 }
+
+
+// --------- CD tray close menu ----------
+
+static void draw_menu_tray(int menu_sel)
+{
+	int tl_x = 70, tl_y = 90, y;
+	memset(gp2x_screen, 0xe0, 320*240);
+
+	gp2x_text_out8(tl_x, 20, "The unit is about to");
+	gp2x_text_out8(tl_x, 30, "close the CD tray.");
+
+	y = tl_y;
+	gp2x_text_out8(tl_x, y,       "Load new CD image");
+	gp2x_text_out8(tl_x, (y+=10), "Insert nothing");
+
+	// draw cursor
+	gp2x_text_out8(tl_x - 16, tl_y + menu_sel*10, ">");
+	// error
+	if (menuErrorMsg[0]) gp2x_text_out8(5, 226, menuErrorMsg);
+	gp2x_video_flip2();
+}
+
+
+int menu_loop_tray(void)
+{
+	int menu_sel = 0, menu_sel_max = 1;
+	unsigned long inp = 0;
+	char curr_path[PATH_MAX], *selfname;
+	FILE *tstf;
+
+	gp2x_memset_all_buffers(0, 0xe0, 320*240);
+	menu_gfx_prepare();
+
+	if ( (tstf = fopen(currentConfig.lastRomFile, "rb")) )
+	{
+		fclose(tstf);
+		strcpy(curr_path, currentConfig.lastRomFile);
+	}
+	else
+	{
+		getcwd(curr_path, PATH_MAX);
+	}
+
+	/* make sure action buttons are not pressed on entering menu */
+	draw_menu_tray(menu_sel);
+	while (gp2x_joystick_read(1) & GP2X_B) usleep(50*1000);
+
+	for (;;)
+	{
+		draw_menu_tray(menu_sel);
+		inp = wait_for_input(GP2X_UP|GP2X_DOWN|GP2X_B);
+		if(inp & GP2X_UP  )  { menu_sel--; if (menu_sel < 0) menu_sel = menu_sel_max; }
+		if(inp & GP2X_DOWN)  { menu_sel++; if (menu_sel > menu_sel_max) menu_sel = 0; }
+		if(inp & GP2X_B   )  {
+			switch (menu_sel) {
+				case 0: // select image
+					selfname = romsel_loop(curr_path);
+					if (selfname) {
+						int ret = -1, cd_type;
+						cd_type = emu_cd_check(NULL);
+						if (cd_type > 0)
+							ret = Insert_CD(romFileName, cd_type == 2);
+						if (ret != 0) {
+							sprintf(menuErrorMsg, "Load failed, invalid CD image?");
+							printf("%s\n", menuErrorMsg);
+							continue;
+						}
+						engineState = PGS_RestartRun;
+						return 1;
+					}
+					break;
+				case 1: // insert nothing
+					engineState = PGS_RestartRun;
+					return 0;
+			}
+		}
+		menuErrorMsg[0] = 0; // clear error msg
+	}
+}
+
+
diff --git a/gp2x/menu.h b/gp2x/menu.h
index 727f46a..87c51b5 100644
--- a/gp2x/menu.h
+++ b/gp2x/menu.h
@@ -9,6 +9,7 @@ void gp2x_text_out8  (int x, int y, const char *texto, ...);
 void gp2x_text_out15 (int x, int y, const char *text);
 void gp2x_text_out8_2(int x, int y, const char *texto, int color);
 void menu_loop(void);
+int  menu_loop_tray(void);
 
 #define CONFIGURABLE_KEYS \
 	(GP2X_UP|GP2X_DOWN|GP2X_LEFT|GP2X_RIGHT|GP2X_A|GP2X_B|GP2X_X|GP2X_Y| \