link.v 71.3 KB
Newer Older
Alexey Grebenkin's avatar
Alexey Grebenkin committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
/*******************************************************************************
 * Module: link
 * Date: 2015-07-11  
 * Author: Alexey     
 * Description: sata link layer implementation
 *
 * Copyright (c) 2015 Elphel, Inc.
 * link.v is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * link.v file is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/> .
20 21 22 23 24 25 26 27 28 29 30 31 32
 *
 * Additional permission under GNU GPL version 3 section 7:
 * If you modify this Program, or any covered work, by linking or combining it
 * with independent modules provided by the FPGA vendor only (this permission
 * does not extend to any 3-rd party modules, "soft cores" or macros) under
 * different license terms solely for the purpose of generating binary "bitstream"
 * files and/or simulating the code, the copyright holders of this Program give
 * you the right to distribute the covered work without those independent modules
 * as long as the source code for them is available from the FPGA vendor free of
 * charge, and there is no dependence on any encrypted modules for simulating of
 * the combined code. This permission applies to you if the distributed code
 * contains all the components and scripts required to completely simulate it
 * with at least one of the Free Software programs.
Alexey Grebenkin's avatar
Alexey Grebenkin committed
33
 *******************************************************************************/
34 35
//`include "scrambler.v"
//`include "crc.v"
Alexey Grebenkin's avatar
Alexey Grebenkin committed
36
module link #(
37
    // 4 = dword. 4-bytes aligned data transfers TODO 2 = word - easy, 8 = qword - difficult
Andrey Filippov's avatar
Andrey Filippov committed
38
    parameter DATA_BYTE_WIDTH = 4,
39 40 41
`ifdef SIMULATION
    parameter ALIGNES_PERIOD =  10 // period of sending ALIGNp pairs
`else
Andrey Filippov's avatar
Andrey Filippov committed
42
    parameter ALIGNES_PERIOD =  252 // period of sending ALIGNp pairs
43
`endif    
Alexey Grebenkin's avatar
Alexey Grebenkin committed
44 45
)
(
46
    // TODO insert watchdogs
Alexey Grebenkin's avatar
Alexey Grebenkin committed
47 48 49
    input   wire    rst,
    input   wire    clk,

50
    // data inputs from transport layer
Alexey Grebenkin's avatar
Alexey Grebenkin committed
51 52 53 54 55
    // input data stream (if any data during OOB setting => ignored)
    input   wire    [DATA_BYTE_WIDTH*8 - 1:0] data_in,
    // in case of strange data aligments and size (1st mentioned @ doc, p.310, odd number of words case)
    // Actually, only last data bundle shall be masked, others are always valid.
    // Mask could be encoded into 3 bits instead of 4 for qword, but encoding+decoding aren't worth the bit
Andrey Filippov's avatar
Andrey Filippov committed
56 57 58 59 60
    
    input   wire    [DATA_BYTE_WIDTH/2 - 1:0] data_mask_in,  // TODO, for now not supported, all mask bits are assumed to be set
    output  wire    data_strobe_out,                         // buffer read strobe
    input   wire    data_last_in,                            // transaction's last data budle pulse
    input   wire    data_val_in,                             // read data is valid (if 0 while last pulse wasn't received => need to hold the line)
61
    // data outputs to transport layer
Andrey Filippov's avatar
Andrey Filippov committed
62 63 64 65 66
    output  wire    [DATA_BYTE_WIDTH*8 - 1:0] data_out,      // read data, same as related inputs
    output  wire    [DATA_BYTE_WIDTH/2 - 1:0] data_mask_out, // same thing - all 1s for now. TODO
    output  wire    data_val_out,                            // count every data bundle read by transport layer, even if busy flag is set
                                                             // let the transport layer handle oveflows by itself
    input   wire    data_busy_in,                            // transport layer tells if its inner buffer is almost full
67
    output  wire    data_last_out,
Andrey Filippov's avatar
Andrey Filippov committed
68 69 70 71 72 73
    input   wire    frame_req,                               // request for a new frame transition
// a little bit of overkill with the cound of response signals, think of throwing out 1 of them    
    output  wire    frame_busy,                              // LL tells back if it cant handle the request for now
    output  wire    frame_ack,                               // LL tells if the request is transmitting
    output  wire    frame_rej,                               // or if it was cancelled because of simultanious incoming transmission
    output  wire    frame_done_good,                         // Tell TL if the outcoming transaction is done and how it was done
74
    output  wire    frame_done_bad,
Andrey Filippov's avatar
Andrey Filippov committed
75 76 77 78 79
    output  wire    incom_start,                             // if started an incoming transaction
    output  wire    incom_done,                              // if incoming transition was completed
    output  wire    incom_invalidate,                        // if incoming transition had errors
    output  wire    incom_sync_escape,                       // particular type - got sync escape
    input   wire    incom_ack_good,                          // transport layer responds on a completion of a FIS
80 81
    input   wire    incom_ack_bad,                           // Reject frame even if it had good CRC (Bad will be responded automatically)
                                                             // It is OK to send extra incom_ack_bad from transport - it will be discarded
Andrey Filippov's avatar
Andrey Filippov committed
82 83 84 85
    input   wire    link_reset,                              // oob sequence is reinitiated and link now is not established or rxelecidle
    input   wire    sync_escape_req,                         // TL demands to brutally cancel current transaction
    output  wire    sync_escape_ack,                         // acknowlegement of a successful reception
    input   wire    incom_stop_req,                          // TL demands to stop current recieving session
Andrey Filippov's avatar
Andrey Filippov committed
86 87
    output          link_established,                        // received 3 back-to-back non-align primitives.
    output  reg     link_bad_crc,                            // got bad crc at EOF
Alexey Grebenkin's avatar
Alexey Grebenkin committed
88
    // inputs from phy
Andrey Filippov's avatar
Andrey Filippov committed
89
    input   wire    phy_ready,                               // phy is ready - link is established
Andrey Filippov's avatar
Andrey Filippov committed
90 91 92
    input   wire    [DATA_BYTE_WIDTH*8 - 1:0] phy_data_in,   // data-primitives stream from phy
    input   wire    [DATA_BYTE_WIDTH   - 1:0] phy_isk_in,    // charisk
    input   wire    [DATA_BYTE_WIDTH   - 1:0] phy_err_in,    // disperr | notintable
93 94
    // to phy
    output  wire    [DATA_BYTE_WIDTH*8 - 1:0] phy_data_out,
Andrey Filippov's avatar
Andrey Filippov committed
95 96 97
    output  wire    [DATA_BYTE_WIDTH   - 1:0] phy_isk_out,   // charisk
    // debug
    output                             [31:0] debug_out      //
Alexey Grebenkin's avatar
Alexey Grebenkin committed
98
);
99 100 101 102
`ifdef SIMULATION
    reg [639:0] HOST_LINK_TITLE; // to show human-readable state in the GTKWave
    reg  [31:0] HOST_LINK_DATA;
