-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathc48_source.txt
1570 lines (1340 loc) · 52.9 KB
/
c48_source.txt
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 48 version 2.25 Source Code for the ASAP assembler - this assembler is still available but uses a very old version of Perl - I haven't been able to get it to run, but, due to having the binary via the asc, doing so has not been needed. Significant information has been gleened from the comments and design from this source. The compiled c48 binary is available in this repo.
Recovered from https://groups.google.com/forum/#!searchin/comp.sys.handhelds/Andreas$20Gustafsson/comp.sys.handhelds/IhEF9Da2RJk/Hy0SibNcfQwJ
Andreas Gustafsson
10/6/90
Due to popular demand, I'm posting the complete source code for the
CHIP-48 video game interpreter to comp.sys.handhelds. I hope this
will inspire others to write more free machine code software for the
HP48SX.
The intention of this posting is not that people should actually
assemble the code; it's much easier to get the binary which was
posted recently and is also available by FTP from vega.hut.fi as
/pub/misc/hp48sx/asap/chip48-2.25-bin.Z. Rather, it should
serve as a source of programming tips for those writing their own
machine code programs.
This source is written for the ASAP assembler, version 1.01. ASAP is
also FTP:able from vega.hut.fi. To run the assembler, you need a
32-bit Unix machine and Perl 3.0 which is available from most major
Unix archive sites.
Here it is. Enjoy!
================================ Cut here ================================
; @(#) chip.asap 2.25 9/15/90
;
; chip.asap -- a CHIP-8 interpreter for the HP48SX
;
; (C) Copyright 1990 Andreas Gustafsson
;
; Noncommercial distribution allowed, provided that this
; copyright message is preserved, and any modified versions
; are clearly marked as such.
;
; The program makes use of undocumented low-level features of
; the HP48SX calculator, and may or may not cause loss of data,
; excessive battery drainage, and/or damage to the calculator
; hardware. The Author takes no responsibility whatsoever for
; any damage caused by the use of this program.
;
; THIS SOFTWARE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR
; IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
; WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
;
;
; Register usage:
;
; d0 = general pointing
; d1 = points to chip-8 instruction (physical)
;
; r0 = CHIP-8 I register
; r1 = last time value
; r2 = physical address of virtual zero
; r3 = CHIP-8 PC
;
; standard preamble for Kermit download
data.b 'H'
data.b 'P'
data.b 'H'
data.b 'P'
data.b '4'
data.b '8'
data.b '-'
data.b 'A'
data.a #2dcc ; machine code object
begin: data.a end-begin ; length of object
; end of preamble
; HP48SX ROM locations
; These are for the Revision A ROM, they may need to be changed for
; other revisions.
flush_kbd=#00d57 ; flush keyboard buffer
do_in_c=#01160 ; perform "in.4 c" instruction
alloc_str=#05b7d ; allocate string
push_r0_shortint=#06537 ; push r0 as short integer, restore regs
save_rpl_regs=#0679b ; save d0, d1, b, d
restore_rpl_regs=#067d2 ; restore d0, d1, b, d
check_1_arg=#18abf ; make sure stack isn't empty
; HP48SX RAM locations
crcval=#00104 ; hardware CRC register
hwtimer=#00138 ; hardware timer
stackdisp_ptr=#7055B ; contains address of stack display
menudisp_ptr=#70551 ; contains address of menu display
flags_37=#706ce ; flags -37 to -40
; data area layout (with a smarter assembler these offsets could be
; calculated automatically).
; Don't rearrange; in particular, the variables must be first and
; the order of the "V10".."V15" pseudovariables is important.
ofs_vars=0 ; CHIP-8 variables, 16 vars * 2 nibbles = 32
ofs_timer=32 ; delay timer "V10", size = 2
ofs_sound=34 ; sound timer "V11", size = 2
ofs_sndon=36 ; sound on/off flag "V12.0", size = 1
ofs_sdata=37 ; speaker data "V12.1", "V13", size = 3
ofs_ckeys=40 ; control key status "V14-15", size=4
ofs_csp=44 ; CHIP-8 stack pointer, size 5
ofs_cstack=49 ; CHIP-8 stack, size "stacknibbles" = 64
ofs_linetab=113 ; display row table, size 64 * 5 = 320
ofs_regsave=433 ; r0..r3 temporary save location, size = 20
ofs_end=453 ; end of data area
; don't change these without updating the offsets above also
stacklevels=16 ; size of CHIP-8 stack
stacknibbles=64 ; 4*stacklevels
; execution begins here
call.a check_1_arg ; check that stack is not empty
call.a save_rpl_regs ; call ROM routine to save d0, d1, b, d
; if flag -40 (clock display) is set, clear it and exit.
; This is because the clock display interrupt (or whatever)
; causes problems, and doesn't get turned off just by clearing
; the flag. However, it does get turned off when the screen is
; redrawn after we exit, so the next time this program is started
; it will run normally.
move.p5 flags_37,c
move.a c,d0 ; point to flags -37..-40
move.p @d0,a ; get old flags
brbc 3,a,noclock ; jump if flag -40 clear
clrb 3,a ; clear flag -40
move.p a,@d0 ; store updated flags
jump.3 exit ; don't continue just yet
noclock:
; allocate a string for temporary data storage
clrb #a,st ; no garbage collection done
move.p5 ofs_end,c ; size of uninitialized data area
call.a alloc_str ; call ROM routine to allocate a string
swap.a c,d0
move.a c,r4 ; now R4 points to the string
; clear the CHIP-8 variables and initialize some pseudovariables
move.a c,d0 ; point to variables (uses c value set above)
clr.w c
move.w c,@d0 ; clear V0..V7
add.a 16,d0
move.w c,@d0 ; clear V8..VF
add.a 16,d0
move.p8 #40010000,c ; set timers to #00, sndon to #1,
; and sdata to #400
move.8 c,@d0
; fill "linetab" with pointers to the display rows
move.a r4,a ; get start of data area
move.p5 ofs_linetab,c
add.a a,c
move.a c,d0 ; d0 points to linetab
move.p5 stackdisp_ptr,c ; pointer to address of stack display
move.p2 56,a ; 56 rows
call.3 ltfill
move.p5 menudisp_ptr,c ; pointer to address of menu display
move.p2 8,a ; 8 rows
call.3 ltfill
; allocate a 4 kB string for the CHIP-8 virtual memory
clrb #a,st ; no garbage collection done
move.p5 #2000,c ; 4 kB in nibbles
call.a alloc_str ; call ROM routine to allocate a string
; now r0 points to the header of the newly allocated string
; object, and d0 points to the data part
swap.a d0,c
move.a c,r2 ; virtual zero in r2
; hate nondeterministic bugs...
clr.w a
clr.w b
clr.w c
clr.w d
chipmain:
; copy the chip-8 program from the argument string to the virtual
; chip-8 memory
move.a r2,a ; get virtual zero
move.p5 #0400,c ; virtual #0200 bytes
add.a a,c
move.a c,d0 ; now d0 points to virtual 0200
move.a @d1,c ; point to the argument object
move.a c,d1 ; (presumably a string)
move.a @d1,a ; get the object type
move.p5 #02a2c,c ; string type prefix
brne.a c,a,doerror ; exit if it isn't a string
add.a 5,d1 ; skip the type
move.a @d1,a ; get the object length
sub.a 5,a ; subtract length of length
move.p5 #01c00,c ; this is #1000 - #0200 bytes in nibbles
brgt.a c,a,nottoolong
doerror:
jump.3 errexit ; string will not fit in 4 k
nottoolong:
add.a 5,d1 ; point to the object itself
call.3 copynibbles
; pop the argument string off the stack
call.a restore_rpl_regs
add.a 5,d1
inc.a d
call.a save_rpl_regs
; copy the hexadecimal character font to the virtual chip-8 memory,
; unpacking it on-the-fly
move.a r2,c ; get virtual zero
move.a c,d0 ; now d0 points to 0000 virtual
move.a pc,a
ref17: move.p5 hexfont-ref17,c
add.a a,c
move.a c,d1 ; now d1 points to the hex patterns
move.p2 hexfontend-hexfont,a ; length (8 bits are enough)
fontcopylo:
move.1 @d1,c ; read a nibble
sln.a c ; shift to high nibble in byte
add.a 1,d1
move.2 c,@d0 ; store byte
add.a 2,d0
dec.a a
brnz.b a,fontcopylo
intoff
; jump here when the "restart" key is pressed
restart:
; initialize PC and I
clr.a c
move.a c,r0 ; I=0000 in r0
move.p3 #200,c ; relies on c being cleared
move.a c,r3 ; PC=0200 in r3
; initialize stack pointer
move.a r4,a ; get start of data area
move.p5 ofs_csp,c
add.a a,c
move.a c,d0 ; d0 now points to csp
clr.a c
move.a c,@d0 ; csp cleared
call.3 i00e0 ; erase display
; initialize the time value
clr.a c ; must clear all of c for comparison below
move.p3 hwtimer,c
move.a c,d0 ; point to hardware timer
move.a @d0,c
retry1: move.a c,a
move.a @d0,c
brne.a c,a,retry1 ; loop until we get same value twice
move.p5 #00F80,a ; mask away 7 low bits of time value
and.a a,c
move.a a,r1
nextinstr:
call.3 realtime ; do real-time chores
brcc nocarry
rtcarry:
brbs 4,a,restart ; ENTER was pressed
; otherwise, the abort key was pressed; make a clean exit
exit:
call.a restore_rpl_regs
move.a @d0,a ; dispatch next RPL instruction
add.a 5,d0
jump.a @a
nocarry:
; There appears to be some kind of interrupt that
; checks the keyboard status, and that doesn't get
; turned off by "intoff". Not knowing how to turn it off, we
; try to live with it by flushing the keyboard buffer ever so
; often, and trying to keep the "out" register zeroed most of
; the time (so that the keyboard will appear inactive when
; the interrupt checks it, even if keys really are being pressed).
intoff ; just in case someone turned them on again
call.a flush_kbd ; flush keyboard buffer (trashes d1 and c)
dispatch:
; dispatch a CHIP-8 instruction
move.a r3,c ; get cpc
move.a c,a ; keep unincremented value
add.a 2,a ; increment cpc by 2 bytes
move.a a,r3 ; store incremented cpc
call.3 virtophy ; unincremented value is in c
move.a c,d1 ; store virtual pc in d1
clr.a c
add.a 1,d1 ; point to MSN
move.p @d1,c ; get MSN of first byte of CHIP-8 instruction
sub.a 1,d1 ; back to beginning of instruction
add.a c,c
add.a c,c ; now c = nibble * 4
move.a pc,a
ref6: add.a c,a
move.p5 jumptab-ref6,c
add.a a,c ; now c = jumptab + nibble * 4
move.a c,d0
clr.a c ; clear the 5th nibble
move.4 @d0,c ; now c = jumptab entry
move.a pc,a
jtref: add.a c,a ; now a = jump address
move.a pc,c
retref: add.a retloc-retref,c
push.a c ; push return address on stack
jump.a a ; jump to instruction routine
retloc:
brcs errexit ; if carry is set, an error has occurred
jump.3 nextinstr
errexit:
move.w r3,c ; get the current CHIP-8 PC value
move.w c,r0 ; move to R0
call.a push_r0_shortint ; ROM routine: push short integer from R0
; (this also restores saved d0, d1, b, d)
call.a save_rpl_regs ; save the registers again (redundant?)
jump.3 exit
; ltfill -- fill a part of "linetab"
ltfill:
move.a c,d1
move.a @d1,c ; get display address
add.a 16,c ; increment past the GROB header (20 nibbles)
add.a 4,c
ltfill_loop:
move.a c,@d0
add.a 16,c ; increment to next row (34 nibbles)
add.a 16,c
add.a 2,c
add.a 5,d0
dec.b a
brnz.b a,ltfill_loop
ret
; copynibbles -- copy a memory block
;
; d1 points to source, d0 to destination, and
; a contains the number of nibbles to copy
copynibbles:
copylo:
brz.a a,copyend
move.1 @d1,c
move.1 c,@d0
add.a 1,d0
add.a 1,d1
dec.a a
jump.3 copylo
copyend:
ret
; realtime -- do various timer-driven real-time processing
;
; In: nothing
; Out: carry set iff real-time keypress detected; key code is in a
; Uses: all 16 nibbles of a and c; d0
; but neither b nor d
;
realtime:
; flip the speaker if the sound is on
call.3 soundpd0 ; point to sound timer
move.b @d0,c
brz.b c,silent
add.a 2,d0 ; point to sound on/off flag
move.p @d0,c
brz.p c,silent
add.a 1,d0 ; point to speaker data
move.3 @d0,c
out.x c
not.x c ; turn #400 into #800 and vice versa
move.p3 #c00,a
and.x a,c
move.3 c,@d0
silent:
; check the hardware timer register to see if it is time for
; a 64 Hz realtime clock tick
clr.a c ; must clear all of c for comparison below
move.p3 hwtimer,c
move.a c,d0
move.a @d0,c
retry2: move.a c,a
move.a @d0,c
brne.a c,a,retry2 ; loop until we get same value twice
move.p5 #00F80,a ; mask away 7 low bits of time value
and.a a,c
move.a r1,a ; now a is old value, c is new value
brne.a c,a,dotick
jump.3 notick ; code at notick depends on c.0 being zero
dotick: ; handle a 64 Hz tick
clr.a c ; decrement r1 by #80
move.p2 #80,c
sub.a c,a
move.p5 #00F80,c ; mask
and.a c,a
move.a a,r1
call.3 timerpd0 ; point to delay timer
move.b @d0,c
brz.b c,timerzero
dec.b c
move.b c,@d0
timerzero:
add.a 2,d0 ; point to sound timer
move.b @d0,c
brz.b c,soundzero
dec.b c
move.b c,@d0
soundzero:
; check for various control keys
call.3 ckeyspd0 ; point d0 to key status
move.p3 #010,c ; row ENTER..backstep
out.x c
call.a do_in_c
move.a c,a ; save "in" data in a
clr.a c
out.x c ; zero "out" port as fast as possible
move.4 @d0,c ; get previous key status
move.4 a,@d0 ; save current key status
not.a a ; get keys that are not pressed
and.a c,a ; ..but were..
retbs 4,a ; return with carry set if ENTER pressed
retbs 0,a ; same for the backstep key
brbs 3,a,togglesound ; +/-
noabort:
move.p1 #1,c ; set flag to indicate that a tick took place
notick:
retclrc ; return tick flag in c.0
; toggle the sound flag (this is jumped to when the +/- key is pressed)
togglesound:
call.3 sndonpd0
move.p @d0,c
not.a c
move.p1 #1,a
and.a a,c
move.p c,@d0
jump.3 noabort
; nnnc - get NNN field of current instruction to c register
;
; In: d1 pointing to chip-8 instruction
; Out: NNN field of instruction in c (5 valid nibbles)
; Uses: none
nnnc:
clr.a c ; clear nibbles 3..4
move.1 2,p
move.p @d1,c ; set nibble 2
move.1 0,p
add.a 2,d1 ; point to second byte of instruction
move.b @d1,c ; set nibbles 0 and 1
sub.a 2,d1 ; restore d1
ret
; virtophy -- convert virtual address to physical address
;
; In: virtual address in c
; Out: physical address in c
; Uses: a
virtophy:
add.a c,c ; convert bytes to nibbles
move.a r2,a ; convert virtual to physical
add.a a,c
ret
; varpd0 - get pointer to variable to d0
;
; In: d1 points to nibble containing variable number
; Out: d0 points to variable
; Uses: a,c
varpd0:
clr.a c
move.p @d1,c ; get nibble with variable number
cvarpd0:
add.a c,c ; convert bytes to nibbles
move.a r4,a
add.a a,c
move.a c,d0
ret
; var0pd0 -- load d0 with pointer to V0
;
; In: none
; Out: d0 points to variable 0
; Uses: a,c
var0pd0:
clr.a c
move.p1 #0,c
jump.3 cvarpd0
; varfpd0 -- load d0 with pointer to VF
;
; In: none
; Out: d0 points to variable F
; Uses: a,c
varfpd0:
clr.a c
move.p1 #f,c
jump.3 cvarpd0
timerpd0:
clr.a c
move.p2 #10,c
jump.3 cvarpd0 ; point d0 to timer
soundpd0:
clr.a c
move.p2 #11,c
jump.3 cvarpd0 ; point d0 to sound timer
sndonpd0:
clr.a c
move.p2 #12,c
jump.3 cvarpd0 ; point d0 to sound on/off flag
ckeyspd0:
clr.a c
move.p2 #14,c
jump.3 cvarpd0 ; point d0 to control key status
; varxcvarya -- get values of X and Y variables
; In: d1 points to instruction
; Out: c contains VX, zero padded to .a field
; a contains VY, zero padded to .a field
; Uses: d0
varxcvarya:
call.3 varpd0 ; get pointer to X
clr.a c
move.b @d0,c ; get X value
push.a c
add.a 3,d1 ; point to Y nibble in instruction
call.3 varpd0 ; get pointer to Y
clr.a a
move.b @d0,a ; get Y value
pop.a c
sub.a 3,d1 ; back to beginning of instruction
ret
; In: d1 points to beginning instruction
; Out: c contains VX (5 nibbles valid)
; a contains VY (5 nibbles valid)
; d0 points to VX
; Uses: none
alusetup:
add.a 3,d1 ; point to Y nibble in instruction
call.3 varpd0
sub.a 3,d1
clr.a c
move.2 @d0,c ; VY in c
push.a c
call.3 varpd0 ; d0 is pointer to VX
clr.a a
move.2 @d0,a ; VX in a
pop.a c ; VY in c
swap.a a,c ; now VX in c and VY in a
ret
savecarry:
srn.a c ; extract the carry byte
srn.a c
lsbcarry:
move.p2 #01,a ; use low bit only
and.b a,c
push.a c
call.3 varfpd0 ; get a pointer to VF
pop.a c
move.b c,@d0 ; store the carry byte
retclrc
; testkey -- check whether a given hex key is pressed
;
; In: key number in c (5 low nibbles must be valid)
; Out: low nibble of c is nonzero iff key is pressed
; Uses: a,d0
testkey:
add.a c,c ; index into keytab
move.a pc,a
ref16: add.a c,a
move.p5 keytab-ref16,c
add.a a,c
move.a c,d0 ; now d0 points to keytab
clr.a c
move.1 @d0,c ; get "out" data
out.x c
call.a do_in_c
move.a c,a ; store the input value in a
clr.a c
out.x c ; zero "out" port as fast as possible
add.a 1,d0
move.1 @d0,c ; get "in" mask
and.a a,c
ret
; setup subroutine for fx55 or fx65 instruction
;
; In: d0 points to VX
; Out: d0 points to to V0, a points to VX, and d1 points to MI
varcopysetup:
swap.a c,d0 ; copy d0 to c
move.a c,d0
push.a c ; save pointer to the last variable
call.3 var0pd0 ; point d0 to first variable (v0)
move.a r0,c ; get I
call.3 virtophy
move.a c,d1 ; point d1 to data at I
pop.a c
move.a c,a ; now a contains pointer to last var.
ret
i0: ; mcode call
call.3 nnnc
move.a c,a ; routine address is now in a
clr.a c
move.p2 #e0,c
breq.a c,a,i00e0
move.p2 #ee,c
breq.a c,a,i00ee
retsetc ; illegal mcode call
i00e0: ; erase screen
move.a r4,a ; get start of data area
move.p5 ofs_linetab,c
add.a c,a
; a contains linetab pointer
; b counts down from 64
move.p2 64,c
move.a c,b
eraselo:
move.a a,d0
move.a @d0,c
move.a c,d0 ; d0 now points to display memory
clr.w c
move.w c,@d0 ; erase 16 nibbles
add.a 16,d0
move.w c,@d0 ; and 16 more
add.a 16,d0
move.b c,@d0 ; and 2 more, total 34
add.a 5,a
dec.b b
brnz.b b,eraselo
retclrc
i00ee: ; subroutine return
move.a r4,a ; get start of data area
move.p5 ofs_csp,c
add.a a,c
move.a c,d0 ; c and d0 both point to csp
move.a @d0,a ; now a contains the chip-8 stack pointer (0..4n-4)
retz.a a ; return with carry set if stack underflow
sub.a 4,a ; drop one level
move.a a,@d0 ; save new csp
add.a 5,c ; point c at stack[0]
add.a a,c ; point c at popped level
move.a c,d0 ; point d0 at popped level
clr.a c
move.4 @d0,c
move.a c,r3 ; set pc
retclrc
i1: ; 1NNN, jump
dojmp: call.3 nnnc
move.a c,r3 ; assign to pc
retclrc
i2: ; 2NNN, subroutine call
move.a r4,a ; get start of data area
move.p5 ofs_csp,c
add.a a,c
move.a c,d0 ; d0 and c both point to csp
move.a @d0,a ; now a contains the chip-8 stack pointer (0..4n-4)
; c still points at csp
add.a 5,c ; point c at stack[0]
add.a a,c ; point c at first free stack level
swap.a c,d0 ; now d0 points to free stack and c points to csp
move.a r3,a ; get pc
move.4 a,@d0 ; store pc in stack
swap.a c,d0 ; now d0 points to cpc again
move.a @d0,a ; now a contains the chip-8 stack pointer (0..4n-4)
add.a 4,a
move.p5 stacknibbles,c
retlt.a c,a ; return with carry set if stack overflow
move.a a,@d0 ; store incremented sp
jump.3 dojmp ; the reset is like 1nnn
i3: ; 3XKK, skip if X==KK
call.3 varpd0 ; get pointer to X
add.a 2,d1 ; point to second byte of instruction
move.b @d1,a ; now a = KK
move.b @d0,c ; now c = VX
skipeq:
brne.b c,a,noskip
doskip: swap.a c,r3 ; increment cpc by 2
add.a 2,c
swap.a c,r3
noskip:
retclrc
i4: ; 4XKK, skip if X<>KK
call.3 varpd0 ; get pointer to X
add.a 2,d1 ; point to second byte of instruction
move.b @d1,a ; now a = KK
move.b @d0,c ; now c = VX
skipne:
breq.b c,a,noskip
jump.3 doskip
i5: ; 5XY0, skip if X==Y
call.3 varxcvarya ; get VX to c, VY to a
jump.3 skipeq
i9: ; 9XY0, skip if X!=Y
call.3 varxcvarya ; get VX to c, VY to a
jump.3 skipne
i6: ; 6XKK, load variable by constant
call.3 varpd0 ; get pointer to X
add.a 2,d1 ; point to second byte of instruction
move.b @d1,a ; now a = KK
move.b a,@d0 ; store in variable
retclrc
i7: ; 7XKK, add constant to variable
call.3 varpd0 ; get pointer to X
add.a 2,d1 ; point to second byte of instruction
move.b @d1,a ; now a = KK
move.b @d0,c ; get old value
add.b a,c ; add KK
move.b c,@d0 ; store new value
retclrc
i8: ; arithmetic and logic operations
add.a 2,d1 ; point to last nibble of instruction
move.1 @d1,a
sub.a 2,d1
move.p1 #0,c
brne.p c,a,noti8xy0
i8xy0: ; VX := VY
call.3 alusetup
move.b a,@d0 ; store result
retclrc
noti8xy0:
move.p1 #1,c
brne.p c,a,noti8xy1
i8xy1: ; VX := VX or VY
call.3 alusetup
or.b a,c
move.2 c,@d0 ; store result
retclrc
noti8xy1:
move.p1 #2,c
brne.p c,a,noti8xy2
i8xy2: ; VX := VX and VY
call.3 alusetup
and.b a,c
move.2 c,@d0 ; store result
retclrc
noti8xy2:
move.p1 #3,c
brne.p c,a,noti8xy3
i8xy3: ; VX := VX xor VY
call.3 alusetup
move.b a,b
or.b c,b ; (x or y) in b
and.b a,c ; (x and y) in c
not.b c
and.b b,c ; (x xor y) in c
move.b c,@d0 ; store result
retclrc
noti8xy3:
move.p1 #4,c
brne.p c,a,noti8xy4
i8xy4: ; VX := VX + VY; carry in VF
call.3 alusetup
add.a a,c
move.2 c,@d0
jump.3 savecarry
noti8xy4:
move.p1 #5,c
brne.p c,a,noti8xy5
i8xy5: ; VX := VX - VY; carry in VF
call.3 alusetup
subcommon:
not.b a ; do monkey business to get inverted carry
add.a a,c
inc.a c
move.2 c,@d0
jump.3 savecarry
noti8xy5:
move.p1 #6,c
brne.p c,a,noti8xy6
i8xy6: ; VX := VX >> 1; carry in VF
call.3 alusetup
move.b c,a
srb.b c
move.2 c,@d0 ; store result
move.b a,c ; use low bit of original byte for carry
jump.3 lsbcarry
noti8xy6:
move.p1 #7,c
brne.p c,a,noti8xy7
i8xy7: ; VX := VY - VX; carry in VF
call.3 alusetup
swap.a a,c
jump.3 subcommon
noti8xy7:
move.p1 #e,c
brne.p c,a,noti8xye
i8xye: ; VX := VX << 1; carry in VF
call.3 alusetup
add.a c,c
move.2 c,@d0
jump.3 savecarry
noti8xye:
retsetc
ia: ; ANNN, set I
call.3 nnnc
move.a c,r0 ; assign to I
retclrc
ib: ; parametric jump to NNN+V0
call.3 varpd0
call.3 nnnc
clr.a a
move.b @d0,a
add.a a,c
move.a c,r3 ; assign to pc
retclrc
ic: ; pseudo-random number
call.3 varpd0 ; now d0 points to VX
move.p5 crcval,c
swap.a c,d0 ; point d0 to hardware crc
move.2 @d0,a ; read the low byte of the crc
swap.a c,d0 ; now d0 points to VX again
add.a 2,d1 ; point to second byte of instruction
move.b @d1,c ; get mask
sub.a 2,d1 ; restore d1
and.b a,c ; mask
move.b c,@d0 ; store result
retclrc
id_abort:
jump.3 fx0a_abort
id: ; DXYN, show N-byte sprite at MI at screen coordinates (X,Y)
; I doesn't change
; synchronize with 64 Hz tick
tickwait:
call.3 realtime
brcs id_abort ; a realtime key was pressed
brz.p c,tickwait ; wait until a tick occus
call.3 save_rregs ; save r0..r3
move.a r0,c ; get I
call.3 virtophy
move.a c,r0 ; now r0 points to sprite
call.3 varxcvarya
move.a c,d ; save X in d
move.p2 #1f,c
and.b c,a ; mask Y to range 0..31, leave in a
move.p2 #3f,c
and.b c,d ; mask X to range 0..63, save in d
; get the number of bytes in the sprite (preserving a and d)
add.a 2,d1 ; point to N field
clr.b c
move.1 @d1,c
add.b a,c ; now c contains Y + sprite length
move.b c,b
move.p2 #20,c
sub.b c,b ; now b contains no. of overshoot lines
brcc oshoot
clr.b b ; no overshoot
oshoot:
clr.b c ; get sprite length again
move.1 @d1,c
sub.b b,c ; subtract overshoot
move.1 c,0,p ; now p contains adjusted length
move.1 p,c,15 ; byte counter in nibble 15 of c
move.1 14,p
clr.p c ; collision flag in nibble 14 of c
move.1 0,p
sub.a 2,d1 ; back to beginning of instruction
call.3 sprite ; do it
call.3 restore_rregs
call.4 varfpd0 ; d0 points to VF
clr.b c
move.1 14,p
brz.p c,nocolls
inc.b c
nocolls:
move.b c,@d0 ; store collision flag
move.1 0,p
retclrc ; id
ie: ; skip on key pressed / not pressed
call.3 varpd0
clr.a c
move.b @d0,c ; Telmac key number
call.3 testkey
clr.b d
move.p c,d ; now d.b is nonzero iff key was pressed
add.a 2,d1 ; point to second byte of instruction
move.b @d1,a
sub.a 2,d1
move.p2 #9e,c
breq.b c,a,doex9e
move.p2 #a1,c
breq.b c,a,doexa1
retsetc
doex9e: move.b d,c
clr.b a
jump.3 skipne ; skip iff d!=0 (key was pressed)
doexa1: move.b d,c
clr.b a
jump.3 skipeq ; skip iff d==0 (key was not pressed)
; kwait -- wait for key, return it in low byte of c