aboutsummaryrefslogtreecommitdiffstats
path: root/host
diff options
context:
space:
mode:
Diffstat (limited to 'host')
-rw-r--r--host/include/uhd/rfnoc/mock_block.hpp (renamed from host/tests/rfnoc_mock_reg_iface.hpp)69
-rw-r--r--host/lib/rfnoc/CMakeLists.txt1
-rw-r--r--host/lib/rfnoc/mock_block.cpp52
-rw-r--r--host/lib/rfnoc/registry_factory.cpp8
-rw-r--r--host/tests/CMakeLists.txt1
-rw-r--r--host/tests/client_zero_test.cpp3
-rw-r--r--host/tests/rfnoc_blocks_test.cpp70
7 files changed, 135 insertions, 69 deletions
diff --git a/host/tests/rfnoc_mock_reg_iface.hpp b/host/include/uhd/rfnoc/mock_block.hpp
index a6e85b790..18c614d07 100644
--- a/host/tests/rfnoc_mock_reg_iface.hpp
+++ b/host/include/uhd/rfnoc/mock_block.hpp
@@ -4,17 +4,27 @@
// SPDX-License-Identifier: GPL-3.0-or-later
//
-#ifndef INCLUDED_LIBUHD_TESTS_MOCK_REG_IFACE_HPP
-#define INCLUDED_LIBUHD_TESTS_MOCK_REG_IFACE_HPP
+#ifndef INCLUDED_LIBUHD_MOCK_BLOCK_HPP
+#define INCLUDED_LIBUHD_MOCK_BLOCK_HPP
+#include <uhd/config.hpp>
+#include <uhd/property_tree.hpp>
+#include <uhd/rfnoc/defaults.hpp>
+#include <uhd/rfnoc/noc_block_base.hpp>
#include <uhd/rfnoc/register_iface.hpp>
-#include <uhd/utils/log.hpp>
#include <uhd/types/time_spec.hpp>
-#include <boost/format.hpp>
+#include <uhd/utils/log.hpp>
#include <unordered_map>
+#include <boost/format.hpp>
#include <vector>
-class mock_reg_iface_t : public uhd::rfnoc::register_iface
+namespace uhd { namespace rfnoc {
+
+/*! Mock version of a register interface
+ *
+ * This can be used for mock blocks, usually for the sake of unit testing.
+ */
+class UHD_API mock_reg_iface_t : public register_iface
{
public:
mock_reg_iface_t() = default;
@@ -58,7 +68,8 @@ public:
try {
return read_memory.at(addr);
} catch (const std::out_of_range&) {
- throw uhd::runtime_error(str(boost::format("No data defined for address: 0x%04X") % addr));
+ throw uhd::runtime_error(
+ str(boost::format("No data defined for address: 0x%04X") % addr));
}
}
@@ -123,7 +134,10 @@ public:
bool force_timeout = false;
+ //! All pokes end up writing to this map
std::unordered_map<uint32_t, uint32_t> read_memory;
+ //! All peeks read from this map. A peek will fail if the address has not
+ // been previously set.
std::unordered_map<uint32_t, uint32_t> write_memory;
protected:
@@ -131,8 +145,49 @@ protected:
uint32_t /*addr*/, uint32_t /*data*/, uhd::time_spec_t /*time*/, bool /*ack*/)
{
}
+
virtual void _peek_cb(uint32_t /*addr*/, uhd::time_spec_t /*time*/) {}
};
+/*! Container for all the items required for running a mock block
+ */
+struct UHD_API mock_block_container
+{
+ friend class get_mock_block;
+ //! Reference to the register interface object
+ std::shared_ptr<mock_reg_iface_t> reg_iface;
+
+ //! Reference to the prop tree object the block sees
+ uhd::property_tree::sptr tree;
+
+ //! Use this to retrieve a reference to the block controller. Make sure that
+ // the register space is appropiately primed before doing so.
+ template<typename block_type = noc_block_base>
+ std::shared_ptr<block_type> get_block()
+ {
+ return std::dynamic_pointer_cast<block_type>(factory(std::move(make_args)));
+ }
+
+ //! Factory to get the block. Use get_block() instead.
+ std::function<noc_block_base::sptr(noc_block_base::make_args_ptr)> factory;
+
+ // Note: The make args would ideally be captured by the factory function,
+ // but std::functions need to be CopyConstructible, and this struct doesn't,
+ // so it needs to live out here in the open.
+ noc_block_base::make_args_ptr make_args;
+
+};
+
+/*! Factory function for mock block controllers
+ */
+UHD_API mock_block_container get_mock_block(const noc_id_t noc_id,
+ const size_t num_inputs = 1,
+ const size_t num_outputs = 1,
+ const uhd::device_addr_t& args = uhd::device_addr_t(),
+ const size_t mtu = 8000,
+ const device_type_t device_id = ANY_DEVICE);
+
+
+}}; // namespace uhd::rfnoc
-#endif /* INCLUDED_LIBUHD_TESTS_MOCK_REG_IFACE_HPP */
+#endif /* INCLUDED_LIBUHD_MOCK_BLOCK_HPP */
diff --git a/host/lib/rfnoc/CMakeLists.txt b/host/lib/rfnoc/CMakeLists.txt
index e685cef26..ea774b2c8 100644
--- a/host/lib/rfnoc/CMakeLists.txt
+++ b/host/lib/rfnoc/CMakeLists.txt
@@ -38,6 +38,7 @@ LIBUHD_APPEND_SOURCES(
${CMAKE_CURRENT_SOURCE_DIR}/rfnoc_rx_streamer.cpp
${CMAKE_CURRENT_SOURCE_DIR}/rfnoc_tx_streamer.cpp
${CMAKE_CURRENT_SOURCE_DIR}/tx_async_msg_queue.cpp
+ ${CMAKE_CURRENT_SOURCE_DIR}/mock_block.cpp
# Default block control classes:
${CMAKE_CURRENT_SOURCE_DIR}/block_control.cpp
${CMAKE_CURRENT_SOURCE_DIR}/ddc_block_control.cpp
diff --git a/host/lib/rfnoc/mock_block.cpp b/host/lib/rfnoc/mock_block.cpp
new file mode 100644
index 000000000..867311097
--- /dev/null
+++ b/host/lib/rfnoc/mock_block.cpp
@@ -0,0 +1,52 @@
+//
+// Copyright 2019 Ettus Research, a National Instruments Brand
+//
+// SPDX-License-Identifier: GPL-3.0-or-later
+//
+
+#include <uhd/property_tree.hpp>
+#include <uhd/rfnoc/defaults.hpp>
+#include <uhd/rfnoc/mock_block.hpp>
+#include <uhd/types/device_addr.hpp>
+#include <uhdlib/rfnoc/clock_iface.hpp>
+#include <uhdlib/rfnoc/factory.hpp>
+
+using namespace uhd::rfnoc;
+
+uhd::rfnoc::mock_block_container uhd::rfnoc::get_mock_block(const noc_id_t noc_id,
+ const size_t num_inputs,
+ const size_t num_outputs,
+ const device_addr_t& args,
+ const size_t mtu,
+ const device_type_t device_id)
+{
+ block_factory_info_t fac_info = factory::get_block_factory(noc_id, device_id);
+
+ mock_block_container ret_val;
+ ret_val.factory = fac_info.factory_fn;
+ ret_val.reg_iface = std::make_shared<mock_reg_iface_t>();
+ ret_val.tree = uhd::property_tree::make();
+ // Create make args
+ ret_val.make_args = std::make_unique<noc_block_base::make_args_t>();
+ ret_val.make_args->noc_id = noc_id;
+ ret_val.make_args->block_id = block_id_t(fac_info.block_name);
+ ret_val.make_args->num_input_ports = num_inputs;
+ ret_val.make_args->num_output_ports = num_outputs;
+ ret_val.make_args->mtu = mtu;
+ ret_val.make_args->reg_iface = ret_val.reg_iface;
+ ret_val.make_args->tree = ret_val.tree;
+ ret_val.make_args->args = args;
+ ret_val.make_args->tb_clk_iface =
+ std::make_shared<clock_iface>(fac_info.timebase_clk);
+ ret_val.make_args->ctrlport_clk_iface =
+ std::make_shared<clock_iface>(fac_info.ctrlport_clk);
+ // TODO Make a mock mb controller too
+ ret_val.make_args->mb_control = nullptr;
+ if (fac_info.mb_access) {
+ UHD_LOG_WARNING("MOCK", "Mock block controllers cannot have mb_controllers.");
+ }
+
+ // Make block and return
+ return ret_val;
+}
+
diff --git a/host/lib/rfnoc/registry_factory.cpp b/host/lib/rfnoc/registry_factory.cpp
index bf1bc60a5..b251410bf 100644
--- a/host/lib/rfnoc/registry_factory.cpp
+++ b/host/lib/rfnoc/registry_factory.cpp
@@ -5,13 +5,14 @@
//
#include <uhd/exception.hpp>
-#include <uhd/rfnoc/registry.hpp>
-#include <uhd/rfnoc/defaults.hpp>
#include <uhd/rfnoc/constants.hpp>
+#include <uhd/rfnoc/defaults.hpp>
+#include <uhd/rfnoc/mock_block.hpp>
+#include <uhd/rfnoc/registry.hpp>
#include <uhd/utils/static.hpp>
#include <uhdlib/rfnoc/factory.hpp>
-#include <boost/functional/hash.hpp>
#include <unordered_map>
+#include <boost/functional/hash.hpp>
#include <iomanip>
#include <iostream>
#include <sstream>
@@ -106,3 +107,4 @@ block_factory_info_t factory::get_block_factory(noc_id_t noc_id, device_type_t d
}
return get_direct_block_registry().at(key);
}
+
diff --git a/host/tests/CMakeLists.txt b/host/tests/CMakeLists.txt
index 017778fb8..bf47f6638 100644
--- a/host/tests/CMakeLists.txt
+++ b/host/tests/CMakeLists.txt
@@ -245,7 +245,6 @@ UHD_ADD_NONAPI_TEST(
TARGET rfnoc_blocks_test.cpp
EXTRA_SOURCES
${CMAKE_SOURCE_DIR}/lib/rfnoc/graph.cpp
- ${CMAKE_SOURCE_DIR}/lib/rfnoc/registry_factory.cpp
)
UHD_ADD_NONAPI_TEST(
diff --git a/host/tests/client_zero_test.cpp b/host/tests/client_zero_test.cpp
index e97dd83e2..927161418 100644
--- a/host/tests/client_zero_test.cpp
+++ b/host/tests/client_zero_test.cpp
@@ -5,6 +5,7 @@
//
#include <uhd/rfnoc/register_iface.hpp>
+#include <uhd/rfnoc/mock_block.hpp>
#include <uhd/utils/log.hpp>
#include <uhdlib/rfnoc/client_zero.hpp>
#include <uhdlib/utils/narrow.hpp>
@@ -14,8 +15,6 @@
#include <cstring>
#include <memory>
-#include "rfnoc_mock_reg_iface.hpp"
-
using namespace uhd;
using namespace uhd::rfnoc;
diff --git a/host/tests/rfnoc_blocks_test.cpp b/host/tests/rfnoc_blocks_test.cpp
index ea1f91b6b..b9899177e 100644
--- a/host/tests/rfnoc_blocks_test.cpp
+++ b/host/tests/rfnoc_blocks_test.cpp
@@ -5,14 +5,12 @@
//
#include "rfnoc_graph_mock_nodes.hpp"
-#include "rfnoc_mock_reg_iface.hpp"
+#include <uhd/rfnoc/mock_block.hpp>
#include <uhd/rfnoc/actions.hpp>
#include <uhd/rfnoc/ddc_block_control.hpp>
#include <uhd/rfnoc/defaults.hpp>
#include <uhd/rfnoc/duc_block_control.hpp>
#include <uhd/rfnoc/null_block_control.hpp>
-#include <uhdlib/rfnoc/clock_iface.hpp>
-#include <uhdlib/rfnoc/factory.hpp>
#include <uhdlib/rfnoc/graph.hpp>
#include <uhdlib/rfnoc/node_accessor.hpp>
#include <uhdlib/utils/narrow.hpp>
@@ -21,47 +19,10 @@
using namespace uhd::rfnoc;
-// Redeclare this here, since it's only defined outside of UHD_API
-noc_block_base::make_args_t::~make_args_t() = default;
-
namespace {
constexpr size_t DEFAULT_MTU = 8000;
-noc_block_base::make_args_ptr make_make_args(noc_id_t noc_id,
- const std::string& block_id,
- const size_t n_inputs,
- const size_t n_outputs,
- const std::string& tb_clock_name = CLOCK_KEY_GRAPH,
- const std::string& cp_clock_name = "MOCK_CLOCK")
-{
- auto make_args = std::make_unique<noc_block_base::make_args_t>();
- make_args->noc_id = noc_id;
- make_args->num_input_ports = n_inputs;
- make_args->num_output_ports = n_outputs;
- make_args->mtu = DEFAULT_MTU;
- make_args->reg_iface = std::make_shared<mock_reg_iface_t>();
- make_args->block_id = block_id;
- make_args->ctrlport_clk_iface = std::make_shared<clock_iface>(cp_clock_name);
- make_args->tb_clk_iface = std::make_shared<clock_iface>(tb_clock_name);
- make_args->tree = uhd::property_tree::make();
- return make_args;
-}
-
-noc_block_base::sptr make_block(noc_block_base::make_args_ptr&& make_args)
-{
- try {
- auto block_factory_info =
- factory::get_block_factory(make_args->noc_id, ANY_DEVICE);
- return block_factory_info.factory_fn(std::move(make_args));
- } catch (std::out_of_range&) {
- UHD_LOG_WARNING("TEST",
- "Skipping tests due to Windows linker misconfiguration that needs to be "
- "resolved.");
- exit(0);
- }
-}
-
} // namespace
BOOST_AUTO_TEST_CASE(test_null_block)
@@ -72,17 +33,17 @@ BOOST_AUTO_TEST_CASE(test_null_block)
constexpr uint32_t item_width = 32;
constexpr noc_id_t noc_id = 0x00000001;
- auto make_args = make_make_args(noc_id, "0/NullSrcSink#0", num_chans, num_chans);
- auto reg_iface = std::dynamic_pointer_cast<mock_reg_iface_t>(make_args->reg_iface);
- auto set_mem = [&](const uint32_t addr, const uint32_t data) {
+ auto block_container = get_mock_block(noc_id, num_chans, num_chans);
+ // Shorthand to save typing
+ auto& reg_iface = block_container.reg_iface;
+ auto set_mem = [&](const uint32_t addr, const uint32_t data) {
reg_iface->read_memory[addr] = data;
};
auto get_mem = [&](const uint32_t addr) { return reg_iface->write_memory[addr]; };
auto copy_mem = [&](const uint32_t addr) { set_mem(addr, get_mem(addr)); };
-
set_mem(null_block_control::REG_CTRL_STATUS, (nipc << 24) | (item_width << 16));
- auto test_null =
- std::dynamic_pointer_cast<null_block_control>(make_block(std::move(make_args)));
+
+ auto test_null = block_container.get_block<null_block_control>();
BOOST_REQUIRE(test_null);
using uhd::stream_cmd_t;
@@ -163,15 +124,14 @@ BOOST_AUTO_TEST_CASE(test_ddc_block)
constexpr noc_id_t noc_id = DDC_BLOCK;
constexpr int TEST_DECIM = 20;
- auto ddc_make_args = make_make_args(noc_id, "0/DDC#0", num_chans, num_chans);
- ddc_make_args->args = uhd::device_addr_t("foo=bar");
- auto ddc_reg_iface =
- std::dynamic_pointer_cast<mock_reg_iface_t>(ddc_make_args->reg_iface);
+ auto block_container =
+ get_mock_block(noc_id, num_chans, num_chans, uhd::device_addr_t("foo=bar"));
+ auto& ddc_reg_iface = block_container.reg_iface;
ddc_reg_iface->read_memory[ddc_block_control::RB_COMPAT_NUM] =
(ddc_block_control::MAJOR_COMPAT << 16) | ddc_block_control::MINOR_COMPAT;
ddc_reg_iface->read_memory[ddc_block_control::RB_NUM_HB] = num_hb;
ddc_reg_iface->read_memory[ddc_block_control::RB_CIC_MAX_DECIM] = max_cic;
- auto test_ddc = make_block(std::move(ddc_make_args));
+ auto test_ddc = block_container.get_block<ddc_block_control>();
BOOST_REQUIRE(test_ddc);
BOOST_CHECK_EQUAL(test_ddc->get_block_args().get("foo"), "bar");
@@ -266,15 +226,13 @@ BOOST_AUTO_TEST_CASE(test_duc_block)
constexpr noc_id_t noc_id = DUC_BLOCK;
constexpr int TEST_INTERP = 20; // 2 halfbands, CIC==5
- auto duc_make_args = make_make_args(noc_id, "0/DUC#0", num_chans, num_chans);
- duc_make_args->args = uhd::device_addr_t();
- auto duc_reg_iface =
- std::dynamic_pointer_cast<mock_reg_iface_t>(duc_make_args->reg_iface);
+ auto block_container = get_mock_block(noc_id, num_chans, num_chans);
+ auto& duc_reg_iface = block_container.reg_iface;
duc_reg_iface->read_memory[duc_block_control::RB_COMPAT_NUM] =
(duc_block_control::MAJOR_COMPAT << 16) | duc_block_control::MINOR_COMPAT;
duc_reg_iface->read_memory[duc_block_control::RB_NUM_HB] = num_hb;
duc_reg_iface->read_memory[duc_block_control::RB_CIC_MAX_INTERP] = max_cic;
- auto test_duc = make_block(std::move(duc_make_args));
+ auto test_duc = block_container.get_block<duc_block_control>();
BOOST_REQUIRE(test_duc);
node_accessor.init_props(test_duc.get());