forked from altera-fpga/hls-samples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrom_base.hpp
More file actions
72 lines (61 loc) · 2.13 KB
/
rom_base.hpp
File metadata and controls
72 lines (61 loc) · 2.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#ifndef __ROM_BASE_HPP__
#define __ROM_BASE_HPP__
#include <type_traits>
//
// A base class for creating a constexpr ROM.
//
// TEMPLATE PARAMETERS
// T: the datatype stored in the ROM
// _depth: the depth of the ROM
//
// EXAMPLE USAGE
// To use the ROM, you must create a class that inherits from this class and
// provides a constexpr functor to the constructor which determines how the
// ROM is initialized. The following examples show two methods for creating
// a ROM that stores x^2, where 'x' is the index into the ROM.
//
// USING A FUNCTOR
// struct SquareFunctor {
// constexpr float operator () (int x) const { return x * x }
// constexpr SquareFunctor() = default;
// };
//
// constexpr int lut_depth = 1024;
// struct SquareLUT : ROMBase<int, lut_depth> {
// constexpr SquareLUT() : ROMBase<int, lut_depth>(SquareFunctor()) {}
// };
//
// USING A LAMBDA
// constexpr int lut_depth = 1024;
// struct SquareLUT : ROMBase<int, lut_depth> {
// constexpr SquareLUT() : ROMBase<int, lut_depth>(
// [](int x) { return x * x; }) {}
// };
//
namespace fpga_tools {
template<typename T, int rom_depth>
struct ROMBase {
// ensure a positive depth
static_assert(rom_depth > 0);
// allows the depth of the ROM to be queried
static constexpr int depth = rom_depth;
// allows the type stored in the ROM to be queried
using ValType = T;
// constexpr constructor that initializes the contents of the ROM
// using a user specified Functor. NOTE: the functor must be constexpr,
// which can be achieved with a lambda or by marking the operator() function
// as constexpr.
template<typename InitFunctor>
constexpr ROMBase(const InitFunctor& func) : data_() {
static_assert(std::is_invocable_r_v<T, InitFunctor, int>);
for (int i = 0; i < rom_depth; i++) {
data_[i] = func(i);
}
}
// only define a const operator[], since this is a ROM
const T& operator[](int i) const { return data_[i]; }
protected:
T data_[rom_depth];
};
} // namespace fpga_tools
#endif /* __ROM_BASE_HPP__ */