typo, missing semicolon
[dcpu16] / hw_lem1802.c
index 31a69b8be3e8f6ffc5fccf0c5b8ce3ba6f1d9bc6..0b95d1180530d885e533a05b48a8d960d5d8ac0d 100644 (file)
 #include "chargen-4x8.h"
 #include "hw_lem1802.h"
 
+/* lem1802
+ *
+ * TODO:
+ * multiple vnc displays
+ */
+
 #ifdef DEBUG
 #define TRACE(...) do { printf("[debug] "); printf(__VA_ARGS__); printf("\n"); } while (0)
 #else /* DEBUG  */
 #define VOIDFP(__x__) (__x__)
 #endif
 
-static dcpu16_hw_signal_t lem1802_reset_;
-static dcpu16_hw_signal_t lem1802_cycle_;
-static dcpu16_hw_signal_t lem1802_hwi_;
-static struct dcpu16_hw hw_ = {
-    .name_  = "LEM1802 - Low Energy Monitor",
-    .id_l   = 0xf615,
-    .id_h   = 0x7349,
-    .ver    = 0x1802,
-    .mfg_l  = 0x8b36,
-    .mfg_h  = 0x1c6c,
-    .hwi    = lem1802_hwi_,
-    .cycle  = lem1802_cycle_,
-    .reset  = lem1802_reset_,
-    .data   = (struct lem1802_ *)NULL
-};
-
 #define LEM1802_POWER_ON_CYCLES 100000 /* this should vary by, let us say, 10% */
 
 #define PIX_X 160       /* pixels in display */
@@ -329,7 +319,8 @@ f_done:
 
 #ifdef HAVE_LIBVNCSERVER
 /* create and return a new screen, easiest to do here because we know the screen dimensions */
