18 #ifndef INCLUDED_UHD_UTILS_SOFT_REGISTER_HPP 19 #define INCLUDED_UHD_UTILS_SOFT_REGISTER_HPP 21 #include <boost/cstdint.hpp> 22 #include <boost/noncopyable.hpp> 26 #include <boost/thread/mutex.hpp> 27 #include <boost/thread/locks.hpp> 28 #include <boost/unordered_map.hpp> 29 #include <boost/tokenizer.hpp> 30 #include <boost/foreach.hpp> 31 #include <boost/lexical_cast.hpp> 52 #define UHD_DEFINE_SOFT_REG_FIELD(name, width, shift) \ 53 static const uhd::soft_reg_field_t name = (((shift & 0xFF) << 8) | (width & 0xFF)) 63 return __builtin_expect(expr,
true);
73 return __builtin_expect(expr,
false);
88 namespace soft_reg_field {
89 inline size_t width(
const soft_reg_field_t field) {
90 return (field & 0xFF);
93 inline size_t shift(
const soft_reg_field_t field) {
94 return ((field >> 8) & 0xFF);
97 template<
typename data_t>
98 inline size_t mask(
const soft_reg_field_t field) {
99 static const data_t ONE =
static_cast<data_t
>(1);
104 return ((ONE<<
width(field))-ONE)<<
shift(field);
106 return (0-ONE)<<
shift(field);
114 virtual void initialize(
wb_iface& iface,
bool sync =
false) = 0;
115 virtual void flush() = 0;
116 virtual void refresh() = 0;
117 virtual size_t get_bitwidth() = 0;
118 virtual bool is_readable() = 0;
119 virtual bool is_writable() = 0;
124 template <
typename soft_reg_t>
126 soft_reg_t* ptr =
dynamic_cast<soft_reg_t*
>(®);
142 template<
typename reg_data_t,
bool readable,
bool writable>
145 typedef boost::shared_ptr< soft_register_t<reg_data_t, readable, writable> >
sptr;
157 _iface(NULL), _wr_addr(wr_addr), _rd_addr(rd_addr), _soft_copy(0), _flush_mode(mode)
167 _iface(NULL), _wr_addr(addr), _rd_addr(addr), _soft_copy(0), _flush_mode(mode)
180 if (sync && writable) flush();
181 if (sync && readable) refresh();
189 inline void set(
const soft_reg_field_t field,
const reg_data_t value)
191 _soft_copy = (_soft_copy & ~soft_reg_field::mask<reg_data_t>(field)) |
192 ((value << soft_reg_field::shift(field)) & soft_reg_field::mask<reg_data_t>(field));
199 inline reg_data_t
get(
const soft_reg_field_t field)
209 if (writable && _iface) {
213 if (_flush_mode == ALWAYS_FLUSH || _soft_copy.is_dirty()) {
214 if (get_bitwidth() <= 16) {
215 _iface->poke16(_wr_addr, static_cast<boost::uint16_t>(_soft_copy));
216 }
else if (get_bitwidth() <= 32) {
217 _iface->poke32(_wr_addr, static_cast<boost::uint32_t>(_soft_copy));
218 }
else if (get_bitwidth() <= 64) {
219 _iface->poke64(_wr_addr, static_cast<boost::uint64_t>(_soft_copy));
223 _soft_copy.mark_clean();
235 if (readable && _iface) {
236 if (get_bitwidth() <= 16) {
237 _soft_copy =
static_cast<reg_data_t
>(_iface->peek16(_rd_addr));
238 }
else if (get_bitwidth() <= 32) {
239 _soft_copy =
static_cast<reg_data_t
>(_iface->peek32(_rd_addr));
240 }
else if (get_bitwidth() <= 64) {
241 _soft_copy =
static_cast<reg_data_t
>(_iface->peek64(_rd_addr));
245 _soft_copy.mark_clean();
254 inline void write(
const soft_reg_field_t field,
const reg_data_t value)
263 inline reg_data_t
read(
const soft_reg_field_t field)
274 static const size_t BITS_IN_BYTE = 8;
275 return sizeof(reg_data_t) * BITS_IN_BYTE;
306 template<
typename reg_data_t,
bool readable,
bool writable>
309 typedef boost::shared_ptr< soft_register_sync_t<reg_data_t, readable, writable> >
sptr;
315 soft_register_t<reg_data_t, readable, writable>(wr_addr, rd_addr, mode), _mutex()
326 boost::lock_guard<boost::mutex> lock(_mutex);
330 inline void set(
const soft_reg_field_t field,
const reg_data_t value)
332 boost::lock_guard<boost::mutex> lock(_mutex);
336 inline reg_data_t
get(
const soft_reg_field_t field)
338 boost::lock_guard<boost::mutex> lock(_mutex);
344 boost::lock_guard<boost::mutex> lock(_mutex);
350 boost::lock_guard<boost::mutex> lock(_mutex);
354 inline void write(
const soft_reg_field_t field,
const reg_data_t value)
356 boost::lock_guard<boost::mutex> lock(_mutex);
360 inline reg_data_t
read(
const soft_reg_field_t field)
362 boost::lock_guard<boost::mutex> lock(_mutex);
446 typedef boost::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;
472 virtual inline const std::string&
get_name()
const {
return _name; }
481 boost::lock_guard<boost::mutex> lock(_mutex);
493 boost::lock_guard<boost::mutex> lock(_mutex);
505 boost::lock_guard<boost::mutex> lock(_mutex);
516 regmap_t::const_iterator iter = _regmap.find(name);
517 if (iter != _regmap.end()) {
518 return *(iter->second);
529 std::vector<std::string> temp;
530 BOOST_FOREACH(
const regmap_t::value_type& reg, _regmap) {
531 temp.push_back(_name +
"/" + reg.first);
546 boost::lock_guard<boost::mutex> lock(_mutex);
547 if (visible == PUBLIC) {
549 if (not _regmap.insert(regmap_t::value_type(name, ®)).second) {
553 _reglist.push_back(®);
557 typedef boost::unordered_map<std::string, soft_register_base*> regmap_t;
558 typedef std::list<soft_register_base*> reglist_t;
560 const std::string _name;
575 typedef boost::shared_ptr<soft_regmap_db_t>
sptr;
590 const std::string&
get_name()
const {
return _name; }
596 boost::lock_guard<boost::mutex> lock(_mutex);
597 _regmaps.push_back(®map);
604 boost::lock_guard<boost::mutex> lock(_mutex);
608 _regmap_dbs.push_back(&db);
625 std::list<std::string> tokens;
627 const std::string& node,
628 boost::tokenizer< boost::char_separator<char> >(path, boost::char_separator<char>(
"/")))
630 tokens.push_back(node);
632 if ((tokens.size() > 2 && tokens.front() == _name) ||
633 (tokens.size() > 1 && _name ==
"")) {
634 if (_name !=
"") tokens.pop_front();
635 if (tokens.size() == 2) {
637 if (regmap->
get_name() == tokens.front()) {
638 return regmap->
lookup(tokens.back());
642 }
else if (not _regmap_dbs.empty()) {
645 BOOST_FOREACH(
const std::string& node, tokens) {
646 newpath += (
"/" + node);
651 return db->
lookup(newpath.substr(1));
652 }
catch (std::exception&) {
665 std::vector<std::string> paths;
667 const std::vector<std::string>& regs = regmap->
enumerate();
668 paths.insert(paths.end(), regs.begin(), regs.end());
671 const std::vector<std::string>& regs = db->
enumerate();
672 paths.insert(paths.end(), regs.begin(), regs.end());
678 typedef std::list<soft_regmap_accessor_t*> db_t;
680 const std::string _name;
void refresh()
Definition: soft_register.hpp:348
void refresh()
Definition: soft_register.hpp:504
size_t width(const soft_reg_field_t field)
Definition: soft_register.hpp:89
void flush()
Definition: soft_register.hpp:207
reg_data_t get(const soft_reg_field_t field)
Definition: soft_register.hpp:199
void initialize(wb_iface &iface, bool sync=false)
Definition: soft_register.hpp:324
Definition: soft_register.hpp:135
bool likely(bool expr)
hint for the branch prediction
Definition: soft_register.hpp:60
soft_reg_flush_mode_t
Definition: soft_register.hpp:135
boost::shared_ptr< soft_register_t< reg_data_t, readable, writable > > sptr
Definition: soft_register.hpp:145
Definition: soft_register.hpp:307
Definition: exception.hpp:109
soft_register_t< boost::uint64_t, true, false > soft_reg64_ro_t
Definition: soft_register.hpp:398
soft_regmap_t(const std::string &name)
Definition: soft_register.hpp:466
void write(const soft_reg_field_t field, const reg_data_t value)
Definition: soft_register.hpp:354
soft_register_sync_t< boost::uint16_t, true, false > soft_reg16_ro_sync_t
Definition: soft_register.hpp:387
virtual std::vector< std::string > enumerate() const
Definition: soft_register.hpp:528
soft_register_t(wb_iface::wb_addr_type addr, soft_reg_flush_mode_t mode=ALWAYS_FLUSH)
Definition: soft_register.hpp:164
size_t mask(const soft_reg_field_t field)
Definition: soft_register.hpp:98
#define UHD_DEFINE_SOFT_REG_FIELD(name, width, shift)
Definition: soft_register.hpp:52
virtual soft_register_base & lookup(const std::string &name) const
Definition: soft_register.hpp:515
void add_to_map(soft_register_base ®, const std::string &name, const visibility_t visible=PRIVATE)
Definition: soft_register.hpp:545
soft_register_t< boost::uint64_t, false, true > soft_reg64_wo_t
Definition: soft_register.hpp:397
soft_register_sync_t(wb_iface::wb_addr_type addr, soft_reg_flush_mode_t mode=ALWAYS_FLUSH)
Definition: soft_register.hpp:318
bool is_readable()
Definition: soft_register.hpp:281
Definition: exception.hpp:94
reg_data_t read(const soft_reg_field_t field)
Definition: soft_register.hpp:263
Definition: soft_register.hpp:573
soft_register_t< boost::uint32_t, true, false > soft_reg32_ro_t
Definition: soft_register.hpp:391
soft_register_sync_t< boost::uint32_t, true, false > soft_reg32_ro_sync_t
Definition: soft_register.hpp:394
boost::uint32_t wb_addr_type
Definition: wb_iface.hpp:33
soft_register_t< boost::uint16_t, true, false > soft_reg16_ro_t
Definition: soft_register.hpp:384
virtual std::vector< std::string > enumerate() const
Definition: soft_register.hpp:664
soft_register_t< boost::uint32_t, false, true > soft_reg32_wo_t
Definition: soft_register.hpp:390
void write(const soft_reg_field_t field, const reg_data_t value)
Definition: soft_register.hpp:254
soft_register_sync_t< boost::uint16_t, false, true > soft_reg16_wo_sync_t
Definition: soft_register.hpp:386
Definition: soft_register.hpp:538
soft_regmap_db_t(const std::string &name)
Definition: soft_register.hpp:585
virtual ~soft_regmap_accessor_t()
Definition: soft_register.hpp:448
Definition: soft_register.hpp:143
reg_data_t read(const soft_reg_field_t field)
Definition: soft_register.hpp:360
Definition: convert.hpp:28
soft_regmap_db_t()
Definition: soft_register.hpp:580
boost::uint32_t soft_reg_field_t
Definition: soft_register.hpp:86
static soft_reg_t & cast(soft_register_base ®)
Definition: soft_register.hpp:125
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:112
void flush()
Definition: soft_register.hpp:492
visibility_t
Definition: soft_register.hpp:537
soft_register_sync_t< boost::uint16_t, true, true > soft_reg16_rw_sync_t
Definition: soft_register.hpp:388
soft_register_t< boost::uint16_t, false, true > soft_reg16_wo_t
Definition: soft_register.hpp:383
boost::shared_ptr< soft_register_sync_t< reg_data_t, readable, writable > > sptr
Definition: soft_register.hpp:309
void initialize(wb_iface &iface, bool sync=false)
Definition: soft_register.hpp:480
virtual const std::string & get_name() const =0
virtual ~soft_regmap_t()
Definition: soft_register.hpp:467
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:311
boost::shared_ptr< soft_regmap_accessor_t > sptr
Definition: soft_register.hpp:446
Definition: soft_register.hpp:444
size_t shift(const soft_reg_field_t field)
Definition: soft_register.hpp:93
soft_register_sync_t< boost::uint32_t, true, true > soft_reg32_rw_sync_t
Definition: soft_register.hpp:395
soft_register_sync_t< boost::uint32_t, false, true > soft_reg32_wo_sync_t
Definition: soft_register.hpp:393
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:153
boost::shared_ptr< soft_regmap_db_t > sptr
Definition: soft_register.hpp:575
Definition: soft_register.hpp:464
virtual const std::string & get_name() const
Definition: soft_register.hpp:472
Definition: soft_register.hpp:110
Definition: exception.hpp:52
soft_register_sync_t< boost::uint64_t, true, true > soft_reg64_rw_sync_t
Definition: soft_register.hpp:402
#define UHD_API
Definition: config.h:66
void add(soft_regmap_t ®map)
Definition: soft_register.hpp:595
void set(const soft_reg_field_t field, const reg_data_t value)
Definition: soft_register.hpp:189
void flush()
Definition: soft_register.hpp:342
size_t get_bitwidth()
Definition: soft_register.hpp:272
const std::string & get_name() const
Definition: soft_register.hpp:590
bool unlikely(bool expr)
hint for the branch prediction
Definition: soft_register.hpp:70
soft_register_t< boost::uint32_t, true, true > soft_reg32_rw_t
Definition: soft_register.hpp:392
soft_register_t< boost::uint16_t, true, true > soft_reg16_rw_t
Definition: soft_register.hpp:385
soft_register_t< boost::uint64_t, true, true > soft_reg64_rw_t
Definition: soft_register.hpp:399
Definition: exception.hpp:80
void initialize(wb_iface &iface, bool sync=false)
Definition: soft_register.hpp:175
void add(soft_regmap_db_t &db)
Definition: soft_register.hpp:603
virtual void initialize(wb_iface &iface, bool sync=false)=0
soft_register_base & lookup(const std::string &path) const
Definition: soft_register.hpp:622
void refresh()
Definition: soft_register.hpp:233
soft_register_sync_t< boost::uint64_t, true, false > soft_reg64_ro_sync_t
Definition: soft_register.hpp:401
soft_register_sync_t< boost::uint64_t, false, true > soft_reg64_wo_sync_t
Definition: soft_register.hpp:400
bool is_writable()
Definition: soft_register.hpp:289
Definition: soft_register.hpp:135
Definition: wb_iface.hpp:29