Jump to content
IGNORED

Button check - am I doing this the hard way?


Karl G

Recommended Posts

I have some code to check for the fire button, set a bit as a button restrainer if fire is hit and play a sound, or clear the bit if the fire button is not pressed. It works fine, but it seems like an awful lot of code to me. Is there a more optimal way to accomplish this than what I am doing here? Here are the relevant bits of code:

Boolean         ds 1
FireHitBit0 = Boolean
bit0 = %00000001
bit0mask = %11111110

And...

    bit INPT4
    bmi ____no_button_press
    lda FireHitBit0
    and #bit0
    bne ____end_button_check
    lda FireHitBit0
    ora #bit0
    sta FireHitBit0
    lda #31
    sta AUDF0
    lda #4
    sta AUDC0
    lda #$F
    sta AUDV0
    jmp ____end_button_check
____no_button_press
    lda FireHitBit0
    and #bit0mask
    sta FireHitBit0
____end_button_check

Thanks in advance for any input.

Link to comment
Share on other sites

Hi there,

 

Do you need D0 for this check or can you use D7? Are the other registers free for use too? If you can use D7 then it could be a simple compare as such...

   lda INPT4                        ; read left port action button
   and #%10000000                   ; safe to avoid any reads from D6 - D0
   bmi .actionButtonNotPressed      ; branch if action button not pressed
   cmp actionButtonDebounce
   beq .endActionButtonCheck        ; branch if action button held previously
   ldx #31
   stx AUDF0
   stx AUDV0                        ; only lower nybble used for volume
   ldx #4
   stx AUDC0
.actionButtonNotPressed
   sta actionButtonDebounce         ; set debounce value for next frame compare
.endActionButtonCheck
Edited by DEBRO
  • Like 1
Link to comment
Share on other sites

Thanks! This is the kind of thing I was looking for. Presumably I need to do an ORA actionButtonDebounce before the STA line to avoid wiping out the other bit values in the byte, though?

 

Edit: it looks like the "cmp actionButtonDebounce" is also assuming that bit 7 is the only bit used in the actionButtonDebounce byte?

Link to comment
Share on other sites

Hi there

 

Thanks! This is the kind of thing I was looking for. Presumably I need to do an ORA actionButtonDebounce before the STA line to avoid wiping out the other bit values in the byte, though?

 

Edit: it looks like the "cmp actionButtonDebounce" is also assuming that bit 7 is the only bit used in the actionButtonDebounce byte?

 

There should be no need to ORA before setting the value. This is because the value is set from the read of INPT4 and should represent the state when comparing in the next frame.

 

You are correct that I'm assuming the other bits of actionButtonDebounce are not used. If you need to use those bits for other needs; you would need to isolate them before the compare.

Link to comment
Share on other sites

here's one

 

 
    ;  a = accumulator  C = carry flag, I = INPT4,  f = your flags,
    ;  f7 = bit 7 of flags, the debounce (restrainer) bit (sort of, maybe you'd say that was C)
    ;  f7 = previous I7  we want to check for !I7 & f7 = 1
 
    lda flags
    and #$80
    clc
    sbc INPT4     ;   C = !I7 & f7,  a7 = !I7 ^ f7
    and #$80
    eor flags     ;  a7 = !I7
    eor #$80
    sta flags
    bcc skip_audio
    
Edited by bogax
  • Like 2
Link to comment
Share on other sites

  • 1 month later...

Bit0 version

   lsr FireHitBit0
   lda INPT4
   bcc previouslyheld
   bmi notpressed
   ldx #31
   stx AUDF0
   stx AUDV0
   ldx #4
   stx AUDC0
previouslyheld
notpressed
   rol
   rol FireHitBit0

To use bit7 instead, edit the first and last lines to use asl and ror, respectively (the lone rol remains unchanged...that is the current button status going into carry).

 

A side benefit using carry to do all the work is that it will be clear following this.

 

If you want the bit to be SET when the button is pressed/held, alter bcc and bmi to be bcs and bpl, and insert EOR #$80 just above 'em.

 

Carry is still clear, too.

Edited by Nukey Shay
  • Like 1
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...