User guide

CIF Peripheral Model - SimpleTimer
94 CoMET Version 5.9 – Tutorial
Consequently, the code modeling the GTR (General Timer Register) behavior calculates and
updates the counter at the time of read or write. The tick count at the last time the counter was
updated is tracked in an instance variable, LastCounterUpdateTime. The
AmpiClockGetTicks function is used to determine the number of clock ticks since the clock
started. The LastCounterUpdateTime holds timer clock tick count at the last update. When
the GTR register is updated, the LastCounterUpdateTime is updated with the current
TimerClock count. The example below is from the WriteRegister function in SimpleTimer.c.
Count = IP->regGTR + (tWord32)(AmpiClockGetTicks(IP->TimerClock, 0)
- IP->LastCounterUpdateTime);
// Nothing to do if it hasn't changed.
if (Count != Data)
{
// Save the new counter value.
IP->regGTR = Data;
IP->LastCounterUpdateTime = AmpiClockGetTicks(IP->TimerClock,
0);
Determining When a Match Occurs
To determine whether a match has occurred, we could check the value of the counter value
against the match register value at each tick. However this is inefficient in a model. We are
simulating with a scheduler. We simply tell the scheduler when the next match will occur,
determined by the value in the match register and the value in the timer register. To do this
we schedule a callback function.
The time at which the callback function is scheduled is calculated on the basis of the GTR
(General Timer Register) value, the MTR (Match Timer Register) value that is to match it,
and the TimerClock.
The details are in the function SetupNextMatch in SimpleTimer.c. The GTR Count is
calculated as shown above. Then the time to the next match is calculated as follows:
if (IP->regMTR[MTR_ix] > Count)
MatchTicks = IP->regMTR[MTR_ix] - Count;
else
MatchTicks = (tWord32)(ULL(0x100000000) + IP->regMTR[MTR_ix] -
Count);
The callback is then scheduled as follows:
AmpiTaskScheduleCallbackTicksAfterSync(IP->MatchEvent[MTR_ix],
MatchTicks, 0)
It is now up to the scheduler to call the SimpleTimer when the event occurs. Meanwhile the
SimpleTimer remains idle. To set up callback functions, we have to
create the functions
declare handles for the callback functions
create callback data structures
register the callback functions in the SimpleTimerInitTaskInstance function.