Skip to content

Keyboard Integration

Rudolf Stepan edited this page Jun 20, 2026 · 2 revisions

Source file

Keyboard Integration

Overview

Keyboard input is routed through VIA6522 Port A and exposed to 6502 software via memory-mapped reads. SDL keyboard events feed an internal FIFO queue in the VIA implementation.

Hardware View

  • VIA base: typically $8800
  • ORA ($8801): read next key byte
  • DDRA ($8803): set to input mode ($00)
  • IFR ($880D): CA1 bit indicates key available
  • IER ($880E): enable CA1 interrupt if desired

Read Loop (Assembly)

VIA_ORA  = $8801
VIA_DDRA = $8803
VIA_IFR  = $880D

LDA #$00
STA VIA_DDRA

.wait:
  LDA VIA_IFR
  AND #$02
  BEQ .wait
  LDA VIA_ORA

Read Loop (C)

#include <stdint.h>

#define VIA_ORA  (*(volatile uint8_t *)0x8801)
#define VIA_DDRA (*(volatile uint8_t *)0x8803)
#define VIA_IFR  (*(volatile uint8_t *)0x880D)

static inline void kb_init(void) {
    VIA_DDRA = 0x00;
}

static inline uint8_t kb_getc_blocking(void) {
    while ((VIA_IFR & 0x02u) == 0) {
    }
    return VIA_ORA;
}

Supported Input

  • ASCII letters and numbers
  • common punctuation
  • Enter (0x0D)
  • Backspace (0x08)
  • Arrow keys move the VIC hardware cursor around the 40x25 text screen.
  • Backspace in screen-edit mode deletes the character left of the hardware cursor, including on recalled lines above the prompt.
  • Enter in screen-edit mode reads the entire screen line (all 40 columns), trims trailing spaces, and replays the text to BASIC — matching C64 behavior. Echo is suppressed during replay so the line is not duplicated on screen.
  • Pause/Break or Ctrl+C in the SDL window sends BASIC STOP (0x03).
  • Home moves the cursor to the top-left cell; Shift+Home or Ctrl+L clears the screen and homes the cursor.
  • ESC closes emulator window

Related Files

Clone this wiki locally