-rfbScreenInfoPtr lem1802_rfb_new(int argc, char *argv[]) {
+static
+rfbScreenInfoPtr lem1802_rfb_new_(int argc, char *argv[]) {
     rfbScreenInfoPtr s;
     int paddedWidth = PIX_X + ( (PIX_X & 3) ? (4 - (PIX_X & 3)) : 0 );
     int height = PIX_Y;
@@ -353,15 +344,6 @@ rfbScreenInfoPtr lem1802_rfb_new(int argc, char *argv[]) {
     return s;
 }
 
-/* set up a new screen to see our pixels */
-void lem1802_vnc_associate(struct dcpu16_hw *hw, rfbScreenInfoPtr s) {
-    struct lem1802_ *display = (struct lem1802_ *)hw->data;
-
-    s->desktopName = "NYA ELEKTRISKA LEM1802";
-    s->frameBuffer = (char *)display->pixbuf;
-
-    TRACE("%s>> s:%p\n", __func__, VOIDP(s));
-}
 
 /* notify rfb server that pixels may have changed */
 static
@@ -383,8 +365,8 @@ int pixbuf_render_vnc_(void *data, struct pixel_ *pixbuf, size_t x, size_t y) {
 
 
 static
-void lem1802_reset_(struct dcpu16 *vm, void *data) {
-    struct lem1802_ *display = (struct lem1802_ *)data;
+void lem1802_reset_(struct dcpu16 *vm, struct dcpu16_hw *hw) {
+    struct lem1802_ *display = (struct lem1802_ *)hw->data;
 
     (void)vm;
 
@@ -408,8 +390,8 @@ void lem1802_reset_(struct dcpu16 *vm, void *data) {
 }
 
 static
-void lem1802_cycle_(struct dcpu16 *vm, void *data) {
-    struct lem1802_ *display = (struct lem1802_ *)data;
+void lem1802_cycle_(struct dcpu16 *vm, struct dcpu16_hw *hw) {
+    struct lem1802_ *display = (struct lem1802_ *)hw->data;
 
     (void)vm;
     /*
@@ -464,8 +446,8 @@ void lem1802_cycle_(struct dcpu16 *vm, void *data) {
 }
 
 static
-void lem1802_hwi_(struct dcpu16 *vm, void *data) {
-    struct lem1802_ *display = (struct lem1802_ *)data;
+void lem1802_hwi_(struct dcpu16 *vm, struct dcpu16_hw *hw) {
+    struct lem1802_ *display = (struct lem1802_ *)hw->data;
     DCPU16_WORD reg_a = vm->reg[DCPU16_REG_A];
     DCPU16_WORD reg_b = vm->reg[DCPU16_REG_B];
 
@@ -528,22 +510,8 @@ static struct renderer_ {
     { NULL, NULL, NULL }
 };
 
-int lem1802_renderer_set(struct dcpu16_hw *hw, const char *renderer, void *data) {
-    struct renderer_ *r;
-
-    for (r = lem1802_renderers_; r->renderer; r++) {
-        if (strcmp(renderer, r->name) == 0) {
-            ((struct lem1802_ *)(hw->data))->render = r->renderer;
-            ((struct lem1802_ *)(hw->data))->renderer_data = data;
-            TRACE("%s>> renderer set to %s", __func__, renderer);
-            return 0;
-        }
-    }
-
-    return -1;
-}
-
-char *lem1802_renderers_iter(void **iterp, char **name, char **args) {
+static
+char *lem1802_renderers_iter_(void **iterp, char **name, char **args) {
     struct renderer_ **r = (struct renderer_ **)iterp;
 
     if (*r == NULL)
@@ -562,54 +530,181 @@ char *lem1802_renderers_iter(void **iterp, char **name, char **args) {
     return (*r)->name;
 }
 
-/* instantitate a new display */
-struct dcpu16_hw *lem1802_new(struct dcpu16 *vm) {
-    struct dcpu16_hw *hw;
-
-    hw = calloc(1, sizeof *hw);
-    if (hw == NULL) {
-        vm->warn_cb_("%s():%s", "calloc", strerror(errno));
-        return NULL;
-    }
-
-    memcpy(hw, &hw_, sizeof *hw);
+static
+int lem1802_data_init_(struct dcpu16_hw *hw, void *data) {
+    (void)data;
 
     hw->data = calloc(1, sizeof(struct lem1802_));
     if (hw->data == NULL) {
-        vm->warn_cb_("%s():%s", "calloc", strerror(errno));
-        free(hw);
-        return NULL;
+        hw->vm->warn_cb_("%s():%s", "calloc", strerror(errno));
+        return -1;
     }
 
-    ((struct lem1802_ *)(hw->data))->pixbuf = calloc(1, PIX_X * PIX_Y * sizeof *((struct lem1802_ *)(hw->data))->pixbuf);
+    ((struct lem1802_ *)(hw->data))->pixbuf = calloc(PIX_X * PIX_Y, sizeof *((struct lem1802_ *)(hw->data))->pixbuf);
     if (((struct lem1802_ *)(hw->data))->pixbuf == NULL) {
-        vm->warn_cb_("%s():%s", "calloc", strerror(errno));
+        hw->vm->warn_cb_("%s():%s", "calloc", strerror(errno));
         free(hw->data);
-        free(hw);
-        return NULL;
+        hw->data = NULL;
+        return -1;
     }
 
     ((struct lem1802_ *)(hw->data))->refresh_rate = 1666;
     ((struct lem1802_ *)(hw->data))->blink_rate = 75000;
 
-    hw->vm = vm;
-
-    return hw;
+    return 0;
 }
 
-void lem1802_del(struct dcpu16_hw **hw) {
+static
+void lem1802_data_free_(struct dcpu16_hw *hw) {
     if (hw) {
-        if (*hw) {
-            if ((*hw)->data) {
-                if (((struct lem1802_ *)(*hw)->data)->pixbuf) {
-                    free(((struct lem1802_ *)(*hw)->data)->pixbuf);
-                    ((struct lem1802_ *)(*hw)->data)->pixbuf = NULL;
-                }
-                free((*hw)->data);
-                (*hw)->data = NULL;
+        if (hw->data) {
+            if (((struct lem1802_ *)(hw->data))->pixbuf) {
+                free(((struct lem1802_ *)(hw->data))->pixbuf);
+                ((struct lem1802_ *)(hw->data))->pixbuf = NULL;
             }
-            free(*hw);
-            *hw = NULL;
+            free(hw->data);
+            hw->data = NULL;
         }
     }
 }
+
+static struct dcpu16_hw_ctl_cmd ctl_[] = {
+    { "blink_rate", "const unsigned int *", "unsigned int *", "sets or gets cycles per blink toggle" },
+    { "refresh_rate", "const unsigned int *", "unsigned int *", "sets or gets cycles per screen refresh" },
+#ifdef HAVE_LIBVNCSERVER
+    { "new_rfbScreen", "struct { int argc; char **argv;} *", "rfbScreenInfoPtr *", "allocates a new rfb screen" },
+    { "associate_rfbScreen", "rfbScreenInfoPtr", "NULL", "associates this lem1802 instance with an rfb display" },
+#endif /* HAVE_LIBVNCSERVER */
+    { "renderers_iter", "void **", "struct {char *name; char *args;} *", "returns the next renderer this module is capable of using" },
+    { "renderer", "const char *", "NULL", "sets this lem1802 instance to use renderer" },
+    { "renderer_data", "void *", "NULL", "sets renderer-specific data" },
+    { NULL, NULL, NULL, NULL }
+};
+
+static
+int lem1802_data_ctl_(struct dcpu16_hw *hw, const char *cmd, void *data_in, void *data_out) {
+    if (strcmp(cmd, "blink_rate") == 0) {
+        struct lem1802_ *display = (struct lem1802_ *)hw->data;
+        const unsigned int *rate_in = (const unsigned int *)data_in;
+        unsigned int *rate_out = (unsigned int *)data_out;
+
+        if (rate_out) {
+            *rate_out = display->blink_rate;
+        }
+
+        if (rate_in) {
+            display->blink_rate = *rate_in;
+        }
+
+        return 0;
+    }
+
+    if (strcmp(cmd, "refresh_rate") == 0) {
+        struct lem1802_ *display = (struct lem1802_ *)hw->data;
+        const unsigned int *rate_in = (const unsigned int *)data_in;
+        unsigned int *rate_out = (unsigned int *)data_out;
+
+        if (rate_out) {
+            *rate_out = display->refresh_rate;
+        }
+
+        if (rate_in) {
+            display->refresh_rate = *rate_in;
+        }
+
+        return 0;
+    }
+
+#ifdef HAVE_LIBVNCSERVER
+    if (strcmp(cmd, "new_rfbScreen") == 0) {
+        struct args_ { int argc; char **argv;} *in = (struct args_ *)data_in;
+        rfbScreenInfoPtr *s_out = (rfbScreenInfoPtr *)data_out;
+
+        if (in == NULL || s_out == NULL)
+            return -EFAULT;
+
+        *s_out = lem1802_rfb_new_(in->argc, in->argv);
+
+        return 0;
+    }
+
+    if (strcmp(cmd, "associate_rfbScreen") == 0) {
+        struct lem1802_ *display = (struct lem1802_ *)hw->data;
+        rfbScreenInfoPtr rfbScreen = (rfbScreenInfoPtr)data_in;
+        (void)data_out;
+
+        if (rfbScreen == NULL)
+            return -EFAULT;
+
+        rfbScreen->desktopName = "NYA ELEKTRISKA LEM1802";
+        rfbScreen->frameBuffer = (char *)display->pixbuf;
+
+        return 0;
+    }
+#endif /* HAVE_LIBVNCSERVER */
+
+    if (strcmp(cmd, "renderers_iter") == 0) {
+        void **iterp = (void **)data_in;
+        struct packed_out_ {
+            char *name;
+            char *args;
+        } *parg = (struct packed_out_ *)data_out;
+
+        if (iterp == NULL || parg == NULL)
+            return -EFAULT;
+
+        (void)lem1802_renderers_iter_(iterp, &parg->name, &parg->args);
+
+        return 0;
+    }
+
+    if (strcmp(cmd, "renderer") == 0) {
+        struct lem1802_ *display = (struct lem1802_ *)hw->data;
+        char *renderer = (char *)data_in;
+        (void)data_out;
+        struct renderer_ *r;
+
+        for (r = lem1802_renderers_; r->renderer; r++) {
+            if (strcmp(renderer, r->name) == 0) {
+                display->render = r->renderer;
+                TRACE("%s>> renderer set to %s", __func__, renderer);
+                return 0;
+            }
+        }
+
+        hw->vm->warn_cb_("unknown renderer '%s'", renderer);
+
+        return -ENOENT;
+    }
+
+    if (strcmp(cmd, "renderer_data") == 0) {
+        struct lem1802_ *display = (struct lem1802_ *)hw->data;
+        (void)data_out;
+
+        display->renderer_data = data_in;
+
+        return 0;
+    }
+
+    return -EINVAL;
+}
+
+
+struct dcpu16_hw_module dcpu16_hw_module_lem1802 = {
+    .name_  = "LEM1802 - Low Energy Monitor",
+
+    .id_l   = 0xf615,
+    .id_h   = 0x7349,
+    .ver    = 0x1802,
+    .mfg_l  = 0x8b36,
+    .mfg_h  = 0x1c6c,
+    .hwi    = lem1802_hwi_,
+    .cycle  = lem1802_cycle_,
+    .reset  = lem1802_reset_,
+
+    .data_init = lem1802_data_init_,
+    .data_free = lem1802_data_free_,
+    .ctl = lem1802_data_ctl_,
+    .ctl_cmd = ctl_,
+};
+