Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- enum Level : ubyte
- {
- low = 0,
- high = 1
- }
- struct PORTRegister
- {
- private ubyte _store = void;
- // Load/Store methods
- /**
- * Toggle(invert) the bit representing a field.
- *
- * Note: Performs exactly one load followed by one store
- */
- @attribute("inlineonly") void toggle(string name)() nothrow @trusted if (name == "PIN0" || name == "PIN1")
- {
- ubyte val = __builtin_volatile_load(&_store);
- mixin("enum rhs = mask" ~ name ~ " << shift" ~ name ~ ";");
- mixin("val ^= rhs;");
- __builtin_volatile_store(&_store, val);
- }
- /**
- * Load the complete register and return the value
- *
- * Note: Performs exactly one load
- */
- @attribute("inlineonly") ubyte load()() nothrow @trusted const
- {
- return __builtin_volatile_load(&_store);
- }
- /**
- * Store value into register.
- *
- * Note: Performs exactly one store
- */
- @attribute("inlineonly") void store()(const ubyte value) nothrow @trusted
- {
- __builtin_volatile_store(&_store, cast(ubyte)value);
- }
- // Operator overloading
- /**
- * Operator overload for Read-Modify-Write operators
- *
- * Note: Performs exactly one load followed by one store
- */
- @attribute("inlineonly") void opOpAssign(string op)(in ubyte rhs) nothrow @trusted
- {
- ubyte val = __builtin_volatile_load(&_store);
- mixin("val" ~ op ~ "= rhs;");
- __builtin_volatile_store(&_store, val);
- }
- /**
- * Operator overload for assignment
- *
- * Note: Performs exactly one store
- */
- @attribute("inlineonly") void opAssign()(const ubyte rhs) nothrow @trusted
- {
- __builtin_volatile_store(&_store, rhs);
- }
- /**
- * Operator overload for equality check
- *
- * Note: Performs exactly one load
- */
- @attribute("inlineonly") bool opEquals()(const ubyte rhs) nothrow @trusted const
- {
- return __builtin_volatile_load(&_store) == rhs;
- }
- /**
- * Operator overload for comparison
- *
- * Note: Performs exactly one load
- */
- @attribute("inlineonly") int opCmp()(const ubyte rhs) nothrow @trusted const
- {
- ubyte val = __builtin_volatile_load(&_store);
- if(val == rhs)
- return 0;
- else if(val < rhs)
- return -1;
- else
- return 1;
- }
- /// Shift constant for PIN0 field
- enum shiftPIN0 = 0;
- /// Shift constant for PIN1 field
- enum shiftPIN1 = 1;
- /// Shift constant for TEST field
- enum shiftTEST = 2;
- /// Mask constant for PIN0 field
- enum maskPIN0 = 0b1;
- /// Mask constant for PIN1 field
- enum maskPIN1 = 0b1;
- /// Mask constant for TEST field
- enum maskTEST = 0b111;
- /**
- *
- */
- @attribute("inlineonly") @property Level PIN0()() nothrow @trusted const
- {
- auto val = __builtin_volatile_load(&_store);
- return cast(Level)((val >> shiftPIN0) & maskPIN0);
- }
- /**
- *
- */
- @attribute("inlineonly") @property void PIN0()(Level value) nothrow @trusted
- {
- auto val = __builtin_volatile_load(&_store);
- auto maskOR = cast(typeof(val))((value & maskPIN0) << shiftPIN0);
- auto maskAND = ~cast(typeof(val))((~value & maskPIN0) << shiftPIN0);
- val |= maskOR;
- val &= maskAND;
- __builtin_volatile_store(&_store, val);
- }
- /**
- *
- */
- @attribute("inlineonly") @property Level PIN1()() nothrow @trusted const
- {
- auto val = __builtin_volatile_load(&_store);
- return cast(Level)((val >> shiftPIN1) & maskPIN1);
- }
- /**
- *
- */
- @attribute("inlineonly") @property void PIN1()(Level value) nothrow @trusted
- {
- auto val = __builtin_volatile_load(&_store);
- auto maskOR = cast(typeof(val))((value & maskPIN1) << shiftPIN1);
- auto maskAND = ~cast(typeof(val))((~value & maskPIN1) << shiftPIN1);
- val |= maskOR;
- val &= maskAND;
- __builtin_volatile_store(&_store, val);
- }
- /**
- *
- */
- @attribute("inlineonly") @property ubyte TEST()() nothrow @trusted const
- {
- auto val = __builtin_volatile_load(&_store);
- return cast(ubyte)((val >> shiftTEST) & maskTEST);
- }
- /**
- *
- */
- @attribute("inlineonly") @property void TEST()(ubyte value) nothrow @trusted
- {
- auto val = __builtin_volatile_load(&_store);
- auto maskOR = cast(typeof(val))((value & maskTEST) << shiftTEST);
- auto maskAND = ~cast(typeof(val))((~value & maskTEST) << shiftTEST);
- val |= maskOR;
- val &= maskAND;
- __builtin_volatile_store(&_store, val);
- }
- }
- pragma(address, 0x25) extern __gshared PORTRegister PORTB;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement