aboutsummaryrefslogtreecommitdiffstats
path: root/rx/phsaligner.v
blob: 683ac502b3b6f6a3bab200ded8e8f702853d7eaa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
//////////////////////////////////////////////////////////////////////////////
//
//  Xilinx, Inc. 2010                 www.xilinx.com
//
//  XAPP xxx - TMDS serial stream phase aligner
//
//////////////////////////////////////////////////////////////////////////////
//
//  File name :       phasealigner.v
//
//  Description :     This module determines whether the Spartan-6 IOSERDES
//                    has validate the incoming TMDS data stream 
//                    
//
//  Note:             
//
//  Author :    Bob Feng 
//
//  Disclaimer: LIMITED WARRANTY AND DISCLAMER. These designs are
//              provided to you "as is". Xilinx and its licensors make and you
//              receive no warranties or conditions, express, implied,
//              statutory or otherwise, and Xilinx specifically disclaims any
//              implied warranties of merchantability, non-infringement,or
//              fitness for a particular purpose. Xilinx does not warrant that
//              the functions contained in these designs will meet your
//              requirements, or that the operation of these designs will be
//              uninterrupted or error free, or that defects in the Designs
//              will be corrected. Furthermore, Xilinx does not warrantor
//              make any representations regarding use or the results of the
//              use of the designs in terms of correctness, accuracy,
//              reliability, or otherwise.
//
//              LIMITATION OF LIABILITY. In no event will Xilinx or its
//              licensors be liable for any loss of data, lost profits,cost
//              or procurement of substitute goods or services, or for any
//              special, incidental, consequential, or indirect damages
//              arising from the use or operation of the designs or
//              accompanying documentation, however caused and on any theory
//              of liability. This limitation will apply even if Xilinx
//              has been advised of the possibility of such damage. This
//              limitation shall apply not-withstanding the failure of the
//              essential purpose of any limited remedies herein.
//
//  Copyright © 2006 Xilinx, Inc.
//  All rights reserved
//
//////////////////////////////////////////////////////////////////////////////
// Modifications copyright (c) 2011, Andrew "bunnie" Huang
// All rights reserved as permitted by law.
//
// Redistribution and use in source and binary forms, with or without modification, 
// are permitted provided that the following conditions are met:
//
//  * Redistributions of source code must retain the above copyright notice, 
//    this list of conditions and the following disclaimer.
//  * Redistributions in binary form must reproduce the above copyright notice, 
//    this list of conditions and the following disclaimer in the documentation and/or 
//    other materials provided with the distribution.
//
//    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY 
//    EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 
//    OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT 
//    SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 
//    INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 
//    LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 
//    PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 
//    WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
//    ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
//    POSSIBILITY OF SUCH DAMAGE.
//
//////////////////////////////////////////////////////////////////////////////
//
`timescale 1 ns / 1ps
`define SIMULATION 1

// here's what this module does.
// 1. searches for an incoming control token
// 2. when it finds one, makes sure it sticks around at least CTKNCNTWD.
// 3. if it can't find one within SRCHTIMERWD, try a new bitslip and/or gearbox setting.
//    keep on trying bitslips and gear settings over and over again ewvery SRCHTIMERWD
//    until we've found a CTKNCNTWD.
// 4. once the token has stuck around for the pre-determined number of cycles, repeat
//    the search to confirm it. (this function is degenerate in this implementation)
// 5. once setting has been confirmed, assume it never changes.

module phsaligner # (
  parameter OPENEYE_CNT_WD = 3, // valid open eye counter width
