Skip to content

Commit

Permalink
Adjust source info where needed
Browse files Browse the repository at this point in the history
  • Loading branch information
konda-x1 authored and milovanovic committed Jul 17, 2023
1 parent ece2788 commit c2c9fb0
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 26 deletions.
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ Currently, this library works with [Chisel v3.5.6](https://github.com/chipsallia
## Usage

Here is an example module using this library's FixedPoint type:

```scala
import chisel3.{fromIntToBinaryPoint => _, fromDoubleToLiteral => _, _}
import fixedpoint._
Expand All @@ -32,7 +33,9 @@ object ExampleApp extends App {
println(stage.ChiselStage.emitSystemVerilog(new Example))
}
```

This outputs the following SystemVerilog code:

```systemverilog
module Example(
input clock,
Expand All @@ -41,9 +44,9 @@ module Example(
output [7:0] out1,
output [10:0] out2
);
wire [8:0] _out1_T = {$signed(in), 1'h0}; // @[FixedPoint.scala 301:34]
wire [8:0] _out1_T = {$signed(in), 1'h0}; // @[Example.scala 9:8]
assign out1 = _out1_T[7:0]; // @[Example.scala 9:8]
assign out2 = 11'sh324; // @[FixedPoint.scala 301:34]
assign out2 = 11'sh324; // @[Example.scala 10:22]
endmodule
```

Expand Down
80 changes: 56 additions & 24 deletions src/main/scala/fixedpoint/FixedPoint.scala
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,12 @@ import chisel3.{fromDoubleToLiteral => _, fromIntToBinaryPoint => _, _}
import chisel3.experimental.BundleLiterals.AddBundleLiteralConstructor
import chisel3.experimental.{AutoCloneType, OpaqueType}
import chisel3.internal.firrtl.Width
import chisel3.internal.sourceinfo.SourceInfo
import chisel3.internal.sourceinfo.{SourceInfo, SourceInfoTransform, SourceInfoWhiteboxTransform}
import chisel3.stage.ChiselStage
import fixedpoint.shadow.{Mux, Mux1H, MuxCase, MuxLookup, PriorityMux}

import scala.collection.immutable.SeqMap
import scala.language.experimental.macros

object FixedPoint extends NumObject {

Expand Down Expand Up @@ -84,7 +85,7 @@ object FixedPoint extends NumObject {

/** Create a FixedPoint bundle with its data port connected to an SInt literal
*/
private[fixedpoint] def fromData(width: Width, binaryPoint: BinaryPoint, data: SInt): FixedPoint = {
private[fixedpoint] def fromData(width: Width, binaryPoint: BinaryPoint, data: SInt)(implicit sourceInfo: SourceInfo, compileOptions: CompileOptions): FixedPoint = {
val _new = Wire(FixedPoint(width, binaryPoint))
_new.data := data
_new
Expand All @@ -97,7 +98,7 @@ object FixedPoint extends NumObject {

/** Align all FixedPoints in a (possibly heterogeneous) sequence by width and binary point
*/
private[fixedpoint] def dataAligned[T <: Data](in: Iterable[T]): Seq[T] = {
private[fixedpoint] def dataAligned[T <: Data](in: Iterable[T])(implicit sourceInfo: SourceInfo, compileOptions: CompileOptions): Seq[T] = {

val bps = in.collect {
case el: FixedPoint =>
Expand Down Expand Up @@ -129,7 +130,7 @@ object FixedPoint extends NumObject {
out.toSeq
}

private[fixedpoint] def dataAligned(in: FixedPoint*): Seq[FixedPoint] = dataAligned(in)
private[fixedpoint] def dataAligned(in: FixedPoint*)(implicit sourceInfo: SourceInfo, compileOptions: CompileOptions): Seq[FixedPoint] = dataAligned(in)

class ImplicitsCls private[fixedpoint] {

Expand Down Expand Up @@ -172,7 +173,7 @@ sealed class FixedPoint private[fixedpoint] (width: Width, private var _inferred
require(_inferredBinaryPoint.isInstanceOf[KnownBinaryPoint], message)
}

private def additiveOp(that: FixedPoint, f: (SInt, SInt) => SInt, width: Width = Width()): FixedPoint = {
private def additiveOp(that: FixedPoint, f: (SInt, SInt) => SInt, width: Width = Width())(implicit sourceInfo: SourceInfo, compileOptions: CompileOptions): FixedPoint = {
val Seq(_this, _that) = FixedPoint.dataAligned(this, that).map(WireDefault(_))
FixedPoint.fromData(width, _inferredBinaryPoint.max(that._inferredBinaryPoint), f(_this.data, _that.data))
}
Expand Down Expand Up @@ -210,6 +211,18 @@ sealed class FixedPoint private[fixedpoint] (width: Width, private var _inferred
override def do_-(that: FixedPoint)(implicit sourceInfo: SourceInfo, compileOptions: CompileOptions): FixedPoint =
additiveOp(that, _ - _)

def do_+%(that: FixedPoint)(implicit sourceInfo: SourceInfo, compileOptions: CompileOptions): FixedPoint = additiveOp(that, _ +% _)

def do_+&(that: FixedPoint)(implicit sourceInfo: SourceInfo, compileOptions: CompileOptions): FixedPoint = additiveOp(that, _ +& _)

def do_-%(that: FixedPoint)(implicit sourceInfo: SourceInfo, compileOptions: CompileOptions): FixedPoint = additiveOp(that, _ -% _)

def do_-&(that: FixedPoint)(implicit sourceInfo: SourceInfo, compileOptions: CompileOptions): FixedPoint = additiveOp(that, _ -& _)

def do_unary_-(implicit sourceInfo: SourceInfo, compileOptions: CompileOptions): FixedPoint = FixedPoint.fromData(width, _inferredBinaryPoint, -data)

def do_unary_-%(implicit sourceInfo: SourceInfo, compileOptions: CompileOptions): FixedPoint = FixedPoint.fromData(width, _inferredBinaryPoint, data.unary_-%)

override def do_*(that: FixedPoint)(implicit sourceInfo: SourceInfo, compileOptions: CompileOptions): FixedPoint =
FixedPoint.fromData(Width(), _inferredBinaryPoint + that._inferredBinaryPoint, data * that.data)

Expand All @@ -234,33 +247,50 @@ sealed class FixedPoint private[fixedpoint] (width: Width, private var _inferred
override def do_abs(implicit sourceInfo: SourceInfo, compileOptions: CompileOptions): FixedPoint =
FixedPoint.fromData(Width(), _inferredBinaryPoint, data.abs)

def +%(that: FixedPoint): FixedPoint = additiveOp(that, _ +% _)
def +&(that: FixedPoint): FixedPoint = additiveOp(that, _ +& _)
def -%(that: FixedPoint): FixedPoint = additiveOp(that, _ -% _)
def -&(that: FixedPoint): FixedPoint = additiveOp(that, _ -& _)
def unary_- : FixedPoint = FixedPoint.fromData(width, _inferredBinaryPoint, -data)
def unary_-% : FixedPoint = FixedPoint.fromData(width, _inferredBinaryPoint, data.unary_-%)
def ===(that: FixedPoint): Bool = comparativeOp(that, _ === _)
def =/=(that: FixedPoint): Bool = comparativeOp(that, _ =/= _)
def !=(that: FixedPoint): Bool = comparativeOp(that, _ =/= _)

def >>(that: Int): FixedPoint =
def do_===(that: FixedPoint)(implicit sourceInfo: SourceInfo, compileOptions: CompileOptions): Bool = comparativeOp(that, _ === _)

def do_=/=(that: FixedPoint)(implicit sourceInfo: SourceInfo, compileOptions: CompileOptions): Bool = comparativeOp(that, _ =/= _)

def do_!=(that: FixedPoint)(implicit sourceInfo: SourceInfo, compileOptions: CompileOptions): Bool = comparativeOp(that, _ =/= _)

def do_>>(that: Int)(implicit sourceInfo: SourceInfo, compileOptions: CompileOptions): FixedPoint =
FixedPoint.fromData(Width(), _inferredBinaryPoint, (data >> that).asSInt)
def >>(that: BigInt): FixedPoint =

def do_>>(that: BigInt)(implicit sourceInfo: SourceInfo, compileOptions: CompileOptions): FixedPoint =
FixedPoint.fromData(Width(), _inferredBinaryPoint, (data >> that).asSInt)
def >>(that: UInt): FixedPoint =

def do_>>(that: UInt)(implicit sourceInfo: SourceInfo, compileOptions: CompileOptions): FixedPoint =
FixedPoint.fromData(Width(), _inferredBinaryPoint, (data >> that).asSInt)
def <<(that: Int): FixedPoint =

def do_<<(that: Int)(implicit sourceInfo: SourceInfo, compileOptions: CompileOptions): FixedPoint =
FixedPoint.fromData(Width(), _inferredBinaryPoint, (data << that).asSInt)
def <<(that: BigInt): FixedPoint =

def do_<<(that: BigInt)(implicit sourceInfo: SourceInfo, compileOptions: CompileOptions): FixedPoint =
FixedPoint.fromData(Width(), _inferredBinaryPoint, (data << that).asSInt)
def <<(that: UInt): FixedPoint =

def do_<<(that: UInt)(implicit sourceInfo: SourceInfo, compileOptions: CompileOptions): FixedPoint =
FixedPoint.fromData(Width(), _inferredBinaryPoint, (data << that).asSInt)

def +%(that: FixedPoint): FixedPoint = macro SourceInfoTransform.thatArg
def +&(that: FixedPoint): FixedPoint = macro SourceInfoTransform.thatArg
def -%(that: FixedPoint): FixedPoint = macro SourceInfoTransform.thatArg
def -&(that: FixedPoint): FixedPoint = macro SourceInfoTransform.thatArg
def unary_- : FixedPoint = macro SourceInfoTransform.noArg
def unary_-% : FixedPoint = macro SourceInfoTransform.noArg
def ===(that: FixedPoint): Bool = macro SourceInfoTransform.thatArg
def =/=(that: FixedPoint): Bool = macro SourceInfoTransform.thatArg
def !=(that: FixedPoint): Bool = macro SourceInfoTransform.thatArg
def >>(that: Int): FixedPoint = macro SourceInfoWhiteboxTransform.thatArg
def >>(that: BigInt): FixedPoint = macro SourceInfoWhiteboxTransform.thatArg
def >>(that: UInt): FixedPoint = macro SourceInfoWhiteboxTransform.thatArg
def <<(that: Int): FixedPoint = macro SourceInfoWhiteboxTransform.thatArg
def <<(that: BigInt): FixedPoint = macro SourceInfoWhiteboxTransform.thatArg
def <<(that: UInt): FixedPoint = macro SourceInfoWhiteboxTransform.thatArg

override def connect(that: Data)(implicit sourceInfo: SourceInfo, connectCompileOptions: CompileOptions): Unit =
connectOp(that, _ := _)(sourceInfo, connectCompileOptions)
connectOp(that, _ := _)
override def bulkConnect(that: Data)(implicit sourceInfo: SourceInfo, connectCompileOptions: CompileOptions): Unit =
connectOp(that, _ <> _)(sourceInfo, connectCompileOptions)
connectOp(that, _ <> _)

override def connectFromBits(
that: Bits
Expand Down Expand Up @@ -291,7 +321,7 @@ sealed class FixedPoint private[fixedpoint] (width: Width, private var _inferred
}
}

def setBinaryPoint(that: Int): FixedPoint = {
def do_setBinaryPoint(that: Int)(implicit sourceInfo: SourceInfo, compileOptions: CompileOptions): FixedPoint = {
_inferredBinaryPoint match {
case KnownBinaryPoint(current) =>
val diff = that - current
Expand All @@ -308,6 +338,8 @@ sealed class FixedPoint private[fixedpoint] (width: Width, private var _inferred
}
}

def setBinaryPoint(that: Int): FixedPoint = macro SourceInfoTransform.thatArg

def widthKnown: Boolean = data.widthKnown

override def typeEquivalent(that: Data): Boolean = {
Expand Down

0 comments on commit c2c9fb0

Please sign in to comment.