Advertisement
NovaYoshi

I love macros

Aug 28th, 2015
382
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. .macpack generic    ; add and sub
  2. .macpack longbranch ; jeq, jne, jcc, jcs
  3.  
  4. ; Meant to be an easy replacement for .repeat and .endrepeat
  5. ; when you're trying to save space. Uses a zeropage memory location
  6. ; instead of a register as a loop counter so as not to disturb any
  7. ; registers.
  8. ; Times - Number of times to loop ( may be a memory location )
  9. ; Free  - Free zeropage memory location to use
  10. .macro .dj_loop Times, Free
  11.   .scope
  12.     DJ_Counter = Free
  13.     lda Times
  14.     sta Free
  15. DJ_Label:
  16. .endmacro
  17. .macro .end_djl
  18.   NextIndex:
  19.     dec DJ_Counter
  20.     jne DJ_Label
  21.   .endscope
  22. .endmacro
  23.  
  24. ; Working with X,Y is much more fun than working with PPU addresses
  25. ; give it an X and Y position, as well as a nametable number (0-3),
  26. ; and if you want to save the address to a 16-bit zeropage address
  27. ; ( big endian ) you can give an additional argument.
  28. ; NT - Nametable number (0-3)
  29. ; PX - X position in tiles
  30. ; PY - Y position in tiles
  31. ; Var - Variable to store address in (optional)
  32. .macro PositionXY NT, PX, PY, Var
  33.     .scope
  34.         t0 = $2000 + (NT * 1024)    ; Nametable data starts at $2000
  35.         t1 = PX                 ; and each nametable is 1024 bytes in size
  36.         t2 = PY * 32            ; Nametable rows are 32 bytes large
  37.         t3 = t0 + t1 + t2
  38.         .ifblank Var        ; Are we going to be writing this directly to PPUADDR?
  39.           lda #>t3
  40.           sta $2006
  41.           lda #<t3
  42.           sta $2006
  43.         .else               ; Are we going to be storing this to a pointer in zeropage instead?
  44.           lda #>t3
  45.           sta Var+0
  46.           lda #<t3
  47.           sta Var+1
  48.         .endif
  49.     .endscope
  50. .endmacro
  51.  
  52. .macro .nyb InpA, InpB      ; Makes a .byt storing two 4 bit values
  53.     .byt ( InpA<<4 ) | InpB
  54. .endmacro
  55.  
  56. .macro .raddr This          ; like .addr but for making "RTS trick" tables with
  57.  .addr This-1
  58. .endmacro
  59.  
  60. ; swap using X
  61. .macro swapx mema, memb
  62.   ldx mema
  63.   lda memb
  64.   stx memb
  65.   sta mema
  66. .endmacro
  67.  
  68. ; swap using Y
  69. .macro swapy mema, memb
  70.   ldy mema
  71.   lda memb
  72.   sty memb
  73.   sta mema
  74. .endmacro
  75.  
  76. ; swap using just A + stack
  77. .macro swapa mema, memb
  78.   lda mema
  79.   pha
  80.   lda memb
  81.   sta mema
  82.   pla
  83.   sta memb
  84. .endmacro
  85.  
  86. .macro neg
  87.   eor #255
  88.   add #1
  89. .endmacro
  90.  
  91. .macro abs ; absolute value
  92. .local Skip
  93.   bpl Skip
  94.   neg
  95. Skip:
  96. .endmacro
  97.  
  98. .macro sex ; sign extend
  99. .local Skip
  100.   ora #$7F
  101.   bmi Skip
  102.   lda #0
  103. Skip:
  104. .endmacro
  105.  
  106. .macro neg16 lo, hi
  107.   sec             ;Ensure carry is set
  108.   lda #0          ;Load constant zero
  109.   sbc lo          ;... subtract the least significant byte
  110.   sta lo          ;... and store the result
  111.   lda #0          ;Load constant zero again
  112.   sbc hi          ;... subtract the most significant byte
  113.   sta hi          ;... and store the result
  114. .endmacro
  115.  
  116. .macro pushaxy
  117.   pha
  118.   txa
  119.   pha
  120.   tya
  121.   pha
  122. .endmacro
  123.  
  124. .macro pullaxy
  125.   pla
  126.   tay
  127.   pla
  128.   tax
  129.   pla
  130. .endmacro
  131.  
  132. .macro pushax
  133.   pha
  134.   txa
  135.   pha
  136. .endmacro
  137.  
  138. .macro pullax
  139.   pla
  140.   tax
  141.   pla
  142. .endmacro
  143.  
  144. .macro pushay
  145.   pha
  146.   tya
  147.   pha
  148. .endmacro
  149.  
  150. .macro pullay
  151.   pla
  152.   tay
  153.   pla
  154. .endmacro
  155.  
  156. .macro addcarry to
  157. .local Skip
  158.   bcc Skip
  159.   inc to
  160. Skip:
  161. .endmacro
  162.  
  163. .macro subcarry to
  164. .local Skip
  165.   bcs Skip
  166.   dec to
  167. Skip:
  168. .endmacro
  169.  
  170. .macro addcarryx to
  171. .local Skip
  172.   bcc Skip
  173.   inc to,x
  174. Skip:
  175. .endmacro
  176.  
  177. .macro subcarryx to
  178. .local Skip
  179.   bcs Skip
  180.   dec to,x
  181. Skip:
  182. .endmacro
  183.  
  184. .macro RealPosToScreenPos RealLo, RealHi, Scroll, Store
  185.   lda RealLo
  186.   sub Scroll+0
  187.   sta Store
  188.  
  189.   lda RealHi
  190.   sbc Scroll+1
  191.   .repeat 4
  192.     lsr
  193.     ror Store
  194.   .endrep
  195. .endmacro
  196.  
  197. .macro RealPosToScreenPosByX RealLo, RealHi, Scroll, Store
  198.   lda RealLo,x
  199.   sub Scroll+0
  200.   sta Store
  201.  
  202.   lda RealHi,x
  203.   sbc Scroll+1
  204.   .repeat 4
  205.     lsr
  206.     ror Store
  207.   .endrep
  208. .endmacro
  209.  
  210. .macro ChrFile File, Size, Address
  211.   .word Address
  212.   .byt (Size/128)
  213.   .incbin File
  214. .endmacro
  215.  
  216. .macro LSpr Type, Direction, XPos, YPos, Extra
  217.   .byt XPos
  218.   .ifnblank Extra
  219.     .byt (Extra<<4)|YPos
  220.   .else
  221.     .byt YPos
  222.   .endif
  223.   .byt (Type<<1)|Direction
  224. .endmacro
  225.  
  226. .macro LObj Type, XPos, YPos, Extra1, Extra2
  227.   .byt Type
  228.   .byt (XPos<<4)|YPos
  229.   .ifnblank Extra1
  230.     .byt Extra1
  231.     .ifnblank Extra2
  232.       .byt Extra2
  233.     .endif
  234.   .endif
  235. .endmacro
  236.  
  237. .macro LObjN Type, XPos, YPos, Width, Height, Extra
  238.   LObj Type, XPos, YPos, (Width<<4)|Height
  239.   .ifnblank Extra
  240.     .byt Extra
  241.   .endif
  242. .endmacro
  243.  
  244. .macro LFinished
  245.   .byt $f0
  246. .endmacro
  247.  
  248. .macro LSetX NewX
  249.   .byt $f1, NewX
  250. .endmacro
  251.  
  252. .macro LWriteCol Col1, Col2, Col3
  253.   .ifnblank Col3
  254.     .byt $f4
  255.     .byt Col1, Col2, Col3
  256.   .else
  257.     .ifnblank Col2
  258.       .byt $f3
  259.       .byt Col1, Col2
  260.     .else
  261.       .byt $f2
  262.       .byt Col1
  263.     .endif
  264.   .endif
  265. .endmacro
  266.  
  267. .macro LXMinus16
  268.   .byt $f5
  269. .endmacro
  270.  
  271. .macro LXPlus16
  272.   .byt $f6
  273. .endmacro
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement