1 /*****************************************************************************/
5 /* Opcode and addressing mode definitions */
9 /* (C) 2001 Ullrich von Bassewitz */
11 /* D-70597 Stuttgart */
12 /* EMail: uz@cc65.org */
15 /* This software is provided 'as-is', without any expressed or implied */
16 /* warranty. In no event will the authors be held liable for any damages */
17 /* arising from the use of this software. */
19 /* Permission is granted to anyone to use this software for any purpose, */
20 /* including commercial applications, and to alter it and redistribute it */
21 /* freely, subject to the following restrictions: */
23 /* 1. The origin of this software must not be misrepresented; you must not */
24 /* claim that you wrote the original software. If you use this software */
25 /* in a product, an acknowledgment in the product documentation would be */
26 /* appreciated but is not required. */
27 /* 2. Altered source versions must be plainly marked as such, and must not */
28 /* be misrepresented as being the original software. */
29 /* 3. This notice may not be removed or altered from any source */
32 /*****************************************************************************/
51 /*****************************************************************************/
53 /*****************************************************************************/
57 /* Opcode description table */
58 const OPCDesc OPCTable[OPCODE_COUNT] = {
60 /* Opcodes for the virtual stack machine */
61 { OPC_LDA, /* opcode */
62 "loada", /* mnemonic */
66 OF_CPU_VM | OF_LOAD /* flags */
68 { OPC_LDAX, /* opcode */
69 "loadax", /* mnemonic */
73 OF_CPU_VM | OF_LOAD /* flags */
75 { OPC_LDEAX, /* opcode */
76 "loadeax", /* mnemonic */
80 OF_CPU_VM | OF_LOAD /* flags */
82 { OPC_PHA, /* opcode */
83 "pusha", /* mnemonic */
89 { OPC_PHAX, /* opcode */
90 "pushax", /* mnemonic */
96 { OPC_PHEAX, /* opcode */
97 "pusheax", /* mnemonic */
101 OF_CPU_VM /* flags */
103 { OPC_STA, /* opcode */
104 "storea", /* mnemonic */
108 OF_CPU_VM /* flags */
110 { OPC_STAX, /* opcode */
111 "storeax", /* mnemonic */
115 OF_CPU_VM /* flags */
117 { OPC_STEAX, /* opcode */
118 "storeeax", /* mnemonic */
122 OF_CPU_VM /* flags */
124 { OPC_LEA, /* opcode */
125 "lea", /* mnemonic */
129 OF_CPU_VM /* flags */
131 { OPC_JMP, /* opcode */
132 "jump", /* mnemonic */
136 OF_CPU_VM | OF_UBRA /* flags */
140 { OP65_ADC, /* opcode */
141 "adc", /* mnemonic */
147 { OP65_AND, /* opcode */
148 "and", /* mnemonic */
154 { OP65_ASL, /* opcode */
155 "asl", /* mnemonic */
161 { OP65_BCC, /* opcode */
162 "bcc", /* mnemonic */
168 { OP65_BCS, /* opcode */
169 "bcs", /* mnemonic */
175 { OP65_BEQ, /* opcode */
176 "beq", /* mnemonic */
180 OF_CBRA | OF_ZBRA | OF_FBRA /* flags */
182 { OP65_BIT, /* opcode */
183 "bit", /* mnemonic */
189 { OP65_BMI, /* opcode */
190 "bmi", /* mnemonic */
194 OF_CBRA | OF_FBRA /* flags */
196 { OP65_BNE, /* opcode */
197 "bne", /* mnemonic */
201 OF_CBRA | OF_ZBRA | OF_FBRA /* flags */
203 { OP65_BPL, /* opcode */
204 "bpl", /* mnemonic */
208 OF_CBRA | OF_FBRA /* flags */
210 { OP65_BRA, /* opcode */
211 "bra", /* mnemonic */
217 { OP65_BRK, /* opcode */
218 "brk", /* mnemonic */
224 { OP65_BVC, /* opcode */
225 "bvc", /* mnemonic */
231 { OP65_BVS, /* opcode */
232 "bvs", /* mnemonic */
238 { OP65_CLC, /* opcode */
239 "clc", /* mnemonic */
245 { OP65_CLD, /* opcode */
246 "cld", /* mnemonic */
252 { OP65_CLI, /* opcode */
253 "cli", /* mnemonic */
259 { OP65_CLV, /* opcode */
260 "clv", /* mnemonic */
266 { OP65_CMP, /* opcode */
267 "cmp", /* mnemonic */
273 { OP65_CPX, /* opcode */
274 "cpx", /* mnemonic */
280 { OP65_CPY, /* opcode */
281 "cpy", /* mnemonic */
287 { OP65_DEA, /* opcode */
288 "dea", /* mnemonic */
294 { OP65_DEC, /* opcode */
295 "dec", /* mnemonic */
301 { OP65_DEX, /* opcode */
302 "dex", /* mnemonic */
308 { OP65_DEY, /* opcode */
309 "dey", /* mnemonic */
315 { OP65_EOR, /* opcode */
316 "eor", /* mnemonic */
322 { OP65_INA, /* opcode */
323 "ina", /* mnemonic */
329 { OP65_INC, /* opcode */
330 "inc", /* mnemonic */
336 { OP65_INX, /* opcode */
337 "inx", /* mnemonic */
343 { OP65_INY, /* opcode */
344 "iny", /* mnemonic */
350 { OP65_JCC, /* opcode */
351 "jcc", /* mnemonic */
355 OF_CBRA | OF_LBRA /* flags */
357 { OP65_JCS, /* opcode */
358 "jcs", /* mnemonic */
362 OF_CBRA | OF_LBRA /* flags */
364 { OP65_JEQ, /* opcode */
365 "jeq", /* mnemonic */
369 OF_CBRA | OF_LBRA | OF_ZBRA | OF_FBRA /* flags */
371 { OP65_JMI, /* opcode */
372 "jmi", /* mnemonic */
376 OF_CBRA | OF_LBRA | OF_FBRA /* flags */
378 { OP65_JMP, /* opcode */
379 "jmp", /* mnemonic */
383 OF_UBRA | OF_LBRA /* flags */
385 { OP65_JNE, /* opcode */
386 "jne", /* mnemonic */
390 OF_CBRA | OF_LBRA | OF_ZBRA | OF_FBRA /* flags */
392 { OP65_JPL, /* opcode */
393 "jpl", /* mnemonic */
397 OF_CBRA | OF_LBRA | OF_FBRA /* flags */
399 { OP65_JSR, /* opcode */
400 "jsr", /* mnemonic */
406 { OP65_JVC, /* opcode */
407 "jvc", /* mnemonic */
411 OF_CBRA | OF_LBRA /* flags */
413 { OP65_JVS, /* opcode */
414 "jvs", /* mnemonic */
418 OF_CBRA | OF_LBRA /* flags */
420 { OP65_LDA, /* opcode */
421 "lda", /* mnemonic */
427 { OP65_LDX, /* opcode */
428 "ldx", /* mnemonic */
434 { OP65_LDY, /* opcode */
435 "ldy", /* mnemonic */
441 { OP65_LSR, /* opcode */
442 "lsr", /* mnemonic */
448 { OP65_NOP, /* opcode */
449 "nop", /* mnemonic */
455 { OP65_ORA, /* opcode */
456 "ora", /* mnemonic */
462 { OP65_PHA, /* opcode */
463 "pha", /* mnemonic */
469 { OP65_PHP, /* opcode */
470 "php", /* mnemonic */
476 { OP65_PHX, /* opcode */
477 "phx", /* mnemonic */
483 { OP65_PHY, /* opcode */
484 "phy", /* mnemonic */
490 { OP65_PLA, /* opcode */
491 "pla", /* mnemonic */
497 { OP65_PLP, /* opcode */
498 "plp", /* mnemonic */
504 { OP65_PLX, /* opcode */
505 "plx", /* mnemonic */
511 { OP65_PLY, /* opcode */
512 "ply", /* mnemonic */
518 { OP65_ROL, /* opcode */
519 "rol", /* mnemonic */
525 { OP65_ROR, /* opcode */
526 "ror", /* mnemonic */
532 { OP65_RTI, /* opcode */
533 "rti", /* mnemonic */
539 { OP65_RTS, /* opcode */
540 "rts", /* mnemonic */
546 { OP65_SBC, /* opcode */
547 "sbc", /* mnemonic */
553 { OP65_SEC, /* opcode */
554 "sec", /* mnemonic */
560 { OP65_SED, /* opcode */
561 "sed", /* mnemonic */
567 { OP65_SEI, /* opcode */
568 "sei", /* mnemonic */
574 { OP65_STA, /* opcode */
575 "sta", /* mnemonic */
581 { OP65_STX, /* opcode */
582 "stx", /* mnemonic */
588 { OP65_STY, /* opcode */
589 "sty", /* mnemonic */
595 { OP65_TAX, /* opcode */
596 "tax", /* mnemonic */
602 { OP65_TAY, /* opcode */
603 "tay", /* mnemonic */
609 { OP65_TRB, /* opcode */
610 "trb", /* mnemonic */
616 { OP65_TSB, /* opcode */
617 "tsb", /* mnemonic */
623 { OP65_TSX, /* opcode */
624 "tsx", /* mnemonic */
630 { OP65_TXA, /* opcode */
631 "txa", /* mnemonic */
637 { OP65_TXS, /* opcode */
638 "txs", /* mnemonic */
644 { OP65_TYA, /* opcode */
645 "tya", /* mnemonic */
655 /*****************************************************************************/
657 /*****************************************************************************/
661 static int FindCmp (const void* Key, const void* Desc)
662 /* Compare function for FindOpcode */
664 return strcmp (Key, ((OPCDesc*)Desc)->Mnemo);
669 const OPCDesc* FindOP65 (const char* M)
670 /* Find the given opcode and return the opcode number. If the opcode was not
671 * found, return NULL.
677 /* Check the length of the given string, then copy it into local
678 * storage, converting it to upper case.
680 char Mnemo[sizeof (OPCTable[0].Mnemo)];
682 if (Len >= sizeof (OPCTable[0].Mnemo)) {
683 /* Invalid length means invalid opcode */
686 for (I = 0; I < Len; ++I) {
687 Mnemo[I] = tolower (M[I]);
691 /* Search for the mnemonic in the table and return the result */
692 return bsearch (Mnemo, OPCTable+OP65_FIRST, OP65_COUNT,
693 sizeof (OPCTable[0]), FindCmp );
698 unsigned GetInsnSize (opc_t OPC, am_t AM)
699 /* Return the size of the given instruction */
701 /* Get the opcode desc and check the size given there */
702 const OPCDesc* D = &OPCTable[OPC];
707 /* Check the addressing mode. */
709 case AM65_IMP: return 1;
710 case AM65_ACC: return 1;
711 case AM65_IMM: return 2;
712 case AM65_ZP: return 2;
713 case AM65_ZPX: return 2;
714 case AM65_ABS: return 3;
715 case AM65_ABSX: return 3;
716 case AM65_ABSY: return 3;
717 case AM65_ZPX_IND: return 2;
718 case AM65_ZP_INDY: return 2;
719 case AM65_ZP_IND: return 2;
721 Internal ("Invalid addressing mode");
728 unsigned char GetAMUseInfo (am_t AM)
729 /* Get usage info for the given addressing mode (addressing modes that use
730 * index registers return REG_r info for these registers).
733 /* Check the addressing mode. */
735 case AM65_ACC: return REG_A;
736 case AM65_ZPX: return REG_X;
737 case AM65_ABSX: return REG_X;
738 case AM65_ABSY: return REG_Y;
739 case AM65_ZPX_IND: return REG_X;
740 case AM65_ZP_INDY: return REG_Y;
741 default: return REG_NONE;
747 opc_t GetInverseBranch (opc_t OPC)
748 /* Return a branch that reverse the condition of the branch given in OPC */
751 case OP65_BCC: return OP65_BCS;
752 case OP65_BCS: return OP65_BCC;
753 case OP65_BEQ: return OP65_BNE;
754 case OP65_BMI: return OP65_BPL;
755 case OP65_BNE: return OP65_BEQ;
756 case OP65_BPL: return OP65_BMI;
757 case OP65_BVC: return OP65_BVS;
758 case OP65_BVS: return OP65_BVC;
759 case OP65_JCC: return OP65_JCS;
760 case OP65_JCS: return OP65_JCC;
761 case OP65_JEQ: return OP65_JNE;
762 case OP65_JMI: return OP65_JPL;
763 case OP65_JNE: return OP65_JEQ;
764 case OP65_JPL: return OP65_JMI;
765 case OP65_JVC: return OP65_JVS;
766 case OP65_JVS: return OP65_JVC;
768 Internal ("GetInverseBranch: Invalid opcode: %d", OPC);
775 opc_t MakeShortBranch (opc_t OPC)
776 /* Return the short version of the given branch. If the branch is already
777 * a short branch, return the opcode unchanged.
782 case OP65_JCC: return OP65_BCC;
784 case OP65_JCS: return OP65_BCS;
786 case OP65_JEQ: return OP65_BEQ;
788 case OP65_JMI: return OP65_BMI;
790 case OP65_JNE: return OP65_BNE;
792 case OP65_JPL: return OP65_BPL;
794 case OP65_JVC: return OP65_BVC;
796 case OP65_JVS: return OP65_BVS;
798 case OP65_JMP: return (CPU == CPU_65C02)? OP65_BRA : OP65_JMP;
800 Internal ("MakeShortBranch: Invalid opcode: %d", OPC);
807 opc_t MakeLongBranch (opc_t OPC)
808 /* Return the long version of the given branch. If the branch is already
809 * a long branch, return the opcode unchanged.
814 case OP65_JCC: return OP65_JCC;
816 case OP65_JCS: return OP65_JCS;
818 case OP65_JEQ: return OP65_JEQ;
820 case OP65_JMI: return OP65_JMI;
822 case OP65_JNE: return OP65_JNE;
824 case OP65_JPL: return OP65_JPL;
826 case OP65_JVC: return OP65_JVC;
828 case OP65_JVS: return OP65_JVS;
830 case OP65_JMP: return OP65_JMP;
832 Internal ("MakeLongBranch: Invalid opcode: %d", OPC);
839 bc_t GetBranchCond (opc_t OPC)
840 /* Get the condition for the conditional branch in OPC */
843 case OP65_BCC: return BC_CC;
844 case OP65_BCS: return BC_CS;
845 case OP65_BEQ: return BC_EQ;
846 case OP65_BMI: return BC_MI;
847 case OP65_BNE: return BC_NE;
848 case OP65_BPL: return BC_PL;
849 case OP65_BVC: return BC_VC;
850 case OP65_BVS: return BC_VS;
851 case OP65_JCC: return BC_CC;
852 case OP65_JCS: return BC_CS;
853 case OP65_JEQ: return BC_EQ;
854 case OP65_JMI: return BC_MI;
855 case OP65_JNE: return BC_NE;
856 case OP65_JPL: return BC_PL;
857 case OP65_JVC: return BC_VC;
858 case OP65_JVS: return BC_VS;
860 Internal ("GetBranchCond: Invalid opcode: %d", OPC);
867 bc_t GetInverseCond (bc_t BC)
868 /* Return the inverse condition of the given one */
871 case BC_CC: return BC_CS;
872 case BC_CS: return BC_CC;
873 case BC_EQ: return BC_NE;
874 case BC_MI: return BC_PL;
875 case BC_NE: return BC_EQ;
876 case BC_PL: return BC_MI;
877 case BC_VC: return BC_VS;
878 case BC_VS: return BC_VC;
880 Internal ("GetInverseCond: Invalid condition: %d", BC);