2022-05-23 20:03:10 -05:00
|
|
|
// SPDX-License-Identifier: AGPL-3.0-Only
|
|
|
|
/*
|
|
|
|
* Copyright (C) 2022 Sean Anderson <seanga2@gmail.com>
|
|
|
|
*
|
|
|
|
* This roughly follows the design of XAPP225. However, we use a 2x rate DDR
|
|
|
|
* clock instead of two clocks 90 degrees out of phase. Yosys/nextpnr cannot
|
|
|
|
* guarantee the phase relationship of any clocks, even those from the same
|
2022-10-30 20:48:56 -05:00
|
|
|
* PLL. Because of this, we assume that clk_250 and clk_125 are unrelated.
|
2022-05-23 20:03:10 -05:00
|
|
|
*/
|
|
|
|
|
|
|
|
`include "common.vh"
|
|
|
|
`include "io.vh"
|
|
|
|
|
|
|
|
`timescale 1ns/1ps
|
|
|
|
|
2022-10-30 20:48:56 -05:00
|
|
|
module pmd_dp83223_rx (
|
|
|
|
input clk_250,
|
|
|
|
input clk_125,
|
2022-05-23 20:03:10 -05:00
|
|
|
|
|
|
|
input signal_detect,
|
2022-10-16 17:27:32 -05:00
|
|
|
input indicate_data,
|
2022-05-23 20:03:10 -05:00
|
|
|
|
|
|
|
/* PMD */
|
2022-08-06 14:36:27 -05:00
|
|
|
output signal_status,
|
2022-10-16 17:27:32 -05:00
|
|
|
output reg [1:0] rx_data,
|
|
|
|
output reg [1:0] rx_data_valid
|
2022-05-23 20:03:10 -05:00
|
|
|
);
|
|
|
|
|
|
|
|
reg [1:0] rx_p, rx_n;
|
2022-10-30 21:02:32 -05:00
|
|
|
reg [4:0] sd_delay;
|
2023-02-20 17:21:44 -06:00
|
|
|
initial sd_delay[4:0] = 5'b0;
|
2022-05-23 20:03:10 -05:00
|
|
|
|
|
|
|
`ifdef SYNTHESIS
|
|
|
|
SB_IO #(
|
|
|
|
.PIN_TYPE(`PIN_OUTPUT_NEVER | `PIN_INPUT_DDR),
|
2022-10-16 17:27:32 -05:00
|
|
|
) rx_data_pin (
|
|
|
|
.PACKAGE_PIN(indicate_data),
|
2022-10-30 20:48:56 -05:00
|
|
|
.INPUT_CLK(clk_250),
|
2022-05-23 20:03:10 -05:00
|
|
|
.D_IN_0(rx_p[0]),
|
|
|
|
.D_IN_1(rx_n[0])
|
|
|
|
);
|
|
|
|
`else
|
2022-10-30 20:48:56 -05:00
|
|
|
always @(posedge clk_250)
|
2022-10-16 17:27:32 -05:00
|
|
|
rx_p[0] <= indicate_data;
|
2022-05-23 20:03:10 -05:00
|
|
|
|
2022-10-30 20:48:56 -05:00
|
|
|
always @(negedge clk_250)
|
2022-10-16 17:27:32 -05:00
|
|
|
rx_n[0] <= indicate_data;
|
2022-05-23 20:03:10 -05:00
|
|
|
`endif
|
|
|
|
|
2022-08-06 14:36:27 -05:00
|
|
|
/*
|
|
|
|
* Delay signal status until the known good data has had a chance to
|
|
|
|
* make it through the pipeline. This isn't necessary for real hardware
|
|
|
|
* (since signal status is asserted long after we have good data), but
|
|
|
|
* it helps out during simulation. It also helps avoid metastability.
|
|
|
|
*/
|
2022-10-30 20:48:56 -05:00
|
|
|
always @(posedge clk_125)
|
2023-02-20 17:21:44 -06:00
|
|
|
sd_delay[4:0] <= { sd_delay[3:0], signal_detect };
|
2022-08-06 14:36:27 -05:00
|
|
|
|
2022-10-30 21:02:32 -05:00
|
|
|
assign signal_status = sd_delay[4];
|
2022-08-06 14:36:27 -05:00
|
|
|
|
2022-05-23 20:03:10 -05:00
|
|
|
/*
|
2022-10-30 20:48:56 -05:00
|
|
|
* Get things into the clk_250 domain so that we sample posedge before
|
2022-05-23 20:03:10 -05:00
|
|
|
* negedge. Without this we can have a negedge which happens before the
|
|
|
|
* posedge.
|
|
|
|
*/
|
2022-10-30 20:48:56 -05:00
|
|
|
always @(posedge clk_250) begin
|
2022-08-06 14:43:14 -05:00
|
|
|
rx_p[1] <= rx_p[0];
|
|
|
|
rx_n[1] <= rx_n[0];
|
2022-05-23 20:03:10 -05:00
|
|
|
end
|
|
|
|
|
2022-10-30 19:58:18 -05:00
|
|
|
reg [3:0] rx_a, rx_b, rx_c, rx_d;
|
2022-05-23 20:03:10 -05:00
|
|
|
|
2022-10-30 20:48:56 -05:00
|
|
|
/* Get everything in the clk_125 domain */
|
|
|
|
always @(posedge clk_125) begin
|
2022-05-23 20:03:10 -05:00
|
|
|
rx_a[0] <= rx_p[1];
|
|
|
|
rx_b[0] <= rx_n[1];
|
|
|
|
end
|
|
|
|
|
2022-10-30 20:48:56 -05:00
|
|
|
always @(negedge clk_125) begin
|
2022-05-23 20:03:10 -05:00
|
|
|
rx_c[0] <= rx_p[1];
|
|
|
|
rx_d[0] <= rx_n[1];
|
|
|
|
end
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Buffer things a bit. We wait a cycle to avoid metastability. After
|
2022-10-30 19:58:18 -05:00
|
|
|
* that, we need two cycles of history to detect edges, plus a final
|
|
|
|
* cycle to select from.
|
2022-05-23 20:03:10 -05:00
|
|
|
*/
|
2022-10-30 20:48:56 -05:00
|
|
|
always @(posedge clk_125) begin
|
2022-10-30 19:58:18 -05:00
|
|
|
rx_a[3:1] <= rx_a[2:0];
|
|
|
|
rx_b[3:1] <= rx_b[2:0];
|
|
|
|
rx_c[3:1] <= rx_c[2:0];
|
|
|
|
rx_d[3:1] <= rx_d[2:0];
|
2022-05-23 20:03:10 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
localparam A = 0;
|
|
|
|
localparam B = 1;
|
|
|
|
localparam C = 2;
|
|
|
|
localparam D = 3;
|
|
|
|
|
2022-10-16 16:46:04 -05:00
|
|
|
reg [1:0] state, state_next;
|
|
|
|
initial state = A;
|
|
|
|
reg valid, valid_next;
|
2022-10-30 19:58:18 -05:00
|
|
|
reg wraparound, wraparound_next;
|
2022-10-16 16:46:04 -05:00
|
|
|
initial valid = 0;
|
2022-10-16 17:27:32 -05:00
|
|
|
reg [1:0] rx_data_next, rx_data_valid_next;
|
2022-05-23 20:03:10 -05:00
|
|
|
reg [3:0] rx_r, rx_f;
|
|
|
|
|
|
|
|
always @(*) begin
|
2022-10-30 20:48:56 -05:00
|
|
|
rx_r = {
|
2022-05-23 20:03:10 -05:00
|
|
|
rx_a[1] & ~rx_a[2],
|
|
|
|
rx_b[1] & ~rx_b[2],
|
|
|
|
rx_c[1] & ~rx_c[2],
|
|
|
|
rx_d[1] & ~rx_d[2]
|
|
|
|
};
|
|
|
|
|
|
|
|
rx_f = {
|
|
|
|
~rx_a[1] & rx_a[2],
|
|
|
|
~rx_b[1] & rx_b[2],
|
|
|
|
~rx_c[1] & rx_c[2],
|
|
|
|
~rx_d[1] & rx_d[2]
|
|
|
|
};
|
|
|
|
|
|
|
|
state_next = state;
|
|
|
|
valid_next = 1;
|
2022-10-30 19:58:18 -05:00
|
|
|
wraparound_next = 0;
|
|
|
|
if (rx_r == 4'b1111 || rx_f == 4'b1111) begin
|
2022-05-23 20:03:10 -05:00
|
|
|
state_next = C;
|
2022-10-30 19:58:18 -05:00
|
|
|
end else if (rx_r == 4'b1000 || rx_f == 4'b1000) begin
|
2022-05-23 20:03:10 -05:00
|
|
|
state_next = D;
|
2022-10-30 19:58:18 -05:00
|
|
|
wraparound_next = state == A;
|
|
|
|
end else if (rx_r == 4'b1100 || rx_f == 4'b1100) begin
|
2022-05-23 20:03:10 -05:00
|
|
|
state_next = A;
|
2022-10-30 19:58:18 -05:00
|
|
|
wraparound_next = state == D;
|
|
|
|
end else if (rx_r == 4'b1110 || rx_f == 4'b1110) begin
|
2022-05-23 20:03:10 -05:00
|
|
|
state_next = B;
|
2022-10-30 19:58:18 -05:00
|
|
|
end else begin
|
2022-05-23 20:03:10 -05:00
|
|
|
valid_next = valid;
|
2022-10-30 19:58:18 -05:00
|
|
|
end
|
2022-05-23 20:03:10 -05:00
|
|
|
|
2022-08-06 14:36:27 -05:00
|
|
|
if (!signal_status) begin
|
|
|
|
state_next = A;
|
2022-05-23 20:03:10 -05:00
|
|
|
valid_next = 0;
|
2022-08-06 14:36:27 -05:00
|
|
|
end
|
2022-10-30 20:48:56 -05:00
|
|
|
|
2022-10-30 19:58:18 -05:00
|
|
|
rx_data_next[0] = rx_d[3];
|
2022-10-16 17:27:32 -05:00
|
|
|
rx_data_valid_next = 1;
|
2022-10-30 19:58:18 -05:00
|
|
|
case (state)
|
2022-05-23 20:03:10 -05:00
|
|
|
A: begin
|
2022-10-30 19:58:18 -05:00
|
|
|
rx_data_next[1] = rx_a[3];
|
|
|
|
rx_data_valid_next = !wraparound;
|
2022-05-23 20:03:10 -05:00
|
|
|
end
|
|
|
|
B: begin
|
2022-10-30 19:58:18 -05:00
|
|
|
rx_data_next[1] = rx_b[3];
|
2022-05-23 20:03:10 -05:00
|
|
|
end
|
|
|
|
C: begin
|
2022-10-30 19:58:18 -05:00
|
|
|
rx_data_next[1] = rx_c[3];
|
2022-05-23 20:03:10 -05:00
|
|
|
end
|
|
|
|
D: begin
|
2022-10-30 19:58:18 -05:00
|
|
|
rx_data_next[1] = rx_d[3];
|
|
|
|
if (wraparound) begin
|
|
|
|
rx_data_next[1] = rx_a[3];
|
2022-10-16 17:27:32 -05:00
|
|
|
rx_data_valid_next = 2;
|
2022-05-23 20:03:10 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
endcase
|
|
|
|
|
|
|
|
if (!valid_next)
|
2022-10-16 17:27:32 -05:00
|
|
|
rx_data_valid_next = 0;
|
2022-05-23 20:03:10 -05:00
|
|
|
end
|
|
|
|
|
2022-10-30 20:48:56 -05:00
|
|
|
always @(posedge clk_125) begin
|
2022-05-23 20:03:10 -05:00
|
|
|
state <= state_next;
|
|
|
|
valid <= valid_next;
|
2022-10-30 19:58:18 -05:00
|
|
|
wraparound <= wraparound_next;
|
2022-10-16 17:27:32 -05:00
|
|
|
rx_data <= rx_data_next;
|
|
|
|
rx_data_valid <= rx_data_valid_next;
|
2022-05-23 20:03:10 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
`ifndef SYNTHESIS
|
|
|
|
reg [255:0] state_text;
|
2023-01-09 19:31:39 -06:00
|
|
|
wire [13:0] delay;
|
2022-05-23 20:03:10 -05:00
|
|
|
|
|
|
|
always @(*) begin
|
|
|
|
case (state)
|
|
|
|
A: state_text = "A";
|
|
|
|
B: state_text = "B";
|
|
|
|
C: state_text = "C";
|
|
|
|
D: state_text = "D";
|
|
|
|
endcase
|
|
|
|
end
|
|
|
|
`endif
|
|
|
|
|
|
|
|
endmodule
|