Advertisement
Redxone

[6502ASM] Simple Game

Mar 19th, 2017
233
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ; Simple 6502 ASM Game!
  2. ; Created by Lewisk3 (Redxone)
  3.  
  4. ;Define variables
  5.  
  6. define player $06             ; Color of player 1b
  7. define key $ff                ; Byte of memory that holds our key pressed information
  8. define up $77                 ; ID Of W Key
  9. define down $73               ; ID Of S Key
  10. define left $61               ; ID Of A Key
  11. define right $64              ; ID Of D Key
  12. define scr $0200              ; Indicates Starting screen memory
  13. define pos $11                ; LSB Of Player location
  14. define ppos $12               ; MSB Of Player location
  15. define colflag $1F            ; Location of collision flag
  16. define place $7A
  17. define break $78
  18. define blockplaced $01
  19.  
  20. init:
  21.   lda #$02      ; Starting location of player x
  22.   sta $11       ; Store starting location X to player LSB
  23.   lda #$03      ; Starting location of player y ( Must be greater than 02, or it will not be written to screen space! )
  24.   sta $12       ; Store starting location Y to player MSB
  25. ; Place sample block
  26. lda #$01
  27. ldx #$05
  28. ldy #$04
  29. jsr nblock
  30.  
  31. jsr draw        ; Draw player
  32. jmp loop        ; Start game Loop (check for inputs)
  33.  
  34. getax:
  35.   lda ppos      ; Returns MSB of player location
  36. rts
  37. getay:
  38.   lda pos       ; Returns LSB of player location
  39. rts
  40.  
  41. nblock:
  42.   pha             ; Push accumulator
  43.   txa             ; Move X into A
  44.   sta $14         ; Store X into 14
  45.   tya             ; Move Y into A
  46.   sta $15         ; Store Y into 15
  47.   pla             ; Pull A back
  48.   ldx #$00        ; Setup indirect for screen placing  
  49.   sta ($14, X)    ; Store A pixel into screen
  50. rts
  51.  
  52. loadp:
  53.   ldx #$00        ; Here is where things get a bit tricky. We load 0 Into X so we can call an indirect address
  54.   sta (pos, X)    ; Stores A into 2Bytes of player locations, (MSB and LSB) + 0. Indirect addressing takes 2 bytes
  55.                   ; If its given 1 byte, the second byte it uses is its immediate byte: sta (byte, X) = sta byte:byte+1
  56.                   ; Where byte and byte+1 become the address passed into STA.
  57. rts
  58.  
  59. undraw:
  60.   lda #$00        ; Load a blank pixel into A
  61.   ldx #$00        ; Setup indirect addressing
  62.   sta (pos, X)    ; Store blank pixel into player location on screen
  63. rts
  64.  
  65. negk:
  66.   lda #$00        ; This routine has a simple purpose, clear the key from the memory address
  67.   sta key         ; This is just a precaution, as i noticed sometimes the key says and the input persists
  68.                   ; Result in a run away player.
  69. rts
  70.  
  71. draw:
  72.   lda #player     ; Load the players pixel into A
  73.   jsr loadp       ; Jumps to the routine that places the pixel at A into the players screen memory location.
  74. rts
  75.  
  76. blockUpHigh:
  77.    dec y
  78.    clc
  79. rts
  80.  
  81. placePlayerBlock:
  82.     pha
  83.     lda pos       ; Stores the LSB of player pos into A
  84.     sec           ; Sets the Carry flag, for later use to subtract multiple bytes.
  85.     sbc #$20      ; Subtracts 20, from the LSB 20 = 1 Horizontal screen line of space, pushing the player up on the next line     ; Stores the resulting LSB value into its memory location
  86.     ldy $12
  87.     bcc blockUpHigh   ; If the carry flag was cleared then we want to move along the MSB once.
  88.     tax               ; Tranfer A to X
  89.     pla               ; Load block back from A
  90.     jsr nblock        ; Place
  91. rts
  92.  
  93. checkCollision:
  94.   lda #$00        ; Clear collision flag
  95.   sta colflag      
  96.   ldx #$00        ; Setup indirect addressing
  97.   lda (pos, X)    ; Load player location into A
  98.   cmp #$00        ; If next player location has something in it, set collision flag
  99.   bne setcol      ; If a block is found, set collision flag
  100. rts
  101. setcol:
  102.   lda #$01
  103.   sta colflag
  104. rts
  105.  
  106. chkblock:
  107.   lda key
  108.   cmp #place
  109.   beq pl
  110.   lda key
  111.   cmp #break
  112.   beq br
  113.   rts
  114.   pl:
  115.      lda #blockplaced
  116.      jsr placePlayerBlock
  117.   rts
  118.   br:
  119.      lda #$00
  120.      jsr placePlayerBlock
  121.   rts
  122. rts
  123.  
  124. game:
  125.   lda key         ; Now for the key input checking, comparing the keys is easy, moving the player... not so much.
  126.   cmp #up         ; Compare the key at A [$ff] with the Up ($77, "w") keys value
  127.   beq mu          ; Jump to Up routine if up was pressed.
  128.   cmp #down       ; Compare A with the down keys value
  129.   beq md          ; Jump to Down routine if up was pressed.
  130.   cmp #left       ; Compare A with the left keys value
  131.   beq ml          ; Jump to Left routine if up was pressed.
  132.   cmp #right      ; Compare A with the right keys value
  133.   beq mr          ; Jump to Right routine if up was pressed.
  134.   jsr chkblock
  135.   rts
  136.   mu:    
  137.     jsr undraw    ; Moving Up
  138.   muDraw:
  139.     jsr getay     ; Stores the LSB of player pos into A
  140.     sec           ; Sets the Carry flag, for later use to subtract multiple bytes.
  141.     sbc #$20      ; Subtracts 20, from the LSB 20 = 1 Horizontal screen line of space, pushing the player up on the next line
  142.     sta $11       ; Stores the resulting LSB value into its memory location
  143.     bcc mua       ; If the carry flag was cleared then we want to move along the MSB once, Jumps to mua routine.
  144.     jsr negk      ; Clears key stored in memory
  145.     jsr checkCollision
  146.     lda colflag
  147.     cmp #$01
  148.     beq mdDraw
  149.     jsr draw      ; Draws the player
  150.   rts
  151.     mua:
  152.       dec $12     ; Decrement the MSB stored in $12 by 1
  153.       jsr negk    ; Clears the stored key
  154.       jsr checkCollision
  155.       lda colflag
  156.       cmp #$01
  157.       beq mdDraw
  158.       jsr draw    ; Draw player
  159.     rts
  160.     md:           ; Moving Down
  161.       jsr undraw
  162.     mdDraw:
  163.       jsr getay
  164.       clc         ; Clear the carry flag
  165.       adc #$20    ; Add 20, Exactly 1 horizontal screen line to the playrs LSB
  166.       sta $11     ; Store that into the LSBs memory location
  167.       bcs mda     ; If the number carried over 1 byte, then increment the next byte MSB
  168.       jsr negk  
  169.       jsr checkCollision
  170.       lda colflag
  171.       cmp #$01
  172.       beq muDraw
  173.       jsr draw
  174.    rts
  175.    mda:
  176.       inc $12     ; Increment $12 MSB player location by 1
  177.       jsr negk
  178.       jsr checkCollision
  179.       lda colflag
  180.       cmp #$01
  181.       beq muDraw
  182.       jsr draw
  183.    rts
  184.    ml:            ; Moving Left
  185.     jsr undraw
  186.    mlDraw:
  187.     dec $11       ; Decrement the players LSB by 1
  188.     jsr negk
  189.     jsr checkCollision
  190.     lda colflag
  191.     cmp #$01
  192.     beq mrDraw
  193.     jsr draw
  194.     rts
  195.    mr:            ;  Moving Right
  196.     jsr undraw
  197.    mrDraw:
  198.     inc $11       ; Increment the players LSB by 1
  199.     jsr negk
  200.     jsr checkCollision
  201.     lda colflag
  202.     cmp #$01
  203.     beq mlDraw
  204.     jsr draw
  205.   rts
  206. rts
  207.  
  208. loop:
  209.   jsr game        ; Loop endlessly
  210. jmp loop
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement