Page 2 of 3

Re: Waiting some time!

Posted: Thu Aug 28, 2014 6:14 pm
by antoniovillena
Based on DrBeep solution

Code: Select all

        ld      hl, frames
        ld      a, (hl)
        sub     n
repeat  cp      (hl)
        jr      nz, repeat

Re: Waiting some time!

Posted: Fri Aug 29, 2014 10:37 am
by marste
antoniovillena wrote:Based on DrBeep solution

Code: Select all

        ld      hl, frames
        ld      a, (hl)
        sub     n
repeat  cp      (hl)
        jr      nz, repeat
The power of cooperation!!
Every single bit count on 1K machine!! :)

Re: Waiting some time!

Posted: Fri Aug 29, 2014 1:23 pm
by PokeMon
This goes even one byte shorter when timing is not needed very accurate and you can use timers of up to 10 seconds length while the above solution is limited to 5,5 seconds. ;)

Code: Select all

        LD  C,22
.loop:  HALT
        DJNZ $-1
        DEC  C
        JR NZ,.loop
This takes only 8 bytes instead of 9 and gives a one second wait with C=22. It executes one HALT instruction during idle scanlines, which are 55 for the upper and lower border (non visible part of screen), so 2.3 frames are passed for every C count. So one second is 22*2.3=50 frames=1 second. As said not exact but who cares if a wait is executed one second or a tenth longer or shorter. B is not initialized so the time could be 1 frame more or less (+/- 20ms). All in life has a price.

Spares one byte more you need so much. Lets give it a challenge. :mrgreen:

Re: Waiting some time!

Posted: Mon Sep 01, 2014 10:37 pm
by antoniovillena
Another 8 bytes solution if you don't want multiple of frames:

Code: Select all

        ld      b, N
outer   push    bc
inner   djnz    inner
        pop     bc
        djnz    outer
Aprox 7*N^2, with a limit of about 450.000 cycles

Re: Waiting some time!

Posted: Mon Sep 01, 2014 11:35 pm
by 1024MAK
antoniovillena wrote:Another 8 bytes solution if you don't want multiple of frames:

Code: Select all

        ld      b, N
outer   push    bc
inner   djnz    inner
        pop     bc
        djnz    outer
Aprox 7*N^2, with a limit of about 450.000 cycles
Like 8-)

Mark

Re: Waiting some time!

Posted: Wed Sep 03, 2014 10:17 am
by poglad
TMAOne wrote:According to the User Manual,

PAUSE 50 will wait approximately 1 second.
PAUSE 32767 will wait approx. 11 minutes.
PAUSE [anything larger] will wait forever.
The old classic was "PAUSE 4E4" which fulfills the "anything larger" criteria and can (sort of) be pronounced "pause for ever". :lol:

Re: Waiting some time!

Posted: Thu Sep 04, 2014 4:55 pm
by marste
antoniovillena wrote:Another 8 bytes solution if you don't want multiple of frames:

Code: Select all

        ld      b, N
outer   push    bc
inner   djnz    inner
        pop     bc
        djnz    outer
Aprox 7*N^2, with a limit of about 450.000 cycles
If I did proper calculation (the sum of the squares) the inner loop will be executed 5.5 million time with B=255.
Considering that djnz is 3 cycles (2 last one) this means 16.5 millions clock cycles, that in fast mode at 3.5MHz should result in little less than 5 seconds (probably 5 times more in slow mode).
Nice this "optimization" attitude (and challenge participation! ;)), I will need a lot for my 1K little project to work out! :D
Let's see!!

Re: Waiting some time!

Posted: Mon Sep 08, 2014 3:37 pm
by yerzmyey
TMAOne wrote:According to the User Manual,
PAUSE 50 will wait approximately 1 second.

PAUSE 50 is 1 second on ZX Spectrum too.
But there is also a significant difference.
As far as I remember, the PAUSE command on ZX81 stops also the interrupts.

Re: Waiting some time!

Posted: Tue Sep 09, 2014 8:11 pm
by PokeMon
marste wrote:
antoniovillena wrote:Another 8 bytes solution if you don't want multiple of frames:

Code: Select all

        ld      b, N
outer   push    bc
inner   djnz    inner
        pop     bc
        djnz    outer
Aprox 7*N^2, with a limit of about 450.000 cycles
If I did proper calculation (the sum of the squares) the inner loop will be executed 5.5 million time with B=255.
Considering that djnz is 3 cycles (2 last one) this means 16.5 millions clock cycles, that in fast mode at 3.5MHz should result in little less than 5 seconds (probably 5 times more in slow mode).
Nice this "optimization" attitude (and challenge participation! ;)), I will need a lot for my 1K little project to work out! :D
Let's see!!

Your calculations are wrong in this case.
DJNZ uses only register B.
So maximum execution for inner loop is 256*256 = 65536.

If you count clock cylces then DJNZ has 13 clock cycles if jumping, 8 if not.
So the inner loop is 3323 clock cycles plus 21 cycles for PUSH/POP = 3344 cycles = 1ms for 308ns cycle time (@ 3.25 MHz).
So the outer loop is maximum 256ms running time.
I would calculate about 25% running time in slow mode - so this is about 1 second in slow mode.
;)

Re: Waiting some time!

Posted: Wed Sep 10, 2014 4:33 am
by antoniovillena
PokeMon wrote:Your calculations are wrong in this case.
DJNZ uses only register B.
So maximum execution for inner loop is 256*256 = 65536.

If you count clock cylces then DJNZ has 13 clock cycles if jumping, 8 if not.
So the inner loop is 3323 clock cycles plus 21 cycles for PUSH/POP = 3344 cycles = 1ms for 308ns cycle time (@ 3.25 MHz).
So the outer loop is maximum 256ms running time.
I would calculate about 25% running time in slow mode - so this is about 1 second in slow mode.
;)
Inner loop only has 3323 cycles in the first iteration (if you put ld b,0 as maximum count). The rest of the iterations the time is decremented, so it's 256,255,254..3,2,1. Instead 256*256*13 cycles you can aproximate with 256*128*13, that it's the half.