Page 1 of 1

Multi-Key Handling

Posted: Thu Aug 22, 2019 12:58 pm
by goyo
Hi,

I m looking for a multi-key press handling C script

I saw that Chema did it with Blake's 7 ( with the arrow ) . but don't found this code.

Is it possible to use a such routine since the C ?

Re: Multi-Key Handling

Posted: Thu Aug 22, 2019 2:30 pm
by Chema
Hi goyo!

For keyboard handling I always use an idea from Dbug, which consists in updating a virtual matrix of 1s and 0s representing the state of each key (pressed or released). This matrix is updated from the interrupt service routine. You can find the code in assembler here http://miniserve.defence-force.org/svn/ ... keyboard.s

The routine is called ReadKeyboard. There are also 3 more routines used to read a single key and return its ASCII code, with and without bouncing (repeating) and one to wait for a key press.

It is surely possible to add the Readkeyboard routine in asm to your C project and export the virtual matrix as a global C variable so you can use it from C. The ReadKeyboard routine should be called from the interrupt handler, but that can be done with the chain_irq library function or directly in asm in your project.

I could give some examples, but I am not near my laptop now. Holidays, you know ;)

Hope this helps.

Re: Multi-Key Handling

Posted: Thu Aug 22, 2019 3:05 pm
by goyo
Thanks a lot Chema i think that is i wanted !

I ll try to understand this script... and call it from C

have good Holidays , maybe a good time to think about a new genial Oric game ! :wink:

Re: Multi-Key Handling

Posted: Thu Aug 22, 2019 3:12 pm
by ibisum
Awesome thread!

Re: Multi-Key Handling

Posted: Thu Aug 22, 2019 4:07 pm
by iss
In my projects I'm using the Dbug/Chema keyboard routines too (with some minor modifications related to cc65 compatibility) - everything works nice!

But for multi-key handling there is already another routine from Geoff Philips's
Oric Atmos and Oric 1 Graphics and Machine code techniques
- in DF library (see chap.4).
The idea is to "ask" for status of particular key. Here is the code (ready for copy/paste):

Code: Select all

; ------------------------------------------------------------------
; from Oric Atmos and Oric 1 Graphics and Machine code techniques
; by Geoff Philips (See chapter 4.)
; ------------------------------------------------------------------
; Routine to check for particular key
; ------------------------------------------------------------------
;  Key required                Accumulator               X register
; ------------------------------------------------------------------
;  1 2 3                       0 2 0                     DF BF 7F
;  4 5 6                       2 0 2                     F7 FB FD
;  7 8 9                       0 7 3                     FE FE FD
;  0 -=                        7 3 7                     FB F7 7F
;  \ ESC Q                     3 1 1                     BF DF BF
;  W E R                       6 6 1                     7F F7 FB
;  T Y U                       1 6 5                     FD FE FE
;  I 0 P                       5 5 5                     FD FB F7
;  [ ] DEL                     5 5 5                     7F BF DF
;  CTRL A S                    2 6 6                     EF DF BF
;  D F G                       1 1 6                     7F F7 FB
;  H J K                       6 1 3                     FD FE FE
;  L ; “                       7 3 3                     FD FB 7F
;  RETURN                      7                         DF
;  SHIFT (LEFT)                4                         EF
;  Z X C                       2 0 2                     DF BF 7F
;  VBN                         0 2 0                     F7 FB FD
;  M comma period              2 4 4                     FE FD FB
;  / SHIFT (RIGHT)             7 7                       F7 EF
;  LEFT ARROW                  4                         DF
;  DOWN ARROW                  4                         BF
;  SPACE                       4                         FE
;  UP ARROW                    4                         F7
;  RIGHT ARROW                 4                         7F
; ------------------------------------------------------------------
check_key
        php
        sei
        pha
        lda     #$0e
        jsr     $f590 ; ROM1.0 $f535
        pla
        ora     #$b8
        sta     $0300
        ldx     #$04
delay   dex     
        bne     delay
        lda     $0300
        and     #$08
        tax
        plp
        txa
        rts
Next you need to create small sub-routines per your needs, for instance to check if keys 'A' AND 'Z' are pressed export these functions:

Code: Select all

;  key A => 6 + DF
_check_A
        lda     #$06
        ldx     #$df
        jmp     check_key

;  key Z => 2 + DF
_check_Z
        lda     #$02
        ldx     #$df
        jmp     check_key
Now you can use from C:

Code: Select all

if (check_A() && check_Z())
{
    // both keys A and Z are pressed
}
That's it! :)

Re: Multi-Key Handling

Posted: Thu Aug 22, 2019 4:14 pm
by ibisum
Also nice, but what I like about Dbug/Chema/Twi's approach, is the bonus sound handler ..

Re: Multi-Key Handling

Posted: Sat Aug 24, 2019 9:28 am
by goyo
iss wrote:
Thu Aug 22, 2019 4:07 pm
In my projects I'm using the Dbug/Chema keyboard routines too (with some minor modifications related to cc65 compatibility) - everything works nice!

But for multi-key handling there is already another routine from Geoff Philips's
Oric Atmos and Oric 1 Graphics and Machine code techniques
- in DF library (see chap.4).
The idea is to "ask" for status of particular key. Here is the code (ready for copy/paste):

Code: Select all

; ------------------------------------------------------------------
; from Oric Atmos and Oric 1 Graphics and Machine code techniques
; by Geoff Philips (See chapter 4.)
; ------------------------------------------------------------------
; Routine to check for particular key
; ------------------------------------------------------------------
;  Key required                Accumulator               X register
; ------------------------------------------------------------------
;  1 2 3                       0 2 0                     DF BF 7F
;  4 5 6                       2 0 2                     F7 FB FD
;  7 8 9                       0 7 3                     FE FE FD
;  0 -=                        7 3 7                     FB F7 7F
;  \ ESC Q                     3 1 1                     BF DF BF
;  W E R                       6 6 1                     7F F7 FB
;  T Y U                       1 6 5                     FD FE FE
;  I 0 P                       5 5 5                     FD FB F7
;  [ ] DEL                     5 5 5                     7F BF DF
;  CTRL A S                    2 6 6                     EF DF BF
;  D F G                       1 1 6                     7F F7 FB
;  H J K                       6 1 3                     FD FE FE
;  L ; “                       7 3 3                     FD FB 7F
;  RETURN                      7                         DF
;  SHIFT (LEFT)                4                         EF
;  Z X C                       2 0 2                     DF BF 7F
;  VBN                         0 2 0                     F7 FB FD
;  M comma period              2 4 4                     FE FD FB
;  / SHIFT (RIGHT)             7 7                       F7 EF
;  LEFT ARROW                  4                         DF
;  DOWN ARROW                  4                         BF
;  SPACE                       4                         FE
;  UP ARROW                    4                         F7
;  RIGHT ARROW                 4                         7F
; ------------------------------------------------------------------
check_key
        php
        sei
        pha
        lda     #$0e
        jsr     $f590 ; ROM1.0 $f535
        pla
        ora     #$b8
        sta     $0300
        ldx     #$04
delay   dex     
        bne     delay
        lda     $0300
        and     #$08
        tax
        plp
        txa
        rts
Next you need to create small sub-routines per your needs, for instance to check if keys 'A' AND 'Z' are pressed export these functions:

Code: Select all

;  key A => 6 + DF
_check_A
        lda     #$06
        ldx     #$df
        jmp     check_key

;  key Z => 2 + DF
_check_Z
        lda     #$02
        ldx     #$df
        jmp     check_key
Now you can use from C:

Code: Select all

if (check_A() && check_Z())
{
    // both keys A and Z are pressed
}
That's it! :)
Thank you Iss, It's work fine !!!

Code: Select all

unsigned char check_A();
unsigned char check_7();

void main()
{  
    while (1)
    {
	if (check_A() && check_Z())
	{
	printf("// both keys A and Z are pressed !\n");
	}
    }
}

Re: Multi-Key Handling

Posted: Wed Aug 28, 2019 12:37 pm
by goyo
Chema wrote:
Thu Aug 22, 2019 2:30 pm
Hi goyo!

For keyboard handling I always use an idea from Dbug, which consists in updating a virtual matrix of 1s and 0s representing the state of each key (pressed or released). This matrix is updated from the interrupt service routine. You can find the code in assembler here http://miniserve.defence-force.org/svn/ ... keyboard.s

The routine is called ReadKeyboard. There are also 3 more routines used to read a single key and return its ASCII code, with and without bouncing (repeating) and one to wait for a key press.

It is surely possible to add the Readkeyboard routine in asm to your C project and export the virtual matrix as a global C variable so you can use it from C. The ReadKeyboard routine should be called from the interrupt handler, but that can be done with the chain_irq library function or directly in asm in your project.

I could give some examples, but I am not near my laptop now. Holidays, you know ;)

Hope this helps.
In my C program, can I use the matrix like this to read it ?

Code: Select all

unsigned char extern KeyBank[8];

keyboard.s
; The virtual Key Matrix
_KeyBank .dsb 8


Re: Multi-Key Handling

Posted: Wed Aug 28, 2019 2:58 pm
by Chema
Yep :D

Re: Multi-Key Handling

Posted: Tue Sep 03, 2019 10:57 am
by goyo
multikey.zip
(9.7 KiB) Downloaded 4 times
It's work fine ! Thank you Chema!

It's very nice to make a multi-player game !! :wink:
I think they are no many on Oric.

Code: Select all

void extern InitISR();

unsigned char extern KeyBank[8];

void main()
{
  unsigned char i;	
    
  InitISR();
  
  while (1) 
  {
	gotoxy(4,10);
	for (i=0;i<8;i++) 
		printf("[%d]",peek(KeyBank+i));;		
  }  
}
 

Re: Multi-Key Handling

Posted: Tue Sep 03, 2019 10:47 pm
by Chema
Don't thank me! It was Dbug's idea :D