further reorg of module abstraction and control interface
[dcpu16] / hw_spc2000.c
index 49ba35042c5ace4360c7eca649617c52b5cecb20..c967822279768111ec770cb7dbb42a3fcd39c325 100644 (file)
@@ -5,30 +5,36 @@
 #include "dcpu16.h"
 #include "hw_spc2000.h"
 
-static dcpu16_hw_signal_t spc2000_reset_;
-static dcpu16_hw_signal_t spc2000_cycle_;
-static dcpu16_hw_signal_t spc2000_hwi_;
-static struct dcpu16_hw hw_ = {
-    .name_  = "SPC2000 - Suspension Chamber 2000",
-    .id_l   = 0x1d9d,
-    .id_h   = 0x40e4,
-    .ver    = 0x005e,
-    .mfg_l  = 0x8b36,
-    .mfg_h  = 0x1c6c,
-    .hwi    = spc2000_hwi_,
-    .cycle  = spc2000_cycle_,
-    .reset  = spc2000_reset_,
-    .data   = (struct spc2000_ *)NULL
-};
-
 struct spc2000_ {
     DCPU16_WORD skip_unit;
     long long skip;
 };
 
 static
-void spc2000_reset_(struct dcpu16 *vm, void *data) {
-    struct spc2000_ *spc2000 = (struct spc2000_ *)data;
+int spc2000_data_init_(struct dcpu16_hw *hw, void *data) {
+    (void)data;
+
+    hw->data = calloc(1, sizeof(struct spc2000_));
+    if (hw->data == NULL) {
+        hw->vm->warn_cb_("%s():%s", "calloc", strerror(errno));
+        return -1;
+    }
+    return 0;
+}
+
+static
+void spc2000_data_free_(struct dcpu16_hw *hw) {
+    if (hw) {
+        if (hw->data) {
+            free(hw->data);
+            hw->data = NULL;
+        }
+    }
+}
+
+static
+void spc2000_reset_(struct dcpu16 *vm, struct dcpu16_hw *hw) {
+    struct spc2000_ *spc2000 = (struct spc2000_ *)hw->data;
 
     (void)vm;
 
@@ -36,16 +42,16 @@ void spc2000_reset_(struct dcpu16 *vm, void *data) {
 }
 
 static
-void spc2000_cycle_(struct dcpu16 *vm, void *data) {
-    struct spc2000_ *spc2000 = (struct spc2000_ *)data;
+void spc2000_cycle_(struct dcpu16 *vm, struct dcpu16_hw *hw) {
+    struct spc2000_ *spc2000 = (struct spc2000_ *)hw->data;
 
     (void)vm;
     (void)spc2000;
 }
 
 static
-void spc2000_hwi_(struct dcpu16 *vm, void *data) {
-    struct spc2000_ *spc2000 = (struct spc2000_ *)data;
+void spc2000_hwi_(struct dcpu16 *vm, struct dcpu16_hw *hw) {
+    struct spc2000_ *spc2000 = (struct spc2000_ *)hw->data;
     DCPU16_WORD reg_a = vm->reg[DCPU16_REG_A],
                 reg_b = vm->reg[DCPU16_REG_B];
     long long x;
@@ -79,38 +85,24 @@ void spc2000_hwi_(struct dcpu16 *vm, void *data) {
     }
 }
 
-struct dcpu16_hw *spc2000_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);
-
-    hw->data = calloc(1, sizeof(struct spc2000_));
-    if (hw->data == NULL) {
-        vm->warn_cb_("%s():%s", "calloc", strerror(errno));
-        free(hw);
-        return NULL;
-    }
-
-    hw->vm = vm;
-
-    return hw;
-}
+static struct dcpu16_hw hw_ = {
+    .vm     = NULL,
+    .name_  = "SPC2000 - Suspension Chamber 2000",
+    .id_l   = 0x1d9d,
+    .id_h   = 0x40e4,
+    .ver    = 0x005e,
+    .mfg_l  = 0x8b36,
+    .mfg_h  = 0x1c6c,
+    .hwi    = spc2000_hwi_,
+    .cycle  = spc2000_cycle_,
+    .reset  = spc2000_reset_,
+    .data   = (struct spc2000_ *)NULL
+};
 
-void spc2000_del(struct dcpu16_hw **hw) {
-    if (hw) {
-        if (*hw) {
-            if ((*hw)->data) {
-                free((*hw)->data);
-                (*hw)->data = NULL;
-            }
-            free(*hw);
-            *hw = NULL;
-        }
-    }
-}
+struct dcpu16_hw_module dcpu16_hw_module_spc2000 = {
+    .template = &hw_,
+    .data_init = spc2000_data_init_,
+    .data_free = spc2000_data_free_,
+    .ctl = NULL,
+    .ctl_cmd = NULL,
+};