`timescale 1ns/1ps

module tb_FIX_processor();


logic		i_clk;
logic		i_rst;
logic		i_validpkt;
logic[2:0]	i_empty;
logic		i_startofpkt;
logic		i_endofpkt;
logic[63:0]	i_pkt;
logic		i_ready_backend;
logic		i_validpkt_down;
logic[2:0]	i_empty_down;
logic		i_startofpkt_down;
logic		i_endofpkt_down;
logic[63:0]	i_pkt_down;
logic		i_ready_backend_down;

logic		o_ready_pktz;
logic		o_valid_pktz;
logic[2:0]	o_empty;
logic		o_startofpkt;
logic		o_endofpkt;
logic[63:0]	o_data;
logic		o_ready_pktz_down;
logic		o_valid_pktz_down;
logic[2:0]	o_empty_down;
logic		o_startofpkt_down;
logic		o_endofpkt_down;
logic[63:0]	o_data_down;

integer counter4=0;
integer counter5=0;
integer counter6=0;
integer counter7=0;
integer counter8=0;
integer counter9=0;
integer m0,m1,m2,m3,m4,r0,r1,r2,r3,r4;
integer i,j,k,m,n,q;

//logic [7:0] message0 [0:167];
logic [7:0] message1 [0:223];
//logic [7:0] message2 [0:223];
logic [7:0] message8 [0:207];
logic [7:0] message3 [0:239];

//logic [63:0] message4 [0:20];
logic [63:0] message5 [0:27];
//logic [63:0] message6 [0:27];
logic [63:0] message7 [0:29];
logic [63:0] message9 [0:25];


FIX_processor FIX_processor_1(.i_clk(i_clk), .i_rst(i_rst), .i_valid_pkt(i_validpkt), .i_startofpkt(i_startofpkt), .i_endofpkt(i_endofpkt), .i_pkt(i_pkt), .i_ready_backend(i_ready_backend), .i_empty(i_empty),  .i_valid_pkt_down(i_validpkt_down), .i_startofpkt_down(i_startofpkt_down), .i_endofpkt_down(i_endofpkt_down), .i_pkt_down(i_pkt_down), .i_ready_backend_down(i_ready_backend_down), .i_empty_down(i_empty_down), .o_ready_pktz(o_ready_pktz), .o_valid_pktz(o_valid_pktz), .o_startofpkt(startofpkt), .o_endofpkt(o_endofpkt), .o_data(o_data), .o_empty(o_empty), .o_ready_pktz_down(o_ready_pktz_down), .o_valid_pktz_down(o_valid_pktz_down), .o_startofpkt_down(o_startofpkt_down), .o_endofpkt_down(o_endofpkt_down), .o_data_down(o_data_down), .o_empty_down(o_empty_down));

	initial
	begin
		i_clk = 0;
		i_rst = 0;
		i_validpkt = 0;
		i_startofpkt = 0;
		i_endofpkt = 0;
		i_pkt=64'd0;
		i_validpkt_down = 0;
		i_startofpkt_down = 0;
		i_endofpkt_down = 0;
		i_pkt_down=64'd0;
		i_empty=3'd0;
		i_empty_down=3'd0;
		counter4=0;
		counter5=0;
		counter6=0;

		/*m0 = $fopen("nrs0.txt","r");
		for (i = 0 ; i < 168; i++) begin
			r0=$fscanf(m0,"%c",message0[i]);
		end*/
		m1 = $fopen("nrs1.txt","r");
		for (j = 0 ; j < 224; j++) begin
			r1=$fscanf(m1,"%c",message1[j]);	
		end
		
		/*m2 = $fopen("nrs2.txt","r");
		for (k = 0 ; k < 224; k++) begin
			r2=$fscanf(m2,"%c",message2[k]);	
		end*/
		
		m3 = $fopen("execreport1.txt","r");
		for (k = 0 ; k < 240; k++) begin
			r3=$fscanf(m3,"%c",message3[k]);	
		end

		m4 = $fopen("execreport0.txt","r");
		for (k = 0 ; k < 209; k++) begin
			r4=$fscanf(m4,"%c",message8[k]);	
		end

		/*for (m = 0; m < 168; m=m+8) begin
			message4[m/8] = {message0[m],message0[m+1],message0[m+2],message0[m+3],message0[m+4],message0[m+5],message0[m+6],message0[m+7]};
		end*/
		for (n = 0; n < 224; n=n+8) begin
			message5[n/8] = {message1[n],message1[n+1],message1[n+2],message1[n+3],message1[n+4],message1[n+5],message1[n+6],message1[n+7]};
		end
		/*for (q = 0; q < 224; q=q+8) begin
			message6[q/8] = {message2[q],message2[q+1],message2[q+2],message2[q+3],message2[q+4],message2[q+5],message2[q+6],message2[q+7]};
		end*/

		for (q = 0; q < 240; q=q+8) begin
			message7[q/8] = {message3[q],message3[q+1],message3[q+2],message3[q+3],message3[q+4],message3[q+5],message3[q+6],message3[q+7]};
		end
		for (q = 0; q < 209; q=q+8) begin
			message9[q/8] = {message8[q],message8[q+1],message8[q+2],message8[q+3],message8[q+4],message8[q+5],message8[q+6],message3[q+7]};
		end

		#1 i_rst = 1;

		#50 i_rst=0;
		#100 i_ready_backend=1;i_ready_backend_down=1;
		
		
		
		
	end

always #25 i_clk = ~i_clk;

	always_ff@(posedge i_clk) begin
		if(i_rst)begin
			i_clk <= 0;
			i_rst <= 0;
			i_validpkt <= 0;
			i_startofpkt <= 0;
			i_endofpkt <= 0;
			i_pkt<=64'd0;
			i_validpkt_down <= 0;
			i_startofpkt_down <= 0;
			i_endofpkt_down <= 0;
			i_pkt_down<=64'd0;
			i_empty<=3'd0;
			i_empty_down<=3'd0;
			counter4<=0;
			counter5<=0;
			counter6<=0;

		end
		else begin
			if(o_ready_pktz==1) begin

				if(counter4==0) begin
					i_startofpkt<=1;
					i_validpkt<=1;
					i_pkt<=message5[counter4];
					counter4<=counter4+1;
				end
				else if(counter4<27) begin
					i_startofpkt<=0;
					i_validpkt<=1;
					i_pkt<=message5[counter4];
					counter4<=counter4+1;
				end
				else if(counter4==27) begin
					i_endofpkt<=1;
					i_validpkt<=1;
					i_pkt<=message5[counter4];
					counter4<=counter4+1;
					i_empty<=3'd3;
				end
				
				else if(counter5==0) begin
					i_empty<=3'd0;
					i_endofpkt<=0;
					i_startofpkt<=1;
					i_validpkt<=1;
					i_pkt<=message5[counter5];
					counter5<=counter5+1;
				end
				else if(counter5<27) begin
					i_startofpkt<=0;
					i_validpkt<=1;
					i_pkt<=message5[counter5];
					counter5<=counter5+1;
				end
				else if(counter5==27) begin
					i_endofpkt<=1;
					i_validpkt<=1;
					i_pkt<=message5[counter5];
					counter5<=counter5+1;
					i_empty<=3'd3;
				end
			       
				else if(counter6==0) begin
					i_empty<=3'd0;
					i_endofpkt<=0;
					i_startofpkt<=1;
					i_validpkt<=1;
					i_pkt<=message5[counter6];
					counter6<=counter6+1;
				end
				else if(counter6<27) begin
					i_startofpkt<=0;
					i_validpkt<=1;
					i_pkt<=message5[counter6];
					counter6<=counter6+1;
				end
				else if(counter6==27) begin
					i_endofpkt<=1;
					i_validpkt<=1;
					i_pkt<=message5[counter6];
					counter6<=counter6+1;
					i_empty<=3'd3;
				end
				else if(counter7<100) begin
					i_empty<=3'd0;
					i_endofpkt<=0;
					i_validpkt<=0;
					i_pkt<=64'd0;

					counter7<=counter7+1;

				end
				/*else if(counter7==100) begin

					i_startofpkt_down<=1;
					i_validpkt_down<=1;
					i_pkt_down<=message9[counter7-100];

					counter7<=counter7+1;
				end
				else if(counter7<125) begin
					i_startofpkt_down<=0;
					i_validpkt_down<=1;
					i_pkt_down<=message9[counter7-100];
			
					counter7<=counter7+1;
					
				end
				else if(counter7==125) begin
					i_endofpkt_down<=1;
					i_validpkt_down<=1;
					i_empty_down<=3'd0;
					i_pkt_down<=message9[counter7-100];
					i_empty_down<=64'd0;
					counter7<=counter7+1;
				end
				*/
				
				else if(counter8==0) begin
					i_empty_down<=3'd0;
					i_endofpkt_down<=0;
					
					i_startofpkt_down<=1;
					i_validpkt_down<=1;
					i_pkt_down<=message7[counter8];
					counter8<=counter8+1;
				end

				else if(counter8<29) begin
					i_startofpkt_down<=0;
					i_validpkt_down<=1;
					i_pkt_down<=message7[counter8];
			
					counter8<=counter8+1;
					
				end
				else if(counter8==29) begin
					i_endofpkt_down<=1;
					i_validpkt_down<=1;
					i_empty_down<=3'd5;
					i_pkt_down<=message7[counter8];
					i_empty_down<=64'd0;
					counter8<=counter8+1;
				end
				
				else if(counter9<100)begin
					i_empty_down<=3'd0;
					i_endofpkt_down<=0;
					i_validpkt_down<=0;
					counter9<=counter9+1;
				end
				
				else if(counter9==100) begin
					
					i_startofpkt<=1;
					i_validpkt<=1;
					i_pkt<=message5[counter9-100];
					counter9<=counter9+1;
				end
				else if(counter9<127) begin
					i_startofpkt<=0;
					i_validpkt<=1;
					i_pkt<=message5[counter9-100];
					counter9<=counter9+1;
				end
				else if(counter9==127) begin
					i_endofpkt<=1;
					i_validpkt<=1;
					i_pkt<=message5[counter9-100];
					counter9<=counter9+1;
					i_empty<=3'd3;
				end
			       
				
				


				else  	begin
				i_empty<=3'd0;
				i_endofpkt<=0;
				i_validpkt<=0;
				end
			end	

		end
end


endmodule
