-
Notifications
You must be signed in to change notification settings - Fork 0
/
instructions.cpp
2001 lines (1943 loc) · 50.1 KB
/
instructions.cpp
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 <cstddef>
#include <iostream>
#include <sstream>
#include <string>
#include <locale>
#include "nes_instruction.h"
#include "instructions.h"
#include "state.h"
void hex_print(unsigned short num, unsigned min_length) {
std::locale loc;
std::stringstream stream;
stream << std::hex << num;
std::string str = stream.str();
while(str.length() < min_length) {
str = "0" + str;
}
for (unsigned i=0; i<str.length(); i++) {
std::cout << std::toupper(str[i],loc);
}
}
void push_val(state& current_state, unsigned short val) {
current_state.set_memory(0x100+*current_state.get_reg('s'), val & 0x00FF);
// std::cout << "pushing: ";
// hex_print(val & 0x00FF, 2);
// std::cout << " to: ";
// hex_print(0x100+*current_state.get_reg('s'), 4);
// std::cout << std::endl;
current_state.set_reg('s', *current_state.get_reg('s') - 1);
}
unsigned short pull_val(state& current_state) {
current_state.set_reg('s', *current_state.get_reg('s') + 1);
unsigned short tmp = *current_state.get_memory(0x100+*current_state.get_reg('s'));
return tmp & 0x00FF;
}
void set_z_flag(unsigned short val, state& current_state) {
current_state.set_flag('z', val == 0);
}
void set_n_flag(unsigned short val, state& current_state) {
current_state.set_flag('n', (bool)(val & 0b10000000));
}
void load(state& current_state, unsigned short a_address, unsigned short * b) { // load memory to register
unsigned short * a = current_state.get_memory(a_address);
*b = *a & 0x00FF;
set_z_flag(*b, current_state);
set_n_flag(*b, current_state);
}
void store(state& current_state, unsigned short a_address, unsigned short * b) { // store register to memory
current_state.set_memory(a_address, *b & 0x00FF);
}
void reg_to_acc(state& current_state, unsigned short a_address, unsigned short * b) { // set accumulator to b
*current_state.get_reg('a') = *b & 0x00FF;
set_z_flag(*current_state.get_reg('a'), current_state);
set_n_flag(*current_state.get_reg('a'), current_state);
}
void reg_to_inc_x(state& current_state, unsigned short a_address, unsigned short * b) { // set x incrementor to b
*current_state.get_reg('x') = *b & 0x00FF;
set_z_flag(*current_state.get_reg('x'), current_state);
set_n_flag(*current_state.get_reg('x'), current_state);
}
void reg_to_inc_y(state& current_state, unsigned short a_address, unsigned short * b) { // set y incrementor to b
*current_state.get_reg('y') = *b & 0x00FF;
set_z_flag(*current_state.get_reg('y'), current_state);
set_n_flag(*current_state.get_reg('y'), current_state);
}
void reg_to_sp(state& current_state, unsigned short a_address, unsigned short * b) { // set stack pointer to b
*current_state.get_reg('s') = *b & 0x00FF;
}
void add_carry(state& current_state, unsigned short a_address, unsigned short * b) { // add a and b (8-bit) with carry flag
unsigned short * a = current_state.get_memory(a_address);
unsigned short carry = 0;
if (current_state.get_flag('c')) {
carry = 1;
}
unsigned short result = *a + *b+ carry;
if (!(*a & 0b10000000) ^ (*b & 0b10000000)) {
if ((*a & 0b10000000) ^ (result & 0b10000000)) {
current_state.set_flag('v', true);
} else {
current_state.set_flag('v', false);
}
} else {
current_state.set_flag('v', false);
}
current_state.set_flag('c', (bool)(result & 0b100000000));
*b = result & 0x00FF;
set_z_flag(*b, current_state);
set_n_flag(*b, current_state);
}
void subtract_carry(state& current_state, unsigned short a_address, unsigned short * b) { // add a and b (8-bit) with carry flag
unsigned short * a = current_state.get_memory(a_address);
unsigned short carry = 1;
if (current_state.get_flag('c')) {
carry = 0;
}
unsigned short result = *b - (*a + carry);
if ((*a & 0b10000000) ^ (*b & 0b10000000)) {
if ((*b & 0b10000000) ^ (result & 0b10000000)) {
current_state.set_flag('v', true);
} else {
current_state.set_flag('v', false);
}
} else {
current_state.set_flag('v', false);
}
current_state.set_flag('c', !(bool)(result & 0b100000000));
*b = result & 0x00FF;
set_z_flag(*b, current_state);
set_n_flag(*b, current_state);
}
void bitwise_and(state& current_state, unsigned short a_address, unsigned short * b) { // and a and b
unsigned short * a = current_state.get_memory(a_address);
*b = *a & *b;
set_z_flag(*current_state.get_reg('a'), current_state);
set_n_flag(*b, current_state);
}
void bitwise_or(state& current_state, unsigned short a_address, unsigned short * b) { // or a and b
unsigned short * a = current_state.get_memory(a_address);
*b = *a | *b;
set_z_flag(*current_state.get_reg('a'), current_state);
set_n_flag(*b, current_state);
}
void bitwise_xor(state& current_state, unsigned short a_address, unsigned short * b) { // exclusive or a and b
unsigned short * a = current_state.get_memory(a_address);
*b = *a^*b;
set_z_flag(*current_state.get_reg('a'), current_state);
set_n_flag(*b, current_state);
}
void shift_left(state& current_state, unsigned short a_address, unsigned short * b) { // shift a or b left one bit
unsigned short * a = current_state.get_memory(a_address);
unsigned short tmp;
if (a) {
tmp = *a << 1;
current_state.set_memory(a_address, tmp & 0x00FF);
} else if (b) {
tmp = *b << 1;
*b = tmp & 0x00FF;
}
current_state.set_flag('c', (bool)(tmp & 0b100000000));
set_z_flag(tmp & 0x00FF, current_state);
set_n_flag(tmp & 0x00FF, current_state);
}
void shift_right(state& current_state, unsigned short a_address, unsigned short * b) { // shift a or b left one bit
unsigned short * a = current_state.get_memory(a_address);
unsigned short tmp;
if (a) {
current_state.set_flag('c', (bool)(*a & 0b00000001));
tmp = *a >> 1;
current_state.set_memory(a_address, tmp & 0x00FF);
} else if (b) {
current_state.set_flag('c', (bool)(*b & 0b00000001));
tmp = *b >> 1;
*b = tmp & 0x00FF;
}
set_z_flag(tmp & 0x00FF, current_state);
set_n_flag(tmp & 0x00FF, current_state);
}
void roll_left(state& current_state, unsigned short a_address, unsigned short * b) { // shift a or b left one bit
unsigned short * a = current_state.get_memory(a_address);
unsigned short * tmp;
if (a) {
tmp = a; // TODO: use set_memory
} else if (b) {
tmp = b;
}
bool tmpc = current_state.get_flag('c');
current_state.set_flag('c', (bool)(*tmp & 0b10000000));
*tmp = *tmp << 1;
if (tmpc) {
*tmp = *tmp | 0b00000001;
}
set_z_flag(*current_state.get_reg('a'), current_state);
set_n_flag(*tmp, current_state);
*tmp = *tmp & 0x00FF;
}
void roll_right(state& current_state, unsigned short a_address, unsigned short * b) { // shift a or b right one bit
unsigned short * a = current_state.get_memory(a_address);
unsigned short * tmp;
if (a) {
tmp = a; // TODO: use set_memory
} else if (b) {
tmp = b;
}
bool tmpc = current_state.get_flag('c');
current_state.set_flag('c', (bool)(*tmp & 0b00000001));
*tmp = *tmp >> 1;
if (tmpc) {
*tmp = *tmp | 0b10000000;
}
set_z_flag(*current_state.get_reg('a'), current_state);
set_n_flag(*tmp, current_state);
*tmp = *tmp & 0x00FF;
}
void test_bits(state& current_state, unsigned short a_address, unsigned short * b) { // test bits in memory based on the accumulator
unsigned short * a = current_state.get_memory(a_address);
unsigned short tmp = *a & *b;
current_state.set_flag('z', tmp == 0);
current_state.set_flag('v', (bool)(*a & 0b01000000));
current_state.set_flag('n', (bool)(*a & 0b10000000));
}
void branch(state& current_state, unsigned short a_address, unsigned short * b) { // jump if the bit in b is set in the p register
if (*(current_state.get_reg('p')) & *b) {
current_state.set_reg('c', a_address); // jump
*b = *b | 0b100000000; // set this bit to notify the execute_instruction function that we took a branch
}
}
void branch_not(state& current_state, unsigned short a_address, unsigned short * b) { // jump if the bit in b is not set in the p register
if (!(*(current_state.get_reg('p')) & *b)) {
current_state.set_reg('c', a_address); // jump
*b = *b | 0b100000000; // set this bit to notify the execute_instruction function that we took a branch
}
}
void compare(state& current_state, unsigned short a_address, unsigned short * b) { // compare a to b
unsigned short * a = current_state.get_memory(a_address);
current_state.set_flag('c', false);
current_state.set_flag('z', false);
if ((*b & 0x00FF) >= (*a & 0x00FF)) {
current_state.set_flag('c', true);
}
if ((*a & 0x00FF) == (*b & 0x00FF)) {
current_state.set_flag('z', true);
}
current_state.set_flag('n', (bool)((*b-*a) & 0b10000000));
}
void CLC(state& current_state, unsigned short a_address, unsigned short * b) { //
current_state.set_flag('c', false);
}
void CLD(state& current_state, unsigned short a_address, unsigned short * b) { //
current_state.set_flag('d', false);
}
void CLI(state& current_state, unsigned short a_address, unsigned short * b) { //
current_state.set_flag('i', false);
}
void CLV(state& current_state, unsigned short a_address, unsigned short * b) { //
current_state.set_flag('v', false);
}
void SEC(state& current_state, unsigned short a_address, unsigned short * b) { //
current_state.set_flag('c', true);
}
void SED(state& current_state, unsigned short a_address, unsigned short * b) { //
current_state.set_flag('d', true);
}
void SEI(state& current_state, unsigned short a_address, unsigned short * b) { //
current_state.set_flag('i', true);
}
void jump(state& current_state, unsigned short a_address, unsigned short * b) { // jump to a unconditionally
current_state.set_reg('c', a_address);
}
void inc_mem(state& current_state, unsigned short a_address, unsigned short * b) { // increment a
unsigned short * a = current_state.get_memory(a_address);
current_state.set_memory(a_address, (*a + 1) & 0x00FF);
set_z_flag(*a, current_state);
set_n_flag(*a, current_state);
}
void inc_reg(state& current_state, unsigned short a_address, unsigned short * b) { // increment b
*b = (*b + 1) & 0x00FF;
set_z_flag(*b, current_state);
set_n_flag(*b, current_state);
}
void dec_mem(state& current_state, unsigned short a_address, unsigned short * b) { // decrement a
unsigned short * a = current_state.get_memory(a_address);
current_state.set_memory(a_address, (*a - 1) & 0x00FF);
set_z_flag(*a, current_state);
set_n_flag(*a, current_state);
}
void dec_reg(state& current_state, unsigned short a_address, unsigned short * b) { // decrement b
*b = (*b - 1) & 0x00FF;
set_z_flag(*b, current_state);
set_n_flag(*b, current_state);
}
void jump_subrutine(state& current_state, unsigned short a_address, unsigned short * b) { // jump to a subroutine, pushing pc onto stack
push_val(current_state, (*current_state.get_reg('c')-1)>>8 & 0x00FF);
push_val(current_state, (*current_state.get_reg('c')-1) & 0x00FF);
current_state.set_reg('c', a_address);
}
void return_subrutine(state& current_state, unsigned short a_address, unsigned short * b) { // jump from a subroutine, pull pc from the stack
unsigned short tmp;
tmp = pull_val(current_state)&0x00FF;
tmp = tmp | ((pull_val(current_state)&0x00FF)<<8);
current_state.set_reg('c', tmp+1);
}
void push_reg(state& current_state, unsigned short a_address, unsigned short * b) { // push b onto the stack
push_val(current_state, *b);
}
void push_flags(state& current_state, unsigned short a_address, unsigned short * b) {
push_val(current_state, *b | 0b00110000);
}
void pull_acc(state& current_state, unsigned short a_address, unsigned short * b) { // pull b from the stack, and set z aand n flags
*b = pull_val(current_state);
set_n_flag(*b, current_state);
set_z_flag(*b, current_state);
}
void pull_flags(state& current_state, unsigned short a_address, unsigned short * b) { // pull b from the stack
current_state.set_reg('p', pull_val(current_state));
}
void return_interupt(state& current_state, unsigned short a_address, unsigned short * b) { // return from an interupt proccesing subroutine
current_state.set_reg('p', pull_val(current_state));
unsigned short tmp;
tmp = pull_val(current_state)&0x00FF;
tmp = tmp | ((pull_val(current_state)&0x00FF)<<8);
current_state.set_reg('c', tmp);
}
void cpu::execute_instruction(state& current_state, bool debug_mode) {
// std::cout << "ins_start" << std::endl;
unsigned short pc = *current_state.get_reg('c');
unsigned short inst_hex = *current_state.get_memory(pc);
nes_instruction instruction = instructions[inst_hex];
unsigned short a_address;
unsigned short * b;
unsigned short tmp;
// std::cout << "ins_fetch" << std::endl;
if (debug_mode) {
hex_print(pc, 4);
std::cout << " ";
}
unsigned short inst_size;
bool page_boundary_crossed = false;
switch (instruction.address_type) {
case MODE_NOTHING: a_address = 0;
inst_size = (unsigned short)1;
break;
case MODE_IMMEDIATE: a_address = pc+1;
inst_size = (unsigned short)2;
break;
case MODE_ZEROPAGE: a_address = *current_state.get_memory(pc+1);
inst_size = (unsigned short)2;
break;
case MODE_ZEROPAGEX: a_address = (*current_state.get_memory(pc+1) + *current_state.get_reg('x')) & 0xFF;
inst_size = (unsigned short)2;
break;
case MODE_ZEROPAGEY: a_address = (*current_state.get_memory(pc+1) + *current_state.get_reg('y')) & 0xFF;
inst_size = (unsigned short)2;
break;
case MODE_ABSOLUTE: a_address = *current_state.get_memory(pc+1) + (*current_state.get_memory(pc+2)<<8);
inst_size = (unsigned short)3;
break;
case MODE_ABSOLUTEX: tmp = (*current_state.get_memory(pc+1) + (*current_state.get_memory(pc+2)<<8));
a_address = tmp + *current_state.get_reg('x');
page_boundary_crossed = (tmp & 0xFF00) != (a_address & 0xFF00);
inst_size = (unsigned short)3;
break;
case MODE_ABSOLUTEY: tmp = (*current_state.get_memory(pc+1) + (*current_state.get_memory(pc+2)<<8));
a_address = tmp + *current_state.get_reg('y');
page_boundary_crossed = (tmp & 0xFF00) != (a_address & 0xFF00);
inst_size = (unsigned short)3;
break;
case MODE_INDIRECT: tmp = *current_state.get_memory(pc+1) + (*current_state.get_memory(pc+2)<<8);
a_address = ((tmp&0xFF) == 0xFF) ? (*current_state.get_memory(tmp) + (*current_state.get_memory(tmp&0xFF00)<<8)):(*current_state.get_memory(tmp) + (*current_state.get_memory(tmp+1)<<8));
inst_size = (unsigned short)3;
break;
case MODE_INDIRECTX: tmp = (*current_state.get_memory(pc+1) + *current_state.get_reg('x')) & 0xFF;
a_address = *current_state.get_memory(tmp) + (*current_state.get_memory((tmp+1) & 0x00FF)<<8);
page_boundary_crossed = (tmp & 0xFF00) != (a_address & 0xFF00);
inst_size = (unsigned short)2;
break;
case MODE_INDIRECTY: tmp = *current_state.get_memory(*current_state.get_memory(pc+1)) + (*current_state.get_memory((*current_state.get_memory(pc+1)+1)&0xFF) << 8);
a_address = tmp + *current_state.get_reg('y');
page_boundary_crossed = (tmp & 0xFF00) != (a_address & 0xFF00);
inst_size = (unsigned short)2;
break;
case MODE_RELATIVE: tmp = *current_state.get_reg('c')+2; // offset for this instruction (since 0 goes to the next instruction)
short offset = *current_state.get_memory(pc+1);
if (offset & 0x80) {
offset = offset - 0x100;
}
a_address = tmp + offset;
page_boundary_crossed = (tmp & 0xFF00) != (a_address & 0xFF00);
inst_size = (unsigned short)2;
break;
}
// std::cout << "ins_addr" << std::endl;
if (debug_mode) {
for (int i = 0; i < 3; i++) {
if (i < inst_size) {
hex_print(*current_state.get_memory(pc+i), 2);
} else {
std::cout << " ";
}
std::cout << " ";
}
if (instruction.is_unofficial) {
std::cout << "*";
} else {
std::cout << " ";
}
std::cout << instruction.opcode << " ";
unsigned int a = 0xFF;
if (!(a_address >= 0x2000 && a_address < 0x4020)) { // reading may result in side-affects
a = *current_state.get_memory(a_address);
}
switch (instruction.address_type) {
case MODE_NOTHING:
if (inst_hex == 0x0A || inst_hex == 0x4A || inst_hex == 0x2A || inst_hex == 0x6A) {
std::cout << "A ";
} else {
std::cout << " ";
}
break;
case MODE_IMMEDIATE:
std::cout << "#$";
hex_print(a, 2);
std::cout << " ";
break;
case MODE_ZEROPAGE:
std::cout << "$";
hex_print(a_address, 2);
std::cout << " = ";
hex_print(a, 2);
std::cout << " ";
break;
case MODE_ZEROPAGEX:
std::cout << "$";
hex_print(*current_state.get_memory(pc+1), 2);
std::cout << ",X @ ";
hex_print(a_address, 2);
std::cout << " = ";
hex_print(a, 2);
std::cout << " ";
break;
case MODE_ZEROPAGEY:
std::cout << "$";
hex_print(*current_state.get_memory(pc+1), 2);
std::cout << ",Y @ ";
hex_print(a_address, 2);
std::cout << " = ";
hex_print(a, 2);
std::cout << " ";
break;
case MODE_ABSOLUTE:
std::cout << "$";
hex_print(a_address, 4);
if (instruction.opcode[0] == 'J') {
std::cout << " ";
} else {
std::cout << " = ";
hex_print(a, 2);
std::cout << " ";
}
break;
case MODE_ABSOLUTEX:
std::cout << "$";
hex_print(*current_state.get_memory(pc+1) + (*current_state.get_memory(pc+2)<<8), 4);
std::cout << ",X @ ";
hex_print(a_address, 4);
std::cout << " = ";
hex_print(a, 2);
std::cout << " ";
break;
case MODE_ABSOLUTEY:
std::cout << "$";
hex_print(*current_state.get_memory(pc+1) + (*current_state.get_memory(pc+2)<<8), 4);
std::cout << ",Y @ ";
hex_print(a_address, 4);
std::cout << " = ";
hex_print(a, 2);
std::cout << " ";
break;
case MODE_INDIRECT:
std::cout << "($";
hex_print(*current_state.get_memory(pc+1) + (*current_state.get_memory(pc+2)<<8), 4);
std::cout << ") = ";
hex_print(a_address, 4);
std::cout << " ";
break;
case MODE_INDIRECTX:
std::cout << "($";
hex_print(*current_state.get_memory(pc+1), 2);
std::cout << ",X) @ ";
hex_print((*current_state.get_memory(pc+1)+*current_state.get_reg('x')) & 0xFF, 2);
std::cout << " = ";
hex_print(a_address, 4);
std::cout << " = ";
hex_print(a, 2);
std::cout << " ";
break;
case MODE_INDIRECTY:
std::cout << "($";
hex_print(*current_state.get_memory(pc+1), 2);
std::cout << "),Y = ";
tmp = *current_state.get_memory(*current_state.get_memory(pc+1)) + (*current_state.get_memory((*current_state.get_memory(pc+1)+1)&0xFF) << 8);
hex_print(tmp, 4);
std::cout << " @ ";
hex_print(tmp + *current_state.get_reg('y'), 4);
std::cout << " = ";
hex_print(*current_state.get_memory(tmp + *current_state.get_reg('y')), 2);
std::cout << " ";
break;
case MODE_RELATIVE:
std::cout << "$";
hex_print(a_address, 4);
std::cout << " ";
}
std::cout << "A:";
hex_print(*current_state.get_reg('a'), 2);
std::cout << " X:";
hex_print(*current_state.get_reg('x'), 2);
std::cout << " Y:";
hex_print(*current_state.get_reg('y'), 2);
std::cout << " P:";
hex_print(*current_state.get_reg('p'), 2);
std::cout << " SP:";
hex_print(*current_state.get_reg('s'), 2);
std::cout << " CYCLES: ";
std::cout << current_state.get_cycle();
std::cout << " PPU_BUF: ";
std::cout << current_state.get_ppu_buf();
std::cout << " STACK:";
for (int i = 0xFF; i > *current_state.get_reg('s'); i--) {
std::cout << " ";
hex_print(*current_state.get_memory(i + 0x0100) & 0x00FF, 2);
}
std::cout << std::endl;
}
// std::cout << "ins_debug" << std::endl;
pc += inst_size;
current_state.set_reg('c', pc);
if (instruction.pram_register != 'd') {
b = current_state.get_reg(instruction.pram_register);
} else {
b = NULL;
}
// std::cout << "ins_pc" << std::endl;
if (instruction.execute_function) {
if (instruction.is_branch) {
tmp = 0;
char oc[4] = {instruction.opcode[0], instruction.opcode[1], instruction.opcode[2], 0};
switch(oc[1]) {
case 'C':
tmp = 0b00000001;
break;
case 'E':
case 'N':
tmp = 0b00000010;
break;
case 'V':
tmp = 0b01000000;
break;
default:
tmp = 0b10000000;
}
instruction.execute_function(current_state, a_address, &tmp);
if (tmp & 0x100) {
current_state.inc_cycle(1);
} else {
page_boundary_crossed = false;
}
// std::cout << "ins_branch" << std::endl;
} else if (instruction.opcode[0] == 'J') {
// std::cout << "ins_jump" << std::endl;
instruction.execute_function(current_state, a_address, b);
} else if (instruction.address_type == MODE_NOTHING) {
// std::cout << "ins_mode_a" << std::endl;
instruction.execute_function(current_state, 0, b);
} else {
// std::cout << "ins_other" << std::endl;
instruction.execute_function(current_state, a_address, b);
}
}
// std::cout << "ins_exec" << std::endl;
current_state.inc_cycle(instruction.cycles + ((instruction.page_boundary_slowdown & page_boundary_crossed) ? 1 : 0));
// std::cout << "ins_cycle\nins_end" << std::endl;
}
cpu::cpu(void) {
//ADC instructions
instructions[0x69] = nes_instruction(
2, // time
MODE_IMMEDIATE, // mode
'a', // pram 2
false, // page boundary slowdown
"ADC", // opcode
add_carry // function
);
instructions[0x65] = nes_instruction(
3, // time
MODE_ZEROPAGE, // mode
'a', // pram 2
false, // page boundary slowdown
"ADC", // opcode
add_carry // function
);
instructions[0x75] = nes_instruction(
4, // time
MODE_ZEROPAGEX, // mode
'a', // pram 2
false, // page boundary slowdown
"ADC", // opcode
add_carry // function
);
instructions[0x6D] = nes_instruction(
4, // time
MODE_ABSOLUTE, // mode
'a', // pram 2
false, // page boundary slowdown
"ADC", // opcode
add_carry // function
);
instructions[0x7D] = nes_instruction(
4, // time
MODE_ABSOLUTEX, // mode
'a', // pram 2
true, // page boundary slowdown
"ADC", // opcode
add_carry // function
);
instructions[0x79] = nes_instruction(
4, // time
MODE_ABSOLUTEY, // mode
'a', // pram 2
true, // page boundary slowdown
"ADC", // opcode
add_carry // function
);
instructions[0x61] = nes_instruction(
6, // time
MODE_INDIRECTX, // mode
'a', // pram 2
false, // page boundary slowdown
"ADC", // opcode
add_carry // function
);
instructions[0x71] = nes_instruction(
5, // time
MODE_INDIRECTY, // mode
'a', // pram 2
true, // page boundary slowdown
"ADC", // opcode
add_carry // function
);
//AND instructions
instructions[0x29] = nes_instruction(
2, // time
MODE_IMMEDIATE, // mode
'a', // pram 2
false, // page boundary slowdown
"AND", // opcode
bitwise_and // function
);
instructions[0x25] = nes_instruction(
2, // time
MODE_ZEROPAGE, // mode
'a', // pram 2
false, // page boundary slowdown
"AND", // opcode
bitwise_and // function
);
instructions[0x35] = nes_instruction(
3, // time
MODE_ZEROPAGEX, // mode
'a', // pram 2
false, // page boundary slowdown
"AND", // opcode
bitwise_and // function
);
instructions[0x2D] = nes_instruction(
4, // time
MODE_ABSOLUTE, // mode
'a', // pram 2
false, // page boundary slowdown
"AND", // opcode
bitwise_and // function
);
instructions[0x3D] = nes_instruction(
4, // time
MODE_ABSOLUTEX, // mode
'a', // pram 2
true, // page boundary slowdown
"AND", // opcode
bitwise_and // function
);
instructions[0x39] = nes_instruction(
4, // time
MODE_ABSOLUTEY, // mode
'a', // pram 2
true, // page boundary slowdown
"AND", // opcode
bitwise_and // function
);
instructions[0x21] = nes_instruction(
6, // time
MODE_INDIRECTX, // mode
'a', // pram 2
false, // page boundary slowdown
"AND", // opcode
bitwise_and // function
);
instructions[0x31] = nes_instruction(
5, // time
MODE_INDIRECTY, // mode
'a', // pram 2
true, // page boundary slowdown
"AND", // opcode
bitwise_and // function
);
//ASL instructions
instructions[0x0A] = nes_instruction(
2, // time
MODE_NOTHING, // mode
'a', // pram 2
false, // page boundary slowdown
"ASL", // opcode
shift_left // function
);
instructions[0x06] = nes_instruction(
5, // time
MODE_ZEROPAGE, // mode
'd', // pram 2
false, // page boundary slowdown
"ASL", // opcode
shift_left // function
);
instructions[0x16] = nes_instruction(
6, // time
MODE_ZEROPAGEX, // mode
'd', // pram 2
false, // page boundary slowdown
"ASL", // opcode
shift_left // function
);
instructions[0x0E] = nes_instruction(
6, // time
MODE_ABSOLUTE, // mode
'd', // pram 2
false, // page boundary slowdown
"ASL", // opcode
shift_left // function
);
instructions[0x1E] = nes_instruction(
7, // time
MODE_ABSOLUTEX, // mode
'd', // pram 2
false, // page boundary slowdown
"ASL", // opcode
shift_left // function
);
//BIT instructions
instructions[0x24] = nes_instruction(
3, // time
MODE_ZEROPAGE, // mode
'a', // pram 2
false, // page boundary slowdown
"BIT", // opcode
test_bits // function
);
instructions[0x2C] = nes_instruction(
4, // time
MODE_ABSOLUTE, // mode
'a', // pram 2
false, // page boundary slowdown
"BIT", // opcode
test_bits // function
);
//Branch instructions (Bxx)
instructions[0x10] = nes_instruction(
2, // time
MODE_RELATIVE, // mode
'd', // pram 2
true, // page boundary slowdown
"BPL", // opcode
branch_not, // function
true // is a branch
);
instructions[0x30] = nes_instruction(
2, // time
MODE_RELATIVE, // mode
'd', // pram 2
true, // page boundary slowdown
"BMI", // opcode
branch, // function
true // is a branch
);
instructions[0x50] = nes_instruction(
2, // time
MODE_RELATIVE, // mode
'd', // pram 2
true, // page boundary slowdown
"BVC", // opcode
branch_not, // function
true // is a branch
);
instructions[0x70] = nes_instruction(
2, // time
MODE_RELATIVE, // mode
'd', // pram 2
true, // page boundary slowdown
"BVS", // opcode
branch, // function
true // is a branch
);
instructions[0x90] = nes_instruction(
2, // time
MODE_RELATIVE, // mode
'd', // pram 2
true, // page boundary slowdown
"BCC", // opcode
branch_not, // function
true // is a branch
);
instructions[0xB0] = nes_instruction(
2, // time
MODE_RELATIVE, // mode
'd', // pram 2
true, // page boundary slowdown
"BCS", // opcode
branch, // function
true // is a branch
);
instructions[0xD0] = nes_instruction(
2, // time
MODE_RELATIVE, // mode
'd', // pram 2
true, // page boundary slowdown
"BNE", // opcode
branch_not, // function
true // is a branch
);
instructions[0xF0] = nes_instruction(
2, // time
MODE_RELATIVE, // mode
'd', // pram 2
true, // page boundary slowdown
"BEQ", // opcode
branch, // function
true // is a branch
);
//BRK instruction
instructions[0x00] = nes_instruction(
7, // time
MODE_NOTHING, // mode
'a', // pram 2
false, // page boundary slowdown
"BRK", // opcode
NULL // function
);
//CMP instructions
instructions[0xC9] = nes_instruction(
2, // time
MODE_IMMEDIATE, // mode
'a', // pram 2
false, // page boundary slowdown
"CMP", // opcode
compare // function
);
instructions[0xC5] = nes_instruction(
3, // time
MODE_ZEROPAGE, // mode
'a', // pram 2
false, // page boundary slowdown
"CMP", // opcode
compare // function
);
instructions[0xD5] = nes_instruction(
4, // time
MODE_ZEROPAGEX, // mode
'a', // pram 2
false, // page boundary slowdown
"CMP", // opcode
compare // function
);
instructions[0xCD] = nes_instruction(
4, // time
MODE_ABSOLUTE, // mode
'a', // pram 2
false, // page boundary slowdown
"CMP", // opcode
compare // function
);
instructions[0xDD] = nes_instruction(
4, // time
MODE_ABSOLUTEX, // mode
'a', // pram 2
true, // page boundary slowdown
"CMP", // opcode
compare // function
);
instructions[0xD9] = nes_instruction(
4, // time
MODE_ABSOLUTEY, // mode
'a', // pram 2
true, // page boundary slowdown
"CMP", // opcode
compare // function
);
instructions[0xC1] = nes_instruction(
6, // time
MODE_INDIRECTX, // mode
'a', // pram 2
false, // page boundary slowdown
"CMP", // opcode
compare // function
);
instructions[0xD1] = nes_instruction(
5, // time
MODE_INDIRECTY, // mode
'a', // pram 2
true, // page boundary slowdown
"CMP", // opcode
compare // function
);
//CPX instructions
instructions[0xE0] = nes_instruction(
2, // time
MODE_IMMEDIATE, // mode
'x', // pram 2
false, // page boundary slowdown
"CPX", // opcode
compare // function
);
instructions[0xE4] = nes_instruction(
3, // time
MODE_ZEROPAGE, // mode
'x', // pram 2
false, // page boundary slowdown
"CPX", // opcode
compare // function
);
instructions[0xEC] = nes_instruction(
4, // time
MODE_ABSOLUTE, // mode
'x', // pram 2
false, // page boundary slowdown
"CPX", // opcode
compare // function
);
//CPY instructions
instructions[0xC0] = nes_instruction(
2, // time
MODE_IMMEDIATE, // mode
'y', // pram 2
false, // page boundary slowdown
"CPY", // opcode
compare // function
);
instructions[0xC4] = nes_instruction(
3, // time
MODE_ZEROPAGE, // mode
'y', // pram 2
false, // page boundary slowdown
"CPY", // opcode
compare // function
);
instructions[0xCC] = nes_instruction(
4, // time
MODE_ABSOLUTE, // mode
'y', // pram 2
false, // page boundary slowdown