Commodore‎ > ‎BASIC‎ > ‎Keywords‎ > ‎


Keyword Abbreviation Token (hex) Version(s) Classification
WAIT W{Shift+A} 92 1.0 to 7.0 Command and Statement

WAIT address , andMask [ , xorMask ]
Parameters Type Legal Value(s) Default Value Note(s)
address Unsigned Integer 0 ~ 65535
andMask Unsigned Byte 0 ~ 255
Specifies which bit(s) should be tested.
Should be > 0 unless you want to wait forever.
xorMask Unsigned Byte  0 ~ 255 
Specifies which bit(s) must be 0.
Bit(s) specfied here will be ignored if not included by andMask.
Bit(s) not specified here but included by andMask must be 1.
Program flow control.  Wait for "memory" at address to become a member of a set of values.

WAIT is a rather obscure statment; you could say in combines the effects of STOP, PEEK, and CONT.  However, the program (assuming one is running) doesn't actually end, it just waits for the "memory" to obtain a desired value before the program continues to the next statement.  WAIT works in direct mode too, although there is no running program to "stop".
The desired value is not directly specified; instead one or two "masks" are specified which actually define a set of possible value(s).  In fact, they may define an empty set, in which case the computer would wait forever!  If the defined set is not empty, the computer will wait until the "memory" conforms to the specified set, which may be forever as well.  To make matters worse, the STOP key is not checked while the computer is waiting for the desired value; so if a programming error is made (wrong address or set definition), or the desired value doesn't occur in a timely fashion, the user must "reset" the computer to regain control.  (Most CBM computers have a Restore key which allows for a soft reset, so this isn't as dramatic as could be.)
For machines with more than 64K of RAM, the currently configured BANK is also used (along with address) to specify the desired byte of memory for testing.
If your computer is operating correctly, the memory will not magically change values, which might make this statement seem quite useless.  However there are background tasks performed by the computer which update some memory locations, and on the 6502 series of CPUs, I/O registers are mapped to memory.  I/O registers typically do change automatically (based on events external to the CPU or memory) so it seems testing of I/O registers is this statement's main purpose.  As you might imagine, the memory locations involved and the value(s) desired are machine-specific, which make this statement very non-portable.
WAIT performs this test on the "memory" address : ( XOR( xorMask, PEEK(address) ) AND andMask ) > 0.  BASIC repeats the test until the result is true.  To understand that, you need to know how AND and XOR operate (hopefully PEEK is obvious).  It is pretty easy to play around with AND in BASIC to get a "feal" for things, but unless you are using v7.0, there is no XOR to help you in design/debugging!  See the individual pages for details, but basically AND isolates bits while XOR toggles bits.
Okay, here is an alternate explanation.  Use the andMask to specify the bits in which you are interested.  If you are only looking for bits to have a value of 1, congradulations, you are done!  To check for a bit to be 0, specify a xorMask to toggle the desired bit(s) into a 1. For example, if you want to wait for a byte of memory to have bit 4 set (value 1) and bit 5 clear (value 0) then the andMask should be 24 + 25 = 16 + 32 = 48.  Because you want bit 5 to be clear, the xorMask should be 25 = 32.  So if the address you wanted to test was 1024, the magic statement would be:
 WAIT 1024, 48, 32
In v2.0 of CBM BASIC (and some other 6502-based BASICs) there is a so-called "Easter Egg" (a hidden feature).  Entering WAIT 6502,1 will print MICROSOFT! at the top of the screen.  For CBM computers, this happens only on the PET's v2 BASIC, but not the VIC-20 nor C64's v2 BASIC, which goes to show those later machines have something other than v2.0 (I call it v2.1).  Interestingly this feature is not in version 1.0.
  Compare With  
  Contrast With  
  See Also  

© H2Obsession, 2014