sata_device.v 58.6 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: sata_device
 * Date: 2015-07-11  
 * Author: Alexey     
 * Description: sata device emul top level
 *
 * Copyright (c) 2015 Elphel, Inc.
 * sata_device.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.
 *
 * sata_device.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
//`include "sata_phy_dev.v"
Alexey Grebenkin's avatar
Alexey Grebenkin committed
35 36 37 38 39 40 41 42 43
module sata_device(
    input   wire    rst,
    input   wire    RXN,
    input   wire    RXP,
    output  wire    TXN,
    output  wire    TXP,
    input   wire    EXTCLK_P,
    input   wire    EXTCLK_N
);
Andrey Filippov's avatar
Andrey Filippov committed
44
`include "includes/fis_types.vh"
45
//`ifdef SIMULATION
Andrey Filippov's avatar
Andrey Filippov committed
46
    reg [639:0] DEV_TITLE = 'bz; // to show human-readable state in the GTKWave
47 48 49
//    reg  [31:0] DEV_DATA;
    integer DEV_DATA;
//`endif
50 51
//`define TERMINATE_DMA_H2D
//`undef TERMINATE_DMA_H2D
Alexey Grebenkin's avatar
Alexey Grebenkin committed
52 53 54 55 56 57 58 59 60 61 62
wire            phy_ready;

wire    [31:0]  phy2dev_data;
wire    [3:0]   phy2dev_charisk;
wire    [3:0]   phy2dev_err;
wire            clk;
wire            dev_rst;

reg     [31:0]  dev2phy_data    = 32'hB5B5957C; // SYNCP
reg     [3:0]   dev2phy_isk     = 4'h1;

Andrey Filippov's avatar
Andrey Filippov committed
63
reg     [2:0]   phy_ready_syncp_r;  // allow device to send 3 SYNCp before getting ready
Andrey Filippov's avatar
Andrey Filippov committed
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
reg     [4:0]   serial_delay = 7;  // delay output to check host alignment

localparam DEV_ALIGN_PERIOD = 32; // make it more frequent than 250
//integer    align_cntr = DEV_ALIGN_PERIOD;
reg     [7:0]  align_cntr = DEV_ALIGN_PERIOD;
initial forever @ (posedge clk) begin
    if (align_cntr > 0) align_cntr = align_cntr - 1;
end

task send_align_pair_if_needed;
    if (align_cntr ==0) begin
        dev2phy_data    <= PRIM_ALIGNP;
        dev2phy_isk     <= 4'h1;
        align_cntr = DEV_ALIGN_PERIOD;
        @ (posedge clk);
        @ (posedge clk);
    end
endtask    
Andrey Filippov's avatar
Andrey Filippov committed
82

Alexey Grebenkin's avatar
Alexey Grebenkin committed
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
sata_phy_dev phy(
    // pll reset
    .extrst             (rst),
    // top-level ifaces
    // ref clk from an external source, shall be connected to pads
    .extclk_p           (EXTCLK_P),
    .extclk_n           (EXTCLK_N),
    // sata link data pins
    .txp_out            (TXP),
    .txn_out            (TXN),
    .rxp_in             (RXP),
    .rxn_in             (RXN),

    .clk                (clk),
    .rst                (dev_rst),

    .phy_ready          (phy_ready),

    .ll_data_out        (phy2dev_data),
    .ll_charisk_out     (phy2dev_charisk),
    .ll_err_out         (phy2dev_err),

    .ll_data_in         (dev2phy_data),
Andrey Filippov's avatar
Andrey Filippov committed
106 107 108
    .ll_charisk_in      (dev2phy_isk),
    .serial_delay       (serial_delay) // delay output to check host alignment
    
Alexey Grebenkin's avatar
Alexey Grebenkin committed
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
);

localparam [31:0] PRIM_SYNCP    = {3'd5, 5'd21, 3'd5, 5'd21, 3'd4, 5'd21, 3'd3, 5'd28};
localparam [31:0] PRIM_ALIGNP   = {3'd3, 5'd27, 3'd2, 5'd10, 3'd2, 5'd10, 3'd5, 5'd28};
localparam [31:0] PRIM_XRDYP    = {3'd2, 5'd23, 3'd2, 5'd23, 3'd5, 5'd21, 3'd3, 5'd28};
localparam [31:0] PRIM_SOFP     = {3'd1, 5'd23, 3'd1, 5'd23, 3'd5, 5'd21, 3'd3, 5'd28};
localparam [31:0] PRIM_HOLDAP   = {3'd4, 5'd21, 3'd4, 5'd21, 3'd5, 5'd10, 3'd3, 5'd28};
localparam [31:0] PRIM_HOLDP    = {3'd6, 5'd21, 3'd6, 5'd21, 3'd5, 5'd10, 3'd3, 5'd28};
localparam [31:0] PRIM_EOFP     = {3'd6, 5'd21, 3'd6, 5'd21, 3'd5, 5'd21, 3'd3, 5'd28};
localparam [31:0] PRIM_WTRMP    = {3'd2, 5'd24, 3'd2, 5'd24, 3'd5, 5'd21, 3'd3, 5'd28};
localparam [31:0] PRIM_RRDYP    = {3'd2, 5'd10, 3'd2, 5'd10, 3'd4, 5'd21, 3'd3, 5'd28};
localparam [31:0] PRIM_IPP      = {3'd2, 5'd21, 3'd2, 5'd21, 3'd5, 5'd21, 3'd3, 5'd28};
localparam [31:0] PRIM_DMATP    = {3'd1, 5'd22, 3'd1, 5'd22, 3'd5, 5'd21, 3'd3, 5'd28};
localparam [31:0] PRIM_OKP      = {3'd1, 5'd21, 3'd1, 5'd21, 3'd5, 5'd21, 3'd3, 5'd28};
localparam [31:0] PRIM_ERRP     = {3'd2, 5'd22, 3'd2, 5'd22, 3'd5, 5'd21, 3'd3, 5'd28};

initial begin
    $display("LIST OF PRIMITIVES:");
    $display("SYNC  = %x", PRIM_SYNCP );
    $display("ALIGN = %x", PRIM_ALIGNP);
    $display("XRDY  = %x", PRIM_XRDYP );
    $display("SOF   = %x", PRIM_SOFP  );
    $display("HOLDA = %x", PRIM_HOLDAP);
    $display("HOLD  = %x", PRIM_HOLDP );
    $display("EOF   = %x", PRIM_EOFP  );
    $display("WTRM  = %x", PRIM_WTRMP );
    $display("RRDY  = %x", PRIM_RRDYP );
    $display("IP    = %x", PRIM_IPP   );
    $display("DMAT  = %x", PRIM_DMATP );
    $display("OK    = %x", PRIM_OKP   );
    $display("ERR   = %x", PRIM_ERRP  );
end



integer transmit_lock = 0;
integer receive_lock = 0;
integer suppress_receive = 0;

reg [31:0] receive_data [2047:0];
reg [31:0] receive_data_pause [2047:0];
Andrey Filippov's avatar
Andrey Filippov committed
150
integer    received_size; // number of data FIS payload dwords received (minus head and CRC)
Alexey Grebenkin's avatar
Alexey Grebenkin committed
151 152 153 154 155
reg [31:0] receive_wait_fifo;
reg [31:0] receive_crc;
integer    receive_id = 0;
integer    receive_status = 0;

Andrey Filippov's avatar
Andrey Filippov committed
156 157


Andrey Filippov's avatar
Andrey Filippov committed
158

Alexey Grebenkin's avatar
Alexey Grebenkin committed
159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178
/*
 * Monitor incoming primitives every clock cycle
 * if there is a data transfer request, start a receive sequence
 */
initial forever @ (posedge clk) begin
    if (~transmit_lock) begin
    // transmitting sequence is not started
        if (~receive_lock) begin
        // if for the current xrdy stream we haven't aready started a receiving sequence 
            if (~suppress_receive) begin
            // if we do not intentionally ignore host's transmissions
                if (linkGetPrim(0) == "XRDY") begin
                    linkMonitorFIS(receive_id, 2049, receive_status);
                    receive_id = receive_id + 1;
                end
            end
        end
    end
end

179
function [31:0] scrambleFunc; //SuppressThisWarning VEditor: VDT bug? it is used
Alexey Grebenkin's avatar
Alexey Grebenkin committed
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
    input [31:0] context;
    reg [31:0] next;
    reg [15:0] now;
    begin
        now = context[15:0];

        next[31] = now[12] ^ now[10] ^ now[7] ^ now[3] ^ now[1] ^ now[0];
        next[30] = now[15] ^ now[14] ^ now[12] ^ now[11] ^ now[9] ^ now[6] ^ now[3] ^ now[2] ^ now[0];
        next[29] = now[15] ^ now[13] ^ now[12] ^ now[11] ^ now[10] ^ now[8] ^ now[5] ^ now[3] ^ now[2] ^ now[1];
        next[28] = now[14] ^ now[12] ^ now[11] ^ now[10] ^ now[9] ^ now[7] ^ now[4] ^ now[2] ^ now[1] ^ now[0];
        next[27] = now[15] ^ now[14] ^ now[13] ^ now[12] ^ now[11] ^ now[10] ^ now[9] ^ now[8] ^ now[6] ^ now[1] ^ now[0];
        next[26] = now[15] ^ now[13] ^ now[11] ^ now[10] ^ now[9] ^ now[8] ^ now[7] ^ now[5] ^ now[3] ^ now[0];
        next[25] = now[15] ^ now[10] ^ now[9] ^ now[8] ^ now[7] ^ now[6] ^ now[4] ^ now[3] ^ now[2];
        next[24] = now[14] ^ now[9] ^ now[8] ^ now[7] ^ now[6] ^ now[5] ^ now[3] ^ now[2] ^ now[1];
        next[23] = now[13] ^ now[8] ^ now[7] ^ now[6] ^ now[5] ^ now[4] ^ now[2] ^ now[1] ^ now[0];
        next[22] = now[15] ^ now[14] ^ now[7] ^ now[6] ^ now[5] ^ now[4] ^ now[1] ^ now[0];
        next[21] = now[15] ^ now[13] ^ now[12] ^ now[6] ^ now[5] ^ now[4] ^ now[0];
        next[20] = now[15] ^ now[11] ^ now[5] ^ now[4];
        next[19] = now[14] ^ now[10] ^ now[4] ^ now[3];
        next[18] = now[13] ^ now[9] ^ now[3] ^ now[2];
        next[17] = now[12] ^ now[8] ^ now[2] ^ now[1];
        next[16] = now[11] ^ now[7] ^ now[1] ^ now[0];

        next[15] = now[15] ^ now[14] ^ now[12] ^ now[10] ^ now[6] ^ now[3] ^ now[0];
        next[14] = now[15] ^ now[13] ^ now[12] ^ now[11] ^ now[9] ^ now[5] ^ now[3] ^ now[2];
        next[13] = now[14] ^ now[12] ^ now[11] ^ now[10] ^ now[8] ^ now[4] ^ now[2] ^ now[1];
        next[12] = now[13] ^ now[11] ^ now[10] ^ now[9] ^ now[7] ^ now[3] ^ now[1] ^ now[0];
        next[11] = now[15] ^ now[14] ^ now[10] ^ now[9] ^ now[8] ^ now[6] ^ now[3] ^ now[2] ^ now[0];
        next[10] = now[15] ^ now[13] ^ now[12] ^ now[9] ^ now[8] ^ now[7] ^ now[5] ^ now[3] ^ now[2] ^ now[1];
        next[9] = now[14] ^ now[12] ^ now[11] ^ now[8] ^ now[7] ^ now[6] ^ now[4] ^ now[2] ^ now[1] ^ now[0];
        next[8] = now[15] ^ now[14] ^ now[13] ^ now[12] ^ now[11] ^ now[10] ^ now[7] ^ now[6] ^ now[5] ^ now[1] ^ now[0];
        next[7] = now[15] ^ now[13] ^ now[11] ^ now[10] ^ now[9] ^ now[6] ^ now[5] ^ now[4] ^ now[3] ^ now[0];
        next[6] = now[15] ^ now[10] ^ now[9] ^ now[8] ^ now[5] ^ now[4] ^ now[2];
        next[5] = now[14] ^ now[9] ^ now[8] ^ now[7] ^ now[4] ^ now[3] ^ now[1];
        next[4] = now[13] ^ now[8] ^ now[7] ^ now[6] ^ now[3] ^ now[2] ^ now[0];
        next[3] = now[15] ^ now[14] ^ now[7] ^ now[6] ^ now[5] ^ now[3] ^ now[2] ^ now[1];
        next[2] = now[14] ^ now[13] ^ now[6] ^ now[5] ^ now[4] ^ now[2] ^ now[1] ^ now[0];
        next[1] = now[15] ^ now[14] ^ now[13] ^ now[5] ^ now[4] ^ now[1] ^ now[0];
        next[0] = now[15] ^ now[13] ^ now[4] ^ now[0];

        scrambleFunc = next;
    end
endfunction

224
function [31:0] calculateCRC;  //SuppressThisWarning VEditor VDT bug, it is used
Alexey Grebenkin's avatar
Alexey Grebenkin committed
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 302 303 304 305 306 307 308 309 310 311 312 313
    input [31:0] seed;
    input [31:0] data;
    reg [31:0] crc_bit;
    reg [31:0] new_bit;
    begin
        crc_bit = seed ^ data;

        new_bit[31] = crc_bit[31] ^ crc_bit[30] ^ crc_bit[29] ^ crc_bit[28] ^ crc_bit[27] ^ crc_bit[25] ^ crc_bit[24] ^
                      crc_bit[23] ^ crc_bit[15] ^ crc_bit[11] ^ crc_bit[9]  ^ crc_bit[8]  ^ crc_bit[5];
        new_bit[30] = crc_bit[30] ^ crc_bit[29] ^ crc_bit[28] ^ crc_bit[27] ^ crc_bit[26] ^ crc_bit[24] ^ crc_bit[23] ^
                      crc_bit[22] ^ crc_bit[14] ^ crc_bit[10] ^ crc_bit[8]  ^ crc_bit[7]  ^ crc_bit[4];
        new_bit[29] = crc_bit[31] ^ crc_bit[29] ^ crc_bit[28] ^ crc_bit[27] ^ crc_bit[26] ^ crc_bit[25] ^ crc_bit[23] ^
                      crc_bit[22] ^ crc_bit[21] ^ crc_bit[13] ^ crc_bit[9]  ^ crc_bit[7]  ^ crc_bit[6]  ^ crc_bit[3];
        new_bit[28] = crc_bit[30] ^ crc_bit[28] ^ crc_bit[27] ^ crc_bit[26] ^ crc_bit[25] ^ crc_bit[24] ^ crc_bit[22] ^
                      crc_bit[21] ^ crc_bit[20] ^ crc_bit[12] ^ crc_bit[8]  ^ crc_bit[6]  ^ crc_bit[5]  ^ crc_bit[2];
        new_bit[27] = crc_bit[29] ^ crc_bit[27] ^ crc_bit[26] ^ crc_bit[25] ^ crc_bit[24] ^ crc_bit[23] ^ crc_bit[21] ^
                      crc_bit[20] ^ crc_bit[19] ^ crc_bit[11] ^ crc_bit[7]  ^ crc_bit[5]  ^ crc_bit[4]  ^ crc_bit[1];
        new_bit[26] = crc_bit[31] ^ crc_bit[28] ^ crc_bit[26] ^ crc_bit[25] ^ crc_bit[24] ^ crc_bit[23] ^ crc_bit[22] ^
                      crc_bit[20] ^ crc_bit[19] ^ crc_bit[18] ^ crc_bit[10] ^ crc_bit[6]  ^ crc_bit[4]  ^ crc_bit[3]  ^
                      crc_bit[0];
        new_bit[25] = crc_bit[31] ^ crc_bit[29] ^ crc_bit[28] ^ crc_bit[22] ^ crc_bit[21] ^ crc_bit[19] ^ crc_bit[18] ^
                      crc_bit[17] ^ crc_bit[15] ^ crc_bit[11] ^ crc_bit[8]  ^ crc_bit[3]  ^ crc_bit[2];
        new_bit[24] = crc_bit[30] ^ crc_bit[28] ^ crc_bit[27] ^ crc_bit[21] ^ crc_bit[20] ^ crc_bit[18] ^ crc_bit[17] ^
                      crc_bit[16] ^ crc_bit[14] ^ crc_bit[10] ^ crc_bit[7]  ^ crc_bit[2]  ^ crc_bit[1];
        new_bit[23] = crc_bit[31] ^ crc_bit[29] ^ crc_bit[27] ^ crc_bit[26] ^ crc_bit[20] ^ crc_bit[19] ^ crc_bit[17] ^
                      crc_bit[16] ^ crc_bit[15] ^ crc_bit[13] ^ crc_bit[9]  ^ crc_bit[6]  ^ crc_bit[1]  ^ crc_bit[0];
        new_bit[22] = crc_bit[31] ^ crc_bit[29] ^ crc_bit[27] ^ crc_bit[26] ^ crc_bit[24] ^ crc_bit[23] ^ crc_bit[19] ^
                      crc_bit[18] ^ crc_bit[16] ^ crc_bit[14] ^ crc_bit[12] ^ crc_bit[11] ^ crc_bit[9]  ^ crc_bit[0];
        new_bit[21] = crc_bit[31] ^ crc_bit[29] ^ crc_bit[27] ^ crc_bit[26] ^ crc_bit[24] ^ crc_bit[22] ^ crc_bit[18] ^
                      crc_bit[17] ^ crc_bit[13] ^ crc_bit[10] ^ crc_bit[9]  ^ crc_bit[5];
        new_bit[20] = crc_bit[30] ^ crc_bit[28] ^ crc_bit[26] ^ crc_bit[25] ^ crc_bit[23] ^ crc_bit[21] ^ crc_bit[17] ^
                      crc_bit[16] ^ crc_bit[12] ^ crc_bit[9]  ^ crc_bit[8]  ^ crc_bit[4];
        new_bit[19] = crc_bit[29] ^ crc_bit[27] ^ crc_bit[25] ^ crc_bit[24] ^ crc_bit[22] ^ crc_bit[20] ^ crc_bit[16] ^
                      crc_bit[15] ^ crc_bit[11] ^ crc_bit[8]  ^ crc_bit[7]  ^ crc_bit[3];
        new_bit[18] = crc_bit[31] ^ crc_bit[28] ^ crc_bit[26] ^ crc_bit[24] ^ crc_bit[23] ^ crc_bit[21] ^ crc_bit[19] ^
                      crc_bit[15] ^ crc_bit[14] ^ crc_bit[10] ^ crc_bit[7]  ^ crc_bit[6]  ^ crc_bit[2];
        new_bit[17] = crc_bit[31] ^ crc_bit[30] ^ crc_bit[27] ^ crc_bit[25] ^ crc_bit[23] ^ crc_bit[22] ^ crc_bit[20] ^
                      crc_bit[18] ^ crc_bit[14] ^ crc_bit[13] ^ crc_bit[9]  ^ crc_bit[6]  ^ crc_bit[5]  ^ crc_bit[1];
        new_bit[16] = crc_bit[30] ^ crc_bit[29] ^ crc_bit[26] ^ crc_bit[24] ^ crc_bit[22] ^ crc_bit[21] ^ crc_bit[19] ^
                      crc_bit[17] ^ crc_bit[13] ^ crc_bit[12] ^ crc_bit[8]  ^ crc_bit[5]  ^ crc_bit[4]  ^ crc_bit[0];
        new_bit[15] = crc_bit[30] ^ crc_bit[27] ^ crc_bit[24] ^ crc_bit[21] ^ crc_bit[20] ^ crc_bit[18] ^ crc_bit[16] ^
                      crc_bit[15] ^ crc_bit[12] ^ crc_bit[9]  ^ crc_bit[8]  ^ crc_bit[7]  ^ crc_bit[5]  ^ crc_bit[4]  ^
                      crc_bit[3];
        new_bit[14] = crc_bit[29] ^ crc_bit[26] ^ crc_bit[23] ^ crc_bit[20] ^ crc_bit[19] ^ crc_bit[17] ^ crc_bit[15] ^
                      crc_bit[14] ^ crc_bit[11] ^ crc_bit[8]  ^ crc_bit[7]  ^ crc_bit[6]  ^ crc_bit[4]  ^ crc_bit[3]  ^
                      crc_bit[2];
        new_bit[13] = crc_bit[31] ^ crc_bit[28] ^ crc_bit[25] ^ crc_bit[22] ^ crc_bit[19] ^ crc_bit[18] ^ crc_bit[16] ^
                      crc_bit[14] ^ crc_bit[13] ^ crc_bit[10] ^ crc_bit[7]  ^ crc_bit[6]  ^ crc_bit[5]  ^ crc_bit[3]  ^
                      crc_bit[2]  ^ crc_bit[1];
        new_bit[12] = crc_bit[31] ^ crc_bit[30] ^ crc_bit[27] ^ crc_bit[24] ^ crc_bit[21] ^ crc_bit[18] ^ crc_bit[17] ^
                      crc_bit[15] ^ crc_bit[13] ^ crc_bit[12] ^ crc_bit[9]  ^ crc_bit[6]  ^ crc_bit[5]  ^ crc_bit[4]  ^
                      crc_bit[2]  ^ crc_bit[1]  ^ crc_bit[0];
        new_bit[11] = crc_bit[31] ^ crc_bit[28] ^ crc_bit[27] ^ crc_bit[26] ^ crc_bit[25] ^ crc_bit[24] ^ crc_bit[20] ^
                      crc_bit[17] ^ crc_bit[16] ^ crc_bit[15] ^ crc_bit[14] ^ crc_bit[12] ^ crc_bit[9]  ^ crc_bit[4]  ^
                      crc_bit[3]  ^ crc_bit[1]  ^ crc_bit[0];
        new_bit[10] = crc_bit[31] ^ crc_bit[29] ^ crc_bit[28] ^ crc_bit[26] ^ crc_bit[19] ^ crc_bit[16] ^ crc_bit[14] ^
                      crc_bit[13] ^ crc_bit[9]  ^ crc_bit[5]  ^ crc_bit[3]  ^ crc_bit[2]  ^ crc_bit[0];
        new_bit[9]  = crc_bit[29] ^ crc_bit[24] ^ crc_bit[23] ^ crc_bit[18] ^ crc_bit[13] ^ crc_bit[12] ^ crc_bit[11] ^
                      crc_bit[9]  ^ crc_bit[5]  ^ crc_bit[4]  ^ crc_bit[2]  ^ crc_bit[1];
        new_bit[8]  = crc_bit[31] ^ crc_bit[28] ^ crc_bit[23] ^ crc_bit[22] ^ crc_bit[17] ^ crc_bit[12] ^ crc_bit[11] ^
                      crc_bit[10] ^ crc_bit[8]  ^ crc_bit[4]  ^ crc_bit[3]  ^ crc_bit[1]  ^ crc_bit[0];
        new_bit[7]  = crc_bit[29] ^ crc_bit[28] ^ crc_bit[25] ^ crc_bit[24] ^ crc_bit[23] ^ crc_bit[22] ^ crc_bit[21] ^
                      crc_bit[16] ^ crc_bit[15] ^ crc_bit[10] ^ crc_bit[8]  ^ crc_bit[7]  ^ crc_bit[5]  ^ crc_bit[3]  ^
                      crc_bit[2]  ^ crc_bit[0];
        new_bit[6]  = crc_bit[30] ^ crc_bit[29] ^ crc_bit[25] ^ crc_bit[22] ^ crc_bit[21] ^ crc_bit[20] ^ crc_bit[14] ^
                      crc_bit[11] ^ crc_bit[8]  ^ crc_bit[7]  ^ crc_bit[6]  ^ crc_bit[5]  ^ crc_bit[4]  ^ crc_bit[2]  ^
                      crc_bit[1];
        new_bit[5]  = crc_bit[29] ^ crc_bit[28] ^ crc_bit[24] ^ crc_bit[21] ^ crc_bit[20] ^ crc_bit[19] ^ crc_bit[13] ^
                      crc_bit[10] ^ crc_bit[7]  ^ crc_bit[6]  ^ crc_bit[5]  ^ crc_bit[4]  ^ crc_bit[3]  ^ crc_bit[1]  ^
                      crc_bit[0];
        new_bit[4]  = crc_bit[31] ^ crc_bit[30] ^ crc_bit[29] ^ crc_bit[25] ^ crc_bit[24] ^ crc_bit[20] ^ crc_bit[19] ^
                      crc_bit[18] ^ crc_bit[15] ^ crc_bit[12] ^ crc_bit[11] ^ crc_bit[8]  ^ crc_bit[6]  ^ crc_bit[4]  ^
                      crc_bit[3]  ^ crc_bit[2]  ^ crc_bit[0];
        new_bit[3]  = crc_bit[31] ^ crc_bit[27] ^ crc_bit[25] ^ crc_bit[19] ^ crc_bit[18] ^ crc_bit[17] ^ crc_bit[15] ^
                      crc_bit[14] ^ crc_bit[10] ^ crc_bit[9]  ^ crc_bit[8]  ^ crc_bit[7]  ^ crc_bit[3]  ^ crc_bit[2]  ^
                      crc_bit[1];
        new_bit[2]  = crc_bit[31] ^ crc_bit[30] ^ crc_bit[26] ^ crc_bit[24] ^ crc_bit[18] ^ crc_bit[17] ^ crc_bit[16] ^
                      crc_bit[14] ^ crc_bit[13] ^ crc_bit[9]  ^ crc_bit[8]  ^ crc_bit[7]  ^ crc_bit[6]  ^ crc_bit[2]  ^
                      crc_bit[1]  ^ crc_bit[0];
        new_bit[1]  = crc_bit[28] ^ crc_bit[27] ^ crc_bit[24] ^ crc_bit[17] ^ crc_bit[16] ^ crc_bit[13] ^ crc_bit[12] ^
                      crc_bit[11] ^ crc_bit[9]  ^ crc_bit[7]  ^ crc_bit[6]  ^ crc_bit[1]  ^ crc_bit[0];
        new_bit[0]  = crc_bit[31] ^ crc_bit[30] ^ crc_bit[29] ^ crc_bit[28] ^ crc_bit[26] ^ crc_bit[25] ^ crc_bit[24] ^
                      crc_bit[16] ^ crc_bit[12] ^ crc_bit[10] ^ crc_bit[9]  ^ crc_bit[6]  ^ crc_bit[0];

        calculateCRC = new_bit;
    end
