Log inRegister an accountBrowse CSDbHelp & documentationFacts & StatisticsThe forumsAvailable RSS-feeds on CSDbSupport CSDb Commodore 64 Scene Database
You are not logged in - nap
CSDb User Forums


Forums > C64 Coding > Short routine to check if 1,2 or 3 is pressed
2023-01-02 22:16
Monte Carlos

Registered: Jun 2004
Posts: 351
Short routine to check if 1,2 or 3 is pressed

I remember i often checked for space using
lda #$10
bit $dc01
rts

Space was pressed if the code returned with a cleared zero flag. Also, I remember some other codes like #$02 for left arrow or #$20 for cbm (???). Other codes I don't remember.

Now, I'm in search for a short routine to check if 1,2 or 3 is pressed and return 0,1,2 respectively.
Do you have any idea how to set/read the CIA regs in this case?

I'm working with this keyboard matrix: https://sta.c64.org/cbm64kbdlay.html
2023-01-02 22:31
tlr

Registered: Sep 2003
Posts: 1723
Quote: I remember i often checked for space using
lda #$10
bit $dc01
rts

Space was pressed if the code returned with a cleared zero flag. Also, I remember some other codes like #$02 for left arrow or #$20 for cbm (???). Other codes I don't remember.

Now, I'm in search for a short routine to check if 1,2 or 3 is pressed and return 0,1,2 respectively.
Do you have any idea how to set/read the CIA regs in this case?

I'm working with this keyboard matrix: https://sta.c64.org/cbm64kbdlay.html


How about:
.C:c000  78          SEI
.C:c001  A2 00       LDX #$00
.C:c003  A9 7F       LDA #$7F
.C:c005  8D 00 DC    STA $DC00
.C:c008  AD 01 DC    LDA $DC01
.C:c00b  4A          LSR A
.C:c00c  90 12       BCC $C020
.C:c00e  4A          LSR A
.C:c00f  4A          LSR A
.C:c010  4A          LSR A
.C:c011  90 0C       BCC $C01F
.C:c013  A9 FD       LDA #$FD
.C:c015  8D 00 DC    STA $DC00
.C:c018  AD 01 DC    LDA $DC01
.C:c01b  4A          LSR A
.C:c01c  B0 E5       BCS $C003
.C:c01e  E8          INX
.C:c01f  E8          INX
.C:c020  8E 00 04    STX $0400
.C:c023  90 DC       BCC $C001
2023-01-02 22:47
Monte Carlos

Registered: Jun 2004
Posts: 351
Thank you! However, meanwhile I made it up myself.
Here is my code:

        lda #%11111101
        sta CIA1_PRA
        
        lda CIA1_PRB
        cmp #%11111110
        beq is3
        
        lda #%01111111
        sta CIA1_PRA
        
        lda CIA1_PRB
        cmp #%11111110
        beq is1
        cmp #%11110111
        beq is2
        
        lda #$FF
        rts
    is1:
        lda #0
        rts
    is2:
        lda #1
        rts
    is3:
        lda #2
        rts
2023-01-03 00:25
ws

Registered: Apr 2012
Posts: 229
You already solved it for your case, but maybe for future reference:

i found Shift key + one other key by Oswald to be pretty useful.
(Note: for some reason, "minikey" never worked reliably for me, reasons might be found below.)

Also this article is of general interest if one encounters problems : https://codebase64.org/doku.php?id=base:scanning_the_keyboard_t..
2023-01-03 10:49
tlr

Registered: Sep 2003
Posts: 1723
Quote: Thank you! However, meanwhile I made it up myself.
Here is my code:

        lda #%11111101
        sta CIA1_PRA
        
        lda CIA1_PRB
        cmp #%11111110
        beq is3
        
        lda #%01111111
        sta CIA1_PRA
        
        lda CIA1_PRB
        cmp #%11111110
        beq is1
        cmp #%11110111
        beq is2
        
        lda #$FF
        rts
    is1:
        lda #0
        rts
    is2:
        lda #1
        rts
    is3:
        lda #2
        rts


a bit longer, but you leave $7f in $dc00 which is cleaner I suppose.
2023-01-03 15:05
Krill

