aboutsummaryrefslogtreecommitdiff
path: root/src/test/scala/firrtlTests/ReplaceTruncatingArithmeticSpec.scala
blob: 34693d66bf479a743c706062d7d4c1a58977d4ad (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
// SPDX-License-Identifier: Apache-2.0

package firrtlTests

import firrtl._
import firrtl.testutils._
import firrtl.testutils.FirrtlCheckers._

class ReplaceTruncatingArithmeticSpec extends FirrtlFlatSpec {
  def compile(input: String): CircuitState =
    (new VerilogCompiler).compileAndEmit(CircuitState(parse(input), ChirrtlForm), List.empty)
  def compileBody(body: String) = {
    val str = """
                |circuit Test :
                |  module Test :
                |""".stripMargin + body.split("\n").mkString("    ", "\n    ", "")
    compile(str)
  }

  "Truncting addition" should "be inferred and emitted in Verilog" in {
    val result = compileBody(s"""
                                |input x : UInt<8>
                                |input y : UInt<8>
                                |output z : UInt<8>
                                |z <= tail(add(x, y), 1)""".stripMargin)
    result should containLine(s"assign z = x + y;")
  }
  it should "be inferred and emitted in Verilog even with an intermediate node" in {
    val result = compileBody(s"""
                                |input x : UInt<8>
                                |input y : UInt<8>
                                |output z : UInt<8>
                                |node n = add(x, y)
                                |z <= tail(n, 1)""".stripMargin)
    result should containLine(s"assign z = x + y;")
  }
  "Truncting subtraction" should "be inferred and emitted in Verilog" in {
    val result = compileBody(s"""
                                |input x : UInt<8>
                                |input y : UInt<8>
                                |output z : UInt<8>
                                |z <= tail(sub(x, y), 1)""".stripMargin)
    result should containLine(s"assign z = x - y;")
  }
  "Tailing more than 1" should "not result in a truncating operator" in {
    val result = compileBody(s"""
                                |input x : UInt<8>
                                |input y : UInt<8>
                                |output z : UInt<7>
                                |node n = sub(x, y)
                                |z <= tail(n, 2)""".stripMargin)
    result should containLine(s"wire [8:0] n = x - y;")
    result should containLine(s"assign z = n[6:0];")
  }

}