-
Notifications
You must be signed in to change notification settings - Fork 1
/
chip8.asm
2479 lines (2235 loc) · 50.4 KB
/
chip8.asm
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
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; CHIP-8 EMULATOR
;;; WRITTEN FOR THE SNES BY Ersanio
;;; https://twitter.com/Ersanio
;;; https://github.com/Ersanio/Super-Chip8x/
;;;
;;; Extra thanks goes to:
;;; - p4plus2 (https://github.com/p4plus2) for pointing me in
;;; the right direction whenever I got stuck
;;; - Lui37 (https://github.com/Lui37) for hardware testing and
;;; helping a bit with audio
;;; - Ladida (https://github.com/Matsurika) for hardware testing and
;;; helping with controller input
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
LOROM
incsrc const.asm
ORG $808000
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; INITIALIZE THE SNES
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
RESET:
SEI
; Exit emulation mode
CLC
XCE
; Clear interrupts, DMA, HDMA
STZ $4200
STZ $420B
STZ $420C
; Jump to FastROM area. Works because ORG specifies bank $80
JML +
+ REP #$20
; Set stack pointer to $00:0FFF
LDA #$0FFF
TCS
; Set data bank to $80 (FastROM area)
PHK
PLB
; Set direct page register to $0000
.zero PEA $0000
PLD
; Enable FastROM through hardware register
LDX #$01
STX $420D
SEP #$20
SEI
LDA.b #SPC
STA $00
LDA.b #SPC>>8
STA $01
LDA.b #SPC>>16
STA $02
JSR UploadDataToSPC
REP #$10
; Clear RAM $7E:0000-$7F:FFFF
LDX #$8008
STX $4300
LDX.w #.zero+1
STX $4302
LDA.b #.zero+1>>16
STA $4304
LDX #$0000
STX $4305
STX $2181
STZ $2183
LDA #$01
STA $420B
LDA #$01
STA $2183
STA $420B
SEP #$10
; Initialize every single hardware register ($21xx & $42xx)
PHD
PEA $2100
PLD
LDA #$80
STA $00
STZ $01
STZ $02
STZ $03
STZ $04
STZ $05
STZ $06
STZ $07
STZ $08
STZ $09
STZ $0A
STZ $0B
STZ $0C
STZ $0D
STZ $0D
STZ $0E
STZ $0E
STZ $0F
STZ $0F
STZ $10
STZ $10
STZ $11
STZ $11
STZ $12
STZ $12
STZ $13
STZ $13
STZ $14
STZ $14
STZ $15
STZ $16
STZ $17
STZ $18
STZ $19
STZ $1A
STZ $1B
STZ $1B
STZ $1C
STZ $1C
STZ $1D
STZ $1D
STZ $1E
STZ $1E
STZ $1F
STZ $1F
STZ $20
STZ $20
STZ $21
STZ $22
STZ $22
STZ $23
STZ $24
STZ $25
STZ $26
STZ $27
STZ $28
STZ $29
STZ $2A
STZ $2B
STZ $2C
STZ $2D
STZ $2E
STZ $2F
STZ $30
STZ $31
LDA #$80
STA $32
LDA #$40
STA $32
LDA #$20
STA $32
STZ $33
STZ $40
STZ $41
STZ $42
STZ $43
PEA $4200
PLD
STZ $01
STZ $02
STZ $03
STZ $04
STZ $05
STZ $06
STZ $07
STZ $08
STZ $09
STZ $0A
STZ $0B
STZ $0C
PLD
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; INITIALIZE THE SCREEN
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
LDA #$07 ; Mode 7
STA $2105 ;
LDA #$01 ; BG1 main screen
STA $212C
STZ $212D ; No subscreen
LDA #$80
STA $211A
JSR ClearVRAM
JSR InitTilemapRAM
JSR UploadPalette
;Color math designation on backdrop
LDA #$20
STA $2131
; NMI and auto-read joypad enable
LDA #$81
STA $4200
LDA #$08
STA !Mode7ScaleX
STA !Mode7ScaleY
REP #$20
LDA #$FFCF ;vertically center
STA !BG1Y
STZ !BG1X
LDA #$0080
STA !Mode7CenterX
STA !Mode7CenterY
SEP #$20
JSR ProcessMode7ScalingAndRotation
JSR ScreenHDMA
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; INITIALIZE THE EMULATOR
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
JSR ResetEmu
JSR LoadROM
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; START THE EMULATOR
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
STZ.b !ROMnumber
;Enable screen
LDA #$0F
STA $2100
EmuLoop:
WAI
LDA !Wait
BEQ EmuLoop
JSR ProcessEmuROMSwitching
LDA !OpcodesPerFrame
STA !OpcodeLoop
- JSR TickEmu
DEC !OpcodeLoop
BNE -
JSR DecreaseTimers
STZ.b !Wait
BRA EmuLoop
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; SPC-700 UPLOAD CODE
;;; Borrowed from "Super Mario World" (SNES)
;;; Disassembly from "SMW IRQ" bank_00.asm:
;;; https://github.com/Alcaro/smw-irq/
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
UploadDataToSPC:
SEI
JSR UploadSPC700Data
CLI
RTS
UploadSPC700Data:
PHP ;$008079 \ Preserve processor flags
REP #$30 ;$00807A | 16 bit A/X/Y
LDY.w #$0000 ;$00807C |
LDA.w #$BBAA ;$00807F |\ Value to check if the SPC is ready
.SPC_wait ; | |
CMP.w $2140 ;$008082 | | Wait for the SPC to be ready
BNE .SPC_wait ;$008085 |/
SEP #$20 ;$008087 | 8 bit A
LDA.b #$CC ;$008089 |\ Byte used to enable SPC block upload
BRA send_SPC_block ;$00808B |/
SPC_transfer_bytes: ; |\
LDA [$00],Y ;$00808D | | Load the Byte into the low byte
INY ;$00808F | | Increase the index
XBA ;$008090 | | Move it to the high byte
LDA.b #$00 ;$008091 |/ Set the validation byte to the low byte
BRA start_block_upload ;$008093 |
next_byte: ; |\
XBA ;$008095 | | Switch the high and low byte
LDA [$00],Y ;$008096 | | Load a new low byte
INY ;$008098 | | Increase the index
XBA ;$008099 |/ Switch the new low byte to the high byte
.SPC_wait ; |\ SPC wait loop
CMP.w $2140 ;$00809A | | Wait till $2140 matches the validation byte
BNE .SPC_wait ;$00809D |/
INC A ;$00809F | Increment the validation byte
start_block_upload: ; |\
REP #$20 ;$0080A0 | | 16 bit A
STA.w $2140 ;$0080A2 | | Store to $2140/$2141
SEP #$20 ;$0080A5 | | 8 bit A
DEX ;$0080A7 |/ Decrement byte counter
BNE next_byte ;$0080A8 |
.SPC_wait ; |\ SPC wait loop
CMP.w $2140 ;$0080AA | |
BNE .SPC_wait ;$0080AD |/
.add_three ; |\
ADC.b #$03 ;$0080AD | | If A is 0 add 3 again
BEQ .add_three ;$0080B1 |/
send_SPC_block: ; |
PHA ;$0080B3 | Preserve A to store to $2140 later
REP #$20 ;$0080B4 | 16 bit A
LDA [$00],Y ;$0080B6 |\ Get data length
INY ;$0080B8 | |
INY ;$0080B9 | |
TAX ;$0080BA |/
LDA [$00],Y ;$0080BB |\ Get address to write to in SPC RAM
INY ;$0080BD | |
INY ;$0080BE |/
STA.w $2142 ;$0080BF | Store the address of SPC RAM to write to
SEP #$20 ;$0080C2 | 8 bit A
CPX.w #$0001 ;$0080C4 |
LDA.b #$00 ;$0080C7 |\ Store the carry flag in $2141
ROL ;$0080C9 | |
STA.w $2141 ;$0080CA |/
ADC.b #$7F ;$0080CD | if A is one this sets the overflow flag
PLA ;$0080CF |\ Store the A pushed earlier
STA.w $2140 ;$0080D0 |/
.SPC_wait ; |\ SPC wait loop
CMP.w $2140 ;$0080D3 | |
BNE .SPC_wait ;$0080D6 |/
BVS SPC_transfer_bytes ;$0080D8 | If the overflow is not set, keep uploading
STZ.w $2140 ;$0080DA |\ Clear SPC I/O ports
STZ.w $2141 ;$0080DD | |
STZ.w $2142 ;$0080E0 | |
STZ.w $2143 ;$0080E3 |/
PLP ;$0080E6 | Restore processor flag
RTS ;$0080E7 /
incsrc spc700.asm
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; EMULATOR CODE
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; Decrease global timers
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
DecreaseTimers:
INC.b !FrameCounter
LDA.b !DelayTimer
BEQ +
DEC.b !DelayTimer
+ LDA.b !SoundTimer
BEQ +
DEC.b !SoundTimer
+ LDA.b !ROMSwitchDelay
BEQ +
DEC.b !ROMSwitchDelay
+ RTS
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; HDMA to paint the Chip-8 region different in a different
;;; color
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
ScreenHDMA:
PHP
REP #$20
LDY #$03
STY $4350
LDY #$21
STY $4351
LDA.w #.Table
STA $4352
LDY.b #.Table>>16
STY $4354
SEP #$20
LDA #$20
TSB !HDMA
PLP
RTS
.Table
db $30 : dw $0000,!BlackBarsColor
db $80 : dw $0000,!BackgroundColor
db $30 : dw $0000,!BlackBarsColor
db $00
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; Processes back and forth switching of the ROMs
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
ProcessEmuROMSwitching:
LDA !ROMSwitchDelay
BNE +
LDA !ControllerData2
BIT #$20
BNE .previous
BIT #$10
BNE .next
+ RTS
.previous
LDA !ROMnumber
BNE .notunderflowing
LDA.b #ROMS_END-ROMS/5-1
STA !ROMnumber
JSR .reset
RTS
.notunderflowing
DEC !ROMnumber
JSR .reset
RTS
.next
LDA !ROMnumber
CMP.b #ROMS_END-ROMS/5-1
BNE .notoverflowing
STZ !ROMnumber
JSR .reset
RTS
.notoverflowing
INC !ROMnumber
JSR .reset
RTS
.reset
LDA #$20
STA !ROMSwitchDelay
JSR ResetEmu
JSR LoadROM
RTS
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; (Re)initialize the emulator and all its variables
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
ResetEmu:
JSR ClearBuffer
JSR ClearChip8Memory
JSR LoadCHRROM
REP #$20
LDA #$0200
STA !ProgramCounter
STZ.b !Stack
STZ.b !Stack+2
STZ.b !Stack+4
STZ.b !Stack+6
STZ.b !Stack+8
STZ.b !Stack+10
STZ.b !Stack+12
STZ.b !Stack+14
STZ.b !Vreg
STZ.b !Vreg+2
STZ.b !Vreg+4
STZ.b !Vreg+6
STZ.b !Vreg+8
STZ.b !Vreg+10
STZ.b !Vreg+12
STZ.b !Vreg+14
STZ.b !Vreg+16
STZ.b !Ireg
STZ.b !NNN
STZ.b !NN
STZ.b !N
STZ.b !X
STZ.b !Y
SEP #$20
LDA #$1E
STA !StackPointer
STZ.b !DelayTimer
STZ.b !SoundTimer
STZ.b !DrawOffsetX
STZ.b !DrawOffsetY
STZ.b !PressedKey
STZ.b !IsPressingKey
RTS
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; Actual emulator code which handles opcode interpretation
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
TickEmu:
REP #$30
LDX.b !ProgramCounter
LDA.w !Chip8Memory,x
XBA
STA.b !Opcode
INX
INX
STX.w !ProgramCounter
SEP #$10
AND #$0FFF
STA.b !NNN
SEP #$20
AND #$FF
STA.b !NN
AND #$0F
STA.b !N
REP #$20
LDA !Opcode
AND #$0F00
XBA ; lsr 8
TAY
STY.b !X
LDA.b !Opcode
AND #$00F0
REP 4 : LSR A
TAY
STY.b !Y
SEP #$20
LDA !Opcode+1
AND #$F0
LSR A
LSR A
LSR A
TAX
JMP (Opcodes,x)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; OPCODES
;;; This block handles opcodes $0-$F.
;;; Opcodes $8 and $F are handled in yet another table as
;;; they contain multiple other opcodes
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
Opcodes:
dw ClearOrReturn ; $00
dw Jump ; $01
dw CallSubroutine ; $02
dw SkipIfXEqual ; $03
dw SkipIfXNotEqual ; $04
dw SkipIfXEqualY ; $05
dw SetX ; $06
dw AddX ; $07
dw Arithmetic ; $08
dw SkipIfXNotEqualY ; $09
dw SetI ; $0A
dw JumpWithOffset ; $0B
dw Rnd ; $0C
dw DrawSprite ; $0D
dw SkipOnKey ; $0E
dw Misc ; $0F
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; Clear or Return
;;; Either clears the screen
;;; or returns from a subroutine
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
ClearOrReturn:
LDA.b !NN
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; Clear the screen
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
CMP #$E0
BNE +
JSR ClearBuffer
LDA #$01
STA.b !UpdateGFX
RTS
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; Return from CALL
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
+ CMP #$EE
BNE +
JSR PopAndRET
+ RTS
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; Jump: Jumps to a subroutine
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
Jump:
REP #$20
LDA.b !NNN
STA.b !ProgramCounter
SEP #$20
RTS
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; Call: Jumps to a subroutine, pushes return address
;;; to the stack
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
CallSubroutine:
JSR PushPC
REP #$20
LDA.b !NNN
STA.b !ProgramCounter
SEP #$20
RTS
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; Skip if X equal - Skip next opcode if V[X] == NN
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
SkipIfXEqual:
LDX.b !X
LDA.b !Vreg,x
CMP !NN
BNE +
REP #$20
LDA.b !ProgramCounter
INC
INC
STA.b !ProgramCounter
SEP #$20
+ RTS
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; Skip if X not equal - Skip next opcode if V[X] != NN
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
SkipIfXNotEqual:
LDX.b !X
LDA.b !Vreg,x
CMP !NN
BEQ +
REP #$20
INC.b !ProgramCounter
INC.b !ProgramCounter
SEP #$20
+ RTS
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; Skip if X equals Y - Skip next opcode if V[X] == V[Y]
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
SkipIfXEqualY:
LDX.b !X
LDY.b !Y
LDA.b !Vreg,x
CMP.w !Vreg,y
BNE +
REP #$20
INC.b !ProgramCounter
INC.b !ProgramCounter
SEP #$20
+ RTS
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; Set X: V[X] = NN
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
SetX:
LDX.b !X
LDA.b !NN
STA.b !Vreg,x
RTS
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; Add X: V[X] += NN
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
AddX:
LDX.b !X
LDA.b !NN
CLC
ADC.b !Vreg,x
STA.b !Vreg,x
RTS
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; Arithmetic and bitwise operations
;;; This opcode contains multiple opcodes, depending on N
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
Arithmetic:
LDA.b !N
ASL A
TAX
JSR (.JumpTable,x)
RTS
.JumpTable
dw LD_XY ; $0 - X = Y
dw OR_XY ; $1 - X |= Y
dw AND_XY ; $2 - X &= Y
dw XOR_XY ; $3 - X ^= Y
dw ADD_XY ; $4 - X += Y
dw SUB_XY ; $5 - X -= Y
dw SHR_X ; $6 - X = X>>2
dw SUBN_YX ; $7 - X = Y-X (opposite of $5 basically)
dw dummy ; $8 - Unused
dw dummy ; $9 - Unused
dw dummy ; $A - Unused
dw dummy ; $B - Unused
dw dummy ; $C - Unused
dw dummy ; $D - Unused
dw SHL_X ; $E - X = X<<2
dw dummy ; $F - Unused
dummy:
RTS
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; V[X] = V[Y]
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
LD_XY:
LDX.b !Y
LDA.b !Vreg,x
LDX.b !X
STA.b !Vreg,x
RTS
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; V[X] |= V[Y]
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
OR_XY:
LDX.b !X
LDY.b !Y
LDA.b !Vreg,x
ORA.w !Vreg,y
STA.b !Vreg,x
RTS
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; V[X] &= V[Y]
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
AND_XY:
LDX.b !X
LDY.b !Y
LDA.b !Vreg,x
AND.w !Vreg,y
STA.b !Vreg,x
RTS
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; V[X] ^= V[Y]
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
XOR_XY:
LDX.b !X
LDY.b !Y
LDA.b !Vreg,x
EOR.w !Vreg,y
STA.b !Vreg,x
RTS
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; V[X] += V[Y]
;;; V[0xF] = Carry
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
ADD_XY:
LDX.b !X
LDY.b !Y
LDA.b !Vreg,x
CLC
ADC.w !Vreg,y
STA.b !Vreg,x
JSR SetVFCarry
RTS
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; V[X] -= V[Y]
;;; V[0xF] = Carry
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
SUB_XY:
LDX.b !X
LDY.b !Y
LDA.b !Vreg,x
SEC
SBC.w !Vreg,y
STA.b !Vreg,x
JSR SetVFCarry
RTS
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; V[X] = V[X] >> 1
;;; V[0xF] = Carry
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
SHR_X:
LDX.b !X
LDA.b !Vreg,x
LSR A
STA.b !Vreg,x
JSR SetVFCarry
RTS
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; V[X] = V[Y] - V[X]
;;; V[0xF] = Carry
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
SUBN_YX:
LDX.b !X
LDY.b !Y
LDA.w !Vreg,y
SEC
SBC.b !Vreg,x
STA.w !Vreg,x
JSR SetVFCarry
RTS
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; V[X] = V[X] << 1
;;; V[0xF] = Carry
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
SHL_X:
LDX.b !X
LDA.b !Vreg,x
ASL A
STA.b !Vreg,x
JSR SetVFCarry
RTS
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; Set V[0xF] = Carry subroutine
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
SetVFCarry:
LDA #$00
ROL A
STA !Vreg+$0F
RTS
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; End Arithmetic block
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; Skip if X doesn't equal Y - Skip next opcode if V[X] != V[Y]
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
SkipIfXNotEqualY:
LDX.b !X
LDY.b !Y
LDA.b !Vreg,x
CMP.w !Vreg,y
BEQ +
REP #$20
LDA.b !ProgramCounter
INC
INC
STA.b !ProgramCounter
SEP #$20
+ RTS
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; Set I: Set I register
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
SetI:
REP #$20
LDA.b !NNN
STA.b !Ireg
SEP #$20
RTS
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; Jump with Offset: Jump to NNN+V[0]
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
JumpWithOffset:
LDA #$00
XBA
LDA.b !Vreg
REP #$20
CLC
ADC.b !NNN
STA.b !ProgramCounter
SEP #$20
RTS
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; Random: Generate random number, AND with NN
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
Rnd:
LDX.b !X
JSR GetRand
LDA.b !RNGOutput
AND.b !NN
STA.b !Vreg,x
RTS
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; Draw: Draws a sprite on-screen by XORing pixels
;;; This allows for 'undrawing' a sprite by drawing sprite B over sprite A
;;; Sprite is 8 pixels wide
;;; Sprite is N pixels high (max 15)
;;; Each line of pixels is exactly 1 byte
;;; Screen X pos is V[X]
;;; Screen Y pos is V[Y]
;;; Set V[0xF] if there is a collision
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
DrawSprite:
STZ.b !Vreg+$F
STZ.b !DrawOffsetY
LDY #$00
-- REP #$30
TYA
CLC
ADC.b !Ireg
TAX
SEP #$20
LDA !Chip8Memory,x
SEP #$10
STZ.b !DrawOffsetX
LDX #$00 ;bits
- PHA
AND .bits,x
BEQ +
PHX
LDX.b !X
LDA.b !Vreg,x
CLC
ADC.b !DrawOffsetX
STA $00
LDX.b !Y
LDA.b !Vreg,x
CLC
ADC.b !DrawOffsetY
STA $01
JSR DrawPixel
PLX
+ PLA
INC.b !DrawOffsetX
INX
CPX #$08
BNE -
INC.b !DrawOffsetY
INY
CPY.b !N
BNE --
LDA #$01
STA.b !UpdateGFX
RTS
.bits
db $80,$40,$20,$10,$08,$04,$02,$01
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; Skip on Key: Skips the next instruction based on the key at V[x] being pressed/not pressed.
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
SkipOnKey:
JSR TranslateButtonsToKeypresses
LDA.b !NN
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; Skip if key X pressed
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
CMP #$9E
BNE +
LDA.b !IsPressingKey
BEQ .notpressinganything
LDA.b !PressedKey
LDX.b !X
CMP.b !Vreg,x
BNE .notthesame
REP #$20
INC.b !ProgramCounter
INC.b !ProgramCounter
SEP #$20
.notthesame
.notpressinganything
RTS
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; Skip if key X not pressed
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
+ CMP #$A1
BNE +
LDA.b !PressedKey
CMP #$00
BEQ .iszeropressed
.zeroispressedafterall
LDA.b !PressedKey
LDX.b !X
CMP.b !Vreg,x
BEQ .pressed