module p_data2pipe #( parameter PipeSpec = `PS_def ) (
        input clock,
        input reset,

        input send,

        input [`P_Data_m(PipeSpec):0] data,
        input data_valid,

        inout [`P_m(PipeSpec):0] pipe
    );

    reg [`P_Data_m(PipeSpec):0] out_data;
    reg out_valid;
    wire out_ready;
    reg out_start;
    reg out_stop;

    reg send_previous;

    p_pack_valid_ready #( .PipeSpec(PipeSpec) ) p_p_v( out_valid, out_ready, pipe );
    p_pack_data #( .PipeSpec(PipeSpec) ) p_p_d( out_data, pipe );
    p_pack_start_stop #( .PipeSpec(PipeSpec) ) p_p_ss( out_start, out_stop, pipe );

    reg [`P_Data_m(PipeSpec):0] transferred_data;
    reg transferred_any;

    always @(posedge clock) begin
        if ( reset ) begin
            out_data <= 0;
            out_valid <= 0;
            out_start <= 0;
            out_stop <= 0;
            transferred_data <= 0;
            transferred_any <= 0;
            send_previous <= 0;
        end else begin
            send_previous <= send;
            if ( out_valid && out_ready ) begin
                transferred_data <= out_data;
                if ( data_valid && (( data != out_data ) || ( send && ~send_previous ) ) ) begin
                    out_data <= data;
                end else begin
                    out_data <= 0;
                    out_valid <= 0;
                    out_start <= 0;
                    out_stop <= 0;
                end
            end else begin
                if ( data_valid ) begin
                    if ( ( data != transferred_data ) || !transferred_any ||  ( send && ~send_previous ) ) begin
                        transferred_any <= 1;
                        out_start <= 1;
                        out_stop <= 1;
                        out_data <= data;
                        out_valid <= 1;
                    end else begin
                        out_start <= 0;
                        out_stop <= 0;
                        out_data <= 0;
                        out_valid <= 0;
                    end
                end
            end
        end
    end

endmodule