support png output, buffered file writing
[dcpu16] / vm-dcpu16.c
index 522682a6732e871ff50157323a227b85e619e76e..5924074aa468ce5c5370538fd819864ef69d53c2 100644 (file)
@@ -2,6 +2,8 @@
 #include <stdio.h>
 #include <unistd.h>
 #include <string.h>
+#include <strings.h>
+#include <signal.h>
 #include <errno.h>
 #include <assert.h>
 #include <sysexits.h>
@@ -9,6 +11,8 @@
 #include <readline/readline.h>
 
 #include "dcpu16.h"
+#include "common.h"
+#include "display.h"
 
 /*
  *  shell-like driver for dcpu16 core
@@ -43,7 +47,8 @@ void sigint_handler_(int sig) {
 
 #define VERBOSE_PRINTF(...) do { if (opt_.verbose) printf(__VA_ARGS__); } while (0)
 
-static void usage_(char *prog, unsigned int full) {
+static
+void usage_(char *prog, unsigned int full) {
     FILE *f = full ? stdout : stderr;
     char *x = strrchr(prog, '/');
 
@@ -60,47 +65,22 @@ static void usage_(char *prog, unsigned int full) {
     if (full) {
         fprintf(f, "\nOptions:\n"
                 "\t [file] -- ram image to load initially\n"
-                "\t -v -- verbose execution tracing\n"
+                "\t -v -- displays slightly more information\n"
                 "\t -h -- this screen\n");
 
         fprintf(f, "\n%78s\n", src_id_);
     }
 }
 
-/* simplified strtoul with range checking */
-static
-int str_to_word_(char *s) {
-    unsigned long l;
-    char *ep;
-
-    assert(s);
-
-    errno = 0;
-    l = strtoul(s, &ep, 0);
-
-    if (errno
-    ||  !(*s && *ep == '\0') ) {
-        /* out of range of conversion, or invalid character encountered */
-        return -1;
-    }
-
-    if (l >= DCPU16_RAM) {
-        /* out of range for our needs */
-        errno = ERANGE;
-        return -1;
-    }
-
-    return l;
-}
 
 /* flense a buffer into a newly-allocated argument list */
