solitaire-c64/joystick.c65

72 lines
2.2 KiB
Text

#IFNDEF __lib_joystick
#DEFINE __lib_joystick 1
GOTO __skip_lib_joystick
// ============================================================================
// JOYSTICK INPUT DRIVER FOR COMMODORE 64
// ============================================================================
// This library provides joystick input reading for the Commodore 64.
//
// Joystick reading uses CIA port 2 ($DC00) which is preferred for games
// Port 1 ($DC01) conflicts with keyboard matrix scanning
//
// Usage:
// #INCLUDE "joystick.c65"
// WHILE 1
// joy_read()
// BYTE test_bit
// test_bit = joy_state & JOY_UP_MASK
// IF test_bit
// // handle up
// ENDIF
// WEND
// ============================================================================
// CIA Port addresses
WORD CONST JOY_PORT1 = $DC01 // Port 1 (conflicts with keyboard)
WORD CONST JOY_PORT2 = $DC00 // Port 2 (recommended for games)
// Joystick bit masks (active low - 0 = pressed)
BYTE CONST JOY_UP_MASK = %00000001 // Bit 0
BYTE CONST JOY_DOWN_MASK = %00000010 // Bit 1
BYTE CONST JOY_LEFT_MASK = %00000100 // Bit 2
BYTE CONST JOY_RIGHT_MASK = %00001000 // Bit 3
BYTE CONST JOY_FIRE_MASK = %00010000 // Bit 4
// Joystick state variable
BYTE joy_state // Inverted joystick state (1=pressed, 0=not pressed)
// ============================================================================
// FUNC joy_read_port1
// Read joystick port 1 (alternative port, may conflict with keyboard)
// Updates the same joy_state variable as joy_read()
// ============================================================================
FUNC joy_read_port1
BYTE joy_raw
joy_raw = PEEK JOY_PORT1
joy_state = joy_raw ^ $FF // Invert bits: 1=pressed, 0=not pressed
FEND
// ============================================================================
// FUNC joy_read
// Read joystick port 2 and update state variable
// Call this every frame to update joystick state
// Inverts active-low CIA bits to active-high (1=pressed, 0=not pressed)
// ============================================================================
FUNC joy_read_port2
BYTE joy_raw
joy_raw = PEEK JOY_PORT2
joy_state = joy_raw ^ $FF // Invert bits: 1=pressed, 0=not pressed
FEND
LABEL __skip_lib_joystick
#IFEND