moved module/hw fields around
[dcpu16] / dcpu16.c
index 2b39bafd35507092237027462fdf3d1b419502f0..817cfb0da6c54d6490f4d32fda344526d152ed1f 100644 (file)
--- a/dcpu16.c
+++ b/dcpu16.c
@@ -171,8 +171,9 @@ void dcpu16_cycle_inc(struct dcpu16 *vm, unsigned int n) {
 
         /* signal attached hardware */
         for (i = 0; i < vm->hw_table_entries_; i++) {
-            TRACE("%s>> notifying %s", __func__, vm->hw_table_[i].name_);
-            vm->hw_table_[i].cycle(vm, &vm->hw_table_[i]);
+            TRACE("%s>> notifying %s", __func__, vm->hw_table_[i].mod->name_);
+            if (vm->hw_table_[i].mod->cycle)
+                vm->hw_table_[i].mod->cycle(vm, &vm->hw_table_[i]);
         }
     }
 }
@@ -512,11 +513,11 @@ OP_IMPL(nbi_hwq) {
         return;
     }
 
-    vm->reg[DCPU16_REG_A] = vm->hw_table_[*a].id_l;
-    vm->reg[DCPU16_REG_B] = vm->hw_table_[*a].id_h;
-    vm->reg[DCPU16_REG_C] = vm->hw_table_[*a].ver;
-    vm->reg[DCPU16_REG_X] = vm->hw_table_[*a].mfg_l;
-    vm->reg[DCPU16_REG_Y] = vm->hw_table_[*a].mfg_h;
+    vm->reg[DCPU16_REG_A] = vm->hw_table_[*a].mod->id_l;
+    vm->reg[DCPU16_REG_B] = vm->hw_table_[*a].mod->id_h;
+    vm->reg[DCPU16_REG_C] = vm->hw_table_[*a].mod->ver;
+    vm->reg[DCPU16_REG_X] = vm->hw_table_[*a].mod->mfg_l;
+    vm->reg[DCPU16_REG_Y] = vm->hw_table_[*a].mod->mfg_h;
 
     dcpu16_cycle_inc(vm, 4);
 }
@@ -532,8 +533,8 @@ OP_IMPL(nbi_hwi) {
     }
 
     dcpu16_cycle_inc(vm, 4);
-    if (vm->hw_table_[*a].hwi)
-        vm->hw_table_[*a].hwi(vm, &vm->hw_table_[*a]);
+    if (vm->hw_table_[*a].mod->hwi)
+        vm->hw_table_[*a].mod->hwi(vm, &vm->hw_table_[*a]);
     else
         WARN("hardware 0x%04x has no interrupt handler", *a);
 }
@@ -1314,67 +1315,27 @@ void dcpu16_step(struct dcpu16 *vm) {
     }
 }
 
-/*
-    print the current state of the machine
-    shows current cycle count, registers, and next instruction
-*/
-void dcpu16_state_print(struct dcpu16 *vm) {
-    unsigned int i;
-
-    if (!vm) return;
-
-    printf("  ");
-    for (i = 0; i < 8; i++)
-        printf("  %s:0x%04x", dcpu16_reg_names[i], vm->reg[i]);
-    printf("\n");
-
-    printf("(0x%08llx) %2s:0x%04x %2s:0x%04x %2s:0x%04x %2s:0x%04x [%2s]:",
-           vm->cycle_,
-           dcpu16_reg_names[DCPU16_REG_EX], vm->reg[DCPU16_REG_EX],
-           dcpu16_reg_names[DCPU16_REG_SP], vm->reg[DCPU16_REG_SP],
-           dcpu16_reg_names[DCPU16_REG_PC], vm->reg[DCPU16_REG_PC],
-           dcpu16_reg_names[DCPU16_REG_IA], vm->reg[DCPU16_REG_IA],
-           "PC");
-
-    dcpu16_disassemble_print(vm, vm->reg[DCPU16_REG_PC]);
-    printf("\n");
-}
-
-/*  dcpu16_dump_ram
- *  print raw ram contents from start to stop
- */
-void dcpu16_dump_ram(struct dcpu16 *vm, DCPU16_WORD start, DCPU16_WORD end) {
-    unsigned int i, j;
-    const unsigned int n = 8; /* words per line */
-
-    if (!vm) return;
-
-    for (i = start, j = 0; i <= end; i++, j++) {
-        if (j % n == 0)
-            printf("0x%04x:\t", i);
-        printf(" %04x%s", vm->ram[i], (j % n) == (n - 1) ? "\n" : "");
-    }
-    if ((j % n) != (n - 1))
-        printf("\n");
-}
 
 /* instantiate a new 'hardware' device */
 struct dcpu16_hw *dcpu16_hw_new(struct dcpu16 *vm, struct dcpu16_hw_module *mod, void *data) {
     struct dcpu16_hw *hw;
 
-    hw = calloc(1, sizeof *hw);
+    hw = malloc(sizeof *hw);
     if (hw == NULL) {
-        vm->warn_cb_("%s():%s", "calloc", strerror(errno));
+        vm->warn_cb_("%s():%s", "malloc", strerror(errno));
         return NULL;
     }
-    memcpy(hw, mod->template, sizeof *hw);
     hw->vm = vm;
     hw->mod = mod;
 
-    if (mod->data_init(hw, data)) {
-        vm->warn_cb_("failed to init hw module data");
-        free(hw);
-        return NULL;
+    if (mod->data_init) {
+        if (mod->data_init(hw, data)) {
+            vm->warn_cb_("failed to init hw module data");
+            free(hw);
+            return NULL;
+        }
+    } else {
+        hw->data = NULL;
     }
 
     return hw;
@@ -1419,10 +1380,10 @@ int dcpu16_hw_attach(struct dcpu16 *vm, struct dcpu16_hw *hw) {
 
     TRACE("%s>> name:%s ID:0x%04x%04x MFG:0x%04x%04x VER:0x%04x",
           __func__,
-          hw->name_,
-          hw->id_h, hw->id_l,
-          hw->mfg_l, hw->mfg_h,
-          hw->ver);
+          hw->mod->name_,
+          hw->mod->id_h, hw->mod->id_l,
+          hw->mod->mfg_l, hw->mod->mfg_h,
+          hw->mod->ver);
 
     if (vm->hw_table_entries_ == 0xffff) {
         WARN("maximum hardware entries reached");
@@ -1501,8 +1462,8 @@ void dcpu16_reset(struct dcpu16 *vm) {
 
     /* signal attached hardware */
     for (i = 0; i < vm->hw_table_entries_; i++) {
-        if (vm->hw_table_[i].reset) 
-            vm->hw_table_[i].reset(vm, &vm->hw_table_[i]);
+        if (vm->hw_table_[i].mod->reset) 
+            vm->hw_table_[i].mod->reset(vm, &vm->hw_table_[i]);
     }
 
     memset(vm->reg, 0, sizeof vm->reg);