diff options
| author | Julian Blake Kongslie | 2022-10-15 14:03:28 -0700 |
|---|---|---|
| committer | Julian Blake Kongslie | 2022-10-15 14:03:28 -0700 |
| commit | eaef9348431ea331ecf118aefc21246dbcf2c998 (patch) | |
| tree | d12a2f3c8d86b47a12ef2ef78a02be552cf59475 /infra/queue.h | |
| parent | Initial commit (copied from biggolf) (diff) | |
| download | nanosim-eaef9348431ea331ecf118aefc21246dbcf2c998.tar.xz | |
Add memory implementation as well; reorg.
Diffstat (limited to 'infra/queue.h')
| -rw-r--r-- | infra/queue.h | 29 |
1 files changed, 29 insertions, 0 deletions
diff --git a/infra/queue.h b/infra/queue.h new file mode 100644 index 0000000..1e490bc --- /dev/null +++ b/infra/queue.h | |||
| @@ -0,0 +1,29 @@ | |||
| 1 | #pragma once | ||
| 2 | |||
| 3 | #include <cassert> | ||
| 4 | #include <deque> | ||
| 5 | #include <optional> | ||
| 6 | #include <utility> | ||
| 7 | |||
| 8 | #include "infra/port.h" | ||
| 9 | #include "infra/sim.h" | ||
| 10 | |||
| 11 | namespace infra { | ||
| 12 | template<typename T, unsigned int size> struct queue : public sim { | ||
| 13 | port<T> input; | ||
| 14 | port<T> *output = nullptr; | ||
| 15 | std::deque<T> elements; | ||
| 16 | |||
| 17 | void clock() { | ||
| 18 | if (input.can_read() && elements.size() < size) { | ||
| 19 | auto x = input.read(); | ||
| 20 | elements.emplace_back(std::move(x)); | ||
| 21 | } | ||
| 22 | if (output->can_write() && !elements.empty()) { | ||
| 23 | auto &x = elements.front(); | ||
| 24 | output->write(std::move(x)); | ||
| 25 | elements.pop_front(); | ||
| 26 | } | ||
| 27 | } | ||
| 28 | }; | ||
| 29 | } | ||
