Skip to content

Commit 87a5d12

Browse files
committed
1 parent 824925a commit 87a5d12

File tree

1 file changed

+62
-0
lines changed

1 file changed

+62
-0
lines changed
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
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 FsmPs2.sv
10+
object HdlBitsFsmPs2 extends App {
11+
ChiselStage.emitSystemVerilogFile(
12+
new HdlBitsFsmPs2,
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/Fsm_ps2
22+
class HdlBitsFsmPs2 extends RawModule {
23+
val clk = IO(Input(Clock()))
24+
val reset = IO(Input(Bool())) // Synchronous reset
25+
val in = IO(Input(UInt(8.W)))
26+
val done = IO(Output(Bool()))
27+
28+
// Define state parameters
29+
val byte1 :: byte2 :: byte3 :: idle :: Nil = Enum(4)
30+
val nextState = WireInit(
31+
idle
32+
) // NOTE: This is a default value, missing which will cause a compile error
33+
34+
// State register with clock and reset
35+
val state = withClockAndReset(clk, reset) { RegInit(idle) }
36+
37+
// State transition logic
38+
switch(state) {
39+
is(idle) {
40+
when(in(3)) {
41+
nextState := byte1
42+
}
43+
}
44+
is(byte1) {
45+
nextState := byte2
46+
}
47+
is(byte2) {
48+
nextState := byte3
49+
}
50+
is(byte3) {
51+
when(in(3)) {
52+
nextState := byte1
53+
}
54+
}
55+
}
56+
57+
// State update
58+
state := nextState
59+
60+
// Output logic
61+
done := state === byte3
62+
}

0 commit comments

Comments
 (0)