-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathops.mrc
1109 lines (753 loc) · 28.5 KB
/
ops.mrc
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
;;set interrupt
alias nes.cpu.mnemonic.sei {
;; SEI is always implict
hadd nes.cpu status.interrupt 1
}
;; clear decimal flag
alias nes.cpu.mnemonic.cld {
;; CLD is always implicit
hadd nes.cpu status.decimal 0
}
;; clear carry flag
alias nes.cpu.mnemonic.clc {
;; always implicit
hadd nes.cpu status.carry 0
}
;; load accumulator
alias nes.cpu.mnemonic.lda {
var %length $1
var %mode $2
var %operand $3-
if (%mode == immediate) {
var %result %operand
}
elseif (%mode == absolute) {
var %result $nes.mem.read($mergeBytes(%operand))
}
elseif (%mode == absolute,x) {
var %address $calc($mergeBytes(%operand) + $hget(nes.cpu, x))
if (%address > $dec(ffff)) {
var %address $calc(%address - $dec(ffff))
}
var %result $nes.mem.read(%address)
}
elseif (%mode == zeropage) {
var %result $nes.mem.read(%operand)
}
elseif (%mode == indirect,y) {
;; get lower byte of address from zeropage
var %addressLower $hex($nes.mem.read(%operand))
;; get higher byte of address from consecutive zeropage address
;; if crossing zeropage address ff, loop back around.
if (%operand < 255) {
var %addressHigher $hex($nes.mem.read(%operand + 1))
}
else {
var %addressHigher $hex($nes.mem.read(0))
}
;; combine higher/lower, add y index, that is our target address.
var %address $calc($dec($+(%addressHigher,%addressLower)) + $hget(nes.cpu, y))
;; loop back to $0000 if crossing $ffff
if (%address > $hex(ffff)) {
var %address $calc(%address - $hex(ffff))
}
;; get value from address
var %result $nes.mem.read(%address)
}
;; store result in accumulator
hadd nes.cpu accumulator %result
setFlag zero $hget(nes.cpu, accumulator)
setFlag negative $hget(nes.cpu, accumulator)
return %result
}
;; store accumulator
alias nes.cpu.mnemonic.sta {
var %length $1
var %mode $2
var %operand $3-
if (%mode == absolute) {
var %address $mergeBytes(%operand)
}
elseif (%mode == indirect,y) {
;; get lower byte of address from zeropage
var %addressLower $hex($nes.mem.read(%operand))
;; get higher byte of address from consecutive zeropage address
;; if crossing zeropage address ff, loop back around.
if (%operand < 255) {
var %addressHigher $hex($nes.mem.read(%operand + 1))
}
else {
var %addressHigher $hex($nes.mem.read(0))
}
;; combine higher/lower, add y index, that is our target address.
var %address $calc($dec($+(%addressHigher,%addressLower)) + $hget(nes.cpu, y))
;; loop back to $0000 if crossing $ffff
if (%address > $hex(ffff)) {
var %address $calc(%address - $hex(ffff))
}
}
elseif (%mode == zeropage) {
var %address %operand
}
elseif (%mode == absolute,x) {
var %address $calc($mergeBytes(%operand) + $hget(nes.cpu, x))
;; handle overflow
if (%address > $dec(ffff)) {
var %address $calc(%address - $dec(ffff))
}
}
nes.mem.write %address $hget(nes.cpu, accumulator)
return %address
}
;; push accumulator to stack
alias nes.cpu.mnemonic.pha {
nes.mem.stack push $hget(nes.cpu, accumulator)
}
;; pull accu from stack
alias nes.cpu.mnemonic.pla {
hadd nes.cpu accumulator $nes.mem.stack(pop)
setFlag zero $hget(nes.cpu, accumulator)
setFlag negative $hget(nes.cpu, accumulator)
}
;; LoaD X index with memory
alias nes.cpu.mnemonic.ldx {
var %length $1
var %mode $2
var %operand $3
if (%mode == immediate) {
var %result %operand
}
elseif (%mode == absolute) {
var %address $mergeBytes(%operand)
var %result $nes.mem.read(%address)
}
;; set x register to result
hadd nes.cpu x %result
setFlag zero %result
setFlag negative %result
return %result
}
alias nes.cpu.mnemonic.stx {
var %mode $2
var %operand $3
if (%mode == absolute) {
var %address $mergeBytes(%operand)
}
nes.mem.write %address $hget(nes.cpu, x)
return %address
}
;; transfer X to accumulator
alias nes.cpu.mnemonic.txa {
var %value $hget(nes.cpu, x)
;; write contents of x to accumulator
hadd nes.cpu accumulator %value
setFlag zero $hget(nes.cpu, accumulator)
setFlag negative $hget(nes.cpu, accumulator)
}
;; transfer accumulator to X
alias nes.cpu.mnemonic.tax {
var %value $hget(nes.cpu, accumulator)
;; write contents of accumulator to x
hadd nes.cpu x %value
setFlag zero $hget(nes.cpu, accumulator)
setFlag negative $hget(nes.cpu, accumulator)
}
;;Transfer X to StackPointer
alias nes.cpu.mnemonic.txs {
;; i was so wrong about this. I thought this pushed
;; the current value of X onto the stack, but all it
;; does is set the stack pointer of whatever value is
;; in the x register. this has been wrong for weeks.
;; end me 😭
hadd nes.cpu StackPointer $hget(nes.cpu, x)
}
;;compare X to memory
alias nes.cpu.mnemonic.cpx {
var %mode $2
var %operand $3
if (%mode == immediate) {
var %value %operand
}
var %result $calc($hget(nes.cpu, x) - %value)
if (%result < 0) {
var %result $calc(%result + 255)
}
;; set zero flag on "equal" comparison (i.e., both accu and
;; operand are the same value and result is 0)
setFlag zero %result
;; negative flag is set if bit 7 is set
setFlag negative %result
;; carry is set if value is <= to accumulator,
;; reset if greater than.
;; hardcoded for now until I'm sure I can re-use
;; this code for other instructions
if (%result <= $hget(nes.cpu, x)) {
hadd nes.cpu status.carry 1
}
else {
hadd nes.cpu status.carry 0
}
}
;; load y index with memory
alias nes.cpu.mnemonic.ldy {
var %length $1
var %mode $2
var %operand $3
if (%mode == immediate) {
var %result %operand
}
;; set y register to result
hadd nes.cpu y %result
setFlag zero $hget(nes.cpu, y)
setFlag negative $hget(nes.cpu, y)
return %result
}
;; store value of y to location
alias nes.cpu.mnemonic.sty {
var %length $1
var %mode $2
var %operand $3
if (%mode == zeropage) {
;; no need to merge anything,
;; since zeropage addresses are 1 byte.
var %address %operand
}
elseif (%mode == absolute) {
var %address $mergeBytes(%operand)
}
;; store the value of y at the given address.
nes.mem.write %address $hget(nes.cpu, y)
return %address
}
;; increment y register
alias nes.cpu.mnemonic.iny {
hinc nes.cpu y
;; if y is now less than 0, roll back over to $ff
if ($hget(nes.cpu, y) > 255) {
hadd nes.cpu y 0
}
setFlag zero $hget(nes.cpu, y)
setFlag negative $hget(nes.cpu, y)
}
;; decrement y register
alias nes.cpu.mnemonic.dey {
hdec nes.cpu y
;; if y is now less than 0, roll back over to $ff
if ($hget(nes.cpu, y) < 0) {
hadd nes.cpu y $dec(ff)
}
setFlag zero $hget(nes.cpu, y)
setFlag negative $hget(nes.cpu, y)
}
;; increment x register
alias nes.cpu.mnemonic.inx {
hinc nes.cpu x
;; if x is now less than 0, roll back over to $ff
if ($hget(nes.cpu, x) > 255) {
hadd nes.cpu x 0
}
setFlag zero $hget(nes.cpu, x)
setFlag negative $hget(nes.cpu, x)
}
;; decrement x register
alias nes.cpu.mnemonic.dex {
hdec nes.cpu x
if ($hget(nes.cpu, x) < 0) {
hadd nes.cpu x $dec(ff)
}
setFlag zero $hget(nes.cpu, x)
setFlag negative $hget(nes.cpu, x)
}
;; transfer y to accumulator
alias nes.cpu.mnemonic.tya {
var %value $hget(nes.cpu, y)
hadd nes.cpu accumulator %value
setFlag zero $hget(nes.cpu, y)
setFlag negative $hget(nes.cpu, y)
}
alias nes.cpu.mnemonic.sty {
var %mode $2
var %operand $3
if (%mode == absolute) {
var %address $mergeBytes(%operand)
}
nes.mem.write %address $hget(nes.cpu, y)
return %address
}
;; Logical AND memory with accumulator
alias nes.cpu.mnemonic.and {
var %length $1
var %mode $2
var %operand $3
var %accumulator $hget(nes.cpu, accumulator)
if (%mode == immediate) {
var %value %operand
}
elseif (%mode == zeropage) {
;; get value from zeropage
var %value $nes.mem.read(%operand)
}
;; logical AND between operand and accumulator
;; 6502.org says bit by bit, so... let's try that?
var %i 0
while (%i < 8) {
var %and $and($getBit(%value, %i), $getBit(%accumulator, %i))
;echo -s > %a AND %b = %and
var %result $+(%and,%result)
inc %i
}
;; not entirely convinced the above does anything different
;; than just straight using $and() on the operand/accu,
;; so uncomment this line to double check in the future.
;echo -s . %operand AND %accumulator = $and(%operand, %accumulator)
;; convert result back to decimal
;echo -s b: %result d: $dec(%result).bin h: $hex($dec(%result).bin)
var %result $dec(%result).bin
;; push the result to the accumulator
hadd nes.cpu accumulator %result
setFlag zero $hget(nes.cpu, accumulator)
setFlag negative $hget(nes.cpu, accumulator)
}
;; compare contents of accumulator with another value
alias nes.cpu.mnemonic.cmp {
var %mode $2
var %operand $3
if (%mode == immediate) {
var %value %operand
}
var %result $calc($hget(nes.cpu, accumulator) - %value)
;; set zero flag on "equal" comparison (i.e., both accu and
;; operand are the same value and result is 0)
setFlag zero %result
;; negative flag is set if bit 7 is set
setFlag negative %result
;; carry is set if value is <= to accumulator,
;; reset if greater than.
;; hardcoded for now until I'm sure I can re-use
;; this code for other instructions
if (%result <= $hget(nes.cpu, accumulator)) {
hadd nes.cpu status.carry 1
}
else {
hadd nes.cpu status.carry 0
}
}
;; add with carry
alias nes.cpu.mnemonic.adc {
var %mode $2
var %operand $3-
var %accumulator $hget(nes.cpu, accumulator)
if (%mode == zeropage) {
var %value $nes.mem.read(%operand)
}
;; i'm so incredibly grateful for everyone who has patiently
;; helped me and explained stuff ❤ special shoutout to
;; TheMogMiner for this one:
;; add accumulator + value... plus the carry bit? somehow?
;; ...maybe i don't understand just yet...
var %value $calc(%value + $hget(nes.cpu, status.carry))
var %result $calc(%accumulator + %value)
;; if >255, loop around, and set the carry flag
if (%result > 255) {
;; loop result around
var %result $calc(%result - 255)
;; set carry flag
hadd nes.cpu status.carry 1
}
else {
;; always gotta unset things if any condition
;; that sets a flag isn't met.
hadd nes.cpu status.carry 0
}
;; compare bit 7 of accu and value.
if ($getBit(%accumulator, 7) == $getBit(%value, 7)) {
;; if identical, we check if bit 7 of the result is different
if ($getBit(%accumulator, 7) != $getBit(%result, 7)) {
;; set overflow flag if bit 7 of accu and result are different
hadd nes.cpu status.overflow 1
}
else {
hadd nes.cpu status.overflow 0
}
}
else {
hadd nes.cpu status.overflow 0
}
setFlag negative
}
;; ROtate Right
alias nes.cpu.mnemonic.ror {
var %mode $2
if (%mode == accumulator) {
var %value $hget(nes.cpu, accumulator)
}
;; get bit 0
var %bit0 $getbit(%value, 0)
;; get carry
var %carry $hget(nes.cpu, status.carry)
;; result is all bits shifted 1 place right,
;; bit 7 is set to current value of carry.
var %result $dec($+(%carry,$left(%value, 7))).bin
;; set N flag to "input carry". like this?
hadd nes.cpu status.negative $hget(nes.cpu, status.carry)
;; set carry to previous value of bit 0
hadd nes.cpu status.carry %bit0
if (%result == 0) {
hadd nes.cpu status.zero 1
}
else {
hadd nes.cpu status.zero 0
}
}
;; logical shift right
alias nes.cpu.mnemonic.lsr {
var %mode $2
var %operand $3-
;; lowest bit is shifted into the carry flag,
;; highest bit is set to 0
if (%mode == accumulator) {
var %value $bin($hget(nes.cpu, accumulator))
}
;; get the bit to shift into the carry flag
var %carry $right($bin(%value), 1)
;; get leftmost 7 bits (strip lowest bit),
;; and stick 0 in front of it.
;; yeaaaaaaaah
var %result $dec($+(0,$left(%value, 7))).bin
;; i can't think of a better way to do this right now
if (%mode == accumulator) {
hadd nes.cpu accumulator %result
}
;; set status flags
setFlag zero %result
;; negative flag is always set to 0
hadd nes.cpu status.negative 0
hadd nes.cpu status.carry %carry
}
alias nes.cpu.mnemonic.asl {
var %mode $2
var %operand $3
if (%mode == zeropage) {
var %address %operand
var %value $nes.mem.read(%address)
}
if (%mode == accumulator) {
var %value $hget(nes.cpu, accumulator)
}
var %bit7 $left($bin(%value), 1)
var %result $dec($+($right(%value, 7),0)).bin
hadd nes.cpu status.carry %bit7
hadd nes.cpu status.negative %bit7
setFlag zero %result
}
;; branch if equal
alias nes.cpu.mnemonic.beq {
;; wrong. it's wrong. it's all wrong.
;; aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
;; *stabs brain with fork*
;; mode is always relative
var %operand $3
;; i've been doing this completely wrong. oops.
;; look at https://twitter.com/kebby/status/1658532782803410946
;; and https://www.cs.cornell.edu/~tomf/notes/cps104/twoscomp.html
;; signed bytes use the most significant bit (7) as the sign
;; if 0, it's a negative number, and if 1, positive.
;; we'll prepare for some mIRC interpreter abuse later
;; by setting %sign to either - or + depending on the result,
;; which we can then just feed into $calc(), which will
;; happily be interpreted as the correct mathmetical operator.
var %sign $iif($getBit(%operand, 7) == 1, -, +)
;; two's complement
;; we'll make the value binary
var %value $bin(%operand)
;; invert the bits
var %value $invert(%value)
;; and add 1 to get the result
var %value $calc($dec(%value).bin + 1)
;; here comes the interpreter abuse! since variables and everything
;; are evaluated first, %sign will just become - or +, and $calc()
;; will just accept this and perform our desired operation.
var %result $calc($hget(nes.cpu, programCounter) %sign %value)
;; branch only if the Zero flag of the status register is 1
if ($hget(nes.cpu, status.zero) == 1) {
hadd nes.cpu programCounter %result
}
;; add 1 to the output result for display purposes.
;; the actually calculated value is correct, setting the program
;; counter 1 before the desired branch address, which it will
;; be set to as soon as the CPU loop restarts
return $calc(%result + 1)
}
;; branch if not equal
alias nes.cpu.mnemonic.bne {
;; most of this is like `beq` above.
;; mode is always relative
var %operand $3
;; get sign to determine branching forward or backward
var %sign $iif($getBit(%operand, 7) == 1, -, +)
;; two's complement, condensed!
;var %value $calc($dec($invert($bin(%operand))).bin + 1)
;; above line used $invert, which is a function i wrote (find it lower
;; in this file...) -- it seemed a bit silly though, but...
;; $not returns a 32-bit value, i don't know how to cull it
;; apart from converting from decimal to binary, then keeping
;; only the rightmost 8 bits and converting that back to decimal...
;; so this isn't really much better at all.
var %value $calc($base($right($base($not(%operand), 10, 2), 8), 2, 10) + 1)
;; interpreter abuse, as above
var %result $calc($hget(nes.cpu, programCounter) %sign %value)
if ($hget(nes.cpu, status.zero) == 0) {
hadd nes.cpu programCounter %result
}
;; add 1 to the output result for display purposes.
;; the actually calculated value is correct, setting the program
;; counter 1 before the desired branch address, which it will
;; be set to as soon as the CPU loop restarts
return $calc(%result + 1)
}
;; branch if carry set
alias nes.cpu.mnemonic.bcs {
var %operand $3
;; get sign to determine branching forward or backward
var %sign $iif($getBit(%operand, 7) == 1, -, +)
;; two's complement, condensed!
var %value $calc($dec($invert($bin(%operand))).bin + 1)
;; interpreter abuse, as above
var %result $calc($hget(nes.cpu, programCounter) %sign %value)
if ($hget(nes.cpu, status.carry) == 1) {
hadd nes.cpu programCounter %result
}
;; add 1 to the output result for display purposes.
;; the actually calculated value is correct, setting the program
;; counter 1 before the desired branch address, which it will
;; be set to as soon as the CPU loop restarts
return $calc(%result + 1)
}
;; branch on carry clear
alias nes.cpu.mnemonic.bcc {
var %operand $3
;; get sign to determine branching forward or backward
var %sign $iif($getBit(%operand, 7) == 1, -, +)
;; two's complement, condensed!
var %value $calc($dec($invert($bin(%operand))).bin + 1)
;; interpreter abuse, as above
var %result $calc($hget(nes.cpu, programCounter) %sign %value)
if ($hget(nes.cpu, status.carry) == 0) {
hadd nes.cpu programCounter %result
}
;; add 1 to the output result for display purposes.
;; the actually calculated value is correct, setting the program
;; counter 1 before the desired branch address, which it will
;; be set to as soon as the CPU loop restarts
return $calc(%result + 1)
}
;; branch if positive
alias nes.cpu.mnemonic.bpl {
;; mode is always relative
var %operand $3
;; get sign to determine branching forward or backward
var %sign $iif($getBit(%operand, 7) == 1, -, +)
;; two's complement, condensed!
var %value $calc($dec($invert($bin(%operand))).bin + 1)
;; interpreter abuse, as above
var %result $calc($hget(nes.cpu, programCounter) %sign %value)
if ($hget(nes.cpu, status.negative) == 0) {
hadd nes.cpu programCounter %result
}
return %result
}
alias nes.cpu.mnemonic.jmp {
var %mode $2
var %operand $3-
if (%mode == absolute) {
;; subtract 1 from target address so we actually end up
;; in the right place...
var %address $calc($mergeBytes(%operand) - 1)
}
hadd nes.cpu programCounter %address
return %address
}
;; Jump to SubRoutine
alias nes.cpu.mnemonic.jsr {
echo -s 56 JSR
;; mode is always absolute
var %operand $3-
var %address $calc($mergeBytes(%operand) - 1)
;; calculate the return point.
;; this should be the current value of the program counter, minus a few.
;; Everywhere I'm reading it says it should be -1, but I'm having doubts.
;; the program counter, at this point, is increased by operand length,
;; mainly because that's what various sources led me to believe.
;; so if we subtract 1, we would point back to the 2nd byte of the
;; operand... which... actually makes sense, because next cycle we
;; increment the program counter again and end up at the next instruction!
;; ...thanks for listening, sometimes you just gotta talk through
;; a problem to figure it out ^-^
var %returnAddress $base($calc($hget(nes.cpu, programCounter) - 1), 10, 16, 4)
;; split into upper / lower byte
var %upper $dec($left(%returnAddress, 2))
var %lower $dec($right(%returnAddress, 2))
;; now we push this to the stack.
echo -s . nes.mem.stack push %upper
nes.mem.stack push %upper
echo -s . nes.mem.stack push %upper
nes.mem.stack push %lower
echo -s . jsr return: %returnAddress
;; and now we set the program counter to our target address!
;; we do gotta subtract... no, add?? 1 tho. stubid off by one errors...
hadd nes.cpu programCounter %address
return %address
}
;; ReTurn from Subroutine
alias nes.cpu.mnemonic.rts {
echo -s 52 RTS
;; get topmost 2 values from the stack, that is the return address.
;; i love how incredibly cursed this is. if you performed an odd
;; number of push/pop operations in between a jsr and rts, you have
;; essentially modified the return address, which means you could
;; theoretically abuse this to jump anywhere in memory.
;; ...why you would do that over just a plain jmp, i don't know.
var %lower $hex($nes.mem.stack(pop))
echo -s . lower: %lower
var %upper $hex($nes.mem.stack(pop))
echo -s . upper: %upper
var %returnAddress $dec($+(%upper,%lower))
echo -s . RTS return: $hex(%returnAddress)
hadd nes.cpu programCounter %returnAddress
}
alias nes.cpu.mnemonic.brk {
;; mode is always implicit
;; increase program counter by 2(?)
hinc nes.cpu programCounter 2
;; get pc in hex padded to 4 digits
var %pc $base($hget(nes.cpu, programCounter), 10, 16, 4)
;; split into upper and lower byte
var %upper $dec($left(%pc, 2))
var %lower $dec($right(%pc, 2))
;; push to stack
nes.mem.stack push %upper
nes.mem.stack push %lower
;; push processor status onto the stack...
nes.mem.stack push $dec($nes.cpu.statusFlags).bin
;; "the cpu transfers control to the interrupt vector"
;; what does this mean?
; get IRQ interrupt vector at $FFFE-$FFFF
;var %lower $hex($nes.mem.read($dec(FFFE)))
;var %upper $hex($nes.mem.read($dec(FFFF)))
;var %address $+(%upper,%lower)
;hadd nes.cpu programCounter $dec(%address)
;hadd nes.cpu status.break 1
}
alias nes.cpu.mnemonic.inc {
var %mode $2
var %operand $3
if (%mode == zeropage) {
var %address %operand
var %result $calc($nes.mem.read(%address) + 1)
if (%result > 255) {
var %result 0
}
nes.mem.write %address %result
}
setFlag zero %result
setFlag negative %result
return %result
}
alias nes.cpu.mnemonic.dec {
var %mode $2
var %operand $3
if (%mode == zeropage) {
var %address %operand
var %result $calc($nes.mem.read(%address) - 1)
if (%result < 0) {
var %result 255
}
nes.mem.write %address %result
}
setFlag zero %result
setFlag negative %result
return %result
}
alias nes.cpu.mnemonic.ora {
var %mode $2
var %operand $3
if (%mode == immediate) {
var %value %operand
}
;; binary OR
var %accumulator $bin($hget(nes.cpu, accumulator))