News:

Printed Amstrad Addict magazine announced, check it out here!

Main Menu

Timing

Started by kelp7, 21:13, 03 August 17

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

kelp7

Hi,

Just wondering, on a 2mhz machine that I have, would the following code take 2 seconds to run?



LD BC,&0064
LOOP2:
PUSH BC
LD BC,&4E20H
LOOP:NOP
DEC BC
LD A,B
OR C
JR NZ,LOOP
POP BC
DEC BC
LD A,B
OR C
JR NZ,LOOP2



I think the emulator for the machine is not running at the right speeds but am trying to make some code that would verify that. Was hoping to try and work out how many NOPs I might need to burn to count for 2 seconds..... think I may have got that vastly wrong!!
Thanks!

robcfg

Which machine and what emulators are we talking about?


Sent from my iPhone using Tapatalk

rpalmer

Kelp7,

You can work out how long it takes (in terms of total machines clock counts)  knowing its a Z80 and based on the Z80 CPU manual that documents the instruction timings.

Also you say its 2 MHz meaning the total time can be calculated assuming no interrupts occurred. You can include DI/EI to stop them if you need good timing numbers.

rpalmer

kelp7

Thanks both, yes the MZ-80A is my machine (Z80A processor). The only interrupt that occurs is a 12 hour one to change the realtime clock from AM to PM (or vice versa). But I guess I could include a DI for completeness :) I think I've just got my maths / code extremely wrong (or misunderstood the NOP instruction). I'll carry on working this through, thanks!

Docent

Quote from: kelp7 on 21:13, 03 August 17
Hi,

Just wondering, on a 2mhz machine that I have, would the following code take 2 seconds to run?



LD BC,&0064
LOOP2:
PUSH BC
LD BC,&4E20H
LOOP:NOP
DEC BC
LD A,B
OR C
JR NZ,LOOP
POP BC
DEC BC
LD A,B
OR C
JR NZ,LOOP2



I think the emulator for the machine is not running at the right speeds but am trying to make some code that would verify that. Was hoping to try and work out how many NOPs I might need to burn to count for 2 seconds..... think I may have got that vastly wrong!!
Thanks!

No, it wouldn't.  You need to provide correct loop counter. On 2MHz Z80 cpu 2 seconds of execution require 4 millions clock cycles =~1 million NOPs and you loops through 2 million of NOPs.
You also need to account for the timing of other instructions in the loop. Your whole loop takes 60005205 cycles = 30 secs.
Here's the timing of your code for 2 Mhz Z80 cpu in clock cycles:


#0000 di ;4T          4T
#0001 ld bc,#0064 ;10T         10T
l0004:
#0004 push bc ;11T       1100T
#0005 ld bc,#4e20 ;10T       1000T
l0008:
#0008 nop ;4T    8000000T
#0009 dec bc ;6T   12000000T
#000a ld a,b ;4T    8000000T
#000b or c ;4T    8000000T
#000c jr nz,l0008 ;7/12T   23999500T
#000e pop bc ;10T       1000T
#000f dec bc ;6T        600T
#0010 ld a,b ;4T        400T
#0011 or c ;4T        400T
#0012 jr nz,l0004 ;7/12T       1195T
#0014 ret ;10T          4T

and execution time in microseconds for the same code:
#0000 di ;             2.00ms
#0001 ld bc,#0064 ;             5.00ms
l0004:
#0004 push bc ;           550.00ms
#0005 ld bc,#4e20 ;     500.00ms
l0008:
#0008 nop ; 4000000.00ms
#0009 dec bc ; 6000000.00ms
#000a ld a,b ; 4000000.00ms
#000b or c ; 4000000.00ms
#000c jr nz,l0008 ;      11999750.00ms
#000e pop bc ;     500.00ms
#000f dec bc ;     300.00ms
#0010 ld a,b ;     200.00ms
#0011 or c ;     200.00ms
#0012 jr nz,l0004 ;     597.50ms
#0014 ret ;       2.00ms


To get rough approximation of the timing, you can use the following equation for the loop counter: loopcounter=delay in microseconds/15
In your case, the loop should run 133333 times to get 2 sec delay.


kelp7

Sorry, only just saw your response. Thanks very much for the excellent explanation.

Powered by SMFPacks Menu Editor Mod