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