RudolfGeosits / MSP430-Emulator

Providing a software model of the TI MSP430
GNU General Public License v3.0
87 stars 20 forks source link

Incorrect handling of long jumps #14

Open Lurkerpas opened 4 years ago

Lurkerpas commented 4 years ago

Jump +778 jumps backwards.

devices/cpu/formatIII.c:decode_formatIII contains the following code:

  int16_t signed_offset = (instruction & 0x03FF) * 2;
  bool negative = signed_offset >> 9;

  char value[20];

  char mnemonic[100] = {0};
  /* String to show hex value of instruction */
  char hex_str[100] = {0};

  sprintf(hex_str, "%04X", instruction);

  if (negative) { /* Sign Extend for Arithmetic Operations */
    signed_offset |= 0xF800;
  }

The sign on bit 9 is tested after the offset is already multiplied by 2, without adjusting for the shift of one bit to the left, yielding an incorrectly negative value.

Multiplying the offset after the sign test and sign extension (with mask set to 0xFC00 to adjust) seems to work correctly and is more readable.