-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathbasic.scala
136 lines (105 loc) · 3.06 KB
/
basic.scala
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
/**
* This code supplements instructions.org
* Once you've gone through the instructions you can do
* whatever you want with it.
*/
package Examples
import Ex0._
import chisel3._
import chisel3.iotesters.PeekPokeTester
import org.scalatest.{Matchers, FlatSpec}
import TestUtils._
class MyIncrementTest extends FlatSpec with Matchers {
class MyIncrement(val incrementBy: Int) extends Module {
val io = IO(
new Bundle {
val dataIn = Input(UInt(32.W))
val dataOut = Output(UInt(32.W))
}
)
io.dataOut := io.dataIn + incrementBy.U
}
class TheTestRunner(c: MyIncrement) extends PeekPokeTester(c) {
for(ii <- 0 until 5){
poke(c.io.dataIn, ii)
val o = peek(c.io.dataOut)
println(s"At cycle $ii the output of myIncrement was $o")
expect(c.io.dataOut, ii+c.incrementBy)
}
}
behavior of "my increment"
it should "increment its input by 3" in {
chisel3.iotesters.Driver(() => new MyIncrement(3)) { c =>
new TheTestRunner(c)
} should be(true)
}
}
class MyIncrementManyTest extends FlatSpec with Matchers {
class MyIncrement(val incrementBy: Int) extends Module {
val io = IO(
new Bundle {
val dataIn = Input(UInt(32.W))
val dataOut = Output(UInt(32.W))
}
)
io.dataOut := io.dataIn + incrementBy.U
}
class MyIncrementN(val incrementBy: Int, val numIncrementors: Int) extends Module {
val io = IO(
new Bundle {
val dataIn = Input(UInt(32.W))
val dataOut = Output(UInt(32.W))
}
)
val incrementors = Array.fill(numIncrementors){ Module(new MyIncrement(incrementBy)) }
for(ii <- 1 until numIncrementors){
incrementors(ii).io.dataIn := incrementors(ii - 1).io.dataOut
}
incrementors(0).io.dataIn := io.dataIn
io.dataOut := incrementors.last.io.dataOut
}
class TheTestRunner(c: MyIncrementN) extends PeekPokeTester(c) {
for(ii <- 0 until 5){
poke(c.io.dataIn, ii)
val o = peek(c.io.dataOut)
println(s"At cycle $ii the output of myIncrement was $o")
expect(c.io.dataOut, ii+(c.incrementBy*c.numIncrementors))
}
}
behavior of "my incrementN"
it should "increment its input by 3*4" in {
chisel3.iotesters.Driver.execute(Array("--generate-vcd-output", "on", "--target-dir", ".", "--backend-name", "treadle"), () => new MyIncrementN(3,4)) { c =>
// chisel3.iotesters.Driver(() => new MyIncrementN(4, 3)) { c =>
new TheTestRunner(c)
} should be(true)
}
}
class ChiselConditional() extends Module {
val io = IO(
new Bundle {
val a = Input(UInt(32.W))
val b = Input(UInt(32.W))
val opSel = Input(Bool())
val out = Output(UInt(32.W))
}
)
when(io.opSel){
io.out := io.a + io.b
}.otherwise{
io.out := io.a - io.b
}
}
class ScalaConditional(opSel: Boolean) extends Module {
val io = IO(
new Bundle {
val a = Input(UInt(32.W))
val b = Input(UInt(32.W))
val out = Output(UInt(32.W))
}
)
if(opSel){
io.out := io.a + io.b
} else {
io.out := io.a - io.b
}
}