Qbasicnews.com
May 31, 2020, 05:42:23 PM
 Pages: [1]
 Author Topic: Clock speed?  (Read 3018 times)
Ralph
Ancient Guru

Posts: 544

 « on: January 24, 2007, 06:26:28 PM »

I was just thinking that, if a program can detect the computer speed for the computer it is running on, we could just use that value as part of our short program pauses, so that the pause time would be the same length of real time, no matter the particular speed of the computer in use.  For instance, if my computer clock speed = 3GHx, I could write use it for a program, like this:
Code:

CLS
SCREEN 13
'LIGHTNING
CS = 3                          'clock speed, in MHz
T = 30000*CS                   'a short time
bg = 0                          'clock speed, in MHz

FOR I= 1 TO 20
LINE (0,0)-(319.199),15,BF   'show the screen as bright white
FOR J = 1 TO T:NEXT J        'short pause
LINE(0,0)-(319,199),bg,BF    'show the screen with the bg color
FOR J = 1 TO T:NEXT J        'short pause
NEXT I

So, is it possible - and fairly easy - to detect the computer's clock speed?  If so, how?
 Logged

Ralph, using QuickBASIC 4.5 and Windows XP Home Edition and Service Pack 2, with HP LaserJet 4L printer.
DrV
Na_th_an

Posts: 1553

 « Reply #1 on: January 24, 2007, 10:23:28 PM »

Clock speed is not really a good thing to base delays on, for various reasons, the most important being that you can't depend on your program getting the CPU all of the time (unless you're running on DOS).  If you need to wait for a particular amount of real time, just check TIMER in a loop.
 Logged
Ralph
Ancient Guru

Posts: 544

 « Reply #2 on: January 25, 2007, 12:10:10 AM »

DrV:

The best I can get with TIMER is something like 0.56 or so of a second.  I can't get less!  I'll give it a try, though.

(Pause, to give it a try)

I'm back.  Nope, it didn't work good enough for my lightning program, using this code:
Code:

T1 = 0.00000005
T0 = TIMER
WHILE TIMER-0 < T1:WEND

I started with T1 = 0.5, and worked my self all the way down to
T1 = 0.00000005.  Am I doing it right?
 Logged

Ralph, using QuickBASIC 4.5 and Windows XP Home Edition and Service Pack 2, with HP LaserJet 4L printer.
nkk_kan
Member

Posts: 73

 « Reply #3 on: January 25, 2007, 10:32:26 AM »

But does that count clock speed?

Don't u need to use an ASM Interuppt to find it out?
 Logged

\__/)
(='.'=) Copy bunny into your signature to
(")_(") help him gain world domination.
DrV
Na_th_an

Posts: 1553

 « Reply #4 on: January 25, 2007, 02:32:41 PM »

You might be interested in these articles: http://faq.qbasicnews.com/?blast=TimerTopics
 Logged
Ralph
Ancient Guru

Posts: 544

 « Reply #5 on: January 25, 2007, 05:46:44 PM »

I tried the CLOCK function from the link in DrV's post, with the code below. My part is the "to test it:" part, where I'm trying to flash the screen to show lightning.  It does a very poor job, as the timing I get seems very inconsistent!
Code:

DECLARE FUNCTION CLOCK& ()

'to test it:
SCREEN 13
CLS
t1 = .02
t2 = 2 * t1

FOR I = 1 TO 1220
clockVal! = CLOCK / 4660.859#
LINE (0, 0)-(319, 199), 15, BF
WHILE (TIMER - clockVal!) < t1: WEND
LINE (0, 0)-(319, 199), 0, BF
WHILE (TIMER - clockVal!) < t2: WEND
NEXT I

FUNCTION CLOCK&

' Get the number of timer ticks at
' 0000:046C
DEF SEG = 0
Ticks& = PEEK(&H46C)
Ticks& = Ticks& + PEEK(&H46D) * 256&
Ticks& = Ticks& + PEEK(&H46E) * 65536
DEF SEG

' Latch the counter and obtain the PIT
' countdown status.
OUT &H43, &H4
LSB = INP(&H40)
HSB = 255 - INP(&H40)

' Compute the CLOCK& value
CLOCK& = Ticks& * 256& + HSB

END FUNCTION
 Logged

Ralph, using QuickBASIC 4.5 and Windows XP Home Edition and Service Pack 2, with HP LaserJet 4L printer.
Dinosaur
Member

Posts: 67

 « Reply #6 on: January 25, 2007, 08:10:47 PM »

Ralph

The only way to use timer tick accurately is to get the interupt to update your variable each time the interupt happens. I use a scheme as below to accurately time events, without creating a lot of overhead.

Code:

'---------------------------------
TYPE TIMES
Clk AS INTEGER
Divider AS INTEGER
Speed AS INTEGER
Increment AS LONG
Msec AS LONG
NowTime AS LONG
KeyTime                   AS LONG
PassCount AS           LONG
StartTime                AS           LONG
WaitState                AS LONG
RunTime                  AS            LONG
OldTime                  AS            LONG
ShowTime              AS            LONG
Hours                     AS           INTEGER
Minutes                 AS            INTEGER
Seconds                 AS            INTEGER
Tyd AS STRING * 7
END TYPE
COMMON SHARED TIMES AS TIMES
'---------------------------------
DECLARE FUNCTION TsrTime& (byval a&,byval B&)

'First Hook the Interupt so that everytime the clock tick increments,
'your variable increments.(If you want the asm routine that I can send it)

A& = TsrTime& (byval varptr(Times.NowTime),byval varseg(Times.NowTime))

'I write my programs so that I never hang around or wait for a delay.
'Then at the beginning there is a main loop

DO
TimeShow
'call whatever else here,

LOOP

'So if you need an accurate delay.
Times.StartTime = Times.Msec
IF Times.Msec - Times.StartTime > 12 THEN ......

'You can also use it to see how long your loop takes
'by printing Times.Increment on the screen

SUB TimeShow
Times.PassCount  = Times.PassCount  + 1 'increment the program loops
Times.Msec = clng((Times.NowTime * 54.9254) + (Times.Increment \ 1000 )) '
'-----------------------------------------------
IF Times.NowTime > Times.OldTime  THEN 'if a tick has happened ,recalculate
Times.Increment  = (54.9254 / Times.PassCount) * 1000 '55 msec / Nr of Passes = msec per pass
Times.OldTime    = Times.NowTime
Times.PassCount  = 0
END IF
END SUB
 Logged

t is the End result that matters, not the Tools used to get there.
Ralph
Ancient Guru

Posts: 544

 « Reply #7 on: January 26, 2007, 01:29:31 AM »

Thanks to all that suggested different methods of obtaining the desired time delay for values much less than 1 second.  As I am getting over my head, I will stay with my present method, which seems "good enough", even though far from perfect.  You know, the old KISS principle, which I had forgotten to apply!
 Logged

Ralph, using QuickBASIC 4.5 and Windows XP Home Edition and Service Pack 2, with HP LaserJet 4L printer.
 Pages: [1]