From a614e935fb5dd99f7157da9ff1db2896edb7c172 Mon Sep 17 00:00:00 2001 From: OBattler Date: Sat, 25 May 2024 02:00:19 +0200 Subject: [PATCH] Clean-ups in device.c/h and the _params() device add functions now work exactly as originally planned. --- src/device.c | 132 +++++++++++------------------- src/include/86box/device.h | 22 ++--- src/network/net_eeprom_nmc93cxx.c | 12 +-- src/network/net_rtl8139.c | 4 +- src/network/net_tulip.c | 4 +- 5 files changed, 63 insertions(+), 111 deletions(-) diff --git a/src/device.c b/src/device.c index 5e3b394c2..24042b83a 100644 --- a/src/device.c +++ b/src/device.c @@ -45,14 +45,11 @@ #include #include #include -#include #define HAVE_STDARG_H #include <86box/86box.h> #include <86box/ini.h> #include <86box/config.h> #include <86box/device.h> -#include <86box/timer.h> -#include <86box/lpt.h> #include <86box/machine.h> #include <86box/mem.h> #include <86box/rom.h> @@ -94,9 +91,6 @@ device_init(void) void device_set_context(device_context_t *c, const device_t *dev, int inst) { - const void *sec; - void *single_sec; - memset(c, 0, sizeof(device_context_t)); c->dev = dev; c->instance = inst; @@ -106,8 +100,8 @@ device_set_context(device_context_t *c, const device_t *dev, int inst) /* If this is the first instance and a numbered section is not present, but a non-numbered section of the same name is, rename the non-numbered section to numbered. */ if (inst == 1) { - sec = config_find_section(c->name); - single_sec = config_find_section((char *) dev->name); + const void *sec = config_find_section(c->name); + void * single_sec = config_find_section((char *) dev->name); if ((sec == NULL) && (single_sec != NULL)) config_rename_section(single_sec, c->name); } @@ -141,10 +135,18 @@ device_context_restore(void) } static void * -device_add_common(const device_t *dev, const device_t *cd, void *p, void *params, int inst) +device_add_common(const device_t *dev, void *p, void *params, int inst) { - void *priv = NULL; - int c; + device_t *init_dev; + void *priv = NULL; + int c; + + if (params != NULL) { + init_dev = calloc(1, sizeof(device_t)); + memcpy(init_dev, dev, sizeof(device_t)); + init_dev->local |= (uintptr_t) params; + } else + init_dev = (device_t *) dev; for (c = 0; c < 256; c++) { if (!inst && (devices[c] == dev)) { @@ -154,7 +156,7 @@ device_add_common(const device_t *dev, const device_t *cd, void *p, void *params if (devices[c] == NULL) break; } - if ((c >= DEVICE_MAX) || (c >= 256)) { + if (c >= DEVICE_MAX) { fatal("DEVICE: too many devices\n"); return NULL; } @@ -167,33 +169,43 @@ device_add_common(const device_t *dev, const device_t *cd, void *p, void *params if (p == NULL) { memcpy(&device_prev, &device_current, sizeof(device_context_t)); - device_set_context(&device_current, cd, inst); + device_set_context(&device_current, dev, inst); if (dev->init != NULL) { - priv = (dev->flags & DEVICE_EXTPARAMS) ? dev->init_ext(dev, params) : dev->init(dev); + /* Give it our temporary device in case we have dynamically changed info->local. */ + priv = dev->init(init_dev); + if (priv == NULL) { +#ifdef ENABLE_DEVICE_LOG if (dev->name) device_log("DEVICE: device '%s' init failed\n", dev->name); else device_log("DEVICE: device init failed\n"); +#endif devices[c] = NULL; device_priv[c] = NULL; + free(init_dev); + return (NULL); } } +#ifdef ENABLE_DEVICE_LOG if (dev->name) device_log("DEVICE: device '%s' init successful\n", dev->name); else device_log("DEVICE: device init successful\n"); +#endif memcpy(&device_current, &device_prev, sizeof(device_context_t)); device_priv[c] = priv; } else device_priv[c] = p; + free(init_dev); + return priv; } @@ -209,7 +221,7 @@ device_get_internal_name(const device_t *dev) void * device_add(const device_t *dev) { - return device_add_common(dev, dev, NULL, NULL, 0); + return device_add_common(dev, NULL, NULL, 0); } void * @@ -217,105 +229,53 @@ device_add_linked(const device_t *dev, void *priv) { void *ret; device_common_priv = priv; - ret = device_add_common(dev, dev, NULL, NULL, 0); + ret = device_add_common(dev, NULL, NULL, 0); device_common_priv = NULL; return ret; } void * -device_add_parameters(const device_t *dev, void *params) +device_add_params(const device_t *dev, void *params) { - return device_add_common(dev, dev, NULL, params, 0); + return device_add_common(dev, NULL, params, 0); } /* For devices that do not have an init function (internal video etc.) */ void device_add_ex(const device_t *dev, void *priv) { - device_add_common(dev, dev, priv, NULL, 0); + device_add_common(dev, priv, NULL, 0); } void -device_add_ex_parameters(const device_t *dev, void *priv, void *params) +device_add_ex_params(const device_t *dev, void *priv, void *params) { - device_add_common(dev, dev, priv, params, 0); + device_add_common(dev, priv, params, 0); } void * device_add_inst(const device_t *dev, int inst) { - return device_add_common(dev, dev, NULL, NULL, inst); + return device_add_common(dev, NULL, NULL, inst); } void * -device_add_inst_parameters(const device_t *dev, int inst, void *params) +device_add_inst_params(const device_t *dev, int inst, void *params) { - return device_add_common(dev, dev, NULL, params, inst); + return device_add_common(dev, NULL, params, inst); } /* For devices that do not have an init function (internal video etc.) */ void device_add_inst_ex(const device_t *dev, void *priv, int inst) { - device_add_common(dev, dev, priv, NULL, inst); + device_add_common(dev, priv, NULL, inst); } void -device_add_inst_ex_parameters(const device_t *dev, void *priv, int inst, void *params) +device_add_inst_ex_params(const device_t *dev, void *priv, int inst, void *params) { - device_add_common(dev, dev, priv, params, inst); -} - -/* These eight are to add a device with another device's context - will be - used to add machines' internal devices. */ -void * -device_cadd(const device_t *dev, const device_t *cd) -{ - return device_add_common(dev, cd, NULL, NULL, 0); -} - -void * -device_cadd_parameters(const device_t *dev, const device_t *cd, void *params) -{ - return device_add_common(dev, cd, NULL, params, 0); -} - -/* For devices that do not have an init function (internal video etc.) */ -void -device_cadd_ex(const device_t *dev, const device_t *cd, void *priv) -{ - device_add_common(dev, cd, priv, NULL, 0); -} - -void -device_cadd_ex_parameters(const device_t *dev, const device_t *cd, void *priv, void *params) -{ - device_add_common(dev, cd, priv, params, 0); -} - -void * -device_cadd_inst(const device_t *dev, const device_t *cd, int inst) -{ - return device_add_common(dev, cd, NULL, NULL, inst); -} - -void * -device_cadd_inst_parameters(const device_t *dev, const device_t *cd, int inst, void *params) -{ - return device_add_common(dev, cd, NULL, params, inst); -} - -/* For devices that do not have an init function (internal video etc.) */ -void -device_cadd_inst_ex(const device_t *dev, const device_t *cd, void *priv, int inst) -{ - device_add_common(dev, cd, priv, NULL, inst); -} - -void -device_cadd_inst_ex_parameters(const device_t *dev, const device_t *cd, void *priv, int inst, void *params) -{ - device_add_common(dev, cd, priv, params, inst); + device_add_common(dev, priv, params, inst); } void * @@ -329,8 +289,10 @@ device_close_all(void) { for (int16_t c = (DEVICE_MAX - 1); c >= 0; c--) { if (devices[c] != NULL) { +#ifdef ENABLE_DEVICE_LOG if (devices[c]->name) device_log("Closing device: \"%s\"...\n", devices[c]->name); +#endif if (devices[c]->close != NULL) devices[c]->close(device_priv[c]); devices[c] = device_priv[c] = NULL; @@ -388,21 +350,21 @@ device_get_priv(const device_t *dev) int device_available(const device_t *dev) { - const device_config_t *config = NULL; - const device_config_bios_t *bios = NULL; - int roms_present = 0; - int i = 0; + const device_config_t *config = NULL; + const device_config_bios_t *bios = NULL; if (dev != NULL) { config = dev->config; if (config != NULL) { while (config->type != -1) { if (config->type == CONFIG_BIOS) { + int roms_present = 0; + bios = (const device_config_bios_t *) config->bios; /* Go through the ROM's in the device configuration. */ while (bios->files_no != 0) { - i = 0; + int i = 0; for (int bf = 0; bf < bios->files_no; bf++) i += !!rom_present(bios->files[bf]); if (i == bios->files_no) @@ -615,7 +577,7 @@ device_force_redraw(void) } } -const int +int device_get_instance(void) { return device_current.instance; diff --git a/src/include/86box/device.h b/src/include/86box/device.h index cc564ceb2..30ce1c093 100644 --- a/src/include/86box/device.h +++ b/src/include/86box/device.h @@ -102,9 +102,7 @@ enum { DEVICE_LPT = 0x200000, /* requires a parallel port */ DEVICE_KBC = 0x400000, /* is a keyboard controller */ - DEVICE_ONBOARD = 0x20000000, /* is on-board */ - DEVICE_EXTPARAMS = 0x40000000, /* accepts extended parameters */ - + DEVICE_ONBOARD = 0x40000000, /* is on-board */ DEVICE_PIT = 0x80000000, /* device is a PIT */ DEVICE_ALL = 0xffffffff /* match all devices */ @@ -204,21 +202,13 @@ extern void device_context_inst(const device_t *dev, int inst); extern void device_context_restore(void); extern void *device_add(const device_t *d); extern void *device_add_linked(const device_t *d, void *priv); -extern void *device_add_parameters(const device_t *dev, void *params); +extern void *device_add_params(const device_t *dev, void *params); extern void device_add_ex(const device_t *dev, void *priv); -extern void device_add_ex_parameters(const device_t *dev, void *priv, void *params); +extern void device_add_ex_params(const device_t *dev, void *priv, void *params); extern void *device_add_inst(const device_t *dev, int inst); -extern void *device_add_inst_parameters(const device_t *dev, int inst, void *params); +extern void *device_add_inst_params(const device_t *dev, int inst, void *params); extern void device_add_inst_ex(const device_t *dev, void *priv, int inst); -extern void device_add_inst_ex_parameters(const device_t *dev, void *priv, int inst, void *params); -extern void *device_cadd(const device_t *dev, const device_t *cd); -extern void *device_cadd_parameters(const device_t *dev, const device_t *cd, void *params); -extern void device_cadd_ex(const device_t *dev, const device_t *cd, void *priv); -extern void device_cadd_ex_parameters(const device_t *dev, const device_t *cd, void *priv, void *params); -extern void *device_cadd_inst(const device_t *dev, const device_t *cd, int inst); -extern void *device_cadd_inst_parameters(const device_t *dev, const device_t *cd, int inst, void *params); -extern void device_cadd_inst_ex(const device_t *dev, const device_t *cd, void *priv, int inst); -extern void device_cadd_inst_ex_parameters(const device_t *dev, const device_t *cd, void *priv, int inst, void *params); +extern void device_add_inst_ex_params(const device_t *dev, void *priv, int inst, void *params); extern void *device_get_common_priv(void); extern void device_close_all(void); extern void device_reset_all(uint32_t match_flags); @@ -246,7 +236,7 @@ extern void device_set_config_hex16(const char *s, int val); extern void device_set_config_hex20(const char *s, int val); extern void device_set_config_mac(const char *s, int val); extern const char *device_get_config_string(const char *name); -extern const int device_get_instance(void); +extern int device_get_instance(void); #define device_get_config_bios device_get_config_string extern const char *device_get_internal_name(const device_t *dev); diff --git a/src/network/net_eeprom_nmc93cxx.c b/src/network/net_eeprom_nmc93cxx.c index 681263717..3ad6bd030 100644 --- a/src/network/net_eeprom_nmc93cxx.c +++ b/src/network/net_eeprom_nmc93cxx.c @@ -60,14 +60,14 @@ nmc93cxx_eeprom_log(int lvl, const char *fmt, ...) #endif static void * -nmc93cxx_eeprom_init_params(UNUSED(const device_t *info), void *params) +nmc93cxx_eeprom_init(const device_t *info) { uint16_t nwords = 64; uint8_t addrbits = 6; uint8_t filldefault = 1; - nmc93cxx_eeprom_params_t *params_details = (nmc93cxx_eeprom_params_t *) params; + nmc93cxx_eeprom_params_t *params_details = (nmc93cxx_eeprom_params_t *) info->local; nmc93cxx_eeprom_t *eeprom = NULL; - if (!params) + if (info->local == 0) return NULL; nwords = params_details->nwords; @@ -263,7 +263,7 @@ nmc93cxx_eeprom_close(void *priv) uint16_t * nmc93cxx_eeprom_data(nmc93cxx_eeprom_t *eeprom) { - if (UNLIKELY(!eeprom)) + if (UNLIKELY(eeprom == NULL)) return NULL; /* Get EEPROM data array. */ return &eeprom->dev.data[0]; @@ -272,9 +272,9 @@ nmc93cxx_eeprom_data(nmc93cxx_eeprom_t *eeprom) const device_t nmc93cxx_device = { .name = "National Semiconductor NMC93Cxx", .internal_name = "nmc93cxx", - .flags = DEVICE_EXTPARAMS, + .flags = 0, .local = 0, - .init_ext = nmc93cxx_eeprom_init_params, + .init = nmc93cxx_eeprom_init, .close = nmc93cxx_eeprom_close, .reset = NULL, { .available = NULL }, diff --git a/src/network/net_rtl8139.c b/src/network/net_rtl8139.c index a7c007115..2e85d67bf 100644 --- a/src/network/net_rtl8139.c +++ b/src/network/net_rtl8139.c @@ -3283,8 +3283,8 @@ nic_init(const device_t *info) params.default_content = (uint16_t *) s->eeprom_data; params.filename = filename; snprintf(filename, sizeof(filename), "nmc93cxx_eeprom_%s_%d.nvr", info->internal_name, device_get_instance()); - s->eeprom = device_add_parameters(&nmc93cxx_device, ¶ms); - if (!s->eeprom) { + s->eeprom = device_add_params(&nmc93cxx_device, ¶ms); + if (s->eeprom == NULL) { free(s); return NULL; } diff --git a/src/network/net_tulip.c b/src/network/net_tulip.c index e34327eb5..03eb21e88 100644 --- a/src/network/net_tulip.c +++ b/src/network/net_tulip.c @@ -1613,8 +1613,8 @@ nic_init(const device_t *info) params.default_content = (uint16_t *) s->eeprom_data; params.filename = filename; snprintf(filename, sizeof(filename), "nmc93cxx_eeprom_%s_%d.nvr", info->internal_name, device_get_instance()); - s->eeprom = device_add_parameters(&nmc93cxx_device, ¶ms); - if (!s->eeprom) { + s->eeprom = device_add_params(&nmc93cxx_device, ¶ms); + if (s->eeprom == NULL) { free(s); return NULL; }