| 1 | // |
| 2 | // ACME - a crossassembler for producing 6502/65c02/65816 code. |
| 3 | // Copyright (C) 1998-2006 Marco Baye |
| 4 | // Have a look at "acme.c" for further info |
| 5 | // |
| 6 | // Mnemonics stuff |
| 7 | |
| 8 | #include "config.h" |
| 9 | #include "alu.h" |
| 10 | #include "cpu.h" |
| 11 | #include "dynabuf.h" |
| 12 | #include "global.h" |
| 13 | #include "input.h" |
| 14 | #include "output.h" |
| 15 | #include "tree.h" |
| 16 | |
| 17 | |
| 18 | // Constants |
| 19 | #define s_ror (s_error+2) // Yes, I know I'm sick |
| 20 | #define MNEMO_DYNABUF_INITIALSIZE 8 // 4+terminator should suffice |
| 21 | |
| 22 | // These values are needed to recognize addressing modes. |
| 23 | // Bits: |
| 24 | // 7....... "Implied" no value given |
| 25 | // .6...... "Immediate" "#" at start |
| 26 | // ..5..... "IndirectLong" "[" at start and "]" after value |
| 27 | // ...4.... "Indirect" Value has at least one unnecessary pair of "()" |
| 28 | // ....32.. "Indexed-Int" Index given inside of "()" |
| 29 | // ......10 "Indexed-Ext" Index given outside of (or without any) "()" |
| 30 | // |
| 31 | // Index bits: |
| 32 | // 00 = no index |
| 33 | // 01 = ",s" (Stack-indexed) |
| 34 | // 10 = ",x" (X-indexed) |
| 35 | // 11 = ",y" (Y-indexed) |
| 36 | |
| 37 | // Components (Values for indices) |
| 38 | #define HAM__ (0u << 0) // No index |
| 39 | #define HAM_S (1u << 0) // Stack-indexed |
| 40 | #define HAM_X (2u << 0) // X-indexed |
| 41 | #define HAM_Y (3u << 0) // Y-indexed |
| 42 | |
| 43 | // End values base value internal index external index |
| 44 | #define HAM_IMP (1u << 7) |
| 45 | #define HAM_IMM (1u << 6) |
| 46 | #define HAM_ABS 0 |
| 47 | #define HAM_ABSS (1u << 0) |
| 48 | #define HAM_ABSX (2u << 0) |
| 49 | #define HAM_ABSY (3u << 0) |
| 50 | #define HAM_IND (1u << 4) |
| 51 | #define HAM_XIND ((1u << 4)| (2u << 2)) |
| 52 | #define HAM_INDY ((1u << 4)| (3u << 0)) |
| 53 | #define HAM_SINDY ((1u << 4)| (1u << 2)| (3u << 0)) |
| 54 | #define HAM_LIND (1u << 5) |
| 55 | #define HAM_LINDY ((1u << 5)| (3u << 0)) |
| 56 | // Values of internal indices equal values of external indices, shifted left |
| 57 | // by two bits. The program relies on this ! |
| 58 | |
| 59 | // Constant values, used to mark the possible parameter lengths of commands. |
| 60 | // Not all of the eight values are actually used, however (because of the |
| 61 | // supported CPUs). |
| 62 | #define MAYBE______ (0) |
| 63 | #define MAYBE_1____ (MVALUE_FORCE08) |
| 64 | #define MAYBE___2__ (MVALUE_FORCE16) |
| 65 | #define MAYBE_1_2__ (MVALUE_FORCE08 | MVALUE_FORCE16) |
| 66 | #define MAYBE_____3 (MVALUE_FORCE24) |
| 67 | #define MAYBE_1___3 (MVALUE_FORCE08 | MVALUE_FORCE24) |
| 68 | #define MAYBE___2_3 (MVALUE_FORCE16 | MVALUE_FORCE24) |
| 69 | #define MAYBE_1_2_3 (MVALUE_FORCE08 | MVALUE_FORCE16 | MVALUE_FORCE24) |
| 70 | |
| 71 | // The mnemonics are split up into groups, each group has its own function to |
| 72 | // be dealt with: |
| 73 | // G_IMPLIED Mnemonics using only implied addressing. Byte value = opcode |
| 74 | // G_MAIN The main accumulator stuff (plus PEI) Byte value = table index |
| 75 | // G_MISC Most of the other opcodes Byte value = table index |
| 76 | // G_REL_SHORT The short branch instructions. Byte value = opcode |
| 77 | // G_REL_LONG Mnemonics with 16bit relative addressing. Byte value = opcode |
| 78 | // G_JUMP The jump instructions Byte value = table index |
| 79 | // G_MOVE The "move" commands. Byte value = opcode |
| 80 | enum mnemogroup_t { |
| 81 | G_IMPLIED, // only implied addressing |
| 82 | G_MAIN, // main accumulator stuff (plus PEI) |
| 83 | G_MISC, // misc |
| 84 | G_REL_SHORT, // short relative |
| 85 | G_REL_LONG, // long relative |
| 86 | G_JUMP, // jump |
| 87 | G_MOVE // MVP, MVN |
| 88 | }; |
| 89 | |
| 90 | // save some space |
| 91 | #define SCB static const unsigned char |
| 92 | #define SCS static const unsigned short |
| 93 | #define SCL static const unsigned long |
| 94 | |
| 95 | // Code tables for group G_MAIN: |
| 96 | // These tables are used for the main accumulator-related mnemonics. By reading |
| 97 | // the mnemonic's byte value (from the mnemotable), the assembler finds out the |
| 98 | // column to use here. The row depends on the used addressing mode. A zero |
| 99 | // entry in these tables means that the combination of mnemonic and addressing |
| 100 | // mode is illegal. |
| 101 | enum { IDX_ORA, IDX_AND, IDX_EOR, IDX_ADC, IDX_STA, IDX_LDA, IDX_CMP, IDX_SBC, |
| 102 | IDXC_ORA, IDXC_AND, IDXC_EOR, IDXC_ADC, IDXC_STA, IDXC_LDA, IDXC_CMP, IDXC_SBC, |
| 103 | IDX8_ORA, IDX8_AND, IDX8_EOR, IDX8_ADC, IDX8_STA, IDX8_LDA, IDX8_CMP, IDX8_SBC, IDX8_PEI, |
| 104 | IDXI_SLO, IDXI_RLA, IDXI_SRE, IDXI_RRA, IDXI_SAX, IDXI_LAX, IDXI_DCP, IDXI_ISC}; |
| 105 | // | 6502 | 65c02 | 65816 | 6510 | |
| 106 | // | ora and eor adc sta lda cmp sbc| ora and eor adc sta lda cmp sbc| ora and eor adc sta lda cmp sbc pei| slo rla sre rra sax lax dcp isc| |
| 107 | SCB accu_imm[] = { 0x09, 0x29, 0x49, 0x69, 0, 0xa9, 0xc9, 0xe9, 0x09, 0x29, 0x49, 0x69, 0, 0xa9, 0xc9, 0xe9, 0x09, 0x29, 0x49, 0x69, 0, 0xa9, 0xc9, 0xe9, 0, 0, 0, 0, 0, 0, 0, 0, 0};// #$ff/#$ffff |
| 108 | SCB accu_sabs8[] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x03, 0x23, 0x43, 0x63, 0x83, 0xa3, 0xc3, 0xe3, 0, 0, 0, 0, 0, 0, 0, 0, 0};// $ff,s |
| 109 | SCB accu_xind8[] = { 0x01, 0x21, 0x41, 0x61, 0x81, 0xa1, 0xc1, 0xe1, 0x01, 0x21, 0x41, 0x61, 0x81, 0xa1, 0xc1, 0xe1, 0x01, 0x21, 0x41, 0x61, 0x81, 0xa1, 0xc1, 0xe1, 0, 0x03, 0x23, 0x43, 0x63, 0x83, 0xa3, 0xc3, 0xe3};// ($ff,x) |
| 110 | SCB accu_sindy8[] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x13, 0x33, 0x53, 0x73, 0x93, 0xb3, 0xd3, 0xf3, 0, 0, 0, 0, 0, 0, 0, 0, 0};// ($ff,s),y |
| 111 | SCB accu_ind8[] = { 0, 0, 0, 0, 0, 0, 0, 0, 0x12, 0x32, 0x52, 0x72, 0x92, 0xb2, 0xd2, 0xf2, 0x12, 0x32, 0x52, 0x72, 0x92, 0xb2, 0xd2, 0xf2,0xd4, 0, 0, 0, 0, 0, 0, 0, 0};// ($ff) |
| 112 | SCB accu_indy8[] = { 0x11, 0x31, 0x51, 0x71, 0x91, 0xb1, 0xd1, 0xf1, 0x11, 0x31, 0x51, 0x71, 0x91, 0xb1, 0xd1, 0xf1, 0x11, 0x31, 0x51, 0x71, 0x91, 0xb1, 0xd1, 0xf1, 0, 0x13, 0x33, 0x53, 0x73, 0, 0xb3, 0xd3, 0xf3};// ($ff),y |
| 113 | SCB accu_lind8[] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x07, 0x27, 0x47, 0x67, 0x87, 0xa7, 0xc7, 0xe7, 0, 0, 0, 0, 0, 0, 0, 0, 0};// [$ff] |
| 114 | SCB accu_lindy8[] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x17, 0x37, 0x57, 0x77, 0x97, 0xb7, 0xd7, 0xf7, 0, 0, 0, 0, 0, 0, 0, 0, 0};// [$ff],y |
| 115 | SCL accu_abs[] = {0x0d05,0x2d25,0x4d45,0x6d65,0x8d85,0xada5,0xcdc5,0xede5,0x0d05,0x2d25,0x4d45,0x6d65,0x8d85,0xada5,0xcdc5,0xede5,0x0f0d05,0x2f2d25,0x4f4d45,0x6f6d65,0x8f8d85,0xafada5,0xcfcdc5,0xefede5, 0,0x0f07,0x2f27,0x4f47,0x6f67,0x8f87,0xafa7,0xcfc7,0xefe7};// $ff /$ffff /$ffffff |
| 116 | SCL accu_xabs[] = {0x1d15,0x3d35,0x5d55,0x7d75,0x9d95,0xbdb5,0xddd5,0xfdf5,0x1d15,0x3d35,0x5d55,0x7d75,0x9d95,0xbdb5,0xddd5,0xfdf5,0x1f1d15,0x3f3d35,0x5f5d55,0x7f7d75,0x9f9d95,0xbfbdb5,0xdfddd5,0xfffdf5, 0,0x1f17,0x3f37,0x5f57,0x7f77, 0, 0,0xdfd7,0xfff7};// $ff,x/$ffff,x/$ffffff,x |
| 117 | SCS accu_yabs[] = {0x1900,0x3900,0x5900,0x7900,0x9900,0xb900,0xd900,0xf900,0x1900,0x3900,0x5900,0x7900,0x9900,0xb900,0xd900,0xf900, 0x1900, 0x3900, 0x5900, 0x7900, 0x9900, 0xb900, 0xd900, 0xf900, 0,0x1b00,0x3b00,0x5b00,0x7b00, 0x97,0xbfb7,0xdb00,0xfb00};// $ff,y/$ffff,y |
| 118 | |
| 119 | // Code tables for group G_MISC: |
| 120 | // These tables are needed for finding out the correct code in cases when |
| 121 | // there are no general rules. By reading the mnemonic's byte value (from the |
| 122 | // mnemotable), the assembler finds out the column to use here. The row |
| 123 | // depends on the used addressing mode. A zero entry in these tables means |
| 124 | // that the combination of mnemonic and addressing mode is illegal. |
| 125 | enum { IDX_BIT, IDX_ASL, IDX_ROL, IDX_LSR, IDX_ROR, IDX_STY, IDX_STX, |
| 126 | IDX_LDY, IDX_LDX, IDX_CPY, IDX_CPX, IDX_DEC, IDX_INC, |
| 127 | IDXC_TSB, IDXC_TRB, IDXC_BIT, IDXC_DEC, IDXC_INC, IDXC_STZ, |
| 128 | IDX8_COP, IDX8_REP, IDX8_SEP, IDX8_PEA, |
| 129 | IDXI_ANC, IDXI_ASR, IDXI_ARR, IDXI_SBX, IDXI_DOP, IDXI_TOP, IDXI_JAM}; |
| 130 | // | 6502 | 65c02 | 65816 | 6510 | |
| 131 | // | bit asl rol lsr ror sty stx ldy ldx cpy cpx dec inc| tsb trb bit dec inc stz| cop rep sep pea| anc asr arr sbx dop top jam| |
| 132 | SCB misc_impl[] = { 0, 0x0a, 0x2a, 0x4a, 0x6a, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x3a, 0x1a, 0, 0, 0, 0, 0, 0, 0, 0, 0,0x80, 0x0c,0x02};// implied/accu |
| 133 | SCB misc_imm[] = { 0, 0, 0, 0, 0, 0, 0, 0xa0, 0xa2, 0xc0, 0xe0, 0, 0, 0, 0, 0x89, 0, 0, 0, 0,0xc2,0xe2, 0,0x2b,0x4b,0x6b,0xcb,0x80, 0, 0};// #$ff |
| 134 | SCS misc_abs[] = {0x2c24,0x0e06,0x2e26,0x4e46,0x6e66,0x8c84,0x8e86,0xaca4,0xaea6,0xccc4,0xece4,0xcec6,0xeee6,0x0c04,0x1c14,0x2c24,0xcec6,0xeee6,0x9c64,0x02, 0, 0,0xf400, 0, 0, 0, 0,0x04,0x0c00, 0};// $ff/$ffff |
| 135 | SCS misc_xabs[] = { 0,0x1e16,0x3e36,0x5e56,0x7e76, 0x94, 0,0xbcb4, 0, 0, 0,0xded6,0xfef6, 0, 0,0x3c34,0xded6,0xfef6,0x9e74, 0, 0, 0, 0, 0, 0, 0, 0,0x14,0x1c00, 0};// $ff,x/$ffff,x |
| 136 | SCS misc_yabs[] = { 0, 0, 0, 0, 0, 0, 0x96, 0,0xbeb6, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};// $ff,y/$ffff,y |
| 137 | |
| 138 | // Code tables for group G_JUMP: |
| 139 | // |
| 140 | // These tables are needed for finding out the correct code when the mnemonic |
| 141 | // is "jmp" or "jsr" (or the long versions "jml" and "jsl"). |
| 142 | // By reading the mnemonic's byte value (from the mnemotable), the assembler |
| 143 | // finds out the column to use here. The row depends on the used addressing |
| 144 | // mode. A zero entry in these tables means that the combination of mnemonic |
| 145 | // and addressing mode is illegal. |
| 146 | enum { IDX_JMP, IDX_JSR, IDXC_JMP, IDX8_JMP, IDX8_JML, IDX8_JSR, IDX8_JSL}; |
| 147 | // | 6502 | 65c02| 65816 | |
| 148 | // | jmp jsr | jmp | jmp jml jsr jsl | |
| 149 | SCL jump_abs[] = {0x4c00,0x2000,0x4c00,0x5c4c00,0x5c0000,0x222000,0x220000};// $ffff/$ffffff |
| 150 | SCS jump_ind[] = {0x6c00, 0,0x6c00, 0x6c00, 0, 0, 0};// ($ffff) |
| 151 | SCS jump_xind[] = { 0, 0,0x7c00, 0x7c00, 0, 0xfc00, 0};// ($ffff,x) |
| 152 | SCS jump_lind[] = { 0, 0, 0, 0xdc00, 0xdc00, 0, 0};// [$ffff] |
| 153 | |
| 154 | #undef SCB |
| 155 | #undef SCS |
| 156 | #undef SCL |
| 157 | |
| 158 | // error message strings |
| 159 | static const char exception_illegal_combination[] = "Illegal combination of command and addressing mode."; |
| 160 | static const char exception_highbyte_zero[]= "Using oversized addressing mode."; |
| 161 | static const char exception_too_far[] = "Target out of range."; |
| 162 | |
| 163 | |
| 164 | // Variables |
| 165 | |
| 166 | static dynabuf_t* mnemo_dyna_buf; // dynamic buffer for mnemonics |
| 167 | // predefined stuff |
| 168 | static node_t* mnemo_6502_tree = NULL;// holds 6502 mnemonics |
| 169 | static node_t* mnemo_6510_tree = NULL;// holds 6510 extensions |
| 170 | static node_t* mnemo_65c02_tree= NULL;// holds 65c02 extensions |
| 171 | //static node_t* mnemo_Rockwell65c02_tree = NULL;// Rockwell |
| 172 | static node_t* mnemo_WDC65c02_tree = NULL;// WDC's "stp"/"wai" |
| 173 | static node_t* mnemo_65816_tree = NULL;// holds 65816 extensions |
| 174 | |
| 175 | // Command's code and group values are stored together in a single integer. |
| 176 | // To extract the code, use "& CODEMASK". |
| 177 | // To extract the group, use ">> GROUPSHIFT" |
| 178 | #define CODEMASK 0xff // opcode or table index |
| 179 | #define FLAGSMASK 0x300 // flags concerning immediate addressing: |
| 180 | #define IMM_ACCU 0x100 // ...depends on accumulator length |
| 181 | #define IMM_IDX 0x200 // ...depends on index register length |
| 182 | #define GROUPSHIFT 10 // shift right by this to extract group |
| 183 | #define MERGE(g, v) ((g << GROUPSHIFT) | v) |
| 184 | |
| 185 | static node_t mnemos_6502[] = { |
| 186 | PREDEFNODE("ora", MERGE(G_MAIN , IDX_ORA | IMM_ACCU)), |
| 187 | PREDEFNODE(s_and, MERGE(G_MAIN , IDX_AND | IMM_ACCU)), |
| 188 | PREDEFNODE(s_eor, MERGE(G_MAIN , IDX_EOR | IMM_ACCU)), |
| 189 | PREDEFNODE("adc", MERGE(G_MAIN , IDX_ADC | IMM_ACCU)), |
| 190 | PREDEFNODE("sta", MERGE(G_MAIN , IDX_STA)), |
| 191 | PREDEFNODE("lda", MERGE(G_MAIN , IDX_LDA | IMM_ACCU)), |
| 192 | PREDEFNODE("cmp", MERGE(G_MAIN , IDX_CMP | IMM_ACCU)), |
| 193 | PREDEFNODE("sbc", MERGE(G_MAIN , IDX_SBC | IMM_ACCU)), |
| 194 | PREDEFNODE("bit", MERGE(G_MISC , IDX_BIT | IMM_ACCU)), |
| 195 | PREDEFNODE(s_asl, MERGE(G_MISC , IDX_ASL)), |
| 196 | PREDEFNODE("rol", MERGE(G_MISC , IDX_ROL)), |
| 197 | PREDEFNODE(s_lsr, MERGE(G_MISC , IDX_LSR)), |
| 198 | PREDEFNODE(s_ror, MERGE(G_MISC , IDX_ROR)), |
| 199 | PREDEFNODE("sty", MERGE(G_MISC , IDX_STY)), |
| 200 | PREDEFNODE("stx", MERGE(G_MISC , IDX_STX)), |
| 201 | PREDEFNODE("ldy", MERGE(G_MISC , IDX_LDY | IMM_IDX)), |
| 202 | PREDEFNODE("ldx", MERGE(G_MISC , IDX_LDX | IMM_IDX)), |
| 203 | PREDEFNODE("cpy", MERGE(G_MISC , IDX_CPY | IMM_IDX)), |
| 204 | PREDEFNODE("cpx", MERGE(G_MISC , IDX_CPX | IMM_IDX)), |
| 205 | PREDEFNODE("dec", MERGE(G_MISC , IDX_DEC)), |
| 206 | PREDEFNODE("inc", MERGE(G_MISC , IDX_INC)), |
| 207 | PREDEFNODE("bpl", MERGE(G_REL_SHORT , 16)), |
| 208 | PREDEFNODE("bmi", MERGE(G_REL_SHORT , 48)), |
| 209 | PREDEFNODE("bvc", MERGE(G_REL_SHORT , 80)), |
| 210 | PREDEFNODE("bvs", MERGE(G_REL_SHORT , 112)), |
| 211 | PREDEFNODE("bcc", MERGE(G_REL_SHORT , 144)), |
| 212 | PREDEFNODE("bcs", MERGE(G_REL_SHORT , 176)), |
| 213 | PREDEFNODE("bne", MERGE(G_REL_SHORT , 208)), |
| 214 | PREDEFNODE("beq", MERGE(G_REL_SHORT , 240)), |
| 215 | PREDEFNODE("jmp", MERGE(G_JUMP , IDX_JMP)), |
| 216 | PREDEFNODE("jsr", MERGE(G_JUMP , IDX_JSR)), |
| 217 | PREDEFNODE("brk", MERGE(G_IMPLIED , 0)), |
| 218 | PREDEFNODE("php", MERGE(G_IMPLIED , 8)), |
| 219 | PREDEFNODE("clc", MERGE(G_IMPLIED , 24)), |
| 220 | PREDEFNODE("plp", MERGE(G_IMPLIED , 40)), |
| 221 | PREDEFNODE("sec", MERGE(G_IMPLIED , 56)), |
| 222 | PREDEFNODE("rti", MERGE(G_IMPLIED , 64)), |
| 223 | PREDEFNODE("pha", MERGE(G_IMPLIED , 72)), |
| 224 | PREDEFNODE("cli", MERGE(G_IMPLIED , 88)), |
| 225 | PREDEFNODE("rts", MERGE(G_IMPLIED , 96)), |
| 226 | PREDEFNODE("pla", MERGE(G_IMPLIED , 104)), |
| 227 | PREDEFNODE("sei", MERGE(G_IMPLIED , 120)), |
| 228 | PREDEFNODE("dey", MERGE(G_IMPLIED , 136)), |
| 229 | PREDEFNODE("txa", MERGE(G_IMPLIED , 138)), |
| 230 | PREDEFNODE("tya", MERGE(G_IMPLIED , 152)), |
| 231 | PREDEFNODE("txs", MERGE(G_IMPLIED , 154)), |
| 232 | PREDEFNODE("tay", MERGE(G_IMPLIED , 168)), |
| 233 | PREDEFNODE("tax", MERGE(G_IMPLIED , 170)), |
| 234 | PREDEFNODE("clv", MERGE(G_IMPLIED , 184)), |
| 235 | PREDEFNODE("tsx", MERGE(G_IMPLIED , 186)), |
| 236 | PREDEFNODE("iny", MERGE(G_IMPLIED , 200)), |
| 237 | PREDEFNODE("dex", MERGE(G_IMPLIED , 202)), |
| 238 | PREDEFNODE("cld", MERGE(G_IMPLIED , 216)), |
| 239 | PREDEFNODE("inx", MERGE(G_IMPLIED , 232)), |
| 240 | PREDEFNODE("nop", MERGE(G_IMPLIED , 234)), |
| 241 | PREDEFLAST("sed", MERGE(G_IMPLIED , 248)), |
| 242 | // ^^^^ this marks the last element |
| 243 | }; |
| 244 | |
| 245 | static node_t mnemos_6510[] = { |
| 246 | PREDEFNODE("slo", MERGE(G_MAIN, IDXI_SLO)),// ASL+ORA, ASO |
| 247 | PREDEFNODE("rla", MERGE(G_MAIN, IDXI_RLA)),// ROL+AND |
| 248 | PREDEFNODE("sre", MERGE(G_MAIN, IDXI_SRE)),// LSR+EOR, LSE |
| 249 | PREDEFNODE("rra", MERGE(G_MAIN, IDXI_RRA)),// ROR+ADC |
| 250 | PREDEFNODE("sax", MERGE(G_MAIN, IDXI_SAX)),// STX+STA, AAX, AXS |
| 251 | PREDEFNODE("lax", MERGE(G_MAIN, IDXI_LAX)),// LDX+LDA |
| 252 | PREDEFNODE("dcp", MERGE(G_MAIN, IDXI_DCP)),// DEC+CMP, DCM |
| 253 | PREDEFNODE("isc", MERGE(G_MAIN, IDXI_ISC)),// INC+SBC, ISB, INS |
| 254 | PREDEFNODE("anc", MERGE(G_MISC, IDXI_ANC)),// ROL+AND, ASL+ORA, AAC |
| 255 | PREDEFNODE(s_asr, MERGE(G_MISC, IDXI_ASR)),// LSR+EOR, ALR |
| 256 | PREDEFNODE("arr", MERGE(G_MISC, IDXI_ARR)),// ROR+ADC |
| 257 | PREDEFNODE("sbx", MERGE(G_MISC, IDXI_SBX)),// DEX+CMP, AXS, SAX |
| 258 | PREDEFNODE("dop", MERGE(G_MISC, IDXI_DOP)),// skip next byte |
| 259 | PREDEFNODE("top", MERGE(G_MISC, IDXI_TOP)),// skip next two bytes |
| 260 | PREDEFLAST("jam", MERGE(G_MISC, IDXI_JAM)),// jam/crash/halt/kill |
| 261 | // ^^^^ this marks the last element |
| 262 | }; |
| 263 | |
| 264 | static node_t mnemos_65c02[] = { |
| 265 | PREDEFNODE("ora", MERGE(G_MAIN , IDXC_ORA | IMM_ACCU)), |
| 266 | PREDEFNODE(s_and, MERGE(G_MAIN , IDXC_AND | IMM_ACCU)), |
| 267 | PREDEFNODE(s_eor, MERGE(G_MAIN , IDXC_EOR | IMM_ACCU)), |
| 268 | PREDEFNODE("adc", MERGE(G_MAIN , IDXC_ADC | IMM_ACCU)), |
| 269 | PREDEFNODE("sta", MERGE(G_MAIN , IDXC_STA)), |
| 270 | PREDEFNODE("lda", MERGE(G_MAIN , IDXC_LDA | IMM_ACCU)), |
| 271 | PREDEFNODE("cmp", MERGE(G_MAIN , IDXC_CMP | IMM_ACCU)), |
| 272 | PREDEFNODE("sbc", MERGE(G_MAIN , IDXC_SBC | IMM_ACCU)), |
| 273 | PREDEFNODE("jmp", MERGE(G_JUMP , IDXC_JMP)), |
| 274 | PREDEFNODE("bit", MERGE(G_MISC , IDXC_BIT | IMM_ACCU)), |
| 275 | PREDEFNODE("dec", MERGE(G_MISC , IDXC_DEC)), |
| 276 | PREDEFNODE("inc", MERGE(G_MISC , IDXC_INC)), |
| 277 | PREDEFNODE("bra", MERGE(G_REL_SHORT , 128)), |
| 278 | PREDEFNODE("phy", MERGE(G_IMPLIED , 90)), |
| 279 | PREDEFNODE("ply", MERGE(G_IMPLIED , 122)), |
| 280 | PREDEFNODE("phx", MERGE(G_IMPLIED , 218)), |
| 281 | PREDEFNODE("plx", MERGE(G_IMPLIED , 250)), |
| 282 | PREDEFNODE("tsb", MERGE(G_MISC , IDXC_TSB)), |
| 283 | PREDEFNODE("trb", MERGE(G_MISC , IDXC_TRB)), |
| 284 | PREDEFLAST("stz", MERGE(G_MISC , IDXC_STZ)), |
| 285 | // ^^^^ this marks the last element |
| 286 | }; |
| 287 | |
| 288 | //static node_t mnemos_Rockwell65c02[] = { |
| 289 | // PREDEFNODE("rmb0", MERGE(G_ , 0x07)), |
| 290 | // PREDEFNODE("rmb1", MERGE(G_ , 0x17)), |
| 291 | // PREDEFNODE("rmb2", MERGE(G_ , 0x27)), |
| 292 | // PREDEFNODE("rmb3", MERGE(G_ , 0x37)), |
| 293 | // PREDEFNODE("rmb4", MERGE(G_ , 0x47)), |
| 294 | // PREDEFNODE("rmb5", MERGE(G_ , 0x57)), |
| 295 | // PREDEFNODE("rmb6", MERGE(G_ , 0x67)), |
| 296 | // PREDEFNODE("rmb7", MERGE(G_ , 0x77)), |
| 297 | // PREDEFNODE("smb0", MERGE(G_ , 0x87)), |
| 298 | // PREDEFNODE("smb1", MERGE(G_ , 0x97)), |
| 299 | // PREDEFNODE("smb2", MERGE(G_ , 0xa7)), |
| 300 | // PREDEFNODE("smb3", MERGE(G_ , 0xb7)), |
| 301 | // PREDEFNODE("smb4", MERGE(G_ , 0xc7)), |
| 302 | // PREDEFNODE("smb5", MERGE(G_ , 0xd7)), |
| 303 | // PREDEFNODE("smb6", MERGE(G_ , 0xe7)), |
| 304 | // PREDEFNODE("smb7", MERGE(G_ , 0xf7)), |
| 305 | // PREDEFNODE("bbr0", MERGE(G_ , 0x0f)), |
| 306 | // PREDEFNODE("bbr1", MERGE(G_ , 0x1f)), |
| 307 | // PREDEFNODE("bbr2", MERGE(G_ , 0x2f)), |
| 308 | // PREDEFNODE("bbr3", MERGE(G_ , 0x3f)), |
| 309 | // PREDEFNODE("bbr4", MERGE(G_ , 0x4f)), |
| 310 | // PREDEFNODE("bbr5", MERGE(G_ , 0x5f)), |
| 311 | // PREDEFNODE("bbr6", MERGE(G_ , 0x6f)), |
| 312 | // PREDEFNODE("bbr7", MERGE(G_ , 0x7f)), |
| 313 | // PREDEFNODE("bbs0", MERGE(G_ , 0x8f)), |
| 314 | // PREDEFNODE("bbs1", MERGE(G_ , 0x9f)), |
| 315 | // PREDEFNODE("bbs2", MERGE(G_ , 0xaf)), |
| 316 | // PREDEFNODE("bbs3", MERGE(G_ , 0xbf)), |
| 317 | // PREDEFNODE("bbs4", MERGE(G_ , 0xcf)), |
| 318 | // PREDEFNODE("bbs5", MERGE(G_ , 0xdf)), |
| 319 | // PREDEFNODE("bbs6", MERGE(G_ , 0xef)), |
| 320 | // PREDEFLAST("bbs7", MERGE(G_ , 0xff)), |
| 321 | // // ^^^^ this marks the last element |
| 322 | //}; |
| 323 | |
| 324 | static node_t mnemos_WDC65c02[] = { |
| 325 | PREDEFNODE("stp", MERGE(G_IMPLIED , 219)), |
| 326 | PREDEFLAST("wai", MERGE(G_IMPLIED , 203)), |
| 327 | // ^^^^ this marks the last element |
| 328 | }; |
| 329 | |
| 330 | static node_t mnemos_65816[] = { |
| 331 | PREDEFNODE("ora", MERGE(G_MAIN , IDX8_ORA | IMM_ACCU)), |
| 332 | PREDEFNODE(s_and, MERGE(G_MAIN , IDX8_AND | IMM_ACCU)), |
| 333 | PREDEFNODE(s_eor, MERGE(G_MAIN , IDX8_EOR | IMM_ACCU)), |
| 334 | PREDEFNODE("adc", MERGE(G_MAIN , IDX8_ADC | IMM_ACCU)), |
| 335 | PREDEFNODE("sta", MERGE(G_MAIN , IDX8_STA)), |
| 336 | PREDEFNODE("lda", MERGE(G_MAIN , IDX8_LDA | IMM_ACCU)), |
| 337 | PREDEFNODE("cmp", MERGE(G_MAIN , IDX8_CMP | IMM_ACCU)), |
| 338 | PREDEFNODE("sbc", MERGE(G_MAIN , IDX8_SBC | IMM_ACCU)), |
| 339 | PREDEFNODE("pei", MERGE(G_MAIN , IDX8_PEI)), |
| 340 | PREDEFNODE("jmp", MERGE(G_JUMP , IDX8_JMP)), |
| 341 | PREDEFNODE("jsr", MERGE(G_JUMP , IDX8_JSR)), |
| 342 | PREDEFNODE("jml", MERGE(G_JUMP , IDX8_JML)), |
| 343 | PREDEFNODE("jsl", MERGE(G_JUMP , IDX8_JSL)), |
| 344 | PREDEFNODE("mvp", MERGE(G_MOVE , 0x44)), |
| 345 | PREDEFNODE("mvn", MERGE(G_MOVE , 0x54)), |
| 346 | PREDEFNODE("per", MERGE(G_REL_LONG , 98)), |
| 347 | PREDEFNODE(s_brl, MERGE(G_REL_LONG , 130)), |
| 348 | PREDEFNODE("cop", MERGE(G_MISC , IDX8_COP)), |
| 349 | PREDEFNODE("rep", MERGE(G_MISC , IDX8_REP)), |
| 350 | PREDEFNODE("sep", MERGE(G_MISC , IDX8_SEP)), |
| 351 | PREDEFNODE("pea", MERGE(G_MISC , IDX8_PEA)), |
| 352 | PREDEFNODE("phd", MERGE(G_IMPLIED , 11)), |
| 353 | PREDEFNODE("tcs", MERGE(G_IMPLIED , 27)), |
| 354 | PREDEFNODE("pld", MERGE(G_IMPLIED , 43)), |
| 355 | PREDEFNODE("tsc", MERGE(G_IMPLIED , 59)), |
| 356 | PREDEFNODE("wdm", MERGE(G_IMPLIED , 66)), |
| 357 | PREDEFNODE("phk", MERGE(G_IMPLIED , 75)), |
| 358 | PREDEFNODE("tcd", MERGE(G_IMPLIED , 91)), |
| 359 | PREDEFNODE("rtl", MERGE(G_IMPLIED , 107)), |
| 360 | PREDEFNODE("tdc", MERGE(G_IMPLIED , 123)), |
| 361 | PREDEFNODE("phb", MERGE(G_IMPLIED , 139)), |
| 362 | PREDEFNODE("txy", MERGE(G_IMPLIED , 155)), |
| 363 | PREDEFNODE("plb", MERGE(G_IMPLIED , 171)), |
| 364 | PREDEFNODE("tyx", MERGE(G_IMPLIED , 187)), |
| 365 | PREDEFNODE("xba", MERGE(G_IMPLIED , 235)), |
| 366 | PREDEFLAST("xce", MERGE(G_IMPLIED , 251)), |
| 367 | // ^^^^ this marks the last element |
| 368 | }; |
| 369 | |
| 370 | |
| 371 | // Functions |
| 372 | |
| 373 | // create dynamic buffer, build keyword trees |
| 374 | void Mnemo_init(void) { |
| 375 | mnemo_dyna_buf = DynaBuf_create(MNEMO_DYNABUF_INITIALSIZE); |
| 376 | Tree_add_table(&mnemo_6502_tree, mnemos_6502); |
| 377 | Tree_add_table(&mnemo_6510_tree, mnemos_6510); |
| 378 | Tree_add_table(&mnemo_65c02_tree, mnemos_65c02); |
| 379 | // Tree_add_table(&mnemo_Rockwell65c02_tree, mnemos_Rockwell65c02); |
| 380 | Tree_add_table(&mnemo_WDC65c02_tree, mnemos_WDC65c02); |
| 381 | Tree_add_table(&mnemo_65816_tree, mnemos_65816); |
| 382 | } |
| 383 | |
| 384 | |
| 385 | // Address mode parsing |
| 386 | |
| 387 | // Utility function for parsing indices. |
| 388 | static int get_index(bool next) { |
| 389 | int addressing_mode = HAM__; |
| 390 | |
| 391 | if(next) |
| 392 | GetByte(); |
| 393 | if(!Input_accept_comma()) |
| 394 | return(addressing_mode); |
| 395 | switch(GotByte) { |
| 396 | |
| 397 | case 's': |
| 398 | case 'S': |
| 399 | addressing_mode = HAM_S; |
| 400 | break; |
| 401 | |
| 402 | case 'x': |
| 403 | case 'X': |
| 404 | addressing_mode = HAM_X; |
| 405 | break; |
| 406 | |
| 407 | case 'y': |
| 408 | case 'Y': |
| 409 | addressing_mode = HAM_Y; |
| 410 | break; |
| 411 | |
| 412 | default: |
| 413 | Throw_error(exception_syntax); |
| 414 | |
| 415 | } |
| 416 | if(addressing_mode != HAM__) |
| 417 | NEXTANDSKIPSPACE(); |
| 418 | return(addressing_mode); |
| 419 | } |
| 420 | |
| 421 | // This function stores the command's argument in the given result_int_t |
| 422 | // structure (using the valueparser). The addressing mode is returned. |
| 423 | static int get_argument(result_int_t* result) { |
| 424 | int open_paren, |
| 425 | addressing_mode = HAM_ABS; |
| 426 | |
| 427 | SKIPSPACE(); |
| 428 | switch(GotByte) { |
| 429 | |
| 430 | case '[': |
| 431 | GetByte();// proceed with next char |
| 432 | ALU_int_result(result); |
| 433 | if(GotByte == ']') |
| 434 | addressing_mode |= HAM_LIND | get_index(TRUE); |
| 435 | else |
| 436 | Throw_error(exception_syntax); |
| 437 | break; |
| 438 | |
| 439 | case '#': |
| 440 | GetByte();// proceed with next char |
| 441 | addressing_mode |= HAM_IMM; |
| 442 | ALU_int_result(result); |
| 443 | break; |
| 444 | |
| 445 | default: |
| 446 | // liberal, to allow for "(...," |
| 447 | open_paren = ALU_liberal_int(result); |
| 448 | // check for implied addressing |
| 449 | if((result->flags & MVALUE_EXISTS) == 0) |
| 450 | addressing_mode |= HAM_IMP; |
| 451 | // check for indirect addressing |
| 452 | if(result->flags & MVALUE_INDIRECT) |
| 453 | addressing_mode |= HAM_IND; |
| 454 | // check for internal index (before closing parenthesis) |
| 455 | if(open_paren) { |
| 456 | // in case there are still open parentheses, |
| 457 | // read internal index |
| 458 | addressing_mode |= (get_index(FALSE) << 2); |
| 459 | if(GotByte == ')') |
| 460 | GetByte();// go on with next char |
| 461 | else |
| 462 | Throw_error(exception_syntax); |
| 463 | } |
| 464 | // check for external index (after closing parenthesis) |
| 465 | addressing_mode |= get_index(FALSE); |
| 466 | break; |
| 467 | |
| 468 | } |
| 469 | // ensure end of line |
| 470 | Input_ensure_EOS(); |
| 471 | //printf("AM: %x\n", addressing_mode); |
| 472 | return(addressing_mode); |
| 473 | } |
| 474 | |
| 475 | // Helper function for calc_arg_size() |
| 476 | // Only call with "size_bit = MVALUE_FORCE16" or "size_bit = MVALUE_FORCE24" |
| 477 | static int check_oversize(int size_bit, result_int_t* argument) { |
| 478 | // only check if value is *defined* |
| 479 | if((argument->flags & MVALUE_DEFINED) == 0) |
| 480 | return(size_bit);// pass on result |
| 481 | |
| 482 | // value is defined, so check |
| 483 | if(size_bit == MVALUE_FORCE16) { |
| 484 | // check 16-bit argument for high byte zero |
| 485 | if((argument->intval <= 255) && (argument->intval >= -128)) |
| 486 | Throw_warning(exception_highbyte_zero); |
| 487 | } else |
| 488 | // check 24-bit argument for bank byte zero |
| 489 | if((argument->intval <= 65535) && (argument->intval >= -32768)) |
| 490 | Throw_warning(exception_highbyte_zero); |
| 491 | return(size_bit);// pass on result |
| 492 | } |
| 493 | |
| 494 | // Utility function for comparing force bits, argument value, argument size, |
| 495 | // "unsure"-flag and possible addressing modes. Returns force bit matching |
| 496 | // number of parameter bytes to send. If it returns zero, an error occurred |
| 497 | // (and has already been delivered). |
| 498 | // force_bit none, 8b, 16b, 24b |
| 499 | // argument value and flags of parameter |
| 500 | // addressing_modes adressing modes (8b, 16b, 24b or any combination) |
| 501 | // Return value = force bit for number of parameter bytes to send (0 = error) |
| 502 | static int calc_arg_size(int force_bit, result_int_t* argument, int addressing_modes) { |
| 503 | // If there are no possible addressing modes, complain |
| 504 | if(addressing_modes == MAYBE______) { |
| 505 | Throw_error(exception_illegal_combination); |
| 506 | return(0); |
| 507 | } |
| 508 | // If command has force bit, act upon it |
| 509 | if(force_bit) { |
| 510 | // If command allows this force bit, return it |
| 511 | if(addressing_modes & force_bit) |
| 512 | return(force_bit); |
| 513 | // If not, complain |
| 514 | Throw_error("Illegal combination of command and postfix."); |
| 515 | return(0); |
| 516 | } |
| 517 | // Command has no force bit. Check whether value has one |
| 518 | // If value has force bit, act upon it |
| 519 | if(argument->flags & MVALUE_FORCEBITS) { |
| 520 | // Value has force bit set, so return this or bigger size |
| 521 | if(MVALUE_FORCE08 & addressing_modes & argument->flags) |
| 522 | return(MVALUE_FORCE08); |
| 523 | if(MVALUE_FORCE16 & addressing_modes & argument->flags) |
| 524 | return(MVALUE_FORCE16); |
| 525 | if(MVALUE_FORCE24 & addressing_modes) |
| 526 | return(MVALUE_FORCE24); |
| 527 | Throw_error(exception_number_out_of_range);// else error |
| 528 | return(0); |
| 529 | } |
| 530 | // Value has no force bit. Check whether there's only one addr mode |
| 531 | // If only one addressing mode, use that |
| 532 | if((addressing_modes == MVALUE_FORCE08) |
| 533 | || (addressing_modes == MVALUE_FORCE16) |
| 534 | || (addressing_modes == MVALUE_FORCE24)) |
| 535 | return(addressing_modes);// There's only one, so use it |
| 536 | // There's more than one addressing mode. Check whether value is sure |
| 537 | // If value is unsure, use default size |
| 538 | if(argument->flags & MVALUE_UNSURE) { |
| 539 | // If there is an 8-bit addressing mode *and* the value |
| 540 | // is sure to fit into 8 bits, use the 8-bit mode |
| 541 | if((addressing_modes & MVALUE_FORCE08) && (argument->flags & MVALUE_ISBYTE)) |
| 542 | return(MVALUE_FORCE08); |
| 543 | // If there is a 16-bit addressing, use that |
| 544 | // Call helper function for "oversized addr mode" warning |
| 545 | if(MVALUE_FORCE16 & addressing_modes) |
| 546 | return(check_oversize(MVALUE_FORCE16, argument)); |
| 547 | // If there is a 24-bit addressing, use that |
| 548 | // Call helper function for "oversized addr mode" warning |
| 549 | if(MVALUE_FORCE24 & addressing_modes) |
| 550 | return(check_oversize(MVALUE_FORCE24, argument)); |
| 551 | // otherwise, use 8-bit-addressing, which will raise an |
| 552 | // error later on if the value won't fit |
| 553 | return(MVALUE_FORCE08); |
| 554 | } |
| 555 | // Value is sure, so use its own size |
| 556 | // If value is negative, size cannot be chosen. Complain! |
| 557 | if(argument->intval < 0) { |
| 558 | Throw_error("Negative value - cannot choose addressing mode."); |
| 559 | return(0); |
| 560 | } |
| 561 | // Value is positive or zero. Check size ranges |
| 562 | // If there is an 8-bit addressing mode and value fits, use 8 bits |
| 563 | if((addressing_modes & MVALUE_FORCE08) && (argument->intval < 256)) |
| 564 | return(MVALUE_FORCE08); |
| 565 | // If there is a 16-bit addressing mode and value fits, use 16 bits |
| 566 | if((addressing_modes & MVALUE_FORCE16) && (argument->intval < 65536)) |
| 567 | return(MVALUE_FORCE16); |
| 568 | // If there is a 24-bit addressing mode, use that. In case the |
| 569 | // value doesn't fit, the output function will do the complaining. |
| 570 | if(addressing_modes & MVALUE_FORCE24) |
| 571 | return(MVALUE_FORCE24); |
| 572 | // Value is too big for all possible addressing modes |
| 573 | Throw_error(exception_number_out_of_range); |
| 574 | return(0); |
| 575 | } |
| 576 | |
| 577 | // Mnemonics using only implied addressing. |
| 578 | static void group_only_implied_addressing(int opcode) { |
| 579 | Output_byte(opcode); |
| 580 | Input_ensure_EOS(); |
| 581 | } |
| 582 | |
| 583 | // Mnemonics using only 8bit relative addressing (short branch instructions). |
| 584 | static void group_only_relative8_addressing(int opcode) { |
| 585 | result_int_t result; |
| 586 | |
| 587 | ALU_int_result(&result); |
| 588 | if(result.flags & MVALUE_DEFINED) { |
| 589 | result.intval -= (CPU_pc.intval + 2); |
| 590 | if((result.intval > 127) || (result.intval < -128)) |
| 591 | Throw_error(exception_too_far); |
| 592 | } |
| 593 | Output_byte(opcode); |
| 594 | // this fn has its own range check (see above). |
| 595 | // No reason to irritate the user with another error message, |
| 596 | // so use Output_byte() instead of Output_8b() |
| 597 | //Output_8b(result.value); |
| 598 | Output_byte(result.intval); |
| 599 | Input_ensure_EOS(); |
| 600 | } |
| 601 | |
| 602 | // Mnemonics using only 16bit relative addressing (BRL and PER). |
| 603 | static void group_only_relative16_addressing(int opcode) { |
| 604 | result_int_t result; |
| 605 | |
| 606 | ALU_int_result(&result); |
| 607 | if(result.flags & MVALUE_DEFINED) { |
| 608 | result.intval -= (CPU_pc.intval + 3); |
| 609 | if((result.intval > 32767) || (result.intval < -32768)) |
| 610 | Throw_error(exception_too_far); |
| 611 | } |
| 612 | Output_byte(opcode); |
| 613 | // this fn has its own range check (see above). |
| 614 | // No reason to irritate the user with another error message, |
| 615 | // so use Output_byte() instead of Output_16b() |
| 616 | //Output_16b(result.value); |
| 617 | Output_byte(result.intval); |
| 618 | Output_byte(result.intval >> 8); |
| 619 | Input_ensure_EOS(); |
| 620 | } |
| 621 | |
| 622 | // set addressing mode bits depending on which opcodes exist, then calculate |
| 623 | // argument size and output both opcode and argument |
| 624 | static void make_command(int force_bit, result_int_t* result, unsigned long opcodes) { |
| 625 | int addressing_modes = MAYBE______; |
| 626 | |
| 627 | if(opcodes & 0x0000ff) |
| 628 | addressing_modes |= MAYBE_1____; |
| 629 | if(opcodes & 0x00ff00) |
| 630 | addressing_modes |= MAYBE___2__; |
| 631 | if(opcodes & 0xff0000) |
| 632 | addressing_modes |= MAYBE_____3; |
| 633 | switch(calc_arg_size(force_bit, result, addressing_modes)) { |
| 634 | |
| 635 | case MVALUE_FORCE08: |
| 636 | Output_byte(opcodes & 255); |
| 637 | Output_8b(result->intval); |
| 638 | break; |
| 639 | |
| 640 | case MVALUE_FORCE16: |
| 641 | Output_byte((opcodes >> 8) & 255); |
| 642 | Output_16b(result->intval); |
| 643 | break; |
| 644 | |
| 645 | case MVALUE_FORCE24: |
| 646 | Output_byte((opcodes >> 16) & 255); |
| 647 | Output_24b(result->intval); |
| 648 | break; |
| 649 | |
| 650 | } |
| 651 | } |
| 652 | |
| 653 | // check whether 16bit immediate addressing is allowed. If not, return given |
| 654 | // opcode. If it is allowed, set force bits according to CPU register length |
| 655 | // and return given opcode for both 8- and 16-bit mode. |
| 656 | static unsigned int imm_ops(int *force_bit, unsigned char opcode, int imm_flag) { |
| 657 | // if the CPU does not allow 16bit immediate addressing (or if the |
| 658 | // opcode does not allow it), return immediately. |
| 659 | if((CPU_now->long_regs == NULL) || (imm_flag == 0)) |
| 660 | return(opcode); |
| 661 | // check force bits (if no force bits given, use relevant flag) |
| 662 | if(*force_bit == 0) |
| 663 | *force_bit = ((imm_flag & IMM_ACCU) ? |
| 664 | CPU_now->long_regs[LONGREG_IDX_A] : |
| 665 | CPU_now->long_regs[LONGREG_IDX_R]) ? |
| 666 | MVALUE_FORCE16 : |
| 667 | MVALUE_FORCE08; |
| 668 | // return identical opcodes for 8bit and 16bit args! |
| 669 | return((((unsigned int) opcode) << 8) | opcode); |
| 670 | } |
| 671 | |
| 672 | // The main accumulator stuff (ADC, AND, CMP, EOR, LDA, ORA, SBC, STA) |
| 673 | // plus PEI. |
| 674 | static void group_main(int index, int imm_flag) { |
| 675 | unsigned long imm_opcodes; |
| 676 | result_int_t result; |
| 677 | int force_bit = Input_get_force_bit();// skips spaces after |
| 678 | |
| 679 | switch(get_argument(&result)) { |
| 680 | |
| 681 | // #$ff or #$ffff (depending on accu length) |
| 682 | case HAM_IMM: |
| 683 | imm_opcodes = imm_ops(&force_bit, accu_imm[index], imm_flag); |
| 684 | // CAUTION - do not incorporate the line above into the line |
| 685 | // below - "fb" might be undefined (depends on compiler). |
| 686 | make_command(force_bit, &result, imm_opcodes); |
| 687 | break; |
| 688 | |
| 689 | // $ff, $ffff, $ffffff |
| 690 | case HAM_ABS: |
| 691 | make_command(force_bit, &result, accu_abs[index]); |
| 692 | break; |
| 693 | |
| 694 | // $ff,x, $ffff,x, $ffffff,x |
| 695 | case HAM_ABSX: |
| 696 | make_command(force_bit, &result, accu_xabs[index]); |
| 697 | break; |
| 698 | |
| 699 | // $ffff,y (in theory, "$ff,y" as well) |
| 700 | case HAM_ABSY: |
| 701 | make_command(force_bit, &result, accu_yabs[index]); |
| 702 | break; |
| 703 | |
| 704 | // $ff,s |
| 705 | case HAM_ABSS: |
| 706 | make_command(force_bit, &result, accu_sabs8[index]); |
| 707 | break; |
| 708 | |
| 709 | // ($ff,x) |
| 710 | case HAM_XIND: |
| 711 | make_command(force_bit, &result, accu_xind8[index]); |
| 712 | break; |
| 713 | |
| 714 | // ($ff) |
| 715 | case HAM_IND: |
| 716 | make_command(force_bit, &result, accu_ind8[index]); |
| 717 | break; |
| 718 | |
| 719 | // ($ff),y |
| 720 | case HAM_INDY: |
| 721 | make_command(force_bit, &result, accu_indy8[index]); |
| 722 | break; |
| 723 | |
| 724 | // [$ff] |
| 725 | case HAM_LIND: |
| 726 | make_command(force_bit, &result, accu_lind8[index]); |
| 727 | break; |
| 728 | |
| 729 | // [$ff],y |
| 730 | case HAM_LINDY: |
| 731 | make_command(force_bit, &result, accu_lindy8[index]); |
| 732 | break; |
| 733 | |
| 734 | // ($ff,s),y |
| 735 | case HAM_SINDY: |
| 736 | make_command(force_bit, &result, accu_sindy8[index]); |
| 737 | break; |
| 738 | |
| 739 | // other combinations are illegal |
| 740 | default: |
| 741 | Throw_error(exception_illegal_combination); |
| 742 | |
| 743 | } |
| 744 | } |
| 745 | |
| 746 | // Various mnemonics with different addressing modes. |
| 747 | static void group_misc(int index, int imm_flag) { |
| 748 | unsigned long imm_opcodes; |
| 749 | result_int_t result; |
| 750 | int force_bit = Input_get_force_bit();// skips spaces after |
| 751 | |
| 752 | switch(get_argument(&result)) { |
| 753 | |
| 754 | // implied addressing |
| 755 | case HAM_IMP: |
| 756 | if(misc_impl[index]) |
| 757 | Output_byte(misc_impl[index]); |
| 758 | else |
| 759 | Throw_error(exception_illegal_combination); |
| 760 | break; |
| 761 | |
| 762 | // #$ff or #$ffff (depending on index register length) |
| 763 | case HAM_IMM: |
| 764 | imm_opcodes = imm_ops(&force_bit, misc_imm[index], imm_flag); |
| 765 | // CAUTION - do not incorporate the line above into the line |
| 766 | // below - force_bit might be undefined (depends on compiler). |
| 767 | make_command(force_bit, &result, imm_opcodes); |
| 768 | break; |
| 769 | |
| 770 | // $ff or $ffff |
| 771 | case HAM_ABS: |
| 772 | make_command(force_bit, &result, misc_abs[index]); |
| 773 | break; |
| 774 | |
| 775 | // $ff,x or $ffff,x |
| 776 | case HAM_ABSX: |
| 777 | make_command(force_bit, &result, misc_xabs[index]); |
| 778 | break; |
| 779 | |
| 780 | // $ff,y or $ffff,y |
| 781 | case HAM_ABSY: |
| 782 | make_command(force_bit, &result, misc_yabs[index]); |
| 783 | break; |
| 784 | |
| 785 | // other combinations are illegal |
| 786 | default: |
| 787 | Throw_error(exception_illegal_combination); |
| 788 | } |
| 789 | } |
| 790 | |
| 791 | // Mnemonics using "8bit, 8bit" addressing. Only applies to "MVN" and "MVP". |
| 792 | static void group_move(int opcode) { |
| 793 | intval_t source, |
| 794 | target; |
| 795 | |
| 796 | // assembler syntax: "opcode source, target" |
| 797 | source = ALU_any_int(); |
| 798 | if(Input_accept_comma()) { |
| 799 | target = ALU_any_int(); // machine language: |
| 800 | Output_byte(opcode); // opcode |
| 801 | Output_8b(target); // target |
| 802 | Output_8b(source); // source |
| 803 | Input_ensure_EOS(); |
| 804 | } else |
| 805 | Throw_error(exception_syntax); |
| 806 | } |
| 807 | |
| 808 | // The jump instructions. |
| 809 | static void group_jump(int index) { |
| 810 | result_int_t result; |
| 811 | int force_bit = Input_get_force_bit();// skips spaces after |
| 812 | |
| 813 | switch(get_argument(&result)) { |
| 814 | |
| 815 | // absolute16 or absolute24 |
| 816 | case HAM_ABS: |
| 817 | make_command(force_bit, &result, jump_abs[index]); |
| 818 | break; |
| 819 | |
| 820 | // ($ffff) |
| 821 | case HAM_IND: |
| 822 | make_command(force_bit, &result, jump_ind[index]); |
| 823 | // check whether to warn about 6502's JMP() bug |
| 824 | if(((result.intval & 0xff) == 0xff) |
| 825 | && (result.flags & MVALUE_DEFINED) |
| 826 | && (CPU_now->flags & CPUFLAG_INDJMPBUGGY)) |
| 827 | Throw_warning("Assembling buggy JMP($xxff) instruction"); |
| 828 | break; |
| 829 | |
| 830 | // ($ffff,x) |
| 831 | case HAM_XIND: |
| 832 | make_command(force_bit, &result, jump_xind[index]); |
| 833 | break; |
| 834 | |
| 835 | // [$ffff] |
| 836 | case HAM_LIND: |
| 837 | make_command(force_bit, &result, jump_lind[index]); |
| 838 | break; |
| 839 | |
| 840 | // other combinations are illegal |
| 841 | default: |
| 842 | Throw_error(exception_illegal_combination); |
| 843 | |
| 844 | } |
| 845 | } |
| 846 | |
| 847 | // Work function |
| 848 | static bool check_mnemo_tree(node_t* tree, dynabuf_t* dyna_buf) { |
| 849 | void* node_body; |
| 850 | int code, |
| 851 | imm_flag; // flag for immediate addressing |
| 852 | |
| 853 | // search for tree item |
| 854 | if(!Tree_easy_scan(tree, &node_body, dyna_buf)) |
| 855 | return(FALSE); |
| 856 | code = ((int) node_body) & CODEMASK; // get opcode or table index |
| 857 | imm_flag = ((int) node_body) & FLAGSMASK; // get flag |
| 858 | switch(((long) node_body) >> GROUPSHIFT) { |
| 859 | |
| 860 | // mnemonics with only implied addressing |
| 861 | case G_IMPLIED: |
| 862 | group_only_implied_addressing(code); |
| 863 | break; |
| 864 | |
| 865 | // main accumulator stuff |
| 866 | case G_MAIN: |
| 867 | group_main(code, imm_flag); |
| 868 | break; |
| 869 | |
| 870 | // misc |
| 871 | case G_MISC: |
| 872 | group_misc(code, imm_flag); |
| 873 | break; |
| 874 | |
| 875 | // short relative |
| 876 | case G_REL_SHORT: |
| 877 | group_only_relative8_addressing(code); |
| 878 | break; |
| 879 | |
| 880 | // long relative |
| 881 | case G_REL_LONG: |
| 882 | group_only_relative16_addressing(code); |
| 883 | break; |
| 884 | |
| 885 | // the jump instructions |
| 886 | case G_JUMP: |
| 887 | group_jump(code); |
| 888 | break; |
| 889 | |
| 890 | // "mvp" and "mvn" |
| 891 | case G_MOVE: |
| 892 | group_move(code); |
| 893 | break; |
| 894 | |
| 895 | // others indicate bugs |
| 896 | default: |
| 897 | Bug_found("IllegalGroupIndex", code); |
| 898 | |
| 899 | } |
| 900 | return(TRUE); |
| 901 | } |
| 902 | |
| 903 | // Check whether mnemonic in GlobalDynaBuf is supported by 6502 cpu. |
| 904 | bool keyword_is_6502mnemo(int length) { |
| 905 | if(length != 3) |
| 906 | return(FALSE); |
| 907 | // make lower case version of mnemonic in local dynamic buffer |
| 908 | DynaBuf_to_lower(mnemo_dyna_buf, GlobalDynaBuf); |
| 909 | if(check_mnemo_tree(mnemo_6502_tree, mnemo_dyna_buf)) |
| 910 | return(TRUE); |
| 911 | return(FALSE); |
| 912 | } |
| 913 | |
| 914 | // Check whether mnemonic in GlobalDynaBuf is supported by 6510 cpu. |
| 915 | bool keyword_is_6510mnemo(int length) { |
| 916 | if(length != 3) |
| 917 | return(FALSE); |
| 918 | // make lower case version of mnemonic in local dynamic buffer |
| 919 | DynaBuf_to_lower(mnemo_dyna_buf, GlobalDynaBuf); |
| 920 | // first check extensions... |
| 921 | if(check_mnemo_tree(mnemo_6510_tree, mnemo_dyna_buf)) |
| 922 | return(TRUE); |
| 923 | // ...then check original opcodes |
| 924 | if(check_mnemo_tree(mnemo_6502_tree, mnemo_dyna_buf)) |
| 925 | return(TRUE); |
| 926 | return(FALSE); |
| 927 | } |
| 928 | |
| 929 | // Check whether mnemonic in GlobalDynaBuf is supported by 65c02 cpu. |
| 930 | bool keyword_is_65c02mnemo(int length) { |
| 931 | if(length != 3) |
| 932 | return(FALSE); |
| 933 | // make lower case version of mnemonic in local dynamic buffer |
| 934 | DynaBuf_to_lower(mnemo_dyna_buf, GlobalDynaBuf); |
| 935 | // first check extensions... |
| 936 | if(check_mnemo_tree(mnemo_65c02_tree, mnemo_dyna_buf)) |
| 937 | return(TRUE); |
| 938 | // ...then check original opcodes |
| 939 | if(check_mnemo_tree(mnemo_6502_tree, mnemo_dyna_buf)) |
| 940 | return(TRUE); |
| 941 | return(FALSE); |
| 942 | } |
| 943 | |
| 944 | //// Check whether mnemonic in GlobalDynaBuf is supported by Rockwell 65c02 cpu. |
| 945 | //bool keyword_is_Rockwell65c02mnemo(int length) { |
| 946 | // if((length != 3) && (length != 4)) |
| 947 | // return(FALSE); |
| 948 | // // make lower case version of mnemonic in local dynamic buffer |
| 949 | // DynaBuf_to_lower(mnemo_dyna_buf, GlobalDynaBuf); |
| 950 | // // first check 65c02 extensions... |
| 951 | // if(check_mnemo_tree(mnemo_65c02_tree, mnemo_dyna_buf)) |
| 952 | // return(TRUE); |
| 953 | // // ...then check original opcodes... |
| 954 | // if(check_mnemo_tree(mnemo_6502_tree, mnemo_dyna_buf)) |
| 955 | // return(TRUE); |
| 956 | // // ...then check Rockwell/WDC extensions (rmb, smb, bbr, bbs) |
| 957 | // if(check_mnemo_tree(mnemo_Rockwell65c02_tree, mnemo_dyna_buf)) |
| 958 | // return(TRUE); |
| 959 | // return(FALSE); |
| 960 | //} |
| 961 | |
| 962 | //// Check whether mnemonic in GlobalDynaBuf is supported by WDC 65c02 cpu. |
| 963 | //bool keyword_is_WDC65c02mnemo(int length) { |
| 964 | // if((length != 3) && (length != 4)) |
| 965 | // return(FALSE); |
| 966 | // // make lower case version of mnemonic in local dynamic buffer |
| 967 | // DynaBuf_to_lower(mnemo_dyna_buf, GlobalDynaBuf); |
| 968 | // // first check 65c02 extensions... |
| 969 | // if(check_mnemo_tree(mnemo_65c02_tree, mnemo_dyna_buf)) |
| 970 | // return(TRUE); |
| 971 | // // ...then check original opcodes... |
| 972 | // if(check_mnemo_tree(mnemo_6502_tree, mnemo_dyna_buf)) |
| 973 | // return(TRUE); |
| 974 | // // ...then check Rockwell/WDC extensions (rmb, smb, bbr, bbs)... |
| 975 | // if(check_mnemo_tree(mnemo_Rockwell65c02_tree, mnemo_dyna_buf)) |
| 976 | // return(TRUE); |
| 977 | // // ...then check WDC extensions (only two mnemonics, so do last) |
| 978 | // if(check_mnemo_tree(mnemo_WDC65c02_tree, mnemo_dyna_buf)) |
| 979 | // return(TRUE); |
| 980 | // return(FALSE); |
| 981 | //} |
| 982 | |
| 983 | // Check whether mnemonic in GlobalDynaBuf is supported by 65816 cpu. |
| 984 | bool keyword_is_65816mnemo(int length) { |
| 985 | if(length != 3) |
| 986 | return(FALSE); |
| 987 | // make lower case version of mnemonic in local dynamic buffer |
| 988 | DynaBuf_to_lower(mnemo_dyna_buf, GlobalDynaBuf); |
| 989 | // first check "new" extensions... |
| 990 | if(check_mnemo_tree(mnemo_65816_tree, mnemo_dyna_buf)) |
| 991 | return(TRUE); |
| 992 | // ...then "old" extensions... |
| 993 | if(check_mnemo_tree(mnemo_65c02_tree, mnemo_dyna_buf)) |
| 994 | return(TRUE); |
| 995 | // ...then check original opcodes... |
| 996 | if(check_mnemo_tree(mnemo_6502_tree, mnemo_dyna_buf)) |
| 997 | return(TRUE); |
| 998 | // ...then check WDC extensions "stp" and "wai" |
| 999 | if(check_mnemo_tree(mnemo_WDC65c02_tree, mnemo_dyna_buf)) |
| 1000 | return(TRUE); |
| 1001 | return(FALSE); |
| 1002 | } |