Cleanups in device

This commit is contained in:
Jasmine Iwanek
2025-01-06 23:28:08 -05:00
parent 2b465810f6
commit 738087ae13
2 changed files with 138 additions and 137 deletions

View File

@@ -19,7 +19,7 @@
* Copyright 2016-2019 Miran Grca. * Copyright 2016-2019 Miran Grca.
* Copyright 2008-2019 Sarah Walker. * Copyright 2008-2019 Sarah Walker.
* Copyright 2021 Andreas J. Reichel. * Copyright 2021 Andreas J. Reichel.
* Copyright 2021-2022 Jasmine Iwanek. * Copyright 2021-2025 Jasmine Iwanek.
* *
* This program is free software; you can redistribute it and/or modify * This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by * it under the terms of the GNU General Public License as published by
@@ -89,38 +89,38 @@ device_init(void)
} }
void void
device_set_context(device_context_t *c, const device_t *dev, int inst) device_set_context(device_context_t *ctx, const device_t *dev, int inst)
{ {
memset(c, 0, sizeof(device_context_t)); memset(ctx, 0, sizeof(device_context_t));
c->dev = dev; ctx->dev = dev;
c->instance = inst; ctx->instance = inst;
if (inst) { if (inst) {
sprintf(c->name, "%s #%i", dev->name, inst); sprintf(ctx->name, "%s #%i", dev->name, inst);
/* If a numbered section is not present, but a non-numbered of the same name /* If a numbered section is not present, but a non-numbered of the same name
is, rename the non-numbered section to numbered. */ is, rename the non-numbered section to numbered. */
const void *sec = config_find_section(c->name); const void *sec = config_find_section(ctx->name);
void * 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, ctx->name);
} else if (!strcmp(dev->name, "PS/2 Mouse")) { } else if (!strcmp(dev->name, "PS/2 Mouse")) {
sprintf(c->name, "%s", dev->name); sprintf(ctx->name, "%s", dev->name);
/* Migrate the old "Standard PS/2 Mouse" section */ /* Migrate the old "Standard PS/2 Mouse" section */
const void *sec = config_find_section(c->name); const void *sec = config_find_section(ctx->name);
void * old_sec = config_find_section("Standard PS/2 Mouse"); void * old_sec = config_find_section("Standard PS/2 Mouse");
if ((sec == NULL) && (old_sec != NULL)) if ((sec == NULL) && (old_sec != NULL))
config_rename_section(old_sec, c->name); config_rename_section(old_sec, ctx->name);
} else if (!strcmp(dev->name, "Microsoft RAMCard")) { } else if (!strcmp(dev->name, "Microsoft RAMCard")) {
sprintf(c->name, "%s", dev->name); sprintf(ctx->name, "%s", dev->name);
/* Migrate the old "Standard PS/2 Mouse" section */ /* Migrate the old "Microsoft RAMCard for IBM PC" section */
const void *sec = config_find_section(c->name); const void *sec = config_find_section(ctx->name);
void * old_sec = config_find_section("Microsoft RAMCard for IBM PC"); void * old_sec = config_find_section("Microsoft RAMCard for IBM PC");
if ((sec == NULL) && (old_sec != NULL)) if ((sec == NULL) && (old_sec != NULL))
config_rename_section(old_sec, c->name); config_rename_section(old_sec, ctx->name);
} else } else
sprintf(c->name, "%s", dev->name); sprintf(ctx->name, "%s", dev->name);
} }
static void static void
@@ -153,7 +153,7 @@ device_add_common(const device_t *dev, void *p, void *params, int inst)
{ {
device_t *init_dev = NULL; device_t *init_dev = NULL;
void *priv = NULL; void *priv = NULL;
int c; int16_t c;
if (params != NULL) { if (params != NULL) {
init_dev = calloc(1, sizeof(device_t)); init_dev = calloc(1, sizeof(device_t));
@@ -162,7 +162,7 @@ device_add_common(const device_t *dev, void *p, void *params, int inst)
} else } else
init_dev = (device_t *) dev; init_dev = (device_t *) dev;
for (c = 0; c < 256; c++) { for (c = 0; c < DEVICE_MAX; c++) {
if (!inst && (devices[c] == dev)) { if (!inst && (devices[c] == dev)) {
device_log("DEVICE: device already exists!\n"); device_log("DEVICE: device already exists!\n");
return (NULL); return (NULL);
@@ -244,6 +244,7 @@ void *
device_add_linked(const device_t *dev, void *priv) 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, NULL, NULL, 0); ret = device_add_common(dev, NULL, NULL, 0);
device_common_priv = NULL; device_common_priv = NULL;
@@ -311,7 +312,8 @@ device_close_all(void)
#endif #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] = NULL;
device_priv[c] = NULL;
} }
} }
} }
@@ -372,7 +374,7 @@ device_available(const device_t *dev)
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 != CONFIG_END) {
if (config->type == CONFIG_BIOS) { if (config->type == CONFIG_BIOS) {
int roms_present = 0; int roms_present = 0;
@@ -414,7 +416,7 @@ device_get_bios_file(const device_t *dev, const char *internal_name, int file_no
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 != CONFIG_END) {
if (config->type == CONFIG_BIOS) { if (config->type == CONFIG_BIOS) {
bios = config->bios; bios = config->bios;
@@ -452,7 +454,7 @@ device_has_config(const device_t *dev)
config = dev->config; config = dev->config;
while (config->type != -1) { while (config->type != CONFIG_END) {
c++; c++;
config++; config++;
} }
@@ -543,8 +545,7 @@ device_get_name(const device_t *dev, int bus, char *name)
strcat(pbus, ")"); strcat(pbus, ")");
/* Allocate the temporary device name string and set it to all zeroes. */ /* Allocate the temporary device name string and set it to all zeroes. */
tname = (char *) malloc(strlen(dev->name) + 1); tname = (char *) calloc(1, strlen(dev->name) + 1);
memset(tname, 0x00, strlen(dev->name) + 1);
/* First strip the bus string with parentheses. */ /* First strip the bus string with parentheses. */
fbus = strstr(dev->name, pbus); fbus = strstr(dev->name, pbus);
@@ -612,256 +613,256 @@ device_get_instance(void)
} }
const char * const char *
device_get_config_string(const char *s) device_get_config_string(const char *str)
{ {
const device_config_t *c = device_current.dev->config; const device_config_t *cfg = device_current.dev->config;
while (c && c->type != -1) { while (cfg && cfg->type != CONFIG_END) {
if (!strcmp(s, c->name)) if (!strcmp(str, cfg->name))
return (config_get_string((char *) device_current.name, (char *) s, (char *) c->default_string)); return (config_get_string((char *) device_current.name, (char *) str, (char *) cfg->default_string));
c++; cfg++;
} }
return (NULL); return (NULL);
} }
int int
device_get_config_int(const char *s) device_get_config_int(const char *str)
{ {
const device_config_t *c = device_current.dev->config; const device_config_t *cfg = device_current.dev->config;
while (c && c->type != -1) { while (cfg && cfg->type != CONFIG_END) {
if (!strcmp(s, c->name)) if (!strcmp(str, cfg->name))
return (config_get_int((char *) device_current.name, (char *) s, c->default_int)); return (config_get_int((char *) device_current.name, (char *) str, cfg->default_int));
c++; cfg++;
} }
return 0; return 0;
} }
int int
device_get_config_int_ex(const char *s, int def) device_get_config_int_ex(const char *str, int def)
{ {
const device_config_t *c = device_current.dev->config; const device_config_t *cfg = device_current.dev->config;
while (c && c->type != -1) { while (cfg && cfg->type != CONFIG_END) {
if (!strcmp(s, c->name)) if (!strcmp(str, cfg->name))
return (config_get_int((char *) device_current.name, (char *) s, def)); return (config_get_int((char *) device_current.name, (char *) str, def));
c++; cfg++;
} }
return def; return def;
} }
int int
device_get_config_hex16(const char *s) device_get_config_hex16(const char *str)
{ {
const device_config_t *c = device_current.dev->config; const device_config_t *cfg = device_current.dev->config;
while (c && c->type != -1) { while (cfg && cfg->type != CONFIG_END) {
if (!strcmp(s, c->name)) if (!strcmp(str, cfg->name))
return (config_get_hex16((char *) device_current.name, (char *) s, c->default_int)); return (config_get_hex16((char *) device_current.name, (char *) str, cfg->default_int));
c++; cfg++;
} }
return 0; return 0;
} }
int int
device_get_config_hex20(const char *s) device_get_config_hex20(const char *str)
{ {
const device_config_t *c = device_current.dev->config; const device_config_t *cfg = device_current.dev->config;
while (c && c->type != -1) { while (cfg && cfg->type != CONFIG_END) {
if (!strcmp(s, c->name)) if (!strcmp(str, cfg->name))
return (config_get_hex20((char *) device_current.name, (char *) s, c->default_int)); return (config_get_hex20((char *) device_current.name, (char *) str, cfg->default_int));
c++; cfg++;
} }
return 0; return 0;
} }
int int
device_get_config_mac(const char *s, int def) device_get_config_mac(const char *str, int def)
{ {
const device_config_t *c = device_current.dev->config; const device_config_t *cfg = device_current.dev->config;
while (c && c->type != -1) { while (cfg && cfg->type != CONFIG_END) {
if (!strcmp(s, c->name)) if (!strcmp(str, cfg->name))
return (config_get_mac((char *) device_current.name, (char *) s, def)); return (config_get_mac((char *) device_current.name, (char *) str, def));
c++; cfg++;
} }
return def; return def;
} }
void void
device_set_config_int(const char *s, int val) device_set_config_int(const char *str, int val)
{ {
const device_config_t *c = device_current.dev->config; const device_config_t *cfg = device_current.dev->config;
while (c && c->type != -1) { while (cfg && cfg->type != CONFIG_END) {
if (!strcmp(s, c->name)) { if (!strcmp(str, cfg->name)) {
config_set_int((char *) device_current.name, (char *) s, val); config_set_int((char *) device_current.name, (char *) str, val);
break; break;
} }
c++; cfg++;
} }
} }
void void
device_set_config_hex16(const char *s, int val) device_set_config_hex16(const char *str, int val)
{ {
const device_config_t *c = device_current.dev->config; const device_config_t *cfg = device_current.dev->config;
while (c && c->type != -1) { while (cfg && cfg->type != CONFIG_END) {
if (!strcmp(s, c->name)) { if (!strcmp(str, cfg->name)) {
config_set_hex16((char *) device_current.name, (char *) s, val); config_set_hex16((char *) device_current.name, (char *) str, val);
break; break;
} }
c++; cfg++;
} }
} }
void void
device_set_config_hex20(const char *s, int val) device_set_config_hex20(const char *str, int val)
{ {
const device_config_t *c = device_current.dev->config; const device_config_t *cfg = device_current.dev->config;
while (c && c->type != -1) { while (cfg && cfg->type != CONFIG_END) {
if (!strcmp(s, c->name)) { if (!strcmp(str, cfg->name)) {
config_set_hex20((char *) device_current.name, (char *) s, val); config_set_hex20((char *) device_current.name, (char *) str, val);
break; break;
} }
c++; cfg++;
} }
} }
void void
device_set_config_mac(const char *s, int val) device_set_config_mac(const char *str, int val)
{ {
const device_config_t *c = device_current.dev->config; const device_config_t *cfg = device_current.dev->config;
while (c && c->type != -1) { while (cfg && cfg->type != CONFIG_END) {
if (!strcmp(s, c->name)) { if (!strcmp(str, cfg->name)) {
config_set_mac((char *) device_current.name, (char *) s, val); config_set_mac((char *) device_current.name, (char *) str, val);
break; break;
} }
c++; cfg++;
} }
} }
int int
device_is_valid(const device_t *device, int m) device_is_valid(const device_t *device, int mch)
{ {
if (device == NULL) if (device == NULL)
return 1; return 1;
if ((device->flags & DEVICE_PCJR) && !machine_has_bus(m, MACHINE_BUS_PCJR)) if ((device->flags & DEVICE_PCJR) && !machine_has_bus(mch, MACHINE_BUS_PCJR))
return 0; return 0;
if ((device->flags & DEVICE_XTKBC) && machine_has_bus(m, MACHINE_BUS_ISA16) && !machine_has_bus(m, MACHINE_BUS_DM_KBC)) if ((device->flags & DEVICE_XTKBC) && machine_has_bus(mch, MACHINE_BUS_ISA16) && !machine_has_bus(mch, MACHINE_BUS_DM_KBC))
return 0; return 0;
if ((device->flags & DEVICE_AT) && !machine_has_bus(m, MACHINE_BUS_ISA16)) if ((device->flags & DEVICE_AT) && !machine_has_bus(mch, MACHINE_BUS_ISA16))
return 0; return 0;
if ((device->flags & DEVICE_ATKBC) && !machine_has_bus(m, MACHINE_BUS_ISA16) && !machine_has_bus(m, MACHINE_BUS_DM_KBC)) if ((device->flags & DEVICE_ATKBC) && !machine_has_bus(mch, MACHINE_BUS_ISA16) && !machine_has_bus(mch, MACHINE_BUS_DM_KBC))
return 0; return 0;
if ((device->flags & DEVICE_PS2) && !machine_has_bus(m, MACHINE_BUS_PS2_PORTS)) if ((device->flags & DEVICE_PS2) && !machine_has_bus(mch, MACHINE_BUS_PS2_PORTS))
return 0; return 0;
if ((device->flags & DEVICE_ISA) && !machine_has_bus(m, MACHINE_BUS_ISA)) if ((device->flags & DEVICE_ISA) && !machine_has_bus(mch, MACHINE_BUS_ISA))
return 0; return 0;
if ((device->flags & DEVICE_CBUS) && !machine_has_bus(m, MACHINE_BUS_CBUS)) if ((device->flags & DEVICE_CBUS) && !machine_has_bus(mch, MACHINE_BUS_CBUS))
return 0; return 0;
if ((device->flags & DEVICE_PCMCIA) && !machine_has_bus(m, MACHINE_BUS_PCMCIA) && !machine_has_bus(m, MACHINE_BUS_ISA)) if ((device->flags & DEVICE_PCMCIA) && !machine_has_bus(mch, MACHINE_BUS_PCMCIA) && !machine_has_bus(mch, MACHINE_BUS_ISA))
return 0; return 0;
if ((device->flags & DEVICE_MCA) && !machine_has_bus(m, MACHINE_BUS_MCA)) if ((device->flags & DEVICE_MCA) && !machine_has_bus(mch, MACHINE_BUS_MCA))
return 0; return 0;
if ((device->flags & DEVICE_HIL) && !machine_has_bus(m, MACHINE_BUS_HIL)) if ((device->flags & DEVICE_HIL) && !machine_has_bus(mch, MACHINE_BUS_HIL))
return 0; return 0;
if ((device->flags & DEVICE_EISA) && !machine_has_bus(m, MACHINE_BUS_EISA)) if ((device->flags & DEVICE_EISA) && !machine_has_bus(mch, MACHINE_BUS_EISA))
return 0; return 0;
if ((device->flags & DEVICE_AT32) && !machine_has_bus(m, MACHINE_BUS_AT32)) if ((device->flags & DEVICE_AT32) && !machine_has_bus(mch, MACHINE_BUS_AT32))
return 0; return 0;
if ((device->flags & DEVICE_OLB) && !machine_has_bus(m, MACHINE_BUS_OLB)) if ((device->flags & DEVICE_OLB) && !machine_has_bus(mch, MACHINE_BUS_OLB))
return 0; return 0;
if ((device->flags & DEVICE_VLB) && !machine_has_bus(m, MACHINE_BUS_VLB)) if ((device->flags & DEVICE_VLB) && !machine_has_bus(mch, MACHINE_BUS_VLB))
return 0; return 0;
if ((device->flags & DEVICE_PCI) && !machine_has_bus(m, MACHINE_BUS_PCI)) if ((device->flags & DEVICE_PCI) && !machine_has_bus(mch, MACHINE_BUS_PCI))
return 0; return 0;
if ((device->flags & DEVICE_CARDBUS) && !machine_has_bus(m, MACHINE_BUS_CARDBUS) && !machine_has_bus(m, MACHINE_BUS_PCI)) if ((device->flags & DEVICE_CARDBUS) && !machine_has_bus(mch, MACHINE_BUS_CARDBUS) && !machine_has_bus(mch, MACHINE_BUS_PCI))
return 0; return 0;
if ((device->flags & DEVICE_USB) && !machine_has_bus(m, MACHINE_BUS_USB)) if ((device->flags & DEVICE_USB) && !machine_has_bus(mch, MACHINE_BUS_USB))
return 0; return 0;
if ((device->flags & DEVICE_AGP) && !machine_has_bus(m, MACHINE_BUS_AGP)) if ((device->flags & DEVICE_AGP) && !machine_has_bus(mch, MACHINE_BUS_AGP))
return 0; return 0;
if ((device->flags & DEVICE_AC97) && !machine_has_bus(m, MACHINE_BUS_AC97)) if ((device->flags & DEVICE_AC97) && !machine_has_bus(mch, MACHINE_BUS_AC97))
return 0; return 0;
return 1; return 1;
} }
int int
machine_get_config_int(char *s) machine_get_config_int(char *str)
{ {
const device_t *d = machine_get_device(machine); const device_t *dev = machine_get_device(machine);
const device_config_t *c; const device_config_t *cfg;
if (d == NULL) if (dev == NULL)
return 0; return 0;
c = d->config; cfg = dev->config;
while (c && c->type != -1) { while (cfg && cfg->type != CONFIG_END) {
if (!strcmp(s, c->name)) if (!strcmp(str, cfg->name))
return (config_get_int((char *) d->name, s, c->default_int)); return (config_get_int((char *) dev->name, str, cfg->default_int));
c++; cfg++;
} }
return 0; return 0;
} }
char * char *
machine_get_config_string(char *s) machine_get_config_string(char *str)
{ {
const device_t *d = machine_get_device(machine); const device_t *dev = machine_get_device(machine);
const device_config_t *c; const device_config_t *cfg;
if (d == NULL) if (dev == NULL)
return 0; return 0;
c = d->config; cfg = dev->config;
while (c && c->type != -1) { while (cfg && cfg->type != CONFIG_END) {
if (!strcmp(s, c->name)) if (!strcmp(str, cfg->name))
return (config_get_string((char *) d->name, s, (char *) c->default_string)); return (config_get_string((char *) dev->name, str, (char *) cfg->default_string));
c++; cfg++;
} }
return NULL; return NULL;
@@ -881,7 +882,7 @@ const device_t device_none = {
.init = NULL, .init = NULL,
.close = NULL, .close = NULL,
.reset = NULL, .reset = NULL,
{ .available = NULL }, .available = NULL,
.speed_changed = NULL, .speed_changed = NULL,
.force_redraw = NULL, .force_redraw = NULL,
.config = NULL .config = NULL
@@ -895,7 +896,7 @@ const device_t device_internal = {
.init = NULL, .init = NULL,
.close = NULL, .close = NULL,
.reset = NULL, .reset = NULL,
{ .available = NULL }, .available = NULL,
.speed_changed = NULL, .speed_changed = NULL,
.force_redraw = NULL, .force_redraw = NULL,
.config = NULL .config = NULL

View File

@@ -18,7 +18,7 @@
* Copyright 2016-2019 Miran Grca. * Copyright 2016-2019 Miran Grca.
* Copyright 2008-2019 Sarah Walker. * Copyright 2008-2019 Sarah Walker.
* Copyright 2021 Andreas J. Reichel. * Copyright 2021 Andreas J. Reichel.
* Copyright 2021-2022 Jasmine Iwanek. * Copyright 2021-2025 Jasmine Iwanek.
* *
* This program is free software; you can redistribute it and/or modify * This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by * it under the terms of the GNU General Public License as published by
@@ -197,12 +197,12 @@ extern "C" {
#endif #endif
extern void device_init(void); extern void device_init(void);
extern void device_set_context(device_context_t *c, const device_t *dev, int inst); extern void device_set_context(device_context_t *ctx, const device_t *dev, int inst);
extern void device_context(const device_t *dev); extern void device_context(const device_t *dev);
extern void device_context_inst(const device_t *dev, int inst); 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 *dev);
extern void *device_add_linked(const device_t *d, void *priv); extern void *device_add_linked(const device_t *dev, void *priv);
extern void *device_add_params(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_params(const device_t *dev, void *priv, void *params); extern void device_add_ex_params(const device_t *dev, void *priv, void *params);
@@ -223,27 +223,27 @@ extern void device_get_name(const device_t *dev, int bus, char *name);
extern int device_has_config(const device_t *dev); extern int device_has_config(const device_t *dev);
extern const char *device_get_bios_file(const device_t *dev, const char *internal_name, int file_no); extern const char *device_get_bios_file(const device_t *dev, const char *internal_name, int file_no);
extern int device_is_valid(const device_t *, int m); extern int device_is_valid(const device_t *, int mch);
extern const device_t* device_context_get_device(void); extern const device_t* device_context_get_device(void);
extern int device_get_config_int(const char *name); extern int device_get_config_int(const char *name);
extern int device_get_config_int_ex(const char *s, int dflt_int); extern int device_get_config_int_ex(const char *str, int def);
extern int device_get_config_hex16(const char *name); extern int device_get_config_hex16(const char *name);
extern int device_get_config_hex20(const char *name); extern int device_get_config_hex20(const char *name);
extern int device_get_config_mac(const char *name, int dflt_int); extern int device_get_config_mac(const char *name, int def);
extern void device_set_config_int(const char *s, int val); extern void device_set_config_int(const char *str, int val);
extern void device_set_config_hex16(const char *s, int val); extern void device_set_config_hex16(const char *str, int val);
extern void device_set_config_hex20(const char *s, int val); extern void device_set_config_hex20(const char *str, int val);
extern void device_set_config_mac(const char *s, int val); extern void device_set_config_mac(const char *str, int val);
extern const char *device_get_config_string(const char *name); extern const char *device_get_config_string(const char *name);
extern 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);
extern int machine_get_config_int(char *s); extern int machine_get_config_int(char *str);
extern char *machine_get_config_string(char *s); extern char *machine_get_config_string(char *str);
extern const device_t device_none; extern const device_t device_none;
extern const device_t device_internal; extern const device_t device_internal;