From e321a4211c7c324993f30e1d2c24fa44e5438d03 Mon Sep 17 00:00:00 2001 From: Christopher Bonhage Date: Wed, 22 Jan 2025 10:36:20 -0500 Subject: [PATCH] Early return on cart_init if already initialized Disambiguates the undefined vs null detection states. --- include/cart.h | 1 + src/cart/cartexit.c | 9 +++++++-- src/cart/cartinit.c | 29 ++++++++++++++++++----------- 3 files changed, 26 insertions(+), 13 deletions(-) diff --git a/include/cart.h b/include/cart.h index 4ac8c34..610b6c1 100644 --- a/include/cart.h +++ b/include/cart.h @@ -9,6 +9,7 @@ typedef uint32_t u32; #endif /* Cartridge types */ +#define CART_UNDEFINED -2 #define CART_NULL -1 #define CART_CI 0 /* 64Drive */ #define CART_EDX 1 /* EverDrive-64 X-series */ diff --git a/src/cart/cartexit.c b/src/cart/cartexit.c index 67e94aa..b31ef25 100644 --- a/src/cart/cartexit.c +++ b/src/cart/cartexit.c @@ -10,6 +10,11 @@ int cart_exit(void) ed_exit, sc_exit, }; - if (cart_type < 0) return -1; - return exit[cart_type](); + int result = -1; + if (cart_type >= 0) + { + result = exit[cart_type](); + } + cart_type = CART_UNDEFINED; + return result; } diff --git a/src/cart/cartinit.c b/src/cart/cartinit.c index e8a8123..7f917ba 100644 --- a/src/cart/cartinit.c +++ b/src/cart/cartinit.c @@ -1,7 +1,7 @@ #include #include "cartint.h" -int cart_type = CART_NULL; +int cart_type = CART_UNDEFINED; int cart_init(void) { @@ -13,8 +13,17 @@ int cart_init(void) sc_init, }; int i, result; + /* bail if already initialized */ + if (cart_type != CART_UNDEFINED) + { + return -1; + } /* bbplayer */ - if ((IO_READ(MI_VERSION_REG) & 0xF0) == 0xB0) return -1; + if ((IO_READ(MI_VERSION_REG) & 0xF0) == 0xB0) + { + cart_type = CART_NULL; + return -1; + } if (!__cart_dom1) { __cart_dom1 = 0x8030FFFF; @@ -23,17 +32,15 @@ int cart_init(void) __cart_acs_rel(); } if (!__cart_dom2) __cart_dom2 = __cart_dom1; - if (cart_type < 0) + /* detect */ + for (i = 0; i < CART_MAX; i++) { - for (i = 0; i < CART_MAX; i++) + if ((result = init[i]()) >= 0) { - if ((result = init[i]()) >= 0) - { - cart_type = i; - return result; - } + cart_type = i; + return result; } - return -1; } - return init[cart_type](); + cart_type = CART_NULL; + return -1; }