COLDET.S 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  1. ;file: coldet.s
  2. ;collision detect
  3. ;sister file: cdvars.s (collision detect variables)
  4. ;
  5. ; precision version
  6. ; call with movs in x and y regs
  7. ; rts carry set if collision
  8. ;
  9. coldet:
  10. pushx
  11. pushy
  12. ;
  13. ; calc useful params
  14. ;
  15. lda movy,x ;xd = movy,x
  16. sta xd
  17. lda movht,x ;xu = (movht,x * 16) + xd
  18. asl
  19. asl
  20. asl
  21. asl
  22. clc
  23. adc xd
  24. sta xu
  25. lda movy,y ;yd = movy,y
  26. sta yd
  27. lda movht,y ;yu = (movht,y * 16) + yx
  28. asl
  29. asl
  30. asl
  31. asl
  32. clc
  33. adc yd
  34. sta yu
  35. lda movx,x ;xl = movx,x
  36. clc
  37. adc #($100 - SCWP)/2
  38. sta xl
  39. lda movx,y ;yl = movx,y
  40. clc
  41. adc #($100 - SCWP)/2
  42. sta yl
  43. lda movw,x ;xr = xl + (movw,x << 2)
  44. asl
  45. asl
  46. clc
  47. adc xl
  48. sta xr
  49. lda movw,y ;yr = yl + (movw,y << 2)
  50. asl
  51. asl
  52. clc
  53. adc yl
  54. sta yr
  55. ;
  56. ; find lesser of xu,yu store as lesserup
  57. ;
  58. lda xu
  59. cmp yu
  60. bcc .yuOVERxu
  61. ;xuOVERyu:
  62. lda yu
  63. jmp .break1
  64. .yuOVERxu:
  65. lda xu
  66. .break1:
  67. sta lesserup
  68. ;
  69. ; switch on greater of (xd,yd)
  70. ;
  71. lda xd
  72. cmp yd
  73. bcc .ydOVERxd
  74. ;
  75. ; set high addresses and height of overlap
  76. ;
  77. ;xdOVERyd:
  78. lda movfrh,x ;xah = movfrh,x
  79. sta xah
  80. lda movfrh,y ;yah = movfrh,y + (xd - yd)
  81. clc
  82. adc xd
  83. sec
  84. sbc yd
  85. sta yah
  86. lda xd ;cdheight = xd - lesser(xu,yu) (2's comp)
  87. sec
  88. sbc lesserup
  89. sta cdheight
  90. jmp .break2
  91. .ydOVERxd:
  92. lda movfrh,y ;yah = movfrh,y
  93. sta yah
  94. lda movfrh,x ;xah = movfrh,x + (yd - xd)
  95. clc
  96. adc yd
  97. sec
  98. sbc xd
  99. sta xah
  100. lda yd ;cdheight = yd - lesser(xu,yu) (2's comp)
  101. sec
  102. sbc lesserup
  103. sta cdheight
  104. .break2:
  105. ;
  106. ; 2`s comp of height and its carry are ready here
  107. ; if carry set, then no overlap, ret false
  108. ;
  109. bcc .lineoverlap
  110. jmp .exitfalse
  111. .lineoverlap:
  112. ;
  113. ; find lesser of xr,yr store as lesserright
  114. ;
  115. lda xr
  116. cmp yr
  117. bcc .yrOVERxr
  118. ;xrOVERyr:
  119. lda yr
  120. jmp .break3
  121. .yrOVERxr:
  122. lda xr
  123. .break3:
  124. sta lesserright
  125. ;
  126. ; switch on greater of (xl,yl)
  127. ;
  128. lda xl
  129. cmp yl
  130. bcc .yprimary
  131. ;
  132. ; set low addresses and width of overlap
  133. ;
  134. ;xprimary:
  135. lda xah ;pah = xah
  136. sta pah
  137. lda yah ;sah = yah
  138. sta sah
  139. lda movfrl,x ;pal = movfrl,x
  140. sta pal
  141. lda xl ;pshift = (xl - yl) & %11
  142. sec
  143. sbc yl
  144. sta i ;save this intermediate calc (xl - yl)
  145. and #%11
  146. sta pshift
  147. eor #%11 ;sshift = ~pshift + 1 & %11
  148. clc
  149. adc #1
  150. sta sshift
  151. lda i ;sal = ((xl - yl) >> 2) + movfrl,y
  152. lsr
  153. lsr
  154. clc
  155. adc movfrl,y
  156. sta sal
  157. lda xl ;cdwidth = xl - lesser(xr,yr) (2's comp)
  158. sec
  159. sbc lesserright
  160. sta cdwidth
  161. jmp .break4
  162. .yprimary:
  163. lda yah ;pah = yah
  164. sta pah
  165. lda xah ;sah = xah
  166. sta sah
  167. lda movfrl,y ;pal = movfrl,y
  168. sta pal
  169. lda yl ;pshift = (yl - xl) & %11
  170. sec
  171. sbc xl
  172. sta i ;save this intermediate calc (yl - xl)
  173. and #%11
  174. sta pshift
  175. eor #%11 ;sshift = ~pshift + 1 & %11
  176. clc
  177. adc #1
  178. sta sshift
  179. lda i ;sal = ((yl - xl) >> 2) + movfrl,x
  180. lsr
  181. lsr
  182. clc
  183. adc movfrl,x
  184. sta sal
  185. lda yl ;cdwidth = yl - lesser(yr,xr) (2's comp)
  186. sec
  187. sbc lesserright
  188. sta cdwidth
  189. .break4:
  190. ;
  191. ; 2`s comp of width and its carry are ready here
  192. ; carry set, then no overlap, rts false
  193. ;
  194. bcc .pixoverlap
  195. jmp .exitfalse
  196. .pixoverlap:
  197. ;
  198. ; x and y regs now used for new purposes
  199. ;
  200. ldy #0
  201. ;
  202. ; save vars that will be clobbered in inner loop but needed again
  203. lda pal
  204. sta palsav
  205. lda sal
  206. sta salsav
  207. .lineloop:
  208. lda (sal),y ;get first secondary byte
  209. beq .skipc1
  210. .if 0
  211. jsr convbits
  212. .endif
  213. tax ;convert 2bits per pixel to 1
  214. lda COLTAB,x
  215. .skipc1:
  216. inc sal ;point to next byte
  217. sta sbyte
  218. lda cdwidth ;init remaining width
  219. sta rwidth
  220. .pixloop:
  221. lda (pal),y ;get primary byte
  222. beq .skipc2
  223. .if 0
  224. jsr convbits ;convert 2bits per pixel to 1
  225. .endif
  226. tax
  227. lda COLTAB,x
  228. .skipc2:
  229. inc pal ;point to next byte
  230. sta pbyte
  231. ldx pshift ;shift primary byte
  232. beq .donepshift
  233. .pshiftloop:
  234. lsr
  235. dex
  236. bne .pshiftloop
  237. .donepshift:
  238. and sbyte ;AND shifted primary with secondary bytes
  239. bne .exittrue
  240. lda rwidth ;update remaining width, exit inner if zero
  241. clc
  242. adc sshift
  243. sta rwidth
  244. beq .breakinner
  245. ;
  246. ; Get secondary byte, inc pointer, shift, AND with primary byte
  247. ;
  248. lda (sal),y
  249. beq .skipc3
  250. .if 0
  251. jsr convbits
  252. .endif
  253. tax ;convert 2 bits per pixel to 1
  254. lda COLTAB,x
  255. .skipc3:
  256. inc sal
  257. sta sbyte
  258. ldx sshift ;shift secondary byte
  259. beq .donesshift
  260. .sshiftloop:
  261. lsr
  262. dex
  263. bne .sshiftloop
  264. .donesshift:
  265. and pbyte ;AND shifted secondary with primary
  266. bne .exittrue
  267. lda rwidth ;update remaining width, exit inner if zero
  268. clc
  269. adc pshift
  270. sta rwidth
  271. beq .breakinner
  272. jmp .pixloop
  273. .breakinner:
  274. ;
  275. ; if no remaining height, exit false
  276. ;
  277. inc cdheight
  278. beq .exitfalse
  279. ;
  280. ; restore clobbered vars, check next line
  281. ;
  282. lda palsav
  283. sta pal
  284. lda salsav
  285. sta sal
  286. inc pah ;inc address highs
  287. inc sah
  288. jmp .lineloop
  289. .exittrue:
  290. popy
  291. popx
  292. sec
  293. rts
  294. .exitfalse:
  295. popy
  296. popx
  297. clc
  298. rts
  299. ;
  300. ; convert bits
  301. ; compresses one byte of 2 bits per pixel to one bit per pixel
  302. ; input and output in accumulator
  303. ; clobbers x reg, i
  304. .if 0
  305. convbits:
  306. tax
  307. lda COLTAB,x
  308. rts
  309. tax
  310. lda #0
  311. sta i
  312. txa
  313. and #%11
  314. beq .skip1
  315. lda #1
  316. sta i
  317. .skip1: txa
  318. and #%1100
  319. beq .skip2
  320. lda #%10
  321. ora i
  322. sta i
  323. .skip2: txa
  324. and #%110000
  325. beq .skip3
  326. lda #%100
  327. ora i
  328. sta i
  329. .skip3: txa
  330. and #%11000000
  331. beq .skip4
  332. lda #%1000
  333. ora i
  334. sta i
  335. .skip4: lda i
  336. .endif
  337. rts
  338. ;
  339. ; set score to an absolute number, in A reg (octal readout)
  340. ;
  341. .if 0
  342. setscore:
  343. sta i
  344. pushx
  345. pushy
  346. lda #0
  347. sta score
  348. sta score+1
  349. sta score+2
  350. lda i
  351. asl
  352. asl
  353. asl
  354. asl
  355. and #%01110000
  356. jsr incsc
  357. lda i
  358. asl
  359. and #%01110000
  360. ora #1
  361. jsr incsc
  362. lda i
  363. lsr
  364. lsr
  365. and #%01110000
  366. ora #2
  367. jsr incsc
  368. popy
  369. popx
  370. lda i
  371. rts
  372. .endif