-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathnespad.pio
22 lines (19 loc) · 940 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 1 ; Set IRQ and wait for CPU to clear it
set pins, 1 side 1 [10] ; Latch high, 12 uS total
set x, 7 side 1 ; Set bit counter
set pins, 0 side 1 ; Latch low
bitloop:
set pins, 0 side 1 [8]
in pins, 1 side 0 [2] ; Read bit on falling clock or latch, wait 3 uS
jmp x-- bitloop side 1 ; Clock high, repeat for 8 bits
; Autopush will write byte to FIFO at this point
.wrap