-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchip8.system.s
More file actions
2245 lines (1855 loc) · 55.2 KB
/
chip8.system.s
File metadata and controls
2245 lines (1855 loc) · 55.2 KB
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 Interpreter
;;; ============================================================
.feature line_continuations +
.setcpu "6502"
.include "apple2.inc"
.include "opcodes.inc"
.include "prodos.inc"
.include "longbranch.mac"
.org $2000
;;; TODO:
;;; * Test on Mac IIe Card
;;; ============================================================
;;; Equates
SPKR := $C030
CLR80VID := $C00C
SET80VID := $C00D
CLR80STORE := $C000 ; in CC65 as `CLR80COL` (confusing!)
SET80STORE := $C001 ; in CC65 as `SET80COL` (confusing!)
INIT := $FB2F
VERSION := $FBB3 ; Monitor ROM ID byte. $06 = IIe or later
ZIDBYTE := $FBC0 ; * $EA = IIe, $E0 = IIe enh/IIgs, $00 = IIc/IIc+
BELL1 := $FBDD
HOME := $FC58
CROUT := $FD8E
COUT := $FDED
PRBYTE := $FDDA
IDROUTINE := $FE1F ; RTS ($60) on pre-IIgs, clears carry on IIgs
SETVID := $FE93
SETNORM := $FE84
SETKBD := $FE89
;;; ============================================================
;;; Memory map
;;; Main Aux
;;; : : : :
;;; | | | |
;;; | | | (unused) |
;;; $5300 +-------------+ | |
;;; | Bitmap | | |
;;; $5200 +-------------+ | |
;;; | CHIP-8 | | |
;;; | Stack | | |
;;; $5000 +-------------+ | |
;;; | | | |
;;; | | | |
;;; | CHIP-8 | | |
;;; | Memory | | |
;;; $4000 +-------------+ | |
;;; | | | |
;;; | | | |
;;; | | | |
;;; | Interpreter | | |
;;; $2000 +-------------+ | |
;;; | | | |
;;; | | | |
;;; | | | |
;;; | I/O | | |
;;; $800 +-------------+ +-------------+
;;; | Double | | Double |
;;; | Low-Res | | Low-Res |
;;; | Graphics | | Graphics |
;;; $400 +-------------+ +-------------+
;;; | (unused) | | |
;;; $300 +-------------+ +-------------+
;;; | (unused) | | |
;;; $200 +-------------+ +-------------+
;;; | Stack | | |
;;; $100 +-------------+ +-------------+
;;; | Zero Page | | |
;;; $00 +-------------+ +-------------+
;;;
IO_BUF := $800 ; $800...$BFF
CHIP8_MEMORY := $4000 ; virtual memory address
CHIP8_SIZE := $1000 ; 4K
CHIP8_STACK_LO := $5000 ; low bytes of stack entries
CHIP8_STACK_HI := $5100 ; high bytes of stack entries
CHIP8_BITMAP := $5200 ; copy of screen bitmap (1 bit per pixel)
ADDR_MASK_HI := %00001111 ; mask high address byte to 12 bits
.assert .lobyte(CHIP8_MEMORY) = 0, error, "code assumes page alignment"
LOAD_ADDR := $200
FONT_ADDR := $050
CHIP8_SCREEN_WIDTH = 64
CHIP8_SCREEN_HEIGHT = 32
;;; Defaults
COLOR_BORDER = 2 ; dark blue
COLOR_BG = 0 ; black
COLOR_FG = 15 ; white
;;; ============================================================
;;; Start of Binary
;;; ============================================================
;;; ============================================================
;;; Interpreter protocol
;;; http://www.easy68k.com/paulrsm/6502/PDOS8TRM.HTM#5.1.5.1
jmp start
.byte $EE, $EE ; signature
.byte 65 ; pathname buffer length ($2005)
PATHBUF:
.res 65, 0
;;; ============================================================
;;; ProDOS parameters
.proc get_file_info_params
param_count: .byte $A
pathname: .addr PATHBUF
access: .byte 0
file_type: .byte 0
aux_type: .word 0
storage_type: .byte 0
blocks_used: .word 0
mod_date: .word 0
mod_time: .word 0
create_date: .word 0
create_time: .word 0
.endproc
.proc open_params
param_count: .byte 3
pathname: .addr PATHBUF
io_buffer: .addr IO_BUF
ref_num: .byte 0
.endproc
.proc read_params
param_count: .byte 4
ref_num: .byte 0
data_buffer: .addr CHIP8_MEMORY + LOAD_ADDR
request_count: .word CHIP8_SIZE - LOAD_ADDR
trans_count: .word 0
.endproc
.proc close_params
param_count: .byte 1
ref_num: .addr 0
.endproc
.proc quit_params
param_count: .byte 4
quit_type: .byte 0
reserved1: .word 0
reserved2: .byte 0
reserved3: .word 0
.endproc
;;; ============================================================
;;; Quirks flags
;;; See the associated `README.md` for more details.
;;; Should `8XY1`, `8XY2` and `8XY3` reset `VF`?
;;; https://tobiasvl.github.io/blog/write-a-chip-8-emulator/#logical-and-arithmetic-instructions
;;; https://github.com/Timendus/chip8-test-suite?tab=readme-ov-file#the-test
quirks_vf_reset: .byte $80
;;; Should `FX55` and `FX65` increment `I`?
;;; https://tobiasvl.github.io/blog/write-a-chip-8-emulator/#fx55-and-fx65-store-and-load-memory
;;; https://github.com/Timendus/chip8-test-suite?tab=readme-ov-file#the-test
quirks_memory: .byte $80
;;; Should sprite drawing be clamped to 60Hz?
;;; https://github.com/Timendus/chip8-test-suite?tab=readme-ov-file#the-test
quirks_disp_wait: .byte $80
;;; Should sprites clip or wrap?
;;; https://github.com/Timendus/chip8-test-suite?tab=readme-ov-file#the-test
quirks_clipping: .byte $80
;;; Should `8XY6` and `8XYE` shift `VY`?
;;; https://tobiasvl.github.io/blog/write-a-chip-8-emulator/#8xy6-and-8xye-shift
;;; https://github.com/Timendus/chip8-test-suite?tab=readme-ov-file#the-test
quirks_shifting: .byte $00
;;; Should `BNNN` add `V0` or `VX`?
;;; https://tobiasvl.github.io/blog/write-a-chip-8-emulator/#bnnn-jump-with-offset
;;; https://github.com/Timendus/chip8-test-suite?tab=readme-ov-file#the-test
quirks_jumping: .byte $00
;;; Should `VF` be set to 1 if `FX1E` causes overflow?
;;; https://tobiasvl.github.io/blog/write-a-chip-8-emulator/#fx1e-add-to-index
;;; NOTE: Not runtime configured, but included anyway.
quirks_overflow: .byte $80
;;; ============================================================
;;; For restarting
saved_stack: .byte 0
;;; ============================================================
start:
tsx
stx saved_stack
lda #$95 ; Disable 80-col firmware
jsr COUT
jsr SetTextMode
;; ----------------------------------------
;; Can we run on this model? (requires DGR)
lda VERSION ; IIe or later = $06
cmp #$06
bne quit ; no, just exit
lda MACHID
and #%00110000 ; 128K?
cmp #%00110000
bne quit ; no, just exit
;; IIc? Enable VBL
lda ZIDBYTE ; IIc = $00
bne :+
lda #0 ; don't invert like we do on the IIe
sta VBL_XOR
sei ; we'll just poll
sta IOUDISOFF
bit ENVBL
bit PTRIG ; reset `RDVBL`
:
;; IIgs? Invert VBL sense
sec
jsr IDROUTINE ; clears carry on IIgs
bcs :+
lda #0 ; don't invert like we do on the IIe
sta VBL_XOR
:
;; Macintosh IIe Option Card? No palette swap
lda BELL1
cmp #$02 ; illegal opcode $02 is signature
bne :+
lda #OPC_RTS
sta ROR8 ; nerf the routine
:
;; ----------------------------------------
;; Pathname passed?
lda PATHBUF
bne get_info
quit:
;; Quit back to ProDOS
jsr SetTextMode
MLI_CALL QUIT, quit_params
brk
;; ----------------------------------------
;; Error encountered - just beep annoyingly
fail:
jsr BELL1
jmp quit
;; --------------------------------------------------
;; Check for filetype signature and set flags
get_info:
MLI_CALL GET_FILE_INFO, get_file_info_params
bcs fail
;; See the associated `README.md` for more details.
lda get_file_info_params::file_type
cmp #$5D ; `$5D` = `ENT` "Entertainment"
bne :+
lda get_file_info_params::aux_type+1
cmp #$C8 ; CHIP-8 signature
bne :+
lda get_file_info_params::aux_type
lsr
ror quirks_vf_reset
lsr
ror quirks_memory
lsr
ror quirks_disp_wait
lsr
ror quirks_clipping
lsr
ror quirks_shifting
lsr
ror quirks_jumping
:
;; ----------------------------------------
;; Load the file
;; Also entry point for restarting
load_file:
ldx saved_stack
txs
jsr InitMemory
jsr InitRand
MLI_CALL OPEN, open_params
bcs fail
lda open_params::ref_num
sta read_params::ref_num
sta close_params::ref_num
MLI_CALL READ, read_params
bcs fail
MLI_CALL CLOSE, close_params
bcs fail
;;; ============================================================
;;; Zero Page Usage
;;; ============================================================
.struct
.org $00
pc_ptr .addr ; pointer to current instruction
instr .word ; copy of current instruction
stack_ptr .byte ; offset into stack
vbl .byte ; VBL state (bit7 = blanking)
;;; Timers
delay_timer .byte
sound_timer .byte
;;; Registers
addr_ptr .addr ; `I` (address register)
registers .byte 16 ; `V0`...`VF`
addr_copy .addr ; temporary copy of `I`
;;; Keys
keys .byte 16 ; state of keys 0-F (bit7 = 1 if pressed)
akd .byte ; do we think any key is down
;;; Graphics
graph_ptr .addr ; pointer for graphics work
fg_bits_main .byte ; pre-computed/shifted color bits pattern
fg_bits_aux .byte ; "
bg_bits_main .byte ; "
bg_bits_aux .byte ; "
fg .byte ; temp during drawing
bg .byte ; "
mask1 .byte ; bits we're setting
mask2 .byte ; bits we're keeping
sprite_x .byte
sprite_y .byte
sprite_rows .byte
collision .byte
_ZP_END_ .byte
.endstruct
;;; NOTES: Avoid `$40`-`$4E` (used by ProDOS)
.assert _ZP_END_ <= $40, error, "ZP collision"
;;; NOTE: `pc_ptr` and `addr_ptr` are stored as real memory addresses,
;;; not virtual memory addresses.
;;; ============================================================
;;; Interpreter
;; Initialize all registers
lda #0
sta stack_ptr
sta delay_timer
sta sound_timer
sta addr_ptr
sta addr_ptr+1
ldx #$F
: sta registers,x
dex
bpl :-
lda #<(CHIP8_MEMORY+LOAD_ADDR)
sta pc_ptr
lda #>(CHIP8_MEMORY+LOAD_ADDR)
sta pc_ptr+1
sta KBDSTRB
jsr ExpandColorPatterns
sta LORES
sta MIXCLR
sta TXTCLR
sta SET80VID
sta DHIRESON
lda border_color
jsr DrawBorder ; calls `WaitVBL`, so ensure timers are reset
jsr ClearKeys
jsr ClearScreen ; calls `WaitVBL`, so ensure timers are reset
;; fall through to fetch/execute/decode
;;; ============================================================
;;; Fetch / Decode / Execute
InterpreterLoop:
;; --------------------------------------------------
;; Key state
jsr UpdateKeys
;; --------------------------------------------------
;; Service timers
jsr ServiceTimers
;; --------------------------------------------------
;; Fetch
ldy #0
lda (pc_ptr),y
sta instr+1 ; big-endian
iny
lda (pc_ptr),y
sta instr
jsr IncPCPtrBy2
;; --------------------------------------------------
;; Decode
;; Use the high nibble to index a jump table, since most
;; CHIP-8 instructions are determined solely by the high
;; nibble.
lda instr+1
lsr
lsr
lsr
lsr
tax
lda dispatch_lo,x
sta dispatch
lda dispatch_hi,x
sta dispatch+1
;; Not every instruction is of the form `_XY_` but most are so
;; load nibbles into X and Y
lda instr
and #%11110000
lsr
lsr
lsr
lsr
tay ; Y = Y
lda instr+1
and #%00001111
tax ; X = X
;; And using `__NN` is common so load low byte into A
lda instr ; commonly needed
;; --------------------------------------------------
;; Execute
dispatch := *+1
jsr $1234 ; self-modified
jmp InterpreterLoop
dispatch_lo:
.lobytes Op0, Op1, Op2, Op3
.lobytes Op4, Op5, Op6, Op7
.lobytes Op8, Op9, OpA, OpB
.lobytes OpC, OpD, OpE, OpF
dispatch_hi:
.hibytes Op0, Op1, Op2, Op3
.hibytes Op4, Op5, Op6, Op7
.hibytes Op8, Op9, OpA, OpB
.hibytes OpC, OpD, OpE, OpF
;;; ============================================================
;;; All of the following are called with:
;;; X register = X (assuming `_X__`)
;;; Y register = Y (assuming `__Y_`)
;;; A register = low byte (i.e. `__NN`)
;;; ============================================================
.proc Op0
cpx #0
bne fail
;;; `0NNN` - Execute machine language subroutine (not supported)
cmp #$EE
bne :+
;; `00EE` - Return from subroutine
ldx stack_ptr
dex
lda CHIP8_STACK_LO,x
sta pc_ptr
lda CHIP8_STACK_HI,x
sta pc_ptr+1
stx stack_ptr
rts
:
cmp #$E0
bne fail
;; `00E0` - Clear the screen
jmp ClearScreen
fail: jmp BadInstruction
.endproc
;;; ============================================================
.proc Op1
;; `1NNN` - Jump to address `NNN`
sta pc_ptr
txa
and #ADDR_MASK_HI
ora #.hibyte(CHIP8_MEMORY)
sta pc_ptr+1
rts
.endproc
;;; ============================================================
.proc Op2
;; `2NNN` - Execute subroutine starting at address `NNN`
pha
txa
pha
ldx stack_ptr
lda pc_ptr
sta CHIP8_STACK_LO,x
lda pc_ptr+1
sta CHIP8_STACK_HI,x
inx
stx stack_ptr
pla
tax
pla
;; now same as jump
jmp Op1
.endproc
;;; ============================================================
.proc Op3
;; `3XNN` - Skip the following instruction if the value of
;; register `VX` equals `NN`
cmp registers,x ; A = VX
bne :+
jsr IncPCPtrBy2
: rts
.endproc
;;; ============================================================
.proc Op4
;; `4XNN` - Skip the following instruction if the value of
;; register `VX` is not equal to `NN`
cmp registers,x ; A = VX
beq :+
jsr IncPCPtrBy2
: rts
.endproc
;;; ============================================================
.proc Op5
;; `5XY0` - Skip the following instruction if the value of
;; register `VX` is equal to the value of register `VY`
and #%00001111
bne fail
lda registers,x
cmp registers,y
bne :+
jsr IncPCPtrBy2
: rts
fail: jmp BadInstruction
.endproc
;;; ============================================================
.proc Op6
;; `6XNN` - Store number `NN` in register `VX`
sta registers,x
rts
.endproc
;;; ============================================================
.proc Op7
;; `7XNN` - Add the value `NN` to register `VX`
clc
adc registers,x
sta registers,x
rts
.endproc
;;; ============================================================
.proc Op8
;; Secondary decode / dispatch
txa
pha
lda instr
and #%00001111
tax
lda dispatch_lo,x
sta dispatch
lda dispatch_hi,x
sta dispatch+1
pla
tax
dispatch := *+1
jmp $1234 ; self-modified
dispatch_lo:
.lobytes Op8xy0, Op8xy1, Op8xy2, Op8xy3
.lobytes Op8xy4, Op8xy5, Op8xy6, Op8xy7
.lobytes Op8xy8, Op8xy9, Op8xyA, Op8xyB
.lobytes Op8xyC, Op8xyD, Op8xyE, Op8xyF
dispatch_hi:
.hibytes Op8xy0, Op8xy1, Op8xy2, Op8xy3
.hibytes Op8xy4, Op8xy5, Op8xy6, Op8xy7
.hibytes Op8xy8, Op8xy9, Op8xyA, Op8xyB
.hibytes Op8xyC, Op8xyD, Op8xyE, Op8xyF
.endproc
;;; --------------------------------------------------
.proc Op8xy0
;; `8XY0` - Store the value of register `VY` in register `VX`
lda registers,y
sta registers,x
rts
.endproc
;;; --------------------------------------------------
.proc Op8xy1
;; `8XY1` - Set `VX` to `VX` OR `VY`
lda registers,x
ora registers,y
sta registers,x
bit quirks_vf_reset
bpl :+
lda #0
sta registers+$F
:
rts
.endproc
;;; --------------------------------------------------
.proc Op8xy2
;; `8XY2` - Set `VX` to `VX` AND `VY`
lda registers,x
and registers,y
sta registers,x
bit quirks_vf_reset
bpl :+
lda #0
sta registers+$F
:
rts
.endproc
;;; --------------------------------------------------
.proc Op8xy3
;; `8XY3` - Set `VX` to `VX` XOR `VY`
lda registers,x
eor registers,y
sta registers,x
bit quirks_vf_reset
bpl :+
lda #0
sta registers+$F
:
rts
.endproc
;;; --------------------------------------------------=
.proc Op8xy4
;; `8XY4` - Add the value of register `VY` to register `VX`
;; ; Set `VF` to 01 if a carry occurs
;; ; Set `VF` to 00 if a carry does not occur
lda registers,x
clc
adc registers,y
sta registers,x
jmp set_vf_to_carry
.endproc
;;; --------------------------------------------------
.proc Op8xy5
;; `8XY5` - Subtract the value of register `VY` from register
;; `VX` ; Set `VF` to 00 if a borrow occurs ; Set `VF` to 01
;; if a borrow does not occur
lda registers,x
sec
sbc registers,y
sta registers,x
jmp set_vf_to_carry
.endproc
;;; --------------------------------------------------
.proc Op8xy6
;; `8XY6` - Store the value of register `VY` shifted right one
;; bit in register `VX` ; Set register `VF` to the least
;; significant bit prior to the shift ; `VY` is unchanged
lda registers,y
bit quirks_shifting
bpl :+
;; Original CHIP-8 on COSMAC VIP ignored `VY`.
lda registers,x
:
lsr
sta registers,x
jmp set_vf_to_carry
.endproc
;;; --------------------------------------------------
.proc Op8xy7
;; `8XY7` - Set register `VX` to the value of `VY` minus `VX`
;; ; Set `VF` to 00 if a borrow occurs ; Set `VF` to 01 if a
;; borrow does not occur
lda registers,y
sec
sbc registers,x
sta registers,x
jmp set_vf_to_carry
.endproc
;;; --------------------------------------------------
Op8xy8 := BadInstruction
Op8xy9 := BadInstruction
Op8xyA := BadInstruction
Op8xyB := BadInstruction
Op8xyC := BadInstruction
Op8xyD := BadInstruction
;;; --------------------------------------------------
.proc Op8xyE
;; `8XYE` - Store the value of register `VY` shifted left one
;; bit in register `VX` ; Set register `VF` to the most
;; significant bit prior to the shift ; `VY` is unchanged
lda registers,y
bit quirks_shifting
bpl :+
;; Original CHIP-8 on COSMAC VIP ignored `VY`.
lda registers,x
:
asl
sta registers,x
.assert * = set_vf_to_carry, error, "fall-through"
.endproc
set_vf_to_carry:
rol
and #1
sta registers+$F
rts
;;; --------------------------------------------------
Op8xyF := BadInstruction
;;; ============================================================
.proc Op9
;; `9XY0` - Skip the following instruction if the value of
;; register `VX` is not equal to the value of register `VY`
and #%00001111
bne fail
lda registers,x
cmp registers,y
beq :+
jsr IncPCPtrBy2
: rts
fail: jmp BadInstruction
.endproc
;;; ============================================================
.proc OpA
;; `ANNN` - Store memory address `NNN` in register `I`
sta addr_ptr
txa
and #ADDR_MASK_HI
ora #.hibyte(CHIP8_MEMORY)
sta addr_ptr+1
rts
.endproc
;;; ============================================================
.proc OpB
;; `BNNN` - Jump to address `NNN` + `V0`
bit quirks_jumping
bmi :+
ldx #0
:
clc
adc registers,x
sta pc_ptr
lda instr+1
and #%00001111
adc #0
and #ADDR_MASK_HI
ora #.hibyte(CHIP8_MEMORY)
sta pc_ptr+1
rts
.endproc
;;; ============================================================
.proc OpC
;; `CXNN` - Set `VX` to a random number with a mask of `NN`
jsr Random
and instr
sta registers,x
rts
.endproc
;;; ============================================================
.proc OpD
;; `DXYN` - Draw a sprite at position `VX`, `VY` with `N`
;; bytes of sprite data starting at the address stored in `I`;
;; Set `VF` to 01 if any set pixels are changed to unset, and
;; 00 otherwise
lda registers,x
and #CHIP8_SCREEN_WIDTH-1 ; coordinates wrap
tax
lda registers,y
and #CHIP8_SCREEN_HEIGHT-1 ; coordinates wrap
tay
lda instr
and #%00001111
jsr DrawSprite
rol
and #1
sta registers+$F
rts
.endproc
;;; ============================================================
.proc OpE
lda registers,x
and #%00001111 ; TODO: Invalid key behavior?
tax ; X = key
lda instr
cmp #$9E
bne :+
;; `EX9E` - Skip the following instruction if the key
;; corresponding to the hex value currently stored in register
;; `VX` is pressed
lda keys,x
bpl ret ; N set if pressed
jmp IncPCPtrBy2
:
cmp #$A1
bne :+
;; `EXA1` - Skip the following instruction if the key
;; corresponding to the hex value currently stored in register
;; `VX` is not pressed
lda keys,x
bmi ret ; N clear if not pressed
jmp IncPCPtrBy2
:
jmp BadInstruction
ret: rts
.endproc
;;; ============================================================
.proc OpF
;; Secondary decode / dispatch
txa
pha
lda instr
ldx #NUM_OPS-1
: cmp dispatch_op,x
beq :+
dex
bpl :-
jmp BadInstruction
:
lda dispatch_lo,x
sta dispatch
lda dispatch_hi,x
sta dispatch+1
pla
tax
dispatch := *+1
jmp $1234 ; self-modified
dispatch_op:
.byte $07, $0A, $15, $18, $1E, $29, $33, $55, $65
NUM_OPS = * - dispatch_op
dispatch_lo:
.lobytes OpFX07, OpFX0A, OpFX15, OpFX18, OpFX1E, OpFX29, OpFX33, OpFX55, OpFX65
.assert * - dispatch_lo = NUM_OPS, error, "table size"
dispatch_hi:
.hibytes OpFX07, OpFX0A, OpFX15, OpFX18, OpFX1E, OpFX29, OpFX33, OpFX55, OpFX65
.assert * - dispatch_hi = NUM_OPS, error, "table size"
.endproc
.proc OpFX07
;; `FX07` - Store the current value of the delay timer in
;; register `VX`
lda delay_timer
sta registers,x
rts
.endproc
.proc OpFX0A
;; `FX0A` - Wait for a keypress and store the result in
;; register `VX`
txa
pha
jsr WaitForKey
tay
pla
tax
sty registers,x
rts
.endproc
.proc OpFX15
;; `FX15` - Set the delay timer to the value of register `VX`
lda registers,x
sta delay_timer
rts
.endproc
.proc OpFX18
;; `FX18` - Set the sound timer to the value of register `VX`
lda registers,x
sta sound_timer
rts
.endproc
.proc OpFX1E
;; `FX1E` - Add the value stored in register `VX` to register
;; `I`
lda registers,x