Registered: Apr 2002
Posts: 2852
How sensible is it to compare the keyboard matrix row bytes ($dc01) with literal values, rather than checking for individual bits to be clear?

This mostly depends on desired tolerance of key rollovers, i guess, and 1 vs 2 (same matrix column) would have to have a preferred default when both are pressed.
2023-01-03 17:22
tlr

Registered: Sep 2003
Posts: 1723
Quote: How sensible is it to compare the keyboard matrix row bytes ($dc01) with literal values, rather than checking for individual bits to be clear?

This mostly depends on desired tolerance of key rollovers, i guess, and 1 vs 2 (same matrix column) would have to have a preferred default when both are pressed.


I guess for a key check with immediate action as opposed to keyboard input this is ok. If you hold 1 and 2 when the routine is entered nothing happens, but if repeatedly called then as soon as only one of them is pressed it will trigger.
2023-01-03 18:36
Monte Carlos

Registered: Jun 2004
Posts: 351
There should only be reaction on non mistakable input. So it would be ok.
I think the cases structure in my code could be optimized away using ldx #0 and then inx on every unmatched condition. This would make it a little shorter. However, I needed it fast. So I didn't mind.
2023-01-03 22:10
Silver Dream !

Registered: Nov 2005
Posts: 107
If you have system vars intact then even w/o IRQs:
:	JSR $FF9F ; SCNKEY
	JSR $FFE4 ; GETIN
	BEQ :-
	CMP #$34  ; >= code for '4'?
	BCS :-
	CMP #$31  ; < code for '1'?
	BCC :-
	AND #$0F  ; optional
2023-01-03 22:17
TheRyk

Registered: Mar 2009
Posts: 2076
Quoting Monte Carlos
...

I'm working with this keyboard matrix: https://sta.c64.org/cbm64kbdlay.html


Especially as you're German, I've always found this one useful:
https://www.c64-wiki.de/wiki/Tastatur#Schaltung
much clearer imho than the English equivalent
https://www.c64-wiki.com/wiki/Keyboard#Keyboard_Matrix

@Silver-Dream: KERNAL stuff of course often does it as well (i.e. if you can and want to use ROM in your situation), but I thought TS really wanted to remember how to do it properly by reading CIA regs without accessing ROMs
2023-01-03 23:14
TWW

Registered: Jul 2009
Posts: 541
Quoting Monte Carlos
There should only be reaction on non mistakable input. So it would be ok.


Only way to ensure there is no shadowing (wrongfully reported keys due to 3 keys or more being pressed simultaneously), is to check the entire matrix. Kernal will not help wth this.
RefreshSubscribe to this thread:

You need to be logged in to post in the forum.

Search the forum:
Search   for   in  
All times are CET.
Search CSDb
Advanced
Users Online
Knut Clausen/SHAPE/F..
Frostbyte/Artline De..
encore
Mibri/ATL^MSL^PRX
E$G/hOKUtO fOrcE
Airwolf/F4CG
Guests online: 147
Top Demos
1 Next Level  (9.8)
2 Mojo  (9.7)
3 Coma Light 13  (9.7)
4 Edge of Disgrace  (9.6)
5 Comaland 100%  (9.6)
6 No Bounds  (9.6)
7 Uncensored  (9.6)
8 Wonderland XIV  (9.6)
9 Memento Mori  (9.6)
10 Bromance  (9.5)
Top onefile Demos
1 It's More Fun to Com..  (9.7)
2 Party Elk 2  (9.7)
3 Cubic Dream  (9.6)
4 Copper Booze  (9.5)
5 TRSAC, Gabber & Pebe..  (9.5)
6 Rainbow Connection  (9.5)
7 Dawnfall V1.1  (9.5)
8 Quadrants  (9.5)
9 Daah, Those Acid Pil..  (9.5)
10 Birth of a Flower  (9.5)
Top Groups
1 Nostalgia  (9.3)
2 Oxyron  (9.3)
3 Booze Design  (9.3)
4 Censor Design  (9.3)
5 Crest  (9.3)
Top Webmasters
1 Slaygon  (9.7)
2 Perff  (9.6)
3 Morpheus  (9.5)
4 Sabbi  (9.5)
5 CreaMD  (9.1)

Home - Disclaimer
Copyright © No Name 2001-2024
Page generated in: 0.043 sec.