data/full_repos/permissive/41807012/rtl/control/sdio_card_control.v
1: b"%Error: data/full_repos/permissive/41807012/rtl/control/sdio_card_control.v:187: Define or directive not defined: '`CMD_RSP_CMD'\n        response_value[`CMD_RSP_CMD]          <=  i_cmd;\n                       ^~~~~~~~~~~~\n%Error: data/full_repos/permissive/41807012/rtl/control/sdio_card_control.v:187: syntax error, unexpected ']', expecting TYPE-IDENTIFIER\n        response_value[`CMD_RSP_CMD]          <=  i_cmd;\n                                   ^\n%Error: data/full_repos/permissive/41807012/rtl/control/sdio_card_control.v:188: Define or directive not defined: '`R1_OUT_OF_RANGE'\n        response_value[`R1_OUT_OF_RANGE]      <=  cmd_arg_out_of_range;\n                       ^~~~~~~~~~~~~~~~\n%Error: data/full_repos/permissive/41807012/rtl/control/sdio_card_control.v:188: syntax error, unexpected ']', expecting TYPE-IDENTIFIER\n        response_value[`R1_OUT_OF_RANGE]      <=  cmd_arg_out_of_range;\n                                       ^\n%Error: data/full_repos/permissive/41807012/rtl/control/sdio_card_control.v:189: Define or directive not defined: '`R1_COM_CRC_ERROR'\n        response_value[`R1_COM_CRC_ERROR]     <=  bad_crc;\n                       ^~~~~~~~~~~~~~~~~\n%Error: data/full_repos/permissive/41807012/rtl/control/sdio_card_control.v:189: syntax error, unexpected ']', expecting TYPE-IDENTIFIER\n        response_value[`R1_COM_CRC_ERROR]     <=  bad_crc;\n                                        ^\n%Error: data/full_repos/permissive/41807012/rtl/control/sdio_card_control.v:190: Define or directive not defined: '`R1_ILLEGAL_COMMAND'\n        response_value[`R1_ILLEGAL_COMMAND]   <=  illegal_command;\n                       ^~~~~~~~~~~~~~~~~~~\n%Error: data/full_repos/permissive/41807012/rtl/control/sdio_card_control.v:190: syntax error, unexpected ']', expecting TYPE-IDENTIFIER\n        response_value[`R1_ILLEGAL_COMMAND]   <=  illegal_command;\n                                          ^\n%Error: data/full_repos/permissive/41807012/rtl/control/sdio_card_control.v:191: Define or directive not defined: '`R1_ERROR'\n        response_value[`R1_ERROR]             <=  card_error;\n                       ^~~~~~~~~\n%Error: data/full_repos/permissive/41807012/rtl/control/sdio_card_control.v:191: syntax error, unexpected ']', expecting TYPE-IDENTIFIER\n        response_value[`R1_ERROR]             <=  card_error;\n                                ^\n%Error: data/full_repos/permissive/41807012/rtl/control/sdio_card_control.v:192: Define or directive not defined: '`R1_CURRENT_STATE'\n        response_value[`R1_CURRENT_STATE]     <=  4'hF;\n                       ^~~~~~~~~~~~~~~~~\n%Error: data/full_repos/permissive/41807012/rtl/control/sdio_card_control.v:192: syntax error, unexpected ']', expecting TYPE-IDENTIFIER\n        response_value[`R1_CURRENT_STATE]     <=  4'hF;\n                                        ^\n%Error: data/full_repos/permissive/41807012/rtl/control/sdio_card_control.v:198: Define or directive not defined: '`R4_RSRVD'\n        response_value[`R4_RSRVD]             <=  6'h3F;\n                       ^~~~~~~~~\n%Error: data/full_repos/permissive/41807012/rtl/control/sdio_card_control.v:198: syntax error, unexpected ']', expecting TYPE-IDENTIFIER\n        response_value[`R4_RSRVD]             <=  6'h3F;\n                                ^\n%Error: data/full_repos/permissive/41807012/rtl/control/sdio_card_control.v:199: Define or directive not defined: '`R4_READY'\n        response_value[`R4_READY]             <=  1'b1;\n                       ^~~~~~~~~\n%Error: data/full_repos/permissive/41807012/rtl/control/sdio_card_control.v:199: syntax error, unexpected ']', expecting TYPE-IDENTIFIER\n        response_value[`R4_READY]             <=  1'b1;\n                                ^\n%Error: data/full_repos/permissive/41807012/rtl/control/sdio_card_control.v:200: Define or directive not defined: '`R4_NUM_FUNCS'\n        response_value[`R4_NUM_FUNCS]         <=  `NUM_FUNCS;\n                       ^~~~~~~~~~~~~\n%Error: data/full_repos/permissive/41807012/rtl/control/sdio_card_control.v:200: syntax error, unexpected ']', expecting TYPE-IDENTIFIER\n        response_value[`R4_NUM_FUNCS]         <=  `NUM_FUNCS;\n                                    ^\n%Error: data/full_repos/permissive/41807012/rtl/control/sdio_card_control.v:200: Define or directive not defined: '`NUM_FUNCS'\n        response_value[`R4_NUM_FUNCS]         <=  `NUM_FUNCS;\n                                                  ^~~~~~~~~~\n%Error: data/full_repos/permissive/41807012/rtl/control/sdio_card_control.v:201: Define or directive not defined: '`R4_MEM_PRESENT'\n        response_value[`R4_MEM_PRESENT]       <=  `MEM_PRESENT;\n                       ^~~~~~~~~~~~~~~\n%Error: data/full_repos/permissive/41807012/rtl/control/sdio_card_control.v:201: syntax error, unexpected ']', expecting TYPE-IDENTIFIER\n        response_value[`R4_MEM_PRESENT]       <=  `MEM_PRESENT;\n                                      ^\n%Error: data/full_repos/permissive/41807012/rtl/control/sdio_card_control.v:201: Define or directive not defined: '`MEM_PRESENT'\n        response_value[`R4_MEM_PRESENT]       <=  `MEM_PRESENT;\n                                                  ^~~~~~~~~~~~\n%Error: data/full_repos/permissive/41807012/rtl/control/sdio_card_control.v:202: Define or directive not defined: '`R4_UHSII_AVAILABLE'\n        response_value[`R4_UHSII_AVAILABLE]   <=  `UHSII_AVAILABLE;\n                       ^~~~~~~~~~~~~~~~~~~\n%Error: data/full_repos/permissive/41807012/rtl/control/sdio_card_control.v:202: syntax error, unexpected ']', expecting TYPE-IDENTIFIER\n        response_value[`R4_UHSII_AVAILABLE]   <=  `UHSII_AVAILABLE;\n                                          ^\n%Error: data/full_repos/permissive/41807012/rtl/control/sdio_card_control.v:202: Define or directive not defined: '`UHSII_AVAILABLE'\n        response_value[`R4_UHSII_AVAILABLE]   <=  `UHSII_AVAILABLE;\n                                                  ^~~~~~~~~~~~~~~~\n%Error: data/full_repos/permissive/41807012/rtl/control/sdio_card_control.v:203: Define or directive not defined: '`R4_S18A'\n        response_value[`R4_S18A]              <=  v1p8_sel;\n                       ^~~~~~~~\n%Error: data/full_repos/permissive/41807012/rtl/control/sdio_card_control.v:203: syntax error, unexpected ']', expecting TYPE-IDENTIFIER\n        response_value[`R4_S18A]              <=  v1p8_sel;\n                               ^\n%Error: data/full_repos/permissive/41807012/rtl/control/sdio_card_control.v:205: Define or directive not defined: '`R4_IO_OCR'\n        response_value[`R4_IO_OCR]            <=  `OCR_VALUE;\n                       ^~~~~~~~~~\n%Error: data/full_repos/permissive/41807012/rtl/control/sdio_card_control.v:205: syntax error, unexpected ']', expecting TYPE-IDENTIFIER\n        response_value[`R4_IO_OCR]            <=  `OCR_VALUE;\n                                 ^\n%Error: data/full_repos/permissive/41807012/rtl/control/sdio_card_control.v:205: Define or directive not defined: '`OCR_VALUE'\n        response_value[`R4_IO_OCR]            <=  `OCR_VALUE;\n                                                  ^~~~~~~~~~\n%Error: data/full_repos/permissive/41807012/rtl/control/sdio_card_control.v:212: Define or directive not defined: '`CMD_RSP_CMD'\n        response_value[`CMD_RSP_CMD]          <=  i_cmd;\n                       ^~~~~~~~~~~~\n%Error: data/full_repos/permissive/41807012/rtl/control/sdio_card_control.v:212: syntax error, unexpected ']', expecting TYPE-IDENTIFIER\n        response_value[`CMD_RSP_CMD]          <=  i_cmd;\n                                   ^\n%Error: data/full_repos/permissive/41807012/rtl/control/sdio_card_control.v:213: Define or directive not defined: '`R5_FLAG_CRC_ERROR'\n        response_value[`R5_FLAG_CRC_ERROR]    <=  bad_crc;\n                       ^~~~~~~~~~~~~~~~~~\n%Error: data/full_repos/permissive/41807012/rtl/control/sdio_card_control.v:213: syntax error, unexpected ']', expecting TYPE-IDENTIFIER\n        response_value[`R5_FLAG_CRC_ERROR]    <=  bad_crc;\n                                         ^\n%Error: data/full_repos/permissive/41807012/rtl/control/sdio_card_control.v:214: Define or directive not defined: '`R5_INVALID_CMD'\n        response_value[`R5_INVALID_CMD]       <=  illegal_command;\n                       ^~~~~~~~~~~~~~~\n%Error: data/full_repos/permissive/41807012/rtl/control/sdio_card_control.v:214: syntax error, unexpected ']', expecting TYPE-IDENTIFIER\n        response_value[`R5_INVALID_CMD]       <=  illegal_command;\n                                      ^\n%Error: data/full_repos/permissive/41807012/rtl/control/sdio_card_control.v:215: Define or directive not defined: '`R5_FLAG_CURR_STATE'\n        response_value[`R5_FLAG_CURR_STATE]   <=  r5_cmd;\n                       ^~~~~~~~~~~~~~~~~~~\n%Error: data/full_repos/permissive/41807012/rtl/control/sdio_card_control.v:215: syntax error, unexpected ']', expecting TYPE-IDENTIFIER\n        response_value[`R5_FLAG_CURR_STATE]   <=  r5_cmd;\n                                          ^\n%Error: data/full_repos/permissive/41807012/rtl/control/sdio_card_control.v:216: Define or directive not defined: '`R5_FLAG_ERROR'\n        response_value[`R5_FLAG_ERROR]        <=  card_error;\n                       ^~~~~~~~~~~~~~\n%Error: data/full_repos/permissive/41807012/rtl/control/sdio_card_control.v:216: syntax error, unexpected ']', expecting TYPE-IDENTIFIER\n        response_value[`R5_FLAG_ERROR]        <=  card_error;\n                                     ^\n%Error: data/full_repos/permissive/41807012/rtl/control/sdio_card_control.v:217: Define or directive not defined: '`R5_DATA'\n        response_value[`R5_DATA]              <=  cmd_data;\n                       ^~~~~~~~\n%Error: data/full_repos/permissive/41807012/rtl/control/sdio_card_control.v:217: syntax error, unexpected ']', expecting TYPE-IDENTIFIER\n        response_value[`R5_DATA]              <=  cmd_data;\n                               ^\n%Error: data/full_repos/permissive/41807012/rtl/control/sdio_card_control.v:223: Define or directive not defined: '`CMD_RSP_CMD'\n        response_value[`CMD_RSP_CMD]          <=  i_cmd;\n                       ^~~~~~~~~~~~\n%Error: data/full_repos/permissive/41807012/rtl/control/sdio_card_control.v:223: syntax error, unexpected ']', expecting TYPE-IDENTIFIER\n        response_value[`CMD_RSP_CMD]          <=  i_cmd;\n                                   ^\n%Error: data/full_repos/permissive/41807012/rtl/control/sdio_card_control.v:224: Define or directive not defined: '`R6_REL_ADDR'\n        response_value[`R6_REL_ADDR]          <=  register_card_address;\n                       ^~~~~~~~~~~~\n%Error: data/full_repos/permissive/41807012/rtl/control/sdio_card_control.v:224: syntax error, unexpected ']', expecting TYPE-IDENTIFIER\n        response_value[`R6_REL_ADDR]          <=  register_card_address;\n                                   ^\n%Error: data/full_repos/permissive/41807012/rtl/control/sdio_card_control.v:225: Define or directive not defined: '`R6_STS_CRC_COMM_ERR'\n        response_value[`R6_STS_CRC_COMM_ERR]  <=  bad_crc;\n                       ^~~~~~~~~~~~~~~~~~~~\n%Error: data/full_repos/permissive/41807012/rtl/control/sdio_card_control.v:225: syntax error, unexpected ']', expecting TYPE-IDENTIFIER\n        response_value[`R6_STS_CRC_COMM_ERR]  <=  bad_crc;\n                                           ^\n%Error: data/full_repos/permissive/41807012/rtl/control/sdio_card_control.v:226: Define or directive not defined: '`R6_STS_ILLEGAL_CMD'\n        response_value[`R6_STS_ILLEGAL_CMD]   <=  illegal_command;\n                       ^~~~~~~~~~~~~~~~~~~\n%Error: data/full_repos/permissive/41807012/rtl/control/sdio_card_control.v:226: syntax error, unexpected ']', expecting TYPE-IDENTIFIER\n        response_value[`R6_STS_ILLEGAL_CMD]   <=  illegal_command;\n                                          ^\n%Error: Exiting due to too many errors encountered; --error-limit=50\n"
module sdio_card_control (

  input                     sdio_clk,
  input                     rst,
  input                     i_soft_reset,
  input         [7:0]       i_func_interrupt,
  input         [7:0]       i_func_interrupt_en,
  output                    o_interrupt,


  output                    o_mem_en,

  output  reg               o_func_activate,
  input                     i_func_finished,

  output  reg   [3:0]       o_func_num,
  output  reg               o_func_inc_addr,
  output  reg               o_func_block_mode,

  output  reg               o_func_write_flag,
  output  reg               o_func_rd_after_wr,
  output  reg   [17:0]      o_func_addr,
  output  reg   [12:0]      o_func_data_count,


  output  reg               o_cmd_bus_sel,
  output  reg   [7:0]       o_func_write_data,
  input         [7:0]       i_func_read_data,


  output  reg               o_tunning_block,


  input                     i_cmd_stb,
  input                     i_cmd_crc_good_stb,
  input         [5:0]       i_cmd,
  input         [31:0]      i_cmd_arg,
  input                     i_cmd_phy_idle,

  input                     i_chip_select_n,

  output  reg               o_rsps_stb,
  output        [39:0]      o_rsps,
  output        [7:0]       o_rsps_len,
  output  reg               o_rsps_fail,
  input                     i_rsps_idle

);


