-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcpu.c
1815 lines (1628 loc) · 49.1 KB
/
cpu.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*-
* Copyright (c) 2016 Jared McNeill <[email protected]>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include <assert.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include "cpu.h"
#define CPU_READ8(c, addr) ((c)->read8((addr)))
#define CPU_WRITE8(c, addr, v) ((c)->write8((addr), (v)))
#define CPU_READ16(c, addr) (CPU_READ8((c), (addr)) | ((uint16_t)CPU_READ8((c), (addr)+1))<<8)
#define CPU_PAGE_CROSSED(addr, off) (((addr) & 0xff) + (off) > 0xff)
#define NMI_VECTOR_L 0xFFFA
#define NMI_VECTOR_H 0xFFFB
#define RESET_VECTOR_L 0xFFFC
#define RESET_VECTOR_H 0xFFFD
#define INTR_VECTOR_L 0xFFFE
#define INTR_VECTOR_H 0xFFFF
#define STACK_TOP 0x0100
#define OPCODE_COUNT 256
enum cpu_opcode_type {
OP_def,
OP_imm,
OP_zp, OP_zpx, OP_zpy,
OP_izx, OP_izy,
OP_abs, OP_abx, OP_aby,
OP_ind,
OP_rel
};
struct cpu_opcode {
uint8_t opcode;
const char *name;
enum cpu_opcode_type type;
uint8_t args;
int cycles;
int (*op)(struct cpu_context *, struct cpu_opcode *);
};
#define OP(o,n,t,a,c,f) \
[(o)] = { .opcode = (o), .name = (n), .type = (t), .args = (a), .cycles = (c),.op = (f) }
static void
cpu_stackpush(struct cpu_context *c, uint8_t val)
{
struct cpu_frame *f = &c->frame;
CPU_WRITE8(c, STACK_TOP + f->SP, val);
f->SP--;
}
static uint8_t
cpu_stackpull(struct cpu_context *c)
{
struct cpu_frame *f = &c->frame;
f->SP++;
return CPU_READ8(c, STACK_TOP + f->SP);
}
static int
cpuop_notimpl(struct cpu_context *c, struct cpu_opcode *o)
{
printf("[OPCODE %02X] not implemented\n", o->opcode);
abort();
}
static int
cpuop_nop(struct cpu_context *c, struct cpu_opcode *o)
{
struct cpu_frame *f = &c->frame;
f->PC += o->args;
return o->cycles;
}
static int
cpuop_break(struct cpu_context *c, struct cpu_opcode *o)
{
struct cpu_frame *f = &c->frame;
printf("BRK $%04X\n", f->PC + o->args);
/* Push PC and P to stack */
cpu_stackpush(c, (f->PC + o->args) >> 8);
cpu_stackpush(c, (f->PC + o->args) & 0xff);
cpu_stackpush(c, f->P);
/* Load IRQ vector into PC */
f->PC = CPU_READ16(c, INTR_VECTOR_L);
/* Set break flag */
f->P |= P_B;
return o->cycles;
}
static int
cpuop_load(struct cpu_context *c, struct cpu_opcode *o)
{
struct cpu_frame *f = &c->frame;
uint16_t addr;
uint8_t nv;
int cycles = o->cycles;
switch (o->opcode) {
case 0xA0: /* LDY Immediate */
nv = f->Y = CPU_READ8(c, f->PC + 1);
break;
case 0xA4: /* LDY Zero Page */
nv = f->Y = CPU_READ8(c, CPU_READ8(c, f->PC + 1));
break;
case 0xA5: /* LDA Zero Page */
nv = f->A = CPU_READ8(c, CPU_READ8(c, f->PC + 1));
break;
case 0xA6: /* LDX Zero Page */
nv = f->X = CPU_READ8(c, CPU_READ8(c, f->PC + 1));
break;
case 0xA9: /* LDA Immediate */
nv = f->A = CPU_READ8(c, f->PC + 1);
break;
case 0xAC: /* LDY Absolute */
nv = f->Y = CPU_READ8(c, CPU_READ16(c, f->PC + 1));
break;
case 0xAD: /* LDA Absolute */
nv = f->A = CPU_READ8(c, CPU_READ16(c, f->PC + 1));
break;
case 0xAE: /* LDX Absolute */
nv = f->X = CPU_READ8(c, CPU_READ16(c, f->PC + 1));
break;
case 0xB4: /* LDY Zero Page,X */
nv = f->Y = CPU_READ8(c, (uint8_t)(CPU_READ8(c, f->PC + 1) + f->X));
break;
case 0xB5: /* LDA Zero Page,X */
nv = f->A = CPU_READ8(c, (uint8_t)(CPU_READ8(c, f->PC + 1) + f->X));
break;
case 0xB6: /* LDX Zero Page,Y */
nv = f->X = CPU_READ8(c, (uint8_t)(CPU_READ8(c, f->PC + 1) + f->Y));
break;
case 0xB9: /* LDA Absolute,Y */
addr = CPU_READ16(c, f->PC + 1);
nv = f->A = CPU_READ8(c, addr + f->Y);
if (CPU_PAGE_CROSSED(addr, f->Y))
cycles++;
break;
case 0xBC: /* LDY Absolute,X */
addr = CPU_READ16(c, f->PC + 1);
nv = f->Y = CPU_READ8(c, addr + f->X);
if (CPU_PAGE_CROSSED(addr, f->X))
cycles++;
break;
case 0xBD: /* LDA Absolute,X */
addr = CPU_READ16(c, f->PC + 1);
nv = f->A = CPU_READ8(c, addr + f->X);
if (CPU_PAGE_CROSSED(addr, f->X))
cycles++;
break;
case 0xBE: /* LDX Absolute,Y */
addr = CPU_READ16(c, f->PC + 1);
nv = f->X = CPU_READ8(c, addr + f->Y);
if (CPU_PAGE_CROSSED(addr, f->Y))
cycles++;
break;
case 0xA1: /* LDA Indirect,X */
addr = CPU_READ8(c, (uint8_t)(CPU_READ8(c, f->PC + 1) + f->X)) |
(uint16_t)CPU_READ8(c, (uint8_t)(CPU_READ8(c, f->PC + 1) + f->X + 1)) << 8;
nv = f->A = CPU_READ8(c, addr);
break;
case 0xA2: /* LDX Immediate */
nv = f->X = CPU_READ8(c, f->PC + 1);
break;
case 0xB1: /* LDA Indirect,Y */
addr = CPU_READ8(c, CPU_READ8(c, f->PC + 1)) |
(uint16_t)CPU_READ8(c, (uint8_t)(CPU_READ8(c, f->PC + 1) + 1)) << 8;
nv = f->A = CPU_READ8(c, addr + f->Y);
if (CPU_PAGE_CROSSED(addr, f->Y))
cycles++;
break;
default:
assert("Unsupported load register opcode" == NULL);
}
/* Set Zero Flag if A = 0 */
if (nv == 0)
f->P |= P_Z;
else
f->P &= ~P_Z;
/* Set Negative Flag if bit 7 of A is set */
if ((nv & 0x80) != 0)
f->P |= P_N;
else
f->P &= ~P_N;
f->PC += o->args;
return cycles;
}
static int
cpuop_store(struct cpu_context *c, struct cpu_opcode *o)
{
struct cpu_frame *f = &c->frame;
uint16_t addr;
switch (o->opcode) {
case 0x81: /* STA Indirect,X */
addr = CPU_READ8(c, (uint8_t)(CPU_READ8(c, f->PC + 1) + f->X)) |
(uint16_t)CPU_READ8(c, (uint8_t)(CPU_READ8(c, f->PC + 1) + f->X + 1)) << 8;
CPU_WRITE8(c, addr, f->A);
break;
case 0x84: /* STY Zero Page */
CPU_WRITE8(c, CPU_READ8(c, f->PC + 1), f->Y);
break;
case 0x85: /* STA Zero Page */
CPU_WRITE8(c, CPU_READ8(c, f->PC + 1), f->A);
break;
case 0x86: /* STX Zero Page */
CPU_WRITE8(c, CPU_READ8(c, f->PC + 1), f->X);
break;
case 0x8C: /* STY Absolute */
CPU_WRITE8(c, CPU_READ16(c, f->PC + 1), f->Y);
break;
case 0x8D: /* STA Absolute */
CPU_WRITE8(c, CPU_READ16(c, f->PC + 1), f->A);
break;
case 0x8E: /* STX Absolute */
CPU_WRITE8(c, CPU_READ16(c, f->PC + 1), f->X);
break;
case 0x91: /* STA Indirect,Y */
addr = CPU_READ8(c, CPU_READ8(c, f->PC + 1)) |
(uint16_t)CPU_READ8(c, (uint8_t)(CPU_READ8(c, f->PC + 1) + 1)) << 8;
CPU_WRITE8(c, addr + f->Y, f->A);
break;
case 0x94: /* STY Zero Page,X */
CPU_WRITE8(c, (uint8_t)(CPU_READ8(c, f->PC + 1) + f->X), f->Y);
break;
case 0x95: /* STA Zero Page,X */
CPU_WRITE8(c, (uint8_t)(CPU_READ8(c, f->PC + 1) + f->X), f->A);
break;
case 0x96: /* STX Zero Page,Y */
CPU_WRITE8(c, (uint8_t)(CPU_READ8(c, f->PC + 1) + f->Y), f->X);
break;
case 0x99: /* STA Absolute,Y */
CPU_WRITE8(c, CPU_READ16(c, f->PC + 1) + f->Y, f->A);
break;
case 0x9D: /* STA Absolute,X */
CPU_WRITE8(c, CPU_READ16(c, f->PC + 1) + f->X, f->A);
break;
default:
assert("Unsupported store opcode" == NULL);
}
f->PC += o->args;
return o->cycles;
}
static int
cpuop_compare(struct cpu_context *c, struct cpu_opcode *o)
{
struct cpu_frame *f = &c->frame;
uint16_t addr;
uint8_t M, v;
int cycles = o->cycles;
switch (o->opcode) {
case 0xC0: /* CPY Immediate */
M = CPU_READ8(c, f->PC + 1);
v = f->Y;
break;
case 0xC1: /* CMP Indirect,X */
addr = CPU_READ8(c, CPU_READ8(c, f->PC + 1) + f->X) |
(uint16_t)CPU_READ8(c, (uint8_t)(CPU_READ8(c, f->PC + 1) + f->X + 1)) << 8;
M = CPU_READ8(c, addr);
v = f->A;
break;
case 0xC4: /* CPY Zero Page */
M = CPU_READ8(c, CPU_READ8(c, f->PC + 1));
v = f->Y;
break;
case 0xC5: /* CMP Zero Page */
M = CPU_READ8(c, CPU_READ8(c, f->PC + 1));
v = f->A;
break;
case 0xC9: /* CMP Immediate */
M = CPU_READ8(c, f->PC + 1);
v = f->A;
break;
case 0xCC: /* CPY Absolute */
M = CPU_READ8(c, CPU_READ16(c, f->PC + 1));
v = f->Y;
break;
case 0xCD: /* CMP Absolute */
M = CPU_READ8(c, CPU_READ16(c, f->PC + 1));
v = f->A;
break;
case 0xD1: /* CMP Indirect,Y */
addr = CPU_READ16(c, CPU_READ8(c, f->PC + 1));
M = CPU_READ8(c, addr + f->Y);
v = f->A;
if (CPU_PAGE_CROSSED(addr, f->Y))
cycles++;
break;
case 0xD5: /* CMP Zero Page,X */
M = CPU_READ8(c, (uint8_t)(CPU_READ8(c, f->PC + 1) + f->X));
v = f->A;
break;
case 0xD9: /* CMP Absolute,Y */
addr = CPU_READ16(c, f->PC + 1);
M = CPU_READ8(c, addr + f->Y);
v = f->A;
if (CPU_PAGE_CROSSED(addr, f->Y))
cycles++;
break;
case 0xDD: /* CMP Absolute,X */
addr = CPU_READ16(c, f->PC + 1);
M = CPU_READ8(c, addr + f->X);
v = f->A;
if (CPU_PAGE_CROSSED(addr, f->X))
cycles++;
break;
case 0xE0: /* CPX Immediate */
M = CPU_READ8(c, f->PC + 1);
v = f->X;
break;
case 0xE4: /* CPX Zero Page */
M = CPU_READ8(c, CPU_READ8(c, f->PC + 1));
v = f->X;
break;
case 0xEC: /* CPX Absolute */
M = CPU_READ8(c, CPU_READ16(c, f->PC + 1));
v = f->X;
break;
default:
assert("Unsupported compare opcode" == NULL);
}
/* Set Carry Flag if value >= M */
if (v >= M)
f->P |= P_C;
else
f->P &= ~P_C;
/* Set Zero Flag if value = M */
if (v == M)
f->P |= P_Z;
else
f->P &= ~P_Z;
/* Set Negative Flag if bit 7 of the result (v-M) is set */
if ((((int)v - (int)M) & 0x80) != 0)
f->P |= P_N;
else
f->P &= ~P_N;
f->PC += o->args;
return cycles;
}
static int
cpuop_increment(struct cpu_context *c, struct cpu_opcode *o)
{
struct cpu_frame *f = &c->frame;
uint8_t ov, nv;
switch (o->opcode) {
case 0xC8: /* Increment Y register */
nv = ++f->Y;
break;
case 0xE6: /* Increment Memory Zero Page */
ov = CPU_READ8(c, CPU_READ8(c, f->PC + 1));
nv = ov + 1;
CPU_WRITE8(c, CPU_READ8(c, f->PC + 1), nv);
break;
case 0xE8: /* Increment X register */
nv = ++f->X;
break;
case 0xEE: /* Increment Memory Absolute */
ov = CPU_READ8(c, CPU_READ16(c, f->PC + 1));
nv = ov + 1;
CPU_WRITE8(c, CPU_READ16(c, f->PC + 1), nv);
break;
case 0xF6: /* Increment Memory Zero Page,X */
ov = CPU_READ8(c, (uint8_t)(CPU_READ8(c, f->PC + 1) + f->X));
nv = ov + 1;
CPU_WRITE8(c, (uint8_t)(CPU_READ8(c, f->PC + 1) + f->X), nv);
break;
case 0xFE: /* Increment Memory Absolute,X */
ov = CPU_READ8(c, CPU_READ16(c, f->PC + 1) + f->X);
nv = ov + 1;
CPU_WRITE8(c, CPU_READ16(c, f->PC + 1) + f->X, nv);
break;
default:
assert("Unsupported increment opcode" == NULL);
}
if (nv == 0)
f->P |= P_Z;
else
f->P &= ~P_Z;
if ((nv & 0x80) != 0)
f->P |= P_N;
else
f->P &= ~P_N;
f->PC += o->args;
return o->cycles;
}
static int
cpuop_decrement(struct cpu_context *c, struct cpu_opcode *o)
{
struct cpu_frame *f = &c->frame;
uint8_t ov, nv;
switch (o->opcode) {
case 0x88: /* Decrement Y register */
nv = --f->Y;
break;
case 0xC6: /* Decrement Memory Zero Page */
ov = CPU_READ8(c, CPU_READ8(c, f->PC + 1));
nv = ov - 1;
CPU_WRITE8(c, CPU_READ8(c, f->PC + 1), nv);
break;
case 0xCA: /* Decrement X register */
nv = --f->X;
break;
case 0xD6: /* Decrement Memory Zero Page,X */
ov = CPU_READ8(c, (uint8_t)(CPU_READ8(c, f->PC + 1) + f->X));
nv = ov - 1;
CPU_WRITE8(c, (uint8_t)(CPU_READ8(c, f->PC + 1) + f->X), nv);
break;
case 0xCE: /* Decrement Memory Absolute */
ov = CPU_READ8(c, CPU_READ16(c, f->PC + 1));
nv = ov - 1;
CPU_WRITE8(c, CPU_READ16(c, f->PC + 1), nv);
break;
case 0xDE: /* Decrement Memory Absolute,X */
ov = CPU_READ8(c, CPU_READ16(c, f->PC + 1) + f->X);
nv = ov - 1;
CPU_WRITE8(c, CPU_READ16(c, f->PC + 1) + f->X, nv);
break;
default:
assert("Unsupported decrement opcode" == NULL);
}
if (nv == 0)
f->P |= P_Z;
else
f->P &= ~P_Z;
if ((nv & 0x80) != 0)
f->P |= P_N;
else
f->P &= ~P_N;
f->PC += o->args;
return o->cycles;
}
static int
cpuop_add(struct cpu_context *c, struct cpu_opcode *o)
{
struct cpu_frame *f = &c->frame;
uint16_t addr;
uint8_t M, C;
unsigned int nv;
int cycles = o->cycles;
C = (f->P & P_C) ? 1 : 0;
switch (o->opcode) {
case 0x61: /* Add with Carry Indirect,X */
addr = CPU_READ8(c, CPU_READ8(c, f->PC + 1) + f->X) |
(uint16_t)CPU_READ8(c, (uint8_t)(CPU_READ8(c, f->PC + 1) + f->X + 1)) << 8;
M = CPU_READ8(c, addr);
nv = f->A + M + C;
break;
case 0x65: /* Add with Carry Zero Page */
M = CPU_READ8(c, CPU_READ8(c, f->PC + 1));
nv = f->A + M + C;
break;
case 0x69: /* Add with Carry Immediate */
M = CPU_READ8(c, f->PC + 1);
nv = f->A + M + C;
break;
case 0x6D: /* Add with Carry Absolute */
M = CPU_READ8(c, CPU_READ16(c, f->PC + 1));
nv = f->A + M + C;
break;
case 0x71: /* Add with Carry Indirect,Y */
addr = CPU_READ16(c, CPU_READ8(c, f->PC + 1));
M = CPU_READ8(c, addr + f->Y);
nv = f->A + M + C;
if (CPU_PAGE_CROSSED(addr, f->Y))
cycles++;
break;
case 0x75: /* Add with Carry Zero Page,X */
M = CPU_READ8(c, (uint8_t)(CPU_READ8(c, f->PC + 1) + f->X));
nv = f->A + M + C;
break;
case 0x79: /* Add with Carry Absolute,Y */
addr = CPU_READ16(c, f->PC + 1);
M = CPU_READ8(c, addr + f->Y);
nv = f->A + M + C;
if (CPU_PAGE_CROSSED(addr, f->Y))
cycles++;
break;
case 0x7D: /* Add with Carry Absolute,X */
addr = CPU_READ16(c, f->PC + 1);
M = CPU_READ8(c, addr + f->X);
nv = f->A + M + C;
if (CPU_PAGE_CROSSED(addr, f->X))
cycles++;
break;
default:
assert("Unsupported add opcode" == NULL);
}
/* Set Zero Flag if A = 0 */
if ((nv & 0xff) == 0)
f->P |= P_Z;
else
f->P &= ~P_Z;
/* Set Overflow Flag if sign bit is incorrect */
if (((f->A ^ nv) & 0x80) && !((f->A ^ M) & 0x80))
f->P |= P_V;
else
f->P &= ~P_V;
/* Set Carry Flag if overflow in bit 7 */
if (nv > 0xff)
f->P |= P_C;
else
f->P &= ~P_C;
/* Set Negative Flag if bit 7 is set */
if ((nv & 0x80) != 0)
f->P |= P_N;
else
f->P &= ~P_N;
f->A = nv & 0xff;
f->PC += o->args;
return cycles;
}
static int
cpuop_subtract(struct cpu_context *c, struct cpu_opcode *o)
{
struct cpu_frame *f = &c->frame;
uint16_t addr;
uint8_t M, C;
unsigned int nv;
int cycles = o->cycles;
C = (f->P & P_C) ? 1 : 0;
switch (o->opcode) {
case 0xE1: /* Subtract with Carry Indirect,X */
addr = CPU_READ8(c, CPU_READ8(c, f->PC + 1) + f->X) |
(uint16_t)CPU_READ8(c, (uint8_t)(CPU_READ8(c, f->PC + 1) + f->X + 1)) << 8;
M = CPU_READ8(c, addr);
nv = f->A - M - (1 - C);
break;
case 0xE5: /* Subtract with Carry Zero Page */
M = CPU_READ8(c, CPU_READ8(c, f->PC + 1));
nv = f->A - M - (1 - C);
break;
case 0xE9: /* Subtract with Carry Immediate */
M = CPU_READ8(c, f->PC + 1);
nv = f->A - M - (1 - C);
break;
case 0xED: /* Subtract with Carry Absolute */
M = CPU_READ8(c, CPU_READ16(c, f->PC + 1));
nv = f->A - M - (1 - C);
break;
case 0xF1: /* Subtract with Carry Indirect,Y */
addr = CPU_READ16(c, CPU_READ8(c, f->PC + 1));
M = CPU_READ8(c, addr + f->Y);
nv = f->A - M - (1 - C);
if (CPU_PAGE_CROSSED(addr, f->Y))
cycles++;
break;
case 0xF5: /* Subtract with Carry Zero Page,X */
M = CPU_READ8(c, (uint8_t)(CPU_READ8(c, f->PC + 1) + f->X));
nv = f->A - M - (1 - C);
break;
case 0xF9: /* Subtract with Carry Absolute,Y */
addr = CPU_READ16(c, f->PC + 1);
M = CPU_READ8(c, addr + f->Y);
nv = f->A - M - (1 - C);
if (CPU_PAGE_CROSSED(addr, f->Y))
cycles++;
break;
case 0xFD: /* Subtract with Carry Absolute,X */
addr = CPU_READ16(c, f->PC + 1);
M = CPU_READ8(c, addr + f->X);
nv = f->A - M - (1 - C);
if (CPU_PAGE_CROSSED(addr, f->X))
cycles++;
break;
default:
assert("Unsupported subtract opcode" == NULL);
}
/* Set Zero Flag if A = 0 */
if ((nv & 0xff) == 0)
f->P |= P_Z;
else
f->P &= ~P_Z;
/* Set Overflow Flag if sign bit is incorrect */
if (((f->A ^ nv) & 0x80) && ((f->A ^ M) & 0x80))
f->P |= P_V;
else
f->P &= ~P_V;
/* Set Carry Flag if overflow in bit 7 */
if (nv < 0x100)
f->P |= P_C;
else
f->P &= ~P_C;
/* Set Negative Flag if bit 7 is set */
if ((nv & 0x80) != 0)
f->P |= P_N;
else
f->P &= ~P_N;
f->A = nv & 0xff;
f->PC += o->args;
return cycles;
}
static int
cpuop_shift(struct cpu_context *c, struct cpu_opcode *o)
{
struct cpu_frame *f = &c->frame;
uint8_t ov, nv;
switch (o->opcode) {
case 0x06: /* Arithmetic Shift Left Zero Page */
ov = CPU_READ8(c, CPU_READ8(c, f->PC + 1));
nv = ov << 1;
/* Set Carry Flag to contents of old bit 7 */
if ((ov & 0x80) != 0)
f->P |= P_C;
else
f->P &= ~P_C;
CPU_WRITE8(c, CPU_READ8(c, f->PC + 1), nv);
break;
case 0x0A: /* Arithmetic Shift Left Accumulator */
nv = f->A << 1;
/* Set Carry Flag to contents of old bit 7 */
if ((f->A & 0x80) != 0)
f->P |= P_C;
else
f->P &= ~P_C;
f->A = nv;
break;
case 0x0E: /* Arithmetic Shift Left Absolute */
ov = CPU_READ8(c, CPU_READ16(c, f->PC + 1));
nv = ov << 1;
/* Set Carry Flag to contents of old bit 7 */
if ((ov & 0x80) != 0)
f->P |= P_C;
else
f->P &= ~P_C;
CPU_WRITE8(c, CPU_READ16(c, f->PC + 1), nv);
break;
case 0x16: /* Arithmetic Shift Left Zero Page,X */
ov = CPU_READ8(c, (uint8_t)(CPU_READ8(c, f->PC + 1) + f->X));
nv = ov << 1;
/* Set Carry Flag to contents of old bit 7 */
if ((ov & 0x80) != 0)
f->P |= P_C;
else
f->P &= ~P_C;
CPU_WRITE8(c, (uint8_t)(CPU_READ8(c, f->PC + 1) + f->X), nv);
break;
case 0x1E: /* Arithmetic Shift Left Absolute,X */
ov = CPU_READ8(c, CPU_READ16(c, f->PC + 1) + f->X);
nv = ov << 1;
/* Set Carry Flag to contents of old bit 7 */
if ((ov & 0x80) != 0)
f->P |= P_C;
else
f->P &= ~P_C;
CPU_WRITE8(c, CPU_READ16(c, f->PC + 1) + f->X, nv);
break;
case 0x26: /* Rotate Left Zero Page */
ov = CPU_READ8(c, CPU_READ8(c, f->PC + 1));
nv = (ov << 1) | ((f->P & P_C) ? 1 : 0);
/* Set Carry Flag to contents of old bit 7 */
if ((ov & 0x80) != 0)
f->P |= P_C;
else
f->P &= ~P_C;
CPU_WRITE8(c, CPU_READ8(c, f->PC + 1), nv);
break;
case 0x2A: /* Rotate Left Accumulator */
ov = f->A;
nv = (ov << 1) | ((f->P & P_C) ? 1 : 0);
/* Set Carry Flag to contents of old bit 7 */
if ((ov & 0x80) != 0)
f->P |= P_C;
else
f->P &= ~P_C;
f->A = nv;
break;
case 0x2E: /* Rotate Right Absolute */
ov = CPU_READ8(c, CPU_READ16(c, f->PC + 1));
nv = (ov << 1) | ((f->P & P_C) ? 1 : 0);
/* Set Carry Flag to contents of old bit 7 */
if ((ov & 0x80) != 0)
f->P |= P_C;
else
f->P &= ~P_C;
CPU_WRITE8(c, CPU_READ16(c, f->PC + 1), nv);
break;
case 0x36: /* Rotate Left Zero Page,X */
ov = CPU_READ8(c, (uint8_t)(CPU_READ8(c, f->PC + 1) + f->X));
nv = (ov << 1) | ((f->P & P_C) ? 1 : 0);
/* Set Carry Flag to contents of old bit 7 */
if ((ov & 0x80) != 0)
f->P |= P_C;
else
f->P &= ~P_C;
CPU_WRITE8(c, (uint8_t)(CPU_READ8(c, f->PC + 1) + f->X), nv);
break;
case 0x3E: /* Rotate Left Absolute,X */
ov = CPU_READ8(c, CPU_READ16(c, f->PC + 1) + f->X);
nv = (ov << 1) | ((f->P & P_C) ? 1 : 0);
/* Set Carry Flag to contents of old bit 7 */
if ((ov & 0x80) != 0)
f->P |= P_C;
else
f->P &= ~P_C;
CPU_WRITE8(c, CPU_READ16(c, f->PC + 1) + f->X, nv);
break;
case 0x46: /* Logical Shift Right Zero Page */
ov = CPU_READ8(c, CPU_READ8(c, f->PC + 1));
nv = ov >> 1;
/* Set Carry Flag to contents of old bit 0 */
if ((ov & 0x1) != 0)
f->P |= P_C;
else
f->P &= ~P_C;
CPU_WRITE8(c, CPU_READ8(c, f->PC + 1), nv);
break;
case 0x4A: /* Logical Shift Right Accumulator */
nv = f->A >> 1;
/* Set Carry Flag to contents of old bit 0 */
if ((f->A & 0x1) != 0)
f->P |= P_C;
else
f->P &= ~P_C;
f->A = nv;
break;
case 0x4E: /* Logical Shift Right Absolute */
ov = CPU_READ8(c, CPU_READ16(c, f->PC + 1));
nv = ov >> 1;
/* Set Carry Flag to contents of old bit 0 */
if ((ov & 0x1) != 0)
f->P |= P_C;
else
f->P &= ~P_C;
CPU_WRITE8(c, CPU_READ16(c, f->PC + 1), nv);
break;
case 0x56: /* Logical Shift Right Zero Page,X */
ov = CPU_READ8(c, (uint8_t)(CPU_READ8(c, f->PC + 1) + f->X));
nv = ov >> 1;
/* Set Carry Flag to contents of old bit 0 */
if ((ov & 0x1) != 0)
f->P |= P_C;
else
f->P &= ~P_C;
CPU_WRITE8(c, (uint8_t)(CPU_READ8(c, f->PC + 1) + f->X), nv);
break;
case 0x5E: /* Logical Shift Right Absolute,X */
ov = CPU_READ8(c, CPU_READ16(c, f->PC + 1) + f->X);
nv = ov >> 1;
/* Set Carry Flag to contents of old bit 0 */
if ((ov & 0x1) != 0)
f->P |= P_C;
else
f->P &= ~P_C;
CPU_WRITE8(c, CPU_READ16(c, f->PC + 1) + f->X, nv);
break;
case 0x66: /* Rotate Right Zero Page */
ov = CPU_READ8(c, CPU_READ8(c, f->PC + 1));
nv = (ov >> 1) | ((f->P & P_C) ? (1 << 7) : 0);
/* Set Carry Flag to contents of old bit 0 */
if ((ov & 0x01) != 0)
f->P |= P_C;
else
f->P &= ~P_C;
CPU_WRITE8(c, CPU_READ8(c, f->PC + 1), nv);
break;
case 0x6A: /* Rotate Right Accumulator */
ov = f->A;
nv = (ov >> 1) | ((f->P & P_C) ? (1 << 7) : 0);
/* Set Carry Flag to contents of old bit 0 */
if ((ov & 0x01) != 0)
f->P |= P_C;
else
f->P &= ~P_C;
f->A = nv;
break;
case 0x6E: /* Rotate Right Absolute */
ov = CPU_READ8(c, CPU_READ16(c, f->PC + 1));
nv = (ov >> 1) | ((f->P & P_C) ? (1 << 7) : 0);
/* Set Carry Flag to contents of old bit 0 */
if ((ov & 0x01) != 0)
f->P |= P_C;
else
f->P &= ~P_C;
CPU_WRITE8(c, CPU_READ16(c, f->PC + 1), nv);
break;
case 0x76: /* Rotate Right Zero Page,X */
ov = CPU_READ8(c, (uint8_t)(CPU_READ8(c, f->PC + 1) + f->X));
nv = (ov >> 1) | ((f->P & P_C) ? (1 << 7) : 0);
/* Set Carry Flag to contents of old bit 0 */
if ((ov & 0x01) != 0)
f->P |= P_C;
else
f->P &= ~P_C;
CPU_WRITE8(c, (uint8_t)(CPU_READ8(c, f->PC + 1) + f->X), nv);
break;
case 0x7E: /* Rotate Right Absolute,X */
ov = CPU_READ8(c, CPU_READ16(c, f->PC + 1) + f->X);
nv = (ov >> 1) | ((f->P & P_C) ? (1 << 7) : 0);
/* Set Carry Flag to contents of old bit 0 */
if ((ov & 0x01) != 0)
f->P |= P_C;
else
f->P &= ~P_C;
CPU_WRITE8(c, CPU_READ16(c, f->PC + 1) + f->X, nv);
break;
default:
assert("Unsupported shift opcode" == NULL);
}
/* Set Zero Flag if A = 0 */
if (nv == 0)
f->P |= P_Z;
else
f->P &= ~P_Z;
/* Set Negative Flag if bit 7 is set */
if ((nv & 0x80) != 0)
f->P |= P_N;
else
f->P &= ~P_N;
f->PC += o->args;
return o->cycles;
}
static int
cpuop_or(struct cpu_context *c, struct cpu_opcode *o)
{
struct cpu_frame *f = &c->frame;
uint16_t addr;
int cycles = o->cycles;
switch (o->opcode) {
case 0x01: /* Logical inclusive OR Indirect,X */
addr = CPU_READ8(c, CPU_READ8(c, f->PC + 1) + f->X) |
(uint16_t)CPU_READ8(c, (uint8_t)(CPU_READ8(c, f->PC + 1) + f->X + 1)) << 8;
f->A |= CPU_READ8(c, addr);
break;
case 0x05: /* Logical inclusive OR Zero Page */
f->A |= CPU_READ8(c, CPU_READ8(c, f->PC + 1));
break;
case 0x09: /* Logical inclusive OR Immediate */
f->A |= CPU_READ8(c, f->PC + 1);
break;
case 0x0D: /* Logical inclusive OR Absolute */
f->A |= CPU_READ8(c, CPU_READ16(c, f->PC + 1));
break;
case 0x11: /* Logical inclusive OR Indirect,Y */
addr = CPU_READ16(c, CPU_READ8(c, f->PC + 1));
f->A |= CPU_READ8(c, addr + f->Y);
if (CPU_PAGE_CROSSED(addr, f->Y))
cycles++;
break;
case 0x15: /* Logical inclusive OR Zero Page,X */
f->A |= CPU_READ8(c, (uint8_t)(CPU_READ8(c, f->PC + 1) + f->X));
break;
case 0x19: /* Logical inclusive OR Absolute,Y */
addr = CPU_READ16(c, f->PC + 1);
f->A |= CPU_READ8(c, addr + f->Y);
if (CPU_PAGE_CROSSED(addr, f->Y))
cycles++;
break;
case 0x1D: /* Logical inclusive OR Absolute,X */
addr = CPU_READ16(c, f->PC + 1);
f->A |= CPU_READ8(c, addr + f->X);
if (CPU_PAGE_CROSSED(addr, f->X))
cycles++;
break;
default:
assert("Unsupported or opcode" == NULL);
}
/* Set Zero Flag if A = 0 */
if (f->A == 0)
f->P |= P_Z;
else
f->P &= ~P_Z;
/* Set Negative Flag if bit 7 is set */
if ((f->A & 0x80) != 0)
f->P |= P_N;
else
f->P &= ~P_N;
f->PC += o->args;
return cycles;
}
static int
cpuop_xor(struct cpu_context *c, struct cpu_opcode *o)
{
struct cpu_frame *f = &c->frame;
uint16_t addr;
int cycles = o->cycles;
switch (o->opcode) {
case 0x41: /* Exclusive OR Indirect,X */
addr = CPU_READ8(c, CPU_READ8(c, f->PC + 1) + f->X) |
(uint16_t)CPU_READ8(c, (uint8_t)(CPU_READ8(c, f->PC + 1) + f->X + 1)) << 8;
f->A ^= CPU_READ8(c, addr);
break;
case 0x45: /* Exclusive OR Zero Page */
f->A ^= CPU_READ8(c, CPU_READ8(c, f->PC + 1));
break;
case 0x49: /* Exclusive OR Immediate */
f->A ^= CPU_READ8(c, f->PC + 1);
break;
case 0x4D: /* Exclusive OR Absolute */
f->A ^= CPU_READ8(c, CPU_READ16(c, f->PC + 1));
break;
case 0x51: /* Exclusive OR Indirect,Y */
addr = CPU_READ16(c, CPU_READ8(c, f->PC + 1));
f->A ^= CPU_READ8(c, addr + f->Y);
if (CPU_PAGE_CROSSED(addr, f->Y))
cycles++;
break;
case 0x55: /* Exclusive OR Zero Page,X */
f->A ^= CPU_READ8(c, (uint8_t)(CPU_READ8(c, f->PC + 1) + f->X));
break;
case 0x59: /* Exclusive OR Absolute,Y */
addr = CPU_READ16(c, f->PC + 1);
f->A ^= CPU_READ8(c, addr + f->Y);