fixed bug with instruction decoding in dcpu, fixed bugs in display
[dcpu16] / dcpu16.c
index d7b3d4822eb25cf61edf0cd2c4b9fd1f5f6dbd48..ea0ee261d7c02398e8d4c26caf9a2fd5720545d1 100644 (file)
--- a/dcpu16.c
+++ b/dcpu16.c
@@ -180,8 +180,9 @@ void value_decode_(struct dcpu16 *vm, DCPU16_WORD value, unsigned int value_is_a
     DCPU16_WORD pc = (DCPU16_WORD)(vm->reg[DCPU16_REG_PC] + *pc_adjust),
                 sp = (DCPU16_WORD)(vm->reg[DCPU16_REG_PC] + *sp_adjust);
 
-    TRACE("%s: pc:0x%04x sp:0x%04x value_data:0x%04x\n", 
+    TRACE("%s>> is_a:%u pc:0x%04x sp:0x%04x value_data:0x%04x\n", 
           __func__,
+          value_is_a,
           pc,
           sp,
           value_data);
@@ -1113,9 +1114,9 @@ void instruction_decode_(DCPU16_WORD *mem, DCPU16_WORD addr,
     *a = (mem[addr] >> (OPCODE_BASIC_BITS + OPCODE_OPERAND_B_BITS)) & ((1 << OPCODE_OPERAND_A_BITS) - 1);
     *instr_len = 1;
 
-    if ( (*b >= 0x10 && *b <= 0x17) || *b == 0x1e || *b == 0x1f ) {
+    if ((*opcode != 0x0000) &&
+        ( (*b >= 0x10 && *b <= 0x17) || *b == 0x1e || *b == 0x1f ) ) {
         *b_data = mem + (DCPU16_WORD)(addr + *instr_len);
-        TRACE("**b_data:%hu", **b_data);
         *instr_len += 1;
     } else {
         *b_data = NULL;
@@ -1124,13 +1125,11 @@ void instruction_decode_(DCPU16_WORD *mem, DCPU16_WORD addr,
     if ( (*opcode != 0x0000 || (*opcode == 0 && *b != 0x0000) )
     &&   ( (*a >= 0x10 && *a <= 0x17) || *a == 0x1e || *a == 0x1f) ) {
         *a_data = mem + (DCPU16_WORD)(addr + *instr_len);
-        TRACE("**a_data:%hu", **a_data);
         *instr_len += 1;
     } else {
         *a_data = NULL;
     }
 
-#if 0
     TRACE("\n%s: [0x%04x]:0x%04x op:0x%02x b:0x%02x (b_data:0x%04x) a:0x%02x (a_data:0x%04x) len:0x%02x\n",
             __func__, 
             addr,
@@ -1141,7 +1140,6 @@ void instruction_decode_(DCPU16_WORD *mem, DCPU16_WORD addr,
             *a,
             *a_data ? **a_data : 0,
             *instr_len);
-#endif
 }
 
 /*  dcpu16_mnemonify_buf
@@ -1234,16 +1232,17 @@ void dcpu16_step(struct dcpu16 *vm) {
     size_t i;
     const struct opcode_entry *e;
 
-    if (!vm) return;
+    if (!vm)
+        return;
 
     /* signal interested parties that a new cycle has ticked */
-    TRACE("%s: sending global cycle event", __func__);
+    TRACE("%s>> sending global cycle event", __func__);
     acct_event_(vm, DCPU16_ACCT_EV_CYCLE, vm->reg[DCPU16_REG_PC]);
 
     /* signal attached hardware */
     for (i = 0; i < vm->hw_table_entries_; i++) {
         if (vm->hw_table_[i].cycle) {
-            TRACE("%s: sending cycle to %s", __func__, vm->hw_table_[i].name_);
+            TRACE("%s>> sending cycle to %s", __func__, vm->hw_table_[i].name_);
             vm->hw_table_[i].cycle(vm, vm->hw_table_[i].data);
         }
     }
@@ -1284,7 +1283,7 @@ void dcpu16_step(struct dcpu16 *vm) {
             message = interrupt_dequeue_(vm);
 
             if (vm->reg[DCPU16_REG_IA]) {
-                TRACE("servicing interrupt IA:0x%04x message:0x%04x \n", vm->reg[DCPU16_REG_IA], message);
+                TRACE("%s>> servicing interrupt IA:0x%04x message:0x%04x \n", __func__, vm->reg[DCPU16_REG_IA], message);
                 /* then service the next interrupt */
                 vm->interrupts_deferred_ = 1;
                 vm->ram[--vm->reg[DCPU16_REG_SP]] = vm->reg[DCPU16_REG_PC];
@@ -1292,7 +1291,7 @@ void dcpu16_step(struct dcpu16 *vm) {
                 vm->reg[DCPU16_REG_PC] = vm->reg[DCPU16_REG_IA];
                 vm->reg[DCPU16_REG_A] = message;
             } else {
-                TRACE("ignoring interrupt IA:0");
+                TRACE("%s>> ignoring interrupt IA:0", __func__);
             }
         }
     }
@@ -1349,6 +1348,13 @@ int dcpu16_hw_add(struct dcpu16 *vm, struct dcpu16_hw *hw) {
     if (!vm || !hw)
         return -1;
 
+    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);
+
     if (vm->hw_table_entries_ == 0xffff) {
         WARN("maximum hardware entries reached");
         return -1;
@@ -1368,6 +1374,8 @@ int dcpu16_hw_add(struct dcpu16 *vm, struct dcpu16_hw *hw) {
     memcpy(vm->hw_table_ + vm->hw_table_entries_, hw, sizeof *hw);
     vm->hw_table_entries_++;
 
+    TRACE("%s>> added hw entry %zu", __func__, vm->hw_table_entries_);
+
     return 0;
 }
 
@@ -1399,6 +1407,8 @@ int dcpu16_acct_add(struct dcpu16 *vm, dcpu16_acct_event mask, dcpu16_ev_cb_t *f
     memcpy(vm->cb_table_ + vm->cb_table_entries_, &cb, sizeof cb);
     vm->cb_table_entries_++;
 
+    TRACE("%s>> attached event callback %zu", __func__, vm->cb_table_entries_);
+
     return 0;
 }
 
@@ -1411,6 +1421,8 @@ void dcpu16_reset(struct dcpu16 *vm) {
     if (!vm)
         return;
 
+    TRACE("%s>> reset", __func__);
+
     vm->skip_ = 0;
     vm->interrupts_deferred_ = 0;
     vm->on_fire_ = 0;