ya6502 0.1.0

YA6502 — Yet Another 6502 CPU emulator
Documentation
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
#![cfg(test)]

extern crate test;

use super::*;
use crate::memory::SimpleRam;
use test::Bencher;

fn reset<M: Memory + Debug>(cpu: &mut Cpu<M>) {
    cpu.reset();
    cpu.ticks(8).unwrap();
}

fn cpu_with_program(program: &[u8]) -> Cpu<SimpleRam> {
    let memory = Box::new(SimpleRam::with_test_program(program));
    let mut cpu = Cpu::new(memory);
    reset(&mut cpu);
    return cpu;
}

macro_rules! cpu_with_code {
    ($($tokens:tt)*) => {
        cpu_with_program(&assemble6502!({
            start: 0xF000,
            code: {$($tokens)*}
        }))
    };
}

fn reversed_stack(cpu: &Cpu<SimpleRam>) -> Vec<u8> {
    cpu.memory.bytes[(cpu.stack_pointer() as usize + 1)..=0x1FF]
        .iter()
        .copied()
        .rev()
        .collect()
}

#[test]
fn it_resets() {
    // We test resetting the CPU by providing a memory image with two
    // separate programs. The first starts, as usually, at 0xF000, and it
    // will store a value of 1 at 0x0000.
    let mut program = vec![
        opcodes::LDX_IMM,
        1,
        opcodes::STX_ZP,
        0,
        opcodes::TXS,
        opcodes::PHP,
    ];
    // The next one will start exactly 0x101 bytes later, at 0xF101. This is
    // because we want to change both bytes of the program's address. We
    // resize the memory so that it contains zeros until 0xF101.
    program.resize(0x101, 0);
    // Finally, the second program. It stores 2 at 0x0000.
    program.extend_from_slice(&[opcodes::LDX_IMM, 2, opcodes::STX_ZP, 0]);

    let mut cpu = cpu_with_program(&program);
    reset(&mut cpu);
    cpu.ticks(10).unwrap();
    assert_eq!(cpu.memory.bytes[0], 1, "the first program wasn't executed");
    assert_eq!(
        cpu.memory.bytes[0x101] & (flags::UNUSED | flags::I),
        flags::UNUSED | flags::I,
        "I and UNUSED flags are not set by default"
    );

    cpu.memory.bytes[0xFFFC] = 0x01;
    cpu.memory.bytes[0xFFFD] = 0xF1;
    reset(&mut cpu);
    cpu.ticks(5).unwrap();
    assert_eq!(cpu.memory.bytes[0], 2, "the second program wasn't executed");
}

#[test]
fn nop() {
    let mut cpu = cpu_with_code! {
        lda #0xFF
        nop
        sta 1
    };
    cpu.ticks(4).unwrap();
    assert_eq!(cpu.memory.bytes[1], 0);
    cpu.ticks(3).unwrap();
    assert_eq!(cpu.memory.bytes[1], 0xFF);
}

#[test]
fn lda_sta() {
    let mut cpu = cpu_with_code! {
            lda #65
            sta 4
            lda #73
            sta 4
            lda #12
            sta 5
            // (15 cycles)

            lda 4
            clc
            cld
            adc #1
            sta 6
            // (12 cycles)

            ldx #2
        loop1:
            lda 4,x
            sta 7,x
            dex
            bpl loop1
            // (2 + 10 * 3 + 3 * 2 + 2 cycles)

            // Copy arguments of first three instructions from this program.
            lda abs 0xF001
            sta abs 0xABC0
            lda abs 0xF003
            sta abs 0xABC1
            lda abs 0xF005
            sta abs 0xABC2
            // (8 * 3 cycles)

            ldx #2
        loop2:
            lda abs 0xABC0,x
            sta abs 0xABC3,x
            dex
            bpl loop2
            // (2 + 11 * 3 + 3 * 2 + 2 cycles)

            ldy #2
        loop3:
            lda abs 0xABC0,y
            sta abs 0xABC6,y
            dey
            bpl loop3
            // (2 + 11 * 3 + 3 * 2 + 2 cycles)

            ldx #4
        loop4:
            lda (10,x)
            sta (20,x)
            dex
            dex
            bpl loop4
            // (2 + 16 * 3 + 3 * 2 + 2 cycles)

            ldy #2
        loop5:
            lda (12),y
            sta (26),y
            dey
            bpl loop5
            // (2 + 13 * 3 + 3 * 2 + 2 cycles)
    };
    // Prepare address vectors for the (X, indirect) addressing.
    cpu.mut_memory().bytes[10..=15].copy_from_slice(&[0xC1, 0xAB, 0xC2, 0xAB, 0xC3, 0xAB]);
    cpu.mut_memory().bytes[20..=27]
        .copy_from_slice(&[0xCB, 0xAB, 0xCA, 0xAB, 0xC9, 0xAB, 0xCC, 0xAB]);
    cpu.ticks(5).unwrap();
    assert_eq!(cpu.memory.bytes[4..6], [65, 0]);
    cpu.ticks(5).unwrap();
    assert_eq!(cpu.memory.bytes[4..6], [73, 0]);
    cpu.ticks(5).unwrap();
    assert_eq!(cpu.memory.bytes[4..6], [73, 12]);
    cpu.ticks(
        12 + (2 + 10 * 3 + 3 * 2 + 2)
            + (8 * 3)
            + 2 * (2 + 11 * 3 + 3 * 2 + 2)
            + (2 + 16 * 3 + 3 * 2 + 2)
            + (2 + 13 * 3 + 3 * 2 + 2),
    )
    .unwrap();
    assert_eq!(cpu.memory.bytes[4..=9], [73, 12, 74, 73, 12, 74]);
    assert_eq!(
        cpu.memory.bytes[0xABC0..=0xABCE],
        [65, 4, 73, 65, 4, 73, 65, 4, 73, 65, 73, 4, 73, 65, 4]
    );
}

#[test]
fn ldx_stx() {
    let mut cpu = cpu_with_code! {
            ldx #65
            stx 4
            ldx #73
            stx 4
            ldx #12
            stx 5
            ldx 4
            stx 6
            ldx abs 0xF002  // should load the STX opcode
            stx abs 0xABCD
    };
    cpu.ticks(5).unwrap();
    assert_eq!(cpu.memory.bytes[4..6], [65, 0]);
    cpu.ticks(5).unwrap();
    assert_eq!(cpu.memory.bytes[4..6], [73, 0]);
    cpu.ticks(5).unwrap();
    assert_eq!(cpu.memory.bytes[4..6], [73, 12]);
    cpu.ticks(14).unwrap();
    assert_eq!(cpu.memory.bytes[4..7], [73, 12, 73]);
    assert_eq!(cpu.memory.bytes[0xABCD], opcodes::STX_ZP);
}

#[test]
fn ldy_sty() {
    let mut cpu = cpu_with_code! {
            ldy #65
            sty 4
            ldy #73
            sty 4
            ldy #12
            sty 5
            ldy 4
            sty 6
            ldy abs 0xF002  // should load the STY opcode
            sty abs 0xABCD
    };
    cpu.ticks(5).unwrap();
    assert_eq!(cpu.memory.bytes[4..6], [65, 0]);
    cpu.ticks(5).unwrap();
    assert_eq!(cpu.memory.bytes[4..6], [73, 0]);
    cpu.ticks(5).unwrap();
    assert_eq!(cpu.memory.bytes[4..6], [73, 12]);
    cpu.ticks(14).unwrap();
    assert_eq!(cpu.memory.bytes[4..7], [73, 12, 73]);
    assert_eq!(cpu.memory.bytes[0xABCD], opcodes::STY_ZP);
}

#[test]
fn multiple_registers() {
    let mut cpu = cpu_with_code! {
            lda #10
            ldx #20
            sta 0
            stx 1
    };
    cpu.ticks(10).unwrap();
    assert_eq!(cpu.memory.bytes[0..2], [10, 20]);
}

#[test]
fn storing_addressing_mode_quirks() {
    let mut cpu = cpu_with_code! {
            ldx #5
            lda #42
            ldy #100
        loop:
            sta 0xFC,x
            sty 0x02,x
            dex
            bne loop
    };
    cpu.ticks(6 + 5 * 13).unwrap();
    assert_eq!(cpu.memory.bytes[0xFC..0x100], [0, 42, 42, 42]);
    assert_eq!(
        cpu.memory.bytes[0x00..0x09],
        [42, 42, 0, 100, 100, 100, 100, 100, 0]
    );
}