`endif
Andrey Filippov's avatar
Andrey Filippov committed
103

104 105 106 107
// latching data-primitives stream from phy
reg     [DATA_BYTE_WIDTH*8 - 1:0] phy_data_in_r;
reg     [DATA_BYTE_WIDTH   - 1:0] phy_isk_in_r; // charisk
reg     [DATA_BYTE_WIDTH   - 1:0] phy_err_in_r; // disperr | notintable
108 109 110 111 112 113 114
//one extra layer to process CONTp
reg     [DATA_BYTE_WIDTH*8 - 1:0] phy_data_in_r0;
reg     [DATA_BYTE_WIDTH   - 1:0] phy_isk_in_r0; // charisk
reg     [DATA_BYTE_WIDTH   - 1:0] phy_err_in_r0; // disperr | notintable

reg     [DATA_BYTE_WIDTH*8 - 1:0] last_not_cont_di; // last primitive dword, but not CONTp
reg                               rcv_junk;         // receiving CONTp junk data
Andrey Filippov's avatar
Andrey Filippov committed
115 116 117 118 119 120
wire                              is_non_cont_non_align_p_w;  // got primitive other than CONTp and ALIGNp (early by 1)
wire                              is_cont_p_w;                // got CONTp primitive  (early by 1)
wire                              is_align_p_w;               // got ALIGNp primitive  (early by 1)

//CONTp should pass ALIGNp

121

122
wire    frame_done;
123 124 125 126 127 128 129
// scrambled data
wire    [DATA_BYTE_WIDTH*8 - 1:0]   scrambler_out;
wire    dec_err; // doc, p.311 
// while receiving session shows crc check status
wire    crc_good;
wire    crc_bad;
// current crc
Andrey Filippov's avatar
Andrey Filippov committed
130 131 132 133 134 135
wire    [31:0] crc_dword;
// Removing - state_align is handled by OOB 
///reg                                link_established_r;                        // received 3 back-to-back non-align primitives.
///reg                          [1:0] non_align_cntr;                  
///assign link_established = link_established_r; 
assign link_established = phy_ready;
136
// send primitives variety count, including CRC and DATA as primitives
137
localparam  PRIM_NUM = 16; // 15;
138 139 140
wire    [PRIM_NUM - 1:0] rcvd_dword;    // shows current processing primitive (or just data dword)
wire                     dword_val;     // any valid primitive/data
wire                     dword_val_na;  // any valid primitive but ALIGNp
141
// list of bits of rcvd_dword
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164
localparam  CODE_DATA   = 0;  // DATA
localparam  CODE_CRC    = 1;  // CRC
localparam  CODE_SYNCP  = 2;  // SYNCp
localparam  CODE_ALIGNP = 3;  // ALIGNp PHY layer control
localparam  CODE_XRDYP  = 4;  // X_RDYp Transmission data ready
localparam  CODE_SOFP   = 5;  // SOFp Start of Frame
localparam  CODE_HOLDAP = 6;  // HOLDAp HOLD acknowledge
localparam  CODE_HOLDP  = 7;  // HOLDp Hold data transmission
localparam  CODE_EOFP   = 8;  // EOFp End Of Frame
localparam  CODE_WTRMP  = 9;  // WTRMp Wait for frame termination
localparam  CODE_RRDYP  = 10; // R_RDYp Receiver ready
localparam  CODE_IPP    = 11; // R_IPp - Reception in progress
localparam  CODE_DMATP  = 12; // DMATp - DMA terminate
localparam  CODE_OKP    = 13; // R_OKp - Reception with no error
localparam  CODE_ERRP   = 14; // R_ERRp - Reception with Error
localparam  CODE_CONTP  = 15; // CONTp - Continue repeating

// processing CONTp/junk, delaying everything by 1 clock 
always @ (posedge clk) begin
    phy_data_in_r0   <= phy_data_in;
    phy_isk_in_r0    <= phy_isk_in;
    phy_err_in_r0    <= phy_err_in;
    
Andrey Filippov's avatar
Andrey Filippov committed
165 166 167 168 169 170 171 172
    if (is_non_cont_non_align_p_w) last_not_cont_di <= phy_data_in_r0; // last_not_cont_di - primitive to repeat instead of junk
    
    if (rst || is_non_cont_non_align_p_w) rcv_junk <= 0;
    else if (is_cont_p_w)                 rcv_junk <= 1;
    
    if (is_cont_p_w || (rcv_junk && !(is_non_cont_non_align_p_w || is_align_p_w))) begin
        phy_data_in_r   <= last_not_cont_di;  // last non-cont/non-align primitive will be sent instead of junk
        phy_isk_in_r    <= 1;                 // it was always primitive (4'b0001)
173
    end else begin
Andrey Filippov's avatar
Andrey Filippov committed
174 175
        phy_data_in_r   <= phy_data_in_r0;   // data and ALIGNp will go through
        phy_isk_in_r    <= phy_isk_in_r0;    // data and ALIGNp will go through
176 177 178 179
    end
    phy_err_in_r    <= phy_err_in_r0;
    
end
180 181
// When switching from state_rcvr_shold to state_rcvr_data we need to know that it will be data 1 cycle ahead
wire                     next_will_be_data = !(is_cont_p_w || (rcv_junk && !(is_non_cont_non_align_p_w || is_align_p_w))) && !(|phy_isk_in_r0);
182

183 184 185 186
reg                      data_txing_r; // if there are still some data to transmit and the transaction wasn't cancelled
wire                     data_txing = data_txing_r & ~state_send_crc;
// does not work with ALIGNp pair
/*
Andrey Filippov's avatar
Andrey Filippov committed
187
always @ (posedge clk) begin
188
///    data_txing <= rst | (data_last_in & data_strobe_out | dword_val_na & rcvd_dword[CODE_DMATP]) ? 1'b0 : frame_req ? 1'b1 : data_txing;
Andrey Filippov's avatar
Andrey Filippov committed
189 190
    if (rst ||
         (data_last_in && data_strobe_out)  ||
191
         (dword_val_na && rcvd_dword[CODE_DMATP])) data_txing <= 0;
Andrey Filippov's avatar
Andrey Filippov committed
192
    else if (frame_req)                         data_txing <= 1;    
193 194 195 196 197
end   
*/
// Trying alternative, as SM sometimes got stuck in state_send_data, last was set 
// Make it safe
always @ (posedge clk) begin
198
///    data_txing <= rst | (data_last_in & data_strobe_out | dword_val_na & rcvd_dword[CODE_DMATP]) ? 1'b0 : frame_req ? 1'b1 : data_txing;
199 200 201 202 203 204 205 206 207
    if (rst)                                    data_txing_r <= 0;
    else if (frame_req)                         data_txing_r <= 1;    
    else if (state_send_crc)                    data_txing_r <= 0;
end   




 
Alexey Grebenkin's avatar
Alexey Grebenkin committed
208 209 210 211 212 213 214 215 216 217 218
// fsm
// states and transitions are taken from the doc, "Link Layer State Machine" chapter
// power mode states are not implemented. TODO insert them as an additional branch of fsm

// !!!IMPORTANT!!! If add/remove any states, dont forget to change this parameter value
localparam STATES_COUNT = 23;
// idle state
wire    state_idle;
reg     state_sync_esc;     // SyncEscape
reg     state_nocommerr;    // NoComErr
reg     state_nocomm;       // NoComm
Andrey Filippov's avatar
Andrey Filippov committed
219
reg     state_align;        // SendAlign - not used, handled by OOB
Alexey Grebenkin's avatar
Alexey Grebenkin committed
220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240
reg     state_reset;        // RESET
// tranmitter branch
reg     state_send_rdy;     // SendChkRdy
reg     state_send_sof;     // SendSOF
reg     state_send_data;    // SendData
reg     state_send_rhold;   // RcvrHold - hold initiated by current data reciever
reg     state_send_shold;   // SendHold - hold initiated by current data sender
reg     state_send_crc;     // SendCVC
reg     state_send_eof;     // SendEOF
reg     state_wait;         // Wait
// receiver branch
reg     state_rcvr_wait;    // RcvWaitFifo
reg     state_rcvr_rdy;     // RcvChkRdy
reg     state_rcvr_data;    // RcvData
reg     state_rcvr_rhold;   // Hold     - hold initiated by current data reciever
reg     state_rcvr_shold;   // RcvHold  - hold initiated by current data sender
reg     state_rcvr_eof;     // RcvEOF
reg     state_rcvr_goodcrc; // GoodCRC
reg     state_rcvr_goodend; // GoodEnd
reg     state_rcvr_badend;  // BadEnd

241 242 243 244 245 246 247
// handling single-cycle incom_ack_good/incom_ack_bad when they arrive at alignes_pair
reg     incom_ack_good_pend;
reg     incom_ack_bad_pend;
wire    incom_ack_good_or_pend = incom_ack_good || incom_ack_good_pend;
wire    incom_ack_bad_or_pend =  incom_ack_bad || incom_ack_bad_pend;


Alexey Grebenkin's avatar
Alexey Grebenkin committed
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
wire    set_sync_esc;
wire    set_nocommerr;
wire    set_nocomm;
wire    set_align;
wire    set_reset;
wire    set_send_rdy;
wire    set_send_sof;
wire    set_send_data;
wire    set_send_rhold;
wire    set_send_shold;
wire    set_send_crc;
wire    set_send_eof;
wire    set_wait;
wire    set_rcvr_wait;
wire    set_rcvr_rdy;
wire    set_rcvr_data;
wire    set_rcvr_rhold;
wire    set_rcvr_shold;
wire    set_rcvr_eof;
wire    set_rcvr_goodcrc;
wire    set_rcvr_goodend;
wire    set_rcvr_badend;
                            
wire    clr_sync_esc;
wire    clr_nocommerr;
wire    clr_nocomm;
wire    clr_align;
wire    clr_reset;
wire    clr_send_rdy;
wire    clr_send_sof;
wire    clr_send_data;
wire    clr_send_rhold;
wire    clr_send_shold;
wire    clr_send_crc;
wire    clr_send_eof;
wire    clr_wait;
wire    clr_rcvr_wait;
wire    clr_rcvr_rdy;
wire    clr_rcvr_data;
wire    clr_rcvr_rhold;
wire    clr_rcvr_shold;
wire    clr_rcvr_eof;
wire    clr_rcvr_goodcrc;
wire    clr_rcvr_goodend;
wire    clr_rcvr_badend;

assign state_idle = ~state_sync_esc
Alexey Grebenkin's avatar
Alexey Grebenkin committed
295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315
                  & ~state_nocommerr
                  & ~state_nocomm
                  & ~state_align
                  & ~state_reset
                  & ~state_send_rdy
                  & ~state_send_sof
                  & ~state_send_data
                  & ~state_send_rhold
                  & ~state_send_shold
                  & ~state_send_crc
                  & ~state_send_eof
                  & ~state_wait
                  & ~state_rcvr_wait
                  & ~state_rcvr_rdy
                  & ~state_rcvr_data
                  & ~state_rcvr_rhold
                  & ~state_rcvr_shold
                  & ~state_rcvr_eof
                  & ~state_rcvr_goodcrc
                  & ~state_rcvr_goodend
                  & ~state_rcvr_badend;
Alexey Grebenkin's avatar
Alexey Grebenkin committed
316

317
// got an escaping primitive = request to cancel the transmission
318 319 320 321 322 323 324 325 326 327 328 329
// may be 1 cycle, need to extend over alignes_pair
//wire    got_escape_w;
//reg     got_escape_pend;
//wire    got_escape = got_escape_w || got_escape_pend;
//assign got_escape_w = dword_val & rcvd_dword[CODE_SYNCP];
wire got_escape = dword_val & rcvd_dword[CODE_SYNCP]; // can wait over alignes pair
reg     sync_escape_req_r;  // ahci sends 1 single-clock pulse, it may hit alignes_pair
always @ (posedge clk) begin
//    got_escape_pend <= alignes_pair && (got_escape_w || got_escape_pend);
    sync_escape_req_r <= alignes_pair && (sync_escape_req || sync_escape_req_r);
end
//sync_escape_req
Andrey Filippov's avatar
Andrey Filippov committed
330 331 332 333 334

// escaping is done
assign  sync_escape_ack = state_sync_esc;


335
reg           alignes_pair;   // pauses every state go give a chance to insert 2 align primitives on a line at least every 256 dwords due to spec
Andrey Filippov's avatar
Andrey Filippov committed
336 337
//wire    alignes_pair_0; // time for 1st align primitive
//wire    alignes_pair_1; // time for 2nd align primitive
338 339
reg     [8:0] alignes_timer;

Andrey Filippov's avatar
Andrey Filippov committed
340 341 342 343 344 345
///assign  alignes_pair_0 = alignes_timer == 9'd252;
///assign  alignes_pair_1 = alignes_timer == 9'd253;
///assign  alignes_pair_0 = alignes_timer == 9'd254;
///assign  alignes_pair_1 = alignes_timer == 9'd255;
///always @ (posedge clk)
///    alignes_timer <= rst | alignes_pair_1 | state_reset ? 9'h0 : alignes_timer + 1'b1;
346 347 348



Andrey Filippov's avatar
Andrey Filippov committed
349 350 351
//select_prim[CODE_ALIGNP]
//ALIGNES_PERIOD
reg    alignes_pair_0; // time for 1st align primitive
352
//reg    alignes_pair_1; // time for 2nd align primitive
Andrey Filippov's avatar
Andrey Filippov committed
353 354

always @ (posedge clk) begin
Andrey Filippov's avatar
Andrey Filippov committed
355 356 357
///    if (!link_established_r || select_prim[CODE_ALIGNP]) alignes_timer <= ALIGNES_PERIOD;
    if (!phy_ready || select_prim[CODE_ALIGNP]) alignes_timer <= ALIGNES_PERIOD;
    else                                        alignes_timer <= alignes_timer -1;
Andrey Filippov's avatar
Andrey Filippov committed
358
    alignes_pair_0 <= alignes_timer == 0;
359 360 361 362
//    alignes_pair_1 <= alignes_pair_0;
    
    alignes_pair <= phy_ready && ((alignes_timer == 0) || alignes_pair_0);
    
Andrey Filippov's avatar
Andrey Filippov committed
363
end
Andrey Filippov's avatar
Andrey Filippov committed
364
///assign  alignes_pair   = link_established_r && (alignes_pair_0 | alignes_pair_1);
365
// assign  alignes_pair   = phy_ready && (alignes_pair_0 | alignes_pair_1);
366

Andrey Filippov's avatar
Andrey Filippov committed
367 368
always @ (posedge clk) begin
    link_bad_crc <= state_rcvr_eof & crc_bad;
369 370 371 372 373 374 375 376
    
    if      (incom_ack_good)                             incom_ack_good_pend <= 1;
//    else if (!state_rcvr_goodend && !state_rcvr_goodcrc) incom_ack_good_pend <= 0;
    else if (!state_rcvr_goodcrc)                        incom_ack_good_pend <= 0;
    
    if      (incom_ack_bad)                              incom_ack_bad_pend <= 1;
//    else if (!state_rcvr_badend && !state_rcvr_goodcrc)  incom_ack_bad_pend <= 0; // didn't like it even with good crc
    else if (!state_rcvr_goodcrc)                        incom_ack_bad_pend <= 0; // didn't like it even with good crc
Andrey Filippov's avatar
Andrey Filippov committed
377
end
378 379

// Whole transitions table, literally from doc pages 311-328
380
assign  set_sync_esc        = sync_escape_req || sync_escape_req_r; // extended over alignes_pair
381 382
assign  set_nocommerr       = ~phy_ready & ~state_nocomm & ~state_reset;
assign  set_nocomm          = state_nocommerr;
Andrey Filippov's avatar
Andrey Filippov committed
383 384 385
///assign  set_align           = state_reset       & ~link_reset;
///assign  set_align           = state_reset       & ~link_reset & rcvd_dword[CODE_ALIGNP];
assign  set_align  = 0; // never, as this state is handled by OOB
386
assign  set_reset           = link_reset;
387

Alexey Grebenkin's avatar
Alexey Grebenkin committed
388
assign  set_send_rdy        = state_idle        & frame_req;
389 390 391

assign  set_send_sof        = state_send_rdy    & phy_ready  &                                dword_val      &  rcvd_dword[CODE_RRDYP];

Alexey Grebenkin's avatar
Alexey Grebenkin committed
392
assign  set_send_data       = state_send_sof    & phy_ready 
393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409
//                            | state_send_rhold  & data_txing & ~dec_err &                     dword_val_na & ~rcvd_dword[CODE_HOLDP] & ~rcvd_dword[CODE_SYNCP] & ~rcvd_dword[CODE_DMATP]
//                            | state_send_shold  & data_txing &  data_val_in &                 dword_val_na & ~rcvd_dword[CODE_HOLDP] & ~rcvd_dword[CODE_SYNCP];
                            | state_send_rhold  & data_txing & ~dec_err &                     dword_val_na  & ~rcvd_dword[CODE_HOLDP] & ~rcvd_dword[CODE_SYNCP] & ~rcvd_dword[CODE_DMATP]
                            | state_send_shold  & data_txing &  data_val_in &                 dword_val_na  & ~rcvd_dword[CODE_HOLDP] & ~rcvd_dword[CODE_SYNCP];

assign  set_send_rhold      = state_send_data   & data_txing &  data_val_in & ~data_last_in & dword_val    &  rcvd_dword[CODE_HOLDP]
                            | state_send_shold  & data_txing &  data_val_in &                 dword_val    &  rcvd_dword[CODE_HOLDP];
                            
assign  set_send_shold      = state_send_data   & data_txing & ~data_val_in &                 dword_val    & ~rcvd_dword[CODE_SYNCP];

assign  set_send_crc        = state_send_data   & data_txing &  data_val_in &  data_last_in & dword_val    & ~rcvd_dword[CODE_SYNCP] 
                            | state_send_data   &                                             dword_val    &  rcvd_dword[CODE_DMATP];
                            
assign  set_send_eof        = state_send_crc    & phy_ready &                                 dword_val    & ~rcvd_dword[CODE_SYNCP];

assign  set_wait            = state_send_eof    & phy_ready &                                 dword_val    & ~rcvd_dword[CODE_SYNCP];

Alexey Grebenkin's avatar
Alexey Grebenkin committed
410
// receiver's branch
411 412 413 414 415 416
assign  set_rcvr_wait       = state_idle        & dword_val    &  rcvd_dword[CODE_XRDYP]
                            | state_send_rdy    & dword_val    &  rcvd_dword[CODE_XRDYP];
                            
assign  set_rcvr_rdy        = state_rcvr_wait   & dword_val    &  rcvd_dword[CODE_XRDYP]  & ~data_busy_in;

assign  set_rcvr_data       = state_rcvr_rdy    & dword_val    &  rcvd_dword[CODE_SOFP]
417 418 419
//                            | state_rcvr_rhold  & dword_val_na & ~rcvd_dword[CODE_HOLDP] & ~rcvd_dword[CODE_EOFP] & ~rcvd_dword[CODE_SYNCP] & ~data_busy_in
                            | state_rcvr_rhold  & next_will_be_data & ~data_busy_in
//                            | state_rcvr_shold  & dword_val_na & ~rcvd_dword[CODE_HOLDP] & ~rcvd_dword[CODE_EOFP] & ~rcvd_dword[CODE_SYNCP];
420 421
                            | state_rcvr_shold  & next_will_be_data  // So it will not be align
                            | state_rcvr_data   & next_will_be_data; // to skip over single-cycle CODE_HOLDP
422
//next_will_be_data                            
423 424
assign  set_rcvr_rhold      = state_rcvr_data   & dword_val    &  rcvd_dword[CODE_DATA]  &  data_busy_in;

425 426
assign  set_rcvr_shold      = state_rcvr_data   & dword_val    &  (rcvd_dword[CODE_HOLDP] & ~next_will_be_data)
                            | state_rcvr_rhold  & dword_val    &  (rcvd_dword[CODE_HOLDP] & ~next_will_be_data) & ~data_busy_in;
427 428 429 430 431
                            
assign  set_rcvr_eof        = state_rcvr_data   & dword_val    &  rcvd_dword[CODE_EOFP]
                            | state_rcvr_rhold  & dword_val    &  rcvd_dword[CODE_EOFP]
                            | state_rcvr_shold  & dword_val    &  rcvd_dword[CODE_EOFP];
                            
Alexey Grebenkin's avatar
Alexey Grebenkin committed
432
assign  set_rcvr_goodcrc    = state_rcvr_eof    & crc_good;
433

434
assign  set_rcvr_goodend    = state_rcvr_goodcrc& incom_ack_good_or_pend; // incom_ack_good; // may arrive at aligns_pair
435

436 437 438
assign  set_rcvr_badend     = state_rcvr_data   & dword_val    &  rcvd_dword[CODE_WTRMP]     // Missed EOF
                            | state_rcvr_eof    & crc_bad                                    // Got bad CRC
                            | state_rcvr_goodcrc& incom_ack_bad_or_pend; // incom_ack_bad;   // Transport didn't like it (may arrive at aligns_pair)
439

440 441 442
assign  clr_sync_esc        = set_nocommerr | set_reset                | dword_val & (rcvd_dword[CODE_RRDYP] | rcvd_dword[CODE_SYNCP]);
assign  clr_nocommerr       =                 set_reset                | set_nocomm;
assign  clr_nocomm          =                 set_reset                | set_align;
Andrey Filippov's avatar
Andrey Filippov committed
443 444 445
///assign  clr_align        = set_nocommerr | set_reset                | phy_ready;
///assign  clr_align           = set_nocommerr | set_reset                | link_established_r; // Not phy_ready !!!
assign  clr_align           = 0; // never - this state is handled in OOB
446
assign  clr_reset           =                                           ~link_reset;
Andrey Filippov's avatar
Andrey Filippov committed
447
///assign  clr_reset           =                                           set_align;
448
assign  clr_send_rdy        = set_nocommerr | set_reset | set_sync_esc | set_send_sof | set_rcvr_wait;
449 450 451 452 453 454 455
assign  clr_send_sof        = set_nocommerr | set_reset | set_sync_esc | set_send_data; // | got_escape;
assign  clr_send_data       = set_nocommerr | set_reset | set_sync_esc | set_send_rhold | set_send_shold | set_send_crc; // | got_escape;
assign  clr_send_rhold      = set_nocommerr | set_reset | set_sync_esc | set_send_data | set_send_crc; //  | got_escape;
assign  clr_send_shold      = set_nocommerr | set_reset | set_sync_esc | set_send_data | set_send_rhold | set_send_crc; //  | got_escape;
assign  clr_send_crc        = set_nocommerr | set_reset | set_sync_esc | set_send_eof; // | got_escape;
assign  clr_send_eof        = set_nocommerr | set_reset | set_sync_esc | set_wait; //  | got_escape;
assign  clr_wait            = set_nocommerr | set_reset | set_sync_esc | frame_done; // | got_escape;
456
/* 
457 458
assign  clr_rcvr_wait       = set_nocommerr | set_reset | set_sync_esc | set_rcvr_rdy | dword_val_na & ~rcvd_dword[CODE_XRDYP];
assign  clr_rcvr_rdy        = set_nocommerr | set_reset | set_sync_esc | set_rcvr_data | dword_val_na & ~rcvd_dword[CODE_XRDYP] & ~rcvd_dword[CODE_SOFP];
459 460 461
assign  clr_rcvr_data       = set_nocommerr | set_reset | set_sync_esc | set_rcvr_rhold | set_rcvr_shold | set_rcvr_eof | set_rcvr_badend | got_escape;
assign  clr_rcvr_rhold      = set_nocommerr | set_reset | set_sync_esc | set_rcvr_data | set_rcvr_eof | set_rcvr_shold | got_escape;
assign  clr_rcvr_shold      = set_nocommerr | set_reset | set_sync_esc | set_rcvr_data | set_rcvr_eof | got_escape;
Alexey Grebenkin's avatar
Alexey Grebenkin committed
462 463
assign  clr_rcvr_eof        = set_nocommerr | set_reset | set_sync_esc | set_rcvr_goodcrc | set_rcvr_badend;
assign  clr_rcvr_goodcrc    = set_nocommerr | set_reset | set_sync_esc | set_rcvr_goodend | set_rcvr_badend | got_escape;
464
*/
465 466 467 468 469
assign  clr_rcvr_wait       = set_nocommerr | set_reset | set_sync_esc /*| set_rcvr_rdy */ | (dword_val_na & ~rcvd_dword[CODE_XRDYP]);
assign  clr_rcvr_rdy        = set_nocommerr | set_reset | set_sync_esc /*| set_rcvr_data */ | (dword_val_na & ~rcvd_dword[CODE_XRDYP] & ~rcvd_dword[CODE_SOFP]);
assign  clr_rcvr_data       = set_nocommerr | set_reset | set_sync_esc /*| set_rcvr_rhold | set_rcvr_shold | set_rcvr_eof */ | set_rcvr_badend; // | got_escape;
assign  clr_rcvr_rhold      = set_nocommerr | set_reset | set_sync_esc /*| set_rcvr_data | set_rcvr_eof | set_rcvr_shold */; //  | got_escape;
assign  clr_rcvr_shold      = set_nocommerr | set_reset | set_sync_esc /*| set_rcvr_data | set_rcvr_eof */; //  | got_escape;
470
assign  clr_rcvr_eof        = set_nocommerr | set_reset | set_sync_esc /*|set_rcvr_goodcrc | set_rcvr_badend*/;
471
assign  clr_rcvr_goodcrc    = set_nocommerr | set_reset | set_sync_esc /*set_rcvr_goodend | set_rcvr_badend |*/; // | got_escape;
472

473 474
assign  clr_rcvr_goodend    = set_nocommerr | set_reset | set_sync_esc; // | got_escape; // can be 1 cycle only
assign  clr_rcvr_badend     = set_nocommerr | set_reset | set_sync_esc; // | got_escape;
475 476 477 478

// the only truely asynchronous transaction between states is -> state_ reset. It shall not be delayed by sending alignes
// Luckily, while in that state, the line is off, so we dont need to care about merging alignes and state-bounded primitives
// Others transitions are straightforward
Alexey Grebenkin's avatar
Alexey Grebenkin committed
479
always @ (posedge clk)
480
begin
481 482 483
    state_sync_esc      <= (state_sync_esc     | set_sync_esc     & ~alignes_pair) & ~(clr_sync_esc & ~alignes_pair)                    & ~rst;
    state_nocommerr     <= (state_nocommerr    | set_nocommerr    & ~alignes_pair) & ~(clr_nocommerr & ~alignes_pair)                   & ~rst;
    state_nocomm        <= (state_nocomm       | set_nocomm       & ~alignes_pair) & ~(clr_nocomm & ~alignes_pair)                      & ~rst;
Andrey Filippov's avatar
Andrey Filippov committed
484
    // state_align is not used, it is handled by OOB
485 486 487 488 489 490 491 492 493 494
    state_align         <= (state_align        | set_align        & ~alignes_pair) & ~(clr_align & ~alignes_pair)                       & ~rst;
    state_reset         <= (state_reset        | set_reset                       ) & ~ clr_reset                                        & ~rst;
    state_send_rdy      <= (state_send_rdy     | set_send_rdy     & ~alignes_pair) & ~(clr_send_rdy & ~alignes_pair)                    & ~rst;
    state_send_sof      <= (state_send_sof     | set_send_sof     & ~alignes_pair) & ~(got_escape | (clr_send_sof & ~alignes_pair))     & ~rst;
    state_send_data     <= (state_send_data    | set_send_data    & ~alignes_pair) & ~(got_escape | (clr_send_data & ~alignes_pair))    & ~rst;
    state_send_rhold    <= (state_send_rhold   | set_send_rhold   & ~alignes_pair) & ~(got_escape | (clr_send_rhold & ~alignes_pair))   & ~rst;
    state_send_shold    <= (state_send_shold   | set_send_shold   & ~alignes_pair) & ~(got_escape | (clr_send_shold & ~alignes_pair))   & ~rst;
    state_send_crc      <= (state_send_crc     | set_send_crc     & ~alignes_pair) & ~(got_escape | (clr_send_crc & ~alignes_pair))     & ~rst;
    state_send_eof      <= (state_send_eof     | set_send_eof     & ~alignes_pair) & ~(got_escape | (clr_send_eof & ~alignes_pair))     & ~rst;
    state_wait          <= (state_wait         | set_wait         & ~alignes_pair) & ~(got_escape | (clr_wait & ~alignes_pair))         & ~rst;
495 496 497 498 499 500 501 502 503 504 505 506
    // Andrey: most receiver states can not wait for transmitting aligns_pair. What host sends in this states matters when confirmed by the device
    // So it seems OK if alignes_pair will just overwrite whatever host was going to send in these state.
    // Care should be taken only for transitions between these states and others (transmit) that need to wait for alignes_pair to finish
    // set_* are considered fast (no wait), clr_* - slow (to non-receive states), next opeartors use OR-ed "set_*" in immediate transitions
    // to other states, clr_* - to other states
    //    rdy->data, data->eof 
    state_rcvr_wait     <= (state_rcvr_wait    | (set_rcvr_wait    & ~alignes_pair)) & ~(set_rcvr_rdy |    (clr_rcvr_wait & ~alignes_pair))  & ~rst;

    state_rcvr_rdy      <= (state_rcvr_rdy     | set_rcvr_rdy                      ) & ~(set_rcvr_data |   (clr_rcvr_rdy  & ~alignes_pair))  & ~rst;
    
    state_rcvr_data     <= (state_rcvr_data    | set_rcvr_data                     ) & ~(set_rcvr_shold |
                                                                                         set_rcvr_shold |
507 508
                                                                                         set_rcvr_eof |
                                                                                         got_escape |      (clr_rcvr_data & ~alignes_pair))  & ~rst;
509 510 511
                                                                                         
    state_rcvr_rhold    <= (state_rcvr_rhold   | set_rcvr_rhold                   )  & ~(set_rcvr_data |
                                                                                         set_rcvr_shold |
512 513
                                                                                         set_rcvr_eof |
                                                                                         got_escape |      (clr_rcvr_rhold & ~alignes_pair)) & ~rst;
514 515
                                                                                         
    state_rcvr_shold    <= (state_rcvr_shold   | set_rcvr_shold                   )  & ~(set_rcvr_data |
516 517
                                                                                         set_rcvr_eof |
                                                                                         got_escape |      (clr_rcvr_shold & ~alignes_pair)) & ~rst;
518 519 520 521 522
                                                                                         
    state_rcvr_eof      <= (state_rcvr_eof     | set_rcvr_eof                     )  & ~(set_rcvr_goodcrc |
                                                                                         state_rcvr_badend |(clr_rcvr_eof & ~alignes_pair))  & ~rst;
    
    state_rcvr_goodcrc  <= (state_rcvr_goodcrc | set_rcvr_goodcrc                 )  & ~(set_rcvr_goodend |
523 524
                                                                                         set_rcvr_badend |
                                                                                         got_escape |      (clr_rcvr_goodcrc & ~alignes_pair)) & ~rst;
525
                                                                                         
526
    state_rcvr_goodend  <= (state_rcvr_goodend | set_rcvr_goodend                 ) & ~(got_escape |       (clr_rcvr_goodend & ~alignes_pair)) & ~rst;
527
    
528
    state_rcvr_badend   <= (state_rcvr_badend  | set_rcvr_badend                  ) & ~(got_escape |       (clr_rcvr_badend & ~alignes_pair))  & ~rst;
529 530

/*
531 532 533 534 535 536 537 538 539
    state_rcvr_wait     <= (state_rcvr_wait    | set_rcvr_wait    & ~alignes_pair) & ~(clr_rcvr_wait & ~alignes_pair)    & ~rst;
    state_rcvr_rdy      <= (state_rcvr_rdy     | set_rcvr_rdy     & ~alignes_pair) & ~(clr_rcvr_rdy & ~alignes_pair)     & ~rst;
    state_rcvr_data     <= (state_rcvr_data    | set_rcvr_data    & ~alignes_pair) & ~(clr_rcvr_data & ~alignes_pair)    & ~rst;
    state_rcvr_rhold    <= (state_rcvr_rhold   | set_rcvr_rhold   & ~alignes_pair) & ~(clr_rcvr_rhold & ~alignes_pair)   & ~rst;
    state_rcvr_shold    <= (state_rcvr_shold   | set_rcvr_shold   & ~alignes_pair) & ~(clr_rcvr_shold & ~alignes_pair)   & ~rst;
    state_rcvr_eof      <= (state_rcvr_eof     | set_rcvr_eof     & ~alignes_pair) & ~(clr_rcvr_eof & ~alignes_pair)     & ~rst;
    state_rcvr_goodcrc  <= (state_rcvr_goodcrc | set_rcvr_goodcrc & ~alignes_pair) & ~(clr_rcvr_goodcrc & ~alignes_pair) & ~rst;
    state_rcvr_goodend  <= (state_rcvr_goodend | set_rcvr_goodend & ~alignes_pair) & ~(clr_rcvr_goodend & ~alignes_pair) & ~rst;
    state_rcvr_badend   <= (state_rcvr_badend  | set_rcvr_badend  & ~alignes_pair) & ~(clr_rcvr_badend & ~alignes_pair)  & ~rst;
540 541 542

*/

543 544 545 546 547
end

// flag if incoming request to terminate current transaction came from TL
reg     incom_stop_f;
always @ (posedge clk)
548 549 550 551 552
//    incom_stop_f <= rst | incom_done | ~frame_busy ? 1'b0 : incom_stop_req ? 1'b1 : incom_stop_f;
    if      (rst)                      incom_stop_f <= 0;
    else if (incom_stop_req)           incom_stop_f <= 1;
    else if (incom_done | ~frame_busy) incom_stop_f <= 0;
    
553 554 555

// form data to phy
reg     [DATA_BYTE_WIDTH*8 - 1:0] to_phy_data;
Alexey Grebenkin's avatar
Alexey Grebenkin committed
556
reg     [DATA_BYTE_WIDTH   - 1:0] to_phy_isk;
557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583
// TODO implement CONTP
localparam [15:0] PRIM_SYNCP_HI     = {3'd5, 5'd21, 3'd5, 5'd21};
localparam [15:0] PRIM_SYNCP_LO     = {3'd4, 5'd21, 3'd3, 5'd28};
localparam [15:0] PRIM_ALIGNP_HI	= {3'd3, 5'd27, 3'd2, 5'd10};
localparam [15:0] PRIM_ALIGNP_LO	= {3'd2, 5'd10, 3'd5, 5'd28};
localparam [15:0] PRIM_XRDYP_HI		= {3'd2, 5'd23, 3'd2, 5'd23};
localparam [15:0] PRIM_XRDYP_LO		= {3'd5, 5'd21, 3'd3, 5'd28};
localparam [15:0] PRIM_SOFP_HI		= {3'd1, 5'd23, 3'd1, 5'd23};
localparam [15:0] PRIM_SOFP_LO		= {3'd5, 5'd21, 3'd3, 5'd28};
localparam [15:0] PRIM_HOLDAP_HI	= {3'd4, 5'd21, 3'd4, 5'd21};
localparam [15:0] PRIM_HOLDAP_LO	= {3'd5, 5'd10, 3'd3, 5'd28};
localparam [15:0] PRIM_HOLDP_HI		= {3'd6, 5'd21, 3'd6, 5'd21};
localparam [15:0] PRIM_HOLDP_LO		= {3'd5, 5'd10, 3'd3, 5'd28};
localparam [15:0] PRIM_EOFP_HI		= {3'd6, 5'd21, 3'd6, 5'd21};
localparam [15:0] PRIM_EOFP_LO		= {3'd5, 5'd21, 3'd3, 5'd28};
localparam [15:0] PRIM_WTRMP_HI		= {3'd2, 5'd24, 3'd2, 5'd24};
localparam [15:0] PRIM_WTRMP_LO		= {3'd5, 5'd21, 3'd3, 5'd28};
localparam [15:0] PRIM_RRDYP_HI		= {3'd2, 5'd10, 3'd2, 5'd10};
localparam [15:0] PRIM_RRDYP_LO		= {3'd4, 5'd21, 3'd3, 5'd28};
localparam [15:0] PRIM_IPP_HI		= {3'd2, 5'd21, 3'd2, 5'd21};
localparam [15:0] PRIM_IPP_LO		= {3'd5, 5'd21, 3'd3, 5'd28};
localparam [15:0] PRIM_DMATP_HI		= {3'd1, 5'd22, 3'd1, 5'd22};
localparam [15:0] PRIM_DMATP_LO		= {3'd5, 5'd21, 3'd3, 5'd28};
localparam [15:0] PRIM_OKP_HI		= {3'd1, 5'd21, 3'd1, 5'd21};
localparam [15:0] PRIM_OKP_LO		= {3'd5, 5'd21, 3'd3, 5'd28};
localparam [15:0] PRIM_ERRP_HI		= {3'd2, 5'd22, 3'd2, 5'd22};
localparam [15:0] PRIM_ERRP_LO		= {3'd5, 5'd21, 3'd3, 5'd28};
584 585
//The transmission of CONTp is optional, but the ability to receive and properly process CONTp is required.
localparam [15:0] PRIM_CONTP_HI     = {3'd4, 5'd25, 3'd4, 5'd25};
Andrey Filippov's avatar
Andrey Filippov committed
586
localparam [15:0] PRIM_CONTP_LO     = {3'd5, 5'd10, 3'd3, 5'd28};
587 588 589 590 591 592 593 594 595


wire    [DATA_BYTE_WIDTH*8 - 1:0] prim_data [PRIM_NUM - 1:0];

// fill all possible output primitives to choose from them after
generate
if (DATA_BYTE_WIDTH == 2)
begin
    reg     prim_word; // word counter in a primitive TODO logic
596 597 598 599 600 601 602 603 604 605 606 607 608 609 610
    assign  prim_data[CODE_SYNCP] [15:0]    =  prim_word ? PRIM_SYNCP_HI    : PRIM_SYNCP_LO;
    assign  prim_data[CODE_ALIGNP][15:0]    =  prim_word ? PRIM_ALIGNP_HI   : PRIM_ALIGNP_LO;
    assign  prim_data[CODE_XRDYP] [15:0]    =  prim_word ? PRIM_XRDYP_HI    : PRIM_XRDYP_LO;
    assign  prim_data[CODE_SOFP]  [15:0]    =  prim_word ? PRIM_SOFP_HI     : PRIM_SOFP_LO;
    assign  prim_data[CODE_DATA]  [15:0]    =  scrambler_out[15:0];
    assign  prim_data[CODE_HOLDAP][15:0]    =  prim_word ? PRIM_HOLDAP_HI   : PRIM_HOLDAP_LO;
    assign  prim_data[CODE_HOLDP] [15:0]    =  prim_word ? PRIM_HOLDP_HI    : PRIM_HOLDP_LO;
    assign  prim_data[CODE_CRC]   [15:0]    =  scrambler_out[15:0];
    assign  prim_data[CODE_EOFP]  [15:0]    =  prim_word ? PRIM_EOFP_HI     : PRIM_EOFP_LO;
    assign  prim_data[CODE_WTRMP] [15:0]    =  prim_word ? PRIM_WTRMP_HI    : PRIM_WTRMP_LO;
    assign  prim_data[CODE_RRDYP] [15:0]    =  prim_word ? PRIM_RRDYP_HI    : PRIM_RRDYP_LO;
    assign  prim_data[CODE_IPP]   [15:0]    =  prim_word ? PRIM_IPP_HI      : PRIM_IPP_LO;
    assign  prim_data[CODE_DMATP] [15:0]    =  prim_word ? PRIM_DMATP_HI    : PRIM_DMATP_LO;
    assign  prim_data[CODE_OKP]   [15:0]    =  prim_word ? PRIM_OKP_HI      : PRIM_OKP_LO;
    assign  prim_data[CODE_ERRP]  [15:0]    =  prim_word ? PRIM_ERRP_HI     : PRIM_ERRP_LO;
611
    assign  prim_data[CODE_CONTP] [15:0]    =  prim_word ? PRIM_CONTP_HI    : PRIM_CONTP_LO;
612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635
    always @ (posedge clk)
    begin
        $display("%m: unsupported data width");
        $finish;
    end
end
else
if (DATA_BYTE_WIDTH == 4)
begin
    assign  prim_data[CODE_SYNCP]     = {PRIM_SYNCP_HI    , PRIM_SYNCP_LO};
    assign  prim_data[CODE_ALIGNP]    = {PRIM_ALIGNP_HI   , PRIM_ALIGNP_LO};
    assign  prim_data[CODE_XRDYP]     = {PRIM_XRDYP_HI    , PRIM_XRDYP_LO};
    assign  prim_data[CODE_SOFP]      = {PRIM_SOFP_HI     , PRIM_SOFP_LO};
    assign  prim_data[CODE_DATA]      = scrambler_out;
    assign  prim_data[CODE_HOLDAP]    = {PRIM_HOLDAP_HI   , PRIM_HOLDAP_LO};
    assign  prim_data[CODE_HOLDP]     = {PRIM_HOLDP_HI    , PRIM_HOLDP_LO};
    assign  prim_data[CODE_CRC]       = scrambler_out;
    assign  prim_data[CODE_EOFP]      = {PRIM_EOFP_HI     , PRIM_EOFP_LO};
    assign  prim_data[CODE_WTRMP]     = {PRIM_WTRMP_HI    , PRIM_WTRMP_LO};
    assign  prim_data[CODE_RRDYP]     = {PRIM_RRDYP_HI    , PRIM_RRDYP_LO};
    assign  prim_data[CODE_IPP]       = {PRIM_IPP_HI      , PRIM_IPP_LO};
    assign  prim_data[CODE_DMATP]     = {PRIM_DMATP_HI    , PRIM_DMATP_LO};
    assign  prim_data[CODE_OKP]       = {PRIM_OKP_HI      , PRIM_OKP_LO};
    assign  prim_data[CODE_ERRP]      = {PRIM_ERRP_HI     , PRIM_ERRP_LO};
636
    assign  prim_data[CODE_CONTP]     = {PRIM_CONTP_HI    , PRIM_CONTP_LO};
637 638 639 640 641 642 643 644 645 646 647 648 649 650
end
else
begin
    always @ (posedge clk)
    begin
        $display("%m: unsupported data width");
        $finish;
    end
end
endgenerate

// select which primitive shall be sent 
wire    [PRIM_NUM - 1:0]    select_prim;
assign  select_prim[CODE_SYNCP]     = ~alignes_pair & (state_idle | state_sync_esc | state_rcvr_wait | state_reset);
651
assign  select_prim[CODE_ALIGNP]    =  alignes_pair | (state_nocomm | state_nocommerr | state_align);
652 653 654 655 656 657 658
assign  select_prim[CODE_XRDYP]     = ~alignes_pair & (state_send_rdy);
assign  select_prim[CODE_SOFP]      = ~alignes_pair & (state_send_sof);
assign  select_prim[CODE_DATA]      = ~alignes_pair & (state_send_data & ~set_send_shold); // if there's no data availible for a transmission, fsm still = state_send_data. Need to explicitly count this case.
assign  select_prim[CODE_HOLDAP]    = ~alignes_pair & (state_send_rhold | state_rcvr_shold & ~incom_stop_f);
assign  select_prim[CODE_HOLDP]     = ~alignes_pair & (state_send_shold | state_rcvr_rhold | state_send_data & set_send_shold); // the case mentioned 2 lines upper
assign  select_prim[CODE_CRC]       = ~alignes_pair & (state_send_crc);
assign  select_prim[CODE_EOFP]      = ~alignes_pair & (state_send_eof);
659
assign  select_prim[CODE_WTRMP]     = ~alignes_pair & (state_wait);
660 661 662 663 664
assign  select_prim[CODE_RRDYP]     = ~alignes_pair & (state_rcvr_rdy);
assign  select_prim[CODE_IPP]       = ~alignes_pair & (state_rcvr_data & ~incom_stop_f | state_rcvr_eof | state_rcvr_goodcrc);
assign  select_prim[CODE_DMATP]     = ~alignes_pair & (state_rcvr_data &  incom_stop_f | state_rcvr_shold & incom_stop_f);
assign  select_prim[CODE_OKP]       = ~alignes_pair & (state_rcvr_goodend);
assign  select_prim[CODE_ERRP]      = ~alignes_pair & (state_rcvr_badend);
665
// No sending of CONTp
666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684

// primitive selector MUX 
always @ (posedge clk)
    to_phy_data <=  rst ? {DATA_BYTE_WIDTH*8{1'b0}}: 
                    {DATA_BYTE_WIDTH*8{select_prim[CODE_SYNCP]}}  & prim_data[CODE_SYNCP]  |
                    {DATA_BYTE_WIDTH*8{select_prim[CODE_ALIGNP]}} & prim_data[CODE_ALIGNP] |
                    {DATA_BYTE_WIDTH*8{select_prim[CODE_RRDYP]}}  & prim_data[CODE_RRDYP]  |
                    {DATA_BYTE_WIDTH*8{select_prim[CODE_SOFP]}}   & prim_data[CODE_SOFP]   |
                    {DATA_BYTE_WIDTH*8{select_prim[CODE_HOLDAP]}} & prim_data[CODE_HOLDAP] |
                    {DATA_BYTE_WIDTH*8{select_prim[CODE_HOLDP]}}  & prim_data[CODE_HOLDP]  |
                    {DATA_BYTE_WIDTH*8{select_prim[CODE_EOFP]}}   & prim_data[CODE_EOFP]   |
                    {DATA_BYTE_WIDTH*8{select_prim[CODE_WTRMP]}}  & prim_data[CODE_WTRMP]  |
                    {DATA_BYTE_WIDTH*8{select_prim[CODE_XRDYP]}}  & prim_data[CODE_XRDYP]  |
                    {DATA_BYTE_WIDTH*8{select_prim[CODE_IPP]}}    & prim_data[CODE_IPP]    |
                    {DATA_BYTE_WIDTH*8{select_prim[CODE_DMATP]}}  & prim_data[CODE_DMATP]  |
                    {DATA_BYTE_WIDTH*8{select_prim[CODE_OKP]}}    & prim_data[CODE_OKP]    |
                    {DATA_BYTE_WIDTH*8{select_prim[CODE_ERRP]}}   & prim_data[CODE_ERRP]   |
                    {DATA_BYTE_WIDTH*8{select_prim[CODE_CRC]}}    & prim_data[CODE_CRC]    |
                    {DATA_BYTE_WIDTH*8{select_prim[CODE_DATA]}}   & prim_data[CODE_DATA];
Alexey Grebenkin's avatar
Alexey Grebenkin committed
685

686
always @ (posedge clk)
Alexey Grebenkin's avatar
Alexey Grebenkin committed
687
    to_phy_isk <= rst | ~select_prim[CODE_DATA] & ~select_prim[CODE_CRC] ? {{(DATA_BYTE_WIDTH - 1){1'b0}}, 1'b1} : {DATA_BYTE_WIDTH{1'b0}} ;
688 689 690

// incoming data is data
wire    inc_is_data;
691
assign  inc_is_data = dword_val & rcvd_dword[CODE_DATA] & (state_rcvr_data | state_rcvr_rhold);
692
//wire    inc_is_crc = dword_val_na & rcvd_dword[CODE_CRC] & (state_rcvr_data | state_rcvr_rhold);
693 694 695 696 697 698 699
/*
 * Scrambler can work both as a scrambler and a descramler, because data stream could be
 * one direction at a time
 */
scrambler scrambler(
    .rst        (select_prim[CODE_SOFP] | dword_val & rcvd_dword[CODE_SOFP]),
    .clk        (clk),
700
    .val_in     (select_prim[CODE_DATA] | inc_is_data | select_prim[CODE_CRC]),
701 702
    .data_in    (crc_dword & {DATA_BYTE_WIDTH*8{select_prim[CODE_CRC]}} | 
                 data_in & {DATA_BYTE_WIDTH*8{select_prim[CODE_DATA]}} | 
703
                 phy_data_in_r & {DATA_BYTE_WIDTH*8{inc_is_data}}),
704 705
    .data_out   (scrambler_out)
);
Alexey Grebenkin's avatar
Alexey Grebenkin committed
706

707 708 709 710 711 712 713
/*
 * Same as for scrambler, crc computation for both directions
 */
crc crc(
    .clk        (clk),
    .rst        (select_prim[CODE_SOFP] | dword_val & rcvd_dword[CODE_SOFP]),
    .val_in     (select_prim[CODE_DATA] | inc_is_data),
Alexey Grebenkin's avatar
Alexey Grebenkin committed
714
    .data_in    (data_in & {DATA_BYTE_WIDTH*8{select_prim[CODE_DATA]}} | scrambler_out & {DATA_BYTE_WIDTH*8{inc_is_data}}),
715 716
    .crc_out    (crc_dword)
);
Alexey Grebenkin's avatar
Alexey Grebenkin committed
717

718 719 720 721 722
// the output of crc module shall be 0 if 1 tick later reciever got a crc checksum and no errors occured
assign  crc_good = ~|crc_dword & state_rcvr_eof;
assign  crc_bad  =  |crc_dword & state_rcvr_eof;

// to TL data outputs assigment
723 724 725 726 727 728
// delay outputs so the last data would be marked
reg [31:0]  data_out_r;
reg         data_val_out_r;
reg [31:0]  data_out_rr;
reg         data_val_out_rr;
// if current == EOF => _r == CRC and _rr == last data piece
729
/*
730 731 732 733 734
always @ (posedge clk)
begin
    data_out_r      <= scrambler_out;
    data_out_rr     <= data_out_r;
    data_val_out_r  <= inc_is_data;
735
    data_val_out_rr <= data_val_out_r & ~set_rcvr_eof; // means that @ previous clock cycle the delivered data was crc
736
end
737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753
*/
reg  data_held;   // some data is held in data_out_r over primitives - to be restored if not EOF
// no need to check for set_rcvr_eof - last dword will be always lost
always @ (posedge clk) begin
    if (dword_val & rcvd_dword[CODE_SOFP]) data_held <= 0;
    else if (inc_is_data)                  data_held <= 1;
    
    if (inc_is_data)                       data_out_r   <= scrambler_out;
    if (data_val_out_r)                    data_out_rr  <= data_out_r;
    
    data_val_out_r  <= inc_is_data;

    data_val_out_rr <= inc_is_data && data_held;
    
end


754
assign  data_out        = data_out_rr;
755
assign  data_mask_out   = 2'b11;//{DATA_BYTE_WIDTH/2{1'b1}};
756 757 758 759
assign  data_val_out    = data_val_out_rr;
assign  data_last_out   = set_rcvr_eof;


760 761 762 763 764

// from TL data
// gives a strobe everytime data is present and we're at a corresponding state.
assign  data_strobe_out = select_prim[CODE_DATA];

765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787
// Just to make output signals single-cycel regardless of alignes_pair and remove dependence on SM code
wire frame_rej_w;
wire incom_start_w;
wire incom_done_w;
wire incom_invalidate_w;

reg frame_rej_r;
reg incom_start_r;
reg incom_done_r;
reg incom_invalidate_r;

assign frame_rej =        frame_rej_w        && !frame_rej_r;
assign incom_start =      incom_start_w      && ! incom_start_r;
assign incom_done =       incom_done_w       && ! incom_done_r;
assign incom_invalidate = incom_invalidate_w && ! incom_invalidate_r;

always @ (posedge clk) begin
    frame_rej_r <=        frame_rej_w;
    incom_start_r <=      incom_start_w;
    incom_done_r <=       incom_done_w;
    incom_invalidate_r <= incom_invalidate_w;
end

788 789 790 791 792 793
// assign phy data outputs
assign  phy_data_out = to_phy_data;
assign  phy_isk_out  = to_phy_isk;

assign  frame_busy  = ~state_idle;
assign  frame_ack   = state_send_sof;
794
assign  frame_rej_w = set_rcvr_wait & state_send_rdy; //  & ~alignes_pair; // OK to mask with 
795 796

// incoming fises detected
797
assign  incom_start_w = set_rcvr_wait; //  & ~alignes_pair;
798
// ... and processed
799
assign  incom_done_w  = set_rcvr_goodcrc; // & ~alignes_pair;
800
// or the FIS had errors
801
//assign  incom_invalidate = state_rcvr_eof & crc_bad & ~alignes_pair | state_rcvr_data   & dword_val_na &  rcvd_dword[CODE_WTRMP] 
802 803
//                         | (state_rcvr_wait | state_rcvr_rdy | state_rcvr_data | state_rcvr_rhold | state_rcvr_shold | state_rcvr_eof | state_rcvr_goodcrc) & got_escape;
// Separating different types of errors, sync_escape from other problems. TODO: route individual errors to set SERR bits
804
//assign  incom_invalidate =  (state_rcvr_eof &  crc_bad & ~alignes_pair) | // CRC mismatch
805
//                            (state_rcvr_data & dword_val_na &  rcvd_dword[CODE_WTRMP]);
806 807 808 809
assign  incom_invalidate_w =  (state_rcvr_eof &  crc_bad) | // CRC mismatch
                              (state_rcvr_data & dword_val &  rcvd_dword[CODE_WTRMP]); // missed EOF?
assign  incom_sync_escape =   (state_rcvr_wait | state_rcvr_rdy | state_rcvr_data | state_rcvr_rhold |
                               state_rcvr_shold | state_rcvr_eof | state_rcvr_goodcrc) & got_escape;
810

811 812 813 814
// shows that incoming primitive or data is ready to be processed // TODO somehow move alignes_pair into dword_val_na

assign  dword_val =    |rcvd_dword & phy_ready;                            // any valid primitive/data
assign  dword_val_na = |rcvd_dword & phy_ready & ~rcvd_dword[CODE_ALIGNP]; // any valid primitive/data but ALIGNp
815
// determine imcoming primitive type
Andrey Filippov's avatar
Andrey Filippov committed
816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836
/*
// determine imcoming primitive type
assign  rcvd_dword[CODE_DATA]   = ~|phy_isk_in_r;
assign  rcvd_dword[CODE_CRC]    = 1'b0;
assign  rcvd_dword[CODE_SYNCP]  = phy_isk_in_r[0] == 1'b1 & ~|phy_isk_in_r[DATA_BYTE_WIDTH-1:1] & prim_data[CODE_SYNCP ] == phy_data_in_r;
assign  rcvd_dword[CODE_ALIGNP] = phy_isk_in_r[0] == 1'b1 & ~|phy_isk_in_r[DATA_BYTE_WIDTH-1:1] & prim_data[CODE_ALIGNP] == phy_data_in_r;
assign  rcvd_dword[CODE_XRDYP]  = phy_isk_in_r[0] == 1'b1 & ~|phy_isk_in_r[DATA_BYTE_WIDTH-1:1] & prim_data[CODE_XRDYP ] == phy_data_in_r;
assign  rcvd_dword[CODE_SOFP]   = phy_isk_in_r[0] == 1'b1 & ~|phy_isk_in_r[DATA_BYTE_WIDTH-1:1] & prim_data[CODE_SOFP  ] == phy_data_in_r;
assign  rcvd_dword[CODE_HOLDAP] = phy_isk_in_r[0] == 1'b1 & ~|phy_isk_in_r[DATA_BYTE_WIDTH-1:1] & prim_data[CODE_HOLDAP] == phy_data_in_r;
assign  rcvd_dword[CODE_HOLDP]  = phy_isk_in_r[0] == 1'b1 & ~|phy_isk_in_r[DATA_BYTE_WIDTH-1:1] & prim_data[CODE_HOLDP ] == phy_data_in_r;
assign  rcvd_dword[CODE_EOFP]   = phy_isk_in_r[0] == 1'b1 & ~|phy_isk_in_r[DATA_BYTE_WIDTH-1:1] & prim_data[CODE_EOFP  ] == phy_data_in_r;
assign  rcvd_dword[CODE_WTRMP]  = phy_isk_in_r[0] == 1'b1 & ~|phy_isk_in_r[DATA_BYTE_WIDTH-1:1] & prim_data[CODE_WTRMP ] == phy_data_in_r;
assign  rcvd_dword[CODE_RRDYP]  = phy_isk_in_r[0] == 1'b1 & ~|phy_isk_in_r[DATA_BYTE_WIDTH-1:1] & prim_data[CODE_RRDYP ] == phy_data_in_r;
assign  rcvd_dword[CODE_IPP]    = phy_isk_in_r[0] == 1'b1 & ~|phy_isk_in_r[DATA_BYTE_WIDTH-1:1] & prim_data[CODE_IPP   ] == phy_data_in_r;
assign  rcvd_dword[CODE_DMATP]  = phy_isk_in_r[0] == 1'b1 & ~|phy_isk_in_r[DATA_BYTE_WIDTH-1:1] & prim_data[CODE_DMATP ] == phy_data_in_r;
assign  rcvd_dword[CODE_OKP]    = phy_isk_in_r[0] == 1'b1 & ~|phy_isk_in_r[DATA_BYTE_WIDTH-1:1] & prim_data[CODE_OKP   ] == phy_data_in_r;
assign  rcvd_dword[CODE_ERRP]   = phy_isk_in_r[0] == 1'b1 & ~|phy_isk_in_r[DATA_BYTE_WIDTH-1:1] & prim_data[CODE_ERRP  ] == phy_data_in_r;
// was missing
assign  rcvd_dword[CODE_CONTP]  = phy_isk_in_r[0] == 1'b1 & ~|phy_isk_in_r[DATA_BYTE_WIDTH-1:1] & prim_data[CODE_CONTP ] == phy_data_in_r;

*/
837
assign  rcvd_dword[CODE_DATA]	= ~|phy_isk_in_r;
838
assign  rcvd_dword[CODE_CRC]	= 1'b0;
Andrey Filippov's avatar
Andrey Filippov committed
839 840 841 842 843 844 845 846 847 848 849 850 851 852 853
assign  rcvd_dword[CODE_SYNCP]	= phy_isk_in_r[0] && !(|phy_isk_in_r[DATA_BYTE_WIDTH-1:1]) && (prim_data[CODE_SYNCP ] == phy_data_in_r);
assign  rcvd_dword[CODE_ALIGNP]	= phy_isk_in_r[0] && !(|phy_isk_in_r[DATA_BYTE_WIDTH-1:1]) && (prim_data[CODE_ALIGNP] == phy_data_in_r);
assign  rcvd_dword[CODE_XRDYP]	= phy_isk_in_r[0] && !(|phy_isk_in_r[DATA_BYTE_WIDTH-1:1]) && (prim_data[CODE_XRDYP ] == phy_data_in_r);
assign  rcvd_dword[CODE_SOFP]	= phy_isk_in_r[0] && !(|phy_isk_in_r[DATA_BYTE_WIDTH-1:1]) && (prim_data[CODE_SOFP  ] == phy_data_in_r);
assign  rcvd_dword[CODE_HOLDAP]	= phy_isk_in_r[0] && !(|phy_isk_in_r[DATA_BYTE_WIDTH-1:1]) && (prim_data[CODE_HOLDAP] == phy_data_in_r);
assign  rcvd_dword[CODE_HOLDP]	= phy_isk_in_r[0] && !(|phy_isk_in_r[DATA_BYTE_WIDTH-1:1]) && (prim_data[CODE_HOLDP ] == phy_data_in_r);
assign  rcvd_dword[CODE_EOFP]	= phy_isk_in_r[0] && !(|phy_isk_in_r[DATA_BYTE_WIDTH-1:1]) && (prim_data[CODE_EOFP  ] == phy_data_in_r);
assign  rcvd_dword[CODE_WTRMP]	= phy_isk_in_r[0] && !(|phy_isk_in_r[DATA_BYTE_WIDTH-1:1]) && (prim_data[CODE_WTRMP ] == phy_data_in_r);
assign  rcvd_dword[CODE_RRDYP]	= phy_isk_in_r[0] && !(|phy_isk_in_r[DATA_BYTE_WIDTH-1:1]) && (prim_data[CODE_RRDYP ] == phy_data_in_r);
assign  rcvd_dword[CODE_IPP]	= phy_isk_in_r[0] && !(|phy_isk_in_r[DATA_BYTE_WIDTH-1:1]) && (prim_data[CODE_IPP   ] == phy_data_in_r);
assign  rcvd_dword[CODE_DMATP]	= phy_isk_in_r[0] && !(|phy_isk_in_r[DATA_BYTE_WIDTH-1:1]) && (prim_data[CODE_DMATP ] == phy_data_in_r);
assign  rcvd_dword[CODE_OKP]	= phy_isk_in_r[0] && !(|phy_isk_in_r[DATA_BYTE_WIDTH-1:1]) && (prim_data[CODE_OKP   ] == phy_data_in_r);
assign  rcvd_dword[CODE_ERRP]	= phy_isk_in_r[0] && !(|phy_isk_in_r[DATA_BYTE_WIDTH-1:1]) && (prim_data[CODE_ERRP  ] == phy_data_in_r);
// was missing
assign  rcvd_dword[CODE_CONTP]  = phy_isk_in_r[0] && ~(|phy_isk_in_r[DATA_BYTE_WIDTH-1:1]) && (prim_data[CODE_CONTP ] == phy_data_in_r);
854

855
// CONTp (*_r0 is one cycle ahead of *_r)
Andrey Filippov's avatar
Andrey Filippov committed
856 857
//assign is_cont_p_w =     phy_isk_in_r0[0] == 1'b1 & ~|phy_isk_in_r[DATA_BYTE_WIDTH-1:1] & prim_data[CODE_CONTP  ] == phy_data_in_r0;
//assign is_non_cont_non_align_p_w = phy_isk_in_r0[0] == 1'b1 & ~|phy_isk_in_r[DATA_BYTE_WIDTH-1:1] & prim_data[CODE_CONTP  ] != phy_data_in_r0;
858
/*
Andrey Filippov's avatar
Andrey Filippov committed
859 860 861 862
assign is_cont_p_w =               phy_isk_in_r0[0] && !(|phy_isk_in_r[DATA_BYTE_WIDTH-1:1]) && (prim_data[CODE_CONTP  ] == phy_data_in_r0);
assign is_align_p_w =              phy_isk_in_r0[0] && !(|phy_isk_in_r[DATA_BYTE_WIDTH-1:1]) && (prim_data[CODE_ALIGNP ] == phy_data_in_r0);
assign is_non_cont_non_align_p_w = phy_isk_in_r0[0] && !(|phy_isk_in_r[DATA_BYTE_WIDTH-1:1]) && (prim_data[CODE_CONTP  ] != phy_data_in_r0)
                                                                                             && (prim_data[CODE_ALIGNP ] != phy_data_in_r0);
863 864 865 866 867 868
*/
// Following is processed one cycle ahead of the others to replace CONTp junk with the replaced repeated primitives
assign is_cont_p_w =               phy_isk_in_r0[0] && !(|phy_isk_in_r0[DATA_BYTE_WIDTH-1:1]) && (prim_data[CODE_CONTP  ] == phy_data_in_r0);
assign is_align_p_w =              phy_isk_in_r0[0] && !(|phy_isk_in_r0[DATA_BYTE_WIDTH-1:1]) && (prim_data[CODE_ALIGNP ] == phy_data_in_r0);
assign is_non_cont_non_align_p_w = phy_isk_in_r0[0] && !(|phy_isk_in_r0[DATA_BYTE_WIDTH-1:1]) && (prim_data[CODE_CONTP  ] != phy_data_in_r0)
                                                                                              && (prim_data[CODE_ALIGNP ] != phy_data_in_r0);
869 870


871
// phy level errors handling TODO
872
assign  dec_err = |phy_err_in_r;
Alexey Grebenkin's avatar
Alexey Grebenkin committed
873

874 875 876 877 878
// form a response to transport layer
assign  frame_done      = frame_done_good | frame_done_bad;
assign  frame_done_good = state_wait & dword_val & rcvd_dword[CODE_OKP];
assign  frame_done_bad  = state_wait & dword_val & rcvd_dword[CODE_ERRP];

Andrey Filippov's avatar
Andrey Filippov committed
879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910
// Handling 3 non-align primitives - removed, this is (should be) done by OOB
/*
always @ (posedge clk) begin
    if      (!phy_ready || link_reset)           link_established_r <= 0;
    else if (state_align && !(|non_align_cntr))  link_established_r <= 1;
    
    if (!state_align || rcvd_dword[CODE_ALIGNP]) non_align_cntr <= 3;
    else                                         non_align_cntr <=  non_align_cntr - 1;
end
*/


// =========== Debug code ===================
wire [PRIM_NUM - 1:0] rcvd_dword0; // at least oce received after reset

assign  rcvd_dword0[CODE_DATA]   = ~|phy_isk_in_r0;
assign  rcvd_dword0[CODE_CRC]    = 1'b0;
assign  rcvd_dword0[CODE_SYNCP]  = phy_isk_in_r0[0] && !(|phy_isk_in_r0[DATA_BYTE_WIDTH-1:1]) && (prim_data[CODE_SYNCP ] == phy_data_in_r0);
assign  rcvd_dword0[CODE_ALIGNP] = phy_isk_in_r0[0] && !(|phy_isk_in_r0[DATA_BYTE_WIDTH-1:1]) && (prim_data[CODE_ALIGNP] == phy_data_in_r0);
assign  rcvd_dword0[CODE_XRDYP]  = phy_isk_in_r0[0] && !(|phy_isk_in_r0[DATA_BYTE_WIDTH-1:1]) && (prim_data[CODE_XRDYP ] == phy_data_in_r0);
assign  rcvd_dword0[CODE_SOFP]   = phy_isk_in_r0[0] && !(|phy_isk_in_r0[DATA_BYTE_WIDTH-1:1]) && (prim_data[CODE_SOFP  ] == phy_data_in_r0);
assign  rcvd_dword0[CODE_HOLDAP] = phy_isk_in_r0[0] && !(|phy_isk_in_r0[DATA_BYTE_WIDTH-1:1]) && (prim_data[CODE_HOLDAP] == phy_data_in_r0);
assign  rcvd_dword0[CODE_HOLDP]  = phy_isk_in_r0[0] && !(|phy_isk_in_r0[DATA_BYTE_WIDTH-1:1]) && (prim_data[CODE_HOLDP ] == phy_data_in_r0);
assign  rcvd_dword0[CODE_EOFP]   = phy_isk_in_r0[0] && !(|phy_isk_in_r0[DATA_BYTE_WIDTH-1:1]) && (prim_data[CODE_EOFP  ] == phy_data_in_r0);
assign  rcvd_dword0[CODE_WTRMP]  = phy_isk_in_r0[0] && !(|phy_isk_in_r0[DATA_BYTE_WIDTH-1:1]) && (prim_data[CODE_WTRMP ] == phy_data_in_r0);
assign  rcvd_dword0[CODE_RRDYP]  = phy_isk_in_r0[0] && !(|phy_isk_in_r0[DATA_BYTE_WIDTH-1:1]) && (prim_data[CODE_RRDYP ] == phy_data_in_r0);
assign  rcvd_dword0[CODE_IPP]    = phy_isk_in_r0[0] && !(|phy_isk_in_r0[DATA_BYTE_WIDTH-1:1]) && (prim_data[CODE_IPP   ] == phy_data_in_r0);
assign  rcvd_dword0[CODE_DMATP]  = phy_isk_in_r0[0] && !(|phy_isk_in_r0[DATA_BYTE_WIDTH-1:1]) && (prim_data[CODE_DMATP ] == phy_data_in_r0);
assign  rcvd_dword0[CODE_OKP]    = phy_isk_in_r0[0] && !(|phy_isk_in_r0[DATA_BYTE_WIDTH-1:1]) && (prim_data[CODE_OKP   ] == phy_data_in_r0);
assign  rcvd_dword0[CODE_ERRP]   = phy_isk_in_r0[0] && !(|phy_isk_in_r0[DATA_BYTE_WIDTH-1:1]) && (prim_data[CODE_ERRP  ] == phy_data_in_r0);
assign  rcvd_dword0[CODE_CONTP]  = phy_isk_in_r0[0] && !(|phy_isk_in_r0[DATA_BYTE_WIDTH-1:1]) && (prim_data[CODE_CONTP ] == phy_data_in_r0);

911
reg [PRIM_NUM - 1:0] debug_rcvd_dword; // at least once received after reset
Andrey Filippov's avatar
Andrey Filippov committed
912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958
//reg [7:0]            debug_alignp_cntr;
///reg [7:0] debug_unknown_primitives;
///reg [7:0] debug_notaligned_primitives;
///reg [7:0] debug_data_primitives;
///reg  [7:0] debug_alignes;
///reg  [1:0]  debug_state_reset_r;
///reg         debug_alignp_r;
///reg         debug_syncp_r;
reg         debug_first_error;
reg         debug_first_alignp;
reg         debug_first_syncp;
reg         debug_first_nonsyncp;
reg         debug_first_unknown; 
reg  [19:0] debug_to_first_err;
reg  [15:0] debug_num_aligns;
reg  [15:0] debug_num_syncs;
reg  [15:0] debug_num_later_aligns;
reg         other_prim_r;
reg  [15:0] debug_num_other; // other primitives - not aligh, sync or cont
reg  [31:0] debug_unknown_dword; 
wire  debug_is_sync_p_w = phy_isk_in_r0[0] && !(|phy_isk_in_r[DATA_BYTE_WIDTH-1:1]) && (prim_data[CODE_SYNCP ] == phy_data_in_r0);

wire [STATES_COUNT - 1:0] debug_states_concat = {
                           state_idle
                         , state_sync_esc
                         , state_nocommerr
                         , state_nocomm
                         , state_align
                         , state_reset
                         , state_send_rdy
                         , state_send_sof
                         , state_send_data
                         , state_send_rhold
                         , state_send_shold
                         , state_send_crc
                         , state_send_eof
                         , state_wait
                         , state_rcvr_wait
                         , state_rcvr_rdy
                         , state_rcvr_data
                         , state_rcvr_rhold
                         , state_rcvr_shold
                         , state_rcvr_eof
                         , state_rcvr_goodcrc
                         , state_rcvr_goodend
                         , state_rcvr_badend
                         };
959 960
reg [4:0] debug_states_encoded;

Andrey Filippov's avatar
Andrey Filippov committed
961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008
reg [STATES_COUNT - 1:0] debug_states_visited;

always @ (posedge clk) begin
    other_prim_r <= is_non_cont_non_align_p_w && !debug_is_sync_p_w;

    if  (rst)                                              debug_first_alignp <= 0;
    else if (is_align_p_w)                                 debug_first_alignp <= 1;

    if  (rst)                                              debug_first_syncp <= 0;
    else if (debug_is_sync_p_w && debug_first_alignp)      debug_first_syncp <= 1;

    if  (rst)                                              debug_first_error <= 0;
    else if (dec_err && debug_first_syncp)                 debug_first_error <= 1;


    if  (rst)                                              debug_first_nonsyncp <= 0;
    else if (!debug_is_sync_p_w && debug_first_syncp)      debug_first_nonsyncp <= 1;
    
    if  (rst)                                              debug_first_unknown <= 0;
    else if ((rcvd_dword0 ==0) && debug_first_alignp)      debug_first_unknown <= 1;
    
    
    
    if  (rst)                                              debug_to_first_err <= 0;
    else if (debug_first_alignp && !debug_first_error)     debug_to_first_err <= debug_to_first_err + 1;
    
    if  (rst)                                              debug_num_aligns <= 0;
    else if (debug_first_alignp && !debug_first_syncp)     debug_num_aligns <= debug_num_aligns + 1;
    
    if  (rst)                                              debug_num_syncs <= 0;
    else if (debug_first_syncp && !debug_first_nonsyncp)   debug_num_syncs <= debug_num_syncs + 1;

    if  (rst)                                                              debug_num_later_aligns <= 0;
    else if (debug_first_nonsyncp && !debug_first_error && is_align_p_w)   debug_num_later_aligns <= debug_num_later_aligns + 1;

    if  (rst)                                                              debug_num_other <= 0;
//    else if (debug_first_nonsyncp && !debug_first_error && other_prim_r)   debug_num_later_aligns <= debug_num_later_aligns + 1;
    else if (debug_first_nonsyncp && !debug_first_error && other_prim_r)   debug_num_other <= debug_num_other + 1;
    
    if      (rst)                                                             debug_unknown_dword <= 0;
    else if ((rcvd_dword0 ==0) && debug_first_alignp && !debug_first_unknown) debug_unknown_dword <= phy_data_in_r0;
    
    if      (rst)                                                             debug_rcvd_dword <= 0;
    else if (debug_first_syncp                         && !debug_first_error) debug_rcvd_dword <= debug_rcvd_dword | rcvd_dword0;
    
    if (rst) debug_states_visited <= 0;
    else     debug_states_visited <= debug_states_visited | debug_states_concat;
    
1009 1010 1011 1012 1013 1014 1015 1016 1017 1018
    
    debug_states_encoded <= { |debug_states_concat[22:16],
                              |debug_states_concat[15: 8],
                             (|debug_states_concat[22:20]) | (|debug_states_concat[15:12]) | (|debug_states_concat[7:4]),
                             debug_states_concat[22] | (|debug_states_concat[19:18]) | (|debug_states_concat[15:14]) | 
                             (|debug_states_concat[11:10]) | (|debug_states_concat[7:6]) | (|debug_states_concat[3:2]),
                             debug_states_concat[21] |  debug_states_concat[19] | debug_states_concat[17] | debug_states_concat[15] |
                             debug_states_concat[13] |  debug_states_concat[11] | debug_states_concat[ 9] | debug_states_concat[7] |
                             debug_states_concat[ 5] |  debug_states_concat[ 3] | debug_states_concat[ 1]};
    
Andrey Filippov's avatar
Andrey Filippov committed
1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044
/* 
    if      (rst)          debug_rcvd_dword <= 0;
    debug_alignp_r <= rcvd_dword[CODE_ALIGNP];
    debug_syncp_r <=  rcvd_dword[CODE_SYNCP];
    debug_state_reset_r <= {debug_state_reset_r[0], state_reset};
    
    if      (rst)          debug_rcvd_dword <= 0;
//    if      (rst)       debug_rcvd_dword <= 0;
    else if (debug_state_reset_r[0])  debug_rcvd_dword <= debug_rcvd_dword | rcvd_dword;
   
    if      (state_reset)                           debug_unknown_primitives <= 0;
    else if ((phy_isk_in_r == 1) && !(|rcvd_dword)) debug_unknown_primitives <= debug_unknown_primitives + 1;
    

    if      (rst)                                                 debug_data_primitives <= 0;
    else if ((debug_state_reset_r[0] && debug_syncp_r))  debug_data_primitives <= debug_data_primitives + 1;
    
    if      (rst)                                                 debug_alignes <= 0;
    else if (debug_state_reset_r[0] && debug_alignp_r)   debug_alignes <= debug_alignes + 1;
    
    
    if      (rst)                                   debug_notaligned_primitives <= 0;
//    else if (phy_isk_in_r[3:1] != 0)                debug_notaligned_primitives <= debug_notaligned_primitives + 1;
    else if (debug_state_reset_r == 1)              debug_notaligned_primitives <= debug_notaligned_primitives + 1;
*/    
end
1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073

/*wire    state_idle;
reg     state_sync_esc;     // SyncEscape
reg     state_nocommerr;    // NoComErr
reg     state_nocomm;       // NoComm
reg     state_align;        // SendAlign - not used, handled by OOB
reg     state_reset;        // RESET
// tranmitter branch
reg     state_send_rdy;     // SendChkRdy
reg     state_send_sof;     // SendSOF
reg     state_send_data;    // SendData
reg     state_send_rhold;   // RcvrHold - hold initiated by current data reciever
reg     state_send_shold;   // SendHold - hold initiated by current data sender
reg     state_send_crc;     // SendCVC
reg     state_send_eof;     // SendEOF
reg     state_wait;         // Wait
// receiver branch
reg     state_rcvr_wait;    // RcvWaitFifo
reg     state_rcvr_rdy;     // RcvChkRdy
reg     state_rcvr_data;    // RcvData
reg     state_rcvr_rhold;   // Hold     - hold initiated by current data reciever
reg     state_rcvr_shold;   // RcvHold  - hold initiated by current data sender
reg     state_rcvr_eof;     // RcvEOF
reg     state_rcvr_goodcrc; // GoodCRC
reg     state_rcvr_goodend; // GoodEnd
reg     state_rcvr_badend;  // BadEnd
*/


Andrey Filippov's avatar
Andrey Filippov committed
1074 1075 1076 1077 1078 1079 1080
///assign debug_out[19: 0] =          debug_to_first_err;
//assign debug_out[23:16] =          debug_num_aligns;
//assign debug_out[31:20] =          debug_num_syncs[11:0];
//assign debug_out[31:20] =          debug_num_later_aligns[11:0];
///assign debug_out[31:20] =          debug_num_other[11:0];

///assign debug_out = debug_unknown_dword; // first unknown dword
1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126
reg [1:0] debug_data_last_in_r;
reg [1:0] debug_alignes_pair_r;
reg [1:0] debug_state_send_data_r;
reg [1:0] debug_dword_val_na;
reg [1:0] debug_CODE_SYNCP;
reg [1:0] debug_set_send_crc;
reg [1:0] debug_data_val_in;
reg [1:0] debug_was_OK_ERR;
reg       debug_was_wait;
reg       debug_was_idle;
reg       debug_was_ok_err;
reg       debug_was_state_wait;
reg       debug_was_frame_done;
reg       debug_was_got_escape;
// frame_done | got_escape

always @(posedge clk) begin
    if (data_strobe_out) begin
        debug_data_last_in_r <= {debug_data_last_in_r[0],data_last_in};
        debug_alignes_pair_r <= {debug_alignes_pair_r[0],alignes_pair};
        debug_state_send_data_r <= {debug_state_send_data_r[0],state_send_data};
        debug_dword_val_na <= {debug_dword_val_na[0],dword_val_na};
        debug_CODE_SYNCP <= {debug_CODE_SYNCP[0],rcvd_dword[CODE_SYNCP]};
        debug_set_send_crc <= {debug_set_send_crc[0],set_send_crc};
        debug_data_val_in <= {debug_data_val_in[0],data_val_in};
    end
     
    debug_was_ok_err <= rcvd_dword[CODE_ERRP] | rcvd_dword[CODE_OKP];
    
    if (frame_req) debug_was_OK_ERR <= 0;
    else debug_was_OK_ERR <= debug_was_OK_ERR | {rcvd_dword[CODE_ERRP], rcvd_dword[CODE_OKP]};
    
    if (frame_req) debug_was_state_wait <= 0;
    else debug_was_state_wait <= debug_was_state_wait | state_wait;
    
    if (state_wait && clr_wait && !alignes_pair) debug_was_frame_done <= frame_done;

    if (state_wait && clr_wait && !alignes_pair) debug_was_got_escape <= got_escape;
    
    if ((rcvd_dword[CODE_ERRP] || rcvd_dword[CODE_OKP]) && !debug_was_ok_err) begin
        debug_was_wait <= state_wait;
        debug_was_idle <= state_idle;
    end
    
end

1127
assign debug_out[ 4: 0]  =            debug_states_encoded;
1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142
assign debug_out[7: 5] =  {
                           rcvd_dword[CODE_SYNCP],
                           rcvd_dword[CODE_OKP],
                           alignes_pair};
assign debug_out[31]   =   rcvd_dword[CODE_ALIGNP];
assign debug_out[30]   =   set_send_sof;
assign debug_out[29]   =   clr_send_rdy;
assign debug_out[28]   =   state_send_rdy;
assign debug_out[27]   =   state_send_sof;
assign debug_out[26]   =   state_idle;
assign debug_out[25]   =   state_send_data;
assign debug_out[24]   =   (state_send_sof     | set_send_sof     & ~alignes_pair);
assign debug_out[23]   =   (clr_send_sof & ~alignes_pair);
assign debug_out[22]   =   set_rcvr_wait; // start logging input

1143
//assign debug_out[15: 5] =            debug_to_first_err[14:4];
1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164
assign debug_out[21:16] =              debug_rcvd_dword[5:0];

assign debug_out[15: 8] = {
                           debug_was_wait,         // state was wait when last CODE_ERRP/CODE_OKP was received
                           debug_was_idle,         // state was idle when last CODE_ERRP/CODE_OKP was received
                           debug_was_OK_ERR[1:0],
                           debug_was_state_wait,
                           debug_was_frame_done,
                           debug_was_got_escape,
                           
/*                           debug_data_last_in_r[1],
                           debug_alignes_pair_r[1],
                           debug_state_send_data_r[1],
                           debug_state_send_data_r[0],
                           debug_data_val_in[1],
                           
                           debug_data_val_in[0],
                           debug_set_send_crc[1],
*/                           
//                           debug_dword_val_na[1],
                           ~debug_CODE_SYNCP[1]};
1165 1166

/*
1167 1168 1169 1170 1171 1172
    state_send_sof      <= (state_send_sof     | set_send_sof     & ~alignes_pair) & ~(clr_send_sof & ~alignes_pair)     & ~rst;



_send_crc        = state_send_data   & data_txing &  data_val_in &  data_last_in & dword_val_na & ~rcvd_dword[CODE_SYNCP]
                            | state_send_data   &                                             dword_val_na &  rcvd_dword[CODE_DMATP];
1173 1174 1175

*/

Andrey Filippov's avatar
Andrey Filippov committed
1176 1177


1178
//assign debug_out[STATES_COUNT - 1:0] = debug_states_visited;
Andrey Filippov's avatar
Andrey Filippov committed
1179 1180 1181 1182 1183 1184 1185 1186 1187

/* 
//assign debug_out[PRIM_NUM - 1:0] = debug_rcvd_dword;
assign debug_out[ 7: 0] =          debug_rcvd_dword[7:0];
assign debug_out[15: 8] =          debug_alignes;
assign debug_out[23:16] =          debug_data_primitives;
assign debug_out[30:24] =          debug_notaligned_primitives[6:0]; // now count state_reset _/~
assign debug_out[31] =             debug_state_reset_r[0];
*/
Alexey Grebenkin's avatar
Alexey Grebenkin committed
1188
`ifdef CHECKERS_ENABLED
1189 1190 1191 1192
// incoming primitives
always @ (posedge clk)
    if (~|rcvd_dword & phy_ready)
    begin
Andrey Filippov's avatar
Andrey Filippov committed
1193
        $display("%m: invalid primitive received : %h, conrol : %h, err : %h", phy_data_in_r, phy_isk_in_r, phy_err_in_r);
1194
        #500;
1195 1196
        $finish;
    end
Alexey Grebenkin's avatar
Alexey Grebenkin committed
1197 1198 1199 1200
// States checker
reg  [STATES_COUNT - 1:0] sim_states_concat;
always @ (posedge clk)
    if (~rst)
Alexey Grebenkin's avatar
Alexey Grebenkin committed
1201 1202
    if (( 32'h0
       + state_idle
Alexey Grebenkin's avatar
Alexey Grebenkin committed
1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226
       + state_sync_esc
       + state_nocommerr
       + state_nocomm
       + state_align
       + state_reset
       + state_send_rdy
       + state_send_sof
       + state_send_data
       + state_send_rhold
       + state_send_shold
       + state_send_crc
       + state_send_eof
       + state_wait
       + state_rcvr_wait
       + state_rcvr_rdy
       + state_rcvr_data
       + state_rcvr_rhold
       + state_rcvr_shold
       + state_rcvr_eof
       + state_rcvr_goodcrc
       + state_rcvr_goodend
       + state_rcvr_badend
       ) != 1)
    begin
