timebuf.hh revision 2760
12SN/A/*
28707Sandreas.hansson@arm.com * Copyright (c) 2004-2005 The Regents of The University of Michigan
38707Sandreas.hansson@arm.com * All rights reserved.
48707Sandreas.hansson@arm.com *
58707Sandreas.hansson@arm.com * Redistribution and use in source and binary forms, with or without
68707Sandreas.hansson@arm.com * modification, are permitted provided that the following conditions are
78707Sandreas.hansson@arm.com * met: redistributions of source code must retain the above copyright
88707Sandreas.hansson@arm.com * notice, this list of conditions and the following disclaimer;
98707Sandreas.hansson@arm.com * redistributions in binary form must reproduce the above copyright
108707Sandreas.hansson@arm.com * notice, this list of conditions and the following disclaimer in the
118707Sandreas.hansson@arm.com * documentation and/or other materials provided with the distribution;
128707Sandreas.hansson@arm.com * neither the name of the copyright holders nor the names of its
138707Sandreas.hansson@arm.com * contributors may be used to endorse or promote products derived from
141762SN/A * this software without specific prior written permission.
157897Shestness@cs.utexas.edu *
162SN/A * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
172SN/A * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
182SN/A * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
192SN/A * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
202SN/A * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
212SN/A * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
222SN/A * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
232SN/A * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
242SN/A * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
252SN/A * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
262SN/A * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
272SN/A *
282SN/A * Authors: Nathan Binkert
292SN/A *          Kevin Lim
302SN/A */
312SN/A
322SN/A#ifndef __BASE_TIMEBUF_HH__
332SN/A#define __BASE_TIMEBUF_HH__
342SN/A
352SN/A#include <vector>
362SN/A
372SN/Atemplate <class T>
382SN/Aclass TimeBuffer
392SN/A{
402665Ssaidi@eecs.umich.edu  protected:
412665Ssaidi@eecs.umich.edu    int past;
422665Ssaidi@eecs.umich.edu    int future;
437897Shestness@cs.utexas.edu    int size;
442SN/A
452SN/A    char *data;
461717SN/A    std::vector<char *> index;
471717SN/A    int base;
482SN/A
492SN/A    void valid(int idx)
502SN/A    {
518745Sgblack@eecs.umich.edu        assert (idx >= -past && idx <= future);
524182Sgblack@eecs.umich.edu    }
535664Sgblack@eecs.umich.edu
54707SN/A  public:
556658Snate@binkert.org    friend class wire;
568229Snate@binkert.org    class wire
5756SN/A    {
588779Sgblack@eecs.umich.edu        friend class TimeBuffer;
594776Sgblack@eecs.umich.edu      protected:
602SN/A        TimeBuffer<T> *buffer;
618901Sandreas.hansson@arm.com        int index;
622190SN/A
632315SN/A        void set(int idx)
642680Sktlim@umich.edu        {
652SN/A            buffer->valid(idx);
662SN/A            index = idx;
672356SN/A        }
682356SN/A
692356SN/A        wire(TimeBuffer<T> *buf, int i)
706144Sksewell@umich.edu            : buffer(buf), index(i)
712356SN/A        { }
722356SN/A
736144Sksewell@umich.edu      public:
742356SN/A        wire()
752356SN/A        { }
766144Sksewell@umich.edu
772356SN/A        wire(const wire &i)
782356SN/A            : buffer(i.buffer), index(i.index)
792356SN/A        { }
806144Sksewell@umich.edu
816144Sksewell@umich.edu        const wire &operator=(const wire &i)
826144Sksewell@umich.edu        {
836144Sksewell@umich.edu            buffer = i.buffer;
846144Sksewell@umich.edu            set(i.index);
855336Shines@cs.fsu.edu            return *this;
862356SN/A        }
872356SN/A
882856Srdreslin@umich.edu        const wire &operator=(int idx)
892SN/A        {
901634SN/A            set(idx);
919157Sandreas.hansson@arm.com            return *this;
923814Ssaidi@eecs.umich.edu        }
933814Ssaidi@eecs.umich.edu
945712Shsul@eecs.umich.edu        const wire &operator+=(int offset)
955712Shsul@eecs.umich.edu        {
965715Shsul@eecs.umich.edu            set(index + offset);
975712Shsul@eecs.umich.edu            return *this;
985712Shsul@eecs.umich.edu        }
991634SN/A
1008832SAli.Saidi@ARM.com        const wire &operator-=(int offset)
1018832SAli.Saidi@ARM.com        {
1028832SAli.Saidi@ARM.com            set(index - offset);
1038832SAli.Saidi@ARM.com            return *this;
1048832SAli.Saidi@ARM.com        }
1058832SAli.Saidi@ARM.com
1068707Sandreas.hansson@arm.com        wire &operator++()
1078707Sandreas.hansson@arm.com        {
1088707Sandreas.hansson@arm.com            set(index + 1);
1098707Sandreas.hansson@arm.com            return *this;
1108707Sandreas.hansson@arm.com        }
1118707Sandreas.hansson@arm.com
1128707Sandreas.hansson@arm.com        wire &operator++(int)
1138707Sandreas.hansson@arm.com        {
1148922Swilliam.wang@arm.com            int i = index;
1158707Sandreas.hansson@arm.com            set(index + 1);
1168707Sandreas.hansson@arm.com            return wire(this, i);
1178707Sandreas.hansson@arm.com        }
1188707Sandreas.hansson@arm.com
1198707Sandreas.hansson@arm.com        wire &operator--()
1208707Sandreas.hansson@arm.com        {
1218707Sandreas.hansson@arm.com            set(index - 1);
1228707Sandreas.hansson@arm.com            return *this;
1238707Sandreas.hansson@arm.com        }
1248707Sandreas.hansson@arm.com
1258922Swilliam.wang@arm.com        wire &operator--(int)
1268707Sandreas.hansson@arm.com        {
1278707Sandreas.hansson@arm.com            int i = index;
1288707Sandreas.hansson@arm.com            set(index - 1);
1298707Sandreas.hansson@arm.com            return wire(this, i);
1308975Sandreas.hansson@arm.com        }
1318707Sandreas.hansson@arm.com        T &operator*() const { return *buffer->access(index); }
1328707Sandreas.hansson@arm.com        T *operator->() const { return buffer->access(index); }
1338707Sandreas.hansson@arm.com    };
1348948Sandreas.hansson@arm.com
1358707Sandreas.hansson@arm.com
1368707Sandreas.hansson@arm.com  public:
1378707Sandreas.hansson@arm.com    TimeBuffer(int p, int f)
1381634SN/A        : past(p), future(f), size(past + future + 1),
1398850Sandreas.hansson@arm.com          data(new char[size * sizeof(T)]), index(size), base(0)
1408850Sandreas.hansson@arm.com    {
1418850Sandreas.hansson@arm.com        assert(past >= 0 && future >= 0);
1428850Sandreas.hansson@arm.com        char *ptr = data;
1438850Sandreas.hansson@arm.com        for (int i = 0; i < size; i++) {
1448850Sandreas.hansson@arm.com            index[i] = ptr;
1458850Sandreas.hansson@arm.com            memset(ptr, 0, sizeof(T));
1468850Sandreas.hansson@arm.com            new (ptr) T;
1478850Sandreas.hansson@arm.com            ptr += sizeof(T);
1488850Sandreas.hansson@arm.com        }
1498850Sandreas.hansson@arm.com    }
1508850Sandreas.hansson@arm.com
1518850Sandreas.hansson@arm.com    TimeBuffer()
1528850Sandreas.hansson@arm.com        : data(NULL)
1538850Sandreas.hansson@arm.com    {
1548850Sandreas.hansson@arm.com    }
1558850Sandreas.hansson@arm.com
1565712Shsul@eecs.umich.edu    ~TimeBuffer()
1575712Shsul@eecs.umich.edu    {
1585712Shsul@eecs.umich.edu        for (int i = 0; i < size; ++i)
1598832SAli.Saidi@ARM.com            (reinterpret_cast<T *>(index[i]))->~T();
1608832SAli.Saidi@ARM.com        delete [] data;
1618832SAli.Saidi@ARM.com    }
1628832SAli.Saidi@ARM.com
1638832SAli.Saidi@ARM.com    void
1648850Sandreas.hansson@arm.com    advance()
1658926Sandreas.hansson@arm.com    {
1668926Sandreas.hansson@arm.com        if (++base >= size)
1678926Sandreas.hansson@arm.com            base = 0;
1688850Sandreas.hansson@arm.com
1698850Sandreas.hansson@arm.com        int ptr = base + future;
1708850Sandreas.hansson@arm.com        if (ptr >= size)
1718850Sandreas.hansson@arm.com            ptr -= size;
1728922Swilliam.wang@arm.com        (reinterpret_cast<T *>(index[ptr]))->~T();
1738850Sandreas.hansson@arm.com        memset(index[ptr], 0, sizeof(T));
1749294Sandreas.hansson@arm.com        new (index[ptr]) T;
1759294Sandreas.hansson@arm.com    }
1768850Sandreas.hansson@arm.com
1777914SBrad.Beckmann@amd.com    T *access(int idx)
1787914SBrad.Beckmann@amd.com    {
1793814Ssaidi@eecs.umich.edu        //Need more complex math here to calculate index.
1803814Ssaidi@eecs.umich.edu        valid(idx);
1811634SN/A
1825664Sgblack@eecs.umich.edu        int vector_index = idx + base;
1835664Sgblack@eecs.umich.edu        if (vector_index >= size) {
1842SN/A            vector_index -= size;
1855704Snate@binkert.org        } else if (vector_index < 0) {
1862SN/A            vector_index += size;
1872SN/A        }
1885645Sgblack@eecs.umich.edu
1895645Sgblack@eecs.umich.edu        return reinterpret_cast<T *>(index[vector_index]);
1905645Sgblack@eecs.umich.edu    }
1915647Sgblack@eecs.umich.edu
1925645Sgblack@eecs.umich.edu    T &operator[](int idx)
1935645Sgblack@eecs.umich.edu    {
1945807Snate@binkert.org        //Need more complex math here to calculate index.
1955807Snate@binkert.org        valid(idx);
1965807Snate@binkert.org
1975807Snate@binkert.org        int vector_index = idx + base;
1985807Snate@binkert.org        if (vector_index >= size) {
1995807Snate@binkert.org            vector_index -= size;
2008779Sgblack@eecs.umich.edu        } else if (vector_index < 0) {
2018779Sgblack@eecs.umich.edu            vector_index += size;
2025807Snate@binkert.org        }
2035807Snate@binkert.org
2045807Snate@binkert.org        return reinterpret_cast<T &>(*index[vector_index]);
2055807Snate@binkert.org    }
2065807Snate@binkert.org
2075807Snate@binkert.org    wire getWire(int idx)
2085807Snate@binkert.org    {
2095807Snate@binkert.org        valid(idx);
2105807Snate@binkert.org
2115807Snate@binkert.org        return wire(this, idx);
2125807Snate@binkert.org    }
2135807Snate@binkert.org
2145807Snate@binkert.org    wire zero()
2152SN/A    {
2165704Snate@binkert.org        return wire(this, 0);
2175704Snate@binkert.org    }
2185704Snate@binkert.org};
2198793Sgblack@eecs.umich.edu
2205704Snate@binkert.org#endif // __BASE_TIMEBUF_HH__
2211917SN/A
2221917SN/A