pktfifo.cc revision 2186
110259SAndrew.Bardsley@arm.com/*
210259SAndrew.Bardsley@arm.com * Copyright (c) 2004-2005 The Regents of The University of Michigan
310259SAndrew.Bardsley@arm.com * All rights reserved.
410259SAndrew.Bardsley@arm.com *
510259SAndrew.Bardsley@arm.com * Redistribution and use in source and binary forms, with or without
610259SAndrew.Bardsley@arm.com * modification, are permitted provided that the following conditions are
710259SAndrew.Bardsley@arm.com * met: redistributions of source code must retain the above copyright
810259SAndrew.Bardsley@arm.com * notice, this list of conditions and the following disclaimer;
910259SAndrew.Bardsley@arm.com * redistributions in binary form must reproduce the above copyright
1010259SAndrew.Bardsley@arm.com * notice, this list of conditions and the following disclaimer in the
1110259SAndrew.Bardsley@arm.com * documentation and/or other materials provided with the distribution;
1210259SAndrew.Bardsley@arm.com * neither the name of the copyright holders nor the names of its
1310259SAndrew.Bardsley@arm.com * contributors may be used to endorse or promote products derived from
1410259SAndrew.Bardsley@arm.com * this software without specific prior written permission.
1510259SAndrew.Bardsley@arm.com *
1610259SAndrew.Bardsley@arm.com * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
1710259SAndrew.Bardsley@arm.com * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
1810259SAndrew.Bardsley@arm.com * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
1910259SAndrew.Bardsley@arm.com * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
2010259SAndrew.Bardsley@arm.com * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
2110259SAndrew.Bardsley@arm.com * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
2210259SAndrew.Bardsley@arm.com * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2310259SAndrew.Bardsley@arm.com * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2410259SAndrew.Bardsley@arm.com * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2510259SAndrew.Bardsley@arm.com * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
2610259SAndrew.Bardsley@arm.com * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2710259SAndrew.Bardsley@arm.com */
2810259SAndrew.Bardsley@arm.com
2910259SAndrew.Bardsley@arm.com#include "base/misc.hh"
3010259SAndrew.Bardsley@arm.com#include "dev/pktfifo.hh"
3110259SAndrew.Bardsley@arm.com
3210259SAndrew.Bardsley@arm.comusing namespace std;
3310259SAndrew.Bardsley@arm.com
3410259SAndrew.Bardsley@arm.combool
3510259SAndrew.Bardsley@arm.comPacketFifo::copyout(void *dest, int offset, int len)
3610259SAndrew.Bardsley@arm.com{
3710259SAndrew.Bardsley@arm.com    char *data = (char *)dest;
3810259SAndrew.Bardsley@arm.com    if (offset + len >= size())
3910259SAndrew.Bardsley@arm.com        return false;
4010259SAndrew.Bardsley@arm.com
4110259SAndrew.Bardsley@arm.com    list<PacketPtr>::iterator p = fifo.begin();
4210259SAndrew.Bardsley@arm.com    list<PacketPtr>::iterator end = fifo.end();
4310259SAndrew.Bardsley@arm.com    while (len > 0) {
4410259SAndrew.Bardsley@arm.com        while (offset >= (*p)->length) {
4510259SAndrew.Bardsley@arm.com            offset -= (*p)->length;
4610259SAndrew.Bardsley@arm.com            ++p;
4710259SAndrew.Bardsley@arm.com        }
4810259SAndrew.Bardsley@arm.com
4910259SAndrew.Bardsley@arm.com        if (p == end)
5010259SAndrew.Bardsley@arm.com            panic("invalid fifo");
5110259SAndrew.Bardsley@arm.com
5210259SAndrew.Bardsley@arm.com        int size = min((*p)->length - offset, len);
5310259SAndrew.Bardsley@arm.com        memcpy(data, (*p)->data, size);
5410259SAndrew.Bardsley@arm.com        offset = 0;
5510259SAndrew.Bardsley@arm.com        len -= size;
5610259SAndrew.Bardsley@arm.com        data += size;
5710259SAndrew.Bardsley@arm.com        ++p;
5810259SAndrew.Bardsley@arm.com    }
5910259SAndrew.Bardsley@arm.com
6010259SAndrew.Bardsley@arm.com    return true;
6110259SAndrew.Bardsley@arm.com}
6210259SAndrew.Bardsley@arm.com
6310259SAndrew.Bardsley@arm.com
6410259SAndrew.Bardsley@arm.comvoid
6510259SAndrew.Bardsley@arm.comPacketFifo::serialize(const string &base, ostream &os)
6610259SAndrew.Bardsley@arm.com{
6710259SAndrew.Bardsley@arm.com    paramOut(os, base + ".size", _size);
6810259SAndrew.Bardsley@arm.com    paramOut(os, base + ".maxsize", _maxsize);
6910259SAndrew.Bardsley@arm.com    paramOut(os, base + ".reserved", _reserved);
7010259SAndrew.Bardsley@arm.com    paramOut(os, base + ".packets", fifo.size());
7110259SAndrew.Bardsley@arm.com
7210259SAndrew.Bardsley@arm.com    int i = 0;
7310259SAndrew.Bardsley@arm.com    list<PacketPtr>::iterator p = fifo.begin();
7410259SAndrew.Bardsley@arm.com    list<PacketPtr>::iterator end = fifo.end();
7510259SAndrew.Bardsley@arm.com    while (p != end) {
7610259SAndrew.Bardsley@arm.com        (*p)->serialize(csprintf("%s.packet%d", base, i), os);
7710259SAndrew.Bardsley@arm.com        ++p;
7810259SAndrew.Bardsley@arm.com        ++i;
7910259SAndrew.Bardsley@arm.com    }
8010259SAndrew.Bardsley@arm.com}
8110259SAndrew.Bardsley@arm.com
8210259SAndrew.Bardsley@arm.comvoid
8310259SAndrew.Bardsley@arm.comPacketFifo::unserialize(const string &base, Checkpoint *cp,
8410259SAndrew.Bardsley@arm.com                        const string &section)
8510259SAndrew.Bardsley@arm.com{
8610259SAndrew.Bardsley@arm.com    paramIn(cp, section, base + ".size", _size);
8710259SAndrew.Bardsley@arm.com//  paramIn(cp, section, base + ".maxsize", _maxsize);
8810259SAndrew.Bardsley@arm.com    paramIn(cp, section, base + ".reserved", _reserved);
8911567Smitch.hayenga@arm.com    int fifosize;
9011567Smitch.hayenga@arm.com    paramIn(cp, section, base + ".packets", fifosize);
9111567Smitch.hayenga@arm.com
9211567Smitch.hayenga@arm.com    fifo.clear();
9310259SAndrew.Bardsley@arm.com
9410259SAndrew.Bardsley@arm.com    for (int i = 0; i < fifosize; ++i) {
9510259SAndrew.Bardsley@arm.com        PacketPtr p = new PacketData(16384);
9610259SAndrew.Bardsley@arm.com        p->unserialize(csprintf("%s.packet%d", base, i), cp, section);
9710259SAndrew.Bardsley@arm.com        fifo.push_back(p);
9810259SAndrew.Bardsley@arm.com    }
9910259SAndrew.Bardsley@arm.com}
10010259SAndrew.Bardsley@arm.com