Alexey Grebenkin's avatar
Alexey Grebenkin committed
1227
        sim_states_concat = {
Alexey Grebenkin's avatar
Alexey Grebenkin committed
1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251
                           state_idle
                         , state_sync_esc
                         , state_nocommerr
                         , state_nocomm
                         , state_align
                         , state_reset
                         , state_send_rdy
                         , state_send_sof
                         , state_send_data
                         , state_send_rhold
                         , state_send_shold
                         , state_send_crc
                         , state_send_eof
                         , state_wait
                         , state_rcvr_wait
                         , state_rcvr_rdy
                         , state_rcvr_data
                         , state_rcvr_rhold
                         , state_rcvr_shold
                         , state_rcvr_eof
                         , state_rcvr_goodcrc
                         , state_rcvr_goodend
                         , state_rcvr_badend
                         };
Alexey Grebenkin's avatar
Alexey Grebenkin committed
1252
        $display("%m: invalid states: %b", sim_states_concat);
1253
//        $finish;
Alexey Grebenkin's avatar
Alexey Grebenkin committed
1254 1255 1256
    end
`endif

Alexey Grebenkin's avatar
Alexey Grebenkin committed
1257
`ifdef SIMULATION
1258 1259 1260 1261 1262 1263 1264
integer sim_cnt;
always @ (posedge clk) begin
    if (incom_start) begin
        HOST_LINK_TITLE = "Incoming start";
        $display("[Host] LINK:        %s @%t", HOST_LINK_TITLE, $time);
        sim_cnt = 0;
    end
