`timescale 1 ns/100 ps
module APB_register_blk(
  PCLK,
  PENABLE,
  PSEL,
  PRESETN,
  PWRITE,
  PREADY,
  PSLVERR,
  PADDR,
  PWDATA,
  PRDATA,

  START_sysservice,

  NRBG_DATA,
  NRBG_HANDLE,

  SELF_TEST_SERVICE, 
  INSTANTIATE_SERVICE,
  GENERATE_SERVICE);


input  PCLK,PENABLE, PSEL, PRESETN, PWRITE;
input  [31:0] PADDR;
input  [31:0] PWDATA;
output reg [31:0] PRDATA;
output reg PREADY;
output PSLVERR;

input [31:0]   	    NRBG_DATA;
input  [7:0]        NRBG_HANDLE;

output reg 			START_sysservice;

output reg			SELF_TEST_SERVICE;
output reg			INSTANTIATE_SERVICE;
output reg			GENERATE_SERVICE;

reg  [1:0]   fsm; //state machine register
reg  [3:0]   count;

wire [15:0]  reg0_16, reg1_16, reg2_16, reg3_16, reg4_16;// Conver 8-bit hex to 16-bit ascii

assign PSLVERR = 1'b0;

Binary2Ascii u0 (.ascii(reg0_16), .binary(NRBG_HANDLE));
Binary2Ascii u1 (.ascii(reg1_16), .binary(NRBG_DATA[31:24]));
Binary2Ascii u2 (.ascii(reg2_16), .binary(NRBG_DATA[23:16]));
Binary2Ascii u3 (.ascii(reg3_16), .binary(NRBG_DATA[15:8]));
Binary2Ascii u4 (.ascii(reg4_16), .binary(NRBG_DATA[7:0]));


always@(posedge PCLK or negedge PRESETN)
if(~PRESETN)
  begin
      fsm <= 2'b00;
      PREADY <= 1'b1;
	  PRDATA 				<= 16'h00;
      START_sysservice 		<= 1'b0;
	  SELF_TEST_SERVICE 	<= 1'b0;
	  INSTANTIATE_SERVICE 	<= 1'b0;
	  GENERATE_SERVICE 		<= 1'b0;
  end
else
  begin
  
  case (fsm)
     2'b00 :  begin
                  if (~PSEL)
                        begin
                          fsm <= 2'b00;
                        end
                  else        
                        begin
                          fsm <= 2'b01;
						  PREADY <= 1'b0;
						end
			  end
	
     2'b01 :  begin
                          PREADY <= 1'b0; 
                          fsm <= 2'b10; 
                          if ((PENABLE) && (PWRITE))
                          	begin 
								if (PADDR[15:0] == 16'h0000)
								begin
									START_sysservice	<= PWDATA[0];
									SELF_TEST_SERVICE 	<= PWDATA[1];
									INSTANTIATE_SERVICE <= PWDATA[2];
									GENERATE_SERVICE 	<= PWDATA[3]; 
								end		
							end
                          if ((PENABLE) && (!PWRITE))
                          	begin 
								case (PADDR[15:0])
   									16'h9000 : begin 
               									PRDATA 	<= reg0_16;
            								end	
   									16'h9004 : begin 
               									PRDATA 	<= reg1_16;
            								end	
   									16'h9008 : begin 
               									PRDATA 	<= reg2_16;
            								end	
   									16'h900c : begin 
               									PRDATA 	<= reg3_16;
            								end	
   									16'h9010 : begin 
               									PRDATA 	<= reg4_16;
            								end	
   									default : begin 
               									PRDATA 	<= 16'h0000;
            								end	
								endcase
							end

               end

     2'b10 :  begin            
                          fsm <= 2'b00;
                          PREADY <= 1'b1;                              
              end


    default : fsm <= 2'b00;
  endcase
end




  
endmodule