SLEEP

From C64-Wiki
Jump to navigationJump to search
BASIC keyword
Keyword: SLEEP
Abbreviation: S SHIFT+L
Type: Command
Token code: 254/$FE 12/$0B
Handling routine
in BASIC ROM:
List of all BASIC keywords


Remark: This article describes the BASIC command SLEEP in Commodore BASIC V7.0 or higher.

Type: Command
General Programming-Syntax: SLEEP integer

The BASIC command SLEEP stops BASIC program execution for a specified amount of seconds according to the integer parameter. The argument might range from 0 65535. The running mode SLOW/FAST does not influence the timing behavior.

If the value exceeds the previous mentioned range this leads to the error ?ILLEGAL QUANTITY ERROR. Without an argument or more than one argument rises a ?SYNTAX ERROR.

Hints:

  • Even in FAST mode SLEEP works as expected!
  • With SLEEP 0 the system waits for the next interrupt. The delay lies in the range from 0 to 16,7 milliseconds (on a NTSC system). Because the timing on the C128 is based on the system's raster interrupt, SLEEP 0 provides a possibility to synchronize with VIC screen frame cycle.

Examples[edit | edit source]

100 PRINT "WAIT A MINUTE ...";
110 SLEEP 60
120 PRINT "TIME PASSED!"

Wait a minute without the commonly seen CPU-based FOR-NEXT loop.

200 TA=TI
210 FOR I=1 TO 100
220 SLEEP 0
230 NEXT I
240 PRINT (TI-TA)/60

The run-time measured and shown at the end is expected to be 2 (for PAL systems) or 1.67 (for NTSC). This corresponds to 100 VIC frames (with the frame frequency of 50 Hz for PAL und 60 Hz for NTSC).

100 DO:SLEEP0:COLOR4,2:COLOR4,1:LOOP

On each frame the color is changed twice without distortions (except some key is pressed).


SLEEP for BASIC 2.0


With BASIC V2 a SLEEP function could be realized as follows (in a subroutine at line 10):

  1 GOTO 100
 10 T0=TI: FOR T=T0+T*60 TO T: T=TI: T=T-5184E3*(T<T0): NEXT: RETURN
100 T=5: GOSUB 10: PRINT "TIME PASSED!"

The assignment T=5 passes the delay time to the subroutine in line 10. It might also contain a fractional value.
This implementation considers also the TI timer wrap-around after 24 h at 5184000 (60 ticks/second × 86400 seconds/day).
The expression (T<T0) gives -1 if true, which leads to the addition of the 24h value to ensure that variable T reaches the end value, even TI wrapped back to 0.

This example uses the CBM BASIC interpreter's peculiarity, that the FOR-NEXT loop body is always executed even the loop-variable has already the end-value.


BASIC V7.0 Commands

ABS | AND | APPEND | ASC | ATN | AUTO | BACKUP | BANK | BEGIN | BEND | BLOAD | BOOT | BOX | BSAVE | BUMP | CATALOG | CHAR | CHR$ | CIRCLE | CLOSE | CLR | CMD | COLLECT | COLLISION | COLOR | CONCAT | CONT | COPY | COS | DATA | DCLEAR | DCLOSE | DEC | DEF FN | DELETE | DIM | DIRECTORY | DLOAD | DO | DOPEN | DRAW | DS | DS$ | DSAVE | DVERIFY | EL | ELSE | END | ENVELOPE | ER | ERR$ | EXIT | EXP | FAST | FETCH | FILTER | FN | FOR | FRE | GET | GET# | GETKEY | GO64 | GOSUB | GOTO | GRAPHIC | GSHAPE | HEADER | HELP | HEX$ | IF | INPUT | INPUT# | INSTR | INT | JOY | KEY | LEFT$ | LEN | LET | LIST | LOAD | LOCATE | LOG | LOOP | MID$ | MONITOR | MOVSPR | NEW | NEXT | NOT | (OFF) | ON | OPEN | OR | PAINT | PEEK | PEN | (PI) | PLAY | POINTER | POKE | POS | POT | PRINT | PRINT USING | PRINT# | PUDEF | (QUIT) | RCLR | RDOT | READ | RECORD | REM | RENAME | RENUMBER | RESTORE | RESUME | RETURN | RGR | RIGHT$ | RND | RREG | RSPCOLOR | RSPPOS | RSPRITE | RUN | RWINDOW | SAVE | SCALE | SCNCLR | SCRATCH | SGN | SIN | SLEEP | SLOW | SOUND | SPC( | SPRCOLOR | SPRDEF | SPRITE | SPRSAV | SQR | SSHAPE | ST | STASH | STEP | STOP | STR$ | SWAP | SYS | TAB( | TAN | TEMPO | THEN | TI | TI$ | TO | TRAP | TROFF | TRON | USR | VAL | VERIFY | VOL | WAIT | WHILE | WINDOW | WIDTH | XOR