www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Mapping hardware

reply 2ud5icomjs0t sneakemail.com writes:
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
Apr 02 2005
parent reply "Walter" <newshound digitalmars.com> writes:
<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
Apr 02 2005
parent reply 2ud5icomjs0t sneakemail.com writes:
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
Apr 02 2005
next sibling parent "Regan Heath" <regan netwin.co.nz> writes:
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
Apr 02 2005
prev sibling parent brad beveridge <brad nowhere.com> writes:
 
 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
Apr 02 2005