//-------------------------------------------------------------------
//                                                                 
//-------------------------------------------------------------------------
//© 2024 Microchip Technology Inc. and its subsidiaries
//All rights reserved.
//ANY USE OR REDISTRIBUTION IN PART OR IN WHOLE MUST BE HANDLED IN
//ACCORDANCE WITH THE MICROCHIP LICENSE AGREEMENT AND MUST BE APPROVED
//IN ADVANCE IN WRITING.
//-------------------------------------------------------------------------
//
//-------------------------------------------------------------------
// Title        : bit_slip_shift.v
// Created      : November-2017
// Description  : This module implements the shift in incoming transceiver
// 		  data based on RX_SLIP assertion to acheive symbol alignment
// Revision     : NA
// Hierarchy    :
//                bit_slip_PMA_top

//                  -- bit_slip_shift.v             <-- This module
//
// Target device: <Family:PolarFire> <Die:MPF300TS_ES> <Pkg:FCG1152>
//-------------------------------------------------------------------

module bit_slip_shft(
 clk_i          ,//input clock
 reset_n_i      ,//reset for logic		
 rx_data_in , //XCVR RX data
 rx_slip_out, //RX_SLIP output signal to XCVR	
 valid_signal, //denotes symbol alignment
 rx_val_i //RX_VALID signal from XCVR
);

//*****************************************************************************
//************************ Signal Declaration     *****************************
//*****************************************************************************

parameter  [39:0] K_CHAR = 40'h00000000bc;

//----------STATE parameter declaration---------

parameter [2:0] STATE_0 = 3'b000;
parameter [2:0] STATE_1 = 3'b001;
parameter [2:0] STATE_2 = 3'b010;
//----------------------------------------------

//----------------------------------------------

input clk_i;
input reset_n_i;
input rx_val_i;
input [39:0] rx_data_in;
output reg rx_slip_out;
output reg valid_signal;

reg [2:0] case_count;

//--------------Logic for implementing data shift based on RX_SLIP port to
//acheive symbol alignment-----------------

always @(posedge clk_i or negedge reset_n_i)
begin
 if(reset_n_i == 1'b0)
 begin
  rx_slip_out <= 1'b0;
  valid_signal <= 1'b0;
  case_count <= 3'b000;
 end
 else
 begin
  case(case_count)
	STATE_0:
	begin
		if(rx_val_i == 1'b1)
		begin
			rx_slip_out <= 1'b0;
			valid_signal <= 1'b0;
			case_count <= STATE_1;
		end
		else
		begin
			rx_slip_out <= 1'b0;
			valid_signal <= 1'b0;
			case_count <= STATE_0;
		end
	end
	STATE_1:
	begin
		if(rx_data_in == K_CHAR)
		begin
			rx_slip_out <= 1'b0;
			valid_signal <= 1'b1;
			case_count <= STATE_2;
		end
		else
		begin
			rx_slip_out <= 1'b1;//assert RX_SLIP 
			valid_signal <= 1'b0;
			case_count <= STATE_0;
		end
	end
	STATE_2:
	begin
			rx_slip_out <= 1'b0;
			valid_signal <= 1'b1;//symbol alignment 
			case_count <= STATE_2;
	end 
	default:
	begin
		rx_slip_out <= 1'b0;
		valid_signal <= 1'b1;
		case_count <= STATE_0;
	end
endcase
end
end

//--------------------------------------------------------------------

endmodule

