     case 0xd:
       if (LCD) printAddr(addr, 8, 1);
       if (LCD) printByte(val, 14, 1);
-      switch (addr & 0xff) {
+      switch (addrl) {
         default:
           pia[addr & 0xff] = val;
           break;

  }
}

void memWrite(const uint8_t addrh, const uint8_t addrl, const uint8_t val) {
  const uint16_t addr = ADDR;
  switch (addr >> 12) {
    default:
      // nothing in the address space ignore the write;

    cli();
    cbi(PORTB, 0);
    __asm__("nop\n\t""nop\n\t""nop\n\t""nop\n\t"); // 4 * 62.5ns delay @ 16mhz
    sbi(PORTB, 0);
    sei();
    if (PINB & _BV(PB2)) {
      // read cycle
      DDRL = 0xff;
      PORTL = memRead();
    } else {
      // write cycle
      DDRL = 0x00;
      PORTL = 0;
      memWrite();
    }
  }
}
