Thread overview | |||||||
---|---|---|---|---|---|---|---|
|
April 03, 2005 Mapping hardware | ||||
---|---|---|---|---|
| ||||
Hi, I'm accessing a hardware port and one of the fields map to the upper 6 bits of the second byte: 0123456701234567 aaaaaaaabcdddddd In C I would use a union between teh 16-bit value and a struct with bitfields, but http://www.digitalmars.com/d/struct.html explicitly says not bitfields. How can I do this in D? Can I use array of 'bit' type? If I put these into a struct, will they pack? Thanks. n |
April 03, 2005 Re: Mapping hardware | ||||
---|---|---|---|---|
| ||||
Posted in reply to 2ud5icomjs0t | <2ud5icomjs0t@sneakemail.com> wrote in message news:d2njii$1gsc$1@digitaldaemon.com... > Hi, > > I'm accessing a hardware port and one of the fields map to the upper 6 bits of > the second byte: > > 0123456701234567 > aaaaaaaabcdddddd > > In C I would use a union between teh 16-bit value and a struct with bitfields, > but http://www.digitalmars.com/d/struct.html explicitly says not bitfields. > > How can I do this in D? Shift & mask ought to do it: ushort* p; (*p >> 10) & 0x3F |
April 03, 2005 Re: Mapping hardware | ||||
---|---|---|---|---|
| ||||
Posted in reply to Walter | In article <d2nqqn$1mj3$1@digitaldaemon.com>, Walter says... > > ><2ud5icomjs0t@sneakemail.com> wrote in message news:d2njii$1gsc$1@digitaldaemon.com... >> Hi, >> >> I'm accessing a hardware port and one of the fields map to the upper 6 >bits of >> the second byte: >> >> 0123456701234567 >> aaaaaaaabcdddddd >> >> In C I would use a union between teh 16-bit value and a struct with >bitfields, >> but http://www.digitalmars.com/d/struct.html explicitly says not >bitfields. >> >> How can I do this in D? > >Shift & mask ought to do it: > > ushort* p; > (*p >> 10) & 0x3F > > Yes, of course it does, but it is very inconvenient when it comes to setting command and data values and toggling handshake bits. Is there no way to pack bit arrays together? Thanks. n |
April 03, 2005 Re: Mapping hardware | ||||
---|---|---|---|---|
| ||||
Posted in reply to 2ud5icomjs0t | On Sun, 3 Apr 2005 05:44:45 +0000 (UTC), <2ud5icomjs0t@sneakemail.com> wrote:
> In article <d2nqqn$1mj3$1@digitaldaemon.com>, Walter says...
>>
>>
>> <2ud5icomjs0t@sneakemail.com> wrote in message
>> news:d2njii$1gsc$1@digitaldaemon.com...
>>> Hi,
>>>
>>> I'm accessing a hardware port and one of the fields map to the upper 6
>> bits of
>>> the second byte:
>>>
>>> 0123456701234567
>>> aaaaaaaabcdddddd
>>>
>>> In C I would use a union between teh 16-bit value and a struct with
>> bitfields,
>>> but http://www.digitalmars.com/d/struct.html explicitly says not
>> bitfields.
>>>
>>> How can I do this in D?
>>
>> Shift & mask ought to do it:
>>
>> ushort* p;
>> (*p >> 10) & 0x3F
>>
>>
>
> Yes, of course it does, but it is very inconvenient when it comes to setting
> command and data values and toggling handshake bits.
>
> Is there no way to pack bit arrays together?
>
> Thanks. n
Packed bits can be represented by a bit[], BUT, a bit[] is a struct in the form:
struct array {
uint length;
void *data;
}
and when you say "bit[] bits" you're creating a reference to an array of bits. So
union {
uint mask;
bit[] bits;
}
places a uint and a "reference" to an array of bits in the same memory space. The problem here is that the data referenced by the array is elsewhere (actually null/undefined at present).
You can however at any time slice a bit* to obtain a bit[] (same holds for other ptr types, int* to int[], etc). You can also cast the address of any basic type byte, short, int, long (and the unsigned variants) to a bit*, so...
import std.stdio;
struct foo {
ushort mask;
}
void main()
{
bit[] bits;
foo a;
a.mask = 0b1101101101101101;
bits = (cast(bit*)&a.mask)[0..16];
foreach(bit b; bits)
writef("%d",b);
writefln("");
}
Output:
1011011011011011
(note, it looks reversed but that is because it iterates from bit[0] to bit[15], LSB to MSB (most significant bit), meaning it goes from right to left.
Regan
|
April 03, 2005 Re: Mapping hardware | ||||
---|---|---|---|---|
| ||||
Posted in reply to 2ud5icomjs0t |
>
> Yes, of course it does, but it is very inconvenient when it comes to setting
> command and data values and toggling handshake bits.
>
> Is there no way to pack bit arrays together?
>
> Thanks. n
At first I thought that this was a limitation also, but
1) This kind of bit packing in C introduces behind the scenes operations, which - especially for hardware mapped devices - may fool the programmer into thinking about the code wrong
2) The bit field packing can be simulated in D using properties, so that the usage code will look just like C code.
Brad
|
Copyright © 1999-2021 by the D Language Foundation