-
Notifications
You must be signed in to change notification settings - Fork 2
/
nespad.pio
22 lines (19 loc) · 967 Bytes
/
nespad.pio
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
; NES (classic) gamepad reader. To read current pad state, trigger a transfer
; by clearing PIO IRQ 0, then use blocking read from FIFO -- result appears
; there after ~100 uS. Since PIO runs concurrently, minor CPU tasks can be
; handled between trigger/read, but it's NOT recommended to immediately
; trigger another read and then catch the result in the same place on the
; next frame, as this introduces latency. Ideal time is at frame start.
.program nespad
.side_set 1
.wrap_target
irq wait 0 side 0 ; Set IRQ and wait for CPU to clear it
set pins, 1 side 0 [10] ; Latch high, 12 uS total
set x, 7 side 0 ; Set bit counter
set pins, 0 side 0 ; Latch low
bitloop:
in pins, 1 side 0 [4] ; Read bit on falling clock or latch, wait 6 uS
set pins, 0 side 1 [5] ; Clock high, wait for 6 uS
jmp x-- bitloop side 0 ; Clock low, repeat for 8 bits
; Autopush will write byte to FIFO at this point
.wrap