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/arbiter.h | |
| parent | Initial commit (copied from biggolf) (diff) | |
| download | nanosim-eaef9348431ea331ecf118aefc21246dbcf2c998.tar.xz | |
Add memory implementation as well; reorg.
Diffstat (limited to 'infra/arbiter.h')
| -rw-r--r-- | infra/arbiter.h | 55 |
1 files changed, 55 insertions, 0 deletions
diff --git a/infra/arbiter.h b/infra/arbiter.h new file mode 100644 index 0000000..79a9920 --- /dev/null +++ b/infra/arbiter.h | |||
| @@ -0,0 +1,55 @@ | |||
| 1 | #pragma once | ||
| 2 | |||
| 3 | #include <array> | ||
| 4 | #include <cassert> | ||
| 5 | |||
| 6 | #include "infra/sim.h" | ||
| 7 | |||
| 8 | namespace infra { | ||
| 9 | template<typename T, unsigned int peers> struct priority_arbiter : public sim { | ||
| 10 | std::array<port<T>, peers> peerp; | ||
| 11 | port<T> *outp = nullptr; | ||
| 12 | |||
| 13 | void clock() { | ||
| 14 | for (unsigned int i = 0; i < peers; ++i) { | ||
| 15 | if (outp->can_write() && peerp[i].can_read()) | ||
| 16 | outp->write(peerp[i].read()); | ||
| 17 | } | ||
| 18 | } | ||
| 19 | }; | ||
| 20 | |||
| 21 | template<typename T, unsigned int peers> struct round_robin_arbiter : public sim { | ||
| 22 | std::array<port<T>, peers> peerp; | ||
| 23 | port<T> *outp = nullptr; | ||
| 24 | unsigned int initial = 0; | ||
| 25 | |||
| 26 | void clock() { | ||
| 27 | bool initially_empty = outp->can_write(); | ||
| 28 | for (unsigned int i = initial; i < peers; ++i) { | ||
| 29 | if (outp->can_write() && peerp[i].can_read()) | ||
| 30 | outp->write(peerp[i].read()); | ||
| 31 | } | ||
| 32 | for (unsigned int i = 0; i < initial; ++i) { | ||
| 33 | if (outp->can_write() && peerp[i].can_read()) | ||
| 34 | outp->write(peerp[i].read()); | ||
| 35 | } | ||
| 36 | if (initially_empty && !outp->can_write()) | ||
| 37 | if (++initial == peers) | ||
| 38 | initial = 0; | ||
| 39 | } | ||
| 40 | }; | ||
| 41 | |||
| 42 | template<typename T, unsigned int peers> struct shared_bus : public sim { | ||
| 43 | std::array<port<T>, peers> peerp; | ||
| 44 | port<T> *outp = nullptr; | ||
| 45 | |||
| 46 | void clock() { | ||
| 47 | for (unsigned int i = 0; i < peers; ++i) { | ||
| 48 | if (peerp[i].can_read()) { | ||
| 49 | assert(outp->can_write()); | ||
| 50 | outp->write(peerp[i].read()); | ||
| 51 | } | ||
| 52 | } | ||
| 53 | } | ||
| 54 | }; | ||
| 55 | } | ||