endfunction

// stub TODO
314
function tranCheckFIS; //SuppressThisWarning VEditor: VDT bug - the function is used in conditional expression
Alexey Grebenkin's avatar
Alexey Grebenkin committed
315 316
    input count;
    begin
317 318
//        $display("[Device] TRANSPORT: Says the FIS is valid");
        DEV_TITLE = "Says the FIS is valid";
Andrey Filippov's avatar
Andrey Filippov committed
319
        $display("[Device] TRANSPORT: %s @%t", DEV_TITLE, $time);   
320
        
Alexey Grebenkin's avatar
Alexey Grebenkin committed
321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353
        tranCheckFIS = 0; // always tell LL the FIS os OK
    end
endfunction

// TODO align every 256 dwords!
/*
 * Receives data from a host. ~Link Receive FSM
 * Correct execution, as it shall be w/o errors from a device side.
 *
 * Received data is stored in receive_data memory. 
 * Data is received by a dword // TODO make support for uneven words (16bit each) count
 *
 * Each data bundle has corresponding "pause" register, stored in a memory 'receive_data_pause'
 * It represents a time (in clock cycles), for which the device shall send HOLD primitives after
 * current data bundle reception. If after HOLD request data is still coming, consequetive 'pause's are summed up.
 * Could be used to test timeout watchdogs of the host.
 *
 * receive_wait_fifo shows how many clock cycles receiver shall spent before it allows the host to transmit data
 *
 * Parameters:
 * id - reception id, shown in logs
 * dmat_index - after this count of received data dwords DMAT primitive would be sent to the line 
 * status - returns 0 when the host acknowledges the transaction with OK code, 
 *                  1 when                                       with ERR code
 *          if it's 1, there are 3 options:
 *          a) Generated CRC is invalid
 *          b) Scrambler messed up
 *          c) There is an error in the host
 */
task linkMonitorFIS;
    input integer id;
    input integer dmat_index;
    output integer status;
354
    reg [111:0] rprim;
Alexey Grebenkin's avatar
Alexey Grebenkin committed
355 356 357 358
    integer pause;
    integer rcv_stop;
    integer rcv_ignore;
    integer cnt;
359
    reg [31:0] descrambled_data;
Alexey Grebenkin's avatar
Alexey Grebenkin committed
360 361
    reg [31:0] scrambler_value;
    reg [31:0] crc;
362
    reg        crc_match;
Alexey Grebenkin's avatar
Alexey Grebenkin committed
363 364 365 366 367 368 369 370 371 372
    begin
        pause = receive_wait_fifo;
        status = 0;
        rcv_ignore = 0;
        rcv_stop = 0;
        crc = 32'h52325032;// crc seed
        scrambler_value = {16'hf0f6, 16'h0000}; // scrambler seed
        cnt = 0;
    // current rprim = XRDY
        rprim = "XRDY";
373 374 375 376 377 378
        DEV_TITLE = "Detected incoming transmission";
        $display("[Device] LINK:      %s @%t", DEV_TITLE, $time);
        DEV_TITLE = "Waiting to empty input buffer";
        DEV_DATA =   pause;
//        $display("[Device] LINK:      Waiting %h cycles to empty input buffer", pause);
        $display("[Device] LINK:      %s, pause = %d @%t", DEV_TITLE, DEV_DATA, $time);
Alexey Grebenkin's avatar
Alexey Grebenkin committed
379 380 381
        while (pause > 0) begin
    // L_RcvWaitFifo
            if (~phy_ready) begin
382 383
                DEV_TITLE = "Unexpected line disconnect #1";
                $display("[Device] LINK:      %s @%t", DEV_TITLE, $time);
