Waiting some time!

Any discussions related to the creation of new hardware or software for the ZX80 or ZX81
antoniovillena
Posts: 23
Joined: Fri Aug 15, 2014 5:48 pm

Re: Waiting some time!

Post by antoniovillena »

Based on DrBeep solution

Code: Select all

        ld      hl, frames
        ld      a, (hl)
        sub     n
repeat  cp      (hl)
        jr      nz, repeat
User avatar
marste
Posts: 179
Joined: Sun Aug 10, 2014 9:58 pm
Location: Italy
Contact:

Re: Waiting some time!

Post 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!! :)
User avatar
PokeMon
Posts: 2253
Joined: Sat Sep 17, 2011 6:48 pm

Re: Waiting some time!

Post 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:
antoniovillena
Posts: 23
Joined: Fri Aug 15, 2014 5:48 pm

Re: Waiting some time!

Post 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
User avatar
1024MAK
Posts: 3462
Joined: Mon Sep 26, 2011 10:56 am
Location: Looking forward to summer in Somerset, UK...

Re: Waiting some time!

Post 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
poglad
Posts: 133
Joined: Mon Mar 24, 2014 3:11 pm
Location: Aberdeen, Scotland

Re: Waiting some time!

Post 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:
User avatar
marste
Posts: 179
Joined: Sun Aug 10, 2014 9:58 pm
Location: Italy
Contact:

Re: Waiting some time!

Post 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!!
User avatar
yerzmyey
Posts: 1240
Joined: Thu May 15, 2008 10:11 am
Location: Rubber Planet
Contact:

Re: Waiting some time!

Post 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.
IN NIHILUM REVERTERIS - a big text-adventure game for ZX81: http://tiny.pl/g2m6m
"MONOCHROME" issue 5 - (Spring 2014) free paper/PDF magazine about ZX81: http://tiny.pl/q2m44
ZX81 COMPETITIONS 2007/2009: http://zx81.republika.pl/
User avatar
PokeMon
Posts: 2253
Joined: Sat Sep 17, 2011 6:48 pm

Re: Waiting some time!

Post 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.
;)
antoniovillena
Posts: 23
Joined: Fri Aug 15, 2014 5:48 pm

Re: Waiting some time!

Post 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.
Post Reply