TIME

From C64-Wiki
(Redirected from TI)
Jump to navigationJump to search
BASIC variable
Variable-Name: TIME
Type: System variable
List of all BASIC keywords


Remark: This article describes the BASIC command TIME in BASIC V2 on the Commodore 64.

Type: Numeric system variable 
General Programming-Syntax: TIME or TI

The system variable TIME (short: TI) is an interval timer counting "jiffies" (1/60 second increments). It is normally incremented during the standard 1/60th-second CIA1-generated IRQ service routine (typically by means of the Kernal UDTIM routine). This "manually" operated timer has nothing to do with either CIA's TOD clock.
Note that TIME follows just the expanded variable name scheme and therefore any character suitable as variable name (letter, digit) after TI is silently ignored, for example TICKAMASALA will still refer to TI. The common usage how it can be found in program listing is the short variant TI.

The time is initialized to 0 at power-on or reset. TI is also affected by the setting of the system variable TIME$, as they are actually based on the same internal timer and carry the same time just in different representations (see TIME$ for details).

TIME variable wraps around (restarts at 0) every 24 hours at midnight, meaning 1 second after TIME$ reaches the value "23:59:59". Just before that moment, TIME will reach its maximum value: 5183999 (=24*60^3 -1), equivalent to 24 hours, measured in jiffies. After wrapping around, TIME continues counting up from 0 again.

TIME is not completely accurate, because it can fall behind during I/O operations, and any time the system IRQ is disabled. When reading or writing to the datasette, the interval-timer is temporarily stopped. When communicating over the IEC bus, a small number of jiffies will be lost.

The system variable TIME is read-only, and cannot be set. An attempt to assign to TIME will result in the BASIC error ?SYNTAX ERROR. Variable TIME$ can however be set and the TIME variable will follow. TI$="000000" will set TI to 0, TI$="000001" sets TI to 60 and so on.

Examples[edit | edit source]

Simple usage[edit | edit source]

PRINT "Your Commodore 64 has been running for about";INT(TIME/60);" seconds." 

Using PEEK for read TIME[edit | edit source]

PRINT (PEEK(160)*65536)+(PEEK(161)*256+PEEK(162))

Name resolving[edit | edit source]

PRINT TICKS

Another example for TI.

PRINT TICKTOCK

This is a buggy code with an embedded token TO. This BASIC code prints out the following on the screen (the syntax error appears delayed, because TICK is a valid variable name, but the token appears unexpected):

43580
?SYNTAX ERROR
READY.

Time delay[edit | edit source]

A delay subroutine in line 900 taking the contents of variable T as parameter for the amount of time to wait:

100 PRINT "START "TI$": T=2.5: GOSUB 900: PRINT "END TIME: "TI$
199 END

900 T0=TI: FOR T=T0+T*60 TO T: T=TI:T=T-5184E3*(T<T0): NEXT: RETURN

The elapsed time in seconds can be retrieved by using the expression (T-T0)/60.
The end value for the FOR-NEXT construction is derived from variable T, which is previously set to the final time value during the initial assignment for the start value.
Expression -5184E3*(T<T0) compensates for the wrap-around on the 24 hour tick count (60 ticks/seconds * 86400 seconds/day). (T<T0) evaluates to -1 if true.

Links[edit | edit source]


BASIC V2.0 (second release) Commands

ABS | AND | ASC | ATN | CHR$ | CLOSE | CLR | CMD | CONT | COS | DATA | DEF | DIM | END | EXP | FN | FOR | FRE | GET | GET# | GOSUB | GOTO | IF | INPUT | INPUT# | INT | LEFT$ | LEN | LET | LIST | LOAD | LOG | MID$ | NEW | NEXT | NOT | ON | OPEN | OR | PEEK | π | POKE | POS | PRINT | PRINT# | READ | REM | RESTORE | RETURN | RIGHT$ | RND | RUN | SAVE | SGN | SIN | SPC | SQR | STATUS/ST | STEP | STOP | STR$ | SYS | TAB | TAN | THEN | TIME/TI | TIME$/TI$ | TO | USR | VAL | VERIFY | WAIT