Advertisement
NovaYoshi

NES.h

Dec 21st, 2013
247
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ; Names are based on
  2. ; http://nesdevwiki.org/index.php/NES_PPU
  3. ; http://nesdevwiki.org/index.php/2A03
  4.  
  5. ; PPU registers
  6. PPUCTRL     = $2000
  7. NT_2000     = %00000000
  8. NT_2400     = %00000001
  9. NT_2800     = %00000010
  10. NT_2C00     = %00000011
  11. MSB_XSCROLL = %00000001
  12. MSB_YSCROLL = %00000010
  13. VRAM_RIGHT  = %00000000 ; writes/reads to PPUDATA increment PPUADDR
  14. VRAM_ACROSS = %00000000
  15. VRAM_DOWN   = %00000100 ; writes/reads to PPUDATA add 32 to PPUADDR
  16. OBJ_0000    = %00000000
  17. OBJ_1000    = %00001000
  18. OBJ_8X8     = %00000000
  19. OBJ_8X16    = %00100000
  20. BG_0000     = %00000000
  21. BG_1000     = %00010000
  22. VBLANK_NMI  = %10000000
  23.  
  24. PPUMASK     = $2001
  25. LIGHTGRAY   = %00000001
  26. BG_OFF      = %00000000
  27. BG_CLIP     = %00001000
  28. BG_ON       = %00001010
  29. OBJ_OFF     = %00000000
  30. OBJ_CLIP    = %00010000
  31. OBJ_ON      = %00010100
  32. INT_RED     = %00100000
  33. INT_GREEN   = %01000000
  34. INT_BLUE    = %10000000
  35.  
  36. PPUSTATUS      = $2002
  37. SPR_OVERFLOW   = %00100000
  38. SPR_HIT        = %01000000
  39. VBLANK_STARTED = %10000000
  40.  
  41. OAMADDR   = $2003
  42. OAMDATA   = $2004
  43. PPUSCROLL = $2005
  44. PPUADDR   = $2006
  45. PPUDATA   = $2007
  46.  
  47. ; Pulse channel registers
  48. SQ1_VOL   = $4000
  49. SQ1_SWEEP = $4001
  50. SQ1_LO    = $4002
  51. SQ1_HI    = $4003
  52. SQ2_VOL   = $4004
  53. SQ2_SWEEP = $4005
  54. SQ2_LO    = $4006
  55. SQ2_HI    = $4007
  56.  
  57. SQ_1_8      = $00  ; 1/8 duty (sounds sharp)
  58. SQ_1_4      = $40  ; 1/4 duty (sounds rich)
  59. SQ_1_2      = $80  ; 1/2 duty (sounds hollow)
  60. SQ_3_4      = $C0  ; 3/4 duty (sounds like 1/4)
  61. SQ_HOLD     = $20  ; halt length counter
  62. SQ_CONSTVOL = $10  ; 0: envelope decays from 15 to 0; 1: constant volume
  63. SWEEP_OFF   = $08
  64.  
  65. ; Triangle channel registers
  66. TRI_LINEAR = $4008
  67. TRI_LO     = $400A
  68. TRI_HI     = $400B
  69.  
  70. TRI_HOLD = $80
  71.  
  72. ; Noise channel registers
  73. NOISE_VOL = $400C
  74. NOISE_LO  = $400E
  75. NOISE_HI  = $400F
  76.  
  77. NOISE_HOLD = SQ_HOLD
  78. NOISE_CONSTVOL = SQ_CONSTVOL
  79. NOISE_LOOP = $80
  80.  
  81. ; DPCM registers
  82. DMC_FREQ  = $4010
  83. DMC_RAW   = $4011
  84. DMC_START = $4012
  85. DMC_LEN   = $4013
  86.  
  87. ; OAM DMA unit register
  88. ; Writing $xx here causes 256 bytes to be copied from $xx00-$xxFF
  89. ; to OAMDATA
  90. OAM_DMA = $4014
  91. OAMDMA  = $4014
  92.  
  93. ; Sound channel control and status register
  94. SND_CHN       = $4015
  95. CH_SQ1   = %00000001
  96. CH_SQ2   = %00000010
  97. CH_TRI   = %00000100
  98. CH_NOISE = %00001000
  99. CH_ALL   = %00001111  ; all tone generators, not dpcm
  100. CH_DPCM  = %00010000
  101.  
  102. JOY1 = $4016
  103. JOY2 = $4017
  104. APUCTRL       = $4017
  105. APUCTRL_5STEP = $80
  106. APUCTRL_NOIRQ = $40
  107.  
  108. OAM_COLOR_0 =  %00000000
  109. OAM_COLOR_1 =  %00000001
  110. OAM_COLOR_2 =  %00000010
  111. OAM_COLOR_3 =  %00000011
  112. OAM_PRIORITY = %00100000
  113. OAM_XFLIP    = %01000000
  114. OAM_YFLIP    = %10000000
  115.  
  116. OAM_YPOS = $200
  117. OAM_TILE = $201
  118. OAM_ATTR = $202
  119. OAM_XPOS = $203
  120.  
  121. KEY_RIGHT = %00000001
  122. KEY_LEFT  = %00000010
  123. KEY_DOWN  = %00000100
  124. KEY_UP    = %00001000
  125. KEY_START = %00010000
  126. KEY_SELECT= %00100000
  127. KEY_B     = %01000000
  128. KEY_A     = %10000000
  129.  
  130. ; and now macros ----------------------------------------------------------
  131.  
  132. .feature leading_dot_in_identifiers
  133. .macpack generic
  134. .macpack longbranch
  135.  
  136. ; Meant to be an easy replacement for .repeat and .endrepeat
  137. ; when you're trying to save space. Uses a zeropage memory location
  138. ; instead of a register as a loop counter so as not to disturb any
  139. ; registers.
  140. ; Times - Number of times to loop ( may be a memory location )
  141. ; Free  - Free zeropage memory location to use
  142. .macro .dj_loop Times, Free
  143.   .scope
  144.     DJ_Counter = Free
  145.     lda Times
  146.     sta Free
  147. DJ_Label:
  148. .endmacro
  149. .macro .end_djl
  150.   NextIndex:
  151.     dec DJ_Counter
  152.     jne DJ_Label
  153.   .endscope
  154. .endmacro
  155.  
  156. ; These use increments (useless)
  157. .macro .ij_loop Times, Free
  158.   .scope
  159.     DJ_Times = Times
  160.     DJ_Counter = Free
  161.     lda #0
  162.     sta Free
  163. DJ_Label:
  164. .endmacro
  165. .macro .end_ijl
  166.   NextIndex:
  167.     inc DJ_Counter
  168.     lda DJ_Counter
  169.     cmp Times
  170.     jne DJ_Label
  171.   .endscope
  172. .endmacro
  173.  
  174. ; swap using X
  175. .macro swapx mema, memb
  176.   ldx mema
  177.   lda memb
  178.   stx memb
  179.   sta mema
  180. .endmacro
  181. ; swap using Y
  182. .macro swapy mema, memb
  183.   ldy mema
  184.   lda memb
  185.   sty memb
  186.   sta mema
  187. .endmacro
  188. ; swap using just A + stack
  189. .macro swapa mema, memb
  190.   lda mema
  191.   pha
  192.   lda memb
  193.   sta mema
  194.   pla
  195.   sta memb
  196. .endmacro
  197.  
  198. ; Imitation of z80's djnz opcode.
  199. ; Can be on A, X, Y, or a zeropage memory location
  200. ; Label - Label to jump to
  201. ; Reg   - Counter register to use: A,X,Y or memory location
  202. .macro djnz Label, Reg
  203.   .if (.match({Reg}, a))
  204.     sub #1
  205.   .elseif (.match({Reg}, x))
  206.     dex
  207.   .elseif (.match({Reg}, y))
  208.     dey
  209.   .else
  210.     dec var
  211.   .endif
  212.   bne Label
  213. .endmacro
  214.  
  215.  
  216. ; Working with X,Y is much more fun than working with PPU addresses
  217. ; give it an X and Y position, as well as a nametable number (0-3),
  218. ; and if you want to save the address to a 16-bit zeropage address
  219. ; ( big endian ) you can give an additional argument.
  220. ; NT - Nametable number (0-3)
  221. ; PX - X position in tiles
  222. ; PY - Y position in tiles
  223. ; Var - Variable to store address in (optional)
  224. .macro PositionXY NT, PX, PY, Var
  225.     .scope
  226.         t0 = $2000 + (NT * 1024)    ; Nametable data starts at $2000
  227.         t1 = PX                 ; and each nametable is 1024 bytes in size
  228.         t2 = PY * 32            ; Nametable rows are 32 bytes large
  229.         t3 = t0 + t1 + t2
  230.         .ifblank Var        ; Are we going to be writing this directly to PPUADDR?
  231.           lda #>t3
  232.           sta PPUADDR
  233.           lda #<t3
  234.           sta PPUADDR
  235.         .else               ; Are we going to be storing this to a pointer in zeropage instead?
  236.           lda #>t3
  237.           sta Var+0
  238.           lda #<t3
  239.           sta Var+1
  240.         .endif
  241.     .endscope
  242. .endmacro
  243.  
  244. .macro .ppuxy NT, PX, PY
  245.     .scope
  246.         t0 = $2000 + (NT * 1024)    ; Nametable data starts at $2000
  247.         t1 = PX                 ; and each nametable is 1024 bytes in size
  248.         t2 = PY * 32            ; Nametable rows are 32 bytes large
  249.         t3 = t0 + t1 + t2
  250.         .byt >t3
  251.         .byt <t3
  252.     .endscope
  253. .endmacro
  254.  
  255. .macro .nyb InpA, InpB      ; Makes a .byt storing two 4 bit values
  256.     .byt ( InpA<<4 ) | InpB
  257. .endmacro
  258.  
  259. .macro .raddr This          ; like .addr but for making "RTS trick" tables with
  260.  .addr This-1
  261. .endmacro
  262.  
  263. .macro addx num
  264.   txa
  265.   axs #-num
  266. .endmacro
  267.  
  268. .macro subx num
  269.   txa
  270.   axs #num
  271. .endmacro
  272.  
  273. .macro unpack low,high
  274.   pha
  275.   and #15
  276.   sta low
  277.   pla
  278.   lsr
  279.   lsr
  280.   lsr
  281.   lsr
  282.   sta high
  283. .endmacro
  284.  
  285. .macro asl4
  286.   asl
  287.   asl
  288.   asl
  289.   asl
  290. .endmacro
  291.  
  292. .macro lsr4
  293.   lsr
  294.   lsr
  295.   lsr
  296.   lsr
  297. .endmacro
  298.  
  299. .macro inw mem
  300.   inc mem+0
  301.   bne :+
  302.   inc mem+1
  303. :
  304. .endmacro
  305.  
  306. .macro dew mem
  307.   lda mem+0
  308.   bne :+
  309.   dec mem+1
  310. : dec mem+0
  311. .endmacro
  312.  
  313. .ifp02
  314. .macro axs Value
  315.   sub Value
  316.   tax
  317. .endmacro
  318. .macro alr Value
  319.   and Value
  320.   lsr
  321. .endmacro
  322. .macro lax Value
  323.   lda Value
  324.   ldx Value
  325. .endmacro
  326. .macro isc Addr, Why
  327.   stx TempX
  328.   pha
  329.   tya
  330.   tax
  331.   inc Addr,x
  332.   pla
  333.   ldx TempX
  334. .endmacro
  335. .macro dcp Addr, Why
  336.   stx TempX
  337.   pha
  338.   tya
  339.   tax
  340.   dec Addr,x
  341.   pla
  342.   ldx TempX
  343. .endmacro
  344. .endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement