Page 1 of 1

Bit-based values/structures

Posted: Thu May 21, 2009 11:13 am
by mback2k
It would be great if PB could support bit-based values/structures.

For example:

Code: Select all

Structure RX
  opcode.b
  StructureBit ri.b   ; byte representation (optional)
    r.b[4]            ; Big-Endian unsigned value with 4 bit length, max value = (2^4)-1
    i.b[4]            ; Big-Endian unsigned value with 4 bit length at 4 bit offset, max value = (2^4)-1
  EndStructureBit
  StructureBit bd.w   ; word representation (optional)
    b.b[4]            ; Big-Endian unsigned value with 4 bit length, max value = (2^4)-1
    d.b[12]           ; Big-Endian unsigned value with 12 bit length at 4 bit offset, max value = (2^12)-1
  EndStructureBit
EndStructure

Define MyValue.RX
MyValue\opcode = $72
MyValue\r = 15
MyValue\i = 0
MyValue\b = 0
MyValue\d = $FFF
.b would mean Big-Endian and .l Little-Endian.

I know that I can handle all this with bit shifting, but this new thing would make it a lot easier IMO.

Posted: Thu May 21, 2009 1:45 pm
by DoubleDutch
.b and .l are already used - byte and long

Posted: Thu May 21, 2009 2:08 pm
by Joakim Christiansen
DoubleDutch wrote:.b and .l are already used - byte and long
Indeed, maybe we should use .bit and .lbit then.

Posted: Thu May 21, 2009 7:00 pm
by Demivec
If it existed wouldn't it's take a form more like this?

Code: Select all

; .y = bit data type Big-Endian, .z = bit data type Little-Endian
Structure ri   ; byte representation (optional)
  r.y[4]            ; Big-Endian unsigned value with 4 bit length, max value = (2^4)-1
  i.y[4]            ; Big-Endian unsigned value with 4 bit length at 4 bit offset, max value = (2^4)-1
EndStructure

Structure bd   ; word representation (optional)
  b.y[4]            ; Big-Endian unsigned value with 4 bit length, max value = (2^4)-1
  D.y[12]           ; Big-Endian unsigned value with 12 bit length at 4 bit offset, max value = (2^12)-1
EndStructure

Structure RX
  StructureUnion
    bd.bd
    opcode.b
    ri.ri
  EndStructureUnion
EndStructure


Define MyValue.RX
MyValue\opcode = $72
MyValue\ri\r = 15
MyValue\ri\i = 0
MyValue\bd\b = 0
MyValue\bd\D = $FFF

Posted: Fri May 22, 2009 3:08 am
by pdwyer
I want a signed and an unsigned bit type ;)

(sorry, couldn't help myself)

Posted: Fri May 22, 2009 7:55 am
by DoubleDutch
Maybe it should be kept simple like a stream of bits, then the Endian applied when the structure is used, this way it could be applied to existing data types.

Code: Select all

Structure  pattern
  r.bit[4]            ; value with 4 bit length, max value = (2^4)-1 
  i.bit[4]            ; value with 4 bit length at 4 bit offset, max value = (2^4)-1 
EndStructure 
settings.pattern ; this uses the default mode on the processor - eg little endian on x86, big on 68k

settings2.<pattern ; forces big endian
settings3.>pattern ; forces little endian

count.<l ; forces big endian long store
count2.>l ; forces little endian long store

name$ ; normal string store for all processors is forward
name2<$ ; force big endian string store
name3>$ ; force little endian string store

Nice side effect: a union of little and big string types would allow you to read the same string in reverse using a different variable name.

This should also be possible:

Code: Select all

Structure  pattern
  r.<bit[4]            ; value with 4 bit length, max value = (2^4)-1 
  i.>bit[4]            ; value with 4 bit length at 4 bit offset, max value = (2^4)-1 
EndStructure 
To store bits reverse order.

Posted: Fri May 22, 2009 8:30 am
by mback2k
DoubleDutch wrote:.b and .l are already used - byte and long
Yep, but that doesn't mean they can't be re-used in another context ;)

Nice ideas, keep it coming :)

Posted: Fri May 22, 2009 8:40 am
by DoubleDutch
I think that currently:

Code: Select all

r.b[4] 
Is a fixed array of 4 bytes:
r[0] to r[3]

imho Using .b for anything else would just be confusing.

Posted: Fri May 22, 2009 11:55 am
by Kaeru Gaman
could anyone explain to me with one, two sentences, what the difference between a "big endian bit" and a "little endian bit" should be?

a bit is a bit, isn't it?

Posted: Fri May 22, 2009 12:07 pm
by DoubleDutch

Posted: Fri May 22, 2009 1:39 pm
by Kaeru Gaman
more than one or two sentences, but ok.

I gets me a bit confused.
it sais, x86 uses little-endian for bits, you say the same,
but the SHIFT commands ( >>, << ) are big-endian formatted...?

*scratch*think*

... byte order in memory is definitely l-e, so seems the shift commands are only this way for better imagination.

it's convention for a long time, ASM shifts always were written b-e, like numbers are written.
maybe that's the only reason...

; . . .

I agree that .b and .l should not be reused.
also, when [ ] should be used, the meaning should stay the same as used to:
a static array of this type inside the struct.

Posted: Fri May 22, 2009 1:45 pm
by DoubleDutch
Once inside the data registers, it's big endian - its when its written to or read from memory that the x86 goes to little endian.

Historically (I think) it's to maintain easy reading of data written by a processor with a lower data bit size.