#[test]
fn loading_across_pages_timing() {
    let mut cpu = cpu_with_code! {
        lda #56
        sta abs 0x5714
        lda #0

        ldx #0x74
        lda abs 0x56A0,x
        sta 0x05

        ldy #0x73
        lda (10),y
        sta 0x06
    };
    cpu.mut_memory().bytes[10..=11].copy_from_slice(&[0xA1, 0x56]);
    cpu.ticks(8 + 9).unwrap();
    assert_eq!(cpu.memory.bytes[5..=6], [0, 0]);
    cpu.ticks(1).unwrap();
    assert_eq!(cpu.memory.bytes[5..=6], [56, 0]);
    cpu.ticks(10).unwrap();
    assert_eq!(cpu.memory.bytes[5..=6], [56, 0]);
    cpu.ticks(1).unwrap();
    assert_eq!(cpu.memory.bytes[5..=6], [56, 56]);
}

#[test]
fn cmp() {
    let mut program = assemble6502! ({
        start: 0xF000,
        code: {
                ldx #0xFE
                txs
                plp
                lda #7
                // 10 cycles

                cmp #6
                beq fail
                bcc fail
                bmi fail
                sta 30
                // 11 cycles

                cmp #7
                bne fail
                bcc fail
                bmi fail
                sta 31
                // 11 cycles

                cmp #8
                beq fail
                bcs fail
                bpl fail
                sta 32
                // 11 cycles

                cmp #(-7i8 as u8)
                beq fail
                bcs fail
                bmi fail
                sta 33
                // 11 cycles

                cmp 30
                php
                // 6 cycles

                ldx #5
                cmp 35,x
                php
                // 9 cycles

                nop  // to be replaced
            fail:
                jmp fail
        }
    });
    // Deliberately inject HLT1 instead of NOP to make sure we never reach that
    // place and test timing.
    program[program.len() - 4] = opcodes::HLT1;
    let mut cpu = cpu_with_program(&program);
    // Some test data.
    cpu.mut_memory().bytes[40..=40].copy_from_slice(&[8]);
    cpu.ticks(10 + 4 * 11 + 6 + 9).unwrap();
    assert_eq!(cpu.memory.bytes[30..=33], [7, 7, 7, 7]);
    assert_eq!(
        reversed_stack(&cpu),
        [
            flags::UNUSED | flags::Z | flags::C,
            flags::UNUSED | flags::N,
        ]
    );
}

#[test]
fn cpx_cpy() {
    let mut cpu = cpu_with_code! {
            ldx #0xFE
            txs
            plp

            cpx #6
            php

            ldy #10
            cpy #25
            php

            lda #10
            ldx #20
            sta 4
            cpx 4
            php

            cpy 4
            php
    };
    cpu.ticks(8 + 5 + 7 + 13 + 6).unwrap();
    assert_eq!(
        reversed_stack(&cpu),
        [
            flags::UNUSED | flags::N | flags::C,
            flags::UNUSED | flags::N,
            flags::UNUSED | flags::C,
            flags::UNUSED | flags::Z | flags::C,
        ]
    );
}

#[test]
fn bit() {
    let mut cpu = cpu_with_code! {
            ldx #0xFE
            txs
            plp
            lda #0b1000_0001
            sta 0x01
            lda #0b0100_0001
            sta 0x02
            lda #0b0011_1110
            sta 0x03
            lda #0b1111_1110
            sta abs 0x1234
            lda #0b0000_0001
            bit 0x01
            php
            bit 0x02
            php
            bit 0x03
            php
            bit abs 0x1234
            php
    };
    cpu.ticks(56).unwrap();
    assert_eq!(
        reversed_stack(&cpu),
        &[
            flags::UNUSED | flags::N,
            flags::UNUSED | flags::V,
            flags::UNUSED | flags::Z,
            flags::UNUSED | flags::N | flags::V | flags::Z,
        ]
    );
}

