P11.41.1 Offset and Delay from Four Timestamps

EasyImplementIcarus Verilog · serverReview the topic

A PTP slave holds four timestamps and three correction fields. Work out how far its clock is off, and how long the path is.

Hardware context: An Ethernet MAC and physical coding sublayer: frame builder and checker, reconciliation sublayer and block encoder. Build, check and pace Ethernet frames, and code them for the physical layer as IEEE 802.3 specifies.

One exchange of the delay request-response mechanism gives a slave four timestamps.

The master stamped the Sync when it left, and the slave when it arrived. That is t1 and t2.

The slave stamped its Delay_Req when it left, and the master when it arrived. That is t3 and t4.

Write ptp_calc. It returns the meanPathDelay and the offsetFromMaster, as the standard defines them.

A timestamp has 48 bits of seconds and 32 bits of nanoseconds. The two clocks may be seconds or years apart.

The three correction fields are signed and count in 1/65,536 of a nanosecond. So do both results.

A one-step master sends no Follow_Up. Then precise and c_fup hold old values, and you must not use them.

A two-step master sends the exact time in the Follow_Up. Then origin is only a rough value.

The lesson has both formulas. P11.41.2 shows a slave that works in 64 bits of whole nanoseconds: that is not enough here.

Signals in the Timeline

SignalWidthRole
test_number8input of each check
Full specification
Signature (keep it)
function automatic [127:0] ptp_calc(input [79:0] origin, input [79:0] precise, input [79:0] t2, input [79:0] t3, input [79:0] t4,
                                    input [63:0] c_sync, input [63:0] c_fup, input [63:0] c_resp, input [63:0] asym, input bit two_step);
// returns {meanPathDelay[63:0], offsetFromMaster[63:0]}
ItemRule
Timestamp{seconds[47:0], nanoseconds[31:0]}. The nanoseconds are always below 1,000,000,000 (5.3.3)
TimeIntervala signed 64-bit count of 1/65,536 ns. So 2.5 ns is 28000h (5.3.2)
originthe originTimestamp of the Sync
precise, c_fupthe preciseOriginTimestamp and the correctionField of the Follow_Up. Used only if two_step is 1
t2, t3the slave's own timestamps: the Sync arrived, the Delay_Req left
t4the receiveTimestamp of the Delay_Resp
c_sync, c_respthe correctionField of the Sync and of the Delay_Resp, as received
asymthe delayAsymmetry of this port, a TimeInterval. Add it to the correction of the Sync (11.2 a)
meanPathDelayas 11.3.2 e) gives it, for a one-step and for a two-step Sync
offsetFromMasteras 11.2 b) and c) give it. The mean delay in it is the meanPathDelay of this call
The halfcourse choice: round the half toward minus infinity. So −3 counts become −2, and 3 become 1
Rangea result outside the TimeInterval type becomes the largest positive or the largest negative value: 7FFFFFFFFFFFFFFFh or 8000000000000000h (5.3.2)
Order of the range rulecourse choice: work with exact values and clamp each result by itself. A delay in range stays right when the offset is clamped
Not in the teststhe correctionField value that means "too big" (13.3.2.9)

The clause numbers are those of IEEE Std 1588-2019.

Random tests compare both results with a private model, on inputs seeded for each submission.

Examples

Example 1

A Slave 1.5 Ms Ahead: The Delay
Input
t1 100.999999700 s, t2 101.001500200 s, t3 101.001600200 s, t4 101.000100700 s
Output
nanoseconds = 500, fraction_in_65536ths = 0x0

t2 − t1 is 1,500,500 ns and t4 − t3 is −1,499,500 ns. Their sum is 1,000 ns: twice the path. So the delay is 500 ns.

Example 2

A Slave 1.5 Ms Ahead: The Offset
Input
the same call
Output
nanoseconds = 1500000, fraction_in_65536ths = 0x0

t2 − t1 holds the offset and one path delay. 1,500,500 − 500 is 1,500,000 ns.

Example 3

A Slave 250 Ns Behind
Input
t2 − t1 is 250 ns, t4 − t3 is 750 ns
Output
nanoseconds = -250, fraction_in_65536ths = 0x0

The delay is (250 + 750) / 2 = 500 ns. The offset is 250 − 500 = −250 ns: the slave is behind.

Constraints and Contract

  • Keep the function name, its ten arguments and the 128-bit result.
  • Helper functions are welcome.
  • Off limits: the names ref_p, rng, tb, root, names that begin with fx_ and the modules of the bench.

Hints

Hint 1

Turn each timestamp into one number first. How many bits does 2^48 seconds in units of 1/65,536 ns need?

Hint 2

The corrections are signed. Widen them with their sign.

Hint 3

Halve with an arithmetic shift, and clamp at the very end.

What the grader checks: A JavaScript model gives the expected values of 43 fixed calls. Nine more tests compare both results with a private model on random inputs, seeded for each submission. They cover exchanges with corrections and timestamps that differ in each single bit. They also cover unrelated timestamps over the full width and clocks that are years apart. The last kind puts the offset at the limits of the TimeInterval type. The private model is written in another way than the solution and agrees with the JavaScript model on random inputs. Not graded: the message formats, the association of messages by sequenceId, the peer delay mechanism, and the correctionField value for "too big".

52 checks on Icarus Verilog · server. Run the examples first, then check the full regression. Full checks use the server judge. Downloads contain your source only; samples require the simulation service. A passing result establishes the tested cases, not a proof for all possible inputs.

See all 2 steps and the final capstone

Report a problem with this page

Your code SystemVerilogSaved locally
Ln 1, Col 1Tab indents · Ctrl/⌘ + Enter runs samples
Not run yet

A Slave 1.5 Ms Ahead: The Delay

Input
t1 100.999999700 s, t2 101.001500200 s, t3 101.001600200 s, t4 101.000100700 s
Expected output
nanoseconds = 500, fraction_in_65536ths = 0x0

t2 − t1 is 1,500,500 ns and t4 − t3 is −1,499,500 ns. Their sum is 1,000 ns: twice the path. So the delay is 500 ns.

Run samples executes all 3 public checks. Check all tests runs 52 checks on the server.

Icarus Verilog · server