accessing keyboard vector

All aspects of programming on the Commander X16.
ch4rl3sb
Posts: 42
Joined: Fri May 12, 2023 10:22 am

accessing keyboard vector

Post by ch4rl3sb »

I finally got my joystick and mouse functions working. I saw the clue on another thread and a post by Matt Heffernan that the cc65 inline assembler wasn't very robust. (I had tried to eliminate my dual asm and c code files for using only c and asm inline statements and it didn't work.)

Now, I'm trying to use the new emulator v43 keyboard vector at $032E. (using KERNAL GETIN function has a delay between the first key input and the keyboard repeat, and it's noticable. You get one input, then a delay, then the constant keydown behavior. This vector was supposed to eliminate that.) I set it up exactly like my irq handler, but it runs to end of ram and crashes the system.

Code: Select all

__init_keyboard:           ;replace irq handler with custom.
    sei
    lda KYBVec
    sta default_keyboard
    lda KYBVec+1
    sta default_keyboard+1

    lda #<custom_keyboard
    sta KYBVec
    lda #>custom_keyboard
    sta KYBVec+1
    cli
    rts

  custom_keyboard:
    stx ZP_PTR_1
    sta ZP_PTR_1+1
    and #$7F
    tax
    lda #0
    cpx ZP_PTR_1+1
    adc #0
    sta __Keyboard_keys,x
    lda ZP_PTR_1+1
    ldx ZP_PTR_1
    jmp (default_keyboard)
    
__restore_keyboard:
    sei
    lda default_keyboard
    sta KYBVec
    lda default_keyboard+1
    sta KYBVec+1
    cli
    rts
DragWx
Posts: 324
Joined: Tue Mar 07, 2023 9:07 pm

Re: accessing keyboard vector

Post by DragWx »

I don't see anything wrong with the code itself, so without knowing anything else, the only other thing I can think of is if modifying ZP_PTR_1 clobbers something, or if the __Keyboard_keys array is somehow not the correct size (should be 128).
Stefan
Posts: 448
Joined: Thu Aug 20, 2020 8:59 am

Re: accessing keyboard vector

Post by Stefan »

custom_keyboard must return with an rts, not a jmp. That is probably why it crashes.
DragWx
Posts: 324
Joined: Tue Mar 07, 2023 9:07 pm

Re: accessing keyboard vector

Post by DragWx »

It can just rts, but I don't think that's what you're intended to do.

The default keyhdl vector points to the instruction just after the original indirect jump to keyhdl (in the fetch_key_code routine), which happens to be an rts right now, but may not always be.

The save and jump you're doing already is, I think, correct.
Stefan
Posts: 448
Joined: Thu Aug 20, 2020 8:59 am

Re: accessing keyboard vector

Post by Stefan »

I wrote that function back in the days, and rts was the intended way to exit the handler
DragWx
Posts: 324
Joined: Tue Mar 07, 2023 9:07 pm

Re: accessing keyboard vector

Post by DragWx »

You're right, I was reading too deeply into the source code's structure.
ch4rl3sb
Posts: 42
Joined: Fri May 12, 2023 10:22 am

Re: accessing keyboard vector

Post by ch4rl3sb »

I've tried rts as well. Neither works for me.
Question though. Why would the vector handlers be used in two different ways?
That is irq uses a jmp back to the default handler but
Keyboard vector uses an rts?
Besides, wouldn't rts bypass whatever default handler was stored at 032E?

(As to the other comments. My Keyboard buffer is 128 bytes. And nothing else uses that zero page pointer)
Stefan
Posts: 448
Joined: Thu Aug 20, 2020 8:59 am

Re: accessing keyboard vector

Post by Stefan »

Hi,

It might work to make a jump to the original vector, but it was not the intended use.

Anyway, I took your code, made some adjustments so it could be run as program and assembled it with CA65. It works as far as I can tell. Code included below

Code: Select all

;Build with: cl65 -v -o TEST.PRG -u __EXEHDR__ -t cx16 test.asm

init_keyboard:           ;replace irq handler with custom.
    sei
    lda KYBVec
    sta default_keyboard
    lda KYBVec+1
    sta default_keyboard+1

    lda #<custom_keyboard
    sta KYBVec
    lda #>custom_keyboard
    sta KYBVec+1
    cli
    rts

custom_keyboard:
    stx ZP_PTR_1
    sta ZP_PTR_1+1
    and #$7F
    tax
    lda #0
    cpx ZP_PTR_1+1
    adc #0
    sta __Keyboard_keys,x
    lda ZP_PTR_1+1
    ldx ZP_PTR_1
    jmp (default_keyboard)
    
restore_keyboard:
    sei
    lda default_keyboard
    sta KYBVec
    lda default_keyboard+1
    sta KYBVec+1
    cli
    rts

  KYBVec = $032e
  default_keyboard: .res 2
  __Keyboard_keys = $2000
  ZP_PTR_1 = $22
Stefan
Posts: 448
Joined: Thu Aug 20, 2020 8:59 am

Re: accessing keyboard vector

Post by Stefan »

BTW, you are using R43, right?
ch4rl3sb
Posts: 42
Joined: Fri May 12, 2023 10:22 am

Re: accessing keyboard vector

Post by ch4rl3sb »

Stefan wrote: Sat Jul 15, 2023 7:51 am BTW, you are using R43, right?
Yes. That was a whole other three page thread getting that working.

This isn't the first time someone took my code, compiled it themselves, and got it to work.
Which makes me think I'm struggling as much, or more, with my dev environment as with programming.

I'm using cygwin so I can have a make file that compiled my asm and c code together, but I'm clueless how it works. But it seems the cc65,ca65,cl65, it's using is giving me problems. I downloaded a newer cc65, But I don't know how to use it separately to link my files together. (Which is why I previously tried to merge everything into one c file, (inline asm,) and just had more problems.)
I am clueless on compilers and the dev environment.
Post Reply