001: //
002: // Copyright (C) 2005 United States Government as represented by the
003: // Administrator of the National Aeronautics and Space Administration
004: // (NASA). All Rights Reserved.
005: //
006: // This software is distributed under the NASA Open Source Agreement
007: // (NOSA), version 1.3. The NOSA has been approved by the Open Source
008: // Initiative. See the file NOSA-1.3-JPF at the top of the distribution
009: // directory tree for the complete NOSA document.
010: //
011: // THE SUBJECT SOFTWARE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY OF ANY
012: // KIND, EITHER EXPRESSED, IMPLIED, OR STATUTORY, INCLUDING, BUT NOT
013: // LIMITED TO, ANY WARRANTY THAT THE SUBJECT SOFTWARE WILL CONFORM TO
014: // SPECIFICATIONS, ANY IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR
015: // A PARTICULAR PURPOSE, OR FREEDOM FROM INFRINGEMENT, ANY WARRANTY THAT
016: // THE SUBJECT SOFTWARE WILL BE ERROR FREE, OR ANY WARRANTY THAT
017: // DOCUMENTATION, IF PROVIDED, WILL CONFORM TO THE SUBJECT SOFTWARE.
018: //
019: package DEOS;
020:
021: import gov.nasa.jpf.jvm.Verify;
022:
023: /**
024: * DOCUMENT ME!
025: */
026: public class Clock {
027: public static int TIME_CONSTRAINT = Registry.numPeriods * 20 * 2; // numPeriods is usually 3
028: public static int NOINTERRUPTS = 0;
029: public static int TIMEOUT = 1;
030: public static int SYSTEMINTERRUPT = 2;
031: public static int NOTIMECHANGE = 3;
032: int currentTime = -20;
033: PeriodicClock clockToNotify; // may use a list in another version
034: NewTimer timerToNotify; // if there are more than two clocking devices
035: boolean eventDriven = false;
036:
037: // For abstraction - need to keep track of current time before event fo
038:
039: /**
040: * Main constructor
041: */
042: public Clock(PeriodicClock periodicIn, NewTimer timerIn) {
043: if (DEOS.abstraction) {
044: currentTime = -20;
045: } else {
046: currentTime = -1;
047: }
048:
049: clockToNotify = periodicIn;
050: timerToNotify = timerIn;
051: }
052:
053: /**
054: * To get the current time
055: * @return currentTime
056: */
057: public int getCurrentTime() {
058: return currentTime;
059: }
060:
061: /**
062: * To set the timer
063: * @param timeIn time with which to set timer
064: */
065: public void setTimer(int timeIn) {
066: timerToNotify.setTimer(timeIn, currentTime);
067: }
068:
069: /**
070: * Clock clears interrupts
071: */
072: public void clearInterrupts() {
073: clockToNotify.clearInterrupt();
074: timerToNotify.clearTimeOut();
075: }
076:
077: /**
078: * Clock ticks
079: * @return int - NOINTERRUPTS or TIMEOUT or SYSTEMINTERRUPT
080: */
081: public int ticks() {
082: clearInterrupts();
083:
084: int delta;
085:
086: if (!DEOS.abstraction) {
087: delta = 1;
088: } else {
089: int timeToEOP = (clockToNotify.getTimeToEOP());
090: int timeOutTime = (timerToNotify.getStoppingTime());
091: int timeToTimeOut = (timeOutTime - currentTime);
092:
093: //System.out.println("currentTime = " + currentTime + " timeToEOP = " + timeToEOP + " timeOutTime = " + timeOutTime + " timeToTimeOut = " + timeToTimeOut);
094: if (Verify.randomBool()) {
095: delta = 0;
096: } else {
097: if (timeToEOP <= timeToTimeOut) {
098: delta = timeToEOP;
099: } else {
100: delta = timeToTimeOut;
101: }
102: }
103: }
104:
105: if (delta == 0) {
106: return NOTIMECHANGE;
107: } else {
108: if ((currentTime + delta) > TIME_CONSTRAINT) {
109: return NOTIMECHANGE;
110: }
111:
112: currentTime = (currentTime + delta);
113: clockToNotify.clockTicks(currentTime);
114: timerToNotify.clockTicks(currentTime);
115:
116: if (clockToNotify.isInterrupted()) {
117: timerToNotify.interruptTimer();
118:
119: return SYSTEMINTERRUPT;
120: } else if (timerToNotify.isTimeOut()) {
121: return TIMEOUT;
122: } else {
123: return NOINTERRUPTS;
124: }
125: }
126: }
127:
128: /**
129: * System interrupt or time out events
130: * @return the event that occurred
131: */
132:
133: /* does not seem to be called anywhere?
134: public int interruptEvents() {
135: clearInterrupts();
136: int timeToEOP = (clockToNotify.getTimeToEOP());
137: int periodTime = (currentTime + timeToEOP);
138: int timeOutTime = (timerToNotify.getStoppingTime());
139: if (periodTime <= timeOutTime) {
140: currentTime = periodTime;
141: clockToNotify.clockTicks(currentTime);
142: timerToNotify.clockTicks(currentTime);
143: return SYSTEMINTERRUPT;
144: } else {
145: currentTime = timeOutTime;
146: clockToNotify.clockTicks(currentTime);
147: timerToNotify.clockTicks(currentTime);
148: return TIMEOUT;
149: }
150: }
151: */
152: }
|