int joystate = gbJoymask;
int joystate = gbJoymask[joy];
u8 gbReadMemory(register u16 address){  if(gbCheatMap[address])    return gbCheatRead(address);    if(address < 0xa000)    return gbMemoryMap[address>>12][address&0x0fff];  if(address < 0xc000) {#ifndef FINAL_VERSION    if(memorydebug) {      log("Memory register read %04x PC=%04x\n",          address,          PC.W);    }#endif        if(mapperReadRAM)      return mapperReadRAM(address);    return gbMemoryMap[address>>12][address & 0x0fff];  }  if(address >= 0xff00) {    switch(address & 0x00ff) {    case 0x00:      {        if(gbSgbMode) {          gbSgbReadingController |= 4;          gbSgbResetPacketState();        }            int b = gbMemory[0xff00];        if((b & 0x30) == 0x20) {          b &= 0xf0;          int joy = 0;          if(gbSgbMode && gbSgbMultiplayer) {            switch(gbSgbNextController) {            case 0x0f:              joy = 0;              break;            case 0x0e:              joy = 1;              break;            case 0x0d:              joy = 2;              break;            case 0x0c:              joy = 3;              break;            default:              joy = 0;            }          }          int joystate = gbJoymask;          if(!(joystate & 128))            b |= 0x08;          if(!(joystate & 64))            b |= 0x04;          if(!(joystate & 32))            b |= 0x02;          if(!(joystate & 16))            b |= 0x01;                    gbMemory[0xff00] = b;        } else if((b & 0x30) == 0x10) {          b &= 0xf0;          int joy = 0;          if(gbSgbMode && gbSgbMultiplayer) {            switch(gbSgbNextController) {            case 0x0f:              joy = 0;              break;            case 0x0e:              joy = 1;              break;            case 0x0d:              joy = 2;              break;            case 0x0c:              joy = 3;              break;            default:              joy = 0;            }          }          int joystate = gbJoymask;          if(!(joystate & 8))            b |= 0x08;          if(!(joystate & 4))            b |= 0x04;          if(!(joystate & 2))            b |= 0x02;          if(!(joystate & 1))            b |= 0x01;                    gbMemory[0xff00] = b;        } else {          if(gbSgbMode && gbSgbMultiplayer) {            gbMemory[0xff00] = 0xf0 | gbSgbNextController;          } else {            gbMemory[0xff00] = 0xff;          }        }      }      return gbMemory[0xff00];      break;    case 0x01:      return gbMemory[0xff01];    case 0x04:      return register_DIV;    case 0x05:      return register_TIMA;    case 0x06:      return register_TMA;    case 0x07:      return (0xf8 | register_TAC);    case 0x0f:      return (0xe0 | register_IF);    case 0x40:      return register_LCDC;    case 0x41:      return (0x80 | register_STAT);    case 0x42:      return register_SCY;    case 0x43:      return register_SCX;    case 0x44:      return register_LY;    case 0x45:      return register_LYC;    case 0x46:      return register_DMA;    case 0x4a:      return register_WY;    case 0x4b:      return register_WX;    case 0x4f:      return (0xfe | register_VBK);    case 0x51:      return register_HDMA1;    case 0x52:      return register_HDMA2;    case 0x53:      return register_HDMA3;    case 0x54:      return register_HDMA4;    case 0x55:      return register_HDMA5;    case 0x70:      return (0xf8 | register_SVBK);    case 0xff:      return register_IE;    }  }    return gbMemoryMap[address>>12][address & 0x0fff];}