#include <inttypes.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#define getclk 0
#define keypoll 0
#define CPS 0x00
#define ADC 0x01
#define AAB 0x02
#define ADC_AB 0x04
#define LDA_IN 0x05
#define ADC_Z 0x06
#define PHP 0x08
#define CPB 0x09
#define PHB 0x0A
#define DEC_AB 0x0C
#define DEC_Z 0x0D
#define JMP_Z 0x0E
#define JMP 0x10
#define SBC 0x11
#define SAB 0x12
#define SBC_AB 0x14
#define STA_IN 0x15
#define SBC_Z 0x16
#define ENT 0x18
#define CPY 0x19
#define PLB 0x1A
#define INC_AB 0x1C
#define INC_Z 0x1D
#define JSR 0x1E
#define JSL 0x20
#define AND 0x21
#define ABA 0x22
#define AND_AB 0x24
#define CMP_IN 0x25
#define AND_Z 0x26
#define PLP 0x28
#define CPX 0x29
#define PHY 0x2A
#define CPB_AB 0x2C
#define CPB_Z 0x2D
#define BPO_Z 0x2E
#define BPO 0x30
#define ORA 0x31
#define OAB 0x32
#define ORA_AB 0x34
#define LDB_IN 0x35
#define ORA_Z 0x36
#define STT 0x38
#define LDA_ZY 0x39
#define PLY 0x3A
#define CPX_AB 0x3C
#define CPY_Z 0x3D
#define BNG_Z 0x3E
#define BNG 0x40
#define XOR 0x41
#define XAB 0x42
#define XOR_AB 0x44
#define STB_IN 0x45
#define XOR_Z 0x46
#define PHA 0x48
#define STA_ZY 0x49
#define PHX 0x4A
#define CPY_AB 0x4C
#define CPX_Z 0x4D
#define BCS_Z 0x4E
#define BCS 0x50
#define LSL 0x51
#define LLB 0x52
#define LSL_AB 0x54
#define CPB_IN 0x55
#define LSL_Z 0x56
#define CLC 0x58
#define LDB_ZY 0x59
#define PLX 0x5A
#define LDA_IY 0x5C
#define LDA_IX 0x5D
#define BCC_Z 0x5E
#define BCC 0x60
#define LSR 0x61
#define LRB 0x62
#define LSR_AB 0x64
#define LDY_IN 0x65
#define LSR_Z 0x66
#define PLA 0x68
#define STB_ZY 0x69
#define TAB 0x6A
#define STA_IY 0x6C
#define STA_IX 0x6D
#define BEQ_Z 0x6E
#define BEQ 0x70
#define ROL 0x71
#define RLB 0x72
#define ROL_AB 0x74
#define STY_IN 0x75
#define ROL_Z 0x76
#define SEC 0x78
#define LDA_ZX 0x79
#define TBA 0x7A
#define CMP_IY 0x7C
#define CMP_IX 0x7D
#define BNE_Z 0x7E
#define BNE 0x80
#define ROR 0x81
#define RRB 0x82
#define ROR_AB 0x84
#define CPY_IN 0x85
#define ROR_Z 0x86
#define DEY 0x88
#define STA_ZX 0x89
#define TAY 0x8A
#define LDB_IY 0x8C
#define LDB_IX 0x8D
#define BVS_Z 0x8E
#define BVS 0x90
#define MUL 0x91
#define MAB 0x92
#define MUL_AB 0x94
#define LDX_IN 0x95
#define MUL_Z 0x96
#define CLI 0x98
#define LDB_ZX 0x99
#define TYA 0x9A
#define STB_IY 0x9C
#define STB_IX 0x9D
#define BVC_Z 0x9E
#define BVC 0xA0
#define DIV 0xA1
#define DAB 0xA2
#define DIV_AB 0xA4
#define STX_IN 0xA5
#define DIV_Z 0xA6
#define INY 0xA8
#define STB_ZX 0xA9
#define TAX 0xAA
#define CPB_IY 0xAC
#define CPB_IX 0xAD
#define RTS 0xAE
#define RTL 0xB0
#define CMP 0xB1
#define CAB 0xB2
#define CMP_AB 0xB4
#define CPX_IN 0xB5
#define CMP_Z 0xB6
#define SEI 0xB8
#define LDX 0xB9
#define TXA 0xBA
#define LDX_AB 0xBC
#define LDX_Z 0xBD
#define JSR_IN 0xBE
#define RTI 0xC0
#define LDA 0xC1
#define LDA_AB 0xC4
#define DEX 0xC5
#define LDA_Z 0xC6
#define CLV 0xC8
#define LDX_ZY 0xC9
#define TYX 0xCA
#define STA 0xCC
#define STA_Z 0xCD
#define JMP_IN 0xCE
#define TSX 0xD0
#define LDB 0xD1
#define LDB_AB 0xD4
#define INX 0xD5
#define LDB_Z 0xD6
#define WAI 0xD8
#define STX_ZY 0xD9
#define TXY 0xDA
#define STB 0xDC
#define STB_Z 0xDD
#define TXS 0xE0
#define LDY 0xE1
#define LDY_AB 0xE4
#define DEC 0xE5
#define LDY_Z 0xE6
#define BRK 0xE8
#define LDY_ZX 0xE9
#define NOP 0xEA
#define STY 0xEC
#define STY_Z 0xED
#define DEB 0xEE
#define ASR 0xF1
#define ARB 0xF2
#define ASR_AB 0xF4
#define INC 0xF5
#define ASR_Z 0xF6
#define STY_ZX 0xF9
#define STX 0xFC
#define STX_Z 0xFD
#define INB 0xFE
#define OPNUM 90
#define C ((uint64_t)1 << 0)
#define Z ((uint64_t)1 << 1)
#define I ((uint64_t)1 << 2)
#define V ((uint64_t)1 << 6)
#define N ((uint64_t)1 << 7)
uint8_t *addr;
union reg {
uint8_t u8[8];
uint16_t u16[4];
uint32_t u32[2];
uint64_t u64;
};
struct sux {
union reg ps;
uint64_t a[8], b[8], y[8], x[8];
uint64_t pc[8];
uint16_t sp[8];
uint16_t stk_st[8];
uint8_t crt;
};
enum {IMM, ZM, ZMX, ZMY, IND, INDX, INDY, ABS, IMPL};
static const uint8_t optype[0x100] = {
[0x00] = IMPL,
[0x01] = IMM,
[0x02] = IMPL,
[0x04] = ABS,
[0x05] = IND,
[0x06] = ZM,
[0x08] = IMM,
[0x09] = IMM,
[0x0A] = IMM,
[0x0C] = ABS,
[0x0D] = ZM,
[0x0E] = ZM,
[0x10] = ABS,
[0x11] = IMM,
[0x12] = IMPL,
[0x14] = ABS,
[0x15] = IND,
[0x16] = ZM,
[0x18] = IMM,
[0x19] = IMM,
[0x1A] = IMM,
[0x1C] = ABS,
[0x1D] = ZM,
[0x1E] = ZM,
[0x20] = ABS,
[0x21] = IMM,
[0x22] = IMPL,
[0x24] = ABS,
[0x25] = IND,
[0x26] = ZM,
[0x28] = IMM,
[0x29] = IMM,
[0x2A] = IMM,
[0x2C] = ABS,
[0x2D] = ZM,
[0x2E] = ZM,
[0x30] = ABS,
[0x31] = IMM,
[0x32] = IMPL,
[0x34] = ABS,
[0x35] = IND,
[0x36] = ZM,
[0x38] = IMM,
[0x39] = ZMY,
[0x3A] = IMM,
[0x3C] = ABS,
[0x3D] = ZM,
[0x3E] = ZM,
[0x40] = ABS,
[0x41] = IMM,
[0x42] = IMPL,
[0x44] = ABS,
[0x45] = IND,
[0x46] = ZM,
[0x48] = IMM,
[0x49] = ZMY,
[0x4A] = IMM,
[0x4C] = ABS,
[0x4D] = ZM,
[0x4E] = ZM,
[0x50] = ABS,
[0x51] = IMM,
[0x52] = IMPL,
[0x54] = ABS,
[0x55] = IND,
[0x56] = ZM,
[0x58] = IMPL,
[0x59] = ZMY,
[0x5A] = IMM,
[0x5C] = INDY,
[0x5D] = INDX,
[0x5E] = ZM,
[0x60] = ABS,
[0x61] = IMM,
[0x62] = IMPL,
[0x64] = ABS,
[0x65] = IND,
[0x66] = ZM,
[0x68] = IMM,
[0x69] = ZMY,
[0x6A] = IMPL,
[0x6C] = INDY,
[0x6D] = INDX,
[0x6E] = ZM,
[0x70] = ABS,
[0x71] = IMM,
[0x72] = IMPL,
[0x74] = ABS,
[0x75] = IND,
[0x76] = ZM,
[0x78] = IMPL,
[0x79] = ZMX,
[0x7A] = IMPL,
[0x7C] = INDY,
[0x7D] = INDX,
[0x7E] = ZM,
[0x80] = ABS,
[0x81] = IMM,
[0x82] = IMPL,
[0x84] = ABS,
[0x85] = IND,
[0x86] = ZM,
[0x88] = IMPL,
[0x89] = ZMX,
[0x8A] = IMPL,
[0x8C] = INDY,
[0x8D] = INDX,
[0x8E] = ZM,
[0x90] = ABS,
[0x91] = IMM,
[0x92] = IMPL,
[0x94] = ABS,
[0x95] = IND,
[0x96] = ZM,
[0x98] = IMPL,
[0x99] = ZMX,
[0x9A] = IMPL,
[0x9C] = INDY,
[0x9D] = INDX,
[0x9E] = ZM,
[0xA0] = ABS,
[0xA1] = IMM,
[0xA2] = IMPL,
[0xA4] = ABS,
[0xA5] = IND,
[0xA6] = ZM,
[0xA8] = IMPL,
[0xA9] = ZMX,
[0xAA] = IMPL,
[0xAC] = INDY,
[0xAD] = INDX,
[0xAE] = IMPL,
[0xB0] = IMPL,
[0xB1] = IMM,
[0xB2] = IMPL,
[0xB4] = ABS,
[0xB5] = IND,
[0xB6] = ZM,
[0xB8] = IMPL,
[0xB9] = IMM,
[0xBA] = IMPL,
[0xBC] = ABS,
[0xBD] = ZM,
[0xBE] = IND,
[0xC0] = IMPL,
[0xC1] = IMM,
[0xC4] = ABS,
[0xC5] = IMPL,
[0xC6] = ZM,
[0xC8] = IMPL,
[0xC9] = ZMY,
[0xCA] = IMPL,
[0xCC] = ABS,
[0xCD] = ZM,
[0xCE] = IND,
[0xD0] = IMPL,
[0xD1] = IMM,
[0xD4] = ABS,
[0xD5] = IMPL,
[0xD6] = ZM,
[0xD8] = IMPL,
[0xD9] = ZMY,
[0xDA] = IMPL,
[0xDC] = ABS,
[0xDD] = ZM,
[0xE0] = IMM,
[0xE1] = IMM,
[0xE4] = ABS,
[0xE5] = IMPL,
[0xE6] = ZM,
[0xE8] = IMPL,
[0xE9] = ZMX,
[0xEA] = IMPL,
[0xEC] = ABS,
[0xED] = ZM,
[0xEE] = IMPL,
[0xF1] = IMM,
[0xF2] = IMPL,
[0xF4] = ABS,
[0xF5] = IMPL,
[0xF6] = ZM,
[0xF9] = ZMX,
[0xFC] = ABS,
[0xFD] = ZM,
[0xFE] = IMPL
};
extern int asmmon();