Jump to content

Photo

Stupid question about CTRLPF and program speed


7 replies to this topic

#1 Random Terrain ONLINE  

Random Terrain

    Visual batari Basic User

  • 24,984 posts
  • Controlled Randomness
    Replay Value
    Nonlinear
  • Location:North Carolina (USA)

Posted Sun May 29, 2011 4:23 AM

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, Sun May 29, 2011 12:53 PM.


#2 abaudrand OFFLINE  

abaudrand

    Chopper Commander

  • 208 posts

Posted Sun May 29, 2011 6:50 AM

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



#3 RevEng ONLINE  

RevEng

    River Patroller

  • 3,398 posts
  • bit player
  • Location:Canada

Posted Sun May 29, 2011 8:07 AM

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

#4 Omegamatrix OFFLINE  

Omegamatrix

    Quadrunner

  • 5,552 posts
  • Location:Canada

Posted Sun May 29, 2011 8:19 AM

If you want to try some inline assembly then:


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


#5 Random Terrain ONLINE  

Random Terrain

    Visual batari Basic User

  • Topic Starter
  • 24,984 posts
  • Controlled Randomness
    Replay Value
    Nonlinear
  • Location:North Carolina (USA)

Posted Sun May 29, 2011 1:32 PM

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, Sun May 29, 2011 1:33 PM.


#6 RevEng ONLINE  

RevEng

    River Patroller

  • 3,398 posts
  • bit player
  • Location:Canada

Posted Sun May 29, 2011 3:22 PM

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

#7 Random Terrain ONLINE  

Random Terrain

    Visual batari Basic User

  • Topic Starter
  • 24,984 posts
  • Controlled Randomness
    Replay Value
    Nonlinear
  • Location:North Carolina (USA)

Posted Sun May 29, 2011 3:26 PM


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.

#8 abaudrand OFFLINE  

abaudrand

    Chopper Commander

  • 208 posts

Posted Mon May 30, 2011 12:20 AM

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.




0 user(s) are browsing this forum

0 members, 0 guests, 0 anonymous users