int i;
void CPUUpdateRegister(u32 address, u16 value){  switch(address) {  case 0x00:    {      bool change = ((DISPCNT ^ value) & 0x80) ? true : false;      bool changeBG = ((DISPCNT ^ value) & 0x0F00) ? true : false;      DISPCNT = (value & 0xFFF7);      UPDATE_REG(0x00, DISPCNT);      layerEnable = layerSettings & value;      windowOn = (layerEnable & 0x6000) ? true : false;      if(change && !((value & 0x80))) {        if(!(DISPSTAT & 1)) {          lcdTicks = 960;          //      VCOUNT = 0;          //      UPDATE_REG(0x06, VCOUNT);          DISPSTAT &= 0xFFFC;          UPDATE_REG(0x04, DISPSTAT);          CPUCompareVCOUNT();        }        //        (*renderLine)();      }      CPUUpdateRender();      // we only care about changes in BG0-BG3      if(changeBG)        CPUUpdateRenderBuffers(false);      //      CPUUpdateTicks();    }    break;  case 0x04:    DISPSTAT = (value & 0xFF38) | (DISPSTAT & 7);    UPDATE_REG(0x04, DISPSTAT);    break;  case 0x06:    // not writable    break;  case 0x08:    BG0CNT = (value & 0xDFCF);    UPDATE_REG(0x08, BG0CNT);    break;  case 0x0A:    BG1CNT = (value & 0xDFCF);    UPDATE_REG(0x0A, BG1CNT);    break;  case 0x0C:    BG2CNT = (value & 0xFFCF);    UPDATE_REG(0x0C, BG2CNT);    break;  case 0x0E:    BG3CNT = (value & 0xFFCF);    UPDATE_REG(0x0E, BG3CNT);    break;  case 0x10:    BG0HOFS = value & 511;    UPDATE_REG(0x10, BG0HOFS);    break;  case 0x12:    BG0VOFS = value & 511;    UPDATE_REG(0x12, BG0VOFS);    break;  case 0x14:    BG1HOFS = value & 511;    UPDATE_REG(0x14, BG1HOFS);    break;  case 0x16:    BG1VOFS = value & 511;    UPDATE_REG(0x16, BG1VOFS);    break;        case 0x18:    BG2HOFS = value & 511;    UPDATE_REG(0x18, BG2HOFS);    break;  case 0x1A:    BG2VOFS = value & 511;    UPDATE_REG(0x1A, BG2VOFS);    break;  case 0x1C:    BG3HOFS = value & 511;    UPDATE_REG(0x1C, BG3HOFS);    break;  case 0x1E:    BG3VOFS = value & 511;    UPDATE_REG(0x1E, BG3VOFS);    break;        case 0x20:    BG2PA = value;    UPDATE_REG(0x20, BG2PA);    break;  case 0x22:    BG2PB = value;    UPDATE_REG(0x22, BG2PB);    break;  case 0x24:    BG2PC = value;    UPDATE_REG(0x24, BG2PC);    break;  case 0x26:    BG2PD = value;    UPDATE_REG(0x26, BG2PD);    break;  case 0x28:    BG2X_L = value;    UPDATE_REG(0x28, BG2X_L);    gfxBG2Changed |= 1;    break;  case 0x2A:    BG2X_H = (value & 0xFFF);    UPDATE_REG(0x2A, BG2X_H);    gfxBG2Changed |= 1;        break;  case 0x2C:    BG2Y_L = value;    UPDATE_REG(0x2C, BG2Y_L);    gfxBG2Changed |= 2;        break;  case 0x2E:    BG2Y_H = value & 0xFFF;    UPDATE_REG(0x2E, BG2Y_H);    gfxBG2Changed |= 2;        break;  case 0x30:    BG3PA = value;    UPDATE_REG(0x30, BG3PA);    break;  case 0x32:    BG3PB = value;    UPDATE_REG(0x32, BG3PB);    break;  case 0x34:    BG3PC = value;    UPDATE_REG(0x34, BG3PC);    break;  case 0x36:    BG3PD = value;    UPDATE_REG(0x36, BG3PD);    break;  case 0x38:    BG3X_L = value;    UPDATE_REG(0x38, BG3X_L);    gfxBG3Changed |= 1;    break;  case 0x3A:    BG3X_H = value & 0xFFF;    UPDATE_REG(0x3A, BG3X_H);    gfxBG3Changed |= 1;        break;  case 0x3C:    BG3Y_L = value;    UPDATE_REG(0x3C, BG3Y_L);    gfxBG3Changed |= 2;        break;  case 0x3E:    BG3Y_H = value & 0xFFF;    UPDATE_REG(0x3E, BG3Y_H);    gfxBG3Changed |= 2;        break;  case 0x40:    WIN0H = value;    UPDATE_REG(0x40, WIN0H);    CPUUpdateWindow0();    break;  case 0x42:    WIN1H = value;    UPDATE_REG(0x42, WIN1H);    CPUUpdateWindow1();        break;        case 0x44:    WIN0V = value;    UPDATE_REG(0x44, WIN0V);    break;  case 0x46:    WIN1V = value;    UPDATE_REG(0x46, WIN1V);    break;  case 0x48:    WININ = value & 0x3F3F;    UPDATE_REG(0x48, WININ);    break;  case 0x4A:    WINOUT = value & 0x3F3F;    UPDATE_REG(0x4A, WINOUT);    break;  case 0x4C:    MOSAIC = value;    UPDATE_REG(0x4C, MOSAIC);    break;  case 0x50:    BLDMOD = value & 0x3FFF;    UPDATE_REG(0x50, BLDMOD);    fxOn = ((BLDMOD>>6)&3) != 0;    CPUUpdateRender();    break;  case 0x52:    COLEV = value & 0x1F1F;    UPDATE_REG(0x52, COLEV);    break;  case 0x54:    COLY = value & 0x1F;    UPDATE_REG(0x54, COLY);    break;  case 0x60:  case 0x62:  case 0x64:  case 0x68:  case 0x6c:  case 0x70:  case 0x72:  case 0x74:  case 0x78:  case 0x7c:  case 0x80:  case 0x84:    soundEvent(address&0xFF, (u8)(value & 0xFF));    soundEvent((address&0xFF)+1, (u8)(value>>8));    break;  case 0x82:  case 0x88:  case 0xa0:  case 0xa2:  case 0xa4:  case 0xa6:  case 0x90:  case 0x92:  case 0x94:  case 0x96:  case 0x98:  case 0x9a:  case 0x9c:  case 0x9e:        soundEvent(address&0xFF, value);    break;  case 0xB0:    DM0SAD_L = value;    UPDATE_REG(0xB0, DM0SAD_L);    break;  case 0xB2:    DM0SAD_H = value & 0x07FF;    UPDATE_REG(0xB2, DM0SAD_H);    break;  case 0xB4:    DM0DAD_L = value;    UPDATE_REG(0xB4, DM0DAD_L);    break;  case 0xB6:    DM0DAD_H = value & 0x07FF;    UPDATE_REG(0xB6, DM0DAD_H);    break;  case 0xB8:    DM0CNT_L = value & 0x3FFF;    UPDATE_REG(0xB8, 0);    break;  case 0xBA:    {      bool start = ((DM0CNT_H ^ value) & 0x8000) ? true : false;      value &= 0xF7E0;      DM0CNT_H = value;      UPDATE_REG(0xBA, DM0CNT_H);              if(start && (value & 0x8000)) {        dma0Source = DM0SAD_L | (DM0SAD_H << 16);        dma0Dest = DM0DAD_L | (DM0DAD_H << 16);        CPUCheckDMA(0, 1);      }    }    break;        case 0xBC:    DM1SAD_L = value;    UPDATE_REG(0xBC, DM1SAD_L);    break;  case 0xBE:    DM1SAD_H = value & 0x0FFF;    UPDATE_REG(0xBE, DM1SAD_H);    break;  case 0xC0:    DM1DAD_L = value;    UPDATE_REG(0xC0, DM1DAD_L);    break;  case 0xC2:    DM1DAD_H = value & 0x07FF;    UPDATE_REG(0xC2, DM1DAD_H);    break;  case 0xC4:    DM1CNT_L = value & 0x3FFF;    UPDATE_REG(0xC4, 0);    break;  case 0xC6:    {      bool start = ((DM1CNT_H ^ value) & 0x8000) ? true : false;      value &= 0xF7E0;            DM1CNT_H = value;      UPDATE_REG(0xC6, DM1CNT_H);            if(start && (value & 0x8000)) {        dma1Source = DM1SAD_L | (DM1SAD_H << 16);        dma1Dest = DM1DAD_L | (DM1DAD_H << 16);        CPUCheckDMA(0, 2);      }    }    break;  case 0xC8:    DM2SAD_L = value;    UPDATE_REG(0xC8, DM2SAD_L);    break;  case 0xCA:    DM2SAD_H = value & 0x0FFF;    UPDATE_REG(0xCA, DM2SAD_H);    break;  case 0xCC:    DM2DAD_L = value;    UPDATE_REG(0xCC, DM2DAD_L);    break;  case 0xCE:    DM2DAD_H = value & 0x07FF;    UPDATE_REG(0xCE, DM2DAD_H);    break;  case 0xD0:    DM2CNT_L = value & 0x3FFF;    UPDATE_REG(0xD0, 0);    break;  case 0xD2:    {      bool start = ((DM2CNT_H ^ value) & 0x8000) ? true : false;            value &= 0xF7E0;            DM2CNT_H = value;      UPDATE_REG(0xD2, DM2CNT_H);            if(start && (value & 0x8000)) {        dma2Source = DM2SAD_L | (DM2SAD_H << 16);        dma2Dest = DM2DAD_L | (DM2DAD_H << 16);        CPUCheckDMA(0, 4);      }                }    break;  case 0xD4:    DM3SAD_L = value;    UPDATE_REG(0xD4, DM3SAD_L);    break;  case 0xD6:    DM3SAD_H = value & 0x0FFF;    UPDATE_REG(0xD6, DM3SAD_H);    break;  case 0xD8:    DM3DAD_L = value;    UPDATE_REG(0xD8, DM3DAD_L);    break;  case 0xDA:    DM3DAD_H = value & 0x0FFF;    UPDATE_REG(0xDA, DM3DAD_H);    break;  case 0xDC:    DM3CNT_L = value;    UPDATE_REG(0xDC, 0);    break;  case 0xDE:    {      bool start = ((DM3CNT_H ^ value) & 0x8000) ? true : false;      value &= 0xFFE0;      DM3CNT_H = value;      UPDATE_REG(0xDE, DM3CNT_H);          if(start && (value & 0x8000)) {        dma3Source = DM3SAD_L | (DM3SAD_H << 16);        dma3Dest = DM3DAD_L | (DM3DAD_H << 16);        CPUCheckDMA(0,8);      }    }    break;  case 0x100:    timer0Reload = value;    break;  case 0x102:    timer0Ticks = timer0ClockReload = TIMER_TICKS[value & 3];            if(!timer0On && (value & 0x80)) {      // reload the counter      TM0D = timer0Reload;            if(timer0ClockReload == 1)        timer0Ticks = 0x10000 - TM0D;      UPDATE_REG(0x100, TM0D);      int event = cpuTotalTicks + timer0Ticks;      if(event < cpuNextEvent)        cpuNextEvent = event;    }    timer0On = value & 0x80 ? true : false;    TM0CNT = value & 0xC7;    UPDATE_REG(0x102, TM0CNT);    //    CPUUpdateTicks();    break;  case 0x104:    timer1Reload = value;    break;  case 0x106:    timer1Ticks = timer1ClockReload = TIMER_TICKS[value & 3];            if(!timer1On && (value & 0x80)) {      // reload the counter      TM1D = timer1Reload;            if(timer1ClockReload == 1)        timer1Ticks = 0x10000 - TM1D;      UPDATE_REG(0x104, TM1D);      int event = cpuTotalTicks + timer1Ticks;      if(event < cpuNextEvent)        cpuNextEvent = event;    }    timer1On = value & 0x80 ? true : false;    TM1CNT = value & 0xC7;    UPDATE_REG(0x106, TM1CNT);    break;  case 0x108:    timer2Reload = value;    break;  case 0x10A:    timer2Ticks = timer2ClockReload = TIMER_TICKS[value & 3];            if(!timer2On && (value & 0x80)) {      // reload the counter      TM2D = timer2Reload;            if(timer2ClockReload == 1)        timer2Ticks = 0x10000 - TM2D;      UPDATE_REG(0x108, TM2D);      int event = cpuTotalTicks + timer2Ticks;      if(event < cpuNextEvent)        cpuNextEvent = event;    }    timer2On = value & 0x80 ? true : false;    TM2CNT = value & 0xC7;    UPDATE_REG(0x10A, TM2CNT);    break;  case 0x10C:    timer3Reload = value;    break;  case 0x10E:    timer3Ticks = timer3ClockReload = TIMER_TICKS[value & 3];            if(!timer3On && (value & 0x80)) {      // reload the counter      TM3D = timer3Reload;            if(timer3ClockReload == 1)        timer3Ticks = 0x10000 - TM3D;      UPDATE_REG(0x10C, TM3D);      int event = cpuTotalTicks + timer3Ticks;      if(event < cpuNextEvent)        cpuNextEvent = event;    }    timer3On = value & 0x80 ? true : false;    TM3CNT = value & 0xC7;    UPDATE_REG(0x10E, TM3CNT);    break;  case 0x128:    if(value & 0x80) {      value &= 0xff7f;      if(value & 1 && (value & 0x4000)) {        UPDATE_REG(0x12a, 0xFF);        IF |= 0x80;        UPDATE_REG(0x202, IF);        value &= 0x7f7f;      }    }    UPDATE_REG(0x128, value);    break;  case 0x130:    P1 |= (value & 0x3FF);    UPDATE_REG(0x130, P1);    break;  case 0x132:    UPDATE_REG(0x132, value & 0xC3FF);    break;  case 0x200:    IE = value & 0x3FFF;    UPDATE_REG(0x200, IE);    if((IME & 1) && (IF & IE) && armIrqEnable) {      cpuNextEvent = 0;    }        break;  case 0x202:    IF ^= (value & IF);    UPDATE_REG(0x202, IF);    break;  case 0x204:    {      int i;      memoryWait[0x0e] = memoryWaitSeq[0x0e] = gamepakRamWaitState[value & 3];            if(!speedHack) {        memoryWait[0x08] = memoryWait[0x09] = gamepakWaitState[(value >> 2) & 7];        memoryWaitSeq[0x08] = memoryWaitSeq[0x09] =          gamepakWaitState0[(value >> 2) & 7];                memoryWait[0x0a] = memoryWait[0x0b] = gamepakWaitState[(value >> 5) & 7];        memoryWaitSeq[0x0a] = memoryWaitSeq[0x0b] =          gamepakWaitState1[(value >> 5) & 7];                memoryWait[0x0c] = memoryWait[0x0d] = gamepakWaitState[(value >> 8) & 7];        memoryWaitSeq[0x0c] = memoryWaitSeq[0x0d] =          gamepakWaitState2[(value >> 8) & 7];      } else {        memoryWait[0x08] = memoryWait[0x09] = 4;        memoryWaitSeq[0x08] = memoryWaitSeq[0x09] = 2;                memoryWait[0x0a] = memoryWait[0x0b] = 4;        memoryWaitSeq[0x0a] = memoryWaitSeq[0x0b] = 4;                memoryWait[0x0c] = memoryWait[0x0d] = 4;        memoryWaitSeq[0x0c] = memoryWaitSeq[0x0d] = 8;      }      for(i = 0; i < 16; i++) {        memoryWaitFetch32[i] = memoryWait32[i] = memoryWait[i] *          (memory32[i] ? 1 : 2);        memoryWaitFetch[i] = memoryWait[i];      }      memoryWaitFetch32[3] += 1;      memoryWaitFetch32[2] += 3;            if(value & 0x4000) {        for(i = 8; i < 16; i++) {          memoryWaitFetch32[i] = cpuMemoryWait32[i];          memoryWaitFetch[i] = cpuMemoryWait[i];        }      }      UPDATE_REG(0x204, value & 0x7FFF);    }    break;  case 0x208:    IME = value & 1;    UPDATE_REG(0x208, IME);    if((IME & 1) && (IF & IE) && armIrqEnable) {      cpuNextEvent = 0;    }    break;  case 0x300:    if(value != 0)      value &= 0xFFFE;    UPDATE_REG(0x300, value);    break;  default:    UPDATE_REG(address&0x3FE, value);    break;  }}