7 #ifndef INCLUDED_UHD_UTILS_SOFT_REGISTER_HPP 8 #define INCLUDED_UHD_UTILS_SOFT_REGISTER_HPP 11 #include <boost/noncopyable.hpp> 15 #include <boost/thread/mutex.hpp> 16 #include <boost/thread/locks.hpp> 17 #include <boost/unordered_map.hpp> 18 #include <boost/tokenizer.hpp> 19 #include <boost/foreach.hpp> 40 #define UHD_DEFINE_SOFT_REG_FIELD(name, width, shift) \ 41 static const uhd::soft_reg_field_t name = (((shift & 0xFF) << 8) | (width & 0xFF)) 51 return __builtin_expect(expr,
true);
61 return __builtin_expect(expr,
false);
76 namespace soft_reg_field {
78 return (field & 0xFF);
82 return ((field >> 8) & 0xFF);
85 template<
typename data_t>
87 static const data_t ONE =
static_cast<data_t
>(1);
92 return ((ONE<<
width(field))-ONE)<<
shift(field);
94 return (0-ONE)<<
shift(field);
102 virtual void initialize(
wb_iface& iface,
bool sync =
false) = 0;
103 virtual void flush() = 0;
104 virtual void refresh() = 0;
105 virtual size_t get_bitwidth() = 0;
106 virtual bool is_readable() = 0;
107 virtual bool is_writable() = 0;
112 template <
typename soft_reg_t>
114 soft_reg_t* ptr =
dynamic_cast<soft_reg_t*
>(®);
130 template<
typename reg_data_t,
bool readable,
bool writable>
133 typedef boost::shared_ptr< soft_register_t<reg_data_t, readable, writable> >
sptr;
145 _iface(NULL), _wr_addr(wr_addr), _rd_addr(rd_addr), _soft_copy(0), _flush_mode(mode)
155 _iface(NULL), _wr_addr(addr), _rd_addr(addr), _soft_copy(0), _flush_mode(mode)
168 if (sync && writable) flush();
169 if (sync && readable) refresh();
177 UHD_INLINE void set(
const soft_reg_field_t field,
const reg_data_t value)
179 _soft_copy = (_soft_copy & ~soft_reg_field::mask<reg_data_t>(field)) |
180 ((value << soft_reg_field::shift(field)) & soft_reg_field::mask<reg_data_t>(field));
197 if (writable && _iface) {
201 if (_flush_mode == ALWAYS_FLUSH || _soft_copy.is_dirty()) {
202 if (get_bitwidth() <= 16) {
203 _iface->poke16(_wr_addr, static_cast<uint16_t>(_soft_copy));
204 }
else if (get_bitwidth() <= 32) {
205 _iface->poke32(_wr_addr, static_cast<uint32_t>(_soft_copy));
206 }
else if (get_bitwidth() <= 64) {
207 _iface->poke64(_wr_addr, static_cast<uint64_t>(_soft_copy));
211 _soft_copy.mark_clean();
223 if (readable && _iface) {
224 if (get_bitwidth() <= 16) {
225 _soft_copy =
static_cast<reg_data_t
>(_iface->peek16(_rd_addr));
226 }
else if (get_bitwidth() <= 32) {
227 _soft_copy =
static_cast<reg_data_t
>(_iface->peek32(_rd_addr));
228 }
else if (get_bitwidth() <= 64) {
229 _soft_copy =
static_cast<reg_data_t
>(_iface->peek64(_rd_addr));
233 _soft_copy.mark_clean();
262 static const size_t BITS_IN_BYTE = 8;
263 return sizeof(reg_data_t) * BITS_IN_BYTE;
294 template<
typename reg_data_t,
bool readable,
bool writable>
297 typedef boost::shared_ptr< soft_register_sync_t<reg_data_t, readable, writable> >
sptr;
303 soft_register_t<reg_data_t, readable, writable>(wr_addr, rd_addr, mode), _mutex()
314 boost::lock_guard<boost::mutex> lock(_mutex);
318 UHD_INLINE void set(
const soft_reg_field_t field,
const reg_data_t value)
320 boost::lock_guard<boost::mutex> lock(_mutex);
326 boost::lock_guard<boost::mutex> lock(_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);
434 typedef boost::shared_ptr<soft_regmap_accessor_t>
sptr;
438 virtual std::vector<std::string> enumerate()
const = 0;
439 virtual const std::string& get_name()
const = 0;
469 boost::lock_guard<boost::mutex> lock(_mutex);
481 boost::lock_guard<boost::mutex> lock(_mutex);
493 boost::lock_guard<boost::mutex> lock(_mutex);
504 regmap_t::const_iterator iter = _regmap.find(name);
505 if (iter != _regmap.end()) {
506 return *(iter->second);
517 std::vector<std::string> temp;
518 BOOST_FOREACH(
const regmap_t::value_type& reg, _regmap) {
519 temp.push_back(_name +
"/" + reg.first);
534 boost::lock_guard<boost::mutex> lock(_mutex);
535 if (visible == PUBLIC) {
537 if (not _regmap.insert(regmap_t::value_type(name, ®)).second) {
541 _reglist.push_back(®);
545 typedef boost::unordered_map<std::string, soft_register_base*> regmap_t;
546 typedef std::list<soft_register_base*> reglist_t;
548 const std::string _name;
563 typedef boost::shared_ptr<soft_regmap_db_t>
sptr;
578 const std::string&
get_name()
const {
return _name; }
584 boost::lock_guard<boost::mutex> lock(_mutex);
585 _regmaps.push_back(®map);
592 boost::lock_guard<boost::mutex> lock(_mutex);
596 _regmap_dbs.push_back(&db);
613 std::list<std::string> tokens;
615 const std::string& node,
616 boost::tokenizer< boost::char_separator<char> >(path, boost::char_separator<char>(
"/")))
618 tokens.push_back(node);
620 if ((tokens.size() > 2 && tokens.front() == _name) ||
621 (tokens.size() > 1 && _name ==
"")) {
622 if (_name !=
"") tokens.pop_front();
623 if (tokens.size() == 2) {
625 if (regmap->
get_name() == tokens.front()) {
626 return regmap->
lookup(tokens.back());
630 }
else if (not _regmap_dbs.empty()) {
633 BOOST_FOREACH(
const std::string& node, tokens) {
634 newpath += (
"/" + node);
639 return db->
lookup(newpath.substr(1));
640 }
catch (std::exception&) {
653 std::vector<std::string> paths;
655 const std::vector<std::string>& regs = regmap->
enumerate();
656 paths.insert(paths.end(), regs.begin(), regs.end());
659 const std::vector<std::string>& regs = db->
enumerate();
660 paths.insert(paths.end(), regs.begin(), regs.end());
666 typedef std::list<soft_regmap_accessor_t*> db_t;
668 const std::string _name;
void refresh()
Definition: soft_register.hpp:492
soft_register_sync_t< uint16_t, false, true > soft_reg16_wo_sync_t
Definition: soft_register.hpp:374
uint32_t soft_reg_field_t
Definition: soft_register.hpp:74
UHD_INLINE void flush()
Definition: soft_register.hpp:195
Definition: soft_register.hpp:123
soft_reg_flush_mode_t
Definition: soft_register.hpp:123
boost::shared_ptr< soft_register_t< reg_data_t, readable, writable > > sptr
Definition: soft_register.hpp:133
Definition: soft_register.hpp:295
Definition: exception.hpp:105
soft_regmap_t(const std::string &name)
Definition: soft_register.hpp:454
UHD_INLINE bool unlikely(bool expr)
hint for the branch prediction
Definition: soft_register.hpp:58
UHD_INLINE void refresh()
Definition: soft_register.hpp:221
soft_register_t< uint64_t, false, true > soft_reg64_wo_t
Definition: soft_register.hpp:385
virtual std::vector< std::string > enumerate() const
Definition: soft_register.hpp:516
soft_register_t(wb_iface::wb_addr_type addr, soft_reg_flush_mode_t mode=ALWAYS_FLUSH)
Definition: soft_register.hpp:152
#define UHD_DEFINE_SOFT_REG_FIELD(name, width, shift)
Definition: soft_register.hpp:40
soft_register_t< uint32_t, true, false > soft_reg32_ro_t
Definition: soft_register.hpp:379
UHD_INLINE size_t get_bitwidth()
Definition: soft_register.hpp:260
virtual soft_register_base & lookup(const std::string &name) const
Definition: soft_register.hpp:503
soft_register_sync_t(wb_iface::wb_addr_type addr, soft_reg_flush_mode_t mode=ALWAYS_FLUSH)
Definition: soft_register.hpp:306
UHD_INLINE bool is_writable()
Definition: soft_register.hpp:277
soft_register_sync_t< uint64_t, true, true > soft_reg64_rw_sync_t
Definition: soft_register.hpp:390
Definition: exception.hpp:90
Definition: soft_register.hpp:561
virtual std::vector< std::string > enumerate() const
Definition: soft_register.hpp:652
soft_register_sync_t< uint32_t, false, true > soft_reg32_wo_sync_t
Definition: soft_register.hpp:381
soft_register_t< uint32_t, false, true > soft_reg32_wo_t
Definition: soft_register.hpp:378
UHD_INLINE size_t width(const soft_reg_field_t field)
Definition: soft_register.hpp:77
Definition: soft_register.hpp:526
UHD_INLINE void set(const soft_reg_field_t field, const reg_data_t value)
Definition: soft_register.hpp:177
soft_regmap_db_t(const std::string &name)
Definition: soft_register.hpp:573
virtual ~soft_regmap_accessor_t()
Definition: soft_register.hpp:436
Definition: soft_register.hpp:131
UHD_INLINE reg_data_t get(const soft_reg_field_t field)
Definition: soft_register.hpp:187
Definition: build_info.hpp:14
soft_regmap_db_t()
Definition: soft_register.hpp:568
UHD_INLINE void write(const soft_reg_field_t field, const reg_data_t value)
Definition: soft_register.hpp:342
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:100
void flush()
Definition: soft_register.hpp:480
visibility_t
Definition: soft_register.hpp:525
UHD_INLINE void initialize(wb_iface &iface, bool sync=false)
Definition: soft_register.hpp:163
boost::shared_ptr< soft_register_sync_t< reg_data_t, readable, writable > > sptr
Definition: soft_register.hpp:297
void initialize(wb_iface &iface, bool sync=false)
Definition: soft_register.hpp:468
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:389
virtual ~soft_regmap_t()
Definition: soft_register.hpp:455
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:299
boost::shared_ptr< soft_regmap_accessor_t > sptr
Definition: soft_register.hpp:434
Definition: soft_register.hpp:432
UHD_INLINE bool is_readable()
Definition: soft_register.hpp:269
#define UHD_INLINE
Definition: config.h:52
UHD_INLINE void flush()
Definition: soft_register.hpp:330
UHD_INLINE reg_data_t read(const soft_reg_field_t field)
Definition: soft_register.hpp:251
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:141
soft_register_t< uint64_t, true, false > soft_reg64_ro_t
Definition: soft_register.hpp:386
boost::shared_ptr< soft_regmap_db_t > sptr
Definition: soft_register.hpp:563
Definition: soft_register.hpp:452
Definition: soft_register.hpp:98
soft_register_sync_t< uint32_t, true, false > soft_reg32_ro_sync_t
Definition: soft_register.hpp:382
Definition: exception.hpp:41
#define UHD_API
Definition: config.h:62
UHD_INLINE data_t mask(const soft_reg_field_t field)
Definition: soft_register.hpp:86
void add(soft_regmap_t ®map)
Definition: soft_register.hpp:583
UHD_INLINE reg_data_t read(const soft_reg_field_t field)
Definition: soft_register.hpp:348
const std::string & get_name() const
Definition: soft_register.hpp:578
UHD_INLINE void refresh()
Definition: soft_register.hpp:336
soft_register_t< uint64_t, true, true > soft_reg64_rw_t
Definition: soft_register.hpp:387
UHD_INLINE void write(const soft_reg_field_t field, const reg_data_t value)
Definition: soft_register.hpp:242
soft_register_t< uint16_t, true, true > soft_reg16_rw_t
Definition: soft_register.hpp:373
Definition: exception.hpp:69
void add(soft_regmap_db_t &db)
Definition: soft_register.hpp:591
virtual void initialize(wb_iface &iface, bool sync=false)=0
soft_register_base & lookup(const std::string &path) const
Definition: soft_register.hpp:610
soft_register_sync_t< uint16_t, true, true > soft_reg16_rw_sync_t
Definition: soft_register.hpp:376
soft_register_sync_t< uint64_t, false, true > soft_reg64_wo_sync_t
Definition: soft_register.hpp:388
UHD_INLINE void initialize(wb_iface &iface, bool sync=false)
Definition: soft_register.hpp:312
soft_register_t< uint16_t, false, true > soft_reg16_wo_t
Definition: soft_register.hpp:371
UHD_INLINE bool likely(bool expr)
hint for the branch prediction
Definition: soft_register.hpp:48
soft_register_sync_t< uint32_t, true, true > soft_reg32_rw_sync_t
Definition: soft_register.hpp:383
virtual UHD_INLINE const std::string & get_name() const
Definition: soft_register.hpp:460
UHD_INLINE void add_to_map(soft_register_base ®, const std::string &name, const visibility_t visible=PRIVATE)
Definition: soft_register.hpp:533
UHD_INLINE size_t shift(const soft_reg_field_t field)
Definition: soft_register.hpp:81
Definition: soft_register.hpp:123
uint32_t wb_addr_type
Definition: wb_iface.hpp:22
static UHD_INLINE soft_reg_t & cast(soft_register_base ®)
Definition: soft_register.hpp:113
Definition: wb_iface.hpp:18
soft_register_t< uint16_t, true, false > soft_reg16_ro_t
Definition: soft_register.hpp:372
soft_register_sync_t< uint16_t, true, false > soft_reg16_ro_sync_t
Definition: soft_register.hpp:375
soft_register_t< uint32_t, true, true > soft_reg32_rw_t
Definition: soft_register.hpp:380