]> git.sur5r.net Git - cc65/blob - src/sim65/6502.c
Merge pull request #504 from jedeoric/master
[cc65] / src / sim65 / 6502.c
1 /*****************************************************************************/
2 /*                                                                           */
3 /*                                  6502.c                                   */
4 /*                                                                           */
5 /*                           CPU core for the 6502                           */
6 /*                                                                           */
7 /*                                                                           */
8 /*                                                                           */
9 /* (C) 2003-2012, Ullrich von Bassewitz                                      */
10 /*                Roemerstrasse 52                                           */
11 /*                D-70794 Filderstadt                                        */
12 /* EMail:         uz@cc65.org                                                */
13 /*                                                                           */
14 /* Mar-2017, Christian Krueger, added support for 65SC02                     */
15 /*                                                                           */
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.                                    */
19 /*                                                                           */
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:                            */
23 /*                                                                           */
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              */
31 /*    distribution.                                                          */
32 /*                                                                           */
33 /*****************************************************************************/
34
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
39    6502)
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)
42 */
43
44 #include "memory.h"
45 #include "error.h"
46 #include "6502.h"
47 #include "paravirt.h"
48
49
50
51 /*****************************************************************************/
52 /*                                   Data                                    */
53 /*****************************************************************************/
54
55
56
57 /* Current CPU */
58 CPUType CPU;
59
60 /* Type of an opcode handler function */
61 typedef void (*OPFunc) (void);
62
63 /* The CPU registers */
64 static CPURegs Regs;
65
66 /* Cycles for the current insn */
67 static unsigned Cycles;
68
69 /* Total number of CPU cycles exec'd */
70 static unsigned long TotalCycles;
71
72 /* NMI request active */
73 static unsigned HaveNMIRequest;
74
75 /* IRQ request active */
76 static unsigned HaveIRQRequest;
77
78 /* flag to print cycles at program termination */
79 int PrintCycles;
80
81
82 /*****************************************************************************/
83 /*                        Helper functions and macros                        */
84 /*****************************************************************************/
85
86
87
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)
95
96 /* Set the flags. The parameter is a boolean flag that says if the flag should be
97 ** set or reset.
98 */
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)
105
106 /* Special test and set macros. The meaning of the parameter depends on the
107 ** actual flag that should be set or reset.
108 */
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)
112
113 /* Program counter halves */
114 #define PCL             (Regs.PC & 0xFF)
115 #define PCH             ((Regs.PC >> 8) & 0xFF)
116
117 /* Stack operations */
118 #define PUSH(Val)       MemWriteByte (0x0100 | (Regs.SP-- & 0xFF), Val)
119 #define POP()           MemReadByte (0x0100 | (++Regs.SP & 0xFF))
120
121 /* Test for page cross */
122 #define PAGE_CROSS(addr,offs)   ((((addr) & 0xFF) + offs) >= 0x100)
123
124 /* #imm */
125 #define AC_OP_IMM(op)                                           \
126     Cycles = 2;                                                 \
127     Regs.AC = Regs.AC op MemReadByte (Regs.PC+1);               \
128     TEST_ZF (Regs.AC);                                          \
129     TEST_SF (Regs.AC);                                          \
130     Regs.PC += 2
131
132 /* zp */
133 #define AC_OP_ZP(op)                                            \
134     Cycles = 3;                                                 \
135     Regs.AC = Regs.AC op MemReadByte (MemReadByte (Regs.PC+1)); \
136     TEST_ZF (Regs.AC);                                          \
137     TEST_SF (Regs.AC);                                          \
138     Regs.PC += 2
139
140 /* zp,x */
141 #define AC_OP_ZPX(op)                                           \
142     unsigned char ZPAddr;                                       \
143     Cycles = 4;                                                 \
144     ZPAddr = MemReadByte (Regs.PC+1) + Regs.XR;                 \
145     Regs.AC = Regs.AC op MemReadByte (ZPAddr);                  \
146     TEST_ZF (Regs.AC);                                          \
147     TEST_SF (Regs.AC);                                          \
148     Regs.PC += 2
149
150 /* zp,y */
151 #define AC_OP_ZPY(op)                                           \
152     unsigned char ZPAddr;                                       \
153     Cycles = 4;                                                 \
154     ZPAddr = MemReadByte (Regs.PC+1) + Regs.YR;                 \
155     Regs.AC = Regs.AC op MemReadByte (ZPAddr);                  \
156     TEST_ZF (Regs.AC);                                          \
157     TEST_SF (Regs.AC);                                          \
158     Regs.PC += 2
159
160 /* abs */
161 #define AC_OP_ABS(op)                                           \
162     unsigned Addr;                                              \
163     Cycles = 4;                                                 \
164     Addr = MemReadWord (Regs.PC+1);                             \
165     Regs.AC = Regs.AC op MemReadByte (Addr);                    \
166     TEST_ZF (Regs.AC);                                          \
167     TEST_SF (Regs.AC);                                          \
168     Regs.PC += 3
169
170 /* abs,x */
171 #define AC_OP_ABSX(op)                                          \
172     unsigned Addr;                                              \
173     Cycles = 4;                                                 \
174     Addr = MemReadWord (Regs.PC+1);                             \
175     if (PAGE_CROSS (Addr, Regs.XR)) {                           \
176         ++Cycles;                                               \
177     }                                                           \
178     Regs.AC = Regs.AC op MemReadByte (Addr + Regs.XR);          \
179     TEST_ZF (Regs.AC);                                          \
180     TEST_SF (Regs.AC);                                          \
181     Regs.PC += 3
182
183 /* abs,y */
184 #define AC_OP_ABSY(op)                                          \
185     unsigned Addr;                                              \
186     Cycles = 4;                                                 \
187     Addr = MemReadWord (Regs.PC+1);                             \
188     if (PAGE_CROSS (Addr, Regs.YR)) {                           \
189         ++Cycles;                                               \
190     }                                                           \
191     Regs.AC = Regs.AC op MemReadByte (Addr + Regs.YR);          \
192     TEST_ZF (Regs.AC);                                          \
193     TEST_SF (Regs.AC);                                          \
194     Regs.PC += 3
195
196 /* (zp,x) */
197 #define AC_OP_ZPXIND(op)                                        \
198     unsigned char ZPAddr;                                       \
199     unsigned Addr;                                              \
200     Cycles = 6;                                                 \
201     ZPAddr = MemReadByte (Regs.PC+1) + Regs.XR;                 \
202     Addr = MemReadZPWord (ZPAddr);                              \
203     Regs.AC = Regs.AC op MemReadByte (Addr);                    \
204     TEST_ZF (Regs.AC);                                          \
205     TEST_SF (Regs.AC);                                          \
206     Regs.PC += 2
207
208 /* (zp),y */
209 #define AC_OP_ZPINDY(op)                                        \
210     unsigned char ZPAddr;                                       \
211     unsigned Addr;                                              \
212     Cycles = 5;                                                 \
213     ZPAddr = MemReadByte (Regs.PC+1);                           \
214     Addr = MemReadZPWord (ZPAddr);                              \
215     if (PAGE_CROSS (Addr, Regs.YR)) {                           \
216         ++Cycles;                                               \
217     }                                                           \
218     Addr += Regs.YR;                                            \
219     Regs.AC = Regs.AC op MemReadByte (Addr);                    \
220     TEST_ZF (Regs.AC);                                          \
221     TEST_SF (Regs.AC);                                          \
222     Regs.PC += 2
223
224 /* (zp) */
225 #define AC_OP_ZPIND(op)                                         \
226     unsigned char ZPAddr;                                       \
227     unsigned Addr;                                              \
228     Cycles = 5;                                                 \
229     ZPAddr = MemReadByte (Regs.PC+1);                           \
230     Addr = MemReadZPWord (ZPAddr);                              \
231     Regs.AC = Regs.AC op MemReadByte (Addr);                    \
232     TEST_ZF (Regs.AC);                                          \
233     TEST_SF (Regs.AC);                                          \
234     Regs.PC += 2
235
236 /* ADC */
237 #define ADC(v)                                                  \
238     do {                                                        \
239         unsigned old = Regs.AC;                                 \
240         unsigned rhs = (v & 0xFF);                              \
241         if (GET_DF ()) {                                        \
242             unsigned lo;                                        \
243             int res;                                            \
244             lo = (old & 0x0F) + (rhs & 0x0F) + GET_CF ();       \
245             if (lo >= 0x0A) {                                   \
246                 lo = ((lo + 0x06) & 0x0F) + 0x10;               \
247             }                                                   \
248             Regs.AC = (old & 0xF0) + (rhs & 0xF0) + lo;         \
249             res = (signed char)(old & 0xF0) +                   \
250                   (signed char)(rhs & 0xF0) +                   \
251                   (signed char)lo;                              \
252             TEST_ZF (old + rhs + GET_CF ());                    \
253             TEST_SF (Regs.AC);                                  \
254             if (Regs.AC >= 0xA0) {                              \
255                 Regs.AC += 0x60;                                \
256             }                                                   \
257             TEST_CF (Regs.AC);                                  \
258             SET_OF ((res < -128) || (res > 127));               \
259             if (CPU != CPU_6502) {                              \
260                 ++Cycles;                                       \
261             }                                                   \
262         } else {                                                \
263             Regs.AC += rhs + GET_CF ();                         \
264             TEST_ZF (Regs.AC);                                  \
265             TEST_SF (Regs.AC);                                  \
266             TEST_CF (Regs.AC);                                  \
267             SET_OF (!((old ^ rhs) & 0x80) &&                    \
268                     ((old ^ Regs.AC) & 0x80));                  \
269             Regs.AC &= 0xFF;                                    \
270         }                                                       \
271     } while (0)
272
273 /* branches */
274 #define BRANCH(cond)                                            \
275     Cycles = 2;                                                 \
276     if (cond) {                                                 \
277         signed char Offs;                                       \
278         unsigned char OldPCH;                                   \
279         ++Cycles;                                               \
280         Offs = (signed char) MemReadByte (Regs.PC+1);           \
281         OldPCH = PCH;                                           \
282         Regs.PC += 2 + (int) Offs;                              \
283         if (PCH != OldPCH) {                                    \
284             ++Cycles;                                           \
285         }                                                       \
286     } else {                                                    \
287         Regs.PC += 2;                                           \
288     }
289
290 /* compares */
291 #define CMP(v1, v2)                                             \
292     do {                                                        \
293         unsigned Result = v1 - v2;                              \
294         TEST_ZF (Result & 0xFF);                                \
295         TEST_SF (Result);                                       \
296         SET_CF (Result <= 0xFF);                                \
297     } while (0)
298
299
300 /* ROL */
301 #define ROL(Val)                                                \
302     Val <<= 1;                                                  \
303     if (GET_CF ()) {                                            \
304         Val |= 0x01;                                            \
305     }                                                           \
306     TEST_ZF (Val);                                              \
307     TEST_SF (Val);                                              \
308     TEST_CF (Val)
309
310 /* ROR */
311 #define ROR(Val)                                                \
312     if (GET_CF ()) {                                            \
313         Val |= 0x100;                                           \
314     }                                                           \
315     SET_CF (Val & 0x01);                                        \
316     Val >>= 1;                                                  \
317     TEST_ZF (Val);                                              \
318     TEST_SF (Val)
319
320 /* SBC */
321 #define SBC(v)                                                  \
322     do {                                                        \
323         unsigned old = Regs.AC;                                 \
324         unsigned rhs = (v & 0xFF);                              \
325         if (GET_DF ()) {                                        \
326             unsigned lo;                                        \
327             int res;                                            \
328             lo = (old & 0x0F) - (rhs & 0x0F) + GET_CF () - 1;   \
329             if (lo & 0x80) {                                    \
330                 lo = ((lo - 0x06) & 0x0F) - 0x10;               \
331             }                                                   \
332             Regs.AC = (old & 0xF0) - (rhs & 0xF0) + lo;         \
333             if (Regs.AC & 0x80) {                               \
334                 Regs.AC -= 0x60;                                \
335             }                                                   \
336             res = Regs.AC - rhs + (!GET_CF ());                 \
337             TEST_ZF (res);                                      \
338             TEST_SF (res);                                      \
339             SET_CF (res <= 0xFF);                               \
340             SET_OF (((old^rhs) & (old^res) & 0x80));            \
341             if (CPU != CPU_6502) {                              \
342                 ++Cycles;                                       \
343             }                                                   \
344         } else {                                                \
345             Regs.AC -= rhs + (!GET_CF ());                      \
346             TEST_ZF (Regs.AC);                                  \
347             TEST_SF (Regs.AC);                                  \
348             SET_CF (Regs.AC <= 0xFF);                           \
349             SET_OF (((old^rhs) & (old^Regs.AC) & 0x80));        \
350             Regs.AC &= 0xFF;                                    \
351         }                                                       \
352     } while (0)
353
354
355
356 /*****************************************************************************/
357 /*                         Opcode handling functions                         */
358 /*****************************************************************************/
359
360
361
362 static void OPC_Illegal (void)
363 {
364     Error ("Illegal opcode $%02X at address $%04X",
365            MemReadByte (Regs.PC), Regs.PC);
366 }
367
368
369
370 static void OPC_6502_00 (void)
371 /* Opcode $00: BRK */
372 {
373     Cycles = 7;
374     Regs.PC += 2;
375     PUSH (PCH);
376     PUSH (PCL);
377     PUSH (Regs.SR);
378     SET_IF (1);
379     if (CPU != CPU_6502)
380     {
381         SET_DF (0);
382     }
383     Regs.PC = MemReadWord (0xFFFE);
384 }
385
386
387
388 static void OPC_6502_01 (void)
389 /* Opcode $01: ORA (ind,x) */
390 {
391     AC_OP_ZPXIND (|);
392 }
393
394
395
396 static void OPC_65SC02_04 (void)
397 /* Opcode $04: TSB zp */
398 {
399     unsigned char ZPAddr;
400     unsigned char Val;
401     Cycles = 5;
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));
406     Regs.PC += 2;
407 }
408
409
410
411 static void OPC_6502_05 (void)
412 /* Opcode $05: ORA zp */
413 {
414     AC_OP_ZP (|);
415 }
416
417
418
419 static void OPC_6502_06 (void)
420 /* Opcode $06: ASL zp */
421 {
422     unsigned char ZPAddr;
423     unsigned Val;
424     Cycles = 5;
425     ZPAddr = MemReadByte (Regs.PC+1);
426     Val = MemReadByte (ZPAddr) << 1;
427     MemWriteByte (ZPAddr, (unsigned char) Val);
428     TEST_ZF (Val & 0xFF);
429     TEST_SF (Val);
430     SET_CF (Val & 0x100);
431     Regs.PC += 2;
432 }
433
434
435
436 static void OPC_6502_08 (void)
437 /* Opcode $08: PHP */
438 {
439     Cycles = 3;
440     PUSH (Regs.SR);
441     Regs.PC += 1;
442 }
443
444
445
446 static void OPC_6502_09 (void)
447 /* Opcode $09: ORA #imm */
448 {
449     AC_OP_IMM (|);
450 }
451
452
453
454 static void OPC_6502_0A (void)
455 /* Opcode $0A: ASL a */
456 {
457     Cycles = 2;
458     Regs.AC <<= 1;
459     TEST_ZF (Regs.AC & 0xFF);
460     TEST_SF (Regs.AC);
461     SET_CF (Regs.AC & 0x100);
462     Regs.AC &= 0xFF;
463     Regs.PC += 1;
464 }
465
466
467
468 static void OPC_65SC02_0C (void)
469 /* Opcode $0C: TSB abs */
470 {
471     unsigned Addr;
472     unsigned char Val;
473     Cycles = 6;
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));
478     Regs.PC += 3;
479 }
480
481
482
483 static void OPC_6502_0D (void)
484 /* Opcode $0D: ORA abs */
485 {
486     AC_OP_ABS (|);
487 }
488
489
490
491 static void OPC_6502_0E (void)
492 /* Opcode $0E: ALS abs */
493 {
494     unsigned Addr;
495     unsigned Val;
496     Cycles = 6;
497     Addr = MemReadWord (Regs.PC+1);
498     Val = MemReadByte (Addr) << 1;
499     MemWriteByte (Addr, (unsigned char) Val);
500     TEST_ZF (Val & 0xFF);
501     TEST_SF (Val);
502     SET_CF (Val & 0x100);
503     Regs.PC += 3;
504 }
505
506
507
508 static void OPC_6502_10 (void)
509 /* Opcode $10: BPL */
510 {
511     BRANCH (!GET_SF ());
512 }
513
514
515
516 static void OPC_6502_11 (void)
517 /* Opcode $11: ORA (zp),y */
518 {
519     AC_OP_ZPINDY (|);
520 }
521
522
523
524 static void OPC_65SC02_12 (void)
525 /* Opcode $12: ORA (zp) */
526 {
527     AC_OP_ZPIND (|);
528 }
529
530
531
532 static void OPC_65SC02_14 (void)
533 /* Opcode $14: TRB zp */
534 {
535     unsigned char ZPAddr;
536     unsigned char Val;
537     Cycles = 5;
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));
542     Regs.PC += 2;
543 }
544
545
546
547 static void OPC_6502_15 (void)
548 /* Opcode $15: ORA zp,x */
549 {
550    AC_OP_ZPX (|);
551 }
552
553
554
555 static void OPC_6502_16 (void)
556 /* Opcode $16: ASL zp,x */
557 {
558     unsigned char ZPAddr;
559     unsigned Val;
560     Cycles = 6;
561     ZPAddr = MemReadByte (Regs.PC+1) + Regs.XR;
562     Val = MemReadByte (ZPAddr) << 1;
563     MemWriteByte (ZPAddr, (unsigned char) Val);
564     TEST_ZF (Val & 0xFF);
565     TEST_SF (Val);
566     SET_CF (Val & 0x100);
567     Regs.PC += 2;
568 }
569
570
571
572 static void OPC_6502_18 (void)
573 /* Opcode $18: CLC */
574 {
575     Cycles = 2;
576     SET_CF (0);
577     Regs.PC += 1;
578 }
579
580
581
582 static void OPC_6502_19 (void)
583 /* Opcode $19: ORA abs,y */
584 {
585     AC_OP_ABSY (|);
586 }
587
588
589
590 static void OPC_65SC02_1A (void)
591 /* Opcode $1A: INC a */
592 {
593     Cycles = 2;
594     Regs.AC = (Regs.AC + 1) & 0xFF;
595     TEST_ZF (Regs.AC);
596     TEST_SF (Regs.AC);
597     Regs.PC += 1;
598 }
599
600
601
602 static void OPC_65SC02_1C (void)
603 /* Opcode $1C: TRB abs */
604 {
605     unsigned Addr;
606     unsigned char Val;
607     Cycles = 6;
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));
612     Regs.PC += 3;
613 }
614
615
616
617 static void OPC_6502_1D (void)
618 /* Opcode $1D: ORA abs,x */
619 {
620     AC_OP_ABSX (|);
621 }
622
623
624
625 static void OPC_6502_1E (void)
626 /* Opcode $1E: ASL abs,x */
627 {
628     unsigned Addr;
629     unsigned Val;
630     Cycles = 7;
631     Addr = MemReadWord (Regs.PC+1) + Regs.XR;
632     Val = MemReadByte (Addr) << 1;
633     MemWriteByte (Addr, (unsigned char) Val);
634     TEST_ZF (Val & 0xFF);
635     TEST_SF (Val);
636     SET_CF (Val & 0x100);
637     Regs.PC += 3;
638 }
639
640
641
642 static void OPC_6502_20 (void)
643 /* Opcode $20: JSR */
644 {
645     unsigned Addr;
646     Cycles = 6;
647     Addr = MemReadWord (Regs.PC+1);
648     Regs.PC += 2;
649     PUSH (PCH);
650     PUSH (PCL);
651     Regs.PC = Addr;
652
653     ParaVirtHooks (&Regs);
654 }
655
656
657
658 static void OPC_6502_21 (void)
659 /* Opcode $21: AND (zp,x) */
660 {
661     AC_OP_ZPXIND (&);
662 }
663
664
665
666 static void OPC_6502_24 (void)
667 /* Opcode $24: BIT zp */
668 {
669     unsigned char ZPAddr;
670     unsigned char Val;
671     Cycles = 3;
672     ZPAddr = MemReadByte (Regs.PC+1);
673     Val = MemReadByte (ZPAddr);
674     SET_SF (Val & 0x80);
675     SET_OF (Val & 0x40);
676     SET_ZF ((Val & Regs.AC) == 0);
677     Regs.PC += 2;
678 }
679
680
681
682 static void OPC_6502_25 (void)
683 /* Opcode $25: AND zp */
684 {
685     AC_OP_ZP (&);
686 }
687
688
689
690 static void OPC_6502_26 (void)
691 /* Opcode $26: ROL zp */
692 {
693     unsigned char ZPAddr;
694     unsigned Val;
695     Cycles = 5;
696     ZPAddr = MemReadByte (Regs.PC+1);
697     Val = MemReadByte (ZPAddr);
698     ROL (Val);
699     MemWriteByte (ZPAddr, Val);
700     Regs.PC += 2;
701 }
702
703
704
705 static void OPC_6502_28 (void)
706 /* Opcode $28: PLP */
707 {
708     Cycles = 4;
709
710     /* Bits 5 and 4 aren't used, and always are 1! */
711     Regs.SR = (POP () | 0x30);
712     Regs.PC += 1;
713 }
714
715
716
717 static void OPC_6502_29 (void)
718 /* Opcode $29: AND #imm */
719 {
720     AC_OP_IMM (&);
721 }
722
723
724
725 static void OPC_6502_2A (void)
726 /* Opcode $2A: ROL a */
727 {
728     Cycles = 2;
729     ROL (Regs.AC);
730     Regs.AC &= 0xFF;
731     Regs.PC += 1;
732 }
733
734
735
736 static void OPC_6502_2C (void)
737 /* Opcode $2C: BIT abs */
738 {
739     unsigned Addr;
740     unsigned char Val;
741     Cycles = 4;
742     Addr = MemReadByte (Regs.PC+1);
743     Val = MemReadByte (Addr);
744     SET_SF (Val & 0x80);
745     SET_OF (Val & 0x40);
746     SET_ZF ((Val & Regs.AC) == 0);
747     Regs.PC += 3;
748 }
749
750
751
752 static void OPC_6502_2D (void)
753 /* Opcode $2D: AND abs */
754 {
755     AC_OP_ABS (&);
756 }
757
758
759
760 static void OPC_6502_2E (void)
761 /* Opcode $2E: ROL abs */
762 {
763     unsigned Addr;
764     unsigned Val;
765     Cycles = 6;
766     Addr = MemReadWord (Regs.PC+1);
767     Val = MemReadByte (Addr);
768     ROL (Val);
769     MemWriteByte (Addr, Val);
770     Regs.PC += 3;
771 }
772
773
774
775 static void OPC_6502_30 (void)
776 /* Opcode $30: BMI */
777 {
778     BRANCH (GET_SF ());
779 }
780
781
782
783 static void OPC_6502_31 (void)
784 /* Opcode $31: AND (zp),y */
785 {
786     AC_OP_ZPINDY (&);
787 }
788
789
790
791 static void OPC_65SC02_32 (void)
792 /* Opcode $32: AND (zp) */
793 {
794     AC_OP_ZPIND (&);
795 }
796
797
798
799 static void OPC_65SC02_34 (void)
800 /* Opcode $34: BIT zp,x */
801 {
802     unsigned char ZPAddr;
803     unsigned char Val;
804     Cycles = 4;
805     ZPAddr = MemReadByte (Regs.PC+1) + Regs.XR;
806     Val = MemReadByte (ZPAddr);
807     SET_SF (Val & 0x80);
808     SET_OF (Val & 0x40);
809     SET_ZF ((Val & Regs.AC) == 0);
810     Regs.PC += 2;
811 }
812
813
814
815 static void OPC_6502_35 (void)
816 /* Opcode $35: AND zp,x */
817 {
818     AC_OP_ZPX (&);
819 }
820
821
822
823 static void OPC_6502_36 (void)
824 /* Opcode $36: ROL zp,x */
825 {
826     unsigned char ZPAddr;
827     unsigned Val;
828     Cycles = 6;
829     ZPAddr = MemReadByte (Regs.PC+1) + Regs.XR;
830     Val = MemReadByte (ZPAddr);
831     ROL (Val);
832     MemWriteByte (ZPAddr, Val);
833     Regs.PC += 2;
834 }
835
836
837
838 static void OPC_6502_38 (void)
839 /* Opcode $38: SEC */
840 {
841     Cycles = 2;
842     SET_CF (1);
843     Regs.PC += 1;
844 }
845
846
847
848 static void OPC_6502_39 (void)
849 /* Opcode $39: AND abs,y */
850 {
851     AC_OP_ABSY (&);
852 }
853
854
855
856 static void OPC_65SC02_3A (void)
857 /* Opcode $3A: DEC a */
858 {
859     Cycles = 2;
860     Regs.AC = (Regs.AC - 1) & 0xFF;
861     TEST_ZF (Regs.AC);
862     TEST_SF (Regs.AC);
863     Regs.PC += 1;
864 }
865
866
867
868 static void OPC_65SC02_3C (void)
869 /* Opcode $3C: BIT abs,x */
870 {
871     unsigned Addr;
872     unsigned char Val;
873     Cycles = 4;
874     Addr = MemReadWord (Regs.PC+1);
875     if (PAGE_CROSS (Addr, Regs.XR))
876         ++Cycles;
877     Val  = MemReadByte (Addr + Regs.XR);
878     SET_SF (Val & 0x80);
879     SET_OF (Val & 0x40);
880     SET_ZF ((Val & Regs.AC) == 0);
881     Regs.PC += 3;
882 }
883
884
885
886 static void OPC_6502_3D (void)
887 /* Opcode $3D: AND abs,x */
888 {
889     AC_OP_ABSX (&);
890 }
891
892
893
894 static void OPC_6502_3E (void)
895 /* Opcode $3E: ROL abs,x */
896 {
897     unsigned Addr;
898     unsigned Val;
899     Cycles = 7;
900     Addr = MemReadWord (Regs.PC+1) + Regs.XR;
901     Val = MemReadByte (Addr);
902     ROL (Val);
903     MemWriteByte (Addr, Val);
904     Regs.PC += 2;
905 }
906
907
908
909 static void OPC_6502_40 (void)
910 /* Opcode $40: RTI */
911 {
912     Cycles = 6;
913
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 */
918 }
919
920
921
922 static void OPC_6502_41 (void)
923 /* Opcode $41: EOR (zp,x) */
924 {
925     AC_OP_ZPXIND (^);
926 }
927
928
929
930 static void OPC_65C02_44 (void)
931 /* Opcode $44: 'zp' 3 cycle NOP */
932 {
933     Cycles = 3;
934     Regs.PC += 2;
935 }
936
937
938
939 static void OPC_6502_45 (void)
940 /* Opcode $45: EOR zp */
941 {
942     AC_OP_ZP (^);
943 }
944
945
946
947 static void OPC_6502_46 (void)
948 /* Opcode $46: LSR zp */
949 {
950     unsigned char ZPAddr;
951     unsigned char Val;
952     Cycles = 5;
953     ZPAddr = MemReadByte (Regs.PC+1);
954     Val = MemReadByte (ZPAddr);
955     SET_CF (Val & 0x01);
956     Val >>= 1;
957     MemWriteByte (ZPAddr, Val);
958     TEST_ZF (Val);
959     TEST_SF (Val);
960     Regs.PC += 2;
961 }
962
963
964
965 static void OPC_6502_48 (void)
966 /* Opcode $48: PHA */
967 {
968     Cycles = 3;
969     PUSH (Regs.AC);
970     Regs.PC += 1;
971 }
972
973
974
975 static void OPC_6502_49 (void)
976 /* Opcode $49: EOR #imm */
977 {
978     AC_OP_IMM (^);
979 }
980
981
982
983 static void OPC_6502_4A (void)
984 /* Opcode $4A: LSR a */
985 {
986     Cycles = 2;
987     SET_CF (Regs.AC & 0x01);
988     Regs.AC >>= 1;
989     TEST_ZF (Regs.AC);
990     TEST_SF (Regs.AC);
991     Regs.PC += 1;
992 }
993
994
995
996 static void OPC_6502_4C (void)
997 /* Opcode $4C: JMP abs */
998 {
999     Cycles = 3;
1000     Regs.PC = MemReadWord (Regs.PC+1);
1001
1002     ParaVirtHooks (&Regs);
1003 }
1004
1005
1006
1007 static void OPC_6502_4D (void)
1008 /* Opcode $4D: EOR abs */
1009 {
1010     AC_OP_ABS (^);
1011 }
1012
1013
1014
1015 static void OPC_6502_4E (void)
1016 /* Opcode $4E: LSR abs */
1017 {
1018     unsigned Addr;
1019     unsigned char Val;
1020     Cycles = 6;
1021     Addr = MemReadWord (Regs.PC+1);
1022     Val = MemReadByte (Addr);
1023     SET_CF (Val & 0x01);
1024     Val >>= 1;
1025     MemWriteByte (Addr, Val);
1026     TEST_ZF (Val);
1027     TEST_SF (Val);
1028     Regs.PC += 3;
1029 }
1030
1031
1032
1033 static void OPC_6502_50 (void)
1034 /* Opcode $50: BVC */
1035 {
1036     BRANCH (!GET_OF ());
1037 }
1038
1039
1040
1041 static void OPC_6502_51 (void)
1042 /* Opcode $51: EOR (zp),y */
1043 {
1044     AC_OP_ZPINDY (^);
1045 }
1046
1047
1048
1049 static void OPC_65SC02_52 (void)
1050 /* Opcode $52: EOR (zp) */
1051 {
1052     AC_OP_ZPIND (^);
1053 }
1054
1055
1056
1057 static void OPC_6502_55 (void)
1058 /* Opcode $55: EOR zp,x */
1059 {
1060     AC_OP_ZPX (^);
1061 }
1062
1063
1064
1065 static void OPC_6502_56 (void)
1066 /* Opcode $56: LSR zp,x */
1067 {
1068     unsigned char ZPAddr;
1069     unsigned char Val;
1070     Cycles = 6;
1071     ZPAddr = MemReadByte (Regs.PC+1) + Regs.XR;
1072     Val = MemReadByte (ZPAddr);
1073     SET_CF (Val & 0x01);
1074     Val >>= 1;
1075     MemWriteByte (ZPAddr, Val);
1076     TEST_ZF (Val);
1077     TEST_SF (Val);
1078     Regs.PC += 2;
1079 }
1080
1081
1082
1083 static void OPC_6502_58 (void)
1084 /* Opcode $58: CLI */
1085 {
1086     Cycles = 2;
1087     SET_IF (0);
1088     Regs.PC += 1;
1089 }
1090
1091
1092
1093 static void OPC_6502_59 (void)
1094 /* Opcode $59: EOR abs,y */
1095 {
1096     AC_OP_ABSY (^);
1097 }
1098
1099
1100
1101 static void OPC_65SC02_5A (void)
1102 /* Opcode $5A: PHY */
1103 {
1104     Cycles = 3;
1105     PUSH (Regs.YR);
1106     Regs.PC += 1;
1107 }
1108
1109
1110
1111 static void OPC_65C02_5C (void)
1112 /* Opcode $5C: 'Absolute' 8 cycle NOP */
1113 {
1114     Cycles = 8;
1115     Regs.PC += 3;
1116 }
1117
1118
1119
1120 static void OPC_6502_5D (void)
1121 /* Opcode $5D: EOR abs,x */
1122 {
1123     AC_OP_ABSX (^);
1124 }
1125
1126
1127
1128 static void OPC_6502_5E (void)
1129 /* Opcode $5E: LSR abs,x */
1130 {
1131     unsigned Addr;
1132     unsigned char Val;
1133     Cycles = 7;
1134     Addr = MemReadWord (Regs.PC+1) + Regs.XR;
1135     Val = MemReadByte (Addr);
1136     SET_CF (Val & 0x01);
1137     Val >>= 1;
1138     MemWriteByte (Addr, Val);
1139     TEST_ZF (Val);
1140     TEST_SF (Val);
1141     Regs.PC += 3;
1142 }
1143
1144
1145
1146 static void OPC_6502_60 (void)
1147 /* Opcode $60: RTS */
1148 {
1149     Cycles = 6;
1150     Regs.PC = POP ();                /* PCL */
1151     Regs.PC |= (POP () << 8);        /* PCH */
1152     Regs.PC += 1;
1153 }
1154
1155
1156
1157 static void OPC_6502_61 (void)
1158 /* Opcode $61: ADC (zp,x) */
1159 {
1160     unsigned char ZPAddr;
1161     unsigned Addr;
1162     Cycles = 6;
1163     ZPAddr = MemReadByte (Regs.PC+1) + Regs.XR;
1164     Addr = MemReadZPWord (ZPAddr);
1165     ADC (MemReadByte (Addr));
1166     Regs.PC += 2;
1167 }
1168
1169
1170
1171 static void OPC_65SC02_64 (void)
1172 /* Opcode $64: STZ zp */
1173 {
1174     unsigned char ZPAddr;
1175     Cycles = 3;
1176     ZPAddr = MemReadByte (Regs.PC+1);
1177     MemWriteByte (ZPAddr, 0);
1178     Regs.PC += 2;
1179 }
1180
1181
1182
1183 static void OPC_6502_65 (void)
1184 /* Opcode $65: ADC zp */
1185 {
1186     unsigned char ZPAddr;
1187     Cycles = 3;
1188     ZPAddr = MemReadByte (Regs.PC+1);
1189     ADC (MemReadByte (ZPAddr));
1190     Regs.PC += 2;
1191 }
1192
1193
1194
1195 static void OPC_6502_66 (void)
1196 /* Opcode $66: ROR zp */
1197 {
1198     unsigned char ZPAddr;
1199     unsigned Val;
1200     Cycles = 5;
1201     ZPAddr = MemReadByte (Regs.PC+1);
1202     Val = MemReadByte (ZPAddr);
1203     ROR (Val);
1204     MemWriteByte (ZPAddr, Val);
1205     Regs.PC += 2;
1206 }
1207
1208
1209
1210 static void OPC_6502_68 (void)
1211 /* Opcode $68: PLA */
1212 {
1213     Cycles = 4;
1214     Regs.AC = POP ();
1215     TEST_ZF (Regs.AC);
1216     TEST_SF (Regs.AC);
1217     Regs.PC += 1;
1218 }
1219
1220
1221
1222 static void OPC_6502_69 (void)
1223 /* Opcode $69: ADC #imm */
1224 {
1225     Cycles = 2;
1226     ADC (MemReadByte (Regs.PC+1));
1227     Regs.PC += 2;
1228 }
1229
1230
1231
1232 static void OPC_6502_6A (void)
1233 /* Opcode $6A: ROR a */
1234 {
1235     Cycles = 2;
1236     ROR (Regs.AC);
1237     Regs.PC += 1;
1238 }
1239
1240
1241
1242 static void OPC_6502_6C (void)
1243 /* Opcode $6C: JMP (ind) */
1244 {
1245     unsigned PC, Lo, Hi;
1246     PC = Regs.PC;
1247     Lo = MemReadWord (PC+1);
1248
1249     if (CPU == CPU_6502)
1250     {
1251          /* Emulate the 6502 bug */
1252         Cycles = 5;
1253         Regs.PC = MemReadByte (Lo);
1254         Hi = (Lo & 0xFF00) | ((Lo + 1) & 0xFF);
1255         Regs.PC |= (MemReadByte (Hi) << 8);
1256
1257         /* Output a warning if the bug is triggered */
1258         if (Hi != Lo + 1)
1259         {
1260             Warning ("6502 indirect jump bug triggered at $%04X, ind addr = $%04X",
1261                      PC, Lo);
1262         }
1263     }
1264     else
1265     {
1266         Cycles = 6;
1267         Regs.PC = MemReadWord(Lo);
1268     }
1269 }
1270
1271
1272
1273 static void OPC_65C02_6C (void)
1274 /* Opcode $6C: JMP (ind) */
1275 {
1276     /* 6502 bug fixed here */
1277     Cycles = 5;
1278     Regs.PC = MemReadWord (MemReadWord (Regs.PC+1));
1279 }
1280
1281
1282
1283 static void OPC_6502_6D (void)
1284 /* Opcode $6D: ADC abs */
1285 {
1286     unsigned Addr;
1287     Cycles = 4;
1288     Addr   = MemReadWord (Regs.PC+1);
1289     ADC (MemReadByte (Addr));
1290     Regs.PC += 3;
1291 }
1292
1293
1294
1295 static void OPC_6502_6E (void)
1296 /* Opcode $6E: ROR abs */
1297 {
1298     unsigned Addr;
1299     unsigned Val;
1300     Cycles = 6;
1301     Addr = MemReadWord (Regs.PC+1);
1302     Val  = MemReadByte (Addr);
1303     ROR (Val);
1304     MemWriteByte (Addr, Val);
1305     Regs.PC += 3;
1306 }
1307
1308
1309
1310 static void OPC_6502_70 (void)
1311 /* Opcode $70: BVS */
1312 {
1313     BRANCH (GET_OF ());
1314 }
1315
1316
1317
1318 static void OPC_6502_71 (void)
1319 /* Opcode $71: ADC (zp),y */
1320 {
1321     unsigned char ZPAddr;
1322     unsigned Addr;
1323     Cycles = 5;
1324     ZPAddr = MemReadByte (Regs.PC+1);
1325     Addr   = MemReadZPWord (ZPAddr);
1326     if (PAGE_CROSS (Addr, Regs.YR)) {
1327         ++Cycles;
1328     }
1329     ADC (MemReadByte (Addr + Regs.YR));
1330     Regs.PC += 2;
1331 }
1332
1333
1334
1335 static void OPC_65SC02_72 (void)
1336 /* Opcode $72: ADC (zp) */
1337 {
1338     unsigned char ZPAddr;
1339     unsigned Addr;
1340     Cycles = 5;
1341     ZPAddr = MemReadByte (Regs.PC+1);
1342     Addr   = MemReadZPWord (ZPAddr);
1343     ADC (MemReadByte (Addr));
1344     Regs.PC += 2;
1345 }
1346
1347
1348
1349 static void OPC_65SC02_74 (void)
1350 /* Opcode $74: STZ zp,x */
1351 {
1352     unsigned char ZPAddr;
1353     Cycles = 4;
1354     ZPAddr = MemReadByte (Regs.PC+1) + Regs.XR;
1355     MemWriteByte (ZPAddr, 0);
1356     Regs.PC += 2;
1357 }
1358
1359
1360
1361 static void OPC_6502_75 (void)
1362 /* Opcode $75: ADC zp,x */
1363 {
1364     unsigned char ZPAddr;
1365     Cycles = 4;
1366     ZPAddr = MemReadByte (Regs.PC+1) + Regs.XR;
1367     ADC (MemReadByte (ZPAddr));
1368     Regs.PC += 2;
1369 }
1370
1371
1372
1373 static void OPC_6502_76 (void)
1374 /* Opcode $76: ROR zp,x */
1375 {
1376     unsigned char ZPAddr;
1377     unsigned Val;
1378     Cycles = 6;
1379     ZPAddr = MemReadByte (Regs.PC+1) + Regs.XR;
1380     Val = MemReadByte (ZPAddr);
1381     ROR (Val);
1382     MemWriteByte (ZPAddr, Val);
1383     Regs.PC += 2;
1384 }
1385
1386
1387
1388 static void OPC_6502_78 (void)
1389 /* Opcode $78: SEI */
1390 {
1391     Cycles = 2;
1392     SET_IF (1);
1393     Regs.PC += 1;
1394 }
1395
1396
1397
1398 static void OPC_6502_79 (void)
1399 /* Opcode $79: ADC abs,y */
1400 {
1401     unsigned Addr;
1402     Cycles = 4;
1403     Addr = MemReadWord (Regs.PC+1);
1404     if (PAGE_CROSS (Addr, Regs.YR)) {
1405         ++Cycles;
1406     }
1407     ADC (MemReadByte (Addr + Regs.YR));
1408     Regs.PC += 3;
1409 }
1410
1411
1412
1413 static void OPC_65SC02_7A (void)
1414 /* Opcode $7A: PLY */
1415 {
1416     Cycles = 4;
1417     Regs.YR = POP ();
1418     TEST_ZF (Regs.YR);
1419     TEST_SF (Regs.YR);
1420     Regs.PC += 1;
1421 }
1422
1423
1424
1425 static void OPC_65SC02_7C (void)
1426 /* Opcode $7C: JMP (ind,X) */
1427 {
1428     unsigned PC, Adr;
1429     Cycles = 6;
1430     PC = Regs.PC;
1431     Adr = MemReadWord (PC+1);
1432     Regs.PC = MemReadWord(Adr+Regs.XR);
1433 }
1434
1435
1436
1437 static void OPC_6502_7D (void)
1438 /* Opcode $7D: ADC abs,x */
1439 {
1440     unsigned Addr;
1441     Cycles = 4;
1442     Addr = MemReadWord (Regs.PC+1);
1443     if (PAGE_CROSS (Addr, Regs.XR)) {
1444         ++Cycles;
1445     }
1446     ADC (MemReadByte (Addr + Regs.XR));
1447     Regs.PC += 3;
1448 }
1449
1450
1451
1452 static void OPC_6502_7E (void)
1453 /* Opcode $7E: ROR abs,x */
1454 {
1455     unsigned Addr;
1456     unsigned Val;
1457     Cycles = 7;
1458     Addr = MemReadWord (Regs.PC+1) + Regs.XR;
1459     Val = MemReadByte (Addr);
1460     ROR (Val);
1461     MemWriteByte (Addr, Val);
1462     Regs.PC += 3;
1463 }
1464
1465
1466
1467 static void OPC_65SC02_80 (void)
1468 /* Opcode $80: BRA */
1469 {
1470     BRANCH (1);
1471 }
1472
1473
1474
1475 static void OPC_6502_81 (void)
1476 /* Opcode $81: STA (zp,x) */
1477 {
1478     unsigned char ZPAddr;
1479     unsigned Addr;
1480     Cycles = 6;
1481     ZPAddr = MemReadByte (Regs.PC+1) + Regs.XR;
1482     Addr = MemReadZPWord (ZPAddr);
1483     MemWriteByte (Addr, Regs.AC);
1484     Regs.PC += 2;
1485 }
1486
1487
1488
1489 static void OPC_6502_84 (void)
1490 /* Opcode $84: STY zp */
1491 {
1492     unsigned char ZPAddr;
1493     Cycles = 3;
1494     ZPAddr = MemReadByte (Regs.PC+1);
1495     MemWriteByte (ZPAddr, Regs.YR);
1496     Regs.PC += 2;
1497 }
1498
1499
1500
1501 static void OPC_6502_85 (void)
1502 /* Opcode $85: STA zp */
1503 {
1504     unsigned char ZPAddr;
1505     Cycles = 3;
1506     ZPAddr = MemReadByte (Regs.PC+1);
1507     MemWriteByte (ZPAddr, Regs.AC);
1508     Regs.PC += 2;
1509 }
1510
1511
1512
1513 static void OPC_6502_86 (void)
1514 /* Opcode $86: STX zp */
1515 {
1516     unsigned char ZPAddr;
1517     Cycles = 3;
1518     ZPAddr = MemReadByte (Regs.PC+1);
1519     MemWriteByte (ZPAddr, Regs.XR);
1520     Regs.PC += 2;
1521 }
1522
1523
1524
1525 static void OPC_6502_88 (void)
1526 /* Opcode $88: DEY */
1527 {
1528     Cycles = 2;
1529     Regs.YR = (Regs.YR - 1) & 0xFF;
1530     TEST_ZF (Regs.YR);
1531     TEST_SF (Regs.YR);
1532     Regs.PC += 1;
1533 }
1534
1535
1536
1537 static void OPC_65SC02_89 (void)
1538 /* Opcode $89: BIT #imm */
1539 {
1540     unsigned char Val;
1541     Cycles = 2;
1542     Val = MemReadByte (Regs.PC+1);
1543     SET_SF (Val & 0x80);
1544     SET_OF (Val & 0x40);
1545     SET_ZF ((Val & Regs.AC) == 0);
1546     Regs.PC += 2;
1547 }
1548
1549
1550
1551 static void OPC_6502_8A (void)
1552 /* Opcode $8A: TXA */
1553 {
1554     Cycles = 2;
1555     Regs.AC = Regs.XR;
1556     TEST_ZF (Regs.AC);
1557     TEST_SF (Regs.AC);
1558     Regs.PC += 1;
1559 }
1560
1561
1562
1563 static void OPC_6502_8C (void)
1564 /* Opcode $8C: STY abs */
1565 {
1566     unsigned Addr;
1567     Cycles = 4;
1568     Addr = MemReadWord (Regs.PC+1);
1569     MemWriteByte (Addr, Regs.YR);
1570     Regs.PC += 3;
1571 }
1572
1573
1574
1575 static void OPC_6502_8D (void)
1576 /* Opcode $8D: STA abs */
1577 {
1578     unsigned Addr;
1579     Cycles = 4;
1580     Addr = MemReadWord (Regs.PC+1);
1581     MemWriteByte (Addr, Regs.AC);
1582     Regs.PC += 3;
1583 }
1584
1585
1586
1587 static void OPC_6502_8E (void)
1588 /* Opcode $8E: STX abs */
1589 {
1590     unsigned Addr;
1591     Cycles = 4;
1592     Addr = MemReadWord (Regs.PC+1);
1593     MemWriteByte (Addr, Regs.XR);
1594     Regs.PC += 3;
1595 }
1596
1597
1598
1599 static void OPC_6502_90 (void)
1600 /* Opcode $90: BCC */
1601 {
1602     BRANCH (!GET_CF ());
1603 }
1604
1605
1606
1607 static void OPC_6502_91 (void)
1608 /* Opcode $91: sta (zp),y */
1609 {
1610     unsigned char ZPAddr;
1611     unsigned Addr;
1612     Cycles = 6;
1613     ZPAddr = MemReadByte (Regs.PC+1);
1614     Addr = MemReadZPWord (ZPAddr) + Regs.YR;
1615     MemWriteByte (Addr, Regs.AC);
1616     Regs.PC += 2;
1617 }
1618
1619
1620
1621 static void OPC_65SC02_92 (void)
1622 /* Opcode $92: sta (zp) */
1623 {
1624     unsigned char ZPAddr;
1625     unsigned Addr;
1626     Cycles = 5;
1627     ZPAddr = MemReadByte (Regs.PC+1);
1628     Addr = MemReadZPWord (ZPAddr);
1629     MemWriteByte (Addr, Regs.AC);
1630     Regs.PC += 2;
1631 }
1632
1633
1634
1635 static void OPC_6502_94 (void)
1636 /* Opcode $94: STY zp,x */
1637 {
1638     unsigned char ZPAddr;
1639     Cycles = 4;
1640     ZPAddr = MemReadByte (Regs.PC+1) + Regs.XR;
1641     MemWriteByte (ZPAddr, Regs.YR);
1642     Regs.PC += 2;
1643 }
1644
1645
1646
1647 static void OPC_6502_95 (void)
1648 /* Opcode $95: STA zp,x */
1649 {
1650     unsigned char ZPAddr;
1651     Cycles = 4;
1652     ZPAddr = MemReadByte (Regs.PC+1) + Regs.XR;
1653     MemWriteByte (ZPAddr, Regs.AC);
1654     Regs.PC += 2;
1655 }
1656
1657
1658
1659 static void OPC_6502_96 (void)
1660 /* Opcode $96: stx zp,y */
1661 {
1662     unsigned char ZPAddr;
1663     Cycles = 4;
1664     ZPAddr = MemReadByte (Regs.PC+1) + Regs.YR;
1665     MemWriteByte (ZPAddr, Regs.XR);
1666     Regs.PC += 2;
1667 }
1668
1669
1670
1671 static void OPC_6502_98 (void)
1672 /* Opcode $98: TYA */
1673 {
1674     Cycles = 2;
1675     Regs.AC = Regs.YR;
1676     TEST_ZF (Regs.AC);
1677     TEST_SF (Regs.AC);
1678     Regs.PC += 1;
1679 }
1680
1681
1682
1683 static void OPC_6502_99 (void)
1684 /* Opcode $99: STA abs,y */
1685 {
1686     unsigned Addr;
1687     Cycles = 5;
1688     Addr = MemReadWord (Regs.PC+1) + Regs.YR;
1689     MemWriteByte (Addr, Regs.AC);
1690     Regs.PC += 3;
1691 }
1692
1693
1694
1695 static void OPC_6502_9A (void)
1696 /* Opcode $9A: TXS */
1697 {
1698     Cycles = 2;
1699     Regs.SP = Regs.XR;
1700     Regs.PC += 1;
1701 }
1702
1703
1704
1705 static void OPC_65SC02_9C (void)
1706 /* Opcode $9C: STZ abs */
1707 {
1708     unsigned Addr;
1709     Cycles = 4;
1710     Addr = MemReadWord (Regs.PC+1);
1711     MemWriteByte (Addr, 0);
1712     Regs.PC += 3;
1713 }
1714
1715
1716
1717 static void OPC_6502_9D (void)
1718 /* Opcode $9D: STA abs,x */
1719 {
1720     unsigned Addr;
1721     Cycles = 5;
1722     Addr = MemReadWord (Regs.PC+1) + Regs.XR;
1723     MemWriteByte (Addr, Regs.AC);
1724     Regs.PC += 3;
1725 }
1726
1727
1728
1729 static void OPC_65SC02_9E (void)
1730 /* Opcode $9E: STZ abs,x */
1731 {
1732     unsigned Addr;
1733     Cycles = 5;
1734     Addr = MemReadWord (Regs.PC+1) + Regs.XR;
1735     MemWriteByte (Addr, 0);
1736     Regs.PC += 3;
1737 }
1738
1739
1740
1741 static void OPC_6502_A0 (void)
1742 /* Opcode $A0: LDY #imm */
1743 {
1744     Cycles = 2;
1745     Regs.YR = MemReadByte (Regs.PC+1);
1746     TEST_ZF (Regs.YR);
1747     TEST_SF (Regs.YR);
1748     Regs.PC += 2;
1749 }
1750
1751
1752
1753 static void OPC_6502_A1 (void)
1754 /* Opcode $A1: LDA (zp,x) */
1755 {
1756     unsigned char ZPAddr;
1757     unsigned Addr;
1758     Cycles = 6;
1759     ZPAddr = MemReadByte (Regs.PC+1) + Regs.XR;
1760     Addr = MemReadZPWord (ZPAddr);
1761     Regs.AC = MemReadByte (Addr);
1762     TEST_ZF (Regs.AC);
1763     TEST_SF (Regs.AC);
1764     Regs.PC += 2;
1765 }
1766
1767
1768
1769 static void OPC_6502_A2 (void)
1770 /* Opcode $A2: LDX #imm */
1771 {
1772     Cycles = 2;
1773     Regs.XR = MemReadByte (Regs.PC+1);
1774     TEST_ZF (Regs.XR);
1775     TEST_SF (Regs.XR);
1776     Regs.PC += 2;
1777 }
1778
1779
1780
1781 static void OPC_6502_A4 (void)
1782 /* Opcode $A4: LDY zp */
1783 {
1784     unsigned char ZPAddr;
1785     Cycles = 3;
1786     ZPAddr = MemReadByte (Regs.PC+1);
1787     Regs.YR = MemReadByte (ZPAddr);
1788     TEST_ZF (Regs.YR);
1789     TEST_SF (Regs.YR);
1790     Regs.PC += 2;
1791 }
1792
1793
1794
1795 static void OPC_6502_A5 (void)
1796 /* Opcode $A5: LDA zp */
1797 {
1798     unsigned char ZPAddr;
1799     Cycles = 3;
1800     ZPAddr = MemReadByte (Regs.PC+1);
1801     Regs.AC = MemReadByte (ZPAddr);
1802     TEST_ZF (Regs.AC);
1803     TEST_SF (Regs.AC);
1804     Regs.PC += 2;
1805 }
1806
1807
1808
1809 static void OPC_6502_A6 (void)
1810 /* Opcode $A6: LDX zp */
1811 {
1812     unsigned char ZPAddr;
1813     Cycles = 3;
1814     ZPAddr = MemReadByte (Regs.PC+1);
1815     Regs.XR = MemReadByte (ZPAddr);
1816     TEST_ZF (Regs.XR);
1817     TEST_SF (Regs.XR);
1818     Regs.PC += 2;
1819 }
1820
1821
1822
1823 static void OPC_6502_A8 (void)
1824 /* Opcode $A8: TAY */
1825 {
1826     Cycles = 2;
1827     Regs.YR = Regs.AC;
1828     TEST_ZF (Regs.YR);
1829     TEST_SF (Regs.YR);
1830     Regs.PC += 1;
1831 }
1832
1833
1834
1835 static void OPC_6502_A9 (void)
1836 /* Opcode $A9: LDA #imm */
1837 {
1838     Cycles = 2;
1839     Regs.AC = MemReadByte (Regs.PC+1);
1840     TEST_ZF (Regs.AC);
1841     TEST_SF (Regs.AC);
1842     Regs.PC += 2;
1843 }
1844
1845
1846
1847 static void OPC_6502_AA (void)
1848 /* Opcode $AA: TAX */
1849 {
1850     Cycles = 2;
1851     Regs.XR = Regs.AC;
1852     TEST_ZF (Regs.XR);
1853     TEST_SF (Regs.XR);
1854     Regs.PC += 1;
1855 }
1856
1857
1858
1859 static void OPC_6502_AC (void)
1860 /* Opcode $Regs.AC: LDY abs */
1861 {
1862     unsigned Addr;
1863     Cycles = 4;
1864     Addr = MemReadWord (Regs.PC+1);
1865     Regs.YR = MemReadByte (Addr);
1866     TEST_ZF (Regs.YR);
1867     TEST_SF (Regs.YR);
1868     Regs.PC += 3;
1869 }
1870
1871
1872
1873 static void OPC_6502_AD (void)
1874 /* Opcode $AD: LDA abs */
1875 {
1876     unsigned Addr;
1877     Cycles = 4;
1878     Addr = MemReadWord (Regs.PC+1);
1879     Regs.AC = MemReadByte (Addr);
1880     TEST_ZF (Regs.AC);
1881     TEST_SF (Regs.AC);
1882     Regs.PC += 3;
1883 }
1884
1885
1886
1887 static void OPC_6502_AE (void)
1888 /* Opcode $AE: LDX abs */
1889 {
1890     unsigned Addr;
1891     Cycles = 4;
1892     Addr = MemReadWord (Regs.PC+1);
1893     Regs.XR = MemReadByte (Addr);
1894     TEST_ZF (Regs.XR);
1895     TEST_SF (Regs.XR);
1896     Regs.PC += 3;
1897 }
1898
1899
1900
1901 static void OPC_6502_B0 (void)
1902 /* Opcode $B0: BCS */
1903 {
1904     BRANCH (GET_CF ());
1905 }
1906
1907
1908
1909 static void OPC_6502_B1 (void)
1910 /* Opcode $B1: LDA (zp),y */
1911 {
1912     unsigned char ZPAddr;
1913     unsigned Addr;
1914     Cycles = 5;
1915     ZPAddr = MemReadByte (Regs.PC+1);
1916     Addr = MemReadZPWord (ZPAddr);
1917     if (PAGE_CROSS (Addr, Regs.YR)) {
1918         ++Cycles;
1919     }
1920     Regs.AC = MemReadByte (Addr + Regs.YR);
1921     TEST_ZF (Regs.AC);
1922     TEST_SF (Regs.AC);
1923     Regs.PC += 2;
1924 }
1925
1926
1927
1928 static void OPC_65SC02_B2 (void)
1929 /* Opcode $B2: LDA (zp) */
1930 {
1931     unsigned char ZPAddr;
1932     unsigned Addr;
1933     Cycles = 5;
1934     ZPAddr = MemReadByte (Regs.PC+1);
1935     Addr = MemReadZPWord (ZPAddr);
1936     Regs.AC = MemReadByte (Addr);
1937     TEST_ZF (Regs.AC);
1938     TEST_SF (Regs.AC);
1939     Regs.PC += 2;
1940 }
1941
1942
1943
1944 static void OPC_6502_B4 (void)
1945 /* Opcode $B4: LDY zp,x */
1946 {
1947     unsigned char ZPAddr;
1948     Cycles = 4;
1949     ZPAddr = MemReadByte (Regs.PC+1) + Regs.XR;
1950     Regs.YR = MemReadByte (ZPAddr);
1951     TEST_ZF (Regs.YR);
1952     TEST_SF (Regs.YR);
1953     Regs.PC += 2;
1954 }
1955
1956
1957
1958 static void OPC_6502_B5 (void)
1959 /* Opcode $B5: LDA zp,x */
1960 {
1961     unsigned char ZPAddr;
1962     Cycles = 4;
1963     ZPAddr = MemReadByte (Regs.PC+1) + Regs.XR;
1964     Regs.AC = MemReadByte (ZPAddr);
1965     TEST_ZF (Regs.AC);
1966     TEST_SF (Regs.AC);
1967     Regs.PC += 2;
1968 }
1969
1970
1971
1972 static void OPC_6502_B6 (void)
1973 /* Opcode $B6: LDX zp,y */
1974 {
1975     unsigned char ZPAddr;
1976     Cycles = 4;
1977     ZPAddr = MemReadByte (Regs.PC+1) + Regs.YR;
1978     Regs.XR = MemReadByte (ZPAddr);
1979     TEST_ZF (Regs.XR);
1980     TEST_SF (Regs.XR);
1981     Regs.PC += 2;
1982 }
1983
1984
1985
1986 static void OPC_6502_B8 (void)
1987 /* Opcode $B8: CLV */
1988 {
1989     Cycles = 2;
1990     SET_OF (0);
1991     Regs.PC += 1;
1992 }
1993
1994
1995
1996 static void OPC_6502_B9 (void)
1997 /* Opcode $B9: LDA abs,y */
1998 {
1999     unsigned Addr;
2000     Cycles = 4;
2001     Addr = MemReadWord (Regs.PC+1);
2002     if (PAGE_CROSS (Addr, Regs.YR)) {
2003         ++Cycles;
2004     }
2005     Regs.AC = MemReadByte (Addr + Regs.YR);
2006     TEST_ZF (Regs.AC);
2007     TEST_SF (Regs.AC);
2008     Regs.PC += 3;
2009 }
2010
2011
2012
2013 static void OPC_6502_BA (void)
2014 /* Opcode $BA: TSX */
2015 {
2016     Cycles = 2;
2017     Regs.XR = Regs.SP & 0xFF;
2018     TEST_ZF (Regs.XR);
2019     TEST_SF (Regs.XR);
2020     Regs.PC += 1;
2021 }
2022
2023
2024
2025 static void OPC_6502_BC (void)
2026 /* Opcode $BC: LDY abs,x */
2027 {
2028     unsigned Addr;
2029     Cycles = 4;
2030     Addr = MemReadWord (Regs.PC+1);
2031     if (PAGE_CROSS (Addr, Regs.XR)) {
2032         ++Cycles;
2033     }
2034     Regs.YR = MemReadByte (Addr + Regs.XR);
2035     TEST_ZF (Regs.YR);
2036     TEST_SF (Regs.YR);
2037     Regs.PC += 3;
2038 }
2039
2040
2041
2042 static void OPC_6502_BD (void)
2043 /* Opcode $BD: LDA abs,x */
2044 {
2045     unsigned Addr;
2046     Cycles = 4;
2047     Addr = MemReadWord (Regs.PC+1);
2048     if (PAGE_CROSS (Addr, Regs.XR)) {
2049         ++Cycles;
2050     }
2051     Regs.AC = MemReadByte (Addr + Regs.XR);
2052     TEST_ZF (Regs.AC);
2053     TEST_SF (Regs.AC);
2054     Regs.PC += 3;
2055 }
2056
2057
2058
2059 static void OPC_6502_BE (void)
2060 /* Opcode $BE: LDX abs,y */
2061 {
2062     unsigned Addr;
2063     Cycles = 4;
2064     Addr = MemReadWord (Regs.PC+1);
2065     if (PAGE_CROSS (Addr, Regs.YR)) {
2066         ++Cycles;
2067     }
2068     Regs.XR = MemReadByte (Addr + Regs.YR);
2069     TEST_ZF (Regs.XR);
2070     TEST_SF (Regs.XR);
2071     Regs.PC += 3;
2072 }
2073
2074
2075
2076 static void OPC_6502_C0 (void)
2077 /* Opcode $C0: CPY #imm */
2078 {
2079     Cycles = 2;
2080     CMP (Regs.YR, MemReadByte (Regs.PC+1));
2081     Regs.PC += 2;
2082 }
2083
2084
2085
2086 static void OPC_6502_C1 (void)
2087 /* Opcode $C1: CMP (zp,x) */
2088 {
2089     unsigned char ZPAddr;
2090     unsigned Addr;
2091     Cycles = 6;
2092     ZPAddr = MemReadByte (Regs.PC+1) + Regs.XR;
2093     Addr = MemReadZPWord (ZPAddr);
2094     CMP (Regs.AC, MemReadByte (Addr));
2095     Regs.PC += 2;
2096 }
2097
2098
2099
2100 static void OPC_6502_C4 (void)
2101 /* Opcode $C4: CPY zp */
2102 {
2103     unsigned char ZPAddr;
2104     Cycles = 3;
2105     ZPAddr = MemReadByte (Regs.PC+1);
2106     CMP (Regs.YR, MemReadByte (ZPAddr));
2107     Regs.PC += 2;
2108 }
2109
2110
2111
2112 static void OPC_6502_C5 (void)
2113 /* Opcode $C5: CMP zp */
2114 {
2115     unsigned char ZPAddr;
2116     Cycles = 3;
2117     ZPAddr = MemReadByte (Regs.PC+1);
2118     CMP (Regs.AC, MemReadByte (ZPAddr));
2119     Regs.PC += 2;
2120 }
2121
2122
2123
2124 static void OPC_6502_C6 (void)
2125 /* Opcode $C6: DEC zp */
2126 {
2127     unsigned char ZPAddr;
2128     unsigned char Val;
2129     Cycles = 5;
2130     ZPAddr = MemReadByte (Regs.PC+1);
2131     Val = MemReadByte (ZPAddr) - 1;
2132     MemWriteByte (ZPAddr, Val);
2133     TEST_ZF (Val);
2134     TEST_SF (Val);
2135     Regs.PC += 2;
2136 }
2137
2138
2139
2140 static void OPC_6502_C8 (void)
2141 /* Opcode $C8: INY */
2142 {
2143     Cycles = 2;
2144     Regs.YR = (Regs.YR + 1) & 0xFF;
2145     TEST_ZF (Regs.YR);
2146     TEST_SF (Regs.YR);
2147     Regs.PC += 1;
2148 }
2149
2150
2151
2152 static void OPC_6502_C9 (void)
2153 /* Opcode $C9: CMP #imm */
2154 {
2155     Cycles = 2;
2156     CMP (Regs.AC, MemReadByte (Regs.PC+1));
2157     Regs.PC += 2;
2158 }
2159
2160
2161
2162 static void OPC_6502_CA (void)
2163 /* Opcode $CA: DEX */
2164 {
2165     Cycles = 2;
2166     Regs.XR = (Regs.XR - 1) & 0xFF;
2167     TEST_ZF (Regs.XR);
2168     TEST_SF (Regs.XR);
2169     Regs.PC += 1;
2170 }
2171
2172
2173
2174 static void OPC_6502_CC (void)
2175 /* Opcode $CC: CPY abs */
2176 {
2177     unsigned Addr;
2178     Cycles = 4;
2179     Addr = MemReadWord (Regs.PC+1);
2180     CMP (Regs.YR, MemReadByte (Addr));
2181     Regs.PC += 3;
2182 }
2183
2184
2185
2186 static void OPC_6502_CD (void)
2187 /* Opcode $CD: CMP abs */
2188 {
2189     unsigned Addr;
2190     Cycles = 4;
2191     Addr = MemReadWord (Regs.PC+1);
2192     CMP (Regs.AC, MemReadByte (Addr));
2193     Regs.PC += 3;
2194 }
2195
2196
2197
2198 static void OPC_6502_CE (void)
2199 /* Opcode $CE: DEC abs */
2200 {
2201     unsigned Addr;
2202     unsigned char Val;
2203     Cycles = 6;
2204     Addr = MemReadWord (Regs.PC+1);
2205     Val  = MemReadByte (Addr) - 1;
2206     MemWriteByte (Addr, Val);
2207     TEST_ZF (Val);
2208     TEST_SF (Val);
2209     Regs.PC += 3;
2210 }
2211
2212
2213
2214 static void OPC_6502_D0 (void)
2215 /* Opcode $D0: BNE */
2216 {
2217     BRANCH (!GET_ZF ());
2218 }
2219
2220
2221
2222 static void OPC_6502_D1 (void)
2223 /* Opcode $D1: CMP (zp),y */
2224 {
2225     unsigned ZPAddr;
2226     unsigned Addr;
2227     Cycles = 5;
2228     ZPAddr = MemReadByte (Regs.PC+1);
2229     Addr = MemReadWord (ZPAddr);
2230     if (PAGE_CROSS (Addr, Regs.YR)) {
2231         ++Cycles;
2232     }
2233     CMP (Regs.AC, MemReadByte (Addr + Regs.YR));
2234     Regs.PC += 2;
2235 }
2236
2237
2238
2239 static void OPC_65SC02_D2 (void)
2240 /* Opcode $D2: CMP (zp) */
2241 {
2242     unsigned ZPAddr;
2243     unsigned Addr;
2244     Cycles = 5;
2245     ZPAddr = MemReadByte (Regs.PC+1);
2246     Addr = MemReadWord (ZPAddr);
2247     CMP (Regs.AC, MemReadByte (Addr));
2248     Regs.PC += 2;
2249 }
2250
2251
2252
2253 static void OPC_6502_D5 (void)
2254 /* Opcode $D5: CMP zp,x */
2255 {
2256     unsigned char ZPAddr;
2257     Cycles = 4;
2258     ZPAddr = MemReadByte (Regs.PC+1) + Regs.XR;
2259     CMP (Regs.AC, MemReadByte (ZPAddr));
2260     Regs.PC += 2;
2261 }
2262
2263
2264
2265 static void OPC_6502_D6 (void)
2266 /* Opcode $D6: DEC zp,x */
2267 {
2268     unsigned char ZPAddr;
2269     unsigned char Val;
2270     Cycles = 6;
2271     ZPAddr = MemReadByte (Regs.PC+1) + Regs.XR;
2272     Val = MemReadByte (ZPAddr) - 1;
2273     MemWriteByte (ZPAddr, Val);
2274     TEST_ZF (Val);
2275     TEST_SF (Val);
2276     Regs.PC += 2;
2277 }
2278
2279
2280
2281 static void OPC_6502_D8 (void)
2282 /* Opcode $D8: CLD */
2283 {
2284     Cycles = 2;
2285     SET_DF (0);
2286     Regs.PC += 1;
2287 }
2288
2289
2290
2291 static void OPC_6502_D9 (void)
2292 /* Opcode $D9: CMP abs,y */
2293 {
2294     unsigned Addr;
2295     Cycles = 4;
2296     Addr = MemReadWord (Regs.PC+1);
2297     if (PAGE_CROSS (Addr, Regs.YR)) {
2298         ++Cycles;
2299     }
2300     CMP (Regs.AC, MemReadByte (Addr + Regs.YR));
2301     Regs.PC += 3;
2302 }
2303
2304
2305
2306 static void OPC_65SC02_DA (void)
2307 /* Opcode $DA: PHX */
2308 {
2309     Cycles = 3;
2310     PUSH (Regs.XR);
2311     Regs.PC += 1;
2312 }
2313
2314
2315
2316 static void OPC_6502_DD (void)
2317 /* Opcode $DD: CMP abs,x */
2318 {
2319     unsigned Addr;
2320     Cycles = 4;
2321     Addr = MemReadWord (Regs.PC+1);
2322     if (PAGE_CROSS (Addr, Regs.XR)) {
2323         ++Cycles;
2324     }
2325     CMP (Regs.AC, MemReadByte (Addr + Regs.XR));
2326     Regs.PC += 3;
2327 }
2328
2329
2330
2331 static void OPC_6502_DE (void)
2332 /* Opcode $DE: DEC abs,x */
2333 {
2334     unsigned Addr;
2335     unsigned char Val;
2336     Cycles = 7;
2337     Addr = MemReadWord (Regs.PC+1) + Regs.XR;
2338     Val = MemReadByte (Addr) - 1;
2339     MemWriteByte (Addr, Val);
2340     TEST_ZF (Val);
2341     TEST_SF (Val);
2342     Regs.PC += 3;
2343 }
2344
2345
2346
2347 static void OPC_6502_E0 (void)
2348 /* Opcode $E0: CPX #imm */
2349 {
2350     Cycles = 2;
2351     CMP (Regs.XR, MemReadByte (Regs.PC+1));
2352     Regs.PC += 2;
2353 }
2354
2355
2356
2357 static void OPC_6502_E1 (void)
2358 /* Opcode $E1: SBC (zp,x) */
2359 {
2360     unsigned char ZPAddr;
2361     unsigned Addr;
2362     Cycles = 6;
2363     ZPAddr = MemReadByte (Regs.PC+1) + Regs.XR;
2364     Addr = MemReadZPWord (ZPAddr);
2365     SBC (MemReadByte (Addr));
2366     Regs.PC += 2;
2367 }
2368
2369
2370
2371 static void OPC_6502_E4 (void)
2372 /* Opcode $E4: CPX zp */
2373 {
2374     unsigned char ZPAddr;
2375     Cycles = 3;
2376     ZPAddr = MemReadByte (Regs.PC+1);
2377     CMP (Regs.XR, MemReadByte (ZPAddr));
2378     Regs.PC += 2;
2379 }
2380
2381
2382
2383 static void OPC_6502_E5 (void)
2384 /* Opcode $E5: SBC zp */
2385 {
2386     unsigned char ZPAddr;
2387     Cycles = 3;
2388     ZPAddr = MemReadByte (Regs.PC+1);
2389     SBC (MemReadByte (ZPAddr));
2390     Regs.PC += 2;
2391 }
2392
2393
2394
2395 static void OPC_6502_E6 (void)
2396 /* Opcode $E6: INC zp */
2397 {
2398     unsigned char ZPAddr;
2399     unsigned char Val;
2400     Cycles = 5;
2401     ZPAddr = MemReadByte (Regs.PC+1);
2402     Val = MemReadByte (ZPAddr) + 1;
2403     MemWriteByte (ZPAddr, Val);
2404     TEST_ZF (Val);
2405     TEST_SF (Val);
2406     Regs.PC += 2;
2407 }
2408
2409
2410
2411 static void OPC_6502_E8 (void)
2412 /* Opcode $E8: INX */
2413 {
2414     Cycles = 2;
2415     Regs.XR = (Regs.XR + 1) & 0xFF;
2416     TEST_ZF (Regs.XR);
2417     TEST_SF (Regs.XR);
2418     Regs.PC += 1;
2419 }
2420
2421
2422
2423 static void OPC_6502_E9 (void)
2424 /* Opcode $E9: SBC #imm */
2425 {
2426     Cycles = 2;
2427     SBC (MemReadByte (Regs.PC+1));
2428     Regs.PC += 2;
2429 }
2430
2431
2432
2433 static void OPC_6502_EA (void)
2434 /* Opcode $EA: NOP */
2435 {
2436     /* This one is easy... */
2437     Cycles = 2;
2438     Regs.PC += 1;
2439 }
2440
2441
2442
2443 static void OPC_65C02_NOP11(void)
2444 /* Opcode 'Illegal' 1 cycle NOP */
2445 {
2446     Cycles = 1;
2447     Regs.PC += 1;
2448 }
2449
2450
2451
2452 static void OPC_65C02_NOP22 (void)
2453 /* Opcode 'Illegal' 2 byte 2 cycle NOP */
2454 {
2455     Cycles = 2;
2456     Regs.PC += 2;
2457 }
2458
2459
2460
2461 static void OPC_65C02_NOP24 (void)
2462 /* Opcode 'Illegal' 2 byte 4 cycle NOP */
2463 {
2464     Cycles = 4;
2465     Regs.PC += 2;
2466 }
2467
2468
2469
2470 static void OPC_65C02_NOP34 (void)
2471 /* Opcode 'Illegal' 3 byte 4 cycle NOP */
2472 {
2473     Cycles = 4;
2474     Regs.PC += 3;
2475 }
2476
2477
2478
2479 static void OPC_6502_EC (void)
2480 /* Opcode $EC: CPX abs */
2481 {
2482     unsigned Addr;
2483     Cycles = 4;
2484     Addr   = MemReadWord (Regs.PC+1);
2485     CMP (Regs.XR, MemReadByte (Addr));
2486     Regs.PC += 3;
2487 }
2488
2489
2490
2491 static void OPC_6502_ED (void)
2492 /* Opcode $ED: SBC abs */
2493 {
2494     unsigned Addr;
2495     Cycles = 4;
2496     Addr = MemReadWord (Regs.PC+1);
2497     SBC (MemReadByte (Addr));
2498     Regs.PC += 3;
2499 }
2500
2501
2502
2503 static void OPC_6502_EE (void)
2504 /* Opcode $EE: INC abs */
2505 {
2506     unsigned Addr;
2507     unsigned char Val;
2508     Cycles = 6;
2509     Addr = MemReadWord (Regs.PC+1);
2510     Val = MemReadByte (Addr) + 1;
2511     MemWriteByte (Addr, Val);
2512     TEST_ZF (Val);
2513     TEST_SF (Val);
2514     Regs.PC += 3;
2515 }
2516
2517
2518
2519 static void OPC_6502_F0 (void)
2520 /* Opcode $F0: BEQ */
2521 {
2522     BRANCH (GET_ZF ());
2523 }
2524
2525
2526
2527 static void OPC_6502_F1 (void)
2528 /* Opcode $F1: SBC (zp),y */
2529 {
2530     unsigned char ZPAddr;
2531     unsigned Addr;
2532     Cycles = 5;
2533     ZPAddr = MemReadByte (Regs.PC+1);
2534     Addr = MemReadZPWord (ZPAddr);
2535     if (PAGE_CROSS (Addr, Regs.YR)) {
2536         ++Cycles;
2537     }
2538     SBC (MemReadByte (Addr + Regs.YR));
2539     Regs.PC += 2;
2540 }
2541
2542
2543
2544 static void OPC_65SC02_F2 (void)
2545 /* Opcode $F2: SBC (zp) */
2546 {
2547     unsigned char ZPAddr;
2548     unsigned Addr;
2549     Cycles = 5;
2550     ZPAddr = MemReadByte (Regs.PC+1);
2551     Addr = MemReadZPWord (ZPAddr);
2552     SBC (MemReadByte (Addr));
2553     Regs.PC += 2;
2554 }
2555
2556
2557
2558 static void OPC_6502_F5 (void)
2559 /* Opcode $F5: SBC zp,x */
2560 {
2561     unsigned char ZPAddr;
2562     Cycles = 4;
2563     ZPAddr = MemReadByte (Regs.PC+1) + Regs.XR;
2564     SBC (MemReadByte (ZPAddr));
2565     Regs.PC += 2;
2566 }
2567
2568
2569
2570 static void OPC_6502_F6 (void)
2571 /* Opcode $F6: INC zp,x */
2572 {
2573     unsigned char ZPAddr;
2574     unsigned char Val;
2575     Cycles = 6;
2576     ZPAddr = MemReadByte (Regs.PC+1) + Regs.XR;
2577     Val = MemReadByte (ZPAddr) + 1;
2578     MemWriteByte (ZPAddr, Val);
2579     TEST_ZF (Val);
2580     TEST_SF (Val);
2581     Regs.PC += 2;
2582 }
2583
2584
2585
2586 static void OPC_6502_F8 (void)
2587 /* Opcode $F8: SED */
2588 {
2589     Cycles = 2;
2590     SET_DF (1);
2591     Regs.PC += 1;
2592 }
2593
2594
2595
2596 static void OPC_6502_F9 (void)
2597 /* Opcode $F9: SBC abs,y */
2598 {
2599     unsigned Addr;
2600     Cycles = 4;
2601     Addr = MemReadWord (Regs.PC+1);
2602     if (PAGE_CROSS (Addr, Regs.YR)) {
2603         ++Cycles;
2604     }
2605     SBC (MemReadByte (Addr + Regs.YR));
2606     Regs.PC += 3;
2607 }
2608
2609
2610
2611 static void OPC_65SC02_FA (void)
2612 /* Opcode $7A: PLX */
2613 {
2614     Cycles = 4;
2615     Regs.XR = POP ();
2616     TEST_ZF (Regs.XR);
2617     TEST_SF (Regs.XR);
2618     Regs.PC += 1;
2619 }
2620
2621
2622
2623 static void OPC_6502_FD (void)
2624 /* Opcode $FD: SBC abs,x */
2625 {
2626     unsigned Addr;
2627     Cycles = 4;
2628     Addr = MemReadWord (Regs.PC+1);
2629     if (PAGE_CROSS (Addr, Regs.XR)) {
2630         ++Cycles;
2631     }
2632     SBC (MemReadByte (Addr + Regs.XR));
2633     Regs.PC += 3;
2634 }
2635
2636
2637
2638 static void OPC_6502_FE (void)
2639 /* Opcode $FE: INC abs,x */
2640 {
2641     unsigned Addr;
2642     unsigned char Val;
2643     Cycles = 7;
2644     Addr = MemReadWord (Regs.PC+1) + Regs.XR;
2645     Val = MemReadByte (Addr) + 1;
2646     MemWriteByte (Addr, Val);
2647     TEST_ZF (Val);
2648     TEST_SF (Val);
2649     Regs.PC += 3;
2650 }
2651
2652
2653
2654 /*****************************************************************************/
2655 /*                           Opcode handler tables                           */
2656 /*****************************************************************************/
2657
2658
2659
2660 /* Opcode handler table for the 6502 */
2661 static const OPFunc OP6502Table[256] = {
2662     OPC_6502_00,
2663     OPC_6502_01,
2664     OPC_Illegal,
2665     OPC_Illegal,
2666     OPC_Illegal,
2667     OPC_6502_05,
2668     OPC_6502_06,
2669     OPC_Illegal,
2670     OPC_6502_08,
2671     OPC_6502_09,
2672     OPC_6502_0A,
2673     OPC_Illegal,
2674     OPC_Illegal,
2675     OPC_6502_0D,
2676     OPC_6502_0E,
2677     OPC_Illegal,
2678     OPC_6502_10,
2679     OPC_6502_11,
2680     OPC_Illegal,
2681     OPC_Illegal,
2682     OPC_Illegal,
2683     OPC_6502_15,
2684     OPC_6502_16,
2685     OPC_Illegal,
2686     OPC_6502_18,
2687     OPC_6502_19,
2688     OPC_Illegal,
2689     OPC_Illegal,
2690     OPC_Illegal,
2691     OPC_6502_1D,
2692     OPC_6502_1E,
2693     OPC_Illegal,
2694     OPC_6502_20,
2695     OPC_6502_21,
2696     OPC_Illegal,
2697     OPC_Illegal,
2698     OPC_6502_24,
2699     OPC_6502_25,
2700     OPC_6502_26,
2701     OPC_Illegal,
2702     OPC_6502_28,
2703     OPC_6502_29,
2704     OPC_6502_2A,
2705     OPC_Illegal,
2706     OPC_6502_2C,
2707     OPC_6502_2D,
2708     OPC_6502_2E,
2709     OPC_Illegal,
2710     OPC_6502_30,
2711     OPC_6502_31,
2712     OPC_Illegal,
2713     OPC_Illegal,
2714     OPC_Illegal,
2715     OPC_6502_35,
2716     OPC_6502_36,
2717     OPC_Illegal,
2718     OPC_6502_38,
2719     OPC_6502_39,
2720     OPC_Illegal,
2721     OPC_Illegal,
2722     OPC_Illegal,
2723     OPC_6502_3D,
2724     OPC_6502_3E,
2725     OPC_Illegal,
2726     OPC_6502_40,
2727     OPC_6502_41,
2728     OPC_Illegal,
2729     OPC_Illegal,
2730     OPC_Illegal,
2731     OPC_6502_45,
2732     OPC_6502_46,
2733     OPC_Illegal,
2734     OPC_6502_48,
2735     OPC_6502_49,
2736     OPC_6502_4A,
2737     OPC_Illegal,
2738     OPC_6502_4C,
2739     OPC_6502_4D,
2740     OPC_6502_4E,
2741     OPC_Illegal,
2742     OPC_6502_50,
2743     OPC_6502_51,
2744     OPC_Illegal,
2745     OPC_Illegal,
2746     OPC_Illegal,
2747     OPC_6502_55,
2748     OPC_6502_56,
2749     OPC_Illegal,
2750     OPC_6502_58,
2751     OPC_6502_59,
2752     OPC_Illegal,
2753     OPC_Illegal,
2754     OPC_Illegal,
2755     OPC_6502_5D,
2756     OPC_6502_5E,
2757     OPC_Illegal,
2758     OPC_6502_60,
2759     OPC_6502_61,
2760     OPC_Illegal,
2761     OPC_Illegal,
2762     OPC_Illegal,
2763     OPC_6502_65,
2764     OPC_6502_66,
2765     OPC_Illegal,
2766     OPC_6502_68,
2767     OPC_6502_69,
2768     OPC_6502_6A,
2769     OPC_Illegal,
2770     OPC_6502_6C,
2771     OPC_6502_6D,
2772     OPC_6502_6E,
2773     OPC_Illegal,
2774     OPC_6502_70,
2775     OPC_6502_71,
2776     OPC_Illegal,
2777     OPC_Illegal,
2778     OPC_Illegal,
2779     OPC_6502_75,
2780     OPC_6502_76,
2781     OPC_Illegal,
2782     OPC_6502_78,
2783     OPC_6502_79,
2784     OPC_Illegal,
2785     OPC_Illegal,
2786     OPC_Illegal,
2787     OPC_6502_7D,
2788     OPC_6502_7E,
2789     OPC_Illegal,
2790     OPC_Illegal,
2791     OPC_6502_81,
2792     OPC_Illegal,
2793     OPC_Illegal,
2794     OPC_6502_84,
2795     OPC_6502_85,
2796     OPC_6502_86,
2797     OPC_Illegal,
2798     OPC_6502_88,
2799     OPC_Illegal,
2800     OPC_6502_8A,
2801     OPC_Illegal,
2802     OPC_6502_8C,
2803     OPC_6502_8D,
2804     OPC_6502_8E,
2805     OPC_Illegal,
2806     OPC_6502_90,
2807     OPC_6502_91,
2808     OPC_Illegal,
2809     OPC_Illegal,
2810     OPC_6502_94,
2811     OPC_6502_95,
2812     OPC_6502_96,
2813     OPC_Illegal,
2814     OPC_6502_98,
2815     OPC_6502_99,
2816     OPC_6502_9A,
2817     OPC_Illegal,
2818     OPC_Illegal,
2819     OPC_6502_9D,
2820     OPC_Illegal,
2821     OPC_Illegal,
2822     OPC_6502_A0,
2823     OPC_6502_A1,
2824     OPC_6502_A2,
2825     OPC_Illegal,
2826     OPC_6502_A4,
2827     OPC_6502_A5,
2828     OPC_6502_A6,
2829     OPC_Illegal,
2830     OPC_6502_A8,
2831     OPC_6502_A9,
2832     OPC_6502_AA,
2833     OPC_Illegal,
2834     OPC_6502_AC,
2835     OPC_6502_AD,
2836     OPC_6502_AE,
2837     OPC_Illegal,
2838     OPC_6502_B0,
2839     OPC_6502_B1,
2840     OPC_Illegal,
2841     OPC_Illegal,
2842     OPC_6502_B4,
2843     OPC_6502_B5,
2844     OPC_6502_B6,
2845     OPC_Illegal,
2846     OPC_6502_B8,
2847     OPC_6502_B9,
2848     OPC_6502_BA,
2849     OPC_Illegal,
2850     OPC_6502_BC,
2851     OPC_6502_BD,
2852     OPC_6502_BE,
2853     OPC_Illegal,
2854     OPC_6502_C0,
2855     OPC_6502_C1,
2856     OPC_Illegal,
2857     OPC_Illegal,
2858     OPC_6502_C4,
2859     OPC_6502_C5,
2860     OPC_6502_C6,
2861     OPC_Illegal,
2862     OPC_6502_C8,
2863     OPC_6502_C9,
2864     OPC_6502_CA,
2865     OPC_Illegal,
2866     OPC_6502_CC,
2867     OPC_6502_CD,
2868     OPC_6502_CE,
2869     OPC_Illegal,
2870     OPC_6502_D0,
2871     OPC_6502_D1,
2872     OPC_Illegal,
2873     OPC_Illegal,
2874     OPC_Illegal,
2875     OPC_6502_D5,
2876     OPC_6502_D6,
2877     OPC_Illegal,
2878     OPC_6502_D8,
2879     OPC_6502_D9,
2880     OPC_Illegal,
2881     OPC_Illegal,
2882     OPC_Illegal,
2883     OPC_6502_DD,
2884     OPC_6502_DE,
2885     OPC_Illegal,
2886     OPC_6502_E0,
2887     OPC_6502_E1,
2888     OPC_Illegal,
2889     OPC_Illegal,
2890     OPC_6502_E4,
2891     OPC_6502_E5,
2892     OPC_6502_E6,
2893     OPC_Illegal,
2894     OPC_6502_E8,
2895     OPC_6502_E9,
2896     OPC_6502_EA,
2897     OPC_Illegal,
2898     OPC_6502_EC,
2899     OPC_6502_ED,
2900     OPC_6502_EE,
2901     OPC_Illegal,
2902     OPC_6502_F0,
2903     OPC_6502_F1,
2904     OPC_Illegal,
2905     OPC_Illegal,
2906     OPC_Illegal,
2907     OPC_6502_F5,
2908     OPC_6502_F6,
2909     OPC_Illegal,
2910     OPC_6502_F8,
2911     OPC_6502_F9,
2912     OPC_Illegal,
2913     OPC_Illegal,
2914     OPC_Illegal,
2915     OPC_6502_FD,
2916     OPC_6502_FE,
2917     OPC_Illegal,
2918 };
2919
2920
2921
2922 /* Opcode handler table for the 65C02 */
2923 static const OPFunc OP65C02Table[256] = {
2924     OPC_6502_00,
2925     OPC_6502_01,
2926     OPC_65C02_NOP22,    // $02
2927     OPC_65C02_NOP11,    // $03
2928     OPC_65SC02_04,
2929     OPC_6502_05,
2930     OPC_6502_06,
2931     OPC_Illegal,        // $07: RMB0 currently unsupported
2932     OPC_6502_08,
2933     OPC_6502_09,
2934     OPC_6502_0A,
2935     OPC_65C02_NOP11,    // $0B
2936     OPC_65SC02_0C,
2937     OPC_6502_0D,
2938     OPC_6502_0E,
2939     OPC_Illegal,        // $0F: BBR0 currently unsupported
2940     OPC_6502_10,
2941     OPC_6502_11,
2942     OPC_65SC02_12,
2943     OPC_65C02_NOP11,    // $13
2944     OPC_65SC02_14,
2945     OPC_6502_15,
2946     OPC_6502_16,
2947     OPC_Illegal,        // $17: RMB1 currently unsupported
2948     OPC_6502_18,
2949     OPC_6502_19,
2950     OPC_65SC02_1A,
2951     OPC_65C02_NOP11,    // $1B
2952     OPC_65SC02_1C,
2953     OPC_6502_1D,
2954     OPC_6502_1E,
2955     OPC_Illegal,        // $1F: BBR1 currently unsupported
2956     OPC_6502_20,
2957     OPC_6502_21,
2958     OPC_65C02_NOP22,    // $22
2959     OPC_65C02_NOP11,    // $23
2960     OPC_6502_24,
2961     OPC_6502_25,
2962     OPC_6502_26,
2963     OPC_Illegal,        // $27: RMB2 currently unsupported
2964     OPC_6502_28,
2965     OPC_6502_29,
2966     OPC_6502_2A,
2967     OPC_65C02_NOP11,    // $2B
2968     OPC_6502_2C,
2969     OPC_6502_2D,
2970     OPC_6502_2E,
2971     OPC_Illegal,        // $2F: BBR2 currently unsupported
2972     OPC_6502_30,
2973     OPC_6502_31,
2974     OPC_65SC02_32,
2975     OPC_65C02_NOP11,    // $33
2976     OPC_65SC02_34,
2977     OPC_6502_35,
2978     OPC_6502_36,
2979     OPC_Illegal,        // $37: RMB3 currently unsupported
2980     OPC_6502_38,
2981     OPC_6502_39,
2982     OPC_65SC02_3A,
2983     OPC_65C02_NOP11,    // $3B
2984     OPC_65SC02_3C,
2985     OPC_6502_3D,
2986     OPC_6502_3E,
2987     OPC_Illegal,        // $3F: BBR3 currently unsupported
2988     OPC_6502_40,
2989     OPC_6502_41,
2990     OPC_65C02_NOP22,    // $42
2991     OPC_65C02_NOP11,    // $43
2992     OPC_65C02_44,       // $44
2993     OPC_6502_45,
2994     OPC_6502_46,
2995     OPC_Illegal,        // $47: RMB4 currently unsupported
2996     OPC_6502_48,
2997     OPC_6502_49,
2998     OPC_6502_4A,
2999     OPC_65C02_NOP11,    // $4B
3000     OPC_6502_4C,
3001     OPC_6502_4D,
3002     OPC_6502_4E,
3003     OPC_Illegal,        // $4F: BBR4 currently unsupported
3004     OPC_6502_50,
3005     OPC_6502_51,
3006     OPC_65SC02_52,
3007     OPC_65C02_NOP11,    // $53
3008     OPC_65C02_NOP24,    // $54
3009     OPC_6502_55,
3010     OPC_6502_56,
3011     OPC_Illegal,        // $57: RMB5 currently unsupported
3012     OPC_6502_58,
3013     OPC_6502_59,
3014     OPC_65SC02_5A,
3015     OPC_65C02_NOP11,    // $5B
3016     OPC_65C02_5C,
3017     OPC_6502_5D,
3018     OPC_6502_5E,
3019     OPC_Illegal,        // $5F: BBR5 currently unsupported
3020     OPC_6502_60,
3021     OPC_6502_61,
3022     OPC_65C02_NOP22,    // $62
3023     OPC_65C02_NOP11,    // $63
3024     OPC_65SC02_64,
3025     OPC_6502_65,
3026     OPC_6502_66,
3027     OPC_Illegal,        // $67: RMB6 currently unsupported
3028     OPC_6502_68,
3029     OPC_6502_69,
3030     OPC_6502_6A,
3031     OPC_65C02_NOP11,    // $6B
3032     OPC_65C02_6C,
3033     OPC_6502_6D,
3034     OPC_6502_6E,
3035     OPC_Illegal,        // $6F: BBR6 currently unsupported
3036     OPC_6502_70,
3037     OPC_6502_71,
3038     OPC_65SC02_72,
3039     OPC_65C02_NOP11,    // $73
3040     OPC_65SC02_74,
3041     OPC_6502_75,
3042     OPC_6502_76,
3043     OPC_Illegal,        // $77: RMB7 currently unsupported
3044     OPC_6502_78,
3045     OPC_6502_79,
3046     OPC_65SC02_7A,
3047     OPC_65C02_NOP11,    // $7B
3048     OPC_65SC02_7C,
3049     OPC_6502_7D,
3050     OPC_6502_7E,
3051     OPC_Illegal,        // $7F: BBR7 currently unsupported
3052     OPC_65SC02_80,
3053     OPC_6502_81,
3054     OPC_65C02_NOP22,    // $82
3055     OPC_65C02_NOP11,    // $83
3056     OPC_6502_84,
3057     OPC_6502_85,
3058     OPC_6502_86,
3059     OPC_Illegal,        // $87: SMB0 currently unsupported
3060     OPC_6502_88,
3061     OPC_65SC02_89,
3062     OPC_6502_8A,
3063     OPC_65C02_NOP11,    // $8B
3064     OPC_6502_8C,
3065     OPC_6502_8D,
3066     OPC_6502_8E,
3067     OPC_Illegal,        // $8F: BBS0 currently unsupported
3068     OPC_6502_90,
3069     OPC_6502_91,
3070     OPC_65SC02_92,
3071     OPC_65C02_NOP11,    // $93
3072     OPC_6502_94,
3073     OPC_6502_95,
3074     OPC_6502_96,
3075     OPC_Illegal,        // $97: SMB1 currently unsupported
3076     OPC_6502_98,
3077     OPC_6502_99,
3078     OPC_6502_9A,
3079     OPC_65C02_NOP11,    // $9B
3080     OPC_65SC02_9C,
3081     OPC_6502_9D,
3082     OPC_65SC02_9E,
3083     OPC_Illegal,        // $9F: BBS1 currently unsupported
3084     OPC_6502_A0,
3085     OPC_6502_A1,
3086     OPC_6502_A2,
3087     OPC_65C02_NOP11,    // $A3
3088     OPC_6502_A4,
3089     OPC_6502_A5,
3090     OPC_6502_A6,
3091     OPC_Illegal,        // $A7: SMB2 currently unsupported
3092     OPC_6502_A8,
3093     OPC_6502_A9,
3094     OPC_6502_AA,
3095     OPC_65C02_NOP11,    // $AB
3096     OPC_6502_AC,
3097     OPC_6502_AD,
3098     OPC_6502_AE,
3099     OPC_Illegal,        // $AF: BBS2 currently unsupported
3100     OPC_6502_B0,
3101     OPC_6502_B1,
3102     OPC_65SC02_B2,
3103     OPC_65C02_NOP11,    // $B3
3104     OPC_6502_B4,
3105     OPC_6502_B5,
3106     OPC_6502_B6,
3107     OPC_Illegal,        // $B7: SMB3 currently unsupported
3108     OPC_6502_B8,
3109     OPC_6502_B9,
3110     OPC_6502_BA,
3111     OPC_65C02_NOP11,    // $BB
3112     OPC_6502_BC,
3113     OPC_6502_BD,
3114     OPC_6502_BE,
3115     OPC_Illegal,        // $BF: BBS3 currently unsupported
3116     OPC_6502_C0,
3117     OPC_6502_C1,
3118     OPC_65C02_NOP22,    // $C2
3119     OPC_65C02_NOP11,    // $C3
3120     OPC_6502_C4,
3121     OPC_6502_C5,
3122     OPC_6502_C6,
3123     OPC_Illegal,        // $C7: SMB4 currently unsupported
3124     OPC_6502_C8,
3125     OPC_6502_C9,
3126     OPC_6502_CA,
3127     OPC_Illegal,        // $CB: WAI currently unsupported
3128     OPC_6502_CC,
3129     OPC_6502_CD,
3130     OPC_6502_CE,
3131     OPC_Illegal,        // $CF: BBS4 currently unsupported
3132     OPC_6502_D0,
3133     OPC_6502_D1,
3134     OPC_65SC02_D2,
3135     OPC_65C02_NOP11,    // $D3
3136     OPC_65C02_NOP24,    // $D4
3137     OPC_6502_D5,
3138     OPC_6502_D6,
3139     OPC_Illegal,        // $D7: SMB5 currently unsupported
3140     OPC_6502_D8,
3141     OPC_6502_D9,
3142     OPC_65SC02_DA,
3143     OPC_Illegal,        // $DB: STP currently unsupported
3144     OPC_65C02_NOP34,    // $DC
3145     OPC_6502_DD,
3146     OPC_6502_DE,
3147     OPC_Illegal,        // $DF: BBS5 currently unsupported
3148     OPC_6502_E0,
3149     OPC_6502_E1,
3150     OPC_65C02_NOP22,    // $E2
3151     OPC_65C02_NOP11,    // $E3
3152     OPC_6502_E4,
3153     OPC_6502_E5,
3154     OPC_6502_E6,
3155     OPC_Illegal,        // $E7: SMB6 currently unsupported
3156     OPC_6502_E8,
3157     OPC_6502_E9,
3158     OPC_6502_EA,
3159     OPC_65C02_NOP11,    // $EB
3160     OPC_6502_EC,
3161     OPC_6502_ED,
3162     OPC_6502_EE,
3163     OPC_Illegal,        // $EF: BBS6 currently unsupported
3164     OPC_6502_F0,
3165     OPC_6502_F1,
3166     OPC_65SC02_F2,
3167     OPC_65C02_NOP11,    // $F3
3168     OPC_65C02_NOP24,    // $F4
3169     OPC_6502_F5,
3170     OPC_6502_F6,
3171     OPC_Illegal,        // $F7: SMB7 currently unsupported
3172     OPC_6502_F8,
3173     OPC_6502_F9,
3174     OPC_65SC02_FA,
3175     OPC_65C02_NOP11,    // $FB
3176     OPC_65C02_NOP34,    // $FC
3177     OPC_6502_FD,
3178     OPC_6502_FE,
3179     OPC_Illegal,        // $FF: BBS7 currently unsupported
3180 };
3181
3182
3183
3184 /* Tables with opcode handlers */
3185 static const OPFunc* Handlers[2] = {OP6502Table, OP65C02Table};
3186
3187
3188
3189 /*****************************************************************************/
3190 /*                                   Code                                    */
3191 /*****************************************************************************/
3192
3193
3194
3195 void IRQRequest (void)
3196 /* Generate an IRQ */
3197 {
3198     /* Remember the request */
3199     HaveIRQRequest = 1;
3200 }
3201
3202
3203
3204 void NMIRequest (void)
3205 /* Generate an NMI */
3206 {
3207     /* Remember the request */
3208     HaveNMIRequest = 1;
3209 }
3210
3211
3212
3213 void Reset (void)
3214 /* Generate a CPU RESET */
3215 {
3216     /* Reset the CPU */
3217     HaveIRQRequest = 0;
3218     HaveNMIRequest = 0;
3219
3220     /* Bits 5 and 4 aren't used, and always are 1! */
3221     Regs.SR = 0x30;
3222     Regs.PC = MemReadWord (0xFFFC);
3223 }
3224
3225
3226
3227 unsigned ExecuteInsn (void)
3228 /* Execute one CPU instruction */
3229 {
3230     /* If we have an NMI request, handle it */
3231     if (HaveNMIRequest) {
3232
3233         HaveNMIRequest = 0;
3234         PUSH (PCH);
3235         PUSH (PCL);
3236         PUSH (Regs.SR & ~BF);
3237         SET_IF (1);
3238         if (CPU != CPU_6502)
3239         {
3240             SET_DF (0);
3241         }
3242         Regs.PC = MemReadWord (0xFFFA);
3243         Cycles = 7;
3244
3245     } else if (HaveIRQRequest && GET_IF () == 0) {
3246
3247         HaveIRQRequest = 0;
3248         PUSH (PCH);
3249         PUSH (PCL);
3250         PUSH (Regs.SR & ~BF);
3251         SET_IF (1);
3252         if (CPU != CPU_6502)
3253         {
3254             SET_DF (0);
3255         }
3256         Regs.PC = MemReadWord (0xFFFE);
3257         Cycles = 7;
3258
3259     } else {
3260
3261         /* Normal instruction - read the next opcode */
3262         unsigned char OPC = MemReadByte (Regs.PC);
3263
3264         /* Execute it */
3265         Handlers[CPU][OPC] ();
3266     }
3267
3268     /* Count cycles */
3269     TotalCycles += Cycles;
3270
3271     /* Return the number of clock cycles needed by this insn */
3272     return Cycles;
3273 }
3274
3275
3276
3277 unsigned long GetCycles (void)
3278 /* Return the total number of cycles executed */
3279 {
3280     /* Return the total number of cycles */
3281     return TotalCycles;
3282 }