tilemap.z80 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877
  1. tilemap_new:
  2. dec a
  3. jp z,tilemap_init
  4. ld hl,(parsePtr)
  5. dec a
  6. jp z,tilemap_fullrender
  7. ld c,1
  8. dec a
  9. jr z,tmap_scroll_down
  10. dec a
  11. jr z,tmap_scroll_left
  12. dec a
  13. jr z,tmap_scroll_right
  14. dec a
  15. jr z,tmap_scroll_up
  16. dec a
  17. jr z,tmap_scroll
  18. dec a
  19. jp z,tmap_gettile
  20. dec a
  21. jp z,tmap_settile
  22. dec a
  23. jp z,tmap_gettile_pxl
  24. jp p1_errbadtoken
  25. tmap_scroll_left:
  26. ld a,(hl)
  27. cp $2B
  28. call z,p1_ParseNextFullArg
  29. ld b,c
  30. tmap_scroll_left_loop:
  31. _:
  32. push bc
  33. call tilemap_scroll_left
  34. pop bc
  35. djnz -_
  36. ret
  37. tmap_scroll_right:
  38. ld a,(hl)
  39. cp $2B
  40. call z,p1_ParseNextFullArg
  41. ld b,c
  42. tmap_scroll_right_loop:
  43. _:
  44. push bc
  45. call tilemap_scroll_right
  46. pop bc
  47. djnz -_
  48. ret
  49. tmap_scroll_down:
  50. ld a,(hl)
  51. cp $2B
  52. call z,p1_ParseNextFullArg
  53. ld b,c
  54. tmap_scroll_down_loop:
  55. _:
  56. push bc
  57. call tilemap_scroll_down
  58. pop bc
  59. djnz -_
  60. ret
  61. tmap_scroll_up:
  62. ld a,(hl)
  63. cp $2B
  64. call z,p1_ParseNextFullArg
  65. ld b,c
  66. tmap_scroll_up_loop:
  67. _:
  68. push bc
  69. call tilemap_scroll_up
  70. pop bc
  71. djnz -_
  72. ret
  73. tmap_scroll:
  74. call p1_ParseNextFullArg
  75. cp $2B
  76. ld a,c
  77. push af
  78. ld c,1
  79. call z,p1_ParseNextFullArg
  80. pop af
  81. ld b,c
  82. rrca
  83. push af
  84. push bc
  85. call c,tmap_scroll_down_loop
  86. pop bc
  87. pop af
  88. rrca
  89. push af
  90. push bc
  91. call c,tmap_scroll_left_loop
  92. pop bc
  93. pop af
  94. rrca
  95. push af
  96. push bc
  97. call c,tmap_scroll_right_loop
  98. pop bc
  99. pop af
  100. rrca
  101. ret nc
  102. jp tmap_scroll_up_loop
  103. tmap_gettile_pxl:
  104. call p1_ParseNextFullArg
  105. ld a,(tilemap_sy)
  106. add a,c
  107. and %11111000
  108. rrca
  109. rrca
  110. rrca
  111. ld c,a
  112. ld a,(tilemap_y)
  113. add a,c
  114. ld c,a
  115. push bc
  116. call p1_ParseNextFullArg
  117. ld a,(tilemap_sx_mask)
  118. .db $FE
  119. _:
  120. inc c
  121. add a,a
  122. jr nc,-_
  123. ld a,c
  124. and %11111000
  125. rrca
  126. rrca
  127. rrca
  128. ld c,a
  129. ld a,(tilemap_x)
  130. add a,c
  131. pop de
  132. ld c,a
  133. jr +_
  134. tmap_gettile:
  135. ; This gets the tile number based on the map coordinates
  136. call p1_ParseNextFullArg
  137. push bc
  138. call p1_ParseNextFullArg
  139. pop de
  140. _:
  141. ld b,0
  142. ld hl,(tilemap_height)
  143. ld a,e
  144. cp l
  145. jr nc,setC_to_B
  146. ld a,d
  147. cp h
  148. jr nc,setC_to_B
  149. ;need tilemap_base+width*e+c
  150. ld hl,(tilemap_base)
  151. ld b,0
  152. add hl,bc
  153. ld d,b
  154. ld a,(tilemap_width)
  155. ld b,a
  156. or a
  157. jr z,+_
  158. add hl,de
  159. djnz $-1
  160. _:
  161. ld c,(hl)
  162. ld b,d
  163. ret
  164. setC_to_B:
  165. ld c,b
  166. ret
  167. tmap_settile:
  168. call p1_ParseNextFullArg
  169. push bc
  170. call p1_ParseNextFullArg
  171. pop de
  172. ;need tilemap_base+width*e+c
  173. ld b,0
  174. ld hl,(tilemap_height)
  175. ld a,e
  176. cp l
  177. jr nc,parse_return_0
  178. ld a,d
  179. cp h
  180. jr nc,parse_return_0
  181. ld hl,(tilemap_base)
  182. add hl,bc
  183. ld d,b
  184. ld a,(tilemap_width)
  185. ld b,a
  186. or a
  187. jr z,+_
  188. add hl,de
  189. djnz $-1
  190. _:
  191. push hl
  192. call p1_ParseNextFullArg
  193. pop hl
  194. ld a,(hl)
  195. ld (hl),c
  196. ld c,a
  197. ld b,0
  198. ret
  199. parse_return_0:
  200. call p1_ParseNextFullArg
  201. ld bc,0
  202. ret
  203. tilemap_init:
  204. ;Use this routine to initialize a tilemap
  205. ; (tilemap_base) points to the tilemap base
  206. ; (tilemap_height) is the height
  207. ; (tilemap_width) is the width
  208. ; (sprite_select) points to the routine for selecting the sprite
  209. ; (tilemap_buf) points to the buffer to draw on
  210. ; tilemap_flags
  211. ;Need to set
  212. ; (tilemap_sy) is the y-offset into the sprite
  213. ; (tilemap_y) is the y-offset into the tilemap
  214. ; (tilemap_sx_mask) is the column mask for the sprite
  215. ; (tilemap_x) is the x-offset into the tilemap
  216. ; (tilemap_top) is the pointer to the upper-left tile in view
  217. ; (tilemap_bottom) is the pointer to the bottom-left incoming row
  218. call p1_ParseNextFullArg
  219. ld (tilemap_base),bc
  220. call p1_ParseNextFullArg
  221. ld (spritesheet_ptr),bc
  222. call p1_ParseNextFullArg
  223. ld a,c
  224. ld (tilemap_height),a
  225. call p1_ParseNextFullArg
  226. cp $2B
  227. ld a,c
  228. ld (tilemap_width),a
  229. ld bc,(bufptr)
  230. call z,p1_ParseNextFullArg
  231. ld (tilemap_buf),bc
  232. ld hl,sprite_selector
  233. ld (sprite_select),hl
  234. xor a
  235. ld (tilemap_flags),a
  236. ;now generate the rest
  237. ld hl,0
  238. ld b,h ;for later
  239. ld (tilemap_sy),hl
  240. ld l,$80
  241. ld (tilemap_sx_mask),hl
  242. ld hl,(tilemap_base)
  243. ld (tilemap_top),hl
  244. ld a,(tilemap_width)
  245. add a,a \ rl b
  246. add a,a \ rl b
  247. add a,a \ rl b
  248. ld c,a
  249. add hl,bc
  250. ld (tilemap_bottom),hl
  251. ret
  252. tilemap_fullrender:
  253. ;Inputs:
  254. ; (D,E) is the offset into the map
  255. ;Set:
  256. ; (tilemap_sx_mask) is the column mask for the sprite
  257. ; (tilemap_x) is the x-offset into the tilemap
  258. ; (tilemap_sy) is the x-offset into the sprite
  259. ; (tilemap_y) is the y-offset into the tilemap
  260. ; (tilemap_top) is the pointer to the upper-left tile in view
  261. ; (tilemap_bottom) is the pointer to the bottom-left incoming row
  262. ld a,(hl)
  263. cp $2B
  264. ld c,0
  265. call z,p1_ParseNextFullArg
  266. push bc
  267. cp $2B
  268. ld c,0
  269. call z,p1_ParseNextFullArg
  270. pop de
  271. ld a,e
  272. ld (tilemap_y),a
  273. ld a,c
  274. ld (tilemap_x),a
  275. xor a
  276. ld d,a ;for later
  277. ld b,a ; for later
  278. ld (tilemap_sy),a
  279. ld a,$80
  280. ld (tilemap_sx_mask),a
  281. ld hl,(tilemap_base)
  282. ;top is x+y*w
  283. add hl,bc
  284. ld a,(tilemap_width)
  285. or a
  286. jr nc,+_
  287. ld b,a
  288. add hl,de
  289. djnz $-1
  290. _:
  291. ld (tilemap_top),hl
  292. ld a,(tilemap_width)
  293. ;now add width*8 to HL for tilemap_bottom
  294. ld e,a
  295. add a,a \ rl d
  296. add a,a \ rl d
  297. add a,a \ rl d
  298. ld e,a
  299. add hl,de
  300. ld (tilemap_bottom),hl
  301. ;Now we need to actually draw the tilemap
  302. ;I'm going to do this the slow and easy way
  303. ld hl,(tilemap_top)
  304. ld de,(tilemap_buf)
  305. ld ix,(sprite_select)
  306. ld c,0
  307. tilemap_y_loop:
  308. ld b,0
  309. push hl
  310. tilemap_x_loop:
  311. push hl
  312. push bc
  313. push de
  314. ld a,(hl)
  315. call tilemap_getsprite_at_BC
  316. pop hl
  317. push hl
  318. ;at HL, draw the tile from DE
  319. ld bc,12
  320. ld a,8
  321. _:
  322. ex de,hl
  323. ldi
  324. ex de,hl
  325. add hl,bc
  326. inc c
  327. dec a
  328. jr nz,-_
  329. pop de
  330. inc de
  331. pop bc
  332. pop hl
  333. inc hl
  334. inc b
  335. ld a,b
  336. cp 12
  337. jr nz,tilemap_x_loop
  338. ld hl,7*12
  339. add hl,de
  340. ex de,hl
  341. pop hl
  342. ;need to add width to HL
  343. ld a,(tilemap_width)
  344. add a,l
  345. ld l,a
  346. jr nc,$+3
  347. inc h
  348. inc c
  349. bit 3,c
  350. jr z,tilemap_y_loop
  351. ret
  352. tilemap_scroll_left:
  353. ld ix,(sprite_select)
  354. ld hl,(tilemap_top)
  355. ld bc,12
  356. add hl,bc
  357. ld (tempword2),hl ;tilemap pointer
  358. ld a,(tilemap_x)
  359. add a,12
  360. ld b,a
  361. ld a,(tilemap_y)
  362. ld c,a
  363. ld (tempword3),bc ;tilemap (x,y)
  364. ld a,(hl)
  365. call tilemap_getsprite
  366. ;DE points to the sprite data
  367. ld hl,(tilemap_buf)
  368. ld bc,11
  369. add hl,bc
  370. ld a,(tilemap_sy)
  371. cpl
  372. and 7
  373. inc a
  374. ld b,a
  375. ld a,(tilemap_sy)
  376. add a,e
  377. ld e,a
  378. jr nc,$+3
  379. inc d
  380. ld a,(tilemap_sx_mask)
  381. ld c,a
  382. tilemap_scroll_left_loop0:
  383. push bc
  384. ld a,(de)
  385. inc de
  386. and c
  387. add a,255
  388. ld b,12
  389. _:
  390. rl (hl)
  391. dec hl
  392. djnz -_
  393. ld c,24
  394. add hl,bc
  395. pop bc
  396. djnz tilemap_scroll_left_loop0
  397. ld b,7
  398. tilemap_scroll_left_loop1:
  399. ld a,(tempword3)
  400. inc a
  401. ld (tempword3),a
  402. push bc
  403. push hl
  404. ld hl,(tempword2)
  405. ld a,(tilemap_width)
  406. ld c,a
  407. ld b,0
  408. add hl,bc
  409. ld (tempword2),hl
  410. ld a,(hl)
  411. call tilemap_getsprite
  412. pop hl
  413. ld b,8
  414. ld a,(tilemap_sx_mask)
  415. ld c,a
  416. tilemap_scroll_left_loop2:
  417. push bc
  418. ld a,(de)
  419. inc de
  420. and c
  421. add a,255
  422. ld b,12
  423. _:
  424. rl (hl)
  425. dec hl
  426. djnz -_
  427. ld c,24
  428. add hl,bc
  429. pop bc
  430. djnz tilemap_scroll_left_loop2
  431. pop bc
  432. djnz tilemap_scroll_left_loop1
  433. ld a,(tempword3)
  434. inc a
  435. ld (tempword3),a
  436. ;final iteration
  437. push hl
  438. ld hl,(tempword2)
  439. ld a,(tilemap_width)
  440. ld c,a
  441. ld b,0
  442. add hl,bc
  443. ld (tempword2),hl
  444. ld a,(hl)
  445. call tilemap_getsprite
  446. pop hl
  447. ld a,(tilemap_sy)
  448. or a
  449. jr z,tilemap_scroll_left_end
  450. ld b,a
  451. ld a,(tilemap_sx_mask)
  452. ld c,a
  453. tilemap_scroll_left_loop3:
  454. push bc
  455. ld a,(de)
  456. inc de
  457. and c
  458. add a,255
  459. ld b,12
  460. _:
  461. rl (hl)
  462. dec hl
  463. djnz -_
  464. ld c,24
  465. add hl,bc
  466. pop bc
  467. djnz tilemap_scroll_left_loop3
  468. tilemap_scroll_left_end:
  469. ld hl,tilemap_sx_mask
  470. rrc (hl)
  471. ret nc
  472. inc hl
  473. inc (hl) ;tilemap_x
  474. ld hl,(tilemap_top)
  475. inc hl
  476. ld (tilemap_top),hl
  477. ld hl,(tilemap_bottom)
  478. inc hl
  479. ld (tilemap_bottom),hl
  480. ret
  481. tilemap_scroll_right:
  482. ld ix,(sprite_select)
  483. ld hl,tilemap_sx_mask
  484. rlc (hl)
  485. jr nc,+_
  486. inc hl
  487. dec (hl) ;tilemap_x
  488. ld hl,(tilemap_bottom)
  489. dec hl
  490. ld (tilemap_bottom),hl
  491. ld hl,(tilemap_top)
  492. dec hl
  493. ld (tilemap_top),hl
  494. _:
  495. ld hl,(tilemap_top)
  496. ld (tempword2),hl ;tilemap pointer
  497. ld a,(tilemap_x)
  498. ld b,a
  499. ld a,(tilemap_y)
  500. ld c,a
  501. ld (tempword3),bc ;tilemap (x,y)
  502. ld a,(hl)
  503. call tilemap_getsprite
  504. ;DE points to the sprite data
  505. ld hl,(tilemap_buf)
  506. ld a,(tilemap_sy)
  507. cpl
  508. and 7
  509. inc a
  510. ld b,a
  511. ld a,(tilemap_sy)
  512. add a,e
  513. ld e,a
  514. jr nc,$+3
  515. inc d
  516. ld a,(tilemap_sx_mask)
  517. ld c,a
  518. tilemap_scroll_right_loop0:
  519. push bc
  520. ld a,(de)
  521. inc de
  522. and c
  523. add a,255
  524. ld b,12
  525. _:
  526. rr (hl)
  527. inc hl
  528. djnz -_
  529. pop bc
  530. djnz tilemap_scroll_right_loop0
  531. ld b,7
  532. tilemap_scroll_right_loop1:
  533. ld a,(tempword3)
  534. inc a
  535. ld (tempword3),a
  536. push bc
  537. push hl
  538. ld hl,(tempword2)
  539. ld a,(tilemap_width)
  540. add a,l
  541. ld l,a
  542. jr nc,$+3
  543. inc h
  544. ld (tempword2),hl
  545. ld a,(hl)
  546. call tilemap_getsprite
  547. pop hl
  548. ld b,8
  549. ld a,(tilemap_sx_mask)
  550. ld c,a
  551. tilemap_scroll_right_loop2:
  552. push bc
  553. ld a,(de)
  554. inc de
  555. and c
  556. add a,255
  557. ld b,12
  558. _:
  559. rr (hl)
  560. inc hl
  561. djnz -_
  562. pop bc
  563. djnz tilemap_scroll_right_loop2
  564. pop bc
  565. djnz tilemap_scroll_right_loop1
  566. ld a,(tempword3)
  567. inc a
  568. ld (tempword3),a
  569. ;final iteration
  570. push hl
  571. ld hl,(tempword2)
  572. ld a,(tilemap_width)
  573. add a,l
  574. ld l,a
  575. jr nc,$+3
  576. inc h
  577. ld (tempword2),hl
  578. ld a,(hl)
  579. call tilemap_getsprite
  580. pop hl
  581. ld a,(tilemap_sy)
  582. or a
  583. ret z
  584. ld b,a
  585. ld a,(tilemap_sx_mask)
  586. ld c,a
  587. tilemap_scroll_right_loop3:
  588. push bc
  589. ld a,(de)
  590. inc de
  591. and c
  592. add a,255
  593. ld b,12
  594. _:
  595. rr (hl)
  596. inc hl
  597. djnz -_
  598. pop bc
  599. djnz tilemap_scroll_right_loop3
  600. ret
  601. tilemap_scroll_up:
  602. ld ix,(sprite_select)
  603. ld a,(tilemap_x)
  604. ld h,a
  605. ld a,(tilemap_y)
  606. add a,8
  607. ld l,a
  608. ld (tempword3),hl ;tilemap (x,y)
  609. ld de,(tilemap_buf)
  610. ld hl,12
  611. add hl,de
  612. ld bc,756
  613. ldir
  614. ;Now we need to populate the row of pixels
  615. ;first right the sprite data as if it is supposed to be aligned
  616. ld hl,(tilemap_bottom)
  617. ld b,12
  618. ;DE points to where to write the sprite data
  619. ;HL points to the tile
  620. ;B is the number of tiles to read
  621. tilemap_scroll_up_loop_0:
  622. push bc
  623. push hl
  624. push de
  625. ld a,(hl)
  626. call tilemap_getsprite
  627. ld a,(tilemap_sy)
  628. add a,e
  629. ld e,a
  630. jr nc,$+3
  631. inc d
  632. ld a,(de)
  633. pop de
  634. ld (de),a
  635. inc de
  636. pop hl
  637. inc hl
  638. ld a,(tempword3+1)
  639. inc a
  640. ld (tempword3+1),a
  641. pop bc
  642. djnz tilemap_scroll_up_loop_0
  643. ;now we need to shift the row left, shifting in the bits from the next tile
  644. push de
  645. ld a,(hl)
  646. call tilemap_getsprite
  647. ld a,(tilemap_sy)
  648. add a,e
  649. ld e,a
  650. jr nc,$+3
  651. inc d
  652. ld a,(de)
  653. pop hl
  654. ld c,a
  655. ld a,(tilemap_sx_mask)
  656. ;need to shift in C until A is $80
  657. ld de,12
  658. jr tilemap_scroll_up_loop_1_start
  659. tilemap_scroll_up_loop_1:
  660. ld b,e
  661. rlc c
  662. _:
  663. dec hl
  664. rl (hl)
  665. djnz -_
  666. add hl,de
  667. tilemap_scroll_up_loop_1_start:
  668. add a,a
  669. jr nc,tilemap_scroll_up_loop_1
  670. ld a,(tilemap_sy)
  671. inc a
  672. and 7
  673. ld (tilemap_sy),a
  674. ret nz
  675. ld a,(tilemap_y)
  676. inc a
  677. ld (tilemap_y),a
  678. ld a,(tilemap_width)
  679. ld c,a
  680. ld b,0
  681. ld hl,(tilemap_top)
  682. add hl,bc
  683. ld (tilemap_top),hl
  684. ld hl,(tilemap_bottom)
  685. add hl,bc
  686. ld (tilemap_bottom),hl
  687. ret
  688. tilemap_scroll_down:
  689. ld ix,(sprite_select)
  690. ld a,(tilemap_x)
  691. ld h,a
  692. ld a,(tilemap_y)
  693. ld l,a
  694. ld (tempword3),hl ;tilemap (x,y)
  695. ld de,(tilemap_buf)
  696. inc d
  697. inc d
  698. inc d
  699. dec de
  700. ld hl,-12
  701. add hl,de
  702. ld bc,756
  703. lddr
  704. ld a,(tilemap_sy)
  705. dec a
  706. ld (tilemap_sy),a
  707. jp p,+_
  708. and 7
  709. ld (tilemap_sy),a
  710. ld a,(tilemap_y)
  711. dec a
  712. ld (tilemap_y),a
  713. ld (tempword3),a
  714. ld a,(tilemap_width)
  715. neg
  716. ld c,a
  717. ld b,-1
  718. ld hl,(tilemap_top)
  719. add hl,bc
  720. ld (tilemap_top),hl
  721. ld hl,(tilemap_bottom)
  722. add hl,bc
  723. ld (tilemap_bottom),hl
  724. _:
  725. ;Now we need to populate the row of pixels
  726. ;first right the sprite data as if it is supposed to be aligned
  727. ld de,(tilemap_buf)
  728. ld hl,(tilemap_top)
  729. ld b,12
  730. ;DE points to where to write the sprite data
  731. ;HL points to the tile
  732. ;B is the number of tiles to read
  733. tilemap_scroll_down_loop_0:
  734. push bc
  735. push hl
  736. push de
  737. ld a,(hl)
  738. call tilemap_getsprite
  739. ld a,(tilemap_sy)
  740. add a,e
  741. ld e,a
  742. jr nc,$+3
  743. inc d
  744. ld a,(de)
  745. pop de
  746. ld (de),a
  747. inc de
  748. pop hl
  749. inc hl
  750. ld a,(tempword3+1)
  751. inc a
  752. ld (tempword3+1),a
  753. pop bc
  754. djnz tilemap_scroll_down_loop_0
  755. ;now we need to shift the row left, shifting in the bits from the next tile
  756. push de
  757. ld a,(hl)
  758. call tilemap_getsprite
  759. ld a,(tilemap_sy)
  760. add a,e
  761. ld e,a
  762. jr nc,$+3
  763. inc d
  764. ld a,(de)
  765. pop hl
  766. ld c,a
  767. ld a,(tilemap_sx_mask)
  768. ;need to shift in C until A is $80
  769. ld de,12
  770. jr tilemap_scroll_down_loop_1_start
  771. tilemap_scroll_down_loop_1:
  772. ld b,e
  773. rlc c
  774. _:
  775. dec hl
  776. rl (hl)
  777. djnz -_
  778. add hl,de
  779. tilemap_scroll_down_loop_1_start:
  780. add a,a
  781. jr nc,tilemap_scroll_down_loop_1
  782. ret
  783. tilemap_getsprite:
  784. ld bc,(tempword3)
  785. tilemap_getsprite_at_BC:
  786. ld hl,(tilemap_height)
  787. jp (ix)
  788. sprite_selector:
  789. ld e,a
  790. ld a,b
  791. cp h
  792. jr nc,+_
  793. ld a,c
  794. cp l
  795. ld a,e
  796. _:
  797. ld de,(spritesheet_ptr)
  798. ret nc
  799. ld l,a
  800. ld h,0
  801. add hl,hl
  802. add hl,hl
  803. add hl,hl
  804. add hl,de
  805. ex de,hl
  806. ret