Advertisement
ZodiacMentor

Battlesnakes ASM

Mar 4th, 2022
3,916
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ; whenever calling player.s functions, put the player struct address for the player we are manipulating in the
  2. ; ZP_ADDRESS
  3.  
  4.     .local playfield
  5.  
  6. playfield:
  7.     .repeat 768
  8.     .byte 0
  9.     .endrepeat
  10.  
  11. playfield_offset:
  12.     .word $0000
  13.  
  14. playerAddress:
  15.     .word $00
  16.  
  17. tempSpriteIndex:
  18.     .byte $00
  19.  
  20. ; write the players head to playfield and also draw to screen
  21. writeHead:
  22.     jsr savePlayerAddress
  23.  
  24.     lda #$0
  25.     sta playfield_offset
  26.     sta playfield_offset+1
  27.  
  28.     ldy #$0
  29.     lda (ZP_ADDRESS), Y     ; get id and save to stack
  30.     ; form the correct sprite index
  31.     ; in the form of 00gpbbdd where
  32.     ; gp - is a combined player id from having gold, and player id (saved in stack in this case)
  33.     ; bb - is body part 0 - head, 1 - straight, 2 - bend, 3 - tail
  34.     ; dd - is forwards direction 0 - right, 1 - down, 2 - left, 3 - up
  35.     ; in this case, bb is 0 and we'll assume direction is left (0b10)
  36.     ; and we just got the id, OR 0b10 with ID and save to stack
  37.     ora #%10
  38.     sta gpuIndex
  39.  
  40.     iny
  41.     ; send set index command with proper arguments to GPU
  42.     lda (ZP_ADDRESS), Y     ; get head_x
  43.     sta gpuX
  44.     sta playfield_offset+1  ; save x to playfield_offset
  45.     iny
  46.     lda (ZP_ADDRESS), Y     ; get head_y
  47.     sta gpuY
  48.  
  49.     ldy #32
  50.     jsr ROM_mul8x8 ; multiply A * Y (or head_y * 32)
  51.     clc
  52.     adc playfield_offset+1
  53.     sta playfield_offset+1
  54.     tya
  55.     sta playfield_offset        ; save 32 (width) times Y added to X in playfield offset
  56.  
  57.     clc                         ; add playfield baseaddress to playfield offset to get absolute address in playfield_offset
  58.     lda #<playfield
  59.     adc playfield_offset+1
  60.     sta ZP_ADDRESS      ; save in ZP_ADDRESS
  61.     lda #>playfield
  62.     adc playfield_offset
  63.     sta ZP_ADDRESS+1
  64.  
  65.     lda gpuIndex
  66.     sta (ZP_ADDRESS),Y
  67.     jsr gpuSetIndex
  68.  
  69.     jsr loadPlayerAddress
  70.  
  71.     rts
  72.  
  73. writeTail:
  74.     jsr savePlayerAddress
  75.  
  76.     ldy #$0
  77.     lda (ZP_ADDRESS), Y     ; get id and save to stack
  78.     ; form the correct sprite index
  79.     ; in the form of 00gpbbdd where
  80.     ; gp - is a combined player id from having gold, and player id (saved in stack in this case)
  81.     ; bb - is body part 0 - head, 1 - straight, 2 - bend, 3 - tail
  82.     ; dd - is forwards direction 0 - right, 1 - down, 2 - left, 3 - up
  83.     ; in this case, bb is 3 (0b11) and we'll assume direction is 2 or left (0b10)
  84.     ; let's OR the id in A with 0b1111 to get the proper sprite index
  85.     ; and save it to stack
  86.     ora #%1110
  87.     sta gpuIndex
  88.     ldy #3
  89.     ; send set index command with proper arguments to GPU
  90.     lda (ZP_ADDRESS), Y     ; get tail_x
  91.     sta gpuX
  92.     sta playfield_offset+1  ; save x to playfield_offset
  93.     iny
  94.     lda (ZP_ADDRESS), Y     ; get tail_y
  95.     sta gpuY
  96.  
  97.     ldy #32
  98.     jsr ROM_mul8x8 ; multiply A * Y (or tail_y * 32)
  99.     clc
  100.     adc playfield_offset+1
  101.     sta playfield_offset+1
  102.     tya
  103.     sta playfield_offset        ; save 32 (width) times Y added to X in playfield offset
  104.  
  105.     clc                         ; add playfield baseaddress to playfield offset to get absolute address in playfield_offset
  106.     lda #<playfield
  107.     adc playfield_offset+1
  108.     sta ZP_ADDRESS      ; save in ZP_ADDRESS
  109.     lda #>playfield
  110.     adc playfield_offset
  111.     sta ZP_ADDRESS+1
  112.  
  113.     lda gpuIndex
  114.     sta (ZP_ADDRESS),Y
  115.     jsr gpuSetIndex
  116.  
  117.     jsr loadPlayerAddress
  118.     rts
  119.  
  120. moveHead:
  121.     jsr savePlayerAddress
  122.  
  123.     ; get player id and save to stack
  124.     ; get player data at head location
  125.     ldy #1
  126.     lda (ZP_ADDRESS), Y     ; get head_x
  127.     sta gpuX
  128.     sta playfield_offset+1  ; save x to playfield_offset
  129.     iny
  130.     lda (ZP_ADDRESS), Y     ; get head_y
  131.     sta gpuY
  132.     ldy #32
  133.     jsr ROM_mul8x8 ; multiply A * Y (or head_y * 32)
  134.  
  135.     clc
  136.     adc playfield_offset+1
  137.     sta playfield_offset+1
  138.     tya
  139.     sta playfield_offset        ; save 32 (width) times Y added to X in playfield offset
  140.  
  141.     clc                         ; add playfield baseaddress to playfield offset to get absolute address in playfield_offset
  142.     lda #<playfield
  143.     adc playfield_offset+1
  144.     sta ZP_ADDRESS
  145.     lda #>playfield
  146.     adc playfield_offset
  147.     sta ZP_ADDRESS+1
  148.  
  149.     ldy #0
  150.     lda (ZP_ADDRESS),Y
  151.     ; change spriteindex to be a straight part
  152.     and #%11110011
  153.     ora #%00000100
  154.     sta (ZP_ADDRESS),Y
  155.     sta gpuIndex
  156.     jsr gpuSetIndex
  157.     lda gpuIndex
  158.  
  159.     and #%11   ; And the direction
  160.     beq moveHeadRight
  161.     cmp #01
  162.     beq moveHeadDown
  163.     cmp #02
  164.     beq moveHeadLeft
  165.    
  166.     ; move head up
  167.     ; TODO: add code
  168.     jsr loadPlayerAddress
  169.     rts
  170.  
  171.   moveHeadRight:
  172.     ; TODO: add code
  173.     jsr loadPlayerAddress
  174.     rts
  175.  
  176.   moveHeadDown:
  177.     ; TODO: add code
  178.     jsr loadPlayerAddress
  179.     rts
  180.  
  181.   moveHeadLeft:
  182.     jsr loadPlayerAddress
  183.  
  184.     ldy #1
  185.     lda (ZP_ADDRESS),Y ; read X
  186.     sec
  187.     sbc #1
  188.     and #%11111     ; scrap carry, ie. % 32
  189.     sta (ZP_ADDRESS),Y  ; save new X
  190.  
  191.     jsr loadPlayerAddress
  192.  
  193.     jsr writeHead
  194.  
  195.     rts
  196.  
  197. moveTail:
  198.     jsr savePlayerAddress
  199.  
  200.     ; get player id and save to stack
  201.     ; get player data at tail location
  202.     ldy #3
  203.     lda (ZP_ADDRESS), Y     ; get tail_x
  204.     sta playfield_offset+1  ; save x to playfield_offset
  205.     iny
  206.     lda (ZP_ADDRESS), Y     ; get tail_y
  207.     ldy #32
  208.     jsr ROM_mul8x8 ; multiply A * Y (or tail_y * 32)
  209.  
  210.     clc
  211.     adc playfield_offset+1
  212.     sta playfield_offset+1
  213.     tya
  214.     sta playfield_offset        ; save 32 (width) times Y added to X in playfield offset
  215.  
  216.     clc                         ; add playfield baseaddress to playfield offset to get absolute address in playfield_offset
  217.     lda #<playfield
  218.     adc playfield_offset+1
  219.     sta ZP_ADDRESS
  220.     lda #>playfield
  221.     adc playfield_offset
  222.     sta ZP_ADDRESS+1
  223.  
  224.     ldy #0
  225.     lda (ZP_ADDRESS),Y
  226.     pha
  227.     lda #$ff
  228.     sta (ZP_ADDRESS),Y
  229.     pla
  230.  
  231.     and #%11   ; And the direction
  232.     beq moveTailRight
  233.     cmp #01
  234.     beq moveTailDown
  235.     cmp #02
  236.     beq moveTailLeft
  237.    
  238.     ; move tail up
  239.     ; TODO: add code
  240.     jsr loadPlayerAddress
  241.     rts
  242.  
  243.   moveTailRight:
  244.     ; TODO: add code
  245.     jsr loadPlayerAddress
  246.     rts
  247.  
  248.   moveTailDown:
  249.     ; TODO: add code
  250.     jsr loadPlayerAddress
  251.     rts
  252.  
  253.   moveTailLeft:
  254.     lda #$ff
  255.     sta (ZP_ADDRESS), Y
  256.     sta gpuIndex
  257.  
  258.     jsr loadPlayerAddress
  259.  
  260.     ldy #3
  261.     lda (ZP_ADDRESS), Y     ; get tail_x
  262.     sta gpuX
  263.     iny
  264.     lda (ZP_ADDRESS), Y     ; get tail_y
  265.     sta gpuY
  266.    
  267.     jsr gpuSetIndex
  268.  
  269.     jsr loadPlayerAddress
  270.  
  271.     ldy #3
  272.     lda (ZP_ADDRESS), Y     ; get tail_x
  273.     sec
  274.     sbc #1
  275.     and #%11111     ; scrap carry, ie. % 32
  276.     sta (ZP_ADDRESS), Y     ; save new head_x
  277.  
  278.     jsr loadPlayerAddress
  279.  
  280.     jsr writeTail
  281.  
  282.     rts
  283.  
  284. loadPlayerAddress:
  285.     lda playerAddress
  286.     sta ZP_ADDRESS
  287.     lda playerAddress+1
  288.     sta ZP_ADDRESS+1
  289.     rts  
  290.  
  291. savePlayerAddress:
  292.     lda ZP_ADDRESS
  293.     sta playerAddress
  294.     lda ZP_ADDRESS+1
  295.     sta playerAddress+1
  296.     rts
  297.  
  298. gpuX:
  299.     .byte $00
  300. gpuY:
  301.     .byte $00
  302. gpuIndex:
  303.     .byte $00
  304.  
  305. gpuSetIndex:
  306.     lda #CF_CMD_SET_INDEX
  307.     sta GPU_CMDQ_ADDRESS
  308.     lda gpuX
  309.     sta GPU_CMDQ_ADDRESS
  310.     lda gpuY
  311.     sta GPU_CMDQ_ADDRESS
  312.     lda gpuIndex
  313.     sta GPU_CMDQ_ADDRESS
  314.  
  315.     rts
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement