14.06.2015 Views

Power ISA™ Version 2.03 - Power.org

Power ISA™ Version 2.03 - Power.org

Power ISA™ Version 2.03 - Power.org

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

<strong>Version</strong> <strong>2.03</strong><br />

Programming Note<br />

Since the update frequency of the Time Base is implementation-dependent,<br />

the algorithm for converting the<br />

current value in the Time Base to time of day is also<br />

implementation-dependent.<br />

As an example, assume that the Time Base is incremented<br />

at a constant rate of once for every 32 cycles of<br />

a 1 GHz CPU instruction clock. What is wanted is the<br />

pair of 32-bit values comprising a POSIX standard<br />

clock: 1 the number of whole seconds that have passed<br />

since 00:00:00 January 1, 1970, UTC, and the remaining<br />

fraction of a second expressed as a number of<br />

nanoseconds.<br />

Assume that:<br />

<br />

<br />

<br />

The value 0 in the Time Base represents the start<br />

time of the POSIX clock (if this is not true, a simple<br />

64-bit subtraction will make it so).<br />

The integer constant ticks_per_sec contains the<br />

value<br />

1 GHz<br />

------------------<br />

32<br />

= 31,250,000<br />

which is the number of times the Time Base is<br />

updated each second.<br />

The integer constant ns_adj contains the value<br />

1,000,000,000<br />

------------------------------------- = 32<br />

31,250,000<br />

which is the number of nanoseconds per tick of the<br />

Time Base.<br />

When the processor is in 64-bit mode, the POSIX clock<br />

can be computed with an instruction sequence such as<br />

this:<br />

mfspr Ry,268 # Ry = Time Base<br />

lwz Rx,ticks_per_sec<br />

divd Rz,Ry,Rx# Rz = whole seconds<br />

stw<br />

mulld<br />

sub<br />

lwz<br />

mulld<br />

stw<br />

Rz,posix_sec<br />

Rz,Rz,Rx# Rz = quotient × divisor<br />

Rz,Ry,Rz# Rz = excess ticks<br />

Rx,ns_adj<br />

Rz,Rz,Rx# Rz = excess nanoseconds<br />

Rz,posix_ns<br />

For the Embedded environment when the processor is<br />

in 32-bit mode, it is not possible to read the Time Base<br />

using a single instruction. Instead, two instructions<br />

must be used, one of which reads TBL and the other of<br />

which reads TBU. Because of the possibility of a carry<br />

from TBL to TBU occurring between the two reads, a<br />

sequence such as the following must be used to read<br />

the Time Base.<br />

loop:<br />

mfspr Rx,TBU # load from TBU<br />

mfspr Ry,TB # load from TB<br />

mfspr Rz,TBU # load from TBU<br />

cmp cr0,0,Rx,Rz# check if ‘old’=’new’<br />

bne loop #branch if carry occurred<br />

Non-constant update frequency<br />

In a system in which the update frequency of the Time<br />

Base may change over time, it is not possible to convert<br />

an isolated Time Base value into time of day.<br />

Instead, a Time Base value has meaning only with<br />

respect to the current update frequency and the time of<br />

day that the update frequency was last changed. Each<br />

time the update frequency changes, either the system<br />

software is notified of the change via an interrupt (see<br />

Book III), or the change was instigated by the system<br />

software itself. At each such change, the system software<br />

must compute the current time of day using the<br />

old update frequency, compute a new value of<br />

ticks_per_sec for the new frequency, and save the time<br />

of day, Time Base value, and tick rate. Subsequent<br />

calls to compute Time of Day use the current Time<br />

Base Value and the saved value.<br />

1. Described in POSIX Draft Standard P1003.4/D12, Draft Standard for Information Technology -- Portable Operating System Interface (POSIX) --<br />

Part 1: System Application Program Interface (API) - Amendment 1: Real-time Extension [C Language]. Institute of Electrical and Electronics Engineers,<br />

Inc., Feb. 1992.<br />

Chapter 4. Time Base<br />

371

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!