data/full_repos/permissive/120518070/main/source/tb_edm_mcu.sv
 line:13: before: "ns"
1: b'%Error: data/full_repos/permissive/120518070/main/source/tb_edm_mcu.sv:13: syntax error, unexpected TIME NUMBER, expecting TYPE-IDENTIFIER\n localparam CLK_PERIOD = 35ns;\n                         ^~~~\n%Warning-STMTDLY: data/full_repos/permissive/120518070/main/source/tb_edm_mcu.sv:18: Unsupported: Ignoring delay on this delayed statement.\n  #(CLK_PERIOD/2.0);\n  ^\n                  ... Use "/* verilator lint_off STMTDLY */" and lint_on around source to disable this message.\n%Warning-STMTDLY: data/full_repos/permissive/120518070/main/source/tb_edm_mcu.sv:20: Unsupported: Ignoring delay on this delayed statement.\n  #(CLK_PERIOD/2.0);\n  ^\n%Error: data/full_repos/permissive/120518070/main/source/tb_edm_mcu.sv:49: syntax error, unexpected \')\', expecting \',\' or \';\'\n reg [1:0] tb_RC4_mode);\n                      ^\n%Error: data/full_repos/permissive/120518070/main/source/tb_edm_mcu.sv:142: syntax error, unexpected \'.\', expecting \')\'\n .HREADY(tb_HREADY),\n ^\n%Error: data/full_repos/permissive/120518070/main/source/tb_edm_mcu.sv:182: syntax error, unexpected TIME NUMBER, expecting TYPE-IDENTIFIER\n localparam CHECK_DELAY  = 1ns;\n                           ^~~\n%Error: data/full_repos/permissive/120518070/main/source/tb_edm_mcu.sv:233: syntax error, unexpected \'@\'\n  @(negedge tb_clk);\n  ^\n%Error: data/full_repos/permissive/120518070/main/source/tb_edm_mcu.sv:334: Unsupported: Dynamic array new\n  tb_input_image = new[num_rows];\n                   ^~~\n%Error: data/full_repos/permissive/120518070/main/source/tb_edm_mcu.sv:337: Unsupported: Dynamic array new\n   tb_input_image[r] = new[num_cols];\n                       ^~~\n%Warning-STMTDLY: data/full_repos/permissive/120518070/main/source/tb_edm_mcu.sv:419: Unsupported: Ignoring delay on this delayed statement.\n  #(1ns);\n  ^\n%Warning-STMTDLY: data/full_repos/permissive/120518070/main/source/tb_edm_mcu.sv:438: Unsupported: Ignoring delay on this delayed statement.\n  #(CLK_PERIOD);\n  ^\n%Warning-STMTDLY: data/full_repos/permissive/120518070/main/source/tb_edm_mcu.sv:442: Unsupported: Ignoring delay on this delayed statement.\n  #(CLK_PERIOD);\n  ^\n%Warning-STMTDLY: data/full_repos/permissive/120518070/main/source/tb_edm_mcu.sv:447: Unsupported: Ignoring delay on this delayed statement.\n  #(CLK_PERIOD);\n  ^\n%Warning-STMTDLY: data/full_repos/permissive/120518070/main/source/tb_edm_mcu.sv:450: Unsupported: Ignoring delay on this delayed statement.\n  #(CLK_PERIOD * 5); \n  ^\n%Warning-STMTDLY: data/full_repos/permissive/120518070/main/source/tb_edm_mcu.sv:452: Unsupported: Ignoring delay on this delayed statement.\n  #(CLK_PERIOD);\n  ^\n%Error: data/full_repos/permissive/120518070/main/source/tb_edm_mcu.sv:459: Unsupported: Dynamic array new\n  tb_output_image = new[num_rows - 2];\n                    ^~~\n%Error: data/full_repos/permissive/120518070/main/source/tb_edm_mcu.sv:462: Unsupported: Dynamic array new\n   tb_output_image[r] = new[num_cols - 2];\n                        ^~~\n%Warning-STMTDLY: data/full_repos/permissive/120518070/main/source/tb_edm_mcu.sv:478: Unsupported: Ignoring delay on this delayed statement.\n    #(CLK_PERIOD);\n    ^\n%Error: data/full_repos/permissive/120518070/main/source/tb_edm_mcu.sv:479: syntax error, unexpected \'@\'\n    @(posedge tb_clk);\n    ^\n%Warning-STMTDLY: data/full_repos/permissive/120518070/main/source/tb_edm_mcu.sv:482: Unsupported: Ignoring delay on this delayed statement.\n    #(CLK_PERIOD);\n    ^\n%Warning-STMTDLY: data/full_repos/permissive/120518070/main/source/tb_edm_mcu.sv:484: Unsupported: Ignoring delay on this delayed statement.\n    #(CLK_PERIOD);\n    ^\n%Warning-STMTDLY: data/full_repos/permissive/120518070/main/source/tb_edm_mcu.sv:489: Unsupported: Ignoring delay on this delayed statement.\n   #(CLK_PERIOD);\n   ^\n%Warning-STMTDLY: data/full_repos/permissive/120518070/main/source/tb_edm_mcu.sv:494: Unsupported: Ignoring delay on this delayed statement.\n   #(CLK_PERIOD);\n   ^\n%Warning-STMTDLY: data/full_repos/permissive/120518070/main/source/tb_edm_mcu.sv:499: Unsupported: Ignoring delay on this delayed statement.\n  #(CLK_PERIOD);\n  ^\n%Warning-STMTDLY: data/full_repos/permissive/120518070/main/source/tb_edm_mcu.sv:501: Unsupported: Ignoring delay on this delayed statement.\n  #(CLK_PERIOD);\n  ^\n%Warning-STMTDLY: data/full_repos/permissive/120518070/main/source/tb_edm_mcu.sv:503: Unsupported: Ignoring delay on this delayed statement.\n  #(CLK_PERIOD);\n  ^\n%Error: Exiting due to 10 error(s), 16 warning(s)\n'
module tb_edm();

	localparam CLK_PERIOD	= 35ns;
	reg tb_clk;
	always
	begin
		tb_clk = 1'b0;
		#(CLK_PERIOD/2.0);
		tb_clk = 1'b1;
		#(CLK_PERIOD/2.0);
	end

	reg tb_n_rst;
	reg tb_ED_start;
	reg tb_ED_dfb;
	reg tb_buff_filled;
	reg [11:0] tb_image_width;
	reg [11:0] tb_image_height;
	reg [95:0] tb_ED_rdata;
	reg tb_fill_buff;
	reg tb_ED_done;
	reg [1:0] tb_ED_mode;
	reg [19:0] tb_ED_rpixnum;
	reg [19:0] tb_ED_wpixnum;
	reg [15:0] tb_ED_wdata;

	reg [1:0] tb_SI_mode;
	reg tb_SI_dfb;
	reg [31:0] tb_SI_rdata;
	reg [19:0] tb_SI_rpixNum;	

	reg tb_start;
	reg tb_process_complete;
	reg tb_RC4_start;
	reg tb_RC4_done;

	reg [31:0]tb_RC4_wdata;
	reg [19:0] tb_RC4_pixNum;
	reg [1:0] tb_RC4_mode);
	reg [31:0] tb_rdata;
	reg tb_data_feedback;
	reg [31:0] tb_RC4_data;
	reg RC4_dfb;
	reg tb_startAddr_sel;
	reg [31:0] wdata;
	reg [1:0] tb_size;
	reg [1:0] tb_mode;
	reg [19:0] pixNum;


	reg tb_HREADY;
	reg [31:0] tb_HRDATA;
	reg [31:0] tb_HADDR;
	reg [1:0] tb_HSIZE;
	reg [31:0] tb_HWDATA;
	reg tb_HWRITE;
	

	edm DUT_EDM(
	.clk(tb_clk),
	.n_rst(tb_n_rst),
	.ED_start(tb_ED_start),
	.ED_dfb(tb_ED_dfb),
	.buff_filled(tb_buff_filled),
	.image_width(tb_image_width),
	.image_height(tb_image_height),
	.ED_rdata(tb_ED_rdata),
	.fill_buff(tb_fill_buff),
	.ED_done(tb_ED_done),
	.ED_mode(tb_ED_mode),
	.ED_rpixnum(tb_ED_rpixnum),
	.ED_wpixnum(tb_ED_wpixnum),
	.ED_wdata(tb_ED_wdata)
	);

	sample_image_data_storage DUT_BUFF(
	.clk(tb_clk),
	.n_rst(tb_n_rst),
	.image_width(tb_image_width),
	.SI_dfb(tb_SI_dfb),
	.SI_rdata(tb_SI_rdata),
	.ED_rpixNum(tb_ED_rpixnum),
	.fill_buff(tb_fill_buff),
	.buff_filled(tb_buff_filled),
	.ED_rdata(tb_ED_rdata),
	.SI_rpixNum(tb_SI_rpixNum),
	.SI_mode(tb_SI_mode)
	);

	mcu DUT_mcu(
	.clk(tb_clk),
	.n_rst(tb_n_rst),	
	.start(tb_start),
	.RC4_done(tb_RC4_done),
	.ED_done(tb_ED_done),
	.RC4_start(tb_RC4_start),
	.ED_start(tb_ED_start),
	.process_complete(tb_process_complete)
	);


	AHB_helper DUT_ahbH(
	.RC4_start(tb_RC4_start),
	.ED_start(tb_ED_start),
	.RC4_wdata(tb_RC4_wdata),
	.RC4_pixNum(tb_RC4_pixNum),
	.RC4_mode(tb_RC4_mode),
	.ED_wdata(tb_ED_wdata),
	.ED_wpixNim(tb_ED_wpixnum),
	.ED_mode(tb_ED_mode),
	.SI_rpixNum(tb_SI_rpixnum),
	.rdata(tb_rdata),
	.data_feedback(tb_data_feedback),
	.SI_mode(tb_SI_mode),
	.RC4_data(tb_RC4_data),
	.RC4_dfb(tb_RC4_dfb),
	.ED_dfb(tb_ED_dfb),
	.SI_rdata(tb_SI_rdata),
	.SI_dfb(tb_SI_dfb),
	.startAddr_sel(tb_startAddr_sel),
	.wdata(tb_wdata),
	.size(tb_size),
	.mode(tb_mode),
	.pixNum(tb_pixNum)
	);

	

	AHB_master DUT_ahbM(
	.clk(tb_clk),
	.n_rst(tb_n_rst)
	.HREADY(tb_HREADY),
	.HRDATA(tb_HRDATA),
	.mode(tb_mode),
	.wdata(tb_wdata),
	.pixNum(tb_pixNum),
	.image_startAddr(tb_image_startAddr),
	.size(tb_size),
	.image_width(tb_image_width),
	.image_height(tb_image_height),
	.startAddr_sel(tb_startAddr_sel),
	.HADDR(tb_HADDR),
	.HWDATA(tb_HWDATA),
	.HWRITE(tb_HWRITE),
	.HSIZE(tb_HSIZE),
	.data_feedback(tb_data_feedback),
	.rdata(tb_rdata)
	);



	parameter	INPUT_FILENAME		= "./images/fruits.bmp";
	parameter	RESULT1_FILENAME	= "./docs/edge_detected_fruit.bmp";
	parameter	RESULT2_FILENAME	= "./docs/edge_detected_fruit2.txt";


	localparam SEEK_START		= 0;
	localparam SEEK_CUR		= 1;
	localparam SEEK_END		= 2;
	


	localparam BMP_HEADER_SIZE_BYTES	= 14;
	localparam PIXEL_ARR_PTR_ADDR			= BMP_HEADER_SIZE_BYTES - 4;
	localparam DIB_HEADER_C1_SIZE			= 40;
	localparam DIB_HEADER_C2_SIZE			= 12;
	localparam NO_COMPRESSION 		= 0;


	localparam NUM_VAL_BITS		= 16;
	localparam MAX_VAL_BIT		= NUM_VAL_BITS - 1;
	localparam CHECK_DELAY		= 1ns;



	integer r;
	integer c;
	reg [7:0] tmp_byte;
	integer in_file;
	integer res_file;
	string  curr_res_filename;
	integer num_rows;
	integer num_cols;
	integer num_pad_bytes;
	reg [7:0] in_pixel_val;
	reg [7:0] res_pixel_val;
	integer i;
	integer quiet_catch;
	integer count;

	reg [(BMP_HEADER_SIZE_BYTES - 1):0][7:0] in_bmp_file_header;
	reg [(BMP_HEADER_SIZE_BYTES - 1):0][7:0] res_bmp_file_header;
	reg [31:0] in_image_data_ptr;
	reg [31:0] res_image_data_ptr;
	

	reg [(DIB_HEADER_C1_SIZE - 1):0][7:0] dib_header;
	reg [31:0] dib_header_size;
	reg [31:0] image_width;
	reg [31:0] image_height;
	reg [15:0] num_pixel_bits;
	reg [31:0] compression_mode;
	

	reg [7:0] tb_input_image [][]; 
	reg [7:0] tb_output_image [][];




	integer row,col;
	reg [3:0][7:0] buffer;


	integer row_size_bytes;

	integer k;
	

	task send_buffer;
	begin

		@(negedge tb_clk);
		
		

		row = tb_SI_rpixNum / image_width;
		col = tb_SI_rpixNum % image_width;
		buffer[0] = tb_input_image[row][col];	
		buffer[1] = tb_input_image[row][col + 1];
		buffer[2] = tb_input_image[row][col + 2];
		buffer[3] = tb_input_image[row][col + 3];	
		
		
	end
	endtask
	


	task read_input_header;
	begin

		in_file = $fopen(INPUT_FILENAME, "rb");

		for(i = 0; i < BMP_HEADER_SIZE_BYTES; i = i + 1)
		begin

			quiet_catch = $fscanf(in_file,"%c" , in_bmp_file_header[i]);
		end

		in_image_data_ptr[31:0] = in_bmp_file_header[(BMP_HEADER_SIZE_BYTES - 1):PIXEL_ARR_PTR_ADDR];

		quiet_catch = $fscanf(in_file,"%c" , dib_header[0]);
		quiet_catch = $fscanf(in_file,"%c" , dib_header[1]);
		quiet_catch = $fscanf(in_file,"%c" , dib_header[2]);
		quiet_catch = $fscanf(in_file,"%c" , dib_header[3]);
		dib_header_size = dib_header[3:0];
		if(DIB_HEADER_C1_SIZE == dib_header_size)
		begin
			$display("Input bitmap file uses the BITMAPINFOHEADER type of DIB header");
			for(i = 4; i < dib_header_size; i = i + 1)
			begin

				quiet_catch = $fscanf(in_file,"%c" , dib_header[i]);
			end
			

			image_width		= dib_header[7:4];
			image_height		= dib_header[11:8];
			num_pixel_bits		= dib_header[15:14];
			compression_mode	= dib_header[19:16];
			
			$info("%d %d %d", image_width , image_height, num_pixel_bits);

			if(16'd8 != num_pixel_bits)
				$fatal("This input file is using a pixel size (%0d)that is not supported, only 24bpp is supported", num_pixel_bits);
			
			if(NO_COMPRESSION != compression_mode)
				$fatal("This input file is using compression, this is not supported by this test bench");
		end
		else if(DIB_HEADER_C2_SIZE == dib_header_size)
		begin
			$display("Input bitmap file uses the BITMAPCOREHEADER  type of DIB header");
			for(i = 4; i < dib_header_size; i = i + 1)
			begin

				quiet_catch = $fscanf(in_file,"%c" , dib_header[i]);
			end
			

			image_width			= {16'd0,dib_header[5:4]};
			image_height			= {16'd0,dib_header[7:6]};
			num_pixel_bits			= dib_header[11:10];
			
			if(16'd8 != num_pixel_bits)
				$fatal("This input file is using a pixel size (%0d)that is not supported, only 24bpp is supported", num_pixel_bits);
		end
		else
		begin
			$fatal("Unsupported DIB header size of %0d found in input file", dib_header_size);
		end
		

		res_image_data_ptr = BMP_HEADER_SIZE_BYTES + dib_header_size;
		


		if($ftell(in_file) != in_image_data_ptr)
			quiet_catch = $fseek(in_file, in_image_data_ptr, SEEK_START);
	end
	endtask


	task extract_input_image;
	begin

		row_size_bytes = (((num_pixel_bits * image_width) + 31) / 32) * 4;

		num_rows = image_height;

		num_cols = image_width;

		num_pad_bytes	= row_size_bytes - (num_cols);
		tb_input_image = new[num_rows];
		for(r = num_rows - 1; r >= 0 ; r = r - 1)
		begin
			tb_input_image[r] = new[num_cols];
			for(c = 0; c < num_cols; c = c + 1)
			begin

				quiet_catch = $fscanf(in_file, "%c", tb_input_image[r][c]);
			end


			quiet_catch = $fseek(in_file, num_pad_bytes, SEEK_CUR);

		end
		


		$fclose(in_file);
	end
	endtask


	task generate_output_header;
		input string filename;
	begin

		curr_res_filename = filename;
		res_file = $fopen(filename, "wb");

		res_bmp_file_header = in_bmp_file_header;

		res_bmp_file_header[(BMP_HEADER_SIZE_BYTES - 1):PIXEL_ARR_PTR_ADDR] = res_image_data_ptr;

		for(i = 0; i < BMP_HEADER_SIZE_BYTES; i = i + 1)
		begin

			$fwrite(res_file, "%c", res_bmp_file_header[i]);
		end

		for(i = 0; i < dib_header_size; i = i + 1)
		begin

			$fwrite(res_file, "%c", dib_header[i]);
		end
		


		if($ftell(res_file) != res_image_data_ptr)
			quiet_catch = $fseek(res_file, res_image_data_ptr, SEEK_START);
	end
	endtask
	

	task dump_image_buffer_to_file;

	begin
		curr_res_filename = RESULT2_FILENAME;
		res_file = $fopen(RESULT2_FILENAME, "wb");

		for(r = 0; r < num_rows - 2 ; r = r + 1)
		begin
			for(c = 0; c < num_cols - 2; c = c + 1)
			begin

				$fwrite(res_file, "%c", tb_output_image[r][c]);
			end


			quiet_catch = $fseek(res_file, num_pad_bytes, SEEK_CUR);
		end
		


		$fwrite(res_file, "%c", 8'd0);

		$fclose(res_file);
		$info("Done generating filtered file '%s' from input file '%s'", curr_res_filename, INPUT_FILENAME);
	end
	endtask


	initial
	begin


		#(1ns);
		

		read_input_header;
		

		extract_input_image;


		tb_n_rst = 1;
		tb_image_width = image_width;
		tb_image_height = image_height;
		tb_ED_dfb = 0;
		tb_SI_dfb = 0;
		tb_ED_start = 0;
		tb_buff_filled = 0;
		tb_startAddr = 0;


		#(CLK_PERIOD);

		tb_n_rst = 0;

		#(CLK_PERIOD);

		tb_n_rst = 1;
		tb_start = 1;

		#(CLK_PERIOD);
		tb_start = 0;

		#(CLK_PERIOD * 5);	
		tb_RC4_done = 1;
		#(CLK_PERIOD);
		tb_RC4_done = 0;


		generate_output_header(RESULT1_FILENAME);


		tb_output_image = new[num_rows - 2];
		for(r = num_rows - 3; r >= 0 ; r = r - 1)
		begin
			tb_output_image[r] = new[num_cols - 2];
		end


		tb_SI_dfb = 0;
		
		while(tb_process_complete != 1)
		begin
			tb_ED_dfb = 0;



			while(tb_ED_mode == 2'b01)
			begin

				send_buffer();
				#(CLK_PERIOD);
				@(posedge tb_clk);
				tb_SI_rdata = buffer;
				tb_SI_dfb = 1;
				#(CLK_PERIOD);
				tb_SI_dfb = 0;
				#(CLK_PERIOD);

					
			end

			#(CLK_PERIOD);
			tb_output_image[tb_ED_wpixnum / (tb_image_width - 2)][tb_ED_wpixnum % (tb_image_width - 2)] = tb_ED_wdata[7:0];
			tb_output_image[tb_ED_wpixnum / (tb_image_width - 2)][(tb_ED_wpixnum % (tb_image_width - 2)) + 1] = tb_ED_wdata[15:8];

			tb_ED_dfb = 1;
			#(CLK_PERIOD);
		end

		tb_start = 0;
		
		#(CLK_PERIOD);
		tb_n_rst = 1;
		#(CLK_PERIOD);
		tb_n_rst = 0;
		#(CLK_PERIOD);
		tb_n_rst = 1;
		
		dump_image_buffer_to_file();		


		for(r = 0; r < num_rows - 2 ; r = r + 1)
		begin
			for(c = 0; c < num_cols - 2; c = c + 1)
			begin
				$display("%d",tb_output_image[r][c]);
			end
		end
		
			
	end

endmodule
module tb_edm();


	localparam CLK_PERIOD	= 35ns;
	reg tb_clk;
	always
	begin
		tb_clk = 1'b0;
		#(CLK_PERIOD/2.0);
		tb_clk = 1'b1;
		#(CLK_PERIOD/2.0);
	end

	reg tb_n_rst;
	reg tb_ED_start;
	reg tb_ED_dfb;
	reg tb_buff_filled;
	reg [11:0] tb_image_width;
	reg [11:0] tb_image_height;
	reg [95:0] tb_ED_rdata;
	reg tb_fill_buff;
	reg tb_ED_done;
	reg [1:0] tb_ED_mode;
	reg [19:0] tb_ED_rpixnum;
	reg [19:0] tb_ED_wpixnum;
	reg [15:0] tb_ED_wdata;

	reg [1:0] tb_SI_mode;
	reg tb_SI_dfb;
	reg [31:0] tb_SI_rdata;
	reg [19:0] tb_SI_rpixNum;	

	reg tb_start;
	reg tb_process_complete;
	reg tb_RC4_start;
	reg tb_RC4_done;

	reg [31:0]tb_RC4_wdata;
	reg [19:0] tb_RC4_pixNum;
	reg [1:0] tb_RC4_mode);
	reg [31:0] tb_rdata;
	reg tb_data_feedback;
	reg [31:0] tb_RC4_data;
	reg RC4_dfb;
	reg tb_startAddr_sel;
	reg [31:0] wdata;
	reg [1:0] tb_size;
	reg [1:0] tb_mode;
	reg [19:0] pixNum;


	reg tb_HREADY;
	reg [31:0] tb_HRDATA;
	reg [31:0] tb_HADDR;
	reg [1:0] tb_HSIZE;
	reg [31:0] tb_HWDATA;
	reg tb_HWRITE;
	

	edm DUT_EDM(
	.clk(tb_clk),
	.n_rst(tb_n_rst),
	.ED_start(tb_ED_start),
	.ED_dfb(tb_ED_dfb),
	.buff_filled(tb_buff_filled),
	.image_width(tb_image_width),
	.image_height(tb_image_height),
	.ED_rdata(tb_ED_rdata),
	.fill_buff(tb_fill_buff),
	.ED_done(tb_ED_done),
	.ED_mode(tb_ED_mode),
	.ED_rpixnum(tb_ED_rpixnum),
	.ED_wpixnum(tb_ED_wpixnum),
	.ED_wdata(tb_ED_wdata)
	);

	sample_image_data_storage DUT_BUFF(
	.clk(tb_clk),
	.n_rst(tb_n_rst),
	.image_width(tb_image_width),
	.SI_dfb(tb_SI_dfb),
	.SI_rdata(tb_SI_rdata),
	.ED_rpixNum(tb_ED_rpixnum),
	.fill_buff(tb_fill_buff),
	.buff_filled(tb_buff_filled),
	.ED_rdata(tb_ED_rdata),
	.SI_rpixNum(tb_SI_rpixNum),
	.SI_mode(tb_SI_mode)
	);

	mcu DUT_mcu(
	.clk(tb_clk),
	.n_rst(tb_n_rst),	
	.start(tb_start),
	.RC4_done(tb_RC4_done),
	.ED_done(tb_ED_done),
	.RC4_start(tb_RC4_start),
	.ED_start(tb_ED_start),
	.process_complete(tb_process_complete)
	);


	AHB_helper DUT_ahbH(
	.RC4_start(tb_RC4_start),
	.ED_start(tb_ED_start),
	.RC4_wdata(tb_RC4_wdata),
	.RC4_pixNum(tb_RC4_pixNum),
	.RC4_mode(tb_RC4_mode),
	.ED_wdata(tb_ED_wdata),
	.ED_wpixNim(tb_ED_wpixnum),
	.ED_mode(tb_ED_mode),
	.SI_rpixNum(tb_SI_rpixnum),
	.rdata(tb_rdata),
	.data_feedback(tb_data_feedback),
	.SI_mode(tb_SI_mode),
	.RC4_data(tb_RC4_data),
	.RC4_dfb(tb_RC4_dfb),
	.ED_dfb(tb_ED_dfb),
	.SI_rdata(tb_SI_rdata),
	.SI_dfb(tb_SI_dfb),
	.startAddr_sel(tb_startAddr_sel),
	.wdata(tb_wdata),
	.size(tb_size),
	.mode(tb_mode),
	.pixNum(tb_pixNum)
	);

	

	AHB_master DUT_ahbM(
	.clk(tb_clk),
	.n_rst(tb_n_rst)
	.HREADY(tb_HREADY),
	.HRDATA(tb_HRDATA),
	.mode(tb_mode),
	.wdata(tb_wdata),
	.pixNum(tb_pixNum),
	.image_startAddr(tb_image_startAddr),
	.size(tb_size),
	.image_width(tb_image_width),
	.image_height(tb_image_height),
	.startAddr_sel(tb_startAddr_sel),
	.HADDR(tb_HADDR),
	.HWDATA(tb_HWDATA),
	.HWRITE(tb_HWRITE),
	.HSIZE(tb_HSIZE),
	.data_feedback(tb_data_feedback),
	.rdata(tb_rdata)
	);



	parameter	INPUT_FILENAME		= "./images/fruits.bmp";
	parameter	RESULT1_FILENAME	= "./docs/edge_detected_fruit.bmp";
	parameter	RESULT2_FILENAME	= "./docs/edge_detected_fruit2.txt";


	localparam SEEK_START		= 0;
	localparam SEEK_CUR		= 1;
	localparam SEEK_END		= 2;
	


	localparam BMP_HEADER_SIZE_BYTES	= 14;
	localparam PIXEL_ARR_PTR_ADDR			= BMP_HEADER_SIZE_BYTES - 4;
	localparam DIB_HEADER_C1_SIZE			= 40;
	localparam DIB_HEADER_C2_SIZE			= 12;
	localparam NO_COMPRESSION 		= 0;


	localparam NUM_VAL_BITS		= 16;
	localparam MAX_VAL_BIT		= NUM_VAL_BITS - 1;
	localparam CHECK_DELAY		= 1ns;



	integer r;
	integer c;
	reg [7:0] tmp_byte;
	integer in_file;
	integer res_file;
	string  curr_res_filename;
	integer num_rows;
	integer num_cols;
	integer num_pad_bytes;
	reg [7:0] in_pixel_val;
	reg [7:0] res_pixel_val;
	integer i;
	integer quiet_catch;
	integer count;

	reg [(BMP_HEADER_SIZE_BYTES - 1):0][7:0] in_bmp_file_header;
	reg [(BMP_HEADER_SIZE_BYTES - 1):0][7:0] res_bmp_file_header;
	reg [31:0] in_image_data_ptr;
	reg [31:0] res_image_data_ptr;
	

	reg [(DIB_HEADER_C1_SIZE - 1):0][7:0] dib_header;
	reg [31:0] dib_header_size;
	reg [31:0] image_width;
	reg [31:0] image_height;
	reg [15:0] num_pixel_bits;
	reg [31:0] compression_mode;
	

	reg [7:0] tb_input_image [][]; 
	reg [7:0] tb_output_image [][];




	integer row,col;
	reg [3:0][7:0] buffer;


	integer row_size_bytes;

	integer k;
	

	task send_buffer;
	begin

		@(negedge tb_clk);
		
		

		row = tb_SI_rpixNum / image_width;
		col = tb_SI_rpixNum % image_width;
		buffer[0] = tb_input_image[row][col];	
		buffer[1] = tb_input_image[row][col + 1];
		buffer[2] = tb_input_image[row][col + 2];
		buffer[3] = tb_input_image[row][col + 3];	
		
		
	end
	endtask
	


	task read_input_header;
	begin

		in_file = $fopen(INPUT_FILENAME, "rb");

		for(i = 0; i < BMP_HEADER_SIZE_BYTES; i = i + 1)
		begin

			quiet_catch = $fscanf(in_file,"%c" , in_bmp_file_header[i]);
		end

		in_image_data_ptr[31:0] = in_bmp_file_header[(BMP_HEADER_SIZE_BYTES - 1):PIXEL_ARR_PTR_ADDR];

		quiet_catch = $fscanf(in_file,"%c" , dib_header[0]);
		quiet_catch = $fscanf(in_file,"%c" , dib_header[1]);
		quiet_catch = $fscanf(in_file,"%c" , dib_header[2]);
		quiet_catch = $fscanf(in_file,"%c" , dib_header[3]);
		dib_header_size = dib_header[3:0];
		if(DIB_HEADER_C1_SIZE == dib_header_size)
		begin
			$display("Input bitmap file uses the BITMAPINFOHEADER type of DIB header");
			for(i = 4; i < dib_header_size; i = i + 1)
			begin

				quiet_catch = $fscanf(in_file,"%c" , dib_header[i]);
			end
			

			image_width		= dib_header[7:4];
			image_height		= dib_header[11:8];
			num_pixel_bits		= dib_header[15:14];
			compression_mode	= dib_header[19:16];
			
			$info("%d %d %d", image_width , image_height, num_pixel_bits);

			if(16'd8 != num_pixel_bits)
				$fatal("This input file is using a pixel size (%0d)that is not supported, only 24bpp is supported", num_pixel_bits);
			
			if(NO_COMPRESSION != compression_mode)
				$fatal("This input file is using compression, this is not supported by this test bench");
		end
		else if(DIB_HEADER_C2_SIZE == dib_header_size)
		begin
			$display("Input bitmap file uses the BITMAPCOREHEADER  type of DIB header");
			for(i = 4; i < dib_header_size; i = i + 1)
			begin

				quiet_catch = $fscanf(in_file,"%c" , dib_header[i]);
			end
			

			image_width			= {16'd0,dib_header[5:4]};
			image_height			= {16'd0,dib_header[7:6]};
			num_pixel_bits			= dib_header[11:10];
			
			if(16'd8 != num_pixel_bits)
				$fatal("This input file is using a pixel size (%0d)that is not supported, only 24bpp is supported", num_pixel_bits);
		end
		else
		begin
			$fatal("Unsupported DIB header size of %0d found in input file", dib_header_size);
		end
		

		res_image_data_ptr = BMP_HEADER_SIZE_BYTES + dib_header_size;
		


		if($ftell(in_file) != in_image_data_ptr)
			quiet_catch = $fseek(in_file, in_image_data_ptr, SEEK_START);
	end
	endtask


	task extract_input_image;
	begin

		row_size_bytes = (((num_pixel_bits * image_width) + 31) / 32) * 4;

		num_rows = image_height;

		num_cols = image_width;

		num_pad_bytes	= row_size_bytes - (num_cols);
		tb_input_image = new[num_rows];
		for(r = num_rows - 1; r >= 0 ; r = r - 1)
		begin
			tb_input_image[r] = new[num_cols];
			for(c = 0; c < num_cols; c = c + 1)
			begin

				quiet_catch = $fscanf(in_file, "%c", tb_input_image[r][c]);
			end


			quiet_catch = $fseek(in_file, num_pad_bytes, SEEK_CUR);

		end
		


		$fclose(in_file);
	end
	endtask


	task generate_output_header;
		input string filename;
	begin

		curr_res_filename = filename;
		res_file = $fopen(filename, "wb");

		res_bmp_file_header = in_bmp_file_header;

		res_bmp_file_header[(BMP_HEADER_SIZE_BYTES - 1):PIXEL_ARR_PTR_ADDR] = res_image_data_ptr;

		for(i = 0; i < BMP_HEADER_SIZE_BYTES; i = i + 1)
		begin

			$fwrite(res_file, "%c", res_bmp_file_header[i]);
		end

		for(i = 0; i < dib_header_size; i = i + 1)
		begin

			$fwrite(res_file, "%c", dib_header[i]);
		end
		


		if($ftell(res_file) != res_image_data_ptr)
			quiet_catch = $fseek(res_file, res_image_data_ptr, SEEK_START);
	end
	endtask
	

	task dump_image_buffer_to_file;

	begin
		curr_res_filename = RESULT2_FILENAME;
		res_file = $fopen(RESULT2_FILENAME, "wb");

		for(r = 0; r < num_rows - 2 ; r = r + 1)
		begin
			for(c = 0; c < num_cols - 2; c = c + 1)
			begin

				$fwrite(res_file, "%c", tb_output_image[r][c]);
			end


			quiet_catch = $fseek(res_file, num_pad_bytes, SEEK_CUR);
		end
		


		$fwrite(res_file, "%c", 8'd0);

		$fclose(res_file);
		$info("Done generating filtered file '%s' from input file '%s'", curr_res_filename, INPUT_FILENAME);
	end
	endtask


	initial
	begin


		#(1ns);
		

		read_input_header;
		

		extract_input_image;


		tb_n_rst = 1;
		tb_image_width = image_width;
		tb_image_height = image_height;
		tb_ED_dfb = 0;
		tb_SI_dfb = 0;
		tb_ED_start = 0;
		tb_buff_filled = 0;
		tb_startAddr = 0;


		#(CLK_PERIOD);

		tb_n_rst = 0;

		#(CLK_PERIOD);

		tb_n_rst = 1;
		tb_start = 1;

		#(CLK_PERIOD);
		tb_start = 0;

		#(CLK_PERIOD * 5);	
		tb_RC4_done = 1;
		#(CLK_PERIOD);
		tb_RC4_done = 0;


		generate_output_header(RESULT1_FILENAME);


		tb_output_image = new[num_rows - 2];
		for(r = num_rows - 3; r >= 0 ; r = r - 1)
		begin
			tb_output_image[r] = new[num_cols - 2];
		end


		tb_SI_dfb = 0;
		
		while(tb_process_complete != 1)
		begin
			tb_ED_dfb = 0;



			while(tb_ED_mode == 2'b01)
			begin

				send_buffer();
				#(CLK_PERIOD);
				@(posedge tb_clk);
				tb_SI_rdata = buffer;
				tb_SI_dfb = 1;
				#(CLK_PERIOD);
				tb_SI_dfb = 0;
				#(CLK_PERIOD);

					
			end

			#(CLK_PERIOD);
			tb_output_image[tb_ED_wpixnum / (tb_image_width - 2)][tb_ED_wpixnum % (tb_image_width - 2)] = tb_ED_wdata[7:0];
			tb_output_image[tb_ED_wpixnum / (tb_image_width - 2)][(tb_ED_wpixnum % (tb_image_width - 2)) + 1] = tb_ED_wdata[15:8];

			tb_ED_dfb = 1;
			#(CLK_PERIOD);
		end

		tb_start = 0;
		
		#(CLK_PERIOD);
		tb_n_rst = 1;
		#(CLK_PERIOD);
		tb_n_rst = 0;
		#(CLK_PERIOD);
		tb_n_rst = 1;
		
		dump_image_buffer_to_file();		


		for(r = 0; r < num_rows - 2 ; r = r + 1)
		begin
			for(c = 0; c < num_cols - 2; c = c + 1)
			begin
				$display("%d",tb_output_image[r][c]);
			end
		end
		
			
	end

endmodule