*nextwordused += 1;
return 0x1e;
} else if (errno) {
- DEBUG_PRINTFQ("is out of range\n");
- fprintf(stderr, "trouble with operand '%s': %s\n", operand_orig, strerror(errno));
+ /* if number wasn't parsable, just fall through and assume it's a label */
}
/* not a number? try a label */
return retval;
}
if (! (*instrp)->ready) {
- fprintf(stderr, "instruction not resolvable\n");
+ fprintf(stderr, "instruction not resolvable at line %lu\n", (*instrp)->src_line);
return -1;
}
}
return tok;
}
+
+int timeval_subtract(struct timeval *result, struct timeval *x, struct timeval *y) {
+ /* Perform the carry for the later subtraction by updating y. */
+ if (x->tv_usec < y->tv_usec) {
+ int nsec = (y->tv_usec - x->tv_usec) / 1000000 + 1;
+ y->tv_usec -= 1000000 * nsec;
+ y->tv_sec += nsec;
+ }
+ if (x->tv_usec - y->tv_usec > 1000000) {
+ int nsec = (x->tv_usec - y->tv_usec) / 1000000;
+ y->tv_usec += 1000000 * nsec;
+ y->tv_sec -= nsec;
+ }
+
+ /* Compute the time remaining to wait.
+ tv_usec is certainly positive. */
+ result->tv_sec = x->tv_sec - y->tv_sec;
+ result->tv_usec = x->tv_usec - y->tv_usec;
+
+ /* Return 1 if result is negative. */
+ return x->tv_sec < y->tv_sec;
+}
#include <errno.h>
#include <assert.h>
#include <sysexits.h>
+#include <time.h>
+#include <sys/time.h>
#include <readline/readline.h>
fprintf(f, "Sets addr to value.");
}
+#define MICROSECONDS_PER_CYCLE 10
COMMAND_IMPL(run) {
struct sigaction act;
+ struct timeval run_start_tv, run_stop_tv;
+ long long run_cycle_start;
+ struct timeval start_tv, now_tv, diff_tv;
+ long long cycle_start, cycles_to_wait;
+ struct timespec sleep_time;
+
(void)arg_count, (void)arg_vector;
running_ = 1;
+ gettimeofday(&run_start_tv, NULL);
+ run_cycle_start = vm->cycle;
memset(&act, 0, sizeof act);
act.sa_handler = sigint_handler_;
}
while(running_) {
+ gettimeofday(&start_tv, NULL);
+ cycle_start = vm->cycle;
+
dcpu16_step(vm);
if (opt_.verbose > 1)
dcpu16_state_print(vm);
dcpu16_disassemble_print(vm, vm->reg[DCPU16_REG_PC]);
printf("\n");
}
+
+ /* how many cycles did this instr use? */
+ cycles_to_wait = vm->cycle - cycle_start;
+
+ if (cycles_to_wait == 0)
+ continue;
+
+ /* each cycle wants 10 microseconds */
+
+
+ /* how much of that did we spend already */
+ gettimeofday(&now_tv, NULL);
+ timeval_subtract(&diff_tv, &now_tv, &start_tv);
+ /* do we have time to kill? */
+ if (cycles_to_wait * MICROSECONDS_PER_CYCLE > diff_tv.tv_usec) {
+ sleep_time.tv_sec = diff_tv.tv_sec;
+ sleep_time.tv_nsec = 1000 * ( (cycles_to_wait * MICROSECONDS_PER_CYCLE) - diff_tv.tv_usec);
+ sleep_time.tv_nsec = diff_tv.tv_usec * 1000;
+
+ nanosleep(&sleep_time, NULL);
+ }
}
+ gettimeofday(&run_stop_tv, NULL);
+ timeval_subtract(&diff_tv, &run_stop_tv, &run_start_tv);
+ fprintf(stderr, "ran %llu cycles in %lds %dus\n",
+ vm->cycle - run_cycle_start,
+ diff_tv.tv_sec,
+ diff_tv.tv_usec);
+
printf("interrupted...\n");
return 0;