data/full_repos/permissive/197102055/src/cons_buf.v
1: b'%Warning-WIDTH: data/full_repos/permissive/197102055/src/cons_buf.v:187: Operator COND expects 17 bits on the Conditional True, but Conditional True\'s VARREF \'capture_data\' generates 16 bits.\n                                                                       : ... In instance cons_buf\n  capture_data_1T <= #1 capture_wr ? capture_data : capture_data_1T;\n                                   ^\n                ... Use "/* verilator lint_off WIDTH */" and lint_on around source to disable this message.\n%Warning-WIDTH: data/full_repos/permissive/197102055/src/cons_buf.v:188: Operator COND expects 17 bits on the Conditional True, but Conditional True\'s VARREF \'capture_data\' generates 16 bits.\n                                                                       : ... In instance cons_buf\n  capture_data_2T <= #1 capture_wr & cfifo_wr_cnt[0] ? capture_data : capture_data_2T;\n                                                     ^\n%Warning-WIDTH: data/full_repos/permissive/197102055/src/cons_buf.v:189: Operator COND expects 17 bits on the Conditional True, but Conditional True\'s VARREF \'capture_data\' generates 16 bits.\n                                                                       : ... In instance cons_buf\n  capture_data_4T <= #1 capture_wr & &cfifo_wr_cnt[1:0] ? capture_data : capture_data_4T;\n                                                        ^\n%Warning-WIDTH: data/full_repos/permissive/197102055/src/cons_buf.v:190: Operator COND expects 17 bits on the Conditional True, but Conditional True\'s VARREF \'capture_data\' generates 16 bits.\n                                                                       : ... In instance cons_buf\n  capture_data_8T <= #1 capture_wr & &cfifo_wr_cnt[2:0] ? capture_data : capture_data_8T;\n                                                        ^\n%Warning-WIDTH: data/full_repos/permissive/197102055/src/cons_buf.v:191: Operator COND expects 17 bits on the Conditional True, but Conditional True\'s VARREF \'capture_data\' generates 16 bits.\n                                                                       : ... In instance cons_buf\n  capture_data_16T <= #1 capture_wr & &cfifo_wr_cnt[3:0] ? capture_data : capture_data_16T;\n                                                         ^\n%Warning-WIDTH: data/full_repos/permissive/197102055/src/cons_buf.v:333: Operator GTE expects 24 bits on the LHS, but LHS\'s VARREF \'cfifo_cnt\' generates 13 bits.\n                                                                       : ... In instance cons_buf\n                         (cfifo_wr & rising0_req & rising0_hit & cfifo_cnt >= dso_triggerPos) ? 1\'b1 : \n                                                                           ^~\n%Warning-WIDTH: data/full_repos/permissive/197102055/src/cons_buf.v:334: Operator GTE expects 24 bits on the LHS, but LHS\'s VARREF \'cfifo_cnt\' generates 13 bits.\n                                                                       : ... In instance cons_buf\n         (cfifo_wr & falling0_req & falling0_hit & cfifo_cnt >= dso_triggerPos) ? 1\'b1 :\n                                                             ^~\n%Warning-WIDTH: data/full_repos/permissive/197102055/src/cons_buf.v:335: Operator GTE expects 24 bits on the LHS, but LHS\'s VARREF \'cfifo_cnt\' generates 13 bits.\n                                                                       : ... In instance cons_buf\n         (cfifo_wr & rising1_req & rising1_hit & cfifo_cnt >= dso_triggerPos) ? 1\'b1 :\n                                                           ^~\n%Warning-WIDTH: data/full_repos/permissive/197102055/src/cons_buf.v:336: Operator GTE expects 24 bits on the LHS, but LHS\'s VARREF \'cfifo_cnt\' generates 13 bits.\n                                                                       : ... In instance cons_buf\n         (cfifo_wr & falling1_req & falling1_hit & cfifo_cnt >= dso_triggerPos) ? 1\'b1 :\n                                                             ^~\n%Warning-WIDTH: data/full_repos/permissive/197102055/src/cons_buf.v:337: Operator GTE expects 24 bits on the LHS, but LHS\'s VARREF \'cfifo_cnt\' generates 13 bits.\n                                                                       : ... In instance cons_buf\n         (cfifo_wr & rising0a1_req & rising0_hit & rising1_hit & cfifo_cnt >= dso_triggerPos) ? 1\'b1 :\n                                                                           ^~\n%Warning-WIDTH: data/full_repos/permissive/197102055/src/cons_buf.v:338: Operator GTE expects 24 bits on the LHS, but LHS\'s VARREF \'cfifo_cnt\' generates 13 bits.\n                                                                       : ... In instance cons_buf\n         (cfifo_wr & falling0a1_req & falling0_hit & falling1_hit & cfifo_cnt >= dso_triggerPos) ? 1\'b1 :\n                                                                              ^~\n%Warning-WIDTH: data/full_repos/permissive/197102055/src/cons_buf.v:339: Operator GTE expects 24 bits on the LHS, but LHS\'s VARREF \'cfifo_cnt\' generates 13 bits.\n                                                                       : ... In instance cons_buf\n         (cfifo_wr & rising0o1_req & (rising0_hit | rising1_hit) & cfifo_cnt >= dso_triggerPos) ? 1\'b1 :\n                                                                             ^~\n%Warning-WIDTH: data/full_repos/permissive/197102055/src/cons_buf.v:340: Operator GTE expects 24 bits on the LHS, but LHS\'s VARREF \'cfifo_cnt\' generates 13 bits.\n                                                                       : ... In instance cons_buf\n         (cfifo_wr & falling0o1_req & (falling0_hit | falling1_hit) & cfifo_cnt >= dso_triggerPos) ? 1\'b1 :         \n                                                                                ^~\n%Warning-WIDTH: data/full_repos/permissive/197102055/src/cons_buf.v:357: Operator SUB expects 24 bits on the LHS, but LHS\'s VARREF \'cfifo_cnt\' generates 13 bits.\n                                                                       : ... In instance cons_buf\n                        (trigger_hit & ~trigger_hit_1T) ? cfifo_cnt - dso_triggerPos :  \n                                                                    ^\n%Warning-WIDTH: data/full_repos/permissive/197102055/src/cons_buf.v:358: Operator SUB expects 24 bits on the LHS, but LHS\'s VARREF \'redunt_cnt\' generates 13 bits.\n                                                                       : ... In instance cons_buf\n                        (redunt_rd & trigger_hit_1T & redunt_cnt != \'b0) ? redunt_cnt - 1\'b1 : redunt_cnt;         \n                                                                                      ^\n%Warning-WIDTH: data/full_repos/permissive/197102055/src/cons_buf.v:358: Operator COND expects 24 bits on the Conditional False, but Conditional False\'s VARREF \'redunt_cnt\' generates 13 bits.\n                                                                       : ... In instance cons_buf\n                        (redunt_rd & trigger_hit_1T & redunt_cnt != \'b0) ? redunt_cnt - 1\'b1 : redunt_cnt;         \n                                                                         ^\n%Warning-WIDTH: data/full_repos/permissive/197102055/src/cons_buf.v:356: Operator ASSIGNW expects 13 bits on the Assign RHS, but Assign RHS\'s COND generates 24 bits.\n                                                                       : ... In instance cons_buf\nassign redunt_cnt_nxt = (sample_en & ~sample_en_1T) ? dso_triggerPos :\n                      ^\n%Warning-WIDTH: data/full_repos/permissive/197102055/src/cons_buf.v:409: Operator COND expects 24 bits on the Conditional True, but Conditional True\'s CONST \'23\'h0\' generates 23 bits.\n                                                                       : ... In instance cons_buf\nassign div_cnt_nxt = (sample_en & ~sample_en_1T) ? 23\'b0 :\n                                                 ^\n%Warning-WIDTH: data/full_repos/permissive/197102055/src/cons_buf.v:410: Operator COND expects 24 bits on the Conditional True, but Conditional True\'s CONST \'23\'h0\' generates 23 bits.\n                                                                       : ... In instance cons_buf\n       (capture_valid & (div_cnt == dso_sampleDivider)) ? 23\'b0 :\n                                                        ^\n%Warning-WIDTH: data/full_repos/permissive/197102055/src/cons_buf.v:411: Operator COND expects 24 bits on the Conditional True, but Conditional True\'s CONST \'23\'h0\' generates 23 bits.\n                                                                       : ... In instance cons_buf\n       buf_rdy ? 23\'b0 :\n               ^\n%Warning-WIDTH: data/full_repos/permissive/197102055/src/cons_buf.v:417: Operator ASSIGNW expects 16 bits on the Assign RHS, but Assign RHS\'s VARREF \'capture_data_1T\' generates 17 bits.\n                                                                       : ... In instance cons_buf\nassign cfifo_din_nxt = capture_data_1T;\n                     ^\n%Error: data/full_repos/permissive/197102055/src/cons_buf.v:434: Cannot find file containing module: \'cfifo\'\ncfifo cfifo (\n^~~~~\n        ... Looked in:\n             data/full_repos/permissive/197102055/src,data/full_repos/permissive/197102055/cfifo\n             data/full_repos/permissive/197102055/src,data/full_repos/permissive/197102055/cfifo.v\n             data/full_repos/permissive/197102055/src,data/full_repos/permissive/197102055/cfifo.sv\n             cfifo\n             cfifo.v\n             cfifo.sv\n             obj_dir/cfifo\n             obj_dir/cfifo.v\n             obj_dir/cfifo.sv\n%Error: data/full_repos/permissive/197102055/src/cons_buf.v:454: Cannot find file containing module: \'asyncfifo\'\nasyncfifo asyncfifo(\n^~~~~~~~~\n%Error: Exiting due to 2 error(s), 21 warning(s)\n'
module cons_buf (

	input					core_clk,
	input					core_rst,
	input   				sdram_clk,
	input   				sd_rst,


	input					cons_mode,


	input					sample_en,
	input					capture_done,
	input					capture_valid,
	input	[15:0]		capture_data,
	input					wr_done,


	input					rd_req,
	output				rd_valid,
	input		[31:0]	rd_addr,
	output				rd_rdy,
	output	[15:0]	rd_data,


	input		[23:0]	dso_sampleDivider,
	input		[23:0]	dso_triggerPos,
	input		[7:0]		dso_triggerSlope,
	input		[7:0]		dso_triggerSource,
	input		[16:0]	dso_triggerValue,
	


	output				read_start,
	output				sd_rd_req,
	input					sd_rd_valid,
	output	[31:0]	sd_rd_addr,
	input					sd_rd_rdy,
	input		[15:0]	sd_rd_data
);

