diff --git a/src/emulator.c b/src/emulator.c index 4cd91526..de32a423 100644 --- a/src/emulator.c +++ b/src/emulator.c @@ -57,10 +57,12 @@ struct emu { volatile int video_width; volatile int video_height; volatile int video_resized; + unsigned frame; struct render_backend *r, *r2; struct imgui *imgui; struct microprofile *mp; + struct trace_writer *trace_writer; /* hosts which support creating multiple gl contexts will render video on @@ -104,7 +106,6 @@ struct emu { /* debug stats */ int debug_menu; int frame_stats; - int frame_seq; float frame_times[360]; int64_t last_paint; }; @@ -302,29 +303,37 @@ static void emu_init_textures(struct emu *emu) { /* * trace recording */ -static void emu_toggle_tracing(struct emu *emu) { +static void emu_stop_tracing(struct emu *emu) { if (!emu->trace_writer) { - char filename[PATH_MAX]; - get_next_trace_filename(filename, sizeof(filename)); - - emu->trace_writer = trace_writer_open(filename); - - if (!emu->trace_writer) { - LOG_INFO("failed to start tracing"); - return; - } - - /* clear texture cache in order to generate insert events for all - textures referenced while tracing */ - emu_dirty_textures(emu); - - LOG_INFO("begin tracing to %s", filename); - } else { - trace_writer_close(emu->trace_writer); - emu->trace_writer = NULL; - - LOG_INFO("end tracing"); + return; } + + trace_writer_close(emu->trace_writer); + emu->trace_writer = NULL; + + LOG_INFO("end tracing"); +} + +static void emu_start_tracing(struct emu *emu) { + if (emu->trace_writer) { + return; + } + + char filename[PATH_MAX]; + get_next_trace_filename(filename, sizeof(filename)); + + emu->trace_writer = trace_writer_open(filename); + + if (!emu->trace_writer) { + LOG_INFO("failed to start tracing"); + return; + } + + /* clear texture cache in order to generate insert events for all + textures referenced while tracing */ + emu_dirty_textures(emu); + + LOG_INFO("begin tracing to %s", filename); } /* @@ -475,9 +484,11 @@ static void emu_paint(struct emu *emu) { emu->frame_stats = !emu->frame_stats; } - if ((!emu->trace_writer && igMenuItem("start trace", NULL, 0, 1)) || - (emu->trace_writer && igMenuItem("stop trace", NULL, 1, 1))) { - emu_toggle_tracing(emu); + if (!emu->trace_writer && igMenuItem("start trace", NULL, 0, 1)) { + emu_start_tracing(emu); + } + if (emu->trace_writer && igMenuItem("stop trace", NULL, 1, 1)) { + emu_stop_tracing(emu); } if (igMenuItem("clear texture cache", NULL, 0, 1)) { @@ -541,8 +552,9 @@ static void emu_paint(struct emu *emu) { igValueFloat("min time", min_time, "%.2f"); igValueFloat("max time", max_time, "%.2f"); igValueFloat("avg time", avg_time, "%.2f"); - igPlotLines("", emu->frame_times, num_frame_times, emu->frame_seq, - NULL, 0.0f, 60.0f, graph_size, sizeof(float)); + igPlotLines("", emu->frame_times, num_frame_times, + emu->frame % num_frame_times, NULL, 0.0f, 60.0f, + graph_size, sizeof(float)); } igEnd(); @@ -561,12 +573,6 @@ static void emu_paint(struct emu *emu) { /* * dreamcast guest interface */ -static void emu_guest_poll_input(void *userdata) { - struct emu *emu = userdata; - - input_poll(emu->host); -} - static void emu_guest_finish_render(void *userdata) { struct emu *emu = userdata; @@ -766,11 +772,11 @@ void emu_run_frame(struct emu *emu) { if (emu->last_paint) { float frame_time_ms = (float)(now - emu->last_paint) / 1000000.0f; int num_frame_times = array_size(emu->frame_times); - emu->frame_times[emu->frame_seq] = frame_time_ms; - emu->frame_seq = (emu->frame_seq + 1) % num_frame_times; + emu->frame_times[emu->frame % num_frame_times] = frame_time_ms; } emu->last_paint = now; + emu->frame++; } int emu_load_game(struct emu *emu, const char *path) { @@ -784,6 +790,8 @@ int emu_load_game(struct emu *emu, const char *path) { } void emu_destroy(struct emu *emu) { + emu_stop_tracing(emu); + dc_destroy(emu->dc); free(emu); @@ -811,7 +819,6 @@ struct emu *emu_create(struct host *host) { emu->dc->push_audio = &emu_guest_push_audio; emu->dc->start_render = &emu_guest_start_render; emu->dc->finish_render = &emu_guest_finish_render; - emu->dc->poll_input = &emu_guest_poll_input; /* start up the video thread */ emu->multi_threaded = video_supports_multiple_contexts(emu->host); diff --git a/src/file/trace.c b/src/file/trace.c index 114d531b..45ff4caf 100644 --- a/src/file/trace.c +++ b/src/file/trace.c @@ -5,20 +5,6 @@ #include "core/math.h" #include "guest/pvr/tr.h" -void get_next_trace_filename(char *filename, size_t size) { - const char *appdir = fs_appdir(); - - for (int i = 0; i < INT_MAX; i++) { - snprintf(filename, size, "%s" PATH_SEPARATOR "%d.trace", appdir, i); - - if (!fs_exists(filename)) { - return; - } - } - - LOG_FATAL("Unable to find available trace filename"); -} - void trace_writer_close(struct trace_writer *writer) { if (writer->file) { fclose(writer->file); @@ -235,3 +221,17 @@ struct trace *trace_parse(const char *filename) { return trace; } + +void get_next_trace_filename(char *filename, size_t size) { + const char *appdir = fs_appdir(); + + for (int i = 0; i < INT_MAX; i++) { + snprintf(filename, size, "%s" PATH_SEPARATOR "%d.trace", appdir, i); + + if (!fs_exists(filename)) { + return; + } + } + + LOG_FATAL("unable to find available trace filename"); +} diff --git a/src/guest/dreamcast.c b/src/guest/dreamcast.c index d0ccb6b1..3b255ad7 100644 --- a/src/guest/dreamcast.c +++ b/src/guest/dreamcast.c @@ -16,14 +16,6 @@ #include "guest/scheduler.h" #include "guest/sh4/sh4.h" -void dc_poll_input(struct dreamcast *dc) { - if (!dc->poll_input) { - return; - } - - dc->poll_input(dc->userdata); -} - void dc_finish_render(struct dreamcast *dc) { if (!dc->finish_render) { return; diff --git a/src/guest/dreamcast.h b/src/guest/dreamcast.h index 7440358b..cc948a3c 100644 --- a/src/guest/dreamcast.h +++ b/src/guest/dreamcast.h @@ -125,7 +125,6 @@ struct device { typedef void (*push_audio_cb)(void *, const int16_t *, int); typedef void (*start_render_cb)(void *, struct tile_context *); typedef void (*finish_render_cb)(void *); -typedef void (*poll_input_cb)(void *); struct dreamcast { int running; @@ -154,7 +153,6 @@ struct dreamcast { push_audio_cb push_audio; start_render_cb start_render; finish_render_cb finish_render; - poll_input_cb poll_input; }; struct dreamcast *dc_create(); @@ -192,6 +190,5 @@ void dc_input(struct dreamcast *dc, int port, int button, int16_t value); void dc_push_audio(struct dreamcast *dc, const int16_t *data, int frames); void dc_start_render(struct dreamcast *dc, struct tile_context *ctx); void dc_finish_render(struct dreamcast *dc); -void dc_poll_input(struct dreamcast *dc); #endif diff --git a/src/guest/maple/controller.c b/src/guest/maple/controller.c index 3f95a30c..bc740124 100644 --- a/src/guest/maple/controller.c +++ b/src/guest/maple/controller.c @@ -33,12 +33,6 @@ struct controller { struct maple_cond cnd; }; -static void controller_update(struct controller *ctrl) { - /* dc_poll_input will call into controller_input if new values are - available */ - dc_poll_input(ctrl->dc); -} - static int controller_input(struct maple_device *dev, int button, int16_t value) { struct controller *ctrl = (struct controller *)dev; @@ -101,8 +95,6 @@ static int controller_frame(struct maple_device *dev, } case MAPLE_REQ_GETCOND: { - controller_update(ctrl); - res->header.command = MAPLE_RES_TRANSFER; res->header.recv_addr = frame->header.send_addr; res->header.send_addr = frame->header.recv_addr; diff --git a/src/host/sdl_host.c b/src/host/sdl_host.c index 13d89758..ece7370a 100644 --- a/src/host/sdl_host.c +++ b/src/host/sdl_host.c @@ -828,9 +828,6 @@ int main(int argc, char **argv) { if (emu_load_game(emu, load)) { while (!g_host->closed) { - /* even though the emulator itself will poll for events when updating - controller input, the main loop needs to also poll to ensure the - close event is received */ host_poll_events(g_host); /* only step the emulator if the available audio is running low. this