; Keyboard matrix reading (CIA 1). Port A selects columns (a 0 bit drives that ; column), port B returns the rows (a 0 bit = key down). Positions checked ; against the KERNAL decode table at $EB81 (keycode = column * 8 + row): ; column 0: CRSR RIGHT row 2 (keycode 2), CRSR DOWN row 7 (keycode 7) ; column 1: left SHIFT row 7 (keycode 15) ; column 6: right SHIFT row 4 (keycode 52) ; column 5: + row 0 (keycode 40), P row 1 (keycode 41), - row 3 (keycode 43) ; column 7: SPACE row 4 (keycode 60) ; SHIFT reverses the cursor keys: SHIFT + CRSR RIGHT = left, SHIFT + CRSR DOWN ; = up. VICE maps the PC arrow keys to these combinations. CIA1_PRA = $DC00 CIA1_PRB = $DC01 CIA1_ICR = $DC0D KEY_COL_CURSOR = %11111110 KEY_COL_LSHIFT = %11111101 KEY_COL_RSHIFT = %10111111 KEY_COL_SPACE = %01111111 KEY_COL_P = %11011111 KEY_ROW_RIGHT = %00000100 KEY_ROW_DOWN = %10000000 KEY_ROW_LSHIFT = %10000000 KEY_ROW_RSHIFT = %00010000 KEY_ROW_SPACE = %00010000 KEY_ROW_P = %00000010 KEY_ROW_PLUS = %00000001 KEY_ROW_MINUS = %00001000 REPEAT_DELAY = 30 ; + and - held: repeat after 30 frames, REPEAT_RATE = 4 ; then every 5th frame .export key_row_cursor, key_row_lshift, key_row_rshift, key_row_space, key_row_p .export scroll_dx .segment "CODE" ; Stop the KERNAL timer interrupt: nothing uses it any more, its keyboard scan ; would fight read_keys over port A, and it delayed the buffer flip. CIA 1 ; timer A keeps running, so it still seeds the generator. stop_kernal_irq: lda #%01111111 ; clear all CIA 1 interrupt enables sta CIA1_ICR lda CIA1_ICR ; acknowledge anything pending lda #1 sta space_down ; SPACE must be released before it counts sta p_down lda #0 sta key_held sta key_held+1 rts ; Read the keys once per frame. Sets scroll_dx and scroll_dy to -1, 0 or +1 ; for as long as a cursor key is held. A new press of P shows the next ; person's scores (R8; held P counts once). + adds a person and - removes ; the last one (M7.3; held, they repeat). Returns carry set when SPACE has ; just been pressed (held SPACE counts once). read_keys: lda #KEY_COL_CURSOR sta CIA1_PRA lda CIA1_PRB key_row_cursor: ; check hook: tools/check-keys.ps1 fakes rows here sta keys_cursor lda #KEY_COL_LSHIFT sta CIA1_PRA lda CIA1_PRB key_row_lshift: sta keys_lshift lda #KEY_COL_RSHIFT sta CIA1_PRA lda CIA1_PRB key_row_rshift: sta keys_rshift lda #KEY_COL_SPACE sta CIA1_PRA lda CIA1_PRB key_row_space: sta keys_space lda #KEY_COL_P sta CIA1_PRA lda CIA1_PRB key_row_p: sta keys_p lda #$FF ; select no column sta CIA1_PRA ldy #1 ; unshifted: right / down lda keys_lshift and #KEY_ROW_LSHIFT beq @shifted lda keys_rshift and #KEY_ROW_RSHIFT bne @unshifted @shifted: ldy #$FF ; shifted: left / up @unshifted: lda #0 sta scroll_dx sta scroll_dy lda keys_cursor and #KEY_ROW_RIGHT bne @no_horizontal sty scroll_dx @no_horizontal: lda keys_cursor and #KEY_ROW_DOWN bne @no_vertical sty scroll_dy @no_vertical: ldx #0 lda keys_p and #KEY_ROW_P bne @p_up inx ; X = 1: P is down @p_up: lda p_down stx p_down bne @p_done ; already down last frame (or up now: X = 0) cpx #1 bne @p_done ldx shown_person ; next person, the last wraps to the first inx cpx people_count bcc @p_next ldx #0 @p_next: stx shown_person lda #1 sta shown_dirty @p_done: ldx #0 ; + : add a person lda keys_p and #KEY_ROW_PLUS jsr key_repeat bcc @no_plus jsr add_person @no_plus: ldx #1 ; - : remove the last person lda keys_p and #KEY_ROW_MINUS jsr key_repeat bcc @no_minus jsr remove_person @no_minus: ldx #0 lda keys_space and #KEY_ROW_SPACE bne @space_up inx ; X = 1: SPACE is down @space_up: lda space_down stx space_down bne @no_press ; already down last frame cpx #1 ; carry set only for a new press rts @no_press: clc rts ; A = the key's row bits (0 = down; Z flag set from them), X = 0 for +, 1 for -. Carry set when ; the key acts this frame: a new press, then while held every REPEAT_RATE + 1 ; frames after REPEAT_DELAY frames. key_repeat: bne @up lda key_held,x beq @first cmp #REPEAT_DELAY bcc @count lda #REPEAT_DELAY - REPEAT_RATE sta key_held,x sec rts @first: inc key_held,x sec rts @count: inc key_held,x clc rts @up: lda #0 sta key_held,x clc rts .segment "BSS" key_held: .res 2 ; frames + and - have been held scroll_dx: .res 1 ; keep scroll_dx and scroll_dy together scroll_dy: .res 1 keys_cursor: .res 1 keys_lshift: .res 1 keys_rshift: .res 1 keys_space: .res 1 space_down: .res 1 keys_p: .res 1 p_down: .res 1