X86: Extend mov2int and mov2fp so they can support insert and extract instructions.
[gem5.git] / src / dev / intel_8254_timer.cc
index 16d09f582e1637bd59716ac119c2c4b55f199bf6..770df1c76dd547613c43d2e56240e4112d2f594a 100644 (file)
 
 using namespace std;
 
-Intel8254Timer::Intel8254Timer(const string &name)
-    : _name(name), counter0(name + ".counter0"), counter1(name + ".counter1"),
-      counter2(name + ".counter2")
+Intel8254Timer::Intel8254Timer(EventManager *em, const string &name,
+    Counter *counter0, Counter *counter1, Counter *counter2) :
+    EventManager(em), _name(name)
 {
-    counter[0] = &counter0;
-    counter[1] = &counter0;
-    counter[2] = &counter0;
+    counter[0] = counter0;
+    counter[1] = counter1;
+    counter[2] = counter2;
+}
+
+Intel8254Timer::Intel8254Timer(EventManager *em, const string &name) :
+    EventManager(em), _name(name)
+{
+    counter[0] = new Counter(this, name + ".counter0", 0);
+    counter[1] = new Counter(this, name + ".counter1", 1);
+    counter[2] = new Counter(this, name + ".counter2", 2);
 }
 
 void
@@ -65,9 +73,9 @@ void
 Intel8254Timer::serialize(const string &base, ostream &os)
 {
     // serialize the counters
-    counter0.serialize(base + ".counter0", os);
-    counter1.serialize(base + ".counter1", os);
-    counter2.serialize(base + ".counter2", os);
+    counter[0]->serialize(base + ".counter0", os);
+    counter[1]->serialize(base + ".counter1", os);
+    counter[2]->serialize(base + ".counter2", os);
 }
 
 void
@@ -75,15 +83,16 @@ Intel8254Timer::unserialize(const string &base, Checkpoint *cp,
         const string &section)
 {
     // unserialze the counters
-    counter0.unserialize(base + ".counter0", cp, section);
-    counter1.unserialize(base + ".counter1", cp, section);
-    counter2.unserialize(base + ".counter2", cp, section);
+    counter[0]->unserialize(base + ".counter0", cp, section);
+    counter[1]->unserialize(base + ".counter1", cp, section);
+    counter[2]->unserialize(base + ".counter2", cp, section);
 }
 
-Intel8254Timer::Counter::Counter(const string &name)
-    : _name(name), event(this), count(0), latched_count(0), period(0),
-      mode(0), output_high(false), latch_on(false), read_byte(LSB),
-      write_byte(LSB)
+Intel8254Timer::Counter::Counter(Intel8254Timer *p,
+        const string &name, unsigned int _num)
+    : _name(name), num(_num), event(this), initial_count(0),
+      latched_count(0), period(0), mode(0), output_high(false),
+      latch_on(false), read_byte(LSB), write_byte(LSB), parent(p)
 {
 
 }
@@ -95,10 +104,24 @@ Intel8254Timer::Counter::latchCount()
     if(!latch_on) {
         latch_on = true;
         read_byte = LSB;
-        latched_count = count;
+        latched_count = currentCount();
     }
 }
 