Andrey Filippov's avatar
Andrey Filippov committed
384
                #100;
Alexey Grebenkin's avatar
Alexey Grebenkin committed
385 386 387
                $finish;
            end
            if (rprim != "XRDY") begin
388 389 390 391
//                $display("[Device] LINK:      Reception terminated by the host, reception id = %d", id);
                DEV_TITLE = "Reception terminated by the host #1";
                DEV_DATA =  id;
                $display("[Device] LINK:      %s, reception id = %d @%t", DEV_TITLE, DEV_DATA, $time);
Andrey Filippov's avatar
Andrey Filippov committed
392
                #100;
Alexey Grebenkin's avatar
Alexey Grebenkin committed
393 394 395 396 397 398 399
                $finish;
            end
            @ (posedge clk)
                rprim = linkGetPrim(0);
        end
    // L_RcvChkRdy
        if (~phy_ready) begin
400 401 402
//            $display("[Device] LINK:      Unexpected line disconnect");
            DEV_TITLE = "Unexpected line disconnect #2";
            $display("[Device] LINK:      %s @%t", DEV_TITLE, $time);
Andrey Filippov's avatar
Andrey Filippov committed
403
            #100;
Alexey Grebenkin's avatar
Alexey Grebenkin committed
404 405 406
            $finish;
        end
        if (rprim != "XRDY") begin
407 408 409 410
//            $display("[Device] LINK:      Reception terminated by the host, reception id = %d", id);
            DEV_TITLE = "Reception terminated by the host #2";
            DEV_DATA =  id;
            $display("[Device] LINK:      %s, reception id = %d @%t", DEV_TITLE, DEV_DATA, $time);
Andrey Filippov's avatar
Andrey Filippov committed
411
            #100;
Alexey Grebenkin's avatar
Alexey Grebenkin committed
412 413 414
            $finish;
        end
        linkSendPrim("RRDY");
415 416 417 418
//        $display("[Device] LINK:      Starting the reception");
        DEV_TITLE = "Starting the reception";
        $display("[Device] LINK:      %s @%t", DEV_TITLE, $time);
        
Alexey Grebenkin's avatar
Alexey Grebenkin committed
419 420 421 422
        @ (posedge clk)
            rprim = linkGetPrim(0);
        while (rprim != "SOF") begin
            if (~phy_ready) begin
423 424 425
//                $display("[Device] LINK:      Unexpected line disconnect");
                DEV_TITLE = "Unexpected line disconnect #3";
                $display("[Device] LINK:      %s @%t", DEV_TITLE, $time);
Andrey Filippov's avatar
Andrey Filippov committed
426
                #100;
Alexey Grebenkin's avatar
Alexey Grebenkin committed
427 428
                $finish;
            end
Andrey Filippov's avatar
Andrey Filippov committed
429
            if (rprim == "ALIGN") begin // ALIGNp in pairs can be inserted anywhere (TODO: check they are paired?)
430
//                $display("[Device] LINK:      Reception terminated by the host, reception id = %d", id);
Andrey Filippov's avatar
Andrey Filippov committed
431
                DEV_TITLE = "Got ALIGNp";
432 433
                DEV_DATA =  id;
                $display("[Device] LINK:      %s, reception id = %d @%t", DEV_TITLE, DEV_DATA, $time);
Andrey Filippov's avatar
Andrey Filippov committed
434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454
                @ (posedge clk)
                    rprim = linkGetPrim(0);
                if (rprim != "ALIGN") begin // ALIGNp in pairs can be inserted anywhere (TODO: check they are paired?)
                    DEV_TITLE = "Was expecting another ALIGNp";
                    DEV_DATA =  id;
                    $display("[Device] LINK:      %s, reception id = %d got primitive = %s @%t", DEV_TITLE, DEV_DATA, rprim, $time);
                    #100;
                    $finish;
                end else begin
                    DEV_TITLE = "Got second ALIGNp";
                    DEV_DATA =  id;
                    $display("[Device] LINK:      %s, reception id = %d @%t", DEV_TITLE, DEV_DATA, $time);
                end
            end
            
            if ((rprim != "XRDY") && (rprim != "ALIGN")) begin // ALIGNp in pairs can be inserted anywhere (TODO: check they are paired?)
                DEV_TITLE = "Reception terminated by the host #3";
                DEV_DATA =  id;
                $display("[Device] LINK:      %s, reception id = %d, rprim = %s @%t", DEV_TITLE, DEV_DATA, rprim, $time);
//                #100;
//                $finish;
Alexey Grebenkin's avatar
Alexey Grebenkin committed
455 456 457 458 459 460
            end
            @ (posedge clk)
                rprim = linkGetPrim(0);
        end
    // L_RcvData
        if (~phy_ready) begin
461 462 463
//            $display("[Device] LINK:      Unexpected line disconnect");
            DEV_TITLE = "Unexpected line disconnect #4";
            $display("[Device] LINK:      %s @%t", DEV_TITLE, $time);
Andrey Filippov's avatar
Andrey Filippov committed
464
            #100;
Alexey Grebenkin's avatar
Alexey Grebenkin committed
465 466
            $finish;
        end
467 468 469
//        $display("[Device] LINK:      Detected Start of FIS");
        DEV_TITLE = "Detected Start of FIS";
        $display("[Device] LINK:      %s @%t", DEV_TITLE, $time);
Alexey Grebenkin's avatar
Alexey Grebenkin committed
470 471 472 473 474 475
        linkSendPrim("IP");
        @ (posedge clk)
            rprim = linkGetPrim(0);
        pause = 0;
        while (rcv_stop == 0) begin
            if (~phy_ready) begin
476 477 478
//                $display("[Device] LINK:      Unexpected line disconnect");
                DEV_TITLE = "Unexpected line disconnect #5";
                $display("[Device] LINK:      %s @%t", DEV_TITLE, $time);
Andrey Filippov's avatar
Andrey Filippov committed
479
                #100;
Alexey Grebenkin's avatar
Alexey Grebenkin committed
480 481 482
                $finish;
            end
            if (rprim == "SYNC") begin
483 484 485 486
//                $display("[Device] LINK:      Reception terminated by the host, reception id = %d", id);
                DEV_TITLE = "Reception terminated by the host #4";
                DEV_DATA =  id;
                $display("[Device] LINK:      %s, reception id = %d @%t", DEV_TITLE, DEV_DATA, $time);
Andrey Filippov's avatar
Andrey Filippov committed
487
                #100;
Alexey Grebenkin's avatar
Alexey Grebenkin committed
488 489 490
                $finish;
            end
            if (rprim == "SCRAP") begin
491 492 493 494
//                $display("[Device] LINK:      Bad primitives from the host, is data = %h, data = %h, reception id = %d", linkIsData(0), linkGetData(0), id);
                DEV_TITLE = "Bad primitives from the host #1";
                DEV_DATA =  id;
                $display("[Device] LINK:      %s, is data = %h, data = %h, reception id = %d @%t", DEV_TITLE, linkIsData(0), linkGetData(0), DEV_DATA, $time);
Andrey Filippov's avatar
Andrey Filippov committed
495
                #100;
Alexey Grebenkin's avatar
Alexey Grebenkin committed
496 497 498
                $finish;
            end
            if (rprim == "EOF") begin
499 500 501
//                $display("[Device] LINK:      Detected End of FIS");
                DEV_TITLE = "Detected End of FIS";
                $display("[Device] LINK:      %s @%t", DEV_TITLE, $time);
Alexey Grebenkin's avatar
Alexey Grebenkin committed
502 503 504 505 506 507 508
                rcv_stop = 1;
            end
            else
            if (pause > 0) begin
                pause = pause - 1;
                linkSendPrim("HOLD");
                if (rprim == "HOLDA") begin
509 510 511
//                    $display("[Device] LINK:      The pause is acknowledged by the host, chilling out");
                    DEV_TITLE = "The pause is acknowledged by the host, chilling out";
                    $display("[Device] LINK:      %s @%t", DEV_TITLE, $time);
Alexey Grebenkin's avatar
Alexey Grebenkin committed
512 513 514
                    rcv_ignore = 1;
                end
                else begin
515 516 517
//                    $display("[Device] LINK:      Asked for a pause");
                    DEV_TITLE = "Asked for a pause";
                    $display("[Device] LINK:      %s @%t", DEV_TITLE, $time);
Alexey Grebenkin's avatar
Alexey Grebenkin committed
518 519 520 521 522
                    rcv_ignore = 0;
                end
            end
            else
            if (rprim == "HOLD") begin
523 524 525
//                $display("[Device] LINK:      the host asked for a pause, acknowledging");
                DEV_TITLE = "the host asked for a pause, acknowledging";
                $display("[Device] LINK:      %s @%t", DEV_TITLE, $time);
Alexey Grebenkin's avatar
Alexey Grebenkin committed
526 527 528 529 530 531 532 533
                linkSendPrim("HOLDA");
                rcv_ignore = 1;
            end
            else begin
                linkSendPrim("IP");
                rcv_ignore = 0;
            end
            if (rprim == "WTRM") begin
534 535 536 537
//                $display("[Device] LINK:      Host invalidated the reception, reception id = %d", id);
                DEV_TITLE = "Host invalidated the reception";
                DEV_DATA =  id;
                $display("[Device] LINK:      %s, reception id = %d @%t", DEV_TITLE, DEV_DATA, $time);
Alexey Grebenkin's avatar
Alexey Grebenkin committed
538 539 540
                rcv_stop = 2;
            end
            if ((rcv_stop == 0) && (rcv_ignore == 0)) begin
541 542 543 544
            
            
                if (rprim == "ALIGN") begin
                    DEV_TITLE = "ALIGN got";
545
                    DEV_DATA =  id;
546 547 548 549 550 551 552
                    $display("[Device] LINK:      %s, reception id = %d, cnt = %d @%t", DEV_TITLE, DEV_DATA, cnt, $time);
                end else begin
                    if (cnt > 2048) begin
    //                    $display("[Device] LINK:      Wrong data dwords count received, reception id = %d", id);
                        DEV_TITLE = "Wrong data dwords count received";
                        DEV_DATA =  id;
                        $display("[Device] LINK:      %s, reception id = %d @%t", DEV_TITLE, DEV_DATA, $time);
Andrey Filippov's avatar
Andrey Filippov committed
553
                        #100;
554 555 556 557 558 559 560 561 562 563 564 565
                        $finish;
                    end
                    if (cnt >= dmat_index) begin
                        linkSendPrim("DMAT");
                    end
                    scrambler_value = scrambleFunc({16'b0,scrambler_value[31:16]});
///                    receive_data[cnt] = linkGetData(0) ^ scrambler_value;
                    descrambled_data = linkGetData(0) ^ scrambler_value;
                    receive_data[cnt] = descrambled_data;
                    DEV_TITLE = "Got data";
                    DEV_DATA =  receive_data[cnt];
                    $display("[Device] LINK:      %s = %h (#%d) @%t", DEV_TITLE, DEV_DATA, cnt, $time);
Alexey Grebenkin's avatar
Alexey Grebenkin committed
566
                    pause = pause + receive_data_pause[cnt];
567 568 569 570 571 572
                    crc_match = (crc == descrambled_data);
                    crc = calculateCRC(crc, descrambled_data); // running crc. shall be 0 
//                    crc_match = (crc == receive_data[cnt]);
                    cnt = cnt + 1;
                    if (cnt <= 2048)
                        pause = pause + receive_data_pause[cnt];
573 574 575 576
`ifdef TERMINATE_DMA_H2D
        linkSendPrim("DMAT");
`endif
                        
577
                end    
Alexey Grebenkin's avatar
Alexey Grebenkin committed
578 579 580 581 582
            end
            @ (posedge clk)
                rprim = linkGetPrim(0);
        end
        if (cnt < 2) begin
583 584 585
//            $display("[Device] LINK:      Incorrect number of received words");
            DEV_TITLE = "Incorrect number of received words";
            $display("[Device] LINK:      %s @%t", DEV_TITLE, $time);
Andrey Filippov's avatar
Andrey Filippov committed
586
            #100;
Alexey Grebenkin's avatar
Alexey Grebenkin committed
587 588
            $finish;
        end
589 590 591 592
//        $display("[Device] LINK:      Running CRC after all data was received = %h", crc);
        DEV_TITLE = "Running CRC after all data was received";
        DEV_DATA =  crc;
        $display("[Device] LINK:      %s = %h @%t", DEV_TITLE, DEV_DATA, $time);
Andrey Filippov's avatar
Andrey Filippov committed
593
        received_size = cnt - 2; // data payload size in DWORDs
594 595
//        if (crc != 32'h88c21025) begin // running disparity when data crc matches actual received crc
        if (!crc_match) begin // running disparity when data crc matches actual received crc
596 597 598
//            $display("[Device] LINK:      Running CRC check failed");
            DEV_TITLE = "Running CRC check failed";
            $display("[Device] LINK:      %s @%t", DEV_TITLE, $time);
Alexey Grebenkin's avatar
Alexey Grebenkin committed
599 600 601
            rcv_stop = 2;
        end 
        else begin
602 603 604
//            $display("[Device] LINK:      Running CRC OK");
            DEV_TITLE = "Running CRC OK";
            $display("[Device] LINK:      %s @%t", DEV_TITLE, $time);            
Alexey Grebenkin's avatar
Alexey Grebenkin committed
605 606 607 608
        end
        if (rcv_stop == 1) begin // ordinary path
        // L_RcvEOF
            if (~phy_ready) begin
609 610 611
//                $display("[Device] LINK:      Unexpected line disconnect");
                DEV_TITLE = "Unexpected line disconnect #6";
                $display("[Device] LINK:      %s @%t", DEV_TITLE, $time);
Andrey Filippov's avatar
Andrey Filippov committed
612
                #100;
Alexey Grebenkin's avatar
Alexey Grebenkin committed
613 614 615
                $finish;
            end
            if (rprim == "SYNC") begin
616 617 618 619
//                $display("[Device] LINK:      Reception terminated by the host, reception id = %d", id);
                DEV_TITLE = "Reception terminated by the host #5";
                DEV_DATA =  id;
                $display("[Device] LINK:      %s, reception id = %d @%t", DEV_TITLE, DEV_DATA, $time);
Andrey Filippov's avatar
Andrey Filippov committed
620
                #100;
Alexey Grebenkin's avatar
Alexey Grebenkin committed
621 622 623
                $finish;
            end
            if ((rprim == "SCRAP") || (rprim == "DATA")) begin
624 625 626 627
//                $display("[Device] LINK:      Bad primitives from the host, is data = %h, data = %h, reception id = %d", linkIsData(0), linkGetData(0), id);
                DEV_TITLE = "Bad primitives from the host #2";
                DEV_DATA =  id;
                $display("[Device] LINK:      %s, is data = %h, data = %h, reception id = %d @%t", DEV_TITLE, linkIsData(0), linkGetData(0), DEV_DATA, $time);
Andrey Filippov's avatar
Andrey Filippov committed
628
                #100;
Alexey Grebenkin's avatar
Alexey Grebenkin committed
629 630 631 632 633 634
                $finish;
            end
            @ (posedge clk)
                rprim = linkGetPrim(0);
        // L_GoodCRC
            if (~phy_ready) begin
635 636 637
//                $display("[Device] LINK:      Unexpected line disconnect");
                DEV_TITLE = "Unexpected line disconnect #7";
                $display("[Device] LINK:      %s @%t", DEV_TITLE, $time);
Andrey Filippov's avatar
Andrey Filippov committed
638
                #100;
Alexey Grebenkin's avatar
Alexey Grebenkin committed
639 640 641
                $finish;
            end
            if (rprim == "SYNC") begin
642 643 644 645
//                $display("[Device] LINK:      Reception terminated by the host, reception id = %d", id);
                DEV_TITLE = "Reception terminated by the host #6";
                DEV_DATA =  id;
                $display("[Device] LINK:      %s, reception id = %d @%t", DEV_TITLE, DEV_DATA, $time);
Andrey Filippov's avatar
Andrey Filippov committed
646
                #100;
Alexey Grebenkin's avatar
Alexey Grebenkin committed
647 648 649
                $finish;
            end
            if ((rprim == "SCRAP") || (rprim == "DATA")) begin
650 651 652 653
//                $display("[Device] LINK:      Bad primitives from the host, is data = %h, data = %h, reception id = %d", linkIsData(0), linkGetData(0), id);
                DEV_TITLE = "Bad primitives from the host #3";
                DEV_DATA =  id;
                $display("[Device] LINK:      %s, is data = %h, data = %h, reception id = %d @%t", DEV_TITLE, linkIsData(0), linkGetData(0), DEV_DATA, $time);
Andrey Filippov's avatar
Andrey Filippov committed
654
                #100;
Alexey Grebenkin's avatar
Alexey Grebenkin committed
655 656 657 658 659 660 661 662 663 664
                $finish;
            end
            if (tranCheckFIS(cnt - 1)) begin
                rcv_stop = 2;
            end
        end
        if (rcv_stop == 2) begin
    // L_BadEnd
            status = 1;
            linkSendPrim("ERR");
665 666 667 668
//            $display("[Device] LINK:      Found an error");
            DEV_TITLE = "Found an error";
            $display("[Device] LINK:      %s @%t", DEV_TITLE, $time);
            
Alexey Grebenkin's avatar
Alexey Grebenkin committed
669 670 671 672 673 674 675 676 677 678
        end
        else begin
    // L_GoodEnd
            status = 0;
            linkSendPrim("OK");
        end
        @ (posedge clk)
            rprim = linkGetPrim(0);
        while (rprim != "SYNC") begin
            if (~phy_ready) begin
679 680 681
//                $display("[Device] LINK:      Unexpected line disconnect");
                DEV_TITLE = "Unexpected line disconnect #8";
                $display("[Device] LINK:      %s @%t", DEV_TITLE, $time);
Andrey Filippov's avatar
Andrey Filippov committed
682
                #100;
Alexey Grebenkin's avatar
Alexey Grebenkin committed
683 684 685
                $finish;
            end
            if ((rprim == "SCRAP") || (rprim == "DATA")) begin
686 687 688 689
//                $display("[Device] LINK:      Bad primitives from the host, is data = %h, data = %h, reception id = %d", linkIsData(0), linkGetData(0), id);
                DEV_TITLE = "Bad primitives from the host #4";
                DEV_DATA =  id;
                $display("[Device] LINK:      %s, is data = %h, data = %h, reception id = %d @%t", DEV_TITLE, linkIsData(0), linkGetData(0), DEV_DATA, $time);
Andrey Filippov's avatar
Andrey Filippov committed
690
                #100;
Alexey Grebenkin's avatar
Alexey Grebenkin committed
691 692 693 694 695 696 697 698
                $finish;
            end
            @ (posedge clk)
                rprim = linkGetPrim(0);
        end
    // L_IDLE
        linkSendPrim("SYNC");
        if (status == 1) begin
699 700 701 702 703
//            $display("[Device] LINK:      Reception done, errors detected, reception id = %d", id);
            DEV_TITLE = "Reception done, errors detected";
            DEV_DATA =  id;
            $display("[Device] LINK:      %s, reception id = %d @%t", DEV_TITLE, DEV_DATA, $time);
            
Alexey Grebenkin's avatar
Alexey Grebenkin committed
704 705 706
        end
        else
        if (status == 0) begin
707 708 709 710
//            $display("[Device] LINK:      Reception done OK, reception id = %d", id);
            DEV_TITLE = "Reception done OK";
            DEV_DATA =  id;
            $display("[Device] LINK:      %s, reception id = %d @%t", DEV_TITLE, DEV_DATA, $time);
Alexey Grebenkin's avatar
Alexey Grebenkin committed
711 712 713 714 715 716
        end
    end
endtask



Andrey Filippov's avatar
Andrey Filippov committed
717
reg [31:0] transmit_data [2047:0];          // @SuppressThisWarning VEditor - Assigned in testbench
Alexey Grebenkin's avatar
Alexey Grebenkin committed
718
reg [31:0] transmit_data_pause [2047:0];
Andrey Filippov's avatar
Andrey Filippov committed
719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738
reg [31:0] transmit_crc; // never assigned  // @SuppressThisWarning VEditor - Assigned in testbench
task clear_transmit_pause; // @SuppressThisWarning VEditor - Used in testbench
    input [31:0] pause_val;
    integer i;
    begin
    for (i = 0; i < 2048; i = i + 1) begin
        transmit_data_pause[i] = pause_val;
    end
    end
endtask

// Wait for device phy ready (full COMRESET/COMINIT sequence) and wait to send several SYNCp primitives so host knows it is done
task wait_ready; // @SuppressThisWarning VEditor - Used in testbench
    input integer num_syncp;
    begin
        wait (phy_ready);
        repeat(num_syncp + 1) @(posedge clk);
    end
endtask
    
Andrey Filippov's avatar
Andrey Filippov committed
739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754
/*
task send_good_status; // @SuppressThisWarning VEditor - Used in testbench
    input integer id;
    input    [2:0] dev_specific_status_bits;
    input          irq;
    output integer status;
    begin
        transmit_data[0] = FIS_D2HR | (irq? 'h4000:0) | (dev_specific_status_bits << 20) | 'h1000000;
        transmit_data[1] = 1;
        transmit_data[2] = 0;
        transmit_data[3] = 1;
        transmit_data[4] = 0;
        linkTransmitFIS(id, 5, 0, status);        
    end
endtask
*/
Andrey Filippov's avatar
Andrey Filippov committed
755 756 757 758 759 760
task send_good_status; // @SuppressThisWarning VEditor - Used in testbench
    input integer id;
    input    [2:0] dev_specific_status_bits;
    input          irq;
    output integer status;
    begin
Andrey Filippov's avatar
Andrey Filippov committed
761 762 763 764 765
        send_D2HR(id,
                  irq,
                  {1'b0,dev_specific_status_bits,4'b0}, // status
                   1,                                   // error
                   0,                                   // device
766
                   1,                                   // lba_low
Andrey Filippov's avatar
Andrey Filippov committed
767
                   0,                                   // lba_high
768
                   1,                                   // count
Andrey Filippov's avatar
Andrey Filippov committed
769 770
                   status);                             // output: result status
                   
771 772 773 774 775 776
//        transmit_data[0] = FIS_D2HR | (irq? 'h4000:0) | (dev_specific_status_bits << 20) | 'h1000000;
//        transmit_data[1] = 1;
//        transmit_data[2] = 0;
//        transmit_data[3] = 1;
//        transmit_data[4] = 0;
//        linkTransmitFIS(id, 5, 0, status);        
Andrey Filippov's avatar
Andrey Filippov committed
777 778
    end
endtask
779

Andrey Filippov's avatar
Andrey Filippov committed
780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802
task send_D2HR; // @SuppressThisWarning VEditor - Used in testbench
    input integer  id;
    input          irq;
    input    [7:0] sts;
    input    [7:0] error;
    input    [7:0] device;
    input   [23:0] lba_low;
    input   [23:0] lba_high;
    input   [15:0] count;
    output integer status;
    begin
        transmit_data[0] = FIS_D2HR | (irq? 'h4000:0) | (sts << 16) | (error << 24);
        transmit_data[1] = {device,lba_low};
        transmit_data[2] = {8'b0,  lba_high};
        transmit_data[3] = {16'b0,count};
        transmit_data[4] = 0;
        linkTransmitFIS(id, 5, 0, status);        
    end
endtask




803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829
task send_pio_setup; // @SuppressThisWarning VEditor - Used in testbench
    input integer id;
    input          d2h; // 'D' bit: 1 - Data will be D2H, 0 - H2D
    input          irq; // Set I bit
    input    [7:0] xmit_status;
    input    [7:0] xmit_error;
    input    [7:0] xmit_e_status;
    input   [15:0] xfer_count;
    input   [ 7:0] dev;    
    input   [23:0] lba_low;  // LBA_low dword
    input   [23:0] lba_high; // LBA high dword 
    input   [15:0] count; // 
    output integer status;
    begin
        transmit_data[0] = FIS_PIOS |
                           (d2h? 'h2000:0) |
                           (irq? 'h4000:0) |
                           (xmit_status << 16) |
                           (xmit_error << 24);
        transmit_data[1] = {dev, lba_low};
        transmit_data[2] = {8'b0,lba_high};
        transmit_data[3] = {xmit_e_status, 8'b0,count};
        transmit_data[4] = {16'b0, xfer_count};
        linkTransmitFIS(id, 5, 0, status);        
    end
endtask

830 831 832 833 834 835 836 837 838 839 840
task send_dma_activate; // @SuppressThisWarning VEditor - Used in testbench
    input integer id;
    output integer status;
    begin
        transmit_data[0] = FIS_DMAA;
        linkTransmitFIS(id, 1, 0, status);        
    end
endtask



841 842 843 844 845 846 847

task send_identify_data; // @SuppressThisWarning VEditor - Used in testbench
    input integer id;
    output integer status;
    reg   [15:0] identify_data[0:255]; // SuppressThisWarning VEditor : assigned in $readmem() system task
    integer i;
    begin
848
        clear_transmit_pause(0);
Andrey Filippov's avatar
Andrey Filippov committed
849 850
//        $readmemh("input_data/identify.dat",identify_data);
        $readmemh("input_data/test512.dat",identify_data);
851 852 853 854 855 856 857 858
        transmit_data[0] = FIS_DATA;
        for (i=0;i<128;i=i+1) begin
            transmit_data[i+1] = {identify_data[2 * i+1], identify_data[2 * i]};
        end
        linkTransmitFIS(id, 129, 0, status);        
    end
endtask

859 860 861 862 863 864
task send_incrementing_data; // @SuppressThisWarning VEditor - Used in testbench
    input integer id;
    input integer len;
    output integer status;
    integer i;
    begin
865
        clear_transmit_pause(0);
866 867 868 869 870 871 872
        transmit_data[0] = FIS_DATA;
        for (i=0;i<len;i=i+1) begin
            transmit_data[i+1] = i;
        end
        linkTransmitFIS(id, 129, 0, status);        
    end
endtask
873

874 875 876 877 878 879 880 881
task send_incrementing_data_pause; // @SuppressThisWarning VEditor - Used in testbench
    input integer id;
    input integer len;
    output integer status;
    integer i;
    begin
        clear_transmit_pause(0);
        for (i=0;i<len;i=i+8) begin
882
            transmit_data_pause[i+1] = i / 8; // each 8-th have increainsg pause 1,2,3...
883 884 885 886 887 888 889 890
        end    
        transmit_data[0] = FIS_DATA;
        for (i=0;i<len;i=i+1) begin
            transmit_data[i+1] = i;
        end
        linkTransmitFIS(id, 129, 0, status);        
    end
endtask
891 892


Alexey Grebenkin's avatar
Alexey Grebenkin committed
893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916
/*
 * Transmits data to a host. ~Link Transmit FSM
 * Correct execution, as it shall be w/o errors from a device side. (except timeouts and data consistency, see below)
 *
 * Data to transmit is stored in transmit_data memory. 
 * Data is transmitted by dwords // TODO make support for uneven words (16bit each) count
 * 
 * It is possible to send incorrect CRC by setting up an input transmit_custom_crc into 1 and desired crc value to transmit_crc
 *
 * Each data bundle has corresponding "pause" register, stored in a memory 'transmit_data_pause'
 * It represents a time (in clock cycles), for which the device shall "wait" for a new portion of data
 * Could be used to test timeout watchdogs of the host.
 *
 * Parameters:
 * id - transmission id, shown in logs
 * size - how much data to transmit in a FIS
 * transmit_custom_crc - see upper
 * status - returns 0 when the host acknowledges the transaction with OK code, 
 *                  1 when                                       with ERR code
 *          if it's 1, there are 3 options:
 *          a) Generated CRC is invalid
 *          b) Scrambler messed up
 *          c) There is an error in the host
 */
Andrey Filippov's avatar
Andrey Filippov committed
917
task linkTransmitFIS; // @SuppressThisWarning VEditor - Used in testbench
Alexey Grebenkin's avatar
Alexey Grebenkin committed
918 919 920 921
    input integer id;
    input integer size; // dwords count
    input integer transmit_custom_crc;
    output integer status;
922
    integer xpause;
Alexey Grebenkin's avatar
Alexey Grebenkin committed
923 924
    integer cnt;
    integer crc;
925
    reg [111:0] rprim;
Alexey Grebenkin's avatar
Alexey Grebenkin committed
926 927 928 929 930 931 932 933
    reg [31:0] scrambler_value;
    begin
        crc = 32'h52325032;// crc seed
        scrambler_value = {16'hf0f6, 16'h0000}; // scrambler seed
    // tell everyone we need a bus to transmit data
        transmit_lock = 1;
    // DL_SendChkRdy
        linkSendPrim("XRDY");
934 935 936 937
//        $display("[Device] LINK:      Started outcoming transmission");
        DEV_TITLE = "Started outcoming transmission";
        $display("[Device] LINK:      %s @%t", DEV_TITLE, $time);
        
Alexey Grebenkin's avatar
Alexey Grebenkin committed
938
        rprim = linkGetPrim(0);
939 940 941
//        $display("[Device] LINK:      Waiting for acknowledgement");
        DEV_TITLE = "Waiting for acknowledgement";
        $display("[Device] LINK:      %s @%t", DEV_TITLE, $time);
Alexey Grebenkin's avatar
Alexey Grebenkin committed
942 943
        while (rprim != "RRDY") begin
            if (~phy_ready) begin
944 945 946
//                $display("[Device] LINK:      Unexpected line disconnect");
                DEV_TITLE = "Unexpected line disconnect #9";
                $display("[Device] LINK:      %s @%t", DEV_TITLE, $time);
Andrey Filippov's avatar
Andrey Filippov committed
947
                #100;
Alexey Grebenkin's avatar
Alexey Grebenkin committed
948 949 950 951 952 953 954
                $finish;
            end
            @ (posedge clk)
                rprim = linkGetPrim(0);
        end
    // L_SendSOF
        linkSendPrim("SOF");
955 956 957
//        $display("[Device] LINK:      Sending Start of FIS");
        DEV_TITLE = "Sending Start of FIS";
        $display("[Device] LINK:      %s @%t", DEV_TITLE, $time);
Alexey Grebenkin's avatar
Alexey Grebenkin committed
958 959 960
        @ (posedge clk)
            rprim = linkGetPrim(0);
        if (~phy_ready) begin
961 962 963
//            $display("[Device] LINK:      Unexpected line disconnect");
            DEV_TITLE = "Unexpected line disconnect #10";
            $display("[Device] LINK:      %s @%t", DEV_TITLE, $time);
Andrey Filippov's avatar
Andrey Filippov committed
964
            #100;
Alexey Grebenkin's avatar
Alexey Grebenkin committed
965 966 967
            $finish;
        end
        if (rprim == "SYNC") begin
968 969 970 971
//            $display("[Device] LINK:      Transmission terminated by the host, transmission id = %d", id);
            DEV_TITLE = "Transmission terminated by the host #1";
            DEV_DATA =  id;
            $display("[Device] LINK:      %s, transmission id = %d @%t", DEV_TITLE, DEV_DATA, $time);
Andrey Filippov's avatar
Andrey Filippov committed
972
            #100;
Alexey Grebenkin's avatar
Alexey Grebenkin committed
973 974 975
            $finish;
        end
        if ((rprim == "SCRAP") || (rprim == "DATA")) begin
976 977 978 979
//            $display("[Device] LINK:      Bad primitives from the host, is data = %h, data = %h, transmission id = %d", linkIsData(0), linkGetData(0), id);
            DEV_TITLE = "Bad primitives from the host #5";
            DEV_DATA =  id;
            $display("[Device] LINK:      %s, is data = %h, data = %h, reception id = %d @%t", DEV_TITLE, linkIsData(0), linkGetData(0), DEV_DATA, $time);
Andrey Filippov's avatar
Andrey Filippov committed
980
            #100;
Alexey Grebenkin's avatar
Alexey Grebenkin committed
981 982 983 984
            $finish;
        end
    // L_SendData + L_RcvrHold + L_SendHold
        cnt = 0;
985
        xpause = transmit_data_pause[0];
Alexey Grebenkin's avatar
Alexey Grebenkin committed
986
        while (cnt < size) begin
987 988 989 990
            if (xpause > 0) begin
                DEV_TITLE = "Transmission is paused";
                $display("[Device] LINK:      %s @%t", DEV_TITLE, $time);
                linkSendPrim("HOLD");
991
//                xpause = xpause - 1;
992 993 994 995 996 997 998 999
            end else begin
                scrambler_value = scrambleFunc({16'b0,scrambler_value[31:16]});
                linkSendData(transmit_data[cnt] ^ scrambler_value);
                crc = calculateCRC(crc, transmit_data[cnt]);
                DEV_TITLE = "Sent data";
                DEV_DATA =  transmit_data[cnt];
                $display("[Device] LINK:      %s = %h (#%d) @%t", DEV_TITLE, DEV_DATA, cnt, $time);
            end
Alexey Grebenkin's avatar
Alexey Grebenkin committed
1000 1001 1002
            @ (posedge clk)
                rprim = linkGetPrim(0);
            if (rprim == "SYNC") begin
1003 1004 1005 1006
//                $display("[Device] LINK:      Transmission terminated by the host, transmission id = %d", id);
                DEV_TITLE = "Transmission terminated by the host #2";
                DEV_DATA =  id;
                $display("[Device] LINK:      %s, transmission id = %d @%t", DEV_TITLE, DEV_DATA, $time);
Andrey Filippov's avatar
Andrey Filippov committed
1007
                #100;
Alexey Grebenkin's avatar
Alexey Grebenkin committed
1008
                $finish;
1009
            end else if ((rprim == "SCRAP") || (rprim == "DATA")) begin
1010 1011 1012 1013
//                $display("[Device] LINK:      Bad primitives from the host, is data = %h, data = %h, transmission id = %d", linkIsData(0), linkGetData(0), id);
                DEV_TITLE = "Bad primitives from the host #6";
                DEV_DATA =  id;
                $display("[Device] LINK:      %s, is data = %h, data = %h, reception id = %d @%t", DEV_TITLE, linkIsData(0), linkGetData(0), DEV_DATA, $time);
Andrey Filippov's avatar
Andrey Filippov committed
1014
                #100;
Alexey Grebenkin's avatar
Alexey Grebenkin committed
1015
                $finish;
1016
            end else if (rprim == "DMAT") begin
1017
//                $display("[Device] LINK:      Transmission terminated by the host via DMAT, transmission id = %d", id);
1018
                DEV_TITLE = "Transmission terminated by the host via DMAT";
1019 1020
                DEV_DATA =  id;
                $display("[Device] LINK:      %s, transmission id = %d @%t", DEV_TITLE, DEV_DATA, $time);
Andrey Filippov's avatar
Andrey Filippov committed
1021
                #100;
Alexey Grebenkin's avatar
Alexey Grebenkin committed
1022
                $finish;
1023 1024 1025 1026 1027 1028
            end else if (xpause > 0) begin
//                DEV_TITLE = "Transmission is paused";
//                $display("[Device] LINK:      %s @%t", DEV_TITLE, $time);
//                linkSendPrim("HOLD");
                xpause = xpause - 1;
            end else if (rprim == "HOLD") begin
1029 1030
                DEV_TITLE = "The host asked for a pause, acknowledging transmission paused";
                $display("[Device] LINK:      %s @%t", DEV_TITLE, $time);
Alexey Grebenkin's avatar
Alexey Grebenkin committed
1031
                linkSendPrim("HOLDA");
1032
            end else begin
Alexey Grebenkin's avatar
Alexey Grebenkin committed
1033 1034
                cnt = cnt + 1;
                if (cnt < size) 
1035
                    xpause = transmit_data_pause[cnt];
Alexey Grebenkin's avatar
Alexey Grebenkin committed
1036 1037 1038
            end
        end
    // L_SendCRC
1039 1040
//        scrambler_value = scrambleFunc(scrambler_value[31:16]);
        scrambler_value = scrambleFunc({16'b0,scrambler_value[31:16]});
Alexey Grebenkin's avatar
Alexey Grebenkin committed
1041 1042 1043 1044
        if (transmit_custom_crc != 0) begin
            crc = transmit_crc;
        end
            linkSendData(crc ^ scrambler_value);
1045 1046 1047 1048 1049
//        $display("[Device] LINK:      Sent crc = %h", crc);
        DEV_TITLE = "Sent crc";
        DEV_DATA =  crc;
        $display("[Device] LINK:      %s = %h @%t", DEV_TITLE, DEV_DATA, $time);
        
Alexey Grebenkin's avatar
Alexey Grebenkin committed
1050 1051 1052
        @ (posedge clk)
            rprim = linkGetPrim(0);
        if (~phy_ready) begin
1053 1054 1055
//            $display("[Device] LINK:      Unexpected line disconnect");
            DEV_TITLE = "Unexpected line disconnect #11";
            $display("[Device] LINK:      %s @%t", DEV_TITLE, $time);
Andrey Filippov's avatar
Andrey Filippov committed
1056
            #100;
Alexey Grebenkin's avatar
Alexey Grebenkin committed
1057 1058 1059
            $finish;
        end
        if (rprim == "SYNC") begin
1060 1061 1062 1063
//            $display("[Device] LINK:      Transmission terminated by the host, transmission id = %d", id);
            DEV_TITLE = "Transmission terminated by the host #3";
            DEV_DATA =  id;
            $display("[Device] LINK:      %s, transmission id = %d @%t", DEV_TITLE, DEV_DATA, $time);
Andrey Filippov's avatar
Andrey Filippov committed
1064
            #100;
Alexey Grebenkin's avatar
Alexey Grebenkin committed
1065 1066 1067
            $finish;
        end
        if ((rprim == "SCRAP") || (rprim == "DATA")) begin
1068 1069 1070 1071
//            $display("[Device] LINK:      Bad primitives from the host, is data = %h, data = %h, transmission id = %d", linkIsData(0), linkGetData(0), id);
            DEV_TITLE = "Bad primitives from the host #7";
            DEV_DATA =  id;
            $display("[Device] LINK:      %s, is data = %h, data = %h, reception id = %d @%t", DEV_TITLE, linkIsData(0), linkGetData(0), DEV_DATA, $time);
Andrey Filippov's avatar
Andrey Filippov committed
1072
            #100;
Alexey Grebenkin's avatar
Alexey Grebenkin committed
1073 1074 1075 1076
            $finish;
        end
    // L_SendEOF
        linkSendPrim("EOF");
1077 1078 1079
//        $display("[Device] LINK:      Sent End of FIS");
        DEV_TITLE = "Sent End of FIS";
        $display("[Device] LINK:      %s @%t", DEV_TITLE, $time);
Alexey Grebenkin's avatar
Alexey Grebenkin committed
1080 1081 1082
        @ (posedge clk)
            rprim = linkGetPrim(0);
        if (~phy_ready) begin
1083 1084 1085
//            $display("[Device] LINK:      Unexpected line disconnect");
            DEV_TITLE = "Unexpected line disconnect #12";
            $display("[Device] LINK:      %s @%t", DEV_TITLE, $time);
Andrey Filippov's avatar
Andrey Filippov committed
1086
            #100;
Alexey Grebenkin's avatar
Alexey Grebenkin committed
1087 1088 1089
            $finish;
        end
        if (rprim == "SYNC") begin
1090 1091 1092 1093
//            $display("[Device] LINK:      Transmission terminated by the host, transmission id = %d", id);
            DEV_TITLE = "Transmission terminated by the host #4";
            DEV_DATA =  id;
            $display("[Device] LINK:      %s, transmission id = %d @%t", DEV_TITLE, DEV_DATA, $time);
Andrey Filippov's avatar
Andrey Filippov committed
1094
            #100;
Alexey Grebenkin's avatar
Alexey Grebenkin committed
1095 1096 1097
            $finish;
        end
        if ((rprim == "SCRAP") || (rprim == "DATA")) begin
1098 1099 1100 1101
//            $display("[Device] LINK:      Bad primitives from the host, is data = %h, data = %h, transmission id = %d", linkIsData(0), linkGetData(0), id);
            DEV_TITLE = "Bad primitives from the host #8";
            DEV_DATA =  id;
            $display("[Device] LINK:      %s, is data = %h, data = %h, reception id = %d @%t", DEV_TITLE, linkIsData(0), linkGetData(0), DEV_DATA, $time);
Andrey Filippov's avatar
Andrey Filippov committed
1102
            #100;
Alexey Grebenkin's avatar
Alexey Grebenkin committed
1103 1104 1105 1106
            $finish;
        end
    // L_Wait
        linkSendPrim("WTRM");
1107 1108 1109
//        $display("[Device] LINK:      Waiting for a response from the host");
        DEV_TITLE = "Waiting for a response from the host";
        $display("[Device] LINK:      %s @%t", DEV_TITLE, $time);
Alexey Grebenkin's avatar
Alexey Grebenkin committed
1110 1111 1112 1113 1114
        @ (posedge clk)
            rprim = linkGetPrim(0);
        status = 0;
        while ((rprim != "OK") && (status == 0)) begin
            if (~phy_ready) begin
1115 1116 1117
                //$display("[Device] LINK:      Unexpected line disconnect");
                DEV_TITLE = "Unexpected line disconnect #13";
                $display("[Device] LINK:      %s @%t", DEV_TITLE, $time);
Andrey Filippov's avatar
Andrey Filippov committed
1118
                #100;
Alexey Grebenkin's avatar
Alexey Grebenkin committed
1119 1120 1121
                $finish;
            end
            if (rprim == "SYNC") begin
1122 1123 1124 1125
//                $display("[Device] LINK:      Transmission terminated by the host, transmission id = %d", id);
                DEV_TITLE = "Transmission terminated by the host #5";
                DEV_DATA =  id;
                $display("[Device] LINK:      %s, transmission id = %d @%t", DEV_TITLE, DEV_DATA, $time);
Andrey Filippov's avatar
Andrey Filippov committed
1126
                #100;
Alexey Grebenkin's avatar
Alexey Grebenkin committed
1127 1128 1129
                $finish;
            end
            if ((rprim == "SCRAP") || (rprim == "DATA")) begin
1130 1131 1132 1133
//                $display("[Device] LINK:      Bad primitives from the host, is data = %h, data = %h, transmission id = %d", linkIsData(0), linkGetData(0), id);
                DEV_TITLE = "Bad primitives from the host #9";
                DEV_DATA =  id;
                $display("[Device] LINK:      %s, is data = %h, data = %h, reception id = %d @%t", DEV_TITLE, linkIsData(0), linkGetData(0), DEV_DATA, $time);
Andrey Filippov's avatar
Andrey Filippov committed
1134
                #100;
Alexey Grebenkin's avatar
Alexey Grebenkin committed
1135 1136 1137
                $finish;
            end
            if (rprim == "ERR") begin
1138 1139 1140 1141
//                $display("[Device] LINK:      Host invalidated the transmission, transmission id = %d", id);
                DEV_TITLE = "Host invalidated the transmission";
                DEV_DATA =  id;
                $display("[Device] LINK:      %s, transmission id = %d @%t", DEV_TITLE, DEV_DATA, $time);
Alexey Grebenkin's avatar
Alexey Grebenkin committed
1142 1143 1144 1145 1146 1147
                status = 1;
            end
            @ (posedge clk)
                rprim = linkGetPrim(0);
        end
        if (status == 0)
1148 1149 1150 1151 1152
//            $display("[Device] LINK:      Transmission done OK, id = %d", id);
            DEV_TITLE = "Transmission done OK";
            DEV_DATA =  id;
            $display("[Device] LINK:      %s, id = %d @%t", DEV_TITLE, DEV_DATA, $time);
            
Alexey Grebenkin's avatar
Alexey Grebenkin committed
1153
        if (status == 1)
1154 1155 1156 1157
//            $display("[Device] LINK:      Transmission done with ERRORs, id = %d", id);
            DEV_TITLE = "Transmission done with ERRORS";
            DEV_DATA =  id;
            $display("[Device] LINK:      %s, id = %d @%t", DEV_TITLE, DEV_DATA, $time);
Alexey Grebenkin's avatar
Alexey Grebenkin committed
1158 1159
    // L_IDLE
        linkSendPrim("SYNC");
1160
        @ (posedge clk);
Alexey Grebenkin's avatar
Alexey Grebenkin committed
1161 1162 1163 1164 1165
    end
endtask

// checks, if it is data coming from the host
function [0:0] linkIsData;
Andrey Filippov's avatar
Andrey Filippov committed
1166
    input dummy; // @SuppressThisWarning VEditor - unused (is it for some simulator?)
Alexey Grebenkin's avatar
Alexey Grebenkin committed
1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177
    begin
        if (|phy2dev_charisk) 
            linkIsData = 1;
        else
            linkIsData = 0;
    end
endfunction

// obvious
function [31:0] linkGetData;
// TODO non-even word count
Andrey Filippov's avatar
Andrey Filippov committed
1178
    input dummy; // @SuppressThisWarning VEditor - unused (is it for some simulator?)
Alexey Grebenkin's avatar
Alexey Grebenkin committed
1179 1180 1181 1182 1183 1184 1185 1186
    begin
        linkGetData = phy2dev_data;
    end
endfunction
/*
 * Returns current primitive at the outputs of phy level
 * Return value is a string containing its name!
 */
1187
function [111:0] linkGetPrim;
Andrey Filippov's avatar
Andrey Filippov committed
1188
    input integer dummy; // @SuppressThisWarning VEditor - unused (is it for some simulator?)
1189
    reg [111:0] type;
Alexey Grebenkin's avatar
Alexey Grebenkin committed
1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240
    begin
        if (~|phy2dev_charisk) begin
            type = "DATA";
        end
        else
        if (phy2dev_charisk == 4'h1) begin
            case (phy2dev_data)
                PRIM_SYNCP: 
                    type    = "SYNC";
                PRIM_ALIGNP:
                    type    = "ALIGN";
                PRIM_XRDYP:
                    type    = "XRDY";
                PRIM_SOFP:
                    type    = "SOF";
                PRIM_HOLDAP:
                    type    = "HOLDA";
                PRIM_HOLDP:
                    type    = "HOLD";
                PRIM_EOFP:
                    type    = "EOF";
                PRIM_WTRMP:
                    type    = "WTRM";
                PRIM_RRDYP:
                    type    = "RRDY";
                PRIM_IPP:
                    type    = "IP";
                PRIM_DMATP:
                    type    = "DMAT";
                PRIM_OKP:
                    type    = "OK";
                PRIM_ERRP:
                    type    = "ERR";
                default:
                    type    = "SCRAP";
            endcase
        end
        else begin
            type = "SCRAP";
        end
        linkGetPrim = type;
    end
endfunction

/*
 * Sets some data to phy inputs
 * input is a data dword
 */
task linkSendData;
    input [31:0] data;
    begin
Andrey Filippov's avatar
Andrey Filippov committed
1241
        send_align_pair_if_needed;
Alexey Grebenkin's avatar
Alexey Grebenkin committed
1242 1243 1244 1245 1246 1247 1248 1249 1250 1251
        dev2phy_data    <= data;
        dev2phy_isk     <= 4'h0;
    end
endtask

/*
 * Set a desired primitive to phy inputs
 * input is a string containing its name!
 */
task linkSendPrim;
1252
    input [111:0] type;
Alexey Grebenkin's avatar
Alexey Grebenkin committed
1253
    begin
Andrey Filippov's avatar
Andrey Filippov committed
1254
//        send_align_pair_if_needed;
Alexey Grebenkin's avatar
Alexey Grebenkin committed
1255 1256 1257
        case (type)
            "SYNC": 
            begin
Andrey Filippov's avatar
Andrey Filippov committed
1258
                send_align_pair_if_needed;            
Alexey Grebenkin's avatar
Alexey Grebenkin committed
1259 1260 1261 1262 1263 1264 1265 1266 1267 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 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331
                dev2phy_data    <= PRIM_SYNCP;
                dev2phy_isk     <= 4'h1;
            end
            "ALIGN":
            begin
                dev2phy_data    <= PRIM_ALIGNP;
                dev2phy_isk     <= 4'h1;
            end
            "XRDY":
            begin
                dev2phy_data    <= PRIM_XRDYP;
                dev2phy_isk     <= 4'h1;
            end
            "SOF":
            begin
                dev2phy_data    <= PRIM_SOFP;
                dev2phy_isk     <= 4'h1;
            end
            "HOLDA":
            begin
                dev2phy_data    <= PRIM_HOLDAP;
                dev2phy_isk     <= 4'h1;
            end
            "HOLD":
            begin
                dev2phy_data    <= PRIM_HOLDP;
                dev2phy_isk     <= 4'h1;
            end
            "EOF":
            begin
                dev2phy_data    <= PRIM_EOFP;
                dev2phy_isk     <= 4'h1;
            end
            "WTRM":
            begin
                dev2phy_data    <= PRIM_WTRMP;
                dev2phy_isk     <= 4'h1;
            end
            "RRDY":
            begin
                dev2phy_data    <= PRIM_RRDYP;
                dev2phy_isk     <= 4'h1;
            end
            "IP":
            begin
                dev2phy_data    <= PRIM_IPP;
                dev2phy_isk     <= 4'h1;
            end
            "DMAT":
            begin
                dev2phy_data    <= PRIM_DMATP;
                dev2phy_isk     <= 4'h1;
            end
            "OK":
            begin
                dev2phy_data    <= PRIM_OKP;
                dev2phy_isk     <= 4'h1;
            end
            "ERR":
            begin
                dev2phy_data    <= PRIM_ERRP;
                dev2phy_isk     <= 4'h1;
            end
            default:
            begin
                dev2phy_data    <= PRIM_SYNCP;
                dev2phy_isk     <= 4'h1;
            end
        endcase
    end
endtask

endmodule