<#: ttcl/GetValueSimParam.ttcl :>
<: if {$two_port_switch == "YES"} ttcl_return :>
<: if {$ep} ttcl_return :>
<: setFileName "cgator_pkt_generator" :>
<: setFileExtension ".v" :>
<: setOutputDirectory [ttcl_string_concat $ComponentName "/example_design"] :>
<#: ttcl/Header.ttcl :>
//-----------------------------------------------------------------------------
//
// Project    : Ultrascale FPGA Gen4 Integrated Block for PCI Express
// File       : cgator_pkt_generator.v
// Version    : 1.0 
//-----------------------------------------------------------------------------

`timescale 1ns/1ns

(* DowngradeIPIdentifiedWarnings = "yes" *)
module cgator_pkt_generator
  #(
    parameter        TCQ          = 1,
    parameter        AXISTEN_IF_RQ_ALIGNMENT_MODE   = "TRUE",
<: if {$axisten_if_width_int != "512"} { :>
    parameter AXI4_RQ_TUSER_WIDTH = 62,
<: } else { :>
    parameter AXI4_RQ_TUSER_WIDTH = 137,
<: } :>
    parameter [15:0] REQUESTER_ID = 16'h10EE,
    parameter C_DATA_WIDTH        = 64,
    parameter KEEP_WIDTH          = C_DATA_WIDTH / 32 )
  (
    // globals
    input wire          user_clk,
    input wire          reset,

    // Tx mux interface
    input                          pg_s_axis_rq_tready,
    output reg [C_DATA_WIDTH-1:0]  pg_s_axis_rq_tdata,
    output reg [KEEP_WIDTH-1:0]    pg_s_axis_rq_tkeep,
    output reg [AXI4_RQ_TUSER_WIDTH-1:0] pg_s_axis_rq_tuser,
    output reg                     pg_s_axis_rq_tlast,
    output reg                     pg_s_axis_rq_tvalid,

    // Controller interface
    input wire [1:0]    pkt_type,  // See TYPE_* below for encodings
    input wire [1:0]    pkt_func_num,
    input wire [9:0]    pkt_reg_num,
    input wire [3:0]    pkt_1dw_be,
    input wire [2:0]    pkt_msg_routing,
    input wire [7:0]    pkt_msg_code,
    input wire [31:0]   pkt_data,
    input wire          pkt_start,
    output reg          pkt_done
  );

  // Encodings for pkt_type
  localparam [1:0] TYPE_CFGRD = 2'b00;
  localparam [1:0] TYPE_CFGWR = 2'b01;
  localparam [1:0] TYPE_MSG   = 2'b10;
  localparam [1:0] TYPE_MSGD  = 2'b11;

  // State encodings
  localparam [2:0] ST_IDLE   = 3'd0;

  localparam [2:0] ST_CFG0   = 3'd1;
  localparam [2:0] ST_CFG1   = 3'd2;
  localparam [2:0] ST_CFG2   = 3'd3;

  localparam [2:0] ST_MSG0   = 3'd4;
  localparam [2:0] ST_MSG1   = 3'd5;
  localparam [2:0] ST_MSG2   = 3'd6;

  // State variable
  reg [2:0]  pkt_state;

  generate
    if (C_DATA_WIDTH == 64) begin : width_64
      // State-machine and controller hand-shake
      always @(posedge user_clk) begin
        if (reset) begin
          pkt_state          <= #TCQ ST_IDLE;
          pkt_done           <= #TCQ 1'b0;
        end else begin
          case (pkt_state)
            ST_IDLE: begin
              // Idle - wait for Controller to request TLP transmission

              pkt_done       <= #TCQ 1'b0;

              if (pkt_start) begin
                if (pkt_type == TYPE_CFGRD || pkt_type == TYPE_CFGWR) begin
                  pkt_state  <= #TCQ ST_CFG0;
                end else begin
                  pkt_state  <= #TCQ ST_CFG0;
                  //pkt_state  <= #TCQ ST_MSG0;
                end
              end
            end // ST_IDLE

            ST_CFG0: begin : beat_1_CFG0
              // First Quad-word (2 dwords) of a CfgRd0 or CfgWr0 TLP
              if (pg_s_axis_rq_tready) begin
                pkt_state    <= #TCQ ST_CFG1;
              end
            end // ST_CFG0

            ST_CFG1: begin : beat_2_CFG0
              // Second QW of a CfgWr0 TLP and last QW of a CfgRd0
              if (pg_s_axis_rq_tready) begin
                 if (pkt_type == TYPE_CFGWR) begin
                     pkt_state    <= #TCQ ST_CFG2;
                 end else begin
                     pkt_state    <= #TCQ ST_IDLE;
                     pkt_done     <= #TCQ 1'b1;
                 end
              end
            end // ST_CFG1

            ST_CFG2: begin : beat_3_CFG0
              // last QW of a CfgWr0 TLP
              if (pg_s_axis_rq_tready) begin
                pkt_state    <= #TCQ ST_IDLE;
                pkt_done     <= #TCQ 1'b1;
              end
            end // ST_CFG2

            ST_MSG0: begin : beat_1_MSG
              // First QW of a Msg or MsgD TLP
              if (pg_s_axis_rq_tready) begin
                pkt_state    <= #TCQ ST_MSG1;
              end
            end // ST_MSG0

            ST_MSG1: begin : beat_2_MSG
              // Second QW of a Msg or MsgD TLP
              if (pg_s_axis_rq_tready) begin
                if (pkt_type == TYPE_MSGD) begin
                  // MsgD TLPs have a third QW
                  pkt_state    <= #TCQ ST_MSG2;
                end else begin
                  // Msg TLPs end after two QWs
                  pkt_state    <= #TCQ ST_IDLE;
                  pkt_done     <= #TCQ 1'b1;
                end
              end
            end // ST_MSG1

            ST_MSG2: begin : beat_3_MSG
              // Third and last QW of a MsgD TLP
              if (pg_s_axis_rq_tready) begin
                pkt_state    <= #TCQ ST_IDLE;
                pkt_done     <= #TCQ 1'b1;
              end
            end // ST_MSG2

            default: begin
              pkt_state      <= #TCQ ST_IDLE;
            end // default case
          endcase
        end
      end

      always @* begin
        case (pkt_state)
          ST_CFG0: begin
            // First QW of a CfgRd0 or CfgWr0 TLP
            pg_s_axis_rq_tlast = 1'b0;
            pg_s_axis_rq_tuser = {32'b0,
                                   4'b1010,      // Seq Number
                                   8'h00,        // TPH Steering Tag
                                   1'b0,         // TPH indirect Tag Enable
                                   2'b0,         // TPH Type
                                   1'b0,         // TPH Present
                                   1'b0,         // Discontinue
                                   3'b000,       // Byte Lane number in case of Address Aligned mode
                                   4'b0,         //last_dw_be_,    // Last BE of the Read Data
                                   pkt_1dw_be};  //first_dw_be_ }; // First BE of the Read Data

            pg_s_axis_rq_tdata = {32'b0,
                                  16'b0,
                                   4'b0,
                                   pkt_reg_num,    // Ext Reg Number, Register Number
                                   2'b00           // Reserved
                                   };
            pg_s_axis_rq_tkeep  = {KEEP_WIDTH{1'b1}};
            pg_s_axis_rq_tvalid = 1'b1;
          end // ST_CFG0

          ST_CFG1: begin
            // Second QW of a CfgWr0 TLP and last QW of a CfgRd0
            pg_s_axis_rq_tlast = (pkt_type == TYPE_CFGWR) ? 1'b0 : 1'b1;
            pg_s_axis_rq_tuser = {AXI4_RQ_TUSER_WIDTH{1'b0}};
            pg_s_axis_rq_tdata = { 1'b0,   // Force ECRC
                                   3'b0,   // Attribute
                                   3'b0,   // TC
                                   1'b0,   // RID

                                   8'h01,         // Bus #            \
                                   5'b00000,      // Device #         |  Completer ID
                                   1'b0,          // Function # (Hi)  |
                                   pkt_func_num,  // Function # (Lo)  /
//                                   8'd1,          // Bus #            \
//                                   5'd0,          // Device #         |  Completer ID
//                                   1'b0,          // Function # (Hi)  |
//                                   pkt_func_num, // Function # (Lo)  /
                                   8'b0,   // Tag

                                   16'h00AF,//REQUESTER_ID, //
                                   1'b0,
                                  (pkt_type == TYPE_CFGRD) ? 4'b1000 : 4'b1010, // Req Type
                                  11'd1 }; 

            pg_s_axis_rq_tkeep  = {KEEP_WIDTH{1'b1}};
            pg_s_axis_rq_tvalid = 1'b1;
          end // ST_CFG1

          ST_CFG2: begin
            // last QW of a CfgWr0 TLP
            pg_s_axis_rq_tlast = 1'b1;
            pg_s_axis_rq_tuser = {AXI4_RQ_TUSER_WIDTH{1'b0}};
            pg_s_axis_rq_tdata = {32'b0,
                                  pkt_data[31:24], 
                                  pkt_data[23:16], 
                                  pkt_data[15: 8], 
                                  pkt_data[ 7: 0] 
                                  };
            pg_s_axis_rq_tkeep  = 2'b1;
            pg_s_axis_rq_tvalid = 1'b1;
          end // ST_CFG2

          ST_MSG0: begin
            // First QW of a Msg or MsgD TLP
            pg_s_axis_rq_tlast = 1'b0;
            pg_s_axis_rq_tuser = {32'b0,
                                   4'b1010,      // Seq Number
                                   8'h00,        // TPH Steering Tag
                                   1'b0,         // TPH indirect Tag Enable
                                   2'b0,         // TPH Type
                                   1'b0,         // TPH Present
                                   1'b0,         // Discontinue
                                   3'b000,       // Byte Lane number in case of Address Aligned mode
                                   4'b0,         //last_dw_be_,    // Last BE of the Read Data
                                   4'b0};        //first_dw_be_ }; // First BE of the Read Data

            pg_s_axis_rq_tdata  = 64'h0000_0000_0000_0000; // Addr[31:2], Reserved, Addr[63:32]

            pg_s_axis_rq_tkeep  = {KEEP_WIDTH{1'b1}};
            pg_s_axis_rq_tvalid = 1'b1;
          end // ST_MSG0

          ST_MSG1: begin
            // Second QW of a Msg or MsgD TLP (last for Msg)
            pg_s_axis_rq_tlast  = (pkt_type == TYPE_MSGD) ? 1'b0 : 1'b1;
            pg_s_axis_rq_tuser = {AXI4_RQ_TUSER_WIDTH{1'b0}};
            pg_s_axis_rq_tdata = { 1'b0,   // Force ECRC
                                   3'b0,   // Attribute
                                   3'b0,   // TC
                                   1'b0,   // RID
                                   5'b0,   // rsvd
                                   pkt_msg_routing,                         // Msg Routing
                                   pkt_msg_code,                            // Message Code
                                   8'b0,   // Tag

                                   REQUESTER_ID, //
                                   1'b0,
                                   4'b1100,   // Req Type
                                  (pkt_type == TYPE_MSGD) ? 11'd1 : 11'd0 }; // Length

            pg_s_axis_rq_tkeep  = {KEEP_WIDTH{1'b1}};
            pg_s_axis_rq_tvalid = 1'b1;
          end // ST_MSG1

          ST_MSG2: begin
            // Third and last QW of a MsgD TLP
            pg_s_axis_rq_tlast  = 1'b1;
            pg_s_axis_rq_tuser = {AXI4_RQ_TUSER_WIDTH{1'b0}};
            pg_s_axis_rq_tdata  = {32'h0000_0000, pkt_data}; // Data, don't-care
            pg_s_axis_rq_tkeep  = 2'h1;
            pg_s_axis_rq_tvalid = 1'b1;
          end // ST_MSG2

          default: begin
            // No TLP active
            pg_s_axis_rq_tlast  = 1'b0;
            pg_s_axis_rq_tuser = {AXI4_RQ_TUSER_WIDTH{1'b0}};
            pg_s_axis_rq_tdata  = 64'h0000_0000_0000_0000;
            pg_s_axis_rq_tkeep  = 8'h00;
            pg_s_axis_rq_tvalid = 1'b0;
          end // default case
        endcase
      end // End 64-bit mode
    end else if (C_DATA_WIDTH == 128) begin : width_128
       // State-machine and controller hand-shake
      always @(posedge user_clk) begin
        if (reset) begin
          pkt_state          <= #TCQ ST_IDLE;
          pkt_done           <= #TCQ 1'b0;
        end else begin
          case (pkt_state)
            ST_IDLE: begin
              // Idle - wait for Controller to request TLP transmission

              pkt_done       <= #TCQ 1'b0;

              if (pkt_start) begin
                if (pkt_type == TYPE_CFGRD || pkt_type == TYPE_CFGWR) begin
                  pkt_state  <= #TCQ ST_CFG0;
                end else begin
//                  pkt_state  <= #TCQ ST_MSG0;
                  pkt_state  <= #TCQ ST_CFG0;
                end
              end
            end // ST_IDLE

            ST_CFG0: begin : beat_1_CFG0
              // First 2 QW's (4 dwords) of a CfgRd0 or CfgWr0 TLP
              if (pg_s_axis_rq_tready) begin
                 if (pkt_type == TYPE_CFGWR) begin
                     pkt_state    <= #TCQ ST_CFG1;
                 end else begin
                     pkt_state    <= #TCQ ST_IDLE;
                     pkt_done     <= #TCQ 1'b1;
                 end
              end
            end // ST_CFG0

            ST_CFG1: begin : beat_2_CFG0
              // Last 2 QW's (4 dwords) of a CfgWr0 TLP
              if (pg_s_axis_rq_tready) begin
                pkt_state    <= #TCQ ST_IDLE;
                pkt_done     <= #TCQ 1'b1;
              end
            end // ST_CFG1

            ST_MSG0: begin : beat_1_MSG
              // First 2 QW's of a Msg or MsgD TLP
              if (pg_s_axis_rq_tready) begin
                if (pkt_type == TYPE_MSGD) begin
                  pkt_state    <= #TCQ ST_MSG1;
                end else begin
                  pkt_state    <= #TCQ ST_IDLE;
                  pkt_done     <= #TCQ 1'b1;
                end
              end
            end // ST_MSG0

            ST_MSG1: begin : beat_2_MSG
              // Third QW of MsgD TLP
              if (pg_s_axis_rq_tready) begin
                  pkt_state    <= #TCQ ST_IDLE;
                  pkt_done     <= #TCQ 1'b1;
              end
            end // ST_MSG1

            default: begin
              pkt_state      <= #TCQ ST_IDLE;
            end // default case
          endcase
        end
      end

      always @* begin
        case (pkt_state)
          ST_CFG0: begin
            // First 2 QW of a CfgRd0 or CfgWr0 TLP
            pg_s_axis_rq_tlast = (pkt_type == TYPE_CFGWR) ? 1'b0 : 1'b1;
            pg_s_axis_rq_tuser = {32'b0,
                                   4'b1010,      // Seq Number
                                   8'h00,        // TPH Steering Tag
                                   1'b0,         // TPH indirect Tag Enable
                                   2'b0,         // TPH Type
                                   1'b0,         // TPH Present
                                   1'b0,         // Discontinue
                                   3'b000,       // Byte Lane number in case of Address Aligned mode
                                   4'b0,         //last_dw_be_,    // Last BE of the Read Data
                                   pkt_1dw_be};  //first_dw_be_ }; // First BE of the Read Data

            pg_s_axis_rq_tdata = {
                                   1'b0,   // Force ECRC
                                   3'b0,   // Attribute
                                   3'b0,   // TC
                                   1'b0,   // RID
                                   8'h01,         // Bus #            \
                                   5'b00000,      // Device #         |  Completer ID
                                   1'b0,          // Function # (Hi)  |
                                   pkt_func_num,  // Function # (Lo)  /
//                                   8'd1,          // Bus #            \
//                                   5'd0,          // Device #         |  Completer ID
//                                   1'b0,          // Function # (Hi)  |
//                                   pkt_func_num, // Function # (Lo)  /
                                   8'b0,   // Tag

                                   16'h00AF,//REQUESTER_ID, //
                                   1'b0,
                                  (pkt_type == TYPE_CFGRD) ? 4'b1000 : 4'b1010, // Req Type
                                  11'd1,
                                  32'b0,
                                  16'b0,
                                   4'b0,
                                   pkt_reg_num,    // Ext Reg Number, Register Number
                                   2'b00           // Reserved
                                   };
            pg_s_axis_rq_tkeep  = {KEEP_WIDTH{1'b1}};
            pg_s_axis_rq_tvalid = 1'b1;
          end // ST_CFG0

          ST_CFG1: begin
            pg_s_axis_rq_tdata = {32'b0,
                                  32'b0,
                                  32'b0,
                                  pkt_data[31:24], 
                                  pkt_data[23:16], 
                                  pkt_data[15: 8], 
                                  pkt_data[ 7: 0] 
                                  };
            pg_s_axis_rq_tkeep  = 4'b1;
            pg_s_axis_rq_tvalid = 1'b1;
            pg_s_axis_rq_tlast  = 1'b1;
            pg_s_axis_rq_tuser = {AXI4_RQ_TUSER_WIDTH{1'b0}};
          end // ST_CFG1

          ST_MSG0: begin
            pg_s_axis_rq_tlast  = (pkt_type == TYPE_MSGD) ? 1'b0 : 1'b1;
            pg_s_axis_rq_tuser = {32'b0,
                                   4'b1010,      // Seq Number
                                   8'h00,        // TPH Steering Tag
                                   1'b0,         // TPH indirect Tag Enable
                                   2'b0,         // TPH Type
                                   1'b0,         // TPH Present
                                   1'b0,         // Discontinue
                                   3'b000,       // Byte Lane number in case of Address Aligned mode
                                   4'b0,         //last_dw_be_,    // Last BE of the Read Data
                                   4'b0};        //first_dw_be_ }; // First BE of the Read Data

            pg_s_axis_rq_tdata = { 1'b0,   // Force ECRC
                                   3'b0,   // Attribute
                                   3'b0,   // TC
                                   1'b0,   // RID
                                   5'b0,   // rsvd
                                   pkt_msg_routing,                         // Msg Routing
                                   pkt_msg_code,                            // Message Code
                                   8'b0,   // Tag

                                   REQUESTER_ID, //
                                   1'b0,
                                   4'b1100,  // Req Type
                                  (pkt_type == TYPE_MSGD) ? 11'd1 : 11'd0,     // Length

                                   64'h0 }; // Addr[31:2], Reserved, Addr[63:32]

            pg_s_axis_rq_tkeep  = {KEEP_WIDTH{1'b1}};
            pg_s_axis_rq_tvalid = 1'b1;
          end // ST_MSG0

          ST_MSG1: begin
            pg_s_axis_rq_tlast  = 1'b1;
            pg_s_axis_rq_tuser = {AXI4_RQ_TUSER_WIDTH{1'b0}};
            pg_s_axis_rq_tdata  = {96'h0000_0000, pkt_data}; // Data, don't-care
            pg_s_axis_rq_tkeep  = 4'b1;
            pg_s_axis_rq_tvalid = 1'b1;
          end // ST_MSG1

          default: begin
            // No TLP active
            pg_s_axis_rq_tlast  = 1'b0;
            pg_s_axis_rq_tuser = {AXI4_RQ_TUSER_WIDTH{1'b0}};
            pg_s_axis_rq_tdata  = {C_DATA_WIDTH{1'b0}};
            pg_s_axis_rq_tkeep  = {KEEP_WIDTH{1'b0}};
            pg_s_axis_rq_tvalid = 1'b0;
          end // default case
        endcase
      end
     // End 128-bit Mode
    end else if (C_DATA_WIDTH == 256) begin : width_256

       // State-machine and controller hand-shake
      always @(posedge user_clk) begin
        if (reset) begin
          pkt_state          <= #TCQ ST_IDLE;
          pkt_done           <= #TCQ 1'b0;
        end else begin
          case (pkt_state)
            ST_IDLE: begin
              // Idle - wait for Controller to request TLP transmission

              pkt_done       <= #TCQ 1'b0;

              if (pkt_start) begin
                if (pkt_type == TYPE_CFGRD || pkt_type == TYPE_CFGWR) begin
                  pkt_state  <= #TCQ ST_CFG0;
                end else begin
//                  pkt_state  <= #TCQ ST_MSG0;
                  pkt_state  <= #TCQ ST_CFG0;
                end
              end
            end // ST_IDLE

            ST_CFG0: begin : beat_1_CFG0
              // First 2 QW's (4 dwords) of a CfgRd0 or CfgWr0 TLP
              if (pg_s_axis_rq_tready) begin
	        if(AXISTEN_IF_RQ_ALIGNMENT_MODE == "TRUE" && (pkt_type == TYPE_CFGWR || pkt_type == TYPE_MSGD)) begin
                  pkt_state    <= #TCQ ST_CFG1;
                  pkt_done     <= #TCQ 1'b0;
		end
		else begin 
                  pkt_state    <= #TCQ ST_IDLE;
                  pkt_done     <= #TCQ 1'b1;
		end
              end
            end // ST_CFG0
            ST_CFG1: begin : beat_2_CFG0
              // First 2 QW's (4 dwords) of a CfgRd0 or CfgWr0 TLP
              if (pg_s_axis_rq_tready) begin
                pkt_state    <= #TCQ ST_IDLE;
                pkt_done     <= #TCQ 1'b1;
              end
            end // ST_CFG1

            ST_MSG0: begin : beat_0_MSG
              // First 2 QW's of a Msg or MsgD TLP
	        if(AXISTEN_IF_RQ_ALIGNMENT_MODE == "TRUE" && pkt_type == TYPE_MSGD) begin
                  pkt_state    <= #TCQ ST_MSG1;
                  pkt_done     <= #TCQ 1'b0;
		end
		else begin 
                  pkt_state    <= #TCQ ST_IDLE;
                  pkt_done     <= #TCQ 1'b1;
		end
            end // ST_MSG0
            ST_MSG1: begin : beat_1_MSG
              // First 2 QW's of a Msg or MsgD TLP
              if (pg_s_axis_rq_tready) begin
                  pkt_state    <= #TCQ ST_IDLE;
                  pkt_done     <= #TCQ 1'b1;
                end
            end // ST_MSG1

            default: begin
              pkt_state      <= #TCQ ST_IDLE;
            end // default case
          endcase
        end
      end

      always @* begin
        case (pkt_state)
          ST_CFG0: begin
            pg_s_axis_rq_tuser = {32'b0,
                                   4'b1010,      // Seq Number
                                   8'h00,        // TPH Steering Tag
                                   1'b0,         // TPH indirect Tag Enable
                                   2'b0,         // TPH Type
                                   1'b0,         // TPH Present
                                   1'b0,         // Discontinue
                                   3'b000,       // Byte Lane number in case of Address Aligned mode
                                   4'b0,         //last_dw_be_,    // Last BE of the Read Data
                                   pkt_1dw_be};  //first_dw_be_ }; // First BE of the Read Data

            pg_s_axis_rq_tdata = {96'b0,
                                  (pkt_type == TYPE_CFGWR && AXISTEN_IF_RQ_ALIGNMENT_MODE == "FALSE" ) ? pkt_data : 32'b0,  
                                   1'b0,   // Force ECRC
                                   3'b0,   // Attribute
                                   3'b0,   // TC
                                   1'b0,   // RID
                                   8'h01,         // Bus #            \
                                   5'b00000,      // Device #         |  Completer ID
                                   1'b0,          // Function # (Hi)  |
                                   pkt_func_num,  // Function # (Lo)  /
//                                   8'd1,          // Bus #            \
//                                   5'd0,          // Device #         |  Completer ID
//                                   1'b0,          // Function # (Hi)  |
//                                   pkt_func_num, // Function # (Lo)  /
                                   8'b0,   // Tag

                                   16'h00AF,//REQUESTER_ID, //
                                   1'b0,
                                  (pkt_type == TYPE_CFGRD) ? 4'b1000 : 4'b1010, // Req Type
                                  11'd1,
                                  32'b0,
                                  16'b0,
                                   4'b0,
                                   pkt_reg_num,    // Ext Reg Number, Register Number
                                   2'b00           // Reserved
                                   };

            pg_s_axis_rq_tvalid = 1'b1;

	    if(AXISTEN_IF_RQ_ALIGNMENT_MODE == "TRUE" && (pkt_type == TYPE_CFGWR || pkt_type == TYPE_MSGD)) begin
              pg_s_axis_rq_tkeep  =  8'hFF;
              pg_s_axis_rq_tlast = 1'b0;
	    end else begin
              pg_s_axis_rq_tkeep  = (pkt_type == TYPE_CFGWR) ? 8'h1F : 8'h0F;
              pg_s_axis_rq_tlast = 1'b1;
	    end


          end // ST_CFG0
          ST_CFG1: begin
            pg_s_axis_rq_tdata = {224'b0,
                                  pkt_data[31:24], 
                                  pkt_data[23:16], 
                                  pkt_data[15: 8], 
                                  pkt_data[ 7: 0] 
                                  };
            pg_s_axis_rq_tkeep  = 8'b1;
            pg_s_axis_rq_tvalid = 1'b1;
            pg_s_axis_rq_tlast  = 1'b1;
            pg_s_axis_rq_tuser = {AXI4_RQ_TUSER_WIDTH{1'b0}};
          end // ST_CFG1

          ST_MSG0: begin
            pg_s_axis_rq_tuser = {32'b0,
                                   4'b1010,      // Seq Number
                                   8'h00,        // TPH Steering Tag
                                   1'b0,         // TPH indirect Tag Enable
                                   2'b0,         // TPH Type
                                   1'b0,         // TPH Present
                                   1'b0,         // Discontinue
                                   3'b000,       // Byte Lane number in case of Address Aligned mode
                                   4'b0,         //last_dw_be_,    // Last BE of the Read Data
                                   4'b0};        //first_dw_be_ }; // First BE of the Read Data

            pg_s_axis_rq_tdata = { 96'b0,
                                   (pkt_type == TYPE_MSGD && AXISTEN_IF_RQ_ALIGNMENT_MODE == "FALSE" ) ? pkt_data : 32'b0,  
                                   1'b0,   // Force ECRC
                                   3'b0,   // Attribute
                                   3'b0,   // TC
                                   1'b0,   // RID
                                   5'b0,   // rsvd
                                   pkt_msg_routing,                         // Msg Routing
                                   pkt_msg_code,                            // Message Code
                                   8'b0,   // Tag

                                   REQUESTER_ID, //
                                   1'b0,
                                   4'b1100, // Req Type
                                  (pkt_type == TYPE_MSGD) ? 11'd1 : 11'd0,     // Length

                                   64'h0 }; // Addr[31:2], Reserved, Addr[63:32]

            pg_s_axis_rq_tvalid = 1'b0;

	    if(AXISTEN_IF_RQ_ALIGNMENT_MODE == "TRUE" && pkt_type == TYPE_MSGD) begin
              pg_s_axis_rq_tkeep  =  8'hFF;
              pg_s_axis_rq_tlast  = 1'b0;
	    end else begin
              pg_s_axis_rq_tkeep  = (pkt_type == TYPE_MSGD) ? 8'h1F : 8'h0F;
              pg_s_axis_rq_tlast  = 1'b1;
	    end

          end // ST_MSG0
          ST_MSG1: begin
            pg_s_axis_rq_tdata = {224'b0, pkt_data};
            pg_s_axis_rq_tkeep  = 8'b1;
            pg_s_axis_rq_tvalid = 1'b1;
            pg_s_axis_rq_tlast  = 1'b1;
            pg_s_axis_rq_tuser = {AXI4_RQ_TUSER_WIDTH{1'b0}};
          end // ST_MSG1

          default: begin
            // No TLP active
            pg_s_axis_rq_tlast  = 1'b0;
            pg_s_axis_rq_tuser = {AXI4_RQ_TUSER_WIDTH{1'b0}};
            pg_s_axis_rq_tdata  = {C_DATA_WIDTH{1'b0}};
            pg_s_axis_rq_tkeep  = {KEEP_WIDTH{1'b0}};
            pg_s_axis_rq_tvalid = 1'b0;
          end // default case
        endcase
      end
    end // End 256-bit Mode

    else begin : width_512

       // State-machine and controller hand-shake
      always @(posedge user_clk) begin
        if (reset) begin
          pkt_state          <= #TCQ ST_IDLE;
          pkt_done           <= #TCQ 1'b0;
        end else begin
          case (pkt_state)
            ST_IDLE: begin
              // Idle - wait for Controller to request TLP transmission

              pkt_done       <= #TCQ 1'b0;

              if (pkt_start) begin
                if (pkt_type == TYPE_CFGRD || pkt_type == TYPE_CFGWR) begin
                  pkt_state  <= #TCQ ST_CFG0;
                end else begin
//                  pkt_state  <= #TCQ ST_MSG0;
                  pkt_state  <= #TCQ ST_CFG0;
                end
              end
            end // ST_IDLE

            ST_CFG0: begin : beat_1_CFG0
              // First 2 QW's (4 dwords) of a CfgRd0 or CfgWr0 TLP
              if (pg_s_axis_rq_tready) begin
	        if(AXISTEN_IF_RQ_ALIGNMENT_MODE == "TRUE" && (pkt_type == TYPE_CFGWR || pkt_type == TYPE_MSGD)) begin
                  pkt_state    <= #TCQ ST_CFG1;
                  pkt_done     <= #TCQ 1'b0;
		end
		else begin 
                  pkt_state    <= #TCQ ST_IDLE;
                  pkt_done     <= #TCQ 1'b1;
		end
              end
            end // ST_CFG0
            ST_CFG1: begin : beat_2_CFG0
              // First 2 QW's (4 dwords) of a CfgRd0 or CfgWr0 TLP
              if (pg_s_axis_rq_tready) begin
                pkt_state    <= #TCQ ST_IDLE;
                pkt_done     <= #TCQ 1'b1;
              end
            end // ST_CFG1

            ST_MSG0: begin : beat_0_MSG
              // First 2 QW's of a Msg or MsgD TLP
	        if(AXISTEN_IF_RQ_ALIGNMENT_MODE == "TRUE" && pkt_type == TYPE_MSGD) begin
                  pkt_state    <= #TCQ ST_MSG1;
                  pkt_done     <= #TCQ 1'b0;
		end
		else begin 
                  pkt_state    <= #TCQ ST_IDLE;
                  pkt_done     <= #TCQ 1'b1;
		end
            end // ST_MSG0
            ST_MSG1: begin : beat_1_MSG
              // First 2 QW's of a Msg or MsgD TLP
              if (pg_s_axis_rq_tready) begin
                  pkt_state    <= #TCQ ST_IDLE;
                  pkt_done     <= #TCQ 1'b1;
                end
            end // ST_MSG1

            default: begin
              pkt_state      <= #TCQ ST_IDLE;
            end // default case
          endcase
        end
      end

      always @* begin
        case (pkt_state)
          ST_CFG0: begin
            pg_s_axis_rq_tuser = {32'b0,
                                   6'b1010,      // Seq Number
                                   29'b0,
                                  (pkt_type == TYPE_CFGWR) ? 4'b0100 : 4'b0011, //is_eop_ptr 
                                   2'b01,        //is_eop
                                   2'b00,
                                   2'b00,        //is_sop_ptr
                                   2'b1,         //is_sop
                                   4'b00,        // TPH Steering Tag
                                   1'b0,         // TPH indirect Tag Enable
                                   2'b0,         // TPH Type
                                   1'b0,         // TPH Present
                                   1'b0,         // Discontinue
                                   3'b000,       // Byte Lane number in case of Address Aligned mode
                                   4'b0,         //last_dw_be_,    // Last BE of the Read Data
                                   pkt_1dw_be};  //first_dw_be_ }; // First BE of the Read Data

            pg_s_axis_rq_tdata = {256'b0,
                                  96'b0,
                                  (pkt_type == TYPE_CFGWR && AXISTEN_IF_RQ_ALIGNMENT_MODE == "FALSE" ) ? pkt_data : 32'b0,  
                                   1'b0,   // Force ECRC
                                   3'b0,   // Attribute
                                   3'b0,   // TC
                                   1'b0,   // RID
                                   8'h01,         // Bus #            \
                                   5'b00000,      // Device #         |  Completer ID
                                   1'b0,          // Function # (Hi)  |
                                   pkt_func_num,  // Function # (Lo)  /
//                                   8'd1,          // Bus #            \
//                                   5'd0,          // Device #         |  Completer ID
//                                   1'b0,          // Function # (Hi)  |
//                                   pkt_func_num, // Function # (Lo)  /
                                   8'b0,   // Tag

                                   16'h00AF,//REQUESTER_ID, //
                                   1'b0,
                                  (pkt_type == TYPE_CFGRD) ? 4'b1000 : 4'b1010, // Req Type
                                  11'd1,
                                  32'b0,
                                  16'b0,
                                   4'b0,
                                   pkt_reg_num,    // Ext Reg Number, Register Number
                                   2'b00           // Reserved
                                   };

            pg_s_axis_rq_tvalid = 1'b1;

	    if(AXISTEN_IF_RQ_ALIGNMENT_MODE == "TRUE" && (pkt_type == TYPE_CFGWR || pkt_type == TYPE_MSGD)) begin
              pg_s_axis_rq_tkeep  =  8'hFF;
              pg_s_axis_rq_tlast = 1'b0;
	    end else begin
              pg_s_axis_rq_tkeep  = (pkt_type == TYPE_CFGWR) ? 8'h1F : 8'h0F;
              pg_s_axis_rq_tlast = 1'b1;
	    end


          end // ST_CFG0
          ST_CFG1: begin
            pg_s_axis_rq_tdata = {256'b0,224'b0,
                                  pkt_data[31:24], 
                                  pkt_data[23:16], 
                                  pkt_data[15: 8], 
                                  pkt_data[ 7: 0] 
                                  };
            pg_s_axis_rq_tkeep  = 8'b1;
            pg_s_axis_rq_tvalid = 1'b1;
            pg_s_axis_rq_tlast  = 1'b1;
            pg_s_axis_rq_tuser = {AXI4_RQ_TUSER_WIDTH{1'b0}};
          end // ST_CFG1

          ST_MSG0: begin
            pg_s_axis_rq_tuser = {32'b0,
                                   4'b1010,      // Seq Number
                                   8'h00,        // TPH Steering Tag
                                   1'b0,         // TPH indirect Tag Enable
                                   2'b0,         // TPH Type
                                   1'b0,         // TPH Present
                                   1'b0,         // Discontinue
                                   3'b000,       // Byte Lane number in case of Address Aligned mode
                                   4'b0,         //last_dw_be_,    // Last BE of the Read Data
                                   4'b0};        //first_dw_be_ }; // First BE of the Read Data

            pg_s_axis_rq_tdata = { 256'b0,96'b0,
                                   (pkt_type == TYPE_MSGD && AXISTEN_IF_RQ_ALIGNMENT_MODE == "FALSE" ) ? pkt_data : 32'b0,  
                                   1'b0,   // Force ECRC
                                   3'b0,   // Attribute
                                   3'b0,   // TC
                                   1'b0,   // RID
                                   5'b0,   // rsvd
                                   pkt_msg_routing,                         // Msg Routing
                                   pkt_msg_code,                            // Message Code
                                   8'b0,   // Tag

                                   REQUESTER_ID, //
                                   1'b0,
                                   4'b1100, // Req Type
                                  (pkt_type == TYPE_MSGD) ? 11'd1 : 11'd0,     // Length

                                   64'h0 }; // Addr[31:2], Reserved, Addr[63:32]

            pg_s_axis_rq_tvalid = 1'b0;

	    if(AXISTEN_IF_RQ_ALIGNMENT_MODE == "TRUE" && pkt_type == TYPE_MSGD) begin
              pg_s_axis_rq_tkeep  =  8'hFF;
              pg_s_axis_rq_tlast  = 1'b0;
	    end else begin
              pg_s_axis_rq_tkeep  = (pkt_type == TYPE_MSGD) ? 8'h1F : 8'h0F;
              pg_s_axis_rq_tlast  = 1'b1;
	    end

          end // ST_MSG0
          ST_MSG1: begin
            pg_s_axis_rq_tdata = {256'b0,224'b0, pkt_data};
            pg_s_axis_rq_tkeep  = 8'b1;
            pg_s_axis_rq_tvalid = 1'b1;
            pg_s_axis_rq_tlast  = 1'b1;
            pg_s_axis_rq_tuser = {AXI4_RQ_TUSER_WIDTH{1'b0}};
          end // ST_MSG1

          default: begin
            // No TLP active
            pg_s_axis_rq_tlast  = 1'b0;
            pg_s_axis_rq_tuser = {AXI4_RQ_TUSER_WIDTH{1'b0}};
            pg_s_axis_rq_tdata  = {C_DATA_WIDTH{1'b0}};
            pg_s_axis_rq_tkeep  = {KEEP_WIDTH{1'b0}};
            pg_s_axis_rq_tvalid = 1'b0;
          end // default case
        endcase
      end
    end // End 512-bit Mode



    endgenerate

endmodule // cgator_pkt_generator
