hexagram/src/sim.c

78 lines
1.5 KiB
C
Raw Normal View History

2019-05-21 09:25:11 -05:00
#include <stdlib.h>
#include <string.h>
#include <hexagram/sim.h>
struct _hexagram_sim {
hexagram_dict *buses;
2019-05-21 09:25:11 -05:00
hexagram_dict *modules;
};
hexagram_sim *hexagram_sim_new() {
hexagram_sim *sim;
if ((sim = malloc(sizeof(*sim))) == NULL) {
goto error_malloc;
}
if ((sim->buses = hexagram_dict_new()) == NULL) {
goto error_dict_new_buses;
2019-05-21 09:25:11 -05:00
}
if ((sim->modules = hexagram_dict_new()) == NULL) {
goto error_dict_new_modules;
}
return sim;
error_dict_new_modules:
hexagram_dict_destroy(sim->buses);
2019-05-21 09:25:11 -05:00
error_dict_new_buses:
2019-05-21 09:25:11 -05:00
free(sim);
error_malloc:
return NULL;
}
void hexagram_sim_destroy(hexagram_sim *sim) {
hexagram_dict_destroy(sim->modules);
hexagram_dict_destroy(sim->buses);
2019-05-21 09:25:11 -05:00
free(sim);
}
int hexagram_sim_add_bus(hexagram_sim *sim,
const char *name,
hexagram_can_if *bus) {
if (hexagram_dict_set_s(sim->buses, name, bus) == NULL) {
goto error_dict_set_s;
2019-05-21 09:25:11 -05:00
}
return 0;
error_dict_set_s:
2019-05-21 09:25:11 -05:00
return -1;
}
int hexagram_sim_add_module(hexagram_sim *sim,
hexagram_module *module) {
const char *name;
if ((name = hexagram_module_name(module)) == NULL) {
goto error_module_name;
}
if (hexagram_dict_set_s(sim->modules, name, module) == NULL) {
goto error_dict_set_s;
2019-05-21 09:25:11 -05:00
}
return 0;
error_dict_set_s:
2019-05-21 09:25:11 -05:00
error_module_name:
return -1;
}
int hexagram_sim_run(hexagram_sim *sim);