Alexey Grebenkin's avatar
Alexey Grebenkin committed
1265
    if (data_val_out) begin
1266 1267
        HOST_LINK_TITLE = "From device - received data";
        HOST_LINK_DATA =  data_out;
1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294
        $display("[Host] LINK:        %s = %h (#%d)@%t", HOST_LINK_TITLE, HOST_LINK_DATA, sim_cnt, $time);
        sim_cnt = sim_cnt + 1;
    end
    if (incom_done) begin
        HOST_LINK_TITLE = "Incoming end";
        $display("[Host] LINK:        %s @%t", HOST_LINK_TITLE, $time);
        sim_cnt = 0;
    end
    if (incom_invalidate) begin
        HOST_LINK_TITLE = "Incoming invalidate";
        $display("[Host] LINK:        %s @%t", HOST_LINK_TITLE, $time);
        sim_cnt = 0;
    end
    if (incom_sync_escape) begin
        HOST_LINK_TITLE = "Incoming sync_escape";
        $display("[Host] LINK:        %s @%t", HOST_LINK_TITLE, $time);
        sim_cnt = 0;
    end
    if (incom_ack_good) begin
        HOST_LINK_TITLE = "Incoming ack_good";
        $display("[Host] LINK:        %s @%t", HOST_LINK_TITLE, $time);
        sim_cnt = 0;
    end
    if (incom_ack_bad) begin
        HOST_LINK_TITLE = "Incoming ack_bad";
        $display("[Host] LINK:        %s @%t", HOST_LINK_TITLE, $time);
        sim_cnt = 0;
Alexey Grebenkin's avatar
Alexey Grebenkin committed
1295
    end
1296
//    if (inc_is_data) begin
1297
//        $display("[Host] LINK:        From device - received raw data = %h", phy_data_in);
1298
//    end
Alexey Grebenkin's avatar
Alexey Grebenkin committed
1299 1300 1301 1302
end
    
`endif

Alexey Grebenkin's avatar
Alexey Grebenkin committed
1303
endmodule