This repository has been archived by the owner on Nov 16, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
VIC-SSS-MMX.s
executable file
·1436 lines (1365 loc) · 28.4 KB
/
VIC-SSS-MMX.s
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
;*********************************************************************
; Commodore VIC 20 Software Sprite Stack - MMX Edition
; last updated: 30-Oct-2011
; written by Robert Hurst <robert@hurst-ri.us>
; with contributions from:
; Matt Dawson <matsondawson@gmail.com>
;*********************************************************************
.fileopt author, "Robert Hurst"
.fileopt comment, "Software Sprite Stack"
.fileopt compiler, "VIC 20 ASSEMBLER"
;*********************************************************************
; To assemble this source using cc65.org project:
; ca65.exe --cpu 6502 --listing VIC-SSS-MMX.s
; Then link it into your project:
; ld65.exe -C yourlinker.cfg -o yourgame.prg yourgame.o VIC-SSS-MMX.o
;
; See the various .bat files used for working examples.
;
.include "VIC-SSS-MMX.h"
.segment "SPRITE"
;*********************************************************************
; Software Sprite Stack INITIALIZATION
;
; MUST BE INVOKED ONCE BEFORE USING ANY OTHER SSS CALL
; Value in COLORCODE will be used to fill the color buffers.
;
SSSINIT:
; SSS geometry
LDA VIC+$02
AND #$1F
STA PLAYCOLS
ASL ; CLC
ADC #$04
ASL
ASL
STA SSSCLIPX
LDA VIC+$03
AND #$7E
LSR
STA PLAYROWS
ASL ; CLC
ADC #$04
ASL
ASL
STA SSSCLIPY
;
LDA #$00
STA CRSRROW
STA CRSRCOL
TAY
TAX
@sss: TYA
STA sss+1,X
LDA CRSRCOL
STA sss,X
CLC
ADC PLAYCOLS
BCC @cc
INY
@cc: STA CRSRCOL
INX
INX
INC CRSRROW
LDA CRSRROW
CMP PLAYROWS
BNE @sss
;
; SSS active / pending video page pointers
LDA #>VICFRAME1
STA SCRNPAGE
LDA #>VICFRAME2
STA PENDING
LDA #$40
STA NEWDIRT
;
; kernal init
LDA #$01
STA RVSFLAG ; character reverse flag
LDA #$80
STA SHIFTMODE ; locked
LDA #$00
STA SCROLLFLAG ; disable
;
; SPRITE register init
STA SPRITES
;
; fill VIC screen / color buffers
LDA #SSSNULL
JSR SSSCLEAR
;
; VIC register init
LDA VIC+$02
AND #$7F ; if $80 enabled, +$0200 to base screen address
STA VIC+$02
LDA #$CF ; point VIC screen @ $1000 w/ char set @ $1C00
; LDA #$C0 ; uncomment for debugging display purposes
STA VIC+$05
;
; FRAME register init
LDY #$00
STY FPS
STY VSYNC
STY VSYNC2
STY VCOUNT
JSR SSSFLIP
RTS
;*********************************************************************
; Software Sprite Stack IRQ HANDLER
;
; PROGRAM USE OF THIS HANDLER IS OPTIONAL
;
; customize to your liking, i.e.,
; - change JMP $EABF here to continue to your custom IRQ handler; or
; - JMP SSSIRQ at the end of your IRQ handler.
;
SSSIRQ:
LDA JIFFYL
AND #%00111111
BNE @cont
LDX VCOUNT
STX FPS
STA VCOUNT
@cont:
INC VSYNC2 ; frame skipped?
LDA VSYNC
BEQ @fskip ; program is NOT waiting ...
LDX #$00
SEC
SBC VSYNC2
STA VSYNC ; save result
BCS @reset ; program wants more than 1 screen refresh
STX VSYNC ; clear wait for vertical sync flag
@reset: STX VSYNC2 ; clear frame skip counter
@fskip:
JMP $EABF
;*********************************************************************
; Software Sprite Stack CLEAR SCREEN
;
; Pass A with the character code to fill the PLAYFIELD buffer.
; Value in COLORCODE will be used to fill the PLAYCOLOR buffer.
;
; These changes will go into effect only after a call to SSSFLIP
;
SSSCLEAR:
PHA ;++ save character fill code
LDX #$00
LDY #$00
JSR SSSPLOT ; home "cursor"
@reset: TYA
STA DIRTYLINE,X
LDA PLAYCOLS
STA DIRTYLINE2,X
INX
CPX PLAYROWS
BNE @reset
;
@cls: PLA ;--
PHA ;++
JSR SSSPRINT
LDX CRSRCOL
BNE @cls ; loop until column wraps
LDY CRSRROW
BNE @cls ; loop until row wraps, too
PLA ;--
RTS
;*********************************************************************
; Software Sprite Stack PLOT CURSOR IN FRAME BUFFER
;
; SSSPLOT uses PLAYFIELD frame buffer.
; SSSPLOTS uses PENDING frame buffer.
;
; Pass X/Y with coordinate to put cursor.
;
SSSPLOT:
LDA #>PLAYFIELD
STA SCRNLINE+1
LDA #>PLAYCOLOR
STA COLORLINE+1
BNE SSSPLOTX
;
; PENDING frame cursor (for writing SPRITE chars)
SSSPLOTS:
LDA PENDING
STA SCRNLINE+1
ORA #$84
STA COLORLINE+1
;
SSSPLOTX:
LDA #>PLAYCOLOR
STA DIRTMAP+1
@x: CPX PLAYCOLS
BMI @y
LDX PLAYCOLS
DEX
@y: CPY PLAYROWS
BMI @ok
LDY PLAYROWS
DEY
@ok: STX CRSRCOL ; maintain column offset to row
STY CRSRROW ; maintain row number
TYA
ASL
TAY
LDA sss+1,Y
BEQ @top
INC SCRNLINE+1
INC COLORLINE+1
INC DIRTMAP+1
@top: LDA sss,Y
STA SCRNLINE
STA COLORLINE
STA DIRTMAP
LDY CRSRROW
RTS
;*********************************************************************
; Software Sprite Stack PRINT TO A FRAME BUFFER
;
; Like SSSPOKE, writes to current cursor, but also advances it to
; the right (with line/screen wrap) upon completion.
;
; All registers are preserved from this call, for common loop use.
;
SSSPRINT:
STX XCOPY ; save index registers
STY YCOPY
JSR SSSPOKE
INC CRSRCOL ; cursor right
LDA CRSRCOL
CMP PLAYCOLS ; moved past right edge?
BCC @fini ; no, all done
LDA #$00
STA CRSRCOL ; reset to 1st column
INC CRSRROW ; and advance down a row
LDA CRSRROW
CMP PLAYROWS ; moved past bottom edge?
BCS @toprow ; yes, wrap back to top
LDA SCRNLINE ; no, re-calculate new row pointer
CLC
ADC PLAYCOLS
BCC @cc
INC SCRNLINE+1
INC COLORLINE+1
@cc: STA SCRNLINE
STA COLORLINE
@fini: LDY CRSRCOL
LDA (COLORLINE),Y
STA CRSRCOLOR
LDA (SCRNLINE),Y
STA CRSRCHAR
LDX XCOPY ; restore index registers
LDY YCOPY
RTS
@toprow:
LDA #$00
STA CRSRROW
STA SCRNLINE
STA COLORLINE
DEC SCRNLINE+1
DEC COLORLINE+1
BNE @fini
;*********************************************************************
; Software Sprite Stack PRINT STRING FROM POINTER ON STACK
;
; Will print bytes, until a NULL, following the JSR call here.
; Carriage control and color codes are interpreted.
;
SSSPRINTS:
PLA
STA VECTORFG
PLA
STA VECTORFG+1
LDY #$01
@loop: LDA (VECTORFG),Y
BEQ @fini
CMP #$0D
BNE @ctrl
TYA
PHA
LDY CRSRROW
INY
LDX #0
JSR SSSPLOT
PLA
TAY
JMP @next
@ctrl: CMP #$F0
BCC @cont ; color code?
AND #$0F ; filter for 16-colors
STA COLORCODE ; 0=blk,1=wht,2=red,3=cyn,4=mag,5=grn,6=blu,7=yel
JMP @next
@cont: JSR SSSPRINT
@next: INY
BNE @loop
INC VECTORFG+1
BNE @loop
;
@fini: TYA
CLC
ADC VECTORFG
BCC @cc
INC VECTORFG+1
@cc: STA VECTORFG
LDA VECTORFG+1
PHA
LDA VECTORFG
PHA
RTS
;*********************************************************************
; Software Sprite Stack READ FROM A FRAME BUFFER
;
; SSSPEEK reads from PLAYFIELD frame buffer.
; SSSPEEKS reads from PENDING frame buffer.
; SSSPEEKXY reads from PLAYFIELD frame buffer, using the sprite pixel
; coordinate system to determine X/Y cursor positioning.
;
; Pass X/Y with the coordinate to put cursor.
; CRSRCHAR and CRSRCOLOR are filled with values under cursor, with
; the former returned in Accumulator.
;
SSSPEEKS:
JSR SSSPLOTS
JMP SSSPEEKX
;
; pixel X/Y
SSSPEEKXY:
CPX #$10
BCC @fini ; hidden by left border
CPX SSSCLIPX
BCS @fini ; hidden by right border
CPY #$10
BCC @fini ; above top border
CPY SSSCLIPY
BCC @ok ; below bottom border
@fini: RTS
@ok: TYA
SEC
SBC #$10
LSR
LSR
LSR
TAY
;
TXA
SEC
SBC #$10
LSR
LSR
LSR
TAX
;
SSSPEEK:
JSR SSSPLOT
;
SSSPEEKX:
LDY CRSRCOL
LDA (COLORLINE),Y
STA CRSRCOLOR
LDA (SCRNLINE),Y
STA CRSRCHAR
RTS
;*********************************************************************
; Software Sprite Stack WRITE TO A FRAME BUFFER
;
; Pass Accumulator with character code to write to current cursor.
; COLORCODE is used to fill same space with that value.
;
SSSPOKE:
LDY CRSRCOL
STA (SCRNLINE),Y
LDA (DIRTMAP),Y
LDX SCRNLINE+1
CPX #>PLAYFIELD
BPL @bg
ORA NEWDIRT
STA (DIRTMAP),Y ; update sprite's dirty bits only
.ifdef SPRITEDEF5
LDA (COLORLINE),Y
BIT COLORCODE
BMI @color ; keep color in place
.endif
LDA COLORCODE ; add color directly to map
JMP @color
@bg: AND #%11000000 ; keep any sprite dirt
ORA #%00100000 ; dirty this PLAYFIELD cell
ORA COLORCODE ; add color
@color: STA (COLORLINE),Y
LDX CRSRROW
LDY CRSRCOL
LDA DIRTYLINE,X
CMP CRSRCOL
BCC @ok ; is dirty seek lower than this write?
STY DIRTYLINE,X ; start looking for dirty cells from here
@ok: TYA
CMP DIRTYLINE2,X
BCC @ok2 ; is dirty seek higher than this write?
INY
STY DIRTYLINE2,X ; stop looking for dirty cells after here
@ok2: RTS
;*********************************************************************
; Software Sprite Stack FLIP ACTIVE / PENDING FRAME BUFFERS
;
; Pass Y with the number of vertical sync counts to wait for, or zero
; to make changes visible immediately.
;
; Only the current cursor position is preserved when completed.
;
; If user is holding RUN/STOP key down, the current video frame is
; paused until it is released.
;
SSSFFLIP:
LDA #$00 ; gameplay needs its action to move faster
BIT VSYNC2
BPL SSSFLIP2 ; do we need to drop a frame?
STA VSYNC2
RTS
;
SSSFLIP:
LDA #$00
STA VSYNC2
SSSFLIP2:
LDA CRSRROW
PHA ;++ save row
LDA CRSRCOL
PHA ;++ save column
TYA
PHA ;++ save Y (frame count)
;
; Phase I:
; - write any new PLAYFIELD updates to PENDING frame
; - erase any SPRITE characters from PENDING frame
;
LDA #%00010000 ; clean TOPFIELD dirt only after this update
JSR SSSCOMMIT
;
; Phase II:
; - render & write SPRITE characters to PENDING frame
;
JSR SSSUPDATE
;
; PHASE III:
; - signal IRQ to flip video to PENDING frame
; - wait for the all clear
;
PLA ;-- restore A (frame count)
TAY
INY ; account for a 'missed' frame
; INY ; allow for another 'missed' frame
CPY VSYNC2
BCS @pace ; is game loop & rendering ok?
LDY #$80 ; nope, flag next call to SSSFFLIP
STY VSYNC2 ; to skip rendering/flip altogether
LDA #$00 ; and don't wait for this vsync either
@pace: STA VSYNC ; enable screen to flip
@vsync: LDA VSYNC
BNE @vsync ; and wait for it to occur
BIT VSYNC2
BMI @ok
STA VSYNC2 ; fast flip, gauge again for next frame
;
@ok: LDA VIC+$02
EOR #$80
STA VIC+$02 ; re-direct VIC to other screen buffer
INC VCOUNT
LDA SCRNPAGE
STA PENDING ; make active screen as pending
EOR #$02
STA SCRNPAGE ; maintain VIC active video page
;
; PHASE IV:
; - write the same PLAYFIELD updates to new PENDING frame
;
LDA #%00100000 ; clean PLAYFIELD dirt as part of this update
JSR SSSCOMMIT
;
PLA ;-- restore X (column)
TAX
PLA ;-- restore Y (row)
TAY
JSR SSSPLOT
;
@pause: JSR STOPKEY
BNE @fini
;LDA #$00 ; clear skipped frame count
;STA VSYNC2
;STA VCOUNT
; === write any custom PAUSE or RESET code here ===
PLA
PLA
.global RESTART
JMP RESTART ; label speaks for itself
;
@fini: RTS
;*********************************************************************
; Software Sprite Stack COMMIT CHANGES TO PENDING FRAME BUFFER
;
; pass Accumulator with the PLAYFIELD bit(s) set for cleaning:
; - bit 7 for video #1
; - bit 6 for video #2
; - bit 5 for playfield
; - bit 4 for topfield
;
; SPRITE dirt for the PENDING frame is always cleaned.
;
; This is used by SSSFLIP and NOT normally called by user programs.
;
DIRTYMASK = $00
CLEANER = $01 ; bit 7=VIDEO1, 6=VIDEO2, 5=PLAYFIELD, 4=STATIC
;
SSSCOMMIT:
LDX PENDING
CPX #>VICFRAME1 ; will flip to video #1 next?
BNE @scrn2
ORA #%10000000 ; erase old sprites from video #1
BNE @cont
@scrn2: ORA #%01000000 ; erase old sprites from video #2
@cont: TAY
EOR #$FF ; reverse check to clean
STA CLEANER
TYA
ORA #%00100000 ; but always look for PLAYFIELD dirt
STA DIRTYMASK
TXA
STA VECTOR2+1
ORA #$84 ; and its COLOR
STA VECTOR3+1
LDX #$00
STX VECTOR2
STX VECTOR3
LDY #$00
JSR SSSPLOT ; home "cursor"
;
LDX #$00
@forx: LDA DIRTYLINE2,X
STA ACOPY
LDA DIRTYLINE,X ; dirty start column for this row
CMP ACOPY
BCS @nextx ; reached end of this line?
LDY PLAYCOLS
STY NEWDIRT
STY DIRTYLINE,X ; reset this line's seek for next commit
LDY #$00
STY DIRTYLINE2,X ; reset this line's end for next commit
TAY ; but start this commit from this column
@fory: LDA (COLORLINE),Y
AND #%11100000 ; is this cell dirty for ANY update?
BEQ @nexty ; no, skip it
STY DIRTYLINE2,X ; new ending column for this line
INC DIRTYLINE2,X ; new ending column for this line
CPY NEWDIRT
BCS @more
STY NEWDIRT
STY DIRTYLINE,X ; new starting column for this line
@more: AND DIRTYMASK ; is this cell dirty for THIS update?
BEQ @nexty ; no, skip it
LDA (COLORLINE),Y
STA (VECTOR3),Y ; update color cell
AND CLEANER ; remove this dirt from this cell
STA (COLORLINE),Y
LDA (SCRNLINE),Y
STA (VECTOR2),Y ; update video cell
@nexty: INY
CPY ACOPY
BCC @fory
@nextx: LDA SCRNLINE
CLC
ADC PLAYCOLS
BCC @cc
INC SCRNLINE+1
INC COLORLINE+1
INC VECTOR2+1
INC VECTOR3+1
@cc: STA SCRNLINE
STA COLORLINE
STA VECTOR2
STA VECTOR3
INX
CPX PLAYROWS
BCC @forx
;
LDA #%10000000
LDX PENDING
CPX #>VICFRAME2 ; will flip to video #2 next?
BNE @scrn1
LDA #%01000000
@scrn1: STA NEWDIRT
RTS
;*********************************************************************
; Software Sprite Stack CREATE A NEW SPRITE IN THE LIST
;
; pass Accumulator with the SPRITEDEF value (see HEADER)
; pass Y with the SPRITEH value (1-16)
; returns X with sprite index #0 thru SPRITEMAX-1
;
SSSCREATE:
STY YCOPY
LDX SPRITES
CPX #SPRITEMAX
BCC @cont
RTS ; sorry, increase SPRITEMAX and re-compile
@cont:
STA SPRITEDEF,X
CPX #$00
BNE @append ; >1 sprite
;
; this is the first sprite in the list ...
LDA #<SSSBUF
LDY #>SSSBUF
STA SPRITEBUFL
STY SPRITEBUFH
LDY #$20 ; start at top of custom character
STX SPRITEC1L
STY SPRITEC1H
STX SPRITEC2L
STY SPRITEC2H
BNE @compute
@append:
; copy prior sprite vectors
LDA SPRITEBUFL-1,X
STA SPRITEBUFL,X
LDA SPRITEBUFH-1,X
STA SPRITEBUFH,X
LDA SPRITEC1L-1,X
STA SPRITEC2L,X
LDA SPRITEC1H-1,X
STA SPRITEC1H,X
STA SPRITEC2H,X
.ifdef SPRITEDEF4
; repeating sprite?
LDA SPRITEDEF,X
AND #SPRITEDEF4
BNE @same
.endif
; no, allocate new image and character buffers
LDA SPRITEDEF-1,X
AND #$0F
TAY
LDA sssALLOC,Y
CLC
ADC SPRITEBUFL,X
BCC @cc
INC SPRITEBUFH,X
@cc: STA SPRITEBUFL,X
@compute:
LDA SPRITEDEF,X
AND #$0F
TAY
LDA sssALLOC,Y
STA sssBYTES
; vector#2 into custom char set
LDA SPRITEC2L,X
SEC
SBC sssBYTES ; account for its entire buffer size
BCS @cc1
DEC SPRITEC2H,X
DEC SPRITEC1H,X
@cc1: STA SPRITEC2L,X
STA SPRITEC1L,X
; vector#1 into custom char set
SEC
SBC sssBYTES ; account for its entire buffer size
BCS @cc2
DEC SPRITEC1H,X
@cc2: STA SPRITEC1L,X
JMP @new
; keep repeating sprite pointing to same custom characters
@same: LDA SPRITEC1L-1,X
STA SPRITEC1L,X
LDA SPRITEC2L-1,X
STA SPRITEC2L,X
LDA SPRITEC2H-1,X
STA SPRITEC2H,X
@new: LDA YCOPY
STA SPRITEH,X
.ifdef SPRITEDEF6
LDA #SSSNULL ; init with nothing in contact
STA SPRITEBACK,X
.endif
LDA #$00
.ifdef SPRITEDEF6
STA SPRITECX,X ; init collision X-coord
STA SPRITECY,X ; init collision Y-coord
.endif
STA SPRITEX,X ; sprite is not in visible area to start
STA SPRITEY,X
STA SPRITEZ,X ; all flags off
LDX SPRITES ; return this new sprite # as initialized
STX sssNUM
INC SPRITES ; account for the new sprite allocated
RTS
;*********************************************************************
; Software Sprite Stack SELECT A SPRITE TO MANIPULATE
;
; pass X index with the SPRITES number (0 - <SPRITEMAX)
; preset values for sssNUM, sssX, sssY, sssBYTES, sssNEXT
;
SSSUSE:
CPX SPRITES
BCC @cont
BRK ; debugging is in your future
@cont: STX sssNUM
LDA SPRITEDEF,X
AND #$0F
TAY
AND #%00000100 ; Y-float enabled?
BEQ @y
LDA SPRITEY,X
AND #$07
BNE @y
TYA
AND #%00001011 ; shave overflow row off
TAY
@y: TYA
AND #%00001000 ; X-float enabled?
BEQ @x
LDA SPRITEX,X
AND #$07
BNE @x
TYA
AND #%00000111 ; shave overflow column off
TAY
@x: ;
LDA sssALLOC,Y
STA sssBYTES
;
LDA sssROWS,Y
STA sssY
ASL
ASL
ASL ; x8
STA sssNEXT
;
LDA sssCOLS,Y
STA sssX
@fini: RTS
;*********************************************************************
; Software Sprite Stack LOAD SPRITE BUFFER WITH NEW IMAGE DATA
;
; SSSUSE must be called prior to point to sprite register entry
; pass Accumulator with this sprite's color
; pass X,Y as the source image pointer
;
SSSANIM:
STX sssDX
LDX sssNUM
STA SPRITECOL,X
LDA sssDX
STA SPRITEIMGL,X
TYA
STA SPRITEIMGH,X
BNE SSSTOUCH
;*********************************************************************
; Software Sprite Stack MOVE A SPRITE TO ABSOLUTE X,Y COORDINATES
;
; SSSUSE must be called prior to point to sprite register entry
; pass X,Y with the sprite coordinates
;
SSSMOVEXY:
TXA
LDX sssNUM
@x: STA SPRITEX,X
@y: TYA
STA SPRITEY,X
;*********************************************************************
; Software Sprite Stack FLAG A SPRITE FOR RENDERING
;
; Pass X with the sprite#
;
SSSTOUCH:
LDA SPRITEZ,X
AND #%11 ; reset flags, except fast copy + custom char
ORA #%11000000 ; force sprite to do make + copy/merge
STA SPRITEZ,X
RTS
;*********************************************************************
; Software Sprite Stack FLAG ALL SPRITES FOR RENDERING
;
SSSREFRESH:
LDX #$00
@loop: CPX SPRITES
BCS @fini
JSR SSSTOUCH
INX
BNE @loop
@fini: RTS
;*********************************************************************
; Software Sprite Stack UPDATE PENDING FRAME BUFFER WITH SPRITES
;
; This is part of the SSSFLIP operation and is NOT expected to be
; called by user programs.
;
SSSUPDATE:
LDX #0
STX sssNUM
@do:
CPX SPRITES
BCC @cc
RTS
@cc: LDA SPRITEDEF,X
.ifdef SPRITEDEF4 ; repeating sprite?
AND #SPRITEDEF4
BEQ @own
LDA SPRITEZ-1,X
STA SPRITEZ,X ; copy Z-flags for display & results
JMP @matrix
.else
ASL
BCC @redraw ; sprite is disabled
LDA SPRITEX,X
BEQ @redraw ; 0 = outside left border visible range
CMP SSSCLIPX
BCS @redraw ; >= outside right border visible range
LDA SPRITEY,X
BEQ @redraw ; 0 = outside top border visible range
CMP SSSCLIPY
BCS @redraw ; >= outside bottom border visible range
.endif
; preset sprite image buffer:
; VECTOR1 = pointer to top-left within sprite matrix
; VECTOR2&3 = pointer to adjacent chars, as necessary
@own: ;
JSR SSSUSE
LDA SPRITEBUFH,X
STA VECTOR1+1
STA VECTOR2+1
.ifdef SPRITEWIDE
STA VECTOR3+1
.endif
LDA SPRITEBUFL,X
STA VECTOR1
CLC
ADC sssNEXT
BCC @cc1
INC VECTOR2+1
.ifdef SPRITEWIDE
INC VECTOR3+1
.endif
@cc1: STA VECTOR2
.ifdef SPRITEWIDE
CLC
ADC sssNEXT
BCC @cc2
INC VECTOR3+1
@cc2: STA VECTOR3
.endif
.ifdef SPRITEDEF5
LDY #$11 ; ORA opcode
LDA SPRITEDEF,X
AND #SPRITEDEF5 ; ghost image?
BEQ @bit
LDY #$51 ; EOR opcode
@bit: TYA
STA @OP1
STA @OP2
EOR #$40 ; swap opcode
STA @OP3
.endif
; branch on make control flags
LDA SPRITEZ,X
ASL
PHA ; ++
BCC @copy ; $80 - (re)make buffered image
JSR @Make
@copy: PLA ; --
ASL
BCC @matrix ; $40 - copy buffered image
JSR @Copy
@matrix:
JSR @Display ; display sprite matrix
JMP @loop
@redraw:
LDA SPRITEZ,X
AND #%11
ORA #%11110000 ; make + copy/merge + null bg + clipped fg
STA SPRITEZ,X
@loop:
INC sssNUM
LDX sssNUM
JMP @do
;
; INIT PHASE
; ----------
; (re)make this sprite's image buffer
@Make: ;
LDA SPRITEH,X
STA sssXFER ; sprite image raster count
LDA SPRITEY,X
AND #$07
CLC
ADC sssXFER
STA sssDY ; 1st raster below image
;
; VECTORBG = pointer to your compact source image
LDA SPRITEIMGL,X
STA VECTORBG
LDA SPRITEIMGH,X
STA VECTORBG+1
LDA sssNEXT
STA sssLINENUM ; this many raster lines to copy
CMP sssDY
BCS @Mloop ; fits within height of sprite
LDA sssDY
SEC
SBC sssLINENUM
STA ACOPY ; compute how many rasters to clip
LDA sssXFER
SBC ACOPY
STA sssXFER ; clip image within sprite height
@Mloop:
LDA #$00 ; erase raster registers
STA sssROR1
STA sssROR2
.ifdef SPRITEWIDE
STA sssROR3
.endif
DEC sssLINENUM
LDY sssLINENUM
LDA sssXFER
BEQ @Mcopy ; no more rasters to copy - zero them
CPY sssDY
BCS @Mcopy ; below sprite image - zero this raster
@Mxfer:
DEC sssXFER ; copying sprite image rasters
LDY sssXFER
LDA (VECTORBG),Y
STA sssROR1
LDA SPRITEDEF,X
AND #%00000010
BEQ @Mxfer2 ; 16w ?
LDY #$08
LDA SPRITEDEF,X
LSR
BCC @Monly8 ; 16h ?
LDY #$10
@Monly8:
TYA
CLC
ADC sssXFER
TAY
LDA (VECTORBG),Y
STA sssROR2 ; load adjacent register
@Mxfer2:
LDA SPRITEX,X
AND #$07
BEQ @Mcopy
STA sssDX
@Mx2: LSR sssROR1
ROR sssROR2 ; shift into image overflow register #1
.ifdef SPRITEWIDE
ROR sssROR3 ; shift into image overflow register #2
.endif