-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFullAdder.scala
40 lines (36 loc) · 928 Bytes
/
FullAdder.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
// See LICENSE.txt for license details.
package TutorialExamples
import Chisel._
class FullAdder extends Module {
val io = new Bundle {
val a = UInt(INPUT, 1)
val b = UInt(INPUT, 1)
val cin = UInt(INPUT, 1)
val sum = UInt(OUTPUT, 1)
val cout = UInt(OUTPUT, 1)
}
// Generate the sum
val a_xor_b = io.a ^ io.b
io.sum := a_xor_b ^ io.cin
// Generate the carry
val a_and_b = io.a & io.b
val b_and_cin = io.b & io.cin
val a_and_cin = io.a & io.cin
io.cout := a_and_b | b_and_cin | a_and_cin
}
class FullAdderTests(c: FullAdder) extends Tester(c) {
for (t <- 0 until 4) {
val a = rnd.nextInt(2)
val b = rnd.nextInt(2)
val cin = rnd.nextInt(2)
val res = a + b + cin
val sum = res & 1
val cout = (res >> 1) & 1
poke(c.io.a, a)
poke(c.io.b, b)
poke(c.io.cin, cin)
step(1)
expect(c.io.sum, sum)
expect(c.io.cout, cout)
}
}