Program: 3205
Project: 3205
Buggy source:
/** 
 * Ported from bsnes
 */
public int mode7Run(){
  int a=Util.sclip(16,BGRegisters.m7a.val);
  int b=Util.sclip(16,BGRegisters.m7b.val);
  int c=Util.sclip(16,BGRegisters.m7c.val);
  int d=Util.sclip(16,BGRegisters.m7d.val);
  int cx=Util.sclip(13,BGRegisters.m7x.val);
  int cy=Util.sclip(13,BGRegisters.m7y.val);
  int hoffset=Util.sclip(13,PPU.m7HOffset);
  int voffset=Util.sclip(13,PPU.m7VOffset);
  int tempX=PPU.x - 22;
  int tempY=PPU.y;
  if (PPU.m7XFlip)   tempX=255 - tempX;
  if (PPU.m7YFlip)   tempY=255 - tempY;
  int psx=((a * m7Clip(hoffset - cx)) & ~63) + ((b * m7Clip(voffset - cy)) & ~63) + ((b * tempY) & ~63)+ (cx << 8);
  int psy=((c * m7Clip(hoffset - cx)) & ~63) + ((d * m7Clip(voffset - cy)) & ~63) + ((d * tempY) & ~63)+ (cy << 8);
  int px=(psx + (a * tempX)) >> 8;
  int py=(psy + (x * tempX)) >> 8;
  int tile=0, palette=0, priority=0;
switch (PPU.m7Repeat) {
case 0:
case 1:
    px&=1023;
  py&=1023;
tile=PPU.vram[((py >> 3) * 128 + (px >> 3)) << 1];
palette=PPU.vram[(((tile << 6) + ((py & 7) << 3) + (px & 7)) << 1) + 1];
break;
case 2:
if (((px | py) & (~1023)) != 0) {
palette=0;
}
 else {
px&=1023;
py&=1023;
tile=PPU.vram[((py >> 3) * 128 + (px >> 3)) << 1];
palette=PPU.vram[(((tile << 6) + ((py & 7) << 3) + (px & 7)) << 1) + 1];
}
break;
case 3:
if (((px | py) & (~1023)) != 0) {
tile=0;
}
 else {
px&=1023;
py&=1023;
tile=PPU.vram[((py >> 3) * 128 + (px >> 3)) << 1];
}
palette=PPU.vram[(((tile << 6) + ((py & 7) << 3) + (px & 7)) << 1) + 1];
break;
}
if (num == 1) {
priority=priority0;
}
 else if (num == 2) {
priority=((palette & 0x80) != 0 ? priority1 : priority0);
palette&=0x7f;
}
if (palette != 0 && enabled()) {
boolean masked=Window.checkBackgroundMask(this);
boolean mainMask=windowMaskMain && masked;
boolean subMask=windowMaskSub && masked;
if (mainScreen && !mainMask && priority > PPU.priorityMain) {
PPU.priorityMain=priority;
PPU.colorMain=palette;
PPU.sourceMain=num - 1;
}
if (subScreen && !subMask && priority > PPU.prioritySub) {
PPU.prioritySub=priority;
PPU.colorSub=palette;
PPU.sourceSub=num - 1;
}
}
x++;
return palette;
}

Fixed version (ground truth):
/** 
 * Ported from bsnes
 */
public int mode7Run(){
  int a=Util.sclip(16,BGRegisters.m7a.val);
  int b=Util.sclip(16,BGRegisters.m7b.val);
  int c=Util.sclip(16,BGRegisters.m7c.val);
  int d=Util.sclip(16,BGRegisters.m7d.val);
  int cx=Util.sclip(13,BGRegisters.m7x.val);
  int cy=Util.sclip(13,BGRegisters.m7y.val);
  int hoffset=Util.sclip(13,PPU.m7HOffset);
  int voffset=Util.sclip(13,PPU.m7VOffset);
  int tempX=PPU.x - 22;
  int tempY=PPU.y;
  if (PPU.m7XFlip)   tempX=255 - tempX;
  if (PPU.m7YFlip)   tempY=255 - tempY;
  int psx=((a * m7Clip(hoffset - cx)) & (~63)) + ((b * m7Clip(voffset - cy)) & (~63)) + ((b * tempY) & (~63))+ (cx << 8);
  int psy=((c * m7Clip(hoffset - cx)) & (~63)) + ((d * m7Clip(voffset - cy)) & (~63)) + ((d * tempY) & (~63))+ (cy << 8);
  int px=(psx + (a * tempX)) >> 8;
  int py=(psy + (c * tempX)) >> 8;
  int tile=0, palette=0, priority=0;
switch (PPU.m7Repeat) {
case 0:
case 1:
    px&=1023;
  py&=1023;
tile=PPU.vram[((py >> 3) * 128 + (px >> 3)) << 1];
palette=PPU.vram[(((tile << 6) + ((py & 7) << 3) + (px & 7)) << 1) + 1];
break;
case 2:
if (((px | py) & (~1023)) != 0) {
palette=0;
}
 else {
px&=1023;
py&=1023;
tile=PPU.vram[((py >> 3) * 128 + (px >> 3)) << 1];
palette=PPU.vram[(((tile << 6) + ((py & 7) << 3) + (px & 7)) << 1) + 1];
}
break;
case 3:
if (((px | py) & (~1023)) != 0) {
tile=0;
}
 else {
px&=1023;
py&=1023;
tile=PPU.vram[((py >> 3) * 128 + (px >> 3)) << 1];
}
palette=PPU.vram[(((tile << 6) + ((py & 7) << 3) + (px & 7)) << 1) + 1];
break;
}
if (num == 1) {
priority=priority0;
}
 else if (num == 2) {
priority=((palette & 0x80) != 0 ? priority1 : priority0);
palette&=0x7f;
}
if (palette != 0 && enabled()) {
boolean masked=Window.checkBackgroundMask(this);
boolean mainMask=windowMaskMain && masked;
boolean subMask=windowMaskSub && masked;
if (mainScreen && !mainMask && priority > PPU.priorityMain) {
PPU.priorityMain=priority;
PPU.colorMain=palette;
PPU.sourceMain=num - 1;
}
if (subScreen && !subMask && priority > PPU.prioritySub) {
PPU.prioritySub=priority;
PPU.colorSub=palette;
PPU.sourceSub=num - 1;
}
}
x++;
return palette;
}