From 76d5fa79af07f10251eebb0c92265cdffbebfc0e Mon Sep 17 00:00:00 2001 From: OBattler Date: Sat, 10 May 2025 05:01:57 +0200 Subject: [PATCH] Improve machine availability checking so that it absolutely never uses the legacy method if the machine has a device and the device has a CONFIG_BIOS setting. --- src/device.c | 63 ++++++++++++++++++++++---------------- src/include/86box/device.h | 2 ++ src/machine/machine.c | 20 +++++++----- 3 files changed, 52 insertions(+), 33 deletions(-) diff --git a/src/device.c b/src/device.c index ecd523274..1922806b9 100644 --- a/src/device.c +++ b/src/device.c @@ -390,37 +390,18 @@ device_get_priv(const device_t *dev) int device_available(const device_t *dev) { - if (dev != NULL) { - const device_config_t *config = dev->config; - if ((config != NULL) && (config->type == CONFIG_BIOS)) { - int roms_present = 0; - const device_config_bios_t *bios = (const device_config_bios_t *) config->bios; - - /* Go through the ROM's in the device configuration. */ - while ((bios != NULL) && - (bios->name != NULL) && - (bios->internal_name != NULL) && - (bios->files_no != 0)) { - int i = 0; - for (uint8_t bf = 0; bf < bios->files_no; bf++) - i += !!rom_present(bios->files[bf]); - if (i == bios->files_no) - roms_present++; - bios++; - } - - return (roms_present ? -1 : 0); - } + int ret = machine_device_available(dev); + if (ret == 0) { /* No CONFIG_BIOS field present, use the classic available(). */ if (dev->available != NULL) - return (dev->available()); + ret = (dev->available()); else - return 1; - } + ret = (dev != NULL); + } else + ret = (ret == -1); - /* A NULL device is never available. */ - return 0; + return ret; } uint8_t @@ -964,6 +945,36 @@ machine_get_config_string(char *str) return ret; } +int +machine_device_available(const device_t *dev) +{ + if (dev != NULL) { + const device_config_t *config = dev->config; + if ((config != NULL) && (config->type == CONFIG_BIOS)) { + int roms_present = 0; + const device_config_bios_t *bios = (const device_config_bios_t *) config->bios; + + /* Go through the ROM's in the device configuration. */ + while ((bios != NULL) && + (bios->name != NULL) && + (bios->internal_name != NULL) && + (bios->files_no != 0)) { + int i = 0; + for (uint8_t bf = 0; bf < bios->files_no; bf++) + i += !!rom_present(bios->files[bf]); + if (i == bios->files_no) + roms_present++; + bios++; + } + + return (roms_present ? -1 : -2); + } + } + + /* NULL device or no CONFIG_BIOS field, return 0. */ + return 0; +} + const device_t * device_context_get_device(void) { diff --git a/src/include/86box/device.h b/src/include/86box/device.h index 91ff2daa6..76f12a0c5 100644 --- a/src/include/86box/device.h +++ b/src/include/86box/device.h @@ -239,6 +239,8 @@ extern const char *device_get_internal_name(const device_t *dev); extern int machine_get_config_int(char *str); extern const char *machine_get_config_string(char *str); +extern int machine_device_available(const device_t *dev); + extern const device_t device_none; extern const device_t device_internal; diff --git a/src/machine/machine.c b/src/machine/machine.c index b171dd505..505674000 100644 --- a/src/machine/machine.c +++ b/src/machine/machine.c @@ -139,18 +139,24 @@ machine_init(void) int machine_available(int m) { - int ret; + int ret = 0; const device_t *dev = machine_get_device(m); - bios_only = 1; + if (dev != NULL) + ret = machine_device_available(dev); + /* + Only via machine_init_ex() if the device is NULL or + it lacks a CONFIG_BIOS field (or the CONFIG_BIOS field + is not the first in list. + */ + if (ret == 0) { + bios_only = 1; - ret = device_available(dev); - /* Do not check via machine_init_ex() if the device is not NULL and - it has a CONFIG_BIOS field. */ - if ((dev == NULL) || (ret != -1)) ret = machine_init_ex(m); - bios_only = 0; + bios_only = 0; + } else if (ret == -2) + ret = 0; return !!ret; }