Skip to content

Commit

Permalink
fix keyboard driver to work on real hardware (hopefully)
Browse files Browse the repository at this point in the history
  • Loading branch information
decrazyo committed Feb 12, 2024
1 parent fe77e3b commit d4b6246
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 8 deletions.
9 changes: 9 additions & 0 deletions kernel/nintendo/keyboard.s
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,13 @@ key_delay: .byte $00

; TODO: check if a keyboard is actually connected.

; burn some cycles to give the keyboard time to settle.
keyb_delay:
ldy #10
- dey
bne -
rts

; interrupt routine, that scans for keys
keyb_scan:
; our current key table offset encodes where we are in the scanning process.
Expand All @@ -101,6 +108,7 @@ read_keyboard:
; reset the keyboard to the 0th row, 0th column.
lda #$05
sta JOYPAD1
jsr keyb_delay ; this may not be strictly necessary but it's safer and only wastes a few cycles.
; reset keytab_offset to 0
ldx #0
stx keytab_offset
Expand All @@ -115,6 +123,7 @@ get_nibble:
; always set the keyboard enable bit.
ora #%00000100
sta JOYPAD1
jsr keyb_delay

; read 4 bits of key data from the keyboard.
lda JOYPAD2
Expand Down
38 changes: 30 additions & 8 deletions kernel/nintendo/keyboard_init.s
Original file line number Diff line number Diff line change
Expand Up @@ -16,26 +16,41 @@
;; initialize and install keyboard scanning routine
keyboard_init:
; check that a keyboard is attached.

; reset the keyboard to the 0th row, 0th column.
lda #$05
sta JOYPAD1

; toggle the column select bit until we reach the keyless 10th row.
; give the keyboard some time to reset.
nop
nop
nop
nop
nop
nop

; toggle the column select bit until we reach the last column of the 9th row.
ldx #9
- lda #$04
sta JOYPAD1
jsr _delay
lda #$06
ldx #$04
ldy #18
- sta JOYPAD1
stx JOYPAD1
dey
sta JOYPAD1
jsr _delay
dex
bne -

; select the keyless 10th row.
lda #$04
sta JOYPAD1
jsr _delay

; check that the keyboard responds correctly to enable/disable signals.
lda JOYPAD2
and #$1e
eor #$1e
bne _printmsg ; branch on error
sta JOYPAD1
jsr _delay
lda JOYPAD2
and #$1e
bne _printmsg ; branch on error
Expand All @@ -60,8 +75,15 @@ _printmsg:
bne -
+ rts

; burn some cycles to give the keyboard time to settle.
_delay:
ldy #10
- dey
bne -
rts

_startmsg:
.text "Family BASIC Keyboard module version 0.3"
.text "Family BASIC Keyboard module version 0.4"
.byte $0a
_errormsg:
.text "No Keyboard"
Expand Down
Binary file modified lunix.fds
Binary file not shown.

0 comments on commit d4b6246

Please sign in to comment.