`define DP_MAXBIT			12
`define FPS_MAX			24'd5000000




reg	buf_rd_valid;
reg	buf_rd_rdy;
wire	[15:0]	buf_rd_data;
wire	cfifo_full;
wire	cfifo_empty;
reg	cfifo_wr;
wire	cfifo_wr_nxt;
reg	capture_wr;
wire	capture_wr_nxt;
reg	[`DP_MAXBIT:0]	cfifo_wr_cnt;
wire	[`DP_MAXBIT:0]	cfifo_wr_cnt_nxt;
wire	[`DP_MAXBIT:0]	cfifo_cnt;
reg	[15:0]	cfifo_din;
wire	[15:0]	cfifo_din_nxt;
wire	cfifo_rd;
reg	cfifo_rd_1T;
wire	[15:0]	cfifo_dout;
reg	[23:0]	div_cnt;
wire	[23:0]	div_cnt_nxt;
reg	sample_en_1T;
reg	[23:0]	fps_cnt;
wire	[23:0]	fps_cnt_nxt;

assign sd_rd_req = cons_mode ? 1'b0 : rd_req;
assign sd_rd_addr = cons_mode ? 32'b0 : rd_addr;
assign rd_valid = cons_mode ? buf_rd_valid : sd_rd_valid;
assign rd_rdy = cons_mode ? buf_rd_rdy : sd_rd_rdy;
assign rd_data = cons_mode ? buf_rd_data : sd_rd_data;




reg				rising0;
wire				rising0_nxt;
reg				falling0;
wire				falling0_nxt;
reg				rising1;
wire				rising1_nxt;
reg				falling1;
wire				falling1_nxt;
reg	[16:0]	capture_data_1T;
reg	[16:0]	capture_data_2T;
reg	[16:0]	capture_data_4T;
reg	[16:0]	capture_data_8T;
reg	[16:0]	capture_data_16T;
reg	[3:0]		slope01_log;
wire	[3:0]		slope01_log_nxt;
reg	[3:0]		slope02_log;
wire	[3:0]		slope02_log_nxt;
reg	[3:0]		slope04_log;
wire	[3:0]		slope04_log_nxt;
reg	[3:0]		slope08_log;
wire	[3:0]		slope08_log_nxt;
reg	[3:0]		slope016_log;
wire	[3:0]		slope016_log_nxt;
reg	[3:0]		slope11_log;
wire	[3:0]		slope11_log_nxt;
reg	[3:0]		slope12_log;
wire	[3:0]		slope12_log_nxt;
reg	[3:0]		slope14_log;
wire	[3:0]		slope14_log_nxt;
reg	[3:0]		slope18_log;
wire	[3:0]		slope18_log_nxt;
reg	[3:0]		slope116_log;
wire	[3:0]		slope116_log_nxt;

assign slope01_log_nxt = (sample_en & ~sample_en_1T) ? 'b0101 :
							   (capture_wr & (capture_data[7:0] > capture_data_1T[7:0])) ? {slope01_log[2:0], 1'b1} :
                        (capture_wr & (capture_data[7:0] < capture_data_1T[7:0])) ? {slope01_log[2:0], 1'b0} : slope01_log;	
assign slope02_log_nxt = (sample_en & ~sample_en_1T) ? 'b0101 :
							   (capture_wr & (capture_data[7:0] > capture_data_2T[7:0])) ? {slope02_log[2:0], 1'b1} :
                        (capture_wr & (capture_data[7:0] < capture_data_2T[7:0])) ? {slope02_log[2:0], 1'b0} : slope02_log;	
assign slope04_log_nxt = (sample_en & ~sample_en_1T) ? 'b0101 :
							   (capture_wr & (capture_data[7:0] > capture_data_4T[7:0])) ? {slope04_log[2:0], 1'b1} :
                        (capture_wr & (capture_data[7:0] < capture_data_4T[7:0])) ? {slope04_log[2:0], 1'b0} : slope04_log;	
assign slope08_log_nxt = (sample_en & ~sample_en_1T) ? 'b0101 :
							   (capture_wr & (capture_data[7:0] > capture_data_8T[7:0])) ? {slope08_log[2:0], 1'b1} :
                        (capture_wr & (capture_data[7:0] < capture_data_8T[7:0])) ? {slope08_log[2:0], 1'b0} : slope08_log;	
assign slope016_log_nxt = (sample_en & ~sample_en_1T) ? 'b0101 :
							   (capture_wr & (capture_data[7:0] > capture_data_16T[7:0])) ? {slope016_log[2:0], 1'b1} :
                        (capture_wr & (capture_data[7:0] < capture_data_16T[7:0])) ? {slope016_log[2:0], 1'b0} : slope016_log;									
assign slope11_log_nxt = (sample_en & ~sample_en_1T) ? 'b0101 :
							   (capture_wr & (capture_data[15:8] > capture_data_1T[15:8])) ? {slope11_log[2:0], 1'b1} :
                        (capture_wr & (capture_data[15:8] < capture_data_1T[15:8])) ? {slope11_log[2:0], 1'b0} : slope11_log;	
assign slope12_log_nxt = (sample_en & ~sample_en_1T) ? 'b0101 :
							   (capture_wr & (capture_data[15:8] > capture_data_2T[15:8])) ? {slope12_log[2:0], 1'b1} :
                        (capture_wr & (capture_data[15:8] < capture_data_2T[15:8])) ? {slope12_log[2:0], 1'b0} : slope12_log;	
assign slope14_log_nxt = (sample_en & ~sample_en_1T) ? 'b0101 :
							   (capture_wr & (capture_data[15:8] > capture_data_4T[15:8])) ? {slope14_log[2:0], 1'b1} :
                        (capture_wr & (capture_data[15:8] < capture_data_4T[15:8])) ? {slope14_log[2:0], 1'b0} : slope14_log;	
assign slope18_log_nxt = (sample_en & ~sample_en_1T) ? 'b0101 :
							   (capture_wr & (capture_data[15:8] > capture_data_8T[15:8])) ? {slope18_log[2:0], 1'b1} :
                        (capture_wr & (capture_data[15:8] < capture_data_8T[15:8])) ? {slope18_log[2:0], 1'b0} : slope18_log;	
assign slope116_log_nxt = (sample_en & ~sample_en_1T) ? 'b0101 :
							   (capture_wr & (capture_data[15:8] > capture_data_16T[15:8])) ? {slope116_log[2:0], 1'b1} :
                        (capture_wr & (capture_data[15:8] < capture_data_16T[15:8])) ? {slope116_log[2:0], 1'b0} : slope116_log;									

always @(posedge core_clk or posedge core_rst)
begin
	if (core_rst) begin
		capture_data_1T <= `D 'b0;
		capture_data_2T <= `D 'b0;
		capture_data_4T <= `D 'b0;
		capture_data_8T <= `D 'b0;
		capture_data_16T <= `D 'b0;
		slope01_log <= `D 'b0101;
		slope02_log <= `D 'b0101;
		slope04_log <= `D 'b0101;
		slope08_log <= `D 'b0101;
		slope016_log <= `D 'b0101;
		slope11_log <= `D 'b0101;
		slope12_log <= `D 'b0101;
		slope14_log <= `D 'b0101;
		slope18_log <= `D 'b0101;
		slope116_log <= `D 'b0101;
	end else begin
		capture_data_1T <= `D capture_wr ? capture_data : capture_data_1T;
		capture_data_2T <= `D capture_wr & cfifo_wr_cnt[0] ? capture_data : capture_data_2T;
		capture_data_4T <= `D capture_wr & &cfifo_wr_cnt[1:0] ? capture_data : capture_data_4T;
		capture_data_8T <= `D capture_wr & &cfifo_wr_cnt[2:0] ? capture_data : capture_data_8T;
		capture_data_16T <= `D capture_wr & &cfifo_wr_cnt[3:0] ? capture_data : capture_data_16T;
		slope01_log <= `D slope01_log_nxt;
		slope02_log <= `D slope02_log_nxt;
		slope04_log <= `D slope04_log_nxt;
		slope08_log <= `D slope08_log_nxt;
		slope016_log <= `D slope016_log_nxt;
		slope11_log <= `D slope11_log_nxt;
		slope12_log <= `D slope12_log_nxt;
		slope14_log <= `D slope14_log_nxt;
		slope18_log <= `D slope18_log_nxt;
		slope116_log <= `D slope116_log_nxt;		
	end
end

assign rising0_nxt = &slope01_log | &slope02_log | &slope04_log |
                     &slope08_log | &slope016_log;
assign rising1_nxt = &slope11_log | &slope12_log | &slope14_log |
                     &slope18_log | &slope116_log;
assign falling0_nxt = ~|slope01_log | ~|slope02_log | ~|slope04_log |
                     ~|slope08_log | ~|slope016_log;
assign falling1_nxt = ~|slope11_log | ~|slope12_log | ~|slope14_log |
                     ~|slope18_log | ~|slope116_log;
always @(posedge core_clk or posedge core_rst)
begin
	if (core_rst) begin
		rising0 <= `D 1'b0;
		rising1 <= `D 1'b0;
		falling0 <= `D 1'b0;
		falling1 <= `D 1'b0;
	end else begin
		rising0 <= `D rising0_nxt;
		rising1 <= `D rising1_nxt;
		falling0 <= `D falling0_nxt;
		falling1 <= `D falling1_nxt;
	end
end




reg	[`DP_MAXBIT:0]	redunt_cnt;
wire	[`DP_MAXBIT:0]	redunt_cnt_nxt;
reg	redunt_rd;
wire	redunt_rd_nxt;
wire	cfifo_almost_full;
reg	buf_rdy;
wire	buf_rdy_nxt;
reg	buf_rdy_1T;
reg	trigger_hit;
wire	trigger_hit_nxt;
reg	trigger_hit_1T;
reg	rising0_hit;
reg	falling0_hit;
reg	rising1_hit;
reg	falling1_hit;	
wire	rising0_hit_nxt;
wire	falling0_hit_nxt;
wire	rising1_hit_nxt;
wire	falling1_hit_nxt;
reg	auto_req;
reg	rising0_req;
reg	falling0_req;
reg	rising1_req;
reg	falling1_req;
reg	rising0a1_req;
reg	falling0a1_req;
reg	rising0o1_req;
reg	falling0o1_req;
wire	auto_req_nxt;
wire	rising0_req_nxt;
wire	falling0_req_nxt;
wire	rising1_req_nxt;
wire	falling1_req_nxt;
wire	rising0a1_req_nxt;
wire	falling0a1_req_nxt;
wire	rising0o1_req_nxt;
wire	falling0o1_req_nxt;


assign rising0_hit_nxt = (capture_data[7:0] >= dso_triggerValue[7:0]) &
                     (capture_data_1T[7:0] <= dso_triggerValue[7:0]) &
							(rising0 | (~rising0 & ~falling0));
assign rising1_hit_nxt = (capture_data[15:8] >= dso_triggerValue[15:8]) &
                     (capture_data_1T[15:8] <= dso_triggerValue[15:8]) &
							(rising1 | (~rising1 & ~falling1));
assign falling0_hit_nxt = (capture_data[7:0] <= dso_triggerValue[7:0]) &
                      (capture_data_1T[7:0] >= dso_triggerValue[7:0]) &
							 (falling0 | (~rising0 & ~falling0));
assign falling1_hit_nxt = (capture_data[15:8] <= dso_triggerValue[15:8]) &
                      (capture_data_1T[15:8] >= dso_triggerValue[15:8]) &
							 (falling1 | (~rising1 & ~falling1));	
always @(posedge core_clk or posedge core_rst)
begin
	if (core_rst) begin
		rising0_hit <= `D 1'b0;
		rising1_hit <= `D 1'b0;
		falling0_hit <= `D 1'b0;
		falling1_hit <= `D 1'b0;
	end else begin
		rising0_hit <= `D rising0_hit_nxt;
		rising1_hit <= `D rising1_hit_nxt;
		falling0_hit <= `D falling0_hit_nxt;
		falling1_hit <= `D falling1_hit_nxt;
	end
end


assign	auto_req_nxt = (dso_triggerSource == 'd0);
assign	rising0_req_nxt = (dso_triggerSource == 'd1) & (dso_triggerSlope == 'b0);
assign	falling0_req_nxt = (dso_triggerSource == 'd1) & (dso_triggerSlope == 'b1);
assign	rising1_req_nxt = (dso_triggerSource == 'd2) & (dso_triggerSlope == 'b0);
assign	falling1_req_nxt = (dso_triggerSource == 'd2) & (dso_triggerSlope == 'b1);
assign	rising0a1_req_nxt = (dso_triggerSource == 'd3) & (dso_triggerSlope == 'b0);
assign	falling0a1_req_nxt = (dso_triggerSource == 'd3) & (dso_triggerSlope == 'b1);
assign	rising0o1_req_nxt = (dso_triggerSource == 'd4) & (dso_triggerSlope == 'b0);
assign	falling0o1_req_nxt = (dso_triggerSource == 'd4) & (dso_triggerSlope == 'b1);
always @(posedge core_clk or posedge core_rst)
begin
	if (core_rst) begin
		auto_req <= `D 1'b0;
		rising0_req <= `D 1'b0;
		falling0_req <= `D 1'b0;
		rising1_req <= `D 1'b0;
		falling1_req <= `D 1'b0;
		rising0a1_req <= `D 1'b0;
		falling0a1_req <= `D 1'b0;
		rising0o1_req <= `D 1'b0;
		falling0o1_req <= `D 1'b0;
	end else begin
		auto_req <= `D auto_req_nxt;
		rising0_req <= `D rising0_req_nxt;
		falling0_req <= `D falling0_req_nxt;
		rising1_req <= `D rising1_req_nxt;
		falling1_req <= `D falling1_req_nxt;
		rising0a1_req <= `D rising0a1_req_nxt;
		falling0a1_req <= `D falling0a1_req_nxt;
		rising0o1_req <= `D rising0o1_req_nxt;
		falling0o1_req <= `D falling0o1_req_nxt;
	end
end
							
assign trigger_hit_nxt = (cfifo_wr & auto_req) ? 1'b1 :
                         (cfifo_wr & rising0_req & rising0_hit & cfifo_cnt >= dso_triggerPos) ? 1'b1 : 
								 (cfifo_wr & falling0_req & falling0_hit & cfifo_cnt >= dso_triggerPos) ? 1'b1 :
								 (cfifo_wr & rising1_req & rising1_hit & cfifo_cnt >= dso_triggerPos) ? 1'b1 :
								 (cfifo_wr & falling1_req & falling1_hit & cfifo_cnt >= dso_triggerPos) ? 1'b1 :
								 (cfifo_wr & rising0a1_req & rising0_hit & rising1_hit & cfifo_cnt >= dso_triggerPos) ? 1'b1 :
								 (cfifo_wr & falling0a1_req & falling0_hit & falling1_hit & cfifo_cnt >= dso_triggerPos) ? 1'b1 :
								 (cfifo_wr & rising0o1_req & (rising0_hit | rising1_hit) & cfifo_cnt >= dso_triggerPos) ? 1'b1 :
								 (cfifo_wr & falling0o1_req & (falling0_hit | falling1_hit) & cfifo_cnt >= dso_triggerPos) ? 1'b1 :								 
								 (buf_rdy_1T & ~buf_rdy) ? 1'b0 : trigger_hit;
always @(posedge core_clk or posedge core_rst)
begin
	if (core_rst) begin
		trigger_hit <= `D 1'b0;
		trigger_hit_1T <= `D 1'b0;
	end else begin
		trigger_hit <= `D trigger_hit_nxt;
		trigger_hit_1T <= `D trigger_hit;
	end
end


assign redunt_rd_nxt = ((trigger_hit & ~trigger_hit_1T) | (redunt_rd & redunt_cnt == 'b10)) ? 1'b0 : 
							  ((cfifo_almost_full & ~trigger_hit) | (trigger_hit_1T & redunt_cnt > 'b1)) ? 1'b1 : redunt_rd;
assign redunt_cnt_nxt = (sample_en & ~sample_en_1T) ? dso_triggerPos :
                        (trigger_hit & ~trigger_hit_1T) ? cfifo_cnt - dso_triggerPos :  
                        (redunt_rd & trigger_hit_1T & redunt_cnt != 'b0) ? redunt_cnt - 1'b1 : redunt_cnt;							  
always @(posedge core_clk or posedge core_rst)
begin
	if (core_rst) begin
		redunt_rd <= `D 1'b0;
		redunt_cnt <= `D 'b0;
	end else begin
		redunt_rd <= `D redunt_rd_nxt;
		redunt_cnt <= `D redunt_cnt_nxt;
	end
end

assign buf_rdy_nxt = (trigger_hit & redunt_cnt == 'b1 & cfifo_full) ? 1'b1 : 
							cfifo_empty ? 1'b0 : buf_rdy;
assign cfifo_rd = buf_rdy & ~afifo_prog_full & ~cfifo_empty & ~|fps_cnt;
assign fps_cnt_nxt = ((sample_en & ~sample_en_1T) | (~buf_rdy & buf_rdy_1T)) ? `FPS_MAX :
                     |fps_cnt ? fps_cnt - 1'b1 : fps_cnt;
always @(posedge core_clk or posedge core_rst)
begin
	if (core_rst) begin
		buf_rdy <= `D 1'b0;
		buf_rdy_1T <= `D 1'b0;
		cfifo_rd_1T <= `D 1'b0;
		fps_cnt <= `D 'b0;
	end else begin
		buf_rdy <= `D buf_rdy_nxt;
		buf_rdy_1T <= `D buf_rdy;
		cfifo_rd_1T <= `D cfifo_rd;
		fps_cnt <= `D fps_cnt_nxt;
	end
end

reg	[`DP_MAXBIT:0]	cfifo_rd_cnt;
wire	[`DP_MAXBIT:0]	cfifo_rd_cnt_nxt;
assign cfifo_rd_cnt_nxt = cfifo_rd ? cfifo_rd_cnt + 1'b1 :
                          cfifo_full ? 'b0 : cfifo_rd_cnt;
always @(posedge core_clk or posedge core_rst)
begin
	if (core_rst)
		cfifo_rd_cnt <= `D 'b0;
	else
		cfifo_rd_cnt <= `D cfifo_rd_cnt_nxt;
end




always @(posedge core_clk)
begin
	sample_en_1T <= `D sample_en;
end	
assign div_cnt_nxt = (sample_en & ~sample_en_1T) ? 23'b0 :
							(capture_valid & (div_cnt == dso_sampleDivider)) ? 23'b0 :
							buf_rdy ? 23'b0 :
                     (capture_valid & ~buf_rdy) ? div_cnt + 1'b1 : div_cnt;
assign capture_wr_nxt = capture_valid & (div_cnt == dso_sampleDivider);							
assign cfifo_wr_nxt = capture_wr_nxt & ~buf_rdy;
assign cfifo_wr_cnt_nxt = (~buf_rdy & buf_rdy_1T) ? 'b0 : 
                          (cfifo_wr & ~&cfifo_wr_cnt) ? cfifo_wr_cnt + 1'b1 : cfifo_wr_cnt;
assign cfifo_din_nxt = capture_data_1T;
always @(posedge core_clk or posedge core_rst)
begin
	if (core_rst) begin
		div_cnt <= `D 24'b0;
		capture_wr <= `D 1'b0;
		cfifo_wr <= `D 1'b0;
		cfifo_wr_cnt <= `D 'b0;
		cfifo_din <= `D 16'b0;
	end else begin
		div_cnt <= `D div_cnt_nxt;
		capture_wr <= `D capture_wr_nxt;
		cfifo_wr <= `D cfifo_wr_nxt;
		cfifo_wr_cnt <= `D cfifo_wr_cnt_nxt;
		cfifo_din <= `D cfifo_din_nxt;
	end
end
cfifo cfifo (
  .clk(core_clk),
  .rst(core_rst),
  .din(cfifo_din),
  .wr_en(cfifo_wr),
  .rd_en(redunt_rd | cfifo_rd),
  .dout(cfifo_dout),
  .full(cfifo_full),
  .empty(cfifo_empty),
  .prog_full(cfifo_almost_full),
  .data_count(cfifo_cnt)
);




wire	afifo_empty;
wire	afifo_full;
wire	afifo_prog_empty;
wire	afifo_prog_full;
asyncfifo asyncfifo(
  .wr_clk(core_clk),
  .wr_rst(core_rst),
  .rd_clk(sdram_clk),
  .rd_rst(sd_rst),
  .din(cfifo_dout),
  .wr_en(cfifo_rd_1T),
  .rd_en(rd_req & ~afifo_empty),
  .dout(buf_rd_data),
  .full(afifo_full),
  .empty(afifo_empty),
  .prog_full(afifo_prog_full),
  .prog_empty(afifo_prog_empty)
);

always @(posedge sdram_clk or posedge sd_rst)
begin
	if (sd_rst) begin
		buf_rd_valid <= `D 1'b0;
		buf_rd_rdy <= `D 1'b0;
	end else begin
		buf_rd_valid <= `D rd_req & ~afifo_empty;
		buf_rd_rdy <= `D rd_req & ~afifo_empty;
	end
end

reg	read_rdy;
wire	read_rdy_nxt;
reg	read_rdy_1T;
assign read_rdy_nxt = afifo_prog_full ? 1'b1 : read_rdy;
always @(posedge sdram_clk or posedge sd_rst)
begin
	if (sd_rst) begin
		read_rdy <= `D 1'b0;
		read_rdy_1T <= `D 1'b0;
	end else begin
		read_rdy <= `D read_rdy_nxt;
		read_rdy_1T <= `D read_rdy;
	end
end
assign read_start = ~cons_mode ? wr_done : (read_rdy & ~read_rdy_1T);

endmodule
module cons_buf (

	input					core_clk,
	input					core_rst,
	input   				sdram_clk,
	input   				sd_rst,


	input					cons_mode,


	input					sample_en,
	input					capture_done,
	input					capture_valid,
	input	[15:0]		capture_data,
	input					wr_done,


	input					rd_req,
	output				rd_valid,
	input		[31:0]	rd_addr,
	output				rd_rdy,
	output	[15:0]	rd_data,


	input		[23:0]	dso_sampleDivider,
	input		[23:0]	dso_triggerPos,
	input		[7:0]		dso_triggerSlope,
	input		[7:0]		dso_triggerSource,
	input		[16:0]	dso_triggerValue,
	


	output				read_start,
	output				sd_rd_req,
	input					sd_rd_valid,
	output	[31:0]	sd_rd_addr,
	input					sd_rd_rdy,
	input		[15:0]	sd_rd_data
);


`define DP_MAXBIT			12
`define FPS_MAX			24'd5000000




reg	buf_rd_valid;
reg	buf_rd_rdy;
wire	[15:0]	buf_rd_data;
wire	cfifo_full;
wire	cfifo_empty;
reg	cfifo_wr;
wire	cfifo_wr_nxt;
reg	capture_wr;
wire	capture_wr_nxt;
reg	[`DP_MAXBIT:0]	cfifo_wr_cnt;
wire	[`DP_MAXBIT:0]	cfifo_wr_cnt_nxt;
wire	[`DP_MAXBIT:0]	cfifo_cnt;
reg	[15:0]	cfifo_din;
wire	[15:0]	cfifo_din_nxt;
wire	cfifo_rd;
reg	cfifo_rd_1T;
wire	[15:0]	cfifo_dout;
reg	[23:0]	div_cnt;
wire	[23:0]	div_cnt_nxt;
reg	sample_en_1T;
reg	[23:0]	fps_cnt;
wire	[23:0]	fps_cnt_nxt;