+int
+Intel8254Timer::Counter::currentCount()
+{
+    int clocks = event.clocksLeft();
+    if (clocks == -1) {
+        warn_once("Reading current count from inactive timer.\n");
+        return 0;
+    }
+    if (mode == RateGen || mode == SquareWave)
+        return clocks + 1;
+    else
+        return clocks;
+}
+
 uint8_t
 Intel8254Timer::Counter::read()
 {
@@ -117,6 +140,7 @@ Intel8254Timer::Counter::read()
             panic("Shouldn't be here");
         }
     } else {
+        uint16_t count = currentCount();
         switch (read_byte) {
           case LSB:
             read_byte = MSB;
@@ -137,23 +161,26 @@ Intel8254Timer::Counter::write(const uint8_t data)
 {
     switch (write_byte) {
       case LSB:
-        count = (count & 0xFF00) | data;
+        initial_count = (initial_count & 0xFF00) | data;
 
         if (event.scheduled())
-          event.deschedule();
+            parent->deschedule(event);
         output_high = false;
         write_byte = MSB;
         break;
 
       case MSB:
-        count = (count & 0x00FF) | (data << 8);
-        period = count;
+        initial_count = (initial_count & 0x00FF) | (data << 8);
+        // In the RateGen or SquareWave modes, the timer wraps around and
+        // triggers on a value of 1, not 0.
+        if (mode == RateGen || mode == SquareWave)
+            period = initial_count - 1;
+        else
+            period = initial_count;
+
+        if (period > 0)
+            event.setTo(period);
 
-        if (period > 0) {
-            DPRINTF(Intel8254Timer, "Timer set to curTick + %d\n",
-                    count * event.interval);
-            event.schedule(curTick + count * event.interval);
-        }
         write_byte = LSB;
         break;
     }
@@ -192,7 +219,7 @@ Intel8254Timer::Counter::outputHigh()
 void
 Intel8254Timer::Counter::serialize(const string &base, ostream &os)
 {
-    paramOut(os, base + ".count", count);
+    paramOut(os, base + ".initial_count", initial_count);
     paramOut(os, base + ".latched_count", latched_count);
     paramOut(os, base + ".period", period);
     paramOut(os, base + ".mode", mode);
@@ -211,7 +238,7 @@ void
 Intel8254Timer::Counter::unserialize(const string &base, Checkpoint *cp,
                                          const string &section)
 {
-    paramIn(cp, section, base + ".count", count);
+    paramIn(cp, section, base + ".initial_count", initial_count);
     paramIn(cp, section, base + ".latched_count", latched_count);
     paramIn(cp, section, base + ".period", period);
     paramIn(cp, section, base + ".mode", mode);
@@ -223,11 +250,10 @@ Intel8254Timer::Counter::unserialize(const string &base, Checkpoint *cp,
     Tick event_tick;
     paramIn(cp, section, base + ".event_tick", event_tick);
     if (event_tick)
-        event.schedule(event_tick);
+        parent->schedule(event, event_tick);
 }
 
 Intel8254Timer::Counter::CounterEvent::CounterEvent(Counter* c_ptr)
-    : Event(&mainEventQueue)
 {
     interval = (Tick)(Clock::Float::s / 1193180.0);
     counter = c_ptr;
@@ -236,20 +262,40 @@ Intel8254Timer::Counter::CounterEvent::CounterEvent(Counter* c_ptr)
 void
 Intel8254Timer::Counter::CounterEvent::process()
 {
-    DPRINTF(Intel8254Timer, "Timer Interrupt\n");
     switch (counter->mode) {
       case InitTc:
         counter->output_high = true;
+        break;
       case RateGen:
       case SquareWave:
+        setTo(counter->period);
         break;
       default:
         panic("Unimplemented PITimer mode.\n");
     }
+    counter->parent->counterInterrupt(counter->num);
+}
+
+void
+Intel8254Timer::Counter::CounterEvent::setTo(int clocks)
+{
+    if (clocks == 0)
+        panic("Timer can't be set to go off instantly.\n");
+    DPRINTF(Intel8254Timer, "Timer set to curTick + %d\n",
+            clocks * interval);
+    counter->parent->schedule(this, curTick + clocks * interval);
+}
+
+int
+Intel8254Timer::Counter::CounterEvent::clocksLeft()
+{
+    if (!scheduled())
+        return -1;
+    return (when() - curTick + interval - 1) / interval;
 }
 
 const char *
 Intel8254Timer::Counter::CounterEvent::description() const
 {
-    return "tsunami 8254 Interval timer";
+    return "Intel 8254 Interval timer";
 }