Skip to content

Commit d4b6246

Browse files
committed
fix keyboard driver to work on real hardware (hopefully)
1 parent fe77e3b commit d4b6246

File tree

3 files changed

+39
-8
lines changed

3 files changed

+39
-8
lines changed

kernel/nintendo/keyboard.s

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,13 @@ key_delay: .byte $00
7979

8080
; TODO: check if a keyboard is actually connected.
8181

82+
; burn some cycles to give the keyboard time to settle.
83+
keyb_delay:
84+
ldy #10
85+
- dey
86+
bne -
87+
rts
88+
8289
; interrupt routine, that scans for keys
8390
keyb_scan:
8491
; our current key table offset encodes where we are in the scanning process.
@@ -101,6 +108,7 @@ read_keyboard:
101108
; reset the keyboard to the 0th row, 0th column.
102109
lda #$05
103110
sta JOYPAD1
111+
jsr keyb_delay ; this may not be strictly necessary but it's safer and only wastes a few cycles.
104112
; reset keytab_offset to 0
105113
ldx #0
106114
stx keytab_offset
@@ -115,6 +123,7 @@ get_nibble:
115123
; always set the keyboard enable bit.
116124
ora #%00000100
117125
sta JOYPAD1
126+
jsr keyb_delay
118127

119128
; read 4 bits of key data from the keyboard.
120129
lda JOYPAD2

kernel/nintendo/keyboard_init.s

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,26 +16,41 @@
1616
;; initialize and install keyboard scanning routine
1717
keyboard_init:
1818
; check that a keyboard is attached.
19-
2019
; reset the keyboard to the 0th row, 0th column.
2120
lda #$05
2221
sta JOYPAD1
2322

24-
; toggle the column select bit until we reach the keyless 10th row.
23+
; give the keyboard some time to reset.
24+
nop
25+
nop
26+
nop
27+
nop
28+
nop
29+
nop
30+
31+
; toggle the column select bit until we reach the last column of the 9th row.
32+
ldx #9
33+
- lda #$04
34+
sta JOYPAD1
35+
jsr _delay
2536
lda #$06
26-
ldx #$04
27-
ldy #18
28-
- sta JOYPAD1
29-
stx JOYPAD1
30-
dey
37+
sta JOYPAD1
38+
jsr _delay
39+
dex
3140
bne -
3241

42+
; select the keyless 10th row.
43+
lda #$04
44+
sta JOYPAD1
45+
jsr _delay
46+
3347
; check that the keyboard responds correctly to enable/disable signals.
3448
lda JOYPAD2
3549
and #$1e
3650
eor #$1e
3751
bne _printmsg ; branch on error
3852
sta JOYPAD1
53+
jsr _delay
3954
lda JOYPAD2
4055
and #$1e
4156
bne _printmsg ; branch on error
@@ -60,8 +75,15 @@ _printmsg:
6075
bne -
6176
+ rts
6277

78+
; burn some cycles to give the keyboard time to settle.
79+
_delay:
80+
ldy #10
81+
- dey
82+
bne -
83+
rts
84+
6385
_startmsg:
64-
.text "Family BASIC Keyboard module version 0.3"
86+
.text "Family BASIC Keyboard module version 0.4"
6587
.byte $0a
6688
_errormsg:
6789
.text "No Keyboard"

lunix.fds

41 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)