data/full_repos/permissive/447976230/fpga/usrp3/top/x400/cpld/spi_slave_to_ctrlport_master.v
1: b'%Error: data/full_repos/permissive/447976230/fpga/usrp3/top/x400/cpld/spi_slave_to_ctrlport_master.v:62: Cannot find include file: ../../../lib/rfnoc/core/ctrlport.vh\n  `include "../../../lib/rfnoc/core/ctrlport.vh" \n           ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n        ... Looked in:\n             data/full_repos/permissive/447976230/fpga/usrp3/top/x400/cpld,data/full_repos/permissive/447976230/../../../lib/rfnoc/core/ctrlport.vh\n             data/full_repos/permissive/447976230/fpga/usrp3/top/x400/cpld,data/full_repos/permissive/447976230/../../../lib/rfnoc/core/ctrlport.vh.v\n             data/full_repos/permissive/447976230/fpga/usrp3/top/x400/cpld,data/full_repos/permissive/447976230/../../../lib/rfnoc/core/ctrlport.vh.sv\n             ../../../lib/rfnoc/core/ctrlport.vh\n             ../../../lib/rfnoc/core/ctrlport.vh.v\n             ../../../lib/rfnoc/core/ctrlport.vh.sv\n             obj_dir/../../../lib/rfnoc/core/ctrlport.vh\n             obj_dir/../../../lib/rfnoc/core/ctrlport.vh.v\n             obj_dir/../../../lib/rfnoc/core/ctrlport.vh.sv\n%Error: Exiting due to 1 error(s)\n'
module spi_slave_to_ctrlport_master #(
  parameter CLK_FREQUENCY = 50000000,
  parameter SPI_FREQUENCY = 10000000
) (




  input wire ctrlport_clk,
  input wire ctrlport_rst,

  output wire        m_ctrlport_req_wr,
  output wire        m_ctrlport_req_rd,
  output wire [19:0] m_ctrlport_req_addr,
  output wire [31:0] m_ctrlport_req_data,

  input wire        m_ctrlport_resp_ack,
  input wire [ 1:0] m_ctrlport_resp_status,
  input wire [31:0] m_ctrlport_resp_data,





  input  wire sclk,
  input  wire cs_n,
  input  wire mosi,
  output wire miso
);

  `include "../../../lib/rfnoc/core/ctrlport.vh"





  wire [7:0] data_in;
  wire [7:0] data_out;
  wire       data_in_valid;
  wire       data_out_valid;
  wire       data_in_required;
  wire       spi_slave_active;

  spi_slave #(
    .CLK_FREQUENCY (CLK_FREQUENCY),
    .SPI_FREQUENCY (SPI_FREQUENCY)
  ) spi_slave_async (
    .sclk             (sclk),
    .cs_n             (cs_n),
    .mosi             (mosi),
    .miso             (miso),
    .clk              (ctrlport_clk),
    .rst              (ctrlport_rst),
    .data_in_required (data_in_required),
    .data_in_valid    (data_in_valid),
    .data_in          (data_in),
    .data_out_valid   (data_out_valid),
    .data_out         (data_out),
    .active           (spi_slave_active)
  );





  reg spi_slave_active_delayed = 1'b0;
  always @(posedge ctrlport_clk) begin
    if (ctrlport_rst) begin
      spi_slave_active_delayed <= 1'b0;
    end
    else begin
      spi_slave_active_delayed <= spi_slave_active;
    end
  end


  wire spi_slave_reset;
  assign spi_slave_reset = spi_slave_active_delayed & (~spi_slave_active);





  localparam NUM_BYTES_TRANSACTION           = 8;
  localparam NUM_BYTES_WRITE_REQUEST_PAYLOAD = 6;
  localparam NUM_BYTES_READ_REQUEST_PAYLOAD  = 2;
  localparam MAX_BYTES_RESPONSE_PAYLOAD      = 5;





  reg [3:0] num_bytes_received;
  reg       request_received;
  reg       write_request;
  reg       provide_response;
  reg [NUM_BYTES_WRITE_REQUEST_PAYLOAD*8-1:0] request_reg = {NUM_BYTES_WRITE_REQUEST_PAYLOAD*8 {1'b0}};

  always @(posedge ctrlport_clk) begin
    if (ctrlport_rst || spi_slave_reset) begin
      num_bytes_received <= 4'b0;
      request_received <= 1'b0;
      write_request <= 1'b0;
      provide_response <= 1'b0;
    end
    else begin

      if (data_out_valid) begin

        num_bytes_received <= num_bytes_received + 1'b1;

        if (num_bytes_received == NUM_BYTES_TRANSACTION-1) begin
          num_bytes_received <= 4'b0;
        end
      end


      if (data_out_valid && (num_bytes_received == 0)) begin
        write_request <= data_out[7];
      end


      request_received <= 1'b0;
      if (data_out_valid) begin
        if (write_request && (num_bytes_received == NUM_BYTES_WRITE_REQUEST_PAYLOAD-1)) begin
          request_received <= 1'b1;
          provide_response <= 1'b1;
        end else if (~write_request && (num_bytes_received == NUM_BYTES_READ_REQUEST_PAYLOAD-1)) begin
          request_received <= 1'b1;
          provide_response <= 1'b1;
        end
      end


      if (num_bytes_received == NUM_BYTES_TRANSACTION-1) begin
        provide_response <= 1'b0;
      end


      if (data_out_valid) begin
        request_reg <= {request_reg[NUM_BYTES_WRITE_REQUEST_PAYLOAD*8-8-1:0], data_out};
      end
    end
  end


  localparam SPI_TRANSFER_ADDRESS_WIDTH = 15;
  assign m_ctrlport_req_wr   = request_received && write_request;
  assign m_ctrlport_req_rd   = request_received && ~write_request;
  assign m_ctrlport_req_data = request_reg[CTRLPORT_DATA_W-1:0];
  assign m_ctrlport_req_addr = (write_request) ?
                               {5'b0, request_reg[CTRLPORT_DATA_W+:SPI_TRANSFER_ADDRESS_WIDTH]} :
                               {5'b0, request_reg[0+:SPI_TRANSFER_ADDRESS_WIDTH]};





  reg  [MAX_BYTES_RESPONSE_PAYLOAD*8-1:0] response_reg;
  reg  ready_for_response;
  wire write_response_byte;

  always @(posedge ctrlport_clk) begin
    if (ctrlport_rst || spi_slave_reset) begin
      response_reg <= {8*MAX_BYTES_RESPONSE_PAYLOAD {1'b0}};
      ready_for_response <= 1'b0;
    end
    else begin

      if (request_received) begin
        ready_for_response <= 1'b1;
        if (write_request) begin

          response_reg <= {5'b0, 1'b1, CTRL_STS_CMDERR, {CTRLPORT_DATA_W{1'b0}}};
        end else begin

          response_reg <= {{CTRLPORT_DATA_W{1'b0}}, 5'b0, 1'b1, CTRL_STS_CMDERR};
        end


      end else if (m_ctrlport_resp_ack && ready_for_response) begin
        if (write_request) begin
          response_reg <= {5'b0, m_ctrlport_resp_ack, m_ctrlport_resp_status, {CTRLPORT_DATA_W{1'b0}}};
        end else begin
          response_reg <= {m_ctrlport_resp_data, 5'b0, m_ctrlport_resp_ack, m_ctrlport_resp_status};
        end
      end


      if (write_response_byte) begin
        response_reg <= {response_reg[0+:(MAX_BYTES_RESPONSE_PAYLOAD-1)*8], 8'b0};
        ready_for_response <= 1'b0;
      end
    end
  end


  assign write_response_byte = data_in_required && provide_response;


  assign data_in       = response_reg[(MAX_BYTES_RESPONSE_PAYLOAD-1)*8+:8];
  assign data_in_valid = write_response_byte;

endmodule
module spi_slave_to_ctrlport_master #(
  parameter CLK_FREQUENCY = 50000000,
  parameter SPI_FREQUENCY = 10000000
) (




  input wire ctrlport_clk,
  input wire ctrlport_rst,

  output wire        m_ctrlport_req_wr,
  output wire        m_ctrlport_req_rd,
  output wire [19:0] m_ctrlport_req_addr,
  output wire [31:0] m_ctrlport_req_data,

  input wire        m_ctrlport_resp_ack,
  input wire [ 1:0] m_ctrlport_resp_status,
  input wire [31:0] m_ctrlport_resp_data,





  input  wire sclk,
  input  wire cs_n,
  input  wire mosi,
  output wire miso
);


  `include "../../../lib/rfnoc/core/ctrlport.vh"





  wire [7:0] data_in;
  wire [7:0] data_out;
  wire       data_in_valid;
  wire       data_out_valid;
  wire       data_in_required;
  wire       spi_slave_active;

  spi_slave #(
    .CLK_FREQUENCY (CLK_FREQUENCY),
    .SPI_FREQUENCY (SPI_FREQUENCY)
  ) spi_slave_async (
    .sclk             (sclk),
    .cs_n             (cs_n),
    .mosi             (mosi),
    .miso             (miso),
    .clk              (ctrlport_clk),
    .rst              (ctrlport_rst),
    .data_in_required (data_in_required),
    .data_in_valid    (data_in_valid),
    .data_in          (data_in),
    .data_out_valid   (data_out_valid),
    .data_out         (data_out),
    .active           (spi_slave_active)
  );





  reg spi_slave_active_delayed = 1'b0;
  always @(posedge ctrlport_clk) begin
    if (ctrlport_rst) begin
      spi_slave_active_delayed <= 1'b0;
    end
    else begin
      spi_slave_active_delayed <= spi_slave_active;
    end
  end


  wire spi_slave_reset;
  assign spi_slave_reset = spi_slave_active_delayed & (~spi_slave_active);





  localparam NUM_BYTES_TRANSACTION           = 8;
  localparam NUM_BYTES_WRITE_REQUEST_PAYLOAD = 6;
  localparam NUM_BYTES_READ_REQUEST_PAYLOAD  = 2;
  localparam MAX_BYTES_RESPONSE_PAYLOAD      = 5;





  reg [3:0] num_bytes_received;
  reg       request_received;
  reg       write_request;
  reg       provide_response;
  reg [NUM_BYTES_WRITE_REQUEST_PAYLOAD*8-1:0] request_reg = {NUM_BYTES_WRITE_REQUEST_PAYLOAD*8 {1'b0}};

  always @(posedge ctrlport_clk) begin
    if (ctrlport_rst || spi_slave_reset) begin
      num_bytes_received <= 4'b0;
      request_received <= 1'b0;
      write_request <= 1'b0;
      provide_response <= 1'b0;
    end
    else begin

      if (data_out_valid) begin

        num_bytes_received <= num_bytes_received + 1'b1;

        if (num_bytes_received == NUM_BYTES_TRANSACTION-1) begin
          num_bytes_received <= 4'b0;
        end
      end


      if (data_out_valid && (num_bytes_received == 0)) begin
        write_request <= data_out[7];
      end


      request_received <= 1'b0;
      if (data_out_valid) begin
        if (write_request && (num_bytes_received == NUM_BYTES_WRITE_REQUEST_PAYLOAD-1)) begin
          request_received <= 1'b1;
          provide_response <= 1'b1;
        end else if (~write_request && (num_bytes_received == NUM_BYTES_READ_REQUEST_PAYLOAD-1)) begin
          request_received <= 1'b1;
          provide_response <= 1'b1;
        end
      end


      if (num_bytes_received == NUM_BYTES_TRANSACTION-1) begin
        provide_response <= 1'b0;
      end


      if (data_out_valid) begin
        request_reg <= {request_reg[NUM_BYTES_WRITE_REQUEST_PAYLOAD*8-8-1:0], data_out};
      end
    end
  end


  localparam SPI_TRANSFER_ADDRESS_WIDTH = 15;
  assign m_ctrlport_req_wr   = request_received && write_request;
  assign m_ctrlport_req_rd   = request_received && ~write_request;
  assign m_ctrlport_req_data = request_reg[CTRLPORT_DATA_W-1:0];
  assign m_ctrlport_req_addr = (write_request) ?
                               {5'b0, request_reg[CTRLPORT_DATA_W+:SPI_TRANSFER_ADDRESS_WIDTH]} :
                               {5'b0, request_reg[0+:SPI_TRANSFER_ADDRESS_WIDTH]};





  reg  [MAX_BYTES_RESPONSE_PAYLOAD*8-1:0] response_reg;
  reg  ready_for_response;
  wire write_response_byte;

  always @(posedge ctrlport_clk) begin
    if (ctrlport_rst || spi_slave_reset) begin
      response_reg <= {8*MAX_BYTES_RESPONSE_PAYLOAD {1'b0}};
      ready_for_response <= 1'b0;
    end
    else begin

      if (request_received) begin
        ready_for_response <= 1'b1;
        if (write_request) begin

          response_reg <= {5'b0, 1'b1, CTRL_STS_CMDERR, {CTRLPORT_DATA_W{1'b0}}};
        end else begin

          response_reg <= {{CTRLPORT_DATA_W{1'b0}}, 5'b0, 1'b1, CTRL_STS_CMDERR};
        end


      end else if (m_ctrlport_resp_ack && ready_for_response) begin
        if (write_request) begin
          response_reg <= {5'b0, m_ctrlport_resp_ack, m_ctrlport_resp_status, {CTRLPORT_DATA_W{1'b0}}};
        end else begin
          response_reg <= {m_ctrlport_resp_data, 5'b0, m_ctrlport_resp_ack, m_ctrlport_resp_status};
        end
      end


      if (write_response_byte) begin
        response_reg <= {response_reg[0+:(MAX_BYTES_RESPONSE_PAYLOAD-1)*8], 8'b0};
        ready_for_response <= 1'b0;
      end
    end
  end


  assign write_response_byte = data_in_required && provide_response;


  assign data_in       = response_reg[(MAX_BYTES_RESPONSE_PAYLOAD-1)*8+:8];
  assign data_in_valid = write_response_byte;

endmodule