data/full_repos/permissive/120518070/main/source/tb_overall.sv
 line:12: before: "ns"
1: b'%Error: data/full_repos/permissive/120518070/main/source/tb_overall.sv:12: syntax error, unexpected TIME NUMBER, expecting TYPE-IDENTIFIER\n  localparam CLK_PERIOD = 20ns; \n                          ^~~~\n%Warning-STMTDLY: data/full_repos/permissive/120518070/main/source/tb_overall.sv:40: 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_overall.sv:42: Unsupported: Ignoring delay on this delayed statement.\n   #(CLK_PERIOD / 2.0);\n   ^\n%Error: data/full_repos/permissive/120518070/main/source/tb_overall.sv:100: syntax error, unexpected TIME NUMBER, expecting TYPE-IDENTIFIER\n localparam CHECK_DELAY  = 1ns;        \n                           ^~~\n%Error: data/full_repos/permissive/120518070/main/source/tb_overall.sv:151: syntax error, unexpected \'@\'\n  @(negedge tb_clk);\n  ^\n%Error: data/full_repos/permissive/120518070/main/source/tb_overall.sv:172: syntax error, unexpected \'@\'\n  @(negedge tb_clk);\n  ^\n%Error: data/full_repos/permissive/120518070/main/source/tb_overall.sv:291: Unsupported: Dynamic array new\n  tb_input_image_RC4 = new[num_rows];\n                       ^~~\n%Error: data/full_repos/permissive/120518070/main/source/tb_overall.sv:297: Unsupported: Dynamic array new\n   tb_input_image_RC4[r] = new[num_cols];\n                           ^~~\n%Error: data/full_repos/permissive/120518070/main/source/tb_overall.sv:332: Unsupported: Dynamic array new\n  tb_input_image_ED = new[num_rows];\n                      ^~~\n%Error: data/full_repos/permissive/120518070/main/source/tb_overall.sv:338: Unsupported: Dynamic array new\n   tb_input_image_ED[r] = new[num_cols];\n                          ^~~\n%Warning-STMTDLY: data/full_repos/permissive/120518070/main/source/tb_overall.sv:559: Unsupported: Ignoring delay on this delayed statement.\n  #(CLK_PERIOD);\n  ^\n%Warning-STMTDLY: data/full_repos/permissive/120518070/main/source/tb_overall.sv:576: Unsupported: Ignoring delay on this delayed statement.\n  #(CLK_PERIOD);\n  ^\n%Warning-STMTDLY: data/full_repos/permissive/120518070/main/source/tb_overall.sv:578: Unsupported: Ignoring delay on this delayed statement.\n  #(CLK_PERIOD);\n  ^\n%Error: data/full_repos/permissive/120518070/main/source/tb_overall.sv:590: syntax error, unexpected \'@\'\n  @(posedge tb_clk);\n  ^\n%Warning-STMTDLY: data/full_repos/permissive/120518070/main/source/tb_overall.sv:602: Unsupported: Ignoring delay on this delayed statement.\n  #(CLK_PERIOD*2);\n  ^\n%Warning-STMTDLY: data/full_repos/permissive/120518070/main/source/tb_overall.sv:611: Unsupported: Ignoring delay on this delayed statement.\n  #(CLK_PERIOD*2);\n  ^\n%Warning-STMTDLY: data/full_repos/permissive/120518070/main/source/tb_overall.sv:622: Unsupported: Ignoring delay on this delayed statement.\n  #(CLK_PERIOD*10);\n  ^\n%Warning-STMTDLY: data/full_repos/permissive/120518070/main/source/tb_overall.sv:632: Unsupported: Ignoring delay on this delayed statement.\n  #(CLK_PERIOD*2);\n  ^\n%Error: data/full_repos/permissive/120518070/main/source/tb_overall.sv:640: Unsupported: Dynamic array new\n  tb_output_image_RC4 = new[num_rows];\n                        ^~~\n%Error: data/full_repos/permissive/120518070/main/source/tb_overall.sv:643: Unsupported: Dynamic array new\n   tb_output_image_RC4[r] = new[num_cols];\n                            ^~~\n%Error: data/full_repos/permissive/120518070/main/source/tb_overall.sv:654: syntax error, unexpected \'@\'\n     @(posedge tb_clk);\n     ^\n%Warning-STMTDLY: data/full_repos/permissive/120518070/main/source/tb_overall.sv:669: Unsupported: Ignoring delay on this delayed statement.\n   #(CLK_PERIOD);\n   ^\n%Error: data/full_repos/permissive/120518070/main/source/tb_overall.sv:684: Unsupported: Dynamic array new\n  tb_output_image_ED = new[num_rows - 2];\n                       ^~~\n%Error: data/full_repos/permissive/120518070/main/source/tb_overall.sv:687: Unsupported: Dynamic array new\n   tb_output_image_ED[r] = new[num_cols - 2];\n                           ^~~\n%Error: data/full_repos/permissive/120518070/main/source/tb_overall.sv:699: syntax error, unexpected \'@\'\n     @(posedge tb_clk);\n     ^\n%Warning-STMTDLY: data/full_repos/permissive/120518070/main/source/tb_overall.sv:710: Unsupported: Ignoring delay on this delayed statement.\n  #(CLK_PERIOD);\n  ^\n%Warning-STMTDLY: data/full_repos/permissive/120518070/main/source/tb_overall.sv:719: Unsupported: Ignoring delay on this delayed statement.\n  #(CLK_PERIOD);\n  ^\n%Warning-STMTDLY: data/full_repos/permissive/120518070/main/source/tb_overall.sv:721: Unsupported: Ignoring delay on this delayed statement.\n  #(CLK_PERIOD);\n  ^\n%Warning-STMTDLY: data/full_repos/permissive/120518070/main/source/tb_overall.sv:723: Unsupported: Ignoring delay on this delayed statement.\n  #(CLK_PERIOD);\n  ^\n%Error: Exiting due to 15 error(s), 14 warning(s)\n'
module tb_overall();


		localparam CLK_PERIOD	= 20ns;	
		reg tb_clk;
		reg tb_n_rst;

		reg tb_HWRITE_slave;

		reg tb_HRESP_slave;
		reg [1:0] tb_HSIZE_slave;
		reg [31:0] tb_HADDR_slave;
		reg [31:0] tb_HWDATA_slave;
		reg [31:0] tb_HRDATA_slave;

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


		always
		begin : CLK_GEN
			tb_clk = 1'b0;
			#(CLK_PERIOD / 2.0);
			tb_clk = 1'b1;
			#(CLK_PERIOD / 2.0);
		end

	overall DUT(
		.tb_clk(tb_clk),
		.tb_n_rst(tb_n_rst),
		.tb_HADDR_slave(tb_HADDR_slave),
		.tb_HWDATA_slave(tb_HWDATA_slave),
		.tb_HWRITE_slave(tb_HWRITE_slave),
		.tb_HREADY(tb_HREADY),
		.tb_HRDATA(tb_HRDATA),
		.tb_HRDATA_slave(tb_HRDATA_slave),

		.tb_HADDR(tb_HADDR),
		.tb_HWDATA(tb_HWDATA),
		.tb_HWRITE(tb_HWRITE),
		.tb_HSIZE(tb_HSIZE),
		.tb_HSIZE_slave(tb_HSIZE_slave),
		.tb_HRESP(tb_HRESP),
		.tb_HRESP_slave(tb_HRESP_slave),
		.tb_RC4_done(tb_RC4_done),
		.tb_process_complete(tb_process_complete)
	);





	
	parameter	INPUT1_FILENAME		= "./images/encrypted_boat.bmp";
	parameter	INT1_FILENAME		= "./docs/decrypted_boat.bmp";
	parameter	RESULT1_FILENAME	= "./docs/edge_detected_boat.bmp";
	
	parameter	INPUT2_FILENAME		= "./images/encrypted_mj.bmp";
	parameter	INT2_FILENAME		= "./docs/decrypted_mj.bmp";
	parameter	RESULT2_FILENAME	= "./docs/edge_detected_mj.bmp";
	
	parameter	INPUT3_FILENAME		= "./images/Marked_enc.bmp";
	parameter	INT3_FILENAME		= "./docs/decrypted_marked.bmp";
	parameter	RESULT3_FILENAME	= "./docs/edge_detected_marked.bmp";


	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;
	integer testCase;
	

	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_RC4 [][];
	reg [7:0] tb_input_image_ED [][];
	reg [7:0] tb_output_image_RC4 [][];
	reg [7:0] tb_output_image_ED [][];

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


	integer row_size_bytes;
	



	task send_buffer_RC4;
	begin

		@(negedge tb_clk);



		row = tb_HADDR / image_width;
		col = tb_HADDR % image_width;
		buffer[0] = tb_input_image_RC4[row][col];
		buffer[1] = tb_input_image_RC4[row][col + 1];
		buffer[2] = tb_input_image_RC4[row][col + 2];
		buffer[3] = tb_input_image_RC4[row][col + 3];


	end
	endtask




	task send_buffer_ED;
	begin

		@(negedge tb_clk);

	


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



	task read_input_header;
		input string filename;
	begin
		$display("########################################");
		$display("ImageFile : %s",filename);
		$display("########################################");
			

		in_file = $fopen(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 %d %d", image_width , image_height, num_pixel_bits, dib_header[35:32], in_file);

			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;
		$info("res_image_data_ptr = %d", res_image_data_ptr);
		


		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_RC4;
	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);
		$info("num_pad_bytes in decrpted image = %d", num_pad_bytes);
		tb_input_image_RC4 = new[num_rows];



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

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


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

		end
		


		$fclose(in_file);
	end
	endtask




	task extract_input_image_ED;
	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);
		$info("num_pad_bytes in Decrpyted image = %d", num_pad_bytes);
		tb_input_image_ED = new[num_rows];



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

				quiet_catch = $fscanf(in_file, "%c", tb_input_image_ED[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;
		dib_header[7:4] = dib_header[7:4] - 2;
		dib_header[11:8] = dib_header[11:8] - 2;
		

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

		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 generate_output_header_RC4;
		input string filename;
	begin

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

		res_bmp_file_header = in_bmp_file_header;
		dib_header[7:4] = dib_header[7:4];
		dib_header[11:8] = dib_header[11:8];
		

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

		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_RC4;
		input string filename;
	begin
		curr_res_filename = filename;


		for(i = 0; i < 255; i++)
		begin
			$fwrite(res_file, "%c", i);
			$fwrite(res_file, "%c", i);
			$fwrite(res_file, "%c", i);
			$fwrite(res_file, "%c", 0);
		end	
	
		$fwrite(res_file, "%c", 255);
		$fwrite(res_file, "%c", 255);
		$fwrite(res_file, "%c", 255);


		$info("%d, %d", num_rows, num_cols);
		for(r = num_rows-1; r >= 0 ; r = r - 1)
		begin
			for(c = 0; c < num_cols; c = c + 1)
			begin

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


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


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

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




	task dump_image_buffer_to_file_ED;
		input string filename;
	begin
		curr_res_filename = filename;


		for(i = 0; i < 255; i++)
		begin
			$fwrite(res_file, "%c", i);
			$fwrite(res_file, "%c", i);
			$fwrite(res_file, "%c", i);
			$fwrite(res_file, "%c", 0);
		end		
		
		$fwrite(res_file, "%c", 255);
		$fwrite(res_file, "%c", 255);
		$fwrite(res_file, "%c", 255);
		



		$info("%d, %d", num_rows, num_cols);
		for(r = num_rows-1; r >= 0 ; r = r - 1)
		begin
			for(c = 0; c < num_cols; c = c + 1)
			begin

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


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


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

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




	task resetSignals;
	begin
		
	end
	endtask





	task printOutputArray;
		input reg imageArray [][];
	begin
		for(r = 0; r < num_rows - 2 ; r = r + 1)
		begin
			for(c = 0; c < num_cols - 2; c = c + 1)
			begin
				$display("%d",imageArray[r][c]);
			end
		end
	end
	endtask



	initial
	begin


		#(CLK_PERIOD);

		



		testCase = 0;
		

		read_input_header(INPUT1_FILENAME);
		

		extract_input_image_RC4;



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

		tb_HRESP = 0;
		tb_HREADY = 1;





		testCase = 1;
		@(posedge tb_clk);


		tb_n_rst		= 1'b1; 		
		tb_HWDATA_slave			= '0;
		tb_HADDR_slave[31:28] 		= 4'b1010;
		tb_HADDR_slave[27:4] 		= '0;
		tb_HADDR_slave[3:0] 		= 4'b0001;
		tb_HWRITE_slave 		= 1'b1;
		tb_HSIZE_slave 			= 2'b10;


		#(CLK_PERIOD*2);
	
		tb_HWDATA_slave 		= "DR.J";
		tb_HADDR_slave[31:28] 		= 4'b1010;
		tb_HADDR_slave[27:4] 		= '0;
		tb_HADDR_slave[3:0] 		= 4'b0010;
		tb_HWRITE_slave 		= 1'b1;
		tb_HSIZE_slave 			= 2'b10;
	
		#(CLK_PERIOD*2);

		tb_HWDATA_slave 		= {20'b00000000000000000000, image_width};
		tb_HADDR_slave[31:28] 		= 4'b1010;
		tb_HADDR_slave[27:4] 		= '0;
		tb_HADDR_slave[3:0] 		= 4'b0100;
		tb_HWRITE_slave 		= 1'b1;
		tb_HSIZE_slave 			= 2'b10;

		testCase = 3;

		#(CLK_PERIOD*10);
		
		tb_HWDATA_slave 		= {20'b00000000000000000000, image_height};
		tb_HADDR_slave[31:28] 		= 4'b1010;
		tb_HADDR_slave[27:4] 		= '0;
		tb_HADDR_slave[3:0] 		= 4'b1000;
		tb_HWRITE_slave 		= 1'b1;
		tb_HSIZE_slave 			= 2'b10;


		#(CLK_PERIOD*2);


		testCase = 4;

		generate_output_header_RC4(INT1_FILENAME);


		tb_output_image_RC4 = new[num_rows];
		for(r = 0; r < num_rows ; r = r + 1)
		begin
			tb_output_image_RC4[r] = new[num_cols];
		end

		while(tb_RC4_done != 1)
		begin
			if(tb_HADDR[31:28] == 4'b0101)
			begin
				if(tb_HWRITE == 0)
				begin
					send_buffer_RC4();

					@(posedge tb_clk);
					tb_HRDATA = {buffer[0],buffer[1], buffer[2], buffer[3]};

				end
				else if(tb_HWRITE == 1)
				begin		

					tb_output_image_RC4[tb_HADDR[19:0] / image_width][tb_HADDR[19:0] % image_width + 3] = tb_HWDATA[7:0];
					tb_output_image_RC4[tb_HADDR[19:0] / image_width][(tb_HADDR[19:0] % image_width)+2] = tb_HWDATA[15:8];
					tb_output_image_RC4[tb_HADDR[19:0] / image_width][(tb_HADDR[19:0] % image_width)+1] = tb_HWDATA[23:16];
					tb_output_image_RC4[tb_HADDR[19:0] / image_width][(tb_HADDR[19:0] % image_width)] = tb_HWDATA[31:24];
					
					
				end
			end
			#(CLK_PERIOD);
		end		

		dump_image_buffer_to_file_RC4(INT1_FILENAME);
		$fclose(res_file);		

		testCase = 5;

		read_input_header(INT1_FILENAME);
		extract_input_image_ED;


		generate_output_header(RESULT1_FILENAME);


		tb_output_image_ED = new[num_rows - 2];
		for(r = 0; r < num_rows - 2 ; r = r + 1)
		begin
			tb_output_image_ED[r] = new[num_cols - 2];
		end
		
		
		while(tb_process_complete != 1)
		begin

			if(tb_HADDR[31:28] == 4'b0101)
			begin
				if(tb_HWRITE == 0)
				begin
					send_buffer_ED();
					@(posedge tb_clk);
					tb_HRDATA = buffer;

				end
				else if(tb_HWRITE == 1)
				begin		

					tb_output_image_ED[(tb_HADDR % (image_height * image_width)) / (image_width - 2)][(tb_HADDR % (image_height * image_width)) % (image_width - 2)] = tb_HWDATA[7:0];
					tb_output_image_ED[(tb_HADDR % (image_height * image_width)) / (image_width - 2)][((tb_HADDR % (image_height * image_width)) % (image_width - 2)) + 1] = tb_HWDATA[15:8];
				end
			end
		#(CLK_PERIOD);
		end


		dump_image_buffer_to_file_ED(RESULT1_FILENAME);	

		
		$fclose(res_file);
		
		#(CLK_PERIOD);
		tb_n_rst = 1;
		#(CLK_PERIOD);
		tb_n_rst = 0;
		#(CLK_PERIOD);
		tb_n_rst = 1; 
		

	end


endmodule
module tb_overall();



		localparam CLK_PERIOD	= 20ns;	
		reg tb_clk;
		reg tb_n_rst;

		reg tb_HWRITE_slave;

		reg tb_HRESP_slave;
		reg [1:0] tb_HSIZE_slave;
		reg [31:0] tb_HADDR_slave;
		reg [31:0] tb_HWDATA_slave;
		reg [31:0] tb_HRDATA_slave;

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


		always
		begin : CLK_GEN
			tb_clk = 1'b0;
			#(CLK_PERIOD / 2.0);
			tb_clk = 1'b1;
			#(CLK_PERIOD / 2.0);
		end

	overall DUT(
		.tb_clk(tb_clk),
		.tb_n_rst(tb_n_rst),
		.tb_HADDR_slave(tb_HADDR_slave),
		.tb_HWDATA_slave(tb_HWDATA_slave),
		.tb_HWRITE_slave(tb_HWRITE_slave),
		.tb_HREADY(tb_HREADY),
		.tb_HRDATA(tb_HRDATA),
		.tb_HRDATA_slave(tb_HRDATA_slave),

		.tb_HADDR(tb_HADDR),
		.tb_HWDATA(tb_HWDATA),
		.tb_HWRITE(tb_HWRITE),
		.tb_HSIZE(tb_HSIZE),
		.tb_HSIZE_slave(tb_HSIZE_slave),
		.tb_HRESP(tb_HRESP),
		.tb_HRESP_slave(tb_HRESP_slave),
		.tb_RC4_done(tb_RC4_done),
		.tb_process_complete(tb_process_complete)
	);





	
	parameter	INPUT1_FILENAME		= "./images/encrypted_boat.bmp";
	parameter	INT1_FILENAME		= "./docs/decrypted_boat.bmp";
	parameter	RESULT1_FILENAME	= "./docs/edge_detected_boat.bmp";
	
	parameter	INPUT2_FILENAME		= "./images/encrypted_mj.bmp";
	parameter	INT2_FILENAME		= "./docs/decrypted_mj.bmp";
	parameter	RESULT2_FILENAME	= "./docs/edge_detected_mj.bmp";
	
	parameter	INPUT3_FILENAME		= "./images/Marked_enc.bmp";
	parameter	INT3_FILENAME		= "./docs/decrypted_marked.bmp";
	parameter	RESULT3_FILENAME	= "./docs/edge_detected_marked.bmp";


	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;
	integer testCase;
	

	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_RC4 [][];
	reg [7:0] tb_input_image_ED [][];
	reg [7:0] tb_output_image_RC4 [][];
	reg [7:0] tb_output_image_ED [][];

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


	integer row_size_bytes;
	



	task send_buffer_RC4;
	begin

		@(negedge tb_clk);



		row = tb_HADDR / image_width;
		col = tb_HADDR % image_width;
		buffer[0] = tb_input_image_RC4[row][col];
		buffer[1] = tb_input_image_RC4[row][col + 1];
		buffer[2] = tb_input_image_RC4[row][col + 2];
		buffer[3] = tb_input_image_RC4[row][col + 3];


	end
	endtask




	task send_buffer_ED;
	begin

		@(negedge tb_clk);

	


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



	task read_input_header;
		input string filename;
	begin
		$display("########################################");
		$display("ImageFile : %s",filename);
		$display("########################################");
			

		in_file = $fopen(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 %d %d", image_width , image_height, num_pixel_bits, dib_header[35:32], in_file);

			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;
		$info("res_image_data_ptr = %d", res_image_data_ptr);
		


		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_RC4;
	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);
		$info("num_pad_bytes in decrpted image = %d", num_pad_bytes);
		tb_input_image_RC4 = new[num_rows];



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

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


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

		end
		


		$fclose(in_file);
	end
	endtask




	task extract_input_image_ED;
	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);
		$info("num_pad_bytes in Decrpyted image = %d", num_pad_bytes);
		tb_input_image_ED = new[num_rows];



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

				quiet_catch = $fscanf(in_file, "%c", tb_input_image_ED[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;
		dib_header[7:4] = dib_header[7:4] - 2;
		dib_header[11:8] = dib_header[11:8] - 2;
		

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

		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 generate_output_header_RC4;
		input string filename;
	begin

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

		res_bmp_file_header = in_bmp_file_header;
		dib_header[7:4] = dib_header[7:4];
		dib_header[11:8] = dib_header[11:8];
		

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

		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_RC4;
		input string filename;
	begin
		curr_res_filename = filename;


		for(i = 0; i < 255; i++)
		begin
			$fwrite(res_file, "%c", i);
			$fwrite(res_file, "%c", i);
			$fwrite(res_file, "%c", i);
			$fwrite(res_file, "%c", 0);
		end	
	
		$fwrite(res_file, "%c", 255);
		$fwrite(res_file, "%c", 255);
		$fwrite(res_file, "%c", 255);


		$info("%d, %d", num_rows, num_cols);
		for(r = num_rows-1; r >= 0 ; r = r - 1)
		begin
			for(c = 0; c < num_cols; c = c + 1)
			begin

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


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


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

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




	task dump_image_buffer_to_file_ED;
		input string filename;
	begin
		curr_res_filename = filename;


		for(i = 0; i < 255; i++)
		begin
			$fwrite(res_file, "%c", i);
			$fwrite(res_file, "%c", i);
			$fwrite(res_file, "%c", i);
			$fwrite(res_file, "%c", 0);
		end		
		
		$fwrite(res_file, "%c", 255);
		$fwrite(res_file, "%c", 255);
		$fwrite(res_file, "%c", 255);
		



		$info("%d, %d", num_rows, num_cols);
		for(r = num_rows-1; r >= 0 ; r = r - 1)
		begin
			for(c = 0; c < num_cols; c = c + 1)
			begin

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


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


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

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




	task resetSignals;
	begin
		
	end
	endtask





	task printOutputArray;
		input reg imageArray [][];
	begin
		for(r = 0; r < num_rows - 2 ; r = r + 1)
		begin
			for(c = 0; c < num_cols - 2; c = c + 1)
			begin
				$display("%d",imageArray[r][c]);
			end
		end
	end
	endtask



	initial
	begin


		#(CLK_PERIOD);

		



		testCase = 0;
		

		read_input_header(INPUT1_FILENAME);
		

		extract_input_image_RC4;



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

		tb_HRESP = 0;
		tb_HREADY = 1;





		testCase = 1;
		@(posedge tb_clk);


		tb_n_rst		= 1'b1; 		
		tb_HWDATA_slave			= '0;
		tb_HADDR_slave[31:28] 		= 4'b1010;
		tb_HADDR_slave[27:4] 		= '0;
		tb_HADDR_slave[3:0] 		= 4'b0001;
		tb_HWRITE_slave 		= 1'b1;
		tb_HSIZE_slave 			= 2'b10;


		#(CLK_PERIOD*2);
	
		tb_HWDATA_slave 		= "DR.J";
		tb_HADDR_slave[31:28] 		= 4'b1010;
		tb_HADDR_slave[27:4] 		= '0;
		tb_HADDR_slave[3:0] 		= 4'b0010;
		tb_HWRITE_slave 		= 1'b1;
		tb_HSIZE_slave 			= 2'b10;
	
		#(CLK_PERIOD*2);

		tb_HWDATA_slave 		= {20'b00000000000000000000, image_width};
		tb_HADDR_slave[31:28] 		= 4'b1010;
		tb_HADDR_slave[27:4] 		= '0;
		tb_HADDR_slave[3:0] 		= 4'b0100;
		tb_HWRITE_slave 		= 1'b1;
		tb_HSIZE_slave 			= 2'b10;

		testCase = 3;

		#(CLK_PERIOD*10);
		
		tb_HWDATA_slave 		= {20'b00000000000000000000, image_height};
		tb_HADDR_slave[31:28] 		= 4'b1010;
		tb_HADDR_slave[27:4] 		= '0;
		tb_HADDR_slave[3:0] 		= 4'b1000;
		tb_HWRITE_slave 		= 1'b1;
		tb_HSIZE_slave 			= 2'b10;


		#(CLK_PERIOD*2);


		testCase = 4;

		generate_output_header_RC4(INT1_FILENAME);


		tb_output_image_RC4 = new[num_rows];
		for(r = 0; r < num_rows ; r = r + 1)
		begin
			tb_output_image_RC4[r] = new[num_cols];
		end

		while(tb_RC4_done != 1)
		begin
			if(tb_HADDR[31:28] == 4'b0101)
			begin
				if(tb_HWRITE == 0)
				begin
					send_buffer_RC4();

					@(posedge tb_clk);
					tb_HRDATA = {buffer[0],buffer[1], buffer[2], buffer[3]};

				end
				else if(tb_HWRITE == 1)
				begin		

					tb_output_image_RC4[tb_HADDR[19:0] / image_width][tb_HADDR[19:0] % image_width + 3] = tb_HWDATA[7:0];
					tb_output_image_RC4[tb_HADDR[19:0] / image_width][(tb_HADDR[19:0] % image_width)+2] = tb_HWDATA[15:8];
					tb_output_image_RC4[tb_HADDR[19:0] / image_width][(tb_HADDR[19:0] % image_width)+1] = tb_HWDATA[23:16];
					tb_output_image_RC4[tb_HADDR[19:0] / image_width][(tb_HADDR[19:0] % image_width)] = tb_HWDATA[31:24];
					
					
				end
			end
			#(CLK_PERIOD);
		end		

		dump_image_buffer_to_file_RC4(INT1_FILENAME);
		$fclose(res_file);		

		testCase = 5;

		read_input_header(INT1_FILENAME);
		extract_input_image_ED;


		generate_output_header(RESULT1_FILENAME);


		tb_output_image_ED = new[num_rows - 2];
		for(r = 0; r < num_rows - 2 ; r = r + 1)
		begin
			tb_output_image_ED[r] = new[num_cols - 2];
		end
		
		
		while(tb_process_complete != 1)
		begin

			if(tb_HADDR[31:28] == 4'b0101)
			begin
				if(tb_HWRITE == 0)
				begin
					send_buffer_ED();
					@(posedge tb_clk);
					tb_HRDATA = buffer;

				end
				else if(tb_HWRITE == 1)
				begin		

					tb_output_image_ED[(tb_HADDR % (image_height * image_width)) / (image_width - 2)][(tb_HADDR % (image_height * image_width)) % (image_width - 2)] = tb_HWDATA[7:0];
					tb_output_image_ED[(tb_HADDR % (image_height * image_width)) / (image_width - 2)][((tb_HADDR % (image_height * image_width)) % (image_width - 2)) + 1] = tb_HWDATA[15:8];
				end
			end
		#(CLK_PERIOD);
		end


		dump_image_buffer_to_file_ED(RESULT1_FILENAME);	

		
		$fclose(res_file);
		
		#(CLK_PERIOD);
		tb_n_rst = 1;
		#(CLK_PERIOD);
		tb_n_rst = 0;
		#(CLK_PERIOD);
		tb_n_rst = 1; 
		

	end


endmodule