protected final void writeByte(int adr, int data) {
    cycles++;

    schedule(cycles);
    // Locking only on fetch byte...
    // System.out.println("Writing byte at: " + Integer.toString(adr, 16)
    // + " = " + data);
    if (adr <= 1) {
      memory[adr] = data;
      int p = (memory[0] ^ 0xff) | memory[1];

      kernalROM = ((p & 2) == 2); // Kernal on
      basicROM = ((p & 3) == 3); // Basic on

      charROM = ((p & 3) != 0) && ((p & 4) == 0);
      // ioON is probably not correct!!! Check against table...
      ioON = ((p & 3) != 0) && ((p & 4) != 0);

      if (basicROM)
        romFlag = 0xa000;
      else if (kernalROM)
        romFlag = 0xe000;
      else
        romFlag = 0x10000; // No Rom at all (Basic / Kernal)
    }

    adr &= 0xffff;
    if (ioON && ((adr & 0xf000) == 0xd000)) {
      // System.out.println("IO Write at: " + Integer.toString(adr, 16));
      chips.performWrite(adr, data, cycles);
    } else {
      memory[windex = adr] = data;
    }
  }