From 0b73fea55ab4dbca48141f56ec5cdf193e299c9c Mon Sep 17 00:00:00 2001 From: mrb0nk500 Date: Wed, 2 Feb 2022 15:04:36 -0400 Subject: io.c: Add code for delaying by some amount of time per cycle. --- io.c | 30 ++++++++++++++++++++++++++++-- 1 file changed, 28 insertions(+), 2 deletions(-) diff --git a/io.c b/io.c index 0c3a327..a3eb061 100644 --- a/io.c +++ b/io.c @@ -8,6 +8,9 @@ uint8_t key_idx = 0; uint8_t esc = 0; uint8_t iscsi = 0; +#if delay_clk && getclk && !bench +uint64_t old_clk = 0; +#endif typedef struct escape escape; @@ -49,6 +52,29 @@ int get_key(WINDOW *scr, int delay, uint64_t cycles) { int c; int keycode = 0; int tmp_flag = dbg_print_per_inst; + #if delay_clk && getclk && !bench + if (!step && delay < 0) { + #define MICRO_PER_SEC (1000 * 1000) + #define NANO_PER_SEC (MICRO_PER_SEC * 1000) + #define CYCLE_PER_SEC (2*MICRO_PER_SEC) + struct timespec ts; + const uint64_t clk_diff = cycles - old_clk; + const uint64_t nanos = (uint64_t)((double)clk_diff * ((double)NANO_PER_SEC / CYCLE_PER_SEC)); + if (nanos < NANO_PER_SEC) { + ts.tv_sec = 0; + ts.tv_nsec = nanos; + } else { + ts.tv_sec = nanos / NANO_PER_SEC; + ts.tv_nsec = nanos % NANO_PER_SEC; + } + #if debug + wmove(scr, getmaxy(scr)-2, 0); + wprintw(scr, "ts.tv_sec: %"PRIu64", tv.nsec: %"PRIu64"\n", ts.tv_sec, ts.tv_nsec); + #endif + nanosleep(&ts, NULL); + old_clk = cycles; + } + #endif wtimeout(scr, delay); curs_set((delay < 0)); if ((key == NULL) || (key && key[key_idx] == '\0') || !kbd_rdy || delay >= 0) { @@ -310,7 +336,7 @@ void handle_ctrlcode(int c) { } -void io(uint64_t address, uint8_t rw) { +void io(uint64_t address, uint8_t rw, uint64_t cycles) { /*step |= (!rw) ? (cpu->pc[thread] == CTRL_ADDR) : step;*/ switch (address) { case STEP_ADDR: step = (!rw) ? addr[STEP_ADDR] : step; break; @@ -325,7 +351,7 @@ void io(uint64_t address, uint8_t rw) { wrefresh(inst_win); wrefresh(dbg_win); #endif - addr[RX_ADDR] = get_key(scr); + addr[RX_ADDR] = get_key(scr, -1, cycles); kbd_rdy = 0; break; case TX_ADDR: -- cgit v1.2.3-13-gbd6f