Jump to content
IGNORED

bB suggestion for addressing 4 bits and 2 bits as atomic number


Fort Apocalypse

Recommended Posts

It would be really helpful if bB had a convention to address 4 bits (or even 2) as a number. While you can still accomplish the same with a lot of bitwise logic, it seems like it might be faster to develop if you could address 4 or 2 bits in a var at once as if it were a short and really short int. Not sure what the convention would look like to address these vars though, and I'd bet it would be extremely hard to implement.

Edited by Fort Apocalypse
Link to comment
Share on other sites

It would be really helpful if bB had a convention to address 4 bits (or even 2) as a number. While you can still accomplish the same with a lot of bitwise logic, it seems like it might be faster to develop if you could address 4 or 2 bits in a var at once as if it were a short and really short int. Not sure what the convention would look like to address these vars though, and I'd bet it would be extremely hard to implement.

The problems I foresee might be (1) where do we start, which bit and how many?; and (2) where do we put the retrieved value, in an 8-bit user variable, temp variable, or just in A, X, or Y?

 

I'd say, start by writing a bB user function with inline assembly, then if it's something really useful, it could be put into an include file.

 

Michael

Link to comment
Share on other sites

It would be really helpful if bB had a convention to address 4 bits (or even 2) as a number. While you can still accomplish the same with a lot of bitwise logic, it seems like it might be faster to develop if you could address 4 or 2 bits in a var at once as if it were a short and really short int. Not sure what the convention would look like to address these vars though, and I'd bet it would be extremely hard to implement.

It may speed up development, but it would require a lot of work to get it fully integrated, if the fixed-point math routines are any indication of difficulty - it took me probably a year to get these working right.

 

Bitwise stuff can be a pain, so here's some example code. I'll just focus on splitting a variable in two (lower 4 or upper 4 bits.)

 

If you just want to deal with the upper 4 bits:

add 1: myvar=myvar+16

add 2: myvar=myvar+32

subtract 4: myvar=myvar-64

add arbitrary value: myvar=myvar+value*16

Copy to normal variable: othervar=myvar/16

Copy normal variable to upper 4: myvar=(myvar&%00001111) | othervar*16

 

For the lower 4:

Copy to normal var: othervar=myvar&15

Copy normal var to lower 4: myvar=(myvar & %11110000) | (othervar & %00001111)

Add arbitrary value: myvar=(myvar & %11110000) | ((myvar + value) & %00001111)

Subtract arbitrary value: myvar=(myvar & %11110000) | ((myvar - value) & %00001111)

 

The longer ones would be better as functions if used a lot.

 

Disclaimer: I have not tested the above code!

 

EDIT: the %00 forum bug strikes again!

Edited by batari
Link to comment
Share on other sites

For the lower 4:

Copy normal var to lower 4: myvar=(myvar & %11110000) | (othervar & %00001111)

 

If othervar doesn't have any excess bits set, you can skip the masking operation; that should thus yield:

  lda myvar
 and #$F0
 ora othervar
 sta myvar

If othervar does have other bits set, or if it is the result of a computation, then you should use:

; Put new data into accumulator somehow.  Then
 eor myvar
 and #$0F ; Bit mask
 eor myvar
 sta myvar

Probably written in bB as myvar = (((newvalue) ^ myvar) & 0xF0) ^ myvar

 

This latter approach can be particularly handy if you are performing math upon part of myvar, since there's no need to store the result of calculation to a temp address before putting it into myvar.

Link to comment
Share on other sites

; Put new data into accumulator somehow.  Then
 eor myvar
 and #$0F; Bit mask
 eor myvar
 sta myvar

Probably written in bB as myvar = (((newvalue) ^ myvar) & 0xF0) ^ myvar

Makes sense. The bB code for the above would be myvar = ((newvalue ^ myvar) & $0F) ^ myvar, which bB compiles as:

		LDA newvalue
	EOR myvar
	AND #$0F
	EOR myvar
	STA myvar

:cool:

Link to comment
Share on other sites

  • 2 months later...

Hey... resurrecting this thread because I'm looking for the most efficient way of storing 4 2-bit "pseudovariables" in a single variable (byte) to hold velocity for the 4 cars in the indy game. I'll use the timer variable to delay incrementing/decrementing of each of the 4 velocities (so it can jump in speed like you're changing gears).

 

So the plan is to have a carvelocitybits variable such that:

 

tempcar0velocity = carvelocitybits & %11000000

tempcar1velocity = carvelocitybits & %00110000

tempcar2velocity = carvelocitybits & %00001100

tempcar3velocity = carvelocitybits & %00000011

 

so if I wanted to:

 

add "1" to car0 velocity:

if tempcar0velocity < 255 then carvelocitybits = carvelocitybits + 64

add "1" to car1 velocity:

if tempcar1velocity < 64 then carvelocitybits = carvelocitybits + 16

add "1" to car2 velocity:

if tempcar2velocity < 16 then carvelocitybits = carvelocitybits + 4

add "1" to car3 velocity:

if tempcar3velocity < 4 then carvelocitybits = carvelocitybits + 1

 

subtract "1" from car0 velocity:

if tempcar0velocity > 0 then carvelocitybits = carvelocitybits - 64

subtract "1" from car1 velocity:

if tempcar1velocity > 0 then carvelocitybits = carvelocitybits - 16

subtract "1" from car2 velocity:

if tempcar2velocity > 0 then carvelocitybits = carvelocitybits - 4

subtract "1" from car3 velocity:

if tempcar3velocity > 0 then carvelocitybits = carvelocitybits - 1

 

and if I need to have 0-3 based numbers I use:

 

tempcar0vel0to3 = (carvelocitybits & %11000000) / 64

tempcar1vel0to3 = (carvelocitybits & %00110000) / 16

tempcar2vel0to3 = (carvelocitybits & %00001100) / 8

tempcar3vel0to3 = carvelocitybits & %00000011

 

Is that the most efficient way (program space-wise) to do those things?

Edited by Fort Apocalypse
Link to comment
Share on other sites

Hey... resurrecting this thread because I'm looking for the most efficient way of storing 4 2-bit "pseudovariables" in a single variable (byte)

 

[...]

 

Is that the most efficient way (program space-wise) to do those things?

If you're going to convert the numbers a lot in your program, then you might want to create some general user functions, so you don't have to keep repeating the same code over and over for different variables.

 

Michael

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...