Every real-time simulation requires a clock to which its tasks can be synchronized. By default, a Trick simulation uses the local system clock, by calling *gettimeofday()*. When simulations running on different computers need to cooperate they need to be synchronized to the same clock. So, sometimes we want our simulation to synchronize a to an **external** clock rather than the local one. The ```Trick::Clock``` base class provides a way to create an interface between an external time source, and a real-time Trick simulation.
The Trick::Clock class is declared in ```trick/Clock.hh```. Deriving a new clock interface class from it requires the following three member functions to be implemented.
In our simulation's S_define file, we could change the real-time scheduler's clock from the default ```Trick::GetTimeOfDayClock``` to our new ```CuckooClock``` using ```real_time_change_clock()```, declared in ```trick/realtimesync_proto.h```.
The following code is an example implementation of a Trick::Clock, called TPROCTEClock. It provides an interface between Trick's real-time job scheduler and Spectracom's TPRO IRIG-B clock board. This is one of many available time sources that one might use. Note that driver support for timing cards, like the "tpro.h" and "tsync.h" files below, must be acquired from the card's vendor.
Here, we initialize the timing card by opening the device file, getting a device handle. Then we wait for for it to be available. If an error occurs, return a non-zero error code, otherwise we call ```set_global_clock()``` and return 0.
printf (" Could Not Open '%s'!! [%d]\n", dev_name.c_str(), rv);
return (1);
}
/* Wait until this handle is the first user of the device. */
if (TPRO_setPropDelayCorr(pBoard, NULL) != TPRO_SUCCESS) {
printf(" Waiting to become first user...\n");
while (TPRO_setPropDelayCorr(pBoard, NULL) != TPRO_SUCCESS);
}
set_global_clock() ;
return 0 ;
#else
printf("ERROR: Not configured for TPRO CTE card.");
return -1 ;
#endif
}
```
### TPROCTEClock::wall\_clock\_time
In this function, we get the time from the timing card, and convert it to microseconds before returning it. If the attempt to get the time fails, we return 0.
printf("ERROR: Not configured for TPRO CTE card.");
return 0 ;
#endif
}
```
### TPROCTEClock::clock\_stop
This function simply closes the handle to the device, and returns 0.
```c
int TPROCTEClock::clock_stop() {
#ifdef _TPRO_CTE
unsigned char rv ;
rv = TPRO_close(pBoard);
/* If unable to close the TPRO/TSAT device... */
if (rv != TPRO_SUCCESS) {
printf (" Could Not Close Board!! [%d]\n", rv);
}
#endif
return 0 ;
}
```
<aid=looking-for-sim-time></a>
## Looking For The Current Simulation Time ?
Trick::Clock is a class for creating interfaces between timing hardware, or other time sources and Trick's realtime synchronization subsystem. It does not maintain the simulation time. That's maintained by the Trick Executive.