+Karl G Posted January 2, 2019 Share Posted January 2, 2019 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. Quote Link to comment Share on other sites More sharing options...
DEBRO Posted January 3, 2019 Share Posted January 3, 2019 (edited) 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 January 3, 2019 by DEBRO 1 Quote Link to comment Share on other sites More sharing options...
+Karl G Posted January 3, 2019 Author Share Posted January 3, 2019 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? Quote Link to comment Share on other sites More sharing options...
DEBRO Posted January 3, 2019 Share Posted January 3, 2019 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. Quote Link to comment Share on other sites More sharing options...
bogax Posted January 7, 2019 Share Posted January 7, 2019 (edited) 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 January 7, 2019 by bogax 2 Quote Link to comment Share on other sites More sharing options...
Nukey Shay Posted March 2, 2019 Share Posted March 2, 2019 (edited) 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 March 2, 2019 by Nukey Shay 1 Quote Link to comment Share on other sites More sharing options...
Recommended Posts
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.