#[test]
fn adc_sbc() {
    let mut cpu = cpu_with_code! {
            ldx #0xFE
            txs
            plp
            lda #0x45

            adc #0x2A
            pha
            php

            adc #0x20
            pha
            php

            adc #0xAC
            pha
            php

            adc #0x01
            pha
            php

            sbc #0x45
            pha
            php

            sbc #0x7F
            pha
            php

            sbc #0xBF
            pha
            php
    };
    cpu.ticks(10 + 7 * 8).unwrap();

    assert_eq!(
        reversed_stack(&cpu),
        [
            0x6F,
            flags::UNUSED,
            0x8F,
            flags::UNUSED | flags::V | flags::N,
            0x3B,
            flags::UNUSED | flags::C | flags::V,
            0x3D,
            flags::UNUSED,
            0xF7,
            flags::UNUSED | flags::N,
            0x77,
            flags::UNUSED | flags::C | flags::V,
            0xB8,
            flags::UNUSED | flags::V | flags::N,
        ]
    );
}

#[test]
fn adc_sbc_decimal_mode() {
    let mut cpu = cpu_with_code! {
            ldx #0xFE
            txs
            plp
            sed
            lda #0x45

            adc #0x68
            pha
            php

            adc #0x16
            pha
            php

            sbc #0x25
            pha
            php

            sbc #0x56
            pha
            php
    };
    cpu.ticks(12 + 4 * 8).unwrap();

    assert_eq!(
        reversed_stack(&cpu),
        [
            0x13,
            flags::UNUSED | flags::D | flags::C,
            0x30,
            flags::UNUSED | flags::D,
            0x04,
            flags::UNUSED | flags::D | flags::C,
            0x48,
            flags::UNUSED | flags::D,
        ]
    );
}

#[test]
fn adc_sbc_addressing_modes() {
    let mut cpu = cpu_with_code! {
            ldx #0xFE
            txs
            plp
            ldx #15
            stx 5
            inx
            stx 6

            lda #20
            clc
            adc 5
            pha
            sec
            sbc 6
            pha
            ldx #2
            clc
            adc 3,x
            pha
            sec
            sbc 4,x
            pha

            clc
            adc abs 0x72C4
            pha
            sec
            sbc abs 0x72C5
            pha

            ldx #4
            clc
            adc abs 0x72C0,x
            pha
            sec
            sbc abs 0x72C1,x
            pha

            ldy #3
            clc
            adc abs 0x72C1,y
            pha
            sec
            sbc abs 0x72C2,y
            pha
    };
    cpu.mut_memory().bytes[0x72C4..=0x72C5].copy_from_slice(&[7, 6]);
    cpu.ticks(18 + 18 + 20 + 18 + 20 + 20).unwrap();
    assert_eq!(
        reversed_stack(&cpu),
        [35, 19, 34, 18, 25, 19, 26, 20, 27, 21]
    );
}

#[test]
fn overflow_flag() {
    let mut cpu = cpu_with_code! {
            ldx #0xFE
            txs
            plp
            // 8 cycles

            lda #0x40
            adc #0x40
            bvc fail
            adc #1
            bvs fail
            sbc #2
            bvc fail
            // 14 cycles

            php
            clv
            php
            // 8 cycles
        fail:
            jmp fail
    };
    cpu.ticks(8 + 14 + 8).unwrap();
    assert_eq!(
        reversed_stack(&cpu),
        [
            flags::UNUSED | flags::V | flags::C,
            flags::UNUSED | flags::C
        ]
    );
}

#[test]
fn and_ora() {
    let mut cpu = cpu_with_code! {
            ldx #0xFF
            txs
            lda #0b0000_1111
            and #0b1100_1100
            pha
            ora #0b1010_1010
            pha
            // 16 cycles

            and 44
            pha
            ora 45
            pha
            // 12 cycles

            ldx #2
            and 44,x
            pha
            inx
            ora 44,x
            pha
            // 18 cycles

            and abs 0x1234
            pha
            ora abs 0x1235
            pha
            // 14 cycles

            ldx #2
            and abs 0x1234,x
            inx
            pha
            ora abs 0x1234,x
            pha
            // 18 cycles

            ldy #3
            and abs 0x1235,y
            iny
            pha
            ora abs 0x1235,y
            pha
            // 18 cycles

            ldx #4
            and (44,x)
            pha
            ora (46,x)
            pha
            // 20 cycles

            ldy #8
            and (52),y
            pha
            iny
            ora (52),y
            pha
            // 20 cycles
    };
    cpu.mut_memory().bytes[44..=53].copy_from_slice(&[
        0b1111_0000,
        0b0101_0101,
        0b0100_0111,
        0b1100_0011,
        0x3A,
        0x12,
        0x3B,
        0x12,
        0x34,
        0x12,
    ]);
    cpu.mut_memory().bytes[0x1234..=0x123D].copy_from_slice(&[
        0b1010_1010,
        0b0011_1100,
        0b1111_0000,
        0b0101_0101,
        0b1100_1100,
        0b0000_1111,
        0b0110_0110,
        0b1001_1001,
        0b1010_1010,
        0b0011_0011,
    ]);

    cpu.ticks(16 + 12 + 18 + 14 + 18 + 18 + 20 + 20).unwrap();
    assert_eq!(
        reversed_stack(&cpu),
        [
            0b0000_1100,
            0b1010_1110,
            0b1010_0000,
            0b1111_0101,
            0b0100_0101,
            0b1100_0111,
            0b1000_0010,
            0b1011_1110,
            0b1011_0000,
            0b1111_0101,
            0b1100_0100,
            0b1100_1111,
            0b0100_0110,
            0b1101_1111,
            0b1000_1010,
            0b1011_1011,
        ]
    );
}

