forked from mattgodbolt/jsbeeb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
6502.opcodes.js
1277 lines (1226 loc) · 39.3 KB
/
6502.opcodes.js
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
"use strict";
import * as utils from './utils.js';
var hexword = utils.hexword;
var hexbyte = utils.hexbyte;
var signExtend = utils.signExtend;
function rotate(left, logical) {
var lines = [];
if (!left) {
if (!logical) lines.push("var newTopBit = cpu.p.c ? 0x80 : 0x00;");
lines.push("cpu.p.c = !!(REG & 0x01);");
if (logical) {
lines.push("REG >>>= 1;");
} else {
lines.push("REG = (REG >>> 1) | newTopBit;");
}
} else {
if (!logical) lines.push("var newBotBit = cpu.p.c ? 0x01 : 0x00;");
lines.push("cpu.p.c = !!(REG & 0x80);");
if (logical) {
lines.push("REG = (REG << 1) & 0xff;");
} else {
lines.push("REG = ((REG << 1) & 0xff) | newBotBit;");
}
}
lines.push("cpu.setzn(REG);");
return lines;
}
function pull(reg) {
if (reg === 'p') {
return [
"var tempFlags = cpu.pull();",
"cpu.p.c = !!(tempFlags & 0x01);",
"cpu.p.z = !!(tempFlags & 0x02);",
"cpu.p.i = !!(tempFlags & 0x04);",
"cpu.p.d = !!(tempFlags & 0x08);",
"cpu.p.v = !!(tempFlags & 0x40);",
"cpu.p.n = !!(tempFlags & 0x80);"
];
}
return ["cpu." + reg + " = cpu.setzn(cpu.pull());"];
}
function push(reg) {
if (reg === 'p') return "cpu.push(cpu.p.asByte());";
return "cpu.push(cpu." + reg + ");";
}
function InstructionGen(is65c12) {
var self = this;
self.is65c12 = is65c12;
self.ops = {};
self.cycle = 0;
function appendOrPrepend(combiner, cycle, op, exact, addr) {
if (op === undefined) {
op = cycle;
cycle = self.cycle;
}
exact = exact || false;
if (typeof op === "string") op = [op];
if (self.ops[cycle]) {
self.ops[cycle].op = combiner(self.ops[cycle].op, op);
if (exact) self.ops[cycle].exact = true;
if (!self.ops[cycle].addr) self.ops[cycle].addr = addr;
} else
self.ops[cycle] = {op: op, exact: exact, addr: addr};
}
self.append = function (cycle, op, exact, addr) {
appendOrPrepend(function (lhs, rhs) {
return lhs.concat(rhs);
}, cycle, op, exact, addr);
};
self.prepend = function (cycle, op, exact, addr) {
appendOrPrepend(function (lhs, rhs) {
return rhs.concat(lhs);
}, cycle, op, exact, addr);
};
self.tick = function (cycles) {
self.cycle += (cycles || 1);
};
self.readOp = function (addr, reg, spurious) {
self.cycle++;
var op;
if (reg)
op = reg + " = cpu.readmem(" + addr + ");";
else
op = "cpu.readmem(" + addr + ");";
if (spurious) op += " // spurious";
self.append(self.cycle, op, true, addr);
};
self.writeOp = function (addr, reg, spurious) {
self.cycle++;
var op = "cpu.writemem(" + addr + ", " + reg + ");";
if (spurious) op += " // spurious";
self.append(self.cycle, op, true, addr);
};
self.zpReadOp = function (addr, reg) {
self.cycle++;
self.append(self.cycle, reg + " = cpu.readmemZpStack(" + addr + ");", false);
};
self.zpWriteOp = function (addr, reg) {
self.cycle++;
self.append(self.cycle, "cpu.writememZpStack(" + addr + ", " + reg + ");", true);
};
self.render = function (startCycle) {
if (self.cycle < 2) self.cycle = 2;
self.prepend(self.cycle - 1, "cpu.checkInt();", true);
return self.renderInternal(startCycle);
};
self.spuriousOp = function (addr, reg) {
if (self.is65c12) {
self.readOp(addr, reg, true);
} else {
self.writeOp(addr, reg, true);
}
};
self.renderInternal = function (startCycle) {
startCycle = startCycle || 0;
var i;
var toSkip = 0;
var out = [];
for (i = startCycle; i < self.cycle; ++i) {
if (!self.ops[i]) {
toSkip++;
continue;
}
if (toSkip && self.ops[i].exact) {
if (self.ops[i].addr) {
out.push("cpu.polltimeAddr(" + toSkip + ", " + self.ops[i].addr + ");");
} else {
out.push("cpu.polltime(" + toSkip + ");");
}
toSkip = 0;
}
out = out.concat(self.ops[i].op);
toSkip++;
}
if (toSkip) {
if (self.ops[self.cycle] && self.ops[self.cycle].addr) {
out.push("cpu.polltimeAddr(" + toSkip + ", " + self.ops[self.cycle].addr + ");");
} else {
out.push("cpu.polltime(" + toSkip + ");");
}
}
if (self.ops[self.cycle]) out = out.concat(self.ops[self.cycle].op);
return out.filter(function (l) {
return l;
});
};
self.split = function (condition) {
return new SplitInstruction(this, condition, self.is65c12);
};
}
function SplitInstruction(preamble, condition, is65c12) {
var self = this;
self.preamble = preamble;
self.ifTrue = new InstructionGen(is65c12);
self.ifTrue.tick(preamble.cycle);
self.ifFalse = new InstructionGen(is65c12);
self.ifFalse.tick(preamble.cycle);
["append", "prepend", "readOp", "writeOp", "spuriousOp"].forEach(function (op) {
self[op] = function () {
self.ifTrue[op].apply(self.ifTrue, arguments);
self.ifFalse[op].apply(self.ifFalse, arguments);
};
});
function indent(a) {
var result = [];
a.forEach(function (x) {
result.push(" " + x);
});
return result;
}
self.render = function () {
return self.preamble.renderInternal()
.concat(["if (" + condition + ") {"])
.concat(indent(self.ifTrue.render(preamble.cycle)))
.concat(["} else {"])
.concat(indent(self.ifFalse.render(preamble.cycle)))
.concat("}");
};
}
function getOp(op, arg) {
switch (op) {
case "NOP":
return {op: "", read: arg !== undefined};
case "BRK":
return {op: "cpu.brk(false);"};
case "CLC":
return {op: "cpu.p.c = false;"};
case "SEC":
return {op: "cpu.p.c = true;"};
case "CLD":
return {op: "cpu.p.d = false;"};
case "SED":
return {op: "cpu.p.d = true;"};
case "CLI":
return {op: "cpu.p.i = false;"};
case "SEI":
return {op: "cpu.p.i = true;"};
case "CLV":
return {op: "cpu.p.v = false;"};
case "LDA":
return {op: ["cpu.a = cpu.setzn(REG);"], read: true};
case "LDX":
return {op: ["cpu.x = cpu.setzn(REG);"], read: true};
case "LDY":
return {op: ["cpu.y = cpu.setzn(REG);"], read: true};
case "STA":
return {op: "REG = cpu.a;", write: true};
case "STX":
return {op: "REG = cpu.x;", write: true};
case "STY":
return {op: "REG = cpu.y;", write: true};
case "INC":
return {
op: ["REG = cpu.setzn(REG + 1);"],
read: true, write: true
};
case "DEC":
return {
op: ["REG = cpu.setzn(REG - 1);"],
read: true, write: true
};
case "INX":
return {op: ["cpu.x = cpu.setzn(cpu.x + 1);"]};
case "INY":
return {op: ["cpu.y = cpu.setzn(cpu.y + 1);"]};
case "DEX":
return {op: ["cpu.x = cpu.setzn(cpu.x - 1);"]};
case "DEY":
return {op: ["cpu.y = cpu.setzn(cpu.y - 1);"]};
case "ADC":
return {op: "cpu.adc(REG);", read: true};
case "SBC":
return {op: "cpu.sbc(REG);", read: true};
case "BIT":
if (arg === "imm") {
// According to: http://forum.6502.org/viewtopic.php?f=2&t=2241&p=27243#p27239
// the v and n flags are unaffected by BIT #xx
return {op: "cpu.p.z = !(cpu.a & REG);", read: true};
}
return {
op: [
"cpu.p.z = !(cpu.a & REG);",
"cpu.p.v = !!(REG & 0x40);",
"cpu.p.n = !!(REG & 0x80);"],
read: true
};
case "ROL":
return {op: rotate(true, false), read: true, write: true, rotate: true};
case "ROR":
return {op: rotate(false, false), read: true, write: true, rotate: true};
case "ASL":
return {op: rotate(true, true), read: true, write: true, rotate: true};
case "LSR":
return {op: rotate(false, true), read: true, write: true, rotate: true};
case "EOR":
return {op: ["cpu.a = cpu.setzn(cpu.a ^ REG);"], read: true};
case "AND":
return {op: ["cpu.a = cpu.setzn(cpu.a & REG);"], read: true};
case "ORA":
return {op: ["cpu.a = cpu.setzn(cpu.a | REG);"], read: true};
case "CMP":
return {
op: ["cpu.setzn(cpu.a - REG);", "cpu.p.c = cpu.a >= REG;"],
read: true
};
case "CPX":
return {
op: ["cpu.setzn(cpu.x - REG);", "cpu.p.c = cpu.x >= REG;"],
read: true
};
case "CPY":
return {
op: ["cpu.setzn(cpu.y - REG);", "cpu.p.c = cpu.y >= REG;"],
read: true
};
case "TXA":
return {op: ["cpu.a = cpu.setzn(cpu.x);"]};
case "TAX":
return {op: ["cpu.x = cpu.setzn(cpu.a);"]};
case "TXS":
return {op: "cpu.s = cpu.x;"};
case "TSX":
return {op: ["cpu.x = cpu.setzn(cpu.s);"]};
case "TYA":
return {op: ["cpu.a = cpu.setzn(cpu.y);"]};
case "TAY":
return {op: ["cpu.y = cpu.setzn(cpu.a);"]};
case "BEQ":
return {op: "cpu.branch(cpu.p.z);"};
case "BNE":
return {op: "cpu.branch(!cpu.p.z);"};
case "BCS":
return {op: "cpu.branch(cpu.p.c);"};
case "BCC":
return {op: "cpu.branch(!cpu.p.c);"};
case "BMI":
return {op: "cpu.branch(cpu.p.n);"};
case "BPL":
return {op: "cpu.branch(!cpu.p.n);"};
case "BVS":
return {op: "cpu.branch(cpu.p.v);"};
case "BVC":
return {op: "cpu.branch(!cpu.p.v);"};
case "PLA":
return {op: pull('a'), extra: 3};
case "PLP":
return {op: pull('p'), extra: 3};
case "PLX":
return {op: pull('x'), extra: 3};
case "PLY":
return {op: pull('y'), extra: 3};
case "PHA":
return {op: push('a'), extra: 2};
case "PHP":
return {op: push('p'), extra: 2};
case "PHX":
return {op: push('x'), extra: 2};
case "PHY":
return {op: push('y'), extra: 2};
case "RTS":
return {
op: [
"var temp = cpu.pull();",
"temp |= cpu.pull() << 8;",
"cpu.pc = (temp + 1) & 0xffff;"], extra: 5
};
case "RTI":
return {
preop: [
"var temp = cpu.pull();",
"cpu.p.c = !!(temp & 0x01);",
"cpu.p.z = !!(temp & 0x02);",
"cpu.p.i = !!(temp & 0x04);",
"cpu.p.d = !!(temp & 0x08);",
"cpu.p.v = !!(temp & 0x40);",
"cpu.p.n = !!(temp & 0x80);",
"temp = cpu.pull();",
"cpu.pc = temp | (cpu.pull() << 8);"], extra: 5
};
case "JSR":
return {
op: [
"var pushAddr = cpu.pc - 1;",
"cpu.push(pushAddr >>> 8);",
"cpu.push(pushAddr & 0xff);",
"cpu.pc = addr;"], extra: 3
};
case "JMP":
return {op: "cpu.pc = addr;"};
// 65c12 opcodes
case "TSB":
return {
op: [
"cpu.p.z = !(REG & cpu.a);",
"REG |= cpu.a;"
], read: true, write: true
};
case "TRB":
return {
op: [
"cpu.p.z = !(REG & cpu.a);",
"REG &= ~cpu.a;"
], read: true, write: true
};
case "BRA":
return {op: "cpu.branch(true);"};
case "STZ":
return {op: "REG = 0;", write: true};
// Undocumented opcodes.
// first 3 used by Zalaga, http://stardot.org.uk/forums/viewtopic.php?f=2&t=3584&p=30514
case "SAX": // stores (A AND X)
return {op: "REG = cpu.a & cpu.x;", write: true};
case "ASR": // aka ALR equivalent to AND #&AA:LSR A
return {
op: ["REG &= cpu.a;"].concat(
rotate(false, true)).concat(["cpu.a = REG;"])
};
case "SLO": // equivalent to ASL zp:ORA zp
return {
op: rotate(true, true).concat([
"cpu.a |= REG;",
"cpu.setzn(cpu.a);"
]), read: true, write: true
};
case "SHX":
return {op: "REG = (cpu.x & ((addr >>> 8)+1)) & 0xff;", write: true};
case "SHY":
return {op: "REG = (cpu.y & ((addr >>> 8)+1)) & 0xff;", write: true};
case "LAX": // NB uses the c64 value for the magic in the OR here. I don't know what would happen on a beeb.
return {
op: [
"var magic = 0xff;",
"cpu.a = cpu.x = cpu.setzn((cpu.a|magic) & REG);"
], read: true
};
case "LXA": // NB uses the c64 value for the magic in the OR here. I don't know what would happen on a beeb.
return {
op: [
"var magic = 0xee;",
"cpu.a = cpu.x = cpu.setzn((cpu.a|magic) & REG);"
], read: true
};
case "SRE":
return {
op: rotate(false, true).concat(["cpu.a = cpu.setzn(cpu.a ^ REG);"]),
read: true, write: true
};
case "RLA":
return {
op: rotate(true, false).concat(["cpu.a = cpu.setzn(cpu.a & REG);"]),
read: true, write: true
};
case "ANC":
return {op: ["cpu.a = cpu.setzn(cpu.a & REG); cpu.p.c = cpu.p.n;"], read: true};
case "ANE":
return {op: ["cpu.a = cpu.setzn((cpu.a | 0xee) & REG & cpu.x);"], read: true};
case "ARR":
return {op: "cpu.arr(REG);", read: true};
case "DCP":
return {
op: [
"REG = cpu.setzn(REG - 1);",
"cpu.setzn(cpu.a - REG);",
"cpu.p.c = cpu.a >= REG;"
],
read: true, write: true
};
case "LAS":
return {op: ["cpu.a = cpu.x = cpu.s = cpu.setzn(cpu.s & REG);"], read: true};
case "RRA":
return {op: rotate(false, false).concat(["cpu.adc(REG);"]), read: true, write: true};
case "SBX":
return {
op: [
"var temp = cpu.a & cpu.x;",
"cpu.p.c = temp >= REG;",
"cpu.x = cpu.setzn(temp - REG);"
],
read: true
};
case "SHA":
return {
op: [
"REG = cpu.a & cpu.x & ((addr >>> 8) + 1) & 0xff;"
],
write: true
};
case "SHS":
return {
op: [
"cpu.s = cpu.a & cpu.x;",
"REG = cpu.a & cpu.x & ((addr >>> 8) + 1) & 0xff;"
],
write: true
};
case "ISB":
return {
op: [
"REG = (REG + 1) & 0xff;",
"cpu.sbc(REG);"
],
read: true, write: true
};
}
return null;
}
var opcodes6502 = {
0x00: "BRK",
0x01: "ORA (,x)",
0x03: "SLO (,x)",
0x04: "NOP zp",
0x05: "ORA zp",
0x06: "ASL zp",
0x07: "SLO zp",
0x08: "PHP",
0x09: "ORA imm",
0x0A: "ASL A",
0x0B: "ANC imm",
0x0C: "NOP abs",
0x0D: "ORA abs",
0x0E: "ASL abs",
0x0F: "SLO abs",
0x10: "BPL branch",
0x11: "ORA (),y",
0x13: "SLO (),y",
0x14: "NOP zp,x",
0x15: "ORA zp,x",
0x16: "ASL zp,x",
0x17: "SLO zp,x",
0x18: "CLC",
0x19: "ORA abs,y",
0x1A: "NOP",
0x1B: "SLO abs,y",
0x1C: "NOP abs,x",
0x1D: "ORA abs,x",
0x1E: "ASL abs,x",
0x1F: "SLO abs,x",
0x20: "JSR abs",
0x21: "AND (,x)",
0x23: "RLA (,x)",
0x24: "BIT zp",
0x25: "AND zp",
0x26: "ROL zp",
0x27: "RLA zp",
0x28: "PLP",
0x29: "AND imm",
0x2A: "ROL A",
0x2B: "ANC imm",
0x2C: "BIT abs",
0x2D: "AND abs",
0x2E: "ROL abs",
0x2F: "RLA abs",
0x30: "BMI branch",
0x31: "AND (),y",
0x33: "RLA (),y",
0x34: "NOP zp,x",
0x35: "AND zp,x",
0x36: "ROL zp,x",
0x37: "RLA zp,x",
0x38: "SEC",
0x39: "AND abs,y",
0x3A: "NOP",
0x3B: "RLA abs,y",
0x3C: "NOP abs,x",
0x3D: "AND abs,x",
0x3E: "ROL abs,x",
0x3F: "RLA abs,x",
0x40: "RTI",
0x41: "EOR (,x)",
0x43: "SRE (,x)",
0x44: "NOP zp",
0x45: "EOR zp",
0x46: "LSR zp",
0x47: "SRE zp",
0x48: "PHA",
0x49: "EOR imm",
0x4A: "LSR A",
0x4B: "ASR imm",
0x4C: "JMP abs",
0x4D: "EOR abs",
0x4E: "LSR abs",
0x4F: "SRE abs",
0x50: "BVC branch",
0x51: "EOR (),y",
0x53: "SRE (),y",
0x54: "NOP zp,x",
0x55: "EOR zp,x",
0x56: "LSR zp,x",
0x57: "SRE zp,x",
0x58: "CLI",
0x59: "EOR abs,y",
0x5A: "NOP",
0x5B: "SRE abs,y",
0x5C: "NOP abs,x",
0x5D: "EOR abs,x",
0x5E: "LSR abs,x",
0x5F: "SRE abs,x",
0x60: "RTS",
0x61: "ADC (,x)",
0x63: "RRA (,x)",
0x64: "NOP zp",
0x65: "ADC zp",
0x66: "ROR zp",
0x67: "RRA zp",
0x68: "PLA",
0x69: "ADC imm",
0x6A: "ROR A",
0x6B: "ARR imm",
0x6C: "JMP (abs)",
0x6D: "ADC abs",
0x6E: "ROR abs",
0x6F: "RRA abs",
0x70: "BVS branch",
0x71: "ADC (),y",
0x73: "RRA (),y",
0x74: "NOP zp,x",
0x75: "ADC zp,x",
0x76: "ROR zp,x",
0x77: "RRA zp,x",
0x78: "SEI",
0x79: "ADC abs,y",
0x7A: "NOP",
0x7B: "RRA abs,y",
0x7C: "NOP abs,x",
0x7D: "ADC abs,x",
0x7E: "ROR abs,x",
0x7F: "RRA abs,x",
0x80: "NOP imm",
0x81: "STA (,x)",
0x82: "NOP imm",
0x83: "SAX (,x)",
0x84: "STY zp",
0x85: "STA zp",
0x86: "STX zp",
0x87: "SAX zp",
0x88: "DEY",
0x89: "NOP imm",
0x8A: "TXA",
0x8B: "ANE imm",
0x8C: "STY abs",
0x8D: "STA abs",
0x8E: "STX abs",
0x8F: "SAX abs",
0x90: "BCC branch",
0x91: "STA (),y",
0x93: "SHA (),y",
0x94: "STY zp,x",
0x95: "STA zp,x",
0x96: "STX zp,y",
0x97: "SAX zp,y",
0x98: "TYA",
0x99: "STA abs,y",
0x9A: "TXS",
0x9B: "SHS abs,y",
0x9C: "SHY abs,x",
0x9D: "STA abs,x",
0x9E: "SHX abs,y",
0x9F: "SHA abs,y",
0xA0: "LDY imm",
0xA1: "LDA (,x)",
0xA2: "LDX imm",
0xA3: "LAX (,x)",
0xA4: "LDY zp",
0xA5: "LDA zp",
0xA6: "LDX zp",
0xA7: "LAX zp",
0xA8: "TAY",
0xA9: "LDA imm",
0xAA: "TAX",
0xAB: "LXA imm",
0xAC: "LDY abs",
0xAD: "LDA abs",
0xAE: "LDX abs",
0xAF: "LAX abs",
0xB0: "BCS branch",
0xB1: "LDA (),y",
0xB3: "LAX (),y",
0xB4: "LDY zp,x",
0xB5: "LDA zp,x",
0xB6: "LDX zp,y",
0xB7: "LAX zp,y",
0xB8: "CLV",
0xB9: "LDA abs,y",
0xBA: "TSX",
0xBB: "LAS abs,y",
0xBC: "LDY abs,x",
0xBD: "LDA abs,x",
0xBE: "LDX abs,y",
0xBF: "LAX abs,y",
0xC0: "CPY imm",
0xC1: "CMP (,x)",
0xC2: "NOP imm",
0xC3: "DCP (,x)",
0xC4: "CPY zp",
0xC5: "CMP zp",
0xC6: "DEC zp",
0xC7: "DCP zp",
0xC8: "INY",
0xC9: "CMP imm",
0xCA: "DEX",
0xCB: "SBX imm",
0xCC: "CPY abs",
0xCD: "CMP abs",
0xCE: "DEC abs",
0xCF: "DCP abs",
0xD0: "BNE branch",
0xD1: "CMP (),y",
0xD3: "DCP (),y",
0xD4: "NOP zp,x",
0xD5: "CMP zp,x",
0xD6: "DEC zp,x",
0xD7: "DCP zp,x",
0xD8: "CLD",
0xD9: "CMP abs,y",
0xDA: "NOP",
0xDB: "DCP abs,y",
0xDC: "NOP abs,x",
0xDD: "CMP abs,x",
0xDE: "DEC abs,x",
0xDF: "DCP abs,x",
0xE0: "CPX imm",
0xE1: "SBC (,x)",
0xE2: "NOP imm",
0xE3: "ISB (,x)",
0xE4: "CPX zp",
0xE5: "SBC zp",
0xE6: "INC zp",
0xE7: "ISB zp",
0xE8: "INX",
0xE9: "SBC imm",
0xEA: "NOP",
0xEB: "SBC imm",
0xEC: "CPX abs",
0xED: "SBC abs",
0xEE: "INC abs",
0xEF: "ISB abs",
0xF0: "BEQ branch",
0xF1: "SBC (),y",
0xF3: "ISB (),y",
0xF4: "NOP zpx",
0xF5: "SBC zp,x",
0xF6: "INC zp,x",
0xF7: "ISB zp,x",
0xF8: "SED",
0xF9: "SBC abs,y",
0xFA: "NOP",
0xFB: "ISB abs,y",
0xFC: "NOP abs,x",
0xFD: "SBC abs,x",
0xFE: "INC abs,x",
0xFF: "ISB abs,x"
};
var opcodes65c12 = {
0x00: "BRK",
0x01: "ORA (,x)",
0x04: "TSB zp",
0x05: "ORA zp",
0x06: "ASL zp",
0x08: "PHP",
0x09: "ORA imm",
0x0A: "ASL A",
0x0C: "TSB abs",
0x0D: "ORA abs",
0x0E: "ASL abs",
0x10: "BPL branch",
0x11: "ORA (),y",
0x12: "ORA ()",
0x14: "TRB zp",
0x15: "ORA zp,x",
0x16: "ASL zp,x",
0x18: "CLC",
0x19: "ORA abs,y",
0x1A: "INC A",
0x1C: "TRB abs",
0x1D: "ORA abs,x",
0x1E: "ASL abs,x",
0x20: "JSR abs",
0x21: "AND (,x)",
0x24: "BIT zp",
0x25: "AND zp",
0x26: "ROL zp",
0x28: "PLP",
0x29: "AND imm",
0x2A: "ROL A",
0x2C: "BIT abs",
0x2D: "AND abs",
0x2E: "ROL abs",
0x30: "BMI branch",
0x31: "AND (),y",
0x32: "AND ()",
0x34: "BIT zp,x",
0x35: "AND zp,x",
0x36: "ROL zp,x",
0x38: "SEC",
0x39: "AND abs,y",
0x3A: "DEC A",
0x3C: "BIT abs,x",
0x3D: "AND abs,x",
0x3E: "ROL abs,x",
0x40: "RTI",
0x41: "EOR (,x)",
0x45: "EOR zp",
0x46: "LSR zp",
0x48: "PHA",
0x49: "EOR imm",
0x4A: "LSR A",
0x4C: "JMP abs",
0x4D: "EOR abs",
0x4E: "LSR abs",
0x50: "BVC branch",
0x51: "EOR (),y",
0x52: "EOR ()",
0x55: "EOR zp,x",
0x56: "LSR zp,x",
0x58: "CLI",
0x59: "EOR abs,y",
0x5A: "PHY",
0x5D: "EOR abs,x",
0x5E: "LSR abs,x",
0x60: "RTS",
0x61: "ADC (,x)",
0x64: "STZ zp",
0x65: "ADC zp",
0x66: "ROR zp",
0x68: "PLA",
0x69: "ADC imm",
0x6A: "ROR A",
0x6C: "JMP (abs)",
0x6D: "ADC abs",
0x6E: "ROR abs",
0x70: "BVS branch",
0x71: "ADC (),y",
0x72: "ADC ()",
0x74: "STZ zp,x",
0x75: "ADC zp,x",
0x76: "ROR zp,x",
0x78: "SEI",
0x79: "ADC abs,y",
0x7A: "PLY",
0x7C: "JMP (abs,x)",
0x7D: "ADC abs,x",
0x7E: "ROR abs,x",
0x80: "BRA branch",
0x81: "STA (,x)",
0x84: "STY zp",
0x85: "STA zp",
0x86: "STX zp",
0x88: "DEY",
0x89: "BIT imm",
0x8A: "TXA",
0x8C: "STY abs",
0x8D: "STA abs",
0x8E: "STX abs",
0x90: "BCC branch",
0x91: "STA (),y",
0x92: "STA ()",
0x94: "STY zp,x",
0x95: "STA zp,x",
0x96: "STX zp,y",
0x98: "TYA",
0x99: "STA abs,y",
0x9A: "TXS",
0x9C: "STZ abs",
0x9D: "STA abs,x",
0x9E: "STZ abs,x",
0xA0: "LDY imm",
0xA1: "LDA (,x)",
0xA2: "LDX imm",
0xA4: "LDY zp",
0xA5: "LDA zp",
0xA6: "LDX zp",
0xA8: "TAY",
0xA9: "LDA imm",
0xAA: "TAX",
0xAC: "LDY abs",
0xAD: "LDA abs",
0xAE: "LDX abs",
0xB0: "BCS branch",
0xB1: "LDA (),y",
0xB2: "LDA ()",
0xB4: "LDY zp,x",
0xB5: "LDA zp,x",
0xB6: "LDX zp,y",
0xB8: "CLV",
0xB9: "LDA abs,y",
0xBA: "TSX",
0xBC: "LDY abs,x",
0xBD: "LDA abs,x",
0xBE: "LDX abs,y",
0xC0: "CPY imm",
0xC1: "CMP (,x)",
0xC4: "CPY zp",
0xC5: "CMP zp",
0xC6: "DEC zp",
0xC8: "INY",
0xC9: "CMP imm",
0xCA: "DEX",
//0xCB: "WAI", // was "WAI" but testing by @tom-seddon indicate this isn't a 65c12 thing
0xCC: "CPY abs",
0xCD: "CMP abs",
0xCE: "DEC abs",
0xD0: "BNE branch",
0xD1: "CMP (),y",
0xD2: "CMP ()",
0xD5: "CMP zp,x",
0xD6: "DEC zp,x",
0xD8: "CLD",
0xD9: "CMP abs,y",
0xDA: "PHX",
0xDD: "CMP abs,x",
0xDE: "DEC abs,x",
0xE0: "CPX imm",
0xE1: "SBC (,x)",
0xE4: "CPX zp",
0xE5: "SBC zp",
0xE6: "INC zp",
0xE8: "INX",
0xE9: "SBC imm",
0xEA: "NOP",
0xEC: "CPX abs",
0xED: "SBC abs",
0xEE: "INC abs",
0xF0: "BEQ branch",
0xF1: "SBC (),y",
0xF2: "SBC ()",
0xF5: "SBC zp,x",
0xF6: "INC zp,x",
0xF8: "SED",
0xF9: "SBC abs,y",
0xFA: "PLX",
0xFD: "SBC abs,x",
0xFE: "INC abs,x"
};
function makeCpuFunctions(cpu, opcodes, is65c12) {
function getInstruction(opcodeString, needsReg) {
var split = opcodeString.split(' ');
var opcode = split[0];
var arg = split[1];
var op = getOp(opcode, arg);
if (!op) return null;
var ig = new InstructionGen(is65c12);
if (needsReg) ig.append("var REG = 0|0;");
switch (arg) {
case undefined:
// Many of these ops need a little special casing.
if (op.read || op.write) throw "Unsupported " + opcodeString;
ig.append(op.preop);
ig.tick(Math.max(2, 1 + (op.extra || 0)));
ig.append(op.op);
return ig.render();
case "branch":
return [op.op]; // special cased here, would be nice to pull out of cpu
case "zp":
case "zpx": // Seems to be enough to keep tests happy, but needs investigation.
case "zp,x":
case "zp,y":
if (arg === "zp") {
ig.tick(2);
ig.append("var addr = cpu.getb() | 0;");
} else {
ig.tick(3);
ig.append("var addr = (cpu.getb() + cpu." + arg[3] + ") & 0xff;");
}
if (op.read) {
ig.zpReadOp("addr", "REG");
if (op.write) {
ig.tick(1); // Spurious write
}
}
ig.append(op.op);
if (op.write) ig.zpWriteOp("addr", "REG");
return ig.render();
case "abs":
ig.tick(3 + (op.extra || 0));
ig.append("var addr = cpu.getw() | 0;");
if (op.read) {
ig.readOp("addr", "REG");
if (op.write) ig.spuriousOp("addr", "REG");
}
ig.append(op.op);
if (op.write) ig.writeOp("addr", "REG");
return ig.render();
case "abs,x":
case "abs,y":
ig.append("var addr = cpu.getw() | 0;");
ig.append("var addrWithCarry = (addr + cpu." + arg[4] + ") & 0xffff;");
ig.append("var addrNonCarry = (addr & 0xff00) | (addrWithCarry & 0xff);");
ig.tick(3);
if ((op.read && !op.write)) {
// For non-RMW, we only pay the cost of the spurious read if the address carried.
ig = ig.split("addrWithCarry !== addrNonCarry");
ig.ifTrue.readOp("addrNonCarry");
ig.readOp("addrWithCarry", "REG");
} else if (op.read) {
if (is65c12 && op.rotate) {
// For rotates on the 65c12, there's an optimization to avoid the extra cycle with no carry
ig = ig.split("addrWithCarry !== addrNonCarry");
ig.ifTrue.readOp("addrNonCarry");
ig.readOp("addrWithCarry", "REG");
ig.writeOp("addrWithCarry", "REG");
} else {
// For RMW we always have a spurious read and then a spurious read or write
ig.readOp("addrNonCarry");
ig.readOp("addrWithCarry", "REG");
ig.spuriousOp("addrWithCarry", "REG");
}
} else if (op.write) {
// Pure stores still exhibit a read at the non-carried address.
ig.readOp("addrNonCarry");
}
ig.append(op.op);
if (op.write) ig.writeOp("addrWithCarry", "REG");
return ig.render();