vpi_session.h 3.39 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/*!
 *  Copyright (c) 2017 by Contributors
 * \file vpi_session.h
 * \brief IPC session call to verilog simulator via VPI.
 */
#ifndef TVM_CODEGEN_VERILOG_VPI_SESSION_H_
#define TVM_CODEGEN_VERILOG_VPI_SESSION_H_

#include <tvm/base.h>
#include <vector>
#include <string>
#include "../../common/pipe.h"
#include "../../../verilog/tvm_vpi.h"

namespace tvm {
namespace codegen {
17

18 19 20 21
// node containers
class VPISessionNode;
class VPIHandleNode;
class VPIHandle;
22 23 24
class VPISessionEntry;

using runtime::PackedFunc;
25 26 27 28 29

/*! \brief Environment */
class VPISession : public NodeRef {
 public:
  VPISession() {}
30
  explicit VPISession(NodePtr<Node> n) : NodeRef(n) {}
31 32 33 34 35 36
  /*!
   * \brief Get handle by name.
   * \param name The name of the handle.
   */
  VPIHandle operator[](const std::string& name) const;
  /*!
37 38 39 40 41 42
   * \brief Get handle by name.
   * \param name The name of the handle.
   * \param allow_undefined whether allow undefined
   */
  VPIHandle GetByName(const std::string& name, bool allow_undefined) const;
  /*!
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
   * \brief Yield control back to the simulator
   *  Block until next cycle.
   */
  void yield();
  /*!
   * \brief Shutdown the session.
   */
  void shutdown();
  /*!
   * \brief Create new session by giving a read and write pipe to VPI process.
   * \param h_pipe_read a read pipe from VPI process.
   * \param h_pipe_write a write pipe from VPI process.
   */
  static VPISession make(int h_pipe_read, int h_pipe_write);
  // Internal methods.
  using ContainerType = VPISessionNode;
  inline VPISessionNode* get() const;
};

/*! \brief VPI Handle */
class VPIHandle : public NodeRef {
 public:
  VPIHandle() {}
66
  explicit VPIHandle(NodePtr<Node> n) : NodeRef(n) {}
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
  /*!
   * \brief Get handle by name.
   * \param name The name of the handle.
   */
  VPIHandle operator[](const std::string& name) const;
  /*! \return number of bits */
  int size() const;
  /*!
   * \brief Set int value to the handle.
   * \param value The value to set.
   */
  void put_int(int value);
  /*!
   * \brief Get int value from handle.
   * \return The result int value.
   */
  int get_int() const;
  /*! \return Name of the handle. */
  std::string name() const;
  /*!
   * \brief Put byte vector into the handle.
   * \param vec The vector to be put.
   * \return The result int value.
   */
  void put_vec(const std::vector<vpi::VPIVecVal>& vec) const;
  /*!
   * \brief Get byte vector from handle.
   * \param vec The result data container.
   */
  void get_vec(std::vector<vpi::VPIVecVal>* vec) const;
  // Internal methods
  using ContainerType = VPIHandleNode;
  inline VPIHandleNode* get() const;
};
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131

/*! \brief Container for session. */
class VPISessionNode : public Node {
 public:
  // internal session.
  std::shared_ptr<VPISessionEntry> sess;
  // callbacks at pos edge end.
  std::vector<PackedFunc> posedge_end_callbacks;

  // visit all attributes
  void VisitAttrs(AttrVisitor* v) final {
  }
  static constexpr const char* _type_key = "VPISession";
  TVM_DECLARE_NODE_TYPE_INFO(VPISessionNode, Node);
};

/*! \brief Container for handle */
class VPIHandleNode : public Node {
 public:
  // internal session.
  std::shared_ptr<VPISessionEntry> sess;
  // Internal handle
  vpi::VPIRawHandle handle;

  void VisitAttrs(AttrVisitor* v) final {
  }

  static constexpr const char* _type_key = "VPIHandle";
  TVM_DECLARE_NODE_TYPE_INFO(VPIHandleNode, Node);
};

132 133 134
}  // namespace codegen
}  // namespace tvm
#endif  // TVM_CODEGEN_VERILOG_VPI_SESSION_H_