Advertisement
truffly

galaxypatrol.asm

Apr 3rd, 2018
229
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ; iNES header
  2.   .inesprg 2  ; 2x 16kb program memory
  3.   .ineschr 1  ; 8kb CHR Data
  4.   .inesmap 0  ; no mapping
  5.   .inesmir 1  ; background mirroring -- vertical mirroring = horizontal scrolling
  6.  
  7. ;---------------------------------------------------
  8. ;; Some Variables
  9.   .rsset $0000 ; start variables from RAM location 0
  10.  
  11. gamestate           .rs 1   ; .rs 1 means reserve 1 byte of space
  12. buttons             .rs 1
  13. playerX             .rs 1
  14. playerY             .rs 1
  15. score                   .rs 1
  16. speedy              .rs 1 ; player's speed in the y direction
  17. speedx                .rs 1 ; object's speed in the x direction
  18. scroll              .rs 1
  19. nametable           .rs 1
  20. collide_flag        .rs 1 ; 1 = asteroid; 2 = fuel
  21. sleep_flag          .rs 1
  22. draw_flag           .rs 1
  23.  
  24. ;---------------------------------------------------
  25. ;; Constants
  26. STATETITLE  = $00   ; Displaying title screen
  27. STATEPLAYING    = $01   ; playing the game; draw graphics, check paddles, etc.
  28. STATEGAMEOVER   = $02   ; game over sequence
  29. STATEPAUSE  = $03 ; we are in pause
  30.  
  31. TOPWALL = $0A
  32. BOTTOMWALL = $D8
  33.  
  34. ;---------------------------------------------------
  35. ;; Our first 8kb bank. Include the sound engine here
  36.   .bank 0
  37.   .org $8000
  38.  
  39.   .include "sound_engine.asm"
  40.  
  41. ;---------------------------------------------------
  42. ;; Second 8kb bank
  43.   .bank 1
  44.   .org $A000
  45.  
  46. ;---------------------------------------------------
  47. ;; Third 8kb bank
  48.  
  49.   .bank 2
  50.   .org $C000
  51.  
  52.   .include "reset.asm"
  53.  
  54.   jsr reset ; put this in a separate file for code that is easier to read
  55.  
  56. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  57. ;;;;  Main Game Loop  ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  58. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  59.  
  60. MainGameLoop:
  61.   ;; put game logic here. Use a "sleep" flag to prevent us from doing too much per frame
  62.   ;; currently, not working...why?
  63. MainGameLoop_Wait:
  64.   lda sleep_flag
  65.   bne MainGameLoop_Wait
  66.  
  67.   jsr ReadController
  68.   jsr GameEngine
  69.  
  70.   inc sleep_flag
  71.  
  72.   JMP MainGameLoop
  73.  
  74. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  75. ;;;;;;      NMI     ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  76. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  77.  
  78. NMI:              ; This interrupt routine will be called every time VBlank begins
  79.   ; transfer all registers to the stack -- in case we decide to move game logic later
  80.   php
  81.   pha
  82.   txa
  83.   pha
  84.   tya
  85.   pha
  86.  
  87.   INC scroll
  88.  
  89. NTSwapCheck:      ; checks to see if we have scrolled all the way to the second nametable
  90.   lda scroll
  91.   cmp #$00
  92.   bne NTSwapCheckDone
  93.  
  94. NTSwap:           ; if we have scrolled all the way to the second, display the second, not first
  95.   lda nametable ; 0 or 1
  96.   eor #$01
  97.   sta nametable
  98.  
  99. NTSwapCheckDone:  ; done with our scroll logic, time to actually draw the graphics
  100.   LDA #$00
  101.   STA $2003       ; Write zero to the OAM register because we want to use DMA
  102.   LDA #$02
  103.   STA $4014       ; Write to OAM using DMA -- copies bytes at $0200 to OAM
  104.  
  105.   ;jsr sound_play_frame  ; play sound
  106.  
  107.   ; Clear PPU address register
  108.   LDA #$00
  109.   STA $2006
  110.   STA $2006
  111.  
  112.   ; scroll the screen
  113.   lda scroll
  114.   sta $2005
  115.   lda #$00
  116.   sta $2005
  117.  
  118.   ;; PPU clean-up; ensure rendering the next frame starts properly.
  119.   LDA #%10010000   ; enable NMI, sprites from Pattern Table 0, background from Pattern Table 1
  120.   ora nametable
  121.   STA $2000
  122.   LDA #%00011110   ; enable sprites, enable background, no clipping on left side
  123.   STA $2001
  124.  
  125.   ; jsr ReadController
  126.   ; jsr GameEngine
  127.  
  128.   lda #$00
  129.   sta draw_flag
  130.   sta sleep_flag
  131.  
  132.   pla
  133.   tay
  134.   pla
  135.   tax
  136.   pla
  137.   plp
  138.  
  139.   RTI
  140.  
  141. GameEngine:
  142.   LDA gamestate
  143.   CMP #STATETITLE
  144.   BEQ EngineTitle   ;; game is displaying Title Screen
  145.  
  146.   LDA gamestate
  147.   CMP #STATEGAMEOVER
  148.   BEQ EngineGameOver    ;; game is displaying game over sequence
  149.  
  150.   LDA gamestate
  151.   CMP #STATEPAUSE
  152.   BEQ EnginePause
  153.  
  154.   LDA gamestate
  155.   CMP #STATEPLAYING
  156.   BEQ EnginePlaying ;; game is in the game loop
  157. GameEngineDone:
  158.   JSR UpdateSprites ;; update all sprites once we are done with the game engine
  159.   RTS
  160.  
  161. ;;;;; Our Game Engine Routines
  162.  
  163. EngineTitle:  ; what do we do on the title screen?
  164.   JMP GameEngineDone    ;; Unconditional jump to GameEngineDone so we can continue with the loop
  165.  
  166. EngineGameOver:
  167.   JMP GameEngineDone
  168.  
  169. EnginePause
  170.   JMP EnginePauseDone
  171. EnginePauseDone:
  172.  
  173. EnginePlaying:
  174.   ; First, we check to see if we need to handle controllers
  175.  
  176. PressSelect:
  177.   LDA buttons
  178.   AND #%00100000
  179.   BEQ PressSelectDone
  180.  
  181.   JSR sound_disable
  182. PressSelectDone:
  183.  
  184. PressStart:
  185.   LDA buttons
  186.   AND #%00010000
  187.   BEQ PressStartDone
  188.  
  189.   LDA #%00111000
  190.   STA $4000
  191.  
  192.   LDA note_ptr ; this makes 1 byte
  193.   ASL A   ; but, we are indexing to a table of words. So, we must multiply by 2 to make the byte into a word
  194.   TAY
  195.   LDA note_table, y
  196.   STA SQ1_LOW
  197.   LDA note_table+1, y
  198.   STA SQ1_HIGH
  199.  
  200.   lda #$00
  201.   sta note_move_flag
  202. PressStartDone:
  203.  
  204. MoveUp:
  205.   LDA buttons
  206.   AND #%00001000
  207.   BEQ MoveUpDone
  208.  
  209.   lda draw_flag
  210.   bne MoveUpDone
  211.  
  212.   LDA playerY       ; same logic as MoveRight here
  213.   SEC
  214.   SBC speedy
  215.   STA playerY
  216.  
  217.   inc draw_flag
  218.  
  219.   LDA playerY
  220.   CMP #TOPWALL
  221.   BCS MoveUpDone    ; must be above or equal to left wall, so carry flag SHOULD be set, not clear
  222.   LDA #TOPWALL
  223.   STA playerY
  224. MoveUpDone:
  225.  
  226. MoveDown:
  227.   LDA buttons       ; first, check user input -- are they hitting down on D pad?
  228.   AND #%00000100
  229.   BEQ MoveDownDone  ; if not, we are done
  230.  
  231.   lda draw_flag
  232.   bne MoveDownDone
  233.  
  234.   LDA playerY       ; if they are, load the current Y position
  235.   CLC           ; clear carry
  236.   ADC speedy        ; add the y speed to the y position
  237.   STA playerY       ; store that in the player y position
  238.  
  239.   inc draw_flag
  240.  
  241.   LDA playerY       ; now we must check to see if player y > wall
  242.   CMP #BOTTOMWALL   ; compare the y position to the right wall. Carry flag set if A >= M
  243.   BCC MoveDownDone  ; if it's less than or equal, then we are done (carry flag not set if less than RIGHTWALL)
  244.   LDA #BOTTOMWALL   ; otherwise, load the wall value into the accumulator
  245.   STA playerY       ; and then set the playerX value equal to the wall
  246. MoveDownDone:
  247.  
  248. PressLeft:
  249.   lda buttons
  250.   and #%00000010
  251.   beq PressLeftDone
  252.  
  253.   lda note_move_flag
  254.   bne PressLeftDone
  255.  
  256.   dec note_ptr
  257.   lda #$01
  258.   sta note_move_flag
  259. PressLeftDone:
  260.  
  261. PressRight:
  262.   lda buttons
  263.   and #%00000001
  264.   beq PressRightDone
  265.  
  266.   lda note_move_flag
  267.   bne PressRightDone
  268.  
  269.   inc note_ptr
  270.   lda #$01
  271.   sta note_move_flag
  272. PressRightDone:
  273.  
  274.   ; Next, we need to check for collisions
  275. CheckCollision:
  276.   JMP CheckCollisionDone
  277. CheckCollisionDone:
  278.  
  279.   JMP GameEngineDone  ; we are done with the game engine code, so let's go to that label
  280.  
  281. ;;;;;     Our Subroutines   ;;;;;
  282.  
  283. ;;; Update our sprite positions ;;;
  284.  
  285. UpdateSprites:
  286.   LDA playerY
  287.   STA $0200
  288.   STA $0204
  289.   CLC
  290.   ADC #$08
  291.   STA $0208
  292.   STA $020C
  293.   ;; once we add in obstacles like rocks and fuel, we will update them here as well
  294.   ;; those routines will probably simply be decrementing the X position
  295.   RTS
  296.  
  297. ;;;;; Read Controller Input ;;;;;
  298.  
  299. ReadController:
  300.   LDA #$01
  301.   STA $4016
  302.   LDA #$00
  303.   STA $4016
  304.   LDX #$08
  305. ReadControllerLoop:
  306.   LDA $4016
  307.   LSR A     ; bit 0 -> carry flag
  308.   ROL buttons   ; carry flag -> bit 0 of buttons 1
  309.   DEX       ; we could start x at 0 and then compare x to 8, but that requires 1 extra instruction
  310.   BNE ReadControllerLoop
  311.   RTS
  312.  
  313. vblankwait: ; subroutine for PPU initialization
  314.   BIT $2002
  315.   BPL vblankwait
  316.   RTS
  317.  
  318. ;---------------------------------------------------
  319. ;; 4th 8kb bank -- Data tables
  320.  
  321.   .bank 3
  322.   .org $E000
  323. palette:
  324.   .db $30,$00,$10,$0F,  $0F,$36,$17,$22,  $0F,$30,$21,$22,  $0F,$27,$17,$22   ;;background palette
  325.   .db $22,$1C,$15,$14,  $22,$02,$38,$3C,  $22,$1C,$15,$14,  $22,$02,$38,$3C   ;;sprite palette
  326.  
  327. sprites:
  328.      ;vert tile attr horiz
  329.   .db $80, $00, $00, $10   ;sprite 0
  330.   .db $80, $01, $00, $18   ;sprite 1
  331.   .db $88, $02, $00, $10
  332.   .db $88, $03, $00, $18
  333.  
  334. background:
  335.   .db $00, $01, $02, $03, $04, $05, $06, $07, $08, $09, $0A, $0B, $0C, $0D, $0E, $0F
  336.   .db $0F, $0E, $0D, $0C, $0B, $0A, $09, $08, $07, $06, $05, $04, $03, $02, $01, $00
  337.   .db $00, $01, $02, $03, $04, $05, $06, $07, $08, $09, $0A, $0B, $0C, $0D, $0E, $0F
  338.   .db $0F, $0E, $0D, $0C, $0B, $0A, $09, $08, $07, $06, $05, $04, $03, $02, $01, $00
  339.   .db $0F, $0E, $0D, $0C, $0B, $0A, $09, $08, $07, $06, $05, $04, $03, $02, $01, $00
  340.   .db $00, $01, $02, $03, $04, $05, $06, $07, $08, $09, $0A, $0B, $0C, $0D, $0E, $0F
  341.   .db $0F, $0E, $0D, $0C, $0B, $0A, $09, $08, $07, $06, $05, $04, $03, $02, $01, $00
  342.   .db $00, $01, $02, $03, $04, $05, $06, $07, $08, $09, $0A, $0B, $0C, $0D, $0E, $0F
  343.  
  344. ;---------------------------------------------------
  345. ;; Vectors
  346.  
  347.   .org $FFFA
  348.   .dw NMI
  349.  
  350.   .dw RESET
  351.  
  352.   .dw 0
  353.  
  354.   .bank 4
  355.   .org $0000
  356.   .incbin "NewFile.chr" ; custom graphics file
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement