|
| 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