localparam      NORMAL_RESPONSE     = 1'b0;
localparam      EXTENDED_RESPONSE   = 1'b1;

localparam      RESET               = 4'h0;
localparam      INITIALIZE          = 4'h1;
localparam      STANDBY             = 4'h2;
localparam      COMMAND             = 4'h3;
localparam      TRANSFER            = 4'h4;
localparam      INACTIVE            = 4'h5;

localparam      R1                  = 4'h1;
localparam      R2                  = 4'h2;
localparam      R3                  = 4'h3;
localparam      R4                  = 4'h4;
localparam      R5                  = 4'h5;
localparam      R6                  = 4'h6;
localparam      R7                  = 4'h7;


reg             [3:0]       state;

reg             [47:0]      response_value;

reg                         response_type;
reg             [15:0]      register_card_address;
reg             [23:0]      voltage_select;
reg                         v1p8_sel;

reg                         bad_crc;
reg                         cmd_arg_out_of_range;
reg                         illegal_command;
reg                         card_error;

reg             [3:0]       response_index;

wire            [1:0]       r5_cmd;
wire            [15:0]      max_f0_block_size;
wire                        enable_async_interrupt;
wire                        data_txrx_in_progress_flag;

reg             [3:0]       component_select;
reg                         rsps_stb;

