-
Notifications
You must be signed in to change notification settings - Fork 14
/
CPU_emulline.i
executable file
·1408 lines (1115 loc) · 28.6 KB
/
CPU_emulline.i
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
/*
Frodo, Commodore 64 emulator for the iPhone
Copyright (C) 1994-1997,2002 Christian Bauer
See gpl.txt for license information.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/*
* CPU_emulline.i - 6510/6502 emulation core (body of
* EmulateLine() function, the same for
* both 6510 and 6502)
*
* Frodo (C) 1994-1997,2002 Christian Bauer
*/
/*
* Addressing mode macros
*/
#if 1
#define read_u16_imm() *((uint16*)pc), pc+=2
#else
#define read_u16_imm() ((*(pc+1)) << 8) | *pc, pc+=2
#endif
// Read immediate operand
#define read_byte_imm() (*pc++)
// Read zeropage operand address
#define read_adr_zero() ((uint16)read_byte_imm())
// Read zeropage x-indexed operand address
#define read_adr_zero_x() ((read_byte_imm() + x) & 0xff)
// Read zeropage y-indexed operand address
#define read_adr_zero_y() ((read_byte_imm() + y) & 0xff)
// Read absolute operand address (uses adr!)
//#define read_adr_abs() (adr = ((*(pc+1)) << 8) | *pc, pc+=2, adr)
#define read_adr_abs() (adr = read_u16_imm(), adr)
// Read absolute x-indexed operand address
#define read_adr_abs_x() (read_adr_abs() + x)
// Read absolute y-indexed operand address
#define read_adr_abs_y() (read_adr_abs() + y)
// Read indexed indirect operand address
#define read_adr_ind_x() (read_zp_word(read_byte_imm() + x))
// Read indirect indexed operand address
#define read_adr_ind_y() (read_zp_word(read_byte_imm()) + y)
// Read zeropage operand
#define read_byte_zero() read_zp(read_adr_zero())
// Read zeropage x-indexed operand
#define read_byte_zero_x() read_zp(read_adr_zero_x())
// Read zeropage y-indexed operand
#define read_byte_zero_y() read_zp(read_adr_zero_y())
// Read absolute operand
//#define read_byte_abs() (adr = ((*(pc+1)) << 8) | *pc, pc+=2, read_byte(adr))
#define read_byte_abs() (adr = read_u16_imm(), read_byte(adr))
#if PRECISE_CPU_CYCLES
// Acount for cyles due to crossing page boundaries
#define page_plus(exp, reg) \
(adr = exp, page_cycles = (adr & 0xff) + reg >= 0x100, adr + reg)
// Read absolute x-indexed operand
//#define read_byte_abs_x() (adr = ((*(pc+1)) << 8) | *pc, pc+=2, read_byte(page_plus(adr, x)))
#define read_byte_abs_x() (adr = read_u16_imm(), read_byte(page_plus(adr, x)))
// Read absolute x-indexed operand
#define read_byte_abs_y() read_byte(page_plus(read_adr_abs(), y))
// Read indirect y-indexed operand
#define read_byte_ind_y() read_byte(page_plus(read_zp_word(read_byte_imm()), y))
#else
// Read absolute x-indexed operand
//#define read_byte_abs_x() (adr = ((*(pc+1)) << 8) | *pc, pc+=2, read_byte(adr + x))
#define read_byte_abs_x() (adr = read_u16_imm(), read_byte(adr + x))
// Read absolute x-indexed operand
//#define read_byte_abs_y() (adr = ((*(pc+1)) << 8) | *pc, pc+=2, read_byte(adr + y))
#define read_byte_abs_y() (adr = read_u16_imm(), read_byte(adr + y))
// Read indirect y-indexed operand
#define read_byte_ind_y() read_byte(read_adr_ind_y())
#endif
// Read indexed indirect operand
#define read_byte_ind_x() read_byte(read_adr_ind_x())
/*
* Set N and Z flags according to byte
*/
#define set_nz(x) (z_flag = n_flag = (x))
/*
* End of opcode, decrement cycles left
*/
#define ENDOP(cyc) last_cycles = cyc; break;
// Main opcode fetch/execute loop
#if PRECISE_CPU_CYCLES
if (cycles_left != 1)
cycles_left -= borrowed_cycles;
int page_cycles = 0;
for (;;) {
if (last_cycles) {
last_cycles += page_cycles;
page_cycles = 0;
#if PRECISE_CIA_CYCLES && !defined(IS_CPU_1541)
TheCIA1->EmulateLine(last_cycles);
TheCIA2->EmulateLine(last_cycles);
#endif
}
if ((cycles_left -= last_cycles) < 0) {
borrowed_cycles = -cycles_left;
break;
}
#else
while ((cycles_left -= last_cycles) >= 0) {
#endif
uint8 _opcode = read_byte_imm();
redo_trap:
switch (_opcode) {
// Load group
case 0xa9: // LDA #imm
set_nz(a = read_byte_imm());
ENDOP(2);
case 0xa5: // LDA zero
set_nz(a = read_byte_zero());
ENDOP(3);
case 0xb5: // LDA zero,X
set_nz(a = read_byte_zero_x());
ENDOP(4);
case 0xad: // LDA abs
set_nz(a = read_byte_abs());
ENDOP(4);
case 0xbd: // LDA abs,X
set_nz(a = read_byte_abs_x());
ENDOP(4);
case 0xb9: // LDA abs,Y
set_nz(a = read_byte_abs_y());
ENDOP(4);
case 0xa1: // LDA (ind,X)
set_nz(a = read_byte_ind_x());
ENDOP(6);
case 0xb1: // LDA (ind),Y
set_nz(a = read_byte_ind_y());
ENDOP(5);
case 0xa2: // LDX #imm
set_nz(x = read_byte_imm());
ENDOP(2);
case 0xa6: // LDX zero
set_nz(x = read_byte_zero());
ENDOP(3);
case 0xb6: // LDX zero,Y
set_nz(x = read_byte_zero_y());
ENDOP(4);
case 0xae: // LDX abs
set_nz(x = read_byte_abs());
ENDOP(4);
case 0xbe: // LDX abs,Y
set_nz(x = read_byte_abs_y());
ENDOP(4);
case 0xa0: // LDY #imm
set_nz(y = read_byte_imm());
ENDOP(2);
case 0xa4: // LDY zero
set_nz(y = read_byte_zero());
ENDOP(3);
case 0xb4: // LDY zero,X
set_nz(y = read_byte_zero_x());
ENDOP(4);
case 0xac: // LDY abs
set_nz(y = read_byte_abs());
ENDOP(4);
case 0xbc: // LDY abs,X
set_nz(y = read_byte_abs_x());
ENDOP(4);
// Store group
case 0x85: // STA zero
write_zp(read_adr_zero(), a);
ENDOP(3);
case 0x95: // STA zero,X
write_zp(read_adr_zero_x(), a);
ENDOP(4);
case 0x8d: // STA abs
write_byte(read_adr_abs(), a);
ENDOP(4);
case 0x9d: // STA abs,X
write_byte(read_adr_abs_x(), a);
ENDOP(5);
case 0x99: // STA abs,Y
write_byte(read_adr_abs_y(), a);
ENDOP(5);
case 0x81: // STA (ind,X)
write_byte(read_adr_ind_x(), a);
ENDOP(6);
case 0x91: // STA (ind),Y
write_byte(read_adr_ind_y(), a);
ENDOP(6);
case 0x86: // STX zero
write_zp(read_adr_zero(), x);
ENDOP(3);
case 0x96: // STX zero,Y
write_zp(read_adr_zero_y(), x);
ENDOP(4);
case 0x8e: // STX abs
write_byte(read_adr_abs(), x);
ENDOP(4);
case 0x84: // STY zero
write_zp(read_adr_zero(), y);
ENDOP(3);
case 0x94: // STY zero,X
write_zp(read_adr_zero_x(), y);
ENDOP(4);
case 0x8c: // STY abs
write_byte(read_adr_abs(), y);
ENDOP(4);
// Transfer group
case 0xaa: // TAX
set_nz(x = a);
ENDOP(2);
case 0x8a: // TXA
set_nz(a = x);
ENDOP(2);
case 0xa8: // TAY
set_nz(y = a);
ENDOP(2);
case 0x98: // TYA
set_nz(a = y);
ENDOP(2);
case 0xba: // TSX
set_nz(x = sp);
ENDOP(2);
case 0x9a: // TXS
sp = x;
ENDOP(2);
// Arithmetic group
#define DoAdc(x) \
tmp = (x); \
if (!d_flag) { \
uint16 tmp16 = a + tmp + (c_flag ? 1 : 0); \
c_flag = tmp16 > 0xff; \
v_flag = !((a ^ tmp) & 0x80) && ((a ^ tmp16) & 0x80); \
z_flag = n_flag = a = tmp16; \
} else { do_adc_bcd(tmp); }
#define DoSbc(x) \
tmp = (x); \
if (!d_flag) { \
uint16 tmp2 = a - tmp - (c_flag ? 0 : 1); \
c_flag = tmp2 < 0x100; \
v_flag = ((a ^ tmp2) & 0x80) && ((a ^ tmp) & 0x80); \
z_flag = n_flag = a = tmp2; \
} else { do_sbc_bcd(tmp); }
case 0x69: // ADC #imm
DoAdc(read_byte_imm());
ENDOP(2);
case 0x65: // ADC zero
DoAdc(read_byte_zero());
ENDOP(3);
case 0x75: // ADC zero,X
DoAdc(read_byte_zero_x());
ENDOP(4);
case 0x6d: // ADC abs
DoAdc(read_byte_abs());
ENDOP(4);
case 0x7d: // ADC abs,X
DoAdc(read_byte_abs_x());
ENDOP(4);
case 0x79: // ADC abs,Y
DoAdc(read_byte_abs_y());
ENDOP(4);
case 0x61: // ADC (ind,X)
DoAdc(read_byte_ind_x());
ENDOP(6);
case 0x71: // ADC (ind),Y
DoAdc(read_byte_ind_y());
ENDOP(5);
case 0xe9: // SBC #imm
case 0xeb: // Undocumented opcode
DoSbc(read_byte_imm());
ENDOP(2);
case 0xe5: // SBC zero
DoSbc(read_byte_zero());
ENDOP(3);
case 0xf5: // SBC zero,X
DoSbc(read_byte_zero_x());
ENDOP(4);
case 0xed: // SBC abs
DoSbc(read_byte_abs());
ENDOP(4);
case 0xfd: // SBC abs,X
DoSbc(read_byte_abs_x());
ENDOP(4);
case 0xf9: // SBC abs,Y
DoSbc(read_byte_abs_y());
ENDOP(4);
case 0xe1: // SBC (ind,X)
DoSbc(read_byte_ind_x());
ENDOP(6);
case 0xf1: // SBC (ind),Y
DoSbc(read_byte_ind_y());
ENDOP(5);
// Increment/decrement group
case 0xe8: // INX
set_nz(++x);
ENDOP(2);
case 0xca: // DEX
set_nz(--x);
ENDOP(2);
case 0xc8: // INY
set_nz(++y);
ENDOP(2);
case 0x88: // DEY
set_nz(--y);
ENDOP(2);
case 0xe6: // INC zero
adr = read_adr_zero();
write_zp(adr, set_nz(read_zp(adr) + 1));
ENDOP(5);
case 0xf6: // INC zero,X
adr = read_adr_zero_x();
write_zp(adr, set_nz(read_zp(adr) + 1));
ENDOP(6);
case 0xee: // INC abs
adr = read_adr_abs();
write_byte(adr, set_nz(read_byte(adr) + 1));
ENDOP(6);
case 0xfe: // INC abs,X
adr = read_adr_abs_x();
write_byte(adr, set_nz(read_byte(adr) + 1));
ENDOP(7);
case 0xc6: // DEC zero
adr = read_adr_zero();
write_zp(adr, set_nz(read_zp(adr) - 1));
ENDOP(5);
case 0xd6: // DEC zero,X
adr = read_adr_zero_x();
write_zp(adr, set_nz(read_zp(adr) - 1));
ENDOP(6);
case 0xce: // DEC abs
adr = read_adr_abs();
write_byte(adr, set_nz(read_byte(adr) - 1));
ENDOP(6);
case 0xde: // DEC abs,X
adr = read_adr_abs_x();
write_byte(adr, set_nz(read_byte(adr) - 1));
ENDOP(7);
// Logic group
case 0x29: // AND #imm
set_nz(a &= read_byte_imm());
ENDOP(2);
case 0x25: // AND zero
set_nz(a &= read_byte_zero());
ENDOP(3);
case 0x35: // AND zero,X
set_nz(a &= read_byte_zero_x());
ENDOP(4);
case 0x2d: // AND abs
set_nz(a &= read_byte_abs());
ENDOP(4);
case 0x3d: // AND abs,X
set_nz(a &= read_byte_abs_x());
ENDOP(4);
case 0x39: // AND abs,Y
set_nz(a &= read_byte_abs_y());
ENDOP(4);
case 0x21: // AND (ind,X)
set_nz(a &= read_byte_ind_x());
ENDOP(6);
case 0x31: // AND (ind),Y
set_nz(a &= read_byte_ind_y());
ENDOP(5);
case 0x09: // ORA #imm
set_nz(a |= read_byte_imm());
ENDOP(2);
case 0x05: // ORA zero
set_nz(a |= read_byte_zero());
ENDOP(3);
case 0x15: // ORA zero,X
set_nz(a |= read_byte_zero_x());
ENDOP(4);
case 0x0d: // ORA abs
set_nz(a |= read_byte_abs());
ENDOP(4);
case 0x1d: // ORA abs,X
set_nz(a |= read_byte_abs_x());
ENDOP(4);
case 0x19: // ORA abs,Y
set_nz(a |= read_byte_abs_y());
ENDOP(4);
case 0x01: // ORA (ind,X)
set_nz(a |= read_byte_ind_x());
ENDOP(6);
case 0x11: // ORA (ind),Y
set_nz(a |= read_byte_ind_y());
ENDOP(5);
case 0x49: // EOR #imm
set_nz(a ^= read_byte_imm());
ENDOP(2);
case 0x45: // EOR zero
set_nz(a ^= read_byte_zero());
ENDOP(3);
case 0x55: // EOR zero,X
set_nz(a ^= read_byte_zero_x());
ENDOP(4);
case 0x4d: // EOR abs
set_nz(a ^= read_byte_abs());
ENDOP(4);
case 0x5d: // EOR abs,X
set_nz(a ^= read_byte_abs_x());
ENDOP(4);
case 0x59: // EOR abs,Y
set_nz(a ^= read_byte_abs_y());
ENDOP(4);
case 0x41: // EOR (ind,X)
set_nz(a ^= read_byte_ind_x());
ENDOP(6);
case 0x51: // EOR (ind),Y
set_nz(a ^= read_byte_ind_y());
ENDOP(5);
// Compare group
case 0xc9: // CMP #imm
set_nz(adr = a - read_byte_imm());
c_flag = adr < 0x100;
ENDOP(2);
case 0xc5: // CMP zero
set_nz(adr = a - read_byte_zero());
c_flag = adr < 0x100;
ENDOP(3);
case 0xd5: // CMP zero,X
set_nz(adr = a - read_byte_zero_x());
c_flag = adr < 0x100;
ENDOP(4);
case 0xcd: // CMP abs
set_nz(adr = a - read_byte_abs());
c_flag = adr < 0x100;
ENDOP(4);
case 0xdd: // CMP abs,X
set_nz(adr = a - read_byte_abs_x());
c_flag = adr < 0x100;
ENDOP(4);
case 0xd9: // CMP abs,Y
set_nz(adr = a - read_byte_abs_y());
c_flag = adr < 0x100;
ENDOP(4);
case 0xc1: // CMP (ind,X)
set_nz(adr = a - read_byte_ind_x());
c_flag = adr < 0x100;
ENDOP(6);
case 0xd1: // CMP (ind),Y
set_nz(adr = a - read_byte_ind_y());
c_flag = adr < 0x100;
ENDOP(5);
case 0xe0: // CPX #imm
set_nz(adr = x - read_byte_imm());
c_flag = adr < 0x100;
ENDOP(2);
case 0xe4: // CPX zero
set_nz(adr = x - read_byte_zero());
c_flag = adr < 0x100;
ENDOP(3);
case 0xec: // CPX abs
set_nz(adr = x - read_byte_abs());
c_flag = adr < 0x100;
ENDOP(4);
case 0xc0: // CPY #imm
set_nz(adr = y - read_byte_imm());
c_flag = adr < 0x100;
ENDOP(2);
case 0xc4: // CPY zero
set_nz(adr = y - read_byte_zero());
c_flag = adr < 0x100;
ENDOP(3);
case 0xcc: // CPY abs
set_nz(adr = y - read_byte_abs());
c_flag = adr < 0x100;
ENDOP(4);
// Bit-test group
case 0x24: // BIT zero
z_flag = a & (tmp = read_byte_zero());
n_flag = tmp;
v_flag = tmp & 0x40;
ENDOP(3);
case 0x2c: // BIT abs
z_flag = a & (tmp = read_byte_abs());
n_flag = tmp;
v_flag = tmp & 0x40;
ENDOP(4);
// Shift/rotate group
case 0x0a: // ASL A
c_flag = a & 0x80;
set_nz(a <<= 1);
ENDOP(2);
case 0x06: // ASL zero
tmp = read_zp(adr = read_adr_zero());
c_flag = tmp & 0x80;
write_zp(adr, set_nz(tmp << 1));
ENDOP(5);
case 0x16: // ASL zero,X
tmp = read_zp(adr = read_adr_zero_x());
c_flag = tmp & 0x80;
write_zp(adr, set_nz(tmp << 1));
ENDOP(6);
case 0x0e: // ASL abs
tmp = read_byte(adr = read_adr_abs());
c_flag = tmp & 0x80;
write_byte(adr, set_nz(tmp << 1));
ENDOP(6);
case 0x1e: // ASL abs,X
tmp = read_byte(adr = read_adr_abs_x());
c_flag = tmp & 0x80;
write_byte(adr, set_nz(tmp << 1));
ENDOP(7);
case 0x4a: // LSR A
c_flag = a & 0x01;
set_nz(a >>= 1);
ENDOP(2);
case 0x46: // LSR zero
tmp = read_zp(adr = read_adr_zero());
c_flag = tmp & 0x01;
write_zp(adr, set_nz(tmp >> 1));
ENDOP(5);
case 0x56: // LSR zero,X
tmp = read_zp(adr = read_adr_zero_x());
c_flag = tmp & 0x01;
write_zp(adr, set_nz(tmp >> 1));
ENDOP(6);
case 0x4e: // LSR abs
tmp = read_byte(adr = read_adr_abs());
c_flag = tmp & 0x01;
write_byte(adr, set_nz(tmp >> 1));
ENDOP(6);
case 0x5e: // LSR abs,X
tmp = read_byte(adr = read_adr_abs_x());
c_flag = tmp & 0x01;
write_byte(adr, set_nz(tmp >> 1));
ENDOP(7);
case 0x2a: // ROL A
tmp = a & 0x80;
set_nz(a = c_flag ? (a << 1) | 0x01 : a << 1);
c_flag = tmp;
ENDOP(2);
case 0x26: // ROL zero
tmp = read_zp(adr = read_adr_zero());
write_zp(adr, set_nz(c_flag ? (tmp << 1) | 0x01 : tmp << 1));
c_flag = tmp & 0x80;
ENDOP(5);
case 0x36: // ROL zero,X
tmp = read_zp(adr = read_adr_zero_x());
write_zp(adr, set_nz(c_flag ? (tmp << 1) | 0x01 : tmp << 1));
c_flag = tmp & 0x80;
ENDOP(6);
case 0x2e: // ROL abs
tmp = read_byte(adr = read_adr_abs());
write_byte(adr, set_nz(c_flag ? (tmp << 1) | 0x01 : tmp << 1));
c_flag = tmp & 0x80;
ENDOP(6);
case 0x3e: // ROL abs,X
tmp = read_byte(adr = read_adr_abs_x());
write_byte(adr, set_nz(c_flag ? (tmp << 1) | 0x01 : tmp << 1));
c_flag = tmp & 0x80;
ENDOP(7);
case 0x6a: // ROR A
tmp = a & 0x01;
set_nz(a = (c_flag ? (a >> 1) | 0x80 : a >> 1));
c_flag = tmp;
ENDOP(2);
case 0x66: // ROR zero
tmp = read_zp(adr = read_adr_zero());
write_zp(adr, set_nz(c_flag ? (tmp >> 1) | 0x80 : tmp >> 1));
c_flag = tmp & 0x01;
ENDOP(5);
case 0x76: // ROR zero,X
tmp = read_zp(adr = read_adr_zero_x());
write_zp(adr, set_nz(c_flag ? (tmp >> 1) | 0x80 : tmp >> 1));
c_flag = tmp & 0x01;
ENDOP(6);
case 0x6e: // ROR abs
tmp = read_byte(adr = read_adr_abs());
write_byte(adr, set_nz(c_flag ? (tmp >> 1) | 0x80 : tmp >> 1));
c_flag = tmp & 0x01;
ENDOP(6);
case 0x7e: // ROR abs,X
tmp = read_byte(adr = read_adr_abs_x());
write_byte(adr, set_nz(c_flag ? (tmp >> 1) | 0x80 : tmp >> 1));
c_flag = tmp & 0x01;
ENDOP(7);
// Stack group
case 0x48: // PHA
push_byte(a);
ENDOP(3);
case 0x68: // PLA
set_nz(a = pop_byte());
ENDOP(4);
case 0x08: // PHP
push_flags(true);
ENDOP(3);
case 0x28: // PLP
pop_flags();
if (interrupt.intr_any && !i_flag)
goto handle_int;
ENDOP(4);
// Jump/branch group
case 0x4c: // JMP abs
jump(read_adr_abs());
ENDOP(3);
case 0x6c: // JMP (ind)
adr = read_adr_abs();
jump(read_byte(adr) | (read_byte((adr + 1) & 0xff | adr & 0xff00) << 8));
ENDOP(5);
case 0x20: // JSR abs
push_byte((pc-pc_base+1) >> 8); push_byte(pc-pc_base+1);
jump(read_adr_abs());
ENDOP(6);
case 0x60: // RTS
adr = pop_byte(); // Split because of pop_byte ++sp side-effect
jump((adr | pop_byte() << 8) + 1);
ENDOP(6);
case 0x40: // RTI
pop_flags();
adr = pop_byte(); // Split because of pop_byte ++sp side-effect
jump(adr | pop_byte() << 8);
if (interrupt.intr_any && !i_flag)
goto handle_int;
ENDOP(6);
case 0x00: // BRK
#ifndef IS_CPU_1541
{
trap_result2_t *res = trap();
switch (res->result) {
case TRAP_REDO:
_opcode = res->trap->org[0];
goto redo_trap;
case TRAP_REPLACE_AND_REDO:
_opcode = res->trap->org[0];
*(pc - 1) = _opcode;
goto redo_trap;
case TRAP_DO_BREAK:
// regular BRK code
push_byte((pc+1-pc_base) >> 8); push_byte(pc+1-pc_base);
push_flags(true);
i_flag = true;
jump(read_word(0xfffe));
ENDOP(7);
}
}
break;
#else
// regular BRK code
push_byte((pc+1-pc_base) >> 8); push_byte(pc+1-pc_base);
push_flags(true);
i_flag = true;
jump(read_word(0xfffe));
ENDOP(7);
#endif
#if PRECISE_CPU_CYCLES
#define Branch(flag) \
if (flag) { \
uint8 *old_pc = pc; \
pc += (int8)*pc + 1; \
if (((pc-pc_base) ^ (old_pc - pc_base)) & 0xff00) { \
ENDOP(4); \
} else { \
ENDOP(3); \
} \
} else { \
pc++; \
ENDOP(2); \
}
#else
#define Branch(flag) \
if (flag) { \
pc += (int8)*pc + 1; \
ENDOP(3); \
} else { \
pc++; \
ENDOP(2); \
}
#endif
case 0xb0: // BCS rel
Branch(c_flag);
case 0x90: // BCC rel
Branch(!c_flag);
case 0xf0: // BEQ rel
Branch(!z_flag);
case 0xd0: // BNE rel
Branch(z_flag);
case 0x70: // BVS rel
#ifndef IS_CPU_1541
Branch(v_flag);
#else
Branch((via2_pcr & 0x0e) == 0x0e ? 1 : v_flag); // GCR byte ready flag
#endif
case 0x50: // BVC rel
#ifndef IS_CPU_1541
Branch(!v_flag);
#else
Branch(!((via2_pcr & 0x0e) == 0x0e) ? 0 : v_flag); // GCR byte ready flag
#endif
case 0x30: // BMI rel
Branch(n_flag & 0x80);
case 0x10: // BPL rel
Branch(!(n_flag & 0x80));
// Flags group
case 0x38: // SEC
c_flag = true;
ENDOP(2);
case 0x18: // CLC
c_flag = false;
ENDOP(2);
case 0xf8: // SED
d_flag = true;
ENDOP(2);
case 0xd8: // CLD
d_flag = false;
ENDOP(2);
case 0x78: // SEI
i_flag = true;
ENDOP(2);
case 0x58: // CLI
i_flag = false;
if (interrupt.intr_any)
goto handle_int;
ENDOP(2);
case 0xb8: // CLV
v_flag = false;
ENDOP(2);
// NOP group
case 0xea: // NOP
ENDOP(2);
/*
* Undocumented opcodes start here
*/
// NOP group
case 0x1a: // NOP
case 0x3a:
case 0x5a:
case 0x7a:
case 0xda:
case 0xfa:
ENDOP(2);
case 0x80: // NOP #imm
case 0x82:
case 0x89:
case 0xc2:
case 0xe2:
pc++;
ENDOP(2);
case 0x04: // NOP zero
case 0x44:
case 0x64:
pc++;
ENDOP(3);
case 0x14: // NOP zero,X
case 0x34:
case 0x54:
case 0x74:
case 0xd4:
case 0xf4:
pc++;
ENDOP(4);
case 0x0c: // NOP abs
pc+=2;
ENDOP(4);
case 0x1c: // NOP abs,X
case 0x3c:
case 0x5c:
case 0x7c:
case 0xdc:
case 0xfc:
#if PRECISE_CPU_CYCLES
read_byte_abs_x();
#else
pc+=2;
#endif
ENDOP(4);
// Load A/X group
case 0xa7: // LAX zero
set_nz(a = x = read_byte_zero());
ENDOP(3);
case 0xb7: // LAX zero,Y
set_nz(a = x = read_byte_zero_y());
ENDOP(4);
case 0xaf: // LAX abs
set_nz(a = x = read_byte_abs());
ENDOP(4);
case 0xbf: // LAX abs,Y
set_nz(a = x = read_byte_abs_y());
ENDOP(4);