Jump to content
Random Terrain

Stupid question about CTRLPF and program speed

Recommended Posts

Since I can't figure out how to use XOR to flip sprite/playfield priority using the fire button with the batari Basic version of CTRLPF, I had to use more code.

 

Here's comes the stupid question. Which bit of code should be faster:

 

  BitOp_Flip_CTRLPF{2} = !BitOp_Flip_CTRLPF{2}

  CTRLPF = $01

  if BitOp_Flip_CTRLPF{2} then CTRLPF = $05

 

 

 

  BitOp_Flip_CTRLPF{2} = !BitOp_Flip_CTRLPF{2}

  if !BitOp_Flip_CTRLPF{2} then CTRLPF = $01

  if BitOp_Flip_CTRLPF{2} then CTRLPF = $05

 

 

My guess is the first one, but I don't know if an if-then is faster than "CTRLPF = $01".

 

Or maybe there is a faster, better way than either one of those, such as getting XOR to work with the bB version of CTRLPF.

Edited by Random Terrain

Share this post


Link to post
Share on other sites

Don't have the knowledge for reply which is the fastest but what about

  BitOp_Flip_CTRLPF{2} = !BitOp_Flip_CTRLPF{2}

  if BitOp_Flip_CTRLPF{2} then CTRLPF = $05  else  CTRLPF = $01

Share this post


Link to post
Share on other sites

Rather than count cycles, I tested all of these in my cycle tester. I knew the answer to RT's routines without it, but I wasn't sure how abaudrand's answer would place.

 

RT, your first one is faster on average than the second.

 

abaudrand, yours is slightly faster on average than RT's first one.

 

RT, your thought about using XOR is good, but you can't do something like "CTRLPF=(CTRLPF^4)", since CTRLPF is a write-only register.

 

Here's one that's faster than the others, and still uses only bit 2 of BitOp_Flip_CTRLPF...

 

  BitOp_Flip_CTRLPF=BitOp_Flip_CTRLPF^4
  CTRLPF=(BitOp_Flip_CTRLPF&4)+1

 

The first line inverts BitOp_Flip_CTRLPF{2} like the originals, but produces more compact assembly.

The second line assigns either 4+1 or 0+1 to CTRLPF, depending on the value in BitOp_Flip_CTRLPF{2}.

  • Like 1

Share this post


Link to post
Share on other sites
Here's one that's faster than the others, and still uses only bit 2 of BitOp_Flip_CTRLPF...

 

  BitOp_Flip_CTRLPF=BitOp_Flip_CTRLPF^4

  CTRLPF=(BitOp_Flip_CTRLPF&4)+1

 

The first line inverts BitOp_Flip_CTRLPF{2} like the originals, but produces more compact assembly.

 

The second line assigns either 4+1 or 0+1 to CTRLPF, depending on the value in BitOp_Flip_CTRLPF{2}.

Thanks. So on the bB page, I can say that users can use a{2} = !a{2} or a = a ^ %00000100 or a = a ^ 4.

 

Examples:

 

a{0} = !a{0}   or   a = a ^ %00000001   or   a = a ^ 1 

a{1} = !a{1}   or   a = a ^ %00000010   or   a = a ^ 2

a{2} = !a{2}   or   a = a ^ %00000100   or   a = a ^ 4

a{3} = !a{3}   or   a = a ^ %00001000   or   a = a ^ 8

a{4} = !a{4}   or   a = a ^ %00010000   or   a = a ^ 16

a{5} = !a{5}   or   a = a ^ %00100000   or   a = a ^ 32

a{6} = !a{6}   or   a = a ^ %01000000   or   a = a ^ 64

a{7} = !a{7}   or   a = a ^ %10000000   or   a = a ^ 128

If I'm not mistaken, I can tell them that the last two produce more compact assembly. Is that correct?

 

 

 

 

If you want to try some inline assembly then:

 

 

     asm
    lda   #1
    bit   INPT4
    bpl   .fireButtonPressed
    lda   #5
.fireButtonPressed:
    sta   CTRLPF
end

Thanks. I'm doing this for an example program, so I better stick with something that resembles bB code for now.

Edited by Random Terrain

Share this post


Link to post
Share on other sites

Thanks. So on the bB page, I can say that users can use a{2} = !a{2} or a = a ^ %00000100 or a = a ^ 4.

Yup

 

If I'm not mistaken, I can tell them that the last two produce more compact assembly. Is that correct?

That's correct. I confirmed it by comparing the generated asm code for both methods

  • Like 1

Share this post


Link to post
Share on other sites

Thanks. So on the bB page, I can say that users can use a{2} = !a{2} or a = a ^ %00000100 or a = a ^ 4.

Yup

 

If I'm not mistaken, I can tell them that the last two produce more compact assembly. Is that correct?

That's correct. I confirmed it by comparing the generated asm code for both methods

Thanks. I'll go update the page right now so I won't forget.

Share this post


Link to post
Share on other sites

after recompiling my program, the RevEng method saved me 26 bytes of rom !!! I couldn't imagine it could spare so much just with a mathematical operation.

Before this post, I've put this into the vblank

 vblank
 if a{4} then a{4}=0:return
 if !a{4} then a{4}=1
 return

 

I use this code in the vblank to create an artificial flickering if i want an object to be displayed on odd frame but not on even frame.

I also use this method to alternate objects colors each frame to give the illusion of another color.

Share this post


Link to post
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.

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...