Advertisement
Guest User

Pong

a guest
Aug 2nd, 2012
482
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.   .inesprg 1   ; 1x 16KB PRG code
  2.   .ineschr 1   ; 1x  8KB CHR data
  3.   .inesmap 0   ; mapper 0 = NROM, no bank swapping
  4.   .inesmir 1   ; background mirroring
  5.  
  6.  
  7. ;;;;;;;;;;;;;;;
  8.  
  9. ;; DECLARE SOME VARIABLES HERE
  10.   .rsset $0000  ;;start variables at ram location 0
  11.  
  12. gamestate  .rs 1  ; .rs 1 means reserve one byte of space
  13. ballx      .rs 1  ; ball horizontal position
  14. bally      .rs 1  ; ball vertical position
  15. ballup     .rs 1  ; 1 = ball moving up
  16. balldown   .rs 1  ; 1 = ball moving down
  17. ballleft   .rs 1  ; 1 = ball moving left
  18. ballright  .rs 1  ; 1 = ball moving right
  19. ballspeedx .rs 1  ; ball horizontal speed per frame
  20. ballspeedy .rs 1  ; ball vertical speed per frame
  21. paddle1y      .rs 1  ; paddle vertical position
  22. paddle2y      .rs 1  ; paddle vertical position
  23. paddle1y2      .rs 1  ; paddle vertical position
  24. paddle2y2      .rs 1  ; paddle vertical position
  25. buttons1   .rs 1  ; player 1 gamepad buttons, one bit per button
  26. buttons2   .rs 1  ; player 2 gamepad buttons, one bit per button
  27. scoreOnes     .rs 1  ; byte for each digit in the decimal score
  28. scoreTens     .rs 1
  29. scoreHundreds .rs 1
  30. scoreOnes2     .rs 1  ; byte for each digit in the decimal score
  31. scoreTens2     .rs 1
  32. scoreHundreds2 .rs 1
  33.  
  34.  
  35. ;; DECLARE SOME CONSTANTS HERE
  36. STATETITLE     = $00  ; displaying title screen
  37. STATEPLAYING   = $01  ; move paddles/ball, check for collisions
  38. STATEGAMEOVER  = $02  ; displaying game over screen
  39.  
  40. RIGHTWALL      = $F4  ; when ball reaches one of these, do something
  41. TOPWALL        = $20
  42. BOTTOMWALL     = $E0
  43. LEFTWALL       = $04
  44.  
  45. PADDLE1X       = $08  ; horizontal position for paddles, doesnt move
  46. PADDLE2X       = $F0
  47.  
  48. PS1 = $01  ; paddle vertical speed per frame
  49. PS2 = $01  ; paddle vertical speed per frame
  50.  
  51. ;;;;;;;;;;;;;;;;;;
  52.  
  53.  
  54.  
  55.  
  56.   .bank 0
  57.   .org $C000
  58. RESET:
  59.   SEI          ; disable IRQs
  60.   CLD          ; disable decimal mode
  61.   LDX #$40
  62.   STX $4017    ; disable APU frame IRQ
  63.   LDX #$FF
  64.   TXS          ; Set up stack
  65.   INX          ; now X = 0
  66.   STX $2000    ; disable NMI
  67.   STX $2001    ; disable rendering
  68.   STX $4010    ; disable DMC IRQs
  69.  
  70. vblankwait1:       ; First wait for vblank to make sure PPU is ready
  71.   BIT $2002
  72.   BPL vblankwait1
  73.  
  74. clrmem:
  75.   LDA #$00
  76.   STA $0000, x
  77.   STA $0100, x
  78.   STA $0300, x
  79.   STA $0400, x
  80.   STA $0500, x
  81.   STA $0600, x
  82.   STA $0700, x
  83.   LDA #$FE
  84.   STA $0200, x
  85.   INX
  86.   BNE clrmem
  87.    
  88. vblankwait2:      ; Second wait for vblank, PPU is ready after this
  89.   BIT $2002
  90.   BPL vblankwait2
  91.  
  92.  
  93. LoadPalettes:
  94.   LDA $2002             ; read PPU status to reset the high/low latch
  95.   LDA #$3F
  96.   STA $2006             ; write the high byte of $3F00 address
  97.   LDA #$00
  98.   STA $2006             ; write the low byte of $3F00 address
  99.   LDX #$00              ; start out at 0
  100. LoadPalettesLoop:
  101.   LDA palette, x        ; load data from address (palette + the value in x)
  102.                           ; 1st time through loop it will load palette+0
  103.                           ; 2nd time through loop it will load palette+1
  104.                           ; 3rd time through loop it will load palette+2
  105.                           ; etc
  106.   STA $2007             ; write to PPU
  107.   INX                   ; X = X + 1
  108.   CPX #$20              ; Compare X to hex $10, decimal 16 - copying 16 bytes = 4 sprites
  109.   BNE LoadPalettesLoop  ; Branch to LoadPalettesLoop if compare was Not Equal to zero
  110.                         ; if compare was equal to 32, keep going down
  111.  
  112.  
  113.  
  114.  
  115.  
  116. ;;;Set some initial ball stats
  117.   LDA #$01
  118.   STA balldown
  119.   STA ballright
  120.   LDA #$00
  121.   STA ballup
  122.   STA ballleft
  123.  
  124.   LDA #$50
  125.   STA bally
  126.  
  127.   LDA #$80
  128.   STA ballx
  129.  
  130.   LDA #$06
  131.   STA ballspeedx
  132.   STA ballspeedy
  133.  
  134.   LDA #$50
  135.   STA paddle1y
  136.   STA paddle2y
  137.  
  138.   LDA #$58
  139.   STA paddle1y2
  140.   STA paddle2y2
  141.  
  142.  
  143.   ;;;Set initial score value
  144.   LDA #$00
  145.   STA scoreOnes
  146.   STA scoreTens
  147.   STA scoreHundreds
  148.   STA scoreOnes2
  149.   STA scoreTens2
  150.   STA scoreHundreds2
  151.  
  152.   ;;:Set starting game state
  153.   LDA #STATETITLE
  154.   STA gamestate
  155.  
  156.  
  157.              
  158.   LDA #%10010000   ; enable NMI, sprites from Pattern Table 0, background from Pattern Table 1
  159.   STA $2000
  160.  
  161.   LDA #%00011110   ; enable sprites, enable background, no clipping on left side
  162.   STA $2001
  163.  
  164. Forever:
  165.   JMP Forever     ;jump back to Forever, infinite loop, waiting for NMI
  166.  
  167.  
  168.  
  169. NMI:
  170.   LDA #$00
  171.   STA $2003       ; set the low byte (00) of the RAM address
  172.   LDA #$02
  173.   STA $4014       ; set the high byte (02) of the RAM address, start the transfer
  174.  
  175.   JSR DrawScore
  176.   JSR DrawScore2
  177.  
  178.   ;;This is the PPU clean up section, so rendering the next frame starts properly.
  179.   LDA #%10010000   ; enable NMI, sprites from Pattern Table 0, background from Pattern Table 1
  180.   STA $2000
  181.   LDA #%00011110   ; enable sprites, enable background, no clipping on left side
  182.   STA $2001
  183.   LDA #$00        ;;tell the ppu there is no background scrolling
  184.   STA $2005
  185.   STA $2005
  186.    
  187.   ;;;all graphics updates done by here, run game engine
  188.  
  189.  
  190.   JSR ReadController1  ;;get the current button data for player 1
  191.   JSR ReadController2  ;;get the current button data for player 2
  192.  
  193.  
  194. GameEngine:  
  195.   LDA gamestate
  196.   CMP #STATETITLE
  197.   BEQ EngineTitle    ;;game is displaying title screen
  198.    
  199.   LDA gamestate
  200.   CMP #STATEGAMEOVER
  201.   BEQ EngineGameOver  ;;game is displaying ending screen
  202.  
  203.   LDA gamestate
  204.   CMP #STATEPLAYING
  205.   BEQ EnginePlaying   ;;game is playing
  206. GameEngineDone:  
  207.  
  208.   JSR UpdateSprites  ;;set ball/paddle sprites from positions
  209.  
  210.   JSR MovePaddle1  ;;get the current button data for player 1
  211.   JSR MovePaddle2  ;;get the current button data for player 2  
  212.   JSR CheckPaddleCollision
  213.  
  214.   RTI             ; return from interrupt
  215.  
  216.  
  217.  
  218.  
  219. ;;;;;;;;
  220.  
  221. EngineTitle:    
  222.         LDA buttons1
  223.         AND #%00010000
  224.         BEQ .return
  225.         LDA #STATEPLAYING
  226.         STA gamestate
  227.         JMP EnginePlaying
  228. .return
  229.         JMP GameEngine
  230.  
  231. ;;;;;;;;;
  232.  
  233. EngineGameOver:    
  234.         LDA buttons1
  235.         AND #%00010000
  236.         BEQ .return1
  237.         LDA #STATEPLAYING
  238.         STA gamestate
  239.         LDA #$00
  240.   STA scoreOnes
  241.   STA scoreTens
  242.   STA scoreHundreds
  243.   STA scoreOnes2
  244.   STA scoreTens2
  245.   STA scoreHundreds2
  246.         JMP EnginePlaying
  247. .return1
  248.         JMP GameEngine
  249.  
  250. ;;;;;;;;;;;
  251.  
  252. EnginePlaying:
  253.  
  254. MoveBallRight:
  255.   LDA ballright
  256.   BEQ MoveBallRightDone   ;;if ballright=0, skip this section
  257.  
  258.   LDA ballx
  259.   CLC
  260.   ADC ballspeedx        ;;ballx position = ballx + ballspeedx
  261.   STA ballx
  262.  
  263.   LDA ballx
  264.   CMP #RIGHTWALL
  265.   BCC MoveBallRightDone      ;;if ball x < right wall, still on screen, skip next section
  266.   LDA #$00
  267.   STA ballright
  268.   LDA #$01
  269.   STA ballleft         ;;bounce, ball now moving left
  270.   ;;in real game, give point to player 1, reset ball
  271.   jsr IncrementScore1
  272. MoveBallRightDone:
  273.  
  274.  
  275. MoveBallLeft:
  276.   LDA ballleft
  277.   BEQ MoveBallLeftDone   ;;if ballleft=0, skip this section
  278.  
  279.   LDA ballx
  280.   SEC
  281.   SBC ballspeedx        ;;ballx position = ballx - ballspeedx
  282.   STA ballx
  283.  
  284.   LDA ballx
  285.   CMP #LEFTWALL
  286.   BCS MoveBallLeftDone      ;;if ball x > left wall, still on screen, skip next section
  287.   LDA #$01
  288.   STA ballright
  289.   LDA #$00
  290.   STA ballleft         ;;bounce, ball now moving right
  291.   ;;in real game, give point to player 2, reset ball
  292.     jsr IncrementScore2
  293. MoveBallLeftDone:
  294.  
  295.  
  296. MoveBallUp:
  297.   LDA ballup
  298.   BEQ MoveBallUpDone   ;;if ballup=0, skip this section
  299.  
  300.   LDA bally
  301.   SEC
  302.   SBC ballspeedy        ;;bally position = bally - ballspeedy
  303.   STA bally
  304.  
  305.   LDA bally
  306.   CMP #TOPWALL
  307.   BCS MoveBallUpDone      ;;if ball y > top wall, still on screen, skip next section
  308.   LDA #$01
  309.   STA balldown
  310.   LDA #$00
  311.   STA ballup         ;;bounce, ball now moving down
  312. MoveBallUpDone:
  313.  
  314.  
  315. MoveBallDown:
  316.   LDA balldown
  317.   BEQ MoveBallDownDone   ;;if ballup=0, skip this section
  318.  
  319.   LDA bally
  320.   CLC
  321.   ADC ballspeedy        ;;bally position = bally + ballspeedy
  322.   STA bally
  323.  
  324.   LDA bally
  325.   CMP #BOTTOMWALL
  326.   BCC MoveBallDownDone      ;;if ball y < bottom wall, still on screen, skip next section
  327.   LDA #$00
  328.   STA balldown
  329.   LDA #$01
  330.   STA ballup         ;;bounce, ball now moving down
  331. MoveBallDownDone:
  332.   JMP GameEngineDone
  333.  
  334. MovePaddle1:
  335.  
  336.     lda buttons1        ;load the current button state for controller1
  337.     and #%00000100   ;isolate the bit representing "up", by clearing all the other bits
  338.     beq ReadDownDone          ;if the result was 0, then the "up" bit was clear (thus not pressed), so skip the following code
  339.     ;...
  340.  
  341.   LDA $0204 ; load sprite 2 position
  342.   CLC ; make sure carry flag is set
  343.   ADC #PS1 ; A = A - 1
  344.   STA $0204 ; save sprite 2 position
  345.   LDA $0204
  346.   STA paddle1y
  347.   LDA $020C ; load sprite 2 position
  348.   CLC ; make sure carry flag is set
  349.   ADC #PS1 ; A = A - 1
  350.   STA $020C ; save sprite 2 position
  351.   LDA $020C
  352.   STA paddle1y2
  353.   LDA paddle1y2
  354.   CMP #BOTTOMWALL
  355.   BCC ReadDownDone
  356.   SEC  ; make sure carry flag is set
  357.   SBC #$01 ; A = A - 1
  358.   STA paddle1y2
  359.   LDA paddle1y
  360.   SEC  ; make sure carry flag is set
  361.   SBC #$01 ; A = A - 1
  362.   STA paddle1y  
  363.  
  364. ;  LDA paddle1y
  365. ;  SEC
  366. ;  SBC PS1        ;;bally position = bally - ballspeedy
  367. ;  STA paddle1y
  368. ;
  369. ;  LDA paddle1y
  370. ;  CMP #TOPWALL
  371. ;  BCS ReadDownDone      ;;if ball y > top wall, still on screen, skip next section
  372. ;  LDA #$01
  373. ;  STA paddle1down
  374. ;  LDA #$00
  375. ;  STA paddle1down         ;;bounce, ball now moving down  
  376.  
  377. ReadDownDone:
  378.  
  379.     lda buttons1        ;load the current button state for controller1
  380.     and #%00001000   ;isolate the bit representing "up", by clearing all the other bits
  381.     beq ReadUpDone           ;if the result was 0, then the "up" bit was clear (thus not pressed), so skip the following code
  382.     ;...
  383.  
  384. ;  LDA $4016    
  385. ;  CMP #%00001000
  386. ;  BNE ReadUpDone
  387.   LDA $0204 ; load sprite 2 position
  388.   SEC ; make sure carry flag is set
  389.   SBC #PS1 ; A = A - 1
  390.   STA $0204 ; save sprite 2 position
  391.   LDA $0204
  392.   STA paddle1y
  393.   LDA $020C ; load sprite 2 position
  394.   SEC ; make sure carry flag is set
  395.   SBC #PS1 ; A = A - 1
  396.   STA $020C ; save sprite 2 position  
  397.   LDA $020C
  398.   STA paddle1y2
  399.   LDA paddle1y
  400.   CMP #TOPWALL
  401.   BCS ReadUpDone
  402.   CLC ; make sure carry flag is set
  403.   ADC #$01 ; A = A - 1
  404.   STA paddle1y
  405.   LDA paddle1y2  
  406.   CLC ; make sure carry flag is set
  407.   ADC #$01 ; A = A - 1
  408.   STA paddle1y2    
  409. ;  LDA paddle1y
  410. ;  SEC
  411. ;  SBC PS1        ;;bally position = bally - ballspeedy
  412. ;  STA paddle1y
  413. ;
  414. ;  LDA paddle1y
  415. ;  CMP #TOPWALL
  416. ;  BCS ReadUpDone      ;;if ball y > top wall, still on screen, skip next section
  417. ;  LDA #$01
  418. ;  STA paddle1down
  419. ;  LDA #$00
  420. ;  STA paddle1up         ;;bounce, ball now moving down
  421. ReadUpDone:    
  422.   RTS
  423.  
  424. MovePaddle2:
  425.  
  426.     lda buttons2        ;load the current button state for controller1
  427.     and #%00000100   ;isolate the bit representing "up", by clearing all the other bits
  428.     beq MovePaddleDownDone           ;if the result was 0, then the "up" bit was clear (thus not pressed), so skip the following code
  429.     ;...
  430.   LDA $0208 ; load sprite 2 position
  431.   CLC ; make sure carry flag is set
  432.   ADC #PS2 ; A = A - 1
  433.   STA $0208 ; save sprite 2 position
  434.   LDA $0208
  435.   STA paddle2y
  436.   LDA $0210 ; load sprite 2 position
  437.   CLC ; make sure carry flag is set
  438.   ADC #PS2 ; A = A - 1
  439.   STA $0210 ; save sprite 2 position
  440.   LDA $0210
  441.   STA paddle2y2  
  442.   LDA paddle2y2
  443.   CMP #BOTTOMWALL
  444.   BCC MovePaddleDownDone
  445.   SEC  ; make sure carry flag is set
  446.   SBC #$01 ; A = A - 1
  447.   STA paddle2y2
  448.   LDA paddle2y
  449.   SEC  ; make sure carry flag is set
  450.   SBC #$01 ; A = A - 1
  451.   STA paddle2y  
  452.  
  453. MovePaddleDownDone:
  454.  
  455.     lda buttons2        ;load the current button state for controller1
  456.     and #%00001000   ;isolate the bit representing "up", by clearing all the other bits
  457.     beq MovePaddleUpDone           ;if the result was 0, then the "up" bit was clear (thus not pressed), so skip the following code
  458.     ;...
  459.   LDA $0208 ; load sprite 2 position
  460.   SEC ; make sure carry flag is set
  461.   SBC #PS2 ; A = A - 1
  462.   STA $0208 ; save sprite 2 position
  463.   LDA $0208
  464.   STA paddle2y
  465.   LDA $0210 ; load sprite 2 position
  466.   SEC ; make sure carry flag is set
  467.   SBC #PS2 ; A = A - 1
  468.   STA $0210 ; save sprite 2 position
  469.   LDA $0210
  470.   STA paddle2y2  
  471.   LDA paddle2y
  472.   CMP #TOPWALL
  473.   BCS MovePaddleUpDone
  474.   CLC ; make sure carry flag is set
  475.   ADC #$01 ; A = A - 1
  476.   STA paddle2y
  477.   LDA paddle2y2
  478.   CLC ; make sure carry flag is set
  479.   ADC #$01 ; A = A - 1
  480.   STA paddle2y2  
  481.  
  482.   ;;if up button pressed
  483.   ;;  if paddle top > top wall
  484.   ;;    move paddle top and bottom up
  485. MovePaddleUpDone:
  486.   RTS
  487.  
  488.  
  489.  
  490. Skip:
  491.  
  492. CheckPaddleCollision:
  493.   LDA ballx
  494.   CMP #PADDLE1X+8
  495.   BCS CheckPaddleCollisionDone      ;;if ball x > left wall, still on screen, skip next section    
  496.   LDA bally
  497.   CMP paddle1y
  498.   BCS CheckPaddleCollisionDone      ;;if ball y > top wall, still on screen, skip next section  
  499.   LDA bally
  500.   CMP paddle1y2
  501.   BCC CheckPaddleCollisionDone      ;;if ball y < bottom wall, still on screen, skip next section  
  502. ;  LDA ballx
  503. ;  CLC
  504. ;  ADC ballspeedx        ;;ballx position = ballx - ballspeedx
  505. ;  STA ballx
  506.   LDA #$01
  507.   STA ballright  
  508.   LDA #$00
  509.   STA ballleft         ;;bounce, ball now moving right
  510.   LDA bally
  511.   CMP paddle1y2
  512.   BCS .point2
  513.   LDA #$01
  514.   STA ballup  
  515.   LDA #$00
  516.   STA balldown  
  517. ;  LDA bally
  518. ;  SEC
  519. ;  SBC ballspeedy        ;;bally position = bally - ballspeedy
  520. ;  STA bally
  521.   JMP CheckPaddleCollisionDone
  522. .point2
  523. ;  LDA #$01  
  524. ;  LDA bally
  525. ;  CLC
  526. ;  ADC ballspeedy        ;;bally position = bally - ballspeedy
  527. ;  STA bally
  528.   LDA #$01
  529.   STA balldown
  530.   LDA #$00
  531.   STA ballup  
  532.  
  533. ;  LDA #$00
  534. ;  STA ballleft
  535. ;  LDA #$00
  536. ;  STA balldown
  537. ;  LDA #$00
  538. ;  STA ballup  
  539. CheckPaddleCollisionDone:
  540.   RTS
  541. ;  JMP GameEngineDone
  542.  
  543.  
  544.  
  545.  
  546. UpdateSprites:
  547.   LDA bally  ;;update all ball sprite info
  548.   STA $0200
  549.  
  550.   LDA #$30
  551.   STA $0201
  552.  
  553.   LDA #$00
  554.   STA $0202
  555.  
  556.   LDA ballx
  557.   STA $0203
  558.  
  559.   LDA paddle1y  ;;update all ball sprite info
  560.   STA $0204
  561.  
  562.   LDA #$7F
  563.   STA $0205
  564.  
  565.   LDA #$10
  566.   STA $0206
  567.  
  568.   LDA #PADDLE1X
  569.   STA $0207
  570.  
  571.   LDA paddle1y2  ;;update all ball sprite info
  572.   STA $020C
  573.  
  574.   LDA #$7F
  575.   STA $020D
  576.  
  577.   LDA #$10
  578.   STA $020E
  579.  
  580.   LDA #PADDLE1X
  581.   STA $020F
  582.  
  583.   LDA paddle2y
  584.   STA $0208
  585.  
  586.   LDA #$7F
  587.   STA $0209
  588.  
  589.   LDA #$10
  590.   STA $020A
  591.  
  592.   LDA #PADDLE2X
  593.   STA $020B
  594.  
  595.   LDA paddle2y2
  596.   STA $0210
  597.  
  598.   LDA #$7F
  599.   STA $0211
  600.  
  601.   LDA #$10
  602.   STA $0212
  603.  
  604.   LDA #PADDLE2X
  605.   STA $0213  
  606.  
  607.   ;;update paddle sprites
  608.   RTS
  609.  
  610.  
  611. DrawScore:
  612.   LDA $2002
  613.   LDA #$20
  614.   STA $2006
  615.   LDA #$20
  616.   STA $2006          ; start drawing the score at PPU $2020
  617.  
  618. .Zero1  
  619.   LDA scoreHundreds      ; last digit  
  620.   cmp #$00
  621.   beq .pointa
  622.   cmp #$24
  623.   bne .point
  624.   LDA scoreTens      ; next digit
  625.   cmp #$00
  626.   bne .point
  627.   LDA #$24
  628.   STA scoreTens
  629.   JMP .point
  630. .pointa
  631.   LDA #$24
  632.   STA scoreHundreds
  633.  
  634. .point  
  635.   LDA scoreHundreds  ; get first digit
  636. ;  CLC
  637. ;  ADC #$30           ; add ascii offset  (this is UNUSED because the tiles for digits start at 0)
  638.   STA $2007          ; draw to background
  639.   LDA scoreTens      ; next digit
  640. ;  CLC
  641. ;  ADC #$30           ; add ascii offset
  642.   STA $2007
  643.   LDA scoreOnes      ; last digit
  644. ;  CLC
  645. ;  ADC #$30           ; add ascii offset
  646.   STA $2007
  647.   RTS
  648.  
  649. DrawScore2:
  650.   LDA $2002
  651.   LDA #$20
  652.   STA $2006
  653.   LDA #$3C
  654.   STA $2006          ; start drawing the score at PPU $203C
  655.  
  656. .Zero  
  657.   LDA scoreHundreds2      ; last digit  
  658.   cmp #$00
  659.   beq .point2b
  660.   cmp #$24
  661.   bne .point2
  662.   LDA scoreTens2      ; next digit
  663.   cmp #$00
  664.   bne .point2
  665.   LDA #$24
  666.   STA scoreTens2
  667.   JMP .point2
  668. .point2b
  669.   LDA #$24
  670.   STA scoreHundreds2
  671.  
  672. .point2
  673.   LDA scoreHundreds2  ; get first digit  
  674. ;  CLC
  675. ;  ADC #$30           ; add ascii offset  (this is UNUSED because the tiles for digits start at 0)
  676.   STA $2007          ; draw to background
  677.   LDA scoreTens2      ; next digit
  678. ;  CLC
  679. ;  ADC #$30           ; add ascii offset
  680.   STA $2007
  681.   LDA scoreOnes2      ; last digit
  682. ;  CLC
  683. ;  ADC #$30           ; add ascii offset
  684.   STA $2007
  685.   RTS
  686.  
  687.  
  688. IncrementScore1:
  689. IncOnes:
  690.   LDA scoreOnes      ; load the lowest digit of the number
  691.   CLC
  692.   ADC #$01           ; add one
  693.   STA scoreOnes
  694.   CMP #$0A           ; check if it overflowed, now equals 10
  695.   bne IncDone        ; if there was no overflow, all done
  696. IncTens:
  697.   LDA #$00
  698.   STA scoreOnes      ; wrap digit to 0
  699.   LDA scoreTens      ; load the next digit
  700.   cmp #$24
  701.   bne .add
  702.   sec
  703.   sbc #$24
  704.   STA scoreTens
  705. .add  
  706.   LDA scoreTens      ; load the next digit
  707.   CLC
  708.   ADC #$01           ; add one, the carry from previous digit
  709.   STA scoreTens
  710.   CMP #$0A           ; check if it overflowed, now equals 10
  711.   bne IncDone        ; if there was no overflow, all done
  712. IncHundreds:
  713.   LDA #$00
  714.   STA scoreTens      ; wrap digit to 0
  715.   LDA scoreHundreds  ; load the next digit
  716.   cmp #$24
  717.   bne .add1
  718.   sec
  719.   sbc #$24
  720.   STA scoreHundreds
  721. .add1  
  722.   LDA scoreHundreds  ; load the next digit
  723.   CLC
  724.   ADC #$01           ; add one, the carry from previous digit
  725.   STA scoreHundreds  
  726. IncDone:
  727.   JMP IncDone2
  728.  
  729. IncrementScore2:
  730. IncOnes2:
  731.   LDA scoreOnes2      ; load the lowest digit of the number
  732.   CLC
  733.   ADC #$01           ; add one
  734.   STA scoreOnes2
  735.   CMP #$0A           ; check if it overflowed, now equals 10
  736.   BNE IncDone2        ; if there was no overflow, all done
  737. IncTens2:
  738.   LDA #$00
  739.   STA scoreOnes2      ; wrap digit to 0
  740.   LDA scoreTens2      ; load the next digit
  741.   cmp #$24
  742.   bne .add
  743.   sec
  744.   sbc #$24
  745.   STA scoreTens2
  746. .add  
  747.   LDA scoreTens2      ; load the next digit
  748.   CLC
  749.   ADC #$01           ; add one, the carry from previous digit
  750.   STA scoreTens2
  751.   CMP #$0A           ; check if it overflowed, now equals 10
  752.   BNE IncDone2        ; if there was no overflow, all done
  753. IncHundreds2:
  754.   LDA #$00
  755.   STA scoreTens2      ; wrap digit to 0
  756.   LDA scoreHundreds2  ; load the next digit
  757.   cmp #$24
  758.   bne .add1
  759.   sec
  760.   sbc #$24
  761.   STA scoreHundreds2
  762. .add1  
  763.   LDA scoreHundreds2  ; load the next digit
  764.   CLC
  765.   ADC #$01           ; add one, the carry from previous digit
  766.   STA scoreHundreds2
  767. IncDone2:
  768.   LDA scoreHundreds
  769.   cmp #$01
  770.   BEQ GameOver
  771.   LDA scoreHundreds2
  772.   cmp #$01
  773.   BEQ GameOver  
  774.  
  775. ReadController1:
  776.   LDA #$01
  777.   STA $4016
  778.   LDA #$00
  779.   STA $4016
  780.   LDX #$08
  781. ReadController1Loop:
  782.   LDA $4016
  783.   LSR A            ; bit0 -> Carry
  784.   ROL buttons1     ; bit0 <- Carry
  785.   DEX
  786.   BNE ReadController1Loop
  787.   RTS
  788.  
  789. ReadController2:
  790.   LDA #$01
  791.   STA $4016
  792.   LDA #$00
  793.   STA $4016
  794.   LDX #$08
  795. ReadController2Loop:
  796.   LDA $4017
  797.   LSR A            ; bit0 -> Carry
  798.   ROL buttons2     ; bit0 <- Carry
  799.   DEX
  800.   BNE ReadController2Loop
  801.   RTS  
  802.  
  803. GameOver:
  804.         LDA #STATEGAMEOVER
  805.         STA gamestate
  806.         JMP GameEngine
  807.  
  808.    
  809.        
  810. ;;;;;;;;;;;;;;  
  811.  
  812.  
  813.  
  814.   .bank 1
  815.   .org $E000
  816. palette:
  817.   .db $22,$29,$1A,$0F,  $22,$36,$17,$0F,  $22,$30,$21,$0F,  $22,$27,$17,$0F   ;;background palette
  818.   .db $22,$1C,$15,$14,  $22,$02,$38,$3C,  $22,$1C,$15,$14,  $22,$02,$38,$3C   ;;sprite palette
  819.  
  820. sprites:
  821.      ;vert tile attr horiz
  822.         .db $52, $01, $00, $28 ; Player1 paddle top
  823.         .db $5A, $11, $00, $28 ; Player1 paddle bottom
  824.        
  825.         .db $52, $01, $01, $D8 ; Player2 paddle top
  826.         .db $5A, $11, $01, $D8 ; Player2 paddle bottom
  827.        
  828.         .db $74, $02, $02, $78 ; Ball
  829.  
  830.  
  831.  
  832.   .org $FFFA     ;first of the three vectors starts here
  833.   .dw NMI        ;when an NMI happens (once per frame if enabled) the
  834.                    ;processor will jump to the label NMI:
  835.   .dw RESET      ;when the processor first turns on or is reset, it will jump
  836.                    ;to the label RESET:
  837.   .dw 0          ;external interrupt IRQ is not used in this tutorial
  838.  
  839.  
  840. ;;;;;;;;;;;;;;  
  841.  
  842.  
  843.   .bank 2
  844.   .org $0000
  845.   .incbin "mario.chr"   ;includes 8KB graphics file from SMB1
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement