-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathnv_sprite_extra_code.asm
1262 lines (1003 loc) · 41.5 KB
/
nv_sprite_extra_code.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
//////////////////////////////////////////////////////////////////////////////
// nv_sprite_extra_code.asm
// Copyright(c) 2021 Neal Smith.
// License: MIT. See LICENSE file in root directory.
//////////////////////////////////////////////////////////////////////////////
// this file contains subroutines that operate on sprites. Usually the
// address of the extra data for a sprite is required to perform some
// operation. For these type of subroutines the address is usually
// passed to the subroutine with MSB in Accumulator and LSB in X Register.
// there are macros in this file, but code will also be generated by
// instanciating the macros at a specific label.
//////////////////////////////////////////////////////////////////////////////
#importonce
#if !NV_C64_UTIL_DATA
.error "Error - nv_sprite_extra_code.asm: NV_C64_UTIL_DATA not defined. Import nv_c64_util_data.asm"
#endif
// the #if above doesn't seem to always work so..
// if data hasn't been imported yet, import it into default location
#importif !NV_C64_UTIL_DATA "nv_c64_util_default_data.asm"
#import "nv_sprite_macs.asm"
#import "nv_sprite_extra_macs.asm"
#import "nv_sprite_raw_macs.asm"
#import "nv_math16_macs.asm"
#import "nv_screen_macs.asm"
.macro nv_sprite_load_extra_ptr()
{
// load the address of the caller's param block to a pointer in
// zero page (first 256 bytes of memory.) we need a zero page
// location to store the address of the caller's nv_sprite_extra_data
// so that we can later use indirect index addressing into the
// extra data for the individual fields (sprit num, x loc, y loc, etc)
stx ZERO_PAGE_LO // store lo byte of addr of caller's param block
sta ZERO_PAGE_HI // store hi byte of addr of caller's param block
}
//////////////////////////////////////////////////////////////////////////////
// Sets a sprites color from the last byte in the sprite data
// the sprite data is found by getting the address of the first byte
// of it from the sprite's extra data.
// To call subroutine setup the following then JSR
// Accum: MSB of address of nv_sprite_extra_data
// X Reg: LSB of address of the nv_sprite_extra_data
.macro nv_sprite_set_color_from_extra_sr()
{
nv_sprite_standard_save(SaveBlock)
// load ZERO_PAGE_LO and ZERO_PAGE_HI with addr of sprite extra data
nv_sprite_load_extra_ptr()
// get the sprite number in X reg
nv_sprite_extra_byte_to_x(NV_SPRITE_NUM_OFFSET)
//ldy #NV_SPRITE_NUM_OFFSET // load Y reg with offset to sprite number
//lda (ZERO_PAGE_LO),y // indirect indexed load sprite num to accum
//tax // keep sprite number in X reg
nv_sprite_extra_word_to_mem(NV_SPRITE_DATA_PTR_OFFSET, scratch_word)
//scratch_word now has the data ptr in it
// store sprite data pointer in scratch word
lda scratch_word
sta ZERO_PAGE_LO
lda scratch_word+1
sta ZERO_PAGE_HI
// our zero page pointer now points to the sprite data
// the 63rd byte of which contains the color data
ldy #63
lda (ZERO_PAGE_LO), y
// now accum has the color data in the low nibble
// and X has the sprite number
// write the color data to the color data register for this
// sprite number. write the whole byte because only the
// low nibble is writable
sta NV_SPRITE_0_COLOR_REG_ADDR,x // store in color reg for this sprite
nv_sprite_standard_restore(SaveBlock)
rts
SaveBlock:
nv_sprite_standard_alloc()
}
//////////////////////////////////////////////////////////////////////////////
// To call this subroutine setup the following then JSR
// Accum: MSB of address of nv_sprite_extra_data
// X Reg: LSB of address of the nv_sprite_extra_data
.macro nv_sprite_set_mode_from_extra_sr()
{
nv_sprite_standard_save(SaveBlock)
// load ZERO_PAGE_LO and ZERO_PAGE_HI with addr of sprite extra data
nv_sprite_load_extra_ptr()
// get the sprite number in X reg then mask for it in Accum
nv_sprite_extra_byte_to_x(NV_SPRITE_NUM_OFFSET)
nv_sprite_get_mask_in_a()
pha // push mask on to stack
// replace zero page pointer that was pointing to extra block to
// instead point to the 64 bytes of sprite data
nv_sprite_data_ptr_to_zero_page()
// our zero page pointer now points to the sprite data
// the 64th byte of which contains the color data
ldy #63
lda (ZERO_PAGE_LO), y
// accum now has the 64th byte of the sprite data
// if any of the four bits in the high nibble are set then
// the sprite is multi color (low res). If
// no bits in the high nibble are set then
// its hi res (single color)
ldx #$F0
stx scratch_byte
bit scratch_byte
beq SingleColor // if none of the high 4 bits set then single color
MultiColor:
// if fell through here then multi color mode
// for multi color mode we need to set the bit for this sprite
// in the sprite mode register
pla // pop the sprite mask to the accumulator
ora NV_SPRITE_MODE_REG_ADDR // or the mask in accum with sprite register
sta NV_SPRITE_MODE_REG_ADDR // store the updated value in sprite reg
nv_sprite_standard_restore(SaveBlock)
rts
SingleColor:
// for single color mode we need to clear the bit in the
// sprite mode register that corresponds to our sprite
pla // pop the sprite mask to the accum
eor #$ff // negate the mask
and NV_SPRITE_MODE_REG_ADDR // clear bit for this sprite
sta NV_SPRITE_MODE_REG_ADDR // store updated sprite reg back
nv_sprite_standard_restore(SaveBlock)
rts
SaveBlock:
nv_sprite_standard_alloc()
}
//////////////////////////////////////////////////////////////////////////////
// Set the sprite hardware register for the sprite data "pointer"
// for a specific sprite (0-7)
// To call subroutine setup the following then JSR
// Accum: MSB of address of nv_sprite_extra_data
// X Reg: LSB of address of the nv_sprite_extra_data
.macro nv_sprite_set_data_ptr_from_extra_sr()
{
nv_sprite_standard_save(SaveBlock)
// load ZERO_PAGE_LO and ZERO_PAGE_HI with addr of sprite extra data
nv_sprite_load_extra_ptr()
// copy sprite data pointer to scratch_word
nv_sprite_extra_word_to_mem(NV_SPRITE_DATA_PTR_OFFSET, scratch_word)
//scratch_word now has the data ptr in it
nv_lsr16u_mem16u_immed8u(scratch_word, 6) // dividing by 64 (more or less)
// the low byte of scratch_word now has
// the sprite data block number
// the posible remaining 2 high bits
// are ignored.
// get the sprite number in X reg
nv_sprite_extra_byte_to_x(NV_SPRITE_NUM_OFFSET)
lda scratch_word // implied this is multiplied by 64 by system
sta NV_SPRITE_0_DATA_PTR_REG_ADDR,x // store in ptr for this sprite
nv_sprite_standard_restore(SaveBlock)
rts
SaveBlock:
nv_sprite_standard_alloc()
}
.macro nv_sprite_push_extra_ptr()
{
// save A and X on stack
pha // push A (hi byte)
tay // save A (hi byte)
txa // lo byte
pha // save lo byte
tax // lo byte back to X
tya // hi byte back to A
}
.macro nv_sprite_pop_extra_ptr()
{
// restore A and X
pla // pop low byte to A
tax // move lo byte to X
pla // pop hi byte to A
}
///////////////////////////////////////////////////////////////////////////////
// macro to save the Accum, the X and the values in the zero page
// locations that we use for indirection in most routines that need to
// indirectly access the sprite's extra data block
// Subroutines that take the extra data address in Accum, and X
// and then reference the fields with ZERO_PAGE_HI and ZERO_PAGE_LO
// can use this macro to save the state and then use
// nv_sprite_standard_restore() macro to restore them befor returning.
// note that if used then the following labels need one byte each
// allocated
// save_a, save_x, save_zero_lo, save_zero_hi
// space can be allocated via including the nv_sprite_standard_alloc() macro
.macro nv_sprite_standard_save(save_block)
{
sta save_block
stx save_block+1
ldy ZERO_PAGE_LO
sty save_block+2
ldy ZERO_PAGE_HI
sty save_block+3
}
//////////////////////////////////////////////////////////////////////////////
// inline macro to allocate enough memory to save commonly used registers
// and memory contents such as:
// Accumulator
// X register
// ZERO_PAGE_HI
// ZERO_PAGE_LO
// use this macro to allocate the memory block passed to the
// nv_sprite_standard_save() and nv_sprite_standard_restore() macros
.macro nv_sprite_standard_alloc()
{
save_a: .byte 0
save_x: .byte 0
save_zero_lo: .byte 0
save_zero_hi: .byte 0
}
//////////////////////////////////////////////////////////////////////////////
// inline macro to restore commonly used registers
// and memory contents such as:
// Accumulator
// X register
// ZERO_PAGE_HI
// ZERO_PAGE_LO
// use this macro to restore the registers and memory locations to the values
// that were saved from the nv_sprite_standard_save() macro to the same
// block of memory.
.macro nv_sprite_standard_restore(save_block)
{
lda save_block
ldx save_block+1
ldy save_block+2
sty ZERO_PAGE_LO
ldy save_block+3
sty ZERO_PAGE_HI
}
//////////////////////////////////////////////////////////////////////////////
// setup a sprite based on its extra data
// To call subroutine setup the following then JSR
// Accum: MSB of address of nv_sprite_extra_data
// X Reg: LSB of address of the nv_sprite_extra_data
.macro nv_sprite_setup_from_extra_sr()
{
sta save_hi
stx save_lo
jsr NvSpriteSetModeFromExtra
lda save_hi
ldx save_lo
jsr NvSpriteSetDataPtrFromExtra
lda save_hi
ldx save_lo
jsr NvSpriteSetColorFromExtra
rts
save_hi: .byte 0
save_lo: .byte 0
}
//////////////////////////////////////////////////////////////////////////////
// inline macro to create an 8 bit mask for the sprite number that is in
// the x register.
// X Reg: must contain sprite number (from 0 to 7)
// Accum: will contain a mask for the sprite number if sprite number in X
// is 0 the mask will have the 0 bit set, ie: $01. if the sprite
// num in X is 1 then the 1 bit in mask will be set, ie: $02, etc.
.macro nv_sprite_get_mask_in_a()
{
// sprite number assumed to be in X register already
lda #0 // load Accum with 0
sec // set carry flag so first rol will rotate in a 1
Loop:
rol // rotate the 1 until we get to our sprite's bit
dex // dec X reg until beyond 0 when we can stop rotating
bpl Loop // when dex cause us to roll from 0 to FF then exit loop
// now the accumulator has the sprite mask for sprite num
}
//////////////////////////////////////////////////////////////////////////////
// subroutine macro to set sprite's location in the sprite registers based on
// the appropriate values in the sprite extra data block
// To call subroutine setup the following then JSR
// Accum: MSB of address of nv_sprite_extra_data
// X Reg: LSB of address of the nv_sprite_extra_data
.macro nv_sprite_set_location_from_extra_sr()
{
nv_sprite_standard_save(SaveBlock)
nv_sprite_load_extra_ptr()
// get sprite number in accum
nv_sprite_extra_byte_to_a(NV_SPRITE_NUM_OFFSET)
pha // push accum (sprite number 0-7)
// multiply by 2 and put in x reg. Need to multiply by 2 because
// there x and y location together for each sprite.
asl
tax
// get sprite x location from extra data block to accum
nv_sprite_extra_byte_to_a(NV_SPRITE_X_OFFSET)
// store the x location to the correct sprite register
sta NV_SPRITE_0_X_REG_ADDR,x // store in right sprite's x loc
// get sprite y location from extra data block to accum
nv_sprite_extra_byte_to_a(NV_SPRITE_Y_OFFSET)
// store y position to correct sprite register
sta NV_SPRITE_0_Y_REG_ADDR,x // store in right sprites y loc
// load MSB of sprite X position to A
nv_sprite_extra_byte_to_a(NV_SPRITE_X_OFFSET + 1)
bne SetBit // high byte was non zero, so set bit
// clear bit
// create a sprite mask for our sprite number in accumulator and negate it
pla // pop sprite number off stack to accum
tax // move sprite number to X reg
nv_sprite_get_mask_in_a()
eor #$ff // negate mask so our bit is 0, other bits 1s
// and with reg that holds all the sprite x hi bits
// then store it back to the same register so our sprite's bit is clear
and NV_SPRITE_ALL_X_HIGH_BIT_REG_ADDR
sta NV_SPRITE_ALL_X_HIGH_BIT_REG_ADDR
nv_sprite_standard_restore(SaveBlock)
rts
SetBit:
// setting bit for the sprite
pla // pop sprite num to accum
tax // sprite num to x for get mask macro
nv_sprite_get_mask_in_a() // get a mask for our sprite num
ora NV_SPRITE_ALL_X_HIGH_BIT_REG_ADDR // or with the reg of all hi X bits
sta NV_SPRITE_ALL_X_HIGH_BIT_REG_ADDR // store back with our bit set
nv_sprite_standard_restore(SaveBlock)
rts
SaveBlock:
nv_sprite_standard_alloc()
}
//////////////////////////////////////////////////////////////////////////////
// subroutine to move a sprite based on information in the sprite extra
// struct (info) that is passed into the macro. The sprite x and y location
// in memory will be updated according to the x and y velocity.
// Note if the sprite goes off the edge it will be reset to the opposite side
// of the screen or bounce based on sprite extra data
// Note that this only updates the location in memory, it doesn't update the
// sprite location in sprite registers. To update sprite location in registers
// and on the screen, call nv_sprite_set_location_from_memory_sr after this.
.macro nv_sprite_move_in_extra_sr()
{
// save standard regs and memory values
nv_sprite_standard_save(SaveBlock)
// load the extra pointer from accum/X reg to zero page location
nv_sprite_load_extra_ptr()
// get sprite Y velocity in accum and branch for pos or negative
nv_sprite_extra_byte_to_a(NV_SPRITE_VEL_Y_OFFSET)
bpl PosVelY
NegVelY:
jsr NvSpriteMoveInExtraNegY
jmp DoneY
PosVelY:
jsr NvSpriteMoveInExtraPosY
jmp DoneY
DoneY:
// Y location done, now on to X
// get sprite x velocity in accum and branch for pos or negative
nv_sprite_extra_byte_to_a(NV_SPRITE_VEL_X_OFFSET)
bpl PosVelX
NegVelX:
jsr NvSpriteMoveInExtraNegX
jmp DoneX
PosVelX:
jsr NvSpriteMoveInExtraPosX
//jmp DoneX
DoneX:
FinishedUpdate:
nv_sprite_standard_restore(SaveBlock)
rts // already popped the return address, jump back now
SaveBlock:
nv_sprite_standard_alloc()
}
//////////////////////////////////////////////////////////////////////////////
// move sprite in positive X direction and bounce or wrap around
// Before calling the following must be set
// ZERO_PAGE_LO: must have the LSB of the address of the sprite extra data
// ZERO_PAGE_HI: must have the MSB of the address of the sprite extra data
// Accumulator: must have the sprites X velocity which must be positive
.macro nv_sprite_move_in_extra_pos_x_sr()
{
sta velocity // move x vel to memory
nv_sprite_extra_word_to_mem(NV_SPRITE_RIGHT_MAX_OFFSET, max_x)
nv_sprite_extra_byte_to_a(NV_SPRITE_VEL_X_OFFSET)
sta velocity
nv_sprite_extra_word_to_mem(NV_SPRITE_X_OFFSET, cur_x)
nv_adc16x_mem16x_mem8s(cur_x, velocity, potential_new_x)
// potential_new_x has the new x if not bouncing or wrapping
nv_ble16(potential_new_x, max_x, UsePotentialX)
TooFar:
// if didn't branch above then trying to move too far. Need
// to bounce or wrap to the other side
nv_sprite_extra_byte_to_a(NV_SPRITE_ACTION_RIGHT_OFFSET)
beq WrapX // action 0=wrap, 1=bounce
BounceX:
// didn't branch so bounce by setting X vel to twos compliement
lda #$ff
eor velocity
sec
adc #$00
nv_sprite_a_to_extra(NV_SPRITE_VEL_X_OFFSET)
jmp DoneX
WrapX:
// wrap by setting x to min position
nv_sprite_extra_word_to_mem(NV_SPRITE_LEFT_MIN_OFFSET, potential_new_x)
// fall through to UsePotentialX
UsePotentialX:
nv_sprite_mem_word_to_extra(potential_new_x, NV_SPRITE_X_OFFSET)
DoneX:
rts
// subroutine variables
velocity: .byte 0
potential_new_x: .word 0
cur_x: .word 0
max_x: .word 0
}
//////////////////////////////////////////////////////////////////////////////
// move sprite in negative X direction and bounce or wrap around
// Before calling the following must be set
// ZERO_PAGE_LO: must have the LSB of the address of the sprite extra data
// ZERO_PAGE_HI: must have the MSB of the address of the sprite extra data
// Accumulator: must have the sprites X velocity which must be positive
.macro nv_sprite_move_in_extra_neg_x_sr()
{
sta velocity // move x vel to memory
nv_sprite_extra_word_to_mem(NV_SPRITE_LEFT_MIN_OFFSET, min_x)
nv_sprite_extra_word_to_mem(NV_SPRITE_RIGHT_MAX_OFFSET, max_x)
nv_sprite_extra_byte_to_a(NV_SPRITE_VEL_X_OFFSET)
sta velocity
nv_sprite_extra_word_to_mem(NV_SPRITE_X_OFFSET, cur_x)
nv_adc16x_mem16x_mem8s(cur_x, velocity, potential_new_x)
// potential_new_x has the new x if not off left edge
nv_bgt16(potential_new_x, max_x, TooFar)
nv_bgt16(potential_new_x, min_x, UsePotentialX)
TooFar:
// if didn't branch above then trying to move too far. Need
// to bounce or wrap to the other side
nv_sprite_extra_byte_to_a(NV_SPRITE_ACTION_LEFT_OFFSET)
beq WrapX // action 0=wrap, 1=bounce
BounceX:
// didn't branch above, so bounce by setting X vel to twos compliement
lda #$ff
eor velocity
sec
adc #$00
nv_sprite_a_to_extra(NV_SPRITE_VEL_X_OFFSET)
jmp DoneX
WrapX:
// wrap by setting x to max position
nv_sprite_extra_word_to_mem(NV_SPRITE_RIGHT_MAX_OFFSET, potential_new_x)
// fall through to UsePotentialX
UsePotentialX:
nv_sprite_mem_word_to_extra(potential_new_x, NV_SPRITE_X_OFFSET)
DoneX:
rts
// subroutine variables
velocity: .byte 0
potential_new_x: .word 0
cur_x: .word 0
min_x: .word 0
max_x: .word 0
}
//////////////////////////////////////////////////////////////////////////////
// move sprite in negative Y direction and bounce or wrap around
// Before calling the following must be set
// ZERO_PAGE_LO: must have the LSB of the address of the sprite extra data
// ZERO_PAGE_HI: must have the MSB of the address of the sprite extra data
// Accumulator: must have the sprites Y velocity which must be negative
.macro nv_sprite_move_in_extra_neg_y_sr()
{
sta velocity // move y vel to memory
nv_sprite_extra_byte_to_a(NV_SPRITE_TOP_MIN_OFFSET)
sta min_position
// get sprite Y position in accum
nv_sprite_extra_byte_to_a(NV_SPRITE_Y_OFFSET)
clc
adc velocity // add the velocity to the position
// accum has potential next position
cmp min_position // compare with max y position
bcs AccumHasNewY // if not past max y then we are done
TooFar:
// new position is too far, either bounce or wrap
nv_sprite_extra_byte_to_a(NV_SPRITE_ACTION_TOP_OFFSET)
beq WrapY // action 0 = Wrap, 1 = bounce
BounceY:
// bounce by setting y velocity to its twos compliment
lda #$ff
eor velocity
sec
adc #$00
nv_sprite_a_to_extra(NV_SPRITE_VEL_Y_OFFSET)
jmp DoneY
WrapY:
// wrap by setting y to min position
// bounce by negating y velocity
nv_sprite_extra_byte_to_a(NV_SPRITE_BOTTOM_MAX_OFFSET)
nv_sprite_a_to_extra(NV_SPRITE_Y_OFFSET)
// fall through to AccumHasNewY
AccumHasNewY:
nv_sprite_a_to_extra(NV_SPRITE_Y_OFFSET)
DoneY:
rts
// subroutine variables
velocity: .byte 0
min_position: .byte 0
}
//////////////////////////////////////////////////////////////////////////////
// move sprite in positive Y direction and bounce or wrap around
// Before calling the following must be set
// ZERO_PAGE_LO: must have the LSB of the address of the sprite extra data
// ZERO_PAGE_HI: must have the MSB of the address of the sprite extra data
// Accumulator: must have the sprites Y velocity which must be positive
.macro nv_sprite_move_in_extra_pos_y_sr()
{
sta velocity // move y vel to memory
nv_sprite_extra_byte_to_a(NV_SPRITE_BOTTOM_MAX_OFFSET)
sta max_position
// get sprite Y position in accum
nv_sprite_extra_byte_to_a(NV_SPRITE_Y_OFFSET)
clc
adc velocity // add the velocity to the position
// accum has potential next position
cmp max_position // compare with max y position
bcc AccumHasNewY // if not past max y then we are done
TooFar:
// new position is too far, either bounce or wrap
nv_sprite_extra_byte_to_a(NV_SPRITE_ACTION_BOTTOM_OFFSET)
beq WrapY // action 0 = wrap, 1 = bounce
BounceY:
// bounce by setting y velocity to its twos compliment
lda #$ff
eor velocity
sec
adc #$00
nv_sprite_a_to_extra(NV_SPRITE_VEL_Y_OFFSET)
jmp DoneY
WrapY:
// wrap by setting y to min position
// bounce by negating y velocity
nv_sprite_extra_byte_to_a(NV_SPRITE_TOP_MIN_OFFSET)
nv_sprite_a_to_extra(NV_SPRITE_Y_OFFSET)
// fall through to AccumHasNewY
AccumHasNewY:
nv_sprite_a_to_extra(NV_SPRITE_Y_OFFSET)
DoneY:
rts
// subroutine variables
velocity: .byte 0
max_position: .byte 0
}
//////////////////////////////////////////////////////////////////////////////
// enable a sprite and update its sprite_enabled flag in its extra data
// To call subroutine setup the following then JSR
// Accum: MSB of address of nv_sprite_extra_data
// X Reg: LSB of address of the nv_sprite_extra_data
.macro nv_sprite_extra_enable_sr()
{
nv_sprite_standard_save(SaveBlock)
nv_sprite_load_extra_ptr()
// set enabled flag in extra
lda #$01
nv_sprite_a_to_extra(NV_SPRITE_ENABLED_OFFSET)
// get sprite number in accum
nv_sprite_extra_byte_to_a(NV_SPRITE_NUM_OFFSET)
// now enable the sprite
nv_sprite_raw_enable_from_reg()
nv_sprite_standard_restore(SaveBlock)
rts
SaveBlock:
nv_sprite_standard_alloc()
}
//////////////////////////////////////////////////////////////////////////////
// disable a sprite and update its sprite_enabled flag in its extra data
// To call subroutine setup the following then JSR
// Accum: MSB of address of nv_sprite_extra_data
// X Reg: LSB of address of the nv_sprite_extra_data
.macro nv_sprite_extra_disable_sr()
{
nv_sprite_standard_save(SaveBlock)
nv_sprite_load_extra_ptr()
// clear enabled flag in extra
lda #$00
nv_sprite_a_to_extra(NV_SPRITE_ENABLED_OFFSET)
// get sprite number in accum
nv_sprite_extra_byte_to_a(NV_SPRITE_NUM_OFFSET)
// now enable the sprite
nv_sprite_raw_disable_from_reg()
nv_sprite_standard_restore(SaveBlock)
rts
SaveBlock:
nv_sprite_standard_alloc()
}
//////////////////////////////////////////////////////////////////////////////
// reverse the x velocity of sprite
// Accum: MSB of address of nv_sprite_extra_data
// X Reg: LSB of address of the nv_sprite_extra_data
.macro nv_sprite_extra_reverse_vel_x_sr()
{
nv_sprite_standard_save(SaveBlock)
nv_sprite_load_extra_ptr()
// load accum with x velocity
nv_sprite_extra_byte_to_a(NV_SPRITE_VEL_X_OFFSET)
nv_twos_comp8x_a()
nv_sprite_a_to_extra(NV_SPRITE_VEL_X_OFFSET)
nv_sprite_standard_restore(SaveBlock)
rts
SaveBlock:
nv_sprite_standard_alloc()
}
//////////////////////////////////////////////////////////////////////////////
// reverse the y velocity of sprite
// Accum: MSB of address of nv_sprite_extra_data
// X Reg: LSB of address of the nv_sprite_extra_data
.macro nv_sprite_extra_reverse_vel_y_sr()
{
nv_sprite_standard_save(SaveBlock)
nv_sprite_load_extra_ptr()
// load accum with x velocity
nv_sprite_extra_byte_to_a(NV_SPRITE_VEL_Y_OFFSET)
nv_twos_comp8x_a()
nv_sprite_a_to_extra(NV_SPRITE_VEL_Y_OFFSET)
nv_sprite_standard_restore(SaveBlock)
rts
SaveBlock:
nv_sprite_standard_alloc()
}
//////////////////////////////////////////////////////////////////////////////
// make sure the sprite's x velocity is positive, if its negative, then
// then reverse it, if its alsready positive then do nothing.
// Accum: MSB of address of nv_sprite_extra_data
// X Reg: LSB of address of the nv_sprite_extra_data
.macro nv_sprite_extra_assure_vel_pos_x_sr()
{
nv_sprite_standard_save(SaveBlock)
nv_sprite_load_extra_ptr()
// load accum with x velocity
nv_sprite_extra_byte_to_a(NV_SPRITE_VEL_X_OFFSET)
bpl NothingToDo // x velocity already positive, just return
nv_twos_comp8x_a() // reverse the x velocity
nv_sprite_a_to_extra(NV_SPRITE_VEL_X_OFFSET) // save back to extra data
NothingToDo:
nv_sprite_standard_restore(SaveBlock)
rts
SaveBlock:
nv_sprite_standard_alloc()
}
//////////////////////////////////////////////////////////////////////////////
// make sure the sprite's x velocity is negative, if its positive, then
// then reverse it, if its alsready negative then do nothing.
// Accum: MSB of address of nv_sprite_extra_data
// X Reg: LSB of address of the nv_sprite_extra_data
.macro nv_sprite_extra_assure_vel_neg_x_sr()
{
nv_sprite_standard_save(SaveBlock)
nv_sprite_load_extra_ptr()
// load accum with x velocity
nv_sprite_extra_byte_to_a(NV_SPRITE_VEL_X_OFFSET)
bmi NothingToDo // x velocity already positive, just return
nv_twos_comp8x_a() // reverse the x velocity
nv_sprite_a_to_extra(NV_SPRITE_VEL_X_OFFSET) // save back to extra data
NothingToDo:
nv_sprite_standard_restore(SaveBlock)
rts
SaveBlock:
nv_sprite_standard_alloc()
}
//////////////////////////////////////////////////////////////////////////////
// assure the y velocity of sprite is positive. If its negative then
// reverse it. If its alredy positive then do nothing.
// Accum: MSB of address of nv_sprite_extra_data
// X Reg: LSB of address of the nv_sprite_extra_data
.macro nv_sprite_extra_assure_vel_pos_y_sr()
{
nv_sprite_standard_save(SaveBlock)
nv_sprite_load_extra_ptr()
// load accum with x velocity
nv_sprite_extra_byte_to_a(NV_SPRITE_VEL_Y_OFFSET)
bpl NothingToDo // if already positive, then done
nv_twos_comp8x_a() // reverse velocity
nv_sprite_a_to_extra(NV_SPRITE_VEL_Y_OFFSET) // save vel back to extra
NothingToDo:
nv_sprite_standard_restore(SaveBlock)
rts
SaveBlock:
nv_sprite_standard_alloc()
}
//////////////////////////////////////////////////////////////////////////////
// assure the y velocity of sprite is negative. If its positive then
// reverse it. If its already negative then do nothing.
// Accum: MSB of address of nv_sprite_extra_data
// X Reg: LSB of address of the nv_sprite_extra_data
.macro nv_sprite_extra_assure_vel_neg_y_sr()
{
nv_sprite_standard_save(SaveBlock)
nv_sprite_load_extra_ptr()
// load accum with x velocity
nv_sprite_extra_byte_to_a(NV_SPRITE_VEL_Y_OFFSET)
bmi NothingToDo // if already negative, then done
nv_twos_comp8x_a() // reverse velocity
nv_sprite_a_to_extra(NV_SPRITE_VEL_Y_OFFSET) // save vel back to extra
NothingToDo:
nv_sprite_standard_restore(SaveBlock)
rts
SaveBlock:
nv_sprite_standard_alloc()
}
//////////////////////////////////////////////////////////////////////////////
// macro subroutine to get the center X and Y screen position of a sprite's
// hitbox
// Macro params:
// center_x_addr: the memory address pointing to a 16 bit word that will
// get the center X location
// center_y_addr: the memory address pointing to a 16 bit word that will
// get the center Y location
// subroutine params
// Accum: MSB of address of nv_sprite_extra_data
// X Reg: LSB of address of the nv_sprite_extra_data
.macro nv_sprite_extra_get_hitbox_center_sr(center_x_addr, center_y_addr)
{
nv_sprite_standard_save(SaveBlock)
nv_sprite_load_extra_ptr()
// get the sprite x center location
nv_sprite_extra_word_to_mem(NV_SPRITE_X_OFFSET, center_x_addr)
nv_sprite_extra_byte_to_mem(NV_SPRITE_HITBOX_LEFT_OFFSET, HitboxLeft)
nv_sprite_extra_byte_to_mem(NV_SPRITE_HITBOX_RIGHT_OFFSET, HitboxRight)
nv_sbc8x(HitboxRight, HitboxLeft, HitboxWidth)
lsr HitboxWidth // hitbox width divided by 2
nv_adc16x_mem16x_mem8u(center_x_addr, HitboxWidth, center_x_addr)
// get the sprite y center location
nv_sprite_extra_byte_to_a(NV_SPRITE_Y_OFFSET)
sta center_y_addr
lda #$00
sta center_y_addr+1
nv_sprite_extra_byte_to_mem(NV_SPRITE_HITBOX_TOP_OFFSET, HitboxTop)
nv_sprite_extra_byte_to_mem(NV_SPRITE_HITBOX_BOTTOM_OFFSET, HitboxBottom)
nv_sbc8x(HitboxBottom, HitboxTop, HitboxHeight)
lsr HitboxHeight // hitbox height divided by 2
nv_adc16x_mem16x_mem8u(center_y_addr, HitboxHeight, center_y_addr)
nv_sprite_standard_restore(SaveBlock)
rts
// use same words for left/top, right/bottom, width/height
HitboxTop:
HitboxLeft: .byte $00
HitboxBottom:
HitboxRight: .byte $00
HitboxHeight:
HitboxWidth: .byte $00
SaveBlock:
nv_sprite_standard_alloc()
}
//////////////////////////////////////////////////////////////////////////////
//
//////////////////////////////////////////////////////////////////////////////
// Inline macro to test if a sprite's hitbox overlaps with a prefilled
// rectangle
//
// Params:
// Accum: MSB of address of nv_sprite_extra_data
// X Reg: LSB of address of the nv_sprite_extra_data
// macro params:
// rect_addr: is address of retangle whose contents will be tested for
// overlap with the sprite's rectangle.. the contents must
// be prefilled with coords
// Return: loads the accum with 0 for no overlap or nonzero if is overlap
// Note: the Zero page locations use will be restored. The accumulator will not
// be restored because the result will be in the accum (0 or 1)
.macro nv_sprite_check_overlap_rect_sr(rect_addr)
{
nv_sprite_standard_save(SaveBlock)
nv_sprite_load_extra_ptr()
.label r2_left = rect_addr
.label r2_top = rect_addr + 2
.label r2_right = rect_addr + 4
.label r2_bottom = rect_addr + 6
.const SPRITE_WIDTH = 24
.const SPRITE_HEIGHT = 21
.const LEFT_OFFSET = 26
.const TOP_OFFSET = 53
.const CHAR_PIXEL_WIDTH = $0008
.const CHAR_PIXEL_HEIGHT = $0008
/////////// put sprite's rectangle to rect1, use the hitbox not full sprite
// get the sprite left and right hitbox coords as screen pixel coords
// and put in r1_left, r1_right
nv_sprite_extra_word_to_mem(NV_SPRITE_X_OFFSET, r1_left)
nv_sprite_extra_byte_to_mem(NV_SPRITE_HITBOX_RIGHT_OFFSET, hitbox_right)
nv_adc16x_mem16x_mem8u(r1_left, hitbox_right, r1_right)
nv_sprite_extra_byte_to_mem(NV_SPRITE_HITBOX_LEFT_OFFSET, hitbox_left)
nv_adc16x_mem16x_mem8u(r1_left, hitbox_left, r1_left)
// get the sprite top and bottom hitbox coords as screen pixel coords
// and put in r1_top, r1_bottom
nv_sprite_extra_byte_to_a(NV_SPRITE_Y_OFFSET)
sta r1_top
lda #$00
sta r1_top+1
nv_sprite_extra_byte_to_mem(NV_SPRITE_HITBOX_TOP_OFFSET, hitbox_top)
nv_sprite_extra_byte_to_mem(NV_SPRITE_HITBOX_BOTTOM_OFFSET, hitbox_bottom)
nv_adc16x_mem16x_mem8u(r1_top, hitbox_bottom, r1_bottom)
nv_adc16x_mem16x_mem8u(r1_top, hitbox_top, r1_top)
// now check for overlap with sprite_rect and the rect for this macro
nv_check_rect_overlap16(sprite_rect, rect_addr)
// use hitbox_left as a temp, finished with it above
// the standard restore will mess up our accumulator so save it here
sta hitbox_left
nv_sprite_standard_restore(SaveBlock)
// restore the accumulator from our temp
lda hitbox_left