Skip to content

Commit 204af86

Browse files
author
Johns575
committed
1 parent 17ed844 commit 204af86

File tree

2 files changed

+94
-0
lines changed

2 files changed

+94
-0
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,6 @@ Please note that these solutions are not necessarily optimal, but rather intende
1111
* In Chisel, individual bit assignment doesn’t work with `UInt` and `Bits`. See <src/main/scala/hdlbits/circuits/Fsm3onehot.scala>
1212
* In Chisel, cannot set the name of the generated top module name. See <src/main/scala/hdlbits/verilog_language/7458.scala>
1313
* In Chisel, the generated Verilog file is optimized, making it difficult to inspect directly when you're still learning and the generated DUT isn't correct. In contrast, with SpinalHDL, you can more easily review the generated Verilog to understand its equivalent and identify issues.
14+
* In Chisel, it is more verbose to do bit assignment. For comparison, see `\src\main\scala\hdlbits\circuits\FsmSerialdata.scala:34`.
15+
* In Chisel, state name does not conflict with module name. See `src\main\scala\hdlbits\circuits\FsmSerialdp.scala:39` for comparison.
16+
* In Chisel, we can control submodule reset signal less verbosely than SpinalHDL. See `src\main\scala\hdlbits\circuits\FsmSerialdp.scala:52` for example. And SpinalHDL is not.
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
package hdlbits.circuits
2+
3+
import chisel3._
4+
import chisel3.util._
5+
6+
// _root_ disambiguated from package chisel3.util.circt if user imports chisel3.util._
7+
import _root_.circt.stage.ChiselStage
8+
9+
object VerilogHdlBitsFsmSerialdp extends App {
10+
ChiselStage.emitSystemVerilogFile(
11+
new HdlBitsFsmSerialdp,
12+
firtoolOpts = Array(
13+
"-disable-all-randomization",
14+
"-strip-debug-info"
15+
),
16+
args = Array("--target-dir", "gen/hdlbits/circuits")
17+
)
18+
}
19+
20+
// https://hdlbits.01xz.net/wiki/Fsm_serialdp
21+
class HdlBitsFsmSerialdp extends RawModule {
22+
val clk = IO(Input(Clock()))
23+
val reset = IO(Input(Bool())) // Synchronous reset
24+
val in = IO(Input(Bool()))
25+
val out_byte = IO(Output(UInt(8.W)))
26+
val done = IO(Output(Bool()))
27+
28+
// Define state parameters
29+
val idle :: start :: parity :: stop :: Nil = Enum(4)
30+
31+
// State register with clock and reset
32+
val state = withClockAndReset(clk, reset) { RegInit(idle) }
33+
val counter = withClockAndReset(clk, reset) { RegInit(0.U(3.W)) }
34+
val outByte = withClockAndReset(clk, reset) {
35+
RegInit(VecInit(Seq.fill(8)(false.B)))
36+
}
37+
val parityState = Module(new parity)
38+
parityState.clk := clk
39+
parityState.reset := reset | (state === idle && !in)
40+
parityState.in := in
41+
42+
// State transition logic
43+
switch(state) {
44+
is(idle) {
45+
when(!in) {
46+
state := start
47+
}
48+
counter := 0.U
49+
}
50+
is(start) {
51+
when(counter === 7.U) {
52+
state := parity // NOTE: `parity` state not conflict with `parity` module, which is different from SpinalHDL
53+
} otherwise {
54+
counter := counter + 1.U
55+
}
56+
outByte(counter) := in
57+
}
58+
is(parity) {
59+
state := stop
60+
}
61+
is(stop) {
62+
when(in) {
63+
state := idle
64+
} otherwise {
65+
counter := 0.U
66+
}
67+
}
68+
}
69+
70+
// State update
71+
// The last state `stop` needs `in` to be high, which have to included in
72+
// the parity check, so odd need to be initialized to true
73+
done := state === idle && counter === 7.U && ~parityState.odd
74+
out_byte := outByte.asUInt
75+
}
76+
77+
// FIX: I would like to use `Parity` as class name and `parity as generated module name. But How?
78+
class parity extends RawModule {
79+
val clk = IO(Input(Clock()))
80+
val reset = IO(Input(Bool())) // Synchronous reset
81+
val in = IO(Input(Bool()))
82+
val odd = IO(Output(Bool()))
83+
84+
val oddState = withClockAndReset(clk, reset) { RegInit(false.B) }
85+
86+
when(in) {
87+
oddState := ~oddState
88+
}
89+
90+
odd := oddState
91+
}

0 commit comments

Comments
 (0)