6502-OS/system/keypad.s65

99 lines
2.5 KiB
Plaintext
Raw Normal View History

2023-12-08 22:56:35 +01:00
.include "keypad.h65"
2023-10-26 19:51:20 +02:00
2023-12-08 22:56:35 +01:00
Export kp, init, read_irq, read
ExportZp kp, _DEBUG_VAL
2023-10-26 19:51:20 +02:00
2023-11-11 12:13:47 +01:00
.zeropage
2023-12-08 22:56:35 +01:00
_COLUMN: .res 1 ; for storing stuff
_DEBUG_VAL: .res 1 ; for storing the last char DEBUG
2023-10-26 19:51:20 +02:00
2023-11-11 12:13:47 +01:00
.bss ; reserve space or ringbuffer
2023-12-08 22:56:35 +01:00
RBUF_MEM_START: .res kp::BUF_SIZE
RBUF_MEM_END = RBUF_MEM_START + kp::BUF_SIZE - 1
2023-10-30 22:11:24 +01:00
.define RBUF_NAME "keypad"
2023-12-08 22:56:35 +01:00
.include "buffer.h65"
2023-10-26 19:51:20 +02:00
2023-11-11 12:13:47 +01:00
.code
2023-12-08 22:56:35 +01:00
.proc init
2023-11-01 13:13:23 +01:00
; todo remove later and test
2023-10-30 22:11:24 +01:00
lda #$ff
2023-12-08 22:56:35 +01:00
sta kp::KP_IO+IO::DDRA
stz kp::KP_IO+IO::RA
2023-10-26 19:51:20 +02:00
; INIT KEYPAD
2023-11-01 13:13:23 +01:00
; todo: use masks
2023-12-08 22:56:35 +01:00
lda #%11110000; kp::KP_IO+IO::RA 0-3 output
sta kp::KP_IO+IO::DDRA
2023-11-13 19:14:18 +01:00
; output 0, key press will pull input pin low
2023-12-08 22:56:35 +01:00
stz kp::KP_IO+IO::RA
stz kp::KP_IO+IO::ACR
2023-10-26 19:51:20 +02:00
2023-11-13 19:14:18 +01:00
; lda #%00010000 ; set CA1 to interrupt on pos. edge
2023-11-01 13:13:23 +01:00
; todo: use masks
2023-11-13 19:14:18 +01:00
lda #IO::PCR::CA1_IP_AE
2023-12-08 22:56:35 +01:00
sta kp::KP_IO+IO::PCR
2023-10-30 22:11:24 +01:00
jsr rb_keypad_init ; init keybuffer
2023-12-08 22:56:35 +01:00
lda #(IO::IRQ::IRQ | IO::IRQ::CA1) ; enable interrupt for CA1 on kp::KP_IO
sta kp::KP_IO+IO::IER
2023-10-26 19:51:20 +02:00
rts
2023-10-30 22:11:24 +01:00
.endproc
2023-10-26 19:51:20 +02:00
2023-12-08 22:56:35 +01:00
read = rb_keypad_read
2023-10-26 19:51:20 +02:00
;********************************************************************************
2023-10-30 22:11:24 +01:00
; @function Read which key is pressed on the keypad
; @details
; Checks which key is pressed and stores it in the keybuffer
; The value stored in the keybuffer is the offset which must be added to
; kp_VALUES to retrieve the key that was pressed
2023-10-26 19:51:20 +02:00
;********************************************************************************
2023-12-08 22:56:35 +01:00
.proc read_irq
2023-10-30 22:11:24 +01:00
; test each "row" and check which column is 1
2023-12-04 20:56:16 +01:00
; TODO dont check every column if the value has been found
2023-11-13 19:14:18 +01:00
lda #%11100000
2023-10-26 19:51:20 +02:00
ldx #$00
2023-10-30 22:11:24 +01:00
jsr @kp_read_column
2023-11-13 19:14:18 +01:00
lda #%11010000
2023-12-04 20:56:16 +01:00
ldx #$04
2023-10-30 22:11:24 +01:00
jsr @kp_read_column
2023-11-13 19:14:18 +01:00
lda #%10110000
2023-12-04 20:56:16 +01:00
ldx #$08
2023-10-30 22:11:24 +01:00
jsr @kp_read_column
2023-11-13 19:14:18 +01:00
lda #%01110000
2023-10-26 19:51:20 +02:00
ldx #$0c
2023-10-30 22:11:24 +01:00
jsr @kp_read_column
2023-12-08 22:56:35 +01:00
stz kp::KP_IO+IO::RA
lda kp::KP_IO+IO::RA ; clear interrupt flag again beceause it might be set again through the above tests
2023-11-01 13:13:23 +01:00
rts
2023-10-30 22:11:24 +01:00
@kp_read_column:
2023-12-08 22:56:35 +01:00
sta kp::KP_IO+IO::RA
lda kp::KP_IO+IO::RA
sta _COLUMN ; store result in zeropage so that bbr can be used
bbr0 _COLUMN,@kp_write ; row 1
2023-10-26 19:51:20 +02:00
inx
2023-12-08 22:56:35 +01:00
bbr1 _COLUMN,@kp_write ; row 3
2023-10-26 19:51:20 +02:00
inx
2023-12-08 22:56:35 +01:00
bbr2 _COLUMN,@kp_write ; row 2
2023-10-26 19:51:20 +02:00
inx
2023-12-08 22:56:35 +01:00
bbr3 _COLUMN,@kp_write ; row 4
2023-10-26 19:51:20 +02:00
rts
2023-10-30 22:11:24 +01:00
@kp_write:
2023-12-08 22:56:35 +01:00
lda VALUES,x
2023-12-08 00:14:02 +01:00
; TODO use ringbuffer
2023-12-08 22:56:35 +01:00
sta _DEBUG_VAL
2023-10-30 22:11:24 +01:00
jsr rb_keypad_write
rts
.endproc
2023-11-11 12:13:47 +01:00
.rodata
2023-12-08 22:56:35 +01:00
VALUES:
2023-12-04 20:56:16 +01:00
.byte "174*"
.byte "2850"
2023-12-08 00:14:02 +01:00
.byte "396#"
.byte "ACBD"
2023-12-04 20:56:16 +01:00
; keypad conencted so that
; row 1 = PA0
; row 2 = PA2
; row 3 = PA1
; row 4 = PA3