6502: Index to bit index

Discuss technical or other issues relating to programming the Nintendo Entertainment System, Famicom, or compatible systems.

Moderator: Moderators

Post Reply
User avatar
olddb
Posts: 107
Joined: Thu Oct 26, 2017 12:29 pm
Contact:

6502: Index to bit index

Post by olddb » Thu Mar 26, 2020 11:39 am

Is there an elegant way to turn an index into a bit index(value), for clearing and/or setting?

I know I could use a loop or a table. Any other way/trick?

index bit
0 -> $01
1 -> $02
2 -> $04
3 -> $08
etc.
Last edited by olddb on Thu Mar 26, 2020 6:59 pm, edited 1 time in total.
...

User avatar
rainwarrior
Posts: 7801
Joined: Sun Jan 22, 2012 12:03 pm
Location: Canada
Contact:

Re: 6502: Index to bit index

Post by rainwarrior » Thu Mar 26, 2020 11:53 am

A table for this is so small and very efficient. Does that not seem like it's optimal enough? (I can't think of anything better.)

User avatar
tokumaru
Posts: 11645
Joined: Sat Feb 12, 2005 9:43 pm
Location: Rio de Janeiro - Brazil

Re: 6502: Index to bit index

Post by tokumaru » Thu Mar 26, 2020 2:35 pm

A program to calculate these would probably be larger than the table (and slower than reading from it too!)...

User avatar
Controllerhead
Posts: 39
Joined: Tue Nov 13, 2018 4:58 am
Location: $4016
Contact:

Re: 6502: Index to bit index

Post by Controllerhead » Thu Mar 26, 2020 4:24 pm

You can do something like this:

Code: Select all

; ORA can set your bit, preserving all others
LDX index
LDA value
ORA bit_set, x
STA value

; EOR can flip your bit, preserving all others
LDX index
LDA value
EOR bit_flip, x
STA value

; AND can clear your bit, preserving all others
LDX index
LDA value
AND bit_clear, x
STA value

bit_set: 
bit_flip:
 .db $01, $02, $04, $08, $10, $20, $40, $80     ; bits 0-7

bit_clear: 
 .db $FE, $FD, $FB, $F7, $EF, $DF, $BF, $7F     ; bits 0-7, negated
 
Setting a single bit using ORA, or flipping a single bit using EOR, requires a positive bit, like #%00000100, for bit 2, for example.

Clearing a single bit using AND requires a negated bit, like #%11111011, for bit 2, for example.
Last edited by Controllerhead on Sun Mar 29, 2020 7:35 pm, edited 3 times in total.

tepples
Posts: 21941
Joined: Sun Sep 19, 2004 11:12 pm
Location: NE Indiana, USA (NTSC)
Contact:

Re: 6502: Index to bit index

Post by tepples » Thu Mar 26, 2020 4:47 pm

If you ever see a table called one_shl_x or not_1_shl_x in my own NES work, you're looking at exactly this.

User avatar
olddb
Posts: 107
Joined: Thu Oct 26, 2017 12:29 pm
Contact:

Re: 6502: Index to bit index

Post by olddb » Thu Mar 26, 2020 7:08 pm

Thanks you all for the replies. A table it is.

BTW, why does wla give an error for this:

Code: Select all

table:
.db 00
lda table,x
I'm used to using the y reg when reading from tables.
...

User avatar
Dwedit
Posts: 4301
Joined: Fri Nov 19, 2004 7:35 pm
Contact:

Re: 6502: Index to bit index

Post by Dwedit » Thu Mar 26, 2020 7:31 pm

No idea. ASM6 has no problem with that syntax.
Here come the fortune cookies! Here come the fortune cookies! They're wearing paper hats!

User avatar
Controllerhead
Posts: 39
Joined: Tue Nov 13, 2018 4:58 am
Location: $4016
Contact:

Re: 6502: Index to bit index

Post by Controllerhead » Thu Mar 26, 2020 7:37 pm

Is TABLE a reserved word? Does it require dollar signed hex data ($00)?

ccovell
Posts: 1011
Joined: Sun Mar 19, 2006 9:44 pm
Location: Japan
Contact:

Re: 6502: Index to bit index

Post by ccovell » Sun Mar 29, 2020 6:56 pm

olddb wrote:
Thu Mar 26, 2020 7:08 pm
Thanks you all for the replies. A table it is.

BTW, why does wla give an error for this:

Code: Select all

table:
.db 00
lda table,x
I'm used to using the y reg when reading from tables.
Perhaps code cannot be in the 1st column of text (reserved for labels and assembler directives) and thus needs spaces or a tab before it?

Also, you're not putting a data table in between valid instructions, are you? Your "lda table,x" at least needs its own label if code is going to JSR or jump to it.

Post Reply