Advertisement
MadnessVX

sprite.asm

Feb 17th, 2014
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ; Atari 7800 sprite sample
  2. ; Written by Daniel Boris (dboris@home.com)
  3. ;
  4. ; Assemble with DASM
  5. ;
  6.  
  7.     processor 6502
  8.  
  9. ;================================================================================= Hardware Addresses =========================================================================================
  10.  
  11. INPTCTRL        equ     $01     ;Input control
  12. AUDC0           equ     $15     ;Audio Control Channel 0
  13. AUDC1           equ     $16     ;Audio Control Channel 1
  14. AUDF0           equ     $17     ;Audio Frequency Channel 0
  15. AUDF1           equ     $18     ;Audio Frequency Channel 1
  16. AUDV0           equ     $19     ;Audio Volume Channel 0
  17. AUDV1           equ     $1A     ;Audio Volume Channel 1
  18. INPT0           equ     $08     ;Paddle Control Input 0
  19. INPT1           equ     $09     ;Paddle Control Input 1
  20. INPT2           equ     $0A     ;Paddle Control Input 2
  21. INPT3           equ     $0B     ;Paddle Control Input 3
  22. INPT4           equ     $0C     ;Player 0 Fire Button Input
  23. INPT5           equ     $0D     ;Player 1 Fire Button Input
  24.  
  25. BACKGRND        equ     $20     ;Background Color
  26. P0C1            equ     $21     ;Palette 0 - Color 1
  27. P0C2            equ     $22     ;Palette 0 - Color 2
  28. P0C3            equ     $23     ;Palette 0 - Color 3
  29. WSYNC           equ     $20     ;Wait For Sync
  30. P1C1            equ     $21     ;Palette 1 - Color 1
  31. P1C2            equ     $22     ;Palette 1 - Color 2
  32. P1C3            equ     $23     ;Palette 1 - Color 3
  33. MSTAT           equ     $28     ;Maria Status
  34. P2C1            equ     $29     ;Palette 2 - Color 1
  35. P2C2            equ     $2A     ;Palette 2 - Color 2
  36. P2C3            equ     $2B     ;Palette 2 - Color 3
  37. DPPH            equ     $2C     ;Display List List Pointer High
  38. P3C1            equ     $2D     ;Palette 3 - Color 1
  39. P3C2            equ     $2E     ;Palette 3 - Color 2
  40. P3C3            equ     $2F     ;Palette 3 - Color 3
  41. DPPL            equ     $30     ;Display List List Pointer Low
  42. P4C1            equ     $31     ;Palette 4 - Color 1
  43. P4C2            equ     $32     ;Palette 4 - Color 2
  44. P4C3            equ     $33     ;Palette 4 - Color 3
  45. CHARBASE        equ     $34     ;Character Base Address
  46. P5C1            equ     $35     ;Palette 5 - Color 1
  47. P5C2            equ     $36     ;Palette 5 - Color 2
  48. P5C3            equ     $37     ;Palette 5 - Color 3
  49. OFFSET          equ     $38     ;Unused - Store zero here
  50. P6C1            equ     $39     ;Palette 6 - Color 1
  51. P6C2            equ     $3A     ;Palette 6 - Color 2
  52. P6C3            equ     $3B     ;Palette 6 - Color 3
  53. CTRL            equ     $3C     ;Maria Control Register
  54. P7C1            equ     $3D     ;Palette 7 - Color 1
  55. P7C2            equ     $3E     ;Palette 7 - Color 2
  56. P7C3            equ     $3F     ;Palette 7 - Color 3
  57.  
  58. SWCHA           equ     $280    ;P0, P1 Joystick Directional Input
  59. SWCHB           equ     $282    ;Console Switches
  60. CTLSWA          equ     $281    ;I/O Control for SCHWA
  61. CTLSWB          equ     $283    ;I/O Control for SCHWB
  62.  
  63.     SEG.U data
  64.  
  65.  
  66. ;========================================================================================= Variables ========================================================================================
  67.  
  68.     org $40
  69.  
  70. xpos    ds.b    1           ;X Position of sprite
  71. ypos    ds.b    1               ;Y Position of sprite
  72. temp    ds.b    1      
  73. dlpnt   ds.w    1
  74. dlend   ds.b    12      ;Index of end of each DL
  75.  
  76.  
  77.  
  78.  
  79. ;============================================================================================================================================================================================
  80.    
  81.     SEG code
  82.    
  83.         org     $8000           ;Start of code
  84.        
  85. START
  86.     sei                     ;Disable interrupts
  87.     cld                     ;Clear decimal mode
  88.    
  89.  
  90. ;=================================================================== Atari recommended start-up procedure =====================================================================================
  91.  
  92.     lda     #$07
  93.     sta     INPTCTRL        ;Lock into 7800 mode
  94.     lda     #$7F
  95.     sta     CTRL            ;Disable DMA
  96.     lda     #$00            
  97.     sta     OFFSET
  98.     sta     INPTCTRL
  99.     ldx     #$FF            ;Reset stack pointer
  100.     txs
  101.    
  102. ;==================================================================== Clear zero page and hardware ===========================================================================================
  103.  
  104.     ldx #$40
  105.     lda #$00
  106. crloop1    
  107.     sta $00,x           ;Clear zero page
  108.     sta $100,x      ;Clear page 1
  109.     inx
  110.     bne crloop1
  111.  
  112. ;============================================================================ Clear RAM ======================================================================================================
  113.  
  114.         ldy     #$00            ;Clear Ram
  115.         lda     #$18            ;Start at $1800
  116.         sta     $81            
  117.         lda     #$00
  118.         sta     $80
  119. crloop3
  120.         lda     #$00
  121.         sta     ($80),y         ;Store data
  122.         iny                     ;Next byte
  123.         bne     crloop3         ;Branch if not done page
  124.         inc     $81             ;Next page
  125.         lda     $81
  126.         cmp     #$20            ;End at $1FFF
  127.         bne     crloop3         ;Branch if not
  128.  
  129.         ldy     #$00            ;Clear Ram
  130.         lda     #$22            ;Start at $2200
  131.         sta     $81            
  132.         lda     #$00
  133.         sta     $80
  134. crloop4
  135.         lda     #$00
  136.         sta     ($80),y         ;Store data
  137.         iny                     ;Next byte
  138.         bne     crloop4         ;Branch if not done page
  139.         inc     $81             ;Next page
  140.         lda     $81
  141.         cmp     #$27            ;End at $27FF
  142.         bne     crloop4         ;Branch if not
  143.  
  144.         ldx     #$00
  145.         lda     #$00
  146. crloop5                         ;Clear 2100-213F
  147.         sta     $2100,x
  148.         inx
  149.         cpx     #$40
  150.         bne     crloop5
  151.        
  152. ;=============================================================================== Build DLL ==================================================================================================
  153. ; 20 blank lines
  154.  
  155.     ldx #$00                  
  156.     lda #$4F            ;16 lines
  157.     sta $1800,x          
  158.     inx
  159.     lda #$21        ;$2100 = blank DL
  160.     sta $1800,x
  161.     inx
  162.     lda #$00
  163.     sta $1800,x
  164.     inx                  
  165.     lda #$44            ;4 lines
  166.     sta $1800,x        
  167.     inx
  168.     lda #$21
  169.     sta $1800,x
  170.     inx
  171.     lda #$00
  172.     sta $1800,x
  173.     inx
  174. ;============================================================================================================================================================================================      
  175. ; 192 mode lines divided into 12 regions
  176.  
  177.     ldy     #$00       
  178. DLLloop2                        
  179.     lda #$4F            ;16 lines
  180.     sta $1800,x        
  181.     inx
  182.     lda DLPOINTH,y
  183.     sta $1800,x
  184.     inx
  185.     lda DLPOINTL,y
  186.     sta $1800,x
  187.     inx
  188.     iny
  189.     cpy #$0D            ;12 DLL entries
  190.     bne DLLloop2
  191.  
  192.  
  193. ; 26 blank lines
  194.                  
  195.     lda #$4F            ;16 lines
  196.     sta $1800,x          
  197.     inx
  198.     lda #$21        ;$2100 = blank DL
  199.     sta $1800,x
  200.     inx
  201.     lda #$00
  202.     sta $1800,x
  203.     inx                  
  204.     lda #$4A            ;10 lines
  205.     sta $1800,x        
  206.     inx
  207.     lda #$21
  208.     sta $1800,x
  209.     inx
  210.     lda #$00
  211.     sta $1800,x
  212.  
  213. ;=============================================================================== RESULT ======================================================================================================
  214. ; $1800-#$4F
  215. ; $1801-#$21
  216. ; $1802-#$00
  217.  
  218. ; $1803-#$44
  219. ; $1804-#$21
  220. ; $1805-#$00
  221.  
  222. ; $1806-#$4F
  223. ; $1807-#$22
  224. ; $1808-#$00
  225.  
  226. ; $1809-#$4F
  227. ; $180A-#$22
  228. ; $180B-#$40
  229.  
  230. ; $180C-#$4F
  231. ; $180D-#$22
  232. ; $180E-#$80
  233.  
  234. ; $180F-#$4F
  235. ; $1810-#$22
  236. ; $1811-#$C0
  237.  
  238. ; $1812-#$4F
  239. ; $1813-#$23
  240. ; $1814-#$00
  241.  
  242. ; $1815-#$4F
  243. ; $1816-#$23
  244. ; $1817-#$40
  245.  
  246. ; $1818-#$4F
  247. ; $1819-#$23
  248. ; $181A-#$80
  249.  
  250. ; $181B-#$4F
  251. ; $181C-#$23
  252. ; $181D-#$C0
  253.  
  254. ; $181E-#$4F
  255. ; $181F-#$24
  256. ; $1820-#$00
  257.  
  258. ; $1821-#$4F
  259. ; $1822-#$24
  260. ; $1823-#$40
  261.  
  262. ; $1824-#$4F
  263. ; $1825-#$24
  264. ; $1826-#$80
  265.  
  266. ; $1827-#$4F
  267. ; $1828-#$24
  268. ; $1829-#$C0
  269.  
  270. ; $182A-#$4f
  271. ; $182B-#$21
  272. ; $182C-#$00
  273.  
  274. ; $182D-#$4A
  275. ; $182E-#$21
  276. ; $182F-#$00
  277. ;========================================================================= Set-up Maria Registers ============================================================================================
  278.    
  279.     lda #$18            ;DLL at $1800
  280.     sta DPPH
  281.     lda #$00
  282.     sta DPPL
  283.     lda #$18        ;Set-up Palette 0
  284.     sta P0C1
  285.     lda #$38
  286.     sta P0C2
  287.     lda #$58
  288.     sta P0C3
  289.     lda #$43        ;Enable DMA
  290.     sta CTRL
  291.     lda #$00        ;Set-up ports to read mode
  292.     sta CTLSWA
  293.     sta CTLSWB 
  294.     lda #$40        ;Set initial X position of sprite
  295.     sta xpos
  296.    
  297. ;==============================================================================================================================================================================================
  298. mainloop
  299.     lda MSTAT       ;Wait for VBLANK
  300.     and #$80
  301.     beq mainloop
  302.    
  303.     lda SWCHA       ;Read stick
  304.     and #$80        ;Pushed Right?
  305.     bne skip1
  306.     ldx xpos        ;Move sprite to right X now = #$40
  307.     inx
  308.     inx
  309.     stx xpos       
  310. skip1
  311.     lda SWCHA       ;Read stick
  312.     and #$40        ;Pushed Left?
  313.     bne skip2
  314.     ldx xpos        ;Move sprite to left
  315.     dex
  316.     dex
  317.     stx xpos
  318. skip2
  319.     lda SWCHA       ;Read stick
  320.     and #$20        ;Pushed Down?
  321.     bne skip3      
  322.     ldx ypos        ;Move sprite down (currently 00?)
  323.     cpx #176   
  324.     bcs skip3       ;Don't move if we are at the bottom
  325.     inx
  326.     inx
  327.     inx
  328.     stx ypos   
  329. skip3
  330.     lda SWCHA       ;Read stick
  331.     and #$10        ;Pushed Up?
  332.     bne skip4      
  333.     ldx ypos        ;Move sprite up
  334.     beq skip4       ;Don't move if we are at the top
  335.     dex
  336.     dex
  337.     dex
  338.     stx ypos
  339. skip4
  340.  
  341. ;========================================================================= reset DL ends =====================================================================================================
  342.    
  343.     ldx #$0C
  344.     lda #$00
  345. dlclearloop
  346.     dex                 ;0D
  347.     sta dlend,x        
  348.     bne dlclearloop
  349.    
  350.    
  351. ;======================================================================== build DL entries ===================================================================================================
  352.  
  353.     lda ypos        ;Get Y position
  354.     and #$F0       
  355.     lsr             ;Divide by 16
  356.     lsr
  357.     lsr
  358.     lsr
  359.     tax
  360.     lda DLPOINTL,x  ;Get pointer to DL that this sprite starts in
  361.     sta dlpnt
  362.     lda DLPOINTH,x
  363.     sta dlpnt+1
  364.    
  365.     ;Create DL entry for upper part of sprite
  366.    
  367.     ldy dlend,x     ;Get the index to the end of this DL
  368.     lda #$00               
  369.     sta     (dlpnt),y   ;Low byte of data address
  370.     iny
  371.     lda #$40        ;Mode 320x1
  372.     sta     (dlpnt),y
  373.     iny
  374.     lda ypos       
  375.     and #$0F       
  376.     ora #$a0
  377.     sta     (dlpnt),y
  378.     iny
  379.     lda #$1F        ;Palette 0, 1 byte wide
  380.     sta     (dlpnt),y
  381.     iny
  382.     lda xpos        ;Horizontal position
  383.     sta     (dlpnt),y
  384.     sty dlend,x
  385.     lda ypos
  386.     and #$0F        ;See if sprite is entirely within this region
  387.     beq doneDL      ;branch if it is
  388.        
  389. ;=============================================================================================================================================================================================     
  390.         ;Create DL entry for lower part of sprite
  391.        
  392.     inx         ;Next region
  393.     lda DLPOINTL,x  ;Get pointer to next DL
  394.     sta dlpnt
  395.     lda DLPOINTH,x
  396.     sta dlpnt+1
  397.     ldy dlend,x     ;Get the index to the end of this DL
  398.     lda #$00               
  399.     sta     (dlpnt),y
  400.     iny
  401.     lda #$40        ;Mode 320x1
  402.     sta     (dlpnt),y
  403.     iny
  404.     lda ypos
  405.     and #$0F
  406.     eor #$0F
  407.     sta temp
  408.     lda #$a0
  409.     clc
  410.     sbc     temp
  411.     sta     (dlpnt),y
  412.     iny
  413.     lda #$1F        ;Palette 0, 1 byte wide
  414.     sta     (dlpnt),y
  415.     iny
  416.     lda xpos        ;Horizontal position
  417.     sta     (dlpnt),y
  418.     sty dlend,x
  419. doneDL
  420.  
  421. ;=========================================================================== add DL end entry on each DL ===================================================================================
  422.  
  423.     ldx #$0C                ; Load X with 13 (#$0C) Starts at 13 to be able to loop from 12.
  424. dlendloop
  425.     dex                     ; X-1
  426.     lda DLPOINTL,x          ; A = $C0
  427.     sta dlpnt               ; $C0 to dlpnt
  428.     lda DLPOINTH,x          ; A = $24
  429.     sta dlpnt+1             ; $24 to dlpnt+1
  430.     ldy     dlend,x         ; Y =
  431.     iny
  432.     lda #$00
  433.     sta (dlpnt),y
  434.     txa
  435.     bne     dlendloop      
  436.    
  437. ;============================================================================================================================================================================================
  438. vbloop
  439.     lda MSTAT       ;Wait for VBLANK to end
  440.     and #$80
  441.     bne     vbloop
  442.    
  443.     jmp     mainloop    ;Loop
  444.  
  445. redraw
  446.      
  447.  
  448. NMI
  449.     RTI
  450.    
  451. IRQ
  452.     RTI
  453.    
  454.  
  455. ;Pointers to the DLs
  456.  
  457. DLPOINTH
  458.         .byte   $22,$22,$22,$22,$23,$23,$23,$23,$24,$24,$24,$24
  459. DLPOINTL
  460.         .byte   $00,$40,$80,$C0,$00,$40,$80,$C0,$00,$40,$80,$C0
  461.  
  462.  
  463.  
  464.  
  465. ;============================================================================= Graphic Data ==================================================================================================
  466.         org $a000
  467.         .byte     %00111100
  468.         org $a100
  469.         .byte     %00111100
  470.         org $a200
  471.         .byte     %11111111
  472.         org $a300
  473.         .byte     %11111111
  474.         org $a400
  475.         .byte     %11111111
  476.         org $a500
  477.         .byte     %11111111
  478.         org $a600
  479.         .byte     %11111111
  480.         org $a700
  481.         .byte     %01100110
  482.         org $a800
  483.         .byte     %11100111
  484.         org $a900
  485.         .byte     %11100111
  486.         org $aA00
  487.         .byte     %11111111
  488.         org $aB00
  489.         .byte     %01111110
  490.         org $aC00
  491.         .byte     %00111100
  492.         org $aD00
  493.         .byte     %01111110
  494.         org $aE00
  495.         .byte     %00111100
  496.         org $aF00
  497.         .byte     %00011000
  498.  
  499.  
  500. ;======================================================================= Cart reset vector ==================================================================================================
  501.  
  502.      org     $fff8
  503.     .byte   $FF         ;Region verification
  504.     .byte   $87         ;ROM start $4000
  505.     .word   #NMI
  506.     .word   #START
  507.     .word   #IRQ
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement