Numworks Epsilon  1.4.1
Graphing Calculator Operating System
register.h
Go to the documentation of this file.
1 #ifndef REGS_REGISTER_H
2 #define REGS_REGISTER_H
3 
4 #include <stdint.h>
5 #include <assert.h>
6 
7 template <typename T>
8 class Register {
9 public:
10  Register() = delete;
11  Register(T v) : m_value(v) {}
12  void set(Register<T> value) volatile {
13  m_value = value.m_value;
14  }
15  void set(T value) volatile {
16  m_value = value;
17  }
18  T get() volatile {
19  return m_value;
20  }
21  void setBitRange(uint8_t high, uint8_t low, T value) volatile {
22  m_value = bit_range_set_value(high, low, m_value, value);
23  }
24  T getBitRange(uint8_t high, uint8_t low) volatile {
25  /* "Shift behavior is undefined if the right operand is negative, or greater
26  * than or equal to the length in bits of the promoted left operand" according
27  * to C++ spec. */
28  assert(low < 8*sizeof(T));
29  return (m_value & bit_range_mask(high,low)) >> low;
30  }
31 protected:
32  static constexpr T bit_range_mask(uint8_t high, uint8_t low) {
33  // Same comment as for getBitRange: we should assert (high-low+1) < 8*sizeof(T)
34  return ((((T)1)<<(high-low+1))-1)<<low;
35  }
36  static constexpr T bit_range_value(T value, uint8_t high, uint8_t low) {
37  // Same comment as for getBitRange: we should assert low < 8*sizeof(T))
38  return (value<<low) & bit_range_mask(high,low);
39  }
40  static constexpr T bit_range_set_value(uint8_t high, uint8_t low, T originalValue, T targetValue) {
41  return (originalValue & ~bit_range_mask(high,low))|bit_range_value(targetValue, high, low);
42  }
43 private:
44  T m_value;
45 };
46 
51 
52 #define REGS_FIELD_R(name,type,high,low) type get##name() volatile { return (type)getBitRange(high,low); };
53 #define REGS_FIELD_W(name,type,high,low) void set##name(type v) volatile { setBitRange(high, low, (uint8_t)v); };
54 #define REGS_FIELD(name,type,high,low) REGS_FIELD_R(name,type,high,low); REGS_FIELD_W(name,type,high,low);
55 #define REGS_TYPE_FIELD(name,high,low) REGS_FIELD(name,name,high,low)
56 #define REGS_BOOL_FIELD(name,bit) REGS_FIELD(name,bool,bit,bit)
57 #define REGS_BOOL_FIELD_R(name,bit) REGS_FIELD_R(name,bool,bit,bit)
58 #define REGS_BOOL_FIELD_W(name,bit) REGS_FIELD_W(name,bool,bit,bit)
59 #define REGS_REGISTER_AT(name, offset) constexpr volatile name * name() const { return (class name *)(Base() + offset); };
60 
61 #endif
#define assert(e)
Definition: assert.h:9
Register()=delete
static constexpr T bit_range_value(T value, uint8_t high, uint8_t low)
Definition: register.h:36
#define T(x)
Definition: events.cpp:26
unsigned char uint8_t
Definition: stdint.h:4
Register< uint16_t > Register16
Definition: register.h:48
Register< uint32_t > Register32
Definition: register.h:49
Register(T v)
Definition: register.h:11
static constexpr T bit_range_mask(uint8_t high, uint8_t low)
Definition: register.h:32
void setBitRange(uint8_t high, uint8_t low, T value) volatile
Definition: register.h:21
Register< uint8_t > Register8
Definition: register.h:47
static constexpr T bit_range_set_value(uint8_t high, uint8_t low, T originalValue, T targetValue)
Definition: register.h:40
T getBitRange(uint8_t high, uint8_t low) volatile
Definition: register.h:24
Register< uint64_t > Register64
Definition: register.h:50