Support NNPC and branch instructions ... Outputs to decoder.cc correctly
[gem5.git] / dev / etherlink.cc
index 0acc50b0b32b2298fe1399af86c85e632daf502d..f68332926d1094b679c81bf9a677c3f66d255bf7 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2002-2004 The Regents of The University of Michigan
+ * Copyright (c) 2002-2005 The Regents of The University of Michigan
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
@@ -35,6 +35,7 @@
 #include <string>
 #include <vector>
 
+#include "base/random.hh"
 #include "base/trace.hh"
 #include "dev/etherdump.hh"
 #include "dev/etherint.hh"
 #include "sim/builder.hh"
 #include "sim/serialize.hh"
 #include "sim/system.hh"
-#include "sim/universe.hh"
+#include "sim/root.hh"
 
 using namespace std;
 
-EtherLink::EtherLink(const string &name, EtherInt *i1, EtherInt *i2,
-                     Tick speed, Tick dly, EtherDump *dump)
+EtherLink::EtherLink(const string &name, EtherInt *peer0, EtherInt *peer1,
+                     double rate, Tick delay, Tick delayVar, EtherDump *dump)
     : SimObject(name)
 {
-    double rate = ((double)ticksPerSecond * 8.0) / (double)speed;
-    Tick delay = US2Ticks(dly);
+    link[0] = new Link(name + ".link0", this, 0, rate, delay, delayVar, dump);
+    link[1] = new Link(name + ".link1", this, 1, rate, delay, delayVar, dump);
 
-    link1 = new Link(name + ".link1", rate, delay, dump);
-    link2 = new Link(name + ".link2", rate, delay, dump);
+    interface[0] = new Interface(name + ".int0", link[0], link[1]);
+    interface[1] = new Interface(name + ".int1", link[1], link[0]);
 
-    int1 = new Interface(name + ".int1", link1, link2);
-    int2 = new Interface(name + ".int2", link2, link1);
-
-    int1->setPeer(i1);
-    i1->setPeer(int1);
-    int2->setPeer(i2);
-    i2->setPeer(int2);
+    interface[0]->setPeer(peer0);
+    peer0->setPeer(interface[0]);
+    interface[1]->setPeer(peer1);
+    peer1->setPeer(interface[1]);
 }
 
 EtherLink::~EtherLink()
 {
-    delete link1;
-    delete link2;
+    delete link[0];
+    delete link[1];
 
-    delete int1;
-    delete int2;
+    delete interface[0];
+    delete interface[1];
 }
 
 EtherLink::Interface::Interface(const string &name, Link *tx, Link *rx)
@@ -82,26 +80,25 @@ EtherLink::Interface::Interface(const string &name, Link *tx, Link *rx)
     rx->setRxInt(this);
 }
 
-EtherLink::Link::Link(const string &name, double rate, Tick delay,
-                      EtherDump *d)
-    : objName(name), txint(NULL), rxint(NULL), ticksPerByte(rate),
-      linkDelay(delay), dump(d), doneEvent(this)
-{}
+EtherLink::Link::Link(const string &name, EtherLink *p, int num,
+                      double rate, Tick delay, Tick delay_var, EtherDump *d)
+    : objName(name), parent(p), number(num), txint(NULL), rxint(NULL),
+      ticksPerByte(rate), linkDelay(delay), delayVar(delay_var), dump(d),
+      doneEvent(this)
+{ }
 
 void
 EtherLink::serialize(ostream &os)
 {
-    nameOut(os, name() + ".link1");
-    link1->serialize(os);
-    nameOut(os, name() + ".link2");
-    link2->serialize(os);
+    link[0]->serialize("link0", os);
+    link[1]->serialize("link1", os);
 }
 
 void
 EtherLink::unserialize(Checkpoint *cp, const string &section)
 {
-    link1->unserialize(cp, section + ".link1");
-    link2->unserialize(cp, section + ".link2");
+    link[0]->unserialize("link0", cp, section);
+    link[1]->unserialize("link1", cp, section);
 }
 
 void
@@ -118,10 +115,9 @@ class LinkDelayEvent : public Event
     EtherLink::Link *link;
     PacketPtr packet;
 
-    // non-scheduling version for createForUnserialize()
-    LinkDelayEvent(EtherLink::Link *link);
-
   public:
+    // non-scheduling version for createForUnserialize()
+    LinkDelayEvent();
     LinkDelayEvent(EtherLink::Link *link, PacketPtr pkt, Tick when);
 
     void process();
@@ -132,7 +128,6 @@ class LinkDelayEvent : public Event
                                               const string &section);
 };
 
-
 void
 EtherLink::Link::txDone()
 {
@@ -165,6 +160,10 @@ EtherLink::Link::transmit(PacketPtr pkt)
 
     packet = pkt;
     Tick delay = (Tick)ceil(((double)pkt->length * ticksPerByte) + 1.0);
+    if (delayVar != 0) {
+        Random<Tick> var;
+        delay +=  var.uniform(0, delayVar);
+    }
     DPRINTF(Ethernet, "scheduling packet: delay=%d, (rate=%f)\n",
             delay, ticksPerByte);
     doneEvent.schedule(curTick + delay);
@@ -173,43 +172,44 @@ EtherLink::Link::transmit(PacketPtr pkt)
 }
 
 void
-EtherLink::Link::serialize(ostream &os)
+EtherLink::Link::serialize(const string &base, ostream &os)
 {
     bool packet_exists = packet;
-    SERIALIZE_SCALAR(packet_exists);
+    paramOut(os, base + ".packet_exists", packet_exists);
+    if (packet_exists)
+        packet->serialize(base + ".packet", os);
 
     bool event_scheduled = doneEvent.scheduled();
-    SERIALIZE_SCALAR(event_scheduled);
+    paramOut(os, base + ".event_scheduled", event_scheduled);
     if (event_scheduled) {
         Tick event_time = doneEvent.when();
-        SERIALIZE_SCALAR(event_time);
+        paramOut(os, base + ".event_time", event_time);
     }
 
-    if (packet_exists)
-        packet->serialize("packet", os);
 }
 
 void
-EtherLink::Link::unserialize(Checkpoint *cp, const string &section)
+EtherLink::Link::unserialize(const string &base, Checkpoint *cp,
+                             const string &section)
 {
     bool packet_exists;
-    UNSERIALIZE_SCALAR(packet_exists);
+    paramIn(cp, section, base + ".packet_exists", packet_exists);
     if (packet_exists) {
         packet = new PacketData(16384);
-        packet->unserialize("packet", cp, section);
+        packet->unserialize(base + ".packet", cp, section);
     }
 
     bool event_scheduled;
-    UNSERIALIZE_SCALAR(event_scheduled);
+    paramIn(cp, section, base + ".event_scheduled", event_scheduled);
     if (event_scheduled) {
         Tick event_time;
-        UNSERIALIZE_SCALAR(event_time);
+        paramIn(cp, section, base + ".event_time", event_time);
         doneEvent.schedule(event_time);
     }
 }
 
-LinkDelayEvent::LinkDelayEvent(EtherLink::Link *l)
-    : Event(&mainEventQueue), link(l)
+LinkDelayEvent::LinkDelayEvent()
+    : Event(&mainEventQueue), link(NULL)
 {
     setFlags(AutoSerialize);
     setFlags(AutoDelete);
@@ -234,7 +234,11 @@ LinkDelayEvent::serialize(ostream &os)
 {
     paramOut(os, "type", string("LinkDelayEvent"));
     Event::serialize(os);
-    SERIALIZE_OBJPTR(link);
+
+    EtherLink *parent = link->parent;
+    bool number = link->number;
+    SERIALIZE_OBJPTR(parent);
+    SERIALIZE_SCALAR(number);
 
     packet->serialize("packet", os);
 }
@@ -244,6 +248,14 @@ void
 LinkDelayEvent::unserialize(Checkpoint *cp, const string &section)
 {
     Event::unserialize(cp, section);
+
+    EtherLink *parent;
+    bool number;
+    UNSERIALIZE_OBJPTR(parent);
+    UNSERIALIZE_SCALAR(number);
+
+    link = parent->link[number];
+
     packet = new PacketData(16384);
     packet->unserialize("packet", cp, section);
 }
@@ -252,9 +264,7 @@ LinkDelayEvent::unserialize(Checkpoint *cp, const string &section)
 Serializable *
 LinkDelayEvent::createForUnserialize(Checkpoint *cp, const string &section)
 {
-    EtherLink::Link *link;
-    UNSERIALIZE_OBJPTR(link);
-    return new LinkDelayEvent(link);
+    return new LinkDelayEvent();
 }
 
 REGISTER_SERIALIZEABLE("LinkDelayEvent", LinkDelayEvent)
@@ -263,8 +273,9 @@ BEGIN_DECLARE_SIM_OBJECT_PARAMS(EtherLink)
 
     SimObjectParam<EtherInt *> int1;
     SimObjectParam<EtherInt *> int2;
-    Param<Tick> speed;
+    Param<double> speed;
     Param<Tick> delay;
+    Param<Tick> delay_var;
     SimObjectParam<EtherDump *> dump;
 
 END_DECLARE_SIM_OBJECT_PARAMS(EtherLink)
@@ -273,15 +284,17 @@ BEGIN_INIT_SIM_OBJECT_PARAMS(EtherLink)
 
     INIT_PARAM(int1, "interface 1"),
     INIT_PARAM(int2, "interface 2"),
-    INIT_PARAM_DFLT(speed, "link speed in bits per second", 100000000),
-    INIT_PARAM_DFLT(delay, "transmit delay of packets in us", 0),
-    INIT_PARAM_DFLT(dump, "object to dump network packets to", NULL)
+    INIT_PARAM(speed, "link speed in bits per second"),
+    INIT_PARAM(delay, "transmit delay of packets in us"),
+    INIT_PARAM(delay_var, "Difference in amount of time to traverse wire"),
+    INIT_PARAM(dump, "object to dump network packets to")
 
 END_INIT_SIM_OBJECT_PARAMS(EtherLink)
 
 CREATE_SIM_OBJECT(EtherLink)
 {
-    return new EtherLink(getInstanceName(), int1, int2, speed, delay, dump);
+    return new EtherLink(getInstanceName(), int1, int2, speed, delay, delay_var,
+                         dump);
 }
 
 REGISTER_SIM_OBJECT("EtherLink", EtherLink)