static const char regnames_[] = "ABCXYZIJ";
-
-static unsigned int trace_mode_ = 0; /* spew overly verbose internals */
-
+/* some default warning and debug reporting functions, which can be overridden by clients */
#define WARN(...) do { if (warn_cb_) warn_cb_(__VA_ARGS__); } while (0)
static inline void warn_(char *fmt, ...) __attribute__((format(printf, 1, 2)));
static inline
warn_cb_ = fn;
}
-#define TRACE(...) do { if (trace_cb_ && trace_mode_) trace_cb_(__VA_ARGS__); } while (0)
+#ifdef DEBUG
+#define TRACE(...) do { if (trace_cb_) trace_cb_(__VA_ARGS__); } while (0)
static inline void trace_(char *fmt, ...) __attribute__((format(printf, 1, 2)));
static inline
void trace_(char *fmt, ...) {
fprintf(stdout, "\n");
fflush(stdout);
}
-static void (*trace_cb_)(char *fmt, ...) = trace_;
+#else /* DEBUG */
+#define TRACE(...) do {} while(0)
+#endif /* DEBUG */
+static void (*trace_cb_)(char *fmt, ...) =
+#ifdef DEBUG
+ trace_
+#else /* DEBUG */
+ NULL
+#endif
+ ;
void dcpu16_trace_cb_set(void (*fn)(char *fmt, ...)) {
trace_cb_ = fn;
}
-
/* value_decode_
* sets *v to be the destination of the value
* workv is buffer to use to accumulate literal value before use
assert(a <= 0x3f);
assert(b <= 0x3f);
- printf(" next instr 0x%04x: %04x", addr, d->ram[addr]);
+ printf("%04x", d->ram[addr]);
if (opcode != 0)
{
}
dump_value(b, d->ram[addr + 1]);
-
- printf("\n");
}
void dcpu16_step(struct dcpu16 *d) {
void dcpu16_state_print(struct dcpu16 *d) {
unsigned int i;
- printf("---- cycle:0x%08llx %2s:0x%04x %2s:0x%04x %2s:0x%04x\n",
+ printf("(0x%08llx) %2s:0x%04x %2s:0x%04x %2s:0x%04x [%2s]:",
d->cycle,
- "PC", d->pc,
+ "O", d->o,
"SP", d->sp,
- "O", d->o);
- printf(" ");
+ "PC", d->pc,
+ "PC");
+
+ dcpu16_disassemble_print(d, d->pc);
+ printf("\n ");
+
for (i = 0; i < 8; i++)
printf(" %c:0x%04x", regnames_[i], d->reg[i]);
printf("\n");
}
-void dcpu16_dump_ram(struct dcpu16 *d, WORD start, WORD stop) {
+/* dcpu16_dump_ram
+ * print raw ram contents from start to stop
+ */
+void dcpu16_dump_ram(struct dcpu16 *d, WORD start, WORD end) {
unsigned int i, j;
const unsigned int n = 8; /* words per line */
- for (i = start, j = 0; i <= stop; i++, j++) {
+ for (i = start, j = 0; i <= end; i++, j++) {
if (j % n == 0)
printf("0x%04x:\t", i);
printf(" %04x%s", d->ram[i], (j % n) == (n - 1) ? "\n" : "");
}
+ if ((j % n) != (n - 1))
+ printf("\n");
+}
+
+/* dcpu16_reset
+ * resets a dcpu16 instance to initial state
+ */
+void dcpu16_reset(struct dcpu16 *d) {
+ memset(d, 0, sizeof *d);
}
/* dcpu16_new