Advertisement
NovaYoshi

6502 Life

Jul 18th, 2013
192
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. TOP_LEFT  = %0001
  2. TOP_RIGHT = %0010
  3. BOT_LEFT  = %0100
  4. BOT_RIGHT = %1000
  5.  
  6. .code
  7. .proc MainLoop
  8. : jsr UpdateGrid
  9.   lda IsPaused
  10.   beq :-
  11.   lda #0
  12.   sta KeyRepeat
  13. EditLoop:
  14.   lda keydown
  15.   and #KEY_LEFT|KEY_DOWN|KEY_UP|KEY_RIGHT
  16.   sta Temp
  17.   lda keylast
  18.   and #KEY_LEFT|KEY_DOWN|KEY_UP|KEY_RIGHT
  19.   cmp Temp
  20.   bne :+
  21.     lda KeyRepeat
  22.     cmp #16
  23.     bcc NoRepeat
  24.     lda retraces
  25.     and #3
  26.     bne NoRepeat
  27.     lda keylast
  28.     and #~(KEY_LEFT|KEY_DOWN|KEY_UP|KEY_RIGHT)
  29.     sta keylast
  30.   NoRepeat:
  31.  
  32.     lda KeyRepeat
  33.     bmi DidRepeat
  34.     inc KeyRepeat
  35.     jmp DidRepeat
  36. : lda #0
  37.   sta KeyRepeat
  38. DidRepeat:
  39.  
  40.   lda keydown
  41.   and #KEY_LEFT
  42.   beq :+
  43.     lda keylast
  44.     and #KEY_LEFT
  45.     bne :+
  46.       dec PlayerX
  47.   :
  48.   lda keydown
  49.   and #KEY_DOWN
  50.   beq :+
  51.     lda keylast
  52.     and #KEY_DOWN
  53.     bne :+
  54.       inc PlayerY
  55.   :
  56.   lda keydown
  57.   and #KEY_UP
  58.   beq :+
  59.     lda keylast
  60.     and #KEY_UP
  61.     bne :+
  62.       dec PlayerY
  63.   :
  64.   lda keydown
  65.   and #KEY_RIGHT
  66.   beq :+
  67.     lda keylast
  68.     and #KEY_RIGHT
  69.     bne :+
  70.       inc PlayerX
  71.   :
  72.  
  73.   lda PlayerX
  74.   and #31
  75.   sta PlayerX
  76.  
  77.   lda PlayerY
  78.   and #31
  79.   sta PlayerY
  80.  
  81.  
  82.   lda keylast
  83.   eor #255
  84.   and keydown
  85.   and #KEY_UP|KEY_LEFT|KEY_DOWN|KEY_RIGHT
  86.   sta Temp
  87.  
  88.   lda keydown
  89.   and #KEY_A
  90.   beq :+
  91.     lda keylast
  92.     and #KEY_A
  93.     eor #KEY_A
  94.     ora Temp
  95.     beq :+
  96.       lda PlayerX
  97.       sta Temp+0
  98.       lda PlayerY
  99.       sta Temp+1
  100.       jsr ToggleCell
  101.       lda #1
  102.       sta NeedRedrawGrid
  103.   :
  104.  
  105.   lda keydown
  106.   and #KEY_START
  107.   beq :+
  108.     lda keylast
  109.     and #KEY_START
  110.     bne :+
  111.       lda #0
  112.       sta IsPaused
  113.   :
  114.  
  115.   lda keydown
  116.   and #KEY_SELECT
  117.   beq NoClear
  118.     lda keylast
  119.     and #KEY_SELECT
  120.     bne NoClear
  121.       lda #0
  122.       tax
  123.     : sta RefGrid,x
  124.       inx
  125.       bne :-
  126.       lda #1
  127.       sta NeedRedrawGrid
  128. NoClear:
  129.  
  130.   jsr wait_vblank
  131.   lda IsPaused
  132.   jne EditLoop
  133.   jmp MainLoop
  134. .endproc
  135.  
  136. .proc ToggleCell ; Temp+0=X, Temp+1=Y
  137.   XPos = Temp+0
  138.   YPos = Temp+1  
  139.   lda XPos
  140.   lsr
  141.   ; ....xxxx
  142.   sta Temp+2
  143.   lda YPos
  144.   asl
  145.   asl
  146.   asl
  147.   and #%11110000
  148.   ora Temp+2
  149.   tax
  150.  
  151.   lda YPos
  152.   and #1
  153.   asl
  154.   sta Temp+2
  155.   lda XPos
  156.   and #1
  157.   ora Temp+2
  158.   tay
  159.  
  160.   lda RefGrid,x
  161.   and MaskOn,y
  162.   php
  163.   lda RefGrid,x
  164.   and MaskOff,y
  165.   sta RefGrid,x
  166.   plp
  167.   bne :+
  168.     ora MaskOn,y
  169.     sta RefGrid,x
  170.   :
  171.   rts
  172. MaskOn:
  173.   .byt 1, 2, 4, 8
  174. MaskOff:
  175.   .byt ~1, ~2, ~4, ~8
  176. .endproc
  177.  
  178. CellBirthTL:
  179.   .byt 0,0,0,1,0,0,0,0,0
  180. CellSurviveTL:
  181.   .byt 0,0,1,1,0,0,0,0,0
  182. CellBirthTR:
  183.   .byt 0,0,0,2,0,0,0,0,0
  184. CellSurviveTR:
  185.   .byt 0,0,2,2,0,0,0,0,0
  186. CellBirthBL:
  187.   .byt 0,0,0,4,0,0,0,0,0
  188. CellSurviveBL:
  189.   .byt 0,0,4,4,0,0,0,0,0
  190. CellBirthBR:
  191.   .byt 0,0,0,8,0,0,0,0,0
  192. CellSurviveBR:
  193.   .byt 0,0,8,8,0,0,0,0,0
  194.  
  195. .macro addneighbor Side
  196.   and #Side
  197.   beq :+
  198.     inc Neighbors
  199.   :
  200. .endmacro
  201.  
  202. .proc UpdateCellRegular
  203. ; find neighbors (top left)
  204.   lda #0
  205.   sta Neighbors
  206.   lda RefGrid-1,y
  207.   tax
  208.   addneighbor TOP_RIGHT
  209.   txa
  210.   addneighbor BOT_RIGHT
  211.   lda RefGrid,y
  212.   tax
  213.   addneighbor BOT_LEFT
  214.   txa
  215.   addneighbor BOT_RIGHT
  216.   txa
  217.   addneighbor TOP_RIGHT
  218.   tya
  219.   tax
  220.   axs #16
  221.   lda RefGrid,x
  222.   pha
  223.   addneighbor BOT_LEFT
  224.   pla
  225.   addneighbor BOT_RIGHT
  226.   lda RefGrid-1,x
  227.   addneighbor BOT_RIGHT
  228. ; use neighbor count
  229.   ldx Neighbors
  230.   lda RefGrid,y
  231.   and #TOP_LEFT
  232.   beq :+
  233.     txa
  234.     axs #-9
  235. : lda CellBirthTL,x
  236.   sta CurGrid,y
  237.  
  238. ; find neighbors (top right)
  239.   lda #0
  240.   sta Neighbors
  241.   lda RefGrid,y
  242.   tax
  243.   addneighbor TOP_LEFT
  244.   txa
  245.   addneighbor BOT_LEFT
  246.   txa
  247.   addneighbor BOT_RIGHT
  248.   lda RefGrid+1,y
  249.   tax
  250.   addneighbor TOP_LEFT
  251.   txa
  252.   addneighbor BOT_LEFT
  253.   tya
  254.   tax
  255.   axs #16
  256.   lda RefGrid,x
  257.   pha
  258.   addneighbor BOT_LEFT
  259.   pla
  260.   addneighbor BOT_RIGHT
  261.   lda RefGrid+1,x
  262.   addneighbor BOT_LEFT
  263. ; use neighbor count
  264.   ldx Neighbors
  265.   lda RefGrid,y
  266.   and #TOP_RIGHT
  267.   beq :+
  268.     txa
  269.     axs #-9
  270. : lda CellBirthTR,x
  271.   ora CurGrid,y
  272.   sta CurGrid,y
  273.  
  274. ; find neighbors (bottom left)
  275.   lda #0
  276.   sta Neighbors
  277.   lda RefGrid,y
  278.   tax
  279.   addneighbor TOP_LEFT
  280.   txa
  281.   addneighbor TOP_RIGHT
  282.   txa
  283.   addneighbor BOT_RIGHT
  284.   lda RefGrid-1,y
  285.   tax
  286.   addneighbor TOP_RIGHT
  287.   txa
  288.   addneighbor BOT_RIGHT
  289.   tya
  290.   tax
  291.   axs #-16
  292.   lda RefGrid,x
  293.   pha
  294.   addneighbor TOP_LEFT
  295.   pla
  296.   addneighbor TOP_RIGHT
  297.   lda RefGrid-1,x
  298.   addneighbor TOP_RIGHT
  299. ; use neighbor count
  300.   ldx Neighbors
  301.   lda RefGrid,y
  302.   and #BOT_LEFT
  303.   beq :+
  304.     txa
  305.     axs #-9
  306. : lda CellBirthBL,x
  307.   ora CurGrid,y
  308.   sta CurGrid,y
  309.  
  310. ; find neighbors (bottom right)
  311.   lda #0
  312.   sta Neighbors
  313.   lda RefGrid,y
  314.   tax
  315.   addneighbor TOP_LEFT
  316.   txa
  317.   addneighbor TOP_RIGHT
  318.   txa
  319.   addneighbor BOT_LEFT
  320.   lda RefGrid+1,y
  321.   tax
  322.   addneighbor TOP_LEFT
  323.   txa
  324.   addneighbor BOT_LEFT
  325.   tya
  326.   tax
  327.   axs #-16
  328.   lda RefGrid,x
  329.   pha
  330.   addneighbor TOP_LEFT
  331.   pla
  332.   addneighbor TOP_RIGHT
  333.   lda RefGrid+1,x
  334.   addneighbor TOP_LEFT
  335. ; use neighbor count
  336.   ldx Neighbors
  337.   lda RefGrid,y
  338.   and #BOT_RIGHT
  339.   beq :+
  340.     txa
  341.     axs #-9
  342. : lda CellBirthBR,x
  343.   ora CurGrid,y
  344.   sta CurGrid,y
  345.   rts
  346. .endproc
  347.  
  348. .proc UpdateCellLeftEdge
  349. ; find neighbors (top left)
  350.   lda #0
  351.   sta Neighbors
  352.   lda RefGrid+15,y
  353.   tax
  354.   addneighbor TOP_RIGHT
  355.   txa
  356.   addneighbor BOT_RIGHT
  357.   lda RefGrid,y
  358.   tax
  359.   addneighbor BOT_LEFT
  360.   txa
  361.   addneighbor BOT_RIGHT
  362.   txa
  363.   addneighbor TOP_RIGHT
  364.   tya
  365.   tax
  366.   axs #16
  367.   lda RefGrid,x
  368.   pha
  369.   addneighbor BOT_LEFT
  370.   pla
  371.   addneighbor BOT_RIGHT
  372.   lda RefGrid+15,x
  373.   addneighbor BOT_RIGHT
  374. ; use neighbor count
  375.   ldx Neighbors
  376.   lda RefGrid,y
  377.   and #TOP_LEFT
  378.   beq :+
  379.     txa
  380.     axs #-9
  381. : lda CellBirthTL,x
  382.   sta CurGrid,y
  383.  
  384. ; find neighbors (top right)
  385.   lda #0
  386.   sta Neighbors
  387.   lda RefGrid,y
  388.   tax
  389.   addneighbor TOP_LEFT
  390.   txa
  391.   addneighbor BOT_LEFT
  392.   txa
  393.   addneighbor BOT_RIGHT
  394.   lda RefGrid+1,y
  395.   tax
  396.   addneighbor TOP_LEFT
  397.   txa
  398.   addneighbor BOT_LEFT
  399.   tya
  400.   tax
  401.   axs #16
  402.   lda RefGrid,x
  403.   pha
  404.   addneighbor BOT_LEFT
  405.   pla
  406.   addneighbor BOT_RIGHT
  407.   lda RefGrid+1,x
  408.   addneighbor BOT_LEFT
  409. ; use neighbor count
  410.   ldx Neighbors
  411.   lda RefGrid,y
  412.   and #TOP_RIGHT
  413.   beq :+
  414.     txa
  415.     axs #-9
  416. : lda CellBirthTR,x
  417.   ora CurGrid,y
  418.   sta CurGrid,y
  419.  
  420. ; find neighbors (bottom left)
  421.   lda #0
  422.   sta Neighbors
  423.   lda RefGrid,y
  424.   tax
  425.   addneighbor TOP_LEFT
  426.   txa
  427.   addneighbor TOP_RIGHT
  428.   txa
  429.   addneighbor BOT_RIGHT
  430.   lda RefGrid+15,y
  431.   tax
  432.   addneighbor TOP_RIGHT
  433.   txa
  434.   addneighbor BOT_RIGHT
  435.   tya
  436.   tax
  437.   axs #-16
  438.   lda RefGrid,x
  439.   pha
  440.   addneighbor TOP_LEFT
  441.   pla
  442.   addneighbor TOP_RIGHT
  443.   lda RefGrid+15,x
  444.   addneighbor TOP_RIGHT
  445. ; use neighbor count
  446.   ldx Neighbors
  447.   lda RefGrid,y
  448.   and #BOT_LEFT
  449.   beq :+
  450.     txa
  451.     axs #-9
  452. : lda CellBirthBL,x
  453.   ora CurGrid,y
  454.   sta CurGrid,y
  455.  
  456. ; find neighbors (bottom right)
  457.   lda #0
  458.   sta Neighbors
  459.   lda RefGrid,y
  460.   tax
  461.   addneighbor TOP_LEFT
  462.   txa
  463.   addneighbor TOP_RIGHT
  464.   txa
  465.   addneighbor BOT_LEFT
  466.   lda RefGrid+1,y
  467.   tax
  468.   addneighbor TOP_LEFT
  469.   txa
  470.   addneighbor BOT_LEFT
  471.   tya
  472.   tax
  473.   axs #-16
  474.   lda RefGrid,x
  475.   pha
  476.   addneighbor TOP_LEFT
  477.   pla
  478.   addneighbor TOP_RIGHT
  479.   lda RefGrid+1,x
  480.   addneighbor TOP_LEFT
  481. ; use neighbor count
  482.   ldx Neighbors
  483.   lda RefGrid,y
  484.   and #BOT_RIGHT
  485.   beq :+
  486.     txa
  487.     axs #-9
  488. : lda CellBirthBR,x
  489.   ora CurGrid,y
  490.   sta CurGrid,y
  491.   rts
  492. .endproc
  493.  
  494. .proc UpdateCellRightEdge
  495. ; find neighbors (top left)
  496.   lda #0
  497.   sta Neighbors
  498.   lda RefGrid-1,y
  499.   tax
  500.   addneighbor TOP_RIGHT
  501.   txa
  502.   addneighbor BOT_RIGHT
  503.   lda RefGrid,y
  504.   tax
  505.   addneighbor BOT_LEFT
  506.   txa
  507.   addneighbor BOT_RIGHT
  508.   txa
  509.   addneighbor TOP_RIGHT
  510.   tya
  511.   tax
  512.   axs #16
  513.   lda RefGrid,x
  514.   pha
  515.   addneighbor BOT_LEFT
  516.   pla
  517.   addneighbor BOT_RIGHT
  518.   lda RefGrid-1,x
  519.   addneighbor BOT_RIGHT
  520. ; use neighbor count
  521.   ldx Neighbors
  522.   lda RefGrid,y
  523.   and #TOP_LEFT
  524.   beq :+
  525.     txa
  526.     axs #-9
  527. : lda CellBirthTL,x
  528.   sta CurGrid,y
  529.  
  530. ; find neighbors (top right)
  531.   lda #0
  532.   sta Neighbors
  533.   lda RefGrid,y
  534.   tax
  535.   addneighbor TOP_LEFT
  536.   txa
  537.   addneighbor BOT_LEFT
  538.   txa
  539.   addneighbor BOT_RIGHT
  540.   lda RefGrid-15,y
  541.   tax
  542.   addneighbor TOP_LEFT
  543.   txa
  544.   addneighbor BOT_LEFT
  545.   tya
  546.   tax
  547.   axs #16
  548.   lda RefGrid,x
  549.   pha
  550.   addneighbor BOT_LEFT
  551.   pla
  552.   addneighbor BOT_RIGHT
  553.   lda RefGrid-15,x
  554.   addneighbor BOT_LEFT
  555. ; use neighbor count
  556.   ldx Neighbors
  557.   lda RefGrid,y
  558.   and #TOP_RIGHT
  559.   beq :+
  560.     txa
  561.     axs #-9
  562. : lda CellBirthTR,x
  563.   ora CurGrid,y
  564.   sta CurGrid,y
  565.  
  566. ; find neighbors (bottom left)
  567.   lda #0
  568.   sta Neighbors
  569.   lda RefGrid,y
  570.   tax
  571.   addneighbor TOP_LEFT
  572.   txa
  573.   addneighbor TOP_RIGHT
  574.   txa
  575.   addneighbor BOT_RIGHT
  576.   lda RefGrid-1,y
  577.   tax
  578.   addneighbor TOP_RIGHT
  579.   txa
  580.   addneighbor BOT_RIGHT
  581.   tya
  582.   tax
  583.   axs #-16
  584.   lda RefGrid,x
  585.   pha
  586.   addneighbor TOP_LEFT
  587.   pla
  588.   addneighbor TOP_RIGHT
  589.   lda RefGrid-1,x
  590.   addneighbor TOP_RIGHT
  591. ; use neighbor count
  592.   ldx Neighbors
  593.   lda RefGrid,y
  594.   and #BOT_LEFT
  595.   beq :+
  596.     txa
  597.     axs #-9
  598. : lda CellBirthBL,x
  599.   ora CurGrid,y
  600.   sta CurGrid,y
  601.  
  602. ; find neighbors (bottom right)
  603.   lda #0
  604.   sta Neighbors
  605.   lda RefGrid,y
  606.   tax
  607.   addneighbor TOP_LEFT
  608.   txa
  609.   addneighbor TOP_RIGHT
  610.   txa
  611.   addneighbor BOT_LEFT
  612.   lda RefGrid-15,y
  613.   tax
  614.   addneighbor TOP_LEFT
  615.   txa
  616.   addneighbor BOT_LEFT
  617.   tya
  618.   tax
  619.   axs #-16
  620.   lda RefGrid,x
  621.   pha
  622.   addneighbor TOP_LEFT
  623.   pla
  624.   addneighbor TOP_RIGHT
  625.   lda RefGrid-15,x
  626.   addneighbor TOP_LEFT
  627. ; use neighbor count
  628.   ldx Neighbors
  629.   lda RefGrid,y
  630.   and #BOT_RIGHT
  631.   beq :+
  632.     txa
  633.     axs #-9
  634. : lda CellBirthBR,x
  635.   ora CurGrid,y
  636.   sta CurGrid,y
  637.   rts
  638. .endproc
  639.  
  640. .proc UpdateGrid
  641.   lda #0
  642.   sta RowCount
  643.  
  644.   ldy #0
  645. RowLoop:
  646.   jsr UpdateCellLeftEdge
  647.   iny
  648.   .repeat 14
  649.     jsr UpdateCellRegular
  650.     iny
  651.   .endrep
  652.   jsr UpdateCellRightEdge
  653.   iny
  654.   inc RowCount
  655.   lda RowCount
  656.   cmp #16
  657.   bne RowLoop
  658.  
  659. ; copy new grid over old one
  660.   ldx #0
  661. : lda CurGrid,x
  662.   sta RefGrid,x
  663.   inx
  664.   bne :-
  665.   lda #1
  666.   sta NeedRedrawGrid
  667.   rts
  668. .endproc
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement