Guest User

Untitled

a guest
Sep 24th, 2018
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.64 KB | None | 0 0
  1. ; vim:ft=asm
  2.  
  3. ; X : width
  4. ; Y : height
  5. ; C : current cell mask (01b or 10b)
  6. ; 0x1000 ... : cell states
  7.  
  8. ; Set width/height
  9. set X, 0x10 ; width
  10. set Y, 0x08 ; height
  11.  
  12. set C, 0x1 ; 01b (curr cell state mask)
  13.  
  14. ; Prepare cells (sets up a glider in top-left corner)
  15. set A, 0x01 ; x-coord
  16. set B, 0x00 ; y-coord
  17. jsr toggle
  18.  
  19. set A, 0x02
  20. set B, 0x01
  21. jsr toggle
  22.  
  23. set A, 0x00
  24. set B, 0x02
  25. jsr toggle
  26.  
  27. set A, 0x01
  28. set B, 0x02
  29. jsr toggle
  30.  
  31. set A, 0x02
  32. set B, 0x02
  33. jsr toggle
  34.  
  35. :mainloop jsr output
  36. jsr tick
  37. set PC, mainloop
  38.  
  39.  
  40. ; Toggles the given cell. NOTE: only safe before starting
  41. :toggle;(x, y)
  42. mul B, X
  43. add A, B
  44. add A, 0x1000
  45.  
  46. xor [A], 0x1 ; toggle it!
  47. set PC, POP
  48.  
  49.  
  50.  
  51. ; Uses the observation that the GoL rules can be summarized as `(t | n) == 3`,
  52. ; where `t` is the state of the current cell (1 or 0) and `n` is the sum of the
  53. ; states of the neighbours (i.e. the number of living neighbours).
  54. ; In each cell memory slot, two statse are stored (in bit 0 and bit 1); the
  55. ; `C` register keeps track of a bitmask of which state is the "current" one.
  56.  
  57. ; Advance a generation (in-memory)
  58. :tick
  59. set I, Y ; let I be loop counter
  60. mul I, X
  61.  
  62. :loop ife I, 0 ; loop until I=0
  63. set PC, loopend
  64.  
  65. add I, 0xfff ; (Temporarily)
  66.  
  67. set A, [1+I] ; set A to value at cell (t)
  68. and A, C
  69.  
  70. ; Set B to (sum of neighbours << (C - 1))
  71. set B, [I]
  72. and B, C
  73.  
  74. set J, [2+I]
  75. and J, C
  76. add B, J
  77.  
  78. sub I, X
  79.  
  80. set J, [I]
  81. and J, C
  82. add B, J
  83.  
  84. set J, [1+I]
  85. and J, C
  86. add B, J
  87.  
  88. set J, [2+I]
  89. and J, C
  90. add B, J
  91.  
  92. add I, X
  93. add I, X
  94.  
  95. set J, [I]
  96. and J, C
  97. add B, J
  98.  
  99. set J, [1+I]
  100. and J, C
  101. add B, J
  102.  
  103. set J, [2+I]
  104. and J, C
  105. add B, J
  106.  
  107. ; Prepare stuff
  108. sub C, 1
  109. shr B, C
  110. shr A, C
  111.  
  112. ; Set B to (t | n)
  113. bor B, A
  114.  
  115. ; Set the new state
  116. ifn B, 3
  117. set B, 0
  118.  
  119. and B, 0x2 ; B to {00b, 10b} >> (c-1)
  120. shr B, C
  121.  
  122. add C, 1
  123.  
  124. sub I, X
  125. and [1+I], C
  126. xor [1+I], B
  127.  
  128. sub I, 0x1000 ; Restore I again, also decrement by 1
  129.  
  130. ; Next iteration
  131. set PC, loop
  132.  
  133. :loopend ; clean up & return
  134. xor C, 0x3
  135. set PC, POP
  136.  
  137.  
  138. ; Output current state to display
  139. :output
  140. set I, Y ; let I be loop counter
  141. mul I, X
  142.  
  143. :loop2 ife I, 0 ; loop until I=0
  144. set PC, loop2end
  145.  
  146. sub I, 1
  147.  
  148. set A, [0x1000+I] ; set A to value at cell (t)
  149. and A, C
  150.  
  151. sub C, 1
  152. shr A, C
  153. add C, 1
  154.  
  155. mul A, 3
  156. add A, 0xf120
  157.  
  158. set Z, I ; prepare output position
  159. set J, I
  160. div Z, X
  161. mod J, X
  162. mul Z, 0x20 ; 0x20 = display width
  163. add J, Z
  164. add J, 0x8000
  165.  
  166. set [J], A
  167.  
  168. ; Next iteration
  169. set PC, loop2
  170.  
  171. :loop2end ; clean up & return
  172. set PC, POP
Add Comment
Please, Sign In to add comment