|
1 | | -#ifndef TEST_CUSTOMIZATIONS_HPP |
2 | | -#define TEST_CUSTOMIZATIONS_HPP |
| 1 | +#ifndef TEST_HELPERS_HPP |
| 2 | +#define TEST_HELPERS_HPP |
3 | 3 |
|
4 | | -#include <NyuCatch2TestUtil.hpp> |
| 4 | +#include <cstdint> |
5 | 5 |
|
6 | | -template <typename T, typename R = int> |
7 | | -concept has_en_assign_from = requires(T t, R r) { t.en = r; }; |
| 6 | +#include <NyuTestUtil.hpp> |
8 | 7 |
|
9 | | -template <typename T, typename R = int> |
10 | | -concept nothrow_has_en_assign_from = requires(T t, R r) { |
11 | | - { t.en = r } noexcept; |
12 | | -}; |
| 8 | +consteval unsigned flog2(unsigned x) { |
| 9 | + return x == 1 ? 0 : flog2(x >> 1) + 1; |
| 10 | +} |
13 | 11 |
|
14 | | -template <typename Dut, typename R = int> |
15 | | -requires has_en_assign_from<Dut, R> && nyu::reset_default_ok<Dut> |
16 | | -void tag_invoke(nyu::reset_t, Dut& dut) noexcept( |
17 | | - nothrow_has_en_assign_from<Dut, R> && nyu::nothrow_reset_default_ok<Dut>) { |
18 | | - dut.en = R {1}; |
19 | | - nyu::reset_default(dut); |
| 12 | +consteval unsigned clog2(unsigned x) { |
| 13 | + return x == 1 ? 0 : flog2(x - 1) + 1; |
20 | 14 | } |
21 | 15 |
|
22 | | -template <typename T, typename R = bool> |
23 | | -concept has_phase_assign_from = |
24 | | - requires(T t, R r) { t.phase = static_cast<bool>(r); }; |
| 16 | +inline constexpr unsigned MaxClockRate = 100 * 1000000; |
| 17 | +inline constexpr unsigned MinBaudRate = 9600; |
| 18 | +inline constexpr unsigned Oversample = 16; |
25 | 19 |
|
26 | | -template <typename T, typename R = bool> |
27 | | -concept nothrow_has_phase_assign_from = requires(T t, R r) { |
28 | | - { t.phase = static_cast<bool>(r) } noexcept; |
29 | | -}; |
| 20 | +inline constexpr unsigned rxRate = MaxClockRate / (MinBaudRate * Oversample); |
| 21 | +inline constexpr unsigned txRate = MaxClockRate / MinBaudRate; |
30 | 22 |
|
31 | | -template <typename T, typename R = int> |
32 | | -concept has_rate_assign_from = requires(T t, R r) { t.rate = r; }; |
| 23 | +inline constexpr unsigned txWidth = clog2(txRate); |
| 24 | +inline constexpr unsigned rxShift = clog2(Oversample); |
| 25 | +inline constexpr unsigned rxWidth = txWidth - rxShift; |
33 | 26 |
|
34 | | -template <typename T, typename R = int> |
35 | | -concept nothrow_has_rate_assign_from = requires(T t, R r) { |
36 | | - { t.rate = r } noexcept; |
37 | | -}; |
38 | 27 |
|
39 | | -template <typename T, typename R = int> |
40 | | -concept has_syncReset_assign_from = requires(T t, R r) { t.syncReset = r; }; |
| 28 | +NYU_META_ASSIGNABLE_CONCEPTS(has_en_assign_from, en) |
| 29 | +NYU_META_ASSIGNABLE_CONCEPTS(has_in_assign_from, in) |
| 30 | +NYU_META_ASSIGNABLE_CONCEPTS(has_phase_assign_from, phase) |
| 31 | +NYU_META_ASSIGNABLE_CONCEPTS(has_rate_assign_from, rate) |
| 32 | +NYU_META_ASSIGNABLE_CONCEPTS(has_syncreset_assign_from, syncReset) |
| 33 | +NYU_META_ASSIGNABLE_CONCEPTS(has_data_assign_from, data) |
| 34 | +NYU_META_ASSIGNABLE_CONCEPTS(has_valid_assign_from, valid) |
41 | 35 |
|
42 | | -template <typename T, typename R = int> |
43 | | -concept nothrow_has_syncReset_assign_from = requires(T t, R r) { |
44 | | - { t.syncReset = r } noexcept; |
45 | | -}; |
46 | 36 |
|
47 | | -template <typename Dut, typename Phase = bool, typename Rate = int, |
48 | | - typename SyncReset = int> |
| 37 | +template <typename Dut, typename R = std::uint8_t> |
| 38 | +requires has_en_assign_from<Dut, R> && nyu::reset_default_ok<Dut> |
| 39 | +void tag_invoke(nyu::reset_t, Dut& dut) noexcept( |
| 40 | + nothrow_has_en_assign_from<Dut, R> && nyu::nothrow_reset_default_ok<Dut>) { |
| 41 | + dut.en = R {1}; |
| 42 | + nyu::reset_default(dut); |
| 43 | +} |
| 44 | + |
| 45 | + |
| 46 | +template <typename Dut, typename Phase, typename Rate, typename SyncReset> |
49 | 47 | concept can_phase_reset = |
50 | 48 | has_phase_assign_from<Dut, Phase> && has_rate_assign_from<Dut, Rate> && |
51 | | - has_syncReset_assign_from<Dut, SyncReset>; |
| 49 | + has_syncreset_assign_from<Dut, SyncReset>; |
52 | 50 |
|
53 | | -template <typename Dut, typename Phase = bool, typename Rate = int, |
54 | | - typename SyncReset = int> |
| 51 | +template <typename Dut, typename Phase, typename Rate, typename SyncReset> |
55 | 52 | concept nothrow_can_phase_reset = nothrow_has_phase_assign_from<Dut, Phase> && |
56 | 53 | nothrow_has_rate_assign_from<Dut, Rate> && |
57 | | - nothrow_has_syncReset_assign_from<Dut, SyncReset>; |
58 | | - |
59 | | -template <typename T, typename R = int> |
60 | | -concept has_data_assign_from = requires(T t, R r) { t.data = r; }; |
61 | | - |
62 | | -template <typename T, typename R = int> |
63 | | -concept nothrow_has_data_assign_from = requires(T t, R r) { |
64 | | - { t.data = r } noexcept; |
65 | | -}; |
66 | | - |
67 | | -template <typename T, typename R = int> |
68 | | -concept has_valid_assign_from = requires(T t, R r) { t.valid = r; }; |
| 54 | + nothrow_has_syncreset_assign_from<Dut, SyncReset>; |
| 55 | + |
| 56 | +template <typename Dut, typename Phase = int, typename Rate = decltype(txRate), |
| 57 | + typename SyncReset = std::uint8_t> |
| 58 | +requires can_phase_reset<Dut, Phase, Rate, SyncReset> && |
| 59 | + nyu::reset_default_ok<Dut> |
| 60 | +void tag_invoke(nyu::reset_t, Dut& dut, Phase phase = 0, |
| 61 | + Rate rate = |
| 62 | + txRate) noexcept(nothrow_can_phase_reset<Dut, Phase, Rate, SyncReset> && |
| 63 | + nyu::nothrow_reset_default_ok<Dut>) { |
| 64 | + dut.phase = static_cast<bool>(phase); |
| 65 | + dut.rate = rate; |
| 66 | + dut.syncReset = SyncReset {0}; |
| 67 | + nyu::reset_default(dut); |
| 68 | +} |
69 | 69 |
|
70 | | -template <typename T, typename R = int> |
71 | | -concept nothrow_has_valid_assign_from = requires(T t, R r) { |
72 | | - { t.valid = r } noexcept; |
73 | | -}; |
74 | 70 |
|
75 | | -template <typename Dut, typename Data = int, typename Valid = int> |
| 71 | +template <typename Dut, typename Data, typename Valid> |
76 | 72 | concept can_send = has_data_assign_from<Dut, Data> && |
77 | 73 | has_valid_assign_from<Dut, Data> && nyu::can_call_tick<Dut>; |
78 | 74 |
|
79 | | -template <typename Dut, typename Data = int, typename Valid = int> |
| 75 | +template <typename Dut, typename Data, typename Valid> |
80 | 76 | concept nothrow_can_send = nothrow_has_data_assign_from<Dut, Data> && |
81 | 77 | nothrow_has_valid_assign_from<Dut, Data> && nyu::nothrow_can_call_tick<Dut>; |
82 | 78 |
|
83 | | -template <typename Dut, typename Data, typename Valid = int> |
| 79 | +template <typename Dut, typename Data, typename Valid = std::uint8_t> |
84 | 80 | requires can_send<Dut, Data, Valid> |
85 | | -void send(Dut& tx, Data val) noexcept(nothrow_can_send<Dut, Data, Valid>) { |
86 | | - tx.data = val; |
87 | | - tx.valid = Valid {1}; |
88 | | - nyu::tick(tx); |
89 | | - tx.valid = Valid {0}; |
| 81 | +void send(Dut& dut, Data val) noexcept(nothrow_can_send<Dut, Data, Valid>) { |
| 82 | + dut.data = val; |
| 83 | + dut.valid = Valid {1}; |
| 84 | + nyu::tick(dut); |
| 85 | + dut.valid = Valid {0}; |
| 86 | +} |
| 87 | + |
| 88 | + |
| 89 | +template <typename Dut, typename In> |
| 90 | +concept can_start_transmit = |
| 91 | + has_in_assign_from<Dut, In> && nyu::can_call_tick<Dut>; |
| 92 | + |
| 93 | +template <typename Dut, typename In> |
| 94 | +concept nothrow_can_start_transmit = |
| 95 | + nothrow_has_in_assign_from<Dut, In> && nyu::nothrow_can_call_tick<Dut>; |
| 96 | + |
| 97 | +template <typename Dut, typename In = std::uint8_t> |
| 98 | +requires can_start_transmit<Dut, In> |
| 99 | +void start(Dut& dut, unsigned ticks = Oversample) noexcept( |
| 100 | + nothrow_can_start_transmit<Dut, In>) { |
| 101 | + dut.in = In {1}; |
| 102 | + nyu::tick(dut); |
| 103 | + dut.in = In {0}; |
| 104 | + nyu::tick(dut, ticks); |
| 105 | +} |
| 106 | + |
| 107 | +template <typename Dut, typename In = std::uint8_t> |
| 108 | +requires can_start_transmit<Dut, In> |
| 109 | +void transmit(Dut& dut, std::uint8_t val, unsigned ticks = Oversample) noexcept( |
| 110 | + nothrow_can_start_transmit<Dut, In>) { |
| 111 | + for(unsigned i {0}; i < 8; ++i) { |
| 112 | + dut.in = static_cast<In>(val & 0x1); |
| 113 | + nyu::tick(dut, ticks); |
| 114 | + val >>= 1; |
| 115 | + } |
90 | 116 | } |
91 | 117 |
|
92 | | -#endif // TEST_CUSTOMIZATIONS_HPP |
| 118 | +template <typename Dut, typename In = std::uint8_t> |
| 119 | +requires can_start_transmit<Dut, In> |
| 120 | +void start_transmit(Dut& dut, std::uint8_t val) { |
| 121 | + start<Dut, In>(dut); |
| 122 | + transmit<Dut, In>(dut, val); |
| 123 | +} |
| 124 | + |
| 125 | + |
| 126 | +#endif // TEST_HELPERS_HPP |
0 commit comments