15 #include <unordered_map> 16 #include <boost/thread/locks.hpp> 17 #include <boost/thread/mutex.hpp> 18 #include <boost/tokenizer.hpp> 39 #define UHD_DEFINE_SOFT_REG_FIELD(name, width, shift) \ 40 static const uhd::soft_reg_field_t name = (((shift & 0xFF) << 8) | (width & 0xFF)) 50 return __builtin_expect(expr,
true);
60 return __builtin_expect(expr,
false);
75 namespace soft_reg_field {
78 return (field & 0xFF);
83 return ((field >> 8) & 0xFF);
86 template <
typename data_t>
89 constexpr data_t ONE =
static_cast<data_t
>(1);
90 constexpr data_t ALL_ONES = ~static_cast<data_t>(0);
95 return ((ONE <<
width(field)) - ONE) <<
shift(field);
97 return ALL_ONES <<
shift(field);
108 virtual void flush() = 0;
117 template <
typename soft_reg_t>
120 soft_reg_t* ptr =
dynamic_cast<soft_reg_t*
>(®);
136 template <
typename reg_data_t,
bool readable,
bool writable>
140 typedef std::shared_ptr<soft_register_t<reg_data_t, readable, writable>>
sptr;
165 : _iface(NULL), _wr_addr(addr), _rd_addr(addr), _soft_copy(0), _flush_mode(mode)
179 if (sync && writable)
181 if (sync && readable)
192 _soft_copy = (_soft_copy & ~soft_reg_field::mask<reg_data_t>(field))
194 & soft_reg_field::mask<reg_data_t>(field));
203 return (_soft_copy & soft_reg_field::mask<reg_data_t>(field))
212 if (writable && _iface) {
216 if (_flush_mode ==
ALWAYS_FLUSH || _soft_copy.is_dirty()) {
217 if (get_bitwidth() <= 32) {
218 _iface->poke32(_wr_addr, static_cast<uint32_t>(_soft_copy));
219 }
else if (get_bitwidth() <= 64) {
220 _iface->poke64(_wr_addr, static_cast<uint64_t>(_soft_copy));
223 "soft_register only supports up to 64 bits.");
225 _soft_copy.mark_clean();
229 "soft_register is not writable or uninitialized.");
238 if (readable && _iface) {
239 if (get_bitwidth() <= 32) {
240 _soft_copy =
static_cast<reg_data_t
>(_iface->peek32(_rd_addr));
241 }
else if (get_bitwidth() <= 64) {
242 _soft_copy =
static_cast<reg_data_t
>(_iface->peek64(_rd_addr));
245 "soft_register only supports up to 64 bits.");
247 _soft_copy.mark_clean();
250 "soft_register is not readable or uninitialized.");
277 static const size_t BITS_IN_BYTE = 8;
278 return sizeof(reg_data_t) * BITS_IN_BYTE;
309 template <
typename reg_data_t,
bool readable,
bool writable>
314 typedef std::shared_ptr<soft_register_sync_t<reg_data_t, readable, writable>>
sptr;
319 :
soft_register_t<reg_data_t, readable, writable>(wr_addr, rd_addr, mode)
326 :
soft_register_t<reg_data_t, readable, writable>(addr, mode), _mutex()
332 boost::lock_guard<boost::mutex> lock(_mutex);
338 boost::lock_guard<boost::mutex> lock(_mutex);
344 boost::lock_guard<boost::mutex> lock(_mutex);
350 boost::lock_guard<boost::mutex> lock(_mutex);
356 boost::lock_guard<boost::mutex> lock(_mutex);
362 boost::lock_guard<boost::mutex> lock(_mutex);
368 boost::lock_guard<boost::mutex> lock(_mutex);
446 typedef std::shared_ptr<soft_regmap_accessor_t>
sptr;
450 virtual std::vector<std::string> enumerate()
const = 0;
451 virtual const std::string& get_name()
const = 0;
486 boost::lock_guard<boost::mutex> lock(_mutex);
488 reg->initialize(iface, sync);
499 boost::lock_guard<boost::mutex> lock(_mutex);
512 boost::lock_guard<boost::mutex> lock(_mutex);
524 regmap_t::const_iterator iter = _regmap.find(name);
525 if (iter != _regmap.end()) {
526 return *(iter->second);
538 std::vector<std::string> temp;
539 for (
const regmap_t::value_type& reg : _regmap) {
540 temp.push_back(_name +
"/" + reg.first);
555 const std::string& name,
558 boost::lock_guard<boost::mutex> lock(_mutex);
559 if (visible == PUBLIC) {
561 if (not _regmap.insert(regmap_t::value_type(name, ®)).second) {
563 "cannot add two registers with the same name to regmap: " + name);
566 _reglist.push_back(®);
570 typedef std::unordered_map<std::string, soft_register_base*> regmap_t;
571 typedef std::list<soft_register_base*> reglist_t;
573 const std::string _name;
589 typedef std::shared_ptr<soft_regmap_db_t>
sptr;
614 boost::lock_guard<boost::mutex> lock(_mutex);
615 _regmaps.push_back(®map);
623 boost::lock_guard<boost::mutex> lock(_mutex);
627 _regmap_dbs.push_back(&db);
644 std::list<std::string> tokens;
645 for (
const std::string& node : boost::tokenizer<boost::char_separator<char>>(
646 path, boost::char_separator<char>(
"/"))) {
647 tokens.push_back(node);
649 if ((tokens.size() > 2 && tokens.front() == _name) ||
650 (tokens.size() > 1 && _name ==
"")) {
653 if (tokens.size() == 2) {
655 if (regmap->get_name() == tokens.front()) {
656 return regmap->lookup(tokens.back());
660 }
else if (not _regmap_dbs
664 for (
const std::string& node : tokens) {
665 newpath += (
"/" + node);
670 return db->lookup(newpath.substr(1));
671 }
catch (std::exception&) {
685 std::vector<std::string> paths;
687 const std::vector<std::string>& regs = regmap->enumerate();
688 paths.insert(paths.end(), regs.begin(), regs.end());
691 const std::vector<std::string>& regs = db->enumerate();
692 paths.insert(paths.end(), regs.begin(), regs.end());
698 typedef std::list<soft_regmap_accessor_t*> db_t;
700 const std::string _name;
void refresh()
Definition: soft_register.hpp:510
uint32_t soft_reg_field_t
Definition: soft_register.hpp:73
UHD_INLINE void flush()
Definition: soft_register.hpp:210
std::shared_ptr< soft_register_t< reg_data_t, readable, writable > > sptr
Definition: soft_register.hpp:140
Definition: soft_register.hpp:129
soft_reg_flush_mode_t
Definition: soft_register.hpp:129
Definition: soft_register.hpp:310
Definition: exception.hpp:157
soft_regmap_t(const std::string &name)
Definition: soft_register.hpp:467
UHD_INLINE bool unlikely(bool expr)
hint for the branch prediction
Definition: soft_register.hpp:57
UHD_INLINE void refresh()
Definition: soft_register.hpp:236
soft_register_t< uint64_t, false, true > soft_reg64_wo_t
Definition: soft_register.hpp:396
virtual std::vector< std::string > enumerate() const
Definition: soft_register.hpp:536
soft_register_t(wb_iface::wb_addr_type addr, soft_reg_flush_mode_t mode=ALWAYS_FLUSH)
Definition: soft_register.hpp:163
boost::noncopyable noncopyable
Definition: noncopyable.hpp:45
#define UHD_DEFINE_SOFT_REG_FIELD(name, width, shift)
Definition: soft_register.hpp:39
soft_register_t< uint32_t, true, false > soft_reg32_ro_t
Definition: soft_register.hpp:390
UHD_INLINE size_t get_bitwidth()
Definition: soft_register.hpp:275
virtual soft_register_base & lookup(const std::string &name) const
Definition: soft_register.hpp:522
soft_register_sync_t(wb_iface::wb_addr_type addr, soft_reg_flush_mode_t mode=ALWAYS_FLUSH)
Definition: soft_register.hpp:324
UHD_INLINE bool is_writable()
Definition: soft_register.hpp:292
virtual size_t get_bitwidth()=0
soft_register_sync_t< uint64_t, true, true > soft_reg64_rw_sync_t
Definition: soft_register.hpp:401
Definition: exception.hpp:132
Definition: soft_register.hpp:586
virtual std::vector< std::string > enumerate() const
Definition: soft_register.hpp:683
soft_register_sync_t< uint32_t, false, true > soft_reg32_wo_sync_t
Definition: soft_register.hpp:392
soft_register_t< uint32_t, false, true > soft_reg32_wo_t
Definition: soft_register.hpp:389
UHD_INLINE size_t width(const soft_reg_field_t field)
Definition: soft_register.hpp:76
Definition: soft_register.hpp:547
UHD_INLINE void set(const soft_reg_field_t field, const reg_data_t value)
Definition: soft_register.hpp:190
virtual bool is_writable()=0
soft_regmap_db_t(const std::string &name)
Definition: soft_register.hpp:599
virtual ~soft_regmap_accessor_t()
Definition: soft_register.hpp:448
Definition: soft_register.hpp:137
UHD_INLINE reg_data_t get(const soft_reg_field_t field)
Definition: soft_register.hpp:201
Definition: build_info.hpp:12
soft_regmap_db_t()
Definition: soft_register.hpp:594
UHD_INLINE void write(const soft_reg_field_t field, const reg_data_t value)
Definition: soft_register.hpp:360
virtual ~soft_register_base()
Definition: soft_register.hpp:105
void flush()
Definition: soft_register.hpp:497
std::shared_ptr< soft_regmap_db_t > sptr
Definition: soft_register.hpp:589
visibility_t
Definition: soft_register.hpp:546
UHD_INLINE void initialize(wb_iface &iface, bool sync=false)
Definition: soft_register.hpp:174
void initialize(wb_iface &iface, bool sync=false)
Definition: soft_register.hpp:484
soft_register_sync_t< uint64_t, true, false > soft_reg64_ro_sync_t
Definition: soft_register.hpp:400
virtual ~soft_regmap_t()
Definition: soft_register.hpp:468
virtual bool is_readable()=0
soft_register_sync_t(wb_iface::wb_addr_type wr_addr, wb_iface::wb_addr_type rd_addr, soft_reg_flush_mode_t mode=ALWAYS_FLUSH)
Definition: soft_register.hpp:316
Definition: soft_register.hpp:443
UHD_INLINE bool is_readable()
Definition: soft_register.hpp:284
#define UHD_INLINE
Definition: config.h:52
UHD_INLINE void flush()
Definition: soft_register.hpp:348
UHD_INLINE reg_data_t read(const soft_reg_field_t field)
Definition: soft_register.hpp:266
soft_register_t(wb_iface::wb_addr_type wr_addr, wb_iface::wb_addr_type rd_addr, soft_reg_flush_mode_t mode=ALWAYS_FLUSH)
Definition: soft_register.hpp:148
soft_register_t< uint64_t, true, false > soft_reg64_ro_t
Definition: soft_register.hpp:397
Definition: soft_register.hpp:464
Definition: soft_register.hpp:102
soft_register_sync_t< uint32_t, true, false > soft_reg32_ro_sync_t
Definition: soft_register.hpp:393
Definition: exception.hpp:48
#define UHD_API
Definition: config.h:67
UHD_INLINE data_t mask(const soft_reg_field_t field)
Definition: soft_register.hpp:87
void add(soft_regmap_t ®map)
Definition: soft_register.hpp:612
UHD_INLINE reg_data_t read(const soft_reg_field_t field)
Definition: soft_register.hpp:366
const std::string & get_name() const
Definition: soft_register.hpp:604
UHD_INLINE void refresh()
Definition: soft_register.hpp:354
soft_register_t< uint64_t, true, true > soft_reg64_rw_t
Definition: soft_register.hpp:398
UHD_INLINE void write(const soft_reg_field_t field, const reg_data_t value)
Definition: soft_register.hpp:257
Definition: exception.hpp:96
void add(soft_regmap_db_t &db)
Definition: soft_register.hpp:621
std::shared_ptr< soft_regmap_accessor_t > sptr
Definition: soft_register.hpp:446
virtual void initialize(wb_iface &iface, bool sync=false)=0
soft_register_base & lookup(const std::string &path) const
Definition: soft_register.hpp:641
soft_register_sync_t< uint64_t, false, true > soft_reg64_wo_sync_t
Definition: soft_register.hpp:399
std::shared_ptr< soft_register_sync_t< reg_data_t, readable, writable > > sptr
Definition: soft_register.hpp:314
UHD_INLINE void initialize(wb_iface &iface, bool sync=false)
Definition: soft_register.hpp:330
UHD_INLINE bool likely(bool expr)
hint for the branch prediction
Definition: soft_register.hpp:47
soft_register_sync_t< uint32_t, true, true > soft_reg32_rw_sync_t
Definition: soft_register.hpp:394
virtual UHD_INLINE const std::string & get_name() const
Definition: soft_register.hpp:473
UHD_INLINE void add_to_map(soft_register_base ®, const std::string &name, const visibility_t visible=PRIVATE)
Definition: soft_register.hpp:554
UHD_INLINE size_t shift(const soft_reg_field_t field)
Definition: soft_register.hpp:81
Definition: soft_register.hpp:129
uint32_t wb_addr_type
Definition: wb_iface.hpp:21
static UHD_INLINE soft_reg_t & cast(soft_register_base ®)
Definition: soft_register.hpp:118
Definition: wb_iface.hpp:17
soft_register_t< uint32_t, true, true > soft_reg32_rw_t
Definition: soft_register.hpp:391