Accessing peripheral registers - the next version

Johannes Pfau via D.gnu d.gnu at puremagic.com
Sun Aug 28 06:26:37 PDT 2016


Am Sun, 28 Aug 2016 09:28:24 +0000
schrieb Timo Sintonen <t.sintonen at luukku.com>:

> I just translated my sample program and everything seems to work 
> in my limited tests. Here is a simplified example of an uart:
> 
> alias uarttype = uartreg*;
> enum uarttype uart1=cast (uarttype)0x40011000;
> enum uarttype uart2=cast (uarttype)0x40004400;
> 

That's a clever solution. AFAICS it works because D supports
the '.' operator on pointers. With this solution you can't directly
assign the complete value:

uart1 = uart2; // can't be valid, assigning pointers
*uart1 = *uart2; // should work, though IIRC there could be a DMDFE bug

IIRC you also cant use operator overloading:

uart1 += 49; // can't be valid, assigning the pointer
*uart1 += 49; // might work? (not sure if dereferencing
              // and overloads work in same statement)


And you can't use the & operator to get the address, but as uart1
already is a pointer that's not really a problem.


In your example this isn't really a restriction. But it could be more
annoying if you don't have related fields with contiguous addresses.
Consider a single 8 bit counter value:

alias CtrType = (Volatile!ubyte)*; 
enum CtrType ctrA=cast (CtrType)0x40011000;

*ctrA += 3;
*ctrA = 42;

OTOH this is a nice solution without language changes, so
@attribute("address") is probably not necessary. I think it'd be a
little more idiomatic to just treat memory mapped registers as extern
variables, but maybe it's not worth the effort.

 
> Then let it be Volatile and we can alias that later.
> Because GDC is only at 2.068, it seems we can not name the module 
> as 'volatile'.
> 

Even DMD 2.071 doesn't allow 'volatile' as a module name :-) We need a
DMD patch first and then we could backport that patch to GDC if
necessary.



More information about the D.gnu mailing list