From fb06a3ec326d24350dcaaf33364bdaaf6f1d6def Mon Sep 17 00:00:00 2001 From: Rodrigo Arias Date: Tue, 17 Jan 2023 19:28:52 +0100 Subject: [PATCH] Add new emulator --- src/emu/emu.c | 99 +++++++++++++++++++++++++++++++++++++++++++++++++++ src/emu/emu.h | 49 +++++++++++++++++++++++++ 2 files changed, 148 insertions(+) create mode 100644 src/emu/emu.c create mode 100644 src/emu/emu.h diff --git a/src/emu/emu.c b/src/emu/emu.c new file mode 100644 index 0000000..e72fd25 --- /dev/null +++ b/src/emu/emu.c @@ -0,0 +1,99 @@ +/* Copyright (c) 2021-2023 Barcelona Supercomputing Center (BSC) + * SPDX-License-Identifier: GPL-3.0-or-later */ + +#define _POSIX_C_SOURCE 2 + +#include "emu.h" + +#include + +static char progname[] = "ovniemu"; + +static void +usage(void) +{ + err("Usage: %s [-c offsetfile] tracedir\n", progname); + err("\n"); + err("Options:\n"); + err(" -c offsetfile Use the given offset file to correct\n"); + err(" the clocks among nodes. It can be\n"); + err(" generated by the ovnisync program\n"); + err("\n"); + err(" tracedir The output trace dir generated by ovni.\n"); + err("\n"); + err("The output PRV files are placed in the tracedir directory.\n"); + + exit(EXIT_FAILURE); +} + +static void +parse_args(struct emu *emu, int argc, char *argv[]) +{ + int opt; + + while ((opt = getopt(argc, argv, "c:l")) != -1) { + switch (opt) { + case 'c': + emu->clock_offset_file = optarg; + break; + case 'l': + emu->enable_linter = 1; + break; + default: /* '?' */ + usage(); + } + } + + if (optind >= argc) { + err("missing tracedir\n"); + usage(); + } + + emu->tracedir = argv[optind]; +} + +int +emu_model_register(struct emu *emu, struct model_spec *spec, void *ctx) +{ + emu->model_ctx[spec->model] = ctx; + emu->model[spec->model] = spec; + return 0; +} + +void * +emu_model_get_context(struct emu *emu, struct model_spec *spec, int model) +{ + for (int i = 0; spec->depends[i]; i++) { + if (spec->depends[i] == model) + return emu->model_ctx[model]; + } + + /* Not allowed */ + return NULL; +} + +int +emu_init(struct emu *emu, int argc, char *argv[]) +{ + memset(emu, 0, sizeof(*emu)); + parse_args(emu, argc, argv); + + /* Load the streams into the emu_trace */ + if (emu_trace_load(&emu->trace, emu->tracedir) != 0) { + err("emu_init: cannot load trace '%s'\n", + emu->tracedir); + return -1; + } + + /* Parse the streams and build the emu_system */ + if (emu_system_load(&emu->system, &emu->trace) != 0) { + err("emu_init: cannot parse trace '%s'\n", + emu->tracedir); + return -1; + } + + /* Register all the models */ + //emu_model_register(emu, ovni_model_spec, ctx); + + return 0; +} diff --git a/src/emu/emu.h b/src/emu/emu.h new file mode 100644 index 0000000..e7dc062 --- /dev/null +++ b/src/emu/emu.h @@ -0,0 +1,49 @@ +/* Copyright (c) 2021-2023 Barcelona Supercomputing Center (BSC) + * SPDX-License-Identifier: GPL-3.0-or-later */ + +#ifndef EMU_H +#define EMU_H + +#include "bay.h" +#include "pvtrace.h" +#include "emu_trace.h" +#include "emu_system.h" + +enum error_values { + ST_BAD = 666, + ST_TOO_MANY_TH = 777, +}; + +struct emu; + +typedef int (emu_hook_t)(struct emu *emu); + +struct model_spec { + char *name; + int model; + char *depends; + emu_hook_t *probe; + emu_hook_t *create; + emu_hook_t *connect; + emu_hook_t *event; +}; + +struct emu { + struct bay *bay; + struct pvman *pvman; + char *tracedir; + int enable_linter; + char *clock_offset_file; + + struct emu_trace trace; + struct emu_system system; + + struct model_spec *model[256]; + void *model_ctx[256]; +}; + +int emu_init(struct emu *emu, int argc, char *argv[]); +int emu_model_register(struct emu *emu, struct model_spec *spec, void *ctx); +void *emu_model_get_context(struct emu *emu, struct model_spec *spec, int model); + +#endif /* EMU_H */