hexagram/src/sim.c
2019-05-22 22:07:24 -05:00

77 lines
1.5 KiB
C

#include <stdlib.h>
#include <string.h>
#include <hexagram/sim.h>
struct _hexagram_sim {
hexagram_dict *buses;
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;
}
if ((sim->modules = hexagram_dict_new()) == NULL) {
goto error_dict_new_modules;
}
return sim;
error_dict_new_modules:
hexagram_dict_destroy(sim->buses);
error_dict_new_buses:
free(sim);
error_malloc:
return NULL;
}
void hexagram_sim_destroy(hexagram_sim *sim) {
hexagram_dict_destroy(sim->modules);
hexagram_dict_destroy(sim->buses);
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;
}
return 0;
error_dict_set_s:
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;
}
return 0;
error_dict_set_s:
error_module_name:
return -1;
}
int hexagram_sim_run(hexagram_sim *sim);