blob: e12d809dd02cbeb4c469d806fccc9f131451b87a [file] [log] [blame]
Derek Chowc7687712023-08-10 13:03:15 -07001// Copyright 2023 Google LLC
Cindy Liu0959e532023-10-19 11:45:06 -07002//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
Derek Chow36ad2bd2023-08-01 11:10:05 -070015#ifndef TESTS_VERILATOR_SIM_FIFO_H_
16#define TESTS_VERILATOR_SIM_FIFO_H_
17
Cindy Liu128b2db2023-10-19 12:04:53 -070018#include <vector>
19
Derek Chow36ad2bd2023-08-01 11:10:05 -070020// A SystemC CRT transaction queue.
21
22template <typename T>
23class fifo_t {
24 public:
25 bool empty() { return entries_.empty(); }
26
27 void write(T v) { entries_.emplace_back(v); }
28
29 bool read(T& v) {
30 if (entries_.empty()) return false;
31 v = entries_.at(0);
32 entries_.erase(entries_.begin());
33 return true;
34 }
35
36 bool next(T& v, int index = 0) {
37 if (index >= count()) return false;
38 v = entries_.at(index);
39 return true;
40 }
41
42 bool rand(T& v) {
43 if (entries_.empty()) return false;
44 int index = ::rand() % count();
45 v = entries_.at(index);
46 return true;
47 }
48
49 void clear() { entries_.clear(); }
50
51 bool remove(int index = 0) {
52 if (index >= count()) return false;
53 entries_.erase(entries_.begin() + index);
54 return true;
55 }
56
57 void shuffle() {
58 const int count = entries_.size();
59 if (count < 2) return;
60 for (int i = 0; i < count; ++i) {
61 const int index = ::rand() % count;
62 T v = entries_.at(index);
63 entries_.erase(entries_.begin() + index);
64 entries_.emplace_back(v);
65 }
66 }
67
68 int count() { return entries_.size(); }
69
70 private:
71 std::vector<T> entries_;
72};
73
74#endif // TESTS_VERILATOR_SIM_FIFO_H_