-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathopcodes.cpp
More file actions
1606 lines (1291 loc) · 47.3 KB
/
opcodes.cpp
File metadata and controls
1606 lines (1291 loc) · 47.3 KB
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
#include "system.h"
/**
* DEPRECATED
*/
/*
* https://ascii.cl/conversion.htm
* Well... I think this is fundamentally flawed now...
* If I'm correct:
* Each opcode is a byte, composed of 8 bits.
* This byte is the input to the processor, and each bit goes to a different unit of the processor
* Therefore something like changing from addressing mode zero page to zero page,x
* would be as simple as turning on the fifth bit in 11000110 to get 11010110 (0xC6 to 0xD6)
*
* I might be totally wrong, but that would make sense... need to look at the opcode relationships
* They seem to have relationships to each other where they do things like increase by 8 or f
* Hmmmmmm
*/
// todo: !!! I have made a very erroneous assumption about the carry flag - it is bit 0, not bit 7
// todo: go back and change every access to carry flag... fuck
// Nooooooooo http://nparker.llx.com/a2/opcodes.html
// todo: need a new opcode model, create decoder class
opcode::opcode(uint8_t ocode, const char * ocodestr)
{
code = ocode;
opcodestr = ocodestr;
}
void opcode::printcode()
{
printf("%x\n", code);
}
const char * addopcode(std::map<uint8_t, std::shared_ptr<opcode> > * curmap, uint8_t code, const char * inststr)
{
/*
* creates a new opcode in the opcode map
* returns the string passed in
* NOTE: I'm not sure if this method will leak the old contents of memory or not if already assigned...
* cursory research suggests no
*/
(*curmap)[code] = std::shared_ptr<opcode>(new opcode(code, inststr));
return inststr;
}
std::map<uint8_t, std::shared_ptr<opcode> > create_opcode_map(cpustate * cpu, Memory * mem)
{
/*
* todo: implement:
* JSR, JMP, BVS, BVC, BPL, BNE, BMI, BEQ, BCS, BCC - jumping and branching, last to do
* BRK
* RTI, RTS
*/
// http://obelisk.me.uk/6502/reference.html
// http://nesdev.com/6502.txt
// http://www.6502.org/tutorials/6502opcodes.html
std::map<uint8_t, std::shared_ptr<opcode> > opmap;
const char * myasm;
// now we need to do this for all opcodes
//std::function<void ()> f = []() { std::cout << << std::endl; };
for(uint8_t i = 0x0; i < 0xff; i++)
{
addopcode(&opmap, i, "test");
opmap[i]->f = [i, cpu]() {
printf("%x-", i);
cpu->pc += 1;
return 1;
};
}
// todo: unit test all opcodes
// http://www.emulator101.com/reference/6502-reference.html
// note: all opcode functions return number of cpu cycles to execute and advance the program counter the appropriate number of bytes
// BRK
myasm = addopcode(&opmap, 0x00, "0x00 BRK");
opmap[0x00]->f = [cpu, mem, myasm]() {
if(DEBUG) printf("\n%s\n", myasm);
// The BRK instruction forces the generation of an interrupt request.
// The program counter and processor status are pushed on the stack then
// the IRQ interrupt vector at $FFFE/F is loaded into the PC and the break flag in the status set to one.
// push program counter and processor status to stack
// todo: but in what order?
push(cpu, mem, cpu->pc);
push(cpu, mem, cpu->p);
// set memory location $FFFE/F to program counter
// todo: do these bytes need to be reversed for endianness? yes
cpu->pc = revlendianbytes(mem->readmem(0xfffe), mem->readmem(0xffff));
// set break flag
cpu->p |= 1 << 4;
cpu->pc += 1;
return 7;
};
// ORA 0x01 indirect,x
myasm = addopcode(&opmap, 0x1, "0x01 ora ($");
opmap[0x1]->f = [cpu, mem, myasm]() {
if(DEBUG) printf("\n%s%x,X)\n", myasm, mem->readmem(cpu->pc+1));
ORA(cpu, indexedindirect(cpu, mem));
cpu->pc += 2;
return 6;
};
// ORA zero page 0x05
myasm = addopcode(&opmap, 0x5, "0x05 ora $");
opmap[0x5]->f = [cpu, mem, myasm]() {
if (DEBUG) printf("\n%s%x\n", myasm, mem->readmem(cpu->pc + 1));
ORA(cpu, zeropage(cpu, mem));
cpu->pc += 2;
return 3;
};
// ASL zero page 0x06
myasm = addopcode(&opmap, 0x6, "0x06 asl $");
opmap[0x6]->f = [cpu, mem, myasm]() {
if (DEBUG) printf("\n%s%x\n", myasm, mem->readmem(cpu->pc + 1));
aslmem(cpu, mem, zeropage(cpu, mem));
cpu->pc += 2;
return 5;
};
// PHP 0x08
myasm = addopcode(&opmap, 0x8, "0x08 php");
opmap[0x8]->f = [cpu, mem, myasm]() {
if (DEBUG) printf("\n%s\n", myasm);
push(cpu, mem, cpu->p);
cpu->pc += 1;
return 3;
};
// ORA immedate 0x09
myasm = addopcode(&opmap, 0x9, "0x09 ora #$");
opmap[0x9]->f = [cpu, mem, myasm]() {
if (DEBUG) printf("\n%s%x\n", myasm, mem->readmem(cpu->pc+1));
ORA(cpu, mem->readmem(cpu->pc+1));
cpu->pc += 2;
return 2;
};
// ASL accumulator 0x0a (1, 2)
myasm = addopcode(&opmap, 0x4a, "0x0a asl A");
opmap[0xa]->f = [cpu, mem, myasm]() {
if(DEBUG) printf("\n%s\n", myasm);
asla(cpu, &cpu->a);
cpu->pc += 1;
return 2;
};
// ORA absolute 0x0d (3, 4)
myasm = addopcode(&opmap, 0xd, "0x0d ora $");
opmap[0xd]->f = [cpu, mem, myasm]() {
uint16_t addr = revlendianbytes(mem->readmem(cpu->pc+1),mem->readmem(cpu->pc+2));
if(DEBUG) printf("\n%s%x\n", myasm, addr);
ORA(cpu, mem->readmem(addr));
cpu->pc += 3;
return 4;
};
// ASL absolute 0x0e
myasm = addopcode(&opmap, 0xe, "0x0e asl $");
opmap[0xe]->f = [cpu, mem, myasm]() {
uint16_t addr = revlendianbytes(mem->readmem(cpu->pc+1),mem->readmem(cpu->pc+2));
if(DEBUG) printf("\n%s%x\n", myasm, addr);
aslmem(cpu, mem, addr);
cpu->pc += 3;
return 6;
};
// ORA 0x11 indirect,y
myasm = addopcode(&opmap, 0x11, "0x11 ora ($");
opmap[0x11]->f = [cpu, mem, myasm]() {
if(DEBUG) printf("\n%s%x),Y\n", myasm, mem->readmem(cpu->pc+1));
ORA(cpu, indirectindexed(cpu, mem));
cpu->pc += 2;
return 5; // todo: +1 if page crossed
};
// ORA zero page,x (2 bytes, 4 cycles) 0x15
myasm = addopcode(&opmap, 0x15, "0x15 ora $");
opmap[0x15]->f = [cpu, mem, myasm]() {
if (DEBUG) printf("\n%s%x,X\n", myasm, mem->readmem(cpu->pc + 1));
ORA(cpu, mem->readmem(zeropagex(cpu, mem)));
cpu->pc += 2;
return 4;
};
// ASL zero page,x (2, 6) 0x16
myasm = addopcode(&opmap, 0x16, "0x16 asl $");
opmap[0x16]->f = [cpu, mem, myasm]() {
if(DEBUG) printf("\n%s%x,X\n", myasm, mem->readmem(cpu->pc + 1));
aslmem(cpu, mem, zeropagex(cpu, mem));
cpu->pc += 2;
return 6;
};
// CLC 0x18
myasm = addopcode(&opmap, 0x18, "0x18 clc");
opmap[0x18]->f = [cpu, myasm]() {
if(DEBUG) printf("\n%s\n", myasm);
// clear carry
cpu->p &= ~(1 << 7);
cpu->pc += 1;
return 2;
};
// ORA absolute,x 0x1D
myasm = addopcode(&opmap, 0x1d, "0x1d ora $");
opmap[0x1d]->f = [cpu, mem, myasm]() {
if (DEBUG) printf("\n%s%x,X\n", myasm, revlendianbytes(mem->readmem(cpu->pc + 1), mem->readmem(cpu->pc + 2)));
ORA(cpu, mem->readmem(absolutex(cpu, mem)));
cpu->pc += 3;
return 4; // todo: +1 if page crossed
};
// ASL absolute,x 0x1e
myasm = addopcode(&opmap, 0x1e, "0x1e asl $");
opmap[0x1e]->f = [cpu, mem, myasm]() {
if (DEBUG) printf("\n%s%x,X\n", myasm, revlendianbytes(mem->readmem(cpu->pc + 1), mem->readmem(cpu->pc + 2)));
aslmem(cpu, mem, absolutex(cpu, mem));
cpu->pc += 3;
return 7;
};
// TODO: reorder this
// ORA absolute,y 0x19
myasm = addopcode(&opmap, 0x19, "0x19 ora $");
opmap[0x19]->f = [cpu, mem, myasm]() {
if(DEBUG) printf("\n%s%x,Y\n", myasm, revlendianbytes(mem->readmem(cpu->pc+1), mem->readmem(cpu->pc+2)));
ORA(cpu, mem->readmem(absolutey(cpu, mem)));
cpu->pc += 3;
return 4; // todo: +1 if page crossed
};
// AND indirect,x 0x21
myasm = addopcode(&opmap, 0x21, "0x21 and ($");
opmap[0x21]->f = [cpu, mem, myasm]() {
if(DEBUG) printf("\n%s%x,X)\n", myasm, mem->readmem(cpu->pc+1));
AND(cpu, indexedindirect(cpu, mem));
cpu->pc += 2;
return 6;
};
// BIT zero page 0x24
myasm = addopcode(&opmap, 0x24, "0x24 bit $");
opmap[0x24]->f = [cpu, mem, myasm]() {
if (DEBUG) printf("\n%s%x\n", myasm, mem->readmem(cpu->pc + 1));
bit(cpu, mem, zeropage(cpu, mem));
cpu->pc += 2;
return 3;
};
// AND zero page 0x25
myasm = addopcode(&opmap, 0x25, "0x25 and $");
opmap[0x25]->f = [cpu, mem, myasm]() {
if (DEBUG) printf("\n%s%x\n", myasm, mem->readmem(cpu->pc + 1));
// todo: AND expects a value, this was passing an address (no readmem around zeropage call), check other AND opcodes
AND(cpu, mem->readmem(zeropage(cpu, mem)));
// flags are set in our AND() function
cpu->pc += 2;
return 3;
};
// ROL zero page 0x26 (2, 5)
myasm = addopcode(&opmap, 0x26, "0x26 rol $");
opmap[0x26]->f = [cpu, mem, myasm]() {
if (DEBUG) printf("\n%s%x\n", myasm, mem->readmem(cpu->pc + 1));
rolmem(cpu, mem, zeropage(cpu, mem));
cpu->pc += 2;
return 5;
};
// PLP 0x28
myasm = addopcode(&opmap, 0x28, "0x28 plp");
opmap[0x28]->f = [cpu, mem, myasm]() {
if (DEBUG) printf("\n%s\n", myasm);
cpu->p = pop(cpu, mem);
cpu->pc += 1;
return 4;
};
// AND immediate 0x29
myasm = addopcode(&opmap, 0x29, "0x29 and #$");
opmap[0x29]->f = [cpu, mem, myasm]() {
// ok, immediate addressing specifies a constant in the code
if (DEBUG) printf("\n%s%x\n", myasm, mem->readmem(cpu->pc+1));
AND(cpu, mem->readmem(cpu->pc+1));
cpu->pc += 2;
return 2;
};
// ROL accumulator 0x2a
myasm = addopcode(&opmap, 0x2a, "0x2a rol A");
opmap[0x2a]->f = [cpu, mem, myasm]() {
if(DEBUG) printf("\n%s\n", myasm);
rola(cpu, &cpu->a);
cpu->pc += 1;
return 2;
};
// BIT absolute 0x2c
myasm = addopcode(&opmap, 0x2c, "0x2c bit $");
opmap[0x2c]->f = [cpu, mem, myasm] () {
uint16_t addr = revlendianbytes(mem->readmem(cpu->pc+1),mem->readmem(cpu->pc+2));
if(DEBUG) printf("\n%s%x\n", myasm, addr);
bit(cpu, mem, addr);
cpu->pc += 3;
return 4;
};
// AND absolute 0x2d
myasm = addopcode(&opmap, 0x2d, "0x2d and $");
opmap[0x2d]->f = [cpu, mem, myasm] () {
uint16_t addr = revlendianbytes(mem->readmem(cpu->pc+1),mem->readmem(cpu->pc+2));
if(DEBUG) printf("\n%s%x\n", myasm, addr);
AND(cpu, mem->readmem(addr));
cpu->pc += 3;
return 4;
};
// ROL absolute 0x2e
myasm = addopcode(&opmap, 0x2e, "0x2e rol $");
opmap[0x2e]->f = [cpu, mem, myasm]() {
uint16_t addr = revlendianbytes(mem->readmem(cpu->pc+1),mem->readmem(cpu->pc+2));
if(DEBUG) printf("\n%s%x\n", myasm, addr);
rolmem(cpu, mem, addr);
cpu->pc += 3;
return 6;
};
// AND indirect,y 0x31
myasm = addopcode(&opmap, 0x31, "0x31 and ($");
opmap[0x31]->f = [cpu, mem, myasm]() {
if(DEBUG) printf("\n%s%x),Y\n", myasm, mem->readmem(cpu->pc+1));
AND(cpu, indirectindexed(cpu, mem));
cpu->pc += 2;
return 5; // todo: +1 if page crossed
};
// AND zero page,x 0x35
myasm = addopcode(&opmap, 0x35, "0x35 and $");
opmap[0x35]->f = [cpu, mem, myasm]() {
if (DEBUG) printf("\n%s%x,X\n", myasm, mem->readmem(cpu->pc + 1));
AND(cpu, mem->readmem(zeropagex(cpu, mem)));
// flags are set in our AND() function
cpu->pc += 2;
return 4;
};
// ROL zero page,x 0x36 (2, 6)
myasm = addopcode(&opmap, 0x36, "0x36 rol $");
opmap[0x36]->f = [cpu, mem, myasm]() {
if (DEBUG) printf("\n%s%x,X\n", myasm, mem->readmem(cpu->pc + 1));
rolmem(cpu, mem, zeropagex(cpu, mem));
cpu->pc += 2;
return 6;
};
// SEC 0x38
myasm = addopcode(&opmap, 0x38, "0x38 sec");
opmap[0x38]->f = [cpu, myasm]() {
if(DEBUG) printf("\n%s\n", myasm);
// set carry
cpu->p |= (1 << 7);
cpu->pc += 1;
return 2;
};
// AND absolute,y 0x39
myasm = addopcode(&opmap, 0x39, "0x39 and $");
opmap[0x39]->f = [cpu, mem, myasm]() {
if(DEBUG) printf("\n%s%x,Y\n", myasm, revlendianbytes(mem->readmem(cpu->pc+1), mem->readmem(cpu->pc+2)));
AND(cpu, mem->readmem(absolutey(cpu, mem)));
cpu->pc += 3;
return 4; // todo: +1 if page crossed
};
// AND absolute,x 0x3d
myasm = addopcode(&opmap, 0x3d, "0x3d and $");
opmap[0x3d]->f = [cpu, mem, myasm]() {
if (DEBUG) printf("\n%s%x,X\n", myasm, revlendianbytes(mem->readmem(cpu->pc + 1), mem->readmem(cpu->pc + 2)));
AND(cpu, mem->readmem(absolutex(cpu, mem)));
cpu->pc += 3;
return 4; // todo: +1 if page crossed
};
// ROL absolute,x 0x3e
myasm = addopcode(&opmap, 0x3e, "0x3e rol $");
opmap[0x3e]->f = [cpu, mem, myasm]() {
if (DEBUG) printf("\n%s%x,X\n", myasm, revlendianbytes(mem->readmem(cpu->pc + 1), mem->readmem(cpu->pc + 2)));
rolmem(cpu, mem, absolutex(cpu, mem));
cpu->pc += 3;
return 7;
};
// RTI 0x40
// EOR indirect,x 0x41
myasm = addopcode(&opmap, 0x41, "0x41 eor ($");
opmap[0x41]->f = [cpu, mem, myasm]() {
if(DEBUG) printf("\n%s%x,X)\n", myasm, mem->readmem(cpu->pc+1));
EOR(cpu, mem->readmem(indexedindirect(cpu, mem)));
cpu->pc += 2;
return 6;
};
// EOR zero page 0x45 (2, 3)
myasm = addopcode(&opmap, 0x45, "0x45 eor $");
opmap[0x45]->f = [cpu, mem, myasm]() {
if (DEBUG) printf("\n%s%x\n", myasm, mem->readmem(cpu->pc + 1));
EOR(cpu, mem->readmem(zeropage(cpu, mem)));
cpu->pc += 2;
return 3;
};
// LSR zero page 0x46
myasm = addopcode(&opmap, 0x46, "0x46 lsr $");
opmap[0x46]->f = [cpu, mem, myasm]() {
if (DEBUG) printf("\n%s%x\n", myasm, mem->readmem(cpu->pc + 1));
lsrmem(cpu, mem, zeropage(cpu, mem));
cpu->pc += 2;
return 5;
};
// PHA 0x48
myasm = addopcode(&opmap, 0x48, "0x48 pha");
opmap[0x48]->f = [cpu, mem, myasm]() {
if (DEBUG) printf("\n%s\n", myasm);
push(cpu, mem, cpu->a);
cpu->pc += 1;
return 3;
};
// EOR immediate 0x49
myasm = addopcode(&opmap, 0x49, "0x49 eor #$");
opmap[0x49]->f = [cpu, mem, myasm]() {
uint8_t val = mem->readmem(cpu->pc + 1);
if (DEBUG) printf("\n%s%x\n", myasm, val);
cpu->pc += 2;
return 2;
};
// LSR accumulator 0x4a
myasm = addopcode(&opmap, 0x4a, "0x4a lsr A");
opmap[0x4a]->f = [cpu, mem, myasm]() {
if(DEBUG) printf("\n%s\n", myasm);
lsra(cpu, &cpu->a);
cpu->pc += 1;
return 2;
};
// EOR absolute 0x4d (3, 4)
myasm = addopcode(&opmap, 0x4d, "0x4d eor $");
opmap[0x4d]->f = [cpu, mem, myasm]() {
uint16_t addr = revlendianbytes(mem->readmem(cpu->pc+1),mem->readmem(cpu->pc+2));
if(DEBUG) printf("\n%s%x\n", myasm, addr);
EOR(cpu, mem->readmem(addr));
cpu->pc += 3;
return 4;
};
// LSR absolute 0x4e (3,6)
myasm = addopcode(&opmap, 0x4e, "0x4e lsr $");
opmap[0x4e]->f = [cpu, mem, myasm]() {
uint16_t addr = revlendianbytes(mem->readmem(cpu->pc+1),mem->readmem(cpu->pc+2));
if(DEBUG) printf("\n%s%x\n", myasm, addr);
lsrmem(cpu, mem, addr);
cpu->pc += 3;
return 6;
};
// EOR indirect,y 0x51
myasm = addopcode(&opmap, 0x51, "0x51 eor ($");
opmap[0x51]->f = [cpu, mem, myasm]() {
if(DEBUG) printf("\n%s%x),Y\n", myasm, mem->readmem(cpu->pc+1));
EOR(cpu, mem->readmem(indirectindexed(cpu, mem)));
cpu->pc += 2;
return 5;
// todo: +1 if page crossed
};
// EOR zero page,x 0x55 (2, 4)
myasm = addopcode(&opmap, 0x55, "0x55 eor $");
opmap[0x55]->f = [cpu, mem, myasm]() {
if(DEBUG) printf("\n%s%x,X\n",myasm,mem->readmem(cpu->pc+1));
EOR(cpu, mem->readmem(zeropagex(cpu, mem)));
cpu->pc += 2;
return 4;
};
// LSR zero page,x 0x56 (2,6)
myasm = addopcode(&opmap, 0x56, "0x56 lsr $");
opmap[0x56]->f = [cpu, mem, myasm]() {
if (DEBUG) printf("\n%s%x,X\n", myasm, mem->readmem(cpu->pc + 1));
lsrmem(cpu, mem, zeropagex(cpu, mem));
cpu->pc += 2;
return 6;
};
// CLI 0x58
myasm = addopcode(&opmap, 0x58, "0x58 cli");
opmap[0x58]->f = [cpu, myasm]() {
if(DEBUG) printf("\n%s\n", myasm);
// clear interrupt disable flag
cpu->p &= ~(1 << 5);
cpu->pc += 1;
return 2;
};
// EOR absolute,y 0x59
myasm = addopcode(&opmap, 0x59, "0x59 eor $");
opmap[0x59]->f = [cpu, mem, myasm]() {
if (DEBUG) printf("\n%s%x,Y\n", myasm, revlendianbytes(mem->readmem(cpu->pc + 1), mem->readmem(cpu->pc + 2)));
EOR(cpu, mem->readmem(absolutey(cpu, mem)));
cpu->pc += 3;
return 4;
// todo: +1 if page crossed
};
// EOR absolute,x 0x5d
myasm = addopcode(&opmap, 0x5d, "0x5d eor $");
opmap[0x5d]->f = [cpu, mem, myasm]() {
if (DEBUG) printf("\n%s%x,X\n", myasm, revlendianbytes(mem->readmem(cpu->pc + 1), mem->readmem(cpu->pc + 2)));
EOR(cpu, mem->readmem(absolutex(cpu, mem)));
cpu->pc += 3;
return 4;
// todo: +1 if page crossed
};
// LSR absolute,x 0x5e (3,7)
myasm = addopcode(&opmap, 0x5e, "0x5e lsr $");
opmap[0x5e]->f = [cpu, mem, myasm]() {
if (DEBUG) printf("\n%s%x,X\n", myasm, revlendianbytes(mem->readmem(cpu->pc + 1), mem->readmem(cpu->pc + 2)));
lsrmem(cpu, mem, absolutex(cpu, mem));
cpu->pc += 3;
return 7;
};
// ADC indexed indirect
myasm = addopcode(&opmap, 0x61, "0x61 adc ($");
opmap[0x61]->f = [cpu, mem, myasm]() {
if(DEBUG) printf("\n%s%x,X)\n", myasm, mem->readmem(cpu->pc+1));
// NOTE: this had been sending an address to ADC before adding readmem()
adc(cpu, mem->readmem(indexedindirect(cpu, mem)));
cpu->pc += 2;
return 6;
};
// ROR absolute 0x6e (3,6)
myasm = addopcode(&opmap, 0x6e, "0x6e ror $");
opmap[0x6e]->f = [cpu, mem, myasm]() {
uint16_t addr = revlendianbytes(mem->readmem(cpu->pc+1),mem->readmem(cpu->pc+2));
if(DEBUG) printf("\n%s%x\n", myasm, addr);
rormem(cpu, mem, addr);
cpu->pc += 3;
return 6;
};
// ROR zero page 0x66 (2, 5)
myasm = addopcode(&opmap, 0x66, "0x66 ror $");
opmap[0x66]->f = [cpu, mem, myasm]() {
if (DEBUG) printf("\n%s%x\n", myasm, mem->readmem(cpu->pc + 1));
rormem(cpu, mem, zeropage(cpu, mem));
cpu->pc += 2;
return 5;
};
// ADC zero page
myasm = addopcode(&opmap, 0x65, "0x65 adc $");
opmap[0x65]->f = [cpu, mem, myasm]() {
if (DEBUG) printf("\n%s%x\n", myasm, mem->readmem(cpu->pc + 1));
adc(cpu, zeropage(cpu, mem));
cpu->pc += 2;
return 3;
};
// PLA 0x68
myasm = addopcode(&opmap, 0x68, "0x68 pla");
opmap[0x68]->f = [cpu, mem, myasm]() {
if (DEBUG) printf("\n%s\n", myasm);
cpu->a = pop(cpu, mem);
setzeroflag(cpu->a, cpu);
setnegflag(cpu->a, cpu);
cpu->pc += 1;
return 4;
};
// ADC immediate
myasm = addopcode(&opmap, 0x69, "0x69 adc #$");
opmap[0x69]->f = [cpu, mem, myasm]() {
uint8_t val = mem->readmem(cpu->pc+1);
if (DEBUG) printf("\n%s%x\n", myasm, val);
adc(cpu, val);
cpu->pc += 2;
return 2;
};
// ROR accumulator 0x6a
myasm = addopcode(&opmap, 0x6a, "0x6a ror A");
opmap[0x6a]->f = [cpu, mem, myasm]() {
if(DEBUG) printf("\n%s\n", myasm);
rora(cpu, &cpu->a);
cpu->pc += 1;
return 2;
};
// ADC absolute $6d
myasm = addopcode(&opmap, 0x6d, "0x6d adc $");
opmap[0x6d]->f = [cpu, mem, myasm]() {
uint16_t addr = revlendianbytes(mem->readmem(cpu->pc+1),mem->readmem(cpu->pc+2));
if(DEBUG) printf("\n%s%x\n", myasm, addr);
adc(cpu, mem->readmem(addr));
cpu->pc += 3;
return 4;
};
// ROR absolute 0x6e 3, 6
myasm = addopcode(&opmap, 0x6e, "0x6e ror $");
opmap[0x6e]->f = [cpu, mem, myasm]() {
uint16_t addr = revlendianbytes(mem->readmem(cpu->pc+1),mem->readmem(cpu->pc+2));
if(DEBUG) printf("\n%s%x\n", myasm, addr);
rormem(cpu, mem, addr);
cpu->pc += 3;
return 6;
};
// ADC indirect indexed $71
myasm = addopcode(&opmap, 0x71, "0x71 adc ($");
opmap[0x71]->f = [cpu, mem, myasm]() {
if(DEBUG) printf("\n%s%x),Y\n", myasm, mem->readmem(cpu->pc+1));
adc(cpu, mem->readmem(indirectindexed(cpu, mem)));
cpu->pc += 2;
return 5;
// todo: +1 if page crossed
};
// ADC zero page,x $75
myasm = addopcode(&opmap, 0x75, "0x75 adc $");
opmap[0x75]->f = [cpu, mem, myasm]() {
if(DEBUG) printf("\n%s%x,X\n",myasm,mem->readmem(cpu->pc+1));
adc(cpu, mem->readmem(zeropagex(cpu, mem)));
cpu->pc += 2;
return 4;
};
// ROR zero page,x 0x76 (2,6)
myasm = addopcode(&opmap, 0x76, "0x76 ror $");
opmap[0x76]->f = [cpu, mem, myasm]() {
if (DEBUG) printf("\n%s%x,X\n", myasm, mem->readmem(cpu->pc + 1));
rormem(cpu, mem, zeropagex(cpu, mem));
cpu->pc += 2;
return 6;
};
// SEI 0x78
myasm = addopcode(&opmap, 0x78, "0x78 sei");
opmap[0x78]->f = [cpu, myasm] () {
if(DEBUG) printf("\n%s\n", myasm);
// set interrupt disable flag
cpu->p |= (1 << 5);
cpu->pc += 1;
return 2;
};
// ADC absolute,y $79
myasm = addopcode(&opmap, 0x79, "0x79 adc $");
opmap[0x79]->f = [cpu, mem, myasm]() {
if(DEBUG) printf("\n%s%x,Y\n", myasm, revlendianbytes(mem->readmem(cpu->pc+1), mem->readmem(cpu->pc+2)));
adc(cpu, mem->readmem(absolutey(cpu, mem)));
cpu->pc += 3;
return 4;
// todo: +1 if page crossed
};
// ADC absolute,x $7d
myasm = addopcode(&opmap, 0x7d, "0x7d adc $");
opmap[0x7d]->f = [cpu, mem, myasm]() {
if (DEBUG) printf("\n%s%x,X\n", myasm, revlendianbytes(mem->readmem(cpu->pc + 1), mem->readmem(cpu->pc + 2)));
adc(cpu, mem->readmem(absolutex(cpu, mem)));
cpu->pc += 3;
return 4;
// todo: +1 if page crossed
};
// ROR absolute,x 0x7e
myasm = addopcode(&opmap, 0x7e, "0x7e ror $");
opmap[0x7e]->f = [cpu, mem, myasm]() {
if (DEBUG) printf("\n%s%x,X\n", myasm, revlendianbytes(mem->readmem(cpu->pc + 1), mem->readmem(cpu->pc + 2)));
rormem(cpu, mem, absolutex(cpu, mem));
cpu->pc += 3;
return 7;
};
// STA indexed indirect
myasm = addopcode(&opmap, 0x81, "0x81 sta ($");
opmap[0x81]->f = [cpu, mem, myasm]() {
if(DEBUG) printf("\n%s%x,X)\n", myasm, mem->readmem(cpu->pc+1));
sta(cpu, mem, indexedindirect(cpu, mem));
cpu->pc += 2;
return 6;
};
// STY zero page 0x84
myasm = addopcode(&opmap, 0x84, "0x84 sty $");
opmap[0x84]->f = [cpu, mem, myasm]() {
if(DEBUG) printf("\n%s%x\n", myasm, mem->readmem(cpu->pc+1));
sty(cpu, mem, mem->readmem(zeropage(cpu, mem)));
cpu->pc += 2;
return 3;
};
// STA zero page
myasm = addopcode(&opmap, 0x85, "0x85 sta $");
opmap[0x85]->f = [cpu, mem, myasm]() {
if(DEBUG) printf("\n%s%x\n", myasm, mem->readmem(cpu->pc+1));
sta(cpu, mem, mem->readmem(zeropage(cpu, mem)));
cpu->pc += 2;
return 3;
};
// STX zero page 0x86
myasm = addopcode(&opmap, 0x86, "0x86 stx $");
opmap[0x86]->f = [cpu, mem, myasm]() {
if(DEBUG) printf("\n%s%x\n", myasm, mem->readmem(cpu->pc+1));
stx(cpu, mem, mem->readmem(zeropage(cpu, mem)));
cpu->pc += 2;
return 3;
};
// DEY 0x88
myasm = addopcode(&opmap, 0x88, "0x88 dey");
opmap[0x88]->f = [cpu, mem, myasm]() {
if(DEBUG) printf("\n%s\n", myasm);
decreg(cpu, &cpu->y);
cpu->pc += 1;
return 2;
};
// TXA 0x8a
myasm = addopcode(&opmap, 0x8a, "0x8a txa");
opmap[0x8a]->f = [cpu, myasm]() {
if(DEBUG) printf("\n%s\n", myasm);
cpu->a = cpu->x;
setzeroflag(cpu->a, cpu);
setnegflag(cpu->a, cpu);
cpu->pc += 1;
return 2;
};
// STY absolute 0x8c
myasm = addopcode(&opmap, 0x8c, "0x8c sty $");
opmap[0x8c]->f = [cpu, mem, myasm]() {
uint16_t addr = revlendianbytes(mem->readmem(cpu->pc+1),mem->readmem(cpu->pc+2));
if(DEBUG) printf("\n%s%x\n", myasm, addr);
// I don't think previous method of absolute addressing is correct
// we now write directory to addr address, not the address read from there
// should be correct now, but also check absolute,x and absolute,y (I think they are correct)
// all in all - todo: unit test
sty(cpu, mem, addr);
cpu->pc += 3;
return 4;
};
// STA absolute
myasm = addopcode(&opmap, 0x8d, "0x8d sta $");
opmap[0x8d]->f = [cpu, mem, myasm]() {
uint16_t addr = revlendianbytes(mem->readmem(cpu->pc+1),mem->readmem(cpu->pc+2));
if(DEBUG) printf("\n%s%x\n", myasm, addr);
sta(cpu, mem, addr);
cpu->pc += 3;
return 4;
};
// STX absolute 0x8e
myasm = addopcode(&opmap, 0x8e, "0x8e stx $");
opmap[0x8e]->f = [cpu, mem, myasm]() {
uint16_t addr = revlendianbytes(mem->readmem(cpu->pc+1),mem->readmem(cpu->pc+2));
if(DEBUG) printf("\n%s%x\n", myasm, addr);
stx(cpu, mem, addr);
cpu->pc += 3;
return 4;
};
// STA indirect indexed
myasm = addopcode(&opmap, 0x91, "0x91 sta ($");
opmap[0x91]->f = [cpu, mem, myasm]() {
if(DEBUG) printf("\n%s%x),Y\n", myasm, mem->readmem(cpu->pc+1));
sta(cpu, mem, mem->readmem(indirectindexed(cpu, mem)));
cpu->pc += 2;
return 6;
};
// STY zero page,x 0x94
myasm = addopcode(&opmap, 0x94, "0x94 sty $");
opmap[0x94]->f = [cpu, mem, myasm]() {
if(DEBUG) printf("\n%s%x,X\n",myasm,mem->readmem(cpu->pc+1));
sty(cpu, mem, mem->readmem(zeropagex(cpu, mem)));
cpu->pc += 2;
return 4;
};
// STA zero page,x
myasm = addopcode(&opmap, 0x95, "0x95 sta $");
opmap[0x95]->f = [cpu, mem, myasm]() {
if(DEBUG) printf("\n%s%x,X\n",myasm,mem->readmem(cpu->pc+1));
sta(cpu, mem, mem->readmem(zeropagex(cpu, mem)));
cpu->pc += 2;
return 4;
};
// STX zero page,y 0x96
myasm = addopcode(&opmap, 0x96, "0x96 stx $");
opmap[0x96]->f = [cpu, mem, myasm]() {
if (DEBUG) printf("\n%s%x,Y\n", myasm, mem->readmem(cpu->pc+1));
stx(cpu, mem, mem->readmem(zeropagey(cpu, mem)));
cpu->pc += 2;
return 4;
};
// TYA 0x98
myasm = addopcode(&opmap, 0x98, "0x98 tya");
opmap[0x98]->f = [cpu, myasm]() {
if(DEBUG) printf("\n%s\n", myasm);
cpu->a = cpu->y;
setzeroflag(cpu->a, cpu);
setnegflag(cpu->a, cpu);
cpu->pc += 1;
return 2;
};
// STA absolute,y
myasm = addopcode(&opmap, 0x99, "0x99 sta $");
opmap[0x99]->f = [cpu, mem, myasm]() {
if(DEBUG) printf("\n%s%x,Y\n", myasm, revlendianbytes(mem->readmem(cpu->pc+1), mem->readmem(cpu->pc+2)));
sta(cpu, mem, mem->readmem(absolutey(cpu, mem)));
cpu->pc += 3;
return 5;
};
// TXS 0x9a
myasm = addopcode(&opmap, 0x9a, "0x9a txs");
opmap[0x9a]->f = [cpu, myasm]() {
if(DEBUG) printf("\n%s\n", myasm);
cpu->sp = cpu->x;
cpu->pc += 1;
return 2;
};
// STA absolute,x
myasm = addopcode(&opmap, 0x9d, "0x9d sta $");
opmap[0x9d]->f = [cpu, mem, myasm]() {
if (DEBUG) printf("\n%s%x,X\n", myasm, revlendianbytes(mem->readmem(cpu->pc + 1), mem->readmem(cpu->pc + 2)));
sta(cpu, mem, mem->readmem(absolutex(cpu, mem)));
cpu->pc += 3;
return 5;
};
// LDY immediate 0xa0
myasm = addopcode(&opmap, 0xa0, "0xa0 ldy #$");
opmap[0xa0]->f = [cpu, mem, myasm]() {
uint8_t val = mem->readmem(cpu->pc+1);
if(DEBUG) printf("\n%s%x\n", myasm, val);
ld(&cpu->y, cpu, val);
cpu->pc += 2;
return 2;
};
// LDA
// indexed indirect address mode - slightly convoluted
// http://www.emulator101.com/6502-addressing-modes.html
myasm = addopcode(&opmap, 0xa1, "0xa1 lda ($");
opmap[0xa1]->f = [cpu, mem, myasm]() {
// todo: the readmem method returns a uint8_t, should it return uint16_t?
if(DEBUG) printf("\n%s%x,X)\n", myasm, mem->readmem(cpu->pc+1));
ld(&cpu->a, cpu, mem->readmem(indexedindirect(cpu, mem)));
cpu->pc += 2;
return 6;
};
// LDX immediate 0xa2
myasm = addopcode(&opmap, 0xa2, "0xa2 ldx #$");
opmap[0xa2]->f = [cpu, mem, myasm]() {
uint8_t val = mem->readmem(cpu->pc+1);
if(DEBUG) printf("\n%s%x\n", myasm, val);
ld(&cpu->x, cpu, val);
cpu->pc += 2;
return 2;
};
// LDY zero page
myasm = addopcode(&opmap, 0xa4, "0xa4 ldy $");
opmap[0xa4]->f = [cpu, mem, myasm]() {
if(DEBUG) printf("\n%s%x\n", myasm, mem->readmem(cpu->pc+1));
// I think passing a uint8_t to a uint16_t should work without issue
ld(&cpu->y, cpu, mem->readmem(zeropage(cpu, mem)));
cpu->pc += 2;