fork of PCE focusing on macplus, supporting DaynaPort SCSI network emulation
1/*****************************************************************************
2 * pce *
3 *****************************************************************************/
4
5/*****************************************************************************
6 * File name: src/cpu/e6502/opcodes.c *
7 * Created: 2004-05-03 by Hampa Hug <hampa@hampa.ch> *
8 * Copyright: (C) 2004-2022 Hampa Hug <hampa@hampa.ch> *
9 *****************************************************************************/
10
11/*****************************************************************************
12 * This program is free software. You can redistribute it and / or modify it *
13 * under the terms of the GNU General Public License version 2 as published *
14 * by the Free Software Foundation. *
15 * *
16 * This program is distributed in the hope that it will be useful, but *
17 * WITHOUT ANY WARRANTY, without even the implied warranty of *
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General *
19 * Public License for more details. *
20 *****************************************************************************/
21
22
23#include "e6502.h"
24#include "internal.h"
25
26#include <stdlib.h>
27#include <stdio.h>
28#include <stdarg.h>
29
30
31#define e6502_check_undef(c) \
32 do { \
33 if (((c)->flags & E6502_FLAG_UNDEF) == 0) { \
34 op_ud (c); \
35 return; \
36 } \
37 } while (0)
38
39
40unsigned char e6502_pull (e6502_t *c)
41{
42 unsigned char val;
43
44 e6502_set_s (c, e6502_get_s (c) + 1);
45 val = e6502_get_mem8 (c, 0x0100 + e6502_get_s (c));
46
47 return (val);
48}
49
50unsigned short e6502_pull16 (e6502_t *c)
51{
52 unsigned char val1, val2;
53
54 val1 = e6502_get_mem8 (c, 0x0100 + ((e6502_get_s (c) + 1) & 0xff));
55 val2 = e6502_get_mem8 (c, 0x0100 + ((e6502_get_s (c) + 2) & 0xff));
56 e6502_set_s (c, e6502_get_s (c) + 2);
57
58 return (e6502_mk_uint16 (val1, val2));
59}
60
61void e6502_push (e6502_t *c, unsigned char val)
62{
63 e6502_set_mem8 (c, 0x0100 + e6502_get_s (c), val);
64 e6502_set_s (c, e6502_get_s (c) - 1);
65}
66
67void e6502_push16 (e6502_t *c, unsigned short val)
68{
69 e6502_set_mem8 (c, 0x0100 + e6502_get_s (c), val >> 8);
70 e6502_set_mem8 (c, 0x0100 + ((e6502_get_s (c) - 1) & 0xff), val & 0xff);
71 e6502_set_s (c, e6502_get_s (c) - 2);
72}
73
74void e6502_trap (e6502_t *c, unsigned short addr)
75{
76 e6502_push16 (c, e6502_get_pc (c));
77 e6502_push (c, e6502_get_p (c));
78
79 e6502_set_if (c, 1);
80
81 e6502_set_pc (c, e6502_get_mem16 (c, addr));
82 e6502_set_clk (c, 0, 7);
83}
84
85
86static
87void e6502_op_adc (e6502_t *c, unsigned char s2)
88{
89 unsigned char s1, s3;
90 unsigned short d;
91
92 s1 = e6502_get_a (c);
93 s3 = e6502_get_cf (c);
94
95 if (e6502_get_df (c)) {
96 d = (s1 & 0x0f) + (s2 & 0x0f) + s3;
97 if (d > 0x09) {
98 d = ((d + 0x06) & 0x0f) + 0x10;
99 }
100 d = d + (s1 & 0xf0) + (s2 & 0xf0);
101
102 e6502_set_nf (c, d & 0x80);
103 e6502_set_vf (c, ~(s1 ^ s2) & (s1 ^ d) & 0x80);
104
105 if ((d & 0xfff0) > 0x90) {
106 d += 0x60;
107 }
108
109 e6502_set_zf (c, ((s1 + s2 + e6502_get_cf (c)) & 0xff) == 0);
110 }
111 else {
112 d = s1 + s2 + s3;
113
114 e6502_set_nf (c, d & 0x80);
115 e6502_set_zf (c, (d & 0xff) == 0);
116 e6502_set_vf (c, ~(s1 ^ s2) & (s1 ^ d) & 0x80);
117 }
118
119 e6502_set_cf (c, d & 0xff00);
120
121 e6502_set_a (c, d);
122}
123
124static
125void e6502_op_and (e6502_t *c, unsigned char s2)
126{
127 unsigned char s1;
128 unsigned short d;
129
130 s1 = e6502_get_a (c);
131 d = s1 & s2;
132 e6502_set_a (c, d);
133
134 e6502_set_nf (c, d & 0x80);
135 e6502_set_zf (c, (d & 0xff) == 0);
136}
137
138static
139unsigned char e6502_op_asl (e6502_t *c, unsigned char s)
140{
141 unsigned char d;
142
143 d = (s << 1) & 0xff;
144
145 e6502_set_nf (c, d & 0x80);
146 e6502_set_zf (c, d == 0);
147 e6502_set_cf (c, s & 0x80);
148
149 return (d);
150}
151
152static
153void e6502_op_bcc (e6502_t *c, int cond)
154{
155 unsigned short addr1, addr2;
156
157 e6502_get_inst1 (c);
158
159 if (cond) {
160 addr1 = e6502_get_pc (c);
161 addr2 = (addr1 + 2 + e6502_mk_sint16 (c->inst[1])) & 0xffff;
162 e6502_set_pc (c, addr2);
163 e6502_set_clk (c, 0, ((addr1 ^ addr2) & 0xff00) ? 4 : 3);
164 }
165 else {
166 e6502_set_clk (c, 2, 2);
167 }
168}
169
170static
171void e6502_op_bit (e6502_t *c, unsigned char s)
172{
173 e6502_set_nf (c, s & 0x80);
174 e6502_set_vf (c, s & 0x40);
175 e6502_set_zf (c, (e6502_get_a (c) & s) == 0);
176}
177
178static
179void e6502_op_cmp (e6502_t *c, unsigned char s)
180{
181 unsigned char d, a;
182
183 a = e6502_get_a (c);
184 d = (a - s) & 0xff;
185
186 e6502_set_nf (c, d & 0x80);
187 e6502_set_cf (c, s <= a);
188 e6502_set_zf (c, d == 0);
189}
190
191static
192void e6502_op_cpx (e6502_t *c, unsigned char s)
193{
194 unsigned char d, x;
195
196 x = e6502_get_x (c);
197 d = (x - s) & 0xff;
198
199 e6502_set_nf (c, d & 0x80);
200 e6502_set_cf (c, s <= x);
201 e6502_set_zf (c, d == 0);
202}
203
204static
205void e6502_op_cpy (e6502_t *c, unsigned char s)
206{
207 unsigned char d, y;
208
209 y = e6502_get_y (c);
210 d = (y - s) & 0xff;
211
212 e6502_set_nf (c, d & 0x80);
213 e6502_set_cf (c, s <= y);
214 e6502_set_zf (c, d == 0);
215}
216
217static
218unsigned char e6502_op_dec (e6502_t *c, unsigned char s)
219{
220 unsigned char d;
221
222 d = (s - 1) & 0xff;
223
224 e6502_set_nf (c, d & 0x80);
225 e6502_set_zf (c, d == 0);
226
227 return (d);
228}
229
230static
231void e6502_op_eor (e6502_t *c, unsigned char s2)
232{
233 unsigned char s1;
234 unsigned short d;
235
236 s1 = e6502_get_a (c);
237 d = s1 ^ s2;
238 e6502_set_a (c, d);
239
240 e6502_set_nf (c, d & 0x80);
241 e6502_set_zf (c, (d & 0xff) == 0);
242}
243
244static
245unsigned char e6502_op_inc (e6502_t *c, unsigned char s)
246{
247 unsigned char d;
248
249 d = (s + 1) & 0xff;
250
251 e6502_set_nf (c, d & 0x80);
252 e6502_set_zf (c, d == 0);
253
254 return (d);
255}
256
257static
258void e6502_op_lda (e6502_t *c, unsigned char s)
259{
260 e6502_set_a (c, s);
261 e6502_set_nf (c, s & 0x80);
262 e6502_set_zf (c, s == 0);
263}
264
265static
266void e6502_op_ldx (e6502_t *c, unsigned char s)
267{
268 e6502_set_x (c, s);
269 e6502_set_nf (c, s & 0x80);
270 e6502_set_zf (c, s == 0);
271}
272
273static
274void e6502_op_ldy (e6502_t *c, unsigned char s)
275{
276 e6502_set_y (c, s);
277 e6502_set_nf (c, s & 0x80);
278 e6502_set_zf (c, s == 0);
279}
280
281static
282unsigned char e6502_op_lsr (e6502_t *c, unsigned char s)
283{
284 unsigned char d;
285
286 d = (s >> 1) & 0x7f;
287
288 e6502_set_nf (c, 0);
289 e6502_set_zf (c, d == 0);
290 e6502_set_cf (c, s & 0x01);
291
292 return (d);
293}
294
295static
296void e6502_op_ora (e6502_t *c, unsigned char s2)
297{
298 unsigned char s1;
299 unsigned short d;
300
301 s1 = e6502_get_a (c);
302 d = s1 | s2;
303 e6502_set_a (c, d);
304
305 e6502_set_nf (c, d & 0x80);
306 e6502_set_zf (c, (d & 0xff) == 0);
307}
308
309static
310unsigned char e6502_op_rol (e6502_t *c, unsigned char s)
311{
312 unsigned char d;
313
314 d = ((s << 1) | e6502_get_cf (c)) & 0xff;
315
316 e6502_set_nf (c, d & 0x80);
317 e6502_set_zf (c, d == 0);
318 e6502_set_cf (c, s & 0x80);
319
320 return (d);
321}
322
323static
324unsigned char e6502_op_ror (e6502_t *c, unsigned char s)
325{
326 unsigned char d;
327
328 d = ((s >> 1) & 0x7f) | (e6502_get_cf (c) ? 0x80 : 0x00);
329
330 e6502_set_nf (c, d & 0x80);
331 e6502_set_zf (c, d == 0);
332 e6502_set_cf (c, s & 0x01);
333
334 return (d);
335}
336
337static
338void e6502_op_sbc (e6502_t *c, unsigned char s2)
339{
340 unsigned char s1, s3;
341 unsigned short d;
342
343 s1 = e6502_get_a (c);
344 s3 = (e6502_get_cf (c) == 0);
345
346 if (e6502_get_df (c)) {
347 d = (s1 & 0x0f) - (s2 & 0x0f) - s3;
348 if (d & 0x10) {
349 d = ((d - 0x06) & 0x0f) | ((s1 & 0xf0) - (s2 & 0xf0) - 0x10);
350 }
351 else {
352 d = (d & 0x0f) | ((s1 & 0xf0) - (s2 & 0xf0));
353 }
354 if (d & 0x100) {
355 d -= 0x60;
356 }
357 }
358 else {
359 d = s1 - s2 - s3;
360 }
361
362 e6502_set_nf (c, d & 0x80);
363 e6502_set_zf (c, (d & 0xff) == 0);
364 e6502_set_vf (c, (s1 ^ s2) & (s1 ^ d) & 0x80);
365 e6502_set_cf (c, (d & 0xff00) == 0);
366
367 e6502_set_a (c, d);
368}
369
370static
371void e6502_op_setnz (e6502_t *c, unsigned char s)
372{
373 e6502_set_nf (c, s & 0x80);
374 e6502_set_zf (c, (s & 0xff) == 0);
375}
376
377
378/*****************************************************************************
379 * opcodes
380 *****************************************************************************/
381
382/* Handle an undefined opcode */
383static void op_ud (e6502_t *c)
384{
385 if (e6502_hook_undefined (c)) {
386 return;
387 }
388
389 e6502_set_clk (c, 1, 1);
390}
391
392/* OP 00: BRK */
393static void op_00 (e6502_t *c)
394{
395 if (e6502_hook_brk (c)) {
396 e6502_set_clk (c, 1, 1);
397 return;
398 }
399
400 e6502_push16 (c, e6502_get_pc (c) + 2);
401 e6502_push (c, e6502_get_p (c) | E6502_FLG_B);
402
403 e6502_set_if (c, 1);
404 e6502_set_pc (c, e6502_get_mem16 (c, 0xfffe));
405
406 e6502_set_clk (c, 0, 7);
407}
408
409/* OP 01: ORA [[xx + X]] */
410static void op_01 (e6502_t *c)
411{
412 e6502_op_ora (c, e6502_get_idx_ind_x (c));
413 e6502_set_clk (c, 2, 6);
414}
415
416/* OP 03: SLO [[xx + X]] */
417static void op_03 (e6502_t *c)
418{
419 unsigned char tmp;
420
421 e6502_check_undef (c);
422
423 tmp = e6502_op_asl (c, e6502_get_idx_ind_x (c));
424 e6502_set_ea (c, tmp);
425 e6502_op_ora (c, tmp);
426
427 e6502_set_clk (c, 2, 8);
428}
429
430/* OP 04: NOP [xx] (undefined) */
431static void op_04 (e6502_t *c)
432{
433 e6502_check_undef (c);
434 e6502_get_zpg (c);
435 e6502_set_clk (c, 2, 3);
436}
437
438/* OP 05: ORA [xx] */
439static void op_05 (e6502_t *c)
440{
441 e6502_op_ora (c, e6502_get_zpg (c));
442 e6502_set_clk (c, 2, 3);
443}
444
445/* OP 06: ASL [xx] */
446static void op_06 (e6502_t *c)
447{
448 unsigned char tmp;
449
450 tmp = e6502_op_asl (c, e6502_get_zpg (c));
451 e6502_set_ea (c, tmp);
452
453 e6502_set_clk (c, 2, 5);
454}
455
456/* OP 07: SLO [xx] (undef) */
457static void op_07 (e6502_t *c)
458{
459 unsigned char tmp;
460
461 e6502_check_undef (c);
462
463 tmp = e6502_op_asl (c, e6502_get_zpg (c));
464 e6502_set_ea (c, tmp);
465 e6502_op_ora (c, tmp);
466
467 e6502_set_clk (c, 2, 5);
468}
469
470/* OP 08: PHP */
471static void op_08 (e6502_t *c)
472{
473 e6502_push (c, e6502_get_p (c));
474 e6502_set_clk (c, 1, 3);
475}
476
477/* OP 09: ORA #xx */
478static void op_09 (e6502_t *c)
479{
480 e6502_op_ora (c, e6502_get_imm (c));
481 e6502_set_clk (c, 2, 2);
482}
483
484/* OP 0A: ASL */
485static void op_0a (e6502_t *c)
486{
487 e6502_set_a (c, e6502_op_asl (c, e6502_get_a (c)));
488 e6502_set_clk (c, 1, 2);
489}
490
491/* OP 0C: NOP [xxxx] (undefined) */
492static void op_0c (e6502_t *c)
493{
494 e6502_check_undef (c);
495
496 e6502_get_abs (c);
497 e6502_set_clk (c, 3, 4);
498}
499
500/* OP 0D: ORA [xxxx] */
501static void op_0d (e6502_t *c)
502{
503 e6502_op_ora (c, e6502_get_abs (c));
504 e6502_set_clk (c, 3, 4);
505}
506
507/* OP 0E: ASL [xxxx] */
508static void op_0e (e6502_t *c)
509{
510 unsigned char tmp;
511
512 tmp = e6502_op_asl (c, e6502_get_abs (c));
513 e6502_set_ea (c, tmp);
514
515 e6502_set_clk (c, 3, 6);
516}
517
518/* OP 0F: SLO [xxxx] (undef) */
519static void op_0f (e6502_t *c)
520{
521 unsigned char tmp;
522
523 e6502_check_undef (c);
524
525 tmp = e6502_op_asl (c, e6502_get_abs (c));
526 e6502_set_ea (c, tmp);
527 e6502_op_ora (c, tmp);
528
529 e6502_set_clk (c, 2, 6);
530}
531
532/* OP 10: BPL */
533static void op_10 (e6502_t *c)
534{
535 e6502_op_bcc (c, e6502_get_nf (c) == 0);
536}
537
538/* OP 11: ORA [[xx] + Y] */
539static void op_11 (e6502_t *c)
540{
541 e6502_op_ora (c, e6502_get_ind_idx_y (c));
542 e6502_set_clk (c, 2, 5 + c->ea_page);
543}
544
545/* OP 13: SLO [[xx] + Y] */
546static void op_13 (e6502_t *c)
547{
548 unsigned char tmp;
549
550 e6502_check_undef (c);
551
552 tmp = e6502_op_asl (c, e6502_get_ind_idx_y (c));
553 e6502_set_ea (c, tmp);
554 e6502_op_ora (c, tmp);
555
556 e6502_set_clk (c, 2, 8);
557}
558
559/* OP 14: NOP [xx + X] (undefined) */
560static void op_14 (e6502_t *c)
561{
562 e6502_check_undef (c);
563 e6502_get_zpg_x (c);
564 e6502_set_clk (c, 2, 4);
565}
566
567/* OP 15: ORA [xx + X] */
568static void op_15 (e6502_t *c)
569{
570 e6502_op_ora (c, e6502_get_zpg_x (c));
571 e6502_set_clk (c, 2, 4);
572}
573
574/* OP 16: ASL [xx + X] */
575static void op_16 (e6502_t *c)
576{
577 unsigned char tmp;
578
579 tmp = e6502_op_asl (c, e6502_get_zpg_x (c));
580 e6502_set_ea (c, tmp);
581
582 e6502_set_clk (c, 2, 6);
583}
584
585/* OP 17: SLO [xx + X] (undef) */
586static void op_17 (e6502_t *c)
587{
588 unsigned char tmp;
589
590 e6502_check_undef (c);
591
592 tmp = e6502_op_asl (c, e6502_get_zpg_x (c));
593 e6502_set_ea (c, tmp);
594 e6502_op_ora (c, tmp);
595
596 e6502_set_clk (c, 2, 6);
597}
598
599/* OP 18: CLC */
600static void op_18 (e6502_t *c)
601{
602 e6502_set_cf (c, 0);
603 e6502_set_clk (c, 1, 2);
604}
605
606/* OP 19: ORA [xxxx + Y] */
607static void op_19 (e6502_t *c)
608{
609 e6502_op_ora (c, e6502_get_abs_y (c));
610 e6502_set_clk (c, 3, 4 + c->ea_page);
611}
612
613/* OP 1A: NOP (undef) */
614static void op_1a (e6502_t *c)
615{
616 e6502_check_undef (c);
617
618 e6502_set_clk (c, 1, 2);
619}
620
621/* OP 1B: SLO [xxxx + Y] (undef) */
622static void op_1b (e6502_t *c)
623{
624 unsigned char tmp;
625
626 e6502_check_undef (c);
627
628 tmp = e6502_op_asl (c, e6502_get_abs_y (c));
629 e6502_set_ea (c, tmp);
630 e6502_op_ora (c, tmp);
631
632 e6502_set_clk (c, 3, 7);
633}
634
635/* OP 1C: NOP [xxxx + X] (undefined) */
636static void op_1c (e6502_t *c)
637{
638 e6502_check_undef (c);
639
640 e6502_get_abs_x (c);
641 e6502_set_clk (c, 3, 4 + c->ea_page);
642}
643
644/* OP 1D: ORA [xxxx + X] */
645static void op_1d (e6502_t *c)
646{
647 e6502_op_ora (c, e6502_get_abs_x (c));
648 e6502_set_clk (c, 3, 4 + c->ea_page);
649}
650
651/* OP 1E: ASL [xxxx + X] */
652static void op_1e (e6502_t *c)
653{
654 unsigned char tmp;
655
656 tmp = e6502_op_asl (c, e6502_get_abs_x (c));
657 e6502_set_ea (c, tmp);
658
659 e6502_set_clk (c, 3, 7);
660}
661
662/* OP 1F: SLO [xxxx + X] (undef) */
663static void op_1f (e6502_t *c)
664{
665 unsigned char tmp;
666
667 e6502_check_undef (c);
668
669 tmp = e6502_op_asl (c, e6502_get_abs_x (c));
670 e6502_set_ea (c, tmp);
671 e6502_op_ora (c, tmp);
672
673 e6502_set_clk (c, 3, 7);
674}
675
676/* OP 20: JSR xxxx */
677static void op_20 (e6502_t *c)
678{
679 e6502_get_inst2 (c);
680
681 e6502_push16 (c, e6502_get_pc (c) + 2);
682 e6502_set_pc (c, e6502_mk_uint16 (c->inst[1], c->inst[2]));
683
684 e6502_set_clk (c, 0, 6);
685}
686
687/* OP 21: AND [[xx + X]] */
688static void op_21 (e6502_t *c)
689{
690 e6502_op_and (c, e6502_get_idx_ind_x (c));
691 e6502_set_clk (c, 2, 6);
692}
693
694/* OP 23: RLA [[xx + X]] */
695static void op_23 (e6502_t *c)
696{
697 unsigned char tmp;
698
699 e6502_check_undef (c);
700
701 tmp = e6502_op_asl (c, e6502_get_idx_ind_x (c));
702 e6502_set_ea (c, tmp);
703 e6502_op_and (c, tmp);
704
705 e6502_set_clk (c, 2, 8);
706}
707
708/* OP 24: BIT [xx] */
709static void op_24 (e6502_t *c)
710{
711 e6502_op_bit (c, e6502_get_zpg (c));
712 e6502_set_clk (c, 2, 3);
713}
714
715/* OP 25: AND [xx] */
716static void op_25 (e6502_t *c)
717{
718 e6502_op_and (c, e6502_get_zpg (c));
719 e6502_set_clk (c, 2, 3);
720}
721
722/* OP 26: ROL [xx] */
723static void op_26 (e6502_t *c)
724{
725 unsigned char tmp;
726
727 tmp = e6502_op_rol (c, e6502_get_zpg (c));
728 e6502_set_ea (c, tmp);
729
730 e6502_set_clk (c, 2, 5);
731}
732
733/* OP 27: RLA [xx] (undef) */
734static void op_27 (e6502_t *c)
735{
736 unsigned char tmp;
737
738 e6502_check_undef (c);
739
740 tmp = e6502_op_rol (c, e6502_get_zpg (c));
741 e6502_set_ea (c, tmp);
742 e6502_op_and (c, tmp);
743
744 e6502_set_clk (c, 2, 5);
745}
746
747/* OP 28: PLP */
748static void op_28 (e6502_t *c)
749{
750 e6502_set_p (c, e6502_pull (c));
751 e6502_set_bf (c, 0);
752 e6502_set_rf (c, 1);
753 e6502_set_clk (c, 1, 4);
754}
755
756/* OP 29: AND #xx */
757static void op_29 (e6502_t *c)
758{
759 e6502_op_and (c, e6502_get_imm (c));
760 e6502_set_clk (c, 2, 2);
761}
762
763/* OP 2A: ROL */
764static void op_2a (e6502_t *c)
765{
766 e6502_set_a (c, e6502_op_rol (c, e6502_get_a (c)));
767 e6502_set_clk (c, 1, 2);
768}
769
770/* OP 2C: BIT [xxxx] */
771static void op_2c (e6502_t *c)
772{
773 e6502_op_bit (c, e6502_get_abs (c));
774 e6502_set_clk (c, 3, 4);
775}
776
777/* OP 2D: AND [xxxx] */
778static void op_2d (e6502_t *c)
779{
780 e6502_op_and (c, e6502_get_abs (c));
781 e6502_set_clk (c, 3, 4);
782}
783
784/* OP 2E: ROL [xxxx] */
785static void op_2e (e6502_t *c)
786{
787 unsigned char tmp;
788
789 tmp = e6502_op_rol (c, e6502_get_abs (c));
790 e6502_set_ea (c, tmp);
791
792 e6502_set_clk (c, 3, 6);
793}
794
795/* OP 2F: RLA [xxxx] (undef) */
796static void op_2f (e6502_t *c)
797{
798 unsigned char tmp;
799
800 e6502_check_undef (c);
801
802 tmp = e6502_op_rol (c, e6502_get_abs (c));
803 e6502_set_ea (c, tmp);
804 e6502_op_and (c, tmp);
805
806 e6502_set_clk (c, 2, 6);
807}
808
809/* OP 30: BMI */
810static void op_30 (e6502_t *c)
811{
812 e6502_op_bcc (c, e6502_get_nf (c) != 0);
813}
814
815/* OP 31: AND [[xx] + Y] */
816static void op_31 (e6502_t *c)
817{
818 e6502_op_and (c, e6502_get_ind_idx_y (c));
819 e6502_set_clk (c, 2, 5 + c->ea_page);
820}
821
822/* OP 33: RLA [[xx] + Y] */
823static void op_33 (e6502_t *c)
824{
825 unsigned char tmp;
826
827 e6502_check_undef (c);
828
829 tmp = e6502_op_asl (c, e6502_get_ind_idx_y (c));
830 e6502_set_ea (c, tmp);
831 e6502_op_and (c, tmp);
832
833 e6502_set_clk (c, 2, 8);
834}
835
836/* OP 35: AND [xx + X] */
837static void op_35 (e6502_t *c)
838{
839 e6502_op_and (c, e6502_get_zpg_x (c));
840 e6502_set_clk (c, 2, 4);
841}
842
843/* OP 36: ROL [xx + X] */
844static void op_36 (e6502_t *c)
845{
846 unsigned char tmp;
847
848 tmp = e6502_op_rol (c, e6502_get_zpg_x (c));
849 e6502_set_ea (c, tmp);
850
851 e6502_set_clk (c, 2, 6);
852}
853
854/* OP 37: RLA [xx + X] (undef) */
855static void op_37 (e6502_t *c)
856{
857 unsigned char tmp;
858
859 e6502_check_undef (c);
860
861 tmp = e6502_op_rol (c, e6502_get_zpg_x (c));
862 e6502_set_ea (c, tmp);
863 e6502_op_and (c, tmp);
864
865 e6502_set_clk (c, 2, 6);
866}
867
868/* OP 38: SEC */
869static void op_38 (e6502_t *c)
870{
871 e6502_set_cf (c, 1);
872 e6502_set_clk (c, 1, 2);
873}
874
875/* OP 39: AND [xxxx + Y] */
876static void op_39 (e6502_t *c)
877{
878 e6502_op_and (c, e6502_get_abs_y (c));
879 e6502_set_clk (c, 3, 4 + c->ea_page);
880}
881
882/* OP 3B: RLA [xxxx + Y] (undef) */
883static void op_3b (e6502_t *c)
884{
885 unsigned char tmp;
886
887 e6502_check_undef (c);
888
889 tmp = e6502_op_rol (c, e6502_get_abs_y (c));
890 e6502_set_ea (c, tmp);
891 e6502_op_and (c, tmp);
892
893 e6502_set_clk (c, 2, 7);
894}
895
896/* OP 3D: AND [xxxx + X] */
897static void op_3d (e6502_t *c)
898{
899 e6502_op_and (c, e6502_get_abs_x (c));
900 e6502_set_clk (c, 3, 4 + c->ea_page);
901}
902
903/* OP 3E: ROL [xxxx + X] */
904static void op_3e (e6502_t *c)
905{
906 unsigned char tmp;
907
908 tmp = e6502_op_rol (c, e6502_get_abs_x (c));
909 e6502_set_ea (c, tmp);
910
911 e6502_set_clk (c, 3, 7);
912}
913
914/* OP 3F: RLA [xxxx + X] (undef) */
915static void op_3f (e6502_t *c)
916{
917 unsigned char tmp;
918
919 e6502_check_undef (c);
920
921 tmp = e6502_op_rol (c, e6502_get_abs_x (c));
922 e6502_set_ea (c, tmp);
923 e6502_op_and (c, tmp);
924
925 e6502_set_clk (c, 2, 7);
926}
927
928/* OP 40: RTI */
929static void op_40 (e6502_t *c)
930{
931 e6502_set_p (c, e6502_pull (c));
932 e6502_set_pc (c, e6502_pull16 (c));
933 e6502_set_rf (c, 1);
934 e6502_set_bf (c, 0);
935 e6502_set_clk (c, 0, 6);
936
937 c->check_irq = (e6502_get_if (c) == 0);
938}
939
940/* OP 41: EOR [[xx + X]] */
941static void op_41 (e6502_t *c)
942{
943 e6502_op_eor (c, e6502_get_idx_ind_x (c));
944 e6502_set_clk (c, 2, 6);
945}
946
947/* OP 43: SRE [[xx + X]] (undef) */
948static void op_43 (e6502_t *c)
949{
950 unsigned char tmp;
951
952 e6502_check_undef (c);
953
954 tmp = e6502_op_lsr (c, e6502_get_idx_ind_x (c));
955 e6502_set_ea (c, tmp);
956 e6502_op_eor (c, tmp);
957
958 e6502_set_clk (c, 2, 8);
959}
960
961/* OP 45: EOR [xx] */
962static void op_45 (e6502_t *c)
963{
964 e6502_op_eor (c, e6502_get_zpg (c));
965 e6502_set_clk (c, 2, 3);
966}
967
968/* OP 46: LSR [xx] */
969static void op_46 (e6502_t *c)
970{
971 unsigned char tmp;
972
973 tmp = e6502_op_lsr (c, e6502_get_zpg (c));
974 e6502_set_ea (c, tmp);
975
976 e6502_set_clk (c, 2, 5);
977}
978
979/* OP 47: SRE [xx] (undef) */
980static void op_47 (e6502_t *c)
981{
982 unsigned char tmp;
983
984 e6502_check_undef (c);
985
986 tmp = e6502_op_lsr (c, e6502_get_zpg (c));
987 e6502_set_ea (c, tmp);
988 e6502_op_eor (c, tmp);
989
990 e6502_set_clk (c, 2, 5);
991}
992
993/* OP 48: PHA */
994static void op_48 (e6502_t *c)
995{
996 e6502_push (c, e6502_get_a (c));
997 e6502_set_clk (c, 1, 3);
998}
999
1000/* OP 49: EOR #xx */
1001static void op_49 (e6502_t *c)
1002{
1003 e6502_op_eor (c, e6502_get_imm (c));
1004 e6502_set_clk (c, 2, 2);
1005}
1006
1007/* OP 4A: LSR */
1008static void op_4a (e6502_t *c)
1009{
1010 e6502_set_a (c, e6502_op_lsr (c, e6502_get_a (c)));
1011 e6502_set_clk (c, 1, 2);
1012}
1013
1014/* OP 4C: JMP xxxx */
1015static void op_4c (e6502_t *c)
1016{
1017 e6502_get_inst2 (c);
1018 e6502_set_pc (c, e6502_mk_uint16 (c->inst[1], c->inst[2]));
1019 e6502_set_clk (c, 0, 3);
1020}
1021
1022/* OP 4D: EOR [xxxx] */
1023static void op_4d (e6502_t *c)
1024{
1025 e6502_op_eor (c, e6502_get_abs (c));
1026 e6502_set_clk (c, 3, 4);
1027}
1028
1029/* OP 4E: LSR [xxxx] */
1030static void op_4e (e6502_t *c)
1031{
1032 unsigned char tmp;
1033
1034 tmp = e6502_op_lsr (c, e6502_get_abs (c));
1035 e6502_set_ea (c, tmp);
1036
1037 e6502_set_clk (c, 3, 6);
1038}
1039
1040/* OP 4F: SRE [xxxx] (undef) */
1041static void op_4f (e6502_t *c)
1042{
1043 unsigned char tmp;
1044
1045 e6502_check_undef (c);
1046
1047 tmp = e6502_op_lsr (c, e6502_get_abs (c));
1048 e6502_set_ea (c, tmp);
1049 e6502_op_eor (c, tmp);
1050
1051 e6502_set_clk (c, 3, 6);
1052}
1053
1054/* OP 50: BVC */
1055static void op_50 (e6502_t *c)
1056{
1057 e6502_op_bcc (c, e6502_get_vf (c) == 0);
1058}
1059
1060/* OP 51: EOR [[xx] + Y] */
1061static void op_51 (e6502_t *c)
1062{
1063 e6502_op_eor (c, e6502_get_ind_idx_y (c));
1064 e6502_set_clk (c, 2, 5 + c->ea_page);
1065}
1066
1067/* OP 53: SRE [[xx] + Y] (undef) */
1068static void op_53 (e6502_t *c)
1069{
1070 unsigned char tmp;
1071
1072 e6502_check_undef (c);
1073
1074 tmp = e6502_op_lsr (c, e6502_get_ind_idx_y (c));
1075 e6502_set_ea (c, tmp);
1076 e6502_op_eor (c, tmp);
1077
1078 e6502_set_clk (c, 2, 8);
1079}
1080
1081/* OP 55: EOR [xx + X] */
1082static void op_55 (e6502_t *c)
1083{
1084 e6502_op_eor (c, e6502_get_zpg_x (c));
1085 e6502_set_clk (c, 2, 4);
1086}
1087
1088/* OP 56: LSR [xx + X] */
1089static void op_56 (e6502_t *c)
1090{
1091 unsigned char tmp;
1092
1093 tmp = e6502_op_lsr (c, e6502_get_zpg_x (c));
1094 e6502_set_ea (c, tmp);
1095
1096 e6502_set_clk (c, 2, 6);
1097}
1098
1099/* OP 57: SRE [xx + X] (undef) */
1100static void op_57 (e6502_t *c)
1101{
1102 unsigned char tmp;
1103
1104 e6502_check_undef (c);
1105
1106 tmp = e6502_op_lsr (c, e6502_get_zpg_x (c));
1107 e6502_set_ea (c, tmp);
1108 e6502_op_eor (c, tmp);
1109
1110 e6502_set_clk (c, 2, 6);
1111}
1112
1113/* OP 58: CLI */
1114static void op_58 (e6502_t *c)
1115{
1116 e6502_set_if (c, 0);
1117 e6502_set_clk (c, 1, 2);
1118}
1119
1120/* OP 59: EOR [xxxx + Y] */
1121static void op_59 (e6502_t *c)
1122{
1123 e6502_op_eor (c, e6502_get_abs_y (c));
1124 e6502_set_clk (c, 3, 4 + c->ea_page);
1125}
1126
1127/* OP 5B: SRE [xxxx + Y] (undef) */
1128static void op_5b (e6502_t *c)
1129{
1130 unsigned char tmp;
1131
1132 e6502_check_undef (c);
1133
1134 tmp = e6502_op_lsr (c, e6502_get_abs_y (c));
1135 e6502_set_ea (c, tmp);
1136 e6502_op_eor (c, tmp);
1137
1138 e6502_set_clk (c, 3, 7);
1139}
1140
1141/* OP 5D: EOR [xxxx + X] */
1142static void op_5d (e6502_t *c)
1143{
1144 e6502_op_eor (c, e6502_get_abs_x (c));
1145 e6502_set_clk (c, 3, 4 + c->ea_page);
1146}
1147
1148/* OP 5E: LSR [xxxx + X] */
1149static void op_5e (e6502_t *c)
1150{
1151 unsigned char tmp;
1152
1153 tmp = e6502_op_lsr (c, e6502_get_abs_x (c));
1154 e6502_set_ea (c, tmp);
1155
1156 e6502_set_clk (c, 3, 7);
1157}
1158
1159/* OP 5F: SRE [xxxx + X] (undef) */
1160static void op_5f (e6502_t *c)
1161{
1162 unsigned char tmp;
1163
1164 e6502_check_undef (c);
1165
1166 tmp = e6502_op_lsr (c, e6502_get_abs_x (c));
1167 e6502_set_ea (c, tmp);
1168 e6502_op_eor (c, tmp);
1169
1170 e6502_set_clk (c, 3, 7);
1171}
1172
1173/* OP 60: RTS */
1174static void op_60 (e6502_t *c)
1175{
1176 e6502_set_pc (c, e6502_pull16 (c) + 1);
1177
1178 e6502_set_clk (c, 0, 6);
1179}
1180
1181/* OP 61: ADC [[xx + X]] */
1182static void op_61 (e6502_t *c)
1183{
1184 e6502_op_adc (c, e6502_get_idx_ind_x (c));
1185 e6502_set_clk (c, 2, 6);
1186}
1187
1188/* OP 65: ADC [xx] */
1189static void op_65 (e6502_t *c)
1190{
1191 e6502_op_adc (c, e6502_get_zpg (c));
1192 e6502_set_clk (c, 2, 3);
1193}
1194
1195/* OP 66: ROR [xx] */
1196static void op_66 (e6502_t *c)
1197{
1198 unsigned char tmp;
1199
1200 tmp = e6502_op_ror (c, e6502_get_zpg (c));
1201 e6502_set_ea (c, tmp);
1202
1203 e6502_set_clk (c, 2, 5);
1204}
1205
1206/* OP 68: PLA */
1207static void op_68 (e6502_t *c)
1208{
1209 unsigned char val;
1210
1211 val = e6502_pull (c);
1212
1213 e6502_set_a (c, val);
1214 e6502_set_nf (c, val & 0x80);
1215 e6502_set_zf (c, (val & 0xff) == 0);
1216
1217 e6502_set_clk (c, 1, 4);
1218}
1219
1220/* OP 69: ADC #xx */
1221static void op_69 (e6502_t *c)
1222{
1223 e6502_op_adc (c, e6502_get_imm (c));
1224 e6502_set_clk (c, 2, 2);
1225}
1226
1227/* OP 6A: ROR */
1228static void op_6a (e6502_t *c)
1229{
1230 e6502_set_a (c, e6502_op_ror (c, e6502_get_a (c)));
1231 e6502_set_clk (c, 1, 2);
1232}
1233
1234/* OP 6C: JMP [xxxx] */
1235static void op_6c (e6502_t *c)
1236{
1237 unsigned ial, iah, adl, adh;
1238
1239 e6502_get_inst2 (c);
1240
1241 ial = c->inst[1];
1242 iah = (unsigned) c->inst[2] << 8;
1243
1244 adl = e6502_get_mem8 (c, iah | ial);
1245 adh = e6502_get_mem8 (c, iah | ((ial + 1) & 0xff));
1246
1247 e6502_set_pc (c, (adh << 8) | adl);
1248 e6502_set_clk (c, 0, 5);
1249}
1250
1251/* OP 6D: ADC [xxxx] */
1252static void op_6d (e6502_t *c)
1253{
1254 e6502_op_adc (c, e6502_get_abs (c));
1255 e6502_set_clk (c, 3, 4);
1256}
1257
1258/* OP 6E: ROR [xxxx] */
1259static void op_6e (e6502_t *c)
1260{
1261 unsigned char tmp;
1262
1263 tmp = e6502_op_ror (c, e6502_get_abs (c));
1264 e6502_set_ea (c, tmp);
1265
1266 e6502_set_clk (c, 3, 6);
1267}
1268
1269/* OP 70: BVS */
1270static void op_70 (e6502_t *c)
1271{
1272 e6502_op_bcc (c, e6502_get_vf (c) != 0);
1273}
1274
1275/* OP 71: ADC [[xx] + Y] */
1276static void op_71 (e6502_t *c)
1277{
1278 e6502_op_adc (c, e6502_get_ind_idx_y (c));
1279 e6502_set_clk (c, 2, 5 + c->ea_page);
1280}
1281
1282/* OP 75: ADC [xx + X] */
1283static void op_75 (e6502_t *c)
1284{
1285 e6502_op_adc (c, e6502_get_zpg_x (c));
1286 e6502_set_clk (c, 2, 4);
1287}
1288
1289/* OP 76: ROR [xx + X] */
1290static void op_76 (e6502_t *c)
1291{
1292 unsigned char tmp;
1293
1294 tmp = e6502_op_ror (c, e6502_get_zpg_x (c));
1295 e6502_set_ea (c, tmp);
1296
1297 e6502_set_clk (c, 2, 6);
1298}
1299
1300/* OP 78: SEI */
1301static void op_78 (e6502_t *c)
1302{
1303 e6502_set_if (c, 1);
1304 e6502_set_clk (c, 1, 2);
1305}
1306
1307/* OP 79: ADC [xxxx + Y] */
1308static void op_79 (e6502_t *c)
1309{
1310 e6502_op_adc (c, e6502_get_abs_y (c));
1311 e6502_set_clk (c, 3, 4 + c->ea_page);
1312}
1313
1314/* OP 7D: ADC [xxxx + X] */
1315static void op_7d (e6502_t *c)
1316{
1317 e6502_op_adc (c, e6502_get_abs_x (c));
1318 e6502_set_clk (c, 3, 4 + c->ea_page);
1319}
1320
1321/* OP 7E: ROR [xxxx + X] */
1322static void op_7e (e6502_t *c)
1323{
1324 unsigned char tmp;
1325
1326 tmp = e6502_op_ror (c, e6502_get_abs_x (c));
1327 e6502_set_ea (c, tmp);
1328
1329 e6502_set_clk (c, 3, 7);
1330}
1331
1332/* OP 80: NOP #xx */
1333static void op_80 (e6502_t *c)
1334{
1335 e6502_get_imm (c);
1336 e6502_set_clk (c, 2, 2);
1337}
1338
1339/* OP 81: STA [[xx + X]] */
1340static void op_81 (e6502_t *c)
1341{
1342 e6502_get_ea_idx_ind_x (c);
1343 e6502_set_ea (c, e6502_get_a (c));
1344 e6502_set_clk (c, 2, 6);
1345}
1346
1347/* OP 84: STY [xx] */
1348static void op_84 (e6502_t *c)
1349{
1350 e6502_get_ea_zpg (c);
1351 e6502_set_ea (c, e6502_get_y (c));
1352 e6502_set_clk (c, 2, 3);
1353}
1354
1355/* OP 85: STA [xx] */
1356static void op_85 (e6502_t *c)
1357{
1358 e6502_get_ea_zpg (c);
1359 e6502_set_ea (c, e6502_get_a (c));
1360 e6502_set_clk (c, 2, 3);
1361}
1362
1363/* OP 86: STX [xx] */
1364static void op_86 (e6502_t *c)
1365{
1366 e6502_get_ea_zpg (c);
1367 e6502_set_ea (c, e6502_get_x (c));
1368 e6502_set_clk (c, 2, 3);
1369}
1370
1371/* OP 88: DEY */
1372static void op_88 (e6502_t *c)
1373{
1374 e6502_set_y (c, e6502_op_dec (c, e6502_get_y (c)));
1375 e6502_set_clk (c, 1, 2);
1376}
1377
1378/* OP 89: NOP #xx (undefined) */
1379static void op_89 (e6502_t *c)
1380{
1381 e6502_check_undef (c);
1382 e6502_get_imm (c);
1383 e6502_set_clk (c, 2, 2);
1384}
1385
1386/* OP 8A: TXA */
1387static void op_8a (e6502_t *c)
1388{
1389 e6502_op_setnz (c, e6502_get_x (c));
1390 e6502_set_a (c, e6502_get_x (c));
1391 e6502_set_clk (c, 1, 2);
1392}
1393
1394/* OP 8C: STY [xxxx] */
1395static void op_8c (e6502_t *c)
1396{
1397 e6502_get_ea_abs (c);
1398 e6502_set_ea (c, e6502_get_y (c));
1399 e6502_set_clk (c, 3, 4);
1400}
1401
1402/* OP 8D: STA [xxxx] */
1403static void op_8d (e6502_t *c)
1404{
1405 e6502_get_ea_abs (c);
1406 e6502_set_ea (c, e6502_get_a (c));
1407 e6502_set_clk (c, 3, 4);
1408}
1409
1410/* OP 8E: STX [xxxx] */
1411static void op_8e (e6502_t *c)
1412{
1413 e6502_get_ea_abs (c);
1414 e6502_set_ea (c, e6502_get_x (c));
1415 e6502_set_clk (c, 3, 4);
1416}
1417
1418/* OP 90: BCC */
1419static void op_90 (e6502_t *c)
1420{
1421 e6502_op_bcc (c, e6502_get_cf (c) == 0);
1422}
1423
1424/* OP 91: STA [[xx] + Y] */
1425static void op_91 (e6502_t *c)
1426{
1427 e6502_get_ea_ind_idx_y (c);
1428 e6502_set_ea (c, e6502_get_a (c));
1429 e6502_set_clk (c, 2, 6);
1430}
1431
1432/* OP 94: STY [xx + X] */
1433static void op_94 (e6502_t *c)
1434{
1435 e6502_get_ea_zpg_x (c);
1436 e6502_set_ea (c, e6502_get_y (c));
1437 e6502_set_clk (c, 2, 4);
1438}
1439
1440/* OP 95: STA [xx + X] */
1441static void op_95 (e6502_t *c)
1442{
1443 e6502_get_ea_zpg_x (c);
1444 e6502_set_ea (c, e6502_get_a (c));
1445 e6502_set_clk (c, 2, 4);
1446}
1447
1448/* OP 96: STX [xx + Y] */
1449static void op_96 (e6502_t *c)
1450{
1451 e6502_get_ea_zpg_y (c);
1452 e6502_set_ea (c, e6502_get_x (c));
1453 e6502_set_clk (c, 2, 4);
1454}
1455
1456/* OP 98: TYA */
1457static void op_98 (e6502_t *c)
1458{
1459 e6502_op_setnz (c, e6502_get_y (c));
1460 e6502_set_a (c, e6502_get_y (c));
1461 e6502_set_clk (c, 1, 2);
1462}
1463
1464/* OP 99: STA [xxxx + Y] */
1465static void op_99 (e6502_t *c)
1466{
1467 e6502_get_ea_abs_y (c);
1468 e6502_set_ea (c, e6502_get_a (c));
1469 e6502_set_clk (c, 3, 5);
1470}
1471
1472/* OP 89: TXS */
1473static void op_9a (e6502_t *c)
1474{
1475 e6502_set_s (c, e6502_get_x (c));
1476 e6502_set_clk (c, 1, 2);
1477}
1478
1479/* OP 9D: STA [xxxx + X] */
1480static void op_9d (e6502_t *c)
1481{
1482 e6502_get_ea_abs_x (c);
1483 e6502_set_ea (c, e6502_get_a (c));
1484 e6502_set_clk (c, 3, 5);
1485}
1486
1487/* OP A0: LDY #xx */
1488static void op_a0 (e6502_t *c)
1489{
1490 e6502_op_ldy (c, e6502_get_imm (c));
1491 e6502_set_clk (c, 2, 2);
1492}
1493
1494/* OP A1: LDA [[xx + X]] */
1495static void op_a1 (e6502_t *c)
1496{
1497 e6502_op_lda (c, e6502_get_idx_ind_x (c));
1498 e6502_set_clk (c, 2, 6);
1499}
1500
1501/* OP A2: LDX #xx */
1502static void op_a2 (e6502_t *c)
1503{
1504 e6502_op_ldx (c, e6502_get_imm (c));
1505 e6502_set_clk (c, 2, 2);
1506}
1507
1508/* OP A4: LDY [xx] */
1509static void op_a4 (e6502_t *c)
1510{
1511 e6502_op_ldy (c, e6502_get_zpg (c));
1512 e6502_set_clk (c, 2, 3);
1513}
1514
1515/* OP A5: LDA [xx] */
1516static void op_a5 (e6502_t *c)
1517{
1518 e6502_op_lda (c, e6502_get_zpg (c));
1519 e6502_set_clk (c, 2, 3);
1520}
1521
1522/* OP A6: LDX [xx] */
1523static void op_a6 (e6502_t *c)
1524{
1525 e6502_op_ldx (c, e6502_get_zpg (c));
1526 e6502_set_clk (c, 2, 3);
1527}
1528
1529/* OP A8: TAY */
1530static void op_a8 (e6502_t *c)
1531{
1532 e6502_op_setnz (c, e6502_get_a (c));
1533 e6502_set_y (c, e6502_get_a (c));
1534 e6502_set_clk (c, 1, 2);
1535}
1536
1537/* OP A9: LDA #xx */
1538static void op_a9 (e6502_t *c)
1539{
1540 e6502_op_lda (c, e6502_get_imm (c));
1541 e6502_set_clk (c, 2, 2);
1542}
1543
1544/* OP AA: TAX */
1545static void op_aa (e6502_t *c)
1546{
1547 e6502_op_setnz (c, e6502_get_a (c));
1548 e6502_set_x (c, e6502_get_a (c));
1549 e6502_set_clk (c, 1, 2);
1550}
1551
1552/* OP AC: LDY [xxxx] */
1553static void op_ac (e6502_t *c)
1554{
1555 e6502_op_ldy (c, e6502_get_abs (c));
1556 e6502_set_clk (c, 3, 4);
1557}
1558
1559/* OP AD: LDA [xxxx] */
1560static void op_ad (e6502_t *c)
1561{
1562 e6502_op_lda (c, e6502_get_abs (c));
1563 e6502_set_clk (c, 3, 4);
1564}
1565
1566/* OP AE: LDX [xxxx] */
1567static void op_ae (e6502_t *c)
1568{
1569 e6502_op_ldx (c, e6502_get_abs (c));
1570 e6502_set_clk (c, 3, 4);
1571}
1572
1573/* OP B0: BCS */
1574static void op_b0 (e6502_t *c)
1575{
1576 e6502_op_bcc (c, e6502_get_cf (c) != 0);
1577}
1578
1579/* OP B1: LDA [[xx] + Y] */
1580static void op_b1 (e6502_t *c)
1581{
1582 e6502_op_lda (c, e6502_get_ind_idx_y (c));
1583 e6502_set_clk (c, 2, 5 + c->ea_page);
1584}
1585
1586/* OP B4: LDY [xx + X] */
1587static void op_b4 (e6502_t *c)
1588{
1589 e6502_op_ldy (c, e6502_get_zpg_x (c));
1590 e6502_set_clk (c, 2, 4);
1591}
1592
1593/* OP B5: LDA [xx + X] */
1594static void op_b5 (e6502_t *c)
1595{
1596 e6502_op_lda (c, e6502_get_zpg_x (c));
1597 e6502_set_clk (c, 2, 4);
1598}
1599
1600/* OP B6: LDX [xx + Y] */
1601static void op_b6 (e6502_t *c)
1602{
1603 e6502_op_ldx (c, e6502_get_zpg_y (c));
1604 e6502_set_clk (c, 2, 4);
1605}
1606
1607/* OP B8: CLV */
1608static void op_b8 (e6502_t *c)
1609{
1610 e6502_set_vf (c, 0);
1611 e6502_set_clk (c, 1, 2);
1612}
1613
1614/* OP B9: LDA [xxxx + Y] */
1615static void op_b9 (e6502_t *c)
1616{
1617 e6502_op_lda (c, e6502_get_abs_y (c));
1618 e6502_set_clk (c, 3, 4 + c->ea_page);
1619}
1620
1621/* OP BA: TSX */
1622static void op_ba (e6502_t *c)
1623{
1624 e6502_op_setnz (c, e6502_get_s (c));
1625 e6502_set_x (c, e6502_get_s (c));
1626 e6502_set_clk (c, 1, 2);
1627}
1628
1629/* OP BC: LDY [xxxx + X] */
1630static void op_bc (e6502_t *c)
1631{
1632 e6502_op_ldy (c, e6502_get_abs_x (c));
1633 e6502_set_clk (c, 3, 4 + c->ea_page);
1634}
1635
1636/* OP BD: LDA [xxxx + X] */
1637static void op_bd (e6502_t *c)
1638{
1639 e6502_op_lda (c, e6502_get_abs_x (c));
1640 e6502_set_clk (c, 3, 4 + c->ea_page);
1641}
1642
1643/* OP BD: LDX [xxxx + Y] */
1644static void op_be (e6502_t *c)
1645{
1646 e6502_op_ldx (c, e6502_get_abs_y (c));
1647 e6502_set_clk (c, 3, 4 + c->ea_page);
1648}
1649
1650/* OP C0: CPY #xx */
1651static void op_c0 (e6502_t *c)
1652{
1653 e6502_op_cpy (c, e6502_get_imm (c));
1654 e6502_set_clk (c, 2, 2);
1655}
1656
1657/* OP C1: CMP [[xx + X]] */
1658static void op_c1 (e6502_t *c)
1659{
1660 e6502_op_cmp (c, e6502_get_idx_ind_x (c));
1661 e6502_set_clk (c, 2, 6);
1662}
1663
1664/* OP C4: CPY [xx] */
1665static void op_c4 (e6502_t *c)
1666{
1667 e6502_op_cpy (c, e6502_get_zpg (c));
1668 e6502_set_clk (c, 2, 3);
1669}
1670
1671/* OP C5: CMP [xx] */
1672static void op_c5 (e6502_t *c)
1673{
1674 e6502_op_cmp (c, e6502_get_zpg (c));
1675 e6502_set_clk (c, 2, 3);
1676}
1677
1678/* OP C6: DEC [xx] */
1679static void op_c6 (e6502_t *c)
1680{
1681 unsigned char tmp;
1682
1683 tmp = e6502_op_dec (c, e6502_get_zpg (c));
1684 e6502_set_ea (c, tmp);
1685
1686 e6502_set_clk (c, 2, 5);
1687}
1688
1689/* OP C7: DCM [xx] (undefined) */
1690static void op_c7 (e6502_t *c)
1691{
1692 unsigned char tmp;
1693
1694 e6502_check_undef (c);
1695
1696 tmp = e6502_op_dec (c, e6502_get_zpg (c));
1697 e6502_set_ea (c, tmp);
1698
1699 e6502_op_cmp (c, tmp);
1700
1701 e6502_set_clk (c, 2, 5);
1702}
1703
1704/* OP C8: INY */
1705static void op_c8 (e6502_t *c)
1706{
1707 e6502_set_y (c, e6502_op_inc (c, e6502_get_y (c)));
1708 e6502_set_clk (c, 1, 2);
1709}
1710
1711/* OP C9: CMP #xx */
1712static void op_c9 (e6502_t *c)
1713{
1714 e6502_op_cmp (c, e6502_get_imm (c));
1715 e6502_set_clk (c, 2, 2);
1716}
1717
1718/* OP CA: DEX */
1719static void op_ca (e6502_t *c)
1720{
1721 e6502_set_x (c, e6502_op_dec (c, e6502_get_x (c)));
1722 e6502_set_clk (c, 1, 2);
1723}
1724
1725/* OP CC: CPY [xxxx] */
1726static void op_cc (e6502_t *c)
1727{
1728 e6502_op_cpy (c, e6502_get_abs (c));
1729 e6502_set_clk (c, 3, 4);
1730}
1731
1732/* OP CD: CMP [xxxx] */
1733static void op_cd (e6502_t *c)
1734{
1735 e6502_op_cmp (c, e6502_get_abs (c));
1736 e6502_set_clk (c, 3, 4);
1737}
1738
1739/* OP CE: DEC [xxxx] */
1740static void op_ce (e6502_t *c)
1741{
1742 unsigned char tmp;
1743
1744 tmp = e6502_op_dec (c, e6502_get_abs (c));
1745 e6502_set_ea (c, tmp);
1746
1747 e6502_set_clk (c, 3, 6);
1748}
1749
1750/* OP D1: CMP [[xx] + Y] */
1751static void op_d1 (e6502_t *c)
1752{
1753 e6502_op_cmp (c, e6502_get_ind_idx_y (c));
1754 e6502_set_clk (c, 2, 5 + c->ea_page);
1755}
1756
1757/* OP D5: CMP [xx + X] */
1758static void op_d5 (e6502_t *c)
1759{
1760 e6502_op_cmp (c, e6502_get_zpg_x (c));
1761 e6502_set_clk (c, 2, 4);
1762}
1763
1764/* OP D6: DEC [xx + X] */
1765static void op_d6 (e6502_t *c)
1766{
1767 unsigned char tmp;
1768
1769 tmp = e6502_op_dec (c, e6502_get_zpg_x (c));
1770 e6502_set_ea (c, tmp);
1771
1772 e6502_set_clk (c, 2, 6);
1773}
1774
1775/* OP D9: CMP [xxxx + Y] */
1776static void op_d9 (e6502_t *c)
1777{
1778 e6502_op_cmp (c, e6502_get_abs_y (c));
1779 e6502_set_clk (c, 3, 4 + c->ea_page);
1780}
1781
1782/* OP DD: CMP [xxxx + X] */
1783static void op_dd (e6502_t *c)
1784{
1785 e6502_op_cmp (c, e6502_get_abs_x (c));
1786 e6502_set_clk (c, 3, 4 + c->ea_page);
1787}
1788
1789/* OP DE: DEC [xxxx + X] */
1790static void op_de (e6502_t *c)
1791{
1792 unsigned char tmp;
1793
1794 tmp = e6502_op_dec (c, e6502_get_abs_x (c));
1795 e6502_set_ea (c, tmp);
1796
1797 e6502_set_clk (c, 3, 7);
1798}
1799
1800/* OP D0: BNE */
1801static void op_d0 (e6502_t *c)
1802{
1803 e6502_op_bcc (c, e6502_get_zf (c) == 0);
1804}
1805
1806/* OP D8: CLD */
1807static void op_d8 (e6502_t *c)
1808{
1809 e6502_set_df (c, 0);
1810 e6502_set_clk (c, 1, 2);
1811}
1812
1813/* OP E0: CPX #xx */
1814static void op_e0 (e6502_t *c)
1815{
1816 e6502_op_cpx (c, e6502_get_imm (c));
1817 e6502_set_clk (c, 2, 2);
1818}
1819
1820/* OP E1: SBC [[xx + X]] */
1821static void op_e1 (e6502_t *c)
1822{
1823 e6502_op_sbc (c, e6502_get_idx_ind_x (c));
1824 e6502_set_clk (c, 2, 6);
1825}
1826
1827/* OP E4: CPX [xx] */
1828static void op_e4 (e6502_t *c)
1829{
1830 e6502_op_cpx (c, e6502_get_zpg (c));
1831 e6502_set_clk (c, 2, 3);
1832}
1833
1834/* OP E5: SBC [xx] */
1835static void op_e5 (e6502_t *c)
1836{
1837 e6502_op_sbc (c, e6502_get_zpg (c));
1838 e6502_set_clk (c, 2, 3);
1839}
1840
1841/* OP E6: INC [xx] */
1842static void op_e6 (e6502_t *c)
1843{
1844 unsigned char tmp;
1845
1846 tmp = e6502_op_inc (c, e6502_get_zpg (c));
1847 e6502_set_ea (c, tmp);
1848
1849 e6502_set_clk (c, 2, 5);
1850}
1851
1852/* OP E8: INX */
1853static void op_e8 (e6502_t *c)
1854{
1855 e6502_set_x (c, e6502_op_inc (c, e6502_get_x (c)));
1856 e6502_set_clk (c, 1, 2);
1857}
1858
1859/* OP E9: SBC #xx */
1860static void op_e9 (e6502_t *c)
1861{
1862 e6502_op_sbc (c, e6502_get_imm (c));
1863 e6502_set_clk (c, 2, 2);
1864}
1865
1866/* OP EA: NOP */
1867static void op_ea (e6502_t *c)
1868{
1869 e6502_set_clk (c, 1, 2);
1870}
1871
1872/* OP EC: CPX [xxxx] */
1873static void op_ec (e6502_t *c)
1874{
1875 e6502_op_cpx (c, e6502_get_abs (c));
1876 e6502_set_clk (c, 3, 4);
1877}
1878
1879/* OP ED: SBC [xxxx] */
1880static void op_ed (e6502_t *c)
1881{
1882 e6502_op_sbc (c, e6502_get_abs (c));
1883 e6502_set_clk (c, 3, 4);
1884}
1885
1886/* OP EE: INC [xxxx] */
1887static void op_ee (e6502_t *c)
1888{
1889 unsigned char tmp;
1890
1891 tmp = e6502_op_inc (c, e6502_get_abs (c));
1892 e6502_set_ea (c, tmp);
1893
1894 e6502_set_clk (c, 3, 6);
1895}
1896
1897/* OP F0: BEQ */
1898static void op_f0 (e6502_t *c)
1899{
1900 e6502_op_bcc (c, e6502_get_zf (c) != 0);
1901}
1902
1903/* OP F1: SBC [[xx] + Y] */
1904static void op_f1 (e6502_t *c)
1905{
1906 e6502_op_sbc (c, e6502_get_ind_idx_y (c));
1907 e6502_set_clk (c, 2, 5 + c->ea_page);
1908}
1909
1910/* OP F5: SBC [xx + X] */
1911static void op_f5 (e6502_t *c)
1912{
1913 e6502_op_sbc (c, e6502_get_zpg_x (c));
1914 e6502_set_clk (c, 2, 4);
1915}
1916
1917/* OP F6: INC [xx + X] */
1918static void op_f6 (e6502_t *c)
1919{
1920 unsigned char tmp;
1921
1922 tmp = e6502_op_inc (c, e6502_get_zpg_x (c));
1923 e6502_set_ea (c, tmp);
1924
1925 e6502_set_clk (c, 2, 6);
1926}
1927
1928/* OP F8: SED */
1929static void op_f8 (e6502_t *c)
1930{
1931 e6502_set_df (c, 1);
1932 e6502_set_clk (c, 1, 2);
1933}
1934
1935/* OP F9: SBC [xxxx + Y] */
1936static void op_f9 (e6502_t *c)
1937{
1938 e6502_op_sbc (c, e6502_get_abs_y (c));
1939 e6502_set_clk (c, 3, 4 + c->ea_page);
1940}
1941
1942/* OP FD: SBC [xxxx + X] */
1943static void op_fd (e6502_t *c)
1944{
1945 e6502_op_sbc (c, e6502_get_abs_x (c));
1946 e6502_set_clk (c, 3, 4 + c->ea_page);
1947}
1948
1949/* OP FE: INC [xxxx + X] */
1950static void op_fe (e6502_t *c)
1951{
1952 unsigned char tmp;
1953
1954 tmp = e6502_op_inc (c, e6502_get_abs_x (c));
1955 e6502_set_ea (c, tmp);
1956
1957 e6502_set_clk (c, 3, 7);
1958}
1959
1960e6502_opcode_f e6502_opcodes[256] = {
1961 &op_00, &op_01, &op_ud, &op_03, &op_04, &op_05, &op_06, &op_07, /* 00 */
1962 &op_08, &op_09, &op_0a, &op_ud, &op_0c, &op_0d, &op_0e, &op_0f,
1963 &op_10, &op_11, &op_ud, &op_13, &op_14, &op_15, &op_16, &op_17, /* 10 */
1964 &op_18, &op_19, &op_1a, &op_1b, &op_1c, &op_1d, &op_1e, &op_1f,
1965 &op_20, &op_21, &op_ud, &op_23, &op_24, &op_25, &op_26, &op_27, /* 20 */
1966 &op_28, &op_29, &op_2a, &op_ud, &op_2c, &op_2d, &op_2e, &op_2f,
1967 &op_30, &op_31, &op_ud, &op_33, &op_14, &op_35, &op_36, &op_37, /* 30 */
1968 &op_38, &op_39, &op_1a, &op_3b, &op_1c, &op_3d, &op_3e, &op_3f,
1969 &op_40, &op_41, &op_ud, &op_43, &op_04, &op_45, &op_46, &op_47, /* 40 */
1970 &op_48, &op_49, &op_4a, &op_ud, &op_4c, &op_4d, &op_4e, &op_4f,
1971 &op_50, &op_51, &op_ud, &op_53, &op_14, &op_55, &op_56, &op_57, /* 50 */
1972 &op_58, &op_59, &op_1a, &op_5b, &op_1c, &op_5d, &op_5e, &op_5f,
1973 &op_60, &op_61, &op_ud, &op_ud, &op_04, &op_65, &op_66, &op_ud, /* 60 */
1974 &op_68, &op_69, &op_6a, &op_ud, &op_6c, &op_6d, &op_6e, &op_ud,
1975 &op_70, &op_71, &op_ud, &op_ud, &op_14, &op_75, &op_76, &op_ud, /* 70 */
1976 &op_78, &op_79, &op_1a, &op_ud, &op_1c, &op_7d, &op_7e, &op_ud,
1977 &op_80, &op_81, &op_80, &op_ud, &op_84, &op_85, &op_86, &op_ud, /* 80 */
1978 &op_88, &op_89, &op_8a, &op_ud, &op_8c, &op_8d, &op_8e, &op_ud,
1979 &op_90, &op_91, &op_ud, &op_ud, &op_94, &op_95, &op_96, &op_ud, /* 90 */
1980 &op_98, &op_99, &op_9a, &op_ud, &op_ud, &op_9d, &op_ud, &op_ud,
1981 &op_a0, &op_a1, &op_a2, &op_ud, &op_a4, &op_a5, &op_a6, &op_ud, /* A0 */
1982 &op_a8, &op_a9, &op_aa, &op_ud, &op_ac, &op_ad, &op_ae, &op_ud,
1983 &op_b0, &op_b1, &op_ud, &op_ud, &op_b4, &op_b5, &op_b6, &op_ud, /* B0 */
1984 &op_b8, &op_b9, &op_ba, &op_ud, &op_bc, &op_bd, &op_be, &op_ud,
1985 &op_c0, &op_c1, &op_80, &op_ud, &op_c4, &op_c5, &op_c6, &op_c7, /* C0 */
1986 &op_c8, &op_c9, &op_ca, &op_ud, &op_cc, &op_cd, &op_ce, &op_ud,
1987 &op_d0, &op_d1, &op_ud, &op_ud, &op_14, &op_d5, &op_d6, &op_ud, /* D0 */
1988 &op_d8, &op_d9, &op_1a, &op_ud, &op_1c, &op_dd, &op_de, &op_ud,
1989 &op_e0, &op_e1, &op_80, &op_ud, &op_e4, &op_e5, &op_e6, &op_ud, /* E0 */
1990 &op_e8, &op_e9, &op_ea, &op_ud, &op_ec, &op_ed, &op_ee, &op_ud,
1991 &op_f0, &op_f1, &op_ud, &op_ud, &op_14, &op_f5, &op_f6, &op_ud, /* F0 */
1992 &op_f8, &op_f9, &op_1a, &op_ud, &op_1c, &op_fd, &op_fe, &op_ud
1993};