@Override
    public int execute(int cycles) {
        m6809_ICount[0] = cycles - m6809.extra_cycles;
    	m6809.extra_cycles = 0;
    
    	if ((m6809.int_state & (M6809_CWAI | M6809_SYNC))!=0)
    	{
    		m6809_ICount[0] = 0;
    	}
    	else
    	{
    		do
    		{
    			m6809.ppc = m6809.pc;
    
    			m6809.ireg = ROP(m6809.pc);
    			m6809.pc = m6809.pc + 1 & 0xFFFF;
                switch( m6809.ireg )
    		{
    			case 0x00: neg_di();   m6809_ICount[0]-= 6; break;
    			case 0x01: illegal();  m6809_ICount[0]-= 2; break;
    			case 0x02: illegal();  m6809_ICount[0]-= 2; break;
    			case 0x03: com_di();   m6809_ICount[0]-= 6; break;
    			case 0x04: lsr_di();   m6809_ICount[0]-= 6; break;
    			case 0x05: illegal();  m6809_ICount[0]-= 2; break;
    			case 0x06: ror_di();   m6809_ICount[0]-= 6; break;
    			case 0x07: asr_di();   m6809_ICount[0]-= 6; break;
    			case 0x08: asl_di();   m6809_ICount[0]-= 6; break;
    			case 0x09: rol_di();   m6809_ICount[0]-= 6; break;
    			case 0x0a: dec_di();   m6809_ICount[0]-= 6; break;
    			case 0x0b: illegal();  m6809_ICount[0]-= 2; break;
    			case 0x0c: inc_di();   m6809_ICount[0]-= 6; break;
    			case 0x0d: tst_di();   m6809_ICount[0]-= 6; break;
    /*TODO*///			case 0x0e: jmp_di();   m6809_ICount[0]-= 3; break;
    			case 0x0f: clr_di();   m6809_ICount[0]-= 6; break;
    			case 0x10: pref10();					 break;
    			case 0x11: pref11();					 break;
    			case 0x12: nop();	   m6809_ICount[0]-= 2; break;
    			case 0x13: sync();	   m6809_ICount[0]-= 4; break;
    			case 0x14: illegal();  m6809_ICount[0]-= 2; break;
    			case 0x15: illegal();  m6809_ICount[0]-= 2; break;
    			case 0x16: lbra();	   m6809_ICount[0]-= 5; break;
    			case 0x17: lbsr();	   m6809_ICount[0]-= 9; break;
    			case 0x18: illegal();  m6809_ICount[0]-= 2; break;
    			case 0x19: daa();	   m6809_ICount[0]-= 2; break;
    			case 0x1a: orcc();	   m6809_ICount[0]-= 3; break;
    			case 0x1b: illegal();  m6809_ICount[0]-= 2; break;
    			case 0x1c: andcc();    m6809_ICount[0]-= 3; break;
    			case 0x1d: sex();	   m6809_ICount[0]-= 2; break;
    			case 0x1e: exg();	   m6809_ICount[0]-= 8; break;
    			case 0x1f: tfr();	   m6809_ICount[0]-= 6; break;
    			case 0x20: bra();	   m6809_ICount[0]-= 3; break;
    			case 0x21: brn();	   m6809_ICount[0]-= 3; break;
    			case 0x22: bhi();	   m6809_ICount[0]-= 3; break;
    			case 0x23: bls();	   m6809_ICount[0]-= 3; break;
    			case 0x24: bcc();	   m6809_ICount[0]-= 3; break;
    			case 0x25: bcs();	   m6809_ICount[0]-= 3; break;
    			case 0x26: bne();	   m6809_ICount[0]-= 3; break;
    			case 0x27: beq();	   m6809_ICount[0]-= 3; break;
    			case 0x28: bvc();	   m6809_ICount[0]-= 3; break;
    /*TODO*///			case 0x29: bvs();	   m6809_ICount[0]-= 3; break;
    			case 0x2a: bpl();	   m6809_ICount[0]-= 3; break;
    			case 0x2b: bmi();	   m6809_ICount[0]-= 3; break;
    			case 0x2c: bge();	   m6809_ICount[0]-= 3; break;
    			case 0x2d: blt();	   m6809_ICount[0]-= 3; break;
    			case 0x2e: bgt();	   m6809_ICount[0]-= 3; break;
    			case 0x2f: ble();	   m6809_ICount[0]-= 3; break;
    			case 0x30: leax();	   m6809_ICount[0]-= 4; break;
    			case 0x31: leay();	   m6809_ICount[0]-= 4; break;
    			case 0x32: leas();	   m6809_ICount[0]-= 4; break;
    			case 0x33: leau();	   m6809_ICount[0]-= 4; break;
    			case 0x34: pshs();	   m6809_ICount[0]-= 5; break;
    			case 0x35: puls();	   m6809_ICount[0]-= 5; break;
    			case 0x36: pshu();	   m6809_ICount[0]-= 5; break;
    			case 0x37: pulu();	   m6809_ICount[0]-= 5; break;
    			case 0x38: illegal();  m6809_ICount[0]-= 2; break;
    			case 0x39: rts();	   m6809_ICount[0]-= 5; break;
    			case 0x3a: abx();	   m6809_ICount[0]-= 3; break;
    			case 0x3b: rti();	   m6809_ICount[0]-= 6; break;
    			case 0x3c: cwai();	   m6809_ICount[0]-=20; break;
    			case 0x3d: mul();	   m6809_ICount[0]-=11; break;
    			case 0x3e: illegal();  m6809_ICount[0]-= 2; break;
    /*TODO*///			case 0x3f: swi();	   m6809_ICount[0]-=19; break;
    			case 0x40: nega();	   m6809_ICount[0]-= 2; break;
    			case 0x41: illegal();  m6809_ICount[0]-= 2; break;
    			case 0x42: illegal();  m6809_ICount[0]-= 2; break;
    			case 0x43: coma();	   m6809_ICount[0]-= 2; break;
    			case 0x44: lsra();	   m6809_ICount[0]-= 2; break;
    			case 0x45: illegal();  m6809_ICount[0]-= 2; break;
    			case 0x46: rora();	   m6809_ICount[0]-= 2; break;
    			case 0x47: asra();	   m6809_ICount[0]-= 2; break;
    			case 0x48: asla();	   m6809_ICount[0]-= 2; break;
                        case 0x49: rola();	   m6809_ICount[0]-= 2; break;
    			case 0x4a: deca();	   m6809_ICount[0]-= 2; break;
    			case 0x4b: illegal();  m6809_ICount[0]-= 2; break;
    			case 0x4c: inca();	   m6809_ICount[0]-= 2; break;
    			case 0x4d: tsta();	   m6809_ICount[0]-= 2; break;
    			case 0x4e: illegal();  m6809_ICount[0]-= 2; break;
    			case 0x4f: clra();	   m6809_ICount[0]-= 2; break;
    			case 0x50: negb();	   m6809_ICount[0]-= 2; break;
    			case 0x51: illegal();  m6809_ICount[0]-= 2; break;
    			case 0x52: illegal();  m6809_ICount[0]-= 2; break;
    			case 0x53: comb();	   m6809_ICount[0]-= 2; break;
    			case 0x54: lsrb();	   m6809_ICount[0]-= 2; break;
    			case 0x55: illegal();  m6809_ICount[0]-= 2; break;
    			case 0x56: rorb();	   m6809_ICount[0]-= 2; break;
    			case 0x57: asrb();	   m6809_ICount[0]-= 2; break;
    			case 0x58: aslb();	   m6809_ICount[0]-= 2; break;
    			case 0x59: rolb();	   m6809_ICount[0]-= 2; break;
    			case 0x5a: decb();	   m6809_ICount[0]-= 2; break;
    			case 0x5b: illegal();  m6809_ICount[0]-= 2; break;
    			case 0x5c: incb();	   m6809_ICount[0]-= 2; break;
    			case 0x5d: tstb();	   m6809_ICount[0]-= 2; break;
    			case 0x5e: illegal();  m6809_ICount[0]-= 2; break;
    			case 0x5f: clrb();	   m6809_ICount[0]-= 2; break;
    			case 0x60: neg_ix();   m6809_ICount[0]-= 6; break;
    			case 0x61: illegal();  m6809_ICount[0]-= 2; break;
    			case 0x62: illegal();  m6809_ICount[0]-= 2; break;
    			case 0x63: com_ix();   m6809_ICount[0]-= 6; break;
    			case 0x64: lsr_ix();   m6809_ICount[0]-= 6; break;
    			case 0x65: illegal();  m6809_ICount[0]-= 2; break;
    			case 0x66: ror_ix();   m6809_ICount[0]-= 6; break;
    			case 0x67: asr_ix();   m6809_ICount[0]-= 6; break;
    			case 0x68: asl_ix();   m6809_ICount[0]-= 6; break;
    			case 0x69: rol_ix();   m6809_ICount[0]-= 6; break;
    			case 0x6a: dec_ix();   m6809_ICount[0]-= 6; break;
    			case 0x6b: illegal();  m6809_ICount[0]-= 2; break;
    			case 0x6c: inc_ix();   m6809_ICount[0]-= 6; break;
    			case 0x6d: tst_ix();   m6809_ICount[0]-= 6; break;
    			case 0x6e: jmp_ix();   m6809_ICount[0]-= 3; break;
    			case 0x6f: clr_ix();   m6809_ICount[0]-= 6; break;
    			case 0x70: neg_ex();   m6809_ICount[0]-= 7; break;
    			case 0x71: illegal();  m6809_ICount[0]-= 2; break;
    			case 0x72: illegal();  m6809_ICount[0]-= 2; break;
    			case 0x73: com_ex();   m6809_ICount[0]-= 7; break;
    			case 0x74: lsr_ex();   m6809_ICount[0]-= 7; break;
    			case 0x75: illegal();  m6809_ICount[0]-= 2; break;
    			case 0x76: ror_ex();   m6809_ICount[0]-= 7; break;
    			case 0x77: asr_ex();   m6809_ICount[0]-= 7; break;
    			case 0x78: asl_ex();   m6809_ICount[0]-= 7; break;
    			case 0x79: rol_ex();   m6809_ICount[0]-= 7; break;
    			case 0x7a: dec_ex();   m6809_ICount[0]-= 7; break;
    			case 0x7b: illegal();  m6809_ICount[0]-= 2; break;
    			case 0x7c: inc_ex();   m6809_ICount[0]-= 7; break;
    			case 0x7d: tst_ex();   m6809_ICount[0]-= 7; break;
    			case 0x7e: jmp_ex();   m6809_ICount[0]-= 4; break;
    			case 0x7f: clr_ex();   m6809_ICount[0]-= 7; break;
    			case 0x80: suba_im();  m6809_ICount[0]-= 2; break;
    			case 0x81: cmpa_im();  m6809_ICount[0]-= 2; break;
    			case 0x82: sbca_im();  m6809_ICount[0]-= 2; break;
    			case 0x83: subd_im();  m6809_ICount[0]-= 4; break;
    			case 0x84: anda_im();  m6809_ICount[0]-= 2; break;
    			case 0x85: bita_im();  m6809_ICount[0]-= 2; break;
    			case 0x86: lda_im();   m6809_ICount[0]-= 2; break;
    /*TODO*///			case 0x87: sta_im();   m6809_ICount[0]-= 2; break;
    			case 0x88: eora_im();  m6809_ICount[0]-= 2; break;
    			case 0x89: adca_im();  m6809_ICount[0]-= 2; break;
    			case 0x8a: ora_im();   m6809_ICount[0]-= 2; break;
    			case 0x8b: adda_im();  m6809_ICount[0]-= 2; break;
    			case 0x8c: cmpx_im();  m6809_ICount[0]-= 4; break;
    			case 0x8d: bsr();	   m6809_ICount[0]-= 7; break;
    			case 0x8e: ldx_im();   m6809_ICount[0]-= 3; break;
    /*TODO*///			case 0x8f: stx_im();   m6809_ICount[0]-= 2; break;
    			case 0x90: suba_di();  m6809_ICount[0]-= 4; break;
    			case 0x91: cmpa_di();  m6809_ICount[0]-= 4; break;
    			case 0x92: sbca_di();  m6809_ICount[0]-= 4; break;
    			case 0x93: subd_di();  m6809_ICount[0]-= 6; break;
    			case 0x94: anda_di();  m6809_ICount[0]-= 4; break;
    			case 0x95: bita_di();  m6809_ICount[0]-= 4; break;
   			case 0x96: lda_di();   m6809_ICount[0]-= 4; break;
    			case 0x97: sta_di();   m6809_ICount[0]-= 4; break;
    			case 0x98: eora_di();  m6809_ICount[0]-= 4; break;
    			case 0x99: adca_di();  m6809_ICount[0]-= 4; break;
    			case 0x9a: ora_di();   m6809_ICount[0]-= 4; break;
    			case 0x9b: adda_di();  m6809_ICount[0]-= 4; break;
    			case 0x9c: cmpx_di();  m6809_ICount[0]-= 6; break;
    /*TODO*///			case 0x9d: jsr_di();   m6809_ICount[0]-= 7; break;
    			case 0x9e: ldx_di();   m6809_ICount[0]-= 5; break;
    			case 0x9f: stx_di();   m6809_ICount[0]-= 5; break;
    			case 0xa0: suba_ix();  m6809_ICount[0]-= 4; break;
    			case 0xa1: cmpa_ix();  m6809_ICount[0]-= 4; break;
    			case 0xa2: sbca_ix();  m6809_ICount[0]-= 4; break;
    			case 0xa3: subd_ix();  m6809_ICount[0]-= 6; break;
    			case 0xa4: anda_ix();  m6809_ICount[0]-= 4; break;
    			case 0xa5: bita_ix();  m6809_ICount[0]-= 4; break;
    			case 0xa6: lda_ix();   m6809_ICount[0]-= 4; break;
    			case 0xa7: sta_ix();   m6809_ICount[0]-= 4; break;
    			case 0xa8: eora_ix();  m6809_ICount[0]-= 4; break;
    			case 0xa9: adca_ix();  m6809_ICount[0]-= 4; break;
    			case 0xaa: ora_ix();   m6809_ICount[0]-= 4; break;
    			case 0xab: adda_ix();  m6809_ICount[0]-= 4; break;
    			case 0xac: cmpx_ix();  m6809_ICount[0]-= 6; break;
    			case 0xad: jsr_ix();   m6809_ICount[0]-= 7; break;
    			case 0xae: ldx_ix();   m6809_ICount[0]-= 5; break;
    			case 0xaf: stx_ix();   m6809_ICount[0]-= 5; break;
    			case 0xb0: suba_ex();  m6809_ICount[0]-= 5; break;
    			case 0xb1: cmpa_ex();  m6809_ICount[0]-= 5; break;
    			case 0xb2: sbca_ex();  m6809_ICount[0]-= 5; break;
    			case 0xb3: subd_ex();  m6809_ICount[0]-= 7; break;
    			case 0xb4: anda_ex();  m6809_ICount[0]-= 5; break;
    			case 0xb5: bita_ex();  m6809_ICount[0]-= 5; break;
    			case 0xb6: lda_ex();   m6809_ICount[0]-= 5; break;
    			case 0xb7: sta_ex();   m6809_ICount[0]-= 5; break;
    			case 0xb8: eora_ex();  m6809_ICount[0]-= 5; break;
    			case 0xb9: adca_ex();  m6809_ICount[0]-= 5; break;
    			case 0xba: ora_ex();   m6809_ICount[0]-= 5; break;
    			case 0xbb: adda_ex();  m6809_ICount[0]-= 5; break;
    			case 0xbc: cmpx_ex();  m6809_ICount[0]-= 7; break;
    			case 0xbd: jsr_ex();   m6809_ICount[0]-= 8; break;
    			case 0xbe: ldx_ex();   m6809_ICount[0]-= 6; break;
    			case 0xbf: stx_ex();   m6809_ICount[0]-= 6; break;
    			case 0xc0: subb_im();  m6809_ICount[0]-= 2; break;
    			case 0xc1: cmpb_im();  m6809_ICount[0]-= 2; break;
    /*TODO*///			case 0xc2: sbcb_im();  m6809_ICount[0]-= 2; break;
    			case 0xc3: addd_im();  m6809_ICount[0]-= 4; break;
    			case 0xc4: andb_im();  m6809_ICount[0]-= 2; break;
    			case 0xc5: bitb_im();  m6809_ICount[0]-= 2; break;
    			case 0xc6: ldb_im();   m6809_ICount[0]-= 2; break;
    /*TODO*///			case 0xc7: stb_im();   m6809_ICount[0]-= 2; break;
    			case 0xc8: eorb_im();  m6809_ICount[0]-= 2; break;
    			case 0xc9: adcb_im();  m6809_ICount[0]-= 2; break;
    			case 0xca: orb_im();   m6809_ICount[0]-= 2; break;
    			case 0xcb: addb_im();  m6809_ICount[0]-= 2; break;
    			case 0xcc: ldd_im();   m6809_ICount[0]-= 3; break;
    /*TODO*///			case 0xcd: std_im();   m6809_ICount[0]-= 2; break;
    			case 0xce: ldu_im();   m6809_ICount[0]-= 3; break;
    /*TODO*///			case 0xcf: stu_im();   m6809_ICount[0]-= 3; break;
    			case 0xd0: subb_di();  m6809_ICount[0]-= 4; break;
    			case 0xd1: cmpb_di();  m6809_ICount[0]-= 4; break;
    /*TODO*///			case 0xd2: sbcb_di();  m6809_ICount[0]-= 4; break;
    			case 0xd3: addd_di();  m6809_ICount[0]-= 6; break;
    			case 0xd4: andb_di();  m6809_ICount[0]-= 4; break;
    			case 0xd5: bitb_di();  m6809_ICount[0]-= 4; break;
    			case 0xd6: ldb_di();   m6809_ICount[0]-= 4; break;
    			case 0xd7: stb_di();   m6809_ICount[0]-= 4; break;
    			case 0xd8: eorb_di();  m6809_ICount[0]-= 4; break;
    /*TODO*///			case 0xd9: adcb_di();  m6809_ICount[0]-= 4; break;
    			case 0xda: orb_di();   m6809_ICount[0]-= 4; break;
    			case 0xdb: addb_di();  m6809_ICount[0]-= 4; break;
    			case 0xdc: ldd_di();   m6809_ICount[0]-= 5; break;
    			case 0xdd: std_di();   m6809_ICount[0]-= 5; break;
    			case 0xde: ldu_di();   m6809_ICount[0]-= 5; break;
    			case 0xdf: stu_di();   m6809_ICount[0]-= 5; break;
    			case 0xe0: subb_ix();  m6809_ICount[0]-= 4; break;
    			case 0xe1: cmpb_ix();  m6809_ICount[0]-= 4; break;
    			case 0xe2: sbcb_ix();  m6809_ICount[0]-= 4; break;
    			case 0xe3: addd_ix();  m6809_ICount[0]-= 6; break;
    			case 0xe4: andb_ix();  m6809_ICount[0]-= 4; break;
    			case 0xe5: bitb_ix();  m6809_ICount[0]-= 4; break;
    			case 0xe6: ldb_ix();   m6809_ICount[0]-= 4; break;
    			case 0xe7: stb_ix();   m6809_ICount[0]-= 4; break;
    			case 0xe8: eorb_ix();  m6809_ICount[0]-= 4; break;
   			case 0xe9: adcb_ix();  m6809_ICount[0]-= 4; break;
    			case 0xea: orb_ix();   m6809_ICount[0]-= 4; break;
    			case 0xeb: addb_ix();  m6809_ICount[0]-= 4; break;
    			case 0xec: ldd_ix();   m6809_ICount[0]-= 5; break;
    			case 0xed: std_ix();   m6809_ICount[0]-= 5; break;
    			case 0xee: ldu_ix();   m6809_ICount[0]-= 5; break;
    			case 0xef: stu_ix();   m6809_ICount[0]-= 5; break;
    			case 0xf0: subb_ex();  m6809_ICount[0]-= 5; break;
    			case 0xf1: cmpb_ex();  m6809_ICount[0]-= 5; break;
    			case 0xf2: sbcb_ex();  m6809_ICount[0]-= 5; break;
    			case 0xf3: addd_ex();  m6809_ICount[0]-= 7; break;
    			case 0xf4: andb_ex();  m6809_ICount[0]-= 5; break;
    			case 0xf5: bitb_ex();  m6809_ICount[0]-= 5; break;
    			case 0xf6: ldb_ex();   m6809_ICount[0]-= 5; break;
    			case 0xf7: stb_ex();   m6809_ICount[0]-= 5; break;
    			case 0xf8: eorb_ex();  m6809_ICount[0]-= 5; break;
    			case 0xf9: adcb_ex();  m6809_ICount[0]-= 5; break;
    			case 0xfa: orb_ex();   m6809_ICount[0]-= 5; break;
    			case 0xfb: addb_ex();  m6809_ICount[0]-= 5; break;
    			case 0xfc: ldd_ex();   m6809_ICount[0]-= 6; break;
    			case 0xfd: std_ex();   m6809_ICount[0]-= 6; break;
    			case 0xfe: ldu_ex();   m6809_ICount[0]-= 6; break;
    			case 0xff: stu_ex();   m6809_ICount[0]-= 6; break;
                         default:
                            System.out.println("6809 opcode 0x" + Integer.toHexString(m6809.ireg));
                            //for debug
                            if(m6809log!=null){
                            fclose(m6809log);
                            System.exit(0);};
    			}
    
    		} while( m6809_ICount[0] > 0 );
    
            m6809_ICount[0] -= m6809.extra_cycles;
            m6809.extra_cycles = 0;
        }
    
        return cycles - m6809_ICount[0];   /* NS 970908 */
    }