|
| 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 | +object VerilogHdlBitsFsmSerialdata extends App { |
| 10 | + ChiselStage.emitSystemVerilogFile( |
| 11 | + new HdlBitsFsmSerialdata, |
| 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_serialdata |
| 21 | +class HdlBitsFsmSerialdata 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 :: stop :: Nil = Enum(3) |
| 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 | + |
| 38 | + // State transition logic |
| 39 | + switch(state) { |
| 40 | + is(idle) { |
| 41 | + when(!in) { |
| 42 | + state := start |
| 43 | + } |
| 44 | + counter := 0.U |
| 45 | + } |
| 46 | + is(start) { |
| 47 | + when(counter === 7.U) { |
| 48 | + state := stop |
| 49 | + } otherwise { |
| 50 | + counter := counter + 1.U |
| 51 | + } |
| 52 | + outByte(counter) := in |
| 53 | + } |
| 54 | + is(stop) { |
| 55 | + when(in) { |
| 56 | + state := idle |
| 57 | + } otherwise { |
| 58 | + counter := 0.U |
| 59 | + } |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + // State update |
| 64 | + done := state === idle && counter === 7.U |
| 65 | + out_byte := outByte.asUInt |
| 66 | +} |
0 commit comments