-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMux3.scala
63 lines (49 loc) · 1.38 KB
/
Mux3.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
package Core
import Chisel._
class Mux3(data_width: Int, regs_in: Int) extends Module {
val io = new Bundle {
val data_in = Vec.fill(regs_in){ UInt(INPUT, data_width) }
val enable_in = Bool(INPUT)
val data_out = UInt(OUTPUT, data_width)
val enable_out = Bool(OUTPUT)
val dbg_enable = UInt(OUTPUT)
}
val balancer = Reg(UInt(width=data_width))
io.data_out := balancer
val sleep :: s0 :: s1 :: s2 :: Nil = Enum(UInt(), 4)
val state = Reg(init=UInt(width=data_width))
when(io.enable_in){
state := s0
}
io.enable_out := Bool(false)
when(state === sleep){
}otherwise{
when(state === s2){
state := sleep
io.enable_out := Bool(true)
}.otherwise{
state := state + UInt(1)
}
}
switch (state) {
is (s0){ balancer := io.data_in(0) }
is (s1){ balancer := io.data_in(1) }
is (s2){ balancer := io.data_in(2) }
}
io.dbg_enable := state
}
class Mux3Test(c: Mux3, data_width: Int, regs_in: Int) extends Tester(c) {
println("Mux3 Test")
for(i <- 0 until 18){
step(1)
poke(c.io.data_in(i%3), i)
peek(c.io.data_out)
peek(c.io.dbg_enable)
if(i%9 == 0){
poke(c.io.enable_in, true)
}
else{
(poke(c.io.enable_in, false))
}
}
}