Hi,
I want to try to calculate how much time (and cycles) a routine takes. This is the code I have implemented but I'm seeing some odd behaviour with it (sorry if the code is a bit messy but mostly for testing and just figuring things out)
include "exec/types.i"
include "exec/io.i"
include "lvo/exec_lib.i"
include "lvo/dos_lib.i"
include "lvo/timer_lib.i"
section code, code
start
lea _dosname,a1
move.l $4.w,a6
jsr _LVOOpenLibrary(a6)
tst.l d0
beq exit
move.l d0,dos_handle
; ==============================
; Open timer.device
lea timer_device_name,a0
lea timer_device,a1
moveq #0,d0
moveq #0,d1
jsr _LVOOpenDevice(a6)
tst.l d0
bne .end
lea timer_device,a1
move.l IO_DEVICE(a1),a6
moveq #8-1,d4
.calc_loop
lea time_output,a0
jsr _LVOReadEClock(a6)
; Code to time goes here...
move.w #40000,d2
moveq #1,d3
.test_loop
nop
dbra d2,.test_loop
; Code to time ends here...
lea time_output2,a0
jsr _LVOReadEClock(a6)
lea format_data,a1
lea time_output,a0
move.l 4(a0),(a1)
jsr print_timing
lea format_data,a1
lea time_output2,a2
move.l 4(a2),(a1)
jsr print_timing
lea time_output,a0
lea format_data,a1
lea time_output2,a2
move.l 4(a2),d0
sub.l 4(a0),d0
move.l d0,(a1)
jsr print_timing
dbra d4,.calc_loop
lea timer_device,a1
move.l $4.w,a6
jsr _LVOCloseDevice(a6)
.end
move.l dos_handle,a1
move.l $4.w,a6
jsr _LVOCloseLibrary(a6)
exit:
rts
print_timing
move.l dos_handle,a6
move.l #format_msg,d1
move.l #format_data,d2
jsr _LVOVPrintf(a6)
rts
section data, data
format_msg dc.b 'time %ld\n',0
align 4
format_data dc.l 10
timer_device_name dc.b 'timer.device', 0
_dosname dc.b "dos.library",0
section bss, bss
time_output ds.b 8
time_output2 ds.b 8
timer_device ds.b 512
dos_handle ds.b 4
So the way this code works is quite straightforward as it opens dos.library (for printing stuff) and then timer.device and user ReadEClock for timing.
The routine is looped 8 times to mesure differences in the timing. And then grabs the lower 32-bit value from the timing output (which is in microseconds afaik) so far so good.
Now if I run the code I get:
time 1099754360
time 1099760305
time 5945
time 1099754360
time 1099760305
time 5945
time 1099754360
time 1099760305
time 5945
time 1099754360
time 1099760305
time 5945
...
Still looks good but if I run it again:
time 1342377417
time 1342384176
time 6759
time 1342377417
time 1342384176
time 6759
time 1342377417
time 1342384176
time 6759
time 1342377417
time 1342384176
time 6759
...
so the time here is quite different from before and the timing is consistent also. So my questions:
1. Is the timer.device/ReadEClock the best way to calculate the time?
2. What is the cause of the unbalanced timing? (is it caused by the OS still running and might "interfere" with the timing or is the timer just crap? :)
3. When I got the actual time (in microseconds) how do I calculate an ~approx cycle count for the routine given a specific MHz that is (say 060/55Mhz)
Thanks for any help,
/Nemas