assign sd_rd_req = cons_mode ? 1'b0 : rd_req;
assign sd_rd_addr = cons_mode ? 32'b0 : rd_addr;
assign rd_valid = cons_mode ? buf_rd_valid : sd_rd_valid;
assign rd_rdy = cons_mode ? buf_rd_rdy : sd_rd_rdy;
assign rd_data = cons_mode ? buf_rd_data : sd_rd_data;




reg				rising0;
wire				rising0_nxt;
reg				falling0;
wire				falling0_nxt;
reg				rising1;
wire				rising1_nxt;
reg				falling1;
wire				falling1_nxt;
reg	[16:0]	capture_data_1T;
reg	[16:0]	capture_data_2T;
reg	[16:0]	capture_data_4T;
reg	[16:0]	capture_data_8T;
reg	[16:0]	capture_data_16T;
reg	[3:0]		slope01_log;
wire	[3:0]		slope01_log_nxt;
reg	[3:0]		slope02_log;
wire	[3:0]		slope02_log_nxt;
reg	[3:0]		slope04_log;
wire	[3:0]		slope04_log_nxt;
reg	[3:0]		slope08_log;
wire	[3:0]		slope08_log_nxt;
reg	[3:0]		slope016_log;
wire	[3:0]		slope016_log_nxt;
reg	[3:0]		slope11_log;
wire	[3:0]		slope11_log_nxt;
reg	[3:0]		slope12_log;
wire	[3:0]		slope12_log_nxt;
reg	[3:0]		slope14_log;
wire	[3:0]		slope14_log_nxt;
reg	[3:0]		slope18_log;
wire	[3:0]		slope18_log_nxt;
reg	[3:0]		slope116_log;
wire	[3:0]		slope116_log_nxt;

