From: <jo...@us...> - 2008-09-03 16:01:45
|
Revision: 297 http://mspsim.svn.sourceforge.net/mspsim/?rev=297&view=rev Author: joxe Date: 2008-09-03 16:01:38 +0000 (Wed, 03 Sep 2008) Log Message: ----------- cleaned up timer code slightly Modified Paths: -------------- mspsim/se/sics/mspsim/core/MSP430.java mspsim/se/sics/mspsim/core/Timer.java Modified: mspsim/se/sics/mspsim/core/MSP430.java =================================================================== --- mspsim/se/sics/mspsim/core/MSP430.java 2008-08-29 22:09:45 UTC (rev 296) +++ mspsim/se/sics/mspsim/core/MSP430.java 2008-09-03 16:01:38 UTC (rev 297) @@ -280,9 +280,12 @@ } private void printCPUSpeed(int pc) { + // Passed time int td = (int)(System.currentTimeMillis() - time); - int cd = (int) (cycles - lastCycles); - int cpud = (int) (cpuCycles - lastCpuCycles); + // Passed total cycles + long cd = (cycles - lastCycles); + // Passed "active" CPU cycles + long cpud = (cpuCycles - lastCpuCycles); if (td == 0 || cd == 0) { return; @@ -295,7 +298,7 @@ + 1000 * (cpud / td ) + " cyc/s " + (10000 * cpud / cd)/100.0 + "%"); } - lastCPUPercent = (10000 * cpud / cd)/100.0; + lastCPUPercent = (10000 * cpud / cd) / 100.0; time = System.currentTimeMillis(); lastCycles = cycles; lastCpuCycles = cpuCycles; Modified: mspsim/se/sics/mspsim/core/Timer.java =================================================================== --- mspsim/se/sics/mspsim/core/Timer.java 2008-08-29 22:09:45 UTC (rev 296) +++ mspsim/se/sics/mspsim/core/Timer.java 2008-09-03 16:01:38 UTC (rev 297) @@ -65,7 +65,7 @@ * * ___---___---___ * - * ==> Reads might be another problem. If a loop is just cheking the + * ==> Reads might be another problem. If a loop is just checking the * counter it will be reading same value for a long time. Needs to "capture" * reads to Timers by some simple means... */ @@ -134,9 +134,18 @@ // useful for setting expected compare and capture times to correct time. // valid for timer A private final int timerOverflow; - private long counterStart = 0; private long nextTimerTrigger = 0; + // this is used to create "tick" since last reset of the timer. + // it will contain the full number of ticks since that reset and + // is used to calculate the real counter value + private long counterStart = 0; + private long counterAcc; + + // Counter stores the current timer counter register (TR) + private int counter = 0; + private int counterPassed = 0; + // Input map for timer A public static final int[] TIMER_Ax149 = new int[] { SRC_PORT + 0x10, SRC_ACLK, SRC_SMCLK, SRC_PORT + 0x21, // Timer @@ -171,13 +180,6 @@ private int clockSource; private int mode; - - private int counter = 0; - private int counterPassed = 0; - // The value of the counter when something changed last time - // (change of divisor, or a start TAR different than 0 when timer - // starts, etc). - private int initialCounter = 0; // The IO registers private int tctl; @@ -263,7 +265,7 @@ counter = 0; counterPassed = 0; counterStart = 0; - initialCounter = 0; + counterAcc = 0; clockSource = 0; cyclesMultiplicator = 1; mode = STOP; @@ -387,8 +389,8 @@ if ((data & TCLR) != 0) { counter = 0; - counterStart = cycles; - // inputDivider = 1; ???? + resetCounter(cycles); + updateCaptures(-1, cycles); } @@ -396,8 +398,8 @@ if (mode == STOP && newMode != STOP) { // Set the initial counter to the value that counter should have after // recalculation - initialCounter = counter; - counterStart = cycles; + resetCounter(cycles); + // Wait until full wrap before setting the IRQ flag! nextTimerTrigger = (long) (cycles + cyclesMultiplicator * ((0xffff - counter) & 0xffff)); if (DEBUG) System.out.println(getName() + " Starting timer!"); @@ -469,6 +471,14 @@ case TCCR6: // update of compare register index = (iAddress - TCCR0) / 2; + if (index == 0) { + // Reset the counter to bring it down to a smaller value... + // Check if up or updwn and reset if counter too high... + if (counter > data && (mode == UPDWN || mode == UP)) { + counter = 0; + } + resetCounter(cycles); + } tccr[index] = data; updateCounter(cycles); @@ -500,10 +510,17 @@ calculateNextEventTime(cycles); } } + + private void resetCounter(long cycles) { + counterStart = cycles; + // set counterACC to the last returned value (which is the same + // as bigCounter except that it is "moduloed" to a smaller value + counterAcc = counter; + } private void setCounter(int newCtr, long cycles) { counter = newCtr; - counterStart = cycles; + resetCounter(cycles); } private void updateCaptures(int index, long cycles) { @@ -566,23 +583,36 @@ divider = 1.0 * core.smclkFrq / core.aclkFrq; } divider = divider * inputDivider; + + // These calculations assume that we have a big counter that counts from + // last reset and upwards (without any roundoff errors). + // tick - represent the counted value since last "reset" of some kind + // counterAcc - represent the value of the counter at the last reset. long cycctr = cycles - counterStart; double tick = cycctr / divider; counterPassed = (int) (divider * (tick - (long) (tick))); + long bigCounter = (long) (tick + counterAcc); + + //System.out.println("BigStart: " + bigCounterStart + " C:" + cycles + " bigCounter: " + bigCounter); + switch (mode) { case CONTIN: - counter = ((int) tick + initialCounter) & 0xffff; + counter = (int) (bigCounter & 0xffff); break; case UP: - counter = ((int) tick + initialCounter) % tccr[0]; + counter = (int) (bigCounter % tccr[0]); break; case UPDWN: - counter = ((int) tick + initialCounter) % (tccr[0] * 2); + counter = (int) (bigCounter % (tccr[0] * 2)); if (counter > tccr[0]) { // Should back down to start again! counter = 2 * tccr[0] - counter; } } + +// System.out.println("UpdateCounter: C1: " + counter + " C2:" + ctr); + + if (DEBUG) { System.out.println(getName() + ": Updating counter cycctr: " + cycctr + " divider: " + divider + " mode:" + mode + " => " + counter); } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |