diff options
| author | Julian Blake Kongslie | 2022-10-15 14:01:38 -0700 |
|---|---|---|
| committer | Julian Blake Kongslie | 2022-10-15 14:01:38 -0700 |
| commit | 6ddafafa7292f6f6716d6683579bf1ce3287e1d5 (patch) | |
| tree | 0b215f8e9ddf7010389ead062a8993fdb9142022 /arbiter.h | |
| download | nanosim-6ddafafa7292f6f6716d6683579bf1ce3287e1d5.tar.xz | |
Initial commit (copied from biggolf)
Diffstat (limited to '')
| -rw-r--r-- | arbiter.h | 55 |
1 files changed, 55 insertions, 0 deletions
diff --git a/arbiter.h b/arbiter.h new file mode 100644 index 0000000..79a9920 --- /dev/null +++ b/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 | } | ||
