Gamepad
Inputs
| Pin | Type | Description |
|---|---|---|
| DevAddr | bus8 | Device address — 0x1E for port 0xFE |
| IO_SEL | bit | I/O select — must be 1 to read the gamepad |
Outputs
| Pin | Type | Description |
|---|---|---|
| Data | bus8 | Button mask — bitfield: Up=1, Down=2, Left=4, Right=8 |
How It Works
The gamepad is the first way to "touch" the computer in this course: before it, the processor silently ran the program, but here a human presses a button — and a byte on the bus changes. Inside there are only four buttons, and each hides in its own bit of a single data byte.
The key property is the bitmask: Up = 1, Down = 2, Left = 4, Right = 8. Presses add up: Up and Right together give 1 + 8 = 9; nothing pressed — Data = 0. Example: the processor read Data = 2 — the snake must turn down; Data = 10 — down and right at the same time.
A nuance: the gamepad hands out data only when its port is selected — IO_SEL = 1 and DevAddr = 0x1E (port 0xFE), which in assembly is LDA 254. The buttons are pressed with the mouse right on the element during simulation, and the polling must repeat in the game loop every few ticks — the processor will not "feel" the button on its own.
Examples (button mask)
| Pressed buttons | Data (bin) | Data (dec) |
|---|---|---|
| nothing | 00000000 | 0 |
| Up | 00000001 | 1 |
| Down + Right | 00001010 | 10 |
| Up + Right | 00001001 | 9 |
Checking a single button fits in three instructions: LDA 254 reads the mask, AND 8 isolates the Right bit, JZ moves execution on if the button is not pressed.
Usage
Level 23 "Gamepad" teaches reading the button mask, and in levels 28–29 it drives the moving dot and Snake: the direction of movement changes in response to a press. It is the first genuine "human → processor" channel in the whole course.
A concrete use is polling in the game loop: LDA 254 reads the port, then series of AND 1 (up?), AND 2 (down?), AND 4 and AND 8 with conditional JZ branches take the mask apart bit by bit; on each match the program changes direction. This is exactly how any game on the course computer reacts to the player.
Related components
Related articles
Frequently Asked Questions
How do you read the gamepad buttons?
With an IN instruction from port 0xFE: each bit corresponds to one button.
What does the gamepad return when Up and Right are pressed together?
9: the bits add up, 1 + 8 = 9. The processor sees both presses in a single byte.
How do you check the Right button in assembly?
LDA 254, then AND 8: a non-zero result means the button is pressed. JZ moves execution on if the bit is not set.