assign slope01_log_nxt = (sample_en & ~sample_en_1T) ? 'b0101 :
							   (capture_wr & (capture_data[7:0] > capture_data_1T[7:0])) ? {slope01_log[2:0], 1'b1} :
                        (capture_wr & (capture_data[7:0] < capture_data_1T[7:0])) ? {slope01_log[2:0], 1'b0} : slope01_log;	
assign slope02_log_nxt = (sample_en & ~sample_en_1T) ? 'b0101 :
							   (capture_wr & (capture_data[7:0] > capture_data_2T[7:0])) ? {slope02_log[2:0], 1'b1} :
                        (capture_wr & (capture_data[7:0] < capture_data_2T[7:0])) ? {slope02_log[2:0], 1'b0} : slope02_log;	
assign slope04_log_nxt = (sample_en & ~sample_en_1T) ? 'b0101 :
							   (capture_wr & (capture_data[7:0] > capture_data_4T[7:0])) ? {slope04_log[2:0], 1'b1} :
                        (capture_wr & (capture_data[7:0] < capture_data_4T[7:0])) ? {slope04_log[2:0], 1'b0} : slope04_log;	
assign slope08_log_nxt = (sample_en & ~sample_en_1T) ? 'b0101 :
							   (capture_wr & (capture_data[7:0] > capture_data_8T[7:0])) ? {slope08_log[2:0], 1'b1} :
                        (capture_wr & (capture_data[7:0] < capture_data_8T[7:0])) ? {slope08_log[2:0], 1'b0} : slope08_log;	
assign slope016_log_nxt = (sample_en & ~sample_en_1T) ? 'b0101 :
							   (capture_wr & (capture_data[7:0] > capture_data_16T[7:0])) ? {slope016_log[2:0], 1'b1} :
                        (capture_wr & (capture_data[7:0] < capture_data_16T[7:0])) ? {slope016_log[2:0], 1'b0} : slope016_log;									
assign slope11_log_nxt = (sample_en & ~sample_en_1T) ? 'b0101 :
							   (capture_wr & (capture_data[15:8] > capture_data_1T[15:8])) ? {slope11_log[2:0], 1'b1} :
                        (capture_wr & (capture_data[15:8] < capture_data_1T[15:8])) ? {slope11_log[2:0], 1'b0} : slope11_log;	
assign slope12_log_nxt = (sample_en & ~sample_en_1T) ? 'b0101 :
							   (capture_wr & (capture_data[15:8] > capture_data_2T[15:8])) ? {slope12_log[2:0], 1'b1} :
                        (capture_wr & (capture_data[15:8] < capture_data_2T[15:8])) ? {slope12_log[2:0], 1'b0} : slope12_log;	
assign slope14_log_nxt = (sample_en & ~sample_en_1T) ? 'b0101 :
							   (capture_wr & (capture_data[15:8] > capture_data_4T[15:8])) ? {slope14_log[2:0], 1'b1} :
                        (capture_wr & (capture_data[15:8] < capture_data_4T[15:8])) ? {slope14_log[2:0], 1'b0} : slope14_log;	
assign slope18_log_nxt = (sample_en & ~sample_en_1T) ? 'b0101 :
							   (capture_wr & (capture_data[15:8] > capture_data_8T[15:8])) ? {slope18_log[2:0], 1'b1} :
                        (capture_wr & (capture_data[15:8] < capture_data_8T[15:8])) ? {slope18_log[2:0], 1'b0} : slope18_log;	
assign slope116_log_nxt = (sample_en & ~sample_en_1T) ? 'b0101 :
							   (capture_wr & (capture_data[15:8] > capture_data_16T[15:8])) ? {slope116_log[2:0], 1'b1} :
                        (capture_wr & (capture_data[15:8] < capture_data_16T[15:8])) ? {slope116_log[2:0], 1'b0} : slope116_log;									

always @(posedge core_clk or posedge core_rst)
begin
	if (core_rst) begin
		capture_data_1T <= `D 'b0;
		capture_data_2T <= `D 'b0;
		capture_data_4T <= `D 'b0;
		capture_data_8T <= `D 'b0;
		capture_data_16T <= `D 'b0;
		slope01_log <= `D 'b0101;
		slope02_log <= `D 'b0101;
		slope04_log <= `D 'b0101;
		slope08_log <= `D 'b0101;
		slope016_log <= `D 'b0101;
		slope11_log <= `D 'b0101;
		slope12_log <= `D 'b0101;
		slope14_log <= `D 'b0101;
		slope18_log <= `D 'b0101;
		slope116_log <= `D 'b0101;
	end else begin
		capture_data_1T <= `D capture_wr ? capture_data : capture_data_1T;
		capture_data_2T <= `D capture_wr & cfifo_wr_cnt[0] ? capture_data : capture_data_2T;
		capture_data_4T <= `D capture_wr & &cfifo_wr_cnt[1:0] ? capture_data : capture_data_4T;
		capture_data_8T <= `D capture_wr & &cfifo_wr_cnt[2:0] ? capture_data : capture_data_8T;
		capture_data_16T <= `D capture_wr & &cfifo_wr_cnt[3:0] ? capture_data : capture_data_16T;
		slope01_log <= `D slope01_log_nxt;
		slope02_log <= `D slope02_log_nxt;
		slope04_log <= `D slope04_log_nxt;
		slope08_log <= `D slope08_log_nxt;
		slope016_log <= `D slope016_log_nxt;
		slope11_log <= `D slope11_log_nxt;
		slope12_log <= `D slope12_log_nxt;
		slope14_log <= `D slope14_log_nxt;
		slope18_log <= `D slope18_log_nxt;
		slope116_log <= `D slope116_log_nxt;		
	end
end

assign rising0_nxt = &slope01_log | &slope02_log | &slope04_log |
                     &slope08_log | &slope016_log;
assign rising1_nxt = &slope11_log | &slope12_log | &slope14_log |
                     &slope18_log | &slope116_log;
assign falling0_nxt = ~|slope01_log | ~|slope02_log | ~|slope04_log |
                     ~|slope08_log | ~|slope016_log;
assign falling1_nxt = ~|slope11_log | ~|slope12_log | ~|slope14_log |
                     ~|slope18_log | ~|slope116_log;
always @(posedge core_clk or posedge core_rst)
begin
	if (core_rst) begin
		rising0 <= `D 1'b0;
		rising1 <= `D 1'b0;
		falling0 <= `D 1'b0;
		falling1 <= `D 1'b0;
	end else begin
		rising0 <= `D rising0_nxt;
		rising1 <= `D rising1_nxt;
		falling0 <= `D falling0_nxt;
		falling1 <= `D falling1_nxt;
	end
end




reg	[`DP_MAXBIT:0]	redunt_cnt;
wire	[`DP_MAXBIT:0]	redunt_cnt_nxt;
reg	redunt_rd;
wire	redunt_rd_nxt;
wire	cfifo_almost_full;
reg	buf_rdy;
wire	buf_rdy_nxt;
reg	buf_rdy_1T;
reg	trigger_hit;
wire	trigger_hit_nxt;
reg	trigger_hit_1T;
reg	rising0_hit;
reg	falling0_hit;
reg	rising1_hit;
reg	falling1_hit;	
wire	rising0_hit_nxt;
wire	falling0_hit_nxt;
wire	rising1_hit_nxt;
wire	falling1_hit_nxt;
reg	auto_req;
reg	rising0_req;
reg	falling0_req;
reg	rising1_req;
reg	falling1_req;
reg	rising0a1_req;
reg	falling0a1_req;
reg	rising0o1_req;
reg	falling0o1_req;
wire	auto_req_nxt;
wire	rising0_req_nxt;
wire	falling0_req_nxt;
wire	rising1_req_nxt;
wire	falling1_req_nxt;
wire	rising0a1_req_nxt;
wire	falling0a1_req_nxt;
wire	rising0o1_req_nxt;
wire	falling0o1_req_nxt;


assign rising0_hit_nxt = (capture_data[7:0] >= dso_triggerValue[7:0]) &
                     (capture_data_1T[7:0] <= dso_triggerValue[7:0]) &
							(rising0 | (~rising0 & ~falling0));
assign rising1_hit_nxt = (capture_data[15:8] >= dso_triggerValue[15:8]) &
                     (capture_data_1T[15:8] <= dso_triggerValue[15:8]) &
							(rising1 | (~rising1 & ~falling1));
assign falling0_hit_nxt = (capture_data[7:0] <= dso_triggerValue[7:0]) &
                      (capture_data_1T[7:0] >= dso_triggerValue[7:0]) &
							 (falling0 | (~rising0 & ~falling0));
assign falling1_hit_nxt = (capture_data[15:8] <= dso_triggerValue[15:8]) &
                      (capture_data_1T[15:8] >= dso_triggerValue[15:8]) &
							 (falling1 | (~rising1 & ~falling1));	
always @(posedge core_clk or posedge core_rst)
begin
	if (core_rst) begin
		rising0_hit <= `D 1'b0;
		rising1_hit <= `D 1'b0;
		falling0_hit <= `D 1'b0;
		falling1_hit <= `D 1'b0;
	end else begin
		rising0_hit <= `D rising0_hit_nxt;
		rising1_hit <= `D rising1_hit_nxt;
		falling0_hit <= `D falling0_hit_nxt;
		falling1_hit <= `D falling1_hit_nxt;
	end
end


assign	auto_req_nxt = (dso_triggerSource == 'd0);
assign	rising0_req_nxt = (dso_triggerSource == 'd1) & (dso_triggerSlope == 'b0);
assign	falling0_req_nxt = (dso_triggerSource == 'd1) & (dso_triggerSlope == 'b1);
assign	rising1_req_nxt = (dso_triggerSource == 'd2) & (dso_triggerSlope == 'b0);
assign	falling1_req_nxt = (dso_triggerSource == 'd2) & (dso_triggerSlope == 'b1);
assign	rising0a1_req_nxt = (dso_triggerSource == 'd3) & (dso_triggerSlope == 'b0);
assign	falling0a1_req_nxt = (dso_triggerSource == 'd3) & (dso_triggerSlope == 'b1);
assign	rising0o1_req_nxt = (dso_triggerSource == 'd4) & (dso_triggerSlope == 'b0);
assign	falling0o1_req_nxt = (dso_triggerSource == 'd4) & (dso_triggerSlope == 'b1);
always @(posedge core_clk or posedge core_rst)
begin
	if (core_rst) begin
		auto_req <= `D 1'b0;
		rising0_req <= `D 1'b0;
		falling0_req <= `D 1'b0;
		rising1_req <= `D 1'b0;
		falling1_req <= `D 1'b0;
		rising0a1_req <= `D 1'b0;
		falling0a1_req <= `D 1'b0;
		rising0o1_req <= `D 1'b0;
		falling0o1_req <= `D 1'b0;
	end else begin
		auto_req <= `D auto_req_nxt;
		rising0_req <= `D rising0_req_nxt;
		falling0_req <= `D falling0_req_nxt;
		rising1_req <= `D rising1_req_nxt;
		falling1_req <= `D falling1_req_nxt;
		rising0a1_req <= `D rising0a1_req_nxt;
		falling0a1_req <= `D falling0a1_req_nxt;
		rising0o1_req <= `D rising0o1_req_nxt;
		falling0o1_req <= `D falling0o1_req_nxt;
	end
end
							
assign trigger_hit_nxt = (cfifo_wr & auto_req) ? 1'b1 :
                         (cfifo_wr & rising0_req & rising0_hit & cfifo_cnt >= dso_triggerPos) ? 1'b1 : 
								 (cfifo_wr & falling0_req & falling0_hit & cfifo_cnt >= dso_triggerPos) ? 1'b1 :
								 (cfifo_wr & rising1_req & rising1_hit & cfifo_cnt >= dso_triggerPos) ? 1'b1 :
								 (cfifo_wr & falling1_req & falling1_hit & cfifo_cnt >= dso_triggerPos) ? 1'b1 :
								 (cfifo_wr & rising0a1_req & rising0_hit & rising1_hit & cfifo_cnt >= dso_triggerPos) ? 1'b1 :
								 (cfifo_wr & falling0a1_req & falling0_hit & falling1_hit & cfifo_cnt >= dso_triggerPos) ? 1'b1 :
								 (cfifo_wr & rising0o1_req & (rising0_hit | rising1_hit) & cfifo_cnt >= dso_triggerPos) ? 1'b1 :
								 (cfifo_wr & falling0o1_req & (falling0_hit | falling1_hit) & cfifo_cnt >= dso_triggerPos) ? 1'b1 :								 
								 (buf_rdy_1T & ~buf_rdy) ? 1'b0 : trigger_hit;
always @(posedge core_clk or posedge core_rst)
begin
	if (core_rst) begin
		trigger_hit <= `D 1'b0;
		trigger_hit_1T <= `D 1'b0;
	end else begin
		trigger_hit <= `D trigger_hit_nxt;
		trigger_hit_1T <= `D trigger_hit;
	end
end


assign redunt_rd_nxt = ((trigger_hit & ~trigger_hit_1T) | (redunt_rd & redunt_cnt == 'b10)) ? 1'b0 : 
							  ((cfifo_almost_full & ~trigger_hit) | (trigger_hit_1T & redunt_cnt > 'b1)) ? 1'b1 : redunt_rd;
assign redunt_cnt_nxt = (sample_en & ~sample_en_1T) ? dso_triggerPos :
                        (trigger_hit & ~trigger_hit_1T) ? cfifo_cnt - dso_triggerPos :  
                        (redunt_rd & trigger_hit_1T & redunt_cnt != 'b0) ? redunt_cnt - 1'b1 : redunt_cnt;							  
always @(posedge core_clk or posedge core_rst)
begin
	if (core_rst) begin
		redunt_rd <= `D 1'b0;
		redunt_cnt <= `D 'b0;
	end else begin
		redunt_rd <= `D redunt_rd_nxt;
		redunt_cnt <= `D redunt_cnt_nxt;
	end
end

assign buf_rdy_nxt = (trigger_hit & redunt_cnt == 'b1 & cfifo_full) ? 1'b1 : 
							cfifo_empty ? 1'b0 : buf_rdy;
assign cfifo_rd = buf_rdy & ~afifo_prog_full & ~cfifo_empty & ~|fps_cnt;
assign fps_cnt_nxt = ((sample_en & ~sample_en_1T) | (~buf_rdy & buf_rdy_1T)) ? `FPS_MAX :
                     |fps_cnt ? fps_cnt - 1'b1 : fps_cnt;
always @(posedge core_clk or posedge core_rst)
begin
	if (core_rst) begin
		buf_rdy <= `D 1'b0;
		buf_rdy_1T <= `D 1'b0;
		cfifo_rd_1T <= `D 1'b0;
		fps_cnt <= `D 'b0;
	end else begin
		buf_rdy <= `D buf_rdy_nxt;
		buf_rdy_1T <= `D buf_rdy;
		cfifo_rd_1T <= `D cfifo_rd;
		fps_cnt <= `D fps_cnt_nxt;
	end
end

reg	[`DP_MAXBIT:0]	cfifo_rd_cnt;
wire	[`DP_MAXBIT:0]	cfifo_rd_cnt_nxt;
assign cfifo_rd_cnt_nxt = cfifo_rd ? cfifo_rd_cnt + 1'b1 :
                          cfifo_full ? 'b0 : cfifo_rd_cnt;
always @(posedge core_clk or posedge core_rst)
begin
	if (core_rst)
		cfifo_rd_cnt <= `D 'b0;
	else
		cfifo_rd_cnt <= `D cfifo_rd_cnt_nxt;
end




always @(posedge core_clk)
begin
	sample_en_1T <= `D sample_en;
end	
assign div_cnt_nxt = (sample_en & ~sample_en_1T) ? 23'b0 :
							(capture_valid & (div_cnt == dso_sampleDivider)) ? 23'b0 :
							buf_rdy ? 23'b0 :
                     (capture_valid & ~buf_rdy) ? div_cnt + 1'b1 : div_cnt;
assign capture_wr_nxt = capture_valid & (div_cnt == dso_sampleDivider);							
assign cfifo_wr_nxt = capture_wr_nxt & ~buf_rdy;
assign cfifo_wr_cnt_nxt = (~buf_rdy & buf_rdy_1T) ? 'b0 : 
                          (cfifo_wr & ~&cfifo_wr_cnt) ? cfifo_wr_cnt + 1'b1 : cfifo_wr_cnt;
assign cfifo_din_nxt = capture_data_1T;
always @(posedge core_clk or posedge core_rst)
begin
	if (core_rst) begin
		div_cnt <= `D 24'b0;
		capture_wr <= `D 1'b0;
		cfifo_wr <= `D 1'b0;
		cfifo_wr_cnt <= `D 'b0;
		cfifo_din <= `D 16'b0;
	end else begin
		div_cnt <= `D div_cnt_nxt;
		capture_wr <= `D capture_wr_nxt;
		cfifo_wr <= `D cfifo_wr_nxt;
		cfifo_wr_cnt <= `D cfifo_wr_cnt_nxt;
		cfifo_din <= `D cfifo_din_nxt;
	end
end
cfifo cfifo (
  .clk(core_clk),
  .rst(core_rst),
  .din(cfifo_din),
  .wr_en(cfifo_wr),
  .rd_en(redunt_rd | cfifo_rd),
  .dout(cfifo_dout),
  .full(cfifo_full),
  .empty(cfifo_empty),
  .prog_full(cfifo_almost_full),
  .data_count(cfifo_cnt)
);




wire	afifo_empty;
wire	afifo_full;
wire	afifo_prog_empty;
wire	afifo_prog_full;
asyncfifo asyncfifo(
  .wr_clk(core_clk),
  .wr_rst(core_rst),
  .rd_clk(sdram_clk),
  .rd_rst(sd_rst),
  .din(cfifo_dout),
  .wr_en(cfifo_rd_1T),
  .rd_en(rd_req & ~afifo_empty),
  .dout(buf_rd_data),
  .full(afifo_full),
  .empty(afifo_empty),
  .prog_full(afifo_prog_full),
  .prog_empty(afifo_prog_empty)
);

always @(posedge sdram_clk or posedge sd_rst)
begin
	if (sd_rst) begin
		buf_rd_valid <= `D 1'b0;
		buf_rd_rdy <= `D 1'b0;
	end else begin
		buf_rd_valid <= `D rd_req & ~afifo_empty;
		buf_rd_rdy <= `D rd_req & ~afifo_empty;
	end
end

reg	read_rdy;
wire	read_rdy_nxt;
reg	read_rdy_1T;
assign read_rdy_nxt = afifo_prog_full ? 1'b1 : read_rdy;
always @(posedge sdram_clk or posedge sd_rst)
begin
	if (sd_rst) begin
		read_rdy <= `D 1'b0;
		read_rdy_1T <= `D 1'b0;
	end else begin
		read_rdy <= `D read_rdy_nxt;
		read_rdy_1T <= `D read_rdy;
	end
end
assign read_start = ~cons_mode ? wr_done : (read_rdy & ~read_rdy_1T);

endmodule