data/full_repos/permissive/173232294/hdcp_engine.srcs/sources_1/imports/srcs-origin/hdcp_engine.v
1: b'%Warning-IMPLICIT: data/full_repos/permissive/173232294/hdcp_engine.srcs/sources_1/imports/srcs-origin/hdcp_engine.v:116: Signal definition not found, creating implicitly: \'vsync_rising\'\n   assign vsync_rising = vsync_v & !vsync_v2;\n          ^~~~~~~~~~~~\n                   ... Use "/* verilator lint_off IMPLICIT */" and lint_on around source to disable this message.\n%Error: data/full_repos/permissive/173232294/hdcp_engine.srcs/sources_1/imports/srcs-origin/hdcp_engine.v:483: Cannot find file containing module: \'hdcp_cipher\'\n   hdcp_cipher  cipher (\n   ^~~~~~~~~~~\n        ... Looked in:\n             data/full_repos/permissive/173232294/hdcp_engine.srcs/sources_1/imports/srcs-origin,data/full_repos/permissive/173232294/hdcp_cipher\n             data/full_repos/permissive/173232294/hdcp_engine.srcs/sources_1/imports/srcs-origin,data/full_repos/permissive/173232294/hdcp_cipher.v\n             data/full_repos/permissive/173232294/hdcp_engine.srcs/sources_1/imports/srcs-origin,data/full_repos/permissive/173232294/hdcp_cipher.sv\n             hdcp_cipher\n             hdcp_cipher.v\n             hdcp_cipher.sv\n             obj_dir/hdcp_cipher\n             obj_dir/hdcp_cipher.v\n             obj_dir/hdcp_cipher.sv\n%Error: Exiting due to 1 error(s), 1 warning(s)\n'
module hdcp_engine (
		   input wire 	 clk,
		   input 	 reset,


		   input 	 hpd,


		   input [3:0] 	 ctl_code,
		   input 	 cv,
		   input 	 de,
		   input 	 line_end,
		   input 	 hsync,
		   input 	 vsync,
		   input 	 hdcp_ena,


		   input 	 Km_rdy,
		   input [55:0]  Km,
		    

		   input 	 An_rdy,
		   input [63:0]  An,


		   output [23:0] cipher_stream, 
		   output 	 hdcp_is_ready
		    );

   wire 			 Aksv14_write;
   assign Aksv14_write = An_rdy;
   
   reg 				 hpd_n;
   always @(posedge clk) begin

      hpd_n <= !hpd;
   end


   reg 	hdmi_vsync_pol;
   reg 	hdmi_hsync_pol;
   reg 	vsync_v_raw, hsync_v_raw;
   reg  hdmi_de_d;
   wire hdmi_de_rising;
   
   assign hdmi_de_rising = !hdmi_de_d & de;

   always @(posedge clk) begin
      if(reset) begin
	 hdmi_vsync_pol <= 0;
	 hdmi_hsync_pol <= 0;
	 vsync_v_raw <= 0;
	 hsync_v_raw <= 0;
      end else begin
	 hdmi_de_d <= de;

	 if(cv) begin
	    vsync_v_raw <= vsync;
	    hsync_v_raw <= hsync;
	 end else begin
	    vsync_v_raw <= vsync_v_raw;
	    hsync_v_raw <= hsync_v_raw;
	 end

	 if( hdmi_de_rising ) begin
	    hdmi_vsync_pol <= !vsync_v_raw;
	    hdmi_hsync_pol <= !hsync_v_raw;
	 end else begin
	    hdmi_vsync_pol <= hdmi_vsync_pol;
	    hdmi_hsync_pol <= hdmi_hsync_pol;
	 end
      end
   end

   reg 	      vsync_v;
   reg 	      hsync_v;
   reg 	      hsync_v2;
   reg 	      vsync_v2;
   
   always @(posedge clk) begin
      if( reset == 1'b1 ) begin
	 vsync_v <= 0;
	 hsync_v <= 0;
	 hsync_v2 <= 0;
	 vsync_v2 <= 0;
      end else begin
	 hsync_v2 <= hsync_v;
	 vsync_v2 <= vsync_v;
   	 if(cv) begin
	    vsync_v <= vsync ^ !hdmi_vsync_pol;
	    hsync_v <= hsync ^ !hdmi_hsync_pol;
	 end else begin
	    vsync_v <= vsync_v;
	    hsync_v <= hsync_v;
	 end
      end
   end
   assign vsync_rising = vsync_v & !vsync_v2;
   



























   parameter HDCP_UNPLUG      = 18'b1 << 0;
   parameter HDCP_WAIT_AKSV   = 18'b1 << 1;
   parameter HDCP_AUTH_PULSE  = 18'b1 << 2;
   parameter HDCP_AUTH        = 18'b1 << 3;
   parameter HDCP_AUTH_WAIT   = 18'b1 << 4;
   parameter HDCP_AUTH_VSYNC_PULSE  = 18'b1 << 5;
   parameter HDCP_AUTH_VSYNC        = 18'b1 << 6;
   parameter HDCP_AUTH_VSYNC_WAIT   = 18'b1 << 7;
   parameter HDCP_WAIT_1001   = 18'b1 << 8;
   parameter HDCP_WAIT_1001_END = 18'b1 << 9;
   parameter HDCP_VSYNC       = 18'b1 << 10;
   parameter HDCP_VSYNC_PULSE = 18'b1 << 11;
   parameter HDCP_VSYNC_WAIT  = 18'b1 << 12;
   parameter HDCP_READY       = 18'b1 << 13;
   parameter HDCP_REKEY       = 18'b1 << 14;
   parameter HDCP_REKEY_PULSE = 18'b1 << 15;
   parameter HDCP_REKEY_WAIT  = 18'b1 << 16;
   parameter HDCP_WAIT_KMRDY  = 18'b1 << 17;

   parameter HDCP_nSTATES = 18;
   
   wire   		    stream_ready;
   reg [(HDCP_nSTATES-1):0] HDCP_cstate = {{(HDCP_nSTATES-1){1'b0}}, 1'b1};
   reg [(HDCP_nSTATES-1):0] HDCP_nstate;

   reg 			    auth_mode;
   reg 			    hdcp_init;
   reg 			    hdcp_rekey;
   wire 		    hdcp_stream_ena;

   reg 			    active_line;
   reg 			    hdcp_rekey_2;
   reg 			    hdcp_rekey_1;

   reg 			    appleTV2_bug;

   reg 			    Km_rdy0;
   reg 			    Km_rdy1;
   reg 			    Km_rdy2;
   wire 		    Km_ready;
   reg 			    hdcp_requested;
   


   always @ (posedge clk) begin
      if( reset == 1'b1 ) begin
	 appleTV2_bug <= 0;
      end else begin
	 if( (HDCP_cstate == HDCP_VSYNC_WAIT) && (ctl_code[3:0] == 4'b1001) ) begin
	    appleTV2_bug <= 1;
	 end else if( (HDCP_cstate == HDCP_READY) ) begin
	    appleTV2_bug <= 0;
	 end else begin
	    appleTV2_bug <= appleTV2_bug;
	 end
      end
   end
   
   assign hdcp_is_ready = (HDCP_cstate == HDCP_READY);
   


   always @ (posedge clk) begin
      if( reset == 1'b1 ) begin
	 active_line <= 1'b0;
	 hdcp_rekey_2 <= 1'b0;
	 hdcp_rekey_1 <= 1'b0;
      end else begin
	 hdcp_rekey_2 <= hdcp_rekey_1;
	 hdcp_rekey_1 <= hdcp_rekey || 
			 (line_end && (HDCP_cstate == HDCP_READY) && de);
	 if( de ) begin
	    active_line <= 1'b1;
	 end else if( !hsync_v & hsync_v2 ) begin
	    active_line <= 1'b0;
	 end
      end
   end
   
   always @ (posedge clk) begin
      if ( reset | hpd_n )
	HDCP_cstate <= HDCP_UNPLUG; 
      else
	if( Aksv14_write ) begin
	   HDCP_cstate <= HDCP_AUTH_PULSE;
	end else begin
	   HDCP_cstate <=#1 HDCP_nstate;
	end
   end

   always @ (*) begin
      case (HDCP_cstate)
	HDCP_UNPLUG: begin
	   HDCP_nstate = hpd_n ? HDCP_UNPLUG : HDCP_WAIT_AKSV;
	end
	HDCP_WAIT_AKSV: begin


	   HDCP_nstate = Aksv14_write ? HDCP_AUTH_PULSE : HDCP_WAIT_AKSV;


	end


	HDCP_WAIT_KMRDY: begin
	   HDCP_nstate = Km_ready ? HDCP_AUTH_PULSE : HDCP_WAIT_KMRDY;
	end
	
















	HDCP_AUTH_PULSE: begin
	   HDCP_nstate = HDCP_AUTH;
	end
	HDCP_AUTH: begin
	   HDCP_nstate = stream_ready? HDCP_AUTH : HDCP_AUTH_WAIT;
	end
	HDCP_AUTH_WAIT: begin
	   HDCP_nstate = stream_ready ? HDCP_AUTH_VSYNC_PULSE : HDCP_AUTH_WAIT;
	end





	HDCP_AUTH_VSYNC_PULSE: begin
	   HDCP_nstate = HDCP_AUTH_VSYNC;
	end
	HDCP_AUTH_VSYNC: begin
	   HDCP_nstate = stream_ready ? HDCP_AUTH_VSYNC : HDCP_AUTH_VSYNC_WAIT;
	end
	HDCP_AUTH_VSYNC_WAIT: begin
	   HDCP_nstate = stream_ready ? HDCP_WAIT_1001 : HDCP_AUTH_VSYNC_WAIT;
	end


	HDCP_WAIT_1001: begin
	   HDCP_nstate = ((vsync_v && (ctl_code[3:0] == 4'b1001)) || appleTV2_bug) ? 
			 HDCP_WAIT_1001_END : HDCP_WAIT_1001;
	end
	HDCP_WAIT_1001_END: begin
	   HDCP_nstate = (vsync_v && (ctl_code[3:0] == 4'b1001)) ?
			 HDCP_WAIT_1001_END : HDCP_READY;
	end
	

	HDCP_VSYNC_PULSE: begin
	   HDCP_nstate = HDCP_VSYNC;
	end
	HDCP_VSYNC: begin
	   HDCP_nstate = stream_ready ? HDCP_VSYNC : HDCP_VSYNC_WAIT;
	end
	HDCP_VSYNC_WAIT: begin
	   HDCP_nstate = stream_ready ? HDCP_WAIT_1001 : HDCP_VSYNC_WAIT;
	end


	HDCP_READY: begin









	   HDCP_nstate = (stream_ready == 1'b0) ? HDCP_REKEY_WAIT : 
			 vsync_rising ? HDCP_VSYNC_PULSE :
			 HDCP_READY;
	end

	HDCP_REKEY_PULSE: begin
	   HDCP_nstate = HDCP_REKEY;
	end
	HDCP_REKEY: begin
	   HDCP_nstate = stream_ready ? HDCP_REKEY : HDCP_REKEY_WAIT;
	end
	HDCP_REKEY_WAIT: begin
	   HDCP_nstate = stream_ready ? HDCP_READY : HDCP_REKEY_WAIT;
	end
      endcase
   end


   assign Km_ready = Km_rdy2;
		     
   always @ (posedge clk) begin
      if( reset | hpd_n ) begin
	 auth_mode <=#1 1'b0;
	 hdcp_init <=#1 1'b0;
	 hdcp_rekey <=#1 1'b0;
	 hdcp_requested <=#1 1'b0;
	 
	 Km_rdy0 <= 1'b0;
	 Km_rdy1 <= 1'b0;
	 Km_rdy2 <= 1'b0;
      end else begin
	 Km_rdy0 <= Km_rdy;
	 Km_rdy1 <= Km_rdy0;
	 Km_rdy2 <= Km_rdy1;

	 case (HDCP_cstate)
	   HDCP_UNPLUG: begin
	      auth_mode <=#1 1'b0;
	      hdcp_init <=#1 1'b0;
	      hdcp_rekey <=#1 1'b0;
	      hdcp_requested <=#1 1'b0;
	   end
	   HDCP_WAIT_AKSV: begin
	      auth_mode <=#1 1'b0;
	      hdcp_init <=#1 1'b0;
	      hdcp_rekey <=#1 1'b0;
	      hdcp_requested <=#1 1'b0;
	   end
	   
	   HDCP_WAIT_KMRDY: begin
	      auth_mode <=#1 1'b0;
	      hdcp_init <=#1 1'b0;
	      hdcp_rekey <=#1 1'b0;
	      hdcp_requested <=#1 1'b0;
	   end
	   
	   HDCP_AUTH_PULSE: begin
	      auth_mode <=#1 1'b1;
	      hdcp_init <=#1 1'b1;
	      hdcp_rekey <=#1 1'b0;
	      hdcp_requested <=#1 1'b0;
	   end
	   HDCP_AUTH: begin
	      auth_mode <=#1 1'b0;
	      hdcp_init <=#1 1'b0;
	      hdcp_rekey <=#1 1'b0;
	      hdcp_requested <=#1 1'b0;
	   end
	   HDCP_AUTH_WAIT: begin
	      auth_mode <=#1 1'b0;
	      hdcp_init <=#1 1'b0;
	      hdcp_rekey <=#1 1'b0;
	      hdcp_requested <=#1 1'b0;
	   end

	   HDCP_AUTH_VSYNC_PULSE: begin
	      auth_mode <=#1 1'b0;
	      hdcp_init <=#1 1'b1;
	      hdcp_rekey <=#1 1'b0;
	      hdcp_requested <=#1 1'b0;
	   end
	   HDCP_AUTH_VSYNC: begin
	      auth_mode <=#1 1'b0;
	      hdcp_init <=#1 1'b0;
	      hdcp_rekey <=#1 1'b0;
	      hdcp_requested <=#1 1'b0;
	   end
	   HDCP_AUTH_VSYNC_WAIT: begin
	      auth_mode <=#1 1'b0;
	      hdcp_init <=#1 1'b0;
	      hdcp_rekey <=#1 1'b0;
	      hdcp_requested <=#1 1'b0;
	   end
	   
	   HDCP_WAIT_1001: begin
	      auth_mode <=#1 1'b0;
	      hdcp_init <=#1 1'b0;
	      hdcp_rekey <=#1 1'b0;
	      hdcp_requested <=#1 1'b0;
	   end
	   HDCP_WAIT_1001_END: begin
	      auth_mode <=#1 1'b0;
	      hdcp_init <=#1 1'b0;
	      hdcp_rekey <=#1 1'b0;
	      hdcp_requested <=#1 1'b0;
	   end
	   
	   HDCP_VSYNC_PULSE: begin
	      auth_mode <=#1 1'b0;
	      hdcp_init <=#1 1'b1;
	      hdcp_rekey <=#1 1'b0;
	      hdcp_requested <=#1 1'b1;
	   end
	   HDCP_VSYNC: begin
	      auth_mode <=#1 1'b0;
	      hdcp_init <=#1 1'b0;
	      hdcp_rekey <=#1 1'b0;
	      hdcp_requested <=#1 1'b1;
	   end
	   HDCP_VSYNC_WAIT: begin
	      auth_mode <=#1 1'b0;
	      hdcp_init <=#1 1'b0;
	      hdcp_rekey <=#1 1'b0;
	      hdcp_requested <=#1 1'b1;
	   end
	   
	   HDCP_READY: begin
	      auth_mode <=#1 1'b0;
	      hdcp_init <=#1 1'b0;
	      hdcp_rekey <=#1 1'b0;
	      hdcp_requested <=#1 1'b1;
	   end
	   
	   HDCP_REKEY_PULSE: begin
	      auth_mode <=#1 1'b0;
	      hdcp_init <=#1 1'b0;

	      hdcp_rekey <=#1 1'b0;


	      hdcp_requested <=#1 1'b1;
	   end
	   HDCP_REKEY: begin
	      auth_mode <=#1 1'b0;
	      hdcp_init <=#1 1'b0;
	      hdcp_rekey <=#1 1'b0;
	      hdcp_requested <=#1 1'b1;
	   end
	   HDCP_REKEY_WAIT: begin
	      auth_mode <=#1 1'b0;
	      hdcp_init <=#1 1'b0;
	      hdcp_rekey <=#1 1'b0;
	      hdcp_requested <=#1 1'b1;
	   end
	 endcase
      end
   end
   
   hdcp_cipher  cipher (
		.clk(clk),
		.reset(reset),
		.Km(Km),
		.An(An),
		.hdcpBlockCipher_init(hdcp_init),
		.authentication(auth_mode),
		.hdcpRekeyCipher(hdcp_rekey_2),
		.hdcpStreamCipher(hdcp_ena && (HDCP_cstate == HDCP_READY)),
		.pr_data(cipher_stream),
		.stream_ready(stream_ready)
		);
   

endmodule
module hdcp_engine (
		   input wire 	 clk,
		   input 	 reset,


		   input 	 hpd,


		   input [3:0] 	 ctl_code,
		   input 	 cv,
		   input 	 de,
		   input 	 line_end,
		   input 	 hsync,
		   input 	 vsync,
		   input 	 hdcp_ena,


		   input 	 Km_rdy,
		   input [55:0]  Km,
		    

		   input 	 An_rdy,
		   input [63:0]  An,


		   output [23:0] cipher_stream, 
		   output 	 hdcp_is_ready
		    );


   wire 			 Aksv14_write;
   assign Aksv14_write = An_rdy;
   
   reg 				 hpd_n;
   always @(posedge clk) begin

      hpd_n <= !hpd;
   end


   reg 	hdmi_vsync_pol;
   reg 	hdmi_hsync_pol;
   reg 	vsync_v_raw, hsync_v_raw;
   reg  hdmi_de_d;
   wire hdmi_de_rising;
   
   assign hdmi_de_rising = !hdmi_de_d & de;

   always @(posedge clk) begin
      if(reset) begin
	 hdmi_vsync_pol <= 0;
	 hdmi_hsync_pol <= 0;
	 vsync_v_raw <= 0;
	 hsync_v_raw <= 0;
      end else begin
	 hdmi_de_d <= de;

	 if(cv) begin
	    vsync_v_raw <= vsync;
	    hsync_v_raw <= hsync;
	 end else begin
	    vsync_v_raw <= vsync_v_raw;
	    hsync_v_raw <= hsync_v_raw;
	 end

	 if( hdmi_de_rising ) begin
	    hdmi_vsync_pol <= !vsync_v_raw;
	    hdmi_hsync_pol <= !hsync_v_raw;
	 end else begin
	    hdmi_vsync_pol <= hdmi_vsync_pol;
	    hdmi_hsync_pol <= hdmi_hsync_pol;
	 end
      end
   end

   reg 	      vsync_v;
   reg 	      hsync_v;
   reg 	      hsync_v2;
   reg 	      vsync_v2;
   
   always @(posedge clk) begin
      if( reset == 1'b1 ) begin
	 vsync_v <= 0;
	 hsync_v <= 0;
	 hsync_v2 <= 0;
	 vsync_v2 <= 0;
      end else begin
	 hsync_v2 <= hsync_v;
	 vsync_v2 <= vsync_v;
   	 if(cv) begin
	    vsync_v <= vsync ^ !hdmi_vsync_pol;
	    hsync_v <= hsync ^ !hdmi_hsync_pol;
	 end else begin
	    vsync_v <= vsync_v;
	    hsync_v <= hsync_v;
	 end
      end
   end
   assign vsync_rising = vsync_v & !vsync_v2;
   



























   parameter HDCP_UNPLUG      = 18'b1 << 0;
   parameter HDCP_WAIT_AKSV   = 18'b1 << 1;
   parameter HDCP_AUTH_PULSE  = 18'b1 << 2;
   parameter HDCP_AUTH        = 18'b1 << 3;
   parameter HDCP_AUTH_WAIT   = 18'b1 << 4;
   parameter HDCP_AUTH_VSYNC_PULSE  = 18'b1 << 5;
   parameter HDCP_AUTH_VSYNC        = 18'b1 << 6;
   parameter HDCP_AUTH_VSYNC_WAIT   = 18'b1 << 7;
   parameter HDCP_WAIT_1001   = 18'b1 << 8;
   parameter HDCP_WAIT_1001_END = 18'b1 << 9;
   parameter HDCP_VSYNC       = 18'b1 << 10;
   parameter HDCP_VSYNC_PULSE = 18'b1 << 11;
   parameter HDCP_VSYNC_WAIT  = 18'b1 << 12;
   parameter HDCP_READY       = 18'b1 << 13;
   parameter HDCP_REKEY       = 18'b1 << 14;
   parameter HDCP_REKEY_PULSE = 18'b1 << 15;
   parameter HDCP_REKEY_WAIT  = 18'b1 << 16;
   parameter HDCP_WAIT_KMRDY  = 18'b1 << 17;

   parameter HDCP_nSTATES = 18;
   
   wire   		    stream_ready;
   reg [(HDCP_nSTATES-1):0] HDCP_cstate = {{(HDCP_nSTATES-1){1'b0}}, 1'b1};
   reg [(HDCP_nSTATES-1):0] HDCP_nstate;

   reg 			    auth_mode;
   reg 			    hdcp_init;
   reg 			    hdcp_rekey;
   wire 		    hdcp_stream_ena;

   reg 			    active_line;
   reg 			    hdcp_rekey_2;
   reg 			    hdcp_rekey_1;

   reg 			    appleTV2_bug;

   reg 			    Km_rdy0;
   reg 			    Km_rdy1;
   reg 			    Km_rdy2;
   wire 		    Km_ready;
   reg 			    hdcp_requested;
   


   always @ (posedge clk) begin
      if( reset == 1'b1 ) begin
	 appleTV2_bug <= 0;
      end else begin
	 if( (HDCP_cstate == HDCP_VSYNC_WAIT) && (ctl_code[3:0] == 4'b1001) ) begin
	    appleTV2_bug <= 1;
	 end else if( (HDCP_cstate == HDCP_READY) ) begin
	    appleTV2_bug <= 0;
	 end else begin
	    appleTV2_bug <= appleTV2_bug;
	 end
      end
   end
   
   assign hdcp_is_ready = (HDCP_cstate == HDCP_READY);
   


   always @ (posedge clk) begin
      if( reset == 1'b1 ) begin
	 active_line <= 1'b0;
	 hdcp_rekey_2 <= 1'b0;
	 hdcp_rekey_1 <= 1'b0;
      end else begin
	 hdcp_rekey_2 <= hdcp_rekey_1;
	 hdcp_rekey_1 <= hdcp_rekey || 
			 (line_end && (HDCP_cstate == HDCP_READY) && de);
	 if( de ) begin
	    active_line <= 1'b1;
	 end else if( !hsync_v & hsync_v2 ) begin
	    active_line <= 1'b0;
	 end
      end
   end
   
   always @ (posedge clk) begin
      if ( reset | hpd_n )
	HDCP_cstate <= HDCP_UNPLUG; 
      else
	if( Aksv14_write ) begin
	   HDCP_cstate <= HDCP_AUTH_PULSE;
	end else begin
	   HDCP_cstate <=#1 HDCP_nstate;
	end
   end

   always @ (*) begin
      case (HDCP_cstate)
	HDCP_UNPLUG: begin
	   HDCP_nstate = hpd_n ? HDCP_UNPLUG : HDCP_WAIT_AKSV;
	end
	HDCP_WAIT_AKSV: begin


	   HDCP_nstate = Aksv14_write ? HDCP_AUTH_PULSE : HDCP_WAIT_AKSV;


	end


	HDCP_WAIT_KMRDY: begin
	   HDCP_nstate = Km_ready ? HDCP_AUTH_PULSE : HDCP_WAIT_KMRDY;
	end
	
















	HDCP_AUTH_PULSE: begin
	   HDCP_nstate = HDCP_AUTH;
	end
	HDCP_AUTH: begin
	   HDCP_nstate = stream_ready? HDCP_AUTH : HDCP_AUTH_WAIT;
	end
	HDCP_AUTH_WAIT: begin
	   HDCP_nstate = stream_ready ? HDCP_AUTH_VSYNC_PULSE : HDCP_AUTH_WAIT;
	end





	HDCP_AUTH_VSYNC_PULSE: begin
	   HDCP_nstate = HDCP_AUTH_VSYNC;
	end
	HDCP_AUTH_VSYNC: begin
	   HDCP_nstate = stream_ready ? HDCP_AUTH_VSYNC : HDCP_AUTH_VSYNC_WAIT;
	end
	HDCP_AUTH_VSYNC_WAIT: begin
	   HDCP_nstate = stream_ready ? HDCP_WAIT_1001 : HDCP_AUTH_VSYNC_WAIT;
	end


	HDCP_WAIT_1001: begin
	   HDCP_nstate = ((vsync_v && (ctl_code[3:0] == 4'b1001)) || appleTV2_bug) ? 
			 HDCP_WAIT_1001_END : HDCP_WAIT_1001;
	end
	HDCP_WAIT_1001_END: begin
	   HDCP_nstate = (vsync_v && (ctl_code[3:0] == 4'b1001)) ?
			 HDCP_WAIT_1001_END : HDCP_READY;
	end
	

	HDCP_VSYNC_PULSE: begin
	   HDCP_nstate = HDCP_VSYNC;
	end
	HDCP_VSYNC: begin
	   HDCP_nstate = stream_ready ? HDCP_VSYNC : HDCP_VSYNC_WAIT;
	end
	HDCP_VSYNC_WAIT: begin
	   HDCP_nstate = stream_ready ? HDCP_WAIT_1001 : HDCP_VSYNC_WAIT;
	end


	HDCP_READY: begin









	   HDCP_nstate = (stream_ready == 1'b0) ? HDCP_REKEY_WAIT : 
			 vsync_rising ? HDCP_VSYNC_PULSE :
			 HDCP_READY;
	end

	HDCP_REKEY_PULSE: begin
	   HDCP_nstate = HDCP_REKEY;
	end
	HDCP_REKEY: begin
	   HDCP_nstate = stream_ready ? HDCP_REKEY : HDCP_REKEY_WAIT;
	end
	HDCP_REKEY_WAIT: begin
	   HDCP_nstate = stream_ready ? HDCP_READY : HDCP_REKEY_WAIT;
	end
      endcase
   end


   assign Km_ready = Km_rdy2;
		     
   always @ (posedge clk) begin
      if( reset | hpd_n ) begin
	 auth_mode <=#1 1'b0;
	 hdcp_init <=#1 1'b0;
	 hdcp_rekey <=#1 1'b0;
	 hdcp_requested <=#1 1'b0;
	 
	 Km_rdy0 <= 1'b0;
	 Km_rdy1 <= 1'b0;
	 Km_rdy2 <= 1'b0;
      end else begin
	 Km_rdy0 <= Km_rdy;
	 Km_rdy1 <= Km_rdy0;
	 Km_rdy2 <= Km_rdy1;

	 case (HDCP_cstate)
	   HDCP_UNPLUG: begin
	      auth_mode <=#1 1'b0;
	      hdcp_init <=#1 1'b0;
	      hdcp_rekey <=#1 1'b0;
	      hdcp_requested <=#1 1'b0;
	   end
	   HDCP_WAIT_AKSV: begin
	      auth_mode <=#1 1'b0;
	      hdcp_init <=#1 1'b0;
	      hdcp_rekey <=#1 1'b0;
	      hdcp_requested <=#1 1'b0;
	   end
	   
	   HDCP_WAIT_KMRDY: begin
	      auth_mode <=#1 1'b0;
	      hdcp_init <=#1 1'b0;
	      hdcp_rekey <=#1 1'b0;
	      hdcp_requested <=#1 1'b0;
	   end
	   
	   HDCP_AUTH_PULSE: begin
	      auth_mode <=#1 1'b1;
	      hdcp_init <=#1 1'b1;
	      hdcp_rekey <=#1 1'b0;
	      hdcp_requested <=#1 1'b0;
	   end
	   HDCP_AUTH: begin
	      auth_mode <=#1 1'b0;
	      hdcp_init <=#1 1'b0;
	      hdcp_rekey <=#1 1'b0;
	      hdcp_requested <=#1 1'b0;
	   end
	   HDCP_AUTH_WAIT: begin
	      auth_mode <=#1 1'b0;
	      hdcp_init <=#1 1'b0;
	      hdcp_rekey <=#1 1'b0;
	      hdcp_requested <=#1 1'b0;
	   end

	   HDCP_AUTH_VSYNC_PULSE: begin
	      auth_mode <=#1 1'b0;
	      hdcp_init <=#1 1'b1;
	      hdcp_rekey <=#1 1'b0;
	      hdcp_requested <=#1 1'b0;
	   end
	   HDCP_AUTH_VSYNC: begin
	      auth_mode <=#1 1'b0;
	      hdcp_init <=#1 1'b0;
	      hdcp_rekey <=#1 1'b0;
	      hdcp_requested <=#1 1'b0;
	   end
	   HDCP_AUTH_VSYNC_WAIT: begin
	      auth_mode <=#1 1'b0;
	      hdcp_init <=#1 1'b0;
	      hdcp_rekey <=#1 1'b0;
	      hdcp_requested <=#1 1'b0;
	   end
	   
	   HDCP_WAIT_1001: begin
	      auth_mode <=#1 1'b0;
	      hdcp_init <=#1 1'b0;
	      hdcp_rekey <=#1 1'b0;
	      hdcp_requested <=#1 1'b0;
	   end
	   HDCP_WAIT_1001_END: begin
	      auth_mode <=#1 1'b0;
	      hdcp_init <=#1 1'b0;
	      hdcp_rekey <=#1 1'b0;
	      hdcp_requested <=#1 1'b0;
	   end
	   
	   HDCP_VSYNC_PULSE: begin
	      auth_mode <=#1 1'b0;
	      hdcp_init <=#1 1'b1;
	      hdcp_rekey <=#1 1'b0;
	      hdcp_requested <=#1 1'b1;
	   end
	   HDCP_VSYNC: begin
	      auth_mode <=#1 1'b0;
	      hdcp_init <=#1 1'b0;
	      hdcp_rekey <=#1 1'b0;
	      hdcp_requested <=#1 1'b1;
	   end
	   HDCP_VSYNC_WAIT: begin
	      auth_mode <=#1 1'b0;
	      hdcp_init <=#1 1'b0;
	      hdcp_rekey <=#1 1'b0;
	      hdcp_requested <=#1 1'b1;
	   end
	   
	   HDCP_READY: begin
	      auth_mode <=#1 1'b0;
	      hdcp_init <=#1 1'b0;
	      hdcp_rekey <=#1 1'b0;
	      hdcp_requested <=#1 1'b1;
	   end
	   
	   HDCP_REKEY_PULSE: begin
	      auth_mode <=#1 1'b0;
	      hdcp_init <=#1 1'b0;

	      hdcp_rekey <=#1 1'b0;


	      hdcp_requested <=#1 1'b1;
	   end
	   HDCP_REKEY: begin
	      auth_mode <=#1 1'b0;
	      hdcp_init <=#1 1'b0;
	      hdcp_rekey <=#1 1'b0;
	      hdcp_requested <=#1 1'b1;
	   end
	   HDCP_REKEY_WAIT: begin
	      auth_mode <=#1 1'b0;
	      hdcp_init <=#1 1'b0;
	      hdcp_rekey <=#1 1'b0;
	      hdcp_requested <=#1 1'b1;
	   end
	 endcase
      end
   end
   
   hdcp_cipher  cipher (
		.clk(clk),
		.reset(reset),
		.Km(Km),
		.An(An),
		.hdcpBlockCipher_init(hdcp_init),
		.authentication(auth_mode),
		.hdcpRekeyCipher(hdcp_rekey_2),
		.hdcpStreamCipher(hdcp_ena && (HDCP_cstate == HDCP_READY)),
		.pr_data(cipher_stream),
		.stream_ready(stream_ready)
		);
   

endmodule