A PTP port needs the time at which each frame passed. Build the unit that keeps the clock and stamps every start of frame, in both directions.
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.
The unit sits beside the MAC of a 1 Gb/s port, on the 125 MHz transmit clock of the GMII.
It keeps the time of day, and it stamps every frame at its start frame delimiter.
A stamp marks the start of the SFD, the point of Clause 90 (90.7). It is not the PTP message timestamp point.
On transmit it watches the GMII and finds the SFD itself.
On receive the SFD is found in another clock domain. The unit only gets a level that changes once a frame.
Every stamp is corrected by the path data delay of the PHY, and waits in a small FIFO with a tag.
A servo trims the clock while frames pass: it sets the rate and it steps the time.
The table gives every rule. It does not say how to build the unit: that is the problem.
Many rules are course choices, and the table labels each. The standards fix the SFD, the delays and their signs.
The lesson's readable eth_sync2 is the two flops with an edge detector. You may port it: it is three lines of the answer.
Signals in the Timeline
| Signal | Width | Role |
|---|---|---|
test_number | 8 | input of each check |
Full specification
reg [47:0] tod_sec; reg [29:0] tod_ns; reg [15:0] tod_frac; // the clock
reg [2:0] ts_count; reg [7:0] ts_lost; // the FIFO
bit ts_dir; reg [7:0] ts_tag; // the oldest stamp
reg [47:0] ts_sec; reg [29:0] ts_ns; reg [15:0] ts_frac;
function automatic void tsu_reset();
function automatic void tsu_delays(input [31:0] tx_min, input [31:0] tx_max, input [31:0] rx_min, input [31:0] rx_max);
function automatic void tsu_rate(input [23:0] incr);
function automatic void tsu_step(input bit back, input [47:0] sec, input [29:0] ns);
function automatic void tsu_pop();
function automatic void tsu_tick(input bit tx_en, input [7:0] txd, input bit rx_tgl);
| Item | Rule |
|---|---|
tsu_tick | one rising edge of the transmit clock. tx_en and txd are TX_EN and TXD<7:0> of the GMII at that edge (35.2.2) |
| The clock | tod_sec seconds, tod_ns nanoseconds below 1,000,000,000, and tod_frac in 1/65,536 ns. A course choice: 16 fraction bits |
| Counting | every tick adds the increment: nanoseconds times 65,536, in 24 bits. After tsu_reset it is 080000h, which is 8 ns |
| Seconds | a course choice: seconds count modulo 2^48, in the clock, in a step and in a stamp. One second before 0 is 2^48 - 1 |
tsu_rate(incr) | a course choice: every tick after the call adds incr |
tsu_step | a course choice: the next tick adds its increment and also moves the clock by sec seconds and ns nanoseconds, once. back = 1 moves it back |
| Two steps | a second tsu_step before that tick replaces the first. The tests keep ns below 1,000,000,000 |
| Reading the clock | a course choice: a stamp reads the clock as it stands before its tick adds anything. That is what tod_sec, tod_ns and tod_frac showed after the call before |
| A run | ticks in a row with tx_en = 1 |
| Transmit SFD | the first octet of a run that is not 55h decides. If it is D5h, it is the SFD (35.2.3.2.1). If not, the run has no SFD |
| Preamble | a course choice: any number of octets 55h may come first, also none. A MAC sends seven (35.2.3.2.1) |
| One a run | a run has at most one SFD. Only an SFD makes a stamp (90.5.1) |
| Transmit stamp | it enters the FIFO in the tick of the SFD. Its time is the clock reading of that tick plus the transmit delay (90.7; IEEE Std 1588-2019 7.3.4.2). This is the Clause 90 point. The octet to the PTP point is left to software |
rx_tgl | a level from the receive clock domain. It changes once for each SFD found there (90.5.2). Every tick samples it |
| Receive event | a tick that samples another level than the tick before. After tsu_reset the level before counts as 0 |
| Two flops | a course choice: the stamp of a receive event enters the FIFO in the second tick after the tick that sampled it |
| Receive stamp | its time is the clock reading of the sampling tick less the receive delay (90.7; 1588 7.3.4.2). No later request changes it |
tsu_delays | the quartet of 90.7 in whole nanoseconds. A register holds 32 bits (45.2.1.176, 45.2.1.177), and each value is the sum over all sublayers (30.13.1.3, 30.13.1.4). Here each has 32 bits. It holds for every stamp that enters the FIFO after the call |
| Which delay | a course choice: a delay is half the sum of its least and greatest value. So half a nanosecond counts. The least may be the greater one |
ts_tag | a course choice: transmit SFDs are numbered 0, 1, 2 and so on, modulo 256. Receive events have a count of their own. A lost stamp uses up its number |
| The FIFO | a course choice: four stamps. ts_count says how many wait. ts_dir, ts_tag, ts_sec, ts_ns and ts_frac show the oldest |
| A full FIFO | a course choice: a stamp that finds four waiting is lost, and ts_lost counts it. ts_lost stops at 255 |
tsu_pop() | a course choice: the next tick removes the oldest stamp, if there is one. Two calls before one tick still remove one |
| Order in a tick | a course choice: first the pop, then the transmit stamp, then the receive stamp, then the clock |
tsu_reset() | clock 0, increment 8 ns, delays 0, both tag counts 0, ts_lost 0. No stamp waits or is on its way, and no request waits. A course choice: the next tick with tx_en = 1 starts a new run, also when tx_en was 1 before. In the standard a packet starts when TX_EN rises (35.2.3.2.1) |
A full check runs about 8,000 ticks. Keep the work of one tick small.
Do the arithmetic of a tick with the operators of the language: +, -, / and %, also on 128 bits.
An adder or a divider written as a loop over bits runs out of time, even when its results are right.
Examples
Example 1
A Frame on the GMII- Input
20 idle ticks, then 55h seven times, D5h and three data octets; transmit delay 40 to 61 ns
- Output
ts_ns = 266, ts_frac = 0x8000, ts_sec = 0, ts_tag = 0, ts_dir = transmit (0), ts_count = 1, ts_lost = 0, ticks_that_differ_from_the_model = 0
The SFD is the octet of tick 28. Before that tick the clock reads 27 times 8 ns: 216 ns. Half of 40 + 61 is 50.5 ns. So the stamp is 266 ns and 8000h: 266.5 ns.
Example 2
A Received Frame Just After a Full Second- Input
a step of 1 s and 999,999,950 ns in tick 1; the level changes in tick 8; receive delay 100 to 120 ns
- Output
ts_ns = 999999896, ts_frac = 0x0, ts_sec = 1, ts_tag = 0, ts_dir = receive (1), ts_count = 1, ts_lost = 0, ticks_that_differ_from_the_model = 0
Before tick 8 the clock reads 2 s and 6 ns. The delay is 110 ns, so the stamp is 1 s and 999,999,896 ns. It enters the FIFO in tick 10.
Example 3
Six Frames and Three Pops- Input
six frames with no pop, then three pops
- Output
ts_ns = 48, ts_frac = 0x0, ts_sec = 0, ts_tag = 3, ts_dir = transmit (0), ts_count = 1, ts_lost = 2, ticks_that_differ_from_the_model = 0
Four stamps fit. Frames 4 and 5 are lost, and ts_lost is 2. Three pops leave frame 3. Its SFD came in tick 7, when the clock read 48 ns.
Example 4
A New Rate While a Receive Stamp Is on Its Way- Input
the level changes in tick 10; the rate becomes 10 ns a tick from tick 11
- Output
ts_ns = 72, ts_frac = 0x0, ts_sec = 0, ts_tag = 0, ts_dir = receive (1), ts_count = 1, ts_lost = 0, ticks_that_differ_from_the_model = 0
Before tick 10 the clock reads 72 ns. That is the stamp. It enters in tick 12, when the clock already reads 90 ns.
Constraints and Contract
- Keep the ten output names and the six functions with their arguments.
- The five outputs of the oldest stamp are read only while
ts_countis not 0. - The outputs are read after
tsu_resetand after everytsu_tick. - The simulator here does not accept
break. Stop a loop with a done flag in its condition. - Declare variables at the top of a function, not inside a loop.
before,cellanduseare reserved words. - You may add helper functions and module-level variables.
- Off limits: the names
ref_tsu,rng,tb,root, names that begin withfx_and the modules of the bench.
Hints
Hint 1
Build the clock alone first. Step it to 999,999,996 ns. What must it show two ticks later?
Hint 2
A receive stamp enters two ticks after its level was sampled. Which clock reading does it need? Where is that reading by then?
Hint 3
What if the rate changed in those two ticks, or a step came? What if the level changed again?
Hint 4
Write down the order of one tick. Which of your variables must still hold its old value when each part runs?
What the grader checks: Every row runs the answer beside a private model. It compares the clock, ts_count, ts_lost and the oldest stamp after every tick and every tsu_reset. The four visible rows show the oldest stamp at the end, with values from an independent JavaScript model. Three hidden rows are scripted. One has the clock around a full second, and one has requests in the same ticks as frames. One has runs of tx_en that start with octets one bit away from 55h and D5h. Fourteen hidden rows are generated runs with a fixed seed. Their check value over all outputs also comes from the JavaScript model. Fourteen rows are random and change with every submission. A failed row names what its first wrong tick held and which output was wrong first. A stamp is compared when it becomes the oldest, so a wrong stamp may show a few ticks late. A stamp marks the Clause 90 point, the start of the SFD. The octet to the PTP point is not graded. The runs reach a full second and the end of the 48-bit range. They reach delays above four seconds, 255 lost stamps and more than 256 frames in a direction. Many rules are course choices. For the clock: the 16 fraction bits, seconds modulo 2^48, and the reading before the tick. For a stamp: the preamble rule, half the sum of the delays, and the tags. For the rest: the FIFO of four with ts_lost, the tick in which a request acts, and the order in a tick. Not graded: metastability, the MM parameter of the MAC Merge sublayer (90.4.3.1.1) and the SFD detection of the receive domain. The management registers of 45.2.1.175 to 45.2.1.177 are not graded either.
35 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 1 steps and the final capstone