Meneer_Jansen

Part 5. Sprites

Jul 3rd, 2020 (edited)
198
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.95 KB | None | 0 0
  1. .---------------------------.
  2. ( P A R T 5 . S P R I T E S )
  3. `---------------------------´
  4.  
  5.  
  6.  
  7. This part has examples in BASIC because this part is mostly a summary of the excellent webpage by Marcello [1]. However, I think that it illustrates quite well how sprites can be used used in Assembly too.
  8.  
  9.  
  10.  
  11. o==========o
  12. | Contents |
  13. o==========o
  14.  
  15. general contents: https://pastebin.com/hmpJmurr
  16.  
  17. A. Activate & define
  18.  
  19. B. Position
  20.  
  21. C. Position beyond 255
  22.  
  23. D. Expand sprites
  24.  
  25. E. Collision detection
  26.  
  27. F. Multicolor
  28.  
  29. Y. Summary of registers
  30.  
  31. X. References
  32.  
  33.  
  34. -<>-<>-<>-<>-<>-<>-<>-<>-
  35.  
  36.  
  37.  
  38. o======================o
  39. | A. Activate & define |
  40. o======================o
  41.  
  42. Summary: 2040_dec (pointer), V = D000_hex (base addr), V + 21 (on/off), V + 39 (color).
  43.  
  44. 1. (Real) Base address of the VIC-II: V = 53248_dec = D000_hex.
  45.  
  46. 2. Turn sprites on/off: V + 21 = 53269_dec (= D015_hex). Example to turn all 8 on:
  47.  
  48. POKE 53269,255 (255 = 1111 1111_bin)
  49.  
  50. 3. A sprite = 24 x 21 pix = (24/8) bytes x 21 lines = 3 x 21 bytes = 63 bytes. Round that up to 64 B.
  51.  
  52. 4. Pointer for sprite zero to the location in RAM where it's 64 k of data is:
  53.  
  54. 2040_dec (07F8_hex)
  55.  
  56. Range: 2040 - 2047_dec. They are the last 8 bytes below the 38 kB free RAM.
  57.  
  58. 5. Set location (pointer) in RAM to "192" for sprite number zero. This actually means
  59.  
  60. 192 x 64 bytes = 12288 Bytes = 12 kB = 3000_hex
  61.  
  62. in RAM, that's in the middle of the 38 kB of free RAM:
  63.  
  64. POKE 2040,192 (= 12288_dec)
  65.  
  66. 6. Define a 24x21 solid block sprite at 12288_dec in free RAM:
  67.  
  68. FOR T = 12288 TO 12350: POKE T,255: NEXT
  69.  
  70. 7. The color of sprite number zero is defined at the sprite color register: V + 39 (53287_dec, D027_hex):
  71.  
  72. POKE 53287,1 (color white)
  73.  
  74.  
  75. o=============o
  76. | B. Position |
  77. o=============o
  78.  
  79. Summary: V (pos).
  80.  
  81. 1. Screen size is 320 x 200. Notice that 320 = 255 (an 8 bit number) + 65. I.e. the screen is wider than 1 byte can address.
  82.  
  83. 2. Sprite positions are set in registers V (53248_dec = D000_hex) to V + 15 (D00F_hex). Example, set X-position for sprite zero to 255:
  84.  
  85. POKE 53248,255
  86.  
  87. Y-pos. for sprite zero:
  88.  
  89. POKE 53249,200
  90.  
  91. X-pos. for sprite 1:
  92.  
  93. POKE 53250,255
  94.  
  95. and so on...
  96.  
  97.  
  98. o========================o
  99. | C. Position beyond 255 |
  100. o========================o
  101.  
  102. Summary: V + 16 (extendend X-pos).
  103.  
  104. 1. To position sprite beyond the 255 boundary (extended X position) use V + 16 (D010_hex). It's an 8 bit register of which each bit corresponds to one sprite. Bit 0 is for sprite 0 and so on. These bits set "the ninth bit" for the X-pos for a sprite on/off. Like so for sprite 0:
  105.  
  106. Byte D010_hex: 0000 0001
  107. |_ Sprite No. 0 extd. pos.
  108.  
  109. 2. It's X-position can be 255 (1 0000 0000_bin) if the 9 bits defining it are like so:
  110.  
  111. 1 0000 0000
  112. | |_ The 8 bits from byte D000_hex
  113. |___ Bit No. 0 of byte D010_hex
  114.  
  115. 3. Example for X = 320 = 256 + 64 (1 0100 000_bin):
  116.  
  117. 1 0100 0000
  118. | |_ Byte D000_hex is 64_dec
  119. |___ Byte D010_hex is 1_dec
  120.  
  121. 4. Or, more completely to get sprite 0 to X=320 and Y=200:
  122.  
  123. V = 53248:
  124. POKE V+21,1 (set sprite 0 on):
  125. POKE 2040,192 (set mem location for sprite 0 to 192 x 64_dec = 12288_dec):
  126. POKE V+39,1 (color sprite black): FOR T = 12288 TO 12350: POKE T,255: NEXT:
  127. POKE V+16,1 (set extended X-pos bit for sprite 0 on):
  128. POKE V ,64 (set X-pos to 1000_0000_bin):
  129. POKE V+1 ,200 (Y-pos = 200)
  130.  
  131.  
  132. o===================o
  133. | D. Expand sprites |
  134. o===================o
  135.  
  136. Summary: V + 23 (Y), V + 29 (X)
  137.  
  138. Expand sprites in Y direction: V + 23 = 53271_dec = D017_hex. Each bit is a sprite. Example for sprite 0:
  139.  
  140. D017_hex: 0000 0001
  141. |_ Sprite 0 will be expanded in Y direction.
  142.  
  143.  
  144. o========================o
  145. | E. Collision detection |
  146. o========================o
  147.  
  148. Summary: V + 30 (sprite-sprite), V + 31 (sprite-backgr.)
  149.  
  150. 1. To the 8 bits in register V + 30 = D01E_hex is written if a sprite collides with another sprite. Example if D01E is 3:
  151.  
  152. 0000 0011
  153. ||_ Sprite 0 collided w/ another sprite.
  154. |__ It collided w/ sprite 1.
  155.  
  156. 2. Same thing goes for V + 31 (D01F_hex) for sprite collision with a character in the background.
  157.  
  158. 3. If *any* sprite collided then bits 1 and/or 2 are set to 1 in D019_hex. Interrupts for those events are written to bit 1 and/or 2 of D01A_hex (see [5]).
  159.  
  160.  
  161. o===============o
  162. | F. Multicolor |
  163. 0===============0
  164.  
  165. Set a sprite to be multicolor in D01C_hex. Two bits in the 24x21 bitmap now represent a color (that's 4 colors). The sprite's pixels are 2 by 1 (i.e. horizontally stretched). The background color (00_bin) is in it's "usual" place of D021_hex and is the same for every sprite.
  166.  
  167. Two additional colors are in D025 and D026_hex. They are also the same for every sprite.
  168.  
  169. The forth color is different for every sprite and are at their "usual" places from D027 to D02E_hex.
  170.  
  171.  
  172. o=========================o
  173. | Y. Summary of registers |
  174. o=========================o
  175.  
  176. Registers in hex:
  177.  
  178. 07F8 - F Pointers to sprite data in RAM
  179. D000 - F X- and Y-positions of sprites on screen
  180. D010 Extended X-positions on/off
  181. D015 Turn sprites on/off
  182. D017 & D Expand in X- & Y-direction
  183. D01E & F Collision detection
  184. D027 - E Colors of sprites
  185.  
  186.  
  187. o===============o
  188. | X. References |
  189. o===============o
  190.  
  191. [1] Good tutorial:
  192. http://retro64.altervista.org/blog/programming-sprites-the-commodore-64-simple-tutorial-using-basic-v2/
  193.  
  194. [2] "C64 Assembly programming" by Mark Andrews, Howard W. Sams & Co. publ., ISBN 0-672-22244-5 (1985).
  195.  
  196. [3] "The master memory map for the C64" by P. Pavelko and T. Kelly, Prentice Hall, ISBN 0-13-574351-6 (1983).
  197.  
  198. [4] "C64 programmer's reference guide", Commodore Business Machines and Howard W. Sams and Co. publ., First ed. (1983).
  199.  
  200. [5] HTML C64 memory map by Joe Forster:
  201. https://sta.c64.org/cbm64mem.html
Add Comment
Please, Sign In to add comment