//  parameter CTKNCNTWD      = 7, // Control Token Counter Width (dvi spec is 128)
  parameter CTKNCNTWD      = 3, // Control Token Counter Width (hdmi spec is 8)
  parameter SRCHTIMERWD    = 12 // Idle Timer Width
)
(
  input  wire       rst,
  input  wire       clk,
  input  wire [9:0] sdata,       // 10 bit serial stream sync. to clk
  output reg        flipgear,
  output reg        bitslip,
  output reg        psaligned    // FSM output
);  

  parameter CTRLTOKEN0 = 10'b1101010100;
  parameter CTRLTOKEN1 = 10'b0010101011;
  parameter CTRLTOKEN2 = 10'b0101010100;
  parameter CTRLTOKEN3 = 10'b1010101011;

  ///////////////////////////////////////////////////////
  // Control Token Detection
  ///////////////////////////////////////////////////////
  reg rcvd_ctkn, rcvd_ctkn_q;
  reg blnkbgn; //blank period begins

  always @ (posedge clk) begin
    rcvd_ctkn <=#1 ((sdata == CTRLTOKEN0) || (sdata == CTRLTOKEN1) || 
                    (sdata == CTRLTOKEN2) || (sdata == CTRLTOKEN3));

    rcvd_ctkn_q <=#1 rcvd_ctkn;
    blnkbgn <=#1 !rcvd_ctkn_q & rcvd_ctkn;
  end

  /////////////////////////////////////////////////////
  // Control Token Search Timer
  //
  // DVI 1.0 Spec. says periodic blanking should start
  // no less than every 50ms or 20HZ
  // 2^24 of 74.25MHZ cycles is about 200ms
  /////////////////////////////////////////////////////
  reg [(SRCHTIMERWD-1):0] ctkn_srh_timer;
  reg ctkn_srh_rst;  //FSM output

  always @ (posedge clk) begin
    if (ctkn_srh_rst)
      ctkn_srh_timer <=#1 {SRCHTIMERWD{1'b0}};
    else
      ctkn_srh_timer <=#1 ctkn_srh_timer + 1'b1;
  end

  reg ctkn_srh_tout;
  always @ (posedge clk) begin
    ctkn_srh_tout <=#1 (ctkn_srh_timer == {SRCHTIMERWD{1'b1}});
  end

  /////////////////////////////////////////////////////
  // Contorl Token Event Counter
  //
  // DVI 1.0 Spec. says the minimal blanking period
  // is at least 128 pixels long in order to achieve
  // synchronization
  //
  // HDMI reduces this to as little as 8
  /////////////////////////////////////////////////////
  reg [(CTKNCNTWD-1):0] ctkn_counter;
  reg ctkn_cnt_rst; //FSM output

  always @ (posedge clk) begin
    if(ctkn_cnt_rst)
      ctkn_counter <=#1 {CTKNCNTWD{1'b0}};
    else
      ctkn_counter <=#1 ctkn_counter + 1'b1;
  end

  reg ctkn_cnt_tout;
  always @ (posedge clk) begin
    ctkn_cnt_tout <=#1 (ctkn_counter == {CTKNCNTWD{1'b1}});
  end

  //////////////////////////////////////////////////////////
  // Below starts the phase alignment state machine
  //////////////////////////////////////////////////////////
  parameter INIT        = 6'b1 << 0;
  parameter SEARCH      = 6'b1 << 1;  // Searching for control tokens
  parameter BITSLIP     = 6'b1 << 2;
  parameter RCVDCTKN    = 6'b1 << 3;  // Received at one Control Token and check for more
  parameter BLNKPRD     = 6'b1 << 4;
  parameter PSALGND     = 6'b1 << 5;  // Phase alignment achieved
  parameter nSTATES     = 6;

  reg [(nSTATES-1):0] cstate = {{(nSTATES-1){1'b0}}, 1'b1};  //current and next states
  reg [(nSTATES-1):0] nstate;
  
`ifdef SIMULATION
  // synthesis translate_off
  reg  [8*20:1] state_ascii = "INIT               ";
  always @(cstate) begin
    if      (cstate == INIT     ) state_ascii <= "INIT               ";
    else if (cstate == SEARCH   ) state_ascii <= "SEARCH             ";
    else if (cstate == BITSLIP  ) state_ascii <= "BITSLIP            ";
    else if (cstate == RCVDCTKN ) state_ascii <= "RCVDCTKN           ";
    else if (cstate == BLNKPRD  ) state_ascii <= "BLNKPRD            ";
    else state_ascii                          <= "PSALGND            ";
  end
  // synthesis translate_on
`endif

  always @ (posedge clk or posedge rst) begin
    if (rst)
      cstate <= INIT;
    else
      cstate <=#1 nstate;
  end

  //////////////////////////////////////////////////////////
  // Counter counts number of blank period detected
  // in order to qualify the bitslip position
  //////////////////////////////////////////////////////////
  parameter BLNKPRD_CNT_WD = 1;

  reg [(BLNKPRD_CNT_WD-1):0] blnkprd_cnt = {BLNKPRD_CNT_WD{1'b0}};

  always @ (*) begin
    case (cstate) //synthesis parallel_case full_case
      INIT: begin
        nstate = (ctkn_srh_tout) ? SEARCH : INIT;
      end

      SEARCH: begin
        if(blnkbgn)
          nstate = RCVDCTKN;
        else
          nstate = (ctkn_srh_tout) ? BITSLIP : SEARCH;
      end

      BITSLIP: begin
        nstate = SEARCH;
      end

      RCVDCTKN: begin
        if(rcvd_ctkn)
          nstate = (ctkn_cnt_tout) ? BLNKPRD : RCVDCTKN;
        else
          nstate = SEARCH;
      end

      BLNKPRD: begin
        nstate = (blnkprd_cnt == {BLNKPRD_CNT_WD{1'b1}}) ? PSALGND : SEARCH;
      end

      PSALGND: begin
        nstate = PSALGND; // Phase aligned so hang around here
      end
    endcase
  end

  reg [2:0] bitslip_cnt;

  always @ (posedge clk or posedge rst) begin
    if(rst) begin
      psaligned          <=#1 1'b0; //phase alignment success flag
      bitslip            <=#1 1'b0;
      ctkn_srh_rst       <=#1 1'b1; //control token search timer reset
      ctkn_cnt_rst       <=#1 1'b1; //control token counter reset

      bitslip            <=#1 1'b0;
      bitslip_cnt        <=#1 3'h0;
      flipgear           <=#1 1'b0;
      blnkprd_cnt        <=#1 {BLNKPRD_CNT_WD{1'b0}};
    end else begin
      case (cstate) // synthesis parallel_case full_case
        INIT: begin
          ctkn_srh_rst       <=#1 1'b0;
          ctkn_cnt_rst       <=#1 1'b1;
          bitslip            <=#1 1'b0;
          psaligned          <=#1 1'b0;

          bitslip            <=#1 1'b0;
          bitslip_cnt        <=#1 3'h0;
          flipgear           <=#1 1'b0;
          blnkprd_cnt        <=#1 {BLNKPRD_CNT_WD{1'b0}};
        end

        SEARCH: begin
          ctkn_srh_rst       <=#1 1'b0;
          ctkn_cnt_rst       <=#1 1'b1;
          bitslip            <=#1 1'b0;
          psaligned          <=#1 1'b0;
        end

        BITSLIP: begin
          ctkn_srh_rst       <=#1 1'b1;

          bitslip            <=#1 1'b1;
          bitslip_cnt        <=#1 bitslip_cnt + 1'b1;
          flipgear           <=#1 bitslip_cnt[2]; //bitslip has toggled for 4 times
        end

        RCVDCTKN: begin
          ctkn_srh_rst       <=#1 1'b0;
          ctkn_cnt_rst       <=#1 1'b0;
        end

        BLNKPRD: begin
          blnkprd_cnt        <=#1 blnkprd_cnt + 1'b1;
        end

        PSALGND: begin
          psaligned <=#1 1'b1;
        end
      endcase
    end
  end

endmodule