Home
CBM
  ASCII-X
  BASIC
    Disk Commands
    Enter RUN mode
    Program Format
    Secret Variables
    Variable Format
    Expressions
    Keywords
      (divide)
      (equal)
      (less)
      (minus)
      (more)
      (multiply)
      (plus)
      (power)
      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
      Delete
      Dim
      Directory
      Dispose
      Dload
      Do
      Dopen
      Draw
      Ds
      Ds string
      Dsave
      Dverify
      El
      Else
      End
      Envelope
      Er
      Err
      Exit
      Exp
      Fast
      Fetch
      Filter
      Fn
      For
      Fre
      Get
      Get num
      Getkey
      Go
      Gosub
      Goto
      Graphic
      Gshape
      Header
      Help
      Hex
      If
      Input
      Input num
      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 num
      Pudef
      Quit
      Rclr
      Rdot
      Read
      Record
      Rem
      Rename
      Renumber
      Restore
      Resume
      Return
      Rgr
      Right
      Rlum
      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 string
      To
      Trap
      Troff
      Tron
      Until
      Using
      Usr
      Val
      Verify
      Vol
      Wait
      While
      Width
      Window
      Xor
    Syntax
    Tokens
  C128
  D64plus
  Disk
  Escape Codes
  Hardware
  PCxface
  PETSCII
  Pet2asc
Futurama
IBM PC-AT
Contact
Games
Glossary
Hall of fame
Hall of shame
Miscellaneous
Privacy policy
Programming
Twisty puzzles
KeywordAbbreviationToken (hex)Version(s)Classification
WAITW{Shift+A}921.0 to 7.0Command and Statement

 Syntax 
WAIT address , andMask [ , xorMask ]
 
ParametersTypeLegal Value(s)Default ValueNote(s)
addressUnsigned Integer0 ~ 65535
andMaskUnsigned Byte0 ~ 255
Specifies which bit(s) should be tested.
Should be > 0 unless you want to wait forever.
xorMaskUnsigned 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.
 
 Purpose 
Program flow control.  Wait for "memory" at address to become a member of a set of values.

 
 Remarks 
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