data/full_repos/permissive/14432793/rfid-verilog/tag/packetparse.v
 line:298: before: "begin"
data/verilator_xmls/80d27189-9868-4ffc-bfe4-ec96e93c9afd.xml
module packetparse(reset, bitin, bitinclk, packettype, rx_q, rx_updn,
                   currenthandle, currentrn, handlematch,
                   readwritebank, readwriteptr, readwords,
                   writedataout, writedataclk );
input reset, bitin, bitinclk;
input [8:0]  packettype;

input [15:0] currenthandle;
input [15:0] currentrn;
output handlematch;
output [3:0] rx_q;
output [2:0] rx_updn;

output [1:0] readwritebank;
output [7:0] readwriteptr;
output [7:0] readwords;
output writedataout, writedataclk;


reg [3:0] handlebitcounter;
reg [5:0] bitincounter;
reg       handlematch;
reg       matchfailed;

reg [3:0] rx_q;
reg [2:0] rx_updn;
reg [1:0] readwritebank;
reg [7:0] readwriteptr;
reg [7:0] readwords;
reg       writedataen;
reg       writedataengated;
reg       bankdone;
reg       ptrdone;
reg       ebvdone;
reg       datadone;

reg writedataout;
wire writedataclk;
assign writedataclk = bitinclk & writedataengated;




wire thisbitmatches;
assign thisbitmatches = (bitin == currenthandle[~handlebitcounter]);

wire lastbit;
assign lastbit  = (handlebitcounter == 15);

wire handlematchout;
assign handlematchout = handlematch | 
       ((handlebitcounter == 15) && packettype[1] && thisbitmatches);

  parameter ACK        = 9'b000000010;
  parameter QUERY      = 9'b000000100;
  parameter QUERYADJ   = 9'b000001000;
  parameter REQRN      = 9'b001000000;
  parameter READ       = 9'b010000000;
  parameter WRITE      = 9'b100000000;



always @ (negedge bitinclk or posedge reset) begin
  if (reset) begin
    writedataengated <= 0;
  end else begin
    writedataengated <= writedataen;
  end
end

always @ (posedge bitinclk or posedge reset) begin
  if (reset) begin
    handlebitcounter <= 0;
    bitincounter     <= 0;
    handlematch      <= 0;
    matchfailed      <= 0;

    bankdone         <= 0;
    ptrdone          <= 0;
    ebvdone          <= 0;
    datadone         <= 0;

    writedataen      <= 0;
    writedataout     <= 0;
    
    rx_q             <= 4'd0;
    rx_updn          <= 3'd0;

    readwritebank    <= 2'd0;
    readwriteptr     <= 8'd0;
    readwords        <= 8'd0;


  end else begin
    case(packettype)
      QUERY: begin
        if (!ptrdone) begin
          if (bitincounter >= 8) begin
            ptrdone      <= 1;
            bitincounter <= 0;
          end else begin
            bitincounter <= bitincounter + 6'd1;
          end

        end else if (bitincounter == 0) begin
          bitincounter <= bitincounter + 6'd1;
          rx_q[3] <= bitin;
        end else if (bitincounter == 1) begin
          bitincounter <= bitincounter + 6'd1;
          rx_q[2] <= bitin;
        end else if (bitincounter == 2) begin
          bitincounter <= bitincounter + 6'd1;
          rx_q[1] <= bitin;
        end else if (bitincounter == 3) begin
          bitincounter <= bitincounter + 6'd1;
          rx_q[0] <= bitin;
        end
      end
      QUERYADJ: begin
        if (!ptrdone) begin
          if (bitincounter >= 1) begin
            ptrdone      <= 1;
            bitincounter <= 0;
          end else begin
            bitincounter <= bitincounter + 6'd1;
          end

        end else if (bitincounter == 0) begin
          bitincounter <= bitincounter + 6'd1;
          rx_updn[2] <= bitin;
        end else if (bitincounter == 1) begin
          bitincounter <= bitincounter + 6'd1;
          rx_updn[1] <= bitin;
        end else if (bitincounter == 2) begin
          bitincounter <= bitincounter + 6'd1;
          rx_updn[0] <= bitin;
        end
      end
      ACK: begin
        if (!matchfailed && !lastbit && thisbitmatches) begin
          handlebitcounter <= handlebitcounter + 4'd1;

        end else if (!matchfailed && lastbit && thisbitmatches) begin
          handlematch <= 1;
        end else if (!handlematch) begin
          matchfailed <= 1;
        end
      end
      REQRN: begin
        if (!matchfailed && !lastbit && thisbitmatches) begin
          handlebitcounter <= handlebitcounter + 4'd1;

        end else if (!matchfailed && lastbit && thisbitmatches) begin
          handlematch <= 1;
        end else if (!handlematch) begin
          matchfailed <= 1;
        end
      end
      READ: begin

        if (!bankdone && (bitincounter == 0)) begin
          bitincounter <= bitincounter + 6'd1;
          readwritebank[1] <= bitin;
        end else if (!bankdone && (bitincounter >= 1)) begin
          bankdone <= 1;
          bitincounter <= 0;
          readwritebank[0] <= bitin;


        end else if (!ebvdone && (bitincounter == 0)) begin
          if (!bitin) begin
            ebvdone        <= 1;
            bitincounter   <= 1;
          end else begin
            bitincounter <= bitincounter + 6'd1;
          end

        end else if (!ebvdone && (bitincounter < 7)) begin
          bitincounter <= bitincounter + 6'd1;

        end else if (!ebvdone && (bitincounter >= 7)) begin
          bitincounter <= 0;


        end else if (!ptrdone) begin
          if (bitincounter >= 7) begin
            ptrdone      <= 1;
            bitincounter <= 0;
          end else begin
            bitincounter <= bitincounter + 6'd1;
          end
          readwriteptr[~bitincounter[2:0]] <= bitin;


        end else if (ptrdone && (bitincounter < 8)) begin
          bitincounter <= bitincounter + 6'd1;
          readwords[~bitincounter[2:0]] <= bitin;


        end else if (!matchfailed && !lastbit && thisbitmatches) begin
          handlebitcounter <= handlebitcounter + 4'd1;

        end else if (!matchfailed && lastbit && thisbitmatches) begin
          handlematch <= 1;
        end else if (!handlematch) begin
          matchfailed <= 1;
        end
      end
      WRITE: begin

        if (!bankdone && (bitincounter == 0)) begin
          bitincounter <= bitincounter + 6'd1;
          readwritebank[1] <= bitin;
        end else if (!bankdone && (bitincounter >= 1)) begin
          bankdone <= 1;
          bitincounter <= 0;
          readwritebank[0] <= bitin;


        end else if (!ebvdone && (bitincounter == 0)) begin
          if (!bitin) begin
            ebvdone      <= 1;
            bitincounter <= 1;
          end else begin
            bitincounter <= bitincounter + 6'd1;
          end

        end else if (!ebvdone && (bitincounter < 7)) begin
          bitincounter <= bitincounter + 6'd1;

        end else if (!ebvdone && (bitincounter >= 7)) begin
          bitincounter <= 0;


        end else if (!ptrdone) begin
          if (bitincounter >= 7) begin
            ptrdone      <= 1;
            bitincounter <= 0;
            writedataen  <= 1;
          end else begin
            bitincounter <= bitincounter + 6'd1;
          end
          readwriteptr[~bitincounter[2:0]] <= bitin;


        end else if (!datadone) begin
          if (bitincounter >= 15) begin
            datadone     <= 1;
            bitincounter <= 0;
            writedataen  <= 0;
          end else begin
            bitincounter <= bitincounter + 6'd1;
          end
          writedataout <= bitin ^ currentrn[~bitincounter[3:0]];


        end else if (!matchfailed && !lastbit && thisbitmatches) begin
          handlebitcounter <= handlebitcounter + 4'd1;

        end else if (!matchfailed && lastbit && thisbitmatches) begin
          handlematch <= 1;
        end else if (!handlematch) begin
          matchfailed <= 1;
        end else begin
          writedataen <= 0;
        end
      end
      default begin

      end
    endcase
  end
