TIME
| BASIC variable | |
| Variable-Name: | TIME |
| Type: | System variable |
| List of all BASIC keywords | |
Remark: This article describes the BASIC variable TIme in BASIC V2 on the Commodore 64.
Type: Numeric/ system variable General Programming-Syntax: TI
The system variable 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 timer is updated by the system Interrupt Service Routine, not by either CIA's TOD clock.
The time is initialized to 0 at power-on or reset. TI is also affected by the setting of the system variable TI$, as they are based on the same internal timer and represent the same value. TI$ is formatted as a human readable string (see TI$ for details).
The TI variable wraps around -- restarts at 0 -- every 24 hours at midnight. 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, TI resumes counting up from 0.
Because TI can lag during I/O operations and if the system IRQ is disabled, it's not always reliable. For example, when reading or writing to the datasette, the interval-timer is temporarily stopped, and when communicating over the IEC bus, a small number of jiffies will be lost.
The system variable TI is read-only, and cannot be set; any assignment attempts will result in the BASIC error ?SYNTAX ERROR. Variable TI$ can, however, be set and the TI 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(TI/60);" seconds."
Using PEEK for read TI[edit | edit source]
PRINT (PEEK(160)*65536)+(PEEK(161)*256+PEEK(162))
Name resolving[edit | edit source]
Because Commodore 64 BASIC only considers the first two characters of variable names, using TIME resolves to TI, making them interchangeable identifiers. This may provide a convenient mnemonic, but it comes at a very slight performance cost due to the extra characters being parsed during variable name resolution.
Another example:
PRINT TICKS
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 TIME: "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]
- IRQ-Routine (on AAY64), for the computer clock.
- $FFEA/65514 KERNAL start UDTIM for increasing the timer.
- $FFDE/65502 KERNAL start RDTIM for reading the timer.
- $FFDB/65499 KERNAL start SETTIM for setting the timer.
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