Program: 5590
Project: 5590
Buggy source:
public void dataReceived(USART source,int data){
  int oldStatus=status;
  if (DEBUG) {
    log("String_Node_Str" + Utils.hex8(data) + "String_Node_Str"+ ((data >= ' ' && data <= 'Z') ? (char)data : '.')+ ')'+ "String_Node_Str"+ chipSelect+ "String_Node_Str"+ state+ "String_Node_Str"+ stateMachine);
  }
  if ((stateMachine != RadioState.VREG_OFF) && chipSelect) {
switch (state) {
case WAITING:
      if ((data & FLAG_READ) != 0) {
        state=SpiState.READ_REGISTER;
      }
 else {
        state=SpiState.WRITE_REGISTER;
      }
    if ((data & FLAG_RAM) != 0) {
      state=SpiState.RAM_ACCESS;
      address=data & 0x7f;
    }
 else {
      address=data & 0x3f;
      if (address == REG_RXFIFO) {
        state=SpiState.READ_RXFIFO;
      }
 else       if (address == REG_TXFIFO) {
        state=SpiState.WRITE_TXFIFO;
      }
    }
  if (data < 0x0f) {
    strobe(data);
    state=SpiState.WAITING;
  }
pos=0;
break;
case WRITE_REGISTER:
if (pos == 0) {
source.byteReceived(registers[address] >> 8);
registers[address]=(registers[address] & 0xff) | (data << 8);
pos=1;
}
 else {
source.byteReceived(registers[address] & 0xff);
registers[address]=(registers[address] & 0xff00) | data;
if (DEBUG) {
log("String_Node_Str" + Utils.hex8(address) + "String_Node_Str"+ registers[address]);
}
data=registers[address];
setReg(address,data);
state=SpiState.WAITING;
}
break;
case READ_REGISTER:
if (pos == 0) {
source.byteReceived(registers[address] >> 8);
pos=1;
}
 else {
source.byteReceived(registers[address] & 0xff);
if (DEBUG) {
log("String_Node_Str" + Utils.hex8(address) + "String_Node_Str"+ registers[address]);
}
state=SpiState.WAITING;
}
return;
case READ_RXFIFO:
if (DEBUG) log("String_Node_Str" + rxfifoReadPos + "String_Node_Str"+ (memory[RAM_RXFIFO + rxfifoReadPos] & 0xFF)+ "String_Node_Str"+ rxfifoLen);
source.byteReceived((memory[RAM_RXFIFO + rxfifoReadPos] & 0xFF));
if (rxfifoLen > 0) {
if (rxfifoReadLeft == 0) {
rxfifoReadLeft=(memory[RAM_RXFIFO + rxfifoReadPos] & 0xFF);
}
 else if (--rxfifoReadLeft == 0) {
if (rxfifoLen > 1 && rxfifoLen >= (memory[RAM_RXFIFO + (rxfifoReadPos + 1) & 127] & 0xFF) + 1) {
if (!overflow) setFIFOP(true);
}
}
rxfifoReadPos=(rxfifoReadPos + 1) & 127;
rxfifoLen--;
}
if (rxfifoLen == 0) {
if (DEBUG) log("String_Node_Str");
setFIFO(false);
}
if (fifoP && !overflow) {
if (DEBUG) log("String_Node_Str" + rxfifoReadPos + "String_Node_Str"+ lastPacketStart);
setFIFOP(false);
}
return;
case WRITE_TXFIFO:
if (txfifoFlush) {
txCursor=0;
txfifoFlush=false;
}
if (DEBUG) log("String_Node_Str" + data + "String_Node_Str"+ txCursor);
if (txCursor == 0) {
if ((data & 0xff) > 127) {
logger.warning(this,"String_Node_Str");
}
}
 else if (txCursor > 127) {
logger.warning(this,"String_Node_Str");
txCursor=0;
}
memory[RAM_TXFIFO + txCursor]=data & 0xff;
txCursor++;
if (sendEvents) {
sendEvent("String_Node_Str",null);
}
break;
case RAM_ACCESS:
if (pos == 0) {
address|=(data << 1) & 0x180;
ramRead=(data & 0x20) != 0;
if (DEBUG) {
log("String_Node_Str" + Utils.hex16(address) + "String_Node_Str"+ ramRead);
}
pos++;
}
 else {
if (!ramRead) {
memory[address++]=data;
if (address >= 0x180) {
logger.warning(this,"String_Node_Str");
address=0;
}
if (DEBUG && address == RAM_PANID + 2) {
log("String_Node_Str" + Utils.hex8(memory[RAM_PANID]) + Utils.hex8(memory[RAM_PANID + 1]));
}
}
 else {
source.byteReceived(memory[address++]);
if (address >= 0x180) {
logger.warning(this,"String_Node_Str");
address=0;
}
return;
}
}
break;
}
source.byteReceived(oldStatus);
}
}

