-
Notifications
You must be signed in to change notification settings - Fork 1
/
6502.go
1796 lines (1492 loc) · 27.2 KB
/
6502.go
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
package main
// the main struct, containing the main registers,
// the processor's status and the memory
type Cpu struct {
pc, sp, ac, x, y int
pbCrossed bool
p ProcStat
mem Mem
}
// the status flags of the processor
// carry, zero, interrupt, decimal, break,
// negative, overflow
type ProcStat struct {
c, z, i, d, b, n, v int
}
// returns the status as an int word
func (p *ProcStat) getAsWord() (pstatus int) {
pstatus = p.c | p.z<<1 | p.i<<2 | p.d<<3 | p.b<<4 | p.v<<6 | p.n<<7
return
}
func (p *ProcStat) setAsWord(pstatus int) {
if pstatus&BIT_0 == 0 {
p.c = 0
} else {
p.c = 1
}
if pstatus&BIT_1 == 0 {
p.z = 0
} else {
p.z = 1
}
if pstatus&BIT_2 == 0 {
p.i = 0
} else {
p.i = 1
}
if pstatus&BIT_3 == 0 {
p.d = 0
} else {
p.d = 1
}
if pstatus&BIT_4 == 0 {
p.b = 0
} else {
p.b = 1
}
if pstatus&BIT_6 == 0 {
p.n = 0
} else {
p.n = 1
}
if pstatus&BIT_7 == 0 {
p.v = 0
} else {
p.v = 1
}
}
// sets the negative flag (n) from the
// data given
func (p *ProcStat) setN(data int) {
if data&BIT_7 == BIT_7 {
p.n = 1
} else {
p.n = 0
}
}
// set the zero flag (z) from the data
// given
func (p *ProcStat) setZ(data int) {
if data == 0 {
p.z = 1
} else {
p.z = 0
}
}
// the memory interface
// a memory must be provided for the cpu to work
type Mem interface {
Read(addr int) int
Write(addr, value int)
}
// interprets a word as bcd
func bcd(n int) int {
return (n & 0xF) + (n & 0xF0 >> 4 * 10)
}
// Register constants
const (
A = iota
X
Y
)
// Bit constants
const (
BIT_0 = 1 << iota
BIT_1
BIT_2
BIT_3
BIT_4
BIT_5
BIT_6
BIT_7
BIT_8
)
func (cpu *Cpu) execute() (resCycles int) {
// grab current instruction and increment pc
inst := cpu.mem.Read(cpu.pc)
cpu.pc++
switch inst {
case 0x69:
cpu.adc(cpu.imm())
resCycles = 2
case 0x65:
cpu.adc(cpu.zp())
resCycles = 3
case 0x75:
cpu.adc(cpu.zpx())
resCycles = 4
case 0x6D:
cpu.adc(cpu.abs())
resCycles = 4
case 0x7D:
cpu.adc(cpu.abx())
if cpu.pbCrossed {
resCycles = 5
} else {
resCycles = 4
}
case 0x79:
cpu.adc(cpu.aby())
if cpu.pbCrossed {
resCycles = 5
} else {
resCycles = 4
}
case 0x61:
cpu.adc(cpu.indx())
resCycles = 6
case 0x71:
cpu.adc(cpu.indy())
if cpu.pbCrossed {
resCycles = 6
} else {
resCycles = 5
}
// AND's
case 0x29:
cpu.and(cpu.imm())
resCycles = 2
case 0x25:
cpu.and(cpu.zp())
resCycles = 2
case 0x35:
cpu.and(cpu.zpx())
resCycles = 3
case 0x2D:
cpu.and(cpu.abs())
resCycles = 4
case 0x3D:
cpu.and(cpu.abx())
if cpu.pbCrossed {
resCycles = 5
} else {
resCycles = 4
}
case 0x39:
cpu.and(cpu.aby())
if cpu.pbCrossed {
resCycles = 5
} else {
resCycles = 4
}
case 0x21:
cpu.and(cpu.indx())
resCycles = 6
case 0x31:
cpu.and(cpu.indy())
if cpu.pbCrossed {
resCycles = 6
} else {
resCycles = 5
}
// ASL's
case 0x0A:
cpu.asla()
resCycles = 2
case 0x06:
cpu.asl(cpu.zp())
resCycles = 5
case 0x16:
cpu.asl(cpu.zpx())
resCycles = 6
case 0x0E:
cpu.asl(cpu.abs())
resCycles = 6
case 0x1E:
cpu.asl(cpu.abx())
resCycles = 7
// BCC
case 0x90:
if cpu.bcc(cpu.rel()) {
if cpu.pbCrossed {
resCycles = 4
} else {
resCycles = 3
}
} else {
resCycles = 2
}
// BCS
case 0xB0:
if cpu.bcs(cpu.rel()) {
if cpu.pbCrossed {
resCycles = 4
} else {
resCycles = 3
}
} else {
resCycles = 2
}
// BEQ
case 0xF0:
if cpu.beq(cpu.rel()) {
if cpu.pbCrossed {
resCycles = 4
} else {
resCycles = 3
}
} else {
resCycles = 2
}
// BIT
case 0x24:
cpu.bit(cpu.zp())
resCycles = 3
case 0x2C:
cpu.bit(cpu.abs())
resCycles = 4
// BMI
case 0x30:
if cpu.bmi(cpu.rel()) {
if cpu.pbCrossed {
resCycles = 4
} else {
resCycles = 3
}
} else {
resCycles = 2
}
// BNE
case 0xD0:
if cpu.bne(cpu.rel()) {
if cpu.pbCrossed {
resCycles = 4
} else {
resCycles = 3
}
} else {
resCycles = 2
}
// BPL
case 0x10:
if cpu.bpl(cpu.rel()) {
if cpu.pbCrossed {
resCycles = 4
} else {
resCycles = 3
}
} else {
resCycles = 2
}
// BRK
case 0x00:
cpu.brk()
resCycles = 7
// BVC
case 0x50:
if cpu.bvc(cpu.rel()) {
if cpu.pbCrossed {
resCycles = 4
} else {
resCycles = 3
}
} else {
resCycles = 2
}
// BVS
case 0x70:
if cpu.bvs(cpu.rel()) {
if cpu.pbCrossed {
resCycles = 4
} else {
resCycles = 3
}
} else {
resCycles = 2
}
case 0x18:
cpu.clc()
resCycles = 2
case 0xD8:
cpu.cld()
resCycles = 2
case 0x58:
cpu.cli()
resCycles = 2
case 0xB8:
cpu.clv()
// CMP
case 0xC9:
cpu.cmp(cpu.imm(), A)
resCycles = 2
case 0xC5:
cpu.cmp(cpu.zp(), A)
resCycles = 3
case 0xD5:
cpu.cmp(cpu.zpx(), A)
resCycles = 4
case 0xCD:
cpu.cmp(cpu.abs(), A)
resCycles = 4
case 0xDD:
cpu.cmp(cpu.abx(), A)
if cpu.pbCrossed {
resCycles = 5
} else {
resCycles = 4
}
case 0xD9:
cpu.cmp(cpu.aby(), A)
if cpu.pbCrossed {
resCycles = 5
} else {
resCycles = 4
}
case 0xC1:
cpu.cmp(cpu.indx(), A)
resCycles = 6
case 0xD1:
cpu.cmp(cpu.indy(), A)
if cpu.pbCrossed {
resCycles = 6
} else {
resCycles = 5
}
// CPX
case 0xE0:
cpu.cmp(cpu.imm(), X)
resCycles = 2
case 0xE4:
cpu.cmp(cpu.zp(), X)
resCycles = 3
case 0xEC:
cpu.cmp(cpu.abs(), X)
resCycles = 4
// CPY
case 0xC0:
cpu.cmp(cpu.imm(), Y)
resCycles = 2
case 0xC4:
cpu.cmp(cpu.zp(), Y)
resCycles = 3
case 0xCC:
cpu.cmp(cpu.abs(), Y)
resCycles = 4
// DEC
case 0xC6:
cpu.dec(cpu.zp())
resCycles = 5
case 0xD6:
cpu.dec(cpu.zpx())
resCycles = 6
case 0xCE:
cpu.dec(cpu.abs())
resCycles = 6
case 0xDE:
cpu.dec(cpu.abx())
resCycles = 7
// DEX
case 0xCA:
cpu.decxy(X)
resCycles = 2
// DEY
case 0x88:
cpu.decxy(Y)
resCycles = 2
// EOR
case 0x49:
cpu.eor(cpu.imm())
resCycles = 2
case 0x45:
cpu.eor(cpu.zp())
resCycles = 3
case 0x55:
cpu.eor(cpu.zpx())
resCycles = 4
case 0x4D:
cpu.eor(cpu.abs())
resCycles = 4
case 0x5D:
cpu.eor(cpu.abx())
if cpu.pbCrossed {
resCycles = 5
} else {
resCycles = 4
}
case 0x59:
cpu.eor(cpu.aby())
if cpu.pbCrossed {
resCycles = 5
} else {
resCycles = 4
}
case 0x41:
cpu.eor(cpu.indx())
resCycles = 6
case 0x51:
cpu.eor(cpu.indy())
if cpu.pbCrossed {
resCycles = 6
} else {
resCycles = 5
}
// INC
case 0xE6:
cpu.inc(cpu.zp())
resCycles = 5
case 0xF6:
cpu.inc(cpu.zpx())
resCycles = 6
case 0xEE:
cpu.inc(cpu.abs())
resCycles = 6
case 0xFE:
cpu.inc(cpu.abx())
resCycles = 7
// INX
case 0xE8:
cpu.incxy(X)
resCycles = 2
// INY
case 0xC8:
cpu.incxy(Y)
resCycles = 2
// JMP
case 0x4C:
cpu.jmp(cpu.abs())
resCycles = 3
case 0x6C:
cpu.jmp(cpu.ind())
resCycles = 5
// JSR
case 0x20:
cpu.jsr(cpu.abs())
resCycles = 6
// LDA
case 0xA9:
cpu.ldr(cpu.imm(), A)
resCycles = 2
case 0xA5:
cpu.ldr(cpu.zp(), A)
resCycles = 3
case 0xB5:
cpu.ldr(cpu.zpx(), A)
resCycles = 4
case 0xAD:
cpu.ldr(cpu.abs(), A)
resCycles = 4
case 0xBD:
cpu.ldr(cpu.abx(), A)
if cpu.pbCrossed {
resCycles = 5
} else {
resCycles = 4
}
case 0xB9:
cpu.ldr(cpu.aby(), A)
if cpu.pbCrossed {
resCycles = 5
} else {
resCycles = 4
}
case 0xA1:
cpu.ldr(cpu.indx(), A)
resCycles = 6
case 0xB1:
cpu.ldr(cpu.indy(), A)
if cpu.pbCrossed {
resCycles = 6
} else {
resCycles = 5
}
// LDX
case 0xA2:
cpu.ldr(cpu.imm(), X)
resCycles = 2
case 0xA6:
cpu.ldr(cpu.zp(), X)
resCycles = 3
case 0xB6:
cpu.ldr(cpu.zpy(), X)
resCycles = 4
case 0xAE:
cpu.ldr(cpu.abs(), X)
resCycles = 4
case 0xBE:
cpu.ldr(cpu.aby(), X)
if cpu.pbCrossed {
resCycles = 5
} else {
resCycles = 4
}
// LDY
case 0xA0:
cpu.ldr(cpu.imm(), Y)
resCycles = 2
case 0xA4:
cpu.ldr(cpu.zp(), Y)
resCycles = 3
case 0xB4:
cpu.ldr(cpu.zpx(), Y)
resCycles = 4
case 0xAC:
cpu.ldr(cpu.abs(), Y)
resCycles = 4
case 0xBC:
cpu.ldr(cpu.abx(), Y)
if cpu.pbCrossed {
resCycles = 5
} else {
resCycles = 4
}
// LSR
case 0x4A:
cpu.lsra()
resCycles = 2
case 0x46:
cpu.lsrm(cpu.zp())
resCycles = 5
case 0x56:
cpu.lsrm(cpu.zpx())
resCycles = 6
case 0x4E:
cpu.lsrm(cpu.abs())
resCycles = 6
case 0x5E:
cpu.lsrm(cpu.abx())
resCycles = 7
// NOP
case 0xEA:
cpu.nop()
resCycles = 2
// ORA
case 0x09:
cpu.ora(cpu.imm())
resCycles = 2
case 0x05:
cpu.ora(cpu.imm())
resCycles = 2
case 0x15:
cpu.ora(cpu.imm())
resCycles = 3
case 0x1D:
cpu.ora(cpu.imm())
if cpu.pbCrossed {
resCycles = 5
} else {
resCycles = 4
}
case 0x19:
cpu.ora(cpu.imm())
if cpu.pbCrossed {
resCycles = 5
} else {
resCycles = 4
}
case 0x01:
cpu.ora(cpu.imm())
resCycles = 6
case 0x11:
cpu.ora(cpu.imm())
if cpu.pbCrossed {
resCycles = 6
} else {
resCycles = 5
}
// PHA
case 0x48:
cpu.pha()
resCycles = 3
// PHP
case 0x08:
cpu.php()
resCycles = 3
// PLA
case 0x68:
cpu.pla()
resCycles = 4
// PLP
case 0x28:
cpu.plp()
resCycles = 4
// ROL
case 0x2A:
cpu.rola()
resCycles = 2
case 0x26:
cpu.rolm(cpu.zp())
resCycles = 5
case 0x36:
cpu.rolm(cpu.zpx())
resCycles = 6
case 0x2E:
cpu.rolm(cpu.abs())
resCycles = 6
case 0x3E:
cpu.rolm(cpu.abx())
resCycles = 7
// ROR
case 0x6A:
cpu.rora()
resCycles = 2
case 0x66:
cpu.rorm(cpu.zp())
resCycles = 5
case 0x76:
cpu.rorm(cpu.zpx())
resCycles = 6
case 0x6E:
cpu.rorm(cpu.abs())
resCycles = 6
case 0x7E:
cpu.rorm(cpu.abx())
resCycles = 7
// RTI
case 0x40:
cpu.rti()
resCycles = 6
// RTS
case 0x60:
cpu.rts()
resCycles = 6
// SBC
case 0xE9:
cpu.sbc(cpu.imm())
resCycles = 2
case 0xE5:
cpu.sbc(cpu.zp())
resCycles = 3
case 0xF5:
cpu.sbc(cpu.zpx())
resCycles = 4
case 0xED:
cpu.sbc(cpu.abs())
resCycles = 4
case 0xFD:
cpu.sbc(cpu.abx())
if cpu.pbCrossed {
resCycles = 5
} else {
resCycles = 4
}
case 0xF9:
cpu.sbc(cpu.aby())
if cpu.pbCrossed {
resCycles = 5
} else {
resCycles = 4
}
case 0xE1:
cpu.sbc(cpu.indx())
resCycles = 6
case 0xF1:
cpu.sbc(cpu.indy())
if cpu.pbCrossed {
resCycles = 6
} else {
resCycles = 5
}
// SEC
case 0x38:
cpu.sec()
resCycles = 2
// SED
case 0xF8:
cpu.sed()
resCycles = 2
// SEI
case 0x78:
cpu.sei()
resCycles = 2
// STA
case 0x85:
cpu.st(cpu.zp(), A)
resCycles = 3
case 0x95:
cpu.st(cpu.zpx(), A)
resCycles = 4
case 0x8D:
cpu.st(cpu.abs(), A)
resCycles = 4
case 0x9D:
cpu.st(cpu.abx(), A)
if cpu.pbCrossed {
resCycles = 5
} else {
resCycles = 4
}
case 0x99:
cpu.st(cpu.aby(), A)
if cpu.pbCrossed {
resCycles = 5
} else {
resCycles = 4
}
case 0x81:
cpu.st(cpu.indx(), A)
resCycles = 6
case 0x91:
cpu.st(cpu.indy(), A)
if cpu.pbCrossed {
resCycles = 6
} else {
resCycles = 5
}
// STX
case 0x86:
cpu.st(cpu.zp(), X)
resCycles = 3
case 0x96:
cpu.st(cpu.zpy(), X)
resCycles = 4
case 0x8E:
cpu.st(cpu.abs(), X)
resCycles = 4
// STY
case 0x84:
cpu.st(cpu.zp(), Y)
resCycles = 3
case 0x94:
cpu.st(cpu.zpx(), Y)
resCycles = 4
case 0x8C:
cpu.st(cpu.abs(), Y)
resCycles = 4
// TAX
case 0xAA:
cpu.taxy(X)
resCycles = 2
// TAY
case 0xA8:
cpu.taxy(Y)
resCycles = 2
// TSX
case 0xBA:
cpu.tsx()
resCycles = 2
// TXA
case 0x8A:
cpu.txya(X)
resCycles = 2
// TXS
case 0x9A:
cpu.txs()
resCycles = 2
// TYA
case 0x98:
cpu.txya(Y)
resCycles = 2
}
return
}
// instruction implementations
// add with carry
func (cpu *Cpu) adc(addr int) {
data := cpu.mem.Read(addr)
if cpu.p.d == 1 {
// Calculate auxiliary value
aux := bcd2bin(cpu.ac) + bcd2bin(data) + cpu.p.c
if aux > 99 {
aux -= 100
cpu.p.c = 1
} else {
cpu.p.c = 0
}
cpu.ac = bin2bcd(aux)
} else {
// Calculate auxiliary value
aux := cpu.ac + data + cpu.p.c
// Set flags: overflow, sign, zero, and carry
// If the sum of two positive numbers yields a negative result,
// it has overflowed. If the sum of two negative numbers
// yields a positive result, it has overflowed.
isAcPos := cpu.ac & BIT_7 == 0
isDataPos := data & BIT_7 == 0
isResPos := aux & BIT_7 == 0
if (isAcPos && isDataPos && !isResPos) ||
(!isAcPos && !isDataPos && isResPos) {
cpu.p.v = 1
}
cpu.p.setN(aux)
cpu.p.setZ(aux)
if aux > 255 {
cpu.p.c = 1
} else {
cpu.p.c = 0
}
// take the possible carry out
cpu.ac = aux & 0xFF
}
}
// and accumulator with memory
func (cpu *Cpu) and(addr int) {
data := cpu.mem.Read(addr)
cpu.ac &= data
// flags: sign, zero.
cpu.p.setN(cpu.ac)
cpu.p.setZ(cpu.ac)
}
// asymetric shift left accumulator
func (cpu *Cpu) asla() {
carry := (cpu.ac & BIT_7) == BIT_7
if carry {
cpu.p.c = 1
} else {
cpu.p.c = 0
}
cpu.ac <<= 1