end

endmodule
module packetparse(reset, bitin, bitinclk, packettype, rx_q, rx_updn,
                   currenthandle, currentrn, handlematch,
                   readwritebank, readwriteptr, readwords,
                   writedataout, writedataclk );

input reset, bitin, bitinclk;
input [8:0]  packettype;

input [15:0] currenthandle;
input [15:0] currentrn;
output handlematch;
output [3:0] rx_q;
output [2:0] rx_updn;

output [1:0] readwritebank;
output [7:0] readwriteptr;
output [7:0] readwords;
output writedataout, writedataclk;


reg [3:0] handlebitcounter;
reg [5:0] bitincounter;
reg       handlematch;
reg       matchfailed;

reg [3:0] rx_q;
reg [2:0] rx_updn;
reg [1:0] readwritebank;
reg [7:0] readwriteptr;
reg [7:0] readwords;
reg       writedataen;
reg       writedataengated;
reg       bankdone;
reg       ptrdone;
reg       ebvdone;
reg       datadone;

reg writedataout;
wire writedataclk;
assign writedataclk = bitinclk & writedataengated;




wire thisbitmatches;
assign thisbitmatches = (bitin == currenthandle[~handlebitcounter]);

wire lastbit;
assign lastbit  = (handlebitcounter == 15);

wire handlematchout;
assign handlematchout = handlematch | 
       ((handlebitcounter == 15) && packettype[1] && thisbitmatches);

  parameter ACK        = 9'b000000010;
  parameter QUERY      = 9'b000000100;
  parameter QUERYADJ   = 9'b000001000;
  parameter REQRN      = 9'b001000000;
  parameter READ       = 9'b010000000;
  parameter WRITE      = 9'b100000000;



always @ (negedge bitinclk or posedge reset) begin
  if (reset) begin
    writedataengated <= 0;
  end else begin
    writedataengated <= writedataen;
  end
end

always @ (posedge bitinclk or posedge reset) begin
  if (reset) begin
    handlebitcounter <= 0;
    bitincounter     <= 0;
    handlematch      <= 0;
    matchfailed      <= 0;

    bankdone         <= 0;
    ptrdone          <= 0;
    ebvdone          <= 0;
    datadone         <= 0;

    writedataen      <= 0;
    writedataout     <= 0;
    
    rx_q             <= 4'd0;
    rx_updn          <= 3'd0;

    readwritebank    <= 2'd0;
    readwriteptr     <= 8'd0;
    readwords        <= 8'd0;


  end else begin
    case(packettype)
      QUERY: begin
        if (!ptrdone) begin
          if (bitincounter >= 8) begin
            ptrdone      <= 1;
            bitincounter <= 0;
          end else begin
            bitincounter <= bitincounter + 6'd1;
          end

        end else if (bitincounter == 0) begin
          bitincounter <= bitincounter + 6'd1;
          rx_q[3] <= bitin;
        end else if (bitincounter == 1) begin
          bitincounter <= bitincounter + 6'd1;
          rx_q[2] <= bitin;
        end else if (bitincounter == 2) begin
          bitincounter <= bitincounter + 6'd1;
          rx_q[1] <= bitin;
        end else if (bitincounter == 3) begin
          bitincounter <= bitincounter + 6'd1;
          rx_q[0] <= bitin;
        end
      end
      QUERYADJ: begin
        if (!ptrdone) begin
          if (bitincounter >= 1) begin
            ptrdone      <= 1;
            bitincounter <= 0;
          end else begin
            bitincounter <= bitincounter + 6'd1;
          end

        end else if (bitincounter == 0) begin
          bitincounter <= bitincounter + 6'd1;
          rx_updn[2] <= bitin;
        end else if (bitincounter == 1) begin
          bitincounter <= bitincounter + 6'd1;
          rx_updn[1] <= bitin;
        end else if (bitincounter == 2) begin
          bitincounter <= bitincounter + 6'd1;
          rx_updn[0] <= bitin;
        end
      end
      ACK: begin
        if (!matchfailed && !lastbit && thisbitmatches) begin
          handlebitcounter <= handlebitcounter + 4'd1;

        end else if (!matchfailed && lastbit && thisbitmatches) begin
          handlematch <= 1;
        end else if (!handlematch) begin
          matchfailed <= 1;
        end
      end
      REQRN: begin
        if (!matchfailed && !lastbit && thisbitmatches) begin
          handlebitcounter <= handlebitcounter + 4'd1;

        end else if (!matchfailed && lastbit && thisbitmatches) begin
          handlematch <= 1;
        end else if (!handlematch) begin
          matchfailed <= 1;
        end
      end
      READ: begin

        if (!bankdone && (bitincounter == 0)) begin
          bitincounter <= bitincounter + 6'd1;
          readwritebank[1] <= bitin;
        end else if (!bankdone && (bitincounter >= 1)) begin
          bankdone <= 1;
          bitincounter <= 0;
          readwritebank[0] <= bitin;


        end else if (!ebvdone && (bitincounter == 0)) begin
          if (!bitin) begin
            ebvdone        <= 1;
            bitincounter   <= 1;
          end else begin
            bitincounter <= bitincounter + 6'd1;
          end

        end else if (!ebvdone && (bitincounter < 7)) begin
          bitincounter <= bitincounter + 6'd1;

        end else if (!ebvdone && (bitincounter >= 7)) begin
          bitincounter <= 0;


        end else if (!ptrdone) begin
          if (bitincounter >= 7) begin
            ptrdone      <= 1;
            bitincounter <= 0;
          end else begin
            bitincounter <= bitincounter + 6'd1;
          end
          readwriteptr[~bitincounter[2:0]] <= bitin;


        end else if (ptrdone && (bitincounter < 8)) begin
          bitincounter <= bitincounter + 6'd1;
          readwords[~bitincounter[2:0]] <= bitin;


        end else if (!matchfailed && !lastbit && thisbitmatches) begin
          handlebitcounter <= handlebitcounter + 4'd1;

        end else if (!matchfailed && lastbit && thisbitmatches) begin
          handlematch <= 1;
        end else if (!handlematch) begin
          matchfailed <= 1;
        end
      end
      WRITE: begin

        if (!bankdone && (bitincounter == 0)) begin
          bitincounter <= bitincounter + 6'd1;
          readwritebank[1] <= bitin;
        end else if (!bankdone && (bitincounter >= 1)) begin
          bankdone <= 1;
          bitincounter <= 0;
          readwritebank[0] <= bitin;


        end else if (!ebvdone && (bitincounter == 0)) begin
          if (!bitin) begin
            ebvdone      <= 1;
            bitincounter <= 1;
          end else begin
            bitincounter <= bitincounter + 6'd1;
          end

        end else if (!ebvdone && (bitincounter < 7)) begin
          bitincounter <= bitincounter + 6'd1;

        end else if (!ebvdone && (bitincounter >= 7)) begin
          bitincounter <= 0;


        end else if (!ptrdone) begin
          if (bitincounter >= 7) begin
            ptrdone      <= 1;
            bitincounter <= 0;
            writedataen  <= 1;
          end else begin
            bitincounter <= bitincounter + 6'd1;
          end
          readwriteptr[~bitincounter[2:0]] <= bitin;


        end else if (!datadone) begin
          if (bitincounter >= 15) begin
            datadone     <= 1;
            bitincounter <= 0;
            writedataen  <= 0;
          end else begin
            bitincounter <= bitincounter + 6'd1;
          end
          writedataout <= bitin ^ currentrn[~bitincounter[3:0]];


        end else if (!matchfailed && !lastbit && thisbitmatches) begin
          handlebitcounter <= handlebitcounter + 4'd1;

        end else if (!matchfailed && lastbit && thisbitmatches) begin
          handlematch <= 1;
        end else if (!handlematch) begin
          matchfailed <= 1;
        end else begin
          writedataen <= 0;
        end
      end
      default begin

      end
    endcase
  end
end

endmodule