-
Notifications
You must be signed in to change notification settings - Fork 146
/
Copy pathSimpleFsm.scala
90 lines (82 loc) · 1.62 KB
/
SimpleFsm.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
//- start simple_fsm
import chisel3._
import chisel3.util._
class SimpleFsm extends Module {
val io = IO(new Bundle{
val badEvent = Input(Bool())
val clear = Input(Bool())
val ringBell = Output(Bool())
})
// The three states
object State extends ChiselEnum {
val green, orange, red = Value
}
import State._
// The state register
val stateReg = RegInit(green)
// Next state logic
switch (stateReg) {
is (green) {
when(io.badEvent) {
stateReg := orange
}
}
is (orange) {
when(io.badEvent) {
stateReg := red
} .elsewhen(io.clear) {
stateReg := green
}
}
is (red) {
when (io.clear) {
stateReg := green
}
}
}
// Output logic
io.ringBell := stateReg === red
}
//- end
class SimpleFsmCopy extends Module {
//- start simple_fsm_io
val io = IO(new Bundle{
val badEvent = Input(Bool())
val clear = Input(Bool())
val ringBell = Output(Bool())
})
//- end
//- start simple_fsm_states
object State extends ChiselEnum {
val green, orange, red = Value
}
import State._
//- end
//- start simple_fsm_register
val stateReg = RegInit(green)
//- end
//- start simple_fsm_next
switch (stateReg) {
is (green) {
when(io.badEvent) {
stateReg := orange
}
}
is (orange) {
when(io.badEvent) {
stateReg := red
} .elsewhen(io.clear) {
stateReg := green
}
}
is (red) {
when (io.clear) {
stateReg := green
}
}
}
//- end
//- start simple_fsm_output
io.ringBell := stateReg === red
//- end
}