vm-dcpu16 shell rewritten to be use command table
[dcpu16] / dcpu16.c
index 6f5617ccff86cdd76accf814a68d77bbb011edbe..cfa6d8a2b02e2faa3f11e144db8528b0338a8d5b 100644 (file)
--- a/dcpu16.c
+++ b/dcpu16.c
@@ -33,9 +33,7 @@ static const char * const src_id_ = "$Id$";
 
 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
@@ -54,7 +52,8 @@ void dcpu16_warn_cb_set(void (*fn)(char *fmt, ...)) {
     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, ...) {
@@ -67,12 +66,20 @@ 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
@@ -543,7 +550,7 @@ void dcpu16_disassemble_print(struct dcpu16 *d, WORD addr) {
     assert(a <= 0x3f);
     assert(b <= 0x3f);
 
-    printf("   next instr 0x%04x: %04x", addr, d->ram[addr]);
+    printf("%04x", d->ram[addr]);
 
     if (opcode != 0)
     {
@@ -574,8 +581,6 @@ void dcpu16_disassemble_print(struct dcpu16 *d, WORD addr) {
     }
 
     dump_value(b, d->ram[addr + 1]);
-
-    printf("\n");
 }
 
 void dcpu16_step(struct dcpu16 *d) {
@@ -603,26 +608,42 @@ 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