#[test]
fn eor() {
    let mut cpu = cpu_with_code! {
            ldx #0xFF
            txs
            lda #0b0000_1111
            eor #0b1100_1100
            pha
            // 11 cycles

            eor 20
            pha
            // 6 cycles
    };
    cpu.mut_memory().bytes[20..=20].copy_from_slice(&[0b1111_0000]);
    // cpu.mut_memory().bytes[0x1234..=0x123D].copy_from_slice(&[
    // ]);

    cpu.ticks(11 + 6).unwrap();
    assert_eq!(reversed_stack(&cpu), [0b1100_0011, 0b0011_0011,]);
}

#[test]
fn asl() {
    let mut cpu = cpu_with_code! {
            sec
            lda #0b0101_0000

            asl a
        stop1:
            bcs stop1
            sta 0x01

            asl 0x01
        stop2:
            bcc stop2
            sta 0x02

            ldx #1
            asl 0x01,x
        stop3:
            bcc stop3

            stx abs 0x0234
            asl abs 0x0234
    };
    cpu.ticks(4 + 7 + 10 + 10 + 10).unwrap();
    assert_eq!(cpu.memory.bytes[1..=2], [0b0100_0000, 0b0100_0000]);
    assert_eq!(cpu.memory.bytes[0x0234], 2);
}

#[test]
fn lsr() {
    let mut cpu = cpu_with_code! {
            sec
            lda #0b0000_1010

            lsr a
        stop1:
            bcs stop1
            sta 0x05

            lsr 0x05
        stop2:
            bcc stop2
            sta 0x06

            ldx #2
            lsr 0x04,x
        stop3:
            bcc stop3

            stx abs 0x0234
            lsr abs 0x0234
    };
    cpu.ticks(4 + 7 + 10 + 10 + 10).unwrap();
    assert_eq!(cpu.memory.bytes[5..=6], [0b0000_0010, 0b0000_0010]);
    assert_eq!(cpu.memory.bytes[0x0234], 1);
}

#[test]
fn rol() {
    let mut cpu = cpu_with_code! {
            clc
            lda #0b1010_0000

            rol a
        stop1:
            bcc stop1
            sta 0x01

            rol 0x01
        stop2:
            bcs stop2
            sta 0x02

            ldx #1
            rol 0x01,x
        stop3:
            bcs stop3

            stx abs 0x0234
            rol abs 0x0234
    };
    cpu.ticks(4 + 7 + 10 + 10 + 10).unwrap();
    assert_eq!(cpu.memory.bytes[1..=2], [0b1000_0001, 0b1000_0000]);
    assert_eq!(cpu.memory.bytes[0x0234], 2);
}

#[test]
fn ror() {
    let mut cpu = cpu_with_code! {
            clc
            lda #0b0000_0101

            ror a
        stop1:
            bcc stop1
            sta 0x05

            ror 0x05
        stop2:
            bcs stop2
            sta 0x06

            ldx #2
            ror 0x04,x
        stop3:
            bcs stop3

            stx abs 0x0234
            ror abs 0x0234
    };
    cpu.ticks(4 + 7 + 10 + 10 + 10).unwrap();
    assert_eq!(cpu.memory.bytes[5..=6], [0b1000_0001, 0b0000_0001]);
    assert_eq!(cpu.memory.bytes[0x0234], 1);
}

#[test]
fn inc_dec() {
    let mut cpu = cpu_with_code! {
            inc 10
            inc 10
            dec 11
            dec 11
            // 20 cycles

            ldx #1
            inc 11,x
            inx
            dec 11,x
            // 16 cycles
    };
    cpu.ticks(20 + 16).unwrap();
    assert_eq!(
        cpu.memory.bytes[10..=13],
        [2, -2 as i8 as u8, 1, -1 as i8 as u8]
    );
}

#[test]
fn inx_dex() {
    let mut cpu = cpu_with_code! {
            ldx #0xFE
            inx
            stx 5
            inx
            stx 6
            inx
            stx 7
            dex
            stx 8
            dex
            stx 9
    };
    cpu.ticks(27).unwrap();
    assert_eq!(cpu.memory.bytes[5..10], [0xFF, 0x00, 0x01, 0x00, 0xFF]);
}

#[test]
fn iny_dey() {
    let mut cpu = cpu_with_code! {
            ldy #0xFE
            iny
            sty 5
            iny
            sty 6
            iny
            sty 7
            dey
            sty 8
            dey
            sty 9
    };
    cpu.ticks(27).unwrap();
    assert_eq!(cpu.memory.bytes[5..10], [0xFF, 0x00, 0x01, 0x00, 0xFF]);
}

#[test]
fn tya() {
    let mut cpu = cpu_with_code! {
            ldy #15
            tya
            sta 1
    };
    cpu.ticks(7).unwrap();
    assert_eq!(cpu.memory.bytes[0x01], 15);
}

#[test]
fn tax() {
    let mut cpu = cpu_with_code! {
            lda #13
            tax
            stx 0x01
    };
    cpu.ticks(7).unwrap();
    assert_eq!(cpu.memory.bytes[0x01], 13);
}

#[test]
fn tay() {
    let mut cpu = cpu_with_code! {
            lda #76
            tay
            sty 0x01
    };
    cpu.ticks(7).unwrap();
    assert_eq!(cpu.memory.bytes[0x01], 76);
}

#[test]
fn txa() {
    let mut cpu = cpu_with_code! {
            ldx #43
            txa
            sta 0x01
    };
    cpu.ticks(7).unwrap();
    assert_eq!(cpu.memory.bytes[0x01], 43);
}

#[test]
fn flag_manipulation() {
    let mut cpu = cpu_with_code! {
            ldx #0xFE
            txs
            plp

            sei
            sec
            lda #0
            php

            ldx #0xFF
            php

            cli
            ldy #0x01
            php

            clc
            php
    };
    cpu.ticks(34).unwrap();
    assert_eq!(
        cpu.memory.bytes[0x1FC..0x200],
        [
            flags::UNUSED,
            flags::C | flags::UNUSED,
            flags::C | flags::I | flags::N | flags::UNUSED,
            flags::C | flags::I | flags::Z | flags::UNUSED,
        ]
    );
}

#[test]
fn bne() {
    let mut cpu = cpu_with_code! {
            ldx #5
            lda #5
        loop:
            sta 9,x
            dex
            bne loop
            stx 12
    };
    cpu.ticks(4 + 4 * 9 + 8 + 3).unwrap();
    assert_eq!(cpu.memory.bytes[9..16], [0, 5, 5, 0, 5, 5, 0]);
}

#[test]
fn branching_across_pages_adds_one_cpu_cycle() {
    let memory = Box::new(SimpleRam::with_test_program_at(
        0xF0FB,
        &[
            opcodes::LDA_IMM,
            10,
            opcodes::BNE,
            1,
            opcodes::HLT1,
            opcodes::STA_ZP,
            20,
        ],
    ));
    let mut cpu = Cpu::new(memory);
    reset(&mut cpu);
    cpu.ticks(8).unwrap();
    assert_ne!(cpu.memory.bytes[20], 10);
    cpu.ticks(1).unwrap();
    assert_eq!(cpu.memory.bytes[20], 10);
}

#[test]
fn jmp() {
    let mut cpu = cpu_with_code! {
            ldx #1
        loop:
            stx 9
            inx
            jmp loop
    };

    cpu.ticks(13).unwrap();
    assert_eq!(cpu.memory.bytes[9], 2);
    cpu.ticks(8).unwrap();
    assert_eq!(cpu.memory.bytes[9], 3);
}

