Qbasicnews.com
February 22, 2020, 07:03:53 PM
 Pages: [1] 2
 Author Topic: Clock & Timer Program Question  (Read 6597 times)
John Monti/
Member

Posts: 83

 « on: September 10, 2004, 10:32:32 AM »

The highlighted red part gives me an Overflow every time I run this program now.

Any idea why?

'to test it:
CLS
DO
timerVal! = TIMER
clockVal! = CLOCK / 4660.859#

LOCATE 1
PRINT USING "CLOCK: #####.###"; clockVal!
PRINT USING "TIMER: #####.###"; timerVal!
LOOP UNTIL LEN(INKEY\$)

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
[/quote]
 Logged

I'm your Huckleberry; that's just my game."

Doc Holiday, TOMBSTONE
Z!re
*/-\*

Posts: 4599

 « Reply #1 on: September 10, 2004, 10:53:54 AM »

Because 256 fits as an integer value, and QB assumes integer calculation, creating an overflow.

Fix it by adding an & to the end of 256:
Code:
CLS
DO
timerVal! = TIMER
clockVal! = CLOCK / 4660.859#

LOCATE 1
PRINT USING "CLOCK: #####.###"; clockVal!
PRINT USING "TIMER: #####.###"; timerVal!
LOOP UNTIL LEN(INKEY\$)

FUNCTION CLOCK&

' Get the number of timer ticks at
' 0000:046C
DEF SEG = 0
Ticks& = PEEK(&H46C)

Ticks& = Ticks& + PEEK(&H46D) * 256& 'force long math

Ticks& = Ticks& + PEEK(&H46E) * 65536& 'force long math
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& = HSB + Ticks& * 256&  'force long math
END FUNCTION

To make your code appear in a code block as above, use the [ code ] and [ /code ] tags, (without the spaces)
 Logged
John Monti/
Member

Posts: 83

 « Reply #2 on: September 10, 2004, 11:09:17 AM »

Ok, I'll put that "&" next to the 256.  Thanks.

Must have deleted it somehow or it got left out when I copied it from this site.
 Logged

I'm your Huckleberry; that's just my game."

Doc Holiday, TOMBSTONE
Jofers
Been there, done that

Posts: 1040

 « Reply #3 on: September 10, 2004, 01:34:16 PM »

I wish someone would update that part of the faq.  This isn't the first time someone's gotten confused.
 Logged
TheBigBasicQ
*/-\*

Posts: 4550

 « Reply #4 on: September 10, 2004, 02:00:06 PM »

u used quote instead or code that to improperly.

Quote from: "John Monti/"

CLOCK& = Ticks& * 256 + HSB

I think you will need to modify this statement to read as:

[script="qb"]CLOCK& = Ticks& * 256& + HSB&[/script]

though i am not sure whether HSB will need a &
 Logged
Z!re
*/-\*

Posts: 4599

 « Reply #5 on: September 10, 2004, 02:16:10 PM »

 Logged
whitetiger0990
__/--\__

Posts: 2964

 « Reply #6 on: September 10, 2004, 11:13:16 PM »

I think he meant the Syntax one.
[syntax="QBASIC" ]Print "Code goes here"[/syntax] (w/o spaces)

that would give you

[syntax="QBASIC"]Print "Code goes here"[/syntax]
 Logged

[size=10]Back by popular demand!
I will byte and nibble you bit by bit until nothing remains but crumbs.[/size]
oracle
*/-\*

Posts: 3652

 « Reply #7 on: September 11, 2004, 02:26:36 AM »

Quote from: "Jofers"
I wish someone would update that part of the faq.  This isn't the first time someone's gotten confused.

Done. Don't you have wiki access?
 Logged

Jofers
Been there, done that

Posts: 1040

 « Reply #8 on: September 11, 2004, 12:14:49 PM »

nah. or else I would've done it myself.

There are a few concerns before using that routine, mainly that the tick can be up to 1/18.2th of a second off under Windows, according to Antoni.  It's perfectly accurate under DOS, though.

Man, I wish someone still had those Windows interrupt routines.
 Logged
Moneo
Na_th_an

Posts: 1971

 « Reply #9 on: September 15, 2004, 11:25:13 PM »

John Monti,
What are you doing that requires PEEKS and getting the TICKS of the clock? Are you just fooling around or do you have some real application for this time logic that needs this fine granularity?
*****
 Logged
Jofers
Been there, done that

Posts: 1040

 « Reply #10 on: September 15, 2004, 11:39:40 PM »

That code refreshes the timer more quickly.  And lots of programs do, the TIMER only allows about 18 frames a second.

Does ANYONE have that library that used a Windows virtual device to time in milliseconds?  I forget who released it, but his first name was Robert (and there were a few people by that name).
 Logged
Plasma
Na_th_an

Posts: 1770

 « Reply #11 on: September 16, 2004, 01:21:27 AM »

Never heard of that one...

If you want a fast and accurate timer without reprogramming the PIT, you can hook int 70h (BIOS RTC). By default it gets triggered 1024 times/sec, and is compatible with XP. (If you need even more precise timing, you can reprogram the RTC to trigger the interrupt at up to 8192 times/sec. That's 122 microseconds.)

Of course it requires a little assembly, but all the "good stuff" does.
 Logged
John Monti/
Member

Posts: 83

 « Reply #12 on: September 16, 2004, 09:50:27 AM »

Sounds like something I can use but I'll need some help implementing it into my program after I have it.  Where can I get it anyways?  or should I say; How do I emplement it?

Obviously I mangaged to get myself thinking I needed a faster Frame Rate but what I really needed was a better timer since that is what I want to program.  That is... a Time based game and having things happen at certain times.  I seem to have worked out animations without a real Frame Per Second Program.  It's more of a hashed, mix-mashed version of something strange that works for me right now.

The frame per second thing really doesn't concern me all that much because I've somehow done something else that seems to work but it probabaly required more code; oh well.
 Logged

I'm your Huckleberry; that's just my game."

Doc Holiday, TOMBSTONE
John Monti/
Member

Posts: 83

 « Reply #13 on: September 16, 2004, 09:58:10 AM »

Here is the program that I need to get timed just the way "I Think" should be the proper speed for it to run.

In other words, I might need some help in understanding how to run whatever you are going to give me for a better timer.

Code:

' Initializing Box Array
z = 0
FOR y = 0 TO 176 STEP 16
FOR x = 3 TO 18
z = z + 1
Box(x, y / 16) = z
NEXT x
NEXT y

z = 0

' Now randomly place on screen until it is covered
' But not too fast and not too slow
' This doesn't do that yet
DO

j = INT(RND * 19)
k = INT(RND * 12)

SELECT CASE j
CASE 1
j = j + 2
CASE 2
j = j + 1
END SELECT

IF Box(j, k) <> 0 THEN
z = z + 1
Box(j, k) = 0
PUT (j * 16, k * 16), Blue.Tag, PSET
END IF
i\$ = INKEY\$
LOOP UNTIL z = 192
 Logged

I'm your Huckleberry; that's just my game."

Doc Holiday, TOMBSTONE
Jofers
Been there, done that

Posts: 1040

 « Reply #14 on: September 16, 2004, 10:13:23 AM »