1 /*****************************************************************************/
5 /* CPU core for the 6502 */
9 /* (C) 2003-2012, Ullrich von Bassewitz */
10 /* Roemerstrasse 52 */
11 /* D-70794 Filderstadt */
12 /* EMail: uz@cc65.org */
14 /* Mar-2017, Christian Krueger, added support for 65SC02 */
16 /* This software is provided 'as-is', without any expressed or implied */
17 /* warranty. In no event will the authors be held liable for any damages */
18 /* arising from the use of this software. */
20 /* Permission is granted to anyone to use this software for any purpose, */
21 /* including commercial applications, and to alter it and redistribute it */
22 /* freely, subject to the following restrictions: */
24 /* 1. The origin of this software must not be misrepresented; you must not */
25 /* claim that you wrote the original software. If you use this software */
26 /* in a product, an acknowledgment in the product documentation would be */
27 /* appreciated but is not required. */
28 /* 2. Altered source versions must be plainly marked as such, and must not */
29 /* be misrepresented as being the original software. */
30 /* 3. This notice may not be removed or altered from any source */
33 /*****************************************************************************/
35 /* Known bugs and limitations of the 65C02 simulation:
36 * support currently only on the level of 65SC02:
37 BBRx, BBSx, RMBx, SMBx, WAI, and STP are unsupported
38 * BCD flag handling equals 6502 (unchecked if bug is simulated or wrong for
40 * one cycle win for fetch-modify-write instructions ignored
41 (e.g., ROL abs,x takes only 6 cycles if no page break occurs)
51 /*****************************************************************************/
53 /*****************************************************************************/
60 /* Type of an opcode handler function */
61 typedef void (*OPFunc) (void);
63 /* The CPU registers */
66 /* Cycles for the current insn */
67 static unsigned Cycles;
69 /* Total number of CPU cycles exec'd */
70 static unsigned long TotalCycles;
72 /* NMI request active */
73 static unsigned HaveNMIRequest;
75 /* IRQ request active */
76 static unsigned HaveIRQRequest;
78 /* flag to print cycles at program termination */
82 /*****************************************************************************/
83 /* Helper functions and macros */
84 /*****************************************************************************/
88 /* Return the flags as boolean values (0/1) */
89 #define GET_CF() ((Regs.SR & CF) != 0)
90 #define GET_ZF() ((Regs.SR & ZF) != 0)
91 #define GET_IF() ((Regs.SR & IF) != 0)
92 #define GET_DF() ((Regs.SR & DF) != 0)
93 #define GET_OF() ((Regs.SR & OF) != 0)
94 #define GET_SF() ((Regs.SR & SF) != 0)
96 /* Set the flags. The parameter is a boolean flag that says if the flag should be
99 #define SET_CF(f) do { if (f) { Regs.SR |= CF; } else { Regs.SR &= ~CF; } } while (0)
100 #define SET_ZF(f) do { if (f) { Regs.SR |= ZF; } else { Regs.SR &= ~ZF; } } while (0)
101 #define SET_IF(f) do { if (f) { Regs.SR |= IF; } else { Regs.SR &= ~IF; } } while (0)
102 #define SET_DF(f) do { if (f) { Regs.SR |= DF; } else { Regs.SR &= ~DF; } } while (0)
103 #define SET_OF(f) do { if (f) { Regs.SR |= OF; } else { Regs.SR &= ~OF; } } while (0)
104 #define SET_SF(f) do { if (f) { Regs.SR |= SF; } else { Regs.SR &= ~SF; } } while (0)
106 /* Special test and set macros. The meaning of the parameter depends on the
107 ** actual flag that should be set or reset.
109 #define TEST_ZF(v) SET_ZF (((v) & 0xFF) == 0)
110 #define TEST_SF(v) SET_SF (((v) & 0x80) != 0)
111 #define TEST_CF(v) SET_CF (((v) & 0xFF00) != 0)
113 /* Program counter halves */
114 #define PCL (Regs.PC & 0xFF)
115 #define PCH ((Regs.PC >> 8) & 0xFF)
117 /* Stack operations */
118 #define PUSH(Val) MemWriteByte (0x0100 | (Regs.SP-- & 0xFF), Val)
119 #define POP() MemReadByte (0x0100 | (++Regs.SP & 0xFF))
121 /* Test for page cross */
122 #define PAGE_CROSS(addr,offs) ((((addr) & 0xFF) + offs) >= 0x100)
125 #define AC_OP_IMM(op) \
127 Regs.AC = Regs.AC op MemReadByte (Regs.PC+1); \
133 #define AC_OP_ZP(op) \
135 Regs.AC = Regs.AC op MemReadByte (MemReadByte (Regs.PC+1)); \
141 #define AC_OP_ZPX(op) \
142 unsigned char ZPAddr; \
144 ZPAddr = MemReadByte (Regs.PC+1) + Regs.XR; \
145 Regs.AC = Regs.AC op MemReadByte (ZPAddr); \
151 #define AC_OP_ZPY(op) \
152 unsigned char ZPAddr; \
154 ZPAddr = MemReadByte (Regs.PC+1) + Regs.YR; \
155 Regs.AC = Regs.AC op MemReadByte (ZPAddr); \
161 #define AC_OP_ABS(op) \
164 Addr = MemReadWord (Regs.PC+1); \
165 Regs.AC = Regs.AC op MemReadByte (Addr); \
171 #define AC_OP_ABSX(op) \
174 Addr = MemReadWord (Regs.PC+1); \
175 if (PAGE_CROSS (Addr, Regs.XR)) { \
178 Regs.AC = Regs.AC op MemReadByte (Addr + Regs.XR); \
184 #define AC_OP_ABSY(op) \
187 Addr = MemReadWord (Regs.PC+1); \
188 if (PAGE_CROSS (Addr, Regs.YR)) { \
191 Regs.AC = Regs.AC op MemReadByte (Addr + Regs.YR); \
197 #define AC_OP_ZPXIND(op) \
198 unsigned char ZPAddr; \
201 ZPAddr = MemReadByte (Regs.PC+1) + Regs.XR; \
202 Addr = MemReadZPWord (ZPAddr); \
203 Regs.AC = Regs.AC op MemReadByte (Addr); \
209 #define AC_OP_ZPINDY(op) \
210 unsigned char ZPAddr; \
213 ZPAddr = MemReadByte (Regs.PC+1); \
214 Addr = MemReadZPWord (ZPAddr); \
215 if (PAGE_CROSS (Addr, Regs.YR)) { \
219 Regs.AC = Regs.AC op MemReadByte (Addr); \
225 #define AC_OP_ZPIND(op) \
226 unsigned char ZPAddr; \
229 ZPAddr = MemReadByte (Regs.PC+1); \
230 Addr = MemReadZPWord (ZPAddr); \
231 Regs.AC = Regs.AC op MemReadByte (Addr); \
239 unsigned old = Regs.AC; \
240 unsigned rhs = (v & 0xFF); \
244 lo = (old & 0x0F) + (rhs & 0x0F) + GET_CF (); \
246 lo = ((lo + 0x06) & 0x0F) + 0x10; \
248 Regs.AC = (old & 0xF0) + (rhs & 0xF0) + lo; \
249 res = (signed char)(old & 0xF0) + \
250 (signed char)(rhs & 0xF0) + \
252 TEST_ZF (old + rhs + GET_CF ()); \
254 if (Regs.AC >= 0xA0) { \
258 SET_OF ((res < -128) || (res > 127)); \
259 if (CPU != CPU_6502) { \
263 Regs.AC += rhs + GET_CF (); \
267 SET_OF (!((old ^ rhs) & 0x80) && \
268 ((old ^ Regs.AC) & 0x80)); \
274 #define BRANCH(cond) \
278 unsigned char OldPCH; \
280 Offs = (signed char) MemReadByte (Regs.PC+1); \
282 Regs.PC += 2 + (int) Offs; \
283 if (PCH != OldPCH) { \
291 #define CMP(v1, v2) \
293 unsigned Result = v1 - v2; \
294 TEST_ZF (Result & 0xFF); \
296 SET_CF (Result <= 0xFF); \
315 SET_CF (Val & 0x01); \
323 unsigned old = Regs.AC; \
324 unsigned rhs = (v & 0xFF); \
328 lo = (old & 0x0F) - (rhs & 0x0F) + GET_CF () - 1; \
330 lo = ((lo - 0x06) & 0x0F) - 0x10; \
332 Regs.AC = (old & 0xF0) - (rhs & 0xF0) + lo; \
333 if (Regs.AC & 0x80) { \
336 res = Regs.AC - rhs + (!GET_CF ()); \
339 SET_CF (res <= 0xFF); \
340 SET_OF (((old^rhs) & (old^res) & 0x80)); \
341 if (CPU != CPU_6502) { \
345 Regs.AC -= rhs + (!GET_CF ()); \
348 SET_CF (Regs.AC <= 0xFF); \
349 SET_OF (((old^rhs) & (old^Regs.AC) & 0x80)); \
356 /*****************************************************************************/
357 /* Opcode handling functions */
358 /*****************************************************************************/
362 static void OPC_Illegal (void)
364 Error ("Illegal opcode $%02X at address $%04X",
365 MemReadByte (Regs.PC), Regs.PC);
370 static void OPC_6502_00 (void)
371 /* Opcode $00: BRK */
383 Regs.PC = MemReadWord (0xFFFE);
388 static void OPC_6502_01 (void)
389 /* Opcode $01: ORA (ind,x) */
396 static void OPC_65SC02_04 (void)
397 /* Opcode $04: TSB zp */
399 unsigned char ZPAddr;
402 ZPAddr = MemReadByte (Regs.PC+1);
403 Val = MemReadByte (ZPAddr);
404 SET_ZF ((Val & Regs.AC) == 0);
405 MemWriteByte (ZPAddr, (unsigned char)(Val | Regs.AC));
411 static void OPC_6502_05 (void)
412 /* Opcode $05: ORA zp */
419 static void OPC_6502_06 (void)
420 /* Opcode $06: ASL zp */
422 unsigned char ZPAddr;
425 ZPAddr = MemReadByte (Regs.PC+1);
426 Val = MemReadByte (ZPAddr) << 1;
427 MemWriteByte (ZPAddr, (unsigned char) Val);
428 TEST_ZF (Val & 0xFF);
430 SET_CF (Val & 0x100);
436 static void OPC_6502_08 (void)
437 /* Opcode $08: PHP */
446 static void OPC_6502_09 (void)
447 /* Opcode $09: ORA #imm */
454 static void OPC_6502_0A (void)
455 /* Opcode $0A: ASL a */
459 TEST_ZF (Regs.AC & 0xFF);
461 SET_CF (Regs.AC & 0x100);
468 static void OPC_65SC02_0C (void)
469 /* Opcode $0C: TSB abs */
474 Addr = MemReadWord (Regs.PC+1);
475 Val = MemReadByte (Addr);
476 SET_ZF ((Val & Regs.AC) == 0);
477 MemWriteByte (Addr, (unsigned char) (Val | Regs.AC));
483 static void OPC_6502_0D (void)
484 /* Opcode $0D: ORA abs */
491 static void OPC_6502_0E (void)
492 /* Opcode $0E: ALS abs */
497 Addr = MemReadWord (Regs.PC+1);
498 Val = MemReadByte (Addr) << 1;
499 MemWriteByte (Addr, (unsigned char) Val);
500 TEST_ZF (Val & 0xFF);
502 SET_CF (Val & 0x100);
508 static void OPC_6502_10 (void)
509 /* Opcode $10: BPL */
516 static void OPC_6502_11 (void)
517 /* Opcode $11: ORA (zp),y */
524 static void OPC_65SC02_12 (void)
525 /* Opcode $12: ORA (zp) */
532 static void OPC_65SC02_14 (void)
533 /* Opcode $14: TRB zp */
535 unsigned char ZPAddr;
538 ZPAddr = MemReadByte (Regs.PC+1);
539 Val = MemReadByte (ZPAddr);
540 SET_ZF ((Val & Regs.AC) == 0);
541 MemWriteByte (ZPAddr, (unsigned char)(Val & ~Regs.AC));
547 static void OPC_6502_15 (void)
548 /* Opcode $15: ORA zp,x */
555 static void OPC_6502_16 (void)
556 /* Opcode $16: ASL zp,x */
558 unsigned char ZPAddr;
561 ZPAddr = MemReadByte (Regs.PC+1) + Regs.XR;
562 Val = MemReadByte (ZPAddr) << 1;
563 MemWriteByte (ZPAddr, (unsigned char) Val);
564 TEST_ZF (Val & 0xFF);
566 SET_CF (Val & 0x100);
572 static void OPC_6502_18 (void)
573 /* Opcode $18: CLC */
582 static void OPC_6502_19 (void)
583 /* Opcode $19: ORA abs,y */
590 static void OPC_65SC02_1A (void)
591 /* Opcode $1A: INC a */
594 Regs.AC = (Regs.AC + 1) & 0xFF;
602 static void OPC_65SC02_1C (void)
603 /* Opcode $1C: TRB abs */
608 Addr = MemReadWord (Regs.PC+1);
609 Val = MemReadByte (Addr);
610 SET_ZF ((Val & Regs.AC) == 0);
611 MemWriteByte (Addr, (unsigned char) (Val & ~Regs.AC));
617 static void OPC_6502_1D (void)
618 /* Opcode $1D: ORA abs,x */
625 static void OPC_6502_1E (void)
626 /* Opcode $1E: ASL abs,x */
631 Addr = MemReadWord (Regs.PC+1) + Regs.XR;
632 Val = MemReadByte (Addr) << 1;
633 MemWriteByte (Addr, (unsigned char) Val);
634 TEST_ZF (Val & 0xFF);
636 SET_CF (Val & 0x100);
642 static void OPC_6502_20 (void)
643 /* Opcode $20: JSR */
647 Addr = MemReadWord (Regs.PC+1);
653 ParaVirtHooks (&Regs);
658 static void OPC_6502_21 (void)
659 /* Opcode $21: AND (zp,x) */
666 static void OPC_6502_24 (void)
667 /* Opcode $24: BIT zp */
669 unsigned char ZPAddr;
672 ZPAddr = MemReadByte (Regs.PC+1);
673 Val = MemReadByte (ZPAddr);
676 SET_ZF ((Val & Regs.AC) == 0);
682 static void OPC_6502_25 (void)
683 /* Opcode $25: AND zp */
690 static void OPC_6502_26 (void)
691 /* Opcode $26: ROL zp */
693 unsigned char ZPAddr;
696 ZPAddr = MemReadByte (Regs.PC+1);
697 Val = MemReadByte (ZPAddr);
699 MemWriteByte (ZPAddr, Val);
705 static void OPC_6502_28 (void)
706 /* Opcode $28: PLP */
710 /* Bits 5 and 4 aren't used, and always are 1! */
711 Regs.SR = (POP () | 0x30);
717 static void OPC_6502_29 (void)
718 /* Opcode $29: AND #imm */
725 static void OPC_6502_2A (void)
726 /* Opcode $2A: ROL a */
736 static void OPC_6502_2C (void)
737 /* Opcode $2C: BIT abs */
742 Addr = MemReadWord (Regs.PC+1);
743 Val = MemReadByte (Addr);
746 SET_ZF ((Val & Regs.AC) == 0);
752 static void OPC_6502_2D (void)
753 /* Opcode $2D: AND abs */
760 static void OPC_6502_2E (void)
761 /* Opcode $2E: ROL abs */
766 Addr = MemReadWord (Regs.PC+1);
767 Val = MemReadByte (Addr);
769 MemWriteByte (Addr, Val);
775 static void OPC_6502_30 (void)
776 /* Opcode $30: BMI */
783 static void OPC_6502_31 (void)
784 /* Opcode $31: AND (zp),y */
791 static void OPC_65SC02_32 (void)
792 /* Opcode $32: AND (zp) */
799 static void OPC_65SC02_34 (void)
800 /* Opcode $34: BIT zp,x */
802 unsigned char ZPAddr;
805 ZPAddr = MemReadByte (Regs.PC+1) + Regs.XR;
806 Val = MemReadByte (ZPAddr);
809 SET_ZF ((Val & Regs.AC) == 0);
815 static void OPC_6502_35 (void)
816 /* Opcode $35: AND zp,x */
823 static void OPC_6502_36 (void)
824 /* Opcode $36: ROL zp,x */
826 unsigned char ZPAddr;
829 ZPAddr = MemReadByte (Regs.PC+1) + Regs.XR;
830 Val = MemReadByte (ZPAddr);
832 MemWriteByte (ZPAddr, Val);
838 static void OPC_6502_38 (void)
839 /* Opcode $38: SEC */
848 static void OPC_6502_39 (void)
849 /* Opcode $39: AND abs,y */
856 static void OPC_65SC02_3A (void)
857 /* Opcode $3A: DEC a */
860 Regs.AC = (Regs.AC - 1) & 0xFF;
868 static void OPC_65SC02_3C (void)
869 /* Opcode $3C: BIT abs,x */
874 Addr = MemReadWord (Regs.PC+1);
875 if (PAGE_CROSS (Addr, Regs.XR))
877 Val = MemReadByte (Addr + Regs.XR);
880 SET_ZF ((Val & Regs.AC) == 0);
886 static void OPC_6502_3D (void)
887 /* Opcode $3D: AND abs,x */
894 static void OPC_6502_3E (void)
895 /* Opcode $3E: ROL abs,x */
900 Addr = MemReadWord (Regs.PC+1) + Regs.XR;
901 Val = MemReadByte (Addr);
903 MemWriteByte (Addr, Val);
909 static void OPC_6502_40 (void)
910 /* Opcode $40: RTI */
914 /* Bits 5 and 4 aren't used, and always are 1! */
915 Regs.SR = POP () | 0x30;
916 Regs.PC = POP (); /* PCL */
917 Regs.PC |= (POP () << 8); /* PCH */
922 static void OPC_6502_41 (void)
923 /* Opcode $41: EOR (zp,x) */
930 static void OPC_65C02_44 (void)
931 /* Opcode $44: 'zp' 3 cycle NOP */
939 static void OPC_6502_45 (void)
940 /* Opcode $45: EOR zp */
947 static void OPC_6502_46 (void)
948 /* Opcode $46: LSR zp */
950 unsigned char ZPAddr;
953 ZPAddr = MemReadByte (Regs.PC+1);
954 Val = MemReadByte (ZPAddr);
957 MemWriteByte (ZPAddr, Val);
965 static void OPC_6502_48 (void)
966 /* Opcode $48: PHA */
975 static void OPC_6502_49 (void)
976 /* Opcode $49: EOR #imm */
983 static void OPC_6502_4A (void)
984 /* Opcode $4A: LSR a */
987 SET_CF (Regs.AC & 0x01);
996 static void OPC_6502_4C (void)
997 /* Opcode $4C: JMP abs */
1000 Regs.PC = MemReadWord (Regs.PC+1);
1002 ParaVirtHooks (&Regs);
1007 static void OPC_6502_4D (void)
1008 /* Opcode $4D: EOR abs */
1015 static void OPC_6502_4E (void)
1016 /* Opcode $4E: LSR abs */
1021 Addr = MemReadWord (Regs.PC+1);
1022 Val = MemReadByte (Addr);
1023 SET_CF (Val & 0x01);
1025 MemWriteByte (Addr, Val);
1033 static void OPC_6502_50 (void)
1034 /* Opcode $50: BVC */
1036 BRANCH (!GET_OF ());
1041 static void OPC_6502_51 (void)
1042 /* Opcode $51: EOR (zp),y */
1049 static void OPC_65SC02_52 (void)
1050 /* Opcode $52: EOR (zp) */
1057 static void OPC_6502_55 (void)
1058 /* Opcode $55: EOR zp,x */
1065 static void OPC_6502_56 (void)
1066 /* Opcode $56: LSR zp,x */
1068 unsigned char ZPAddr;
1071 ZPAddr = MemReadByte (Regs.PC+1) + Regs.XR;
1072 Val = MemReadByte (ZPAddr);
1073 SET_CF (Val & 0x01);
1075 MemWriteByte (ZPAddr, Val);
1083 static void OPC_6502_58 (void)
1084 /* Opcode $58: CLI */
1093 static void OPC_6502_59 (void)
1094 /* Opcode $59: EOR abs,y */
1101 static void OPC_65SC02_5A (void)
1102 /* Opcode $5A: PHY */
1111 static void OPC_65C02_5C (void)
1112 /* Opcode $5C: 'Absolute' 8 cycle NOP */
1120 static void OPC_6502_5D (void)
1121 /* Opcode $5D: EOR abs,x */
1128 static void OPC_6502_5E (void)
1129 /* Opcode $5E: LSR abs,x */
1134 Addr = MemReadWord (Regs.PC+1) + Regs.XR;
1135 Val = MemReadByte (Addr);
1136 SET_CF (Val & 0x01);
1138 MemWriteByte (Addr, Val);
1146 static void OPC_6502_60 (void)
1147 /* Opcode $60: RTS */
1150 Regs.PC = POP (); /* PCL */
1151 Regs.PC |= (POP () << 8); /* PCH */
1157 static void OPC_6502_61 (void)
1158 /* Opcode $61: ADC (zp,x) */
1160 unsigned char ZPAddr;
1163 ZPAddr = MemReadByte (Regs.PC+1) + Regs.XR;
1164 Addr = MemReadZPWord (ZPAddr);
1165 ADC (MemReadByte (Addr));
1171 static void OPC_65SC02_64 (void)
1172 /* Opcode $64: STZ zp */
1174 unsigned char ZPAddr;
1176 ZPAddr = MemReadByte (Regs.PC+1);
1177 MemWriteByte (ZPAddr, 0);
1183 static void OPC_6502_65 (void)
1184 /* Opcode $65: ADC zp */
1186 unsigned char ZPAddr;
1188 ZPAddr = MemReadByte (Regs.PC+1);
1189 ADC (MemReadByte (ZPAddr));
1195 static void OPC_6502_66 (void)
1196 /* Opcode $66: ROR zp */
1198 unsigned char ZPAddr;
1201 ZPAddr = MemReadByte (Regs.PC+1);
1202 Val = MemReadByte (ZPAddr);
1204 MemWriteByte (ZPAddr, Val);
1210 static void OPC_6502_68 (void)
1211 /* Opcode $68: PLA */
1222 static void OPC_6502_69 (void)
1223 /* Opcode $69: ADC #imm */
1226 ADC (MemReadByte (Regs.PC+1));
1232 static void OPC_6502_6A (void)
1233 /* Opcode $6A: ROR a */
1242 static void OPC_6502_6C (void)
1243 /* Opcode $6C: JMP (ind) */
1245 unsigned PC, Lo, Hi;
1247 Lo = MemReadWord (PC+1);
1249 if (CPU == CPU_6502)
1251 /* Emulate the 6502 bug */
1253 Regs.PC = MemReadByte (Lo);
1254 Hi = (Lo & 0xFF00) | ((Lo + 1) & 0xFF);
1255 Regs.PC |= (MemReadByte (Hi) << 8);
1257 /* Output a warning if the bug is triggered */
1260 Warning ("6502 indirect jump bug triggered at $%04X, ind addr = $%04X",
1267 Regs.PC = MemReadWord(Lo);
1270 ParaVirtHooks (&Regs);
1275 static void OPC_65C02_6C (void)
1276 /* Opcode $6C: JMP (ind) */
1278 /* 6502 bug fixed here */
1280 Regs.PC = MemReadWord (MemReadWord (Regs.PC+1));
1282 ParaVirtHooks (&Regs);
1287 static void OPC_6502_6D (void)
1288 /* Opcode $6D: ADC abs */
1292 Addr = MemReadWord (Regs.PC+1);
1293 ADC (MemReadByte (Addr));
1299 static void OPC_6502_6E (void)
1300 /* Opcode $6E: ROR abs */
1305 Addr = MemReadWord (Regs.PC+1);
1306 Val = MemReadByte (Addr);
1308 MemWriteByte (Addr, Val);
1314 static void OPC_6502_70 (void)
1315 /* Opcode $70: BVS */
1322 static void OPC_6502_71 (void)
1323 /* Opcode $71: ADC (zp),y */
1325 unsigned char ZPAddr;
1328 ZPAddr = MemReadByte (Regs.PC+1);
1329 Addr = MemReadZPWord (ZPAddr);
1330 if (PAGE_CROSS (Addr, Regs.YR)) {
1333 ADC (MemReadByte (Addr + Regs.YR));
1339 static void OPC_65SC02_72 (void)
1340 /* Opcode $72: ADC (zp) */
1342 unsigned char ZPAddr;
1345 ZPAddr = MemReadByte (Regs.PC+1);
1346 Addr = MemReadZPWord (ZPAddr);
1347 ADC (MemReadByte (Addr));
1353 static void OPC_65SC02_74 (void)
1354 /* Opcode $74: STZ zp,x */
1356 unsigned char ZPAddr;
1358 ZPAddr = MemReadByte (Regs.PC+1) + Regs.XR;
1359 MemWriteByte (ZPAddr, 0);
1365 static void OPC_6502_75 (void)
1366 /* Opcode $75: ADC zp,x */
1368 unsigned char ZPAddr;
1370 ZPAddr = MemReadByte (Regs.PC+1) + Regs.XR;
1371 ADC (MemReadByte (ZPAddr));
1377 static void OPC_6502_76 (void)
1378 /* Opcode $76: ROR zp,x */
1380 unsigned char ZPAddr;
1383 ZPAddr = MemReadByte (Regs.PC+1) + Regs.XR;
1384 Val = MemReadByte (ZPAddr);
1386 MemWriteByte (ZPAddr, Val);
1392 static void OPC_6502_78 (void)
1393 /* Opcode $78: SEI */
1402 static void OPC_6502_79 (void)
1403 /* Opcode $79: ADC abs,y */
1407 Addr = MemReadWord (Regs.PC+1);
1408 if (PAGE_CROSS (Addr, Regs.YR)) {
1411 ADC (MemReadByte (Addr + Regs.YR));
1417 static void OPC_65SC02_7A (void)
1418 /* Opcode $7A: PLY */
1429 static void OPC_65SC02_7C (void)
1430 /* Opcode $7C: JMP (ind,X) */
1435 Adr = MemReadWord (PC+1);
1436 Regs.PC = MemReadWord(Adr+Regs.XR);
1438 ParaVirtHooks (&Regs);
1443 static void OPC_6502_7D (void)
1444 /* Opcode $7D: ADC abs,x */
1448 Addr = MemReadWord (Regs.PC+1);
1449 if (PAGE_CROSS (Addr, Regs.XR)) {
1452 ADC (MemReadByte (Addr + Regs.XR));
1458 static void OPC_6502_7E (void)
1459 /* Opcode $7E: ROR abs,x */
1464 Addr = MemReadWord (Regs.PC+1) + Regs.XR;
1465 Val = MemReadByte (Addr);
1467 MemWriteByte (Addr, Val);
1473 static void OPC_65SC02_80 (void)
1474 /* Opcode $80: BRA */
1481 static void OPC_6502_81 (void)
1482 /* Opcode $81: STA (zp,x) */
1484 unsigned char ZPAddr;
1487 ZPAddr = MemReadByte (Regs.PC+1) + Regs.XR;
1488 Addr = MemReadZPWord (ZPAddr);
1489 MemWriteByte (Addr, Regs.AC);
1495 static void OPC_6502_84 (void)
1496 /* Opcode $84: STY zp */
1498 unsigned char ZPAddr;
1500 ZPAddr = MemReadByte (Regs.PC+1);
1501 MemWriteByte (ZPAddr, Regs.YR);
1507 static void OPC_6502_85 (void)
1508 /* Opcode $85: STA zp */
1510 unsigned char ZPAddr;
1512 ZPAddr = MemReadByte (Regs.PC+1);
1513 MemWriteByte (ZPAddr, Regs.AC);
1519 static void OPC_6502_86 (void)
1520 /* Opcode $86: STX zp */
1522 unsigned char ZPAddr;
1524 ZPAddr = MemReadByte (Regs.PC+1);
1525 MemWriteByte (ZPAddr, Regs.XR);
1531 static void OPC_6502_88 (void)
1532 /* Opcode $88: DEY */
1535 Regs.YR = (Regs.YR - 1) & 0xFF;
1543 static void OPC_65SC02_89 (void)
1544 /* Opcode $89: BIT #imm */
1548 Val = MemReadByte (Regs.PC+1);
1549 SET_SF (Val & 0x80);
1550 SET_OF (Val & 0x40);
1551 SET_ZF ((Val & Regs.AC) == 0);
1557 static void OPC_6502_8A (void)
1558 /* Opcode $8A: TXA */
1569 static void OPC_6502_8C (void)
1570 /* Opcode $8C: STY abs */
1574 Addr = MemReadWord (Regs.PC+1);
1575 MemWriteByte (Addr, Regs.YR);
1581 static void OPC_6502_8D (void)
1582 /* Opcode $8D: STA abs */
1586 Addr = MemReadWord (Regs.PC+1);
1587 MemWriteByte (Addr, Regs.AC);
1593 static void OPC_6502_8E (void)
1594 /* Opcode $8E: STX abs */
1598 Addr = MemReadWord (Regs.PC+1);
1599 MemWriteByte (Addr, Regs.XR);
1605 static void OPC_6502_90 (void)
1606 /* Opcode $90: BCC */
1608 BRANCH (!GET_CF ());
1613 static void OPC_6502_91 (void)
1614 /* Opcode $91: sta (zp),y */
1616 unsigned char ZPAddr;
1619 ZPAddr = MemReadByte (Regs.PC+1);
1620 Addr = MemReadZPWord (ZPAddr) + Regs.YR;
1621 MemWriteByte (Addr, Regs.AC);
1627 static void OPC_65SC02_92 (void)
1628 /* Opcode $92: sta (zp) */
1630 unsigned char ZPAddr;
1633 ZPAddr = MemReadByte (Regs.PC+1);
1634 Addr = MemReadZPWord (ZPAddr);
1635 MemWriteByte (Addr, Regs.AC);
1641 static void OPC_6502_94 (void)
1642 /* Opcode $94: STY zp,x */
1644 unsigned char ZPAddr;
1646 ZPAddr = MemReadByte (Regs.PC+1) + Regs.XR;
1647 MemWriteByte (ZPAddr, Regs.YR);
1653 static void OPC_6502_95 (void)
1654 /* Opcode $95: STA zp,x */
1656 unsigned char ZPAddr;
1658 ZPAddr = MemReadByte (Regs.PC+1) + Regs.XR;
1659 MemWriteByte (ZPAddr, Regs.AC);
1665 static void OPC_6502_96 (void)
1666 /* Opcode $96: stx zp,y */
1668 unsigned char ZPAddr;
1670 ZPAddr = MemReadByte (Regs.PC+1) + Regs.YR;
1671 MemWriteByte (ZPAddr, Regs.XR);
1677 static void OPC_6502_98 (void)
1678 /* Opcode $98: TYA */
1689 static void OPC_6502_99 (void)
1690 /* Opcode $99: STA abs,y */
1694 Addr = MemReadWord (Regs.PC+1) + Regs.YR;
1695 MemWriteByte (Addr, Regs.AC);
1701 static void OPC_6502_9A (void)
1702 /* Opcode $9A: TXS */
1711 static void OPC_65SC02_9C (void)
1712 /* Opcode $9C: STZ abs */
1716 Addr = MemReadWord (Regs.PC+1);
1717 MemWriteByte (Addr, 0);
1723 static void OPC_6502_9D (void)
1724 /* Opcode $9D: STA abs,x */
1728 Addr = MemReadWord (Regs.PC+1) + Regs.XR;
1729 MemWriteByte (Addr, Regs.AC);
1735 static void OPC_65SC02_9E (void)
1736 /* Opcode $9E: STZ abs,x */
1740 Addr = MemReadWord (Regs.PC+1) + Regs.XR;
1741 MemWriteByte (Addr, 0);
1747 static void OPC_6502_A0 (void)
1748 /* Opcode $A0: LDY #imm */
1751 Regs.YR = MemReadByte (Regs.PC+1);
1759 static void OPC_6502_A1 (void)
1760 /* Opcode $A1: LDA (zp,x) */
1762 unsigned char ZPAddr;
1765 ZPAddr = MemReadByte (Regs.PC+1) + Regs.XR;
1766 Addr = MemReadZPWord (ZPAddr);
1767 Regs.AC = MemReadByte (Addr);
1775 static void OPC_6502_A2 (void)
1776 /* Opcode $A2: LDX #imm */
1779 Regs.XR = MemReadByte (Regs.PC+1);
1787 static void OPC_6502_A4 (void)
1788 /* Opcode $A4: LDY zp */
1790 unsigned char ZPAddr;
1792 ZPAddr = MemReadByte (Regs.PC+1);
1793 Regs.YR = MemReadByte (ZPAddr);
1801 static void OPC_6502_A5 (void)
1802 /* Opcode $A5: LDA zp */
1804 unsigned char ZPAddr;
1806 ZPAddr = MemReadByte (Regs.PC+1);
1807 Regs.AC = MemReadByte (ZPAddr);
1815 static void OPC_6502_A6 (void)
1816 /* Opcode $A6: LDX zp */
1818 unsigned char ZPAddr;
1820 ZPAddr = MemReadByte (Regs.PC+1);
1821 Regs.XR = MemReadByte (ZPAddr);
1829 static void OPC_6502_A8 (void)
1830 /* Opcode $A8: TAY */
1841 static void OPC_6502_A9 (void)
1842 /* Opcode $A9: LDA #imm */
1845 Regs.AC = MemReadByte (Regs.PC+1);
1853 static void OPC_6502_AA (void)
1854 /* Opcode $AA: TAX */
1865 static void OPC_6502_AC (void)
1866 /* Opcode $Regs.AC: LDY abs */
1870 Addr = MemReadWord (Regs.PC+1);
1871 Regs.YR = MemReadByte (Addr);
1879 static void OPC_6502_AD (void)
1880 /* Opcode $AD: LDA abs */
1884 Addr = MemReadWord (Regs.PC+1);
1885 Regs.AC = MemReadByte (Addr);
1893 static void OPC_6502_AE (void)
1894 /* Opcode $AE: LDX abs */
1898 Addr = MemReadWord (Regs.PC+1);
1899 Regs.XR = MemReadByte (Addr);
1907 static void OPC_6502_B0 (void)
1908 /* Opcode $B0: BCS */
1915 static void OPC_6502_B1 (void)
1916 /* Opcode $B1: LDA (zp),y */
1918 unsigned char ZPAddr;
1921 ZPAddr = MemReadByte (Regs.PC+1);
1922 Addr = MemReadZPWord (ZPAddr);
1923 if (PAGE_CROSS (Addr, Regs.YR)) {
1926 Regs.AC = MemReadByte (Addr + Regs.YR);
1934 static void OPC_65SC02_B2 (void)
1935 /* Opcode $B2: LDA (zp) */
1937 unsigned char ZPAddr;
1940 ZPAddr = MemReadByte (Regs.PC+1);
1941 Addr = MemReadZPWord (ZPAddr);
1942 Regs.AC = MemReadByte (Addr);
1950 static void OPC_6502_B4 (void)
1951 /* Opcode $B4: LDY zp,x */
1953 unsigned char ZPAddr;
1955 ZPAddr = MemReadByte (Regs.PC+1) + Regs.XR;
1956 Regs.YR = MemReadByte (ZPAddr);
1964 static void OPC_6502_B5 (void)
1965 /* Opcode $B5: LDA zp,x */
1967 unsigned char ZPAddr;
1969 ZPAddr = MemReadByte (Regs.PC+1) + Regs.XR;
1970 Regs.AC = MemReadByte (ZPAddr);
1978 static void OPC_6502_B6 (void)
1979 /* Opcode $B6: LDX zp,y */
1981 unsigned char ZPAddr;
1983 ZPAddr = MemReadByte (Regs.PC+1) + Regs.YR;
1984 Regs.XR = MemReadByte (ZPAddr);
1992 static void OPC_6502_B8 (void)
1993 /* Opcode $B8: CLV */
2002 static void OPC_6502_B9 (void)
2003 /* Opcode $B9: LDA abs,y */
2007 Addr = MemReadWord (Regs.PC+1);
2008 if (PAGE_CROSS (Addr, Regs.YR)) {
2011 Regs.AC = MemReadByte (Addr + Regs.YR);
2019 static void OPC_6502_BA (void)
2020 /* Opcode $BA: TSX */
2023 Regs.XR = Regs.SP & 0xFF;
2031 static void OPC_6502_BC (void)
2032 /* Opcode $BC: LDY abs,x */
2036 Addr = MemReadWord (Regs.PC+1);
2037 if (PAGE_CROSS (Addr, Regs.XR)) {
2040 Regs.YR = MemReadByte (Addr + Regs.XR);
2048 static void OPC_6502_BD (void)
2049 /* Opcode $BD: LDA abs,x */
2053 Addr = MemReadWord (Regs.PC+1);
2054 if (PAGE_CROSS (Addr, Regs.XR)) {
2057 Regs.AC = MemReadByte (Addr + Regs.XR);
2065 static void OPC_6502_BE (void)
2066 /* Opcode $BE: LDX abs,y */
2070 Addr = MemReadWord (Regs.PC+1);
2071 if (PAGE_CROSS (Addr, Regs.YR)) {
2074 Regs.XR = MemReadByte (Addr + Regs.YR);
2082 static void OPC_6502_C0 (void)
2083 /* Opcode $C0: CPY #imm */
2086 CMP (Regs.YR, MemReadByte (Regs.PC+1));
2092 static void OPC_6502_C1 (void)
2093 /* Opcode $C1: CMP (zp,x) */
2095 unsigned char ZPAddr;
2098 ZPAddr = MemReadByte (Regs.PC+1) + Regs.XR;
2099 Addr = MemReadZPWord (ZPAddr);
2100 CMP (Regs.AC, MemReadByte (Addr));
2106 static void OPC_6502_C4 (void)
2107 /* Opcode $C4: CPY zp */
2109 unsigned char ZPAddr;
2111 ZPAddr = MemReadByte (Regs.PC+1);
2112 CMP (Regs.YR, MemReadByte (ZPAddr));
2118 static void OPC_6502_C5 (void)
2119 /* Opcode $C5: CMP zp */
2121 unsigned char ZPAddr;
2123 ZPAddr = MemReadByte (Regs.PC+1);
2124 CMP (Regs.AC, MemReadByte (ZPAddr));
2130 static void OPC_6502_C6 (void)
2131 /* Opcode $C6: DEC zp */
2133 unsigned char ZPAddr;
2136 ZPAddr = MemReadByte (Regs.PC+1);
2137 Val = MemReadByte (ZPAddr) - 1;
2138 MemWriteByte (ZPAddr, Val);
2146 static void OPC_6502_C8 (void)
2147 /* Opcode $C8: INY */
2150 Regs.YR = (Regs.YR + 1) & 0xFF;
2158 static void OPC_6502_C9 (void)
2159 /* Opcode $C9: CMP #imm */
2162 CMP (Regs.AC, MemReadByte (Regs.PC+1));
2168 static void OPC_6502_CA (void)
2169 /* Opcode $CA: DEX */
2172 Regs.XR = (Regs.XR - 1) & 0xFF;
2180 static void OPC_6502_CC (void)
2181 /* Opcode $CC: CPY abs */
2185 Addr = MemReadWord (Regs.PC+1);
2186 CMP (Regs.YR, MemReadByte (Addr));
2192 static void OPC_6502_CD (void)
2193 /* Opcode $CD: CMP abs */
2197 Addr = MemReadWord (Regs.PC+1);
2198 CMP (Regs.AC, MemReadByte (Addr));
2204 static void OPC_6502_CE (void)
2205 /* Opcode $CE: DEC abs */
2210 Addr = MemReadWord (Regs.PC+1);
2211 Val = MemReadByte (Addr) - 1;
2212 MemWriteByte (Addr, Val);
2220 static void OPC_6502_D0 (void)
2221 /* Opcode $D0: BNE */
2223 BRANCH (!GET_ZF ());
2228 static void OPC_6502_D1 (void)
2229 /* Opcode $D1: CMP (zp),y */
2234 ZPAddr = MemReadByte (Regs.PC+1);
2235 Addr = MemReadWord (ZPAddr);
2236 if (PAGE_CROSS (Addr, Regs.YR)) {
2239 CMP (Regs.AC, MemReadByte (Addr + Regs.YR));
2245 static void OPC_65SC02_D2 (void)
2246 /* Opcode $D2: CMP (zp) */
2251 ZPAddr = MemReadByte (Regs.PC+1);
2252 Addr = MemReadWord (ZPAddr);
2253 CMP (Regs.AC, MemReadByte (Addr));
2259 static void OPC_6502_D5 (void)
2260 /* Opcode $D5: CMP zp,x */
2262 unsigned char ZPAddr;
2264 ZPAddr = MemReadByte (Regs.PC+1) + Regs.XR;
2265 CMP (Regs.AC, MemReadByte (ZPAddr));
2271 static void OPC_6502_D6 (void)
2272 /* Opcode $D6: DEC zp,x */
2274 unsigned char ZPAddr;
2277 ZPAddr = MemReadByte (Regs.PC+1) + Regs.XR;
2278 Val = MemReadByte (ZPAddr) - 1;
2279 MemWriteByte (ZPAddr, Val);
2287 static void OPC_6502_D8 (void)
2288 /* Opcode $D8: CLD */
2297 static void OPC_6502_D9 (void)
2298 /* Opcode $D9: CMP abs,y */
2302 Addr = MemReadWord (Regs.PC+1);
2303 if (PAGE_CROSS (Addr, Regs.YR)) {
2306 CMP (Regs.AC, MemReadByte (Addr + Regs.YR));
2312 static void OPC_65SC02_DA (void)
2313 /* Opcode $DA: PHX */
2322 static void OPC_6502_DD (void)
2323 /* Opcode $DD: CMP abs,x */
2327 Addr = MemReadWord (Regs.PC+1);
2328 if (PAGE_CROSS (Addr, Regs.XR)) {
2331 CMP (Regs.AC, MemReadByte (Addr + Regs.XR));
2337 static void OPC_6502_DE (void)
2338 /* Opcode $DE: DEC abs,x */
2343 Addr = MemReadWord (Regs.PC+1) + Regs.XR;
2344 Val = MemReadByte (Addr) - 1;
2345 MemWriteByte (Addr, Val);
2353 static void OPC_6502_E0 (void)
2354 /* Opcode $E0: CPX #imm */
2357 CMP (Regs.XR, MemReadByte (Regs.PC+1));
2363 static void OPC_6502_E1 (void)
2364 /* Opcode $E1: SBC (zp,x) */
2366 unsigned char ZPAddr;
2369 ZPAddr = MemReadByte (Regs.PC+1) + Regs.XR;
2370 Addr = MemReadZPWord (ZPAddr);
2371 SBC (MemReadByte (Addr));
2377 static void OPC_6502_E4 (void)
2378 /* Opcode $E4: CPX zp */
2380 unsigned char ZPAddr;
2382 ZPAddr = MemReadByte (Regs.PC+1);
2383 CMP (Regs.XR, MemReadByte (ZPAddr));
2389 static void OPC_6502_E5 (void)
2390 /* Opcode $E5: SBC zp */
2392 unsigned char ZPAddr;
2394 ZPAddr = MemReadByte (Regs.PC+1);
2395 SBC (MemReadByte (ZPAddr));
2401 static void OPC_6502_E6 (void)
2402 /* Opcode $E6: INC zp */
2404 unsigned char ZPAddr;
2407 ZPAddr = MemReadByte (Regs.PC+1);
2408 Val = MemReadByte (ZPAddr) + 1;
2409 MemWriteByte (ZPAddr, Val);
2417 static void OPC_6502_E8 (void)
2418 /* Opcode $E8: INX */
2421 Regs.XR = (Regs.XR + 1) & 0xFF;
2429 static void OPC_6502_E9 (void)
2430 /* Opcode $E9: SBC #imm */
2433 SBC (MemReadByte (Regs.PC+1));
2439 static void OPC_6502_EA (void)
2440 /* Opcode $EA: NOP */
2442 /* This one is easy... */
2449 static void OPC_65C02_NOP11(void)
2450 /* Opcode 'Illegal' 1 cycle NOP */
2458 static void OPC_65C02_NOP22 (void)
2459 /* Opcode 'Illegal' 2 byte 2 cycle NOP */
2467 static void OPC_65C02_NOP24 (void)
2468 /* Opcode 'Illegal' 2 byte 4 cycle NOP */
2476 static void OPC_65C02_NOP34 (void)
2477 /* Opcode 'Illegal' 3 byte 4 cycle NOP */
2485 static void OPC_6502_EC (void)
2486 /* Opcode $EC: CPX abs */
2490 Addr = MemReadWord (Regs.PC+1);
2491 CMP (Regs.XR, MemReadByte (Addr));
2497 static void OPC_6502_ED (void)
2498 /* Opcode $ED: SBC abs */
2502 Addr = MemReadWord (Regs.PC+1);
2503 SBC (MemReadByte (Addr));
2509 static void OPC_6502_EE (void)
2510 /* Opcode $EE: INC abs */
2515 Addr = MemReadWord (Regs.PC+1);
2516 Val = MemReadByte (Addr) + 1;
2517 MemWriteByte (Addr, Val);
2525 static void OPC_6502_F0 (void)
2526 /* Opcode $F0: BEQ */
2533 static void OPC_6502_F1 (void)
2534 /* Opcode $F1: SBC (zp),y */
2536 unsigned char ZPAddr;
2539 ZPAddr = MemReadByte (Regs.PC+1);
2540 Addr = MemReadZPWord (ZPAddr);
2541 if (PAGE_CROSS (Addr, Regs.YR)) {
2544 SBC (MemReadByte (Addr + Regs.YR));
2550 static void OPC_65SC02_F2 (void)
2551 /* Opcode $F2: SBC (zp) */
2553 unsigned char ZPAddr;
2556 ZPAddr = MemReadByte (Regs.PC+1);
2557 Addr = MemReadZPWord (ZPAddr);
2558 SBC (MemReadByte (Addr));
2564 static void OPC_6502_F5 (void)
2565 /* Opcode $F5: SBC zp,x */
2567 unsigned char ZPAddr;
2569 ZPAddr = MemReadByte (Regs.PC+1) + Regs.XR;
2570 SBC (MemReadByte (ZPAddr));
2576 static void OPC_6502_F6 (void)
2577 /* Opcode $F6: INC zp,x */
2579 unsigned char ZPAddr;
2582 ZPAddr = MemReadByte (Regs.PC+1) + Regs.XR;
2583 Val = MemReadByte (ZPAddr) + 1;
2584 MemWriteByte (ZPAddr, Val);
2592 static void OPC_6502_F8 (void)
2593 /* Opcode $F8: SED */
2602 static void OPC_6502_F9 (void)
2603 /* Opcode $F9: SBC abs,y */
2607 Addr = MemReadWord (Regs.PC+1);
2608 if (PAGE_CROSS (Addr, Regs.YR)) {
2611 SBC (MemReadByte (Addr + Regs.YR));
2617 static void OPC_65SC02_FA (void)
2618 /* Opcode $7A: PLX */
2629 static void OPC_6502_FD (void)
2630 /* Opcode $FD: SBC abs,x */
2634 Addr = MemReadWord (Regs.PC+1);
2635 if (PAGE_CROSS (Addr, Regs.XR)) {
2638 SBC (MemReadByte (Addr + Regs.XR));
2644 static void OPC_6502_FE (void)
2645 /* Opcode $FE: INC abs,x */
2650 Addr = MemReadWord (Regs.PC+1) + Regs.XR;
2651 Val = MemReadByte (Addr) + 1;
2652 MemWriteByte (Addr, Val);
2660 /*****************************************************************************/
2661 /* Opcode handler tables */
2662 /*****************************************************************************/
2666 /* Opcode handler table for the 6502 */
2667 static const OPFunc OP6502Table[256] = {
2928 /* Opcode handler table for the 65C02 */
2929 static const OPFunc OP65C02Table[256] = {
2932 OPC_65C02_NOP22, // $02
2933 OPC_65C02_NOP11, // $03
2937 OPC_Illegal, // $07: RMB0 currently unsupported
2941 OPC_65C02_NOP11, // $0B
2945 OPC_Illegal, // $0F: BBR0 currently unsupported
2949 OPC_65C02_NOP11, // $13
2953 OPC_Illegal, // $17: RMB1 currently unsupported
2957 OPC_65C02_NOP11, // $1B
2961 OPC_Illegal, // $1F: BBR1 currently unsupported
2964 OPC_65C02_NOP22, // $22
2965 OPC_65C02_NOP11, // $23
2969 OPC_Illegal, // $27: RMB2 currently unsupported
2973 OPC_65C02_NOP11, // $2B
2977 OPC_Illegal, // $2F: BBR2 currently unsupported
2981 OPC_65C02_NOP11, // $33
2985 OPC_Illegal, // $37: RMB3 currently unsupported
2989 OPC_65C02_NOP11, // $3B
2993 OPC_Illegal, // $3F: BBR3 currently unsupported
2996 OPC_65C02_NOP22, // $42
2997 OPC_65C02_NOP11, // $43
2998 OPC_65C02_44, // $44
3001 OPC_Illegal, // $47: RMB4 currently unsupported
3005 OPC_65C02_NOP11, // $4B
3009 OPC_Illegal, // $4F: BBR4 currently unsupported
3013 OPC_65C02_NOP11, // $53
3014 OPC_65C02_NOP24, // $54
3017 OPC_Illegal, // $57: RMB5 currently unsupported
3021 OPC_65C02_NOP11, // $5B
3025 OPC_Illegal, // $5F: BBR5 currently unsupported
3028 OPC_65C02_NOP22, // $62
3029 OPC_65C02_NOP11, // $63
3033 OPC_Illegal, // $67: RMB6 currently unsupported
3037 OPC_65C02_NOP11, // $6B
3041 OPC_Illegal, // $6F: BBR6 currently unsupported
3045 OPC_65C02_NOP11, // $73
3049 OPC_Illegal, // $77: RMB7 currently unsupported
3053 OPC_65C02_NOP11, // $7B
3057 OPC_Illegal, // $7F: BBR7 currently unsupported
3060 OPC_65C02_NOP22, // $82
3061 OPC_65C02_NOP11, // $83
3065 OPC_Illegal, // $87: SMB0 currently unsupported
3069 OPC_65C02_NOP11, // $8B
3073 OPC_Illegal, // $8F: BBS0 currently unsupported
3077 OPC_65C02_NOP11, // $93
3081 OPC_Illegal, // $97: SMB1 currently unsupported
3085 OPC_65C02_NOP11, // $9B
3089 OPC_Illegal, // $9F: BBS1 currently unsupported
3093 OPC_65C02_NOP11, // $A3
3097 OPC_Illegal, // $A7: SMB2 currently unsupported
3101 OPC_65C02_NOP11, // $AB
3105 OPC_Illegal, // $AF: BBS2 currently unsupported
3109 OPC_65C02_NOP11, // $B3
3113 OPC_Illegal, // $B7: SMB3 currently unsupported
3117 OPC_65C02_NOP11, // $BB
3121 OPC_Illegal, // $BF: BBS3 currently unsupported
3124 OPC_65C02_NOP22, // $C2
3125 OPC_65C02_NOP11, // $C3
3129 OPC_Illegal, // $C7: SMB4 currently unsupported
3133 OPC_Illegal, // $CB: WAI currently unsupported
3137 OPC_Illegal, // $CF: BBS4 currently unsupported
3141 OPC_65C02_NOP11, // $D3
3142 OPC_65C02_NOP24, // $D4
3145 OPC_Illegal, // $D7: SMB5 currently unsupported
3149 OPC_Illegal, // $DB: STP currently unsupported
3150 OPC_65C02_NOP34, // $DC
3153 OPC_Illegal, // $DF: BBS5 currently unsupported
3156 OPC_65C02_NOP22, // $E2
3157 OPC_65C02_NOP11, // $E3
3161 OPC_Illegal, // $E7: SMB6 currently unsupported
3165 OPC_65C02_NOP11, // $EB
3169 OPC_Illegal, // $EF: BBS6 currently unsupported
3173 OPC_65C02_NOP11, // $F3
3174 OPC_65C02_NOP24, // $F4
3177 OPC_Illegal, // $F7: SMB7 currently unsupported
3181 OPC_65C02_NOP11, // $FB
3182 OPC_65C02_NOP34, // $FC
3185 OPC_Illegal, // $FF: BBS7 currently unsupported
3190 /* Tables with opcode handlers */
3191 static const OPFunc* Handlers[2] = {OP6502Table, OP65C02Table};
3195 /*****************************************************************************/
3197 /*****************************************************************************/
3201 void IRQRequest (void)
3202 /* Generate an IRQ */
3204 /* Remember the request */
3210 void NMIRequest (void)
3211 /* Generate an NMI */
3213 /* Remember the request */
3220 /* Generate a CPU RESET */
3226 /* Bits 5 and 4 aren't used, and always are 1! */
3228 Regs.PC = MemReadWord (0xFFFC);
3233 unsigned ExecuteInsn (void)
3234 /* Execute one CPU instruction */
3236 /* If we have an NMI request, handle it */
3237 if (HaveNMIRequest) {
3242 PUSH (Regs.SR & ~BF);
3244 if (CPU != CPU_6502)
3248 Regs.PC = MemReadWord (0xFFFA);
3251 } else if (HaveIRQRequest && GET_IF () == 0) {
3256 PUSH (Regs.SR & ~BF);
3258 if (CPU != CPU_6502)
3262 Regs.PC = MemReadWord (0xFFFE);
3267 /* Normal instruction - read the next opcode */
3268 unsigned char OPC = MemReadByte (Regs.PC);
3271 Handlers[CPU][OPC] ();
3275 TotalCycles += Cycles;
3277 /* Return the number of clock cycles needed by this insn */
3283 unsigned long GetCycles (void)
3284 /* Return the total number of cycles executed */
3286 /* Return the total number of cycles */