Advertisement
Guest User

Untitled

a guest
Feb 2nd, 2015
274
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
D 4.85 KB | None | 0 0
  1. enum Level : ubyte
  2. {
  3.     low = 0,
  4.     high = 1
  5. }
  6.  
  7. struct PORTRegister
  8. {
  9.     private ubyte _store = void;
  10.    
  11.     // Load/Store methods
  12.    
  13.     /**
  14.      * Toggle(invert) the bit representing a field.
  15.      *
  16.      * Note: Performs exactly one load followed by one store
  17.      */
  18.     @attribute("inlineonly") void toggle(string name)() nothrow @trusted if (name == "PIN0" || name == "PIN1")
  19.     {
  20.         ubyte val = __builtin_volatile_load(&_store);
  21.         mixin("enum rhs = mask" ~ name ~ " << shift" ~ name ~ ";");
  22.         mixin("val ^= rhs;");
  23.         __builtin_volatile_store(&_store, val);
  24.     }
  25.    
  26.     /**
  27.      * Load the complete register and return the value
  28.      *
  29.      * Note: Performs exactly one load
  30.      */
  31.     @attribute("inlineonly") ubyte load()() nothrow @trusted const
  32.     {
  33.         return __builtin_volatile_load(&_store);
  34.     }
  35.    
  36.     /**
  37.      * Store value into register.
  38.      *
  39.      * Note: Performs exactly one store
  40.      */
  41.     @attribute("inlineonly") void store()(const ubyte value) nothrow @trusted
  42.     {
  43.         __builtin_volatile_store(&_store, cast(ubyte)value);
  44.     }
  45.    
  46.     // Operator overloading
  47.    
  48.     /**
  49.      * Operator overload for Read-Modify-Write operators
  50.      *
  51.      * Note: Performs exactly one load followed by one store
  52.      */
  53.     @attribute("inlineonly") void opOpAssign(string op)(in ubyte rhs) nothrow @trusted
  54.     {
  55.         ubyte val = __builtin_volatile_load(&_store);
  56.         mixin("val" ~ op ~ "= rhs;");
  57.         __builtin_volatile_store(&_store, val);
  58.     }
  59.    
  60.     /**
  61.      * Operator overload for assignment
  62.      *
  63.      * Note: Performs exactly one store
  64.      */
  65.     @attribute("inlineonly") void opAssign()(const ubyte rhs) nothrow @trusted
  66.     {
  67.         __builtin_volatile_store(&_store, rhs);
  68.     }
  69.    
  70.     /**
  71.      * Operator overload for equality check
  72.      *
  73.      * Note: Performs exactly one load
  74.      */
  75.     @attribute("inlineonly") bool opEquals()(const ubyte rhs) nothrow @trusted const
  76.     {
  77.         return __builtin_volatile_load(&_store) == rhs;
  78.     }
  79.    
  80.     /**
  81.      * Operator overload for comparison
  82.      *
  83.      * Note: Performs exactly one load
  84.      */
  85.     @attribute("inlineonly") int opCmp()(const ubyte rhs) nothrow @trusted const
  86.     {
  87.         ubyte val = __builtin_volatile_load(&_store);
  88.         if(val == rhs)
  89.             return 0;
  90.         else if(val < rhs)
  91.             return -1;
  92.         else
  93.             return 1;
  94.     }
  95.    
  96.     /// Shift constant for PIN0 field
  97.     enum shiftPIN0 = 0;
  98.     /// Shift constant for PIN1 field
  99.     enum shiftPIN1 = 1;
  100.     /// Shift constant for TEST field
  101.     enum shiftTEST = 2;
  102.     /// Mask constant for PIN0 field
  103.     enum maskPIN0 = 0b1;
  104.     /// Mask constant for PIN1 field
  105.     enum maskPIN1 = 0b1;
  106.     /// Mask constant for TEST field
  107.     enum maskTEST = 0b111;
  108.    
  109.     /**
  110.      *
  111.      */
  112.     @attribute("inlineonly") @property Level PIN0()() nothrow @trusted const
  113.     {
  114.         auto val = __builtin_volatile_load(&_store);
  115.         return cast(Level)((val >> shiftPIN0) & maskPIN0);
  116.     }
  117.    
  118.     /**
  119.      *
  120.      */
  121.     @attribute("inlineonly") @property void PIN0()(Level value) nothrow @trusted
  122.     {
  123.         auto val = __builtin_volatile_load(&_store);
  124.         auto maskOR = cast(typeof(val))((value & maskPIN0) << shiftPIN0);
  125.         auto maskAND = ~cast(typeof(val))((~value & maskPIN0) << shiftPIN0);
  126.         val |= maskOR;
  127.         val &= maskAND;
  128.         __builtin_volatile_store(&_store, val);
  129.     }
  130.    
  131.     /**
  132.      *
  133.      */
  134.     @attribute("inlineonly") @property Level PIN1()() nothrow @trusted const
  135.     {
  136.         auto val = __builtin_volatile_load(&_store);
  137.         return cast(Level)((val >> shiftPIN1) & maskPIN1);
  138.     }
  139.    
  140.     /**
  141.      *
  142.      */
  143.     @attribute("inlineonly") @property void PIN1()(Level value) nothrow @trusted
  144.     {
  145.         auto val = __builtin_volatile_load(&_store);
  146.         auto maskOR = cast(typeof(val))((value & maskPIN1) << shiftPIN1);
  147.         auto maskAND = ~cast(typeof(val))((~value & maskPIN1) << shiftPIN1);
  148.         val |= maskOR;
  149.         val &= maskAND;
  150.         __builtin_volatile_store(&_store, val);
  151.     }
  152.    
  153.     /**
  154.      *
  155.      */
  156.     @attribute("inlineonly") @property ubyte TEST()() nothrow @trusted const
  157.     {
  158.         auto val = __builtin_volatile_load(&_store);
  159.         return cast(ubyte)((val >> shiftTEST) & maskTEST);
  160.     }
  161.    
  162.     /**
  163.      *
  164.      */
  165.     @attribute("inlineonly") @property void TEST()(ubyte value) nothrow @trusted
  166.     {
  167.         auto val = __builtin_volatile_load(&_store);
  168.         auto maskOR = cast(typeof(val))((value & maskTEST) << shiftTEST);
  169.         auto maskAND = ~cast(typeof(val))((~value & maskTEST) << shiftTEST);
  170.         val |= maskOR;
  171.         val &= maskAND;
  172.         __builtin_volatile_store(&_store, val);
  173.     }
  174. }
  175.  
  176. pragma(address, 0x25) extern __gshared PORTRegister PORTB;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement