| |
Flavioweb
Registered: Nov 2011 Posts: 463 |
8 bit flags routine for a menu.
I'm writing a code that work behind a menu with 8 options, selectable as ON/OFF.
I wish that a byte work like a "flags container", each bit as a flag of an option.
So, if an option is "ON" the relative flag is setted like:
lda #%00000001
sta FLAGBYTE
(for the first, %00000010 for second, %00000100 for third, and so on)
If "FLAGBYTE" is %00000111 means that options 1, 2 and 3 are selected as "ON".
Then, with an "lsr FLAGBYTE ... bcc" loop i check and execute the code for any option.
Now the question:
what is the method to do this with the smallest possible number of bytes, and the most -elegant-? |
|
| |
chatGPZ
Registered: Dec 2001 Posts: 11386 |
i would definately rethink this. while its ofcourse possible to do like that, there is a good chance that the code needed to deal with single bits is a great deal larger than if you would simply use 8 bytes instead =P (using bytes instead of bits increases storage by 7 bytes only, you will likely need much more to handle the flags in bits) |
| |
Count Zero
Registered: Jan 2003 Posts: 1932 |
If your menu output (like a trainermenu or so) is modified to reflect the selection you could just walk through the e.g. Y/N chars from top to bottom and create the FLAGBYTE the same way you interpret it.
|
| |
Conrad
Registered: Nov 2006 Posts: 849 |
Isn't it just as simple as mapping a binary masked value to each menu item (e.g. #%00000001 mapped to menu item 1) and thereby EOR the flag byte with the value mapped to the menu item that was clicked? |
| |
Flavioweb
Registered: Nov 2011 Posts: 463 |
Quote: Isn't it just as simple as mapping a binary masked value to each menu item (e.g. #%00000001 mapped to menu item 1) and thereby EOR the flag byte with the value mapped to the menu item that was clicked?
Maybe...
Or can be starting with #%00000001 on first item and "rol" the value "x" times according with option "number".
Or other better way to do it... =) |
| |
Fresh
Registered: Jan 2005 Posts: 101 |
Suppose you have to deal with y/n menu like Count Zero has pointed out.
I'd use a code such this
val0 equ 'n' ; not needed
val1 equ 'y'
...
lda #$80
tax
loop
ldy menu,x
cpy val1
inx
ror
bcc loop
...
menu=*-$80
.byte 'y','n','y','y','n','n','n','y'
Provided you choose a val1 value greater than val0, it should work.
(probably this is not the right way to declare characters in tass... can't remember, but you should get the point anyway). |
| |
Perplex
Registered: Feb 2009 Posts: 255 |
You could easily check four of the bits (0-1 & 6-7) by using the BIT opcode, then BCC/BCS, BEQ/BNE, BVC/BVS and BPL/BMI to check which of those bits are cleared or set.
That said, unless you need to store a very large set of boolean values, you're almost certainly better off storing each option in a separate byte, as the code to handle their status will be simpler, and will account for the larger portion of RAM use either way.
|
| |
Fresh
Registered: Jan 2005 Posts: 101 |
Hmmm... Ok for bit 6 and 7, but how do you check both 0 and 1 with BIT? You can check 0 OR 1 by setting A to 1 or 2. And AFAIK BIT doesn't touch carry so BCC/BCS IMHO should'nt make sense. |
| |
Achim Account closed
Registered: Jan 2010 Posts: 28 |
Groepaz is right.
But if you really want to use bits as flags you should use two table to set, clear and check the bits.
oratable: .byte $80, $40, $20, $10, $08, $04, $02, $01
andtable: .byte $7f, $bf, $df, $ef, $f7, $fb, $fd, $fe
set bits with something like this:
ldx option
lda flagbyte
ora oratable,x
sta flagbyte
check with something like this:
ldx #$07
loop: lda flagbyte,x
and andtable,x
beq nextoption
...whatever has to happen here...
nextoption: dex
bpl loop
|