+
+unsigned long jtagarm7tdmi_get_register(unsigned long reg) { // PROVEN - 100827
+ unsigned long retval=0L, instr, r0;
+ current_dbgstate = eice_read(EICE_DBGSTATUS);
+ if (current_dbgstate & JTAG_ARM7TDMI_DBG_TBIT){
+ if (reg > 7){
+ //debugstr("debug: jtagarm7tdmi_get_register: thumb reg > 15");
+ reg = reg & 7;
+ r0 = jtagarm7_get_reg_prim( THUMB_READ_REG); // save reg0
+ jtagarm7_thumb_swap_reg(THUMB_SWAP_HiLo, reg); // clobber reg0 with hi reg
+ retval = jtagarm7_get_reg_prim( THUMB_READ_REG); // recover 32-bit word
+ jtagarm7_set_reg_prim( THUMB_WRITE_REG, 0, r0); // restore r0
+ return retval;
+ } else {
+ instr = (unsigned long)(THUMB_READ_REG | (unsigned long)reg | (unsigned long)(reg<<16L));
+ }
+ } else
+ instr = (reg<<12L) | ARM_READ_REG; // STR Rx, [R14]
+ return jtagarm7_get_reg_prim(instr);
+}
+
+//! Set a 32-bit Register value
+// writing to a register is a problem child in ARM, actually. if the register you are using as the indirect offset register is misaligned, your results are misaligned.
+// this set_register implementation normalizes this process at the cost of performance. since we don't know what's in the register, we set it to 0 first
+// we could use r14 and hope all is well, but only for arm, not thumb mode, and not always is all well then either. this is a performance trade-off we may have to revisit later
+//
+void jtagarm7tdmi_set_register(unsigned long reg, unsigned long val) { // PROVEN - 100827
+ unsigned long instr, r0;
+ current_dbgstate = eice_read(EICE_DBGSTATUS);
+ if (current_dbgstate & JTAG_ARM7TDMI_DBG_TBIT){
+ if (reg > 7){
+
+ r0 = jtagarm7_get_reg_prim(THUMB_READ_REG);
+ jtagarm7_set_reg_prim(THUMB_WRITE_REG, 0, 0);
+ instr = (unsigned long)(THUMB_WRITE_REG | (unsigned long)reg | (unsigned long)(reg<<16L));
+ jtagarm7_set_reg_prim(instr, reg, val);
+ jtagarm7_thumb_swap_reg(THUMB_SWAP_LoHi, reg); // place 32-bit word into a high register
+ jtagarm7_set_reg_prim( THUMB_WRITE_REG, 0, r0); // restore r0
+ } else
+ instr = THUMB_WRITE_REG | (reg) | ((reg)<<16) | ((reg)<<3) | ((reg)<<19);
+ } else {
+ instr = ARM_WRITE_REG | (reg<<12L) | (reg<<16); // LDR Rx, [R14]
+ }
+
+ //debughex32(instr);
+ // --- first time to clear the register... this ensures the write is not 8-bit offset ---
+ jtagarm7_set_reg_prim(instr, reg, 0);
+ // --- now we actually write to the register ---
+ jtagarm7_set_reg_prim(instr, reg, val);
+}