Merge zizzer:/bk/newmem
[gem5.git] / src / dev / etherbus.cc
1 /*
2 * Copyright (c) 2002-2005 The Regents of The University of Michigan
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
7 * met: redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer;
9 * redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution;
12 * neither the name of the copyright holders nor the names of its
13 * contributors may be used to endorse or promote products derived from
14 * this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 *
28 * Authors: Nathan Binkert
29 */
30
31 /* @file
32 * Device module for modelling an ethernet hub
33 */
34
35 #include <cmath>
36 #include <deque>
37 #include <string>
38 #include <vector>
39
40 #include "base/trace.hh"
41 #include "dev/etherbus.hh"
42 #include "dev/etherdump.hh"
43 #include "dev/etherint.hh"
44 #include "dev/etherpkt.hh"
45 #include "sim/builder.hh"
46 #include "sim/core.hh"
47
48 using namespace std;
49
50 EtherBus::EtherBus(const string &name, double speed, bool loop,
51 EtherDump *packet_dump)
52 : SimObject(name), ticksPerByte(speed), loopback(loop),
53 event(&mainEventQueue, this), sender(0), dump(packet_dump)
54 {
55 }
56
57 void
58 EtherBus::txDone()
59 {
60 devlist_t::iterator i = devlist.begin();
61 devlist_t::iterator end = devlist.end();
62
63 DPRINTF(Ethernet, "ethernet packet received: length=%d\n", packet->length);
64 DDUMP(EthernetData, packet->data, packet->length);
65
66 while (i != end) {
67 if (loopback || *i != sender)
68 (*i)->sendPacket(packet);
69 ++i;
70 }
71
72 sender->sendDone();
73
74 if (dump)
75 dump->dump(packet);
76
77 sender = 0;
78 packet = 0;
79 }
80
81 void
82 EtherBus::reg(EtherInt *dev)
83 { devlist.push_back(dev); }
84
85 bool
86 EtherBus::send(EtherInt *sndr, EthPacketPtr &pkt)
87 {
88 if (busy()) {
89 DPRINTF(Ethernet, "ethernet packet not sent, bus busy\n", curTick);
90 return false;
91 }
92
93 DPRINTF(Ethernet, "ethernet packet sent: length=%d\n", pkt->length);
94 DDUMP(EthernetData, pkt->data, pkt->length);
95
96 packet = pkt;
97 sender = sndr;
98 int delay = (int)ceil(((double)pkt->length * ticksPerByte) + 1.0);
99 DPRINTF(Ethernet, "scheduling packet: delay=%d, (rate=%f)\n",
100 delay, ticksPerByte);
101 event.schedule(curTick + delay);
102
103 return true;
104 }
105
106 BEGIN_DECLARE_SIM_OBJECT_PARAMS(EtherBus)
107
108 Param<bool> loopback;
109 Param<double> speed;
110 SimObjectParam<EtherDump *> packet_dump;
111
112 END_DECLARE_SIM_OBJECT_PARAMS(EtherBus)
113
114 BEGIN_INIT_SIM_OBJECT_PARAMS(EtherBus)
115
116 INIT_PARAM(loopback, "send the packet back to the sending interface"),
117 INIT_PARAM(speed, "bus speed in ticks per byte"),
118 INIT_PARAM(packet_dump, "object to dump network packets to")
119
120 END_INIT_SIM_OBJECT_PARAMS(EtherBus)
121
122 CREATE_SIM_OBJECT(EtherBus)
123 {
124 return new EtherBus(getInstanceName(), speed, loopback, packet_dump);
125 }
126
127 REGISTER_SIM_OBJECT("EtherBus", EtherBus)