File tree Expand file tree Collapse file tree 1 file changed +62
-0
lines changed
src/main/scala/hdlbits/circuits Expand file tree Collapse file tree 1 file changed +62
-0
lines changed Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments