Jump to content



0

The BIT instruction... does it do anything useful?


8 replies to this topic

#1 Richi_S OFFLINE  

Richi_S

    Star Raider

  • 50 posts
  • Location:Germany - Munich

Posted Wed Sep 9, 2009 11:33 AM

I would award the BIT Test instruction as the most useless command of the 6502.
Ok... it comes right after RTI.
http://www.obelisk.d...erence.html#BIT

Is there something usefull you can do with it?
Like testing a Flag of every position of a Byte? Or even better; Testing a Flag of every position of a Byte whether the Flag is 0 or 1.

#2 cd-w OFFLINE  

cd-w

    Stargunner

  • 1,195 posts
  • Juno First!
  • Location:Glasgow, UK

Posted Wed Sep 9, 2009 11:46 AM

View PostRichi_S, on Wed Sep 9, 2009 11:33 AM, said:

I would award the BIT Test instruction as the most useless command of the 6502.
Ok... it comes right after RTI.
http://www.obelisk.d...erence.html#BIT

Is there something useful you can do with it?
Like testing a Flag of every position of a Byte? Or even better; Testing a Flag of every position of a Byte whether the Flag is 0 or 1.

Yes, the BIT instructions copies bits 7 and 6 from a memory location into the status register (N and V flags) without touching any of the other registers (A,X, or Y). This means that you can do things like this, without any saving, loading, or masking:

bit PAUSE         ; Set bit 7 when paused
bmi PauseScreen

Chris

#3 SeaGtGruff ONLINE  

SeaGtGruff

    River Patroller

  • 4,543 posts
  • Location:Georgia, USA

Posted Wed Sep 9, 2009 11:54 AM

View PostRichi_S, on Wed Sep 9, 2009 11:33 AM, said:

I would award the BIT Test instruction as the most useless command of the 6502.
Ok... it comes right after RTI.
http://www.obelisk.d...erence.html#BIT

Is there something usefull you can do with it?
Like testing a Flag of every position of a Byte? Or even better; Testing a Flag of every position of a Byte whether the Flag is 0 or 1.
The BIT instruction lets you check bits 6 and 7 of any byte using a single operation, and without changing the values of the accumulator, X register, or Y register. For the 2600, this is especially useful when checking for collisions, since the collision registers use only bits 6 and 7. For that matter, it's useful for checking the other TIA read addresses, too, as well as the RIOT's timer interrupt flag.

Michael

#4 e1will OFFLINE  

e1will

    Moonsweeper

  • 338 posts

Posted Wed Sep 9, 2009 11:57 AM

View PostRichi_S, on Wed Sep 9, 2009 11:33 AM, said:

Is there something usefull you can do with it?

Lots of things:

- You can use it to set the V flag, since there's no SEV instruction analogous to CLV
- You can use it to skip bytes (see this page for a helpful summary on how)
- And most importantly, like cd-w says, you can test bit 7 or bit 6 of a memory location while keeping the A, X, and Y registers intact.

Its usefulness may not be immediately obvious from its description, but I've found it to be extremely helpful in the coding I've done so far.

--Will

#5 Richi_S OFFLINE  

Richi_S

    Star Raider

  • 50 posts
  • Location:Germany - Munich

Posted Wed Sep 9, 2009 12:11 PM

Thanks for the quick reply.
Well, I was frustrated...
I wasted a lot of time, because I thought it could be used with some wierd trick to test any bit of a byte (would save cycles).

So I'm stuck with the typical coding:
   LDA Control_register
   AND #$08
   EOR #$08
   BNE Jumppoint
         ;Bit 3 is set
Jumppoint 

Is there any way to streamline this code?

#6 e1will OFFLINE  

e1will

    Moonsweeper

  • 338 posts

Posted Wed Sep 9, 2009 12:19 PM

View PostRichi_S, on Wed Sep 9, 2009 12:11 PM, said:

Thanks for the quick reply.
Well, I was frustrated...
I wasted a lot of time, because I thought it could be used with some wierd trick to test any bit of a byte (would save cycles).

So I'm stuck with the typical coding:
   LDA Control_register
   AND #$08
   EOR #$08
   BNE Jumppoint
         ;Bit 3 is set
Jumppoint 

Is there any way to streamline this code?

Well, you could drop the EOR and switch the BNE to a BEQ:

   LDA Control_register
   AND #$08
   BEQ Jumppoint
         ;Bit 3 is set
Jumppoint 

--Will

#7 Nukey Shay OFFLINE  

Nukey Shay

    Sheik Yerbouti

  • 20,458 posts
  • Location:The land of Gorch

Posted Wed Sep 9, 2009 12:19 PM

If it's in a time-critical area, you can throw the results of the test into a different byte (or bit) of temp ram before you need it. This cuts the test down to 3 cycles.

BTW the BIT opcode also alters the zero flag.


Another way of testing (while preserving current registers) is to PHP the processor status onto stack ram following an operation, then use PLP to get it back. The time-critical portion would be 4 cycles in that case. It's basically doing the same as the above...putting the result into temp ram and then using it later.

#8 GroovyBee OFFLINE  

GroovyBee

    7800 Developer

  • 5,781 posts
  • Busy bee!
  • Location:North, England

Posted Wed Sep 9, 2009 12:21 PM

Why do you need the EOR? This code will do the same job if you don't need the accumulator to be $00 if the branch is not taken.

LDA Control_register
AND #$08
BEQ Jumppoint
         ;Bit 3 is set
Jumppoint 


#9 Richi_S OFFLINE  

Richi_S

    Star Raider

  • 50 posts
  • Location:Germany - Munich

Posted Wed Sep 9, 2009 12:58 PM

Thanks for helping me out!

I definily missunderstood the documentation I was using...

When reading this Documentation,
it's clear how the zero flag works with AND.

Great finding, roland p! The Docu is great.

Edited by Richi_S, Wed Sep 9, 2009 1:02 PM.





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users