-/* FIXME: handle quotes */
 static
 int buf_tok_vect_(char ***v, int *c, char *buf) {
     const char *sep = " \t";
+    const char *quot = "\"'`";
     const size_t v_grow = 32;
     size_t v_sz = 32;
-    char *st;
+    char *st, *qt;
 
     *c = 0;
     *v = malloc(v_sz * sizeof **v);
@@ -109,9 +89,9 @@ int buf_tok_vect_(char ***v, int *c, char *buf) {
         return -1;
     }
 
-    for ( (*v)[*c] = strtok_r(buf, sep, &st);
+    for ( (*v)[*c] = strqtok_r(buf, sep, '\\', quot, &qt, &st);
           (*v)[*c];
-          (*v)[*c] = strtok_r(NULL, sep, &st)
+          (*v)[*c] = strqtok_r(NULL, sep, '\\', quot, &qt, &st)
         ) {
             (*c)++;
 
@@ -130,13 +110,17 @@ int buf_tok_vect_(char ***v, int *c, char *buf) {
     return 0;
 }
 
-/* resets the instance and loads an image into ram starting at addr */
+/*
+    resets the vm if addr is zero then
+    loads an image from filename into ram starting at addr
+*/
 static
 int file_load_(struct dcpu16 *vm, char *filename, DCPU16_WORD addr) {
     FILE *f;
     size_t r;
 
-    dcpu16_reset(vm);
+    if (!addr)
+        dcpu16_reset(vm);
 
     f = fopen(filename, "rb");
     if (f == NULL) {
@@ -185,7 +169,7 @@ struct command_ {
 
 COMMAND_IMPL(quit) {
     (void)vm, (void)arg_count, (void)arg_vector;
-    VERBOSE_PRINTF("done\n");
+
     return -1;
 }
 COMMAND_HELP(quit) {
@@ -196,11 +180,26 @@ COMMAND_HELP(quit) {
 }
 
 
+COMMAND_IMPL(reset) {
+    (void)arg_count, (void)arg_vector;
+
+    dcpu16_reset(vm);
+    printf("initialized\n");
+    return 0;
+}
+COMMAND_HELP(reset) {
+    fprintf(f, "\treset\n");
+    if (summary) return;
+
+    fprintf(f, "Clears and reinitializes emulator.\n");
+}
+
+
 COMMAND_IMPL(load) {
     int addr = 0;
 
     if (arg_count > 2) {
-        addr = str_to_word_(arg_vector[2]);
+        addr = str_to_word(arg_vector[2]);
         if (addr < 0) {
             fprintf(stderr, "address '%s' is not a valid word: %s\n", arg_vector[2], strerror(errno));
             return 0;
@@ -230,8 +229,8 @@ COMMAND_IMPL(dump) {
     int i;
 
     for (i = 1; i < arg_count; i++) {
-        addr[i] = str_to_word_(arg_vector[i]);
-        if (addr[i] < 0) {
+        addr[i-1] = str_to_word(arg_vector[i]);
+        if (addr[i-1] < 0) {
             fprintf(stderr, "address '%s' is not a valid word: %s\n", arg_vector[i], strerror(errno));
             return 0;
         }
@@ -261,8 +260,8 @@ COMMAND_IMPL(disassemble) {
     int i;
 
     for (i = 1; i < arg_count; i++) {
-        addr[i] = str_to_word_(arg_vector[i]);
-        if (addr[i] < 0) {
+        addr[i-1] = str_to_word(arg_vector[i]);
+        if (addr[i-1] < 0) {
             fprintf(stderr, "address '%s' is not a valid word: %s\n", arg_vector[i], strerror(errno));
             return 0;
         }
@@ -275,8 +274,11 @@ COMMAND_IMPL(disassemble) {
         return 0;
     }
 
-    for (i = addr[0]; i <= addr[1]; i++)
-        dcpu16_disassemble_print(vm, i), printf("\n");
+    for (i = addr[0]; i <= addr[1]; /* */ ) {
+        printf("0x%04x: ", i);
+        i += dcpu16_disassemble_print(vm, i);
+        printf("\n");
+    }
 
     return 0;
 }
@@ -308,12 +310,11 @@ COMMAND_IMPL(step) {
     }
 
     while (count--) {
-        VERBOSE_PRINTF("executing next cycle, instruction: ");
-        dcpu16_disassemble_print(vm, vm->pc), printf("\n");
-
+        dcpu16_disassemble_print(vm, vm->pc);
+        printf("\n");
         dcpu16_step(vm);
 
-        if (opt_.verbose)
+        if (count > 1 && opt_.verbose)
             dcpu16_state_print(vm);
     }
 
@@ -328,30 +329,31 @@ COMMAND_HELP(step) {
 
 
 COMMAND_IMPL(run) {
-    sig_t osig;
+    struct sigaction act;
     (void)arg_count, (void)arg_vector;
 
     running_ = 1;
 
-    /* install our new interrupt signal handler */
-    if ( (osig = signal(SIGINT, sigint_handler_)) == SIG_ERR ) {
-        fprintf(stderr, "%s():%s\n", "signal", strerror(errno));
+    memset(&act, 0, sizeof act);
+    act.sa_handler = sigint_handler_;
+    act.sa_flags = SA_RESETHAND;
+
+    if (sigaction(SIGINT, &act, NULL)) {
+        fprintf(stderr, "%s():%s\n", "sigaction", strerror(errno));
         return -1;
     }
 
     while(running_) {
         dcpu16_step(vm);
-        if (opt_.verbose)
+        if (opt_.verbose > 1)
             dcpu16_state_print(vm);
+        else if (opt_.verbose) {
+            dcpu16_disassemble_print(vm, vm->pc);
+            printf("\n");
+        }
     }
 
-    /* restore the old interrupt signal handler */
-    if (signal(SIGINT, osig) == SIG_ERR) {
-        fprintf(stderr, "%s():%s\n", "signal", strerror(errno));
-        return -1;
-    }
-
-    VERBOSE_PRINTF("interrupted...\n");
+    printf("interrupted...\n");
 
     return 0;
 }
@@ -359,7 +361,65 @@ COMMAND_HELP(run) {
     fprintf(f, "\trun\n");
     if (summary) return;
 
-    fprintf(f, "Begins executing continuously.\n");
+    fprintf(f, "Begins executing continuously.\n"
+               "May be interrupted with SIGINT.\n");
+}
+
+static const char * const display_filename_default_ =
+#ifdef HAVE_LIBPNG
+    "dcpu16-display.png"
+#else /* HAVE_LIBPNG */
+    "dcpu16-display.pnm"
+#endif /* HAVE_LIBPNG */
+;
+COMMAND_IMPL(display) {
+    static DCPU16_DISPLAY *display = NULL;
+    const char *filename = display_filename_default_;
+
+    if (arg_count == 2) {
+        filename = arg_vector[1];
+    }
+
+    if (display) {
+        printf("display already enabled..\n");
+        return 0;
+    }
+
+    display = display_new(filename);
+
+    if (display == NULL) {
+        fprintf(stderr, "failed to initialize display buffer\n");
+        return 0;
+    }
+
+    if (dcpu16_acct_add(vm, DCPU16_ACCT_EV_WRITE, display_fn, display)) {
+        fprintf(stderr, "failed to register display update callback\n");
+        return 0;
+    }
+
+    if (dcpu16_acct_add(vm, DCPU16_ACCT_EV_RESET, display_reset_fn, display)) {
+        fprintf(stderr, "failed to register display reset callback\n");
+        return 0;
+    }
+
+    if (dcpu16_acct_add(vm, DCPU16_ACCT_EV_CYCLE, display_cycle_fn, display)) {
+        fprintf(stderr, "failed to register display cycle callback\n");
+        return 0;
+    }
+
+    /* init display as if reset occurred */
+    display_reset_fn(vm, DCPU16_ACCT_EV_RESET, 0, display);
+
+    return 0;
+}
+COMMAND_HELP(display) {
+    fprintf(f, "\tdisplay [file]\n");
+    if (summary) return;
+
+    fprintf(f, "Attaches display interface, begins updating an image file of display contents...\n"
+               "Image filename may be specified, defaults to '%s'\n",
+               display_filename_default_
+            );
 }
 
 /* gather all these together into a searchable table */
@@ -376,6 +436,8 @@ static struct command_ command_table_[] = {
     COMMAND_ENTRY(disassemble, 0, 2),
     COMMAND_ENTRY(step, 0, 1),
     COMMAND_ENTRY(run, 0, 0),
+    COMMAND_ENTRY(reset, 0, 0),
+    COMMAND_ENTRY(display, 0, 1),
     { NULL, 0, 0, NULL, NULL }
 };
 
@@ -404,7 +466,7 @@ COMMAND_HELP(help) {
         fprintf(f, "\thelp [command]\n");
         if (summary) return;
 
-    fprintf(f, "Displays a list of available commands, or help on a specific command.\n");
+    fprintf(f, "Displays a list of available commands, or detailed help on a specific command.\n");
 }
 
 
@@ -442,7 +504,7 @@ int main(int argc, char **argv) {
     argv += optind;
 
     if ((vm = dcpu16_new()) == NULL) {
-        fprintf(stderr, "could not allocate new dcpu instance\n");
+        fprintf(stderr, "could not allocate new dcpu16 instance\n");
         exit(EX_UNAVAILABLE);
     }