-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathnv_sprite_extra_macs.asm
1056 lines (912 loc) · 39 KB
/
nv_sprite_extra_macs.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_macs.asm
// Copyright(c) 2021 Neal Smith.
// License: MIT. See LICENSE file in root directory.
//////////////////////////////////////////////////////////////////////////////
// macros to access the sprite extra data block
// including read/write to/from the extra data block
//////////////////////////////////////////////////////////////////////////////
#importonce
#if !NV_C64_UTIL_DATA
.error "Error - nv_sprite_extra_macs.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_raw_macs.asm"
#import "nv_screen_rect_macs.asm"
// zero page pointer to use whenever a zero page pointer is needed
// usually used to store and load to and from the sprite extra pointer
.const ZERO_PAGE_LO = $FB
.const ZERO_PAGE_HI = $FC
// Constants for screen edges for bouncing.
// These are the default max position values when bouncing
.const NV_SPRITE_LEFT_BOUNCE_DEFAULT = 23
.const NV_SPRITE_RIGHT_BOUNCE_DEFAULT = 320
.const NV_SPRITE_TOP_BOUNCE_DEFAULT = 50
.const NV_SPRITE_BOTTOM_BOUNCE_DEFAULT = 234
// These are the default max position values when wrapping
.const NV_SPRITE_LEFT_WRAP_DEFAULT = 2
.const NV_SPRITE_RIGHT_WRAP_DEFAULT = 339
.const NV_SPRITE_TOP_WRAP_DEFAULT = 32
.const NV_SPRITE_BOTTOM_WRAP_DEFAULT = 249
// These are the possible actions when sprite would exceed max position
.const NV_SPRITE_ACTION_WRAP = 0
.const NV_SPRITE_ACTION_BOUNCE = 1
// struct that provides info for a sprite. this is a construct of the assembler
// it just provides an easy way to reference all these different compile time values.
// No actual memory is created when an instance of the struct is created.
.struct nv_sprite_info_struct{name, num, init_x, init_y, init_x_vel, init_y_vel, data_ptr,
base_addr, action_top, bounce_left, bounce_bottom, bounce_right,
top_min, left_min, bottom_max, right_max, enabled,
hitbox_left, hitbox_top, // coords within sprite
hitbox_right, hitbox_bottom} // coords within sprite
//////////////////////////////////////////////////////////////////////////////
// macro that creates a block of memory to hold all the extra data for a
// sprite such as its velocity and an in memory x and y location.
// macro parameter:
// spt_info: is an instance of the nv_sprite_info_struct. This contains
// all the compile time info needed/known about the strite.
.macro nv_sprite_extra_data(spt_info)
{
*=spt_info.base_addr // tell assembler where to put this stuff
sprite_base_addr: // the address of the first byte
sprite_num_addr: .byte spt_info.num // the sprite number (0-7 only)
sprite_x_addr: .word spt_info.init_x // the sprite's x loc
sprite_y_addr: .byte spt_info.init_y // the sprite's y loc
sprite_vel_x_addr: .byte spt_info.init_x_vel // the sprite's x velocity in pixels
sprite_vel_y_addr: .byte spt_info.init_y_vel // the sprite's y velocity in pixels
sprite_data_ptr_addr: .word spt_info.data_ptr // 16 bit addr of sprite data
sprite_action_top: .byte spt_info.action_top // set to 1 to bounce bottom or 0 not to
sprite_bounce_left: .byte spt_info.bounce_left // set to 1 to bounce bottom or 0 not to
sprite_bounce_bottom: .byte spt_info.bounce_bottom // set to 1 to bounce bottom or 0 not to
sprite_bounce_right: .byte spt_info.bounce_right // set to 1 to bounce bottom or 0 not to
// top boundry for the sprite
sprite_top_min_addr: .byte spt_info.top_min == 0 ? (spt_info.action_top == NV_SPRITE_ACTION_BOUNCE ? NV_SPRITE_TOP_BOUNCE_DEFAULT : NV_SPRITE_TOP_WRAP_DEFAULT) : spt_info.top_min
// left boundry for the sprite
sprite_left_min_addr: .word spt_info.left_min == 0 ? (spt_info.bounce_left == 1 ? NV_SPRITE_LEFT_BOUNCE_DEFAULT : NV_SPRITE_LEFT_WRAP_DEFAULT) :spt_info.left_min
// bottom boundry for the sprite
sprite_bottom_max_addr: .byte spt_info.bottom_max == 0 ? (spt_info.bounce_bottom == 1 ? NV_SPRITE_BOTTOM_BOUNCE_DEFAULT : NV_SPRITE_BOTTOM_WRAP_DEFAULT) :spt_info.bottom_max
// right boundry for the sprite
sprite_right_max_addr: .word spt_info.right_max == 0 ? (spt_info.bounce_right == 1 ? NV_SPRITE_RIGHT_BOUNCE_DEFAULT : NV_SPRITE_RIGHT_WRAP_DEFAULT) : spt_info.right_max
// sprite enabled flag. nonzero is enabled, zero is disabled
sprite_enabled: .byte 0
// the hitbox coords are within the sprite's rectangle where the upper left
// corner of the sprite (ie the sprites official location) is (0, 0)
// So the min value will be 0 and max will be sprite height/width.
// To get the screen coords of the hitbox just add the sprite location to
// the hitbox coords within the sprite.
sprite_hitbox_left_addr: .byte spt_info.hitbox_left
sprite_hitbox_top_addr: .byte spt_info.hitbox_top
sprite_hitbox_right_addr: .byte spt_info.hitbox_right
sprite_hitbox_bottom_addr: .byte spt_info.hitbox_bottom
// some scratch memory for each sprite
sprite_scratch1: .word 0
sprite_scratch2: .word 0
sprite_scratch_rect:
sprite_scratch_rect_left: .word 0
sprite_scratch_rect_top: .word 0
sprite_scratch_rect_right: .word 0
sprite_scratch_rect_bottom: .word 0
}
//////////////////////////////////////////////////////////////////////////////
// offsets to use to get to the different fields within the nv_sprite block
.const NV_SPRITE_NUM_OFFSET = 0
.const NV_SPRITE_X_OFFSET = 1
.const NV_SPRITE_Y_OFFSET = 3
.const NV_SPRITE_VEL_X_OFFSET = 4
.const NV_SPRITE_VEL_Y_OFFSET = 5
.const NV_SPRITE_DATA_PTR_OFFSET = 6
.const NV_SPRITE_ACTION_TOP_OFFSET = 8
.const NV_SPRITE_ACTION_LEFT_OFFSET = 9
.const NV_SPRITE_ACTION_BOTTOM_OFFSET = 10
.const NV_SPRITE_ACTION_RIGHT_OFFSET = 11
.const NV_SPRITE_TOP_MIN_OFFSET = 12
.const NV_SPRITE_LEFT_MIN_OFFSET = 13
.const NV_SPRITE_BOTTOM_MAX_OFFSET = 15
.const NV_SPRITE_RIGHT_MAX_OFFSET = 16
.const NV_SPRITE_ENABLED_OFFSET = 18
.const NV_SPRITE_HITBOX_LEFT_OFFSET = 19
.const NV_SPRITE_HITBOX_TOP_OFFSET = 20
.const NV_SPRITE_HITBOX_RIGHT_OFFSET = 21
.const NV_SPRITE_HITBOX_BOTTOM_OFFSET = 22
.const NV_SPRITE_SCRATCH1_OFFSET = 23
.const NV_SPRITE_SCRATCH2_OFFSET = 25
// 8 bytes to create four 16 bit coords for a rect (left, top, right, bottom)
.const NV_SPRITE_SCRATCH_RECT_OFFSET = 27
.const NV_SPRITE_SCRATCH_RECT_LEFT_OFFSET = 27
.const NV_SPRITE_SCRATCH_RECT_TOP_OFFSET = 29
.const NV_SPRITE_SCRATCH_RECT_RIGHT_OFFSET = 31
.const NV_SPRITE_SCRATCH_RECT_BOTTOM_OFFSET = 33
//////////////////////////////////////////////////////////////////////////////
// assembler function to return the address of the sprite number
// function parameters:
// info: nv_sprite_info_struct that contains the address to return
.function nv_sprite_num_addr(info)
{
.return info.base_addr + NV_SPRITE_NUM_OFFSET
}
//////////////////////////////////////////////////////////////////////////////
// assembler function to return the address of the sprite Y velocity
// function parameters:
// info: nv_sprite_info_struct that contains the address to return
.function nv_sprite_vel_y_addr(info)
{
.return info.base_addr + NV_SPRITE_VEL_Y_OFFSET
}
//////////////////////////////////////////////////////////////////////////////
// assembler function to return the address of the sprite X velocity.
// function parameters:
// info: nv_sprite_info_struct that contains the address to return
.function nv_sprite_vel_x_addr(info)
{
.return info.base_addr + NV_SPRITE_VEL_X_OFFSET
}
//////////////////////////////////////////////////////////////////////////////
// Assembler function to return the address of the sprite X position.
// This is a 16 bit value so the address of the LSB will be returned
// function parameters:
// info: nv_sprite_info_struct that contains the address to return
.function nv_sprite_x_addr(info)
{
.return nv_sprite_x_lsb_addr(info)
}
//////////////////////////////////////////////////////////////////////////////
// Assembler function to return the address of the LSB of sprite X position.
// function parameters:
// info: nv_sprite_info_struct that contains the address to return
.function nv_sprite_x_lsb_addr(info)
{
.return info.base_addr + NV_SPRITE_X_OFFSET
}
//////////////////////////////////////////////////////////////////////////////
// Assembler function to return the address of the hitbox left byte
// function parameters:
// info: nv_sprite_info_struct that contains the address to return
.function nv_sprite_hitbox_left_addr(info)
{
.return info.base_addr + NV_SPRITE_HITBOX_LEFT_OFFSET
}
//////////////////////////////////////////////////////////////////////////////
// Assembler function to return the address of the hitbox top byte
// function parameters:
// info: nv_sprite_info_struct that contains the address to return
.function nv_sprite_hitbox_top_addr(info)
{
.return info.base_addr + NV_SPRITE_HITBOX_TOP_OFFSET
}
//////////////////////////////////////////////////////////////////////////////
// Assembler function to return the address of the hitbox right byte
// function parameters:
// info: nv_sprite_info_struct that contains the address to return
.function nv_sprite_hitbox_right_addr(info)
{
.return info.base_addr + NV_SPRITE_HITBOX_RIGHT_OFFSET
}
//////////////////////////////////////////////////////////////////////////////
// Assembler function to return the address of the hitbox bottom byte
// function parameters:
// info: nv_sprite_info_struct that contains the address to return
.function nv_sprite_hitbox_bottom_addr(info)
{
.return info.base_addr + NV_SPRITE_HITBOX_BOTTOM_OFFSET
}
//////////////////////////////////////////////////////////////////////////////
// Assembler function to return the address of the scratch rect
// function parameters:
// info: nv_sprite_info_struct that contains the address to return
.function nv_sprite_scratch_rect_addr(info)
{
.return info.base_addr + NV_SPRITE_SCRATCH_RECT_OFFSET
}
//////////////////////////////////////////////////////////////////////////////
// Assembler function to return the address of the scratch rect left coord
// function parameters:
// info: nv_sprite_info_struct that contains the address to return
.function nv_sprite_scratch_rect_left_addr(info)
{
.return info.base_addr + NV_SPRITE_SCRATCH_RECT_LEFT_OFFSET
}
//////////////////////////////////////////////////////////////////////////////
// Assembler function to return the address of the scratch rect top coord
// function parameters:
// info: nv_sprite_info_struct that contains the address to return
.function nv_sprite_scratch_rect_top_addr(info)
{
.return info.base_addr + NV_SPRITE_SCRATCH_RECT_TOP_OFFSET
}
//////////////////////////////////////////////////////////////////////////////
// Assembler function to return the address of the scratch rect right coord
// function parameters:
// info: nv_sprite_info_struct that contains the address to return
.function nv_sprite_scratch_rect_right_addr(info)
{
.return info.base_addr + NV_SPRITE_SCRATCH_RECT_RIGHT_OFFSET
}
//////////////////////////////////////////////////////////////////////////////
// Assembler function to return the address of the scratch rect bottom coord
// function parameters:
// info: nv_sprite_info_struct that contains the address to return
.function nv_sprite_scratch_rect_bottom_addr(info)
{
.return info.base_addr + NV_SPRITE_SCRATCH_RECT_BOTTOM_OFFSET
}
//////////////////////////////////////////////////////////////////////////////
// Assembler function to return the address of the MSB of sprite X position.
// function parameters:
// info: nv_sprite_info_struct that contains the address to return
.function nv_sprite_x_msb_addr(info)
{
.return info.base_addr + NV_SPRITE_X_OFFSET+1
}
//////////////////////////////////////////////////////////////////////////////
// Assembler function to return the address of the sprite Y position.
// function parameters:
// info: nv_sprite_info_struct that contains the address to return
.function nv_sprite_y_addr(info)
{
.return info.base_addr + NV_SPRITE_Y_OFFSET
}
//////////////////////////////////////////////////////////////////////////////
// Assembler function to return the address of the sprite's data pointer.
// This is not the address the data pointer is pointing to but the address
// of the pointer itself.
// This is a 16 bit value so the address of the LSB will be returned
// function parameters:
// info: nv_sprite_info_struct that contains the address to return
.function nv_sprite_data_ptr_addr(info)
{
.return nv_sprite_data_ptr_lsb_addr(info)
}
//////////////////////////////////////////////////////////////////////////////
// Assembler function to return the address of LSB of the sprite's data
// pointer. This is not the addr the data pointer is pointing to but the addr
// of the pointer itself.
// function parameters:
// info: nv_sprite_info_struct that contains the address to return
.function nv_sprite_data_ptr_lsb_addr(info)
{
.return info.base_addr + NV_SPRITE_DATA_PTR_OFFSET
}
//////////////////////////////////////////////////////////////////////////////
// Assembler function to return the address of MSB of the sprite's data
// pointer. This is not the addr the data pointer is pointing to but the addr
// of the pointer itself.
// function parameters:
// info: nv_sprite_info_struct that contains the address to return
.function nv_sprite_data_ptr_msb_addr(info)
{
.return info.base_addr + NV_SPRITE_DATA_PTR_OFFSET + 1
}
//////////////////////////////////////////////////////////////////////////////
// Assembler function to return the address of the the sprite top action.
// this address should contain one of the NV_SPRITE_ACTION_XXX values
// function parameters:
// info: nv_sprite_info_struct that contains the address to return
.function nv_sprite_top_action_addr(info)
{
.return info.base_addr + NV_SPRITE_ACTION_TOP_OFFSET
}
//////////////////////////////////////////////////////////////////////////////
// Assembler function to return the address of the the sprite left action.
// this address should contain one of the NV_SPRITE_ACTION_XXX values
// function parameters:
// info: nv_sprite_info_struct that contains the address to return
.function nv_sprite_left_action_addr(info)
{
.return info.base_addr + NV_SPRITE_ACTION_LEFT_OFFSET
}
//////////////////////////////////////////////////////////////////////////////
// Assembler function to return the address of the the sprite bottom action.
// this address should contain one of the NV_SPRITE_ACTION_XXX values
// function parameters:
// info: nv_sprite_info_struct that contains the address to return
.function nv_sprite_bottom_action_addr(info)
{
.return info.base_addr + NV_SPRITE_ACTION_BOTTOM_OFFSET
}
//////////////////////////////////////////////////////////////////////////////
// Assembler function to return the address of the the sprite right action.
// this address should contain one of the NV_SPRITE_ACTION_XXX values
// function parameters:
// info: nv_sprite_info_struct that contains the address to return
.function nv_sprite_right_action_addr(info)
{
.return info.base_addr + NV_SPRITE_ACTION_RIGHT_OFFSET
}
//////////////////////////////////////////////////////////////////////////////
// Assembler function to return the address of the the sprite top min
// position on the screen. Values beyond this will result in the top action
// function parameters:
// info: nv_sprite_info_struct that contains the address to return
.function nv_sprite_top_min_addr(info)
{
.return info.base_addr + NV_SPRITE_TOP_MIN_OFFSET
}
//////////////////////////////////////////////////////////////////////////////
// Assembler function to return the address of the the sprite left min
// position on the screen. Values beyond this will result in the left action.
// This is a 16 bit value so the addr of the LSB will be returned
// function parameters:
// info: nv_sprite_info_struct that contains the address to return
.function nv_sprite_left_min_addr(info)
{
.return info.base_addr + NV_SPRITE_LEFT_MIN_OFFSET
}
//////////////////////////////////////////////////////////////////////////////
// Assembler function to return the address of the the LSB of sprite left min
// position on the screen. Values beyond this will result in the left action.
// function parameters:
// info: nv_sprite_info_struct that contains the address to return
.function nv_sprite_left_min_lsb_addr(info)
{
.return info.base_addr + NV_SPRITE_LEFT_MIN_OFFSET
}
//////////////////////////////////////////////////////////////////////////////
// Assembler function to return the address of the the MSB of sprite left min
// position on the screen. Values beyond this will result in the left action.
// function parameters:
// info: nv_sprite_info_struct that contains the address to return
.function nv_sprite_left_min_msb_addr(info)
{
.return info.base_addr + NV_SPRITE_LEFT_MIN_OFFSET+1
}
//////////////////////////////////////////////////////////////////////////////
// Assembler function to return the address of sprite bottom max
// position on the screen. Values beyond this will result in the bottom action.
// function parameters:
// info: nv_sprite_info_struct that contains the address to return
.function nv_sprite_bottom_max_addr(info)
{
.return info.base_addr + NV_SPRITE_BOTTOM_MAX_OFFSET
}
//////////////////////////////////////////////////////////////////////////////
// Assembler function to return the address of the the sprite right max
// position on the screen. Values beyond this will result in the right action.
// This is a 16 bit value so the addr of the LSB will be returned
// function parameters:
// info: nv_sprite_info_struct that contains the address to return
.function nv_sprite_right_max_addr(info)
{
.return nv_sprite_right_max_lsb_addr(info)
}
//////////////////////////////////////////////////////////////////////////////
// Assembler function to return the address of the the LSB of sprite right max
// position on the screen. Values beyond this will result in the right action.
// function parameters:
// info: nv_sprite_info_struct that contains the address to return
.function nv_sprite_right_max_lsb_addr(info)
{
.return info.base_addr + NV_SPRITE_RIGHT_MAX_OFFSET
}
//////////////////////////////////////////////////////////////////////////////
// Assembler function to return the address of the the MSB of sprite right max
// position on the screen. Values beyond this will result in the right action.
// function parameters:
// info: nv_sprite_info_struct that contains the address to return
.function nv_sprite_right_max_msb_addr(info)
{
.return info.base_addr + NV_SPRITE_RIGHT_MAX_OFFSET+1
}
//////////////////////////////////////////////////////////////////////////////
// Assembler function to return the address of the scratch1 word for sprite
// This is 16 bit value so addr of LSB will be returned
// function parameters:
// info: nv_sprite_info_struct that contains the address to return
.function nv_sprite_scratch1_word_addr(info)
{
.return nv_sprite_scratch1_word_lsb_addr(info)
}
//////////////////////////////////////////////////////////////////////////////
// Assembler function to return the address of the LSB of scratch1 word
// for sprite
// function parameters:
// info: nv_sprite_info_struct that contains the address to return
.function nv_sprite_scratch1_word_lsb_addr(info)
{
.return info.base_addr + NV_SPRITE_SCRATCH1_OFFSET
}
//////////////////////////////////////////////////////////////////////////////
// Assembler function to return the address of the MLB of scratch1 word
// for sprite
// function parameters:
// info: nv_sprite_info_struct that contains the address to return
.function nv_sprite_scratch1_word_msb_addr(info)
{
.return info.base_addr + NV_SPRITE_SCRATCH1_OFFSET+1
}
//////////////////////////////////////////////////////////////////////////////
// Assembler function to return the address of the scratch2 word for sprite
// This is 16 bit value so addr of LSB will be returned
// function parameters:
// info: nv_sprite_info_struct that contains the address to return
.function nv_sprite_scratch2_word_addr(info)
{
.return info.base_addr + NV_SPRITE_SCRATCH2_OFFSET
}
//////////////////////////////////////////////////////////////////////////////
// move the sprite's data pointer to the zero page for some indirection
// assume that when this macro is used that the address of the sprite's
// extra data is in ZERO_PAGE_LO and ZERO_PAGE_HI already.
// after this macro is executed the ZERO_PAGE_LO and ZERO_PAGE_HI
// will contain the address of the sprite's data (the 64 bytes that
// define the sprite's shape and color)
.macro nv_sprite_data_ptr_to_zero_page()
{
nv_sprite_extra_word_to_mem(NV_SPRITE_DATA_PTR_OFFSET, scratch_word)
// store sprite data pointer in scratch word to zero page.
// note this over writes the pointer to extra data in zero page
// which many of the macros in this file rely on as a pre condition.
lda scratch_word
sta ZERO_PAGE_LO
lda scratch_word+1
sta ZERO_PAGE_HI
}
//////////////////////////////////////////////////////////////////////////////
// inline macro to get a byte from the sprite extra data bloc assuming
// that the pointer to the extra data block is already in ZERO_PAGE_LO
// and ZERO_PAGE_HI. byte from the extra data will be put in Accumulator
// macro parameters:
// offset: is the byte offset within the extra data block for the byte
// to get.
// Y will be changed,
// X not changed
// A will contain the bye from the extra data
.macro nv_sprite_extra_byte_to_a(offset)
{
// use indirect addressing to get the sprite number
ldy #offset // load Y reg with offset to sprite number
lda (ZERO_PAGE_LO),y // indirect indexed load sprite num to accum
}
//////////////////////////////////////////////////////////////////////////////
// inline macro to get a byte from the sprite extra data bloc assuming
// that the pointer to the extra data block is already in ZERO_PAGE_LO
// and ZERO_PAGE_HI. Byte from the extra data will be put in X register
// macro parameters:
// offset: is the byte offset within the extra data block for the byte
// to get.
.macro nv_sprite_extra_byte_to_x(offset)
{
nv_sprite_extra_byte_to_a(offset)
tax
}
//////////////////////////////////////////////////////////////////////////////
// inline macro to get a byte from the sprite extra data bloc assuming
// that the pointer to the extra data block is already in ZERO_PAGE_LO
// and ZERO_PAGE_HI. Byte from the extra data will be put in Y register
// macro parameters:
// offset: is the byte offset within the extra data block for the byte
// to get.
.macro nv_sprite_extra_byte_to_y(offset)
{
nv_sprite_extra_byte_to_a(offset)
tay
}
//////////////////////////////////////////////////////////////////////////////
// inline macro to get a byte from the sprite extra data block assuming
// that the pointer to the extra data block is already in ZERO_PAGE_LO
// and ZERO_PAGE_HI. Byte from the extra data will be put in the specified
// memory location.
// macro parameters:
// offset: is the byte offset within the extra data block for the byte
// to get.
// mem: is the memory location in which to copy the byte
.macro nv_sprite_extra_byte_to_mem(offset, mem)
{
nv_sprite_extra_byte_to_a(offset)
sta mem
}
//////////////////////////////////////////////////////////////////////////////
// copy a byte from memory to an offset within sprite extra block.
// Assumes that the pointer to the extra data block is
// already in ZERO_PAGE_LO and ZERO_PAGE_HI
// macro parameters:
// mem: the source memory location from which to copy the byte
// offset: the offset within the extra block (destination) to write
// the byte
// Y changes
// A changes
// X unchanged
.macro nv_sprite_mem_byte_to_extra(mem, offset)
{
lda mem
nv_sprite_a_to_extra(offset)
}
//////////////////////////////////////////////////////////////////////////////
// copy a 16 bit word from offset within sprite extra memory to somewhere
// else in memory. Assumes that the pointer to the extra data block is
// already in ZERO_PAGE_LO and ZERO_PAGE_HI
// macro parameters:
// offset: the offset within the sprite extra block of low byte of word
// mem_lo: the low byte of detination memory location
// Y changes
// A changes
// X unchanged
.macro nv_sprite_extra_word_to_mem(offset, mem_lo)
{
nv_sprite_extra_byte_to_a(offset)
sta mem_lo
nv_sprite_extra_byte_to_a(offset+1)
sta mem_lo+1
}
//////////////////////////////////////////////////////////////////////////////
// copy a 16 bit word from memory to an offset within sprite extra block.
// Assumes that the pointer to the extra data block is
// already in ZERO_PAGE_LO and ZERO_PAGE_HI
// macro parameters:
// mem_lo: the LSB of source memory location to copy to the extra block
// offset: the offset of LSB to of the destination word to write within
// the sprite extra block
// Y changes
// A changes
// X unchanged
.macro nv_sprite_mem_word_to_extra(mem_lo, offset)
{
lda mem_lo
nv_sprite_a_to_extra(offset)
lda mem_lo+1
nv_sprite_a_to_extra(offset+1)
}
//////////////////////////////////////////////////////////////////////////////
// store accum contents to the specified offset within the extra data block
// pointed to by ZERO_PAGE_LO and ZERO_PAGE_HI
// Y Reg: will be changed
// Accum: will not be changed,
// but must be set to value to be written before using macro
// X Reg: will not be changed
.macro nv_sprite_a_to_extra(offset)
{
// use indirect addressing to get the sprite number
ldy #offset // load Y reg with offset to sprite number
sta (ZERO_PAGE_LO),y // indirect indexed load sprite num to accum
}
//////////////////////////////////////////////////////////////////////////////
// store x reg contents to the specified offset within the extra data block
// pointed to by ZERO_PAGE_LO and ZERO_PAGE_HI
// Y Reg: will be changed
// Accum: will be changed
// X Reg: set to value to be written before using macro
.macro nv_sprite_x_to_extra(offset)
{
txa
nv_sprite_a_to_extra(offset)
}
//////////////////////////////////////////////////////////////////////////////
// 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_any_direction_sr(info)
{
ldx nv_sprite_vel_y_addr(info)
bpl PosVelY
NegVelY:
nv_sprite_move_negative_y(info)
jmp DoneY
PosVelY:
nv_sprite_move_positive_y(info)
DoneY:
// Y location done, now on to X
ldx nv_sprite_vel_x_addr(info)
bmi NegVelX
PosVelX:
// moving right (positive X velocity)
nv_sprite_move_positive_x(info)
jmp FinishedUpdate
NegVelX:
// moving left (negative X velocity)
nv_sprite_move_negative_x(info)
FinishedUpdate:
rts // already popped the return address, jump back now
}
//////////////////////////////////////////////////////////////////////////////
//
.macro nv_sprite_move_positive_y(info)
{
lda nv_sprite_vel_y_addr(info)
ldx nv_sprite_bottom_action_addr(info)
beq DoWrap // 0 = wrap, 1 = bounce
DoBounce:
clc
adc nv_sprite_y_addr(info)
cmp nv_sprite_bottom_max_addr(info)
bcc AccumHasNewY
// reverse the y velocity here to do that we do bitwise not + 1 (twos comp)
lda #$FF
eor nv_sprite_vel_y_addr(info)
tax
inx
stx nv_sprite_vel_y_addr(info)
jmp DoneY // don't actually update y
// when bouncing
// bounce bottom flag not set, Don't need to check for bounce
DoWrap:
clc
adc nv_sprite_y_addr(info)
cmp nv_sprite_bottom_max_addr(info)
bcc AccumHasNewY // if not off bottom then just update
// wrap to top of screen
lda nv_sprite_top_min_addr(info)
AccumHasNewY:
sta nv_sprite_y_addr(info)
DoneY:
}
//////////////////////////////////////////////////////////////////////////////
//
.macro nv_sprite_move_negative_y(info)
{
lda nv_sprite_vel_y_addr(info) // load y vel to accum
ldx nv_sprite_top_action_addr(info) // load x with top action
beq DoWrap // 0 = wrap, 1 = bounce
DoBounce:
clc
adc nv_sprite_y_addr(info)
cmp nv_sprite_top_min_addr(info)
bcs AccumHasNewY
// reverse the y velocity here to do that we do bitwise not + 1
lda #$FF
eor nv_sprite_vel_y_addr(info)
tax
inx
stx nv_sprite_vel_y_addr(info)
jmp DoneY // don't update Y loc
DoWrap:
// wrap to other side of screen
clc
adc nv_sprite_y_addr(info)
cmp nv_sprite_top_min_addr(info)
bcs AccumHasNewY // branch if accum > min top
// sprite is less than min top so need to move it to bottom
lda nv_sprite_bottom_max_addr(info)
AccumHasNewY:
sta nv_sprite_y_addr(info)
DoneY:
}
//////////////////////////////////////////////////////////////////////////////
//
.macro nv_sprite_move_positive_x(info)
{
// add x offset + x velocity and put in scratch1
// already know x vel is positive only so unsigned add is fine.
nv_adc16x_mem16x_mem8u((nv_sprite_x_addr(info)),
(nv_sprite_vel_x_addr(info)),
(nv_sprite_scratch1_word_addr(info)))
// scratch1 now has potential new X location
nv_ble16(nv_sprite_scratch1_word_lsb_addr(info), nv_sprite_right_max_lsb_addr(info), NewLocInScratch1)
// New X is too far since didn't branch above
ldx nv_sprite_right_action_addr(info)
beq DoWrap // 0 = wrap, 1 = bounce
DoBounce:
// bounce off right side by changing vel to 2's compliment of vel
lda #$FF
eor nv_sprite_vel_x_addr(info)
tax
inx
stx nv_sprite_vel_x_addr(info)
jmp Done
DoWrap:
// this sprite not set to bounce, so wrap it around
lda nv_sprite_left_min_lsb_addr(info)
sta nv_sprite_x_lsb_addr(info)
lda nv_sprite_left_min_msb_addr(info)
sta nv_sprite_x_msb_addr(info)
jmp Done
NewLocInScratch1:
lda nv_sprite_scratch1_word_lsb_addr(info)
sta nv_sprite_x_lsb_addr(info)
lda nv_sprite_scratch1_word_msb_addr(info)
sta nv_sprite_x_msb_addr(info)
Done:
}
//////////////////////////////////////////////////////////////////////////////
//
.macro nv_sprite_move_negative_x(info)
{
nv_adc16x_mem16x_mem8s((nv_sprite_x_addr(info)),
(nv_sprite_vel_x_addr(info)),
(nv_sprite_scratch1_word_lsb_addr(info)))
// scratch1 now has potential new X location
nv_bgt16(nv_sprite_scratch1_word_lsb_addr(info), nv_sprite_left_min_lsb_addr(info), NewLocInScratch1)
// moved too far left, either bounce or wrap
ldx nv_sprite_left_action_addr(info)
beq DoWrap
DoBounce:
// Bounce here, went off left side. Change vel to 2's compliment of vel
lda #$FF
eor nv_sprite_vel_x_addr(info)
tax
inx
stx nv_sprite_vel_x_addr(info)
jmp Done // don't update location this frame, just change vel
DoWrap:
// Wrap from left edge to right edge
lda nv_sprite_right_max_lsb_addr(info)
sta nv_sprite_x_lsb_addr(info)
lda nv_sprite_right_max_msb_addr(info)
sta nv_sprite_x_msb_addr(info)
jmp Done
NewLocInScratch1:
lda nv_sprite_scratch1_word_lsb_addr(info)
sta nv_sprite_x_lsb_addr(info)
lda nv_sprite_scratch1_word_msb_addr(info)
sta nv_sprite_x_msb_addr(info)
Done:
}
//////////////////////////////////////////////////////////////////////////////
// inline macro to set the action for the scenario when the sprite is
// attempting to move past its left min position.
// macro parameters:
// info: one of the nv_sprite_info_struct structs
// value: the action to set, one of the NV_SPRITE_ACTION_XXX values
// NV_SPRITE_ACTION_BOUNCE,
// NV_SPRITE_ACTION_WRAP
.macro nv_sprite_set_left_action(info, value)
{
.if (value != NV_SPRITE_ACTION_BOUNCE && value != NV_SPRITE_ACTION_WRAP)
{
.error("ERROR: Invalid action")
}
ldx #value
stx nv_sprite_left_action_addr(info)
}
//////////////////////////////////////////////////////////////////////////////
// inline macro set the action for the scenario when the sprite is
// attempting to move past its right max position.
// macro parameters:
// info: one of the nv_sprite_info_struct structs
// value: the action to set, one of the NV_SPRITE_ACTION_XXX values
// NV_SPRITE_ACTION_BOUNCE,
// NV_SPRITE_ACTION_WRAP
.macro nv_sprite_set_right_action(info, value)
{
.if (value != NV_SPRITE_ACTION_BOUNCE && value != NV_SPRITE_ACTION_WRAP)
{
.error("ERROR: Invalid action")
}
ldx #value
stx nv_sprite_right_action_addr(info)
}
//////////////////////////////////////////////////////////////////////////////
// inline macro set the action for the scenario when the sprite is
// attempting to move past its top min position.
// macro parameters:
// info: one of the nv_sprite_info_struct structs
// value: the action to set, one of the NV_SPRITE_ACTION_XXX values
// NV_SPRITE_ACTION_BOUNCE,
// NV_SPRITE_ACTION_WRAP
.macro nv_sprite_set_top_action(info, value)
{
.if (value != NV_SPRITE_ACTION_BOUNCE && value != NV_SPRITE_ACTION_WRAP)
{
.error("ERROR: Invalid action")
}
ldx #value
stx nv_sprite_top_action_addr(info)
}
//////////////////////////////////////////////////////////////////////////////
// inline macro to set the action for the scenario when the sprite is
// attempting to move past its bottom max position.
// macro parameters:
// info: one of the nv_sprite_info_struct structs
// value: the action to set, one of the NV_SPRITE_ACTION_XXX values
// NV_SPRITE_ACTION_BOUNCE,
// NV_SPRITE_ACTION_WRAP
.macro nv_sprite_set_bottom_action(info, value)
{
.if (value != NV_SPRITE_ACTION_BOUNCE && value != NV_SPRITE_ACTION_WRAP)
{
.error("ERROR: Invalid action")
}
ldx #value
stx nv_sprite_bottom_action_addr(info)
}
//////////////////////////////////////////////////////////////////////////////
// in line macro set the action for the scenario when the sprite is
// attempting to move past any max or min position.
// macro parameters:
// info: one of the nv_sprite_info_struct structs
// value: the action to set, one of the NV_SPRITE_ACTION_XXX values
// NV_SPRITE_ACTION_BOUNCE,
// NV_SPRITE_ACTION_WRAP
.macro nv_sprite_set_all_actions(info, value)
{
ldx #value
stx nv_sprite_left_action_addr(info)
stx nv_sprite_top_action_addr(info)
stx nv_sprite_right_action_addr(info)
stx nv_sprite_bottom_action_addr(info)
}
//////////////////////////////////////////////////////////////////////////////
// subroutine macro to set the action for the scenario when the sprite is
// attempting to move past its min or max position in any direction.
// macro parameters:
// info: one of the nv_sprite_info_struct structs
// value: the action to set, one of the NV_SPRITE_ACTION_XXX values
// NV_SPRITE_ACTION_BOUNCE,
// NV_SPRITE_ACTION_WRAP
.macro nv_sprite_set_all_actions_sr(info, value)
{
nv_sprite_set_all_actions(info, value)
rts
}
//////////////////////////////////////////////////////////////////////////////
// Inline macro (no rts) to setup everything for a sprite so its ready to
// be enabled and moved.
.macro nv_sprite_setup(info)
{
nv_sprite_raw_set_mode(info.num, info.data_ptr)
nv_sprite_raw_set_data_ptr(info.num, info.data_ptr)
nv_sprite_raw_set_color_from_data(info.num, info.data_ptr)
}
//////////////////////////////////////////////////////////////////////////////
// subroutine macro to setup the sprite so that its ready to be enabled
// and moved.
.macro nv_sprite_setup_sr(info)
{
nv_sprite_setup(info)
rts
}
//////////////////////////////////////////////////////////////////////////////
// Inline macro to test if a sprite overlaps with a character on screen
//
// Params:
// X Reg: character's X loc on screen
// Y Reg: character's Y loc on screen
// macro params:
// rect1_addr: is a temp rectangle that will be used to
// determine overlap. it will be filled with
// the sprite's rectangle pixel coords
// char_rect_addr: is a temp retangle that will be used to
// store the char's rectangle with pixel coords
// it doesn't need to be set prior to using macro