Program: 5618
Project: 5618
Buggy source:
public void dataReceived(USART source,int data){
  if (chipSelect) {
    if (DEBUG) {
      System.out.println("String_Node_Str" + data);
    }
switch (state) {
case READ_IDENT:
      source.byteReceived(identity[pos]);
    pos++;
  if (pos >= identity.length) {
    pos=0;
  }
return;
case WRITE_STATUS:
status=data;
source.byteReceived(0);
return;
case READ_DATA:
if (pos < 3) {
readAddress=(readAddress << 8) + data;
source.byteReceived(0);
pos++;
if (DEBUG && pos == 3) {
System.out.println("String_Node_Str" + Integer.toHexString(readAddress));
}
}
 else {
source.byteReceived(readMemory(readAddress++));
if (readAddress > 0xfffff) {
readAddress=0;
}
}
return;
case SECTOR_ERASE:
if (pos < 3) {
readAddress=(readAddress << 8) + data;
source.byteReceived(0);
pos++;
if (pos == 3) {
sectorErase(readAddress);
}
}
return;
case PAGE_PROGRAM:
if (pos < 3) {
readAddress=(readAddress << 8) + data;
source.byteReceived(0);
pos++;
if (pos == 3) {
for (int i=0; i < buffer.length; i++) {
buffer[i]=(byte)0xff;
}
blockWriteAddress=readAddress & 0xfff00;
if (DEBUG) {
System.out.println("String_Node_Str" + Integer.toHexString(readAddress));
}
}
}
 else {
source.byteReceived(0);
writeBuffer((readAddress++) & 0xff,data);
}
return;
}
if (DEBUG) {
System.out.println("String_Node_Str" + data);
}
switch (data) {
case WRITE_ENABLE:
if (DEBUG) {
System.out.println("String_Node_Str");
}
writeEnable=true;
break;
case WRITE_DISABLE:
if (DEBUG) {
System.out.println("String_Node_Str");
}
writeEnable=false;
break;
case READ_IDENT:
if (DEBUG) {
System.out.println("String_Node_Str");
}
state=READ_IDENT;
pos=0;
source.byteReceived(identity[pos++]);
return;
case READ_STATUS:
status=(status & (0xff - 1 - 2)) | (writeEnable ? 0x02 : 0x00) | (writing ? 0x01 : 0x00);
source.byteReceived(status);
if (DEBUG) {
System.out.println("String_Node_Str" + status);
}
return;
case WRITE_STATUS:
if (DEBUG) {
System.out.println("String_Node_Str");
}
state=WRITE_STATUS;
break;
case READ_DATA:
if (DEBUG) {
System.out.println("String_Node_Str");
}
state=READ_DATA;
pos=readAddress=0;
break;
case PAGE_PROGRAM:
if (DEBUG) {
System.out.println("String_Node_Str");
}
state=PAGE_PROGRAM;
pos=readAddress=0;
break;
case SECTOR_ERASE:
if (DEBUG) {
System.out.println("String_Node_Str");
}
state=SECTOR_ERASE;
pos=0;
break;
case BULK_ERASE:
System.out.println("String_Node_Str");
break;
}
source.byteReceived(0);
}
}

Fixed version (ground truth):
public void dataReceived(USART source,int data){
  if (chipSelect) {
    if (DEBUG) {
      System.out.println("String_Node_Str" + data);
    }
switch (state) {
case READ_STATUS:
      source.byteReceived(status);
    return;
case READ_IDENT:
  source.byteReceived(identity[pos]);
pos++;
if (pos >= identity.length) {
pos=0;
}
return;
case WRITE_STATUS:
status=data;
source.byteReceived(0);
return;
case READ_DATA:
if (pos < 3) {
readAddress=(readAddress << 8) + data;
source.byteReceived(0);
pos++;
if (DEBUG && pos == 3) {
System.out.println("String_Node_Str" + Integer.toHexString(readAddress));
}
}
 else {
source.byteReceived(readMemory(readAddress++));
if (readAddress > 0xfffff) {
readAddress=0;
}
}
return;
case SECTOR_ERASE:
if (pos < 3) {
readAddress=(readAddress << 8) + data;
source.byteReceived(0);
pos++;
if (pos == 3) {
sectorErase(readAddress);
}
}
return;
case PAGE_PROGRAM:
if (pos < 3) {
readAddress=(readAddress << 8) + data;
source.byteReceived(0);
pos++;
if (pos == 3) {
for (int i=0; i < buffer.length; i++) {
buffer[i]=(byte)0xff;
}
blockWriteAddress=readAddress & 0xfff00;
if (DEBUG) {
System.out.println("String_Node_Str" + Integer.toHexString(readAddress));
}
}
}
 else {
source.byteReceived(0);
writeBuffer((readAddress++) & 0xff,data);
}
return;
}
if (DEBUG) {
System.out.println("String_Node_Str" + data);
}
switch (data) {
case WRITE_ENABLE:
if (DEBUG) {
System.out.println("String_Node_Str");
}
writeEnable=true;
break;
case WRITE_DISABLE:
if (DEBUG) {
System.out.println("String_Node_Str");
}
writeEnable=false;
break;
case READ_IDENT:
if (DEBUG) {
System.out.println("String_Node_Str");
}
state=READ_IDENT;
pos=0;
source.byteReceived(0);
return;
case READ_STATUS:
status=(status & (0xff - 1 - 2)) | (writeEnable ? 0x02 : 0x00) | (writing ? 0x01 : 0x00);
state=READ_STATUS;
source.byteReceived(0);
if (DEBUG) {
System.out.println("String_Node_Str" + status + "String_Node_Str"+ cpu.getPC());
}
return;
case WRITE_STATUS:
if (DEBUG) {
System.out.println("String_Node_Str");
}
state=WRITE_STATUS;
break;
case READ_DATA:
if (DEBUG) {
System.out.println("String_Node_Str");
}
state=READ_DATA;
pos=readAddress=0;
break;
case PAGE_PROGRAM:
if (DEBUG) {
System.out.println("String_Node_Str");
}
state=PAGE_PROGRAM;
pos=readAddress=0;
break;
case SECTOR_ERASE:
if (DEBUG) {
System.out.println("String_Node_Str");
}
state=SECTOR_ERASE;
pos=0;
break;
case BULK_ERASE:
System.out.println("String_Node_Str");
break;
}
source.byteReceived(0);
}
}