Skip to content

Read a button on Sord M5

Reuse the actual machine-specific reader, with its caller, state, ownership and a diagnosed input mistake.

Build the complete caller with make -C sord-m5/patterns/assembly/input/read-action. Press Space once, keep it held, then release and press again. The piece moves on each new press and returns to its starting position after two; holding does not repeat the action. Load the cartridge produced by the Makefile in the named configuration; retain its reset/header setup.

Sord M5 native caller after accepted input.
Captured from the running caller in the configuration below.

Take the reader

sample.inc is an inline hardware sampler: include it where the caller samples input. It has no RET/RTS and must not be called as a subroutine. It supplies the held state described below; the caller supplies previous-state storage and decides what a new press does.

The reusable reader

sample.inc
    in a,($30)                ; keyboard row Y0, DIRECT ACTIVE-HIGH read
    and $40                   ; Space is bit 6; no inversion
    ld b,a

IN ($30) reads keyboard row Y0. Space is ACTIVE-HIGH bit 6: mask $40 and keep its polarity. B receives 0/$40; AF/B change.

Calculate a new-press mask as current AND NOT previous, then store current as previous on every update, including release. Test held state for continuous movement and the new-press mask for an action that should happen once. A key already held at the caller’s first sample counts as a press because previous starts at zero.

Caller storage: previous=$7001; turn=$7000, field clock=$7002/$7003; stack below $7FF0. The sample’s display initialiser and any interrupts have their own ownership requirements; keep them with this complete example.

Change it and recognise a mistake

Do not add the complement used by neighbouring Z80 machines: an idle M5 would look pressed. Other keyboard rows need their own selection/read contract.

The broken caller feeds held state into the action decision. Keeping the button down advances repeatedly; the native fault run reaches 32 instead of remaining at one. Restore the previous-state comparison and update previous on release as well.

Deliberately broken input/update path

no-edge.asm
; Original capability caller: owns VDP, one audio channel and declared RAM.
VDP_DATA equ $10
VDP_CTRL equ $11
TURNS equ $7000
SOUND_TICKS equ $7005
; Sord M5 monitor-started homebrew cartridge. Space commits an untimed turn.
; Owns VDP/keyboard and $7000-$7003; stack below $7FF0; no monitor return.
    org $2000
    db 0
    dw start,init_return      ; monitor calls initialiser, then entry vector
    jp irq_return
    jp irq_return
start:
    di
    ld sp,$7ff0
    xor a
    ld ($7000),a
    ld ($7001),a
    ld ($7002),a
    ld ($7003),a
    in a,($11)
    xor a
    out ($11),a
    ld a,$80
    out ($11),a               ; R0 legacy graphics
    xor a
    out ($11),a
    ld a,$81
    out ($11),a               ; R1 display and VDP IRQ disabled
    call init_picture
main:
    call wait_frame
    ld hl,($7002)
    inc hl
    ld ($7002),hl
    include "../sample.inc"
    ld a,($7001)
    ld a,b
    nop
    jr z,no_action
    ld a,($7000)
    inc a
    ld ($7000),a
    call trigger_sound
no_action:
    ld a,b
    ld ($7001),a
    call draw_picture
    call service_sound
    jp main
wait_frame:
    in a,($11)
wait_new:
    in a,($11)
    and $80
    jr z,wait_new
    ret
init_return:
irq_return:
    ret
    include "../picture.inc"
    include "../sound.inc"
    defs $6000-$,0
Sord M5 deliberately broken input caller.
Observed fault; use the working caller to repair it.

The complete caller

Working setup and integration

demo.asm
; Original capability caller: owns VDP, one audio channel and declared RAM.
VDP_DATA equ $10
VDP_CTRL equ $11
TURNS equ $7000
SOUND_TICKS equ $7005
; Sord M5 monitor-started homebrew cartridge. Space commits an untimed turn.
; Owns VDP/keyboard and $7000-$7003; stack below $7FF0; no monitor return.
    org $2000
    db 0
    dw start,init_return      ; monitor calls initialiser, then entry vector
    jp irq_return
    jp irq_return
start:
    di
    ld sp,$7ff0
    xor a
    ld ($7000),a
    ld ($7001),a
    ld ($7002),a
    ld ($7003),a
    in a,($11)
    xor a
    out ($11),a
    ld a,$80
    out ($11),a               ; R0 legacy graphics
    xor a
    out ($11),a
    ld a,$81
    out ($11),a               ; R1 display and VDP IRQ disabled
    call init_picture
main:
    call wait_frame
    ld hl,($7002)
    inc hl
    ld ($7002),hl
    include "sample.inc"
    ld a,($7001)
    cpl
    and b
    jr z,no_action
    ld a,($7000)
    inc a
    ld ($7000),a
    call trigger_sound
no_action:
    ld a,b
    ld ($7001),a
    call draw_picture
    call service_sound
    jp main
wait_frame:
    in a,($11)
wait_new:
    in a,($11)
    and $80
    jr z,wait_new
    ret
init_return:
irq_return:
    ret
    include "picture.inc"
    include "sound.inc"
    defs $6000-$,0

For more responsive controls, compare polling, buffering and press edges in input choices.

Verification and sources

Target: NTSC M5, monitor-started cartridge. These callers were executed with released Emu198x 0.44.1; assembly uses Asm198x 0.0.71. The sample folder contains reproduction instructions and retained observations. The independent checker binds source, native captures and actual output; its rejection tests exercise wrong state, missing files and false audio claims.

This qualifies the named emulator/firmware path. Pitch accuracy, original hardware and other peripherals require their own observations.

Sord BASIC-G Appendix H: I/O map; Sord M5C-01 schematic: decoding; TI SN76489A and TMS9918A data manuals. The specific Space polarity is configuration-specific executed evidence.