8 #ifndef INCLUDED_UHD_UTILS_SOFT_REGISTER_HPP 9 #define INCLUDED_UHD_UTILS_SOFT_REGISTER_HPP 12 #include <boost/noncopyable.hpp> 16 #include <boost/thread/mutex.hpp> 17 #include <boost/thread/locks.hpp> 18 #include <boost/unordered_map.hpp> 19 #include <boost/tokenizer.hpp> 20 #include <boost/foreach.hpp> 41 #define UHD_DEFINE_SOFT_REG_FIELD(name, width, shift) \ 42 static const uhd::soft_reg_field_t name = (((shift & 0xFF) << 8) | (width & 0xFF)) 52 return __builtin_expect(expr,
true);
62 return __builtin_expect(expr,
false);
77 namespace soft_reg_field {
79 return (field & 0xFF);
83 return ((field >> 8) & 0xFF);
86 template<
typename data_t>
88 constexpr data_t ONE =
static_cast<data_t
>(1);
89 constexpr data_t ALL_ONES = ~static_cast<data_t>(0);
94 return ((ONE<<
width(field))-ONE)<<
shift(field);
96 return ALL_ONES<<
shift(field);
105 virtual void initialize(
wb_iface& iface,
bool sync =
false) = 0;
106 virtual void flush() = 0;
107 virtual void refresh() = 0;
108 virtual size_t get_bitwidth() = 0;
109 virtual bool is_readable() = 0;
110 virtual bool is_writable() = 0;
115 template <
typename soft_reg_t>
117 soft_reg_t* ptr =
dynamic_cast<soft_reg_t*
>(®);
133 template<
typename reg_data_t,
bool readable,
bool writable>
136 typedef boost::shared_ptr< soft_register_t<reg_data_t, readable, writable> >
sptr;
148 _iface(NULL), _wr_addr(wr_addr), _rd_addr(rd_addr), _soft_copy(0), _flush_mode(mode)
158 _iface(NULL), _wr_addr(addr), _rd_addr(addr), _soft_copy(0), _flush_mode(mode)
171 if (sync && writable) flush();
172 if (sync && readable) refresh();
180 UHD_INLINE void set(
const soft_reg_field_t field,
const reg_data_t value)
182 _soft_copy = (_soft_copy & ~soft_reg_field::mask<reg_data_t>(field)) |
183 ((value << soft_reg_field::shift(field)) & soft_reg_field::mask<reg_data_t>(field));
200 if (writable && _iface) {
204 if (_flush_mode == ALWAYS_FLUSH || _soft_copy.is_dirty()) {
205 if (get_bitwidth() <= 16) {
206 _iface->poke16(_wr_addr, static_cast<uint16_t>(_soft_copy));
207 }
else if (get_bitwidth() <= 32) {
208 _iface->poke32(_wr_addr, static_cast<uint32_t>(_soft_copy));
209 }
else if (get_bitwidth() <= 64) {
210 _iface->poke64(_wr_addr, static_cast<uint64_t>(_soft_copy));
214 _soft_copy.mark_clean();
226 if (readable && _iface) {
227 if (get_bitwidth() <= 16) {
228 _soft_copy =
static_cast<reg_data_t
>(_iface->peek16(_rd_addr));
229 }
else if (get_bitwidth() <= 32) {
230 _soft_copy =
static_cast<reg_data_t
>(_iface->peek32(_rd_addr));
231 }
else if (get_bitwidth() <= 64) {
232 _soft_copy =
static_cast<reg_data_t
>(_iface->peek64(_rd_addr));
236 _soft_copy.mark_clean();
265 static const size_t BITS_IN_BYTE = 8;
266 return sizeof(reg_data_t) * BITS_IN_BYTE;
297 template<
typename reg_data_t,
bool readable,
bool writable>
300 typedef boost::shared_ptr< soft_register_sync_t<reg_data_t, readable, writable> >
sptr;
306 soft_register_t<reg_data_t, readable, writable>(wr_addr, rd_addr, mode), _mutex()
317 boost::lock_guard<boost::mutex> lock(_mutex);
321 UHD_INLINE void set(
const soft_reg_field_t field,
const reg_data_t value)
323 boost::lock_guard<boost::mutex> lock(_mutex);
329 boost::lock_guard<boost::mutex> lock(_mutex);
335 boost::lock_guard<boost::mutex> lock(_mutex);
341 boost::lock_guard<boost::mutex> lock(_mutex);
347 boost::lock_guard<boost::mutex> lock(_mutex);
353 boost::lock_guard<boost::mutex> lock(_mutex);
437 typedef boost::shared_ptr<soft_regmap_accessor_t>
sptr;
441 virtual std::vector<std::string> enumerate()
const = 0;
442 virtual const std::string& get_name()
const = 0;
472 boost::lock_guard<boost::mutex> lock(_mutex);
484 boost::lock_guard<boost::mutex> lock(_mutex);
496 boost::lock_guard<boost::mutex> lock(_mutex);
507 regmap_t::const_iterator iter = _regmap.find(name);
508 if (iter != _regmap.end()) {
509 return *(iter->second);
520 std::vector<std::string> temp;
521 BOOST_FOREACH(
const regmap_t::value_type& reg, _regmap) {
522 temp.push_back(_name +
"/" + reg.first);
537 boost::lock_guard<boost::mutex> lock(_mutex);
538 if (visible == PUBLIC) {
540 if (not _regmap.insert(regmap_t::value_type(name, ®)).second) {
544 _reglist.push_back(®);
548 typedef boost::unordered_map<std::string, soft_register_base*> regmap_t;
549 typedef std::list<soft_register_base*> reglist_t;
551 const std::string _name;
566 typedef boost::shared_ptr<soft_regmap_db_t>
sptr;
581 const std::string&
get_name()
const {
return _name; }
587 boost::lock_guard<boost::mutex> lock(_mutex);
588 _regmaps.push_back(®map);
595 boost::lock_guard<boost::mutex> lock(_mutex);
599 _regmap_dbs.push_back(&db);
616 std::list<std::string> tokens;
618 const std::string& node,
619 boost::tokenizer< boost::char_separator<char> >(path, boost::char_separator<char>(
"/")))
621 tokens.push_back(node);
623 if ((tokens.size() > 2 && tokens.front() == _name) ||
624 (tokens.size() > 1 && _name ==
"")) {
625 if (_name !=
"") tokens.pop_front();
626 if (tokens.size() == 2) {
628 if (regmap->
get_name() == tokens.front()) {
629 return regmap->
lookup(tokens.back());
633 }
else if (not _regmap_dbs.empty()) {
636 BOOST_FOREACH(
const std::string& node, tokens) {
637 newpath += (
"/" + node);
642 return db->
lookup(newpath.substr(1));
643 }
catch (std::exception&) {
656 std::vector<std::string> paths;
658 const std::vector<std::string>& regs = regmap->
enumerate();
659 paths.insert(paths.end(), regs.begin(), regs.end());
662 const std::vector<std::string>& regs = db->
enumerate();
663 paths.insert(paths.end(), regs.begin(), regs.end());
669 typedef std::list<soft_regmap_accessor_t*> db_t;
671 const std::string _name;
void refresh()
Definition: soft_register.hpp:495
soft_register_sync_t< uint16_t, false, true > soft_reg16_wo_sync_t
Definition: soft_register.hpp:377
uint32_t soft_reg_field_t
Definition: soft_register.hpp:75
UHD_INLINE void flush()
Definition: soft_register.hpp:198
Definition: soft_register.hpp:126
soft_reg_flush_mode_t
Definition: soft_register.hpp:126
boost::shared_ptr< soft_register_t< reg_data_t, readable, writable > > sptr
Definition: soft_register.hpp:136
Definition: soft_register.hpp:298
Definition: exception.hpp:106
soft_regmap_t(const std::string &name)
Definition: soft_register.hpp:457
UHD_INLINE bool unlikely(bool expr)
hint for the branch prediction
Definition: soft_register.hpp:59
UHD_INLINE void refresh()
Definition: soft_register.hpp:224
soft_register_t< uint64_t, false, true > soft_reg64_wo_t
Definition: soft_register.hpp:388
virtual std::vector< std::string > enumerate() const
Definition: soft_register.hpp:519
soft_register_t(wb_iface::wb_addr_type addr, soft_reg_flush_mode_t mode=ALWAYS_FLUSH)
Definition: soft_register.hpp:155
#define UHD_DEFINE_SOFT_REG_FIELD(name, width, shift)
Definition: soft_register.hpp:41
soft_register_t< uint32_t, true, false > soft_reg32_ro_t
Definition: soft_register.hpp:382
UHD_INLINE size_t get_bitwidth()
Definition: soft_register.hpp:263
virtual soft_register_base & lookup(const std::string &name) const
Definition: soft_register.hpp:506
soft_register_sync_t(wb_iface::wb_addr_type addr, soft_reg_flush_mode_t mode=ALWAYS_FLUSH)
Definition: soft_register.hpp:309
UHD_INLINE bool is_writable()
Definition: soft_register.hpp:280
soft_register_sync_t< uint64_t, true, true > soft_reg64_rw_sync_t
Definition: soft_register.hpp:393
Definition: exception.hpp:91
Definition: soft_register.hpp:564
virtual std::vector< std::string > enumerate() const
Definition: soft_register.hpp:655
soft_register_sync_t< uint32_t, false, true > soft_reg32_wo_sync_t
Definition: soft_register.hpp:384
soft_register_t< uint32_t, false, true > soft_reg32_wo_t
Definition: soft_register.hpp:381
UHD_INLINE size_t width(const soft_reg_field_t field)
Definition: soft_register.hpp:78
Definition: soft_register.hpp:529
UHD_INLINE void set(const soft_reg_field_t field, const reg_data_t value)
Definition: soft_register.hpp:180
soft_regmap_db_t(const std::string &name)
Definition: soft_register.hpp:576
virtual ~soft_regmap_accessor_t()
Definition: soft_register.hpp:439
Definition: soft_register.hpp:134
UHD_INLINE reg_data_t get(const soft_reg_field_t field)
Definition: soft_register.hpp:190
Definition: build_info.hpp:14
soft_regmap_db_t()
Definition: soft_register.hpp:571
UHD_INLINE void write(const soft_reg_field_t field, const reg_data_t value)
Definition: soft_register.hpp:345
virtual std::vector< std::string > enumerate() const =0
virtual soft_register_base & lookup(const std::string &path) const =0
virtual ~soft_register_base()
Definition: soft_register.hpp:103
void flush()
Definition: soft_register.hpp:483
visibility_t
Definition: soft_register.hpp:528
UHD_INLINE void initialize(wb_iface &iface, bool sync=false)
Definition: soft_register.hpp:166
boost::shared_ptr< soft_register_sync_t< reg_data_t, readable, writable > > sptr
Definition: soft_register.hpp:300
void initialize(wb_iface &iface, bool sync=false)
Definition: soft_register.hpp:471
virtual const std::string & get_name() const =0
soft_register_sync_t< uint64_t, true, false > soft_reg64_ro_sync_t
Definition: soft_register.hpp:392
virtual ~soft_regmap_t()
Definition: soft_register.hpp:458
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:302
boost::shared_ptr< soft_regmap_accessor_t > sptr
Definition: soft_register.hpp:437
Definition: soft_register.hpp:435
UHD_INLINE bool is_readable()
Definition: soft_register.hpp:272
#define UHD_INLINE
Definition: config.h:53
UHD_INLINE void flush()
Definition: soft_register.hpp:333
UHD_INLINE reg_data_t read(const soft_reg_field_t field)
Definition: soft_register.hpp:254
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:144
soft_register_t< uint64_t, true, false > soft_reg64_ro_t
Definition: soft_register.hpp:389
boost::shared_ptr< soft_regmap_db_t > sptr
Definition: soft_register.hpp:566
Definition: soft_register.hpp:455
Definition: soft_register.hpp:101
soft_register_sync_t< uint32_t, true, false > soft_reg32_ro_sync_t
Definition: soft_register.hpp:385
Definition: exception.hpp:42
#define UHD_API
Definition: config.h:63
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:586
UHD_INLINE reg_data_t read(const soft_reg_field_t field)
Definition: soft_register.hpp:351
const std::string & get_name() const
Definition: soft_register.hpp:581
UHD_INLINE void refresh()
Definition: soft_register.hpp:339
soft_register_t< uint64_t, true, true > soft_reg64_rw_t
Definition: soft_register.hpp:390
UHD_INLINE void write(const soft_reg_field_t field, const reg_data_t value)
Definition: soft_register.hpp:245
soft_register_t< uint16_t, true, true > soft_reg16_rw_t
Definition: soft_register.hpp:376
Definition: exception.hpp:70
void add(soft_regmap_db_t &db)
Definition: soft_register.hpp:594
virtual void initialize(wb_iface &iface, bool sync=false)=0
soft_register_base & lookup(const std::string &path) const
Definition: soft_register.hpp:613
soft_register_sync_t< uint16_t, true, true > soft_reg16_rw_sync_t
Definition: soft_register.hpp:379
soft_register_sync_t< uint64_t, false, true > soft_reg64_wo_sync_t
Definition: soft_register.hpp:391
UHD_INLINE void initialize(wb_iface &iface, bool sync=false)
Definition: soft_register.hpp:315
soft_register_t< uint16_t, false, true > soft_reg16_wo_t
Definition: soft_register.hpp:374
UHD_INLINE bool likely(bool expr)
hint for the branch prediction
Definition: soft_register.hpp:49
soft_register_sync_t< uint32_t, true, true > soft_reg32_rw_sync_t
Definition: soft_register.hpp:386
virtual UHD_INLINE const std::string & get_name() const
Definition: soft_register.hpp:463
UHD_INLINE void add_to_map(soft_register_base ®, const std::string &name, const visibility_t visible=PRIVATE)
Definition: soft_register.hpp:536
UHD_INLINE size_t shift(const soft_reg_field_t field)
Definition: soft_register.hpp:82
Definition: soft_register.hpp:126
uint32_t wb_addr_type
Definition: wb_iface.hpp:23
static UHD_INLINE soft_reg_t & cast(soft_register_base ®)
Definition: soft_register.hpp:116
Definition: wb_iface.hpp:19
soft_register_t< uint16_t, true, false > soft_reg16_ro_t
Definition: soft_register.hpp:375
soft_register_sync_t< uint16_t, true, false > soft_reg16_ro_sync_t
Definition: soft_register.hpp:378
soft_register_t< uint32_t, true, true > soft_reg32_rw_t
Definition: soft_register.hpp:383