Advertisement
Guest User

ConwayLogic.asm

a guest
Oct 7th, 2015
207
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
ARM 3.28 KB | None | 0 0
  1. ResetBoard:
  2.     ld hl,Board
  3.     ld de,Board+1
  4.     ld bc,BoardSize-1
  5.     ld a,OffCellData
  6.     ld (hl),a
  7.     ldir
  8.     ; ld de,BgColor
  9.     ; call ChangeRectColor
  10.     ld de,0 ; X
  11.     ld l,0 ; Y
  12.     ld bc,320 ; Width
  13.     ld h,240 ; Height
  14.     ld a,BgColor
  15.     call FillRect
  16.     call RenderBoard
  17.     ld bc,(Rows/2)*256+Cols/2
  18.     ld.sis (Cursor-M),bc
  19.     call DrawCursor
  20.     xor a ; Prevent accidental "keypresses"
  21.     ret
  22.  
  23. ResetBoard2:
  24.     ld hl,NewBoard
  25.     ld de,NewBoard+1
  26.     ld bc,BoardSize-1
  27.     xor a
  28.     ld (hl),a
  29.     ldir
  30.     ret
  31.  
  32. LdBoard:
  33.     ; DE = mem loc of board to load
  34.     ; DE and HL are sorta switched; DE is the source and HL is the destination
  35.     ld hl,Board
  36.     ld bc,BoardSize/8
  37. LdBoard_Loop:
  38.     push bc
  39.     ld a,(de)
  40.     bit 0,a
  41.     call z,LdBoard_On
  42.     inc hl
  43.     bit 1,a
  44.     call z,LdBoard_On
  45.     inc hl
  46.     bit 2,a
  47.     call z,LdBoard_On
  48.     inc hl
  49.     bit 3,a
  50.     call z,LdBoard_On
  51.     inc hl
  52.     bit 4,a
  53.     call z,LdBoard_On
  54.     inc hl
  55.     bit 5,a
  56.     call z,LdBoard_On
  57.     inc hl
  58.     bit 6,a
  59.     call z,LdBoard_On
  60.     inc hl
  61.     bit 7,a
  62.     call z,LdBoard_On
  63.     inc hl
  64.     inc de
  65.     pop bc
  66.     dec bc
  67.     jp nz,LdBoard_Loop
  68.     jp RenderBoard
  69. LdBoard_On:
  70.     ld (hl),OnCellData
  71.     ret
  72. LdBoard_Off:
  73.     ld (hl),OffCellData
  74.     ret
  75.  
  76. SpreadToAdj:
  77.     call SafeDecX
  78.     call IncCell
  79.     call SafeDecY
  80.     call IncCell
  81.     call SafeIncX
  82.     call IncCell
  83.     call SafeIncX
  84.     call IncCell
  85.     call SafeIncY
  86.     call IncCell
  87.     call SafeIncY
  88.     call IncCell
  89.     call SafeDecX
  90.     call IncCell
  91.     call SafeDecX
  92.     jp IncCell
  93.  
  94. IncCell:
  95.     call GetCell
  96.     ld de,NewBoard-Board
  97.     add hl,de
  98.     inc (hl)
  99.     ret
  100.  
  101. ConwayCell: ; Evaluates a cell at x=b, y=c and redraws (must call GetCell first); destroys AF, BC
  102.     push hl
  103.     ld de,NewBoard-Board
  104.     add hl,de
  105.     ld a,(hl)
  106.     pop hl
  107.     cp 3 ; Compare A (neighboring cells) to 3
  108.     jp z,CellOn ; If A=3, CellOn
  109.     jp nc,CellOff ; ElseIf A >= 3, CellOff (A must be >3, because it can't equal 3)
  110.     cp 2 ; Compare A to 2
  111.     jp c,CellOff ; If A<2, CellOff
  112.     ret ; If A=2, keep current state (on or off)
  113.  
  114. CellOn:
  115.     ld a,(hl)
  116.     cp OnCellData
  117.     ret z
  118.     ld a,OnCellData
  119.     ld (hl),a
  120.     ld a,OnCellColor
  121.     jp DrawCell
  122. CellOff:
  123.     ld a,(hl)
  124.     cp OffCellData
  125.     ret z
  126.     ld a,OffCellData
  127.     ld (hl),a
  128.     ld a,OffCellColor
  129.     jp DrawCell
  130.  
  131. GetCell: ; Stores the memory location of the cell at x=b, y=c into HL; destroys HL, DE
  132.     push bc
  133.     dec b
  134.     dec c
  135.     or a ; maybe not needed?
  136.     sbc hl,hl
  137.     ld l,c
  138.     add hl,hl ; x2
  139.     add hl,hl ; x4
  140.     add hl,hl ; x8
  141.     push hl
  142.     pop de
  143.     add hl,hl ; x16
  144.     add hl,hl ; x32
  145.     add hl,de ; x40
  146.     ld de,0
  147.     ld e,b
  148.     add hl,de
  149.     ld de,Board
  150.     add hl,de
  151.     pop bc
  152.     ret
  153.  
  154. ; *
  155. ; * MATH STUFF
  156. ; *
  157.  
  158. AMult8: ; Multiplies A by 8
  159.     sla a ; x2
  160.     sla a ; x4
  161.     sla a ; x8
  162.     ret
  163. HLMult8: ; Multiplies HL by 8
  164.     add hl,hl ; x2
  165.     add hl,hl ; x4
  166.     add hl,hl ; x8
  167.     ret
  168. HLMult10: ; Multiplies HL by 10
  169.     add hl,hl ; x2
  170.     push hl
  171.     pop de
  172.     add hl,hl ; x4
  173.     add hl,hl ; x8
  174.     add hl,de ; x10
  175.     ret
  176. ; AMult5: ; Multiplies A by 5
  177.     ; push bc
  178.     ; ld b,a
  179.     ; sla a
  180.     ; sla a
  181.     ; add a,b
  182.     ; pop bc
  183.     ; ret
  184. ; HLMult5: ; Multiplies HL by 5
  185.     ; push bc
  186.     ; ld b,h
  187.     ; ld c,l
  188.     ; add hl,hl
  189.     ; add hl,hl
  190.     ; add hl,bc
  191.     ; pop bc
  192.     ; ret
  193.  
  194. SafeIncX:
  195.     ld a,b
  196.     inc a
  197.     cp Cols+1
  198.     jp nc,Min_X
  199.     ld b,a
  200.     ret
  201. Min_X:
  202.     ld b,1
  203.     ret
  204.  
  205. SafeIncY:
  206.     ld a,c
  207.     inc a
  208.     cp Rows+1
  209.     jp nc,Min_Y
  210.     ld c,a
  211.     ret
  212. Min_Y:
  213.     ld c,1
  214.     ret
  215.  
  216. SafeDecX:
  217.     dec b
  218.     jp z,Max_X
  219.     ret
  220. Max_X:
  221.     ld b,Cols
  222.     ret
  223.  
  224. SafeDecY:
  225.     dec c
  226.     jp z,Max_Y
  227.     ret
  228. Max_Y:
  229.     ld c,Rows
  230.     ret
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement