data/full_repos/permissive/197954777/src_v/usbf_sie_rx.v
1: b'%Error: data/full_repos/permissive/197954777/src_v/usbf_sie_rx.v:72: Cannot find include file: usbf_defs.v\n`include "usbf_defs.v" \n         ^~~~~~~~~~~~~\n        ... Looked in:\n             data/full_repos/permissive/197954777/src_v,data/full_repos/permissive/197954777/usbf_defs.v\n             data/full_repos/permissive/197954777/src_v,data/full_repos/permissive/197954777/usbf_defs.v.v\n             data/full_repos/permissive/197954777/src_v,data/full_repos/permissive/197954777/usbf_defs.v.sv\n             usbf_defs.v\n             usbf_defs.v.v\n             usbf_defs.v.sv\n             obj_dir/usbf_defs.v\n             obj_dir/usbf_defs.v.v\n             obj_dir/usbf_defs.v.sv\n%Error: data/full_repos/permissive/197954777/src_v/usbf_sie_rx.v:165: Define or directive not defined: \'`PID_OUT\'\n              `PID_OUT, `PID_IN, `PID_SETUP, `PID_PING:\n              ^~~~~~~~\n%Error: data/full_repos/permissive/197954777/src_v/usbf_sie_rx.v:165: syntax error, unexpected \',\', expecting endcase\n              `PID_OUT, `PID_IN, `PID_SETUP, `PID_PING:\n                      ^\n%Error: data/full_repos/permissive/197954777/src_v/usbf_sie_rx.v:165: Define or directive not defined: \'`PID_IN\'\n              `PID_OUT, `PID_IN, `PID_SETUP, `PID_PING:\n                        ^~~~~~~\n%Error: data/full_repos/permissive/197954777/src_v/usbf_sie_rx.v:165: Define or directive not defined: \'`PID_SETUP\'\n              `PID_OUT, `PID_IN, `PID_SETUP, `PID_PING:\n                                 ^~~~~~~~~~\n%Error: data/full_repos/permissive/197954777/src_v/usbf_sie_rx.v:165: Define or directive not defined: \'`PID_PING\'\n              `PID_OUT, `PID_IN, `PID_SETUP, `PID_PING:\n                                             ^~~~~~~~~\n%Error: data/full_repos/permissive/197954777/src_v/usbf_sie_rx.v:168: Define or directive not defined: \'`PID_SOF\'\n              `PID_SOF:\n              ^~~~~~~~\n%Error: data/full_repos/permissive/197954777/src_v/usbf_sie_rx.v:171: Define or directive not defined: \'`PID_DATA0\'\n              `PID_DATA0, `PID_DATA1, `PID_DATA2, `PID_MDATA:\n              ^~~~~~~~~~\n%Error: data/full_repos/permissive/197954777/src_v/usbf_sie_rx.v:171: Define or directive not defined: \'`PID_DATA1\'\n              `PID_DATA0, `PID_DATA1, `PID_DATA2, `PID_MDATA:\n                          ^~~~~~~~~~\n%Error: data/full_repos/permissive/197954777/src_v/usbf_sie_rx.v:171: Define or directive not defined: \'`PID_DATA2\'\n              `PID_DATA0, `PID_DATA1, `PID_DATA2, `PID_MDATA:\n                                      ^~~~~~~~~~\n%Error: data/full_repos/permissive/197954777/src_v/usbf_sie_rx.v:171: Define or directive not defined: \'`PID_MDATA\'\n              `PID_DATA0, `PID_DATA1, `PID_DATA2, `PID_MDATA:\n                                                  ^~~~~~~~~~\n%Error: data/full_repos/permissive/197954777/src_v/usbf_sie_rx.v:176: Define or directive not defined: \'`PID_ACK\'\n              `PID_ACK, `PID_NAK, `PID_STALL, `PID_NYET:\n              ^~~~~~~~\n%Error: data/full_repos/permissive/197954777/src_v/usbf_sie_rx.v:176: Define or directive not defined: \'`PID_NAK\'\n              `PID_ACK, `PID_NAK, `PID_STALL, `PID_NYET:\n                        ^~~~~~~~\n%Error: data/full_repos/permissive/197954777/src_v/usbf_sie_rx.v:176: Define or directive not defined: \'`PID_STALL\'\n              `PID_ACK, `PID_NAK, `PID_STALL, `PID_NYET:\n                                  ^~~~~~~~~~\n%Error: data/full_repos/permissive/197954777/src_v/usbf_sie_rx.v:176: Define or directive not defined: \'`PID_NYET\'\n              `PID_ACK, `PID_NAK, `PID_STALL, `PID_NYET:\n                                              ^~~~~~~~~\n%Error: data/full_repos/permissive/197954777/src_v/usbf_sie_rx.v:290: Define or directive not defined: \'`PID_ACK\'\n    `PID_ACK, `PID_NAK, `PID_STALL, `PID_NYET:\n    ^~~~~~~~\n%Error: data/full_repos/permissive/197954777/src_v/usbf_sie_rx.v:290: syntax error, unexpected \',\', expecting endcase\n    `PID_ACK, `PID_NAK, `PID_STALL, `PID_NYET:\n            ^\n%Error: data/full_repos/permissive/197954777/src_v/usbf_sie_rx.v:290: Define or directive not defined: \'`PID_NAK\'\n    `PID_ACK, `PID_NAK, `PID_STALL, `PID_NYET:\n              ^~~~~~~~\n%Error: data/full_repos/permissive/197954777/src_v/usbf_sie_rx.v:290: Define or directive not defined: \'`PID_STALL\'\n    `PID_ACK, `PID_NAK, `PID_STALL, `PID_NYET:\n                        ^~~~~~~~~~\n%Error: data/full_repos/permissive/197954777/src_v/usbf_sie_rx.v:290: Define or directive not defined: \'`PID_NYET\'\n    `PID_ACK, `PID_NAK, `PID_STALL, `PID_NYET:\n                                    ^~~~~~~~~\n%Error: data/full_repos/permissive/197954777/src_v/usbf_sie_rx.v:304: syntax error, unexpected always\nalways @ (posedge clk_i or posedge rst_i)\n^~~~~~\n%Error: data/full_repos/permissive/197954777/src_v/usbf_sie_rx.v:316: syntax error, unexpected IDENTIFIER, expecting "\'{"\nreg frame_valid_q;\n    ^~~~~~~~~~~~~\n%Error: data/full_repos/permissive/197954777/src_v/usbf_sie_rx.v:329: syntax error, unexpected always\nalways @ (posedge clk_i or posedge rst_i)\n^~~~~~\n%Error: data/full_repos/permissive/197954777/src_v/usbf_sie_rx.v:339: syntax error, unexpected IDENTIFIER, expecting "\'{"\nreg token_valid_q;\n    ^~~~~~~~~~~~~\n%Error: data/full_repos/permissive/197954777/src_v/usbf_sie_rx.v:352: syntax error, unexpected always\nalways @ (posedge clk_i or posedge rst_i)\n^~~~~~\n%Error: data/full_repos/permissive/197954777/src_v/usbf_sie_rx.v:365: syntax error, unexpected always\nalways @ (posedge clk_i or posedge rst_i)\n^~~~~~\n%Error: data/full_repos/permissive/197954777/src_v/usbf_sie_rx.v:378: syntax error, unexpected wire\nwire [7:0] input_data_w  = data_w;\n^~~~\n%Error: data/full_repos/permissive/197954777/src_v/usbf_sie_rx.v:384: syntax error, unexpected IDENTIFIER, expecting "\'{"\nreg [15:0]  crc_sum_q;\n            ^~~~~~~~~\n%Error: data/full_repos/permissive/197954777/src_v/usbf_sie_rx.v:414: syntax error, unexpected IDENTIFIER, expecting "\'{"\nreg data_complete_q;\n    ^~~~~~~~~~~~~~~\n%Error: data/full_repos/permissive/197954777/src_v/usbf_sie_rx.v:426: syntax error, unexpected IDENTIFIER, expecting "\'{"\nreg data_zlp_q;\n    ^~~~~~~~~~\n%Error: data/full_repos/permissive/197954777/src_v/usbf_sie_rx.v:441: syntax error, unexpected IDENTIFIER, expecting "\'{"\nreg [7:0]  data_q;\n           ^~~~~~\n%Error: Exiting due to 31 error(s)\n'
module usbf_sie_rx
(

     input           clk_i
    ,input           rst_i
    ,input           enable_i
    ,input  [  7:0]  utmi_data_i
    ,input           utmi_rxvalid_i
    ,input           utmi_rxactive_i
    ,input  [  6:0]  current_addr_i


    ,output [  7:0]  pid_o
    ,output          frame_valid_o
    ,output [ 10:0]  frame_number_o
    ,output          token_valid_o
    ,output [  6:0]  token_addr_o
    ,output [  3:0]  token_ep_o
    ,output          token_crc_err_o
    ,output          handshake_valid_o
    ,output          data_valid_o
    ,output          data_strb_o
    ,output [  7:0]  data_o
    ,output          data_last_o
    ,output          data_crc_err_o
    ,output          data_complete_o
);






`include "usbf_defs.v"

localparam STATE_W                       = 4;
localparam STATE_RX_IDLE                 = 4'd0;
localparam STATE_RX_TOKEN2               = 4'd1;
localparam STATE_RX_TOKEN3               = 4'd2;
localparam STATE_RX_TOKEN_COMPLETE       = 4'd3;
localparam STATE_RX_SOF2                 = 4'd4;
localparam STATE_RX_SOF3                 = 4'd5;
localparam STATE_RX_DATA                 = 4'd6;
localparam STATE_RX_DATA_COMPLETE        = 4'd7;
localparam STATE_RX_IGNORED              = 4'd8;
reg [STATE_W-1:0] state_q;




`define USB_FRAME_W    11
reg [`USB_FRAME_W-1:0]      frame_num_q;

`define USB_DEV_W      7
reg [`USB_DEV_W-1:0]        token_dev_q;

`define USB_EP_W       4
reg [`USB_EP_W-1:0]         token_ep_q;

`define USB_PID_W      8
reg [`USB_PID_W-1:0]        token_pid_q;




reg [31:0] data_buffer_q;
reg [3:0]  data_valid_q;
reg [3:0]  rx_active_q;

wire shift_en_w = (utmi_rxvalid_i & utmi_rxactive_i) || !utmi_rxactive_i;

always @ (posedge clk_i or posedge rst_i)
if (rst_i)
    data_buffer_q <= 32'b0;
else if (shift_en_w)
    data_buffer_q <= {utmi_data_i, data_buffer_q[31:8]};

always @ (posedge clk_i or posedge rst_i)
if (rst_i)
    data_valid_q <= 4'b0;
else if (shift_en_w)
    data_valid_q <= {(utmi_rxvalid_i & utmi_rxactive_i), data_valid_q[3:1]};
else
    data_valid_q <= {data_valid_q[3:1], 1'b0};

reg [1:0] data_crc_q;
always @ (posedge clk_i or posedge rst_i)
if (rst_i)
    data_crc_q <= 2'b0;
else if (shift_en_w)
    data_crc_q <= {!utmi_rxactive_i, data_crc_q[1]};

always @ (posedge clk_i or posedge rst_i)
if (rst_i)
    rx_active_q <= 4'b0;
else
    rx_active_q <= {utmi_rxactive_i, rx_active_q[3:1]};

wire [7:0] data_w       = data_buffer_q[7:0];
wire       data_ready_w = data_valid_q[0];
wire       crc_byte_w   = data_crc_q[0];
wire       rx_active_w  = rx_active_q[0];

wire       address_match_w = (token_dev_q == current_addr_i);




reg [STATE_W-1:0] next_state_r;

always @ *
begin
    next_state_r = state_q;

    case (state_q)




    STATE_RX_IDLE :
    begin
       if (data_ready_w)
       begin

           case (data_w)

              `PID_OUT, `PID_IN, `PID_SETUP, `PID_PING:
                    next_state_r  = STATE_RX_TOKEN2;

              `PID_SOF:
                    next_state_r  = STATE_RX_SOF2;

              `PID_DATA0, `PID_DATA1, `PID_DATA2, `PID_MDATA:
              begin
                    next_state_r  = STATE_RX_DATA;
              end

              `PID_ACK, `PID_NAK, `PID_STALL, `PID_NYET:
                    next_state_r  = STATE_RX_IDLE;

              default :
                    next_state_r  = STATE_RX_IGNORED;
           endcase
       end
    end




    STATE_RX_IGNORED :
    begin

        if (!rx_active_w)
           next_state_r = STATE_RX_IDLE;
    end




    STATE_RX_SOF2 :
    begin
       if (data_ready_w)
           next_state_r = STATE_RX_SOF3;
       else if (!rx_active_w)
           next_state_r = STATE_RX_IDLE;
    end




    STATE_RX_SOF3 :
    begin
       if (data_ready_w || !rx_active_w)
           next_state_r = STATE_RX_IDLE;
    end




    STATE_RX_TOKEN2 :
    begin
       if (data_ready_w)
           next_state_r = STATE_RX_TOKEN3;
       else if (!rx_active_w)
           next_state_r = STATE_RX_IDLE;
    end




    STATE_RX_TOKEN3 :
    begin
       if (data_ready_w)
           next_state_r = STATE_RX_TOKEN_COMPLETE;
       else if (!rx_active_w)
           next_state_r = STATE_RX_IDLE;
    end




    STATE_RX_TOKEN_COMPLETE :
    begin
        next_state_r  = STATE_RX_IDLE;
    end




    STATE_RX_DATA :
    begin

       if (crc_byte_w)
            next_state_r = STATE_RX_DATA_COMPLETE;
    end




    STATE_RX_DATA_COMPLETE :
    begin
        if (!rx_active_w)
            next_state_r = STATE_RX_IDLE;
    end

    default :
       ;

    endcase
end


always @ (posedge clk_i or posedge rst_i)
if (rst_i)
    state_q   <= STATE_RX_IDLE;
else if (!enable_i)
    state_q   <= STATE_RX_IDLE;
else
    state_q   <= next_state_r;




reg handshake_valid_q;

always @ (posedge clk_i or posedge rst_i)
if (rst_i)
    handshake_valid_q <= 1'b0;
else if (state_q == STATE_RX_IDLE && data_ready_w)
begin
    case (data_w)
    `PID_ACK, `PID_NAK, `PID_STALL, `PID_NYET:
        handshake_valid_q <= address_match_w;
    default :
        handshake_valid_q <= 1'b0;
    endcase
end
else
    handshake_valid_q <= 1'b0;

assign handshake_valid_o = handshake_valid_q;




always @ (posedge clk_i or posedge rst_i)
if (rst_i)
    frame_num_q         <= `USB_FRAME_W'b0;
else if (state_q == STATE_RX_SOF2 && data_ready_w)
    frame_num_q         <= {3'b0, data_w};
else if (state_q == STATE_RX_SOF3 && data_ready_w)
    frame_num_q         <= {data_w[2:0], frame_num_q[7:0]};
else if (!enable_i)
    frame_num_q         <= `USB_FRAME_W'b0;

assign frame_number_o = frame_num_q;

reg frame_valid_q;

always @ (posedge clk_i or posedge rst_i)
if (rst_i)
    frame_valid_q <= 1'b0;
else
    frame_valid_q <= (state_q == STATE_RX_SOF3 && data_ready_w);

assign frame_valid_o = frame_valid_q;




always @ (posedge clk_i or posedge rst_i)
if (rst_i)
    token_pid_q <= `USB_PID_W'b0;
else if (state_q == STATE_RX_IDLE && data_ready_w)
    token_pid_q <= data_w;
else if (!enable_i)
    token_pid_q <= `USB_PID_W'b0;

assign pid_o = token_pid_q;

reg token_valid_q;

always @ (posedge clk_i or posedge rst_i)
if (rst_i)
    token_valid_q <= 1'b0;
else
    token_valid_q <= (state_q == STATE_RX_TOKEN_COMPLETE) && address_match_w;

assign token_valid_o = token_valid_q;




always @ (posedge clk_i or posedge rst_i)
if (rst_i)
    token_dev_q <= `USB_DEV_W'b0;
else if (state_q == STATE_RX_TOKEN2 && data_ready_w)
    token_dev_q <= data_w[6:0];
else if (!enable_i)
    token_dev_q <= `USB_DEV_W'b0;

assign token_addr_o = token_dev_q;




always @ (posedge clk_i or posedge rst_i)
if (rst_i)
    token_ep_q      <= `USB_EP_W'b0;
else if (state_q == STATE_RX_TOKEN2 && data_ready_w)
    token_ep_q[0]   <= data_w[7];
else if (state_q == STATE_RX_TOKEN3 && data_ready_w)
    token_ep_q[3:1] <= data_w[2:0];
else if (!enable_i)
    token_ep_q      <= `USB_EP_W'b0;

assign token_ep_o = token_ep_q;
assign token_crc_err_o = 1'b0;

wire [7:0] input_data_w  = data_w;
wire       input_ready_w = state_q == STATE_RX_DATA && data_ready_w && !crc_byte_w;




reg [15:0]  crc_sum_q;
wire [15:0] crc_out_w;
reg         crc_err_q;

usbf_crc16
u_crc16
(
    .crc_in_i(crc_sum_q),
    .din_i(data_w),
    .crc_out_o(crc_out_w)
);

always @ (posedge clk_i or posedge rst_i)
if (rst_i)
    crc_sum_q   <= 16'hFFFF;
else if (state_q == STATE_RX_IDLE)
    crc_sum_q   <= 16'hFFFF;
else if (data_ready_w)
    crc_sum_q   <= crc_out_w;

always @ (posedge clk_i or posedge rst_i)
if (rst_i)
    crc_err_q   <= 1'b0;
else if (state_q == STATE_RX_IDLE)
    crc_err_q   <= 1'b0;
else if (state_q == STATE_RX_DATA_COMPLETE && next_state_r == STATE_RX_IDLE)
    crc_err_q   <= (crc_sum_q != 16'hB001);

assign data_crc_err_o = crc_err_q;

reg data_complete_q;

always @ (posedge clk_i or posedge rst_i)
if (rst_i)
    data_complete_q   <= 1'b0;
else if (state_q == STATE_RX_DATA_COMPLETE && next_state_r == STATE_RX_IDLE)
    data_complete_q   <= 1'b1;
else
    data_complete_q   <= 1'b0;

assign data_complete_o = data_complete_q;

reg data_zlp_q;

always @ (posedge clk_i or posedge rst_i)
if (rst_i)
    data_zlp_q   <= 1'b0;
else if (state_q == STATE_RX_IDLE && next_state_r == STATE_RX_DATA)
    data_zlp_q   <= 1'b1;
else if (input_ready_w)
    data_zlp_q   <= 1'b0;




reg        valid_q;
reg        last_q;
reg [7:0]  data_q;
reg        mask_q;

always @ (posedge clk_i or posedge rst_i)
if (rst_i)
begin
    valid_q  <= 1'b0;
    data_q   <= 8'b0;
    mask_q   <= 1'b0;
    last_q   <= 1'b0;
end
else
begin
    valid_q  <= input_ready_w || ((state_q == STATE_RX_DATA) && crc_byte_w && data_zlp_q);
    data_q   <= input_data_w;
    mask_q   <= input_ready_w;
    last_q   <= (state_q == STATE_RX_DATA) && crc_byte_w;
end


assign data_valid_o = valid_q;
assign data_strb_o  = mask_q;
assign data_o       = data_q;
assign data_last_o  = last_q | crc_byte_w;


endmodule
module usbf_sie_rx
(

     input           clk_i
    ,input           rst_i
    ,input           enable_i
    ,input  [  7:0]  utmi_data_i
    ,input           utmi_rxvalid_i
    ,input           utmi_rxactive_i
    ,input  [  6:0]  current_addr_i


    ,output [  7:0]  pid_o
    ,output          frame_valid_o
    ,output [ 10:0]  frame_number_o
    ,output          token_valid_o
    ,output [  6:0]  token_addr_o
    ,output [  3:0]  token_ep_o
    ,output          token_crc_err_o
    ,output          handshake_valid_o
    ,output          data_valid_o
    ,output          data_strb_o
    ,output [  7:0]  data_o
    ,output          data_last_o
    ,output          data_crc_err_o
    ,output          data_complete_o
);







`include "usbf_defs.v"

localparam STATE_W                       = 4;
localparam STATE_RX_IDLE                 = 4'd0;
localparam STATE_RX_TOKEN2               = 4'd1;
localparam STATE_RX_TOKEN3               = 4'd2;
localparam STATE_RX_TOKEN_COMPLETE       = 4'd3;
localparam STATE_RX_SOF2                 = 4'd4;
localparam STATE_RX_SOF3                 = 4'd5;
localparam STATE_RX_DATA                 = 4'd6;
localparam STATE_RX_DATA_COMPLETE        = 4'd7;
localparam STATE_RX_IGNORED              = 4'd8;
reg [STATE_W-1:0] state_q;




`define USB_FRAME_W    11
reg [`USB_FRAME_W-1:0]      frame_num_q;

`define USB_DEV_W      7
reg [`USB_DEV_W-1:0]        token_dev_q;

`define USB_EP_W       4
reg [`USB_EP_W-1:0]         token_ep_q;

`define USB_PID_W      8
reg [`USB_PID_W-1:0]        token_pid_q;




reg [31:0] data_buffer_q;
reg [3:0]  data_valid_q;
reg [3:0]  rx_active_q;

wire shift_en_w = (utmi_rxvalid_i & utmi_rxactive_i) || !utmi_rxactive_i;

always @ (posedge clk_i or posedge rst_i)
if (rst_i)
    data_buffer_q <= 32'b0;
else if (shift_en_w)
    data_buffer_q <= {utmi_data_i, data_buffer_q[31:8]};

always @ (posedge clk_i or posedge rst_i)
if (rst_i)
    data_valid_q <= 4'b0;
else if (shift_en_w)
    data_valid_q <= {(utmi_rxvalid_i & utmi_rxactive_i), data_valid_q[3:1]};
else
    data_valid_q <= {data_valid_q[3:1], 1'b0};

reg [1:0] data_crc_q;
always @ (posedge clk_i or posedge rst_i)
if (rst_i)
    data_crc_q <= 2'b0;
else if (shift_en_w)
    data_crc_q <= {!utmi_rxactive_i, data_crc_q[1]};

always @ (posedge clk_i or posedge rst_i)
if (rst_i)
    rx_active_q <= 4'b0;
else
    rx_active_q <= {utmi_rxactive_i, rx_active_q[3:1]};

wire [7:0] data_w       = data_buffer_q[7:0];
wire       data_ready_w = data_valid_q[0];
wire       crc_byte_w   = data_crc_q[0];
wire       rx_active_w  = rx_active_q[0];

wire       address_match_w = (token_dev_q == current_addr_i);




reg [STATE_W-1:0] next_state_r;

always @ *
begin
    next_state_r = state_q;

    case (state_q)




    STATE_RX_IDLE :
    begin
       if (data_ready_w)
       begin

           case (data_w)

              `PID_OUT, `PID_IN, `PID_SETUP, `PID_PING:
                    next_state_r  = STATE_RX_TOKEN2;

              `PID_SOF:
                    next_state_r  = STATE_RX_SOF2;

              `PID_DATA0, `PID_DATA1, `PID_DATA2, `PID_MDATA:
              begin
                    next_state_r  = STATE_RX_DATA;
              end

              `PID_ACK, `PID_NAK, `PID_STALL, `PID_NYET:
                    next_state_r  = STATE_RX_IDLE;

              default :
                    next_state_r  = STATE_RX_IGNORED;
           endcase
       end
    end




    STATE_RX_IGNORED :
    begin

        if (!rx_active_w)
           next_state_r = STATE_RX_IDLE;
    end




    STATE_RX_SOF2 :
    begin
       if (data_ready_w)
           next_state_r = STATE_RX_SOF3;
       else if (!rx_active_w)
           next_state_r = STATE_RX_IDLE;
    end




    STATE_RX_SOF3 :
    begin
       if (data_ready_w || !rx_active_w)
           next_state_r = STATE_RX_IDLE;
    end




    STATE_RX_TOKEN2 :
    begin
       if (data_ready_w)
           next_state_r = STATE_RX_TOKEN3;
       else if (!rx_active_w)
           next_state_r = STATE_RX_IDLE;
    end




    STATE_RX_TOKEN3 :
    begin
       if (data_ready_w)
           next_state_r = STATE_RX_TOKEN_COMPLETE;
       else if (!rx_active_w)
           next_state_r = STATE_RX_IDLE;
    end




    STATE_RX_TOKEN_COMPLETE :
    begin
        next_state_r  = STATE_RX_IDLE;
    end




    STATE_RX_DATA :
    begin

       if (crc_byte_w)
            next_state_r = STATE_RX_DATA_COMPLETE;
    end




    STATE_RX_DATA_COMPLETE :
    begin
        if (!rx_active_w)
            next_state_r = STATE_RX_IDLE;
    end

    default :
       ;

    endcase
end


always @ (posedge clk_i or posedge rst_i)
if (rst_i)
    state_q   <= STATE_RX_IDLE;
else if (!enable_i)
    state_q   <= STATE_RX_IDLE;
else
    state_q   <= next_state_r;




reg handshake_valid_q;

always @ (posedge clk_i or posedge rst_i)
if (rst_i)
    handshake_valid_q <= 1'b0;
else if (state_q == STATE_RX_IDLE && data_ready_w)
begin
    case (data_w)
    `PID_ACK, `PID_NAK, `PID_STALL, `PID_NYET:
        handshake_valid_q <= address_match_w;
    default :
        handshake_valid_q <= 1'b0;
    endcase
end
else
    handshake_valid_q <= 1'b0;

assign handshake_valid_o = handshake_valid_q;




always @ (posedge clk_i or posedge rst_i)
if (rst_i)
    frame_num_q         <= `USB_FRAME_W'b0;
else if (state_q == STATE_RX_SOF2 && data_ready_w)
    frame_num_q         <= {3'b0, data_w};
else if (state_q == STATE_RX_SOF3 && data_ready_w)
    frame_num_q         <= {data_w[2:0], frame_num_q[7:0]};
else if (!enable_i)
    frame_num_q         <= `USB_FRAME_W'b0;

assign frame_number_o = frame_num_q;

reg frame_valid_q;

always @ (posedge clk_i or posedge rst_i)
if (rst_i)
    frame_valid_q <= 1'b0;
else
    frame_valid_q <= (state_q == STATE_RX_SOF3 && data_ready_w);

assign frame_valid_o = frame_valid_q;




always @ (posedge clk_i or posedge rst_i)
if (rst_i)
    token_pid_q <= `USB_PID_W'b0;
else if (state_q == STATE_RX_IDLE && data_ready_w)
    token_pid_q <= data_w;
else if (!enable_i)
    token_pid_q <= `USB_PID_W'b0;

assign pid_o = token_pid_q;

reg token_valid_q;

always @ (posedge clk_i or posedge rst_i)
if (rst_i)
    token_valid_q <= 1'b0;
else
    token_valid_q <= (state_q == STATE_RX_TOKEN_COMPLETE) && address_match_w;

assign token_valid_o = token_valid_q;




always @ (posedge clk_i or posedge rst_i)
if (rst_i)
    token_dev_q <= `USB_DEV_W'b0;
else if (state_q == STATE_RX_TOKEN2 && data_ready_w)
    token_dev_q <= data_w[6:0];
else if (!enable_i)
    token_dev_q <= `USB_DEV_W'b0;

assign token_addr_o = token_dev_q;




always @ (posedge clk_i or posedge rst_i)
if (rst_i)
    token_ep_q      <= `USB_EP_W'b0;
else if (state_q == STATE_RX_TOKEN2 && data_ready_w)
    token_ep_q[0]   <= data_w[7];
else if (state_q == STATE_RX_TOKEN3 && data_ready_w)
    token_ep_q[3:1] <= data_w[2:0];
else if (!enable_i)
    token_ep_q      <= `USB_EP_W'b0;

assign token_ep_o = token_ep_q;
assign token_crc_err_o = 1'b0;

wire [7:0] input_data_w  = data_w;
wire       input_ready_w = state_q == STATE_RX_DATA && data_ready_w && !crc_byte_w;




reg [15:0]  crc_sum_q;
wire [15:0] crc_out_w;
reg         crc_err_q;

usbf_crc16
u_crc16
(
    .crc_in_i(crc_sum_q),
    .din_i(data_w),
    .crc_out_o(crc_out_w)
);

always @ (posedge clk_i or posedge rst_i)
if (rst_i)
    crc_sum_q   <= 16'hFFFF;
else if (state_q == STATE_RX_IDLE)
    crc_sum_q   <= 16'hFFFF;
else if (data_ready_w)
    crc_sum_q   <= crc_out_w;

always @ (posedge clk_i or posedge rst_i)
if (rst_i)
    crc_err_q   <= 1'b0;
else if (state_q == STATE_RX_IDLE)
    crc_err_q   <= 1'b0;
else if (state_q == STATE_RX_DATA_COMPLETE && next_state_r == STATE_RX_IDLE)
    crc_err_q   <= (crc_sum_q != 16'hB001);

assign data_crc_err_o = crc_err_q;

reg data_complete_q;

always @ (posedge clk_i or posedge rst_i)
if (rst_i)
    data_complete_q   <= 1'b0;
else if (state_q == STATE_RX_DATA_COMPLETE && next_state_r == STATE_RX_IDLE)
    data_complete_q   <= 1'b1;
else
    data_complete_q   <= 1'b0;

assign data_complete_o = data_complete_q;

reg data_zlp_q;

always @ (posedge clk_i or posedge rst_i)
if (rst_i)
    data_zlp_q   <= 1'b0;
else if (state_q == STATE_RX_IDLE && next_state_r == STATE_RX_DATA)
    data_zlp_q   <= 1'b1;
else if (input_ready_w)
    data_zlp_q   <= 1'b0;




reg        valid_q;
reg        last_q;
reg [7:0]  data_q;
reg        mask_q;

always @ (posedge clk_i or posedge rst_i)
if (rst_i)
begin
    valid_q  <= 1'b0;
    data_q   <= 8'b0;
    mask_q   <= 1'b0;
    last_q   <= 1'b0;
end
else
begin
    valid_q  <= input_ready_w || ((state_q == STATE_RX_DATA) && crc_byte_w && data_zlp_q);
    data_q   <= input_data_w;
    mask_q   <= input_ready_w;
    last_q   <= (state_q == STATE_RX_DATA) && crc_byte_w;
end


assign data_valid_o = valid_q;
assign data_strb_o  = mask_q;
assign data_o       = data_q;
assign data_last_o  = last_q | crc_byte_w;


endmodule