modualarized 'hardware' api changes complete
[dcpu16] / hw_lem1802.c
index 31a69b8be3e8f6ffc5fccf0c5b8ce3ba6f1d9bc6..f17ebc73287a2eff7eaa083f37fb400e6bf3548c 100644 (file)
 #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 */
@@ -383,8 +367,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 +392,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 +448,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];
 
@@ -562,54 +546,63 @@ 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);
+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) {
+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) {
+            /* FIXME: free renderer data */
+            hw->vm->warn_cb_("FIXME");
+
+            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 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
+};
+
+struct dcpu16_hw_module dcpu16_hw_module_lem1802 = {
+    .template = &hw_,
+    .data_init = lem1802_data_init_,
+    .data_free = lem1802_data_free_,
+};
+