Advertisement
Guest User

Pong Done

a guest
Aug 4th, 2012
445
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 #$03
  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.   PLA                ;<--------------ADD THESE
  171.   PLA
  172.   PLA
  173.   LDA #$00
  174.   STA $2003       ; set the low byte (00) of the RAM address
  175.   LDA #$02
  176.   STA $4014       ; set the high byte (02) of the RAM address, start the transfer
  177.  
  178.   JSR DrawScore
  179.   JSR DrawScore2
  180.  
  181.   ;;This is the PPU clean up section, so rendering the next frame starts properly.
  182.   LDA #%10010000   ; enable NMI, sprites from Pattern Table 0, background from Pattern Table 1
  183.   STA $2000
  184.   LDA #%00011110   ; enable sprites, enable background, no clipping on left side
  185.   STA $2001
  186.   LDA #$00        ;;tell the ppu there is no background scrolling
  187.   STA $2005
  188.   STA $2005
  189.    
  190.   ;;;all graphics updates done by here, run game engine
  191.  
  192.  
  193.   JSR ReadController1  ;;get the current button data for player 1
  194.   JSR ReadController2  ;;get the current button data for player 2
  195.  
  196.  
  197. GameEngine:  
  198.   LDA gamestate
  199.   CMP #STATETITLE
  200.   BEQ EngineTitle    ;;game is displaying title screen
  201.    
  202.   LDA gamestate
  203.   CMP #STATEGAMEOVER
  204.   BEQ EngineGameOver  ;;game is displaying ending screen
  205.  
  206.   LDA gamestate
  207.   CMP #STATEPLAYING
  208.   BEQ EnginePlaying   ;;game is playing
  209. GameEngineDone:  
  210.  
  211.   JSR UpdateSprites  ;;set ball/paddle sprites from positions
  212.  
  213.   JSR MovePaddle1  ;;get the current button data for player 1
  214.   JSR MovePaddle2  ;;get the current button data for player 2  
  215.   JSR CheckPaddleCollision
  216.   JSR CheckPaddleCollision1  
  217.  
  218. ;  RTI             ; return from interrupt
  219.   JMP Forever
  220.  
  221.  
  222.  
  223. ;;;;;;;;
  224.  
  225. EngineTitle:    
  226.         LDA buttons1
  227.         AND #%00010000
  228.         BEQ .return
  229.         LDA #STATEPLAYING
  230.         STA gamestate
  231.         JMP EnginePlaying
  232. .return
  233.         JMP GameEngine
  234.  
  235. ;;;;;;;;;
  236.  
  237. EngineGameOver:    
  238.         LDA buttons1
  239.         AND #%00010000
  240.         BEQ .return1
  241.         LDA #STATEPLAYING
  242.         STA gamestate
  243.         LDA #$00
  244.   STA scoreOnes
  245.   STA scoreTens
  246.   STA scoreHundreds
  247.   STA scoreOnes2
  248.   STA scoreTens2
  249.   STA scoreHundreds2
  250.         JMP EnginePlaying
  251. .return1
  252.         JMP GameEngine
  253.  
  254. ;;;;;;;;;;;
  255.  
  256. EnginePlaying:
  257.  
  258. MoveBallRight:
  259.   LDA ballright
  260.   BEQ MoveBallRightDone   ;;if ballright=0, skip this section
  261.  
  262.   LDA ballx
  263.   CLC
  264.   ADC ballspeedx        ;;ballx position = ballx + ballspeedx
  265.   STA ballx
  266.  
  267.   LDA ballx
  268.   CMP #RIGHTWALL
  269.   BCC MoveBallRightDone      ;;if ball x < right wall, still on screen, skip next section
  270.   LDA #$00
  271.   STA ballright
  272.   LDA #$01
  273.   STA ballleft         ;;bounce, ball now moving left
  274.   ;;in real game, give point to player 1, reset ball
  275.   jsr IncrementScore1
  276. MoveBallRightDone:
  277.  
  278.  
  279. MoveBallLeft:
  280.   LDA ballleft
  281.   BEQ MoveBallLeftDone   ;;if ballleft=0, skip this section
  282.  
  283.   LDA ballx
  284.   SEC
  285.   SBC ballspeedx        ;;ballx position = ballx - ballspeedx
  286.   STA ballx
  287.  
  288.   LDA ballx
  289.   CMP #LEFTWALL
  290.   BCS MoveBallLeftDone      ;;if ball x > left wall, still on screen, skip next section
  291.   LDA #$01
  292.   STA ballright
  293.   LDA #$00
  294.   STA ballleft         ;;bounce, ball now moving right
  295.   ;;in real game, give point to player 2, reset ball
  296.     jsr IncrementScore2
  297. MoveBallLeftDone:
  298.  
  299.  
  300. MoveBallUp:
  301.   LDA ballup
  302.   BEQ MoveBallUpDone   ;;if ballup=0, skip this section
  303.  
  304.   LDA bally
  305.   SEC
  306.   SBC ballspeedy        ;;bally position = bally - ballspeedy
  307.   STA bally
  308.  
  309.   LDA bally
  310.   CMP #TOPWALL
  311.   BCS MoveBallUpDone      ;;if ball y > top wall, still on screen, skip next section
  312.   LDA #$01
  313.   STA balldown
  314.   LDA #$00
  315.   STA ballup         ;;bounce, ball now moving down
  316. MoveBallUpDone:
  317.  
  318.  
  319. MoveBallDown:
  320.   LDA balldown
  321.   BEQ MoveBallDownDone   ;;if ballup=0, skip this section
  322.  
  323.   LDA bally
  324.   CLC
  325.   ADC ballspeedy        ;;bally position = bally + ballspeedy
  326.   STA bally
  327.  
  328.   LDA bally
  329.   CMP #BOTTOMWALL
  330.   BCC MoveBallDownDone      ;;if ball y < bottom wall, still on screen, skip next section
  331.   LDA #$00
  332.   STA balldown
  333.   LDA #$01
  334.   STA ballup         ;;bounce, ball now moving down
  335. MoveBallDownDone:
  336.   JMP GameEngineDone
  337.  
  338. MovePaddle1:
  339.  
  340.     lda buttons1        ;load the current button state for controller1
  341.     and #%00000100   ;isolate the bit representing "up", by clearing all the other bits
  342.     beq ReadDownDone          ;if the result was 0, then the "up" bit was clear (thus not pressed), so skip the following code
  343.     ;...
  344.  
  345.   LDA $0204 ; load sprite 2 position
  346.   CLC ; make sure carry flag is set
  347.   ADC #PS1 ; A = A - 1
  348.   STA $0204 ; save sprite 2 position
  349.   LDA $0204
  350.   STA paddle1y
  351.   LDA $020C ; load sprite 2 position
  352.   CLC ; make sure carry flag is set
  353.   ADC #PS1 ; A = A - 1
  354.   STA $020C ; save sprite 2 position
  355.   LDA $020C
  356.   STA paddle1y2
  357.   LDA paddle1y2
  358.   CMP #BOTTOMWALL
  359.   BCC ReadDownDone
  360.   SEC  ; make sure carry flag is set
  361.   SBC #$01 ; A = A - 1
  362.   STA paddle1y2
  363.   LDA paddle1y
  364.   SEC  ; make sure carry flag is set
  365.   SBC #$01 ; A = A - 1
  366.   STA paddle1y  
  367.  
  368. ;  LDA paddle1y
  369. ;  SEC
  370. ;  SBC PS1        ;;bally position = bally - ballspeedy
  371. ;  STA paddle1y
  372. ;
  373. ;  LDA paddle1y
  374. ;  CMP #TOPWALL
  375. ;  BCS ReadDownDone      ;;if ball y > top wall, still on screen, skip next section
  376. ;  LDA #$01
  377. ;  STA paddle1down
  378. ;  LDA #$00
  379. ;  STA paddle1down         ;;bounce, ball now moving down  
  380.  
  381. ReadDownDone:
  382.  
  383.     lda buttons1        ;load the current button state for controller1
  384.     and #%00001000   ;isolate the bit representing "up", by clearing all the other bits
  385.     beq ReadUpDone           ;if the result was 0, then the "up" bit was clear (thus not pressed), so skip the following code
  386.     ;...
  387.  
  388. ;  LDA $4016    
  389. ;  CMP #%00001000
  390. ;  BNE ReadUpDone
  391.   LDA $0204 ; load sprite 2 position
  392.   SEC ; make sure carry flag is set
  393.   SBC #PS1 ; A = A - 1
  394.   STA $0204 ; save sprite 2 position
  395.   LDA $0204
  396.   STA paddle1y
  397.   LDA $020C ; load sprite 2 position
  398.   SEC ; make sure carry flag is set
  399.   SBC #PS1 ; A = A - 1
  400.   STA $020C ; save sprite 2 position  
  401.   LDA $020C
  402.   STA paddle1y2
  403.   LDA paddle1y
  404.   CMP #TOPWALL
  405.   BCS ReadUpDone
  406.   CLC ; make sure carry flag is set
  407.   ADC #$01 ; A = A - 1
  408.   STA paddle1y
  409.   LDA paddle1y2  
  410.   CLC ; make sure carry flag is set
  411.   ADC #$01 ; A = A - 1
  412.   STA paddle1y2    
  413. ;  LDA paddle1y
  414. ;  SEC
  415. ;  SBC PS1        ;;bally position = bally - ballspeedy
  416. ;  STA paddle1y
  417. ;
  418. ;  LDA paddle1y
  419. ;  CMP #TOPWALL
  420. ;  BCS ReadUpDone      ;;if ball y > top wall, still on screen, skip next section
  421. ;  LDA #$01
  422. ;  STA paddle1down
  423. ;  LDA #$00
  424. ;  STA paddle1up         ;;bounce, ball now moving down
  425. ReadUpDone:    
  426.   RTS
  427.  
  428. MovePaddle2:
  429.  
  430.     lda buttons2        ;load the current button state for controller1
  431.     and #%00000100   ;isolate the bit representing "up", by clearing all the other bits
  432.     beq MovePaddleDownDone           ;if the result was 0, then the "up" bit was clear (thus not pressed), so skip the following code
  433.     ;...
  434.   LDA $0208 ; load sprite 2 position
  435.   CLC ; make sure carry flag is set
  436.   ADC #PS2 ; A = A - 1
  437.   STA $0208 ; save sprite 2 position
  438.   LDA $0208
  439.   STA paddle2y
  440.   LDA $0210 ; load sprite 2 position
  441.   CLC ; make sure carry flag is set
  442.   ADC #PS2 ; A = A - 1
  443.   STA $0210 ; save sprite 2 position
  444.   LDA $0210
  445.   STA paddle2y2  
  446.   LDA paddle2y2
  447.   CMP #BOTTOMWALL
  448.   BCC MovePaddleDownDone
  449.   SEC  ; make sure carry flag is set
  450.   SBC #$01 ; A = A - 1
  451.   STA paddle2y2
  452.   LDA paddle2y
  453.   SEC  ; make sure carry flag is set
  454.   SBC #$01 ; A = A - 1
  455.   STA paddle2y  
  456.  
  457. MovePaddleDownDone:
  458.  
  459.     lda buttons2        ;load the current button state for controller1
  460.     and #%00001000   ;isolate the bit representing "up", by clearing all the other bits
  461.     beq MovePaddleUpDone           ;if the result was 0, then the "up" bit was clear (thus not pressed), so skip the following code
  462.     ;...
  463.   LDA $0208 ; load sprite 2 position
  464.   SEC ; make sure carry flag is set
  465.   SBC #PS2 ; A = A - 1
  466.   STA $0208 ; save sprite 2 position
  467.   LDA $0208
  468.   STA paddle2y
  469.   LDA $0210 ; load sprite 2 position
  470.   SEC ; make sure carry flag is set
  471.   SBC #PS2 ; A = A - 1
  472.   STA $0210 ; save sprite 2 position
  473.   LDA $0210
  474.   STA paddle2y2  
  475.   LDA paddle2y
  476.   CMP #TOPWALL
  477.   BCS MovePaddleUpDone
  478.   CLC ; make sure carry flag is set
  479.   ADC #$01 ; A = A - 1
  480.   STA paddle2y
  481.   LDA paddle2y2
  482.   CLC ; make sure carry flag is set
  483.   ADC #$01 ; A = A - 1
  484.   STA paddle2y2  
  485.  
  486.   ;;if up button pressed
  487.   ;;  if paddle top > top wall
  488.   ;;    move paddle top and bottom up
  489. MovePaddleUpDone:
  490.   RTS
  491.  
  492.  
  493.  
  494. Skip:
  495.  
  496. CheckPaddleCollision:
  497.   LDA ballx
  498.   SEC
  499.   SBC #$08  
  500.   CMP #PADDLE1X
  501.   BCS CheckPaddleCollisionDone      ;;if ball x > left wall, still on screen, skip next section  <--BCC changed to BCS    
  502.   LDA bally  
  503.   CLC
  504.   ADC #$08
  505.   CMP paddle1y
  506.   BCC CheckPaddleCollisionDone      ;;if ball y > top wall, still on screen, skip next section <--BCS changed to BCC  
  507.   LDA bally
  508.  
  509.   sec                              ;    <-- Fixes the width of the paddle so the ball collides with the whole thing
  510.   sbc #$08                      ;    <-- otherwise it would pass through the bottom half (effectively the same as adding 8 to paddle1y2)
  511. ;  STA bally
  512.  
  513.   CMP paddle1y2
  514.   BCS CheckPaddleCollisionDone      ;;if ball y < bottom wall, still on screen, skip next section  
  515.   LDA #$01
  516.   STA ballright  
  517.   LDA #$00
  518.   STA ballleft         ;;bounce, ball now moving right
  519.   JMP CheckPaddleCollisionDone
  520. CheckPaddleCollisionDone:
  521.   RTS
  522. ;  JMP GameEngineDone
  523.  
  524. CheckPaddleCollision1:
  525.   LDA ballx
  526.   CLC
  527.   ADC #$08  
  528.   CMP #PADDLE2X
  529.   BCC CheckPaddleCollisionDone1      ;;if ball x > left wall, still on screen, skip next section  <--BCC changed to BCS    
  530.   LDA bally  
  531.   CLC
  532.   ADC #$08
  533.   CMP paddle2y
  534.   BCC CheckPaddleCollisionDone1      ;;if ball y > top wall, still on screen, skip next section <--BCS changed to BCC  
  535.   LDA bally
  536.  
  537.   sec                              ;    <-- Fixes the width of the paddle so the ball collides with the whole thing
  538.   sbc #$08                      ;    <-- otherwise it would pass through the bottom half (effectively the same as adding 8 to paddle1y2)
  539. ;  STA bally
  540.  
  541.   CMP paddle2y2
  542.   BCS CheckPaddleCollisionDone1      ;;if ball y < bottom wall, still on screen, skip next section  
  543.   LDA #$00
  544.   STA ballright  
  545.   LDA #$01
  546.   STA ballleft         ;;bounce, ball now moving right
  547.   JMP CheckPaddleCollisionDone1
  548. CheckPaddleCollisionDone1:
  549.   RTS
  550.  
  551.  
  552. UpdateSprites:
  553.   LDA bally  ;;update all ball sprite info
  554.   STA $0200
  555.  
  556.   LDA #$30
  557.   STA $0201
  558.  
  559.   LDA #$00
  560.   STA $0202
  561.  
  562.   LDA ballx
  563.   STA $0203
  564.  
  565.   LDA paddle1y  ;;update all ball sprite info
  566.   STA $0204
  567.  
  568.   LDA #$7F
  569.   STA $0205
  570.  
  571.   LDA #$10
  572.   STA $0206
  573.  
  574.   LDA #PADDLE1X
  575.   STA $0207
  576.  
  577.   LDA paddle1y2  ;;update all ball sprite info
  578.   STA $020C
  579.  
  580.   LDA #$7F
  581.   STA $020D
  582.  
  583.   LDA #$10
  584.   STA $020E
  585.  
  586.   LDA #PADDLE1X
  587.   STA $020F
  588.  
  589.   LDA paddle2y
  590.   STA $0208
  591.  
  592.   LDA #$7F
  593.   STA $0209
  594.  
  595.   LDA #$10
  596.   STA $020A
  597.  
  598.   LDA #PADDLE2X
  599.   STA $020B
  600.  
  601.   LDA paddle2y2
  602.   STA $0210
  603.  
  604.   LDA #$7F
  605.   STA $0211
  606.  
  607.   LDA #$10
  608.   STA $0212
  609.  
  610.   LDA #PADDLE2X
  611.   STA $0213  
  612.  
  613.   ;;update paddle sprites
  614.   RTS
  615.  
  616.  
  617. DrawScore:
  618.   LDA $2002
  619.   LDA #$20
  620.   STA $2006
  621.   LDA #$20
  622.   STA $2006          ; start drawing the score at PPU $2020
  623.  
  624. .Zero1  
  625.   LDA scoreHundreds      ; last digit  
  626.   cmp #$00
  627.   beq .pointa
  628.   cmp #$24
  629.   bne .point
  630.   LDA scoreTens      ; next digit
  631.   cmp #$00
  632.   bne .point
  633.   LDA #$24
  634.   STA scoreTens
  635.   JMP .point
  636. .pointa
  637.   LDA #$24
  638.   STA scoreHundreds
  639.  
  640. .point  
  641.   LDA scoreHundreds  ; get first digit
  642. ;  CLC
  643. ;  ADC #$30           ; add ascii offset  (this is UNUSED because the tiles for digits start at 0)
  644.   STA $2007          ; draw to background
  645.   LDA scoreTens      ; next digit
  646. ;  CLC
  647. ;  ADC #$30           ; add ascii offset
  648.   STA $2007
  649.   LDA scoreOnes      ; last digit
  650. ;  CLC
  651. ;  ADC #$30           ; add ascii offset
  652.   STA $2007
  653.   RTS
  654.  
  655. DrawScore2:
  656.   LDA $2002
  657.   LDA #$20
  658.   STA $2006
  659.   LDA #$3C
  660.   STA $2006          ; start drawing the score at PPU $203C
  661.  
  662. .Zero  
  663.   LDA scoreHundreds2      ; last digit  
  664.   cmp #$00
  665.   beq .point2b
  666.   cmp #$24
  667.   bne .point2
  668.   LDA scoreTens2      ; next digit
  669.   cmp #$00
  670.   bne .point2
  671.   LDA #$24
  672.   STA scoreTens2
  673.   JMP .point2
  674. .point2b
  675.   LDA #$24
  676.   STA scoreHundreds2
  677.  
  678. .point2
  679.   LDA scoreHundreds2  ; get first digit  
  680. ;  CLC
  681. ;  ADC #$30           ; add ascii offset  (this is UNUSED because the tiles for digits start at 0)
  682.   STA $2007          ; draw to background
  683.   LDA scoreTens2      ; next digit
  684. ;  CLC
  685. ;  ADC #$30           ; add ascii offset
  686.   STA $2007
  687.   LDA scoreOnes2      ; last digit
  688. ;  CLC
  689. ;  ADC #$30           ; add ascii offset
  690.   STA $2007
  691.   RTS
  692.  
  693.  
  694. IncrementScore1:
  695. IncOnes:
  696.   LDA scoreOnes      ; load the lowest digit of the number
  697.   CLC
  698.   ADC #$01           ; add one
  699.   STA scoreOnes
  700.   CMP #$0A           ; check if it overflowed, now equals 10
  701.   bne IncDone        ; if there was no overflow, all done
  702. IncTens:
  703.   LDA #$00
  704.   STA scoreOnes      ; wrap digit to 0
  705.   LDA scoreTens      ; load the next digit
  706.   cmp #$24
  707.   bne .add
  708.   sec
  709.   sbc #$24
  710.   STA scoreTens
  711. .add  
  712.   LDA scoreTens      ; load the next digit
  713.   CLC
  714.   ADC #$01           ; add one, the carry from previous digit
  715.   STA scoreTens
  716.   CMP #$0A           ; check if it overflowed, now equals 10
  717.   bne IncDone        ; if there was no overflow, all done
  718. IncHundreds:
  719.   LDA #$00
  720.   STA scoreTens      ; wrap digit to 0
  721.   LDA scoreHundreds  ; load the next digit
  722.   cmp #$24
  723.   bne .add1
  724.   sec
  725.   sbc #$24
  726.   STA scoreHundreds
  727. .add1  
  728.   LDA scoreHundreds  ; load the next digit
  729.   CLC
  730.   ADC #$01           ; add one, the carry from previous digit
  731.   STA scoreHundreds  
  732. IncDone:
  733.   JMP IncDone2
  734.  
  735. IncrementScore2:
  736. IncOnes2:
  737.   LDA scoreOnes2      ; load the lowest digit of the number
  738.   CLC
  739.   ADC #$01           ; add one
  740.   STA scoreOnes2
  741.   CMP #$0A           ; check if it overflowed, now equals 10
  742.   BNE IncDone2        ; if there was no overflow, all done
  743. IncTens2:
  744.   LDA #$00
  745.   STA scoreOnes2      ; wrap digit to 0
  746.   LDA scoreTens2      ; load the next digit
  747.   cmp #$24
  748.   bne .add
  749.   sec
  750.   sbc #$24
  751.   STA scoreTens2
  752. .add  
  753.   LDA scoreTens2      ; load the next digit
  754.   CLC
  755.   ADC #$01           ; add one, the carry from previous digit
  756.   STA scoreTens2
  757.   CMP #$0A           ; check if it overflowed, now equals 10
  758.   BNE IncDone2        ; if there was no overflow, all done
  759. IncHundreds2:
  760.   LDA #$00
  761.   STA scoreTens2      ; wrap digit to 0
  762.   LDA scoreHundreds2  ; load the next digit
  763.   cmp #$24
  764.   bne .add1
  765.   sec
  766.   sbc #$24
  767.   STA scoreHundreds2
  768. .add1  
  769.   LDA scoreHundreds2  ; load the next digit
  770.   CLC
  771.   ADC #$01           ; add one, the carry from previous digit
  772.   STA scoreHundreds2
  773. IncDone2:
  774.   LDA scoreHundreds
  775.   cmp #$01
  776.   BEQ GameOver
  777.   LDA scoreHundreds2
  778.   cmp #$01
  779.   BEQ GameOver  
  780.  
  781. ReadController1:
  782.   LDA #$01
  783.   STA $4016
  784.   LDA #$00
  785.   STA $4016
  786.   LDX #$08
  787. ReadController1Loop:
  788.   LDA $4016
  789.   LSR A            ; bit0 -> Carry
  790.   ROL buttons1     ; bit0 <- Carry
  791.   DEX
  792.   BNE ReadController1Loop
  793.   RTS
  794.  
  795. ReadController2:
  796.   LDA #$01
  797.   STA $4016
  798.   LDA #$00
  799.   STA $4016
  800.   LDX #$08
  801. ReadController2Loop:
  802.   LDA $4017
  803.   LSR A            ; bit0 -> Carry
  804.   ROL buttons2     ; bit0 <- Carry
  805.   DEX
  806.   BNE ReadController2Loop
  807.   RTS  
  808.  
  809. GameOver:
  810.         LDA #STATEGAMEOVER
  811.         STA gamestate
  812.         JMP GameEngine
  813.  
  814.    
  815.        
  816. ;;;;;;;;;;;;;;  
  817.  
  818.  
  819.  
  820.   .bank 1
  821.   .org $E000
  822. palette:
  823.   .db $22,$29,$1A,$0F,  $22,$36,$17,$0F,  $22,$30,$21,$0F,  $22,$27,$17,$0F   ;;background palette
  824.   .db $22,$1C,$15,$14,  $22,$02,$38,$3C,  $22,$1C,$15,$14,  $22,$02,$38,$3C   ;;sprite palette
  825.  
  826. sprites:
  827.      ;vert tile attr horiz
  828.         .db $52, $01, $00, $28 ; Player1 paddle top
  829.         .db $5A, $11, $00, $28 ; Player1 paddle bottom
  830.        
  831.         .db $52, $01, $01, $D8 ; Player2 paddle top
  832.         .db $5A, $11, $01, $D8 ; Player2 paddle bottom
  833.        
  834.         .db $74, $02, $02, $78 ; Ball
  835.  
  836.  
  837.  
  838.   .org $FFFA     ;first of the three vectors starts here
  839.   .dw NMI        ;when an NMI happens (once per frame if enabled) the
  840.                    ;processor will jump to the label NMI:
  841.   .dw RESET      ;when the processor first turns on or is reset, it will jump
  842.                    ;to the label RESET:
  843.   .dw 0          ;external interrupt IRQ is not used in this tutorial
  844.  
  845.  
  846. ;;;;;;;;;;;;;;  
  847.  
  848.  
  849.   .bank 2
  850.   .org $0000
  851.   .incbin "mario.chr"   ;includes 8KB graphics file from SMB1
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement