-
Notifications
You must be signed in to change notification settings - Fork 4
/
emu6502.cs
1163 lines (1021 loc) · 42.6 KB
/
emu6502.cs
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
// emu6502.cs - class Emu6502 - MOS6502 Emulator
//
////////////////////////////////////////////////////////////////////////////////
//
// simple-emu-c64
// C64/6502 Emulator for Microsoft Windows Console
//
// MIT License
//
// Copyright (c) 2020-2023 by David R. Van Wagner
// davevw.com
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//
////////////////////////////////////////////////////////////////////////////////
using System;
using System.Text;
using System.Collections.Generic;
namespace simple_emu_c64
{
public class Emu6502
{
public interface Memory
{
byte this[ushort index]
{
get;
set;
}
}
protected Memory memory;
public HashSet<int> Breakpoints = new HashSet<int>();
protected byte A = 0;
protected byte X = 0;
protected byte Y = 0;
protected byte S = 0xFF;
protected bool N = false;
protected bool V = false;
protected bool B = false;
protected bool D = false;
protected bool I = false;
protected bool Z = false;
protected bool C = false;
protected ushort PC = 0;
public bool trace = false;
protected bool step = false;
protected bool exit = false;
public Emu6502(Memory memory)
{
this.memory = memory;
}
public void ResetRun()
{
ushort addr = (ushort)((memory[0xFFFC] | (memory[0xFFFD] << 8))); // JMP(RESET)
Execute(addr);
}
public virtual void Walk()
{
Walk6502.Reset();
ushort addr = (ushort)((memory[0xFFFC] | (memory[0xFFFD] << 8))); // RESET vector
Walk6502.Walk(this, addr);
}
protected virtual bool ExecutePatch()
{
return false;
}
void Execute(ushort addr)
{
bool conditional;
byte bytes;
PC = addr;
while (true)
{
while (true)
{
if (exit)
return;
bytes = 1;
bool breakpoint = false;
if (Breakpoints.Contains(PC))
breakpoint = true;
if (trace || breakpoint || step)
{
ushort addr2;
string line;
string dis = Disassemble(PC, out conditional, out bytes, out addr2, out line);
string state = GetDisplayState();
System.Diagnostics.Debug.WriteLine(string.Format("{0}{1}", line.PadRight(30), state));
//Console.Error.WriteLine(string.Format("{0}{1}", line.PadRight(30), state));
if (step)
step = step; // user can put debug breakpoint here to allow stepping
if (breakpoint)
breakpoint = breakpoint; // user can put debug breakpoint here to allow break
}
if (!ExecutePatch()) // allow execute to be overriden at a specific address
break;
}
switch (memory[PC])
{
case 0x00: BRK(out bytes); break;
case 0x01: ORA(GetIndX(PC, out bytes)); break;
case 0x05: ORA(GetZP(PC, out bytes)); break;
case 0x06: SetZP(ASL(GetZP(PC, out bytes)), PC, out bytes); break;
case 0x08: PHP(); break;
case 0x09: ORA(GetIM(PC, out bytes)); break;
case 0x0A: SetA(ASL(A)); break;
case 0x0D: ORA(GetABS(PC, out bytes)); break;
case 0x0E: SetABS(ASL(GetABS(PC, out bytes)), PC, out bytes); break;
case 0x10: BPL(ref PC, out conditional, out bytes); break;
case 0x11: ORA(GetIndY(PC, out bytes)); break;
case 0x15: ORA(GetZPX(PC, out bytes)); break;
case 0x16: SetZPX(ASL(GetZPX(PC, out bytes)), PC, out bytes); break;
case 0x18: CLC(); break;
case 0x19: ORA(GetABSY(PC, out bytes)); break;
case 0x1D: ORA(GetABSX(PC, out bytes)); break;
case 0x1E: SetABSX(ASL(GetABSX(PC, out bytes)), PC, out bytes); break;
case 0x20: JSR(ref PC, out bytes); break;
case 0x21: AND(GetIndX(PC, out bytes)); break;
case 0x24: BIT(GetZP(PC, out bytes)); break;
case 0x25: AND(GetZP(PC, out bytes)); break;
case 0x26: SetZP(ROL(GetZP(PC, out bytes)), PC, out bytes); break;
case 0x28: PLP(); break;
case 0x29: AND(GetIM(PC, out bytes)); break;
case 0x2A: SetA(ROL(A)); break;
case 0x2C: BIT(GetABS(PC, out bytes)); break;
case 0x2D: AND(GetABS(PC, out bytes)); break;
case 0x2E: SetABS(ROL(GetABS(PC, out bytes)), PC, out bytes); break;
case 0x30: BMI(ref PC, out conditional, out bytes); break;
case 0x31: AND(GetIndY(PC, out bytes)); break;
case 0x35: AND(GetZPX(PC, out bytes)); break;
case 0x36: SetZPX(ROL(GetZPX(PC, out bytes)), PC, out bytes); break;
case 0x38: SEC(); break;
case 0x39: AND(GetABSY(PC, out bytes)); break;
case 0x3D: AND(GetABSX(PC, out bytes)); break;
case 0x3E: SetABSX(ROL(GetABSX(PC, out bytes)), PC, out bytes); break;
case 0x40: RTI(ref PC, out bytes); break;
case 0x41: EOR(GetIndX(PC, out bytes)); break;
case 0x45: EOR(GetZP(PC, out bytes)); break;
case 0x46: SetZP(LSR(GetZP(PC, out bytes)), PC, out bytes); break;
case 0x48: PHA(); break;
case 0x49: EOR(GetIM(PC, out bytes)); break;
case 0x4A: SetA(LSR(A)); break;
case 0x4C: JMP(ref PC, out bytes); break;
case 0x4D: EOR(GetABS(PC, out bytes)); break;
case 0x4E: SetABS(LSR(GetABS(PC, out bytes)), PC, out bytes); break;
case 0x50: BVC(ref PC, out conditional, out bytes); break;
case 0x51: EOR(GetIndY(PC, out bytes)); break;
case 0x55: EOR(GetZPX(PC, out bytes)); break;
case 0x56: SetZPX(LSR(GetZPX(PC, out bytes)), PC, out bytes); break;
case 0x58: CLI(); break;
case 0x59: EOR(GetABSY(PC, out bytes)); break;
case 0x5D: EOR(GetABSX(PC, out bytes)); break;
case 0x5E: SetABSX(LSR(GetABSX(PC, out bytes)), PC, out bytes); break;
case 0x60: RTS(ref PC, out bytes); break;
case 0x61: ADC(GetIndX(PC, out bytes)); break;
case 0x65: ADC(GetZP(PC, out bytes)); break;
case 0x66: SetZP(ROR(GetZP(PC, out bytes)), PC, out bytes); break;
case 0x68: PLA(); break;
case 0x69: ADC(GetIM(PC, out bytes)); break;
case 0x6A: SetA(ROR(A)); break;
case 0x6C: JMPIND(ref PC, out bytes); break;
case 0x6D: ADC(GetABS(PC, out bytes)); break;
case 0x6E: SetABS(ROR(GetABS(PC, out bytes)), PC, out bytes); break;
case 0x70: BVS(ref PC, out conditional, out bytes); break;
case 0x71: ADC(GetIndY(PC, out bytes)); break;
case 0x75: ADC(GetZPX(PC, out bytes)); break;
case 0x76: SetZPX(ROR(GetZPX(PC, out bytes)), PC, out bytes); break;
case 0x78: SEI(); break;
case 0x79: ADC(GetABSY(PC, out bytes)); break;
case 0x7D: ADC(GetABSX(PC, out bytes)); break;
case 0x7E: SetABSX(ROR(GetABSX(PC, out bytes)), PC, out bytes); break;
case 0x81: SetIndX(A, PC, out bytes); break;
case 0x84: SetZP(Y, PC, out bytes); break;
case 0x85: SetZP(A, PC, out bytes); break;
case 0x86: SetZP(X, PC, out bytes); break;
case 0x88: DEY(); break;
case 0x8A: TXA(); break;
case 0x8C: SetABS(Y, PC, out bytes); break;
case 0x8D: SetABS(A, PC, out bytes); break;
case 0x8E: SetABS(X, PC, out bytes); break;
case 0x90: BCC(ref PC, out conditional, out bytes); break;
case 0x91: SetIndY(A, PC, out bytes); break;
case 0x94: SetZPX(Y, PC, out bytes); break;
case 0x95: SetZPX(A, PC, out bytes); break;
case 0x96: SetZPY(X, PC, out bytes); break;
case 0x98: TYA(); break;
case 0x99: SetABSY(A, PC, out bytes); break;
case 0x9A: TXS(); break;
case 0x9D: SetABSX(A, PC, out bytes); break;
case 0xA0: SetY(GetIM(PC, out bytes)); break;
case 0xA1: SetA(GetIndX(PC, out bytes)); break;
case 0xA2: SetX(GetIM(PC, out bytes)); break;
case 0xA4: SetY(GetZP(PC, out bytes)); break;
case 0xA5: SetA(GetZP(PC, out bytes)); break;
case 0xA6: SetX(GetZP(PC, out bytes)); break;
case 0xA8: TAY(); break;
case 0xA9: SetA(GetIM(PC, out bytes)); break;
case 0xAA: TAX(); break;
case 0xAC: SetY(GetABS(PC, out bytes)); break;
case 0xAD: SetA(GetABS(PC, out bytes)); break;
case 0xAE: SetX(GetABS(PC, out bytes)); break;
case 0xB0: BCS(ref PC, out conditional, out bytes); break;
case 0xB1: SetA(GetIndY(PC, out bytes)); break;
case 0xB4: SetY(GetZPX(PC, out bytes)); break;
case 0xB5: SetA(GetZPX(PC, out bytes)); break;
case 0xB6: SetX(GetZPY(PC, out bytes)); break;
case 0xB8: CLV(); break;
case 0xB9: SetA(GetABSY(PC, out bytes)); break;
case 0xBA: TSX(); break;
case 0xBC: SetY(GetABSX(PC, out bytes)); break;
case 0xBD: SetA(GetABSX(PC, out bytes)); break;
case 0xBE: SetX(GetABSY(PC, out bytes)); break;
case 0xC0: CPY(GetIM(PC, out bytes)); break;
case 0xC1: CMP(GetIndX(PC, out bytes)); break;
case 0xC4: CPY(GetZP(PC, out bytes)); break;
case 0xC5: CMP(GetZP(PC, out bytes)); break;
case 0xC6: SetZP(DEC(GetZP(PC, out bytes)), PC, out bytes); break;
case 0xC8: INY(); break;
case 0xC9: CMP(GetIM(PC, out bytes)); break;
case 0xCA: DEX(); break;
case 0xCC: CPY(GetABS(PC, out bytes)); break;
case 0xCD: CMP(GetABS(PC, out bytes)); break;
case 0xCE: SetABS(DEC(GetABS(PC, out bytes)), PC, out bytes); break;
case 0xD0: BNE(ref PC, out conditional, out bytes); break;
case 0xD1: CMP(GetIndY(PC, out bytes)); break;
case 0xD5: CMP(GetZPX(PC, out bytes)); break;
case 0xD6: SetZPX(DEC(GetZPX(PC, out bytes)), PC, out bytes); break;
case 0xD8: CLD(); break;
case 0xD9: CMP(GetABSY(PC, out bytes)); break;
case 0xDD: CMP(GetABSX(PC, out bytes)); break;
case 0xDE: SetABSX(DEC(GetABSX(PC, out bytes)), PC, out bytes); break;
case 0xE0: CPX(GetIM(PC, out bytes)); break;
case 0xE1: SBC(GetIndX(PC, out bytes)); break;
case 0xE4: CPX(GetZP(PC, out bytes)); break;
case 0xE5: SBC(GetZP(PC, out bytes)); break;
case 0xE6: SetZP(INC(GetZP(PC, out bytes)), PC, out bytes); break;
case 0xE8: INX(); break;
case 0xE9: SBC(GetIM(PC, out bytes)); break;
case 0xEA: NOP(); break;
case 0xEC: CPX(GetABS(PC, out bytes)); break;
case 0xED: SBC(GetABS(PC, out bytes)); break;
case 0xEE: SetABS(INC(GetABS(PC, out bytes)), PC, out bytes); break;
case 0xF0: BEQ(ref PC, out conditional, out bytes); break;
case 0xF1: SBC(GetIndY(PC, out bytes)); break;
case 0xF5: SBC(GetZPX(PC, out bytes)); break;
case 0xF6: SetZPX(INC(GetZPX(PC, out bytes)), PC, out bytes); break;
case 0xF8: SED(); break;
case 0xF9: SBC(GetABSY(PC, out bytes)); break;
case 0xFD: SBC(GetABSX(PC, out bytes)); break;
case 0xFE: SetABSX(INC(GetABSX(PC, out bytes)), PC, out bytes); break;
default:
throw new Exception(string.Format("Invalid opcode {0:X2} at {1:X4}", memory[PC], PC));
}
PC += bytes;
}
}
void CMP(byte value)
{
Subtract(A, value);
}
void CPX(byte value)
{
Subtract(X, value);
}
void CPY(byte value)
{
Subtract(Y, value);
}
void SBC(byte value)
{
if (D)
{
int A_dec = (A & 0xF) + ((A >> 4) * 10);
int value_dec = (value & 0xF) + ((value >> 4) * 10);
int result_dec = A_dec - value_dec - (C ? 0 : 1);
C = (result_dec >= 0);
if (!C)
result_dec += 100; // fixup negative number
int result = (result_dec % 10) | (((result_dec / 10) % 10) << 4);
SetA(result);
N = false; // undefined?
V = false; // undefined?
}
else
{
byte result = Subtract(A, value, out V);
SetA(result);
}
}
byte Subtract(byte reg, byte value)
{
C = true; // init for CMP, etc.
bool unused;
return Subtract(reg, value, out unused);
}
byte Subtract(byte reg, byte value, out bool overflow)
{
bool old_reg_neg = (reg & 0x80) != 0;
bool value_neg = (value & 0x80) != 0;
int result = reg - value - (C ? 0 : 1);
N = (result & 0x80) != 0;
C = (result >= 0);
Z = (result == 0);
bool result_neg = (result & 0x80) != 0;
overflow = (old_reg_neg && !value_neg && !result_neg) // neg - pos = pos
|| (!old_reg_neg && value_neg && result_neg); // pos - neg = neg
return (byte)result;
}
void ADC(byte value)
{
int result;
if (D)
{
int A_dec = (A & 0xF) + ((A >> 4) * 10);
int value_dec = (value & 0xF) + ((value >> 4) * 10);
int result_dec = A_dec + value_dec + (C ? 1 : 0);
C = (result_dec > 99);
result = (result_dec % 10) | (((result_dec / 10) % 10) << 4);
SetA(result);
Z = (result_dec == 0); // BCD quirk -- 100 doesn't set Z
V = false;
}
else
{
bool A_old_neg = (A & 0x80) != 0;
bool value_neg = (value & 0x80) != 0;
result = A + value + (C ? 1 : 0);
C = (result & 0x100) != 0;
SetA(result);
bool result_neg = (result & 0x80) != 0;
V = (!A_old_neg && !value_neg && result_neg) // pos + pos = neg: overflow
|| (A_old_neg && value_neg && !result_neg); // neg + neg = pos: overflow
}
}
void ORA(int value)
{
SetA(A | value);
}
void EOR(int value)
{
SetA(A ^ value);
}
void AND(int value)
{
SetA(A & value);
}
void BIT(byte value)
{
Z = (A & value) == 0;
N = (value & 0x80) != 0;
V = (value & 0x40) != 0;
}
byte ASL(int value)
{
C = (value & 0x80) != 0;
value = (byte)(value << 1);
Z = (value == 0);
N = (value & 0x80) != 0;
return (byte)value;
}
byte LSR(int value)
{
C = (value & 0x01) != 0;
value = (byte)(value >> 1);
Z = (value == 0);
N = false;
return (byte)value;
}
byte ROL(int value)
{
bool newC = (value & 0x80) != 0;
value = (byte)((value << 1) | (C ? 1 : 0));
C = newC;
Z = (value == 0);
N = (value & 0x80) != 0;
return (byte)value;
}
byte ROR(int value)
{
bool newC = (value & 0x01) != 0;
N = C;
value = (byte)((value >> 1) | (C ? 0x80 : 0));
C = newC;
Z = (value == 0);
return (byte)value;
}
protected void Push(int value)
{
memory[(ushort)(0x100 + (S--))] = (byte)value;
}
protected byte Pop()
{
return memory[(ushort)(0x100 + (++S))];
}
void PHP()
{
int flags = (N ? 0x80 : 0)
| (V ? 0x40 : 0)
| 0x20 // reserved, always set
| 0x10 // break always set when push
| (D ? 0x08 : 0)
| (I ? 0x04 : 0)
| (Z ? 0x02 : 0)
| (C ? 0x01 : 0);
Push(flags);
}
void PLP()
{
int flags = Pop();
N = (flags & 0x80) != 0;
V = (flags & 0x40) != 0;
B = (flags & 0x10) != 0;
D = (flags & 0x08) != 0;
I = (flags & 0x04) != 0;
Z = (flags & 0x02) != 0;
C = (flags & 0x01) != 0;
}
void PHA()
{
Push(A);
}
void PLA()
{
SetA(Pop());
}
void CLC()
{
C = false;
}
void CLD()
{
D = false;
}
void CLI()
{
I = false;
}
void CLV()
{
V = false;
}
void SEC()
{
C = true;
}
void SED()
{
D = true;
}
void SEI()
{
I = true;
}
void INX()
{
X = INC(X);
}
void INY()
{
Y = INC(Y);
}
void DEX()
{
X = DEC(X);
}
void DEY()
{
Y = DEC(Y);
}
void NOP()
{
}
byte DEC(byte value)
{
--value;
Z = (value == 0);
N = (value & 0x80) != 0;
return (byte)value;
}
byte INC(byte value)
{
++value;
Z = (value == 0);
N = (value & 0x80) != 0;
return (byte)value;
}
void TXA()
{
SetReg(ref A, X);
}
void TAX()
{
SetReg(ref X, A);
}
void TYA()
{
SetReg(ref A, Y);
}
void TAY()
{
SetReg(ref Y, A);
}
void TXS()
{
S = X;
}
void TSX()
{
SetReg(ref X, S);
}
void BR(bool branch, ref ushort addr, out bool conditional, out byte bytes)
{
ushort addr2 = GetBR(addr, out conditional, out bytes);
if (branch)
{
addr = addr2;
bytes = 0; // don't advance addr
}
}
void BPL(ref ushort addr, out bool conditional, out byte bytes)
{
BR(!N, ref addr, out conditional, out bytes);
}
void BMI(ref ushort addr, out bool conditional, out byte bytes)
{
BR(N, ref addr, out conditional, out bytes);
}
void BCC(ref ushort addr, out bool conditional, out byte bytes)
{
BR(!C, ref addr, out conditional, out bytes);
}
void BCS(ref ushort addr, out bool conditional, out byte bytes)
{
BR(C, ref addr, out conditional, out bytes);
}
void BVC(ref ushort addr, out bool conditional, out byte bytes)
{
BR(!V, ref addr, out conditional, out bytes);
}
void BVS(ref ushort addr, out bool conditional, out byte bytes)
{
BR(V, ref addr, out conditional, out bytes);
}
void BNE(ref ushort addr, out bool conditional, out byte bytes)
{
BR(!Z, ref addr, out conditional, out bytes);
}
void BEQ(ref ushort addr, out bool conditional, out byte bytes)
{
BR(Z, ref addr, out conditional, out bytes);
}
void JSR(ref ushort addr, out byte bytes)
{
bytes = 3; // for next calculation
ushort addr2 = (ushort)(addr + bytes - 1);
ushort addr3 = (ushort)(memory[(ushort)(addr + 1)] | (memory[(ushort)(addr + 2)] << 8));
Push(HI(addr2));
Push(LO(addr2));
addr = addr3;
bytes = 0; // addr already changed
}
public void RTS(ref ushort addr, out byte bytes)
{
byte lo = Pop();
byte hi = Pop();
bytes = 0; // make sure caller does not increase addr because handled here
addr = (ushort)(((hi << 8) | lo) + 1);
}
void RTI(ref ushort addr, out byte bytes)
{
PLP();
byte lo = Pop();
byte hi = Pop();
bytes = 0; // make sure caller does not increase addr by one
addr = (ushort)((hi << 8) | lo);
}
void BRK(out byte bytes)
{
PC += 2;
Push(HI(PC));
Push(LO(PC));
B = true;
PHP();
I = true;
PC = (ushort)(memory[0xFFFE] + (memory[0xFFFF] << 8)); // JMP(IRQ)
bytes = 0;
}
void JMP(ref ushort addr, out byte bytes)
{
bytes = 0; // caller should not advance address
ushort addr2 = (ushort)(memory[(ushort)(addr + 1)] | (memory[(ushort)(addr + 2)] << 8));
addr = addr2;
}
void JMPIND(ref ushort addr, out byte bytes)
{
bytes = 0; // caller should not advance address
ushort addr2 = (ushort)(memory[(ushort)(addr + 1)] | (memory[(ushort)(addr + 2)] << 8));
ushort addr3;
if ((addr2 & 0xFF) == 0xFF) // JMP($XXFF) won't go over page boundary
addr3 = (ushort)(memory[addr2] | (memory[(ushort)(addr2 - 0xFF)] << 8)); // 6502 "bug" - will use XXFF and XX00 as source of address
else
addr3 = (ushort)(memory[addr2] | (memory[(ushort)(addr2 + 1)] << 8));
addr = addr3;
}
public void SetA(int value)
{
SetReg(ref A, value);
}
public void SetX(int value)
{
SetReg(ref X, value);
}
public void SetY(int value)
{
SetReg(ref Y, value);
}
void SetReg(ref byte reg, int value)
{
reg = (byte)value;
Z = (reg == 0);
N = ((reg & 0x80) != 0);
}
byte GetIndX(ushort addr, out byte bytes)
{
bytes = 2;
byte zp = (byte)(memory[(ushort)(addr + 1)] + X); // must truncate to byte to keep in zero page
return memory[(ushort)(memory[zp] | (memory[++zp] << 8))]; // address overflow must keep within zero page, using increment to do this
}
void SetIndX(byte value, ushort addr, out byte bytes)
{
bytes = 2;
byte zpaddr = (byte)(memory[(ushort)(addr + 1)] + X); // must truncate address to byte to keep in zero page
ushort addr3 = (ushort)(memory[zpaddr] | (memory[++zpaddr] << 8)); // address overflow must keep within zero page, using increment to do this
memory[addr3] = value;
}
byte GetIndY(ushort addr, out byte bytes)
{
bytes = 2;
ushort addr2 = (ushort)(memory[(ushort)(addr + 1)]);
ushort addr3 = (ushort)((memory[addr2] | (memory[(ushort)(addr2 + 1)] << 8)) + Y);
return memory[addr3];
}
void SetIndY(byte value, ushort addr, out byte bytes)
{
bytes = 2;
ushort addr2 = (ushort)(memory[(ushort)(addr + 1)]);
ushort addr3 = (ushort)((memory[addr2] | (memory[(ushort)(addr2 + 1)] << 8)) + Y);
memory[addr3]=value;
}
byte GetZP(ushort addr, out byte bytes)
{
bytes = 2;
byte zpaddr = memory[(ushort)(addr + 1)];
return memory[zpaddr];
}
void SetZP(byte value, ushort addr, out byte bytes)
{
bytes = 2;
byte zpaddr = memory[(ushort)(addr + 1)];
memory[zpaddr]=value;
}
byte GetZPX(ushort addr, out byte bytes)
{
bytes = 2;
byte zpaddr = memory[(ushort)(addr + 1)];
return memory[(byte)(zpaddr + X)];
}
void SetZPX(byte value, ushort addr, out byte bytes)
{
bytes = 2;
byte zpaddr = memory[(ushort)(addr + 1)];
memory[(byte)(zpaddr + X)] = value;
}
byte GetZPY(ushort addr, out byte bytes)
{
bytes = 2;
byte zpaddr = memory[(ushort)(addr + 1)];
return memory[(byte)(zpaddr + Y)];
}
void SetZPY(byte value, ushort addr, out byte bytes)
{
bytes = 2;
byte zpaddr = memory[(ushort)(addr + 1)];
memory[(byte)(zpaddr + Y)] = value;
}
byte GetABS(ushort addr, out byte bytes)
{
bytes = 3;
ushort addr2 = (ushort)(memory[(ushort)(addr + 1)] | (memory[(ushort)(addr + 2)] << 8));
return memory[addr2];
}
void SetABS(byte value, ushort addr, out byte bytes)
{
bytes = 3;
ushort addr2 = (ushort)(memory[(ushort)(addr + 1)] | (memory[(ushort)(addr + 2)] << 8));
memory[addr2] = value;
}
byte GetABSX(ushort addr, out byte bytes)
{
bytes = 3;
ushort addr2 = (ushort)(memory[(ushort)(addr + 1)] | (memory[(ushort)(addr + 2)] << 8));
return memory[(ushort)(addr2 + X)];
}
void SetABSX(byte value, ushort addr, out byte bytes)
{
bytes = 3;
ushort addr2 = (ushort)((memory[(ushort)(addr + 1)] | (memory[(ushort)(addr + 2)] << 8)) + X);
memory[addr2] = value;
}
byte GetABSY(ushort addr, out byte bytes)
{
bytes = 3;
ushort addr2 = (ushort)(memory[(ushort)(addr + 1)] | (memory[(ushort)(addr + 2)] << 8));
return memory[(ushort)(addr2 + Y)];
}
void SetABSY(byte value, ushort addr, out byte bytes)
{
bytes = 3;
ushort addr2 = (ushort)((memory[(ushort)(addr + 1)] | (memory[(ushort)(addr + 2)] << 8)) + Y);
memory [addr2] = value;
}
byte GetIM(ushort addr, out byte bytes)
{
bytes = 2;
return memory[(ushort)(addr + 1)];
}
ushort GetBR(ushort addr, out bool conditional, out byte bytes)
{
conditional = true;
bytes = 2;
sbyte offset = (sbyte)memory[(ushort)(addr + 1)];
ushort addr2 = (ushort)(addr + 2 + offset);
return addr2;
}
public byte LO(ushort value)
{
return (byte)value;
}
public byte HI(ushort value)
{
return (byte)(value >> 8);
}
string GetDisplayState()
{
return string.Format("A:{0:X2} X:{1:X2} Y:{2:X2} S:{3:X2} P:{4}{5}-{6}{7}{8}{9}{10}",
A,
X,
Y,
S,
N ? 'N' : ' ',
V ? 'V' : ' ',
B ? 'B' : ' ',
D ? 'D' : ' ',
I ? 'I' : ' ',
Z ? 'Z' : ' ',
C ? 'C' : ' '
);
}
public string Disassemble(ushort addr, out bool conditional, out byte bytes, out ushort addr2, out string line)
{
string dis = Disassemble(addr, out conditional, out bytes, out addr2);
StringBuilder s = new StringBuilder();
s.AppendFormat("{0:X4} ", addr);
for (int i = 0; i < 3; ++i)
{
if (i < bytes)
s.AppendFormat("{0:X2} ", memory[(ushort)(addr + i)]);
else
s.Append(" ");
}
s.Append(dis);
line = s.ToString();
return dis;
}
string Disassemble(ushort addr, out bool conditional, out byte bytes, out ushort addr2)
{
conditional = false;
addr2 = 0;
bytes = 1;
switch (memory[addr])
{
case 0x00: return "BRK";
case 0x01: return IndX("ORA", addr, out bytes);
case 0x05: return ZP("ORA", addr, out bytes);
case 0x06: return ZP("ASL", addr, out bytes);
case 0x08: return "PHP";
case 0x09: return IM("ORA", addr, out bytes);
case 0x0A: return "ASL A";
case 0x0D: return ABS("ORA", addr, out bytes);
case 0x0E: return ABS("ASL", addr, out bytes);
case 0x10: return BR("BPL", addr, out conditional, out addr2, out bytes);
case 0x11: return IndY("ORA", addr, out bytes);
case 0x15: return ZPX("ORA", addr, out bytes);
case 0x16: return ZPX("ASL", addr, out bytes);
case 0x18: return "CLC";
case 0x19: return ABSY("ORA", addr, out bytes);
case 0x1D: return ABSX("ORA", addr, out bytes);
case 0x1E: return ABSX("ASL", addr, out bytes);
case 0x20: return ABS("JSR", addr, out addr2, out bytes);
case 0x21: return IndX("AND", addr, out bytes);
case 0x24: return ZP("BIT", addr, out bytes);
case 0x25: return ZP("AND", addr, out bytes);
case 0x26: return ZP("ROL", addr, out bytes);
case 0x28: return "PLP";
case 0x29: return IM("AND", addr, out bytes);
case 0x2A: return "ROL A";
case 0x2C: return ABS("BIT", addr, out bytes);
case 0x2D: return ABS("AND", addr, out bytes);
case 0x2E: return ABS("ROL", addr, out bytes);
case 0x30: return BR("BMI", addr, out conditional, out addr2, out bytes);
case 0x31: return IndY("AND", addr, out bytes);
case 0x35: return ZPX("AND", addr, out bytes);
case 0x36: return ZPX("ROL", addr, out bytes);
case 0x38: return "SEC";
case 0x39: return ABSY("AND", addr, out bytes);
case 0x3D: return ABSX("AND", addr, out bytes);
case 0x3E: return ABSX("ROL", addr, out bytes);
case 0x40: return "RTI";
case 0x41: return IndX("EOR", addr, out bytes);
case 0x45: return ZP("EOR", addr, out bytes);
case 0x46: return ZP("LSR", addr, out bytes);
case 0x48: return "PHA";
case 0x49: return IM("EOR", addr, out bytes);
case 0x4A: return "LSR A";
case 0x4C: return ABS("JMP", addr, out addr2, out bytes);
case 0x4D: return ABS("EOR", addr, out bytes);
case 0x4E: return ABS("LSR", addr, out bytes);
case 0x50: return BR("BVC", addr, out conditional, out addr2, out bytes);
case 0x51: return IndY("EOR", addr, out bytes);
case 0x55: return ZPX("EOR", addr, out bytes);
case 0x56: return ZPX("LSR", addr, out bytes);
case 0x58: return "CLI";
case 0x59: return ABSY("EOR", addr, out bytes);
case 0x5D: return ABSX("EOR", addr, out bytes);
case 0x5E: return ABSX("LSR", addr, out bytes);
case 0x60: return "RTS";
case 0x61: return IndX("ADC", addr, out bytes);
case 0x65: return ZP("ADC", addr, out bytes);
case 0x66: return ZP("ROR", addr, out bytes);
case 0x68: return "PLA";
case 0x69: return IM("ADC", addr, out bytes);
case 0x6A: return "ROR A";
case 0x6C: return Ind("JMP", addr, out addr2, out bytes);
case 0x6D: return ABS("ADC", addr, out bytes);
case 0x6E: return ABS("ROR", addr, out bytes);
case 0x70: return BR("BVS", addr, out conditional, out addr2, out bytes);
case 0x71: return IndY("ADC", addr, out bytes);
case 0x75: return ZPX("ADC", addr, out bytes);
case 0x76: return ZPX("ROR", addr, out bytes);
case 0x78: return "SEI";
case 0x79: return ABSY("ADC", addr, out bytes);
case 0x7D: return ABSX("ADC", addr, out bytes);
case 0x7E: return ABSX("ROR", addr, out bytes);
case 0x81: return IndX("STA", addr, out bytes);
case 0x84: return ZP("STY", addr, out bytes);
case 0x85: return ZP("STA", addr, out bytes);
case 0x86: return ZP("STX", addr, out bytes);
case 0x88: return "DEY";
case 0x8A: return "TXA";
case 0x8C: return ABS("STY", addr, out bytes);
case 0x8D: return ABS("STA", addr, out bytes);
case 0x8E: return ABS("STX", addr, out bytes);