#[test]
fn subroutines_and_stack() {
    let mut cpu = cpu_with_code! {
        // Main program. Call subroutine A to store 6 at 25. Then call
        // subroutine B to store 7 at 28 and 6 at 26. Finally, store the 10
        // loaded to A in the beginning at 30. Duration: 25 cycles.
            ldx #0xFF
            txs
            lda #10
            ldx #5
            jsr sub_a
            inx
            jsr sub_b
            sta 30
            nop  // to be replaced

        // Subroutine A: store 6 at 20+X. Duration: 19 cycles.
        sub_a:
            pha
            lda #6
            sta 20,x
            pla
            rts
            nop  // to be replaced

        // Subroutine B: store 6 at 20+X and 7 at 22+X. Duration: 25 cycles.
        sub_b:
            pha
            lda #7
            jsr sub_a
            sta 22,x
            pla
            rts
            nop  // to be replaced
    };
    cpu.mut_memory().bytes[0xF010] = opcodes::HLT1;
    cpu.mut_memory().bytes[0xF018] = opcodes::HLT1;
    cpu.mut_memory().bytes[0xF023] = opcodes::HLT1;

    cpu.ticks(25 + 19 + 25 + 19).unwrap();
    assert_eq!(cpu.memory.bytes[24..32], [0, 6, 6, 0, 7, 0, 10, 0]);
}

#[test]
fn stack_wrapping() {
    let mut cpu = cpu_with_code! {
            ldx #1
            txs

            txa
            pha
            tsx
            txa
            pha
            tsx
            txa
            pha
            tsx

            txa
            pla
            pla
            pla
            sta 5
    };
    cpu.ticks(4 + 3 * 7 + 17).unwrap();
    assert_eq!(cpu.memory.bytes[0x1FF], 0xFF);
    assert_eq!(cpu.memory.bytes[0x100..0x102], [0, 1]);
    assert_eq!(cpu.memory.bytes[5], 1);
}

#[test]
fn stack_wrapping_with_subroutines() {
    let mut cpu = cpu_with_code! {
            ldx #0x00
            txs
            jsr subroutine
            sta 20
            nop  // to be replaced
        subroutine:
            lda #34
            rts
    };
    cpu.mut_memory().bytes[0xF008] = opcodes::HLT1;
    cpu.ticks(21).unwrap();
    assert_eq!(cpu.memory.bytes[20], 34);
}

#[test]
fn pc_wrapping() {
    let mut memory = Box::new(SimpleRam::with_test_program_at(
        0xFFF9,
        &[
            opcodes::JMP_ABS,
            0xFE,
            0xFF,
            0, // reset vector, will be filled
            0, // reset vector, will be filled
            opcodes::LDA_IMM,
            10,
        ],
    ));
    memory.bytes[0..2].copy_from_slice(&[opcodes::STA_ZP, 20]);
    let mut cpu = Cpu::new(memory);
    reset(&mut cpu);
    cpu.ticks(8).unwrap();
    assert_eq!(cpu.memory.bytes[20], 10);
}

#[test]
fn pc_wrapping_during_branch() {
    let mut memory = Box::new(SimpleRam::with_test_program_at(
        0xFFF8,
        &[
            opcodes::LDA_IMM,
            10,
            // Jump by 4 bytes: 0xFFFC + 0x06 mod 0x10000 = 0x02
            opcodes::BNE,
            6,
            0, // reset vector, will be filled
            0, // reset vector, will be filled
        ],
    ));
    memory.bytes[2..4].copy_from_slice(&[opcodes::STA_ZP, 20]);
    let mut cpu = Cpu::new(memory);
    reset(&mut cpu);
    cpu.ticks(9).unwrap();
    assert_eq!(cpu.memory.bytes[20], 10);
}

#[bench]
fn benchmark(b: &mut Bencher) {
    let mut cpu = cpu_with_code! {
            clc
            cld
            ldx #1
            lda #42
        loop:
            sta 0,x
            adc #64
            asl 1
            lsr 2
            inx
            jmp loop
    };
    b.iter(|| {
        reset(&mut cpu);
        cpu.ticks(1000).unwrap();
    });
}