Commodore Banner Exchange

# The Commodore 64 BASIC WAIT statement explained

Commodore BASIC WAIT statement is maybe the most difficult BASIC statement to understand. It’s very difficult to explain for sure, but I’ll do my best using a hardware approach.

The WAIT statement waits until some bits change on a given location. It can do three tasks at your choice:

1. wait until some bits of the value contained on a given location are 1, or on;
2. wait until some bits of the value contained on a given location are 0, or off;
3. wait until some bits of the value contained on a given location are 1 or on, or some other bits of that value are 0 or off.

So you can see WAIT as three statements in one.

Task number 1 is performed when the statement is given in the form:

WAIT location, number

The statement will wait until at least one of some bits in the content of location will turn on. Which bits? Just those that are set to 1 in number.

Or, which is the same, the wait statement will keep waiting as long as all of the bits that are set to 1 in number keep being turned off in the content of location.

In the first part of this article you can see how to use WAIT in this form. The statement used there:

WAIT 53265,128

just means: wait until bit 7 in location 53265 is set to 1.

We can say this: WAIT performs a logical AND between number and the content of location. It waits until the result of this logical operation is different than 0. Whenever it stays 0, it keeps waiting.

Task number 2 is performed when the statement has two operands, both the same number:

WAIT location, number1, number2

So, number 1 = number 2.

This statement will wait until at least one of the bits that are set to 1 in number 1 (and number 2 of course) turns off in the content of location.

Or, which is the same, the statement will wait as long as all the bits that are set to 1 in number1 (and number 2 of course) keep being set to 1 in the content of location.

So, the statement:

WAIT 53265,128,128

it’s actually the opposite of WAIT 53265,128: it waits until bit 7 of location 53265 becomes set to 0 or off. That happens when the rasterbeam is on the viewable area of the screen, outside the border.

In the last program example of this article you can see how to use the WAIT statement in this form.

Task number 3 is performed when the WAIT statement is given in the form:

WAIT location, number1, number2

and number1 is different from number 2.

The behaviour is a mix of task 1 and 2.

• The wait statement will stop waiting if at least one of the bits that are set to 1 in number1 ONLY become set to 1 also in the content of location.
• Or, the wait statement will stop waiting if at least one of the bits that are set to 1 in both number1 and number2 will turn off in the content of location.

As a consequence of the previous facts, we can also say the following.

• The wait statement will keep waiting if all the bits that are set to 1 in number 1 only, keep staying turned OFF,
• AND, if all the bits that are set to 1 in both number 1 and number 2 will keep staying turned ON.

To explain task 3 behaviour, let’s take the following example:

WAIT 56320, 15, 9

9 contains bit 3 (2^3 = 8) and bit 0 (2^0 = 1). 15 also contains these bits. So, bit 3 and 0 are both set in number 1 and number 2.

15-9 = 6. The number 6 contains bit 2 (2^2 = 4) and bit 1 (2^1 = 2). So, bit 2 and bit 1 are only set in number 1.

So, when the statement will keep waiting?

• As long as both bit 3 and bit 0 keep being turned ON in the content of location 56320,
• AND, as long as both bit 2 and bit 1 keep being turned OFF in the content of location 56320.

And when the wait statement will stop waiting?

• When either bit 3 or bit 0 turn OFF in the content of location 56320.
• Or, when either bit 2 or bit 1 turn ON in the content of location 56320.

56320 reads the joystick in port 2. Bit 0 is up, bit 3 is right, bit 2 is left, bit 1 is down.

When no joystick direction is selected, the respective bits are on. When a joystick direction is selected, its bit turns off. Yes, the logic is reverted in reading joysticks.

To test this program, you need a joystick with indipendent direction buttons, so that you can press even all four direction contacts at the same time. You may open a joystick and press the contacts directly on the joystick board to accomplish this. Be sure not to make any damage to your joystick.

PC keyboards are not predictable on all multiple keystrokes, so an emulated joystick via keyboard on VICE won’t perfectly work.

Try tu run the following program:

2 print “hit a key when ready.”
5 geta$:ifa$=””then 5
10 wait 56320,15,9: goto 2

Before hitting a key, keep directions left and down pressed. Then, if you hit a key such as spacebar, the program will wait. If you release one or both of the above mentioned directions, it will stop waiting and it will ask you to hit a key again.

Also, if you run the program and still keep left and down pressed, it will anyway stop waiting if you press either up or right.

Remember. If joystick directions are not pressed, the bits in 56320 for joystick directions are all set to 1. If you press one of those directions, their bit turns off.

So, we said earlier: the wait statement will stop waiting when either bit 3 or bit 0 turn OFF in the content of location 56320. That just means, it will stop waiting when either up or right are pressed.

Likewise, we also said earlier: the wait statement will stop waiting when either bit 2 or bit 1 turn ON in the content of location 56320. That means, when either left or bottom are no longer pressed.

So, the program will keep waiting as long as both left and bottom are being pressed and if both up and right are not pressed.

We can explain the overall behaviour of WAIT with logic: it takes number 1 and the content of location and makes and AND of them. Then, if there’s number 2, it takes the result of the previous AND and makes a XOR between that result and number 2. WAIT will keep waiting as long as the end result of the above logic operations is 0. If just one bit turns on in the end result, it will then stop waiting.

## One Reply to “The Commodore 64 BASIC WAIT statement explained”

1. Appreciate this post. Let me try it out.

Commodore Banner Exchange
Insert math as
$${}$$