Machine code from zero

A course for the Dragon 32 · Chapters 0 to 15

Chapter 10

Keys: the Dragon listens

About 20 minutes · The fixed ending, all the way

1What the keyboard checker gives back

In Chapters 7 and 8 we opened up the fixed ending: LDX $A000 points the finger at the keyboard checker, a small job that lives in the Dragon’s fixed memory. JSR ,X goes to it and comes back. We saw that when nothing is pressed, lamp Z is on.

Now the missing part: the checker comes back with the key’s code in drawer A. G pressed? A will hold $47, exactly like LDA #'G. Nothing pressed? A will hold 0, and that is why Z is on.

Figure 24: the keyboard is connected to drawer A. After JSR ,X: a key pressed = its code in A, and lamp Z off. No key = 0 in A, and lamp Z on.

Now you can read the fixed ending like a sentence: “Ask the keyboard. If nothing was pressed, ask again. If something was pressed, go back to DREAM.” Until now we didn’t care which key. From today we do.

From here on the fixed ending is written out in the windows, and the processor really runs it. At the end of every run you will have to press a key, just like on the Dragon.

2Wait for a key, and show it

The fixed ending’s waiting loop, in the middle of the program. Here it is called WAIT, because the name KEY is taken. Under the screen there is a small keyboard. You can also press keys on your own keyboard, after clicking on the window.

3Every key has a code

Press the keys and see what arrives in drawer A. The letters have a code you already know. The arrows and SPACE have codes of their own.

    4A typewriter

    Every key you press is written on the screen, one after another. Q stops. Notice the two questions: BEQ LOOP = “nothing pressed? ask again”. CMPA #'Q and BEQ KEY = “Q pressed? go to the end”.

    Why is the finger on the screen Y? Because LDX $A000 overwrites X on every question. The keyboard checker doesn’t touch Y or B, so they are safe.

    5Answering a key

    Now we decide by the key: R paints the square red, B blue, Q leaves. This is exactly the crossroads from Chapter 8, only the question is “which key?”.

    Notice: the colour goes through B (LDB, STB). Why not A? Because A holds the key’s code, and we still need it for the next question.

    ?Puzzles: keys

    Each puzzle is checked in several cases. In each case different keys are “pressed”, one after another, and at the end the screen must look like the picture. You don’t need to type the fixed ending: it is already there, and the Q key must get to it.

    ▶On the real Dragon

    What we learned: LDX $A000 and JSR ,X ask the keyboard. The key’s code comes back in A. No key: 0, and lamp Z is on.
    Codes: arrows $5E $0A $08 $09, SPACE $20, a letter = its own code. The checker overwrites X; the finger on the screen is Y.

    A hobbyist tribute project, not affiliated with or endorsed by Dragon Data. The course, drawings and text are our own. Every program in the course was checked on the site's Dragon machine (XRoar running the BASIC we wrote).