Joshy
Code: Select all
private function ReadTSC as longint
asm RDTSC
asm mov [function+0],eax
asm mov [function+4],edx
end function
dim shared as ulongint TPC ' ticks per command
dim shared as ulongint TPS ' ticks per second
dim shared as ulongint TPM ' ticks per milli second mS
dim shared as ulongint TPU ' ticks per micro second uS
sub calibrate
dim as ulongint t1,t2,t
'print "calibrate time step counter"
ReadTSC()
ReadTSC()
ReadTSC()
for i as integer=1 to 1000
t1 = ReadTSC()
t2 = ReadTSC()
t+= (t2-t1)
next
TPC=t\2000 ' ticks per one ReadTSC()
t=0
for i as integer=1 to 10
t1 = ReadTSC()
sleep 100,1
t2 = ReadTSC()
t+= (t2-t1)
next
t-=TPC*20 ' subtract time of 20 calls to ReadTSC
t\=10 ' ticks per 100 mS
TPS = t*10 ' ticks per second
TPM = TPS \ 1000 ' ticks per mS
TPU = TPS \ 1000000 ' ticks per uS
'print "TPC command = " & TPC
'print "TPS second = " & TPS
'print "TPM milli sec. = " & TPM
'print "TPU micro sec. = " & TPU
end sub
sub WaitTimer(byval WaitCount as longint)
static as longint oldTickCount=0
if oldTickCount=0 then
calibrate
oldTickCount=ReadTSC()
end if
WaitCount*=TPM
OldTickCount+=WaitCount
dim as longint TickCount=ReadTSC()
if (TickCount > OldTickCount) then
OldTickCount = TickCount
end if
dim as longint Dif = OldTickCount - TickCount
if dif<=0 then return
while dif > 0
if dif>(TPM*15) then sleep 15,1
TickCount=ReadTSC()
dif = OldTickCount - TickCount
wend
OldTickCount=TickCount
end sub
' short test
dim as integer xPos=-100
dim as integer bpp
screeninfo ,,bpp
screenres 800,600,bpp
while inkey=""
ScreenLock:cls
circle (xpos,300),100,RGB(255,0,0),,,,F
ScreenUnlock
xpos+=1:if xpos>900 then xpos=-100
WaitTimer(20) ' 50 Hz
wend