## Re: [stella] Delightful horizontal position code...

 Subject: Re: [stella] Delightful horizontal position code... From: Thomas Jentzsch Date: Mon, 12 Feb 2001 09:15:26 +0100
```At 12.02.2001, 07:44, Andrew Davie wrote:
> I did a bit of work on my sprite horizontal position code, and am quite
> delighted with this contribution... it figures out and positions two sprites
> horizontally, based on their pixel position (X) held in PlayerX[2].  I
> analysed the differences between dividing by 16 (4 shifts) and dividing by
> 15 (repeated subtractions) and wrote a CONSTANT-TIME algorithm to do the
> adjustment.  That is, rather than multiple subtractions, this code does some
> shifting and additions (constant time) to figure the player positions.  It
> works, and is quickish - but best of all... constant execution time!

> This is the sort of trick code I like doing :)

I'm using a different constant timed routine, which i
found somewhere. I think it's an old standard routine
which is based on the same idea that you have
reinvented again.
(i changed the code a little bit to get minimum average
timing, the worst case time is slightly increased)

CalcXPos:
tay                         ; 2
lsr                         ; 2
lsr                         ; 2
lsr                         ; 2
lsr                         ; 2
sta     tmpVar              ; 3 = 13
tya                         ; 2
and     #\$0f                ; 2
clc                         ; 2
cmp     #\$0f                ; 2
bcc     .nextPos            ; 2³
sbc     #\$0f                ; 2
inc     tmpVar              ; 5
.nextPos:                       ;   = 14/20
eor     #%00000111          ; 2
asl                         ; 2
asl                         ; 2
asl                         ; 2
asl                         ; 2
ldy     tmpVar              ; 3
rts                         ; 6 = 19

...

lda   xpos
jsr   CalcXPos
sta   WSYNC
sta   HMPx
...                         ; some extra cycles needed here
.wait:
dey
bpl   .wait
sta   RESPx
sta   WSYNC
sta   HMOVE

I think it's a little bit more compact than your approach.

Have fun!
Thomas
_______________________________________________________
Thomas Jentzsch         | *** Every bit is sacred ! ***
tjentzsch at web dot de |

-
Archives (includes files) at http://www.biglist.com/lists/stella/archives/
Unsub & more at http://www.biglist.com/lists/stella/

```