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 > looking for rom independent keyboard read routine
2010-04-24 07:07
Oswald

Registered: Apr 2002
Posts: 5094
looking for rom independent keyboard read routine

I'm lazy, anyone can help me out? I'd need shift+crsrs + return aswell, not just alphanumeric stuff.
thanks.
 
... 30 posts hidden. Click here to view all posts....
 
2010-04-25 07:51
Oswald

Registered: Apr 2002
Posts: 5094
all keys + shift handling, one key at a time max:

(btw at groepaz's code I think dd02/3 should be dc02/3 or not needed)

lastkey	= $10
actkey	= $11
mask	= $12

matrixlo	= $13
matrixhi	= $14

table	= $0f00

	*= $1000
	
	lda #$37
	sta $01
	jsr $e544
	
	sei
	lda #$35
	sta $01
	
	ldx #$00
	lda #$00
-	sta table,x
	dex
	bne -
	
	lda #$ff
	sta lastkey
	
	lda #$08		;helper table to index into keyboard matrix
	sta table+%01111111
	lda #$07
	sta table+%10111111
	lda #$06
	sta table+%11011111
	lda #$05
	sta table+%11101111
	lda #$04
	sta table+%11110111
	lda #$03
	sta table+%11111011
	lda #$02
	sta table+%11111101
	lda #$01
	sta table+%11111110
	
	lda #$08		;left shift and another key
	sta table+%01111111
	lda #$07
	sta table+%00111111
	lda #$06
	sta table+%01011111
	lda #$05
	sta table+%01101111
	lda #$04
	sta table+%01110111
	lda #$03
	sta table+%01111011
	lda #$02
	sta table+%01111101
	lda #$01
	sta table+%01111110	
	
	lda #$08		;right shift and another key
	sta table+%01101111
	lda #$07
	sta table+%10101111
	lda #$06
	sta table+%11001111
	lda #$05
	sta table+%11101111
	lda #$04
	sta table+%11100111
	lda #$03
	sta table+%11101011
	lda #$02
	sta table+%11101101
	lda #$01
	sta table+%11101110


	;endless dummy 
	
uu	jsr keyscan
	lda actkey
	cmp #$ff	;$ff= no key pressed
	beq +
	cmp #$40	;convert to screen codes
	bmi ok
	sec
	sbc #$40
ok
	sta $0400
+	jmp uu
	

	
	

keyscan	lda #%11111110
	sta mask
	
	lda #%11111101
	sta $dc00
	lda $dc01
	and #%10000000
	beq shifted	;left shift pressed

	lda #%10111111
	sta $dc00
	lda $dc01
	and #%00010000
	beq shifted	;right shift pressed
	
	lda #<keytabunshifted
	sta matrixlo
	lda #>keytabunshifted
	sta matrixhi
	jmp scan

shifted
	lda #<keytabshifted
	sta matrixlo
	lda #>keytabshifted
	sta matrixhi
	
scan	ldx #$07
	
rowloop	lda mask	
	sta $dc00
	ldy $dc01
	lda table,y
	beq next
		
	tay
	lda (matrixlo),y
	cmp #$01
	beq next		;skip left shift
	cmp #$02
	beq next		;skip right shift
	cmp lastkey
	beq debounce
	sta lastkey
	sta actkey
	rts
	
next	
	sec
	rol mask
		
	lda matrixlo
	clc
	adc #$09
	sta matrixlo
	bcc *+4
	inc matrixhi
	
	dex
	bpl rowloop
	rts
	
debounce	lda #$ff
	sta actkey
	rts
		
	;unshifted
	
keytabunshifted

	.byte $ff,$14,$0D,$1D,$88,$85,$86,$87,$11 ;0
	.byte $ff,$33,$57,$41,$34,$5A,$53,$45,$01 ;1
	.byte $ff,$35,$52,$44,$36,$43,$46,$54,$58 ;2
	.byte $ff,$37,$59,$47,$38,$42,$48,$55,$56 ;3
	.byte $ff,$39,$49,$4A,$30,$4D,$4B,$4F,$4E ;4
	.byte $ff,$2B,$50,$4C,$2D,$2E,$3A,$40,$2C ;5
	.byte $ff,$5C,$2A,$3B,$13,$01,$3D,$5E,$2F ;6
	.byte $ff,$31,$5F,$04,$32,$20,$02,$51,$03 ;7
	.byte $ff

keytabshifted	
	.byte $ff,$94,$8D,$9D,$8C,$89,$8A,$8B,$91
	.byte $ff,$23,$D7,$C1,$24,$DA,$D3,$C5,$01
	.byte $ff,$25,$D2,$C4,$26,$C3,$C6,$D4,$D8
	.byte $ff,$27,$D9,$C7,$28,$C2,$C8,$D5,$D6
	.byte $ff,$29,$C9,$CA,$30,$CD,$CB,$CF,$CE
	.byte $ff,$DB,$D0,$CC,$DD,$3E,$5B,$BA,$3C
	.byte $ff,$A9,$C0,$5D,$93,$01,$3D,$DE,$3F
	.byte $ff,$21,$5F,$04,$22,$A0,$02,$D1,$83
	.byte $ff


	;one extra $ff column is added, because the zero value is used to detect unpressed kays
2010-05-29 13:43
TWW

Registered: Jul 2009
Posts: 545
Quote: here is a crude table I made for this kind of routine a while back (Don't remeber where i got this info (maybee 3 key rollover or the ref. manual). Maybee it could be of some use...


$dc00 is used for selecting one of eight the scan rows as folows and the $dc01 value is then peeked:
+----+----------------------+--------------------------------------------------- ----------------------------------------------------+
|    |                      |                                Peek from $dc01 (code in paranthesis):                                 |
|row:| $dc00:               +------------+------------+------------+------------+------------+------------+- -----------+------------+
|    |                      |   BIT 8    |   BIT 7    |   BIT 6    |   BIT 5    |   BIT 4    |   BIT 3    |   BIT 2    |   BIT 1    |
+----+----------------------+------------+------------+------------+------------ +------------+------------+------------+------------+
|1.  | #%11111110 (254/$fe) | DOWN  ($  )|   F5  ($  )|   F3  ($  )|   F1  ($  )|   F7  ($  )| RIGHT ($  )| RETURN($  )|DELETE ($  )|
|2.  | #%11111101 (253/$fd) |LEFT-SH($  )|   e   ($05)|   s   ($13)|   z   ($1a)|   4   ($34)|   a   ($01)|   w   ($17)|   3   ($33)|
|3.  | #%11111011 (251/$fb) |   x   ($18)|   t   ($14)|   f   ($06)|   c   ($03)|   6   ($36)|   d   ($04)|   r   ($12)|   5   ($35)|
|4.  | #%11110111 (247/$f7) |   v   ($16)|   u   ($15)|   h   ($08)|   b   ($02)|   8   ($38)|   g   ($07)|   y   ($19)|   7   ($37)|
|5.  | #%11101111 (239/$ef) |   n   ($0e)|   o   ($0f)|   k   ($0b)|   m   ($0d)|   0   ($30)|   j   ($0a)|   i   ($09)|   9   ($39)|
|6.  | #%11011111 (223/$df) |   ,   ($2c)|   @   ($00)|   :   ($3a)|   .   ($2e)|   -   ($2d)|   l   ($0c)|   p   ($10)|   +   ($2b)|
|7.  | #%10111111 (191/$bf) |   /   ($2f)|   ^   ($1e)|   =   ($3d)|RGHT-SH($  )|  HOME ($  )|   ;   ($3b)|   *   ($2a)|   £   ($1c)|
|8.  | #%01111111 (127/$7f) | STOP  ($  )|   q   ($11)|COMMODR($  )| SPACE ($20)|   2   ($32)|CONTROL($  )|  <-   ($1f)|   1   ($31)|
+----+----------------------+------------+------------+------------+------------ +------------+------------+------------+------------+


I have made a routine aswell for keyboard scanning.

However right now i am using the X & Y-regs to pass any special keys like CTRL, C=, etc.

Are there any standard over which codes should be "inserted" in my keyscan matrix above?

I'm asking coz I want my routine to be as "standard" as possible :)
2010-05-29 17:57
Devia

Registered: Oct 2004
Posts: 401
If you want standard, use KERNAL!

I wrote a routine some years back. Basic operation of that goes something like this: An IRQ routine takes a snapshot of the current state of the matrix. Then it updates 2 other tables with key up and key down states. The key up/down matrices only gets cleared when a message is passed to the IRQ routine from "user code" to do so.
The "user code", running outside IRQ or in another IRQ or whatever, can then choose to do whatever it wants with the key up/down events and then acknowledge them once they have been handled.

For the "user code" to check/ack keys I use macros like these:
.macro KBCheckKey key			;Returns zero if the checked key is pressed
	ldy	#<(key)			;Keyboard matrix row number in Y
	lda	#>(key)			;Keyboard matrix row value for given key in A
	and	KBMatrixSnapShot,y	;Check given key value against row value in matrix
.endmacro				;If equal, Zero flag is cleared

.macro KBCheckKeyDown key		;Returns zero if the checked key has a Key Down event
	ldy	#<(key)			;Keyboard matrix row number in Y
	lda	#>(key)			;Keyboard matrix row value for given key in A
	and	KBMatrixKeyDown,y	;Check given key value against row value in matrix
.endmacro				;If equal, Zero flag is cleared

.macro KBCheckKeyUp key			;Returns zero if the checked key has a Key Up event
	ldy	#<(key)			;Keyboard matrix row number in Y
	lda	#>(key)			;Keyboard matrix row value for given key in A
	and	KBMatrixKeyUp,y		;Check given key value against row value in matrix
.endmacro				;If equal, Zero flag is cleared

.macro KBAckEvents			;Acknowledge all Key Up/Down events
	lda	#$01
	ora	KBMSG
	sta	KBMSG
.endmacro


The keys have then been defined like the following:
KEY_F3		= $2000	;.byte	%00100000,0
KEY_F5		= $4000	;.byte	%01000000,0
KEY_CRSRUD	= $8000	;.byte	%10000000,0

KEY_3		= $0101	;.byte	%00000001,1
KEY_W		= $0201	;.byte	%00000010,1
KEY_A		= $0401	;.byte	%00000100,1


Each key combined by column/row in 2 bytes. (I'm writing $dc00 and reading $dc01)

Is that a KB handler then? No. It's more like a KB hardware driver for which I have to write a handler every time. But since I haven't needed full textual input for any of my projects yet, I haven't even thought out any general purpose style kb handler.
2010-05-29 20:06
TWW

Registered: Jul 2009
Posts: 545
Read the post TOPIC!

Forget about ROM.

To read and decipher $dc00/dc01 isn't a problem...


The problem is to assign the non-graphical keys to the scan-matrix you can find in my post above and return a value which means "CTRL" pressed or somesuch instead of passing the information via bits set/clear in reg's / stack etc...

This also means you don't need to scan for special keys and decipher them differently / pass they differently then ordinary alphanumeric keys if they have a proper code assigned.
2012-04-07 15:41
Hein

Registered: Apr 2004
Posts: 954
How's it possible that when I have this pretty piece of code:

lda #$fd
sta $dc00

lda $dc01
eor #$ff

I get only 2 bits from $dc01 when I press keys 3-4-E at the same time, but I get 3 bits from $dc01 when I press 3-W-A at the same time. According to this matrix sta.c64.org/cmb64kbdlay.html those keys are on the respective selected row.

This happens in VICE btw, and on other row/column combinations as well.
2012-04-07 16:42
Repose

Registered: Oct 2010
Posts: 225
Keyboards explained in detail in Transactor 5-05.
http://cbm.csbruce.com/cbm/transactor/v5/i5/

Also, distinguish left from right shift
ftp://n2dvm.com/Commodore/Commie-CDs/Kazez%20FREE-CD/c64-knowle..
2012-04-07 17:29
Hein

Registered: Apr 2004
Posts: 954
Quote: Keyboards explained in detail in Transactor 5-05.
http://cbm.csbruce.com/cbm/transactor/v5/i5/

Also, distinguish left from right shift
ftp://n2dvm.com/Commodore/Commie-CDs/Kazez%20FREE-CD/c64-knowle..


The transactor pdf is interesting, but it doesn't answer my question.

I only need to know if I can do a 3-key-at-once check (4 if you include shift/cbm-key) by simply reading the column byte each row and shift it 8 times to get all the key-bits in that row.

Seems bits get lost with some combinations of keys.
2012-04-07 19:10
Repose

Registered: Oct 2010
Posts: 225
I think you'll have to try it. I believe there's physical limits to how many keys can be read in one row/column due to the power available. I know Marko Makela has the answer. If you search for him and keyboards he's done a clever trick with reading caps lock or shift even though they are physically connected, or some such thing.

Sorry I didn't give best answer, I didn't have a lot of time. We celebrate Easter in my country.
2012-04-07 19:26
Hein

Registered: Apr 2004
Posts: 954
Quote: I think you'll have to try it. I believe there's physical limits to how many keys can be read in one row/column due to the power available. I know Marko Makela has the answer. If you search for him and keyboards he's done a clever trick with reading caps lock or shift even though they are physically connected, or some such thing.

Sorry I didn't give best answer, I didn't have a lot of time. We celebrate Easter in my country.


The 'physical limits' sounds like a reasonable posibility.

I've tried it, btw, wondering if my routine was borked or the hardware is limited.

The simplest check is what I wrote in my 1st post in this thread, reading the column bits in one row and it turned out some key-combinations in that row result in all proper bits, whereas other key-combinations in that row result in max 2 bits.

Thanks for the effort anyway :) Happy easter.
2012-04-07 19:29
Frantic

Registered: Mar 2003
Posts: 1648
Can't really give solid info either, but I can just confirm that I have also noticed that some key combinations work, and some don't, when you use some key combinations that involve several keys. Seems to be a hardware thing, yes..
Previous - 1 | 2 | 3 | 4 - Next
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
Guests online: 117
Top Demos
1 Next Level  (9.7)
2 13:37  (9.7)
3 Mojo  (9.7)
4 Coma Light 13  (9.6)
5 Edge of Disgrace  (9.6)
6 What Is The Matrix 2  (9.6)
7 The Demo Coder  (9.6)
8 Uncensored  (9.6)
9 Comaland 100%  (9.6)
10 Wonderland XIV  (9.6)
Top onefile Demos
1 No Listen  (9.6)
2 Layers  (9.6)
3 Cubic Dream  (9.6)
4 Party Elk 2  (9.6)
5 Copper Booze  (9.6)
6 Dawnfall V1.1  (9.5)
7 Rainbow Connection  (9.5)
8 Onscreen 5k  (9.5)
9 Morph  (9.5)
10 Libertongo  (9.5)
Top Groups
1 Performers  (9.3)
2 Booze Design  (9.3)
3 Oxyron  (9.3)
4 Triad  (9.3)
5 Censor Design  (9.3)
Top Webmasters
1 Slaygon  (9.6)
2 Perff  (9.6)
3 Sabbi  (9.5)
4 Morpheus  (9.4)
5 CreaMD  (9.1)

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