alvalongo

C64 spiral 05

Jul 23rd, 2016
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. 100 gosub 7000:print chr$(147);chr$(5);
  2. 105 print "2016-July basic month: terraspin"
  3. 110 print "http://reddit.com/r/retrobattlestations"
  4. 115 print "written by fozztexx - commodore 64 port by fozztexx"
  5. 120 print "commodore 64 basic has bitwise operators so much of the line"
  6. 125 print "clipping logic is simplified compared to the apple ii version"
  7. 130 print "the commodore 64 has *no* graphics commands at all"
  8. 135 print "all graphics has to be done using peeks and pokes"
  9. 140 print "line drawing is handled by a complicated routine at 1500"
  10. 145 print "individual pixels are then plotted with the routine at 1700"
  11. 150 print "2016-07-10 alvalongo"
  12. 155 print "optimized plotting using variables"
  13. 160 print "and constants"
  14. 170 print "========================================"
  15. 199 :
  16. 200 rem === initialize variables
  17. 210 sw=320:sh=200:xs=sw/1000:ys=(sh*4)/(1000*3):k1=1:e=1
  18. 220 sw=sw-k1:sh=sh-k1:dg=360:k7=7:k8=8:z0=0:sid=54296
  19. 230 p8={pi}/180:rem pi is a c64 constant
  20. 235 dim b(7),c(39),l(199):rem bitmask, column offset, line
  21. 240 gosub 7100
  22. 250 rem === set turtle start at center of screen pointing up, pen up
  23. 260 tx=500:ty=sh/ys/2:ta=90:tp=z0
  24. 265 :
  25. 300 rem === get command to execute
  26. 301 rem numeric values will be pushed onto a stack
  27. 302 rem a command will pop off a value and then execute
  28. 303 rem d - lower the pen
  29. 304 rem u - raise the pen
  30. 305 rem m - move
  31. 306 rem t - turn
  32. 307 rem ======================
  33. 310 cm$="d 20 m 90 t 40 m 90 t 60 m 90 t 80 m"
  34. 311 cm$=cm$+" 90 t 100 m 90 t 120 m 90 t 140 m 90 t 160 m 90 t 180 m"
  35. 312 cm$=cm$+" 90 t 200 m 90 t 220 m 90 t 240 m 90 t 260 m 90 t 280 m"
  36. 313 cm$=cm$+" 90 t 300 m 90 t 320 m 90 t 340 m 90 t 360 m 90 t 380 m"
  37. 314 cm$=cm$+" 90 t 400 m 90 t 420 m 90 t 440 m 90 t 460 m"
  38. 315 c1$=" 90 t 480 m"
  39. 316 c1$=c1$+" 90 t 500 m 90 t 520 m 90 t 540 m 90 t 560 m 90 t 580 m"
  40. 317 c1$=c1$+" 90 t 600 m 90 t 620 m 90 t 640 m 90 t 660 m 90 t 680 m"
  41. 318 c1$=c1$+" 90 t 700 m 90 t 720 m 90 t 740 m"
  42. 390 :
  43. 460 rem ### if the program crashes it will
  44. 470 rem ### be stuck in graphics mode.
  45. 480 rem ### type run 900 to get back to text.
  46. 490 gosub 6000:gosub 6100
  47. 495 :
  48. 500 rem === command interpreter
  49. 510 ip=k1:sp=z0
  50. 520 c$=mid$(cm$,ip,k1)
  51. 530 if c$=" " then ip=ip+k1:goto 520
  52. 535 print ip;",";tx;",";ty;",";ta;",";tp;",";c$
  53. 540 if c$>="-" and c$<="9" then v=val(mid$(cm$,ip)):gosub 1010:gosub 1210
  54. 550 if c$="(" then v=ip:gosub 1010
  55. 560 if c$=")" then gosub 2010
  56. 570 if c$="m" then gosub 2510
  57. 580 if c$="t" then gosub 3010
  58. 590 if c$="u" then gosub 3510
  59. 600 if c$="d" then gosub 4010
  60. 610 ip=ip+k1
  61. 620 if ip<=len(cm$) then 520
  62. 630 if e<2 then e=2:cm$=c1$:goto 510
  63. 700 get k$:if k$="" then gosub 7200:goto 700
  64. 900 print "End"
  65. 910 gosub 7000
  66. 990 end
  67. 1000 rem === push onto stack
  68. 1010 sp=sp+k1:sk(sp)=v
  69. 1020 return
  70. 1025 :
  71. 1100 rem === pop from stack
  72. 1110 v=sk(sp):sp=sp-k1
  73. 1120 return
  74. 1125 :
  75. 1200 rem === skip over number
  76. 1210 ip=ip+k1
  77. 1220 if ip>len(cm$) then 1250
  78. 1230 c2$=mid$(cm$, ip, k1)
  79. 1240 if c2$>="-" and c2$<="9" then goto 1210
  80. 1250 ip=ip-k1
  81. 1260 return
  82. 1265 :
  83. 1500 rem === plot a line
  84. 1510 x1%=x1:x2%=x2:y1%=y1:y2%=y2
  85. 1520 dx%=abs(x2%-x1%):sx%=-k1:if x1%<x2% then sx%=k1
  86. 1530 dy%=abs(y2%-y1%):sy%=-k1:if y1%<y2% then sy%=k1
  87. 1540 er%=-dy%:if dx%>dy% then er%=dx%
  88. 1550 er%=er%/2
  89. 1560 gosub 1710
  90. 1570 if x1%=x2% and y1%=y2% then return
  91. 1580 e2%=er%
  92. 1590 if e2%>-dx% then er%=er%-dy%:x1%=x1%+sx%
  93. 1600 if x1%=x2% and y1%=y2% then return
  94. 1610 if e2%<dy% then er%=er%+dx%:y1%=y1%+sy%
  95. 1620 if x1%=x2% and y1%=y2% then return
  96. 1630 goto 1560
  97. 1640 :
  98. 1700 rem === plot a point ===========
  99. 1710 by=l(y1%)+c(x1%/k8)
  100. 1730 poke by,peek(by) or b(x1% and k7)
  101. 1740 return
  102. 1750 :
  103. 2000 rem === loop instruction end
  104. 2010 gosub 1110:bp=v
  105. 2020 gosub 1110:lr=v
  106. 2030 lr=lr-k1
  107. 2040 if lr<k1 then return
  108. 2050 v=lr:gosub 1010
  109. 2060 v=bp:gosub 1010
  110. 2070 ip=bp
  111. 2080 return
  112. 2085 :
  113. 2500 rem === move
  114. 2510 gosub 1110
  115. 2520 lx=v*cos((dg-ta)*p8):ly=v*sin((dg-ta)*p8)
  116. 2530 if tp>z0 then x1=tx:y1=ty:x2=x1+lx:y2=y1+ly:gosub 4510
  117. 2540 tx=tx+lx:ty=ty+ly
  118. 2550 return
  119. 2555 :
  120. 3000 rem === turn
  121. 3010 gosub 1110
  122. 3020 ta=ta-v
  123. 3030 if ta<z0  then ta=ta+dg:goto 3030
  124. 3040 if ta>=dg then ta=ta-dg:goto 3040
  125. 3050 return
  126. 3055 :
  127. 3500 rem === pen up
  128. 3510 tp=k0
  129. 3520 return
  130. 3525 :
  131. 4000 rem === pen down
  132. 4010 tp=k1
  133. 4020 return
  134. 4025:
  135. 4500 rem === draw line, clipping if needed
  136. 4510 x1=x1*xs:x2=x2*xs:y1=y1*ys:y2=y2*ys
  137. 4520 x=x1:y=y1:gosub 5010:c1=c
  138. 4530 x=x2:y=y2:gosub 5010:c2=c
  139. 4540 if c1=z0 and c2=z0 then gosub 1510:return
  140. 4550 if c1 and c2 then return
  141. 4560 c=c1:if c=z0 then c=c2
  142. 4570 if c and 1 then x=x1+(x2-x1)*(sh-y1)/(y2-y1):y=sh:goto 4610
  143. 4580 if c and 2 then x=x1+(x2-x1)*(z0-y1)/(y2-y1):y=z0:goto 4610
  144. 4590 if c and 4 then y=y1+(y2-y1)*(sw-x1)/(x2-x1):x=sw:goto 4610
  145. 4600 if c and 8 then y=y1+(y2-y1)*(z0-x1)/(x2-x1):x=z0:goto 4610
  146. 4610 if c=c1 then x1=x:y1=y:goto 4630
  147. 4620 x2=x:y2=y
  148. 4630 goto 4520
  149. 5000 rem === calculate region code
  150. 5010 c=z0
  151. 5020 if y>sh then c=c or k1
  152. 5030 if y<z0 then c=c or 2
  153. 5040 if x>sw then c=c or 4
  154. 5050 if x<z0 then c=c or k8
  155. 5060 return
  156. 5065 :
  157. 6000 rem === enable hi-res 320x200 screen
  158. 6010 poke 56576,(peek(56576)and 252)or 2:rem video bank 1 at $4000
  159. 6015 poke 53272,0+128:rem offset $0000 (hires)+$2000(color)
  160. 6020 poke 53265,peek(53265) or 32:rem hires-on
  161. 6030 bm=16384:rem bitmap memory at $4000
  162. 6040 cm=24576:rem color memory at $6000
  163. 6050 for x=0 to 7:b(x)=2^(k7-(x and k7)):next x:rem bit mask
  164. 6060 for x=0 to 39:c(x)=k8*x:next x:rem column offset
  165. 6070 for y=0 to 199:l(y)=bm+320*int(y/k8)+(y and k7):next y:rem line
  166. 6080 return
  167. 6095 :
  168. 6100 rem === clear hires screen
  169. 6110 for i=cm to cm+999:poke i,3:next:rem color
  170. 6120 sys 49152:rem clear hires
  171. 6130 return
  172. 6135 :
  173. 7000 rem === enable text screen ===
  174. 7010 poke 56576,(peek(56576)and 252)or 3:rem video bank 0 at $0000
  175. 7015 poke 53272,16+6:rem offset $0400+$1800
  176. 7020 poke 53265,peek(53265) and 223:rem text
  177. 7030 return
  178. 7100 rem === clear hires screen assembler
  179. 7110 s=z0:for by=49152 to 49176
  180. 7120 read a:poke by,a:s=s+a:next by
  181. 7130 read a:if s<>a then print "error assembly":end
  182. 7140 data 169,0,133,251,169,64,133,252
  183. 7150 data 162,32,160,0,169,0,145,251
  184. 7160 data 136,208,251,230,252,202,208
  185. 7170 data 246,96,3919
  186. 7180 return
  187. 7195 :
  188. 7200 rem --- beep
  189. 7210 poke 54296,15
  190. 7220 tz=ti+80
  191. 7230 if ti<tz then 7230
  192. 7240 poke 54296,z0
  193. 7250 return
Add Comment
Please, Sign In to add comment