WAIT

From C64-Wiki
Jump to: navigation, search
BASIC keyword
Keyword: WAIT
Abbreviation: W Shift+A
Type: Command
Token code: 146/$92
Handling routine
in BASIC ROM:
47149–47176
$B82D–B848
List of all BASIC keywords


Remark: This article describes the BASIC-Command WAIT in BASIC V2 at the Commodore 64.

Type: Command
General Programming-Syntax: WAIT <Memoryadress>,<bit-1>[,<bit-2>]


WAIT is a keyword in the built-in BASIC interpreter, which waits for a given memory location to assume specific values.

Syntax[edit]

WAIT <address>,<mask1>,[mask2]

  • address is a 16-bit unsigned integer designating the address to "wait for"
  • mask1 and mask2 are unsigned bytes, used as parameters for the logic condition to wait for

Any address can be specified from 0 to 65535. Valid values for mask1 and mask2 may range from 0 to 255. Values outside the given ranges result in the BASIC-error ?ILLEGAL QUANTITY ERROR in line

Operation[edit]

WAIT is performed by a loop in ROM address 47166–47175/$B83E–$B847, which first reads the contents of the given address, then does a bit-wise exclusive-or with the mask2 parameter (if given), and finally a bit-wise and with the mask1 parameter. The loop will run for as long as the result of these operations yields a zero byte.

Examples[edit]

WAIT 653,2
This will wait for the Commodore key to be pressed — until that happens, the "least drastic" way out of this, is to press Run/Stop + Restore. The three least significant bits in address 653 turn "on" (1) if Shift keys, the Commodore key or the Ctrl keys are pressed down at the moment; you may change the "2" at the end of the WAIT instruction to a "1" to wait for Shift keys, or a "4" to wait for the Ctrl key to be pressed.

WAIT 203,64
Try this one in direct mode: When pressing the Return key after typing the command, hold the Return key down for a while, and notice how the "Ready." prompt and cursor doesn't reappear before you release. Address 203 holds the keyboard code for the key currently being pressed, or the value 64/$40 if none are pressed.

WAIT 203,64,64
Since the keyboard codes for all keys are less than 64, bit 6 basically indicate that no key is currently pressed. To wait for a key to be pressed, this example uses the extra mask2 parameter to "invert" that bit, and wait for this "inverted" bit to indicate that a key was pressed.
If this example is given in direct mode, be sure to press and release the Return key very rapidly after typing in the command — otherwise the WAIT command will find the Return key still pressed down as it does it's loop the first time.

WAIT 1, 32, 32 
(The C64 wait until a button is pressed on the datassette.)
  WAIT 53273, 6, 6
(The C64 wait until a sprite has got a collosion with a char.)
 
WAIT 36868, 144, 16
(The C64 wait until at the memoryadress 36868 the 4th bit is deleted or
the 7th bit is set.)
 
POKE 198,0: WAIT 198,1
(The C64 wait until a key is pushed on the keyboard.)
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