`timescale 1ps/1ps
module apb_master_ext#(
	parameter	p_apb_data_width = 32,
	parameter	p_apb_addr_width = 32,
	parameter	p_apb_strb_width = p_apb_data_width/8
	)(
	input							i_apb_pready,
	input  [p_apb_data_width-1:0]	i_apb_prdata,
	input							i_apb_pslverr,
	input							i_apb_presetn,
	input							i_apb_pclk,
	input							i_apb_tran_en,
	input [p_apb_addr_width-1:0]	i_config_addr,
	input [p_apb_data_width-1:0]	i_config_data,
	input							i_config_wrt_rdb,
	output [p_apb_data_width-1:0]	o_apb_prdata_to_vio,
	output							o_apb_tran_done,
	output [p_apb_addr_width-1:0]	o_apb_paddr,
	output [1:0]					o_apb_pprot,
	output							o_apb_psel,
	output							o_apb_penable,
	output							o_apb_pwrite,
	output [p_apb_data_width-1:0]	o_apb_pwdata,
	output [p_apb_strb_width-1:0]	o_apb_pstrb
	);
	
	// local parameter	
	localparam	LP_APB_MST_IDLE		= 4'd0;
	localparam	LP_APB_MST_SETUP	= 4'd1;
	localparam	LP_APB_MST_ACCESS	= 4'd2;
	//wire	 declaration
	//reg declaration
	reg	[3:0]			r_apb_mst_ps;
	reg	[3:0]			r_apb_mst_ns;

	reg					r_apb_psel;
	reg					r_apb_penable;
	reg					r_apb_pwrite;
	reg	[p_apb_data_width-1:0] r_apb_pwdata;
	reg [p_apb_addr_width-1:0] r_apb_paddr;
	reg					r_curr_state_access;
	reg [p_apb_data_width-1:0]	r_apb_prdata_to_vio;

	// assig statements
	assign o_apb_psel		= r_apb_psel;
	assign o_apb_penable	= r_apb_penable;
	assign o_apb_pwrite		= r_apb_pwrite;
	assign o_apb_pwdata		= r_apb_pwdata;
	assign o_apb_paddr		= r_apb_paddr;
	assign o_apb_pprot		= 2'b00;
	assign o_apb_pstrb		= {(p_apb_strb_width){1'b1}};
	assign o_apb_prdata_to_vio = r_apb_prdata_to_vio;

	assign o_apb_tran_done = i_apb_pready && r_apb_penable;


	// next state assignment
	always @ ( posedge i_apb_pclk or negedge i_apb_presetn )
		begin
			if( i_apb_presetn == 1'b0 )
				begin
					r_apb_mst_ps	<= LP_APB_MST_IDLE;
				end
			else
				begin
					r_apb_mst_ps	<= r_apb_mst_ns;
				end
		end
	//next state calculation
	always @ ( * )
		begin
			case( r_apb_mst_ps )
				LP_APB_MST_IDLE:
					begin
						if( i_apb_tran_en == 1'b1)
							begin
								r_apb_mst_ns = LP_APB_MST_SETUP;
							end
						else
							begin
								r_apb_mst_ns = LP_APB_MST_IDLE;
							end
					end
				LP_APB_MST_SETUP:	
					begin
						r_apb_mst_ns = LP_APB_MST_ACCESS;
					end
				LP_APB_MST_ACCESS:
					begin
						if(o_apb_tran_done == 1'b1)
							begin
								if( i_apb_tran_en == 1'b1 )
									begin
										r_apb_mst_ns = LP_APB_MST_SETUP;
									end
								else
									begin
										r_apb_mst_ns = LP_APB_MST_IDLE;
									end
							end
						else
							begin
								r_apb_mst_ns = LP_APB_MST_ACCESS;
							end
					end
				default:
					begin		
						r_apb_mst_ns = LP_APB_MST_IDLE;
					end
			endcase
		end
	// state output tran
	always @ ( posedge i_apb_pclk or negedge i_apb_presetn )
		begin
			if( i_apb_presetn == 1'b0 )
				begin
					r_apb_psel			<= 1'b0;
					r_apb_penable		<= 1'b0;
					r_apb_pwrite		<= 1'b0;
					r_apb_paddr			<= {(p_apb_addr_width){1'b0}};
					r_apb_pwdata		<= {(p_apb_data_width){1'b0}};
					r_curr_state_access	<= 1'b0;
				end
			else
				begin
					case( r_apb_mst_ns )
						LP_APB_MST_IDLE:
							begin
								r_apb_psel			<= 1'b0;
								r_apb_penable		<= 1'b0;
								r_apb_pwrite		<= 1'b0;
								r_apb_paddr			<= {(p_apb_addr_width){1'b0}};
								r_apb_pwdata		<= {(p_apb_data_width){1'b0}};
								r_curr_state_access	<= 1'b0;
							end
						LP_APB_MST_SETUP:
							begin
								r_apb_psel			<= 1'b1;
								r_apb_penable		<= 1'b0;
								r_apb_pwrite		<= i_config_wrt_rdb;
								r_apb_paddr			<= i_config_addr;
								//if( i_config_wrt_rdb == 1'b1 ) begin
								r_apb_pwdata		<= i_config_data;
								//end else begin
								//r_apb_pwdata		<= r_apb_pwdata;
								//end
								r_curr_state_access	<= 1'b0;
							end
						LP_APB_MST_ACCESS:
							begin
								r_apb_psel			<= 1'b1;
								r_apb_penable		<= 1'b1;
								r_apb_pwrite		<= r_apb_pwrite;
								r_apb_paddr			<= r_apb_paddr;
								r_apb_pwdata		<= r_apb_pwdata;
								r_curr_state_access	<= 1'b1;
							end
						default:
							begin
								r_apb_psel			<= 1'b0;
								r_apb_penable		<= 1'b0;
								r_apb_pwrite		<= 1'b0;
								r_apb_paddr			<= {(p_apb_addr_width){1'b0}};
								r_apb_pwdata		<= {(p_apb_data_width){1'b0}};
								r_curr_state_access	<= 1'b0;
							end
					endcase
				end
		end
	// READ DATA LATCH
	// in apb read mode, if apb_pready, apb_penable, apb_psel are asserted.
	always @ ( posedge i_apb_pclk or negedge i_apb_presetn )
		begin
			if( i_apb_presetn == 1'b0 )
				begin
					r_apb_prdata_to_vio		<= {(p_apb_data_width){1'b0}};
				end
			else
				begin
					if( i_apb_pready == 1'b1 && o_apb_penable == 1'b1 && o_apb_psel == 1'b1 && o_apb_pwrite == 1'b0 )
						begin
							r_apb_prdata_to_vio <= i_apb_prdata;
						end
					else
						begin
							r_apb_prdata_to_vio <= r_apb_prdata_to_vio; 
						end
				end
		end

	
endmodule

