|
| 1 | +// SPDX-License-Identifier: Apache-2.0 |
| 2 | + |
| 3 | +package chisel3.util.circt.dpi |
| 4 | + |
| 5 | +import chisel3._ |
| 6 | + |
| 7 | +import chisel3.experimental.{IntParam, IntrinsicModule, Param, StringParam} |
| 8 | + |
| 9 | +private object GetDPIParams { |
| 10 | + def apply( |
| 11 | + functionName: String, |
| 12 | + isClocked: Boolean, |
| 13 | + inputNames: Option[Seq[String]], |
| 14 | + outputName: Option[String] = None |
| 15 | + ): Seq[(String, Param)] = { |
| 16 | + val inputNamesParam = |
| 17 | + inputNames.map(_.mkString(";")).map(x => Seq("inputNames" -> StringParam(x))).getOrElse(Seq()) |
| 18 | + val outputNameParam = outputName.map(x => Seq("outputName" -> StringParam(x))).getOrElse(Seq()) |
| 19 | + Seq[(String, Param)]( |
| 20 | + "functionName" -> functionName, |
| 21 | + "isClocked" -> (if (isClocked) 1 else 0) |
| 22 | + ) ++ inputNamesParam ++ outputNameParam |
| 23 | + } |
| 24 | +} |
| 25 | + |
| 26 | +object RawClockedNonVoidFunctionCall { |
| 27 | + |
| 28 | + /** Creates an intrinsic that calls non-void DPI function at its clock posedge. |
| 29 | + * The result values behave like registers and the DPI function is used as a state |
| 30 | + * transfer function of them. |
| 31 | + * |
| 32 | + * `enable` operand is used to conditionally call the DPI since DPI call could be quite |
| 33 | + * more expensive than native constructs. |
| 34 | + * |
| 35 | + * When an `enable` is false, it means the state transfer function is not called. Hence |
| 36 | + * their values will not be modified in that clock. |
| 37 | + * |
| 38 | + * Please refer https://github.com/llvm/circt/blob/main/docs/Dialects/FIRRTL/FIRRTLIntrinsics.md#dpi-intrinsic-abi for DPI function ABI. |
| 39 | + * @example {{{ |
| 40 | + * val a = RawClockedNonVoidFunctionCall("dpi_func_foo", UInt(1.W), clock, enable, b, c) |
| 41 | + * }}} |
| 42 | + */ |
| 43 | + def apply[T <: Data]( |
| 44 | + functionName: String, |
| 45 | + ret: => T, |
| 46 | + inputNames: Option[Seq[String]] = None, |
| 47 | + outputName: Option[String] = None |
| 48 | + )(clock: Clock, |
| 49 | + enable: Bool, |
| 50 | + data: Data* |
| 51 | + ): T = { |
| 52 | + IntrinsicExpr( |
| 53 | + "circt_dpi_call", |
| 54 | + ret, |
| 55 | + GetDPIParams(functionName, true, inputNames, outputName): _* |
| 56 | + )( |
| 57 | + (Seq(clock, enable) ++ data): _* |
| 58 | + ) |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +object RawUnclockedNonVoidFunctionCall { |
| 63 | + |
| 64 | + /** Creates an intrinsic that calls non-void DPI function for its input value changes. |
| 65 | + * The DPI call is considered as a combinational logic. |
| 66 | + * |
| 67 | + * `enable` operand is used to conditionally call the DPI since DPI call could be quite |
| 68 | + * more expensive than native constructs. |
| 69 | + * When `enable` is false, results of unclocked calls are undefined and evaluated into X. |
| 70 | + * |
| 71 | + * Please refer https://github.com/llvm/circt/blob/main/docs/Dialects/FIRRTL/FIRRTLIntrinsics.md#dpi-intrinsic-abi for DPI function ABI. |
| 72 | + * @example {{{ |
| 73 | + * val a = RawUnclockedNonVoidFunctionCall("dpi_func_foo", UInt(1.W), enable, b, c) |
| 74 | + * }}} |
| 75 | + */ |
| 76 | + def apply[T <: Data]( |
| 77 | + functionName: String, |
| 78 | + ret: => T, |
| 79 | + inputNames: Option[Seq[String]] = None, |
| 80 | + outputName: Option[String] = None |
| 81 | + )(enable: Bool, |
| 82 | + data: Data* |
| 83 | + ): T = { |
| 84 | + IntrinsicExpr( |
| 85 | + "circt_dpi_call", |
| 86 | + ret, |
| 87 | + GetDPIParams(functionName, false, inputNames, outputName): _* |
| 88 | + )( |
| 89 | + (Seq(enable) ++ data): _* |
| 90 | + ) |
| 91 | + } |
| 92 | +} |
| 93 | + |
| 94 | +object RawClockedVoidFunctionCall { |
| 95 | + |
| 96 | + /** Creates an intrinsic that calls void DPI function at its clock posedge. |
| 97 | + * |
| 98 | + * Please refer https://github.com/llvm/circt/blob/main/docs/Dialects/FIRRTL/FIRRTLIntrinsics.md#dpi-intrinsic-abi for DPI function ABI. |
| 99 | + * @example {{{ |
| 100 | + * RawClockedVoidFunctionCall("dpi_func_foo", UInt(1.W), clock, enable, b, c) |
| 101 | + * }}} |
| 102 | + */ |
| 103 | + def apply( |
| 104 | + functionName: String, |
| 105 | + inputNames: Option[Seq[String]] = None |
| 106 | + )(clock: Clock, |
| 107 | + enable: Bool, |
| 108 | + data: Data* |
| 109 | + ): Unit = { |
| 110 | + Intrinsic("circt_dpi_call", GetDPIParams(functionName, true, inputNames): _*)( |
| 111 | + (Seq(clock, enable) ++ data): _* |
| 112 | + ) |
| 113 | + } |
| 114 | +} |
| 115 | + |
| 116 | +// A common trait for DPI functions. |
| 117 | +trait DPIFunctionImport { |
| 118 | + def functionName: String |
| 119 | + def inputNames: Option[Seq[String]] = None |
| 120 | +} |
| 121 | + |
| 122 | +// Base trait for a non-void function that returns `T`. |
| 123 | +trait DPINonVoidFunctionImport[T <: Data] extends DPIFunctionImport { |
| 124 | + def ret: T |
| 125 | + def clocked: Boolean |
| 126 | + def outputName: Option[String] = None |
| 127 | + final def callWithEnable(enable: Bool, data: Data*): T = |
| 128 | + if (clocked) { |
| 129 | + RawClockedNonVoidFunctionCall(functionName, ret, inputNames, outputName)(Module.clock, enable, data: _*) |
| 130 | + } else { |
| 131 | + RawUnclockedNonVoidFunctionCall(functionName, ret, inputNames, outputName)(enable, data: _*) |
| 132 | + } |
| 133 | + final def call(data: Data*): T = callWithEnable(true.B, data: _*) |
| 134 | +} |
| 135 | + |
| 136 | +// Base trait for a clocked void function. |
| 137 | +trait DPIClockedVoidFunctionImport extends DPIFunctionImport { |
| 138 | + final def callWithEnable(enable: Bool, data: Data*): Unit = |
| 139 | + RawClockedVoidFunctionCall(functionName, inputNames)(Module.clock, enable, data: _*) |
| 140 | + final def call(data: Data*): Unit = callWithEnable(true.B, data: _*) |
| 141 | +} |
0 commit comments