D and memory mapped devices
Steven Schveighoffer via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Wed Jun 14 07:19:25 PDT 2017
On 6/14/17 4:10 AM, Russel Winder via Digitalmars-d-learn wrote:
> In C and C++ you often use bitfields to control devices with memory
> mapped hardware control and data words. So for example:
>
> typedef struct some_device {
> unsigned int flob: 3;
> unsigned int adob: 2;
> unsigned int: 3;
> unsigned int data: 8;
> } some_device;
>
> Clearly D has no bitfields. I know of std.bitfield, so somethign like:
>
> struct some_device {
> uint a;
> mixin(bitfields!(
> uint, "flob", 3,
> uint, "adob", 2,
> uint, "someMadeUpNameBecauseBlankIsNotAcceptable", 3,
> uint, "data", 8));
> }
>
> but the bitfields mixin template appears to do more than add all the
> bit twiddling functions to emulate the bitfields. This would appear a
> priori to not allow for actual memory mapped devices using it, or am I
> missing something?
Why not? It should work just the same as C's bitfields, but with more
configurability. It all depends on how the hardware register works, as
they often don't act like normal memory. One thing D does miss is the
volatile keyword.
Have you tried it? It might just work :)
-Steve
More information about the Digitalmars-d-learn
mailing list