data/full_repos/permissive/115771353/rtl/hworb/arp_reply_rx.v
data/verilator_xmls/7d34879a-698f-4c48-b6dd-c696a5975447.xml
module arp_reply_rx #(
  parameter [31:0] DEVICE_IP      = 32'h0a0105dd,
  parameter [47:0] DEVICE_MAC     = 48'h001999cf956f
)
(
  input              clk,
  input              reset_n,
  input              newFrame,
  input              frameType,
  input              newFrameByte,
  input [7:0]        frameData,
  input              frameValid,
  input              ARPSendAvail,
  input [31:0]       requestIP,
  output reg         genARPRep,
  output reg  [31:0] genARPIP,
  output reg  [47:0] lookupMAC,
  output reg         validEntry
);


  parameter [1:0]
	RX_ARP_IDLE    	           = 2'd0,
	RX_ARP_HANDLEARP   	       = 2'd1,
	RX_ARP_OPERATE   	       = 2'd2,
	RX_ARP_CHECKVALID  	       = 2'd3;
  
  reg [1:0]  arp_rep_rx_cur_fsm;
  reg [1:0]  arp_rep_rx_nxt_fsm;

  reg [4:0]  cnt;
  reg        incCnt;
  reg        rstCnt;

  reg        latchFrameData;
  reg [7:0]  frameDataLatch;
  reg        shiftSourceIPIn;
  reg [31:0] sourceIP;
  reg        shiftSourceMACIn;
  reg [47:0] sourceMAC;

  reg        ARPOperation;
  reg        determineOperation;

  reg        updateARPTable;
  reg [31:0] ARPEntryIP;
  reg [47:0] ARPEntryMAC;
  reg [31:0] ARPEntryIPOld;
  reg [47:0] ARPEntryMACOld;

  reg        doGenARPRep;


  always @(posedge clk or negedge reset_n) 
  begin

    if (!reset_n) begin
	  arp_rep_rx_cur_fsm <= RX_ARP_IDLE;
	  ARPEntryIP <= 32'b0;
	  ARPEntryMAC <= 48'b0;
	  ARPEntryIPOld <= 32'b0;
	  ARPEntryMACOld <= 48'b0;
      genARPRep <= 1'b0;
	  ARPOperation <= 1'b0;
	  cnt <= 5'b0;
	end
	else begin
	  arp_rep_rx_cur_fsm <= arp_rep_rx_nxt_fsm;
	  
	  if (incCnt)
	    cnt <= cnt + 1;
	  else if (rstCnt)
	    cnt <= 5'b0;
	  
	  if (latchFrameData)
	    frameDataLatch <= frameData;
	  
	  if (determineOperation)
	    ARPOperation <= frameDataLatch[0];
	  
	  if (shiftSourceIPIn)
	    sourceIP <= {sourceIP[23:0], frameDataLatch};
	  
	  if (shiftSourceMACIn)
	    sourceMAC <= {sourceMAC[39:0], frameDataLatch};
	  
	  if (updateARPTable) begin
	    if (ARPEntryIP == sourceIP)
	      ARPEntryMAC <= sourceMAC;
	    else begin
	      ARPEntryIPOld <= ARPEntryIP;
	      ARPEntryMACOld <= ARPEntryMAC;
	      ARPEntryIP <= sourceIP;
	      ARPEntryMAC <= sourceMAC;
	    end
	  end
	  


	  if (doGenARPRep) begin	
	    genARPRep <= 1'b1;
	    genARPIP <= sourceIP;
	  end	
	  else if (ARPSendAvail) 
	    genARPRep <= 1'b0;
	  						
	  
    end
  end



  always @(arp_rep_rx_cur_fsm or sourceIP or ARPOperation or cnt or newFrame or frameType or newFrameByte or frameDataLatch or frameValid)
  begin

	rstCnt <= 1'b0;
	incCnt <= 1'b0;
	shiftSourceIPIn <= 1'b0;
	determineOperation <= 1'b0;
	updateARPTable <= 1'b0;
	shiftSourceIPIn <= 1'b0;
	shiftSourceMACIn <= 1'b0;
	latchFrameData <= 1'b0;
	doGenARPRep <= 1'b0;
	
    case (arp_rep_rx_cur_fsm)
	  RX_ARP_IDLE:
	  begin

		if (newFrame && !frameType) begin
		  arp_rep_rx_nxt_fsm <= RX_ARP_HANDLEARP;
		  rstCnt <= 1'b1;
		end  
		else
		  arp_rep_rx_nxt_fsm <= RX_ARP_IDLE;
      end

	  RX_ARP_HANDLEARP:
	  begin

		if (!newFrameByte)
		  arp_rep_rx_nxt_fsm <= RX_ARP_HANDLEARP;
		else begin
		  arp_rep_rx_nxt_fsm <= RX_ARP_OPERATE;
		  latchFrameData <= 1'b1;
		end
	  end
			
	  RX_ARP_OPERATE:
	  begin

		incCnt <= 1'b1;








	    if ( 
			  ((cnt == 5'b0 || cnt == 5'b00011 || cnt == 5'b00110) && frameDataLatch != 8'd0 ) || 
			  (cnt == 5'b00001 && frameDataLatch != 8'd1) || 
			  (cnt == 5'b00010 && frameDataLatch != 8'd8) || 
			  (cnt == 5'b00100 && frameDataLatch != 8'd6) ||	
			  (cnt == 5'b00101 && frameDataLatch != 8'd4) ||	 
			  (cnt == 5'b00111 && frameDataLatch != 8'd1 && frameDataLatch != 8'd2 ) || 
			  (cnt == 5'b11000 && frameDataLatch != DEVICE_IP[31:24]) ||	
			  (cnt == 5'b11001 && frameDataLatch != DEVICE_IP[23:16]) || 
			  (cnt == 5'b11010 && frameDataLatch != DEVICE_IP[15:8]) || 
			  (cnt == 5'b11011 && frameDataLatch != DEVICE_IP[7:0])
			)
	      arp_rep_rx_nxt_fsm <= RX_ARP_IDLE;
	    else if (cnt == 5'b11011) 
	      arp_rep_rx_nxt_fsm <= RX_ARP_CHECKVALID;
	    else
	      arp_rep_rx_nxt_fsm <= RX_ARP_HANDLEARP;
				

	    if (cnt == 5'b00111)
	      determineOperation <= 1'b1;	
	    
	    if (cnt == 5'b01000 || cnt ==  5'b01001 || cnt == 5'b01010 || cnt == 5'b01011 || cnt == 5'b01100 || cnt == 5'b01101)
		  shiftSourceMACIn <= 1'b1;
	    
	    if (cnt == 5'b01110 || cnt == 5'b01111 || cnt == 5'b10000 || cnt == 5'b10001) 
	      shiftSourceIPIn <= 1'b1;
	    
	  end
								
	  RX_ARP_CHECKVALID:
	  begin

	    if (!frameValid)

		  arp_rep_rx_nxt_fsm <= RX_ARP_CHECKVALID;
	    else begin

		  if (ARPOperation)
		    doGenARPRep <= 1'b1;
		  			
		  arp_rep_rx_nxt_fsm <= RX_ARP_IDLE;
		  updateARPTable <= 1'b1;
	    end
	  end

	  default:
	  begin
          arp_rep_rx_nxt_fsm <= RX_ARP_IDLE;
	  end
	
    endcase 
	
  end
			


  always @(requestIP or ARPEntryIP or ARPEntryMAC or ARPEntryIPOld or ARPEntryMACOld)
  begin
    if (requestIP == ARPEntryIP) begin
	  validEntry <= 1'b1;
	  lookupMAC <= ARPEntryMAC;
	end  
	else if (requestIP == ARPEntryIPOld) begin
	  validEntry <= 1'b1;
	  lookupMAC <= ARPEntryMACOld;
	end  
	else begin
	  validEntry <= 1'b0;
	  lookupMAC <= 48'b1;
	end
  
  end

endmodule
module arp_reply_rx #(
  parameter [31:0] DEVICE_IP      = 32'h0a0105dd,
  parameter [47:0] DEVICE_MAC     = 48'h001999cf956f
)
(
  input              clk,
  input              reset_n,
  input              newFrame,
  input              frameType,
  input              newFrameByte,
  input [7:0]        frameData,
  input              frameValid,
  input              ARPSendAvail,
  input [31:0]       requestIP,
  output reg         genARPRep,
  output reg  [31:0] genARPIP,
  output reg  [47:0] lookupMAC,
  output reg         validEntry
);



  parameter [1:0]
	RX_ARP_IDLE    	           = 2'd0,
	RX_ARP_HANDLEARP   	       = 2'd1,
	RX_ARP_OPERATE   	       = 2'd2,
	RX_ARP_CHECKVALID  	       = 2'd3;
  
  reg [1:0]  arp_rep_rx_cur_fsm;
  reg [1:0]  arp_rep_rx_nxt_fsm;

  reg [4:0]  cnt;
  reg        incCnt;
  reg        rstCnt;

  reg        latchFrameData;
  reg [7:0]  frameDataLatch;
  reg        shiftSourceIPIn;
  reg [31:0] sourceIP;
  reg        shiftSourceMACIn;
  reg [47:0] sourceMAC;

  reg        ARPOperation;
  reg        determineOperation;

  reg        updateARPTable;
  reg [31:0] ARPEntryIP;
  reg [47:0] ARPEntryMAC;
  reg [31:0] ARPEntryIPOld;
  reg [47:0] ARPEntryMACOld;

  reg        doGenARPRep;


  always @(posedge clk or negedge reset_n) 
  begin

    if (!reset_n) begin
	  arp_rep_rx_cur_fsm <= RX_ARP_IDLE;
	  ARPEntryIP <= 32'b0;
	  ARPEntryMAC <= 48'b0;
	  ARPEntryIPOld <= 32'b0;
	  ARPEntryMACOld <= 48'b0;
      genARPRep <= 1'b0;
	  ARPOperation <= 1'b0;
	  cnt <= 5'b0;
	end
	else begin
	  arp_rep_rx_cur_fsm <= arp_rep_rx_nxt_fsm;
	  
	  if (incCnt)
	    cnt <= cnt + 1;
	  else if (rstCnt)
	    cnt <= 5'b0;
	  
	  if (latchFrameData)
	    frameDataLatch <= frameData;
	  
	  if (determineOperation)
	    ARPOperation <= frameDataLatch[0];
	  
	  if (shiftSourceIPIn)
	    sourceIP <= {sourceIP[23:0], frameDataLatch};
	  
	  if (shiftSourceMACIn)
	    sourceMAC <= {sourceMAC[39:0], frameDataLatch};
	  
	  if (updateARPTable) begin
	    if (ARPEntryIP == sourceIP)
	      ARPEntryMAC <= sourceMAC;
	    else begin
	      ARPEntryIPOld <= ARPEntryIP;
	      ARPEntryMACOld <= ARPEntryMAC;
	      ARPEntryIP <= sourceIP;
	      ARPEntryMAC <= sourceMAC;
	    end
	  end
	  


	  if (doGenARPRep) begin	
	    genARPRep <= 1'b1;
	    genARPIP <= sourceIP;
	  end	
	  else if (ARPSendAvail) 
	    genARPRep <= 1'b0;
	  						
	  
    end
  end



  always @(arp_rep_rx_cur_fsm or sourceIP or ARPOperation or cnt or newFrame or frameType or newFrameByte or frameDataLatch or frameValid)
  begin

	rstCnt <= 1'b0;
	incCnt <= 1'b0;
	shiftSourceIPIn <= 1'b0;
	determineOperation <= 1'b0;
	updateARPTable <= 1'b0;
	shiftSourceIPIn <= 1'b0;
	shiftSourceMACIn <= 1'b0;
	latchFrameData <= 1'b0;
	doGenARPRep <= 1'b0;
	
    case (arp_rep_rx_cur_fsm)
	  RX_ARP_IDLE:
	  begin

		if (newFrame && !frameType) begin
		  arp_rep_rx_nxt_fsm <= RX_ARP_HANDLEARP;
		  rstCnt <= 1'b1;
		end  
		else
		  arp_rep_rx_nxt_fsm <= RX_ARP_IDLE;
      end

	  RX_ARP_HANDLEARP:
	  begin

		if (!newFrameByte)
		  arp_rep_rx_nxt_fsm <= RX_ARP_HANDLEARP;
		else begin
		  arp_rep_rx_nxt_fsm <= RX_ARP_OPERATE;
		  latchFrameData <= 1'b1;
		end
	  end
			
	  RX_ARP_OPERATE:
	  begin

		incCnt <= 1'b1;








	    if ( 
			  ((cnt == 5'b0 || cnt == 5'b00011 || cnt == 5'b00110) && frameDataLatch != 8'd0 ) || 
			  (cnt == 5'b00001 && frameDataLatch != 8'd1) || 
			  (cnt == 5'b00010 && frameDataLatch != 8'd8) || 
			  (cnt == 5'b00100 && frameDataLatch != 8'd6) ||	
			  (cnt == 5'b00101 && frameDataLatch != 8'd4) ||	 
			  (cnt == 5'b00111 && frameDataLatch != 8'd1 && frameDataLatch != 8'd2 ) || 
			  (cnt == 5'b11000 && frameDataLatch != DEVICE_IP[31:24]) ||	
			  (cnt == 5'b11001 && frameDataLatch != DEVICE_IP[23:16]) || 
			  (cnt == 5'b11010 && frameDataLatch != DEVICE_IP[15:8]) || 
			  (cnt == 5'b11011 && frameDataLatch != DEVICE_IP[7:0])
			)
	      arp_rep_rx_nxt_fsm <= RX_ARP_IDLE;
	    else if (cnt == 5'b11011) 
	      arp_rep_rx_nxt_fsm <= RX_ARP_CHECKVALID;
	    else
	      arp_rep_rx_nxt_fsm <= RX_ARP_HANDLEARP;
				

	    if (cnt == 5'b00111)
	      determineOperation <= 1'b1;	
	    
	    if (cnt == 5'b01000 || cnt ==  5'b01001 || cnt == 5'b01010 || cnt == 5'b01011 || cnt == 5'b01100 || cnt == 5'b01101)
		  shiftSourceMACIn <= 1'b1;
	    
	    if (cnt == 5'b01110 || cnt == 5'b01111 || cnt == 5'b10000 || cnt == 5'b10001) 
	      shiftSourceIPIn <= 1'b1;
	    
	  end
								
	  RX_ARP_CHECKVALID:
	  begin

	    if (!frameValid)

		  arp_rep_rx_nxt_fsm <= RX_ARP_CHECKVALID;
	    else begin

		  if (ARPOperation)
		    doGenARPRep <= 1'b1;
		  			
		  arp_rep_rx_nxt_fsm <= RX_ARP_IDLE;
		  updateARPTable <= 1'b1;
	    end
	  end

	  default:
	  begin
          arp_rep_rx_nxt_fsm <= RX_ARP_IDLE;
	  end
	
    endcase 
	
  end
			


  always @(requestIP or ARPEntryIP or ARPEntryMAC or ARPEntryIPOld or ARPEntryMACOld)
  begin
    if (requestIP == ARPEntryIP) begin
	  validEntry <= 1'b1;
	  lookupMAC <= ARPEntryMAC;
	end  
	else if (requestIP == ARPEntryIPOld) begin
	  validEntry <= 1'b1;
	  lookupMAC <= ARPEntryMACOld;
	end  
	else begin
	  validEntry <= 1'b0;
	  lookupMAC <= 48'b1;
	end
  
  end

endmodule