Fixed version (ground truth):
public void dataReceived(USART source,int data){
  int oldStatus=status;
  if (DEBUG) {
    log("String_Node_Str" + Utils.hex8(data) + "String_Node_Str"+ ((data >= ' ' && data <= 'Z') ? (char)data : '.')+ ')'+ "String_Node_Str"+ chipSelect+ "String_Node_Str"+ state+ "String_Node_Str"+ stateMachine);
  }
  if ((stateMachine != RadioState.VREG_OFF) && chipSelect) {
switch (state) {
case WAITING:
      if ((data & FLAG_READ) != 0) {
        state=SpiState.READ_REGISTER;
      }
 else {
        state=SpiState.WRITE_REGISTER;
      }
    if ((data & FLAG_RAM) != 0) {
      state=SpiState.RAM_ACCESS;
      address=data & 0x7f;
    }
 else {
      address=data & 0x3f;
      if (address == REG_RXFIFO) {
        state=SpiState.READ_RXFIFO;
      }
 else       if (address == REG_TXFIFO) {
        state=SpiState.WRITE_TXFIFO;
      }
    }
  if (data < 0x0f) {
    strobe(data);
    state=SpiState.WAITING;
  }
pos=0;
break;
case WRITE_REGISTER:
if (pos == 0) {
source.byteReceived(registers[address] >> 8);
registers[address]=(registers[address] & 0xff) | (data << 8);
pos=1;
}
 else {
source.byteReceived(registers[address] & 0xff);
registers[address]=(registers[address] & 0xff00) | data;
if (DEBUG) {
log("String_Node_Str" + Utils.hex8(address) + "String_Node_Str"+ registers[address]);
}
data=registers[address];
setReg(address,data);
state=SpiState.WAITING;
}
break;
case READ_REGISTER:
if (pos == 0) {
source.byteReceived(registers[address] >> 8);
pos=1;
}
 else {
source.byteReceived(registers[address] & 0xff);
if (DEBUG) {
log("String_Node_Str" + Utils.hex8(address) + "String_Node_Str"+ registers[address]);
}
state=SpiState.WAITING;
}
return;
case READ_RXFIFO:
if (DEBUG) log("String_Node_Str" + rxfifoReadPos + "String_Node_Str"+ (memory[RAM_RXFIFO + rxfifoReadPos] & 0xFF)+ "String_Node_Str"+ rxfifoLen);
source.byteReceived((memory[RAM_RXFIFO + rxfifoReadPos] & 0xFF));
if (rxfifoLen > 0) {
if (fifoP && !overflow) {
if (DEBUG) log("String_Node_Str" + rxfifoReadPos + "String_Node_Str"+ lastPacketStart);
setFIFOP(false);
}
if (rxfifoReadLeft == 0) {
rxfifoReadLeft=(memory[RAM_RXFIFO + rxfifoReadPos] & 0xFF);
System.out.println("String_Node_Str" + rxfifoReadLeft);
}
 else if (--rxfifoReadLeft == 0) {
if (rxfifoLen > 1 && rxfifoLen >= (memory[RAM_RXFIFO + (rxfifoReadPos + 1) & 127] & 0xFF) + 1) {
System.out.println("String_Node_Str");
if (!overflow) setFIFOP(true);
}
}
rxfifoReadPos=(rxfifoReadPos + 1) & 127;
rxfifoLen--;
}
if (rxfifoLen == 0) {
if (DEBUG) log("String_Node_Str");
setFIFO(false);
}
return;
case WRITE_TXFIFO:
if (txfifoFlush) {
txCursor=0;
txfifoFlush=false;
}
if (DEBUG) log("String_Node_Str" + data + "String_Node_Str"+ txCursor);
if (txCursor == 0) {
if ((data & 0xff) > 127) {
logger.warning(this,"String_Node_Str");
}
}
 else if (txCursor > 127) {
logger.warning(this,"String_Node_Str");
txCursor=0;
}
memory[RAM_TXFIFO + txCursor]=data & 0xff;
txCursor++;
if (sendEvents) {
sendEvent("String_Node_Str",null);
}
break;
case RAM_ACCESS:
if (pos == 0) {
address|=(data << 1) & 0x180;
ramRead=(data & 0x20) != 0;
if (DEBUG) {
log("String_Node_Str" + Utils.hex16(address) + "String_Node_Str"+ ramRead);
}
pos++;
}
 else {
if (!ramRead) {
memory[address++]=data;
if (address >= 0x180) {
logger.warning(this,"String_Node_Str");
address=0;
}
if (DEBUG && address == RAM_PANID + 2) {
log("String_Node_Str" + Utils.hex8(memory[RAM_PANID]) + Utils.hex8(memory[RAM_PANID + 1]));
}
}
 else {
source.byteReceived(memory[address++]);
if (address >= 0x180) {
logger.warning(this,"String_Node_Str");
address=0;
}
return;
}
}
break;
}
source.byteReceived(oldStatus);
}
}