summaryrefslogtreecommitdiff
path: root/src/test/scala/chiselTests/UIntOps.scala
blob: addd753ff5c45e3f992f44b6e13d2c8f3fae6608 (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
57
58
59
60
61
62
63
64
65
66
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
101
102
103
104
105
106
107
108
109
110
// See LICENSE for license details.

package chiselTests

import chisel3._
import org.scalatest._
import chisel3.testers.BasicTester

class UIntOps extends Module {
  val io = IO(new Bundle {
    val a = Input(UInt(16.W))
    val b = Input(UInt(16.W))
    val addout = Output(UInt(16.W))
    val subout = Output(UInt(16.W))
    val timesout = Output(UInt(16.W))
    val divout = Output(UInt(16.W))
    val modout = Output(UInt(16.W))
    val lshiftout = Output(UInt(16.W))
    val rshiftout = Output(UInt(16.W))
    val lessout = Output(Bool())
    val greatout = Output(Bool())
    val eqout = Output(Bool())
    val noteqout = Output(Bool())
    val lesseqout = Output(Bool())
    val greateqout = Output(Bool())
  })

  val a = io.a
  val b = io.b

  io.addout := a +% b
  io.subout := a -% b
  io.timesout := (a * b)(15, 0)
  io.divout := a / Mux(b === 0.U, 1.U, b)
  // io.modout := a % b
  // TODO:
  io.modout := 0.U
  io.lshiftout := (a << b(3, 0))(15, 0)
  io.rshiftout := a >> b
  io.lessout := a < b
  io.greatout := a > b
  io.eqout := a === b
  io.noteqout := (a != b)
  io.lesseqout := a <= b
  io.greateqout := a >= b
}

/*
class UIntOpsTester(c: UIntOps) extends Tester(c) {
  def uintExpect(d: Bits, x: BigInt) {
    val mask = (1 << 16) - 1
    println(" E = " + x + " X&M = " + (x & mask))
    expect(d, x & mask)
  }
  for (t <- 0 until 16) {
    val test_a = rnd.nextInt(1 << 16)
    val test_b = rnd.nextInt(1 << 16)
    println("A = " + test_a + " B = " + test_b)
    poke(c.io.a, test_a)
    poke(c.io.b, test_b)
    step(1)
    uintExpect(c.io.addout, test_a + test_b)
    uintExpect(c.io.subout, test_a - test_b)
    uintExpect(c.io.divout, if (test_b == 0) 0 else test_a / test_b)
    uintExpect(c.io.timesout, test_a * test_b)
    // uintExpect(c.io.modout, test_a % test_b)
    uintExpect(c.io.lshiftout, test_a << (test_b&15))
    uintExpect(c.io.rshiftout, test_a >> test_b)
    expect(c.io.lessout, int(test_a < test_b))
    expect(c.io.greatout, int(test_a > test_b))
    expect(c.io.eqout, int(test_a == test_b))
    expect(c.io.noteqout, int(test_a != test_b))
    expect(c.io.lessout, int(test_a <= test_b))
    expect(c.io.greateqout, int(test_a >= test_b))
  }
}
*/

class GoodBoolConversion extends Module {
  val io = IO(new Bundle {
    val u = Input(UInt(1.W))
    val b = Output(Bool())
  })
  io.b := io.u.toBool
}

class BadBoolConversion extends Module {
  val io = IO(new Bundle {
    val u = Input(UInt(5.W))
    val b = Output(Bool())
  })
  io.b := io.u.toBool
}

class UIntOpsSpec extends ChiselPropSpec with Matchers {
  property("Bools can be created from 1 bit UInts") {
    elaborate(new GoodBoolConversion)
  }

  property("Bools cannot be created from >1 bit UInts") {
    a [Exception] should be thrownBy { elaborate(new BadBoolConversion) }
  }

  property("UIntOps should elaborate") {
    elaborate { new UIntOps }
  }

  ignore("UIntOpsTester should return the correct result") { }
}