Skip to content

Commit 14c778d

Browse files
committed
1 parent c045005 commit 14c778d

File tree

1 file changed

+80
-0
lines changed

1 file changed

+80
-0
lines changed
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
package hdlbits.circuits
2+
3+
import chisel3._
4+
import chisel3.util._
5+
6+
// _root_ disambiguates from package chisel3.util.circt if user imports chisel3.util._
7+
import _root_.circt.stage.ChiselStage
8+
9+
// Generate Verilog sources and save it in file Exams2013Q2bfsm.sv
10+
object HdlBitsExams2013Q2bfsm extends App {
11+
ChiselStage.emitSystemVerilogFile(
12+
new HdlBitsExams2013Q2bfsm,
13+
firtoolOpts = Array(
14+
"-disable-all-randomization",
15+
"-strip-debug-info"
16+
),
17+
args = Array("--target-dir", "gen/hdlbits/circuits")
18+
)
19+
}
20+
21+
// https://hdlbits.01xz.net/wiki/Exams/2013_q2bfsm
22+
class HdlBitsExams2013Q2bfsm extends RawModule {
23+
val clk = IO(Input(Clock()))
24+
val resetn = IO(Input(Bool())) // active-low synchronous reset
25+
val x = IO(Input(Bool()))
26+
val y = IO(Input(Bool()))
27+
val f = IO(Output(Bool()))
28+
val g = IO(Output(Bool()))
29+
30+
// Define state parameters
31+
val a :: b :: c :: d :: e :: f_ :: g_ :: h :: i :: Nil = Enum(9)
32+
33+
// State registers with clock and reset
34+
val state = withClockAndReset(clk, !resetn) { RegInit(a) }
35+
36+
// State transition logic
37+
switch(state) {
38+
is(a) {
39+
state := b
40+
}
41+
is(b) {
42+
state := c
43+
}
44+
is(c) {
45+
when(x) {
46+
state := d
47+
}
48+
}
49+
is(d) {
50+
when(!x) {
51+
state := e
52+
}
53+
}
54+
is(e) {
55+
when(!x) {
56+
state := c
57+
} otherwise (
58+
state := f_
59+
)
60+
}
61+
is(f_) {
62+
when(!y) {
63+
state := g_
64+
} otherwise {
65+
state := h
66+
}
67+
}
68+
is(g_) {
69+
when(!y) {
70+
state := i
71+
} otherwise {
72+
state := h
73+
}
74+
}
75+
}
76+
77+
// Output logic
78+
f := state === b
79+
g := state === f_ || state === g_ || state === h
80+
}

0 commit comments

Comments
 (0)