Clean-ups in device.c/h and the _params() device add functions now work exactly as originally planned.

This commit is contained in:
OBattler
2024-05-25 02:00:19 +02:00
parent beddf47de8
commit a614e935fb
5 changed files with 63 additions and 111 deletions

View File

@@ -45,14 +45,11 @@
#include <stdlib.h> #include <stdlib.h>
#include <stdint.h> #include <stdint.h>
#include <string.h> #include <string.h>
#include <wchar.h>
#define HAVE_STDARG_H #define HAVE_STDARG_H
#include <86box/86box.h> #include <86box/86box.h>
#include <86box/ini.h> #include <86box/ini.h>
#include <86box/config.h> #include <86box/config.h>
#include <86box/device.h> #include <86box/device.h>
#include <86box/timer.h>
#include <86box/lpt.h>
#include <86box/machine.h> #include <86box/machine.h>
#include <86box/mem.h> #include <86box/mem.h>
#include <86box/rom.h> #include <86box/rom.h>
@@ -94,9 +91,6 @@ device_init(void)
void void
device_set_context(device_context_t *c, const device_t *dev, int inst) 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)); memset(c, 0, sizeof(device_context_t));
c->dev = dev; c->dev = dev;
c->instance = inst; 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 /* 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. */ section of the same name is, rename the non-numbered section to numbered. */
if (inst == 1) { if (inst == 1) {
sec = config_find_section(c->name); const void *sec = config_find_section(c->name);
single_sec = config_find_section((char *) dev->name); void * single_sec = config_find_section((char *) dev->name);
if ((sec == NULL) && (single_sec != NULL)) if ((sec == NULL) && (single_sec != NULL))
config_rename_section(single_sec, c->name); config_rename_section(single_sec, c->name);
} }
@@ -141,10 +135,18 @@ device_context_restore(void)
} }
static 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; device_t *init_dev;
int c; 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++) { for (c = 0; c < 256; c++) {
if (!inst && (devices[c] == dev)) { 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) if (devices[c] == NULL)
break; break;
} }
if ((c >= DEVICE_MAX) || (c >= 256)) { if (c >= DEVICE_MAX) {
fatal("DEVICE: too many devices\n"); fatal("DEVICE: too many devices\n");
return NULL; return NULL;
} }
@@ -167,33 +169,43 @@ device_add_common(const device_t *dev, const device_t *cd, void *p, void *params
if (p == NULL) { if (p == NULL) {
memcpy(&device_prev, &device_current, sizeof(device_context_t)); 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) { 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) { if (priv == NULL) {
#ifdef ENABLE_DEVICE_LOG
if (dev->name) if (dev->name)
device_log("DEVICE: device '%s' init failed\n", dev->name); device_log("DEVICE: device '%s' init failed\n", dev->name);
else else
device_log("DEVICE: device init failed\n"); device_log("DEVICE: device init failed\n");
#endif
devices[c] = NULL; devices[c] = NULL;
device_priv[c] = NULL; device_priv[c] = NULL;
free(init_dev);
return (NULL); return (NULL);
} }
} }
#ifdef ENABLE_DEVICE_LOG
if (dev->name) if (dev->name)
device_log("DEVICE: device '%s' init successful\n", dev->name); device_log("DEVICE: device '%s' init successful\n", dev->name);
else else
device_log("DEVICE: device init successful\n"); device_log("DEVICE: device init successful\n");
#endif
memcpy(&device_current, &device_prev, sizeof(device_context_t)); memcpy(&device_current, &device_prev, sizeof(device_context_t));
device_priv[c] = priv; device_priv[c] = priv;
} else } else
device_priv[c] = p; device_priv[c] = p;
free(init_dev);
return priv; return priv;
} }
@@ -209,7 +221,7 @@ device_get_internal_name(const device_t *dev)
void * void *
device_add(const device_t *dev) device_add(const device_t *dev)
{ {
return device_add_common(dev, dev, NULL, NULL, 0); return device_add_common(dev, NULL, NULL, 0);
} }
void * void *
@@ -217,105 +229,53 @@ device_add_linked(const device_t *dev, void *priv)
{ {
void *ret; void *ret;
device_common_priv = priv; 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; device_common_priv = NULL;
return ret; return ret;
} }
void * 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.) */ /* For devices that do not have an init function (internal video etc.) */
void void
device_add_ex(const device_t *dev, void *priv) 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 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 * void *
device_add_inst(const device_t *dev, int inst) 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 * 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.) */ /* For devices that do not have an init function (internal video etc.) */
void void
device_add_inst_ex(const device_t *dev, void *priv, int inst) 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 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); device_add_common(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);
} }
void * void *
@@ -329,8 +289,10 @@ device_close_all(void)
{ {
for (int16_t c = (DEVICE_MAX - 1); c >= 0; c--) { for (int16_t c = (DEVICE_MAX - 1); c >= 0; c--) {
if (devices[c] != NULL) { if (devices[c] != NULL) {
#ifdef ENABLE_DEVICE_LOG
if (devices[c]->name) if (devices[c]->name)
device_log("Closing device: \"%s\"...\n", devices[c]->name); device_log("Closing device: \"%s\"...\n", devices[c]->name);
#endif
if (devices[c]->close != NULL) if (devices[c]->close != NULL)
devices[c]->close(device_priv[c]); devices[c]->close(device_priv[c]);
devices[c] = device_priv[c] = NULL; devices[c] = device_priv[c] = NULL;
@@ -388,21 +350,21 @@ device_get_priv(const device_t *dev)
int int
device_available(const device_t *dev) device_available(const device_t *dev)
{ {
const device_config_t *config = NULL; const device_config_t *config = NULL;
const device_config_bios_t *bios = NULL; const device_config_bios_t *bios = NULL;
int roms_present = 0;
int i = 0;
if (dev != NULL) { if (dev != NULL) {
config = dev->config; config = dev->config;
if (config != NULL) { if (config != NULL) {
while (config->type != -1) { while (config->type != -1) {
if (config->type == CONFIG_BIOS) { if (config->type == CONFIG_BIOS) {
int roms_present = 0;
bios = (const device_config_bios_t *) config->bios; bios = (const device_config_bios_t *) config->bios;
/* Go through the ROM's in the device configuration. */ /* Go through the ROM's in the device configuration. */
while (bios->files_no != 0) { while (bios->files_no != 0) {
i = 0; int i = 0;
for (int bf = 0; bf < bios->files_no; bf++) for (int bf = 0; bf < bios->files_no; bf++)
i += !!rom_present(bios->files[bf]); i += !!rom_present(bios->files[bf]);
if (i == bios->files_no) if (i == bios->files_no)
@@ -615,7 +577,7 @@ device_force_redraw(void)
} }
} }
const int int
device_get_instance(void) device_get_instance(void)
{ {
return device_current.instance; return device_current.instance;

View File

@@ -102,9 +102,7 @@ enum {
DEVICE_LPT = 0x200000, /* requires a parallel port */ DEVICE_LPT = 0x200000, /* requires a parallel port */
DEVICE_KBC = 0x400000, /* is a keyboard controller */ DEVICE_KBC = 0x400000, /* is a keyboard controller */
DEVICE_ONBOARD = 0x20000000, /* is on-board */ DEVICE_ONBOARD = 0x40000000, /* is on-board */
DEVICE_EXTPARAMS = 0x40000000, /* accepts extended parameters */
DEVICE_PIT = 0x80000000, /* device is a PIT */ DEVICE_PIT = 0x80000000, /* device is a PIT */
DEVICE_ALL = 0xffffffff /* match all devices */ 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_context_restore(void);
extern void *device_add(const device_t *d); extern void *device_add(const device_t *d);
extern void *device_add_linked(const device_t *d, void *priv); 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(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(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(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_add_inst_ex_params(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_get_common_priv(void); extern void *device_get_common_priv(void);
extern void device_close_all(void); extern void device_close_all(void);
extern void device_reset_all(uint32_t match_flags); 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_hex20(const char *s, int val);
extern void device_set_config_mac(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 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 #define device_get_config_bios device_get_config_string
extern const char *device_get_internal_name(const device_t *dev); extern const char *device_get_internal_name(const device_t *dev);

View File

@@ -60,14 +60,14 @@ nmc93cxx_eeprom_log(int lvl, const char *fmt, ...)
#endif #endif
static void * static void *
nmc93cxx_eeprom_init_params(UNUSED(const device_t *info), void *params) nmc93cxx_eeprom_init(const device_t *info)
{ {
uint16_t nwords = 64; uint16_t nwords = 64;
uint8_t addrbits = 6; uint8_t addrbits = 6;
uint8_t filldefault = 1; 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; nmc93cxx_eeprom_t *eeprom = NULL;
if (!params) if (info->local == 0)
return NULL; return NULL;
nwords = params_details->nwords; nwords = params_details->nwords;
@@ -263,7 +263,7 @@ nmc93cxx_eeprom_close(void *priv)
uint16_t * uint16_t *
nmc93cxx_eeprom_data(nmc93cxx_eeprom_t *eeprom) nmc93cxx_eeprom_data(nmc93cxx_eeprom_t *eeprom)
{ {
if (UNLIKELY(!eeprom)) if (UNLIKELY(eeprom == NULL))
return NULL; return NULL;
/* Get EEPROM data array. */ /* Get EEPROM data array. */
return &eeprom->dev.data[0]; return &eeprom->dev.data[0];
@@ -272,9 +272,9 @@ nmc93cxx_eeprom_data(nmc93cxx_eeprom_t *eeprom)
const device_t nmc93cxx_device = { const device_t nmc93cxx_device = {
.name = "National Semiconductor NMC93Cxx", .name = "National Semiconductor NMC93Cxx",
.internal_name = "nmc93cxx", .internal_name = "nmc93cxx",
.flags = DEVICE_EXTPARAMS, .flags = 0,
.local = 0, .local = 0,
.init_ext = nmc93cxx_eeprom_init_params, .init = nmc93cxx_eeprom_init,
.close = nmc93cxx_eeprom_close, .close = nmc93cxx_eeprom_close,
.reset = NULL, .reset = NULL,
{ .available = NULL }, { .available = NULL },

View File

@@ -3283,8 +3283,8 @@ nic_init(const device_t *info)
params.default_content = (uint16_t *) s->eeprom_data; params.default_content = (uint16_t *) s->eeprom_data;
params.filename = filename; params.filename = filename;
snprintf(filename, sizeof(filename), "nmc93cxx_eeprom_%s_%d.nvr", info->internal_name, device_get_instance()); snprintf(filename, sizeof(filename), "nmc93cxx_eeprom_%s_%d.nvr", info->internal_name, device_get_instance());
s->eeprom = device_add_parameters(&nmc93cxx_device, &params); s->eeprom = device_add_params(&nmc93cxx_device, &params);
if (!s->eeprom) { if (s->eeprom == NULL) {
free(s); free(s);
return NULL; return NULL;
} }

View File

@@ -1613,8 +1613,8 @@ nic_init(const device_t *info)
params.default_content = (uint16_t *) s->eeprom_data; params.default_content = (uint16_t *) s->eeprom_data;
params.filename = filename; params.filename = filename;
snprintf(filename, sizeof(filename), "nmc93cxx_eeprom_%s_%d.nvr", info->internal_name, device_get_instance()); snprintf(filename, sizeof(filename), "nmc93cxx_eeprom_%s_%d.nvr", info->internal_name, device_get_instance());
s->eeprom = device_add_parameters(&nmc93cxx_device, &params); s->eeprom = device_add_params(&nmc93cxx_device, &params);
if (!s->eeprom) { if (s->eeprom == NULL) {
free(s); free(s);
return NULL; return NULL;
} }