data/full_repos/permissive/13196823/fpga/DE4_Ethernet_0/src/op_lut_process_sm.v
 line:178: before: "integer"
1: b'%Error: data/full_repos/permissive/13196823/fpga/DE4_Ethernet_0/src/op_lut_process_sm.v:38: Cannot find include file: ../command_defines.v\n`include "../command_defines.v"\n         ^~~~~~~~~~~~~~~~~~~~~~\n        ... Looked in:\n             data/full_repos/permissive/13196823/fpga/DE4_Ethernet_0/src,data/full_repos/permissive/13196823/../command_defines.v\n             data/full_repos/permissive/13196823/fpga/DE4_Ethernet_0/src,data/full_repos/permissive/13196823/../command_defines.v.v\n             data/full_repos/permissive/13196823/fpga/DE4_Ethernet_0/src,data/full_repos/permissive/13196823/../command_defines.v.sv\n             ../command_defines.v\n             ../command_defines.v.v\n             ../command_defines.v.sv\n             obj_dir/../command_defines.v\n             obj_dir/../command_defines.v.v\n             obj_dir/../command_defines.v.sv\n%Error: data/full_repos/permissive/13196823/fpga/DE4_Ethernet_0/src/op_lut_process_sm.v:362: Define or directive not defined: \'`START_LOAD\'\n       if(in_fifo_data[47:40]==`START_LOAD) begin\n                               ^~~~~~~~~~~\n%Error: data/full_repos/permissive/13196823/fpga/DE4_Ethernet_0/src/op_lut_process_sm.v:362: syntax error, unexpected \')\', expecting TYPE-IDENTIFIER\n       if(in_fifo_data[47:40]==`START_LOAD) begin\n                                          ^\n%Error: data/full_repos/permissive/13196823/fpga/DE4_Ethernet_0/src/op_lut_process_sm.v:366: Define or directive not defined: \'`LOAD_DATA\'\n       else if(in_fifo_data[47:40]==`LOAD_DATA) begin\n                                    ^~~~~~~~~~\n%Error: data/full_repos/permissive/13196823/fpga/DE4_Ethernet_0/src/op_lut_process_sm.v:366: syntax error, unexpected \')\', expecting TYPE-IDENTIFIER\n       else if(in_fifo_data[47:40]==`LOAD_DATA) begin\n                                              ^\n%Error: data/full_repos/permissive/13196823/fpga/DE4_Ethernet_0/src/op_lut_process_sm.v:369: Define or directive not defined: \'`END_LOAD\'\n       else if(in_fifo_data[47:40]==`END_LOAD) begin\n                                    ^~~~~~~~~\n%Error: data/full_repos/permissive/13196823/fpga/DE4_Ethernet_0/src/op_lut_process_sm.v:374: syntax error, unexpected else\n       else if(in_fifo_data[47:40]==`FPGA_TO_WORKER_PUT_REQUEST) begin  \n       ^~~~\n%Error: data/full_repos/permissive/13196823/fpga/DE4_Ethernet_0/src/op_lut_process_sm.v:374: Define or directive not defined: \'`FPGA_TO_WORKER_PUT_REQUEST\'\n       else if(in_fifo_data[47:40]==`FPGA_TO_WORKER_PUT_REQUEST) begin  \n                                    ^~~~~~~~~~~~~~~~~~~~~~~~~~~\n%Error: data/full_repos/permissive/13196823/fpga/DE4_Ethernet_0/src/op_lut_process_sm.v:377: Define or directive not defined: \'`WORKER_TO_FPGA_PUT_REQUEST\'\n       else if(in_fifo_data[47:40]==`WORKER_TO_FPGA_PUT_REQUEST) begin  \n                                    ^~~~~~~~~~~~~~~~~~~~~~~~~~~\n%Error: data/full_repos/permissive/13196823/fpga/DE4_Ethernet_0/src/op_lut_process_sm.v:380: Define or directive not defined: \'`START_UPDATE\'\n       else if(in_fifo_data[47:40]==`START_UPDATE) begin\n                                    ^~~~~~~~~~~~~\n%Error: data/full_repos/permissive/13196823/fpga/DE4_Ethernet_0/src/op_lut_process_sm.v:384: Define or directive not defined: \'`END_UPDATE\'\n       else if(in_fifo_data[47:40]==`END_UPDATE) begin\n                                    ^~~~~~~~~~~\n%Error: data/full_repos/permissive/13196823/fpga/DE4_Ethernet_0/src/op_lut_process_sm.v:388: Define or directive not defined: \'`START_CHECK_TERMINATE\'\n       else if(in_fifo_data[47:40]==`START_CHECK_TERMINATE) begin\n                                    ^~~~~~~~~~~~~~~~~~~~~~\n%Error: data/full_repos/permissive/13196823/fpga/DE4_Ethernet_0/src/op_lut_process_sm.v:392: Define or directive not defined: \'`START_FLUSH_DATA\'\n       else if(in_fifo_data[47:40]==`START_FLUSH_DATA) begin\n                                    ^~~~~~~~~~~~~~~~~\n%Error: data/full_repos/permissive/13196823/fpga/DE4_Ethernet_0/src/op_lut_process_sm.v:424: syntax error, unexpected \'=\', expecting IDENTIFIER\n   state_next = WAIT_PREPROCESS_RDY;\n              ^\n%Error: data/full_repos/permissive/13196823/fpga/DE4_Ethernet_0/src/op_lut_process_sm.v:428: syntax error, unexpected \'=\', expecting IDENTIFIER\n   state_next = ACCUM_KEY_VALUE;\n              ^\n%Error: data/full_repos/permissive/13196823/fpga/DE4_Ethernet_0/src/op_lut_process_sm.v:437: syntax error, unexpected \'=\', expecting IDENTIFIER\n                 state_next = WAIT_PREPROCESS_RDY;\n                            ^\n%Error: Cannot continue\n'
module op_lut_process_sm
  #(parameter DATA_WIDTH = 64,
    parameter CTRL_WIDTH = DATA_WIDTH/8,
    parameter NUM_QUEUES = 8,
    parameter NUM_QUEUES_WIDTH = log2(NUM_QUEUES),
    parameter STAGE_NUM  = 4,
    parameter IOQ_STAGE_NUM = 8'hff)
  (
   input                              in_fifo_vld,
   input [DATA_WIDTH-1:0]             in_fifo_data,
   input [CTRL_WIDTH-1:0]             in_fifo_ctrl,
   output reg                         in_fifo_rd_en,


   input                              is_arp_pkt,
   input                              is_ip_pkt,
   input                              is_for_us,
   input                              is_broadcast,
   input                              eth_parser_info_vld,
   input      [NUM_QUEUES_WIDTH-1:0]  mac_dst_port_num,


   input      [47:0]                  next_hop_mac,
   input      [NUM_QUEUES-1:0]        output_port,
   input                              arp_lookup_hit,
   input                              lpm_lookup_hit,
   input                              arp_mac_vld,


   input                              is_from_cpu,
   input      [NUM_QUEUES-1:0]        to_cpu_output_port,
   input      [NUM_QUEUES-1:0]        from_cpu_output_port,
   input                              is_from_cpu_vld,
   input      [NUM_QUEUES_WIDTH-1:0]  input_port_num,


   input                              ip_checksum_vld,
   input                              ip_checksum_is_good,
   input                              ip_hdr_has_options,
   input      [15:0]                  ip_new_checksum,
   input                              ip_ttl_is_good,
   input      [7:0]                   ip_new_ttl,


   input                              dest_ip_hit,
   input                              dest_ip_filter_vld,


   output reg                         rd_preprocess_info,


   output reg                         out_wr,
   output reg [DATA_WIDTH-1:0]        out_data,
   output reg [CTRL_WIDTH-1:0]        out_ctrl,
   input                              out_rdy,


   output reg                         pkt_sent_from_cpu,
   output reg                         pkt_sent_to_cpu_options_ver,
   output reg                         pkt_sent_to_cpu_bad_ttl,
   output reg                         pkt_sent_to_cpu_dest_ip_hit,
   output reg                         pkt_forwarded     ,
   output reg                         pkt_dropped_checksum,
   output reg                         pkt_sent_to_cpu_non_ip,
   output reg                         pkt_sent_to_cpu_arp_miss,
   output reg                         pkt_sent_to_cpu_lpm_miss,
   output reg                         pkt_dropped_wrong_dst_mac,

   input  [47:0]                      mac_0,
   input  [47:0]                      mac_1,
   input  [47:0]                      mac_2,
   input  [47:0]                      mac_3,


	input  [  9: 0] address,
   output [ 31: 0] readdata,
   input  [  3: 0]  byteenable,
   input            chipselect,
   input            write,
	input [ 31: 0] writedata,


    output  reg [63:0]   rx_ext_update_data,
    input                rx_ext_update_0_full,
    output               rx_ext_update_0_wrreq,
    input                rx_ext_update_1_full,
    output               rx_ext_update_1_wrreq,
    input                rx_ext_update_2_full,
    output               rx_ext_update_2_wrreq,
    input                rx_ext_update_3_full,
    output               rx_ext_update_3_wrreq,
    input             	 rx_ext_update_4_full,
    output               rx_ext_update_4_wrreq,
    input                rx_ext_update_5_full,
    output               rx_ext_update_5_wrreq,
    input                rx_ext_update_6_full,
    output               rx_ext_update_6_wrreq,
    input                rx_ext_update_7_full,
    output               rx_ext_update_7_wrreq,
   
output reg start_update,
   output reg flush_ddr,
   output reg check_terminate,
   output reg flush_data,
   output reg start_load,
   output reg compute_system_reset,



   output reg [63:0] dram_fifo_writedata,
   output reg        dram_fifo_write,
   input             dram_fifo_full,

   input [7:0] proc_bit_mask,


   input reset,
   input clk
   );
   
   function integer log2;
      input integer number;
      begin
         log2=0;
         while(2**log2<number) begin
            log2=log2+1;
         end
      end
   endfunction
   

   localparam NUM_STATES          = 10;
   localparam WAIT_PREPROCESS_RDY = 1;
   localparam MOVE_MODULE_HDRS    = 2;
   localparam SEND_SRC_MAC_LO     = 4;
   localparam SEND_IP_TTL         = 8;
   localparam SEND_IP_CHECKSUM    = 16;
   localparam MOVE_UDP            = 32;
   localparam INTERPRET_COMMAND   = 64;
   localparam LOAD_KEY_VALUE	  = 128;
   localparam ACCUM_KEY_VALUE     = 256;
   localparam DROP_PKT            = 512;


   wire                 preprocess_vld;
   
   reg [NUM_STATES-1:0] state;
   reg [NUM_STATES-1:0] state_next;
   reg [DATA_WIDTH-1:0] out_data_next;
   reg [CTRL_WIDTH-1:0] out_ctrl_next;
   reg                  out_wr_next;
   reg                  ctrl_prev_is_0;
   wire                 eop;

   reg [47:0]           src_mac_sel;

   reg [NUM_QUEUES-1:0] dst_port;
   reg [NUM_QUEUES-1:0] dst_port_next;

   reg                  to_from_cpu;
   reg                  to_from_cpu_next;

   reg [63:0] rx_ext_update_data_next;
   reg [7:0]  rx_ext_update_wrreq, rx_ext_update_wrreq_next;
   wire [7:0] rx_ext_update_full;  
 
   reg [1:0] command, command_next;
   reg start_update_next;
   reg flush_ddr_next;
   reg start_load_next;
   reg check_terminate_next;
   reg flush_data_next;
   wire is_safe_to_write_packet;
   reg compute_system_reset_next;

   reg [63:0] dram_fifo_writedata_next;
   reg        dram_fifo_write_next;
   reg	[4:0] rx_ext_fifo_sel, rx_ext_fifo_sel_next;
   wire [4:0] next_external_fifo;

   reg [31:0] packet_rcv;
   reg [31:0] packet_rcv_next;

   assign rx_ext_update_0_wrreq = rx_ext_update_wrreq[0];
   assign rx_ext_update_1_wrreq = rx_ext_update_wrreq[1];
   assign rx_ext_update_2_wrreq = rx_ext_update_wrreq[2];
   assign rx_ext_update_3_wrreq = rx_ext_update_wrreq[3];
   assign rx_ext_update_4_wrreq = rx_ext_update_wrreq[4];
   assign rx_ext_update_5_wrreq = rx_ext_update_wrreq[5];
   assign rx_ext_update_6_wrreq = rx_ext_update_wrreq[6];
   assign rx_ext_update_7_wrreq = rx_ext_update_wrreq[7];

   assign  rx_ext_update_full[0] = rx_ext_update_0_full;
   assign  rx_ext_update_full[1] = rx_ext_update_1_full;
   assign  rx_ext_update_full[2] = rx_ext_update_2_full;
   assign  rx_ext_update_full[3] = rx_ext_update_3_full;
   assign  rx_ext_update_full[4] = rx_ext_update_4_full;
   assign  rx_ext_update_full[5] = rx_ext_update_5_full;
   assign  rx_ext_update_full[6] = rx_ext_update_6_full;
   assign  rx_ext_update_full[7] = rx_ext_update_7_full;

   reg [3:0] ext_start;

   always@(*) begin
	case(proc_bit_mask)
		8'h00: ext_start = 0;
		8'h01: ext_start = 1;
		8'h03: ext_start = 2;
		8'h07: ext_start = 3;
		8'h0f: ext_start = 4;
		8'h1f: ext_start = 5;
		8'h3f: ext_start = 6;
		8'h7f: ext_start = 7;
		8'hff: ext_start = 0;
		default: ext_start = 0;	
	endcase
   end

   localparam INVALID=0;
   localparam LOAD=1;
   localparam ACCUMULATE=2;
	


   assign preprocess_vld = eth_parser_info_vld & ip_checksum_vld;

   assign eop = (ctrl_prev_is_0 && (in_fifo_ctrl!=0));


   always @(*) begin
      case(output_port)
        'h1: src_mac_sel       = mac_0;
        'h4: src_mac_sel       = mac_1;
        'h10: src_mac_sel      = mac_2;
        'h40: src_mac_sel      = mac_3;
        default: src_mac_sel   = mac_0;
      endcase
   end
        

   always @(*) begin
      rd_preprocess_info            = 0;
      state_next                    = state;
      in_fifo_rd_en                 = 0;
      pkt_dropped_wrong_dst_mac     = 0;
      
      rx_ext_update_data_next = rx_ext_update_data;
      rx_ext_update_wrreq_next = 1'b0;
   
      command_next = command; 

      start_update_next = start_update;
      flush_ddr_next = 1'b0;
      start_load_next = start_load;       
      check_terminate_next = 1'b0;
      flush_data_next = 1'b0;
      dram_fifo_writedata_next = dram_fifo_writedata;
      dram_fifo_write_next = 1'b0;
      compute_system_reset_next = 1'b0;
      rx_ext_fifo_sel_next = rx_ext_fifo_sel;
      packet_rcv_next = packet_rcv;

      case(state)
        WAIT_PREPROCESS_RDY: begin
           if(preprocess_vld) begin

		if(is_for_us && is_ip_pkt) begin
			state_next         = MOVE_MODULE_HDRS;
			packet_rcv_next = packet_rcv+1;
	         end
	         else begin
	                pkt_dropped_wrong_dst_mac   = 1;
        	        rd_preprocess_info          = 1;
                	in_fifo_rd_en               = 1;
	                state_next                  = DROP_PKT;
        	end
	   end
        end

        MOVE_MODULE_HDRS: begin
           if(in_fifo_vld) begin
              in_fifo_rd_en    = 1;
              if(in_fifo_ctrl==0) begin
                 state_next    = SEND_SRC_MAC_LO;
              end
           end
        end

        SEND_SRC_MAC_LO: begin
           if(in_fifo_vld) begin
              in_fifo_rd_en   = 1;
              state_next      = SEND_IP_TTL;
           end
        end

        SEND_IP_TTL: begin
			  
           if(in_fifo_vld) begin
              in_fifo_rd_en   = 1;
              state_next      = SEND_IP_CHECKSUM;
           end
        end

        SEND_IP_CHECKSUM: begin
           if(in_fifo_vld) begin
              in_fifo_rd_en        = 1;
              rd_preprocess_info   = 1;
              state_next           = MOVE_UDP;
           end
        end
	
	MOVE_UDP: begin
	   if(in_fifo_vld) begin
              in_fifo_rd_en        = 1;
              state_next           = INTERPRET_COMMAND;
	   end
	end
     	
	INTERPRET_COMMAND: begin
	   if(in_fifo_vld) begin
              in_fifo_rd_en        = 1;

	      if(in_fifo_data[47:40]==`START_LOAD) begin
	 	 start_load_next = 1'b1;
		 state_next = DROP_PKT;		
	      end
	      else if(in_fifo_data[47:40]==`LOAD_DATA) begin
		 state_next = LOAD_KEY_VALUE;
	      end
	      else if(in_fifo_data[47:40]==`END_LOAD) begin
	         start_load_next = 1'b0;
		 compute_system_reset_next = 1'b1;
		 state_next = DROP_PKT;
	      end
	      else if(in_fifo_data[47:40]==`FPGA_TO_WORKER_PUT_REQUEST) begin
		  state_next = ACCUM_KEY_VALUE;
	      end
	      else if(in_fifo_data[47:40]==`WORKER_TO_FPGA_PUT_REQUEST) begin
		  state_next = ACCUM_KEY_VALUE;
	      end
	      else if(in_fifo_data[47:40]==`START_UPDATE) begin
		  start_update_next = 1'b1;
		  state_next = DROP_PKT;
	      end
	      else if(in_fifo_data[47:40]==`END_UPDATE) begin
		  start_update_next = 1'b0;
		  state_next = DROP_PKT;
	      end
	      else if(in_fifo_data[47:40]==`START_CHECK_TERMINATE) begin
		  check_terminate_next = 1'b1;
		  state_next = DROP_PKT;	
	      end
	      else if(in_fifo_data[47:40]==`START_FLUSH_DATA) begin
		  flush_ddr_next = 1'b1;
		  state_next = DROP_PKT;	
	      end
	      else begin
              	  state_next   = DROP_PKT;
	      end
	   end
	end

	LOAD_KEY_VALUE: begin
           if(in_fifo_vld && (!dram_fifo_full)) begin
		dram_fifo_writedata_next = in_fifo_data[63:0];
		dram_fifo_write_next = 1'b1;
		state_next = (eop)?WAIT_PREPROCESS_RDY:LOAD_KEY_VALUE;
              	in_fifo_rd_en   = (eop)?0:1;
	    end
        end
	
	ACCUM_KEY_VALUE: begin
	   if(in_fifo_vld) begin



		if((|in_fifo_data)&&(!rx_ext_update_full[rx_ext_fifo_sel])) begin 
	      		rx_ext_update_data_next  = in_fifo_data[63:0];
			rx_ext_update_wrreq_next[rx_ext_fifo_sel] = 1'b1;
			rx_ext_fifo_sel_next = ((rx_ext_fifo_sel+1)==8)?ext_start:(rx_ext_fifo_sel+1);
		end

        	in_fifo_rd_en = 1'b1;
		if(eop) begin
			state_next = WAIT_PREPROCESS_RDY;
		end 
		else begin

			state_next = ACCUM_KEY_VALUE;
		end
	   end
	end

        DROP_PKT: begin
           if(in_fifo_vld) begin
              in_fifo_rd_en = 1;
              if(eop) begin
                 state_next = WAIT_PREPROCESS_RDY;
              end
           end
        end
      endcase
   end

   always @(posedge clk or posedge reset) begin
      if(reset) begin
         state             		<= WAIT_PREPROCESS_RDY;
         ctrl_prev_is_0    		<= 0;
         rx_ext_update_data 		<= 0;
         rx_ext_update_wrreq 		<= 0;
	 start_update 			<= 0;
	 start_load 			<= 0;
	 check_terminate 		<= 0;
	 flush_ddr 			<= 0;
         dram_fifo_writedata 		<= 0;
         dram_fifo_write 		<= 0;
	 compute_system_reset	 	<= 0;
         rx_ext_fifo_sel 		<= 0;
	 packet_rcv			<= 0;
      end
      else begin
         state             		<= state_next;
         ctrl_prev_is_0    		<= in_fifo_rd_en ? (in_fifo_ctrl==0) : ctrl_prev_is_0;
         rx_ext_update_data 		<= rx_ext_update_data_next;
         rx_ext_update_wrreq 		<= rx_ext_update_wrreq_next;
	 start_update 			<= start_update_next;
	 start_load 			<= start_load_next;
	 check_terminate 		<= check_terminate_next;
	 flush_ddr 			<= flush_ddr_next;
         dram_fifo_writedata 		<= dram_fifo_writedata_next;
         dram_fifo_write 		<= dram_fifo_write_next;
	 compute_system_reset	 	<= compute_system_reset_next;
         rx_ext_fifo_sel 		<= (rx_ext_fifo_sel_next<ext_start)?ext_start:rx_ext_fifo_sel_next;
	 packet_rcv			<= packet_rcv_next;
      end
   end

endmodule
module op_lut_process_sm
  #(parameter DATA_WIDTH = 64,
    parameter CTRL_WIDTH = DATA_WIDTH/8,
    parameter NUM_QUEUES = 8,
    parameter NUM_QUEUES_WIDTH = log2(NUM_QUEUES),
    parameter STAGE_NUM  = 4,
    parameter IOQ_STAGE_NUM = 8'hff)
  (
   input                              in_fifo_vld,
   input [DATA_WIDTH-1:0]             in_fifo_data,
   input [CTRL_WIDTH-1:0]             in_fifo_ctrl,
   output reg                         in_fifo_rd_en,


   input                              is_arp_pkt,
   input                              is_ip_pkt,
   input                              is_for_us,
   input                              is_broadcast,
   input                              eth_parser_info_vld,
   input      [NUM_QUEUES_WIDTH-1:0]  mac_dst_port_num,


   input      [47:0]                  next_hop_mac,
   input      [NUM_QUEUES-1:0]        output_port,
   input                              arp_lookup_hit,
   input                              lpm_lookup_hit,
   input                              arp_mac_vld,


   input                              is_from_cpu,
   input      [NUM_QUEUES-1:0]        to_cpu_output_port,
   input      [NUM_QUEUES-1:0]        from_cpu_output_port,
   input                              is_from_cpu_vld,
   input      [NUM_QUEUES_WIDTH-1:0]  input_port_num,


   input                              ip_checksum_vld,
   input                              ip_checksum_is_good,
   input                              ip_hdr_has_options,
   input      [15:0]                  ip_new_checksum,
   input                              ip_ttl_is_good,
   input      [7:0]                   ip_new_ttl,


   input                              dest_ip_hit,
   input                              dest_ip_filter_vld,


   output reg                         rd_preprocess_info,


   output reg                         out_wr,
   output reg [DATA_WIDTH-1:0]        out_data,
   output reg [CTRL_WIDTH-1:0]        out_ctrl,
   input                              out_rdy,


   output reg                         pkt_sent_from_cpu,
   output reg                         pkt_sent_to_cpu_options_ver,
   output reg                         pkt_sent_to_cpu_bad_ttl,
   output reg                         pkt_sent_to_cpu_dest_ip_hit,
   output reg                         pkt_forwarded     ,
   output reg                         pkt_dropped_checksum,
   output reg                         pkt_sent_to_cpu_non_ip,
   output reg                         pkt_sent_to_cpu_arp_miss,
   output reg                         pkt_sent_to_cpu_lpm_miss,
   output reg                         pkt_dropped_wrong_dst_mac,

   input  [47:0]                      mac_0,
   input  [47:0]                      mac_1,
   input  [47:0]                      mac_2,
   input  [47:0]                      mac_3,


	input  [  9: 0] address,
   output [ 31: 0] readdata,
   input  [  3: 0]  byteenable,
   input            chipselect,
   input            write,
	input [ 31: 0] writedata,


    output  reg [63:0]   rx_ext_update_data,
    input                rx_ext_update_0_full,
    output               rx_ext_update_0_wrreq,
    input                rx_ext_update_1_full,
    output               rx_ext_update_1_wrreq,
    input                rx_ext_update_2_full,
    output               rx_ext_update_2_wrreq,
    input                rx_ext_update_3_full,
    output               rx_ext_update_3_wrreq,
    input             	 rx_ext_update_4_full,
    output               rx_ext_update_4_wrreq,
    input                rx_ext_update_5_full,
    output               rx_ext_update_5_wrreq,
    input                rx_ext_update_6_full,
    output               rx_ext_update_6_wrreq,
    input                rx_ext_update_7_full,
    output               rx_ext_update_7_wrreq,
   
output reg start_update,
   output reg flush_ddr,
   output reg check_terminate,
   output reg flush_data,
   output reg start_load,
   output reg compute_system_reset,



   output reg [63:0] dram_fifo_writedata,
   output reg        dram_fifo_write,
   input             dram_fifo_full,

   input [7:0] proc_bit_mask,


   input reset,
   input clk
   );

   
   function integer log2;
      input integer number;
      begin
         log2=0;
         while(2**log2<number) begin
            log2=log2+1;
         end
      end
   endfunction
   

   localparam NUM_STATES          = 10;
   localparam WAIT_PREPROCESS_RDY = 1;
   localparam MOVE_MODULE_HDRS    = 2;
   localparam SEND_SRC_MAC_LO     = 4;
   localparam SEND_IP_TTL         = 8;
   localparam SEND_IP_CHECKSUM    = 16;
   localparam MOVE_UDP            = 32;
   localparam INTERPRET_COMMAND   = 64;
   localparam LOAD_KEY_VALUE	  = 128;
   localparam ACCUM_KEY_VALUE     = 256;
   localparam DROP_PKT            = 512;


   wire                 preprocess_vld;
   
   reg [NUM_STATES-1:0] state;
   reg [NUM_STATES-1:0] state_next;
   reg [DATA_WIDTH-1:0] out_data_next;
   reg [CTRL_WIDTH-1:0] out_ctrl_next;
   reg                  out_wr_next;
   reg                  ctrl_prev_is_0;
   wire                 eop;

   reg [47:0]           src_mac_sel;

   reg [NUM_QUEUES-1:0] dst_port;
   reg [NUM_QUEUES-1:0] dst_port_next;

   reg                  to_from_cpu;
   reg                  to_from_cpu_next;

   reg [63:0] rx_ext_update_data_next;
   reg [7:0]  rx_ext_update_wrreq, rx_ext_update_wrreq_next;
   wire [7:0] rx_ext_update_full;  
 
   reg [1:0] command, command_next;
   reg start_update_next;
   reg flush_ddr_next;
   reg start_load_next;
   reg check_terminate_next;
   reg flush_data_next;
   wire is_safe_to_write_packet;
   reg compute_system_reset_next;

   reg [63:0] dram_fifo_writedata_next;
   reg        dram_fifo_write_next;
   reg	[4:0] rx_ext_fifo_sel, rx_ext_fifo_sel_next;
   wire [4:0] next_external_fifo;

   reg [31:0] packet_rcv;
   reg [31:0] packet_rcv_next;

   assign rx_ext_update_0_wrreq = rx_ext_update_wrreq[0];
   assign rx_ext_update_1_wrreq = rx_ext_update_wrreq[1];
   assign rx_ext_update_2_wrreq = rx_ext_update_wrreq[2];
   assign rx_ext_update_3_wrreq = rx_ext_update_wrreq[3];
   assign rx_ext_update_4_wrreq = rx_ext_update_wrreq[4];
   assign rx_ext_update_5_wrreq = rx_ext_update_wrreq[5];
   assign rx_ext_update_6_wrreq = rx_ext_update_wrreq[6];
   assign rx_ext_update_7_wrreq = rx_ext_update_wrreq[7];

   assign  rx_ext_update_full[0] = rx_ext_update_0_full;
   assign  rx_ext_update_full[1] = rx_ext_update_1_full;
   assign  rx_ext_update_full[2] = rx_ext_update_2_full;
   assign  rx_ext_update_full[3] = rx_ext_update_3_full;
   assign  rx_ext_update_full[4] = rx_ext_update_4_full;
   assign  rx_ext_update_full[5] = rx_ext_update_5_full;
   assign  rx_ext_update_full[6] = rx_ext_update_6_full;
   assign  rx_ext_update_full[7] = rx_ext_update_7_full;

   reg [3:0] ext_start;

   always@(*) begin
	case(proc_bit_mask)
		8'h00: ext_start = 0;
		8'h01: ext_start = 1;
		8'h03: ext_start = 2;
		8'h07: ext_start = 3;
		8'h0f: ext_start = 4;
		8'h1f: ext_start = 5;
		8'h3f: ext_start = 6;
		8'h7f: ext_start = 7;
		8'hff: ext_start = 0;
		default: ext_start = 0;	
	endcase
   end

   localparam INVALID=0;
   localparam LOAD=1;
   localparam ACCUMULATE=2;
	


   assign preprocess_vld = eth_parser_info_vld & ip_checksum_vld;

   assign eop = (ctrl_prev_is_0 && (in_fifo_ctrl!=0));


   always @(*) begin
      case(output_port)
        'h1: src_mac_sel       = mac_0;
        'h4: src_mac_sel       = mac_1;
        'h10: src_mac_sel      = mac_2;
        'h40: src_mac_sel      = mac_3;
        default: src_mac_sel   = mac_0;
      endcase
   end
        

   always @(*) begin
      rd_preprocess_info            = 0;
      state_next                    = state;
      in_fifo_rd_en                 = 0;
      pkt_dropped_wrong_dst_mac     = 0;
      
      rx_ext_update_data_next = rx_ext_update_data;
      rx_ext_update_wrreq_next = 1'b0;
   
      command_next = command; 

      start_update_next = start_update;
      flush_ddr_next = 1'b0;
      start_load_next = start_load;       
      check_terminate_next = 1'b0;
      flush_data_next = 1'b0;
      dram_fifo_writedata_next = dram_fifo_writedata;
      dram_fifo_write_next = 1'b0;
      compute_system_reset_next = 1'b0;
      rx_ext_fifo_sel_next = rx_ext_fifo_sel;
      packet_rcv_next = packet_rcv;

      case(state)
        WAIT_PREPROCESS_RDY: begin
           if(preprocess_vld) begin

		if(is_for_us && is_ip_pkt) begin
			state_next         = MOVE_MODULE_HDRS;
			packet_rcv_next = packet_rcv+1;
	         end
	         else begin
	                pkt_dropped_wrong_dst_mac   = 1;
        	        rd_preprocess_info          = 1;
                	in_fifo_rd_en               = 1;
	                state_next                  = DROP_PKT;
        	end
	   end
        end

        MOVE_MODULE_HDRS: begin
           if(in_fifo_vld) begin
              in_fifo_rd_en    = 1;
              if(in_fifo_ctrl==0) begin
                 state_next    = SEND_SRC_MAC_LO;
              end
           end
        end

        SEND_SRC_MAC_LO: begin
           if(in_fifo_vld) begin
              in_fifo_rd_en   = 1;
              state_next      = SEND_IP_TTL;
           end
        end

        SEND_IP_TTL: begin
			  
           if(in_fifo_vld) begin
              in_fifo_rd_en   = 1;
              state_next      = SEND_IP_CHECKSUM;
           end
        end

        SEND_IP_CHECKSUM: begin
           if(in_fifo_vld) begin
              in_fifo_rd_en        = 1;
              rd_preprocess_info   = 1;
              state_next           = MOVE_UDP;
           end
        end
	
	MOVE_UDP: begin
	   if(in_fifo_vld) begin
              in_fifo_rd_en        = 1;
              state_next           = INTERPRET_COMMAND;
	   end
	end
     	
	INTERPRET_COMMAND: begin
	   if(in_fifo_vld) begin
              in_fifo_rd_en        = 1;

	      if(in_fifo_data[47:40]==`START_LOAD) begin
	 	 start_load_next = 1'b1;
		 state_next = DROP_PKT;		
	      end
	      else if(in_fifo_data[47:40]==`LOAD_DATA) begin
		 state_next = LOAD_KEY_VALUE;
	      end
	      else if(in_fifo_data[47:40]==`END_LOAD) begin
	         start_load_next = 1'b0;
		 compute_system_reset_next = 1'b1;
		 state_next = DROP_PKT;
	      end
	      else if(in_fifo_data[47:40]==`FPGA_TO_WORKER_PUT_REQUEST) begin
		  state_next = ACCUM_KEY_VALUE;
	      end
	      else if(in_fifo_data[47:40]==`WORKER_TO_FPGA_PUT_REQUEST) begin
		  state_next = ACCUM_KEY_VALUE;
	      end
	      else if(in_fifo_data[47:40]==`START_UPDATE) begin
		  start_update_next = 1'b1;
		  state_next = DROP_PKT;
	      end
	      else if(in_fifo_data[47:40]==`END_UPDATE) begin
		  start_update_next = 1'b0;
		  state_next = DROP_PKT;
	      end
	      else if(in_fifo_data[47:40]==`START_CHECK_TERMINATE) begin
		  check_terminate_next = 1'b1;
		  state_next = DROP_PKT;	
	      end
	      else if(in_fifo_data[47:40]==`START_FLUSH_DATA) begin
		  flush_ddr_next = 1'b1;
		  state_next = DROP_PKT;	
	      end
	      else begin
              	  state_next   = DROP_PKT;
	      end
	   end
	end

	LOAD_KEY_VALUE: begin
           if(in_fifo_vld && (!dram_fifo_full)) begin
		dram_fifo_writedata_next = in_fifo_data[63:0];
		dram_fifo_write_next = 1'b1;
		state_next = (eop)?WAIT_PREPROCESS_RDY:LOAD_KEY_VALUE;
              	in_fifo_rd_en   = (eop)?0:1;
	    end
        end
	
	ACCUM_KEY_VALUE: begin
	   if(in_fifo_vld) begin



		if((|in_fifo_data)&&(!rx_ext_update_full[rx_ext_fifo_sel])) begin 
	      		rx_ext_update_data_next  = in_fifo_data[63:0];
			rx_ext_update_wrreq_next[rx_ext_fifo_sel] = 1'b1;
			rx_ext_fifo_sel_next = ((rx_ext_fifo_sel+1)==8)?ext_start:(rx_ext_fifo_sel+1);
		end

        	in_fifo_rd_en = 1'b1;
		if(eop) begin
			state_next = WAIT_PREPROCESS_RDY;
		end 
		else begin

			state_next = ACCUM_KEY_VALUE;
		end
	   end
	end

        DROP_PKT: begin
           if(in_fifo_vld) begin
              in_fifo_rd_en = 1;
              if(eop) begin
                 state_next = WAIT_PREPROCESS_RDY;
              end
           end
        end
      endcase
   end

   always @(posedge clk or posedge reset) begin
      if(reset) begin
         state             		<= WAIT_PREPROCESS_RDY;
         ctrl_prev_is_0    		<= 0;
         rx_ext_update_data 		<= 0;
         rx_ext_update_wrreq 		<= 0;
	 start_update 			<= 0;
	 start_load 			<= 0;
	 check_terminate 		<= 0;
	 flush_ddr 			<= 0;
         dram_fifo_writedata 		<= 0;
         dram_fifo_write 		<= 0;
	 compute_system_reset	 	<= 0;
         rx_ext_fifo_sel 		<= 0;
	 packet_rcv			<= 0;
      end
      else begin
         state             		<= state_next;
         ctrl_prev_is_0    		<= in_fifo_rd_en ? (in_fifo_ctrl==0) : ctrl_prev_is_0;
         rx_ext_update_data 		<= rx_ext_update_data_next;
         rx_ext_update_wrreq 		<= rx_ext_update_wrreq_next;
	 start_update 			<= start_update_next;
	 start_load 			<= start_load_next;
	 check_terminate 		<= check_terminate_next;
	 flush_ddr 			<= flush_ddr_next;
         dram_fifo_writedata 		<= dram_fifo_writedata_next;
         dram_fifo_write 		<= dram_fifo_write_next;
	 compute_system_reset	 	<= compute_system_reset_next;
         rx_ext_fifo_sel 		<= (rx_ext_fifo_sel_next<ext_start)?ext_start:rx_ext_fifo_sel_next;
	 packet_rcv			<= packet_rcv_next;
      end
   end

endmodule