support png output, buffered file writing
[dcpu16] / vm-dcpu16.c
index d60c340d2eeaa7a6f4eb4e9630127ae1152bd439..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>
@@ -10,6 +12,7 @@
 
 #include "dcpu16.h"
 #include "common.h"
+#include "display.h"
 
 /*
  *  shell-like driver for dcpu16 core
@@ -326,14 +329,17 @@ 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;
     }
 
@@ -347,12 +353,6 @@ COMMAND_IMPL(run) {
         }
     }
 
-    /* restore the old interrupt signal handler */
-    if (signal(SIGINT, osig) == SIG_ERR) {
-        fprintf(stderr, "%s():%s\n", "signal", strerror(errno));
-        return -1;
-    }
-
     printf("interrupted...\n");
 
     return 0;
@@ -365,6 +365,63 @@ COMMAND_HELP(run) {
                "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 */
 
 /* help command gets some assistance in declarations */
@@ -380,6 +437,7 @@ static struct command_ command_table_[] = {
     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 }
 };