reg                         direct_read_write;
reg             [17:0]      data_count;
reg             [7:0]       cmd_data;






assign  o_rsps      = response_value[47:8];
assign  o_rsps_len  = 40;

assign  r5_cmd      = (state == RESET) || (state == INITIALIZE) || (state == STANDBY) || (state == INACTIVE) ? 2'b00 :
                      (state == COMMAND)                                                                     ? 2'b01 :
                      (state == TRANSFER)                                                                    ? 2'b10 :
                                                                                                               2'b11;



assign  o_mem_en      = 1'b0;
assign  o_interrupt   = ((i_func_interrupt & i_func_interrupt_en) > 0);


always @ (posedge sdio_clk) begin
  o_rsps_stb                <=  0;
  if (rst) begin
    response_type           <=  NORMAL_RESPONSE;
    response_value          <=  48'h00000000;

  end
  else if (rsps_stb) begin


    o_rsps_stb                                <=  1;
    case (response_index)
      R1: begin

        response_type                         <=  NORMAL_RESPONSE;
        response_value                        <=  48'h0;
        response_value[`CMD_RSP_CMD]          <=  i_cmd;
        response_value[`R1_OUT_OF_RANGE]      <=  cmd_arg_out_of_range;
        response_value[`R1_COM_CRC_ERROR]     <=  bad_crc;
        response_value[`R1_ILLEGAL_COMMAND]   <=  illegal_command;
        response_value[`R1_ERROR]             <=  card_error;
        response_value[`R1_CURRENT_STATE]     <=  4'hF;
      end
      R4: begin

        response_type                         <=  NORMAL_RESPONSE;
        response_value                        <=  48'h0;
        response_value[`R4_RSRVD]             <=  6'h3F;
        response_value[`R4_READY]             <=  1'b1;
        response_value[`R4_NUM_FUNCS]         <=  `NUM_FUNCS;
        response_value[`R4_MEM_PRESENT]       <=  `MEM_PRESENT;
        response_value[`R4_UHSII_AVAILABLE]   <=  `UHSII_AVAILABLE;
        response_value[`R4_S18A]              <=  v1p8_sel;

        response_value[`R4_IO_OCR]            <=  `OCR_VALUE;

      end
      R5: begin

        response_type                         <=  NORMAL_RESPONSE;
        response_value                        <=  48'h0;
        response_value[`CMD_RSP_CMD]          <=  i_cmd;
        response_value[`R5_FLAG_CRC_ERROR]    <=  bad_crc;
        response_value[`R5_INVALID_CMD]       <=  illegal_command;
        response_value[`R5_FLAG_CURR_STATE]   <=  r5_cmd;
        response_value[`R5_FLAG_ERROR]        <=  card_error;
        response_value[`R5_DATA]              <=  cmd_data;
      end
      R6: begin

        response_type                         <=  NORMAL_RESPONSE;
        response_value                        <=  48'h0;
        response_value[`CMD_RSP_CMD]          <=  i_cmd;
        response_value[`R6_REL_ADDR]          <=  register_card_address;
        response_value[`R6_STS_CRC_COMM_ERR]  <=  bad_crc;
        response_value[`R6_STS_ILLEGAL_CMD]   <=  illegal_command;
        response_value[`R6_STS_ERROR]         <=  card_error;
      end
      R7: begin

        response_type                         <=  NORMAL_RESPONSE;
        response_value                        <=  48'h0;
        response_value[`CMD_RSP_CMD]          <=  i_cmd;
        response_value[`R7_VHS]               <=  i_cmd_arg[`CMD8_ARG_VHS];
        response_value[`R7_PATTERN]           <=  i_cmd_arg[`CMD8_ARG_PATTERN];
      end
      default: begin
        response_value                        <=  48'h0;
      end
    endcase
  end
end

always @ (posedge sdio_clk) begin

  rsps_stb                          <=  0;
  o_func_activate                   <=  0;

  if (rst || i_soft_reset) begin
    state                           <=  INITIALIZE;
    register_card_address           <=  16'h0001;
    voltage_select                  <=  `OCR_VALUE;
    v1p8_sel                        <=  0;

    bad_crc                         <=  0;
    cmd_arg_out_of_range            <=  0;
    illegal_command                 <=  0;
    card_error                      <=  0;

    o_func_inc_addr                 <=  0;
    o_func_block_mode               <=  0;
    o_func_num                      <=  4'h0;
    o_func_write_flag               <=  0;
    o_func_rd_after_wr              <=  0;
    o_func_addr                     <=  18'h0;
    o_func_write_data               <=  8'h00;
    o_func_data_count               <=  12'h00;
    data_count                      <=  0;

    response_index                  <=  0;
    o_tunning_block                 <=  0;

    cmd_data                        <=  0;
    o_cmd_bus_sel                   <=  1;
    o_rsps_fail                     <=  0;

  end
  else if (i_cmd_stb && !i_cmd_crc_good_stb) begin
    bad_crc                         <=  1;
    o_rsps_fail                     <=  1;

  end
  else
    if (rsps_stb) begin

      bad_crc                       <=  0;
      cmd_arg_out_of_range          <=  0;
      illegal_command               <=  0;
      card_error                    <=  0;
      o_rsps_fail                   <=  0;
    end

    case (state)
      RESET: begin

        o_cmd_bus_sel               <= 1;
        state                       <= INITIALIZE;
      end
      INITIALIZE: begin

        o_cmd_bus_sel               <= 1;
        if (i_cmd_stb) begin

          case (i_cmd)
            `SD_CMD_IO_SEND_OP_CMD: begin
              v1p8_sel              <=  i_cmd_arg[`CMD5_ARG_S18R];

              voltage_select        <=  `OCR_VALUE;
              response_index        <=  R4;
              rsps_stb              <=  1;
            end
            `SD_CMD_SEND_RELATIVE_ADDR: begin
              state                 <=  STANDBY;
              response_index        <=  R6;

              rsps_stb              <=  1;
            end
            `SD_CMD_GO_INACTIVE_STATE: begin
              state                 <=  INACTIVE;
            end
            default: begin
              illegal_command       <=  1;
              o_rsps_fail           <=  1;
            end
          endcase
        end
      end
      STANDBY: begin

        o_cmd_bus_sel               <= 1;
        if (i_cmd_stb) begin
          case (i_cmd)
            `SD_CMD_SEND_RELATIVE_ADDR: begin
              $display("SD CMD Send Relative Address");
              state                 <=  STANDBY;
              response_index        <=  R6;
              rsps_stb              <=  1;
            end
            `SD_CMD_SEL_DESEL_CARD: begin
              response_index        <=  R1;
              if (register_card_address == i_cmd_arg[`CMD7_RCA]) begin
                $display("SD CMD Send Relative Address: Card Selected");
                state               <= COMMAND;
                rsps_stb            <=  1;
              end
              else begin
                $display("SD CMD Send Relative Address: Incorrect Address");
                o_rsps_fail         <=  1;
              end
            end
            `SD_CMD_GO_INACTIVE_STATE: begin
              $display("SD CMD Go to inactive state");
              state                 <=  INACTIVE;
            end
            default: begin
              illegal_command       <=  1;
              o_rsps_fail           <=  1;
            end
          endcase
        end
      end
      COMMAND: begin

        o_cmd_bus_sel               <= 1;
        data_count                  <= 0;
        if (i_cmd_stb) begin
          direct_read_write         <= 0;
          case (i_cmd)
            `SD_CMD_IO_RW_DIRECT: begin
              $display("SD CMD IO RW");
              o_func_write_flag     <= i_cmd_arg[`CMD52_ARG_RW_FLAG ];
              o_func_rd_after_wr    <= i_cmd_arg[`CMD52_ARG_RAW_FLAG];
              o_func_num            <= i_cmd_arg[`CMD52_ARG_FNUM    ];
              o_func_addr           <= i_cmd_arg[`CMD52_ARG_REG_ADDR];
              o_func_write_data     <= i_cmd_arg[`CMD52_ARG_WR_DATA ];
              o_func_inc_addr       <= 0;
              o_func_data_count     <= 1;
              state                 <= TRANSFER;
              direct_read_write     <= 1;
              response_index        <= R5;
              cmd_data              <= 0;
              o_func_activate       <= 1;
            end
            `SD_CMD_IO_RW_EXTENDED: begin
              $display("SD CMD IO RW Extended");
              o_func_write_flag     <= i_cmd_arg[`CMD53_ARG_RW_FLAG   ];
              o_func_rd_after_wr    <= 0;
              o_func_num            <= i_cmd_arg[`CMD53_ARG_FNUM      ];
              o_func_addr           <= i_cmd_arg[`CMD53_ARG_REG_ADDR  ];
              o_func_data_count     <= {4'h0, i_cmd_arg[`CMD53_ARG_DATA_COUNT]};
              o_func_block_mode     <= i_cmd_arg[`CMD53_ARG_BLOCK_MODE];
              o_func_inc_addr       <= i_cmd_arg[`CMD53_ARG_INC_ADDR  ];
              rsps_stb              <= 1;
              state                 <= TRANSFER;
              response_index        <=  R5;
              o_cmd_bus_sel         <=  0;
            end
            `SD_CMD_SEL_DESEL_CARD: begin
              if (register_card_address != i_cmd_arg[`CMD7_RCA]) begin
                $display("Card Deselected");
                state               <= STANDBY;
              end
              response_index        <= R1;
              rsps_stb              <= 1;
            end
            `SD_CMD_SEND_TUNNING_BLOCK: begin
              $display("SD CMD Send tunning block");
              response_index        <= R1;
              rsps_stb              <= 1;
              o_tunning_block       <= 1;
            end
            `SD_CMD_GO_INACTIVE_STATE: begin
              $display("SD CMD Go to inactive state");
              state                 <= INACTIVE;
            end
            default: begin
              illegal_command       <= 1;
              o_rsps_fail           <= 1;
            end
          endcase
        end
      end
      TRANSFER: begin
        if (o_cmd_bus_sel) begin
          o_func_activate             <= 1;

          if (i_func_finished) begin
            o_func_activate           <= 0;
            rsps_stb                  <= 1;
            if (o_func_rd_after_wr || !o_func_write_flag) begin
              cmd_data                <= i_func_read_data;
            end
            else begin
              cmd_data                <= 0;
            end
            state                     <= COMMAND;
          end
        end
        else if (i_rsps_idle) begin

          o_func_activate           <=  1;
          if (i_func_finished) begin
            o_func_activate         <=  0;
            state                   <= COMMAND;
          end
        end
        if (i_cmd_stb) begin
          case (i_cmd)
            `SD_CMD_IO_RW_DIRECT: begin
              $display("Direct Read/Write");
              o_func_write_flag     <= i_cmd_arg[`CMD52_ARG_RW_FLAG ];
              o_func_rd_after_wr    <= i_cmd_arg[`CMD52_ARG_RAW_FLAG];
              o_func_num            <= i_cmd_arg[`CMD52_ARG_FNUM    ];
              o_func_addr           <= i_cmd_arg[`CMD52_ARG_REG_ADDR];
              o_func_write_data     <= i_cmd_arg[`CMD52_ARG_WR_DATA ];
              o_func_activate       <= 0;
              rsps_stb              <= 1;
              o_func_data_count     <= 1;
              cmd_data              <= 0;


            end
            default: begin
              illegal_command       <= 1;
              o_rsps_fail           <= 1;
            end
          endcase
        end
      end
      INACTIVE: begin


        o_rsps_fail                 <= 1;
      end
      default: begin
      end
    endcase



    if (i_cmd_stb) begin
      case (i_cmd)
        `SD_CMD_GO_IDLE_STATE: begin
          $display ("Initialize SD or SPI Mode, SPI MODE NOT SUPPORTED NOW!!");
          o_rsps_fail               <=  0;
          illegal_command           <=  0;
          response_index            <=  R1;
          if (!i_chip_select_n) begin

          end
          else begin

            rsps_stb                <=  1;
          end
        end
        `SD_CMD_SEND_IF_COND: begin
          $display ("Send Interface Condition");
          o_rsps_fail               <=  0;
          illegal_command           <=  0;
          response_index            <=  R7;
          if (i_cmd_arg[`CMD8_ARG_VHS] == `VHS_DEFAULT_VALUE) begin
            rsps_stb                <=  1;
          end
        end
        `SD_CMD_VOLTAGE_SWITCH: begin
          $display ("Voltage Mode Switch");
          o_rsps_fail               <=  0;
          illegal_command           <=  0;
          response_index            <=  R1;
          rsps_stb                  <=  1;
        end
        default: begin
        end
      endcase
  end
end

endmodule
module sdio_card_control (

  input                     sdio_clk,
  input                     rst,
  input                     i_soft_reset,
  input         [7:0]       i_func_interrupt,
  input         [7:0]       i_func_interrupt_en,
  output                    o_interrupt,


  output                    o_mem_en,

  output  reg               o_func_activate,
  input                     i_func_finished,

  output  reg   [3:0]       o_func_num,
  output  reg               o_func_inc_addr,
  output  reg               o_func_block_mode,

  output  reg               o_func_write_flag,
  output  reg               o_func_rd_after_wr,
  output  reg   [17:0]      o_func_addr,
  output  reg   [12:0]      o_func_data_count,


  output  reg               o_cmd_bus_sel,
  output  reg   [7:0]       o_func_write_data,
  input         [7:0]       i_func_read_data,


  output  reg               o_tunning_block,


  input                     i_cmd_stb,
  input                     i_cmd_crc_good_stb,
  input         [5:0]       i_cmd,
  input         [31:0]      i_cmd_arg,
  input                     i_cmd_phy_idle,

  input                     i_chip_select_n,

  output  reg               o_rsps_stb,
  output        [39:0]      o_rsps,
  output        [7:0]       o_rsps_len,
  output  reg               o_rsps_fail,
  input                     i_rsps_idle

);



localparam      NORMAL_RESPONSE     = 1'b0;
localparam      EXTENDED_RESPONSE   = 1'b1;

localparam      RESET               = 4'h0;
localparam      INITIALIZE          = 4'h1;
localparam      STANDBY             = 4'h2;
localparam      COMMAND             = 4'h3;
localparam      TRANSFER            = 4'h4;
localparam      INACTIVE            = 4'h5;

localparam      R1                  = 4'h1;
localparam      R2                  = 4'h2;
localparam      R3                  = 4'h3;
localparam      R4                  = 4'h4;
localparam      R5                  = 4'h5;
localparam      R6                  = 4'h6;
localparam      R7                  = 4'h7;


reg             [3:0]       state;

reg             [47:0]      response_value;

reg                         response_type;
reg             [15:0]      register_card_address;
reg             [23:0]      voltage_select;
reg                         v1p8_sel;

reg                         bad_crc;
reg                         cmd_arg_out_of_range;
reg                         illegal_command;
reg                         card_error;

reg             [3:0]       response_index;

wire            [1:0]       r5_cmd;
wire            [15:0]      max_f0_block_size;
wire                        enable_async_interrupt;
wire                        data_txrx_in_progress_flag;

reg             [3:0]       component_select;
reg                         rsps_stb;

reg                         direct_read_write;
reg             [17:0]      data_count;
reg             [7:0]       cmd_data;






assign  o_rsps      = response_value[47:8];
assign  o_rsps_len  = 40;

assign  r5_cmd      = (state == RESET) || (state == INITIALIZE) || (state == STANDBY) || (state == INACTIVE) ? 2'b00 :
                      (state == COMMAND)                                                                     ? 2'b01 :
                      (state == TRANSFER)                                                                    ? 2'b10 :
                                                                                                               2'b11;



assign  o_mem_en      = 1'b0;
assign  o_interrupt   = ((i_func_interrupt & i_func_interrupt_en) > 0);


always @ (posedge sdio_clk) begin
  o_rsps_stb                <=  0;
  if (rst) begin
    response_type           <=  NORMAL_RESPONSE;
    response_value          <=  48'h00000000;

  end
  else if (rsps_stb) begin


    o_rsps_stb                                <=  1;
    case (response_index)
      R1: begin

        response_type                         <=  NORMAL_RESPONSE;
        response_value                        <=  48'h0;
        response_value[`CMD_RSP_CMD]          <=  i_cmd;
        response_value[`R1_OUT_OF_RANGE]      <=  cmd_arg_out_of_range;
        response_value[`R1_COM_CRC_ERROR]     <=  bad_crc;
        response_value[`R1_ILLEGAL_COMMAND]   <=  illegal_command;
        response_value[`R1_ERROR]             <=  card_error;
        response_value[`R1_CURRENT_STATE]     <=  4'hF;
      end
      R4: begin

        response_type                         <=  NORMAL_RESPONSE;
        response_value                        <=  48'h0;
        response_value[`R4_RSRVD]             <=  6'h3F;
        response_value[`R4_READY]             <=  1'b1;
        response_value[`R4_NUM_FUNCS]         <=  `NUM_FUNCS;
        response_value[`R4_MEM_PRESENT]       <=  `MEM_PRESENT;
        response_value[`R4_UHSII_AVAILABLE]   <=  `UHSII_AVAILABLE;
        response_value[`R4_S18A]              <=  v1p8_sel;

        response_value[`R4_IO_OCR]            <=  `OCR_VALUE;

      end
      R5: begin

        response_type                         <=  NORMAL_RESPONSE;
        response_value                        <=  48'h0;
        response_value[`CMD_RSP_CMD]          <=  i_cmd;
        response_value[`R5_FLAG_CRC_ERROR]    <=  bad_crc;
        response_value[`R5_INVALID_CMD]       <=  illegal_command;
        response_value[`R5_FLAG_CURR_STATE]   <=  r5_cmd;
        response_value[`R5_FLAG_ERROR]        <=  card_error;
        response_value[`R5_DATA]              <=  cmd_data;
      end
      R6: begin

        response_type                         <=  NORMAL_RESPONSE;
        response_value                        <=  48'h0;
        response_value[`CMD_RSP_CMD]          <=  i_cmd;
        response_value[`R6_REL_ADDR]          <=  register_card_address;
        response_value[`R6_STS_CRC_COMM_ERR]  <=  bad_crc;
        response_value[`R6_STS_ILLEGAL_CMD]   <=  illegal_command;
        response_value[`R6_STS_ERROR]         <=  card_error;
      end
      R7: begin

        response_type                         <=  NORMAL_RESPONSE;
        response_value                        <=  48'h0;
        response_value[`CMD_RSP_CMD]          <=  i_cmd;
        response_value[`R7_VHS]               <=  i_cmd_arg[`CMD8_ARG_VHS];
        response_value[`R7_PATTERN]           <=  i_cmd_arg[`CMD8_ARG_PATTERN];
      end
      default: begin
        response_value                        <=  48'h0;
      end
    endcase
  end
end

always @ (posedge sdio_clk) begin

  rsps_stb                          <=  0;
  o_func_activate                   <=  0;

  if (rst || i_soft_reset) begin
    state                           <=  INITIALIZE;
    register_card_address           <=  16'h0001;
    voltage_select                  <=  `OCR_VALUE;
    v1p8_sel                        <=  0;

    bad_crc                         <=  0;
    cmd_arg_out_of_range            <=  0;
    illegal_command                 <=  0;
    card_error                      <=  0;

    o_func_inc_addr                 <=  0;
    o_func_block_mode               <=  0;
    o_func_num                      <=  4'h0;
    o_func_write_flag               <=  0;
    o_func_rd_after_wr              <=  0;
    o_func_addr                     <=  18'h0;
    o_func_write_data               <=  8'h00;
    o_func_data_count               <=  12'h00;
    data_count                      <=  0;

    response_index                  <=  0;
    o_tunning_block                 <=  0;

    cmd_data                        <=  0;
    o_cmd_bus_sel                   <=  1;
    o_rsps_fail                     <=  0;

  end
  else if (i_cmd_stb && !i_cmd_crc_good_stb) begin
    bad_crc                         <=  1;
    o_rsps_fail                     <=  1;

  end
  else
    if (rsps_stb) begin

      bad_crc                       <=  0;
      cmd_arg_out_of_range          <=  0;
      illegal_command               <=  0;
      card_error                    <=  0;
      o_rsps_fail                   <=  0;
    end

    case (state)
      RESET: begin

        o_cmd_bus_sel               <= 1;
        state                       <= INITIALIZE;
      end
      INITIALIZE: begin

        o_cmd_bus_sel               <= 1;
        if (i_cmd_stb) begin

          case (i_cmd)
            `SD_CMD_IO_SEND_OP_CMD: begin
              v1p8_sel              <=  i_cmd_arg[`CMD5_ARG_S18R];

              voltage_select        <=  `OCR_VALUE;
              response_index        <=  R4;
              rsps_stb              <=  1;
            end
            `SD_CMD_SEND_RELATIVE_ADDR: begin
              state                 <=  STANDBY;
              response_index        <=  R6;

              rsps_stb              <=  1;
            end
            `SD_CMD_GO_INACTIVE_STATE: begin
              state                 <=  INACTIVE;
            end
            default: begin
              illegal_command       <=  1;
              o_rsps_fail           <=  1;
            end
          endcase
        end
      end
      STANDBY: begin

        o_cmd_bus_sel               <= 1;
        if (i_cmd_stb) begin
          case (i_cmd)
            `SD_CMD_SEND_RELATIVE_ADDR: begin
              $display("SD CMD Send Relative Address");
              state                 <=  STANDBY;
              response_index        <=  R6;
              rsps_stb              <=  1;
            end
            `SD_CMD_SEL_DESEL_CARD: begin
              response_index        <=  R1;
              if (register_card_address == i_cmd_arg[`CMD7_RCA]) begin
                $display("SD CMD Send Relative Address: Card Selected");
                state               <= COMMAND;
                rsps_stb            <=  1;
              end
              else begin
                $display("SD CMD Send Relative Address: Incorrect Address");
                o_rsps_fail         <=  1;
              end
            end
            `SD_CMD_GO_INACTIVE_STATE: begin
              $display("SD CMD Go to inactive state");
              state                 <=  INACTIVE;
            end
            default: begin
              illegal_command       <=  1;
              o_rsps_fail           <=  1;
            end
          endcase
        end
      end
      COMMAND: begin

        o_cmd_bus_sel               <= 1;
        data_count                  <= 0;
        if (i_cmd_stb) begin
          direct_read_write         <= 0;
          case (i_cmd)
            `SD_CMD_IO_RW_DIRECT: begin
              $display("SD CMD IO RW");
              o_func_write_flag     <= i_cmd_arg[`CMD52_ARG_RW_FLAG ];
              o_func_rd_after_wr    <= i_cmd_arg[`CMD52_ARG_RAW_FLAG];
              o_func_num            <= i_cmd_arg[`CMD52_ARG_FNUM    ];
              o_func_addr           <= i_cmd_arg[`CMD52_ARG_REG_ADDR];
              o_func_write_data     <= i_cmd_arg[`CMD52_ARG_WR_DATA ];
              o_func_inc_addr       <= 0;
              o_func_data_count     <= 1;
              state                 <= TRANSFER;
              direct_read_write     <= 1;
              response_index        <= R5;
              cmd_data              <= 0;
              o_func_activate       <= 1;
            end
            `SD_CMD_IO_RW_EXTENDED: begin
              $display("SD CMD IO RW Extended");
              o_func_write_flag     <= i_cmd_arg[`CMD53_ARG_RW_FLAG   ];
              o_func_rd_after_wr    <= 0;
              o_func_num            <= i_cmd_arg[`CMD53_ARG_FNUM      ];
              o_func_addr           <= i_cmd_arg[`CMD53_ARG_REG_ADDR  ];
              o_func_data_count     <= {4'h0, i_cmd_arg[`CMD53_ARG_DATA_COUNT]};
              o_func_block_mode     <= i_cmd_arg[`CMD53_ARG_BLOCK_MODE];
              o_func_inc_addr       <= i_cmd_arg[`CMD53_ARG_INC_ADDR  ];
              rsps_stb              <= 1;
              state                 <= TRANSFER;
              response_index        <=  R5;
              o_cmd_bus_sel         <=  0;
            end
            `SD_CMD_SEL_DESEL_CARD: begin
              if (register_card_address != i_cmd_arg[`CMD7_RCA]) begin
                $display("Card Deselected");
                state               <= STANDBY;
              end
              response_index        <= R1;
              rsps_stb              <= 1;
            end
            `SD_CMD_SEND_TUNNING_BLOCK: begin
              $display("SD CMD Send tunning block");
              response_index        <= R1;
              rsps_stb              <= 1;
              o_tunning_block       <= 1;
            end
            `SD_CMD_GO_INACTIVE_STATE: begin
              $display("SD CMD Go to inactive state");
              state                 <= INACTIVE;
            end
            default: begin
              illegal_command       <= 1;
              o_rsps_fail           <= 1;
            end
          endcase
        end
      end
      TRANSFER: begin
        if (o_cmd_bus_sel) begin
          o_func_activate             <= 1;

          if (i_func_finished) begin
            o_func_activate           <= 0;
            rsps_stb                  <= 1;
            if (o_func_rd_after_wr || !o_func_write_flag) begin
              cmd_data                <= i_func_read_data;
            end
            else begin
              cmd_data                <= 0;
            end
            state                     <= COMMAND;
          end
        end
        else if (i_rsps_idle) begin

          o_func_activate           <=  1;
          if (i_func_finished) begin
            o_func_activate         <=  0;
            state                   <= COMMAND;
          end
        end
        if (i_cmd_stb) begin
          case (i_cmd)
            `SD_CMD_IO_RW_DIRECT: begin
              $display("Direct Read/Write");
              o_func_write_flag     <= i_cmd_arg[`CMD52_ARG_RW_FLAG ];
              o_func_rd_after_wr    <= i_cmd_arg[`CMD52_ARG_RAW_FLAG];
              o_func_num            <= i_cmd_arg[`CMD52_ARG_FNUM    ];
              o_func_addr           <= i_cmd_arg[`CMD52_ARG_REG_ADDR];
              o_func_write_data     <= i_cmd_arg[`CMD52_ARG_WR_DATA ];
              o_func_activate       <= 0;
              rsps_stb              <= 1;
              o_func_data_count     <= 1;
              cmd_data              <= 0;


            end
            default: begin
              illegal_command       <= 1;
              o_rsps_fail           <= 1;
            end
          endcase
        end
      end
      INACTIVE: begin


        o_rsps_fail                 <= 1;
      end
      default: begin
      end
    endcase



    if (i_cmd_stb) begin
      case (i_cmd)
        `SD_CMD_GO_IDLE_STATE: begin
          $display ("Initialize SD or SPI Mode, SPI MODE NOT SUPPORTED NOW!!");
          o_rsps_fail               <=  0;
          illegal_command           <=  0;
          response_index            <=  R1;
          if (!i_chip_select_n) begin

          end
          else begin

            rsps_stb                <=  1;
          end
        end
        `SD_CMD_SEND_IF_COND: begin
          $display ("Send Interface Condition");
          o_rsps_fail               <=  0;
          illegal_command           <=  0;
          response_index            <=  R7;
          if (i_cmd_arg[`CMD8_ARG_VHS] == `VHS_DEFAULT_VALUE) begin
            rsps_stb                <=  1;
          end
        end
        `SD_CMD_VOLTAGE_SWITCH: begin
          $display ("Voltage Mode Switch");
          o_rsps_fail               <=  0;
          illegal_command           <=  0;
          response_index            <=  R1;
          rsps_stb                  <=  1;
        end
        default: begin
        end
      endcase
  end
end

endmodule