module  scrambler_reset_inserter( 
            input         clk,
            input  [71:0] in_data,
            output reg [71:0] out_data
        );

reg [8:0] bs_count;
reg       substitue_next;

localparam [8:0] BS = 9'b110111100;  // K28.5 0x1BC
localparam [8:0] SR = 9'b100011100;  // K28.0 0x11C

initial begin
    bs_count       = 9'b0;
    substitue_next = 1'b0;
    out_data       = 72'b0;
end

always @(posedge clk) begin
    //----------------------------------------------
    // Subsitute every 512nd Blank start (BS) symbol
    // with a Scrambler Reset (SR) symbol. 
    //----------------------------------------------
    out_data  <= in_data;

    if(substitue_next == 1'b1) begin
        if(in_data[8:0]   == BS) begin
            out_data[8:0]   <= SR;
        end

        if(in_data[26:18] == BS) begin
            out_data[26:18] <= SR;
        end

        if(in_data[44:36] == BS) begin
            out_data[44:36] <= SR;
        end

        if(in_data[62:54] == BS) begin
            out_data[62:54] <= SR;
        end
    end

    if(in_data[8:0] == BS) begin
    	if(bs_count == 1'b0) begin
            substitue_next <= 1'b1;
    	end else begin
            substitue_next <= 1'b0;
    	end
    	bs_count <= bs_count + 1;
    end
end

endmodule