05-29-2010, 03:06 PM
(This post was last modified: 11-03-2024, 09:31 AM by boriel.
Edit Reason: Fix url
)
Ha!
I've been looking at Z80 optimization pages lately.
Under math tricks at http://wikiti.brandonw.net/index.php?tit...timization there are some good ones, including that one. I like the alternatives to using NEG - which is awkward, being an extended instruction:
And this one, when you learn it, is solid gold:
Looping with 16 bit counter
There are two ways to make loops with a 16bit counter :
* the naive one, which results in smaller code but increased loop overhead (24 * n T-states) and destroys a
* the slightly trickier one, which takes a couple more bytes but has a much lower overhead (12 * n + 14 * (n / 16) T-states)
(This is harder to understand why it works, but is MUCH faster - almost as fast as djnz with an 8 bit counter (and oddly written, such that it uses B and D as loop counters; personally I'd use B and C, I think)
How it works is because if b=0 DJNZ will loop 256 times. So we start with B set to the number of loops past a multiple of 256, and then loop D lots of 256 times. It saves a lot of time over a reasonably large loop - for example, it's about half the overhead, just for something as small as a 1000 loop.
There are quite a few other similar tricks listed. You might find some are handy.
I've been looking at Z80 optimization pages lately.
Under math tricks at http://wikiti.brandonw.net/index.php?tit...timization there are some good ones, including that one. I like the alternatives to using NEG - which is awkward, being an extended instruction:
Code:
;Instead of
neg
add a,N ;you want to calculate N-A
;Do it this way:
cpl
add a,N+1 ;neg is practically equivalent to cpl \ inc a
; -> save 1 byte and 4 T-states
And this one, when you learn it, is solid gold:
Looping with 16 bit counter
There are two ways to make loops with a 16bit counter :
* the naive one, which results in smaller code but increased loop overhead (24 * n T-states) and destroys a
Code:
ld bc, ...
loop:
; loop body here
dec bc
ld a, b
or c
jp nz,loop
* the slightly trickier one, which takes a couple more bytes but has a much lower overhead (12 * n + 14 * (n / 16) T-states)
(This is harder to understand why it works, but is MUCH faster - almost as fast as djnz with an 8 bit counter (and oddly written, such that it uses B and D as loop counters; personally I'd use B and C, I think)
Code:
dec de
ld b, e
inc b
inc d
loop2:
; loop body here
djnz loop2
dec d
jp nz,loop2
How it works is because if b=0 DJNZ will loop 256 times. So we start with B set to the number of loops past a multiple of 256, and then loop D lots of 256 times. It saves a lot of time over a reasonably large loop - for example, it's about half the overhead, just for something as small as a 1000 loop.
There are quite a few other similar tricks listed. You might find some are handy.