Access to fixed memory locations, head const, ...

Kuba Ober kuba at mareimbrium.org
Fri Nov 27 21:51:00 PST 2009


Hi,

I'm contemplating using D for an embedded project where system 
configuration registers have fixed memory locations.

One way of doing it would be to have a constant pointer to a structure 
with manually aligned members that match the register map, and access 
them like that. This becomes cumbersome, as all accesses look like

struct Regs {
   ...
};

Regs* cfg = cast(Regs*)0xF00;

cfg.reg0 = ...

This is also where D's const-transitivity becomes problematic. Since
the configuration registers are not going anywhere, ideally you'd want
a way of telling the compiler that the *pointer* is constant, but not
the pointed-to stuff.

For a systems language, this seems like a serious drawback. Why can't
I tell the compiler that noone should muck with the cfg pointer's
value? I presume there can be ways around it via delegates, but this
seems like a C++ approach: instead of making it simple, it becomes
convoluted...


OTOH, there's a C hack that allows you to access variables at fixed
memory locations "directly":

#define CFGREG0 (*(unsigned short*)0xBEEF)

I'm an absolute noob to D, and so far I found no way of approximating
this behavior -- of bringing the configuration registers at a fixed
memory location into the scope as an identifier that can be used naked, like

CFGREG0 = 0xFF;
if (CFGREG0 != 3) { ... }


Any hints?

Cheers, Kuba


More information about the Digitalmars-d-learn mailing list