Log inRegister an accountBrowse CSDbHelp & documentationFacts & StatisticsThe forumsAvailable RSS-feeds on CSDbSupport CSDb Commodore 64 Scene Database
You are not logged in - nap
CSDb User Forums


Forums > C64 Coding > 8 bit flags routine for a menu.
2012-02-21 16:35
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-?
2012-02-21 18:02
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)
2012-02-21 18:33
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.
2012-02-21 19:59
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?
2012-02-21 21:40
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... =)
2012-02-22 01:25
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).
2012-02-26 23:37
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.
2012-02-27 15:29
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.
2012-02-27 18:22
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

RefreshSubscribe to this thread:

You need to be logged in to post in the forum.

Search the forum:
Search   for   in  
All times are CET.
Search CSDb
Advanced
Users Online
Matt
Nordischsound/Hokuto..
Guests online: 87
Top Demos
1 Next Level  (9.7)
2 13:37  (9.7)
3 Mojo  (9.7)
4 Coma Light 13  (9.6)
5 Edge of Disgrace  (9.6)
6 What Is The Matrix 2  (9.6)
7 The Demo Coder  (9.6)
8 Uncensored  (9.6)
9 Comaland 100%  (9.6)
10 Wonderland XIV  (9.6)
Top onefile Demos
1 No Listen  (9.6)
2 Layers  (9.6)
3 Cubic Dream  (9.6)
4 Party Elk 2  (9.6)
5 Copper Booze  (9.6)
6 Dawnfall V1.1  (9.5)
7 Rainbow Connection  (9.5)
8 Onscreen 5k  (9.5)
9 Morph  (9.5)
10 Libertongo  (9.5)
Top Groups
1 Performers  (9.3)
2 Booze Design  (9.3)
3 Oxyron  (9.3)
4 Triad  (9.3)
5 Censor Design  (9.3)
Top Crackers
1 Mr. Z  (9.9)
2 Antitrack  (9.8)
3 OTD  (9.8)
4 Fungus  (9.8)
5 S!R  (9.8)

Home - Disclaimer
Copyright © No Name 2001-2024
Page generated in: 0.048 sec.