[259] | 1 | /* -*- c++ -*- |
---|
| 2 | * |
---|
| 3 | * SOCLIB_LGPL_HEADER_BEGIN |
---|
| 4 | * |
---|
| 5 | * This file is part of SoCLib, GNU LGPLv2.1. |
---|
| 6 | * |
---|
| 7 | * SoCLib is free software; you can redistribute it and/or modify it |
---|
| 8 | * under the terms of the GNU Lesser General Public License as published |
---|
| 9 | * by the Free Software Foundation; version 2.1 of the License. |
---|
| 10 | * |
---|
| 11 | * SoCLib is distributed in the hope that it will be useful, but |
---|
| 12 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
---|
| 14 | * Lesser General Public License for more details. |
---|
| 15 | * |
---|
| 16 | * You should have received a copy of the GNU Lesser General Public |
---|
| 17 | * License along with SoCLib; if not, write to the Free Software |
---|
| 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
---|
| 19 | * 02110-1301 USA |
---|
| 20 | * |
---|
| 21 | * SOCLIB_LGPL_HEADER_END |
---|
| 22 | * |
---|
| 23 | * Copyright (c) UPMC, Lip6, Asim |
---|
| 24 | * Nicolas Pouillon <nipo@ssji.net>, 2007 |
---|
| 25 | * Alain Greiner <alain.greiner@lip6.fr>, 2003 |
---|
| 26 | * |
---|
| 27 | * Maintainers: nipo |
---|
| 28 | */ |
---|
| 29 | #ifndef SOCLIB_CABA_GENERIC_FIFO_H |
---|
| 30 | #define SOCLIB_CABA_GENERIC_FIFO_H |
---|
| 31 | |
---|
| 32 | #include <systemc> |
---|
| 33 | #include "register.h" |
---|
| 34 | |
---|
| 35 | namespace soclib { namespace caba { |
---|
| 36 | |
---|
| 37 | using namespace sc_core; |
---|
| 38 | |
---|
| 39 | template<typename T> |
---|
| 40 | class GenericFifo |
---|
| 41 | { |
---|
| 42 | T *m_data; |
---|
| 43 | sc_signal<int> r_ptr; |
---|
| 44 | sc_signal<int> r_ptw; |
---|
| 45 | sc_signal<int> r_fill_state; |
---|
| 46 | int m_depth; |
---|
| 47 | public: |
---|
| 48 | typedef T data_t; |
---|
| 49 | |
---|
| 50 | void print (void) const |
---|
| 51 | { |
---|
| 52 | std::cout << "r_fill_state : " << r_fill_state << std::endl; |
---|
| 53 | std::cout << "r_ptr/ptw/max : " << r_ptr << " - " << r_ptw << " - " << m_depth << std::endl; |
---|
| 54 | std::cout << "m_data : " << std::hex; |
---|
| 55 | int i=0; |
---|
| 56 | while (i<r_fill_state) |
---|
| 57 | { |
---|
| 58 | std::cout << m_data[(r_ptr+i)%m_depth] <<", "; |
---|
| 59 | i++; |
---|
| 60 | } |
---|
| 61 | std::cout << std::dec << "end" << std::endl; |
---|
| 62 | } |
---|
| 63 | |
---|
| 64 | size_t size() const |
---|
| 65 | { |
---|
| 66 | return m_depth; |
---|
| 67 | } |
---|
| 68 | |
---|
| 69 | void init() |
---|
| 70 | { |
---|
| 71 | r_ptr = 0; |
---|
| 72 | r_ptw = 0; |
---|
| 73 | r_fill_state = 0; |
---|
| 74 | } |
---|
| 75 | |
---|
| 76 | inline uint32_t filled_status() const |
---|
| 77 | { |
---|
| 78 | return (uint32_t)r_fill_state; |
---|
| 79 | } |
---|
| 80 | |
---|
| 81 | inline bool empty() const |
---|
| 82 | { |
---|
| 83 | return !(uint32_t)r_fill_state; |
---|
| 84 | } |
---|
| 85 | |
---|
| 86 | inline bool full() const |
---|
| 87 | { |
---|
| 88 | return r_fill_state == m_depth; |
---|
| 89 | } |
---|
| 90 | |
---|
| 91 | void simple_put(const T &din) |
---|
| 92 | { |
---|
| 93 | if (r_fill_state != m_depth) { |
---|
| 94 | r_fill_state = r_fill_state + 1; |
---|
| 95 | r_ptw = (r_ptw + 1) % m_depth; |
---|
| 96 | m_data[r_ptw] = din; |
---|
| 97 | } |
---|
| 98 | } |
---|
| 99 | |
---|
| 100 | void simple_get() |
---|
| 101 | { |
---|
| 102 | if (r_fill_state != 0) { |
---|
| 103 | r_fill_state = r_fill_state - 1; |
---|
| 104 | r_ptr = (r_ptr + 1) % m_depth; |
---|
| 105 | } |
---|
| 106 | } |
---|
| 107 | |
---|
| 108 | void put_and_get(const T &din) |
---|
| 109 | { |
---|
| 110 | if (r_fill_state == m_depth) { |
---|
| 111 | r_fill_state = r_fill_state - 1; |
---|
| 112 | r_ptr = (r_ptr + 1) % m_depth; |
---|
| 113 | } else if (r_fill_state == 0) { |
---|
| 114 | r_fill_state = r_fill_state + 1; |
---|
| 115 | r_ptw = (r_ptw + 1) % m_depth; |
---|
| 116 | m_data[r_ptw] = din; |
---|
| 117 | } else { |
---|
| 118 | r_ptr = (r_ptr + 1) % m_depth; |
---|
| 119 | r_ptw = (r_ptw + 1) % m_depth; |
---|
| 120 | m_data[r_ptw] = din; |
---|
| 121 | } |
---|
| 122 | } |
---|
| 123 | |
---|
| 124 | void update (bool get, bool put, const T& din) |
---|
| 125 | { |
---|
| 126 | if (put and get) |
---|
| 127 | put_and_get(din); |
---|
| 128 | else |
---|
| 129 | { |
---|
| 130 | if (put) |
---|
| 131 | simple_put(din); |
---|
| 132 | if (get) |
---|
| 133 | simple_get(); |
---|
| 134 | } |
---|
| 135 | } |
---|
| 136 | |
---|
| 137 | inline bool rok() const |
---|
| 138 | { |
---|
| 139 | return (r_fill_state != 0); |
---|
| 140 | } |
---|
| 141 | |
---|
| 142 | inline bool wok() const |
---|
| 143 | { |
---|
| 144 | return (r_fill_state != m_depth); |
---|
| 145 | } |
---|
| 146 | |
---|
| 147 | inline const T &read() const |
---|
| 148 | { |
---|
| 149 | return m_data[r_ptr]; |
---|
| 150 | } |
---|
| 151 | |
---|
| 152 | GenericFifo(const std::string &name, size_t depth) |
---|
| 153 | : m_data(new T[depth]), |
---|
| 154 | r_ptr((name+"_r_ptr").c_str()), |
---|
| 155 | r_ptw((name+"_r_ptw").c_str()), |
---|
| 156 | r_fill_state((name+"_r_fill_state").c_str()), |
---|
| 157 | m_depth(depth) |
---|
| 158 | { |
---|
| 159 | } |
---|
| 160 | |
---|
| 161 | ~GenericFifo() |
---|
| 162 | { |
---|
| 163 | delete [] m_data; |
---|
| 164 | } |
---|
| 165 | }; |
---|
| 166 | |
---|
| 167 | }} |
---|
| 168 | |
---|
| 169 | #endif /* SOCLIB_CABA_GENERIC_FIFO_H */ |
---|
| 170 | |
---|
| 171 | // Local Variables: |
---|
| 172 | // tab-width: 4 |
---|
| 173 | // c-basic-offset: 4 |
---|
| 174 | // c-file-offsets:((innamespace . 0)(inline-open . 0)) |
---|
| 175 | // indent-tabs-mode: nil |
---|
| 176 | // End: |
---|
| 177 | |
---|
| 178 | // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=4:softtabstop=4 |
---|
| 179 | |
---|