Skip to content

Commit 2abb378

Browse files
committed
test: implement sgdma test
1 parent b5c1a8f commit 2abb378

1 file changed

Lines changed: 58 additions & 8 deletions

File tree

tests/test_UioMemSgdma.cpp

Lines changed: 58 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1+
#include <cstdint>
12
#include <memory>
23

4+
#include "udmaio/Logging.hpp"
35
#include "udmaio/UioConfig.hpp"
46
#define BOOST_TEST_MODULE UioMemSgdma
57

@@ -10,31 +12,79 @@
1012
#include "udmaio/UioMemSgdma.hpp"
1113
#include <boost/test/unit_test.hpp>
1214

13-
using namespace udmaio;
15+
namespace udmaio {
1416

15-
struct Fx {
17+
struct UioMemSgdmaTest : public Logger {
1618
HwAccessorPtr hw_sgdma;
1719
UioMemSgdma sgdma;
1820

1921
HwAccessorPtr hw_fpga_mem;
2022
FpgaMemBufferOverAxi fpga_mem;
2123

22-
Fx()
24+
UioMemSgdmaTest()
2325
: hw_sgdma{std::make_shared<udmaio::HwAccessorMock>(16 * 1024)}
2426
, sgdma{UioDeviceLocation{hw_sgdma}}
2527
, hw_fpga_mem{std::make_shared<HwAccessorMock>(16 * 1024)}
26-
, fpga_mem{UioDeviceLocation{hw_fpga_mem}} {}
27-
~Fx() {}
28+
, fpga_mem{UioDeviceLocation{hw_fpga_mem}}
29+
, descriptors{sgdma.descriptors}
30+
, Logger("UioMemSgdmaTest") {}
31+
~UioMemSgdmaTest() {}
32+
33+
RegAccessorArray<UioMemSgdma::S2mmDesc, 0, 1024, UioMemSgdma::DESC_ADDR_STEP>& descriptors;
2834

2935
static constexpr size_t num_bufs = 8, buf_size = 1024;
3036
};
3137

32-
BOOST_FIXTURE_TEST_CASE(init_buffers, Fx) {
38+
BOOST_FIXTURE_TEST_CASE(init_buffers, UioMemSgdmaTest) {
3339
sgdma.init_buffers(fpga_mem, num_bufs, buf_size);
3440
BOOST_CHECK_MESSAGE(sgdma.get_first_desc_addr() == hw_sgdma->get_phys_region().addr,
3541
"Descriptor address mismatch");
42+
}
43+
44+
void fill_buffer(HwAccessorPtr& mem, uintptr_t offs, size_t size) {
45+
assert(offs % sizeof(uint32_t) == 0);
46+
assert(size % sizeof(uint32_t) == 0);
47+
48+
while (offs < size) {
49+
mem->_wr32(offs, offs);
50+
offs += sizeof(uint32_t);
51+
}
52+
}
53+
54+
void check_buffer(std::vector<uint8_t> buf) {
55+
BOOST_CHECK_MESSAGE(buf.size() % sizeof(uint32_t) == 0, "Buffer size not 32bit aligned");
56+
for (size_t i = 0; i < buf.size(); i += sizeof(uint32_t)) {
57+
uint32_t val = *(reinterpret_cast<uint32_t*>(&buf[i]));
58+
BOOST_CHECK_MESSAGE(val == i,
59+
"Buffer contents mismatch at " << i << ", val = " << std::hex << val);
60+
}
61+
}
62+
63+
BOOST_FIXTURE_TEST_CASE(full_buffers, UioMemSgdmaTest) {
64+
sgdma.init_buffers(fpga_mem, num_bufs, buf_size);
65+
66+
auto empty_bufs = sgdma.get_full_buffers();
67+
BOOST_CHECK_MESSAGE(empty_bufs.size() == 0, "Empty buffers result not empty");
68+
69+
// Fill first buffer
70+
fill_buffer(hw_fpga_mem, 0, buf_size);
71+
72+
auto desc = descriptors[0].rd();
73+
desc.status.cmplt = true;
74+
desc.status.rxsof = true;
75+
desc.status.rxeof = true;
76+
desc.status.num_stored_bytes = buf_size;
77+
descriptors[0].wr(desc);
78+
79+
// sgdma.print_descs();
80+
81+
auto bufs = sgdma.get_full_buffers();
82+
BOOST_CHECK_MESSAGE(bufs.size() == 1, "Expected one full buffer, received " << bufs.size());
3683

37-
sgdma.print_descs();
84+
std::vector<uint8_t> data;
85+
fpga_mem.append_from_buf(bufs[0], data);
86+
BOOST_CHECK_MESSAGE(data.size() == buf_size, "Buffer size mismatch");
87+
check_buffer(data);
3888
}
3989

40-
BOOST_FIXTURE_TEST_CASE(foobar, Fx) {}
90+
} // namespace udmaio

0 commit comments

Comments
 (0)